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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
|
#define _POSIX_C_SOURCE 200809L
#include <stdlib.h>
#include <wlr/backend/libinput.h>
#include <wlr/types/wlr_tablet_v2.h>
#include <wlr/types/wlr_tablet_tool.h>
#include <wlr/types/wlr_tablet_pad.h>
#include "log.h"
#include "sway/input/cursor.h"
#include "sway/input/seat.h"
#include "sway/input/tablet.h"
static void handle_pad_tablet_destroy(struct wl_listener *listener, void *data) {
struct sway_tablet_pad *pad =
wl_container_of(listener, pad, tablet_destroy);
pad->tablet = NULL;
wl_list_remove(&pad->tablet_destroy.link);
wl_list_init(&pad->tablet_destroy.link);
}
static void attach_tablet_pad(struct sway_tablet_pad *tablet_pad,
struct sway_tablet *tablet) {
sway_log(SWAY_DEBUG, "Attaching tablet pad \"%s\" to tablet tool \"%s\"",
tablet_pad->seat_device->input_device->wlr_device->name,
tablet->seat_device->input_device->wlr_device->name);
tablet_pad->tablet = tablet;
wl_list_remove(&tablet_pad->tablet_destroy.link);
tablet_pad->tablet_destroy.notify = handle_pad_tablet_destroy;
wl_signal_add(&tablet->seat_device->input_device->wlr_device->events.destroy,
&tablet_pad->tablet_destroy);
}
struct sway_tablet *sway_tablet_create(struct sway_seat *seat,
struct sway_seat_device *device) {
struct sway_tablet *tablet =
calloc(1, sizeof(struct sway_tablet));
if (!sway_assert(tablet, "could not allocate sway tablet for seat")) {
return NULL;
}
wl_list_insert(&seat->cursor->tablets, &tablet->link);
device->tablet = tablet;
tablet->seat_device = device;
return tablet;
}
void sway_configure_tablet(struct sway_tablet *tablet) {
struct wlr_input_device *device =
tablet->seat_device->input_device->wlr_device;
struct sway_seat *seat = tablet->seat_device->sway_seat;
if ((seat->wlr_seat->capabilities & WL_SEAT_CAPABILITY_POINTER) == 0) {
seat_configure_xcursor(seat);
}
if (!tablet->tablet_v2) {
tablet->tablet_v2 =
wlr_tablet_create(server.tablet_v2, seat->wlr_seat, device);
}
/* Search for a sibling tablet pad */
if (!wlr_input_device_is_libinput(device)) {
/* We can only do this on libinput devices */
return;
}
struct libinput_device_group *group =
libinput_device_get_device_group(wlr_libinput_get_device_handle(device));
struct sway_tablet_pad *tablet_pad;
wl_list_for_each(tablet_pad, &seat->cursor->tablet_pads, link) {
struct wlr_input_device *pad_device =
tablet_pad->seat_device->input_device->wlr_device;
if (!wlr_input_device_is_libinput(pad_device)) {
continue;
}
struct libinput_device_group *pad_group =
libinput_device_get_device_group(wlr_libinput_get_device_handle(pad_device));
if (pad_group == group) {
attach_tablet_pad(tablet_pad, tablet);
break;
}
}
}
void sway_tablet_destroy(struct sway_tablet *tablet) {
if (!tablet) {
return;
}
wl_list_remove(&tablet->link);
free(tablet);
}
static void handle_tablet_tool_set_cursor(struct wl_listener *listener, void *data) {
struct sway_tablet_tool *tool =
wl_container_of(listener, tool, set_cursor);
struct wlr_tablet_v2_event_cursor *event = data;
struct sway_cursor *cursor = tool->seat->cursor;
if (!seatop_allows_set_cursor(cursor->seat)) {
return;
}
struct wl_client *focused_client = NULL;
struct wlr_surface *focused_surface = tool->tablet_v2_tool->focused_surface;
if (focused_surface != NULL) {
focused_client = wl_resource_get_client(focused_surface->resource);
}
// TODO: check cursor mode
if (focused_client == NULL ||
event->seat_client->client != focused_client) {
sway_log(SWAY_DEBUG, "denying request to set cursor from unfocused client");
return;
}
cursor_set_image_surface(cursor, event->surface, event->hotspot_x,
event->hotspot_y, focused_client);
}
static void handle_tablet_tool_destroy(struct wl_listener *listener, void *data) {
struct sway_tablet_tool *tool =
wl_container_of(listener, tool, tool_destroy);
wl_list_remove(&tool->tool_destroy.link);
wl_list_remove(&tool->set_cursor.link);
free(tool);
}
void sway_tablet_tool_configure(struct sway_tablet *tablet,
struct wlr_tablet_tool *wlr_tool) {
struct sway_tablet_tool *tool =
calloc(1, sizeof(struct sway_tablet_tool));
if (!sway_assert(tool, "could not allocate sway tablet tool for tablet")) {
return;
}
switch (wlr_tool->type) {
case WLR_TABLET_TOOL_TYPE_LENS:
case WLR_TABLET_TOOL_TYPE_MOUSE:
tool->mode = SWAY_TABLET_TOOL_MODE_RELATIVE;
break;
default:
tool->mode = SWAY_TABLET_TOOL_MODE_ABSOLUTE;
struct input_config *ic = input_device_get_config(
tablet->seat_device->input_device);
if (!ic) {
break;
}
for (int i = 0; i < ic->tools->length; i++) {
struct input_config_tool *tool_config = ic->tools->items[i];
if (tool_config->type == wlr_tool->type) {
tool->mode = tool_config->mode;
break;
}
}
}
tool->seat = tablet->seat_device->sway_seat;
tool->tablet = tablet;
tool->tablet_v2_tool =
wlr_tablet_tool_create(server.tablet_v2,
tablet->seat_device->sway_seat->wlr_seat, wlr_tool);
tool->tool_destroy.notify = handle_tablet_tool_destroy;
wl_signal_add(&wlr_tool->events.destroy, &tool->tool_destroy);
tool->set_cursor.notify = handle_tablet_tool_set_cursor;
wl_signal_add(&tool->tablet_v2_tool->events.set_cursor,
&tool->set_cursor);
wlr_tool->data = tool;
}
static void handle_tablet_pad_attach(struct wl_listener *listener,
void *data) {
struct sway_tablet_pad *pad = wl_container_of(listener, pad, attach);
struct wlr_tablet_tool *wlr_tool = data;
struct sway_tablet_tool *tool = wlr_tool->data;
if (!tool) {
return;
}
attach_tablet_pad(pad, tool->tablet);
}
static void handle_tablet_pad_ring(struct wl_listener *listener, void *data) {
struct sway_tablet_pad *pad = wl_container_of(listener, pad, ring);
struct wlr_tablet_pad_ring_event *event = data;
if (!pad->current_surface) {
return;
}
wlr_tablet_v2_tablet_pad_notify_ring(pad->tablet_v2_pad,
event->ring, event->position,
event->source == WLR_TABLET_PAD_RING_SOURCE_FINGER,
event->time_msec);
}
static void handle_tablet_pad_strip(struct wl_listener *listener, void *data) {
struct sway_tablet_pad *pad = wl_container_of(listener, pad, strip);
struct wlr_tablet_pad_strip_event *event = data;
if (!pad->current_surface) {
return;
}
wlr_tablet_v2_tablet_pad_notify_strip(pad->tablet_v2_pad,
event->strip, event->position,
event->source == WLR_TABLET_PAD_STRIP_SOURCE_FINGER,
event->time_msec);
}
static void handle_tablet_pad_button(struct wl_listener *listener, void *data) {
struct sway_tablet_pad *pad = wl_container_of(listener, pad, button);
struct wlr_tablet_pad_button_event *event = data;
if (!pad->current_surface) {
return;
}
wlr_tablet_v2_tablet_pad_notify_mode(pad->tablet_v2_pad,
event->group, event->mode, event->time_msec);
wlr_tablet_v2_tablet_pad_notify_button(pad->tablet_v2_pad,
event->button, event->time_msec,
(enum zwp_tablet_pad_v2_button_state)event->state);
}
struct sway_tablet_pad *sway_tablet_pad_create(struct sway_seat *seat,
struct sway_seat_device *device) {
struct sway_tablet_pad *tablet_pad =
calloc(1, sizeof(struct sway_tablet_pad));
if (!sway_assert(tablet_pad, "could not allocate sway tablet")) {
return NULL;
}
tablet_pad->seat_device = device;
wl_list_init(&tablet_pad->attach.link);
wl_list_init(&tablet_pad->button.link);
wl_list_init(&tablet_pad->strip.link);
wl_list_init(&tablet_pad->ring.link);
wl_list_init(&tablet_pad->surface_destroy.link);
wl_list_init(&tablet_pad->tablet_destroy.link);
wl_list_insert(&seat->cursor->tablet_pads, &tablet_pad->link);
return tablet_pad;
}
void sway_configure_tablet_pad(struct sway_tablet_pad *tablet_pad) {
struct wlr_input_device *device =
tablet_pad->seat_device->input_device->wlr_device;
struct sway_seat *seat = tablet_pad->seat_device->sway_seat;
if (!tablet_pad->tablet_v2_pad) {
tablet_pad->tablet_v2_pad =
wlr_tablet_pad_create(server.tablet_v2, seat->wlr_seat, device);
}
wl_list_remove(&tablet_pad->attach.link);
tablet_pad->attach.notify = handle_tablet_pad_attach;
wl_signal_add(&device->tablet_pad->events.attach_tablet,
&tablet_pad->attach);
wl_list_remove(&tablet_pad->button.link);
tablet_pad->button.notify = handle_tablet_pad_button;
wl_signal_add(&device->tablet_pad->events.button, &tablet_pad->button);
wl_list_remove(&tablet_pad->strip.link);
tablet_pad->strip.notify = handle_tablet_pad_strip;
wl_signal_add(&device->tablet_pad->events.strip, &tablet_pad->strip);
wl_list_remove(&tablet_pad->ring.link);
tablet_pad->ring.notify = handle_tablet_pad_ring;
wl_signal_add(&device->tablet_pad->events.ring, &tablet_pad->ring);
/* Search for a sibling tablet */
if (!wlr_input_device_is_libinput(device)) {
/* We can only do this on libinput devices */
return;
}
struct libinput_device_group *group =
libinput_device_get_device_group(wlr_libinput_get_device_handle(device));
struct sway_tablet *tool;
wl_list_for_each(tool, &seat->cursor->tablets, link) {
struct wlr_input_device *tablet =
tool->seat_device->input_device->wlr_device;
if (!wlr_input_device_is_libinput(tablet)) {
continue;
}
struct libinput_device_group *tablet_group =
libinput_device_get_device_group(wlr_libinput_get_device_handle(tablet));
if (tablet_group == group) {
attach_tablet_pad(tablet_pad, tool);
break;
}
}
}
void sway_tablet_pad_destroy(struct sway_tablet_pad *tablet_pad) {
if (!tablet_pad) {
return;
}
wl_list_remove(&tablet_pad->link);
wl_list_remove(&tablet_pad->attach.link);
wl_list_remove(&tablet_pad->button.link);
wl_list_remove(&tablet_pad->strip.link);
wl_list_remove(&tablet_pad->ring.link);
wl_list_remove(&tablet_pad->surface_destroy.link);
wl_list_remove(&tablet_pad->tablet_destroy.link);
free(tablet_pad);
}
static void handle_pad_tablet_surface_destroy(struct wl_listener *listener,
void *data) {
struct sway_tablet_pad *tablet_pad =
wl_container_of(listener, tablet_pad, surface_destroy);
wlr_tablet_v2_tablet_pad_notify_leave(tablet_pad->tablet_v2_pad,
tablet_pad->current_surface);
wl_list_remove(&tablet_pad->surface_destroy.link);
wl_list_init(&tablet_pad->surface_destroy.link);
tablet_pad->current_surface = NULL;
}
void sway_tablet_pad_notify_enter(struct sway_tablet_pad *tablet_pad,
struct wlr_surface *surface) {
if (!tablet_pad || !tablet_pad->tablet) {
return;
}
if (surface == tablet_pad->current_surface) {
return;
}
/* Leave current surface */
if (tablet_pad->current_surface) {
wlr_tablet_v2_tablet_pad_notify_leave(tablet_pad->tablet_v2_pad,
tablet_pad->current_surface);
wl_list_remove(&tablet_pad->surface_destroy.link);
wl_list_init(&tablet_pad->surface_destroy.link);
tablet_pad->current_surface = NULL;
}
if (!wlr_surface_accepts_tablet_v2(tablet_pad->tablet->tablet_v2, surface)) {
return;
}
wlr_tablet_v2_tablet_pad_notify_enter(tablet_pad->tablet_v2_pad,
tablet_pad->tablet->tablet_v2, surface);
tablet_pad->current_surface = surface;
wl_list_remove(&tablet_pad->surface_destroy.link);
tablet_pad->surface_destroy.notify = handle_pad_tablet_surface_destroy;
wl_signal_add(&surface->events.destroy, &tablet_pad->surface_destroy);
}
|