mirror of
https://gitlab.freedesktop.org/libinput/libinput.git
synced 2025-12-30 01:20:21 +01:00
Replace event type check switch statements with a helper macro/function
The helper function now prints an error message if the event type passed is not allowed. Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net> Reviewed-by: Hans de Goede <hdegoede@redhat.com>
This commit is contained in:
parent
44f7446b2a
commit
7f28b714d8
4 changed files with 88 additions and 78 deletions
142
src/libinput.c
142
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
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
10
test/misc.c
10
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue