-
Notifications
You must be signed in to change notification settings - Fork 1
/
wayland.cc
512 lines (446 loc) · 15.3 KB
/
wayland.cc
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
#include "wayland.hh"
#include "err.hpp"
#include "io/io.hh"
#include <stdio.h>
#include <sys/mman.h>
#include <QApplication>
namespace cornus::wayland {
static void
wl_buffer_release(void *data, struct wl_buffer *wl_buffer)
{
//mtl_info("Releasing buffer");
/* Sent by the compositor when it's no longer using this buffer */
wl_buffer_destroy(wl_buffer);
}
static const struct wl_buffer_listener wl_buffer_listener = {
.release = wl_buffer_release,
};
static void
xdg_surface_configure(void *data,
struct xdg_surface *xdg_surface, u32 serial)
{
ClientState *state = (ClientState*)data;
xdg_surface_ack_configure(xdg_surface, serial);
struct wl_buffer *buffer = DrawFrame(state);
wl_surface_attach(state->surface, buffer, 0, 0);
wl_surface_commit(state->surface);
}
static const struct xdg_surface_listener xdg_surface_listener = {
.configure = xdg_surface_configure,
};
static void
xdg_wm_base_ping(void *data, struct xdg_wm_base *xdg_wm_base, u32 serial)
{
xdg_wm_base_pong(xdg_wm_base, serial);
}
static const struct xdg_wm_base_listener xdg_wm_base_listener = {
.ping = xdg_wm_base_ping,
};
static const struct wl_callback_listener wl_surface_frame_listener = {
.done = wl_surface_frame_done,
};
void wl_surface_frame_done(void *data, struct wl_callback *cb, u32 time)
{
/* Destroy this callback */
wl_callback_destroy(cb);
/* Request another frame */
struct ClientState *state = (ClientState*)data;
cb = wl_surface_frame(state->surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, state);
/* Update scroll amount at 24 pixels per second */
if (state->last_frame != 0) {
int elapsed = time - state->last_frame;
state->offset += elapsed / 1000.0 * 24;
}
/* Submit a frame for this event */
struct wl_buffer *buffer = DrawFrame(state);
wl_surface_attach(state->surface, buffer, 0, 0);
wl_surface_damage_buffer(state->surface, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_commit(state->surface);
state->last_frame = time;
}
static void
wl_pointer_enter(void *data, struct wl_pointer *wl_pointer,
u32 serial, struct wl_surface *surface,
wl_fixed_t surface_x, wl_fixed_t surface_y)
{
struct ClientState *client_state = (ClientState*)data;
client_state->pointer_event.event_mask |= POINTER_EVENT_ENTER;
client_state->pointer_event.serial = serial;
client_state->pointer_event.surface_x = surface_x,
client_state->pointer_event.surface_y = surface_y;
}
static void
wl_pointer_leave(void *data, struct wl_pointer *wl_pointer,
u32 serial, struct wl_surface *surface)
{
ClientState *client_state = (ClientState*)data;
client_state->pointer_event.serial = serial;
client_state->pointer_event.event_mask |= POINTER_EVENT_LEAVE;
}
static void
wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, u32 time,
wl_fixed_t surface_x, wl_fixed_t surface_y)
{
ClientState *client_state = (ClientState*)data;
client_state->pointer_event.event_mask |= POINTER_EVENT_MOTION;
client_state->pointer_event.time = time;
client_state->pointer_event.surface_x = surface_x,
client_state->pointer_event.surface_y = surface_y;
}
static void
wl_pointer_button(void *data, struct wl_pointer *wl_pointer, u32 serial,
u32 time, u32 button, u32 state)
{
ClientState *client_state = (ClientState*)data;
client_state->pointer_event.event_mask |= POINTER_EVENT_BUTTON;
client_state->pointer_event.time = time;
client_state->pointer_event.serial = serial;
client_state->pointer_event.button = button,
client_state->pointer_event.state = state;
}
static void
wl_pointer_axis(void *data, struct wl_pointer *wl_pointer, u32 time,
u32 axis, wl_fixed_t value)
{
ClientState *client_state = (ClientState*)data;
client_state->pointer_event.event_mask |= POINTER_EVENT_AXIS;
client_state->pointer_event.time = time;
client_state->pointer_event.axes[axis].valid = true;
client_state->pointer_event.axes[axis].value = value;
}
static void
wl_pointer_axis_source(void *data, struct wl_pointer *wl_pointer,
u32 axis_source)
{
ClientState *client_state = (ClientState*)data;
client_state->pointer_event.event_mask |= POINTER_EVENT_AXIS_SOURCE;
client_state->pointer_event.axis_source = axis_source;
}
static void
wl_pointer_axis_stop(void *data, struct wl_pointer *wl_pointer,
u32 time, u32 axis)
{
ClientState *client_state = (ClientState*)data;
client_state->pointer_event.time = time;
client_state->pointer_event.event_mask |= POINTER_EVENT_AXIS_STOP;
client_state->pointer_event.axes[axis].valid = true;
}
static void
wl_pointer_axis_discrete(void *data, struct wl_pointer *wl_pointer,
u32 axis, i32 discrete)
{
ClientState *client_state = (ClientState*)data;
client_state->pointer_event.event_mask |= POINTER_EVENT_AXIS_DISCRETE;
client_state->pointer_event.axes[axis].valid = true;
client_state->pointer_event.axes[axis].discrete = discrete;
}
static void
wl_pointer_frame(void *data, struct wl_pointer *wl_pointer)
{
ClientState *client_state = (ClientState*)data;
struct pointer_event *event = &client_state->pointer_event;
fprintf(stderr, "pointer frame @ %d: ", event->time);
if (event->event_mask & POINTER_EVENT_ENTER) {
fprintf(stderr, "entered %f, %f ",
wl_fixed_to_double(event->surface_x),
wl_fixed_to_double(event->surface_y));
}
if (event->event_mask & POINTER_EVENT_LEAVE) {
fprintf(stderr, "leave");
}
if (event->event_mask & POINTER_EVENT_MOTION) {
fprintf(stderr, "motion %f, %f ",
wl_fixed_to_double(event->surface_x),
wl_fixed_to_double(event->surface_y));
}
if (event->event_mask & POINTER_EVENT_BUTTON) {
const char *state = event->state == WL_POINTER_BUTTON_STATE_RELEASED ?
"released" : "pressed";
fprintf(stderr, "button %d %s ", event->button, state);
}
u32 axis_events = POINTER_EVENT_AXIS
| POINTER_EVENT_AXIS_SOURCE
| POINTER_EVENT_AXIS_STOP
| POINTER_EVENT_AXIS_DISCRETE;
const char *axis_name[2] = {
[WL_POINTER_AXIS_VERTICAL_SCROLL] = "vertical",
[WL_POINTER_AXIS_HORIZONTAL_SCROLL] = "horizontal",
};
const char *axis_source[4] = {
[WL_POINTER_AXIS_SOURCE_WHEEL] = "wheel",
[WL_POINTER_AXIS_SOURCE_FINGER] = "finger",
[WL_POINTER_AXIS_SOURCE_CONTINUOUS] = "continuous",
[WL_POINTER_AXIS_SOURCE_WHEEL_TILT] = "wheel tilt",
};
if (event->event_mask & axis_events) {
for (size_t i = 0; i < 2; ++i) {
if (!event->axes[i].valid) {
continue;
}
fprintf(stderr, "%s axis ", axis_name[i]);
if (event->event_mask & POINTER_EVENT_AXIS) {
fprintf(stderr, "value %f ", wl_fixed_to_double(
event->axes[i].value));
}
if (event->event_mask & POINTER_EVENT_AXIS_DISCRETE) {
fprintf(stderr, "discrete %d ",
event->axes[i].discrete);
}
if (event->event_mask & POINTER_EVENT_AXIS_SOURCE) {
fprintf(stderr, "via %s ",
axis_source[event->axis_source]);
}
if (event->event_mask & POINTER_EVENT_AXIS_STOP) {
fprintf(stderr, "(stopped) ");
}
}
}
fprintf(stderr, "\n");
memset(event, 0, sizeof(*event));
}
static const struct wl_pointer_listener wl_pointer_listener = {
.enter = wl_pointer_enter,
.leave = wl_pointer_leave,
.motion = wl_pointer_motion,
.button = wl_pointer_button,
.axis = wl_pointer_axis,
.frame = wl_pointer_frame,
.axis_source = wl_pointer_axis_source,
.axis_stop = wl_pointer_axis_stop,
.axis_discrete = wl_pointer_axis_discrete,
};
static void
wl_keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,
u32 format, i32 fd, u32 size)
{
struct ClientState *client_state = (ClientState*)data;
assert(format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1);
char *map_shm = (char*)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
assert(map_shm != MAP_FAILED);
struct xkb_keymap *xkb_keymap = xkb_keymap_new_from_string(
client_state->xkb_context, map_shm,
XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(map_shm, size);
close(fd);
struct xkb_state *xkb_state = xkb_state_new(xkb_keymap);
xkb_keymap_unref(client_state->xkb_keymap);
xkb_state_unref(client_state->xkb_state);
client_state->xkb_keymap = xkb_keymap;
client_state->xkb_state = xkb_state;
}
static void
wl_keyboard_enter(void *data, struct wl_keyboard *wl_keyboard,
u32 serial, struct wl_surface *surface,
struct wl_array *keys)
{
struct ClientState *client_state = (ClientState*)data;
fprintf(stderr, "keyboard enter; keys pressed are:\n");
u32 *key;
//wl_array_for_each(key, keys) {
WL_ARRAY_FOR_EACH(key, keys, u32*) {
char buf[128];
xkb_keysym_t sym = xkb_state_key_get_one_sym(
client_state->xkb_state, *key + 8);
xkb_keysym_get_name(sym, buf, sizeof(buf));
fprintf(stderr, "sym: %-12s (%d), ", buf, sym);
xkb_state_key_get_utf8(client_state->xkb_state,
*key + 8, buf, sizeof(buf));
fprintf(stderr, "utf8: '%s'\n", buf);
}
}
static void
wl_keyboard_key(void *data, struct wl_keyboard *wl_keyboard,
u32 serial, u32 time, u32 key, u32 state)
{
struct ClientState *client_state = (ClientState*)data;
char buf[128];
u32 keycode = key + 8;
xkb_keysym_t sym = xkb_state_key_get_one_sym(
client_state->xkb_state, keycode);
xkb_keysym_get_name(sym, buf, sizeof(buf));
const char *action =
state == WL_KEYBOARD_KEY_STATE_PRESSED ? "press" : "release";
fprintf(stderr, "key %s: sym: %-12s (%d), ", action, buf, sym);
xkb_state_key_get_utf8(client_state->xkb_state, keycode,
buf, sizeof(buf));
fprintf(stderr, "utf8: '%s'\n", buf);
}
static void
wl_keyboard_leave(void *data, struct wl_keyboard *wl_keyboard,
u32 serial, struct wl_surface *surface)
{
fprintf(stderr, "keyboard leave\n");
}
static void
wl_keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard,
u32 serial, u32 mods_depressed,
u32 mods_latched, u32 mods_locked,
u32 group)
{
struct ClientState *client_state = (ClientState*)data;
xkb_state_update_mask(client_state->xkb_state,
mods_depressed, mods_latched, mods_locked, 0, 0, group);
}
static void
wl_keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
i32 rate, i32 delay)
{
/* Left as an exercise for the reader */
}
static const struct wl_keyboard_listener wl_keyboard_listener = {
.keymap = wl_keyboard_keymap,
.enter = wl_keyboard_enter,
.leave = wl_keyboard_leave,
.key = wl_keyboard_key,
.modifiers = wl_keyboard_modifiers,
.repeat_info = wl_keyboard_repeat_info,
};
static void
wl_seat_capabilities(void *data, struct wl_seat *wl_seat, u32 capabilities)
{
struct ClientState *state = (ClientState*)data;
cbool have_pointer = capabilities & WL_SEAT_CAPABILITY_POINTER;
if (have_pointer && (!state->wl_pointer)) {
state->wl_pointer = wl_seat_get_pointer(state->wl_seat);
wl_pointer_add_listener(state->wl_pointer,
&wl_pointer_listener, state);
} else if (!have_pointer && state->wl_pointer) {
wl_pointer_release(state->wl_pointer);
state->wl_pointer = 0;
}
cbool have_keyboard = capabilities & WL_SEAT_CAPABILITY_KEYBOARD;
if (have_keyboard && (!state->wl_keyboard)) {
state->wl_keyboard = wl_seat_get_keyboard(state->wl_seat);
wl_keyboard_add_listener(state->wl_keyboard,
&wl_keyboard_listener, state);
} else if (!have_keyboard && state->wl_keyboard) {
wl_keyboard_release(state->wl_keyboard);
state->wl_keyboard = 0;
}
}
static void
wl_seat_name(void *data, struct wl_seat *wl_seat, const char *name)
{
struct ClientState *state = (ClientState*)data;
fprintf(stderr, "seat name: %s\n", name);
}
static const struct wl_seat_listener wl_seat_listener = {
.capabilities = wl_seat_capabilities,
.name = wl_seat_name,
};
static void registry_handle_global(void *data, struct wl_registry *registry,
u32 name, const char *interface, u32 version)
{
//mtl_info("Interface: %-30s version: %d, name: %d\n", interface, version, name);
auto *state = (struct ClientState*)data;
if (strcmp(interface, wl_compositor_interface.name) == 0) {
state->compositor = (struct wl_compositor*)wl_registry_bind(
registry, name, &wl_compositor_interface, 4);
} else if (strcmp(interface, wl_shm_interface.name) == 0) {
state->shm = (struct wl_shm*) wl_registry_bind(
registry, name, &wl_shm_interface, 1);
} else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
state->xdg_wm_base = (xdg_wm_base*)wl_registry_bind(
registry, name, &xdg_wm_base_interface, 1);
xdg_wm_base_add_listener(state->xdg_wm_base,
&xdg_wm_base_listener, state);
} else if (strcmp(interface, wl_seat_interface.name) == 0) {
state->wl_seat = (wl_seat*)wl_registry_bind(
registry, name, &wl_seat_interface, 7);
wl_seat_add_listener(state->wl_seat,
&wl_seat_listener, state);
}
}
static void
registry_handle_global_remove(void *data, struct wl_registry *registry,
u32 name)
{
// This space deliberately left blank
}
static const struct wl_registry_listener registry_listener = {
.global = registry_handle_global,
.global_remove = registry_handle_global_remove,
};
void xdg_toplevel_configure(void *data,
struct xdg_toplevel *xdg_toplevel, i32 width, i32 height,
struct wl_array *states)
{
struct ClientState *state = (struct ClientState*)data;
if (width == 0 || height == 0) {
// Compositor is deferring to us
state->width = 640;
state->height= 480;
return;
}
state->width = width;
state->height = height;
}
void xdg_toplevel_close(void *data, struct xdg_toplevel *toplevel)
{
struct ClientState *state = (struct ClientState*)data;
state->closed = true;
mtl_warn("Received window close event");
QApplication::quit();
exit(EXIT_SUCCESS);
}
static const struct xdg_toplevel_listener xdg_toplevel_listener = {
.configure = xdg_toplevel_configure,
.close = xdg_toplevel_close,
};
wl_buffer* DrawFrame(struct ClientState *state)
{
cint width = state->width, height = state->height;
cint stride = width * 4;
cint pool_size = height * stride;
cint fd = cornus::io::allocate_shm_file(pool_size);
u32 *pool_data = (u32*)mmap(NULL, pool_size,
PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (pool_data == MAP_FAILED) {
close(fd);
return 0;
}
struct wl_shm_pool *pool = wl_shm_create_pool(state->shm, fd, pool_size);
struct wl_buffer *buffer = wl_shm_pool_create_buffer(pool, 0,
width, height, stride, WL_SHM_FORMAT_XRGB8888);
wl_shm_pool_destroy(pool);
close(fd);
u32 *pixels = pool_data;
// memset(pixels, 0xFF, stride * height); //white
int offset = (int)state->offset % 8;
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
cbool flag = ((x+offset) + (y+offset) / 8 * 8) % 16 < 8;
pixels[y * width + x] = flag ? 0xFF666666 : 0xFFEEEEEE;
}
}
munmap(pool_data, pool_size);
wl_buffer_add_listener(buffer, &wl_buffer_listener, NULL);
return buffer;
}
wl_display* test(cornus::App *app)
{
struct ClientState state = {};
state.app = app;
state.display = wl_display_connect(NULL);
state.registry = wl_display_get_registry(state.display);
state.xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
wl_registry_add_listener(state.registry, ®istry_listener, &state);
// Block until all pending requests are processed by the server
wl_display_roundtrip(state.display);
state.surface = wl_compositor_create_surface(state.compositor);
state.xdg_surface = xdg_wm_base_get_xdg_surface(
state.xdg_wm_base, state.surface);
xdg_surface_add_listener(state.xdg_surface, &xdg_surface_listener, &state);
state.xdg_toplevel = xdg_surface_get_toplevel(state.xdg_surface);
xdg_toplevel_set_title(state.xdg_toplevel, "Example client");
wl_surface_commit(state.surface);
struct wl_callback *cb = wl_surface_frame(state.surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, &state);
xdg_toplevel_add_listener(state.xdg_toplevel, &xdg_toplevel_listener, &state);
while (wl_display_dispatch(state.display)) {
/* This space deliberately left blank */
}
return state.display;
}
}