aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorDrew DeVault <sir@cmpwn.com>2017-06-09 17:52:11 -0400
committerDrew DeVault <sir@cmpwn.com>2017-06-13 08:10:36 -0400
commit59ceaf507e9a7c80e04460d1ae4946ce7b6f56ac (patch)
treedf3b5fcc06b47ca3a1beb9ecff01cbf4527473d4 /backend
parent0e75d157f52db45a1af350574bd95cccbd09fa57 (diff)
Handle key presses
Diffstat (limited to 'backend')
-rw-r--r--backend/libinput/backend.c14
-rw-r--r--backend/libinput/events.c56
2 files changed, 63 insertions, 7 deletions
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);