[PATCH libinput 1/2] evdev: improve type-safety on dispatch switches
Peter Hutterer
peter.hutterer at who-t.net
Mon Jan 30 22:21:55 UTC 2017
Set the dispatch type on creation, then check that whenever we try to get the
dispatch struct. This avoids a potential mismatch between the backends.
Plus, use of container_of means we're not dependent on the exact layout
anymore.
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
---
src/evdev-lid.c | 28 +++++++++++++++++-----------
src/evdev-mt-touchpad-tap.c | 22 ++++++++++++----------
src/evdev-mt-touchpad.c | 14 ++++++--------
src/evdev-mt-touchpad.h | 10 ++++++++++
src/evdev-tablet-pad.c | 7 ++++---
src/evdev-tablet-pad.h | 10 ++++++++++
src/evdev-tablet.c | 21 ++++++++-------------
src/evdev-tablet.h | 10 ++++++++++
src/evdev.c | 17 +++++++++--------
src/evdev.h | 27 +++++++++++++++++++++++++++
10 files changed, 113 insertions(+), 53 deletions(-)
diff --git a/src/evdev-lid.c b/src/evdev-lid.c
index c5af753..6a2b506 100644
--- a/src/evdev-lid.c
+++ b/src/evdev-lid.c
@@ -41,13 +41,22 @@ struct lid_switch_dispatch {
} keyboard;
};
+static inline struct lid_switch_dispatch*
+lid_dispatch(struct evdev_dispatch *dispatch)
+{
+ struct lid_switch_dispatch *l;
+
+ evdev_verify_dispatch_type(dispatch, DISPATCH_LID_SWITCH);
+
+ return container_of(dispatch, l, base);
+}
+
static void
lid_switch_keyboard_event(uint64_t time,
struct libinput_event *event,
void *data)
{
- struct lid_switch_dispatch *dispatch =
- (struct lid_switch_dispatch*)data;
+ struct lid_switch_dispatch *dispatch = lid_dispatch(data);
if (!dispatch->lid_is_closed)
return;
@@ -127,8 +136,7 @@ lid_switch_process(struct evdev_dispatch *evdev_dispatch,
struct input_event *event,
uint64_t time)
{
- struct lid_switch_dispatch *dispatch =
- (struct lid_switch_dispatch*)evdev_dispatch;
+ struct lid_switch_dispatch *dispatch = lid_dispatch(evdev_dispatch);
switch (event->type) {
case EV_SW:
@@ -168,8 +176,7 @@ evdev_read_switch_reliability_prop(struct evdev_device *device)
static void
lid_switch_destroy(struct evdev_dispatch *evdev_dispatch)
{
- struct lid_switch_dispatch *dispatch =
- (struct lid_switch_dispatch*)evdev_dispatch;
+ struct lid_switch_dispatch *dispatch = lid_dispatch(evdev_dispatch);
free(dispatch);
}
@@ -179,7 +186,7 @@ lid_switch_pair_keyboard(struct evdev_device *lid_switch,
struct evdev_device *keyboard)
{
struct lid_switch_dispatch *dispatch =
- (struct lid_switch_dispatch*)lid_switch->dispatch;
+ lid_dispatch(lid_switch->dispatch);
unsigned int bus_kbd = libevdev_get_id_bustype(keyboard->evdev);
if ((keyboard->tags & EVDEV_TAG_KEYBOARD) == 0)
@@ -214,8 +221,7 @@ static void
lid_switch_interface_device_removed(struct evdev_device *device,
struct evdev_device *removed_device)
{
- struct lid_switch_dispatch *dispatch =
- (struct lid_switch_dispatch*)device->dispatch;
+ struct lid_switch_dispatch *dispatch = lid_dispatch(device->dispatch);
if (removed_device == dispatch->keyboard.keyboard) {
libinput_device_remove_event_listener(
@@ -228,8 +234,7 @@ static void
lid_switch_sync_initial_state(struct evdev_device *device,
struct evdev_dispatch *evdev_dispatch)
{
- struct lid_switch_dispatch *dispatch =
- (struct lid_switch_dispatch*)evdev_dispatch;
+ struct lid_switch_dispatch *dispatch = lid_dispatch(device->dispatch);
struct libevdev *evdev = device->evdev;
bool is_closed = false;
@@ -284,6 +289,7 @@ evdev_lid_switch_dispatch_create(struct evdev_device *lid_device)
if (dispatch == NULL)
return NULL;
+ dispatch->base.dispatch_type = DISPATCH_LID_SWITCH;
dispatch->base.interface = &lid_switch_interface;
dispatch->device = lid_device;
libinput_device_init_event_listener(&dispatch->keyboard.listener);
diff --git a/src/evdev-mt-touchpad-tap.c b/src/evdev-mt-touchpad-tap.c
index 9fba521..5fccbc2 100644
--- a/src/evdev-mt-touchpad-tap.c
+++ b/src/evdev-mt-touchpad-tap.c
@@ -901,7 +901,7 @@ tp_tap_config_count(struct libinput_device *device)
struct tp_dispatch *tp = NULL;
dispatch = ((struct evdev_device *) device)->dispatch;
- tp = container_of(dispatch, tp, base);
+ tp = tp_dispatch(dispatch);
return min(tp->ntouches, 3U); /* we only do up to 3 finger tap */
}
@@ -910,10 +910,12 @@ static enum libinput_config_status
tp_tap_config_set_enabled(struct libinput_device *device,
enum libinput_config_tap_state enabled)
{
- struct evdev_dispatch *dispatch = ((struct evdev_device *) device)->dispatch;
+ struct evdev_dispatch *dispatch;
struct tp_dispatch *tp = NULL;
- tp = container_of(dispatch, tp, base);
+ dispatch = ((struct evdev_device *) device)->dispatch;
+ tp = tp_dispatch(dispatch);
+
tp_tap_enabled_update(tp, tp->tap.suspended,
(enabled == LIBINPUT_CONFIG_TAP_ENABLED),
libinput_now(device->seat->libinput));
@@ -928,7 +930,7 @@ tp_tap_config_is_enabled(struct libinput_device *device)
struct tp_dispatch *tp = NULL;
dispatch = ((struct evdev_device *) device)->dispatch;
- tp = container_of(dispatch, tp, base);
+ tp = tp_dispatch(dispatch);
return tp->tap.enabled ? LIBINPUT_CONFIG_TAP_ENABLED :
LIBINPUT_CONFIG_TAP_DISABLED;
@@ -971,7 +973,7 @@ tp_tap_config_set_map(struct libinput_device *device,
struct evdev_dispatch *dispatch = ((struct evdev_device *) device)->dispatch;
struct tp_dispatch *tp = NULL;
- tp = container_of(dispatch, tp, base);
+ tp = tp_dispatch(dispatch);
tp->tap.want_map = map;
tp_tap_update_map(tp);
@@ -985,7 +987,7 @@ tp_tap_config_get_map(struct libinput_device *device)
struct evdev_dispatch *dispatch = ((struct evdev_device *) device)->dispatch;
struct tp_dispatch *tp = NULL;
- tp = container_of(dispatch, tp, base);
+ tp = tp_dispatch(dispatch);
return tp->tap.want_map;
}
@@ -1003,7 +1005,7 @@ tp_tap_config_set_drag_enabled(struct libinput_device *device,
struct evdev_dispatch *dispatch = ((struct evdev_device *) device)->dispatch;
struct tp_dispatch *tp = NULL;
- tp = container_of(dispatch, tp, base);
+ tp = tp_dispatch(dispatch);
tp->tap.drag_enabled = enabled;
return LIBINPUT_CONFIG_STATUS_SUCCESS;
@@ -1015,7 +1017,7 @@ tp_tap_config_get_drag_enabled(struct libinput_device *device)
struct evdev_device *evdev = (struct evdev_device *)device;
struct tp_dispatch *tp = NULL;
- tp = container_of(evdev->dispatch, tp, base);
+ tp = tp_dispatch(evdev->dispatch);
return tp->tap.drag_enabled;
}
@@ -1041,7 +1043,7 @@ tp_tap_config_set_draglock_enabled(struct libinput_device *device,
struct evdev_dispatch *dispatch = ((struct evdev_device *) device)->dispatch;
struct tp_dispatch *tp = NULL;
- tp = container_of(dispatch, tp, base);
+ tp = tp_dispatch(dispatch);
tp->tap.drag_lock_enabled = enabled;
return LIBINPUT_CONFIG_STATUS_SUCCESS;
@@ -1053,7 +1055,7 @@ tp_tap_config_get_draglock_enabled(struct libinput_device *device)
struct evdev_device *evdev = (struct evdev_device *)device;
struct tp_dispatch *tp = NULL;
- tp = container_of(evdev->dispatch, tp, base);
+ tp = tp_dispatch(evdev->dispatch);
return tp->tap.drag_lock_enabled;
}
diff --git a/src/evdev-mt-touchpad.c b/src/evdev-mt-touchpad.c
index 5b16645..d232634 100644
--- a/src/evdev-mt-touchpad.c
+++ b/src/evdev-mt-touchpad.c
@@ -1140,8 +1140,7 @@ tp_interface_process(struct evdev_dispatch *dispatch,
struct input_event *e,
uint64_t time)
{
- struct tp_dispatch *tp =
- (struct tp_dispatch *)dispatch;
+ struct tp_dispatch *tp = tp_dispatch(dispatch);
if (tp->ignore_events)
return;
@@ -1185,8 +1184,7 @@ tp_remove_sendevents(struct tp_dispatch *tp)
static void
tp_interface_remove(struct evdev_dispatch *dispatch)
{
- struct tp_dispatch *tp =
- (struct tp_dispatch*)dispatch;
+ struct tp_dispatch *tp = tp_dispatch(dispatch);
tp_remove_tap(tp);
tp_remove_buttons(tp);
@@ -1198,8 +1196,7 @@ tp_interface_remove(struct evdev_dispatch *dispatch)
static void
tp_interface_destroy(struct evdev_dispatch *dispatch)
{
- struct tp_dispatch *tp =
- (struct tp_dispatch*)dispatch;
+ struct tp_dispatch *tp = tp_dispatch(dispatch);
free(tp->touches);
free(tp);
@@ -1261,7 +1258,7 @@ static void
tp_interface_suspend(struct evdev_dispatch *dispatch,
struct evdev_device *device)
{
- struct tp_dispatch *tp = (struct tp_dispatch *)dispatch;
+ struct tp_dispatch *tp = tp_dispatch(dispatch);
tp_clear_state(tp);
}
@@ -1742,7 +1739,7 @@ tp_interface_toggle_touch(struct evdev_dispatch *dispatch,
struct evdev_device *device,
bool enable)
{
- struct tp_dispatch *tp = (struct tp_dispatch*)dispatch;
+ struct tp_dispatch *tp = tp_dispatch(dispatch);
bool ignore_events = !enable;
if (ignore_events == tp->ignore_events)
@@ -2322,6 +2319,7 @@ static int
tp_init(struct tp_dispatch *tp,
struct evdev_device *device)
{
+ tp->base.dispatch_type = DISPATCH_TOUCHPAD;
tp->base.interface = &tp_interface;
tp->device = device;
diff --git a/src/evdev-mt-touchpad.h b/src/evdev-mt-touchpad.h
index 8ae1bf9..a54eb5c 100644
--- a/src/evdev-mt-touchpad.h
+++ b/src/evdev-mt-touchpad.h
@@ -383,6 +383,16 @@ struct tp_dispatch {
} lid_switch;
};
+static inline struct tp_dispatch*
+tp_dispatch(struct evdev_dispatch *dispatch)
+{
+ struct tp_dispatch *tp;
+
+ evdev_verify_dispatch_type(dispatch, DISPATCH_TOUCHPAD);
+
+ return container_of(dispatch, tp, base);
+}
+
#define tp_for_each_touch(_tp, _t) \
for (unsigned int _i = 0; _i < (_tp)->ntouches && (_t = &(_tp)->touches[_i]); _i++)
diff --git a/src/evdev-tablet-pad.c b/src/evdev-tablet-pad.c
index 82542bc..cc7553f 100644
--- a/src/evdev-tablet-pad.c
+++ b/src/evdev-tablet-pad.c
@@ -452,7 +452,7 @@ pad_process(struct evdev_dispatch *dispatch,
struct input_event *e,
uint64_t time)
{
- struct pad_dispatch *pad = (struct pad_dispatch *)dispatch;
+ struct pad_dispatch *pad = pad_dispatch(dispatch);
switch (e->type) {
case EV_ABS:
@@ -481,7 +481,7 @@ static void
pad_suspend(struct evdev_dispatch *dispatch,
struct evdev_device *device)
{
- struct pad_dispatch *pad = (struct pad_dispatch *)dispatch;
+ struct pad_dispatch *pad = pad_dispatch(dispatch);
struct libinput *libinput = pad_libinput_context(pad);
unsigned int code;
@@ -496,7 +496,7 @@ pad_suspend(struct evdev_dispatch *dispatch,
static void
pad_destroy(struct evdev_dispatch *dispatch)
{
- struct pad_dispatch *pad = (struct pad_dispatch*)dispatch;
+ struct pad_dispatch *pad = pad_dispatch(dispatch);
pad_destroy_leds(pad);
free(pad);
@@ -556,6 +556,7 @@ pad_init_left_handed(struct evdev_device *device)
static int
pad_init(struct pad_dispatch *pad, struct evdev_device *device)
{
+ pad->base.dispatch_type = DISPATCH_TABLET_PAD;
pad->base.interface = &pad_interface;
pad->device = device;
pad->status = PAD_NONE;
diff --git a/src/evdev-tablet-pad.h b/src/evdev-tablet-pad.h
index 9002fca..5569007 100644
--- a/src/evdev-tablet-pad.h
+++ b/src/evdev-tablet-pad.h
@@ -70,6 +70,16 @@ struct pad_dispatch {
} modes;
};
+static inline struct pad_dispatch*
+pad_dispatch(struct evdev_dispatch *dispatch)
+{
+ struct pad_dispatch *p;
+
+ evdev_verify_dispatch_type(dispatch, DISPATCH_TABLET_PAD);
+
+ return container_of(dispatch, p, base);
+}
+
static inline struct libinput *
pad_libinput_context(const struct pad_dispatch *pad)
{
diff --git a/src/evdev-tablet.c b/src/evdev-tablet.c
index b76d8ea..12a014b 100644
--- a/src/evdev-tablet.c
+++ b/src/evdev-tablet.c
@@ -201,8 +201,7 @@ tablet_process_absolute(struct tablet_dispatch *tablet,
static void
tablet_change_to_left_handed(struct evdev_device *device)
{
- struct tablet_dispatch *tablet =
- (struct tablet_dispatch*)device->dispatch;
+ struct tablet_dispatch *tablet = tablet_dispatch(device->dispatch);
if (device->left_handed.enabled == device->left_handed.want_enabled)
return;
@@ -1484,8 +1483,7 @@ tablet_process(struct evdev_dispatch *dispatch,
struct input_event *e,
uint64_t time)
{
- struct tablet_dispatch *tablet =
- (struct tablet_dispatch *)dispatch;
+ struct tablet_dispatch *tablet = tablet_dispatch(dispatch);
switch (e->type) {
case EV_ABS:
@@ -1518,8 +1516,7 @@ static void
tablet_suspend(struct evdev_dispatch *dispatch,
struct evdev_device *device)
{
- struct tablet_dispatch *tablet =
- (struct tablet_dispatch *)dispatch;
+ struct tablet_dispatch *tablet = tablet_dispatch(dispatch);
tablet_set_touch_device_enabled(tablet->touch_device, true);
}
@@ -1527,8 +1524,7 @@ tablet_suspend(struct evdev_dispatch *dispatch,
static void
tablet_destroy(struct evdev_dispatch *dispatch)
{
- struct tablet_dispatch *tablet =
- (struct tablet_dispatch*)dispatch;
+ struct tablet_dispatch *tablet = tablet_dispatch(dispatch);
struct libinput_tablet_tool *tool, *tmp;
list_for_each_safe(tool, tmp, &tablet->tool_list, link) {
@@ -1542,8 +1538,7 @@ static void
tablet_device_added(struct evdev_device *device,
struct evdev_device *added_device)
{
- struct tablet_dispatch *tablet =
- (struct tablet_dispatch*)device->dispatch;
+ struct tablet_dispatch *tablet = tablet_dispatch(device->dispatch);
if (libinput_device_get_device_group(&device->base) !=
libinput_device_get_device_group(&added_device->base))
@@ -1560,8 +1555,7 @@ static void
tablet_device_removed(struct evdev_device *device,
struct evdev_device *removed_device)
{
- struct tablet_dispatch *tablet =
- (struct tablet_dispatch*)device->dispatch;
+ struct tablet_dispatch *tablet = tablet_dispatch(device->dispatch);
if (tablet->touch_device == removed_device)
tablet->touch_device = NULL;
@@ -1571,10 +1565,10 @@ static void
tablet_check_initial_proximity(struct evdev_device *device,
struct evdev_dispatch *dispatch)
{
+ struct tablet_dispatch *tablet = tablet_dispatch(dispatch);
bool tool_in_prox = false;
int code, state;
enum libinput_tablet_tool_type tool;
- struct tablet_dispatch *tablet = (struct tablet_dispatch*)dispatch;
for (tool = LIBINPUT_TABLET_TOOL_TYPE_PEN; tool <= LIBINPUT_TABLET_TOOL_TYPE_MAX; tool++) {
code = tablet_tool_to_evcode(tool);
@@ -1746,6 +1740,7 @@ tablet_init(struct tablet_dispatch *tablet,
enum libinput_tablet_tool_axis axis;
int rc;
+ tablet->base.dispatch_type = DISPATCH_TABLET;
tablet->base.interface = &tablet_interface;
tablet->device = device;
tablet->status = TABLET_NONE;
diff --git a/src/evdev-tablet.h b/src/evdev-tablet.h
index 2279e03..2b2b1a7 100644
--- a/src/evdev-tablet.h
+++ b/src/evdev-tablet.h
@@ -76,6 +76,16 @@ struct tablet_dispatch {
struct evdev_device *touch_device;
};
+static inline struct tablet_dispatch*
+tablet_dispatch(struct evdev_dispatch *dispatch)
+{
+ struct tablet_dispatch *t;
+
+ evdev_verify_dispatch_type(dispatch, DISPATCH_TABLET);
+
+ return container_of(dispatch, t, base);
+}
+
static inline enum libinput_tablet_tool_axis
evcode_to_axis(const uint32_t evcode)
{
diff --git a/src/evdev.c b/src/evdev.c
index 6ac3427..e040583 100644
--- a/src/evdev.c
+++ b/src/evdev.c
@@ -1097,7 +1097,7 @@ fallback_process(struct evdev_dispatch *evdev_dispatch,
struct input_event *event,
uint64_t time)
{
- struct fallback_dispatch *dispatch = (struct fallback_dispatch*)evdev_dispatch;
+ struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
enum evdev_event_type sent;
if (dispatch->ignore_events)
@@ -1226,7 +1226,7 @@ static void
fallback_suspend(struct evdev_dispatch *evdev_dispatch,
struct evdev_device *device)
{
- struct fallback_dispatch *dispatch = (struct fallback_dispatch*)evdev_dispatch;
+ struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
fallback_return_to_neutral_state(dispatch, device);
}
@@ -1236,7 +1236,7 @@ fallback_toggle_touch(struct evdev_dispatch *evdev_dispatch,
struct evdev_device *device,
bool enable)
{
- struct fallback_dispatch *dispatch = (struct fallback_dispatch*)evdev_dispatch;
+ struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
bool ignore_events = !enable;
if (ignore_events == dispatch->ignore_events)
@@ -1251,7 +1251,7 @@ fallback_toggle_touch(struct evdev_dispatch *evdev_dispatch,
static void
fallback_destroy(struct evdev_dispatch *evdev_dispatch)
{
- struct fallback_dispatch *dispatch = (struct fallback_dispatch*)evdev_dispatch;
+ struct fallback_dispatch *dispatch = fallback_dispatch(evdev_dispatch);
free(dispatch->mt.slots);
free(dispatch);
@@ -1369,7 +1369,7 @@ evdev_left_handed_has(struct libinput_device *device)
static void
evdev_change_to_left_handed(struct evdev_device *device)
{
- struct fallback_dispatch *dispatch = (struct fallback_dispatch*)device->dispatch;
+ struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);
if (device->left_handed.want_enabled == device->left_handed.enabled)
return;
@@ -1431,7 +1431,7 @@ evdev_scroll_get_methods(struct libinput_device *device)
static void
evdev_change_scroll_method(struct evdev_device *device)
{
- struct fallback_dispatch *dispatch = (struct fallback_dispatch*)device->dispatch;
+ struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);
if (device->scroll.want_method == device->scroll.method &&
device->scroll.want_button == device->scroll.button)
@@ -1620,7 +1620,7 @@ evdev_rotation_config_set_angle(struct libinput_device *libinput_device,
unsigned int degrees_cw)
{
struct evdev_device *device = (struct evdev_device*)libinput_device;
- struct fallback_dispatch *dispatch = (struct fallback_dispatch*)device->dispatch;
+ struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);
dispatch->rotation.angle = degrees_cw;
matrix_init_rotate(&dispatch->rotation.matrix, degrees_cw);
@@ -1632,7 +1632,7 @@ static unsigned int
evdev_rotation_config_get_angle(struct libinput_device *libinput_device)
{
struct evdev_device *device = (struct evdev_device*)libinput_device;
- struct fallback_dispatch *dispatch = (struct fallback_dispatch*)device->dispatch;
+ struct fallback_dispatch *dispatch = fallback_dispatch(device->dispatch);
return dispatch->rotation.angle;
}
@@ -1774,6 +1774,7 @@ fallback_dispatch_create(struct libinput_device *device)
if (dispatch == NULL)
return NULL;
+ dispatch->base.dispatch_type = DISPATCH_FALLBACK;
dispatch->base.interface = &fallback_interface;
dispatch->pending_event = EVDEV_NONE;
diff --git a/src/evdev.h b/src/evdev.h
index 1d802ba..924a4a1 100644
--- a/src/evdev.h
+++ b/src/evdev.h
@@ -284,7 +284,16 @@ struct evdev_dispatch_interface {
bool enable);
};
+enum evdev_dispatch_type {
+ DISPATCH_FALLBACK,
+ DISPATCH_TOUCHPAD,
+ DISPATCH_TABLET,
+ DISPATCH_TABLET_PAD,
+ DISPATCH_LID_SWITCH,
+};
+
struct evdev_dispatch {
+ enum evdev_dispatch_type dispatch_type;
struct evdev_dispatch_interface *interface;
struct {
@@ -293,6 +302,14 @@ struct evdev_dispatch {
} sendevents;
};
+static inline void
+evdev_verify_dispatch_type(struct evdev_dispatch *dispatch,
+ enum evdev_dispatch_type type)
+{
+ if (dispatch->dispatch_type != type)
+ abort();
+}
+
struct fallback_dispatch {
struct evdev_dispatch base;
@@ -336,6 +353,16 @@ struct fallback_dispatch {
bool ignore_events;
};
+static inline struct fallback_dispatch*
+fallback_dispatch(struct evdev_dispatch *dispatch)
+{
+ struct fallback_dispatch *f;
+
+ evdev_verify_dispatch_type(dispatch, DISPATCH_FALLBACK);
+
+ return container_of(dispatch, f, base);
+}
+
struct evdev_device *
evdev_device_create(struct libinput_seat *seat,
struct udev_device *device);
--
2.9.3
More information about the wayland-devel
mailing list