From 7bcf0d9599322ccabf40f8e1d5d85993def8b181 Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 17 Sep 2018 21:53:03 +0200 Subject: [PATCH] backend/libinput: add assertions --- backend/libinput/backend.c | 23 +++++++++------ backend/libinput/events.c | 52 +++++++++++++++++++++------------- backend/libinput/keyboard.c | 37 +++++++++++++++--------- backend/libinput/tablet_pad.c | 12 +++++--- backend/libinput/tablet_tool.c | 21 +++++++++----- include/backend/libinput.h | 1 + 6 files changed, 94 insertions(+), 52 deletions(-) diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 19dc779d..8106af00 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -7,6 +7,12 @@ #include "backend/libinput.h" #include "util/signal.h" +static struct wlr_libinput_backend *get_libinput_backend_from_backend( + struct wlr_backend *wlr_backend) { + assert(wlr_backend_is_libinput(wlr_backend)); + return (struct wlr_libinput_backend *)wlr_backend; +} + static int libinput_open_restricted(const char *path, int flags, void *_backend) { struct wlr_libinput_backend *backend = _backend; @@ -43,9 +49,9 @@ static void log_libinput(struct libinput *libinput_context, _wlr_vlog(WLR_ERROR, fmt, args); } -static bool backend_start(struct wlr_backend *_backend) { +static bool backend_start(struct wlr_backend *wlr_backend) { struct wlr_libinput_backend *backend = - (struct wlr_libinput_backend *)_backend; + get_libinput_backend_from_backend(wlr_backend); wlr_log(WLR_DEBUG, "Initializing libinput"); backend->libinput_context = libinput_udev_create_context(&libinput_impl, @@ -101,7 +107,7 @@ static void backend_destroy(struct wlr_backend *wlr_backend) { return; } struct wlr_libinput_backend *backend = - (struct wlr_libinput_backend *)wlr_backend; + get_libinput_backend_from_backend(wlr_backend); for (size_t i = 0; i < backend->wlr_device_lists.length; i++) { struct wl_list *wlr_devices = backend->wlr_device_lists.items[i]; @@ -158,9 +164,8 @@ static void handle_display_destroy(struct wl_listener *listener, void *data) { struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, struct wlr_session *session) { - assert(display && session); - - struct wlr_libinput_backend *backend = calloc(1, sizeof(struct wlr_libinput_backend)); + struct wlr_libinput_backend *backend = + calloc(1, sizeof(struct wlr_libinput_backend)); if (!backend) { wlr_log(WLR_ERROR, "Allocation failed: %s", strerror(errno)); return NULL; @@ -187,8 +192,10 @@ error_backend: return NULL; } -struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *_dev) { - struct wlr_libinput_input_device *dev = (struct wlr_libinput_input_device *)_dev; +struct libinput_device *wlr_libinput_get_device_handle( + struct wlr_input_device *wlr_dev) { + struct wlr_libinput_input_device *dev = + (struct wlr_libinput_input_device *)wlr_dev; return dev->handle; } diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 0548e502..9ce62d6f 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -9,6 +9,12 @@ #include "backend/libinput.h" #include "util/signal.h" +struct wlr_libinput_input_device *get_libinput_device_from_device( + struct wlr_input_device *wlr_dev) { + assert(wlr_input_device_is_libinput(wlr_dev)); + return (struct wlr_libinput_input_device *)wlr_dev; +} + struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, struct libinput_device *libinput_dev) { @@ -25,8 +31,9 @@ struct wlr_input_device *get_appropriate_device( return NULL; } -static void input_device_destroy(struct wlr_input_device *_dev) { - struct wlr_libinput_input_device *dev = (struct wlr_libinput_input_device *)_dev; +static void input_device_destroy(struct wlr_input_device *wlr_dev) { + struct wlr_libinput_input_device *dev = + get_libinput_device_from_device(wlr_dev); libinput_device_unref(dev->handle); wl_list_remove(&dev->wlr_input_device.link); free(dev); @@ -37,16 +44,18 @@ static const struct wlr_input_device_impl input_device_impl = { }; static struct wlr_input_device *allocate_device( - struct wlr_libinput_backend *backend, struct libinput_device *libinput_dev, - struct wl_list *wlr_devices, enum wlr_input_device_type type) { + struct wlr_libinput_backend *backend, + struct libinput_device *libinput_dev, struct wl_list *wlr_devices, + enum wlr_input_device_type type) { int vendor = libinput_device_get_id_vendor(libinput_dev); int product = libinput_device_get_id_product(libinput_dev); const char *name = libinput_device_get_name(libinput_dev); - struct wlr_libinput_input_device *wlr_libinput_dev; - if (!(wlr_libinput_dev = calloc(1, sizeof(struct wlr_libinput_input_device)))) { + struct wlr_libinput_input_device *dev = + calloc(1, sizeof(struct wlr_libinput_input_device)); + if (dev == NULL) { return NULL; } - struct wlr_input_device *wlr_dev = &wlr_libinput_dev->wlr_input_device; + struct wlr_input_device *wlr_dev = &dev->wlr_input_device; libinput_device_get_size(libinput_dev, &wlr_dev->width_mm, &wlr_dev->height_mm); const char *output_name = libinput_device_get_output_name(libinput_dev); @@ -54,7 +63,7 @@ static struct wlr_input_device *allocate_device( wlr_dev->output_name = strdup(output_name); } wl_list_insert(wlr_devices, &wlr_dev->link); - wlr_libinput_dev->handle = libinput_dev; + dev->handle = libinput_dev; libinput_device_ref(libinput_dev); wlr_input_device_init(wlr_dev, type, &input_device_impl, name, vendor, product); @@ -62,12 +71,11 @@ static struct wlr_input_device *allocate_device( } bool wlr_input_device_is_libinput(struct wlr_input_device *wlr_dev) { - return wlr_dev->impl == &input_device_impl; + return wlr_dev->impl == &input_device_impl; } static void handle_device_added(struct wlr_libinput_backend *backend, struct libinput_device *libinput_dev) { - assert(backend && libinput_dev); /* * Note: the wlr API exposes only devices with a single capability, because * that meshes better with how Wayland does things and is a bit simpler. @@ -85,7 +93,8 @@ static void handle_device_added(struct wlr_libinput_backend *backend, wl_list_init(wlr_devices); wlr_log(WLR_DEBUG, "Added %s [%d:%d]", name, vendor, product); - if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) { + if (libinput_device_has_capability( + libinput_dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) { struct wlr_input_device *wlr_dev = allocate_device(backend, libinput_dev, wlr_devices, WLR_INPUT_DEVICE_KEYBOARD); if (!wlr_dev) { @@ -98,7 +107,8 @@ static void handle_device_added(struct wlr_libinput_backend *backend, } wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev); } - if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_POINTER)) { + if (libinput_device_has_capability( + libinput_dev, LIBINPUT_DEVICE_CAP_POINTER)) { struct wlr_input_device *wlr_dev = allocate_device(backend, libinput_dev, wlr_devices, WLR_INPUT_DEVICE_POINTER); if (!wlr_dev) { @@ -111,7 +121,8 @@ static void handle_device_added(struct wlr_libinput_backend *backend, } wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev); } - if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TOUCH)) { + if (libinput_device_has_capability( + libinput_dev, LIBINPUT_DEVICE_CAP_TOUCH)) { struct wlr_input_device *wlr_dev = allocate_device(backend, libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TOUCH); if (!wlr_dev) { @@ -124,7 +135,8 @@ static void handle_device_added(struct wlr_libinput_backend *backend, } wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev); } - if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { + if (libinput_device_has_capability(libinput_dev, + LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { struct wlr_input_device *wlr_dev = allocate_device(backend, libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_TOOL); if (!wlr_dev) { @@ -137,7 +149,8 @@ static void handle_device_added(struct wlr_libinput_backend *backend, } wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev); } - if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_PAD)) { + if (libinput_device_has_capability( + libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_PAD)) { struct wlr_input_device *wlr_dev = allocate_device(backend, libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_PAD); if (!wlr_dev) { @@ -150,14 +163,16 @@ static void handle_device_added(struct wlr_libinput_backend *backend, } wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev); } - if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_GESTURE)) { + if (libinput_device_has_capability( + libinput_dev, LIBINPUT_DEVICE_CAP_GESTURE)) { // TODO } - if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_SWITCH)) { + if (libinput_device_has_capability( + libinput_dev, LIBINPUT_DEVICE_CAP_SWITCH)) { // TODO } - if (wl_list_length(wlr_devices) > 0) { + if (!wl_list_empty(wlr_devices)) { libinput_device_set_user_data(libinput_dev, wlr_devices); wlr_list_push(&backend->wlr_device_lists, wlr_devices); } else { @@ -199,7 +214,6 @@ static void handle_device_removed(struct wlr_libinput_backend *backend, void handle_libinput_event(struct wlr_libinput_backend *backend, struct libinput_event *event) { - assert(backend && event); struct libinput_device *libinput_dev = libinput_event_get_device(event); enum libinput_event_type event_type = libinput_event_get_type(event); switch (event_type) { diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index d0059e06..93605e77 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -12,35 +12,43 @@ struct wlr_libinput_keyboard { struct libinput_device *libinput_dev; }; +static const struct wlr_keyboard_impl impl; + +static struct wlr_libinput_keyboard *get_libinput_keyboard_from_keyboard( + struct wlr_keyboard *wlr_kb) { + assert(wlr_kb->impl == &impl); + return (struct wlr_libinput_keyboard *)wlr_kb; +} + static void keyboard_set_leds(struct wlr_keyboard *wlr_kb, uint32_t leds) { - struct wlr_libinput_keyboard *wlr_libinput_kb = - (struct wlr_libinput_keyboard *)wlr_kb; - libinput_device_led_update(wlr_libinput_kb->libinput_dev, leds); + struct wlr_libinput_keyboard *kb = + get_libinput_keyboard_from_keyboard(wlr_kb); + libinput_device_led_update(kb->libinput_dev, leds); } static void keyboard_destroy(struct wlr_keyboard *wlr_kb) { - struct wlr_libinput_keyboard *wlr_libinput_kb = - (struct wlr_libinput_keyboard *)wlr_kb; - libinput_device_unref(wlr_libinput_kb->libinput_dev); - free(wlr_libinput_kb); + struct wlr_libinput_keyboard *kb = + get_libinput_keyboard_from_keyboard(wlr_kb); + libinput_device_unref(kb->libinput_dev); + free(kb); } -struct wlr_keyboard_impl impl = { +static const struct wlr_keyboard_impl impl = { .destroy = keyboard_destroy, .led_update = keyboard_set_leds }; struct wlr_keyboard *create_libinput_keyboard( struct libinput_device *libinput_dev) { - assert(libinput_dev); - struct wlr_libinput_keyboard *wlr_libinput_kb; - if (!(wlr_libinput_kb= calloc(1, sizeof(struct wlr_libinput_keyboard)))) { + struct wlr_libinput_keyboard *kb = + calloc(1, sizeof(struct wlr_libinput_keyboard)); + if (kb == NULL) { return NULL; } - wlr_libinput_kb->libinput_dev = libinput_dev; + kb->libinput_dev = libinput_dev; libinput_device_ref(libinput_dev); libinput_device_led_update(libinput_dev, 0); - struct wlr_keyboard *wlr_kb = &wlr_libinput_kb->wlr_keyboard; + struct wlr_keyboard *wlr_kb = &kb->wlr_keyboard; wlr_keyboard_init(wlr_kb, &impl); return wlr_kb; } @@ -50,7 +58,8 @@ void handle_keyboard_key(struct libinput_event *event, struct wlr_input_device *wlr_dev = get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, libinput_dev); if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a keyboard event for a device with no keyboards?"); + wlr_log(WLR_DEBUG, + "Got a keyboard event for a device with no keyboards?"); return; } struct libinput_event_keyboard *kbevent = diff --git a/backend/libinput/tablet_pad.c b/backend/libinput/tablet_pad.c index 0642f925..b053b9a0 100644 --- a/backend/libinput/tablet_pad.c +++ b/backend/libinput/tablet_pad.c @@ -69,7 +69,8 @@ static void add_pad_group_from_libinput(struct wlr_tablet_pad *pad, struct wlr_tablet_pad *create_libinput_tablet_pad( struct libinput_device *libinput_dev) { assert(libinput_dev); - struct wlr_tablet_pad *wlr_tablet_pad = calloc(1, sizeof(struct wlr_tablet_pad)); + struct wlr_tablet_pad *wlr_tablet_pad = + calloc(1, sizeof(struct wlr_tablet_pad)); if (!wlr_tablet_pad) { wlr_log(WLR_ERROR, "Unable to allocate wlr_tablet_pad"); return NULL; @@ -101,7 +102,8 @@ void handle_tablet_pad_button(struct libinput_event *event, struct wlr_input_device *wlr_dev = get_appropriate_device(WLR_INPUT_DEVICE_TABLET_PAD, libinput_dev); if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a tablet pad event for a device with no tablet pad?"); + wlr_log(WLR_DEBUG, + "Got a tablet pad event for a device with no tablet pad?"); return; } struct libinput_event_tablet_pad *pevent = @@ -129,7 +131,8 @@ void handle_tablet_pad_ring(struct libinput_event *event, struct wlr_input_device *wlr_dev = get_appropriate_device(WLR_INPUT_DEVICE_TABLET_PAD, libinput_dev); if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a tablet pad event for a device with no tablet pad?"); + wlr_log(WLR_DEBUG, + "Got a tablet pad event for a device with no tablet pad?"); return; } struct libinput_event_tablet_pad *pevent = @@ -156,7 +159,8 @@ void handle_tablet_pad_strip(struct libinput_event *event, struct wlr_input_device *wlr_dev = get_appropriate_device(WLR_INPUT_DEVICE_TABLET_PAD, libinput_dev); if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a tablet pad event for a device with no tablet pad?"); + wlr_log(WLR_DEBUG, + "Got a tablet pad event for a device with no tablet pad?"); return; } struct libinput_event_tablet_pad *pevent = diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c index adfc6221..4e87b700 100644 --- a/backend/libinput/tablet_tool.c +++ b/backend/libinput/tablet_tool.c @@ -156,7 +156,8 @@ static struct wlr_libinput_tablet_tool *get_wlr_tablet_tool( static void ensure_tool_reference(struct wlr_libinput_tablet_tool *tool, struct wlr_tablet *wlr_dev) { assert(tablet_is_libinput(wlr_dev)); - struct wlr_libinput_tablet *tablet = wl_container_of(wlr_dev, tablet, wlr_tablet); + struct wlr_libinput_tablet *tablet = + wl_container_of(wlr_dev, tablet, wlr_tablet); struct tablet_tool_list_elem *pos; wl_list_for_each(pos, &tablet->tools, link) { @@ -188,7 +189,8 @@ void handle_tablet_tool_axis(struct libinput_event *event, struct wlr_input_device *wlr_dev = get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, libinput_dev); if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + wlr_log(WLR_DEBUG, + "Got a tablet tool event for a device with no tablet tools?"); return; } struct libinput_event_tablet_tool *tevent = @@ -248,7 +250,8 @@ void handle_tablet_tool_proximity(struct libinput_event *event, struct wlr_input_device *wlr_dev = get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, libinput_dev); if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + wlr_log(WLR_DEBUG, + "Got a tablet tool event for a device with no tablet tools?"); return; } struct libinput_event_tablet_tool *tevent = @@ -272,14 +275,16 @@ void handle_tablet_tool_proximity(struct libinput_event *event, } wlr_signal_emit_safe(&wlr_dev->tablet->events.proximity, &wlr_event); - if (libinput_event_tablet_tool_get_proximity_state(tevent) == LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN) { + if (libinput_event_tablet_tool_get_proximity_state(tevent) == + LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN) { handle_tablet_tool_axis(event, libinput_dev); } // If the tool is not unique, libinput will not find it again after the // proximity out, so we should destroy it if (!tool->unique && - libinput_event_tablet_tool_get_proximity_state(tevent) == LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT) { + libinput_event_tablet_tool_get_proximity_state(tevent) == + LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT) { // The tool isn't unique, it can't be on multiple tablets assert(tool->pad_refs == 1); assert(tablet_is_libinput(wlr_dev->tablet)); @@ -305,7 +310,8 @@ void handle_tablet_tool_tip(struct libinput_event *event, struct wlr_input_device *wlr_dev = get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, libinput_dev); if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + wlr_log(WLR_DEBUG, + "Got a tablet tool event for a device with no tablet tools?"); return; } handle_tablet_tool_axis(event, libinput_dev); @@ -336,7 +342,8 @@ void handle_tablet_tool_button(struct libinput_event *event, struct wlr_input_device *wlr_dev = get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, libinput_dev); if (!wlr_dev) { - wlr_log(WLR_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + wlr_log(WLR_DEBUG, + "Got a tablet tool event for a device with no tablet tools?"); return; } handle_tablet_tool_axis(event, libinput_dev); diff --git a/include/backend/libinput.h b/include/backend/libinput.h index d1fb3a44..9531091f 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include