aboutsummaryrefslogtreecommitdiff
path: root/backend/libinput/keyboard.c
blob: 9127217a8f843d5540c54a56227064360e3fcc7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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 "backend/libinput.h"

struct wlr_libinput_keyboard {
	struct wlr_keyboard wlr_keyboard;
	struct libinput_device *libinput_dev;
};

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;
}

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);
}

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);
}

const struct wlr_keyboard_impl libinput_keyboard_impl = {
	.destroy = keyboard_destroy,
	.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);
	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;
}

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 libinput_event_keyboard *kbevent =
		libinput_event_get_keyboard_event(event);
	struct wlr_event_keyboard_key wlr_event = { 0 };
	wlr_event.time_msec =
		usec_to_msec(libinput_event_keyboard_get_time_usec(kbevent));
	wlr_event.keycode = libinput_event_keyboard_get_key(kbevent);
	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);
}