[PATCH libinput 5/6] Replace event type check switch statements with a helper macro/function

Peter Hutterer peter.hutterer at who-t.net
Thu Mar 12 17:37:49 PDT 2015


The helper function now prints an error message if the event type passed is
not allowed.

Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
 src/libinput.c | 142 ++++++++++++++++++++++++++-------------------------------
 test/litest.c  |  12 +++++
 test/litest.h  |   2 +
 test/misc.c    |  10 ++++
 4 files changed, 88 insertions(+), 78 deletions(-)

diff --git a/src/libinput.c b/src/libinput.c
index 42354ba..4d88c46 100644
--- a/src/libinput.c
+++ b/src/libinput.c
@@ -35,6 +35,42 @@
 #include "evdev.h"
 #include "timer.h"
 
+#define require_event_type(li_, type_, retval_, ...)	\
+	if (type_ == LIBINPUT_EVENT_NONE) abort(); \
+	if (!check_event_type(li_, __func__, type_, __VA_ARGS__, -1)) \
+		return retval_; \
+
+static inline bool
+check_event_type(struct libinput *libinput,
+		 const char *function_name,
+		 enum libinput_event_type type_in,
+		 ...)
+{
+	bool rc = false;
+	va_list args;
+	unsigned int type_permitted;
+
+	va_start(args, type_in);
+	type_permitted = va_arg(args, unsigned int);
+
+	while (type_permitted != (unsigned int)-1) {
+		if (type_permitted == type_in) {
+			rc = true;
+			break;
+		}
+		type_permitted = va_arg(args, unsigned int);
+	}
+
+	va_end(args);
+
+	if (!rc)
+		log_bug_client(libinput,
+			       "Invalid event type %d passed to %s()\n",
+			       type_in, function_name);
+
+	return rc;
+}
+
 struct libinput_source {
 	libinput_source_dispatch_t dispatch;
 	void *user_data;
@@ -165,103 +201,53 @@ libinput_event_get_device(struct libinput_event *event)
 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_DOWN:
-	case LIBINPUT_EVENT_TOUCH_UP:
-	case LIBINPUT_EVENT_TOUCH_MOTION:
-	case LIBINPUT_EVENT_TOUCH_CANCEL:
-	case LIBINPUT_EVENT_TOUCH_FRAME:
-		break;
-	}
+	require_event_type(libinput_event_get_context(event),
+			   event->type,
+			   NULL,
+			   LIBINPUT_EVENT_POINTER_MOTION,
+			   LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE,
+			   LIBINPUT_EVENT_POINTER_BUTTON,
+			   LIBINPUT_EVENT_POINTER_AXIS);
 
-	return NULL;
+	return (struct libinput_event_pointer *) event;
 }
 
 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_DOWN:
-	case LIBINPUT_EVENT_TOUCH_UP:
-	case LIBINPUT_EVENT_TOUCH_MOTION:
-	case LIBINPUT_EVENT_TOUCH_CANCEL:
-	case LIBINPUT_EVENT_TOUCH_FRAME:
-		break;
-	}
+	require_event_type(libinput_event_get_context(event),
+			   event->type,
+			   NULL,
+			   LIBINPUT_EVENT_KEYBOARD_KEY);
 
-	return NULL;
+	return (struct libinput_event_keyboard *) event;
 }
 
 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_DOWN:
-	case LIBINPUT_EVENT_TOUCH_UP:
-	case LIBINPUT_EVENT_TOUCH_MOTION:
-	case LIBINPUT_EVENT_TOUCH_CANCEL:
-	case LIBINPUT_EVENT_TOUCH_FRAME:
-		return (struct libinput_event_touch *) event;
-	}
+	require_event_type(libinput_event_get_context(event),
+			   event->type,
+			   NULL,
+			   LIBINPUT_EVENT_TOUCH_DOWN,
+			   LIBINPUT_EVENT_TOUCH_UP,
+			   LIBINPUT_EVENT_TOUCH_MOTION,
+			   LIBINPUT_EVENT_TOUCH_CANCEL,
+			   LIBINPUT_EVENT_TOUCH_FRAME);
 
-	return NULL;
+	return (struct libinput_event_touch *) event;
 }
 
 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_DOWN:
