summaryrefslogtreecommitdiff
path: root/tests/common/test_backends.c
blob: 74860c889ec4ec4f8a0c6a8698254a90f8685da1 (plain)
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
#define _POSIX_C_SOURCE 200809L
#include "yasl/native.h"
#if YASL_HAS_WAYLAND
#include "yasl/native/wayland.h"
#endif
#if YASL_HAS_X11
#include "yasl/native/x11.h"
#endif
#include "yasl/loop.h"
#include "yasl/yasl.h"

#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <assert.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>

struct window {
    struct yasl_surface *surface;
    struct yasl_display *display;
    int draws, configures, closes;
    bool destroy_on_draw, destroy_on_configure;
};

struct pump {
    struct yasl_loop *loop;
    struct yasl_display *displays[2];
    int ticks;
};

static int tick(int fd, short events, void *ud)
{
    (void)events;
    uint64_t expirations;
    assert(read(fd, &expirations, sizeof(expirations)) == sizeof(expirations));
    struct pump *p = ud;
    for (int i = 0; i < 2; i++) {
        if (p->displays[i]) {
            yasl_display_dispatch(p->displays[i], 0);
            yasl_display_flush(p->displays[i]);
        }
    }
    if (--p->ticks == 0) yasl_loop_quit(p->loop);
    return 0;
}

static void pump(struct yasl_loop *loop, struct yasl_display *a, struct yasl_display *b)
{
    int timer = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC);
    assert(timer >= 0);
    struct pump p = {.loop = loop, .displays = {a, b}, .ticks = 20};
    struct itimerspec spec = {.it_value.tv_nsec = 10000000, .it_interval.tv_nsec = 10000000};
    assert(timerfd_settime(timer, 0, &spec, NULL) == 0);
    int watch = yasl_loop_add_fd(loop, timer, POLLIN, tick, &p);
    assert(watch > 0);
    assert(yasl_loop_run(loop) == 0);
    assert(yasl_loop_del_fd(loop, watch) == 0);
    close(timer);
}

static void draw(struct yasl_surface *s, struct yasl_frame *frame, void *ud)
{
    struct window *w = ud;
    assert(s == w->surface);
    assert(frame->surface == s);
    assert(yasl_surface_display(s) == w->display);
    struct yasl_vector2i size = yasl_surface_geometry(s).size;
    assert(size.x == frame->target.size.x && size.y == frame->target.size.y);
    assert(frame->target.pixels);
    struct yasl_native_frame native = {0};
    assert(!yasl_surface_get_native_frame(s, &frame->target, &native));
    for (int i = 0; i < size.x * size.y; i++) frame->target.pixels[i] = 0xff4080c0;
    struct yasl_damage_list damage = {.n = YASL_DAMAGE_MAX + 1};
    assert(!yasl_surface_present(s, &frame->target, &damage));
    damage.n = -2;
    assert(!yasl_surface_present(s, &frame->target, &damage));
    damage.n = 1;
    damage.rects[0].width = -1;
    assert(!yasl_surface_present(s, &frame->target, &damage));
    damage.n = -1;
    struct yasl_framebuffer invalid = frame->target;
    invalid.size.x++;
    assert(!yasl_surface_present(s, &invalid, &damage));
    invalid = frame->target;
    invalid.generation--;
    assert(!yasl_surface_present(s, &invalid, &damage));
    invalid = frame->target;
    invalid.pixels = (uint32_t *)(uintptr_t)1;
    assert(!yasl_surface_present(s, &invalid, &damage));
    assert(!yasl_surface_present(s, &frame->target, NULL));
    assert(yasl_surface_present(s, &frame->target, &damage));
    assert(!yasl_surface_present(s, &frame->target, &damage));
    w->draws++;
    if (w->destroy_on_draw) { yasl_surface_destroy(s); w->surface = NULL; }
}

