backend/libinput: rework keyboard interface

The wlr_libinput_input_device now owns its wlr_keyboard, instead of creating
a new wlr_libinput_input_device for it.
This commit is contained in:
Simon Zeni 2022-02-23 10:42:20 -05:00 committed by Kirill Primak
parent 9dd6e2b905
commit 5eefda1ffe
4 changed files with 83 additions and 70 deletions

View file

@ -1,33 +1,24 @@
#include <assert.h>
#include <libinput.h>
#include <stdlib.h>
#include <wlr/backend/session.h>
#include <wlr/types/wlr_input_device.h>
#include <wlr/util/log.h>
#include <wlr/interfaces/wlr_keyboard.h>
#include "backend/libinput.h"
struct wlr_libinput_keyboard {
struct wlr_keyboard wlr_keyboard;
struct libinput_device *libinput_dev;
};
struct wlr_libinput_input_device *device_from_keyboard(
struct wlr_keyboard *kb) {
assert(kb->impl == &libinput_keyboard_impl);
static struct wlr_libinput_keyboard *get_libinput_keyboard_from_keyboard(
struct wlr_keyboard *wlr_kb) {
assert(wlr_kb->impl == &libinput_keyboard_impl);
return (struct wlr_libinput_keyboard *)wlr_kb;
struct wlr_libinput_input_device *dev = wl_container_of(kb, dev, keyboard);
return dev;
}
static void keyboard_set_leds(struct wlr_keyboard *wlr_kb, uint32_t leds) {
struct wlr_libinput_keyboard *kb =
get_libinput_keyboard_from_keyboard(wlr_kb);
libinput_device_led_update(kb->libinput_dev, leds);
struct wlr_libinput_input_device *dev = device_from_keyboard(wlr_kb);
libinput_device_led_update(dev->handle, leds);
}
static void keyboard_destroy(struct wlr_keyboard *wlr_kb) {
struct wlr_libinput_keyboard *kb =
get_libinput_keyboard_from_keyboard(wlr_kb);
libinput_device_unref(kb->libinput_dev);
free(kb);
/* wlr_keyboard belongs to the wlr_libinput_input_device */
}
const struct wlr_keyboard_impl libinput_keyboard_impl = {
@ -35,33 +26,18 @@ const struct wlr_keyboard_impl libinput_keyboard_impl = {
.led_update = keyboard_set_leds
};
struct wlr_keyboard *create_libinput_keyboard(
struct libinput_device *libinput_dev) {
struct wlr_libinput_keyboard *kb =
calloc(1, sizeof(struct wlr_libinput_keyboard));
if (kb == NULL) {
return NULL;
}
kb->libinput_dev = libinput_dev;
libinput_device_ref(libinput_dev);
libinput_device_led_update(libinput_dev, 0);
struct wlr_keyboard *wlr_kb = &kb->wlr_keyboard;
const char *name = libinput_device_get_name(libinput_dev);
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;
wlr_keyboard_init(wlr_kb, &libinput_keyboard_impl, name);
wlr_kb->base.vendor = libinput_device_get_id_vendor(libinput_dev);
wlr_kb->base.product = libinput_device_get_id_product(libinput_dev);
return wlr_kb;
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);
}
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) {
wlr_log(WLR_DEBUG,
"Got a keyboard event for a device with no keyboards?");
return;
}
struct wlr_keyboard *kb) {
struct libinput_event_keyboard *kbevent =
libinput_event_get_keyboard_event(event);
struct wlr_event_keyboard_key wlr_event = { 0 };
@ -79,5 +55,5 @@ void handle_keyboard_key(struct libinput_event *event,
break;
}
wlr_event.update_state = true;
wlr_keyboard_notify_key(wlr_dev->keyboard, &wlr_event);
wlr_keyboard_notify_key(kb, &wlr_event);
}