-	case LIBINPUT_EVENT_TOUCH_UP:
-	case LIBINPUT_EVENT_TOUCH_MOTION:
-	case LIBINPUT_EVENT_TOUCH_CANCEL:
-	case LIBINPUT_EVENT_TOUCH_FRAME:
-		break;
-	}
+	require_event_type(libinput_event_get_context(event),
+			   event->type,
+			   NULL,
+			   LIBINPUT_EVENT_DEVICE_ADDED,
+			   LIBINPUT_EVENT_DEVICE_REMOVED);
 
-	return NULL;
+	return (struct libinput_event_device_notify *) event;
 }
 
 LIBINPUT_EXPORT uint32_t
diff --git a/test/litest.c b/test/litest.c
index ad0aff2..93a6097 100644
--- a/test/litest.c
+++ b/test/litest.c
@@ -642,6 +642,18 @@ litest_create_context(void)
 	return libinput;
 }
 
+void
+litest_disable_log_handler(struct libinput *libinput)
+{
+	libinput_log_set_handler(libinput, NULL);
+}
+
+void
+litest_restore_log_handler(struct libinput *libinput)
+{
+	libinput_log_set_handler(libinput, litest_log_handler);
+}
+
 struct litest_device *
 litest_add_device_with_overrides(struct libinput *libinput,
 				 enum litest_device_type which,
diff --git a/test/litest.h b/test/litest.h
index e85b511..edbc55e 100644
--- a/test/litest.h
+++ b/test/litest.h
@@ -92,6 +92,8 @@ struct litest_device {
 };
 
 struct libinput *litest_create_context(void);
+void litest_disable_log_handler(struct libinput *libinput);
+void litest_restore_log_handler(struct libinput *libinput);
 
 void litest_add(const char *name, void *func,
 		enum litest_device_feature required_feature,
diff --git a/test/misc.c b/test/misc.c
index 5b8191f..db26d67 100644
--- a/test/misc.c
+++ b/test/misc.c
@@ -126,9 +126,11 @@ START_TEST(event_conversion_device_notify)
 			else if (type == LIBINPUT_EVENT_DEVICE_REMOVED)
 				device_removed++;
 
+			litest_disable_log_handler(li);
 			ck_assert(libinput_event_get_pointer_event(event) == NULL);
 			ck_assert(libinput_event_get_keyboard_event(event) == NULL);
 			ck_assert(libinput_event_get_touch_event(event) == NULL);
+			litest_restore_log_handler(li);
 		}
 
 		libinput_event_destroy(event);
@@ -178,9 +180,11 @@ START_TEST(event_conversion_pointer)
 			else if (type == LIBINPUT_EVENT_POINTER_BUTTON)
 				button++;
 
+			litest_disable_log_handler(li);
 			ck_assert(libinput_event_get_device_notify_event(event) == NULL);
 			ck_assert(libinput_event_get_keyboard_event(event) == NULL);
 			ck_assert(libinput_event_get_touch_event(event) == NULL);
+			litest_restore_log_handler(li);
 		}
 		libinput_event_destroy(event);
 	}
@@ -224,9 +228,11 @@ START_TEST(event_conversion_pointer_abs)
 			else if (type == LIBINPUT_EVENT_POINTER_BUTTON)
 				button++;
 
+			litest_disable_log_handler(li);
 			ck_assert(libinput_event_get_device_notify_event(event) == NULL);
 			ck_assert(libinput_event_get_keyboard_event(event) == NULL);
 			ck_assert(libinput_event_get_touch_event(event) == NULL);
+			litest_restore_log_handler(li);
 		}
 		libinput_event_destroy(event);
 	}
@@ -263,9 +269,11 @@ START_TEST(event_conversion_key)
 
 			key++;
 
+			litest_disable_log_handler(li);
 			ck_assert(libinput_event_get_device_notify_event(event) == NULL);
 			ck_assert(libinput_event_get_pointer_event(event) == NULL);
 			ck_assert(libinput_event_get_touch_event(event) == NULL);
+			litest_restore_log_handler(li);
 		}
 		libinput_event_destroy(event);
 	}
@@ -309,9 +317,11 @@ START_TEST(event_conversion_touch)
 
 			touch++;
 
+			litest_disable_log_handler(li);
 			ck_assert(libinput_event_get_device_notify_event(event) == NULL);
 			ck_assert(libinput_event_get_pointer_event(event) == NULL);
 			ck_assert(libinput_event_get_keyboard_event(event) == NULL);
+			litest_restore_log_handler(li);
 		}
 		libinput_event_destroy(event);
 	}
-- 
2.1.0



More information about the wayland-devel mailing list