-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmulti-seat-wayland.c
561 lines (477 loc) · 13.9 KB
/
multi-seat-wayland.c
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
#define _GNU_SOURCE
#include <Eina.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <poll.h>
#include <wayland-client.h>
#define SEAT_INTERFACE_VERSION (4)
#define COMPOSITOR_INTERFACE_VERSION (1)
#define SHELL_INTERFACE_VERSION (1)
#define SHM_INTERFACE_VERSION (1)
#define HEIGHT (600)
#define WIDTH (800)
#define STRIDE ((WIDTH) * (4))
#define BUFFER_SIZE ((STRIDE) * (HEIGHT))
/* It uses button numbers from linux/input.h */
# define BTN_LEFT 0x110
# define BTN_RIGHT 0x111
# define BTN_MIDDLE 0x112
struct SeatItem {
struct wl_seat *seat;
struct wl_pointer *pointer;
struct wl_keyboard *keyboard;
char *name;
uint32_t id;
uint32_t cap;
struct wl_list link;
};
struct Context {
struct wl_compositor *compositor;
struct wl_buffer *buffer;
struct wl_shell *shell;
struct wl_list seats;
struct wl_shm *shm;
};
static void
_pointer_moved(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
wl_fixed_t surface_x,
wl_fixed_t surface_y)
{
struct SeatItem *item = data;
printf("The pointer from seat '%s' has moved to X:%d Y:%d\n", item->name,
wl_fixed_to_int(surface_x), wl_fixed_to_int(surface_y));
}
static void
_pointer_enter(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
struct wl_surface *surface,
wl_fixed_t surface_x,
wl_fixed_t surface_y)
{
}
static void
_pointer_leave(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
struct wl_surface *surface)
{
}
static void
_pointer_button(void *data,
struct wl_pointer *wl_pointer,
uint32_t serial,
uint32_t time,
uint32_t button,
uint32_t state)
{
struct SeatItem *item = data;
/* Convert from linux to efl buttons */
if (button == BTN_LEFT)
button = 1;
else if (button == BTN_MIDDLE)
button = 2;
else if (button == BTN_RIGHT)
button = 3;
if (state)
printf("The pointer from seat '%s' pressed the button '%"PRIu32"'\n",
item->name, button);
else
printf("The pointer from seat '%s' released the button '%"PRIu32"'\n",
item->name, button);
}
static void
_pointer_axis(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
uint32_t axis,
wl_fixed_t value)
{
}
static void
_pointer_frame(void *data,
struct wl_pointer *wl_pointer)
{
}
static void
_pointer_axis_source(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis_source)
{
}
static void
_pointer_axis_stop(void *data,
struct wl_pointer *wl_pointer,
uint32_t time,
uint32_t axis)
{
}
static void
_pointer_axis_discrete(void *data,
struct wl_pointer *wl_pointer,
uint32_t axis,
int32_t discrete)
{
}
static const struct wl_pointer_listener _pointer_listener = {
.enter = _pointer_enter,
.leave = _pointer_leave,
.motion = _pointer_moved,
.button = _pointer_button,
.axis = _pointer_axis,
.frame = _pointer_frame,
.axis_source = _pointer_axis_source,
.axis_stop = _pointer_axis_stop,
.axis_discrete = _pointer_axis_discrete
};
static void
_keyboard_key_pressed(void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
uint32_t time,
uint32_t key,
uint32_t state)
{
struct SeatItem *item = data;
printf("Keyboard from seat '%s' pressed the key '%"PRIu32"'\n", item->name,
key);
}
static void
_keymap(void *data,
struct wl_keyboard *wl_keyboard,
uint32_t format,
int32_t fd,
uint32_t size)
{
}
static void
_keyboard_enter(void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
struct wl_surface *surface,
struct wl_array *keys)
{
}
static void
_keyboard_leave(void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
struct wl_surface *surface)
{
}
static void
_keyboard_modifiers(void *data,
struct wl_keyboard *wl_keyboard,
uint32_t serial,
uint32_t mods_depressed,
uint32_t mods_latched,
uint32_t mods_locked,
uint32_t group)
{
}
static void
_keyboard_repeat_info(void *data,
struct wl_keyboard *wl_keyboard,
int32_t rate,
int32_t delay)
{
}
static const struct wl_keyboard_listener _keyboard_listener = {
.keymap = _keymap,
.enter = _keyboard_enter,
.leave = _keyboard_leave,
.key = _keyboard_key_pressed,
.modifiers = _keyboard_modifiers,
.repeat_info = _keyboard_repeat_info
};
static void
_release_seat(struct SeatItem *item)
{
if (item->pointer)
wl_pointer_destroy(item->pointer);
if (item->keyboard)
wl_keyboard_destroy(item->keyboard);
if (item->seat)
wl_seat_destroy(item->seat);
wl_list_remove(&item->link);
free(item->name);
free(item);
}
static void
_print_seat_cap(struct SeatItem *item, bool changed)
{
if (changed)
printf("The seat '%s' capabilities changed\n", item->name);
else
printf("The seat '%s' has the following capabilities\n", item->name);
if (!item->cap)
{
printf("\t* No capabilities\n");
return;
}
if ((item->cap & WL_SEAT_CAPABILITY_POINTER) && !item->pointer) {
printf("\t* Mouse\n");
item->pointer = wl_seat_get_pointer(item->seat);
EINA_SAFETY_ON_NULL_GOTO(item->pointer, err);
wl_pointer_add_listener(item->pointer, &_pointer_listener, item);
} else if (!(item->cap & WL_SEAT_CAPABILITY_POINTER) && item->pointer) {
wl_pointer_release(item->pointer);
item->pointer = NULL;
}
if ((item->cap & WL_SEAT_CAPABILITY_KEYBOARD) && !item->keyboard) {
printf("\t* Keyboard\n");
item->keyboard = wl_seat_get_keyboard(item->seat);
wl_keyboard_add_listener(item->keyboard, &_keyboard_listener,
item);
EINA_SAFETY_ON_NULL_GOTO(item->keyboard, err);
} else if (!(item->cap & WL_SEAT_CAPABILITY_KEYBOARD) && item->keyboard) {
wl_keyboard_release(item->keyboard);
item->keyboard = NULL;
}
return;
err:
_release_seat(item);
}
static void
_seat_name(void *data, struct wl_seat *seat, const char *name)
{
struct SeatItem *item = data;
item->name = strdup(name);
EINA_SAFETY_ON_NULL_GOTO(item->name, err_name);
_print_seat_cap(item, false);
return;
err_name:
_release_seat(item);
}
static void
_seat_capabilities(void *data,
struct wl_seat *wl_seat,
uint32_t capabilities)
{
//This callback is called first
struct SeatItem *item = data;
item->cap = capabilities;
if (item->name)
{
_print_seat_cap(item, true);
}
}
static const struct wl_seat_listener _seat_listener = {
.capabilities = _seat_capabilities,
.name = _seat_name
};
static void
_registry_global_add(void *data,
struct wl_registry *wl_registry,
uint32_t id,
const char *interface,
uint32_t version)
{
struct SeatItem *item;
struct Context *ctx = data;
if (!strcmp(interface, wl_seat_interface.name))
{
printf("Found the seat interface with id '%"PRIu32"'\n", id);
item = calloc(1, sizeof(struct SeatItem));
EINA_SAFETY_ON_NULL_RETURN(item);
item->seat = wl_registry_bind(wl_registry, id, &wl_seat_interface,
SEAT_INTERFACE_VERSION);
EINA_SAFETY_ON_NULL_GOTO(item->seat, err_seat);
item->id = id;
wl_seat_add_listener(item->seat, &_seat_listener, item);
wl_list_insert(&ctx->seats, &item->link);
}
else if (!strcmp(interface, wl_compositor_interface.name))
{
ctx->compositor = wl_registry_bind(wl_registry, id,
&wl_compositor_interface,
COMPOSITOR_INTERFACE_VERSION);
}
else if (!strcmp(interface, wl_shell_interface.name))
{
ctx->shell = wl_registry_bind(wl_registry, id, &wl_shell_interface,
SHELL_INTERFACE_VERSION);
}
else if (!strcmp(interface, wl_shm_interface.name))
{
ctx->shm = wl_registry_bind(wl_registry, id, &wl_shm_interface,
SHM_INTERFACE_VERSION);
}
return;
err_seat:
free(item);
}
static void
_registry_global_remove(void *data,
struct wl_registry *wl_registry,
uint32_t id)
{
struct Context *ctx = data;
struct SeatItem *item;
wl_list_for_each(item, &ctx->seats, link) {
if (item->id == id)
{
_release_seat(item);
break;
}
}
}
static const struct wl_registry_listener _registry_listener = {
.global = _registry_global_add,
.global_remove = _registry_global_remove
};
static void
_shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
uint32_t serial)
{
wl_shell_surface_pong(shell_surface, serial);
}
static void
_shell_surface_popup_done(void *data,
struct wl_shell_surface *wl_shell_surface)
{
}
static void
_shell_surface_configure(void *data,
struct wl_shell_surface *wl_shell_surface,
uint32_t edges,
int32_t width,
int32_t height)
{
}
static const struct wl_shell_surface_listener _ss_listener = {
.ping = _shell_surface_ping,
.configure = _shell_surface_configure,
.popup_done = _shell_surface_popup_done
};
static void
buffer_release(void *data, struct wl_buffer *buffer)
{
struct Context *ctx = data;
wl_buffer_destroy(buffer);
ctx->buffer = NULL;
printf("buffer destroy\n");
}
static const struct wl_buffer_listener _buffer_listener = {
.release = buffer_release
};
static int
_setup_buffer(struct Context *ctx, struct wl_surface *surface)
{
int fd, r;
//FIXME: Hardcoded /tmp path
static const char template[] = "/tmp/multi-seat-XXXXXX";
char final_path[sizeof(template)];
struct wl_shm_pool *pool;
struct wl_buffer *buffer;
snprintf(final_path, sizeof(final_path), "%s", template);
fd = mkostemp(final_path, O_CLOEXEC);
EINA_SAFETY_ON_TRUE_RETURN_VAL(fd == -1, -1);
unlink(final_path);
r = ftruncate(fd, BUFFER_SIZE);
EINA_SAFETY_ON_TRUE_GOTO(r == -1, err_truncate);
pool = wl_shm_create_pool(ctx->shm, fd, BUFFER_SIZE);
r = -1;
EINA_SAFETY_ON_NULL_GOTO(pool, err_truncate);
buffer = wl_shm_pool_create_buffer(pool, 0,
WIDTH, HEIGHT,
STRIDE,
WL_SHM_FORMAT_XRGB8888);
EINA_SAFETY_ON_NULL_GOTO(buffer, err_buffer);
wl_surface_attach(surface, buffer, 0, 0);
wl_buffer_add_listener(buffer, &_buffer_listener, ctx);
ctx->buffer = buffer;
r = 0;
err_buffer:
wl_shm_pool_destroy(pool);
err_truncate:
close(fd);
return r;
}
static bool stop = false;
static void
_sig_action(int signum)
{
stop = true;
}
int
main(int argc, char *argv[])
{
int r = -1;
struct Context ctx;
struct wl_display *display;
struct wl_registry *registry;
struct wl_shell_surface *shell_surface;
struct wl_surface *surface;
struct SeatItem *item, *tmp;
struct sigaction sa;
wl_list_init(&ctx.seats);
sa.sa_handler = _sig_action;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESETHAND;
sigaction(SIGINT, &sa, NULL);
printf("Trying to connect to Wayland\n");
display = wl_display_connect(NULL);
EINA_SAFETY_ON_NULL_RETURN_VAL(display, r );
registry = wl_display_get_registry(display);
EINA_SAFETY_ON_NULL_GOTO(registry, err_registry);
wl_registry_add_listener(registry, &_registry_listener, &ctx);
wl_display_dispatch(display);
wl_display_roundtrip(display);
wl_registry_destroy(registry);
EINA_SAFETY_ON_NULL_GOTO(ctx.compositor, err_registry);
EINA_SAFETY_ON_NULL_GOTO(ctx.shell, err_registry);
EINA_SAFETY_ON_NULL_GOTO(ctx.shm, err_registry);
surface = wl_compositor_create_surface(ctx.compositor);
EINA_SAFETY_ON_NULL_GOTO(surface, err_surface);
shell_surface = wl_shell_get_shell_surface(ctx.shell, surface);
EINA_SAFETY_ON_NULL_GOTO(shell_surface, err_shell_surface);
wl_shell_surface_add_listener(shell_surface, &_ss_listener, NULL);
wl_shell_surface_set_toplevel(shell_surface);
wl_surface_damage(surface, 0, 0, 800, 600);
r = _setup_buffer(&ctx, surface);
EINA_SAFETY_ON_TRUE_GOTO(r == -1, err_buffer);
wl_surface_commit(surface);
while (!stop) {
struct pollfd pfd;
r = wl_display_dispatch_pending(display);
EINA_SAFETY_ON_TRUE_GOTO(r == -1, err_loop);
r = wl_display_flush(display);
EINA_SAFETY_ON_TRUE_GOTO(r == -1, err_loop);
pfd.fd = wl_display_get_fd(display);
pfd.events = POLLIN;
pfd.revents = 0;
poll(&pfd, 1, -1);
if (pfd.revents & POLLIN)
wl_display_dispatch(display);
}
r = 0;
wl_list_for_each_safe(item, tmp, &ctx.seats, link) {
_release_seat(item);
}
err_loop:
/* it may be destroyed on buffer_release already */
if (ctx.buffer)
wl_buffer_destroy(ctx.buffer);
err_buffer:
wl_shell_destroy(ctx.shell);
wl_shm_destroy(ctx.shm);
wl_compositor_destroy(ctx.compositor);
wl_shell_surface_destroy(shell_surface);
err_shell_surface:
wl_surface_destroy(surface);
err_surface:
err_registry:
wl_display_disconnect(display);
printf("Disconnected from display\n");
return r;
}