2017-06-10 17:58:25 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <libinput.h>
|
2018-02-12 21:29:23 +01:00
|
|
|
#include <stdlib.h>
|
2022-02-23 16:42:20 +01:00
|
|
|
#include <wlr/interfaces/wlr_keyboard.h>
|
2017-06-10 17:58:25 +02:00
|
|
|
#include "backend/libinput.h"
|
|
|
|
|
2022-02-23 16:42:20 +01:00
|
|
|
struct wlr_libinput_input_device *device_from_keyboard(
|
|
|
|
struct wlr_keyboard *kb) {
|
|
|
|
assert(kb->impl == &libinput_keyboard_impl);
|
2017-06-19 21:15:37 +02:00
|
|
|
|
2022-02-23 16:42:20 +01:00
|
|
|
struct wlr_libinput_input_device *dev = wl_container_of(kb, dev, keyboard);
|
|
|
|
return dev;
|
2018-09-17 21:53:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 00:51:00 +02:00
|
|
|
static void keyboard_set_leds(struct wlr_keyboard *wlr_kb, uint32_t leds) {
|
2022-02-23 16:42:20 +01:00
|
|
|
struct wlr_libinput_input_device *dev = device_from_keyboard(wlr_kb);
|
|
|
|
libinput_device_led_update(dev->handle, leds);
|
2017-06-19 21:15:37 +02:00
|
|
|
}
|
|
|
|
|
2018-04-26 00:51:00 +02:00
|
|
|
static void keyboard_destroy(struct wlr_keyboard *wlr_kb) {
|
2022-02-23 16:42:20 +01:00
|
|
|
/* wlr_keyboard belongs to the wlr_libinput_input_device */
|
2017-06-19 21:15:37 +02:00
|
|
|
}
|
|
|
|
|
2022-02-09 22:03:12 +01:00
|
|
|
const struct wlr_keyboard_impl libinput_keyboard_impl = {
|
2018-04-26 00:51:00 +02:00
|
|
|
.destroy = keyboard_destroy,
|
|
|
|
.led_update = keyboard_set_leds
|
2017-06-19 21:15:37 +02:00
|
|
|
};
|
|
|
|
|
2022-02-23 16:42:20 +01:00
|
|
|
void init_device_keyboard(struct wlr_libinput_input_device *dev) {
|
|
|
|
const char *name = libinput_device_get_name(dev->handle);
|
|
|
|
struct wlr_keyboard *wlr_kb = &dev->keyboard;
|
2022-02-09 22:03:12 +01:00
|
|
|
wlr_keyboard_init(wlr_kb, &libinput_keyboard_impl, name);
|
2022-02-23 16:42:20 +01:00
|
|
|
wlr_kb->base.vendor = libinput_device_get_id_vendor(dev->handle);
|
|
|
|
wlr_kb->base.product = libinput_device_get_id_product(dev->handle);
|
|
|
|
|
|
|
|
libinput_device_led_update(dev->handle, 0);
|
2017-06-10 17:58:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void handle_keyboard_key(struct libinput_event *event,
|
2022-02-23 16:42:20 +01:00
|
|
|
struct wlr_keyboard *kb) {
|
2017-06-10 17:58:25 +02:00
|
|
|
struct libinput_event_keyboard *kbevent =
|
|
|
|
libinput_event_get_keyboard_event(event);
|
2017-08-11 18:38:27 +02:00
|
|
|
struct wlr_event_keyboard_key wlr_event = { 0 };
|
2017-10-30 20:43:06 +01:00
|
|
|
wlr_event.time_msec =
|
|
|
|
usec_to_msec(libinput_event_keyboard_get_time_usec(kbevent));
|
2017-08-11 18:38:27 +02:00
|
|
|
wlr_event.keycode = libinput_event_keyboard_get_key(kbevent);
|
2018-02-12 21:29:23 +01:00
|
|
|
enum libinput_key_state state =
|
2017-06-10 17:58:25 +02:00
|
|
|
libinput_event_keyboard_get_key_state(kbevent);
|
|
|
|
switch (state) {
|
|
|
|
case LIBINPUT_KEY_STATE_RELEASED:
|
2020-10-21 17:21:23 +02:00
|
|
|
wlr_event.state = WL_KEYBOARD_KEY_STATE_RELEASED;
|
2017-06-10 17:58:25 +02:00
|
|
|
break;
|
|
|
|
case LIBINPUT_KEY_STATE_PRESSED:
|
2020-10-21 17:21:23 +02:00
|
|
|
wlr_event.state = WL_KEYBOARD_KEY_STATE_PRESSED;
|
2017-06-10 17:58:25 +02:00
|
|
|
break;
|
|
|
|
}
|
2017-10-06 11:03:34 +02:00
|
|
|
wlr_event.update_state = true;
|
2022-02-23 16:42:20 +01:00
|
|
|
wlr_keyboard_notify_key(kb, &wlr_event);
|
2017-06-10 17:58:25 +02:00
|
|
|
}
|