static void configure(struct yasl_surface *s, struct yasl_geometry geometry, void *ud)
{
    struct window *w = ud;
    assert(w->surface == s);
    struct yasl_vector2i size = yasl_surface_geometry(s).size;
    assert(geometry.size.x == size.x && geometry.size.y == size.y);
    assert(geometry.scale_120 == yasl_surface_geometry(s).scale_120);
    w->configures++;
    if (w->destroy_on_configure) { yasl_surface_destroy(s); w->surface = NULL; }
}

static void close_window(struct yasl_surface *s, void *ud)
{
    struct window *w = ud;
    assert(s == w->surface);
    w->closes++;
    yasl_surface_destroy(s);
    w->surface = NULL;
}

static const struct yasl_window_cbs callbacks = {
    .draw = draw, .configure = configure, .close = close_window,
};

static void attach(struct window *w)
{
    struct yasl_window_cbs temporary = callbacks;
    yasl_surface_set_callbacks(w->surface, &temporary, w);
    temporary.draw = NULL;
    temporary.configure = NULL;
    temporary.close = NULL;
}

static struct window create(struct yasl_display *d)
{
    struct yasl_window_spec spec = {.title = "Backend test", .size = {160, 100}};
    struct yasl_surface *s = yasl_surface_create_toplevel(d, &spec);
    assert(s);
    assert(yasl_display_get_native_handle(d));
    assert(yasl_surface_get_native_handle(s));
    assert(yasl_surface_display(s) == d);
    assert(yasl_surface_timing(s).source == YASL_TIMING_UNKNOWN);
    return (struct window){.surface = s, .display = d};
}

static void test_window_spec(struct yasl_loop *loop, struct yasl_display *d)
{
    struct window w = {.display = d, .destroy_on_draw = true};
    {
        struct yasl_window_cbs temporary = callbacks;
        struct yasl_window_spec spec = {
            .title = "Callbacks at creation", .size = {160, 100},
            .cbs = &temporary, .ud = &w,
        };
        w.surface = yasl_surface_create_toplevel(d, &spec);
        assert(w.surface);
        assert(!w.draws && !w.configures && !w.closes);
        /* Neither the spec nor its callback table needs to outlive creation. */
        memset(&temporary, 0, sizeof(temporary));
        memset(&spec, 0, sizeof(spec));
    }
    yasl_surface_schedule_redraw(w.surface);
    pump(loop, d, NULL);
    assert(w.draws == 1 && !w.surface);
}

