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

@ -238,8 +238,16 @@ struct wlr_backend *wlr_libinput_backend_create(struct wl_display *display,
struct libinput_device *wlr_libinput_get_device_handle( struct libinput_device *wlr_libinput_get_device_handle(
struct wlr_input_device *wlr_dev) { struct wlr_input_device *wlr_dev) {
struct wlr_libinput_input_device *dev = struct wlr_libinput_input_device *dev = NULL;
(struct wlr_libinput_input_device *)wlr_dev; switch (wlr_dev->type) {
case WLR_INPUT_DEVICE_KEYBOARD:
dev = device_from_keyboard(wlr_dev->keyboard);
break;
default:
dev = (struct wlr_libinput_input_device *)wlr_dev;
break;
}
return dev->handle; return dev->handle;
} }

View File

@ -25,6 +25,7 @@ struct wlr_input_device *get_appropriate_device(
return NULL; return NULL;
} }
void destroy_libinput_input_device(struct wlr_libinput_input_device *dev) void destroy_libinput_input_device(struct wlr_libinput_input_device *dev)
{ {
/** /**
@ -34,8 +35,12 @@ void destroy_libinput_input_device(struct wlr_libinput_input_device *dev)
*/ */
if (dev->wlr_input_device._device) { if (dev->wlr_input_device._device) {
wlr_input_device_destroy(&dev->wlr_input_device); wlr_input_device_destroy(&dev->wlr_input_device);
wlr_input_device_finish(&dev->wlr_input_device);
} else {
if (dev->keyboard.impl) {
wlr_keyboard_destroy(&dev->keyboard);
}
} }
wlr_input_device_finish(&dev->wlr_input_device);
libinput_device_unref(dev->handle); libinput_device_unref(dev->handle);
wl_list_remove(&dev->link); wl_list_remove(&dev->link);
@ -98,28 +103,46 @@ static void handle_device_added(struct wlr_libinput_backend *backend,
int vendor = libinput_device_get_id_vendor(libinput_dev); int vendor = libinput_device_get_id_vendor(libinput_dev);
int product = libinput_device_get_id_product(libinput_dev); int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev); const char *name = libinput_device_get_name(libinput_dev);
wlr_log(WLR_DEBUG, "Added %s [%d:%d]", name, vendor, product);
struct wlr_libinput_input_device *dev =
calloc(1, sizeof(struct wlr_libinput_input_device));
if (dev == NULL) {
wlr_log_errno(WLR_ERROR, "failed to allocate wlr_libinput_input_device");
return;
}
dev->handle = libinput_dev;
libinput_device_ref(libinput_dev);
libinput_device_set_user_data(libinput_dev, dev);
bool dev_used = false;
if (libinput_device_has_capability(
libinput_dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
init_device_keyboard(dev);
wlr_signal_emit_safe(&backend->backend.events.new_input,
&dev->keyboard.base);
dev_used = true;
}
if (dev_used) {
wl_list_insert(&backend->devices, &dev->link);
return;
} else {
libinput_device_unref(libinput_dev);
free(dev);
}
struct wl_list *wlr_devices = calloc(1, sizeof(struct wl_list)); struct wl_list *wlr_devices = calloc(1, sizeof(struct wl_list));
if (!wlr_devices) { if (!wlr_devices) {
wlr_log(WLR_ERROR, "Allocation failed"); wlr_log(WLR_ERROR, "Allocation failed");
return; return;
} }
wl_list_init(wlr_devices); wl_list_init(wlr_devices);
wlr_log(WLR_DEBUG, "Added %s [%d:%d]", name, vendor, product);
if (libinput_device_has_capability(
libinput_dev, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_KEYBOARD);
if (!wlr_dev) {
goto fail;
}
wlr_dev->keyboard = create_libinput_keyboard(libinput_dev);
if (!wlr_dev->keyboard) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
}
if (libinput_device_has_capability( if (libinput_device_has_capability(
libinput_dev, LIBINPUT_DEVICE_CAP_POINTER)) { libinput_dev, LIBINPUT_DEVICE_CAP_POINTER)) {
struct wlr_input_device *wlr_dev = allocate_device(backend, struct wlr_input_device *wlr_dev = allocate_device(backend,
@ -210,30 +233,32 @@ static void handle_device_added(struct wlr_libinput_backend *backend,
fail: fail:
wlr_log(WLR_ERROR, "Could not allocate new device"); wlr_log(WLR_ERROR, "Could not allocate new device");
struct wlr_libinput_input_device *dev, *tmp_dev; struct wlr_libinput_input_device *device, *tmp;
wl_list_for_each_safe(dev, tmp_dev, wlr_devices, link) { wl_list_for_each_safe(device, tmp, wlr_devices, link) {
free(dev); free(device);
} }
free(wlr_devices); free(wlr_devices);
} }
static void handle_device_removed(struct wlr_libinput_backend *backend, static void handle_device_removed(struct wlr_libinput_backend *backend,
struct libinput_device *libinput_dev) { struct libinput_device *libinput_dev) {
struct wl_list *wlr_devices = libinput_device_get_user_data(libinput_dev);
int vendor = libinput_device_get_id_vendor(libinput_dev); int vendor = libinput_device_get_id_vendor(libinput_dev);
int product = libinput_device_get_id_product(libinput_dev); int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev); const char *name = libinput_device_get_name(libinput_dev);
wlr_log(WLR_DEBUG, "Removing %s [%d:%d]", name, vendor, product); wlr_log(WLR_DEBUG, "Removing %s [%d:%d]", name, vendor, product);
// TODO: use libinput_device_get_user_data(libinput_dev);
if (!wl_list_empty(&backend->devices)) { if (!wl_list_empty(&backend->devices)) {
struct wlr_libinput_input_device *dev, *tmp_dev; struct wlr_libinput_input_device *dev, *tmp_dev;
wl_list_for_each_safe(dev, tmp_dev, &backend->devices, link) { wl_list_for_each_safe(dev, tmp_dev, &backend->devices, link) {
if (dev->handle == libinput_dev) { if (dev->handle == libinput_dev) {
destroy_libinput_input_device(dev); destroy_libinput_input_device(dev);
return;
} }
} }
} }
struct wl_list *wlr_devices = libinput_device_get_user_data(libinput_dev);
if (!wlr_devices) { if (!wlr_devices) {
return; return;
} }
@ -258,6 +283,8 @@ static void handle_device_removed(struct wlr_libinput_backend *backend,
void handle_libinput_event(struct wlr_libinput_backend *backend, void handle_libinput_event(struct wlr_libinput_backend *backend,
struct libinput_event *event) { struct libinput_event *event) {
struct libinput_device *libinput_dev = libinput_event_get_device(event); struct libinput_device *libinput_dev = libinput_event_get_device(event);
struct wlr_libinput_input_device *dev =
libinput_device_get_user_data(libinput_dev);
enum libinput_event_type event_type = libinput_event_get_type(event); enum libinput_event_type event_type = libinput_event_get_type(event);
switch (event_type) { switch (event_type) {
case LIBINPUT_EVENT_DEVICE_ADDED: case LIBINPUT_EVENT_DEVICE_ADDED:
@ -267,7 +294,7 @@ void handle_libinput_event(struct wlr_libinput_backend *backend,
handle_device_removed(backend, libinput_dev); handle_device_removed(backend, libinput_dev);
break; break;
case LIBINPUT_EVENT_KEYBOARD_KEY: case LIBINPUT_EVENT_KEYBOARD_KEY:
handle_keyboard_key(event, libinput_dev); handle_keyboard_key(event, &dev->keyboard);
break; break;
case LIBINPUT_EVENT_POINTER_MOTION: case LIBINPUT_EVENT_POINTER_MOTION:
handle_pointer_motion(event, libinput_dev); handle_pointer_motion(event, libinput_dev);

View File

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

View File

@ -32,8 +32,11 @@ struct wlr_libinput_backend {
struct wlr_libinput_input_device { struct wlr_libinput_input_device {
struct wlr_input_device wlr_input_device; struct wlr_input_device wlr_input_device;
struct wl_list link;
struct libinput_device *handle; struct libinput_device *handle;
struct wlr_keyboard keyboard;
struct wl_list link;
}; };
uint32_t usec_to_msec(uint64_t usec); uint32_t usec_to_msec(uint64_t usec);
@ -54,10 +57,9 @@ extern const struct wlr_tablet_impl libinput_tablet_impl;
extern const struct wlr_tablet_pad_impl libinput_tablet_pad_impl; extern const struct wlr_tablet_pad_impl libinput_tablet_pad_impl;
extern const struct wlr_touch_impl libinput_touch_impl; extern const struct wlr_touch_impl libinput_touch_impl;
struct wlr_keyboard *create_libinput_keyboard( void init_device_keyboard(struct wlr_libinput_input_device *dev);
struct libinput_device *device); struct wlr_libinput_input_device *device_from_keyboard(struct wlr_keyboard *kb);
void handle_keyboard_key(struct libinput_event *event, void handle_keyboard_key(struct libinput_event *event, struct wlr_keyboard *kb);
struct libinput_device *device);
struct wlr_pointer *create_libinput_pointer( struct wlr_pointer *create_libinput_pointer(
struct libinput_device *device); struct libinput_device *device);