wlroots/backend/libinput/keyboard.c

84 lines
2.5 KiB
C
Raw Permalink Normal View History

#include <assert.h>
#include <libinput.h>
2018-02-12 20:29:23 +00:00
#include <stdlib.h>
2017-07-11 07:18:34 +00:00
#include <wlr/backend/session.h>
2017-06-21 14:27:45 +00:00
#include <wlr/interfaces/wlr_keyboard.h>
2018-02-12 20:29:23 +00:00
#include <wlr/types/wlr_input_device.h>
2017-06-21 16:10:07 +00:00
#include <wlr/util/log.h>
#include "backend/libinput.h"
2017-08-14 13:41:14 +00:00
struct wlr_libinput_keyboard {
struct wlr_keyboard wlr_keyboard;
struct libinput_device *libinput_dev;
2017-06-19 19:15:37 +00:00
};
2018-09-17 19:53:03 +00:00
static const struct wlr_keyboard_impl impl;
static struct wlr_libinput_keyboard *get_libinput_keyboard_from_keyboard(
struct wlr_keyboard *wlr_kb) {
assert(wlr_kb->impl == &impl);
return (struct wlr_libinput_keyboard *)wlr_kb;
}
static void keyboard_set_leds(struct wlr_keyboard *wlr_kb, uint32_t leds) {
2018-09-17 19:53:03 +00:00
struct wlr_libinput_keyboard *kb =
get_libinput_keyboard_from_keyboard(wlr_kb);
libinput_device_led_update(kb->libinput_dev, leds);
2017-06-19 19:15:37 +00:00
}
static void keyboard_destroy(struct wlr_keyboard *wlr_kb) {
2018-09-17 19:53:03 +00:00
struct wlr_libinput_keyboard *kb =
get_libinput_keyboard_from_keyboard(wlr_kb);
libinput_device_unref(kb->libinput_dev);
free(kb);
2017-06-19 19:15:37 +00:00
}
2018-09-17 19:53:03 +00:00
static const struct wlr_keyboard_impl impl = {
.destroy = keyboard_destroy,
.led_update = keyboard_set_leds
2017-06-19 19:15:37 +00:00
};
struct wlr_keyboard *create_libinput_keyboard(
struct libinput_device *libinput_dev) {
2018-09-17 19:53:03 +00:00
struct wlr_libinput_keyboard *kb =
calloc(1, sizeof(struct wlr_libinput_keyboard));
if (kb == NULL) {
return NULL;
}
2018-09-17 19:53:03 +00:00
kb->libinput_dev = libinput_dev;
libinput_device_ref(libinput_dev);
libinput_device_led_update(libinput_dev, 0);
2018-09-17 19:53:03 +00:00
struct wlr_keyboard *wlr_kb = &kb->wlr_keyboard;
2017-08-14 13:41:14 +00:00
wlr_keyboard_init(wlr_kb, &impl);
return wlr_kb;
}
void handle_keyboard_key(struct libinput_event *event,
struct libinput_device *libinput_dev) {
struct wlr_input_device *wlr_dev =
get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, libinput_dev);
if (!wlr_dev) {
2018-09-17 19:53:03 +00:00
wlr_log(WLR_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_event_keyboard_key wlr_event = { 0 };
2017-10-30 19:43:06 +00:00
wlr_event.time_msec =
usec_to_msec(libinput_event_keyboard_get_time_usec(kbevent));
wlr_event.keycode = libinput_event_keyboard_get_key(kbevent);
2018-02-12 20:29:23 +00:00
enum libinput_key_state state =
libinput_event_keyboard_get_key_state(kbevent);
switch (state) {
case LIBINPUT_KEY_STATE_RELEASED:
wlr_event.state = WL_KEYBOARD_KEY_STATE_RELEASED;
break;
case LIBINPUT_KEY_STATE_PRESSED:
wlr_event.state = WL_KEYBOARD_KEY_STATE_PRESSED;
break;
}
wlr_event.update_state = true;
wlr_keyboard_notify_key(wlr_dev->keyboard, &wlr_event);
}