static void x11_behavior(struct yasl_loop *loop, struct yasl_display *d, struct window *w)
{
    Display *native = yasl_display_get_native_handle(d);
    Window window = (Window)(uintptr_t)yasl_surface_get_native_handle(w->surface);
    assert(native && window);
    assert(yasl_x11_display_get_display(d) == native);
    assert(yasl_x11_surface_get_window(w->surface) == (uintptr_t)window);
    XSync(native, False);
    XImage *image = XGetImage(native, window, 0, 0, 1, 1, AllPlanes, ZPixmap);
    assert(image && (XGetPixel(image, 0, 0) & 0xffffff) == 0x4080c0);
    XDestroyImage(image);
    yasl_surface_set_title(w->surface, "Updated title");
    char *title = NULL;
    assert(XFetchName(native, window, &title));
    assert(strcmp(title, "Updated title") == 0);
    XFree(title);
    XResizeWindow(native, window, 200, 120);
    XFlush(native);
    pump(loop, d, NULL);
    assert(w->configures > 0);
    struct yasl_vector2i size = yasl_surface_geometry(w->surface).size;
    assert(size.x == 200 && size.y == 120);
    /* A server-generated motion verifies the XI2 subscription and cookie
     * dispatch path, including source-device lookup, rather than SendEvent. */
    struct yasl_event pointer_event;
    while (yasl_surface_poll_event(w->surface, &pointer_event)) {}
    XWarpPointer(native, None, window, 0, 0, 0, 0, 80, 50);
    XFlush(native);
    pump(loop, d, NULL);
    bool saw_motion = false;
    while (yasl_surface_poll_event(w->surface, &pointer_event)) {
        if (pointer_event.type == YASL_EVENT_POINTER_MOTION) {
            assert(pointer_event.pointer.device == YASL_PTR_DEV_MOUSE);
            assert(!pointer_event.pointer.pen.pressure);
            if (pointer_event.pointer.pos.x == 80 && pointer_event.pointer.pos.y == 50)
                saw_motion = true;
        }
    }
    assert(saw_motion);
    XEvent key = {0};
    key.xkey = (XKeyEvent){.type = KeyPress, .display = native, .window = window,
        .root = DefaultRootWindow(native), .same_screen = True,
        .keycode = XKeysymToKeycode(native, XK_a)};
    XSendEvent(native, window, False, KeyPressMask, &key);
    XFlush(native);
    yasl_display_copy(d, YASL_CLIPBOARD, "clipboard text");
    yasl_display_paste(d, YASL_CLIPBOARD, w->surface);
    pump(loop, d, NULL);
    bool saw_key = false, saw_paste = false, saw_text = false;
    struct yasl_event event;
    while (yasl_surface_poll_event(w->surface, &event)) {
        assert(event.surface == w->surface);
        if (event.type == YASL_EVENT_KEY) {
            assert(event.key.codepoint == 'a');
            assert(event.key.shifted == 'A');
            assert(event.key.native_code == key.xkey.keycode);
            saw_key = event.key.key == YASL_KEY_A;
        }
        if (event.type == YASL_EVENT_PASTE) {
            assert(event.text.len == strlen("clipboard text"));
            assert(memcmp(event.text.data, "clipboard text", event.text.len) == 0);
            saw_paste = true;
        }
        if (event.type == YASL_EVENT_TEXT) {
            assert(event.text.len == 1 && event.text.data[0] == 'a');
            saw_text = true;
        }
    }
    assert(saw_key && saw_paste && saw_text);
    /* Physical identity survives modifier and layout changes, including a
     * release whose modifiers differ from its press. */
    key.xkey.state = ShiftMask;
    XSendEvent(native, window, False, KeyPressMask, &key);
    key.xkey.type = KeyRelease;
    key.xkey.state = 0;
    XSendEvent(native, window, False, KeyReleaseMask, &key);
    XFlush(native);
    pump(loop, d, NULL);
    int key_count = 0, text_count = 0;
    while (yasl_surface_poll_event(w->surface, &event)) {
        if (event.type == YASL_EVENT_KEY) {
            assert(event.key.key == YASL_KEY_A);
            assert(event.key.action == (key_count ? YASL_KEY_RELEASE : YASL_KEY_PRESS));
            key_count++;
        } else if (event.type == YASL_EVENT_TEXT) {
            assert(event.text.len == 1 && event.text.data[0] == 'A');
            text_count++;
        }
    }
    assert(key_count == 2 && text_count == 1);
    key.xkey.type = KeyPress;
    key.xkey.state = ControlMask;
    XSendEvent(native, window, False, KeyPressMask, &key);
    XFlush(native);
    pump(loop, d, NULL);
    assert(yasl_surface_poll_event(w->surface, &event));
    assert(event.type == YASL_EVENT_KEY && event.key.key == YASL_KEY_A);
    assert(event.key.mods & YASL_MOD_CTRL);
    assert(!yasl_surface_poll_event(w->surface, &event));

    int symbols_per_key = 0;
    KeySym *saved_symbols = XGetKeyboardMapping(native, key.xkey.keycode, 1, &symbols_per_key);
    assert(saved_symbols && symbols_per_key >= 2);
    KeySym *changed_symbols = calloc((size_t)symbols_per_key, sizeof(KeySym));
    assert(changed_symbols);
    changed_symbols[0] = XK_q;
    changed_symbols[1] = XK_Q;
    XChangeKeyboardMapping(native, key.xkey.keycode, symbols_per_key, changed_symbols, 1);
    XSync(native, False);
    pump(loop, d, NULL); /* Consume MappingNotify before translating the key. */
    key.xkey.state = 0;
    XSendEvent(native, window, False, KeyPressMask, &key);
    XFlush(native);
    pump(loop, d, NULL);
    saw_key = saw_text = false;
    while (yasl_surface_poll_event(w->surface, &event)) {
        if (event.type == YASL_EVENT_KEY) {
            assert(event.key.key == YASL_KEY_A && event.key.codepoint == 'q');
            saw_key = true;
        } else if (event.type == YASL_EVENT_TEXT) {
            assert(event.text.len == 1 && event.text.data[0] == 'q');
            saw_text = true;
        }
    }
    assert(saw_key && saw_text);
    XChangeKeyboardMapping(native, key.xkey.keycode, symbols_per_key, saved_symbols, 1);
    free(changed_symbols);
    XFree(saved_symbols);
    XSync(native, False);
    pump(loop, d, NULL);

    for (unsigned button = 4; button <= 7; button++) {
        XEvent wheel = {.xbutton = {.type = ButtonPress, .display = native,
            .window = window, .root = DefaultRootWindow(native),
            .same_screen = True, .button = button}};
        /* Direct delivery exercises the core fallback even with XI2 enabled. */
        XSendEvent(native, window, False, NoEventMask, &wheel);
        wheel.xbutton.type = ButtonRelease;
        XSendEvent(native, window, False, NoEventMask, &wheel);
        XFlush(native);
        pump(loop, d, NULL);
        assert(yasl_surface_poll_event(w->surface, &event));
        assert(event.type == YASL_EVENT_POINTER_SCROLL);
        assert(event.pointer.scroll.x == (button == 6 ? -1 : button == 7 ? 1 : 0));
        assert(event.pointer.scroll.y == (button == 4 ? -1 : button == 5 ? 1 : 0));
        assert(!yasl_surface_poll_event(w->surface, &event));
    }
    assert(yasl_surface_timing(w->surface).source == YASL_TIMING_SUBMISSION);
    assert(!(yasl_display_caps(d) & YASL_CAP_FRAME_PACING));

    /* Reuse caller storage, replace the selection, and exceed the old queue
     * cap before polling: each paste must retain its own original contents. */
    for (int i = 0; i < 300; i++) {
        char text[32];
        snprintf(text, sizeof(text), "owned-%d", i);
        yasl_display_copy(d, YASL_CLIPBOARD, text);
        yasl_display_paste(d, YASL_CLIPBOARD, w->surface);
        memset(text, 'x', sizeof(text));
    }
    for (int i = 0; i < 300; i++) {
        char text[32];
        snprintf(text, sizeof(text), "owned-%d", i);
        assert(yasl_surface_poll_event(w->surface, &event));
        assert(event.type == YASL_EVENT_PASTE && strcmp(event.text.data, text) == 0);
    }
    const char *last = event.text.data;
    assert(!yasl_surface_poll_event(w->surface, &event));
    yasl_display_copy(d, YASL_CLIPBOARD, "changed");
    pump(loop, d, NULL);
    assert(strcmp(last, "owned-299") == 0);
    yasl_display_copy(d, YASL_CLIPBOARD, "");
    yasl_display_paste(d, YASL_CLIPBOARD, w->surface);
    assert(yasl_surface_poll_event(w->surface, &event));
    assert(event.type == YASL_EVENT_PASTE && !event.text.len && !event.text.data[0]);

    /* Concurrent external conversions must not share a mutable target slot. */
    struct yasl_display_spec ds = {.backend = YASL_BACKEND_X11};
    struct yasl_display *owner = yasl_display_open(loop, &ds);
    assert(owner);
    yasl_display_copy(owner, YASL_CLIPBOARD, "external");
    struct window other = create(d), cancelled = create(d);
    yasl_display_paste(d, YASL_CLIPBOARD, w->surface);
    yasl_display_paste(d, YASL_CLIPBOARD, other.surface);
    yasl_display_paste(d, YASL_CLIPBOARD, cancelled.surface);
    yasl_surface_destroy(cancelled.surface);
    pump(loop, d, owner);
    int first_pastes = 0, second_pastes = 0;
    while (yasl_surface_poll_event(w->surface, &event)) {
        if (event.type == YASL_EVENT_PASTE) {
            assert(strcmp(event.text.data, "external") == 0);
            first_pastes++;
        }
    }
    while (yasl_surface_poll_event(other.surface, &event)) {
        if (event.type == YASL_EVENT_PASTE) {
            assert(strcmp(event.text.data, "external") == 0);
            second_pastes++;
        }
    }
    assert(first_pastes == 1 && second_pastes == 1);
    yasl_surface_destroy(other.surface);
    yasl_display_close(owner);
    key.xclient = (XClientMessageEvent){.type = ClientMessage, .display = native,
        .window = window, .message_type = XInternAtom(native, "WM_PROTOCOLS", False),
        .format = 32, .data.l = {(long)XInternAtom(native, "WM_DELETE_WINDOW", False)}};
    XSendEvent(native, window, False, NoEventMask, &key);
    XFlush(native);
    pump(loop, d, NULL);
    assert(w->closes == 1 && !w->surface);
}

