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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
#include <stdlib.h>
#include <assert.h>
#include <libinput.h>
#include <wlr/session.h>
#include <wlr/types.h>
#include <wlr/common/list.h>
#include "backend/libinput/backend.h"
#include "common/log.h"
#include "types.h"
static struct wlr_input_device *get_appropriate_device(
enum wlr_input_device_type desired_type,
struct libinput_device *device) {
list_t *devices = libinput_device_get_user_data(device);
if (!devices) {
return NULL;
}
for (size_t i = 0; i < devices->length; ++i) {
struct wlr_input_device *dev = devices->items[i];
if (dev->type == desired_type) {
return dev;
}
}
return NULL;
}
static void wlr_libinput_keyboard_destroy(struct wlr_keyboard_state *state) {
free(state);
}
static struct wlr_keyboard_impl keyboard_impl = {
.destroy = wlr_libinput_keyboard_destroy
};
static struct wlr_keyboard *wlr_libinput_keyboard_create(
struct libinput_device *device) {
assert(device);
struct wlr_keyboard_state *kbstate =
calloc(1, sizeof(struct wlr_keyboard_state));
kbstate->handle = device;
libinput_device_ref(device);
return wlr_keyboard_create(&keyboard_impl, kbstate);
}
static void handle_keyboard_key(struct libinput_event *event,
struct libinput_device *device) {
struct wlr_input_device *dev =
get_appropriate_device(WLR_INPUT_DEVICE_KEYBOARD, device);
if (!dev) {
wlr_log(L_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_keyboard_key *wlr_event =
calloc(1, sizeof(struct wlr_keyboard_key));
wlr_event->time_sec = libinput_event_keyboard_get_time(kbevent);
wlr_event->time_usec = 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 = WLR_KEY_RELEASED;
break;
case LIBINPUT_KEY_STATE_PRESSED:
wlr_event->state = WLR_KEY_PRESSED;
break;
}
wl_signal_emit(&dev->keyboard->events.key, wlr_event);
}
static void handle_device_added(struct wlr_backend_state *state,
struct libinput_device *device) {
assert(state && device);
/*
* Note: the wlr API exposes only devices with a single capability, because
* that meshes better with how Wayland does things and is a bit simpler.
* However, libinput devices often have multiple capabilities - in such
* cases we have to create several devices.
*/
int vendor = libinput_device_get_id_vendor(device);
int product = libinput_device_get_id_product(device);
const char *name = libinput_device_get_name(device);
list_t *devices = list_create();
wlr_log(L_DEBUG, "Added %s [%d:%d]", name, vendor, product);
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_KEYBOARD)) {
struct wlr_input_device *wlr_device = wlr_input_device_create(
WLR_INPUT_DEVICE_KEYBOARD, name, vendor, product);
wlr_device->keyboard = wlr_libinput_keyboard_create(device);
wl_signal_emit(&state->backend->events.input_add, wlr_device);
list_add(devices, wlr_device);
}
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_POINTER)) {
// TODO
}
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TOUCH)) {
// TODO
}
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) {
// TODO
}
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_TABLET_PAD)) {
// TODO
}
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_GESTURE)) {
// TODO
}
if (libinput_device_has_capability(device, LIBINPUT_DEVICE_CAP_SWITCH)) {
// TODO
}
if (devices->length > 0) {
libinput_device_set_user_data(device, devices);
} else {
list_free(devices);
}
}
static void handle_device_removed(struct wlr_backend_state *state,
struct libinput_device *device) {
wlr_log(L_DEBUG, "libinput device removed");
// TODO
}
void wlr_libinput_event(struct wlr_backend_state *state,
struct libinput_event *event) {
assert(state && event);
struct libinput *context = libinput_event_get_context(event);
struct libinput_device *device = libinput_event_get_device(event);
enum libinput_event_type event_type = libinput_event_get_type(event);
(void)context;
switch (event_type) {
case LIBINPUT_EVENT_DEVICE_ADDED:
handle_device_added(state, device);
break;
case LIBINPUT_EVENT_DEVICE_REMOVED:
handle_device_removed(state, device);
break;
case LIBINPUT_EVENT_KEYBOARD_KEY:
handle_keyboard_key(event, device);
break;
default:
wlr_log(L_DEBUG, "Unknown libinput event %d", event_type);
break;
}
}
|