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/libinput/keyboard.c | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 backend/libinput/keyboard.c (limited to 'backend/libinput/keyboard.c') 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); +} -- 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/keyboard.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 5dd96c077238b314162570655ea970ba6c2f8693 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Mon, 12 Jun 2017 21:53:41 -0400 Subject: Incorporate XKBCommon into example --- CMake/FindXKBCommon.cmake | 19 ++++++++++++++ CMakeLists.txt | 1 + backend/libinput/keyboard.c | 1 + example/CMakeLists.txt | 3 +++ example/simple.c | 64 ++++++++++++++++++++++++++++++++++++++++----- include/wlr/types.h | 3 --- types/wlr_keyboard.c | 1 - 7 files changed, 81 insertions(+), 11 deletions(-) create mode 100644 CMake/FindXKBCommon.cmake (limited to 'backend/libinput/keyboard.c') diff --git a/CMake/FindXKBCommon.cmake b/CMake/FindXKBCommon.cmake new file mode 100644 index 00000000..30ac503a --- /dev/null +++ b/CMake/FindXKBCommon.cmake @@ -0,0 +1,19 @@ +# - Find XKBCommon +# Once done, this will define +# +# XKBCOMMON_FOUND - System has XKBCommon +# XKBCOMMON_INCLUDE_DIRS - The XKBCommon include directories +# XKBCOMMON_LIBRARIES - The libraries needed to use XKBCommon +# XKBCOMMON_DEFINITIONS - Compiler switches required for using XKBCommon + +find_package(PkgConfig) +pkg_check_modules(PC_XKBCOMMON QUIET xkbcommon) +find_path(XKBCOMMON_INCLUDE_DIRS NAMES xkbcommon/xkbcommon.h HINTS ${PC_XKBCOMMON_INCLUDE_DIRS}) +find_library(XKBCOMMON_LIBRARIES NAMES xkbcommon HINTS ${PC_XKBCOMMON_LIBRARY_DIRS}) + +set(XKBCOMMON_DEFINITIONS ${PC_XKBCOMMON_CFLAGS_OTHER}) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(XKBCOMMON DEFAULT_MSG XKBCOMMON_LIBRARIES XKBCOMMON_INCLUDE_DIRS) +mark_as_advanced(XKBCOMMON_LIBRARIES XKBCOMMON_INCLUDE_DIRS) + diff --git a/CMakeLists.txt b/CMakeLists.txt index ae1dc9a1..353e96fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,6 +48,7 @@ find_package(GLESv2 REQUIRED) find_package(DRM REQUIRED) find_package(GBM REQUIRED) find_package(LibInput REQUIRED) +find_package(XKBCommon REQUIRED) find_package(Udev REQUIRED) find_package(Systemd) diff --git a/backend/libinput/keyboard.c b/backend/libinput/keyboard.c index 158ded28..9ad41a78 100644 --- a/backend/libinput/keyboard.c +++ b/backend/libinput/keyboard.c @@ -11,6 +11,7 @@ struct wlr_keyboard *wlr_libinput_keyboard_create( struct libinput_device *device) { assert(device); + libinput_device_led_update(device, 0); return wlr_keyboard_create(NULL, NULL); } diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index e8f7ec72..bf9efd64 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,6 +1,7 @@ include_directories( ${DRM_INCLUDE_DIRS} ${CMAKE_CURRENT_SOURCE_DIR} + ${XKBCOMMON_INCLUDE_DIRS} ) add_executable(simple @@ -10,6 +11,7 @@ add_executable(simple target_link_libraries(simple wlr-backend wlr-session + ${XKBCOMMON_LIBRARIES} ) add_executable(rotation @@ -21,4 +23,5 @@ target_link_libraries(rotation wlr-backend wlr-session wlr-render + ${XKBCOMMON_LIBRARIES} ) diff --git a/example/simple.c b/example/simple.c index c9561a7c..89f74330 100644 --- a/example/simple.c +++ b/example/simple.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 199309L +#include #include #include #include @@ -8,12 +9,15 @@ #include #include #include +#include struct state { float color[3]; int dec; bool exit; struct timespec last_frame; + struct xkb_keymap *keymap; + struct xkb_state *xkb_state; struct wl_list keyboards; struct wl_listener input_add; @@ -35,7 +39,6 @@ struct keyboard_state { struct state *state; struct wlr_input_device *device; struct wl_listener key; - struct wl_listener mods; struct wl_list link; }; @@ -98,12 +101,30 @@ void output_remove(struct wl_listener *listener, void *data) { wl_list_remove(&ostate->frame.link); } +static void handle_keysym(struct state *state, xkb_keysym_t sym, + enum wlr_key_state key_state) { + char name[64]; + int l = xkb_keysym_get_name(sym, name, sizeof(name)); + if (l != -1 && l != sizeof(name)) { + fprintf(stderr, "Key event: %s %s\n", name, + key_state == WLR_KEY_PRESSED ? "pressed" : "released"); + } + if (sym == XKB_KEY_Escape) { + state->exit = true; + } +} + 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"); - kbstate->state->exit = true; + uint32_t keycode = event->keycode + 8; + const xkb_keysym_t *syms; + int nsyms = xkb_state_key_get_syms(kbstate->state->xkb_state, keycode, &syms); + for (int i = 0; i < nsyms; ++i) { + handle_keysym(kbstate->state, syms[i], event->state); + } + xkb_state_update_key(kbstate->state->xkb_state, keycode, + event->state == WLR_KEY_PRESSED ? XKB_KEY_DOWN : XKB_KEY_UP); } void input_add(struct wl_listener *listener, void *data) { @@ -116,7 +137,6 @@ void input_add(struct wl_listener *listener, void *data) { kbstate->device = device; kbstate->state = state; 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); @@ -140,7 +160,12 @@ void input_remove(struct wl_listener *listener, void *data) { } wl_list_remove(&kbstate->link); wl_list_remove(&kbstate->key.link); - //wl_list_remove(&kbstate->mods.link); +} + +static int timer_done(void *data) { + struct state *state = data; + state->exit = true; + return 1; } int main() { @@ -151,9 +176,30 @@ int main() { .input_add = { .notify = input_add }, .input_remove = { .notify = input_remove }, .output_add = { .notify = output_add }, - .output_remove = { .notify = output_remove } + .output_remove = { .notify = output_remove }, }; + struct xkb_rule_names rules; + memset(&rules, 0, sizeof(rules)); + rules.rules = getenv("XKB_DEFAULT_RULES"); + rules.model = getenv("XKB_DEFAULT_MODEL"); + rules.layout = getenv("XKB_DEFAULT_LAYOUT"); + rules.variant = getenv("XKB_DEFAULT_VARIANT"); + rules.options = getenv("XKB_DEFAULT_OPTIONS"); + struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + if (!context) { + fprintf(stderr, "Failed to create XKB context\n"); + return 1; + } + state.keymap = + xkb_map_new_from_names(context, &rules, XKB_KEYMAP_COMPILE_NO_FLAGS); + if (!state.keymap) { + fprintf(stderr, "Failed to create XKB keymap\n"); + return 1; + } + xkb_context_unref(context); + state.xkb_state = xkb_state_new(state.keymap); + wl_list_init(&state.keyboards); wl_list_init(&state.input_add.link); wl_list_init(&state.input_remove.link); @@ -179,6 +225,10 @@ int main() { if (!wlr || !wlr_backend_init(wlr)) { return 1; } + struct wl_event_source *timer = wl_event_loop_add_timer(event_loop, + timer_done, &state); + + wl_event_source_timer_update(timer, 30000); while (!state.exit) { wl_event_loop_dispatch(event_loop, 0); diff --git a/include/wlr/types.h b/include/wlr/types.h index 389989e8..af23152c 100644 --- a/include/wlr/types.h +++ b/include/wlr/types.h @@ -50,8 +50,6 @@ void wlr_output_destroy(struct wlr_output *output); void wlr_output_effective_resolution(struct wlr_output *output, int *width, int *height); -// TODO: keymaps - struct wlr_keyboard_state; struct wlr_keyboard_impl; @@ -61,7 +59,6 @@ struct wlr_keyboard { struct { struct wl_signal key; - struct wl_signal mods; } events; }; diff --git a/types/wlr_keyboard.c b/types/wlr_keyboard.c index d102311b..db4f7ca7 100644 --- a/types/wlr_keyboard.c +++ b/types/wlr_keyboard.c @@ -11,7 +11,6 @@ struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl, kb->impl = impl; kb->state = state; wl_signal_init(&kb->events.key); - wl_signal_init(&kb->events.mods); return kb; } -- cgit v1.2.3