int main(int argc, char **argv)
{
    assert(argc >= 2);
    struct yasl_loop *loop = yasl_loop_create();
    assert(loop);
    if (strcmp(argv[1], "failure") == 0) {
        struct yasl_display_spec spec = {.backend = (enum yasl_backend)atoi(argv[2])};
        assert(!yasl_display_open(loop, &spec));
    } else if (strcmp(argv[1], "mixed") == 0) {
        struct yasl_display_spec spec = {.backend = YASL_BACKEND_WAYLAND};
        struct yasl_display *a = yasl_display_open(loop, &spec);
        spec.backend = YASL_BACKEND_X11;
        struct yasl_display *b = yasl_display_open(loop, &spec);
        assert(a && b);
        test_window_spec(loop, a);
        test_window_spec(loop, b);
        struct window wa = create(a), wb = create(b);
#if YASL_HAS_WAYLAND
        assert(yasl_wayland_display_get_wl_display(a) != NULL);
        assert(yasl_wayland_surface_get_wl_surface(wa.surface) != NULL);
        assert(!yasl_x11_display_get_display(a));
#endif
#if YASL_HAS_X11
        assert(yasl_x11_display_get_display(b) != NULL);
        assert(yasl_x11_surface_get_window(wb.surface) != 0);
        assert(!yasl_wayland_display_get_wl_display(b));
#endif
        attach(&wa);
        attach(&wb);
        pump(loop, a, b);
        assert(wa.draws && wb.draws);
        yasl_surface_destroy(wa.surface);
        yasl_surface_destroy(wb.surface);
        yasl_display_close(a);
        yasl_display_close(b);
    } else {
        assert(argc == 4);
        struct yasl_display_spec spec = {.backend = (enum yasl_backend)atoi(argv[2])};
        struct yasl_display *d = strcmp(argv[1], "null") == 0 ?
                yasl_display_open(loop, NULL) : yasl_display_open(loop, &spec);
        assert(d);
        assert(yasl_display_backend(d) == (enum yasl_backend)atoi(argv[3]));
        struct window w = create(d);
        pump(loop, d, NULL);
        assert(w.draws == 0);
        attach(&w);
        yasl_surface_schedule_redraw(w.surface);
        pump(loop, d, NULL);
        assert(w.draws > 0);
        if (yasl_display_backend(d) == YASL_BACKEND_X11)
            x11_behavior(loop, d, &w);
        if (w.surface) yasl_surface_destroy(w.surface);
        w = create(d);
        w.destroy_on_draw = true;
        attach(&w);
        pump(loop, d, NULL);
        assert(w.draws && !w.surface);
        yasl_display_close(d);
    }
    yasl_loop_destroy(loop);
    puts("Backend test passed.");
    return 0;
}