From 0e75d157f52db45a1af350574bd95cccbd09fa57 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 9 Jun 2017 17:31:21 -0400 Subject: Initialize keyboards from libinput --- backend/libinput/events.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 backend/libinput/events.c (limited to 'backend/libinput/events.c') diff --git a/backend/libinput/events.c b/backend/libinput/events.c new file mode 100644 index 00000000..b4816928 --- /dev/null +++ b/backend/libinput/events.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput/backend.h" +#include "common/log.h" +#include "types.h" + +static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) { + free(state); +} + +static struct wlr_keyboard_impl keyboard_impl = { + .destroy = wlr_libinput_keyboard_destroy +}; + +static struct wlr_keyboard *wlr_libinput_keyboard_create( + struct libinput_device *device) { + assert(device); + struct wlr_keyboard_state *kbstate = + calloc(1, sizeof(struct wlr_keyboard_state)); + kbstate->handle = device; + return wlr_keyboard_create(&keyboard_impl, kbstate); +} + +static void device_added(struct wlr_backend_state *state, + struct libinput_device *device) { + assert(state && device); + /* + * 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. + * However, libinput devices often have multiple capabilities - in such + * cases we have to create several devices. + */ + int vendor = libinput_device_get_id_vendor(device); + int product = libinput_device_get_id_product(device); + const char *name = libinput_device_get_name(device); + list_t *devices = list_create(); + wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); + + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { + struct wlr_input_device *wlr_device = wlr_input_device_create( + WLR_INPUT_DEVICE_KEYBOARD, name, vendor, product); + wlr_device->keyboard = wlr_libinput_keyboard_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); + list_add(devices, wlr_device); + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { + // TODO + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { + // TODO + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { + // TODO + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_PAD)) { + // TODO + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_GESTURE)) { + // TODO + } + if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_SWITCH)) { + // TODO + } + + if (devices->length > 0) { + libinput_device_set_user_data(device, devices); + } else { + list_free(devices); + } +} + +static void device_removed(struct wlr_backend_state *state, + struct libinput_device *device) { + wlr_log(L_DEBUG, "libinput device removed"); + // TODO +} + +void wlr_libinput_event(struct wlr_backend_state *state, + struct libinput_event *event) { + assert(state && event); + struct libinput *context = libinput_event_get_context(event); + struct libinput_device *device = libinput_event_get_device(event); + enum libinput_event_type event_type = libinput_event_get_type(event); + (void)context; + switch (event_type) { + case LIBINPUT_EVENT_DEVICE_ADDED: + device_added(state, device); + break; + case LIBINPUT_EVENT_DEVICE_REMOVED: + device_removed(state, device); + break; + default: + wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); + break; + } +} -- cgit v1.2.3 From 59ceaf507e9a7c80e04460d1ae4946ce7b6f56ac Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 9 Jun 2017 17:52:11 -0400 Subject: Handle key presses --- backend/libinput/backend.c | 14 +++++++++--- backend/libinput/events.c | 56 ++++++++++++++++++++++++++++++++++++++++++---- example/simple.c | 9 ++++++++ include/wlr/types.h | 26 ++++++++++++++++++--- 4 files changed, 95 insertions(+), 10 deletions(-) (limited to 'backend/libinput/events.c') diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 3af08875..dc018b10 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -99,7 +99,12 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, struct wlr_backend *backend = wlr_backend_create(&backend_impl, state); if (!backend) { wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); - return NULL; + goto error_state; + } + + if (!(state->keyboards = list_create())) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + goto error_backend; } state->backend = backend; @@ -107,7 +112,10 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, state->udev = udev; state->display = display; - state->keyboards = list_create(); - return backend; +error_state: + free(state); +error_backend: + wlr_backend_destroy(backend); + return NULL; } diff --git a/backend/libinput/events.c b/backend/libinput/events.c index b4816928..c31632b5 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -8,6 +8,22 @@ #include "common/log.h" #include "types.h" +static struct wlr_input_device *get_appropriate_device( + enum wlr_input_device_type desired_type, + struct libinput_device *device) { + list_t *devices = libinput_device_get_user_data(device); + if (!devices) { + return NULL; + } + for (size_t i = 0; i < devices->length; ++i) { + struct wlr_input_device *dev = devices->items[i]; + if (dev->type == desired_type) { + return dev; + } + } + return NULL; +} + static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) { free(state); } @@ -22,10 +38,39 @@ static struct wlr_keyboard *wlr_libinput_keyboard_create( struct wlr_keyboard_state *kbstate = calloc(1, sizeof(struct wlr_keyboard_state)); kbstate->handle = device; + libinput_device_ref(device); return wlr_keyboard_create(&keyboard_impl, kbstate); } -static void device_added(struct wlr_backend_state *state, +static void handle_keyboard_key(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a keyboard event for a device with no keyboards?"); + return; + } + struct libinput_event_keyboard *kbevent = + libinput_event_get_keyboard_event(event); + struct wlr_keyboard_key *wlr_event = + calloc(1, sizeof(struct wlr_keyboard_key)); + wlr_event->time_sec = libinput_event_keyboard_get_time(kbevent); + wlr_event->time_usec = libinput_event_keyboard_get_time_usec(kbevent); + wlr_event->keycode = libinput_event_keyboard_get_key(kbevent); + enum libinput_key_state state = + libinput_event_keyboard_get_key_state(kbevent); + switch (state) { + case LIBINPUT_KEY_STATE_RELEASED: + wlr_event->state = WLR_KEY_RELEASED; + break; + case LIBINPUT_KEY_STATE_PRESSED: + wlr_event->state = WLR_KEY_PRESSED; + break; + } + wl_signal_emit(&dev->keyboard->events.key, wlr_event); +} + +static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); /* @@ -73,7 +118,7 @@ static void device_added(struct wlr_backend_state *state, } } -static void device_removed(struct wlr_backend_state *state, +static void handle_device_removed(struct wlr_backend_state *state, struct libinput_device *device) { wlr_log(L_DEBUG, "libinput device removed"); // TODO @@ -88,10 +133,13 @@ void wlr_libinput_event(struct wlr_backend_state *state, (void)context; switch (event_type) { case LIBINPUT_EVENT_DEVICE_ADDED: - device_added(state, device); + handle_device_added(state, device); break; case LIBINPUT_EVENT_DEVICE_REMOVED: - device_removed(state, device); + handle_device_removed(state, device); + break; + case LIBINPUT_EVENT_KEYBOARD_KEY: + handle_keyboard_key(event, device); break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); diff --git a/example/simple.c b/example/simple.c index 3206b3fa..660c56a6 100644 --- a/example/simple.c +++ b/example/simple.c @@ -97,6 +97,13 @@ void output_remove(struct wl_listener *listener, void *data) { wl_list_remove(&ostate->frame.link); } +static void keyboard_key(struct wl_listener *listener, void *data) { + struct wlr_keyboard_key *event = data; + struct keyboard_state *kbstate = wl_container_of(listener, kbstate, key); + fprintf(stderr, "Key event: %u %s\n", event->keycode, + event->state == WLR_KEY_PRESSED ? "pressed" : "released"); +} + void input_add(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct state *state = wl_container_of(listener, state, input_add); @@ -109,6 +116,8 @@ void input_add(struct wl_listener *listener, void *data) { kbstate->device = device; wl_list_init(&kbstate->key.link); wl_list_init(&kbstate->mods.link); + kbstate->key.notify = keyboard_key; + wl_signal_add(&device->keyboard->events.key, &kbstate->key); wl_list_insert(&state->keyboards, &kbstate->link); } diff --git a/include/wlr/types.h b/include/wlr/types.h index a65b5d9c..e2f3075f 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -65,6 +65,18 @@ struct wlr_keyboard { } events; }; +enum wlr_key_state { + WLR_KEY_RELEASED, + WLR_KEY_PRESSED, +}; + +struct wlr_keyboard_key { + uint32_t time_sec; + uint64_t time_usec; + uint32_t keycode; + enum wlr_key_state state; +}; + struct wlr_pointer_state; struct wlr_pointer_impl; @@ -81,20 +93,26 @@ struct wlr_pointer { }; struct wlr_pointer_motion { + uint32_t time_sec; + uint64_t time_usec; double delta_x, delta_y; }; struct wlr_pointer_motion_absolute { + uint32_t time_sec; + uint64_t time_usec; double x_mm, y_mm; double width_mm, height_mm; }; enum wlr_button_state { - WLR_BUTTON_DEPRESSED, - WLR_BUTTON_RELEASED + WLR_BUTTON_RELEASED, + WLR_BUTTON_PRESSED, }; struct wlr_pointer_button { + uint32_t time_sec; + uint64_t time_usec; uint32_t button; enum wlr_button_state state; }; @@ -108,10 +126,12 @@ enum wlr_axis_source { enum wlr_axis_orientation { WLR_AXIS_ORIENTATION_VERTICAL, - WLR_AXIS_ORIENTATION_HORIZONTAL + WLR_AXIS_ORIENTATION_HORIZONTAL, }; struct wlr_pointer_axis { + uint32_t time_sec; + uint64_t time_usec; enum wlr_axis_source source; enum wlr_axis_orientation orientation; double delta; -- cgit v1.2.3 From f479b7c8c7aa93229c46287f774a30ac8324da1e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 10 Jun 2017 11:58:25 -0400 Subject: Split keyboard code out into its own file --- backend/CMakeLists.txt | 1 + backend/libinput/backend.c | 2 +- backend/libinput/events.c | 50 ++-------------------------------- backend/libinput/keyboard.c | 56 ++++++++++++++++++++++++++++++++++++++ include/backend/libinput.h | 38 ++++++++++++++++++++++++++ include/backend/libinput/backend.h | 28 ------------------- 6 files changed, 98 insertions(+), 77 deletions(-) create mode 100644 backend/libinput/keyboard.c create mode 100644 include/backend/libinput.h delete mode 100644 include/backend/libinput/backend.h (limited to 'backend/libinput/events.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index e22032c3..5505e417 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -16,6 +16,7 @@ add_library(wlr-backend libinput/backend.c libinput/events.c + libinput/keyboard.c backend.c egl.c diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index dc018b10..9b0a6d8c 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -4,7 +4,7 @@ #include #include #include "backend/udev.h" -#include "backend/libinput/backend.h" +#include "backend/libinput.h" #include "common/log.h" static int wlr_libinput_open_restricted(const char *path, diff --git a/backend/libinput/events.c b/backend/libinput/events.c index c31632b5..3c623de4 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -4,11 +4,11 @@ #include #include #include -#include "backend/libinput/backend.h" +#include "backend/libinput.h" #include "common/log.h" #include "types.h" -static struct wlr_input_device *get_appropriate_device( +struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, struct libinput_device *device) { list_t *devices = libinput_device_get_user_data(device); @@ -24,52 +24,6 @@ static struct wlr_input_device *get_appropriate_device( return NULL; } -static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) { - free(state); -} - -static struct wlr_keyboard_impl keyboard_impl = { - .destroy = wlr_libinput_keyboard_destroy -}; - -static struct wlr_keyboard *wlr_libinput_keyboard_create( - struct libinput_device *device) { - assert(device); - struct wlr_keyboard_state *kbstate = - calloc(1, sizeof(struct wlr_keyboard_state)); - kbstate->handle = device; - libinput_device_ref(device); - return wlr_keyboard_create(&keyboard_impl, kbstate); -} - -static void handle_keyboard_key(struct libinput_event *event, - struct libinput_device *device) { - struct wlr_input_device *dev = - get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, device); - if (!dev) { - wlr_log(L_DEBUG, "Got a keyboard event for a device with no keyboards?"); - return; - } - struct libinput_event_keyboard *kbevent = - libinput_event_get_keyboard_event(event); - struct wlr_keyboard_key *wlr_event = - calloc(1, sizeof(struct wlr_keyboard_key)); - wlr_event->time_sec = libinput_event_keyboard_get_time(kbevent); - wlr_event->time_usec = libinput_event_keyboard_get_time_usec(kbevent); - wlr_event->keycode = libinput_event_keyboard_get_key(kbevent); - enum libinput_key_state state = - libinput_event_keyboard_get_key_state(kbevent); - switch (state) { - case LIBINPUT_KEY_STATE_RELEASED: - wlr_event->state = WLR_KEY_RELEASED; - break; - case LIBINPUT_KEY_STATE_PRESSED: - wlr_event->state = WLR_KEY_PRESSED; - break; - } - wl_signal_emit(&dev->keyboard->events.key, wlr_event); -} - static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c new file mode 100644 index 00000000..afae960e --- /dev/null +++ b/backend/libinput/keyboard.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) { + libinput_device_unref(state->handle); + free(state); +} + +static struct wlr_keyboard_impl keyboard_impl = { + .destroy = wlr_libinput_keyboard_destroy +}; + +struct wlr_keyboard *wlr_libinput_keyboard_create( + struct libinput_device *device) { + assert(device); + struct wlr_keyboard_state *kbstate = + calloc(1, sizeof(struct wlr_keyboard_state)); + kbstate->handle = device; + libinput_device_ref(device); + return wlr_keyboard_create(&keyboard_impl, kbstate); +} + +void handle_keyboard_key(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a keyboard event for a device with no keyboards?"); + return; + } + struct libinput_event_keyboard *kbevent = + libinput_event_get_keyboard_event(event); + struct wlr_keyboard_key *wlr_event = + calloc(1, sizeof(struct wlr_keyboard_key)); + wlr_event->time_sec = libinput_event_keyboard_get_time(kbevent); + wlr_event->time_usec = libinput_event_keyboard_get_time_usec(kbevent); + wlr_event->keycode = libinput_event_keyboard_get_key(kbevent); + enum libinput_key_state state = + libinput_event_keyboard_get_key_state(kbevent); + switch (state) { + case LIBINPUT_KEY_STATE_RELEASED: + wlr_event->state = WLR_KEY_RELEASED; + break; + case LIBINPUT_KEY_STATE_PRESSED: + wlr_event->state = WLR_KEY_PRESSED; + break; + } + wl_signal_emit(&dev->keyboard->events.key, wlr_event); +} diff --git a/include/backend/libinput.h b/include/backend/libinput.h new file mode 100644 index 00000000..a59ab401 --- /dev/null +++ b/include/backend/libinput.h @@ -0,0 +1,38 @@ +#ifndef _WLR_BACKEND_LIBINPUT_INTERNAL_H +#define _WLR_BACKEND_LIBINPUT_INTERNAL_H +#include +#include +#include +#include +#include "backend/udev.h" +#include "types.h" + +struct wlr_backend_state { + struct wlr_backend *backend; + struct wlr_session *session; + struct wlr_udev *udev; + struct wl_display *display; + + struct libinput *libinput; + struct wl_event_source *input_event; + + list_t *keyboards; +}; + +void wlr_libinput_event(struct wlr_backend_state *state, + struct libinput_event *event); + +struct wlr_input_device *get_appropriate_device( + enum wlr_input_device_type desired_type, + struct libinput_device *device); + +struct wlr_keyboard_state { + struct libinput_device *handle; +}; + +void handle_keyboard_key(struct libinput_event *event, + struct libinput_device *device); +struct wlr_keyboard *wlr_libinput_keyboard_create( + struct libinput_device *device); + +#endif diff --git a/include/backend/libinput/backend.h b/include/backend/libinput/backend.h deleted file mode 100644 index 6f2fa2a0..00000000 --- a/include/backend/libinput/backend.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef _WLR_BACKEND_LIBINPUT_INTERNAL_H -#define _WLR_BACKEND_LIBINPUT_INTERNAL_H -#include -#include -#include -#include -#include "backend/udev.h" - -struct wlr_backend_state { - struct wlr_backend *backend; - struct wlr_session *session; - struct wlr_udev *udev; - struct wl_display *display; - - struct libinput *libinput; - struct wl_event_source *input_event; - - list_t *keyboards; -}; - -void wlr_libinput_event(struct wlr_backend_state *state, - struct libinput_event *event); - -struct wlr_keyboard_state { - struct libinput_device *handle; -}; - -#endif -- cgit v1.2.3 From 7a5f35b5bb2a30e46defe9ea1bd610cb06df95e8 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 10 Jun 2017 12:21:54 -0400 Subject: Reassign ownership of libinput handle --- backend/backend.c | 6 ++++++ backend/libinput/events.c | 15 ++++++++++++++- backend/libinput/keyboard.c | 15 +-------------- include/backend/libinput.h | 8 ++++---- include/types.h | 10 ++++++++-- include/wlr/backend/libinput.h | 3 +++ include/wlr/types.h | 7 +++++++ types/wlr_input_device.c | 23 +++++++++++++++++++++-- 8 files changed, 64 insertions(+), 23 deletions(-) (limited to 'backend/libinput/events.c') diff --git a/backend/backend.c b/backend/backend.c index 83249a8c..03c662c3 100644 --- a/backend/backend.c +++ b/backend/backend.c @@ -3,10 +3,12 @@ #include #include #include +#include #include #include #include #include +#include "backend/libinput.h" #include "backend/udev.h" #include "common/log.h" @@ -64,3 +66,7 @@ error_udev: error: return NULL; } + +struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *dev) { + return dev->state->handle; +} diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 3c623de4..60a2fd5e 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -24,6 +24,15 @@ struct wlr_input_device *get_appropriate_device( return NULL; } +static void wlr_libinput_device_destroy(struct wlr_input_device_state *state) { + libinput_device_unref(state->handle); + free(state); +} + +static struct wlr_input_device_impl input_device_impl = { + .destroy = wlr_libinput_device_destroy +}; + static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); @@ -38,10 +47,13 @@ static void handle_device_added(struct wlr_backend_state *state, const char *name = libinput_device_get_name(device); list_t *devices = list_create(); wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { struct wlr_input_device *wlr_device = wlr_input_device_create( - WLR_INPUT_DEVICE_KEYBOARD, name, vendor, product); + WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate, + name, vendor, product); wlr_device->keyboard = wlr_libinput_keyboard_create(device); wl_signal_emit(&state->backend->events.input_add, wlr_device); list_add(devices, wlr_device); @@ -68,6 +80,7 @@ static void handle_device_added(struct wlr_backend_state *state, if (devices->length > 0) { libinput_device_set_user_data(device, devices); } else { + wlr_libinput_device_destroy(devstate); list_free(devices); } } diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index afae960e..158ded28 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -8,23 +8,10 @@ #include "common/log.h" #include "types.h" -static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) { - libinput_device_unref(state->handle); - free(state); -} - -static struct wlr_keyboard_impl keyboard_impl = { - .destroy = wlr_libinput_keyboard_destroy -}; - struct wlr_keyboard *wlr_libinput_keyboard_create( struct libinput_device *device) { assert(device); - struct wlr_keyboard_state *kbstate = - calloc(1, sizeof(struct wlr_keyboard_state)); - kbstate->handle = device; - libinput_device_ref(device); - return wlr_keyboard_create(&keyboard_impl, kbstate); + return wlr_keyboard_create(NULL, NULL); } void handle_keyboard_key(struct libinput_event *event, diff --git a/include/backend/libinput.h b/include/backend/libinput.h index a59ab401..f6622695 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -19,6 +19,10 @@ struct wlr_backend_state { list_t *keyboards; }; +struct wlr_input_device_state { + struct libinput_device *handle; +}; + void wlr_libinput_event(struct wlr_backend_state *state, struct libinput_event *event); @@ -26,10 +30,6 @@ struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, struct libinput_device *device); -struct wlr_keyboard_state { - struct libinput_device *handle; -}; - void handle_keyboard_key(struct libinput_event *event, struct libinput_device *device); struct wlr_keyboard *wlr_libinput_keyboard_create( diff --git a/include/types.h b/include/types.h index 564b24ec..20f5ed13 100644 --- a/include/types.h +++ b/include/types.h @@ -34,9 +34,15 @@ struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, struct wlr_pointer_state *state); void wlr_pointer_destroy(struct wlr_pointer *pointer); +struct wlr_input_device_impl { + void (*destroy)(struct wlr_input_device_state *state); +}; + struct wlr_input_device *wlr_input_device_create( - enum wlr_input_device_type type, const char *name, - int vendor, int product); + enum wlr_input_device_type type, + struct wlr_input_device_impl *impl, + struct wlr_input_device_state *state, + const char *name, int vendor, int product); void wlr_input_device_destroy(struct wlr_input_device *dev); #endif diff --git a/include/wlr/backend/libinput.h b/include/wlr/backend/libinput.h index 1e8c3555..7108f42c 100644 --- a/include/wlr/backend/libinput.h +++ b/include/wlr/backend/libinput.h @@ -1,12 +1,15 @@ #ifndef WLR_BACKEND_LIBINPUT_H #define WLR_BACKEND_LIBINPUT_H +#include #include #include #include #include +#include struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, struct wlr_session *session, struct wlr_udev *udev); +struct libinput_device *wlr_libinput_get_device_handle(struct wlr_input_device *dev); #endif diff --git a/include/wlr/types.h b/include/wlr/types.h index e2f3075f..389989e8 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -152,10 +152,17 @@ enum wlr_input_device_type { WLR_INPUT_DEVICE_SWITCH, }; +struct wlr_input_device_state; +struct wlr_input_device_impl; + struct wlr_input_device { + struct wlr_input_device_state *state; + struct wlr_input_device_impl *impl; + enum wlr_input_device_type type; int vendor, product; char *name; + union { void *_device; struct wlr_keyboard *keyboard; diff --git a/types/wlr_input_device.c b/types/wlr_input_device.c index 7cbe2f7e..dbb25487 100644 --- a/types/wlr_input_device.c +++ b/types/wlr_input_device.c @@ -4,13 +4,18 @@ #include #include #include +#include "common/log.h" #include "types.h" struct wlr_input_device *wlr_input_device_create( - enum wlr_input_device_type type, const char *name, - int vendor, int product) { + enum wlr_input_device_type type, + struct wlr_input_device_impl *impl, + struct wlr_input_device_state *state, + const char *name, int vendor, int product) { struct wlr_input_device *dev = calloc(1, sizeof(struct wlr_input_device)); dev->type = type; + dev->impl = impl; + dev->state = state; dev->name = strdup(name); dev->vendor = vendor; dev->product = product; @@ -19,6 +24,20 @@ struct wlr_input_device *wlr_input_device_create( void wlr_input_device_destroy(struct wlr_input_device *dev) { if (!dev) return; + if (dev->impl && dev->impl->destroy && dev->state) { + dev->impl->destroy(dev->state); + } + if (dev->_device) { + switch (dev->type) { + case WLR_INPUT_DEVICE_KEYBOARD: + wlr_keyboard_destroy(dev->keyboard); + break; + default: + wlr_log(L_DEBUG, "Warning: leaking memory %p %p %d", + dev->_device, dev, dev->type); + break; + } + } free(dev->name); free(dev); } -- cgit v1.2.3 From 508d135de78a483feb947ee4cc5e979743bd7317 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 10 Jun 2017 12:24:35 -0400 Subject: Only allocate device state when it will be used This design also avoids double frees in the future when we're creating several wlr_input_devices from one libinput_device. --- backend/libinput/events.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'backend/libinput/events.c') diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 60a2fd5e..2ce368e3 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -47,10 +47,12 @@ static void handle_device_added(struct wlr_backend_state *state, const char *name = libinput_device_get_name(device); list_t *devices = list_create(); wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); - struct wlr_input_device_state *devstate = - calloc(1, sizeof(struct wlr_input_device_state)); if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); + devstate->handle = device; + libinput_device_ref(device); struct wlr_input_device *wlr_device = wlr_input_device_create( WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate, name, vendor, product); @@ -80,7 +82,6 @@ static void handle_device_added(struct wlr_backend_state *state, if (devices->length > 0) { libinput_device_set_user_data(device, devices); } else { - wlr_libinput_device_destroy(devstate); list_free(devices); } } -- cgit v1.2.3 From a63230e59cfdaad42c3444b2bc4be5480202e527 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Tue, 13 Jun 2017 10:27:15 -0400 Subject: Implement libinput wlr_pointer --- backend/CMakeLists.txt | 1 + backend/libinput/events.c | 43 +++++++++++---- backend/libinput/pointer.c | 129 +++++++++++++++++++++++++++++++++++++++++++++ example/rotation.c | 1 - include/backend/libinput.h | 13 ++++- types/CMakeLists.txt | 1 + types/wlr_pointer.c | 24 +++++++++ 7 files changed, 201 insertions(+), 11 deletions(-) create mode 100644 backend/libinput/pointer.c create mode 100644 types/wlr_pointer.c (limited to 'backend/libinput/events.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 23914240..b8df9373 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -17,6 +17,7 @@ add_library(wlr-backend libinput/backend.c libinput/events.c libinput/keyboard.c + libinput/pointer.c multi/backend.c backend.c diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 2ce368e3..5ef58a3e 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -33,6 +33,22 @@ static struct wlr_input_device_impl input_device_impl = { .destroy = wlr_libinput_device_destroy }; +static struct wlr_input_device *allocate_device(struct libinput_device *device, + list_t *devices, enum wlr_input_device_type type) { + int vendor = libinput_device_get_id_vendor(device); + int product = libinput_device_get_id_product(device); + const char *name = libinput_device_get_name(device); + struct wlr_input_device_state *devstate = + calloc(1, sizeof(struct wlr_input_device_state)); + devstate->handle = device; + libinput_device_ref(device); + struct wlr_input_device *wlr_device = wlr_input_device_create( + type, &input_device_impl, devstate, + name, vendor, product); + list_add(devices, wlr_device); + return wlr_device; +} + static void handle_device_added(struct wlr_backend_state *state, struct libinput_device *device) { assert(state && device); @@ -49,19 +65,16 @@ static void handle_device_added(struct wlr_backend_state *state, wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { - struct wlr_input_device_state *devstate = - calloc(1, sizeof(struct wlr_input_device_state)); - devstate->handle = device; - libinput_device_ref(device); - struct wlr_input_device *wlr_device = wlr_input_device_create( - WLR_INPUT_DEVICE_KEYBOARD, &input_device_impl, devstate, - name, vendor, product); + struct wlr_input_device *wlr_device = allocate_device(device, devices, + WLR_INPUT_DEVICE_KEYBOARD); wlr_device->keyboard = wlr_libinput_keyboard_create(device); wl_signal_emit(&state->backend->events.input_add, wlr_device); - list_add(devices, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { - // TODO + struct wlr_input_device *wlr_device = allocate_device(device, devices, + WLR_INPUT_DEVICE_POINTER); + wlr_device->pointer = wlr_libinput_pointer_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { // TODO @@ -109,6 +122,18 @@ void wlr_libinput_event(struct wlr_backend_state *state, case LIBINPUT_EVENT_KEYBOARD_KEY: handle_keyboard_key(event, device); break; + case LIBINPUT_EVENT_POINTER_MOTION: + handle_pointer_motion(event, device); + break; + case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE: + handle_pointer_motion_abs(event, device); + break; + case LIBINPUT_EVENT_POINTER_BUTTON: + handle_pointer_button(event, device); + break; + case LIBINPUT_EVENT_POINTER_AXIS: + handle_pointer_axis(event, device); + break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); break; diff --git a/backend/libinput/pointer.c b/backend/libinput/pointer.c new file mode 100644 index 00000000..0139fd60 --- /dev/null +++ b/backend/libinput/pointer.c @@ -0,0 +1,129 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_pointer *wlr_libinput_pointer_create( + struct libinput_device *device) { + assert(device); + return wlr_pointer_create(NULL, NULL); +} + +void handle_pointer_motion(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_motion *wlr_event = + calloc(1, sizeof(struct wlr_pointer_motion)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->delta_x = libinput_event_pointer_get_dx(pevent); + wlr_event->delta_y = libinput_event_pointer_get_dy(pevent); + wl_signal_emit(&dev->pointer->events.motion, wlr_event); +} + +void handle_pointer_motion_abs(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_motion_absolute *wlr_event = + calloc(1, sizeof(struct wlr_pointer_motion_absolute)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->x_mm = libinput_event_pointer_get_absolute_x(pevent); + wlr_event->y_mm = libinput_event_pointer_get_absolute_y(pevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->pointer->events.motion_absolute, wlr_event); +} + +void handle_pointer_button(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_button *wlr_event = + calloc(1, sizeof(struct wlr_pointer_button)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + wlr_event->button = libinput_event_pointer_get_button(pevent); + switch (libinput_event_pointer_get_button_state(pevent)) { + case LIBINPUT_BUTTON_STATE_PRESSED: + wlr_event->state = WLR_BUTTON_PRESSED; + break; + case LIBINPUT_BUTTON_STATE_RELEASED: + wlr_event->state = WLR_BUTTON_RELEASED; + break; + } + wl_signal_emit(&dev->pointer->events.button, wlr_event); +} + +void handle_pointer_axis(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_POINTER, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a pointer event for a device with no pointers?"); + return; + } + struct libinput_event_pointer *pevent = + libinput_event_get_pointer_event(event); + struct wlr_pointer_axis *wlr_event = + calloc(1, sizeof(struct wlr_pointer_axis)); + wlr_event->time_sec = libinput_event_pointer_get_time(pevent); + wlr_event->time_usec = libinput_event_pointer_get_time_usec(pevent); + switch (libinput_event_pointer_get_axis_source(pevent)) { + case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL: + wlr_event->source = WLR_AXIS_SOURCE_WHEEL; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_FINGER: + wlr_event->source = WLR_AXIS_SOURCE_FINGER; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS: + wlr_event->source = WLR_AXIS_SOURCE_CONTINUOUS; + break; + case LIBINPUT_POINTER_AXIS_SOURCE_WHEEL_TILT: + wlr_event->source = WLR_AXIS_SOURCE_WHEEL_TILT; + break; + } + enum libinput_pointer_axis axies[] = { + LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, + LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, + }; + for (size_t i = 0; i < sizeof(axies) / sizeof(axies[0]); ++i) { + if (libinput_event_pointer_has_axis(pevent, axies[i])) { + switch (axies[i]) { + case LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL: + wlr_event->orientation = WLR_AXIS_ORIENTATION_VERTICAL; + break; + case LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL: + wlr_event->orientation = WLR_AXIS_ORIENTATION_HORIZONTAL; + break; + } + wlr_event->delta = libinput_event_pointer_get_axis_value( + pevent, axies[i]); + } + wl_signal_emit(&dev->pointer->events.axis, wlr_event); + } +} diff --git a/example/rotation.c b/example/rotation.c index 2634ea03..892083e6 100644 --- a/example/rotation.c +++ b/example/rotation.c @@ -21,7 +21,6 @@ struct sample_state { struct wl_list config; - // TODO: Move renderer into shared struct wlr_renderer *renderer; struct wlr_surface *cat_texture; }; diff --git a/include/backend/libinput.h b/include/backend/libinput.h index f6622695..7c318746 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -30,9 +30,20 @@ struct wlr_input_device *get_appropriate_device( enum wlr_input_device_type desired_type, struct libinput_device *device); +struct wlr_keyboard *wlr_libinput_keyboard_create( + struct libinput_device *device); void handle_keyboard_key(struct libinput_event *event, struct libinput_device *device); -struct wlr_keyboard *wlr_libinput_keyboard_create( + +struct wlr_pointer *wlr_libinput_pointer_create( + struct libinput_device *device); +void handle_pointer_motion(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_motion_abs(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_button(struct libinput_event *event, + struct libinput_device *device); +void handle_pointer_axis(struct libinput_event *event, struct libinput_device *device); #endif diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 5ac1baf6..91ab9da0 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -6,6 +6,7 @@ include_directories( add_library(wlr-types wlr_output.c wlr_keyboard.c + wlr_pointer.c wlr_input_device.c ) diff --git a/types/wlr_pointer.c b/types/wlr_pointer.c new file mode 100644 index 00000000..b1364c34 --- /dev/null +++ b/types/wlr_pointer.c @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include +#include "types.h" + +struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, + struct wlr_pointer_state *state) { + struct wlr_pointer *pointer = calloc(1, sizeof(struct wlr_pointer)); + pointer->impl = impl; + pointer->state = state; + wl_signal_init(&pointer->events.motion); + wl_signal_init(&pointer->events.motion_absolute); + wl_signal_init(&pointer->events.button); + wl_signal_init(&pointer->events.axis); + return pointer; +} + +void wlr_pointer_destroy(struct wlr_pointer *kb) { + if (!kb) return; + kb->impl->destroy(kb->state); + free(kb); +} -- cgit v1.2.3 From d6905f86cb9d430e0ba05c6a066ed350761116d1 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 14 Jun 2017 11:40:03 -0400 Subject: Allocate wlr_touch devices --- backend/CMakeLists.txt | 1 + backend/libinput/backend.c | 2 +- backend/libinput/events.c | 17 +++++++++++------ backend/libinput/touch.c | 15 +++++++++++++++ include/backend/libinput.h | 15 ++++++++++++++- include/types.h | 8 ++++++++ include/wlr/types.h | 18 +++++++++++++++++- types/CMakeLists.txt | 1 + types/wlr_keyboard.c | 4 +++- types/wlr_pointer.c | 10 ++++++---- types/wlr_touch.c | 27 +++++++++++++++++++++++++++ 11 files changed, 104 insertions(+), 14 deletions(-) create mode 100644 backend/libinput/touch.c create mode 100644 types/wlr_touch.c (limited to 'backend/libinput/events.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index b8df9373..8272e039 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -18,6 +18,7 @@ add_library(wlr-backend libinput/events.c libinput/keyboard.c libinput/pointer.c + libinput/touch.c multi/backend.c backend.c diff --git a/backend/libinput/backend.c b/backend/libinput/backend.c index 9b0a6d8c..82ba44a6 100644 --- a/backend/libinput/backend.c +++ b/backend/libinput/backend.c @@ -102,7 +102,7 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display, goto error_state; } - if (!(state->keyboards = list_create())) { + if (!(state->devices = list_create())) { wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); goto error_backend; } diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 5ef58a3e..4af8082f 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -33,7 +33,8 @@ static struct wlr_input_device_impl input_device_impl = { .destroy = wlr_libinput_device_destroy }; -static struct wlr_input_device *allocate_device(struct libinput_device *device, +static struct wlr_input_device *allocate_device( + struct wlr_backend_state *state, struct libinput_device *device, list_t *devices, enum wlr_input_device_type type) { int vendor = libinput_device_get_id_vendor(device); int product = libinput_device_get_id_product(device); @@ -46,6 +47,7 @@ static struct wlr_input_device *allocate_device(struct libinput_device *device, type, &input_device_impl, devstate, name, vendor, product); list_add(devices, wlr_device); + list_add(state->devices, wlr_device); return wlr_device; } @@ -65,19 +67,22 @@ static void handle_device_added(struct wlr_backend_state *state, wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product); if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) { - struct wlr_input_device *wlr_device = allocate_device(device, devices, - WLR_INPUT_DEVICE_KEYBOARD); + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_KEYBOARD); wlr_device->keyboard = wlr_libinput_keyboard_create(device); wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) { - struct wlr_input_device *wlr_device = allocate_device(device, devices, - WLR_INPUT_DEVICE_POINTER); + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_POINTER); wlr_device->pointer = wlr_libinput_pointer_create(device); wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) { - // TODO + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_TOUCH); + wlr_device->touch = wlr_libinput_touch_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { // TODO diff --git a/backend/libinput/touch.c b/backend/libinput/touch.c new file mode 100644 index 00000000..55e8609a --- /dev/null +++ b/backend/libinput/touch.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_touch *wlr_libinput_touch_create( + struct libinput_device *device) { + assert(device); + return wlr_touch_create(NULL, NULL); +} diff --git a/include/backend/libinput.h b/include/backend/libinput.h index 7c318746..dd24fbb0 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -16,7 +16,7 @@ struct wlr_backend_state { struct libinput *libinput; struct wl_event_source *input_event; - list_t *keyboards; + list_t *devices; }; struct wlr_input_device_state { @@ -46,4 +46,17 @@ void handle_pointer_button(struct libinput_event *event, void handle_pointer_axis(struct libinput_event *event, struct libinput_device *device); +struct wlr_touch *wlr_libinput_touch_create( + struct libinput_device *device); +void handle_touch_down(struct libinput_event *event, + struct libinput_device *device); +void handle_touch_up(struct libinput_event *event, + struct libinput_device *device); +void handle_touch_motion(struct libinput_event *event, + struct libinput_device *device); +void handle_touch_cancel(struct libinput_event *event, + struct libinput_device *device); +void handle_touch_frame(struct libinput_event *event, + struct libinput_device *device); + #endif diff --git a/include/types.h b/include/types.h index 20f5ed13..5b20e2ed 100644 --- a/include/types.h +++ b/include/types.h @@ -34,6 +34,14 @@ struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, struct wlr_pointer_state *state); void wlr_pointer_destroy(struct wlr_pointer *pointer); +struct wlr_touch_impl { + void (*destroy)(struct wlr_touch_state *state); +}; + +struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl, + struct wlr_touch_state *state); +void wlr_touch_destroy(struct wlr_touch *touch); + struct wlr_input_device_impl { void (*destroy)(struct wlr_input_device_state *state); }; diff --git a/include/wlr/types.h b/include/wlr/types.h index af23152c..b4d69ccb 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -134,7 +134,22 @@ struct wlr_pointer_axis { double delta; }; -// TODO: touch +struct wlr_touch_state; +struct wlr_touch_impl; + +struct wlr_touch { + struct wlr_touch_state *state; + struct wlr_touch_impl *impl; + + struct { + struct wl_signal down; + struct wl_signal up; + struct wl_signal motion; + struct wl_signal cancel; + struct wl_signal frame; + } events; +}; + // TODO: tablet & tablet tool // TODO: gestures // TODO: switch @@ -164,6 +179,7 @@ struct wlr_input_device { void *_device; struct wlr_keyboard *keyboard; struct wlr_pointer *pointer; + struct wlr_touch *touch; }; }; diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index 91ab9da0..b158db39 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -7,6 +7,7 @@ add_library(wlr-types wlr_output.c wlr_keyboard.c wlr_pointer.c + wlr_touch.c wlr_input_device.c ) diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index db4f7ca7..32728c78 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -16,6 +16,8 @@ struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl, void wlr_keyboard_destroy(struct wlr_keyboard *kb) { if (!kb) return; - kb->impl->destroy(kb->state); + if (kb->impl) { + kb->impl->destroy(kb->state); + } free(kb); } diff --git a/types/wlr_pointer.c b/types/wlr_pointer.c index b1364c34..9c40fe2f 100644 --- a/types/wlr_pointer.c +++ b/types/wlr_pointer.c @@ -17,8 +17,10 @@ struct wlr_pointer *wlr_pointer_create(struct wlr_pointer_impl *impl, return pointer; } -void wlr_pointer_destroy(struct wlr_pointer *kb) { - if (!kb) return; - kb->impl->destroy(kb->state); - free(kb); +void wlr_pointer_destroy(struct wlr_pointer *pointer) { + if (!pointer) return; + if (pointer->impl) { + pointer->impl->destroy(pointer->state); + } + free(pointer); } diff --git a/types/wlr_touch.c b/types/wlr_touch.c new file mode 100644 index 00000000..9c8b4b94 --- /dev/null +++ b/types/wlr_touch.c @@ -0,0 +1,27 @@ +#include +#include +#include +#include +#include +#include "types.h" + +struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl, + struct wlr_touch_state *state) { + struct wlr_touch *touch = calloc(1, sizeof(struct wlr_touch)); + touch->impl = impl; + touch->state = state; + wl_signal_init(&touch->events.down); + wl_signal_init(&touch->events.up); + wl_signal_init(&touch->events.motion); + wl_signal_init(&touch->events.frame); + wl_signal_init(&touch->events.cancel); + return touch; +} + +void wlr_touch_destroy(struct wlr_touch *touch) { + if (!touch) return; + if (touch->impl) { + touch->impl->destroy(touch->state); + } + free(touch); +} -- cgit v1.2.3 From 3f24f8a1bee10fb3aadf8c57ca107fe5aaa7cffa Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 14 Jun 2017 14:50:09 -0400 Subject: Flesh out touch events and add demo --- backend/libinput/events.c | 15 ++++++ backend/libinput/touch.c | 78 +++++++++++++++++++++++++++++ example/CMakeLists.txt | 13 +++++ example/shared.c | 86 ++++++++++++++++++++++++++++++-- example/shared.h | 18 +++++++ example/touch.c | 122 +++++++++++++++++++++++++++++++++++++++++++++ include/backend/libinput.h | 2 - include/wlr/types.h | 29 ++++++++++- types/wlr_touch.c | 1 - 9 files changed, 356 insertions(+), 8 deletions(-) create mode 100644 example/touch.c (limited to 'backend/libinput/events.c') diff --git a/backend/libinput/events.c b/backend/libinput/events.c index 4af8082f..f8413dae 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -139,6 +139,21 @@ void wlr_libinput_event(struct wlr_backend_state *state, case LIBINPUT_EVENT_POINTER_AXIS: handle_pointer_axis(event, device); break; + case LIBINPUT_EVENT_TOUCH_DOWN: + handle_touch_down(event, device); + break; + case LIBINPUT_EVENT_TOUCH_UP: + handle_touch_up(event, device); + break; + case LIBINPUT_EVENT_TOUCH_MOTION: + handle_touch_motion(event, device); + break; + case LIBINPUT_EVENT_TOUCH_CANCEL: + handle_touch_cancel(event, device); + break; + case LIBINPUT_EVENT_TOUCH_FRAME: + // no-op (at least for now) + break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); break; diff --git a/backend/libinput/touch.c b/backend/libinput/touch.c index 55e8609a..02c9cfef 100644 --- a/backend/libinput/touch.c +++ b/backend/libinput/touch.c @@ -13,3 +13,81 @@ struct wlr_touch *wlr_libinput_touch_create( assert(device); return wlr_touch_create(NULL, NULL); } + +void handle_touch_down(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_down *wlr_event = + calloc(1, sizeof(struct wlr_touch_down)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wlr_event->x_mm = libinput_event_touch_get_x(tevent); + wlr_event->y_mm = libinput_event_touch_get_y(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->touch->events.down, wlr_event); +} + +void handle_touch_up(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_up *wlr_event = + calloc(1, sizeof(struct wlr_touch_up)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wl_signal_emit(&dev->touch->events.up, wlr_event); +} + +void handle_touch_motion(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_motion *wlr_event = + calloc(1, sizeof(struct wlr_touch_motion)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wlr_event->x_mm = libinput_event_touch_get_x(tevent); + wlr_event->y_mm = libinput_event_touch_get_y(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + wl_signal_emit(&dev->touch->events.motion, wlr_event); +} + +void handle_touch_cancel(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TOUCH, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a touch event for a device with no touch?"); + return; + } + struct libinput_event_touch *tevent = + libinput_event_get_touch_event(event); + struct wlr_touch_cancel *wlr_event = + calloc(1, sizeof(struct wlr_touch_cancel)); + wlr_event->time_sec = libinput_event_touch_get_time(tevent); + wlr_event->time_usec = libinput_event_touch_get_time_usec(tevent); + wlr_event->slot = libinput_event_touch_get_slot(tevent); + wl_signal_emit(&dev->touch->events.cancel, wlr_event); +} diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index ef11a822..a6085392 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -40,3 +40,16 @@ target_link_libraries(pointer wlr-render ${XKBCOMMON_LIBRARIES} ) + +add_executable(touch + touch.c + shared.c + cat.c +) + +target_link_libraries(touch + wlr-backend + wlr-session + wlr-render + ${XKBCOMMON_LIBRARIES} +) diff --git a/example/shared.c b/example/shared.c index 6de16074..2592c924 100644 --- a/example/shared.c +++ b/example/shared.c @@ -114,6 +114,59 @@ static void pointer_add(struct wlr_input_device *device, struct compositor_state wl_list_insert(&state->pointers, &pstate->link); } +static void touch_down_notify(struct wl_listener *listener, void *data) { + struct wlr_touch_down *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, down); + if (tstate->compositor->touch_down_cb) { + tstate->compositor->touch_down_cb(tstate, event->slot, + event->x_mm, event->y_mm, event->width_mm, event->height_mm); + } +} + +static void touch_motion_notify(struct wl_listener *listener, void *data) { + struct wlr_touch_motion *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, motion); + if (tstate->compositor->touch_motion_cb) { + tstate->compositor->touch_motion_cb(tstate, event->slot, + event->x_mm, event->y_mm, event->width_mm, event->height_mm); + } +} + +static void touch_up_notify(struct wl_listener *listener, void *data) { + struct wlr_touch_up *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, up); + if (tstate->compositor->touch_up_cb) { + tstate->compositor->touch_up_cb(tstate, event->slot); + } +} + +static void touch_cancel_notify(struct wl_listener *listener, void *data) { + struct wlr_touch_cancel *event = data; + struct touch_state *tstate = wl_container_of(listener, tstate, cancel); + if (tstate->compositor->touch_cancel_cb) { + tstate->compositor->touch_cancel_cb(tstate, event->slot); + } +} + +static void touch_add(struct wlr_input_device *device, struct compositor_state *state) { + struct touch_state *tstate = calloc(sizeof(struct touch_state), 1); + tstate->device = device; + tstate->compositor = state; + wl_list_init(&tstate->down.link); + wl_list_init(&tstate->motion.link); + wl_list_init(&tstate->up.link); + wl_list_init(&tstate->cancel.link); + tstate->down.notify = touch_down_notify; + tstate->motion.notify = touch_motion_notify; + tstate->up.notify = touch_up_notify; + tstate->cancel.notify = touch_cancel_notify; + wl_signal_add(&device->touch->events.down, &tstate->down); + wl_signal_add(&device->touch->events.motion, &tstate->motion); + wl_signal_add(&device->touch->events.up, &tstate->up); + wl_signal_add(&device->touch->events.cancel, &tstate->cancel); + wl_list_insert(&state->touch, &tstate->link); +} + static void input_add_notify(struct wl_listener *listener, void *data) { struct wlr_input_device *device = data; struct compositor_state *state = wl_container_of(listener, state, input_add); @@ -124,6 +177,9 @@ static void input_add_notify(struct wl_listener *listener, void *data) { case WLR_INPUT_DEVICE_POINTER: pointer_add(device, state); break; + case WLR_INPUT_DEVICE_TOUCH: + touch_add(device, state); + break; default: break; } @@ -156,10 +212,28 @@ static void pointer_remove(struct wlr_input_device *device, struct compositor_st return; } wl_list_remove(&pstate->link); - //wl_list_remove(&pstate->motion.link); - wl_list_remove(&pstate->motion_absolute.link); - //wl_list_remove(&pstate->button.link); - //wl_list_remove(&pstate->axis.link); + wl_list_remove(&pstate->motion.link); + //wl_list_remove(&pstate->motion_absolute.link); + wl_list_remove(&pstate->button.link); + wl_list_remove(&pstate->axis.link); +} + +static void touch_remove(struct wlr_input_device *device, struct compositor_state *state) { + struct touch_state *tstate = NULL, *_tstate; + wl_list_for_each(_tstate, &state->touch, link) { + if (_tstate->device == device) { + tstate = _tstate; + break; + } + } + if (!tstate) { + return; + } + wl_list_remove(&tstate->link); + wl_list_remove(&tstate->down.link); + wl_list_remove(&tstate->motion.link); + wl_list_remove(&tstate->up.link); + wl_list_remove(&tstate->cancel.link); } static void input_remove_notify(struct wl_listener *listener, void *data) { @@ -172,6 +246,9 @@ static void input_remove_notify(struct wl_listener *listener, void *data) { case WLR_INPUT_DEVICE_POINTER: pointer_remove(device, state); break; + case WLR_INPUT_DEVICE_TOUCH: + touch_remove(device, state); + break; default: break; } @@ -245,6 +322,7 @@ void compositor_init(struct compositor_state *state) { wl_list_init(&state->keyboards); wl_list_init(&state->pointers); + wl_list_init(&state->touch); wl_list_init(&state->input_add.link); state->input_add.notify = input_add_notify; wl_list_init(&state->input_remove.link); diff --git a/example/shared.h b/example/shared.h index 7f0dade5..d09bcd0e 100644 --- a/example/shared.h +++ b/example/shared.h @@ -39,6 +39,17 @@ struct pointer_state { void *data; }; +struct touch_state { + struct compositor_state *compositor; + struct wlr_input_device *device; + struct wl_listener down; + struct wl_listener up; + struct wl_listener motion; + struct wl_listener cancel; + struct wl_list link; + void *data; +}; + struct compositor_state { void (*output_add_cb)(struct output_state *s); void (*keyboard_add_cb)(struct keyboard_state *s); @@ -55,6 +66,12 @@ struct compositor_state { enum wlr_axis_source source, enum wlr_axis_orientation orientation, double delta); + void (*touch_down_cb)(struct touch_state *s, int32_t slot, + double x, double y, double width, double height); + void (*touch_motion_cb)(struct touch_state *s, int32_t slot, + double x, double y, double width, double height); + void (*touch_up_cb)(struct touch_state *s, int32_t slot); + void (*touch_cancel_cb)(struct touch_state *s, int32_t slot); struct wl_display *display; struct wl_event_loop *event_loop; @@ -63,6 +80,7 @@ struct compositor_state { struct wl_list keyboards; struct wl_list pointers; + struct wl_list touch; struct wl_listener input_add; struct wl_listener input_remove; diff --git a/example/touch.c b/example/touch.c new file mode 100644 index 00000000..7f4f3800 --- /dev/null +++ b/example/touch.c @@ -0,0 +1,122 @@ +#define _POSIX_C_SOURCE 199309L +#define _XOPEN_SOURCE 500 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "shared.h" +#include "cat.h" + +struct sample_state { + struct wlr_renderer *renderer; + struct wlr_surface *cat_texture; + list_t *touch_points; +}; + +struct touch_point { + int32_t slot; + double x, y; +}; + +static void handle_output_frame(struct output_state *output, struct timespec *ts) { + struct compositor_state *state = output->compositor; + struct sample_state *sample = state->data; + struct wlr_output *wlr_output = output->output; + + int32_t width, height; + wlr_output_effective_resolution(wlr_output, &width, &height); + wlr_renderer_begin(sample->renderer, wlr_output); + + float matrix[16]; + for (size_t i = 0; i < sample->touch_points->length; ++i) { + struct touch_point *p = sample->touch_points->items[i]; + wlr_surface_get_matrix(sample->cat_texture, &matrix, + &wlr_output->transform_matrix, + (int)(p->x * width) - sample->cat_texture->width / 2, + (int)(p->y * height) - sample->cat_texture->height / 2); + wlr_render_with_matrix(sample->renderer, + sample->cat_texture, &matrix); + } + + wlr_renderer_end(sample->renderer); +} + +static void handle_keyboard_key(struct keyboard_state *kbstate, + xkb_keysym_t sym, enum wlr_key_state key_state) { + if (sym == XKB_KEY_Escape) { + kbstate->compositor->exit = true; + } +} + +static void handle_touch_down(struct touch_state *tstate, int32_t slot, + double x, double y, double width, double height) { + struct sample_state *sample = tstate->compositor->data; + struct touch_point *point = calloc(1, sizeof(struct touch_state)); + point->slot = slot; + point->x = x / width; + point->y = y / height; + list_add(sample->touch_points, point); +} + +static void handle_touch_up(struct touch_state *tstate, int32_t slot) { + struct sample_state *sample = tstate->compositor->data; + for (size_t i = 0; i < sample->touch_points->length; ++i) { + struct touch_point *point = sample->touch_points->items[i]; + if (point->slot == slot) { + list_del(sample->touch_points, i); + break; + } + } +} + +static void handle_touch_motion(struct touch_state *tstate, int32_t slot, + double x, double y, double width, double height) { + struct sample_state *sample = tstate->compositor->data; + for (size_t i = 0; i < sample->touch_points->length; ++i) { + struct touch_point *point = sample->touch_points->items[i]; + if (point->slot == slot) { + point->x = x / width; + point->y = y / height; + break; + } + } +} + +int main(int argc, char *argv[]) { + struct sample_state state = { + .touch_points = list_create() + }; + struct compositor_state compositor; + + compositor_init(&compositor); + compositor.output_frame_cb = handle_output_frame; + compositor.keyboard_key_cb = handle_keyboard_key; + compositor.touch_down_cb = handle_touch_down; + compositor.touch_up_cb = handle_touch_up; + compositor.touch_motion_cb = handle_touch_motion; + + state.renderer = wlr_gles3_renderer_init(); + state.cat_texture = wlr_render_surface_init(state.renderer); + wlr_surface_attach_pixels(state.cat_texture, GL_RGB, + cat_tex.width, cat_tex.height, cat_tex.pixel_data); + + compositor.data = &state; + compositor_run(&compositor); + + wlr_surface_destroy(state.cat_texture); + wlr_renderer_destroy(state.renderer); +} diff --git a/include/backend/libinput.h b/include/backend/libinput.h index dd24fbb0..2427ae5c 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -56,7 +56,5 @@ void handle_touch_motion(struct libinput_event *event, struct libinput_device *device); void handle_touch_cancel(struct libinput_event *event, struct libinput_device *device); -void handle_touch_frame(struct libinput_event *event, - struct libinput_device *device); #endif diff --git a/include/wlr/types.h b/include/wlr/types.h index b4d69ccb..4763013a 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -146,10 +146,37 @@ struct wlr_touch { struct wl_signal up; struct wl_signal motion; struct wl_signal cancel; - struct wl_signal frame; } events; }; +struct wlr_touch_down { + uint32_t time_sec; + uint64_t time_usec; + int32_t slot; + double x_mm, y_mm; + double width_mm, height_mm; +}; + +struct wlr_touch_up { + uint32_t time_sec; + uint64_t time_usec; + int32_t slot; +}; + +struct wlr_touch_motion { + uint32_t time_sec; + uint64_t time_usec; + int32_t slot; + double x_mm, y_mm; + double width_mm, height_mm; +}; + +struct wlr_touch_cancel { + uint32_t time_sec; + uint64_t time_usec; + int32_t slot; +}; + // TODO: tablet & tablet tool // TODO: gestures // TODO: switch diff --git a/types/wlr_touch.c b/types/wlr_touch.c index 9c8b4b94..0970f70f 100644 --- a/types/wlr_touch.c +++ b/types/wlr_touch.c @@ -13,7 +13,6 @@ struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl, wl_signal_init(&touch->events.down); wl_signal_init(&touch->events.up); wl_signal_init(&touch->events.motion); - wl_signal_init(&touch->events.frame); wl_signal_init(&touch->events.cancel); return touch; } -- cgit v1.2.3 From 4a9966b1a47d497ccc9473a24ba56b3d0eef72d2 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Thu, 15 Jun 2017 14:32:28 -0400 Subject: Implement wlr_tablet_tool --- backend/CMakeLists.txt | 1 + backend/libinput/events.c | 17 ++++- backend/libinput/tablet_tool.c | 154 +++++++++++++++++++++++++++++++++++++++++ include/backend/libinput.h | 11 +++ include/types.h | 8 +++ include/wlr/types.h | 80 ++++++++++++++++++++- types/CMakeLists.txt | 3 +- types/wlr_tablet_tool.c | 26 +++++++ 8 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 backend/libinput/tablet_tool.c create mode 100644 types/wlr_tablet_tool.c (limited to 'backend/libinput/events.c') diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 8272e039..6ffa2042 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -19,6 +19,7 @@ add_library(wlr-backend libinput/keyboard.c libinput/pointer.c libinput/touch.c + libinput/tablet_tool.c multi/backend.c backend.c diff --git a/backend/libinput/events.c b/backend/libinput/events.c index f8413dae..8ebec63b 100644 --- a/backend/libinput/events.c +++ b/backend/libinput/events.c @@ -85,7 +85,10 @@ static void handle_device_added(struct wlr_backend_state *state, wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) { - // TODO + struct wlr_input_device *wlr_device = allocate_device(state, + device, devices, WLR_INPUT_DEVICE_TABLET_TOOL); + wlr_device->tablet_tool = wlr_libinput_tablet_tool_create(device); + wl_signal_emit(&state->backend->events.input_add, wlr_device); } if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_PAD)) { // TODO @@ -154,6 +157,18 @@ void wlr_libinput_event(struct wlr_backend_state *state, case LIBINPUT_EVENT_TOUCH_FRAME: // no-op (at least for now) break; + case LIBINPUT_EVENT_TABLET_TOOL_AXIS: + handle_tablet_tool_axis(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY: + handle_tablet_tool_proximity(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_TIP: + handle_tablet_tool_tip(event, device); + break; + case LIBINPUT_EVENT_TABLET_TOOL_BUTTON: + handle_tablet_tool_button(event, device); + break; default: wlr_log(L_DEBUG, "Unknown libinput event %d", event_type); break; diff --git a/backend/libinput/tablet_tool.c b/backend/libinput/tablet_tool.c new file mode 100644 index 00000000..276c263c --- /dev/null +++ b/backend/libinput/tablet_tool.c @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +#include "backend/libinput.h" +#include "common/log.h" +#include "types.h" + +struct wlr_tablet_tool *wlr_libinput_tablet_tool_create( + struct libinput_device *device) { + assert(device); + return wlr_tablet_tool_create(NULL, NULL); +} + +void handle_tablet_tool_axis(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_axis *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_axis)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + libinput_device_get_size(device, &wlr_event->width_mm, &wlr_event->height_mm); + if (libinput_event_tablet_tool_x_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_X; + wlr_event->x_mm = libinput_event_tablet_tool_get_x(tevent); + } + if (libinput_event_tablet_tool_y_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_Y; + wlr_event->y_mm = libinput_event_tablet_tool_get_y(tevent); + } + if (libinput_event_tablet_tool_pressure_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_PRESSURE; + wlr_event->pressure = libinput_event_tablet_tool_get_pressure(tevent); + } + if (libinput_event_tablet_tool_distance_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_DISTANCE; + wlr_event->distance = libinput_event_tablet_tool_get_distance(tevent); + } + if (libinput_event_tablet_tool_tilt_x_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_X; + wlr_event->tilt_x = libinput_event_tablet_tool_get_tilt_x(tevent); + } + if (libinput_event_tablet_tool_tilt_y_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_TILT_Y; + wlr_event->tilt_y = libinput_event_tablet_tool_get_tilt_y(tevent); + } + if (libinput_event_tablet_tool_rotation_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_ROTATION; + wlr_event->rotation = libinput_event_tablet_tool_get_rotation(tevent); + } + if (libinput_event_tablet_tool_slider_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_SLIDER; + wlr_event->slider = libinput_event_tablet_tool_get_slider_position(tevent); + } + if (libinput_event_tablet_tool_wheel_has_changed(tevent)) { + wlr_event->updated_axes |= WLR_TABLET_TOOL_AXIS_WHEEL; + wlr_event->wheel_delta = libinput_event_tablet_tool_get_wheel_delta(tevent); + } + wl_signal_emit(&dev->tablet_tool->events.axis, wlr_event); +} + +void handle_tablet_tool_proximity(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + // Proximity events contain axis information. We update this information + // before we send the proximity event + handle_tablet_tool_axis(event, device); + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_proximity *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_proximity)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + switch (libinput_event_tablet_tool_get_proximity_state(tevent)) { + case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_OUT: + wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_OUT; + break; + case LIBINPUT_TABLET_TOOL_PROXIMITY_STATE_IN: + wlr_event->state = WLR_TABLET_TOOL_PROXIMITY_IN; + break; + } + wl_signal_emit(&dev->tablet_tool->events.proximity, wlr_event); +} + +void handle_tablet_tool_tip(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + // Tip events contain axis information. We update this information + // before we send the proximity event + handle_tablet_tool_axis(event, device); + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_tip *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_tip)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + switch (libinput_event_tablet_tool_get_tip_state(tevent)) { + case LIBINPUT_TABLET_TOOL_TIP_UP: + wlr_event->state = WLR_TABLET_TOOL_TIP_UP; + break; + case LIBINPUT_TABLET_TOOL_TIP_DOWN: + wlr_event->state = WLR_TABLET_TOOL_TIP_DOWN; + break; + } + wl_signal_emit(&dev->tablet_tool->events.tip, wlr_event); +} + +void handle_tablet_tool_button(struct libinput_event *event, + struct libinput_device *device) { + struct wlr_input_device *dev = + get_appropriate_device(WLR_INPUT_DEVICE_TABLET_TOOL, device); + if (!dev) { + wlr_log(L_DEBUG, "Got a tablet tool event for a device with no tablet tools?"); + return; + } + // Tip events contain axis information. We update this information + // before we send the proximity event + handle_tablet_tool_axis(event, device); + struct libinput_event_tablet_tool *tevent = + libinput_event_get_tablet_tool_event(event); + struct wlr_tablet_tool_button *wlr_event = + calloc(1, sizeof(struct wlr_tablet_tool_button)); + wlr_event->time_sec = libinput_event_tablet_tool_get_time(tevent); + wlr_event->time_usec = libinput_event_tablet_tool_get_time_usec(tevent); + wlr_event->button = libinput_event_tablet_tool_get_button(tevent); + switch (libinput_event_tablet_tool_get_button_state(tevent)) { + case LIBINPUT_BUTTON_STATE_RELEASED: + wlr_event->state = WLR_BUTTON_RELEASED; + break; + case LIBINPUT_BUTTON_STATE_PRESSED: + wlr_event->state = WLR_BUTTON_PRESSED; + break; + } + wl_signal_emit(&dev->tablet_tool->events.button, wlr_event); +} diff --git a/include/backend/libinput.h b/include/backend/libinput.h index 2427ae5c..80f57177 100644 --- a/include/backend/libinput.h +++ b/include/backend/libinput.h @@ -57,4 +57,15 @@ void handle_touch_motion(struct libinput_event *event, void handle_touch_cancel(struct libinput_event *event, struct libinput_device *device); +struct wlr_tablet_tool *wlr_libinput_tablet_tool_create( + struct libinput_device *device); +void handle_tablet_tool_axis(struct libinput_event *event, + struct libinput_device *device); +void handle_tablet_tool_proximity(struct libinput_event *event, + struct libinput_device *device); +void handle_tablet_tool_tip(struct libinput_event *event, + struct libinput_device *device); +void handle_tablet_tool_button(struct libinput_event *event, + struct libinput_device *device); + #endif diff --git a/include/types.h b/include/types.h index 5b20e2ed..efead03c 100644 --- a/include/types.h +++ b/include/types.h @@ -42,6 +42,14 @@ struct wlr_touch *wlr_touch_create(struct wlr_touch_impl *impl, struct wlr_touch_state *state); void wlr_touch_destroy(struct wlr_touch *touch); +struct wlr_tablet_tool_impl { + void (*destroy)(struct wlr_tablet_tool_state *tool); +}; + +struct wlr_tablet_tool *wlr_tablet_tool_create(struct wlr_tablet_tool_impl *impl, + struct wlr_tablet_tool_state *state); +void wlr_tablet_tool_destroy(struct wlr_tablet_tool *tool); + struct wlr_input_device_impl { void (*destroy)(struct wlr_input_device_state *state); }; diff --git a/include/wlr/types.h b/include/wlr/types.h index 4763013a..aae76f06 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -177,15 +177,88 @@ struct wlr_touch_cancel { int32_t slot; }; -// TODO: tablet & tablet tool -// TODO: gestures +struct wlr_tablet_tool_impl; +struct wlr_tablet_tool_state; + +struct wlr_tablet_tool { + struct wlr_tablet_tool_impl *impl; + struct wlr_tablet_tool_state *state; + + struct { + struct wl_signal axis; + struct wl_signal proximity; + struct wl_signal tip; + struct wl_signal button; + } events; +}; + +enum wlr_tablet_tool_axes { + WLR_TABLET_TOOL_AXIS_X = 1, + WLR_TABLET_TOOL_AXIS_Y = 2, + WLR_TABLET_TOOL_AXIS_DISTANCE = 4, + WLR_TABLET_TOOL_AXIS_PRESSURE = 8, + WLR_TABLET_TOOL_AXIS_TILT_X = 16, + WLR_TABLET_TOOL_AXIS_TILT_Y = 32, + WLR_TABLET_TOOL_AXIS_ROTATION = 64, + WLR_TABLET_TOOL_AXIS_SLIDER = 128, + WLR_TABLET_TOOL_AXIS_WHEEL = 256, +}; + +struct wlr_tablet_tool_axis { + uint32_t time_sec; + uint64_t time_usec; + uint32_t updated_axes; + double x_mm, y_mm; + double width_mm, height_mm; + double pressure; + double distance; + double tilt_x, tilt_y; + double rotation; + double slider; + double wheel_delta; +}; + +enum wlr_tablet_tool_proximity_state { + WLR_TABLET_TOOL_PROXIMITY_OUT, + WLR_TABLET_TOOL_PROXIMITY_IN, +}; + +struct wlr_tablet_tool_proximity { + uint32_t time_sec; + uint64_t time_usec; + double x, y; + double width_mm, height_mm; + enum wlr_tablet_tool_proximity_state state; +}; + +enum wlr_tablet_tool_tip_state { + WLR_TABLET_TOOL_TIP_UP, + WLR_TABLET_TOOL_TIP_DOWN, +}; + +struct wlr_tablet_tool_tip { + uint32_t time_sec; + uint64_t time_usec; + double x, y; + double width_mm, height_mm; + enum wlr_tablet_tool_tip_state state; +}; + +struct wlr_tablet_tool_button { + uint32_t time_sec; + uint64_t time_usec; + uint32_t button; + enum wlr_button_state state; +}; + +// TODO: tablet pad // TODO: switch enum wlr_input_device_type { WLR_INPUT_DEVICE_KEYBOARD, WLR_INPUT_DEVICE_POINTER, WLR_INPUT_DEVICE_TOUCH, - WLR_INPUT_DEVICE_TABLET_PEN, + WLR_INPUT_DEVICE_TABLET_TOOL, WLR_INPUT_DEVICE_TABLET_PAD, WLR_INPUT_DEVICE_GESTURE, WLR_INPUT_DEVICE_SWITCH, @@ -207,6 +280,7 @@ struct wlr_input_device { struct wlr_keyboard *keyboard; struct wlr_pointer *pointer; struct wlr_touch *touch; + struct wlr_tablet_tool *tablet_tool; }; }; diff --git a/types/CMakeLists.txt b/types/CMakeLists.txt index b158db39..b10966ec 100644 --- a/types/CMakeLists.txt +++ b/types/CMakeLists.txt @@ -5,10 +5,11 @@ include_directories( add_library(wlr-types wlr_output.c + wlr_input_device.c wlr_keyboard.c wlr_pointer.c wlr_touch.c - wlr_input_device.c + wlr_tablet_tool.c ) target_link_libraries(wlr-types diff --git a/types/wlr_tablet_tool.c b/types/wlr_tablet_tool.c new file mode 100644 index 00000000..1c1d21ee --- /dev/null +++ b/types/wlr_tablet_tool.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include +#include "types.h" + +struct wlr_tablet_tool *wlr_tablet_tool_create(struct wlr_tablet_tool_impl *impl, + struct wlr_tablet_tool_state *state) { + struct wlr_tablet_tool *tool = calloc(1, sizeof(struct wlr_tablet_tool)); + tool->impl = impl; + tool->state = state; + wl_signal_init(&tool->events.axis); + wl_signal_init(&tool->events.proximity); + wl_signal_init(&tool->events.tip); + wl_signal_init(&tool->events.button); + return tool; +} + +void wlr_tablet_tool_destroy(struct wlr_tablet_tool *tool) { + if (!tool) return; + if (tool->impl) { + tool->impl->destroy(tool->state); + } + free(tool); +} -- cgit v1.2.3