Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/test-fixtures/ui/niri-integration-client.c

Raw
// Synthetic xdg-toplevel, controlled only through an owned sandbox FIFO.
// No capture backend: the solid buffer merely gives native screenshots pixels.
#include <assert.h>
#include <fcntl.h>
#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
#include <wayland-client.h>
#include "niri-integration-xdg.h"

static struct wl_compositor *compositor;
static struct wl_shm *shm;
static struct xdg_wm_base *wm;
static struct wl_surface *surface;
static struct wl_buffer *buffer;
static bool running = true;
static void ping(void *data, struct xdg_wm_base *base, uint32_t serial) {
    (void)data;
    xdg_wm_base_pong(base, serial);
}
static const struct xdg_wm_base_listener wm_listener = { .ping = ping };
static void global(void *data, struct wl_registry *registry, uint32_t name,
                   const char *interface, uint32_t version) {
    (void)data; (void)version;
    if (!strcmp(interface, "wl_compositor"))
        compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 1);
    else if (!strcmp(interface, "wl_shm"))
        shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);
    else if (!strcmp(interface, "xdg_wm_base")) {
        wm = wl_registry_bind(registry, name, &xdg_wm_base_interface, 1);
        xdg_wm_base_add_listener(wm, &wm_listener, NULL);
    }
}
static void removed(void *data, struct wl_registry *registry, uint32_t name) {
    (void)data; (void)registry; (void)name;
}
static const struct wl_registry_listener registry_listener = {global, removed};
static void configure(void *data, struct xdg_surface *xdg, uint32_t serial) {
    (void)data;
    xdg_surface_ack_configure(xdg, serial);
    wl_surface_attach(surface, buffer, 0, 0);
    wl_surface_damage(surface, 0, 0, 480, 280);
    wl_surface_commit(surface);
}
static const struct xdg_surface_listener surface_listener = {configure};
static void size(void *data, struct xdg_toplevel *top, int32_t width, int32_t height,
                 struct wl_array *states) {
    (void)data; (void)top; (void)width; (void)height; (void)states;
}
static void close_window(void *data, struct xdg_toplevel *top) {
    (void)data; (void)top;
    running = false;
}
static const struct xdg_toplevel_listener top_listener = { .configure = size, .close = close_window };
int main(int argc, char **argv) {
    assert(argc == 4);
    assert(getenv("NUGULAND_UI_SANDBOX") && !strcmp(getenv("HOME"), "/home/test"));
    struct wl_display *display = wl_display_connect(NULL);
    assert(display);
    struct wl_registry *registry = wl_display_get_registry(display);
    wl_registry_add_listener(registry, &registry_listener, NULL);
    assert(wl_display_roundtrip(display) >= 0);
    assert(compositor && shm && wm);
    char path[] = "/tmp/niri-integration-buffer-XXXXXX";
    int fd = mkstemp(path);
    assert(fd >= 0);
    unlink(path);
    const int bytes = 480 * 280 * 4;
    assert(ftruncate(fd, bytes) == 0);
    uint32_t *pixels = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    assert(pixels != MAP_FAILED);
    for (int i = 0; i < 480 * 280; ++i) pixels[i] = i % 480 < 240 ? 0xff242424 : 0xff5d5d5d;
    struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, bytes);
    buffer = wl_shm_pool_create_buffer(pool, 0, 480, 280, 480 * 4, WL_SHM_FORMAT_XRGB8888);
    wl_shm_pool_destroy(pool);
    close(fd);
    surface = wl_compositor_create_surface(compositor);
    struct xdg_surface *xdg = xdg_wm_base_get_xdg_surface(wm, surface);
    xdg_surface_add_listener(xdg, &surface_listener, NULL);
    struct xdg_toplevel *top = xdg_surface_get_toplevel(xdg);
    xdg_toplevel_add_listener(top, &top_listener, NULL);
    xdg_toplevel_set_title(top, argv[2]);
    xdg_toplevel_set_app_id(top, argv[3]);
    wl_surface_commit(surface);
    int control = open(argv[1], O_RDWR | O_NONBLOCK);
    assert(control >= 0);
    while (running) {
        while (wl_display_prepare_read(display) != 0)
            assert(wl_display_dispatch_pending(display) >= 0);
        assert(wl_display_flush(display) >= 0);
        struct pollfd fds[] = {{wl_display_get_fd(display), POLLIN, 0}, {control, POLLIN, 0}};
        assert(poll(fds, 2, -1) >= 0);
        if (fds[0].revents & POLLIN) assert(wl_display_read_events(display) >= 0);
        else wl_display_cancel_read(display);
        assert(wl_display_dispatch_pending(display) >= 0);
        if (fds[1].revents & POLLIN) {
            char command[256];
            ssize_t length = read(control, command, sizeof(command) - 1);
            assert(length > 0);
            command[length] = 0;
            char *value = strchr(command, ' ');
            assert(value);
            *value++ = 0;
            value[strcspn(value, "\n")] = 0;
            if (!strcmp(command, "title")) xdg_toplevel_set_title(top, value);
            else if (!strcmp(command, "app")) xdg_toplevel_set_app_id(top, value);
            else abort();
            wl_surface_commit(surface);
        }
    }
    close(control);
    xdg_toplevel_destroy(top);
    xdg_surface_destroy(xdg);
    wl_surface_destroy(surface);
    wl_buffer_destroy(buffer);
    munmap(pixels, bytes);
    xdg_wm_base_destroy(wm);
    wl_shm_destroy(shm);
    wl_compositor_destroy(compositor);
    wl_registry_destroy(registry);
    wl_display_flush(display);
    wl_display_disconnect(display);
    return 0;
}