Handle key presses

This commit is contained in:
Drew DeVault 2017-06-09 17:52:11 -04:00
parent 0e75d157f5
commit 59ceaf507e
4 changed files with 95 additions and 10 deletions

View file

@ -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;
}

View file

@ -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);