Provide accessors to retrieve the right event type
authorPeter Hutterer <peter.hutterer@who-t.net>
Thu, 19 Dec 2013 23:22:32 +0000 (09:22 +1000)
committerPeter Hutterer <peter.hutterer@who-t.net>
Wed, 22 Jan 2014 01:15:54 +0000 (11:15 +1000)
Slightly enhances the type-safety. A caller may now do something along the
lines of

struct libinput_event_pointer *ptrev;
ptrev = libinput_event_get_pointer_event(event);

if (!ptrev)
   oops, that wasn't a pointer event

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

index ec8623948681a43f184b280077ce117f9e81a1a4..45f922ecb915cee589e42e4302152042f1e339a9 100644 (file)
@@ -107,6 +107,92 @@ libinput_event_get_device(struct libinput_event *event)
        return event->device;
 }
 
+LIBINPUT_EXPORT struct libinput_event_pointer*
+libinput_event_get_pointer_event(struct libinput_event *event)
+{
+       switch (event->type) {
+       case LIBINPUT_EVENT_NONE:
+               abort(); /* not used as actual event type */
+       case LIBINPUT_EVENT_DEVICE_ADDED:
+       case LIBINPUT_EVENT_DEVICE_REMOVED:
+       case LIBINPUT_EVENT_KEYBOARD_KEY:
+               break;
+       case LIBINPUT_EVENT_POINTER_MOTION:
+       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
+       case LIBINPUT_EVENT_POINTER_BUTTON:
+       case LIBINPUT_EVENT_POINTER_AXIS:
+               return (struct libinput_event_pointer*)event;
+       case LIBINPUT_EVENT_TOUCH_TOUCH:
+               break;
+       }
+
+       return NULL;
+}
+
+LIBINPUT_EXPORT struct libinput_event_keyboard*
+libinput_event_get_keyboard_event(struct libinput_event *event)
+{
+       switch (event->type) {
+       case LIBINPUT_EVENT_NONE:
+               abort(); /* not used as actual event type */
+       case LIBINPUT_EVENT_DEVICE_ADDED:
+       case LIBINPUT_EVENT_DEVICE_REMOVED:
+               break;
+       case LIBINPUT_EVENT_KEYBOARD_KEY:
+               return (struct libinput_event_keyboard*)event;
+       case LIBINPUT_EVENT_POINTER_MOTION:
+       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
+       case LIBINPUT_EVENT_POINTER_BUTTON:
+       case LIBINPUT_EVENT_POINTER_AXIS:
+       case LIBINPUT_EVENT_TOUCH_TOUCH:
+               break;
+       }
+
+       return NULL;
+}
+
+LIBINPUT_EXPORT struct libinput_event_touch*
+libinput_event_get_touch_event(struct libinput_event *event)
+{
+       switch (event->type) {
+       case LIBINPUT_EVENT_NONE:
+               abort(); /* not used as actual event type */
+       case LIBINPUT_EVENT_DEVICE_ADDED:
+       case LIBINPUT_EVENT_DEVICE_REMOVED:
+       case LIBINPUT_EVENT_KEYBOARD_KEY:
+       case LIBINPUT_EVENT_POINTER_MOTION:
+       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
+       case LIBINPUT_EVENT_POINTER_BUTTON:
+       case LIBINPUT_EVENT_POINTER_AXIS:
+               break;
+       case LIBINPUT_EVENT_TOUCH_TOUCH:
+               return (struct libinput_event_touch*)event;
+       }
+
+       return NULL;
+}
+
+LIBINPUT_EXPORT struct libinput_event_device_notify*
+libinput_event_get_device_notify_event(struct libinput_event *event)
+{
+       switch (event->type) {
+       case LIBINPUT_EVENT_NONE:
+               abort(); /* not used as actual event type */
+       case LIBINPUT_EVENT_DEVICE_ADDED:
+       case LIBINPUT_EVENT_DEVICE_REMOVED:
+               return (struct libinput_event_device_notify*)event;
+       case LIBINPUT_EVENT_KEYBOARD_KEY:
+       case LIBINPUT_EVENT_POINTER_MOTION:
+       case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
+       case LIBINPUT_EVENT_POINTER_BUTTON:
+       case LIBINPUT_EVENT_POINTER_AXIS:
+       case LIBINPUT_EVENT_TOUCH_TOUCH:
+               break;
+       }
+
+       return NULL;
+}
+
 LIBINPUT_EXPORT uint32_t
 libinput_event_keyboard_get_time(
        struct libinput_event_keyboard *event)
index ec8336d737a2bea135b57882868e635f5b4daa9e..1df6cdd341c22056ea339709fe213c7deabb6e91 100644 (file)
@@ -237,6 +237,50 @@ libinput_event_get_context(struct libinput_event *event);
 struct libinput_device*
 libinput_event_get_device(struct libinput_event *event);
 
+/**
+ * @ingroup event
+ *
+ * Return the pointer event that is this input event. If the event type does
+ * not match the pointer event types, this function returns NULL.
+ *
+ * @return A pointer event, or NULL for other events
+ */
+struct libinput_event_pointer*
+libinput_event_get_pointer_event(struct libinput_event *event);
+
+/**
+ * @ingroup event
+ *
+ * Return the keyboard event that is this input event. If the event type does
+ * not match the keyboard event types, this function returns NULL.
+ *
+ * @return A keyboard event, or NULL for other events
+ */
+struct libinput_event_keyboard*
+libinput_event_get_keyboard_event(struct libinput_event *event);
+
+/**
+ * @ingroup event
+ *
+ * Return the touch event that is this input event. If the event type does
+ * not match the touch event types, this function returns NULL.
+ *
+ * @return A touch event, or NULL for other events
+ */
+struct libinput_event_touch*
+libinput_event_get_touch_event(struct libinput_event *event);
+
+/**
+ * @ingroup event
+ *
+ * Return the device event that is this input event. If the event type does
+ * not match the device event types, this function returns NULL.
+ *
+ * @return A device event, or NULL for other events
+ */
+struct libinput_event_device_notify*
+libinput_event_get_device_notify_event(struct libinput_event *event);
+
 /**
  * @defgroup event_keyboard Keyboard events
  *