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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GLES2/gl2.h>
#include <linux/input-event-codes.h>
#include <wayland-egl.h>
#include "egl_common.h"
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
/**
* structs storing global state
*
* These are passed around as user data in the wayland globals.
*/
struct egl_info {
struct wl_egl_window *egl_window;
struct wlr_egl_surface *egl_surface;
uint32_t width;
uint32_t height;
struct wl_surface *surface;
struct wl_callback *frame_callback;
};
struct window {
struct egl_info *egl_info;
struct wl_pointer *pointer;
struct zwp_locked_pointer_v1 *locked_pointer;
struct zwp_relative_pointer_v1 *relative_pointer;
int32_t pointer_x, pointer_y;
uint32_t last_draw;
};
/**
* surface handling and helpers
*
* draw_cursor and draw_relative_cursor draw a small 5px by 5px box around the
* cursor and relative motion coordinates respectively. draw_background colors
* the background black.
*
* The functions are somewhat duplicated, but it doesn't really matter.
*/
static void surface_callback_handle_done(void *data,
struct wl_callback *callback, uint32_t time) {
wl_callback_destroy(callback);
struct egl_info *e = data;
e->frame_callback = NULL;
}
static struct wl_callback_listener surface_callback_listener = {
.done = surface_callback_handle_done,
};
static void draw_init(struct egl_info *e) {
eglMakeCurrent(egl_display, e->egl_surface,
e->egl_surface, egl_context);
glViewport(0, 0, e->width, e->height);
}
static void draw_cursor(struct egl_info *e, int32_t x, int32_t y) {
glEnable(GL_SCISSOR_TEST);
glScissor(x, e->height - y, 5, 5);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f); /* white */
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);
}
static void draw_relative_cursor(struct egl_info *e, int32_t x, int32_t y) {
glEnable(GL_SCISSOR_TEST);
glScissor(x, e->height - y, 5, 5);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f); /* red */
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_SCISSOR_TEST);
}
static void draw_background(struct egl_info *e) {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); /* black */
glClear(GL_COLOR_BUFFER_BIT);
}
static void draw_end(struct egl_info *e) {
e->frame_callback = wl_surface_frame(e->surface);
wl_callback_add_listener(e->frame_callback, &surface_callback_listener, e);
eglSwapBuffers(egl_display, e->egl_surface);
}
/**
* registry and globals handling
*/
static struct wl_compositor *compositor = NULL;
static struct wl_seat *seat = NULL;
static struct xdg_wm_base *wm_base = NULL;
static struct zwp_pointer_constraints_v1 *pointer_constraints = NULL;
static struct zwp_relative_pointer_manager_v1 *relative_pointer_manager = NULL;
static void registry_handle_global(void *data, struct wl_registry *registry,
uint32_t name, const char *interface, uint32_t version) {
if (strcmp(interface, wl_compositor_interface.name) == 0) {
compositor = wl_registry_bind(registry, name,
&wl_compositor_interface, version);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
wm_base = wl_registry_bind(registry, name,
&xdg_wm_base_interface, version);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
seat = wl_registry_bind(registry, name,
&wl_seat_interface, version);
} else if (strcmp(interface, zwp_pointer_constraints_v1_interface.name) == 0) {
pointer_constraints = wl_registry_bind(registry, name,
&zwp_pointer_constraints_v1_interface, version);
} else if (strcmp(interface, zwp_relative_pointer_manager_v1_interface.name) == 0) {
relative_pointer_manager = wl_registry_bind(registry, name,
&zwp_relative_pointer_manager_v1_interface, version);
}
}
static void registry_handle_global_remove(void *data, struct wl_registry *registry,
uint32_t time) {
/* This space intentionally left blank */
}
static const struct wl_registry_listener registry_listener = {
.global = registry_handle_global,
.global_remove = registry_handle_global_remove,
};
/**
* xdg_surface handling
*/
static void xdg_surface_handle_configure(void *data,
struct xdg_surface *xdg_surface, uint32_t serial) {
struct egl_info *e = data;
xdg_surface_ack_configure(xdg_surface, serial);
wl_egl_window_resize(e->egl_window, e->width, e->height, 0, 0);
draw_init(e);
draw_background(e);
draw_end(e);
}
static const struct xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_handle_configure,
};
/**
* xdg_toplevel handling
*/
static void xdg_toplevel_handle_configure(void *data,
struct xdg_toplevel *xdg_toplevel, int32_t width, int32_t height,
struct wl_array *states) {
struct egl_info *e = data;
// TODO: Why do we get 0,0 on initialization here? (in rootston)
if (width == 0 && height == 0) {
return;
}
e->width = width;
e->height = height;
}
static void xdg_toplevel_handle_close(void *data,
struct xdg_toplevel *xdg_toplevel) {
egl_finish();
exit(EXIT_SUCCESS);
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_handle_configure,
.close = xdg_toplevel_handle_close,
};
/**
* zwp_locked_pointer handling
*
* Pointer unlocks need to be handled properly since the relative pointer needs
* to be released as well. Unlocks happen when the focus is changed, for
* example.
*/
static void locked_pointer_handle_locked(void *data,
struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1) {
/* This space intentionally left blank */
}
static void locked_pointer_handle_unlocked(void *data,
struct zwp_locked_pointer_v1 *zwp_locked_pointer_v1) {
struct window *w = data;
/* The locked pointer doesn't need to be destroyed since it was oneshot */
w->locked_pointer = NULL;
if (w->relative_pointer) {
/* Destroy the relative pointer */
zwp_relative_pointer_v1_destroy(w->relative_pointer);
w->relative_pointer = NULL;
}
draw_init(w->egl_info);
draw_background(w->egl_info);
draw_end(w->egl_info);
}
static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
.locked = locked_pointer_handle_locked,
.unlocked = locked_pointer_handle_unlocked,
};
/**
* zwp_relative_pointer handling
*
* Handling relative_motion events is the meat of the code. The handler simply
* tries to indicate what the deltas look like.
*/
static void relative_pointer_handle_relative_motion(void *data,
struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1,
uint32_t utime_hi, uint32_t utime_lo,
wl_fixed_t dx, wl_fixed_t dy,
wl_fixed_t dx_unaccel, wl_fixed_t dy_unaccel) {
struct window *w = data;
/**
* This renders the last location of the pointer (before it as locked), as
* well as what the position would have been after the given relative
* motion. Note that, if the device sends absolute motion events, the
* cursor location after relative motion is always identical to the actual
* cursor position.
*/
uint64_t utime = (((uint64_t) utime_hi << 32) + utime_lo) / 1000;
if (utime - w->last_draw > 30 && w->egl_info->frame_callback == NULL) {
w->last_draw = utime;
struct egl_info *e = w->egl_info;
draw_init(e);
draw_background(e);
draw_cursor(e, w->pointer_x, w->pointer_y);
draw_relative_cursor(e, w->pointer_x + wl_fixed_to_int(dx),
w->pointer_y + wl_fixed_to_int(dy));
draw_end(e);
}
}
static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
.relative_motion = relative_pointer_handle_relative_motion,
};
/**
* wl_pointer handling
*
* The client toggles between locking the pointer and receiving relative motion
* events, and releasing the locked pointer and falling back to normal motion
* events, on a mouse button one (left mouse button) click.
*
* It additionally removes the cursor image, and indicates the pointer location
* via a small white box.
*/
static void pointer_handle_button(void *data, struct wl_pointer *pointer,
uint32_t serial, uint32_t time, uint32_t button, uint32_t state_w) {
struct window *w = data;
struct egl_info *e = w->egl_info;
if (button == BTN_LEFT && state_w == WL_POINTER_BUTTON_STATE_PRESSED) {
if (w->locked_pointer == NULL && w->relative_pointer == NULL) {
/* Get a locked pointer and add listener */
w->locked_pointer = zwp_pointer_constraints_v1_lock_pointer(
pointer_constraints, w->egl_info->surface, w->pointer, NULL,
ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_ONESHOT);
zwp_locked_pointer_v1_add_listener(w->locked_pointer,
&locked_pointer_listener, w);
/* Get relative pointer and add listener */
w->relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(
relative_pointer_manager, w->pointer);
zwp_relative_pointer_v1_add_listener(w->relative_pointer,
&relative_pointer_listener, w);
/* Commit the surface and render */
wl_surface_commit(e->surface);
draw_init(e);
draw_background(e);
draw_cursor(e, w->pointer_x, w->pointer_y);
draw_end(e);
} else if (w->locked_pointer && w->relative_pointer) {
/* Destroy the locked pointer */
zwp_locked_pointer_v1_destroy(w->locked_pointer);
w->locked_pointer = NULL;
/* Destroy the relative pointer */
zwp_relative_pointer_v1_destroy(w->relative_pointer);
w->relative_pointer = NULL;
/* Render */
draw_init(e);
draw_background(e);
draw_cursor(e, w->pointer_x, w->pointer_y);
draw_end(e);
} else {
fprintf(stderr, "Unknown state!\n");
exit(EXIT_FAILURE);
}
}
}
static void pointer_handle_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) {
struct window *w = data;
wl_pointer_set_cursor(w->pointer, serial, NULL, 0, 0);
}
static void pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
uint32_t serial, struct wl_surface *surface) {
/* This space intentionally left blank */
}
static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
uint32_t time, wl_fixed_t surface_x, wl_fixed_t surface_y) {
struct window *w = data;
struct egl_info *e = w->egl_info;
w->pointer_x = wl_fixed_to_int(surface_x);
w->pointer_y = wl_fixed_to_int(surface_y);
if (time - w->last_draw > 30 && e->frame_callback == NULL) {
w->last_draw = time;
draw_init(e);
draw_background(e);
draw_cursor(e, w->pointer_x, w->pointer_y);
draw_end(e);
}
}
static void pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
uint32_t time, uint32_t axis, wl_fixed_t value) {
/* This space intentionally left blank */
}
static void pointer_handle_frame(void *data, struct wl_pointer *wl_pointer) {
/* This space intentionally left blank */
}
static void pointer_handle_axis_source(void *data,
struct wl_pointer *wl_pointer, uint32_t axis_source) {
/* This space intentionally left blank */
}
static void pointer_handle_axis_stop(void *data,
struct wl_pointer *wl_pointer, uint32_t time, uint32_t axis) {
/* This space intentionally left blank */
}
static void pointer_handle_axis_discrete(void *data,
struct wl_pointer *wl_pointer, uint32_t axis, int32_t discrete) {
/* This space intentionally left blank */
}
static const struct wl_pointer_listener pointer_listener = {
.enter = pointer_handle_enter,
.leave = pointer_handle_leave,
.motion = pointer_handle_motion,
.button = pointer_handle_button,
.axis = pointer_handle_axis,
.frame = pointer_handle_frame,
.axis_source = pointer_handle_axis_source,
.axis_stop = pointer_handle_axis_stop,
.axis_discrete = pointer_handle_axis_discrete,
};
/**
* relative-pointer:
*
* This client servers as an example for the relative-pointer-v1 protocol, and
* to some extent the pointer-constraints protocol as well (the locked_pointer
* interface).
*
* The intended behavior is the following. In the default state, the client
* shows a black background, and renders the pointer location as a small white
* box. No cursor is shown. In the locked state, the pointer is locked and the
* client only listens for relative motion events, which are rendered relative
* to the last unlocked pointer location by a small red box. Clicking with the
* left mouse button toggles the state.
*
* Most of the code is standard. The interesting stuff happens in "wl_pointer
* handling" (toggling states), and "zwp_relative_pointer handling" (rendering
* relative motion events).
*/
int main(int argc, char **argv) {
/* Connect to the display */
struct wl_display *display = wl_display_connect(NULL);
if (display == NULL) {
fprintf(stderr, "Could not connect to a Wayland display\n");
return EXIT_FAILURE;
}
/* Get the registry and set listeners */
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, ®istry_listener, NULL);
wl_display_roundtrip(display);
/* Check that all the global interfaces were captured */
if (compositor == NULL) {
fprintf(stderr, "wl_compositor not available\n");
return EXIT_FAILURE;
}
if (seat == NULL) {
fprintf(stderr, "wl_seat not available\n");
return EXIT_FAILURE;
}
if (wm_base == NULL) {
fprintf(stderr, "xdg_wm_base not available\n");
return EXIT_FAILURE;
}
if (pointer_constraints == NULL) {
fprintf(stderr, "zwp_pointer_constraints_v1 not available\n");
return EXIT_FAILURE;
}
if (relative_pointer_manager == NULL) {
fprintf(stderr, "zwp_relative_pointer_manager_v1 not available\n");
return EXIT_FAILURE;
}
/* Initialize EGL context */
struct egl_info *e = calloc(1, sizeof(*e));
e->width = e->height = 512;
egl_init(display);
/* Create the surface and xdg_toplevels, and set listeners */
struct wl_surface *surface = wl_compositor_create_surface(compositor);
struct xdg_surface *xdg_surface =
xdg_wm_base_get_xdg_surface(wm_base, surface);
struct xdg_toplevel *xdg_toplevel = xdg_surface_get_toplevel(xdg_surface);
xdg_surface_add_listener(xdg_surface, &xdg_surface_listener, e);
xdg_toplevel_add_listener(xdg_toplevel, &xdg_toplevel_listener, e);
/* Create the egl window and surface */
wl_surface_commit(surface);
e->egl_window = wl_egl_window_create(surface, e->width, e->height);
e->egl_surface = eglCreatePlatformWindowSurfaceEXT(
egl_display, egl_config, e->egl_window, NULL);
e->surface = surface;
wl_display_roundtrip(display);
/* Setup global state and render */
struct window *w = calloc(1, sizeof(*w));
w->egl_info = e;
draw_init(e);
draw_background(e);
draw_end(e);
/* Setup the pointer */
struct wl_pointer *pointer = wl_seat_get_pointer(seat);
wl_pointer_add_listener(pointer, &pointer_listener, w);
w->pointer = pointer;
/* Run display */
while (wl_display_dispatch(display) != -1) {
/* This space intentionally left blank */
}
return EXIT_SUCCESS;
}
|