Refactor out wlr_keyboard_state

This commit is contained in:
Dominique Martinet 2017-08-14 15:41:14 +02:00
parent e6ad67911b
commit 0a3246ad97
5 changed files with 34 additions and 26 deletions

View File

@ -7,17 +7,21 @@
#include <wlr/util/log.h>
#include "backend/libinput.h"
struct wlr_keyboard_state {
struct wlr_libinput_keyboard {
struct wlr_keyboard wlr_keyboard;
struct libinput_device *libinput_dev;
};
static void wlr_libinput_keyboard_set_leds(struct wlr_keyboard_state *kbstate, uint32_t leds) {
libinput_device_led_update(kbstate->libinput_dev, leds);
static void wlr_libinput_keyboard_set_leds(struct wlr_keyboard *wlr_kb, uint32_t leds) {
struct wlr_libinput_keyboard *wlr_libinput_kb = (struct wlr_libinput_keyboard *)wlr_kb;
libinput_device_led_update(wlr_libinput_kb->libinput_dev, leds);
}
static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *kbstate) {
libinput_device_unref(kbstate->libinput_dev);
free(kbstate);
static void wlr_libinput_keyboard_destroy(struct wlr_keyboard *wlr_kb) {
struct wlr_libinput_keyboard *wlr_libinput_kb =
(struct wlr_libinput_keyboard *)wlr_kb;
libinput_device_unref(wlr_libinput_kb->libinput_dev);
free(wlr_libinput_kb);
}
struct wlr_keyboard_impl impl = {
@ -28,11 +32,14 @@ struct wlr_keyboard_impl impl = {
struct wlr_keyboard *wlr_libinput_keyboard_create(
struct libinput_device *libinput_dev) {
assert(libinput_dev);
struct wlr_keyboard_state *kbstate = calloc(1, sizeof(struct wlr_keyboard_state));
kbstate->libinput_dev = libinput_dev;
struct wlr_libinput_keyboard *wlr_libinput_kb =
calloc(1, sizeof(struct wlr_libinput_keyboard));
wlr_libinput_kb->libinput_dev = libinput_dev;
libinput_device_ref(libinput_dev);
libinput_device_led_update(libinput_dev, 0);
return wlr_keyboard_create(&impl, kbstate);
struct wlr_keyboard *wlr_kb = &wlr_libinput_kb->wlr_keyboard;
wlr_keyboard_init(wlr_kb, &impl);
return wlr_kb;
}
void handle_keyboard_key(struct libinput_event *event,

View File

@ -237,12 +237,18 @@ static void seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
wlr_log(L_ERROR, "Unable to allocate wl_pointer device");
return;
}
wlr_device->keyboard = calloc(1, sizeof(struct wlr_keyboard));
if (!wlr_device->keyboard) {
free(wlr_device);
wlr_log(L_ERROR, "Unable to allocate wlr keyboard");
return;
}
wlr_keyboard_init(wlr_device->keyboard, NULL);
struct wlr_wl_input_device *wlr_wl_device =
(struct wlr_wl_input_device *)wlr_device;
struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(wl_seat);
wl_keyboard_add_listener(wl_keyboard, &keyboard_listener, wlr_device);
wlr_device->keyboard = wlr_keyboard_create(NULL, NULL);
wlr_wl_device->resource = wl_keyboard;
wl_signal_emit(&backend->backend.events.input_add, wlr_device);
}

View File

@ -4,12 +4,11 @@
#include <stdint.h>
struct wlr_keyboard_impl {
void (*destroy)(struct wlr_keyboard_state *state);
void (*led_update)(struct wlr_keyboard_state *state, uint32_t leds);
void (*destroy)(struct wlr_keyboard *keyboard);
void (*led_update)(struct wlr_keyboard *keyboard, uint32_t leds);
};
struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl,
struct wlr_keyboard_state *state);
void wlr_keyboard_init(struct wlr_keyboard *keyboard, struct wlr_keyboard_impl *impl);
void wlr_keyboard_destroy(struct wlr_keyboard *keyboard);
#endif

View File

@ -10,11 +10,9 @@ enum WLR_KEYBOARD_LED {
WLR_LED_LAST
};
struct wlr_keyboard_state;
struct wlr_keyboard_impl;
struct wlr_keyboard {
struct wlr_keyboard_state *state;
struct wlr_keyboard_impl *impl;
struct {

View File

@ -4,25 +4,23 @@
#include <wlr/types/wlr_keyboard.h>
#include <wlr/interfaces/wlr_keyboard.h>
struct wlr_keyboard *wlr_keyboard_create(struct wlr_keyboard_impl *impl,
struct wlr_keyboard_state *state) {
struct wlr_keyboard *kb = calloc(1, sizeof(struct wlr_keyboard));
void wlr_keyboard_init(struct wlr_keyboard *kb,
struct wlr_keyboard_impl *impl) {
kb->impl = impl;
kb->state = state;
wl_signal_init(&kb->events.key);
return kb;
}
void wlr_keyboard_destroy(struct wlr_keyboard *kb) {
if (!kb) return;
if (kb->impl) {
kb->impl->destroy(kb->state);
if (kb->impl && kb->impl->destroy) {
kb->impl->destroy(kb);
} else {
free(kb);
}
free(kb);
}
void wlr_keyboard_led_update(struct wlr_keyboard *kb, uint32_t leds) {
if (kb->impl) {
kb->impl->led_update(kb->state, leds);
if (kb->impl && kb->impl->led_update) {
kb->impl->led_update(kb, leds);
}
}