Add a generic libinput_event_get_device() function
authorPeter Hutterer <peter.hutterer@who-t.net>
Fri, 17 Jan 2014 07:59:30 +0000 (17:59 +1000)
committerJonas Ådahl <jadahl@gmail.com>
Tue, 21 Jan 2014 21:50:58 +0000 (22:50 +0100)
After dropping seat evens, all events are now are associated with a device, so
provide a generic accessor function and drop the custom ones.

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
src/libinput.c
src/libinput.h
test/path.c
test/udev.c

index fd1261d..5c9126c 100644 (file)
@@ -50,7 +50,7 @@ struct libinput_source {
 
 struct libinput_event {
        enum libinput_event_type type;
-       struct libinput *libinput;
+       struct libinput_device *device;
        union libinput_event_target target;
 };
 
@@ -127,19 +127,11 @@ libinput_event_get_target(struct libinput_event *event)
 LIBINPUT_EXPORT struct libinput*
 libinput_event_get_context(struct libinput_event *event)
 {
-       return event->libinput;
-}
-
-LIBINPUT_EXPORT struct libinput_device *
-libinput_event_added_device_get_device(
-       struct libinput_event_added_device *event)
-{
-       return event->device;
+       return event->device->seat->libinput;
 }
 
-LIBINPUT_EXPORT struct libinput_device *
-libinput_event_removed_device_get_device(
-       struct libinput_event_removed_device *event)
+LIBINPUT_EXPORT struct libinput_device*
+libinput_event_get_device(struct libinput_event *event)
 {
        return event->device;
 }
@@ -584,21 +576,22 @@ libinput_dispatch(struct libinput *libinput)
 
 static void
 init_event_base(struct libinput_event *event,
-               struct libinput *libinput,
+               struct libinput_device *device,
                enum libinput_event_type type,
                union libinput_event_target target)
 {
        event->type = type;
-       event->libinput = libinput;
+       event->device = device;
        event->target = target;
 }
 
 static void
-post_base_event(struct libinput *libinput,
+post_base_event(struct libinput_device *device,
                enum libinput_event_type type,
                struct libinput_event *event)
 {
-       init_event_base(event, libinput, type,
+       struct libinput *libinput = device->seat->libinput;
+       init_event_base(event, device, type,
                        (union libinput_event_target) { .libinput = libinput });
        libinput_post_event(libinput, event);
 }
@@ -608,7 +601,7 @@ post_device_event(struct libinput_device *device,
                  enum libinput_event_type type,
                  struct libinput_event *event)
 {
-       init_event_base(event, device->seat->libinput, type,
+       init_event_base(event, device, type,
                        (union libinput_event_target) { .device = device });
        libinput_post_event(device->seat->libinput, event);
 }
@@ -626,7 +619,7 @@ notify_added_device(struct libinput_device *device)
                .device = device,
        };
 
-       post_base_event(device->seat->libinput,
+       post_base_event(device,
                        LIBINPUT_EVENT_ADDED_DEVICE,
                        &added_device_event->base);
 }
@@ -644,7 +637,7 @@ notify_removed_device(struct libinput_device *device)
                .device = device,
        };
 
-       post_base_event(device->seat->libinput,
+       post_base_event(device,
                        LIBINPUT_EVENT_REMOVED_DEVICE,
                        &removed_device_event->base);
 }
index 1fce32b..4b1ec6e 100644 (file)
@@ -248,20 +248,20 @@ struct libinput*
 libinput_event_get_context(struct libinput_event *event);
 
 /**
- * @defgroup event_added_device Added device event
- */
-
-struct libinput_device *
-libinput_event_added_device_get_device(
-       struct libinput_event_added_device *event);
-
-/**
- * @defgroup event_removed_device Removed device event
+ * @ingroup event
+ *
+ * Return the device associated with this event, if applicable. For device
+ * added/removed events this is the device added or removed. For all other
+ * device events, this is the device that generated the event.
+ *
+ * This device is not refcounted and its lifetime is that of the event. Use
+ * libinput_device_ref() before using the device outside of this scope.
+ *
+ * @return The device associated with this event
  */
 
-struct libinput_device *
-libinput_event_removed_device_get_device(
-       struct libinput_event_removed_device *event);
+struct libinput_device*
+libinput_event_get_device(struct libinput_event *event);
 
 /**
  * @defgroup event_keyboard_key Keyboard key event
index 87775a7..c5923f8 100644 (file)
@@ -134,7 +134,6 @@ START_TEST(path_added_seat)
        struct litest_device *dev = litest_current_device();
        struct libinput *li = dev->libinput;
        struct libinput_event *event;
-       struct libinput_event_added_device *device_event;
        struct libinput_device *device;
        struct libinput_seat *seat;
        const char *seat_name;
@@ -148,8 +147,7 @@ START_TEST(path_added_seat)
        type = libinput_event_get_type(event);
        ck_assert_int_eq(type, LIBINPUT_EVENT_ADDED_DEVICE);
 
-       device_event = (struct libinput_event_added_device*)event;
-       device = libinput_event_added_device_get_device(device_event);
+       device = libinput_event_get_device(event);
        seat = libinput_device_get_seat(device);
        ck_assert(seat != NULL);
 
@@ -165,7 +163,6 @@ START_TEST(path_added_device)
        struct litest_device *dev = litest_current_device();
        struct libinput *li = dev->libinput;
        struct libinput_event *event;
-       struct libinput_event_added_device *device_event = NULL;
        struct libinput_device *device;
 
        libinput_dispatch(li);
@@ -175,16 +172,15 @@ START_TEST(path_added_device)
                type = libinput_event_get_type(event);
 
                if (type == LIBINPUT_EVENT_ADDED_DEVICE) {
-                       device_event = (struct libinput_event_added_device*)event;
                        break;
                }
 
                libinput_event_destroy(event);
        }
 
-       ck_assert(device_event != NULL);
+       ck_assert(event != NULL);
 
-       device = libinput_event_added_device_get_device(device_event);
+       device = libinput_event_get_device(event);
        ck_assert(device != NULL);
 
        libinput_event_destroy(event);
index 37658f1..9e16b82 100644 (file)
@@ -138,7 +138,6 @@ START_TEST(udev_added_seat_default)
        struct libinput *li;
        struct libinput_event *event;
        struct udev *udev;
-       struct libinput_event_added_device *device_event;
        struct libinput_device *device;
        struct libinput_seat *seat;
        const char *seat_name;
@@ -159,8 +158,7 @@ START_TEST(udev_added_seat_default)
                        continue;
                }
 
-               device_event = (struct libinput_event_added_device*)event;
-               device = libinput_event_added_device_get_device(device_event);
+               device = libinput_event_get_device(event);
                seat = libinput_device_get_seat(device);
                ck_assert(seat != NULL);