From f479b7c8c7aa93229c46287f774a30ac8324da1e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Sat, 10 Jun 2017 11:58:25 -0400 Subject: [PATCH] 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 +++++++++++++++++++ .../{libinput/backend.h => libinput.h} | 10 ++++ 5 files changed, 70 insertions(+), 49 deletions(-) create mode 100644 backend/libinput/keyboard.c rename include/backend/{libinput/backend.h => libinput.h} (65%) 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/backend.h b/include/backend/libinput.h similarity index 65% rename from include/backend/libinput/backend.h rename to include/backend/libinput.h index 6f2fa2a0..a59ab401 100644 --- a/include/backend/libinput/backend.h +++ b/include/backend/libinput.h @@ -5,6 +5,7 @@ #include #include #include "backend/udev.h" +#include "types.h" struct wlr_backend_state { struct wlr_backend *backend; @@ -21,8 +22,17 @@ struct wlr_backend_state { 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