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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <libinput.h>
#include <stdlib.h>
#include <wayland-util.h>
#include <wlr/backend/session.h>
#include <wlr/interfaces/wlr_input_device.h>
#include <wlr/util/log.h>
#include "backend/libinput.h"
#include "util/signal.h"
struct wlr_input_device *get_appropriate_device(
enum wlr_input_device_type desired_type,
struct libinput_device *libinput_dev) {
struct wl_list *wlr_devices = libinput_device_get_user_data(libinput_dev);
if (!wlr_devices) {
return NULL;
}
struct wlr_input_device *dev;
wl_list_for_each(dev, wlr_devices, link) {
if (dev->type == desired_type) {
return dev;
}
}
return NULL;
}
static void input_device_destroy(struct wlr_input_device *_dev) {
struct wlr_libinput_input_device *dev = (struct wlr_libinput_input_device *)_dev;
libinput_device_unref(dev->handle);
wl_list_remove(&dev->wlr_input_device.link);
free(dev);
}
static const struct wlr_input_device_impl input_device_impl = {
.destroy = input_device_destroy,
};
static struct wlr_input_device *allocate_device(
struct wlr_libinput_backend *backend, struct libinput_device *libinput_dev,
struct wl_list *wlr_devices, enum wlr_input_device_type type) {
int vendor = libinput_device_get_id_vendor(libinput_dev);
int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev);
struct wlr_libinput_input_device *wlr_libinput_dev;
if (!(wlr_libinput_dev = calloc(1, sizeof(struct wlr_libinput_input_device)))) {
return NULL;
}
struct wlr_input_device *wlr_dev = &wlr_libinput_dev->wlr_input_device;
libinput_device_get_size(libinput_dev,
&wlr_dev->width_mm, &wlr_dev->height_mm);
const char *output_name = libinput_device_get_output_name(libinput_dev);
if (output_name != NULL) {
wlr_dev->output_name = strdup(output_name);
}
wl_list_insert(wlr_devices, &wlr_dev->link);
wlr_libinput_dev->handle = libinput_dev;
libinput_device_ref(libinput_dev);
wlr_input_device_init(wlr_dev, type, &input_device_impl,
name, vendor, product);
return wlr_dev;
}
bool wlr_input_device_is_libinput(struct wlr_input_device *wlr_dev) {
return wlr_dev->impl == &input_device_impl;
}
static void handle_device_added(struct wlr_libinput_backend *backend,
struct libinput_device *libinput_dev) {
assert(backend && libinput_dev);
/*
* 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(libinput_dev);
int product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev);
struct wl_list *wlr_devices = calloc(1, sizeof(struct wl_list));
if (!wlr_devices) {
wlr_log(WLR_ERROR, "Allocation failed");
return;
}
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(libinput_dev, LIBINPUT_DEVICE_CAP_POINTER)) {
struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_POINTER);
if (!wlr_dev) {
goto fail;
}
wlr_dev->pointer = create_libinput_pointer(libinput_dev);
if (!wlr_dev->pointer) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TOUCH)) {
struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TOUCH);
if (!wlr_dev) {
goto fail;
}
wlr_dev->touch = create_libinput_touch(libinput_dev);
if (!wlr_dev->touch) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_TOOL)) {
struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_TOOL);
if (!wlr_dev) {
goto fail;
}
wlr_dev->tablet_tool = create_libinput_tablet_tool(libinput_dev);
if (!wlr_dev->tablet_tool) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_TABLET_PAD)) {
struct wlr_input_device *wlr_dev = allocate_device(backend,
libinput_dev, wlr_devices, WLR_INPUT_DEVICE_TABLET_PAD);
if (!wlr_dev) {
goto fail;
}
wlr_dev->tablet_pad = create_libinput_tablet_pad(libinput_dev);
if (!wlr_dev->tablet_pad) {
free(wlr_dev);
goto fail;
}
wlr_signal_emit_safe(&backend->backend.events.new_input, wlr_dev);
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_GESTURE)) {
// TODO
}
if (libinput_device_has_capability(libinput_dev, LIBINPUT_DEVICE_CAP_SWITCH)) {
// TODO
}
if (wl_list_length(wlr_devices) > 0) {
libinput_device_set_user_data(libinput_dev, wlr_devices);
wlr_list_push(&backend->wlr_device_lists, wlr_devices);
} else {
free(wlr_devices);
}
return;
fail:
wlr_log(WLR_ERROR, "Could not allocate new device");
struct wlr_input_device *dev, *tmp_dev;
wl_list_for_each_safe(dev, tmp_dev, wlr_devices, link) {
free(dev);
}
free(wlr_devices);
}
static void handle_device_removed(struct wlr_libinput_backend *backend,
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 product = libinput_device_get_id_product(libinput_dev);
const char *name = libinput_device_get_name(libinput_dev);
wlr_log(WLR_DEBUG, "Removing %s [%d:%d]", name, vendor, product);
if (!wlr_devices) {
return;
}
struct wlr_input_device *dev, *tmp_dev;
wl_list_for_each_safe(dev, tmp_dev, wlr_devices, link) {
if (dev->type == WLR_INPUT_DEVICE_TABLET_TOOL) {
wlr_libinput_tablet_tool_destroy(dev);
}
wlr_input_device_destroy(dev);
}
for (size_t i = 0; i < backend->wlr_device_lists.length; i++) {
if (backend->wlr_device_lists.items[i] == wlr_devices) {
wlr_list_del(&backend->wlr_device_lists, i);
break;
}
}
free(wlr_devices);
}
void handle_libinput_event(struct wlr_libinput_backend *backend,
struct libinput_event *event) {
assert(backend && event);
struct libinput_device *libinput_dev = libinput_event_get_device(event);
enum libinput_event_type event_type = libinput_event_get_type(event);
switch (event_type) {
case LIBINPUT_EVENT_DEVICE_ADDED:
handle_device_added(backend, libinput_dev);
break;
case LIBINPUT_EVENT_DEVICE_REMOVED:
handle_device_removed(backend, libinput_dev);
break;
case LIBINPUT_EVENT_KEYBOARD_KEY:
handle_keyboard_key(event, libinput_dev);
break;
case LIBINPUT_EVENT_POINTER_MOTION:
handle_pointer_motion(event, libinput_dev);
break;
case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
handle_pointer_motion_abs(event, libinput_dev);
break;
case LIBINPUT_EVENT_POINTER_BUTTON:
handle_pointer_button(event, libinput_dev);
break;
case LIBINPUT_EVENT_POINTER_AXIS:
handle_pointer_axis(event, libinput_dev);
break;
case LIBINPUT_EVENT_TOUCH_DOWN:
handle_touch_down(event, libinput_dev);
break;
case LIBINPUT_EVENT_TOUCH_UP:
handle_touch_up(event, libinput_dev);
break;
case LIBINPUT_EVENT_TOUCH_MOTION:
handle_touch_motion(event, libinput_dev);
break;
case LIBINPUT_EVENT_TOUCH_CANCEL:
handle_touch_cancel(event, libinput_dev);
break;
case LIBINPUT_EVENT_TOUCH_FRAME:
// no-op (at least for now)
break;
case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
handle_tablet_tool_axis(event, libinput_dev);
break;
case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
handle_tablet_tool_proximity(event, libinput_dev);
break;
case LIBINPUT_EVENT_TABLET_TOOL_TIP:
handle_tablet_tool_tip(event, libinput_dev);
break;
case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
handle_tablet_tool_button(event, libinput_dev);
break;
case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
handle_tablet_pad_button(event, libinput_dev);
break;
case LIBINPUT_EVENT_TABLET_PAD_RING:
handle_tablet_pad_ring(event, libinput_dev);
break;
case LIBINPUT_EVENT_TABLET_PAD_STRIP:
handle_tablet_pad_strip(event, libinput_dev);
break;
default:
wlr_log(WLR_DEBUG, "Unknown libinput event %d", event_type);
break;
}
}
|