#define _POSIX_C_SOURCE 200809L #include "common/private_native.h" #if YASL_HAS_WAYLAND #include "common/private_native_wayland.h" #endif #if YASL_HAS_X11 #include "common/private_native_x11.h" #endif #include "common/private_loop.h" #include "common/types.h" #include #include #include #include #include #include #include #include #include #include #include #include struct window { struct yasl_surface *surface; struct yasl_private_display *display; int draws, configures, closes; bool destroy_on_draw, destroy_on_configure; }; struct pump { struct yasl_private_loop *loop; struct yasl_private_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_private_display_dispatch(p->displays[i], 0); yasl_private_display_flush(p->displays[i]); } } if (--p->ticks == 0) yasl_private_loop_quit(p->loop); return 0; } static void pump(struct yasl_private_loop *loop, struct yasl_private_display *a, struct yasl_private_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_private_loop_add_fd(loop, timer, POLLIN, tick, &p); assert(watch > 0); assert(yasl_private_loop_run(loop) == 0); assert(yasl_private_loop_del_fd(loop, watch) == 0); close(timer); } static void draw(struct yasl_surface *s, struct yasl_private_frame *frame, void *ud) { struct window *w = ud; assert(s == w->surface); assert(frame->surface == s); assert(yasl_private_surface_display(s) == w->display); struct yasl_private_vector2i size = yasl_private_surface_geometry(s).size; assert(size.x == frame->target.size.x && size.y == frame->target.size.y); assert(frame->target.pixels); struct yasl_private_native_frame native = {0}; assert(!yasl_private_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_private_damage_list damage = {.n = YASL_PRIVATE_DAMAGE_MAX + 1}; assert(!yasl_private_surface_present(s, &frame->target, &damage)); damage.n = -2; assert(!yasl_private_surface_present(s, &frame->target, &damage)); damage.n = 1; damage.rects[0].width = -1; assert(!yasl_private_surface_present(s, &frame->target, &damage)); damage.n = -1; struct yasl_private_framebuffer invalid = frame->target; invalid.size.x++; assert(!yasl_private_surface_present(s, &invalid, &damage)); invalid = frame->target; invalid.generation--; assert(!yasl_private_surface_present(s, &invalid, &damage)); invalid = frame->target; invalid.pixels = (uint32_t *)(uintptr_t)1; assert(!yasl_private_surface_present(s, &invalid, &damage)); assert(!yasl_private_surface_present(s, &frame->target, NULL)); assert(yasl_private_surface_present(s, &frame->target, &damage)); assert(!yasl_private_surface_present(s, &frame->target, &damage)); w->draws++; if (w->destroy_on_draw) { yasl_private_surface_destroy(s); w->surface = NULL; } } static void configure(struct yasl_surface *s, struct yasl_private_geometry geometry, void *ud) { struct window *w = ud; assert(w->surface == s); struct yasl_private_vector2i size = yasl_private_surface_geometry(s).size; assert(geometry.size.x == size.x && geometry.size.y == size.y); assert(geometry.scale_120 == yasl_private_surface_geometry(s).scale_120); w->configures++; if (w->destroy_on_configure) { yasl_private_surface_destroy(s); w->surface = NULL; } } static void draw_without_present(struct yasl_surface *s, struct yasl_private_frame *frame, void *ud) { (void)s; struct window *w = ud; size_t count = (size_t)frame->target.size.x * (size_t)frame->target.size.y; for (size_t i = 0; i < count; i++) frame->target.pixels[i] = 0xff000000; w->draws++; } static void close_window(struct yasl_surface *s, void *ud) { struct window *w = ud; assert(s == w->surface); w->closes++; yasl_private_surface_destroy(s); w->surface = NULL; } static const struct yasl_private_window_cbs callbacks = { .draw = draw, .configure = configure, .close = close_window, }; static void attach(struct window *w) { struct yasl_private_window_cbs temporary = callbacks; yasl_private_surface_set_callbacks(w->surface, &temporary, w); temporary.draw = NULL; temporary.configure = NULL; temporary.close = NULL; } static struct window create(struct yasl_private_display *d) { struct yasl_private_window_spec spec = {.title = "Backend test", .size = {160, 100}}; struct yasl_surface *s = yasl_private_surface_create_toplevel(d, &spec); assert(s); assert(yasl_private_display_get_native_handle(d)); assert(yasl_private_surface_get_native_handle(s)); assert(yasl_private_surface_display(s) == d); assert(yasl_private_surface_timing(s).source == YASL_PRIVATE_TIMING_UNKNOWN); return (struct window){.surface = s, .display = d}; } /* The last presented frame reads back until a later draw replaces it. */ static void test_read_pixels(struct yasl_private_loop *loop, struct yasl_private_display *d, struct window *w) { struct yasl_private_vector2i size = {-1, -1}; assert(!yasl_private_surface_read_pixels(w->surface, NULL, 0, NULL)); assert(!yasl_private_surface_read_pixels(NULL, NULL, 0, &size) && !size.x && !size.y); assert(!yasl_private_surface_read_pixels(w->surface, NULL, 0, &size)); struct yasl_private_vector2i extent = yasl_private_surface_geometry(w->surface).size; assert(size.x == extent.x && size.y == extent.y); size_t count = (size_t)size.x * (size_t)size.y; uint32_t *pixels = calloc(count, sizeof(*pixels)); assert(pixels); assert(!yasl_private_surface_read_pixels(w->surface, pixels, count - 1, &size)); assert(!pixels[0]); assert(yasl_private_surface_read_pixels(w->surface, pixels, count, &size)); for (size_t i = 0; i < count; i++) assert(pixels[i] == 0xff4080c0); /* Draws that never present must not show through. X11 reuses its one * target; Wayland keeps the presented buffer while the compositor holds it. */ struct yasl_private_window_cbs skip = callbacks; skip.draw = draw_without_present; yasl_private_surface_set_callbacks(w->surface, &skip, w); for (int i = 0; i < 4; i++) { yasl_private_surface_schedule_redraw(w->surface); pump(loop, d, NULL); if (yasl_private_surface_read_pixels(w->surface, pixels, count, &size)) { assert(yasl_private_display_backend(d) != YASL_PRIVATE_BACKEND_X11); for (size_t j = 0; j < count; j++) assert(pixels[j] == 0xff4080c0); } else { assert(!size.x && !size.y); } } attach(w); yasl_private_surface_schedule_redraw(w->surface); pump(loop, d, NULL); memset(pixels, 0, count * sizeof(*pixels)); assert(yasl_private_surface_read_pixels(w->surface, pixels, count, &size)); assert(pixels[0] == 0xff4080c0 && pixels[count - 1] == 0xff4080c0); free(pixels); } static void test_window_spec(struct yasl_private_loop *loop, struct yasl_private_display *d) { struct window w = {.display = d, .destroy_on_draw = true}; { struct yasl_private_window_cbs temporary = callbacks; struct yasl_private_window_spec spec = { .title = "Callbacks at creation", .size = {160, 100}, .cbs = &temporary, .ud = &w, }; w.surface = yasl_private_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_private_surface_schedule_redraw(w.surface); pump(loop, d, NULL); assert(w.draws == 1 && !w.surface); } static void x11_behavior(struct yasl_private_loop *loop, struct yasl_private_display *d, struct window *w) { Display *native = yasl_private_display_get_native_handle(d); Window window = (Window)(uintptr_t)yasl_private_surface_get_native_handle(w->surface); assert(native && window); assert(yasl_private_x11_display_get_display(d) == native); assert(yasl_private_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_private_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_private_vector2i size = yasl_private_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_private_event pointer_event; while (yasl_private_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_private_surface_poll_event(w->surface, &pointer_event)) { if (pointer_event.type == YASL_PRIVATE_EVENT_POINTER_MOTION) { assert(pointer_event.pointer.device == YASL_PRIVATE_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_private_display_copy(d, YASL_PRIVATE_CLIPBOARD, "clipboard text"); yasl_private_display_paste(d, YASL_PRIVATE_CLIPBOARD, w->surface); pump(loop, d, NULL); bool saw_key = false, saw_paste = false, saw_text = false; struct yasl_private_event event; while (yasl_private_surface_poll_event(w->surface, &event)) { assert(event.surface == w->surface); if (event.type == YASL_PRIVATE_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_PRIVATE_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_PRIVATE_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_private_surface_poll_event(w->surface, &event)) { if (event.type == YASL_PRIVATE_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_PRIVATE_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_private_surface_poll_event(w->surface, &event)); assert(event.type == YASL_PRIVATE_EVENT_KEY && event.key.key == YASL_KEY_A); assert(event.key.mods & YASL_PRIVATE_MOD_CTRL); assert(!yasl_private_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_private_surface_poll_event(w->surface, &event)) { if (event.type == YASL_PRIVATE_EVENT_KEY) { assert(event.key.key == YASL_KEY_A && event.key.codepoint == 'q'); saw_key = true; } else if (event.type == YASL_PRIVATE_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_private_surface_poll_event(w->surface, &event)); assert(event.type == YASL_PRIVATE_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_private_surface_poll_event(w->surface, &event)); } assert(yasl_private_surface_timing(w->surface).source == YASL_PRIVATE_TIMING_SUBMISSION); assert(!(yasl_private_display_caps(d) & YASL_PRIVATE_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_private_display_copy(d, YASL_PRIVATE_CLIPBOARD, text); yasl_private_display_paste(d, YASL_PRIVATE_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_private_surface_poll_event(w->surface, &event)); assert(event.type == YASL_PRIVATE_EVENT_PASTE && strcmp(event.text.data, text) == 0); } const char *last = event.text.data; assert(!yasl_private_surface_poll_event(w->surface, &event)); yasl_private_display_copy(d, YASL_PRIVATE_CLIPBOARD, "changed"); pump(loop, d, NULL); assert(strcmp(last, "owned-299") == 0); yasl_private_display_copy(d, YASL_PRIVATE_CLIPBOARD, ""); yasl_private_display_paste(d, YASL_PRIVATE_CLIPBOARD, w->surface); assert(yasl_private_surface_poll_event(w->surface, &event)); assert(event.type == YASL_PRIVATE_EVENT_PASTE && !event.text.len && !event.text.data[0]); /* Concurrent external conversions must not share a mutable target slot. */ struct yasl_private_display_spec ds = {.backend = YASL_PRIVATE_BACKEND_X11}; struct yasl_private_display *owner = yasl_private_display_open(loop, &ds); assert(owner); yasl_private_display_copy(owner, YASL_PRIVATE_CLIPBOARD, "external"); struct window other = create(d), cancelled = create(d); yasl_private_display_paste(d, YASL_PRIVATE_CLIPBOARD, w->surface); yasl_private_display_paste(d, YASL_PRIVATE_CLIPBOARD, other.surface); yasl_private_display_paste(d, YASL_PRIVATE_CLIPBOARD, cancelled.surface); yasl_private_surface_destroy(cancelled.surface); pump(loop, d, owner); int first_pastes = 0, second_pastes = 0; while (yasl_private_surface_poll_event(w->surface, &event)) { if (event.type == YASL_PRIVATE_EVENT_PASTE) { assert(strcmp(event.text.data, "external") == 0); first_pastes++; } } while (yasl_private_surface_poll_event(other.surface, &event)) { if (event.type == YASL_PRIVATE_EVENT_PASTE) { assert(strcmp(event.text.data, "external") == 0); second_pastes++; } } assert(first_pastes == 1 && second_pastes == 1); yasl_private_surface_destroy(other.surface); yasl_private_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_private_loop *loop = yasl_private_loop_create(); assert(loop); if (strcmp(argv[1], "failure") == 0) { struct yasl_private_display_spec spec = {.backend = (enum yasl_private_backend)atoi(argv[2])}; assert(!yasl_private_display_open(loop, &spec)); } else if (strcmp(argv[1], "mixed") == 0) { struct yasl_private_display_spec spec = {.backend = YASL_PRIVATE_BACKEND_WAYLAND}; struct yasl_private_display *a = yasl_private_display_open(loop, &spec); spec.backend = YASL_PRIVATE_BACKEND_X11; struct yasl_private_display *b = yasl_private_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_private_wayland_display_get_wl_display(a) != NULL); assert(yasl_private_wayland_surface_get_wl_surface(wa.surface) != NULL); assert(!yasl_private_x11_display_get_display(a)); #endif #if YASL_HAS_X11 assert(yasl_private_x11_display_get_display(b) != NULL); assert(yasl_private_x11_surface_get_window(wb.surface) != 0); assert(!yasl_private_wayland_display_get_wl_display(b)); #endif attach(&wa); attach(&wb); pump(loop, a, b); assert(wa.draws && wb.draws); yasl_private_surface_destroy(wa.surface); yasl_private_surface_destroy(wb.surface); yasl_private_display_close(a); yasl_private_display_close(b); } else { assert(argc == 4); struct yasl_private_display_spec spec = {.backend = (enum yasl_private_backend)atoi(argv[2])}; struct yasl_private_display *d = strcmp(argv[1], "null") == 0 ? yasl_private_display_open(loop, NULL) : yasl_private_display_open(loop, &spec); assert(d); assert(yasl_private_display_backend(d) == (enum yasl_private_backend)atoi(argv[3])); struct window w = create(d); pump(loop, d, NULL); assert(w.draws == 0); attach(&w); yasl_private_surface_schedule_redraw(w.surface); pump(loop, d, NULL); assert(w.draws > 0); test_read_pixels(loop, d, &w); if (yasl_private_display_backend(d) == YASL_PRIVATE_BACKEND_X11) x11_behavior(loop, d, &w); if (w.surface) yasl_private_surface_destroy(w.surface); w = create(d); w.destroy_on_draw = true; attach(&w); pump(loop, d, NULL); assert(w.draws && !w.surface); yasl_private_display_close(d); } yasl_private_loop_destroy(loop); puts("Backend test passed."); return 0; }