AtlatestRepositorysigil-ui
1
/*2
* The Wayland surface for (sigil ui wayland): a wl_shm window, either an3
* xdg-toplevel or a fullscreen layer-shell surface, with keyboard and4
* pointer input.5
*6
* libwayland-client and libxkbcommon are loaded with dlopen when the first7
* surface opens, so a program that never opens one (a boot screen drawing8
* to DRM, a test rendering offscreen) needs neither library, at link time9
* or at run time. The checked-in protocol headers keep the build independent10
* of system Wayland headers. Sigil-window supplies the interface tables.11
*12
* Presenting. Two shm buffers in XRGB8888, the canvas's own layout, so a13
* present is a row copy. Each buffer remembers which parts of it are older14
* than the canvas (its stale damage); a present copies its own damage plus15
* the buffer's stale damage, gives the compositor exactly its own damage,16
* and adds that damage to the other buffer's stale list. One frame17
* callback is outstanding at a time. A present while it is outstanding, or18
* while the compositor holds both buffers, is deferred: the damage is kept19
* and the Sigil side presents again from the same canvas when the callback20
* or a release arrives. A buffer of the wrong size that the compositor21
* still holds (after a resize or a scale change) is retired: it is never22
* written again and is destroyed when the compositor releases it.23
*24
* Nothing here builds a Sigil object larger than one string. Events go out25
* through a vector of fixnums the caller passes in, one event per call,26
* because a C local holding a half-built list is not a GC root.27
*/29
#include <sigil/sigil.h>30
#include <dlfcn.h>31
#include <errno.h>32
#include <fcntl.h>33
#include <poll.h>34
#include <pthread.h>35
#include <signal.h>36
#include <stdint.h>37
#include <stdio.h>38
#include <stdlib.h>39
#include <string.h>40
#include <sys/mman.h>41
#include <sys/syscall.h>42
#include <sys/stat.h>43
#include <time.h>44
#include <unistd.h>45
#include "wayland-bridge.h"46
#include "window-services.h"48
/* Every libwayland-client function this file calls, directly or through49
* the generated protocol headers, becomes a call through a pointer that is50
* filled by dlsym. The headers' own declarations turn into declarations of51
* those pointers. */52
#define wl_display_connect (*sui_wl_display_connect)53
#define wl_display_disconnect (*sui_wl_display_disconnect)54
#define wl_display_get_fd (*sui_wl_display_get_fd)55
#define wl_display_roundtrip (*sui_wl_display_roundtrip)56
#define wl_display_dispatch_pending (*sui_wl_display_dispatch_pending)57
#define wl_display_prepare_read (*sui_wl_display_prepare_read)58
#define wl_display_read_events (*sui_wl_display_read_events)59
#define wl_display_cancel_read (*sui_wl_display_cancel_read)60
#define wl_display_flush (*sui_wl_display_flush)61
#define wl_display_get_error (*sui_wl_display_get_error)62
#define wl_proxy_marshal_flags (*sui_wl_proxy_marshal_flags)63
#define wl_proxy_add_listener (*sui_wl_proxy_add_listener)64
#define wl_proxy_get_version (*sui_wl_proxy_get_version)65
#define wl_proxy_destroy (*sui_wl_proxy_destroy)66
#define wl_proxy_set_user_data (*sui_wl_proxy_set_user_data)67
#define wl_proxy_get_user_data (*sui_wl_proxy_get_user_data)69
/* These declarations bind to sigil-window's hidden interface tables. */70
#define SIGIL_UI_EXTERNAL_WAYLAND_PROTOCOLS 171
#pragma GCC visibility push(hidden)72
#include "wayland-client.h"73
/* The hidden activation interface must resolve to sigil-window's table at link time. */74
#include "xdg-activation-v1-client-protocol.h"75
#include "xdg-shell-client-protocol.h"76
#include "xdg-decoration-unstable-v1-client-protocol.h"77
#include "wlr-layer-shell-unstable-v1-client-protocol.h"78
#include "cursor-shape-v1-client-protocol.h"79
#include "linux-dmabuf-v1-client-protocol.h"80
#pragma GCC visibility pop82
#include "xkb.h"84
/* Exporter half of xdg-foreign-unstable-v2, wire version 1. Keep these85
* tables private so another library can link its own protocol tables. */86
static const struct wl_message exported_requests[] = { { "destroy", "", NULL } };87
static const struct wl_message exported_events[] = { { "handle", "s", NULL } };88
static const struct wl_interface exported_interface = {89
"zxdg_exported_v2", 1, 1, exported_requests, 1, exported_events90
};91
static const struct wl_interface *export_types[] = { &exported_interface, &wl_surface_interface };92
static const struct wl_message exporter_requests[] = {93
{ "destroy", "", NULL },94
{ "export_toplevel", "no", export_types }95
};96
static const struct wl_interface exporter_interface = {97
"zxdg_exporter_v2", 1, 2, exporter_requests, 0, NULL98
};100
/* Stable presentation-time v1. The feedback object has events only. */101
static const struct wl_interface presentation_feedback_interface;102
static const struct wl_interface *presentation_feedback_args[] = {103
&wl_surface_interface, &presentation_feedback_interface,104
};105
static const struct wl_interface *presentation_output_args[] = { &wl_output_interface };106
static const struct wl_message presentation_requests[] = {107
{ "destroy", "", NULL },108
{ "feedback", "on", presentation_feedback_args },109
};110
static const struct wl_message presentation_events[] = { { "clock_id", "u", NULL } };111
static const struct wl_message presentation_feedback_events[] = {112
{ "sync_output", "o", presentation_output_args },113
{ "presented", "uuuuuuu", NULL },114
{ "discarded", "", NULL },115
};116
static const struct wl_interface presentation_interface = {117
"wp_presentation", 1, 2, presentation_requests, 1, presentation_events,118
};119
static const struct wl_interface presentation_feedback_interface = {120
"wp_presentation_feedback", 1, 0, NULL, 3, presentation_feedback_events,121
};123
#define REGISTER_AND_EXPORT(name, func, arity, doc) \124
sigil_module_register_native(vm, name, func, arity, doc); \125
sigil_module_export(vm, name)127
#define DONE SIGIL_UNDEFINED129
/* ------------------------------------------------------------ loading */131
static struct xkb_context *(*xkb_context_new)(int);132
static void (*xkb_context_unref)(struct xkb_context *);133
static struct xkb_keymap *(*xkb_keymap_new_from_string)(struct xkb_context *, const char *, int, int);134
static void (*xkb_keymap_unref)(struct xkb_keymap *);135
static int (*xkb_keymap_key_repeats)(struct xkb_keymap *, xkb_keycode_t);136
static struct xkb_state *(*xkb_state_new)(struct xkb_keymap *);137
static void (*xkb_state_unref)(struct xkb_state *);138
static int (*xkb_state_update_mask)(struct xkb_state *, xkb_mod_mask_t, xkb_mod_mask_t, xkb_mod_mask_t,139
xkb_layout_index_t, xkb_layout_index_t, xkb_layout_index_t);140
static xkb_keysym_t (*xkb_state_key_get_one_sym)(struct xkb_state *, xkb_keycode_t);141
static int (*xkb_state_key_get_utf8)(struct xkb_state *, xkb_keycode_t, char *, size_t);142
static int (*xkb_state_mod_name_is_active)(struct xkb_state *, const char *, int);143
static int (*xkb_keysym_get_name)(xkb_keysym_t, char *, size_t);144
static struct xkb_compose_table *(*xkb_compose_table_new_from_locale)(struct xkb_context *, const char *, int);145
static void (*xkb_compose_table_unref)(struct xkb_compose_table *);146
static struct xkb_compose_state *(*xkb_compose_state_new)(struct xkb_compose_table *, int);147
static void (*xkb_compose_state_unref)(struct xkb_compose_state *);148
static int (*xkb_compose_state_feed)(struct xkb_compose_state *, xkb_keysym_t);149
static int (*xkb_compose_state_get_status)(struct xkb_compose_state *);150
static int (*xkb_compose_state_get_utf8)(struct xkb_compose_state *, char *, size_t);151
static xkb_keysym_t (*xkb_compose_state_get_one_sym)(struct xkb_compose_state *);152
static void (*xkb_compose_state_reset)(struct xkb_compose_state *);154
typedef struct { const char *name; void **slot; int optional; } Sym;156
#define S(name) { #name, (void **)&sui_##name, 0 }157
static const Sym wayland_syms[] = {158
S(wl_display_connect), S(wl_display_disconnect), S(wl_display_get_fd), S(wl_display_roundtrip),159
S(wl_display_dispatch_pending), S(wl_display_prepare_read), S(wl_display_read_events),160
S(wl_display_cancel_read), S(wl_display_flush), S(wl_display_get_error),161
S(wl_proxy_marshal_flags), S(wl_proxy_add_listener), S(wl_proxy_get_version), S(wl_proxy_destroy),162
S(wl_proxy_set_user_data), S(wl_proxy_get_user_data),163
};164
#undef S165
#define X(name, opt) { #name, (void **)&name, opt }166
static const Sym xkb_syms[] = {167
X(xkb_context_new, 0), X(xkb_context_unref, 0), X(xkb_keymap_new_from_string, 0), X(xkb_keymap_unref, 0),168
X(xkb_keymap_key_repeats, 0), X(xkb_state_new, 0), X(xkb_state_unref, 0), X(xkb_state_update_mask, 0),169
X(xkb_state_key_get_one_sym, 0), X(xkb_state_key_get_utf8, 0), X(xkb_state_mod_name_is_active, 0),170
X(xkb_keysym_get_name, 0),171
/* Compose (dead keys and the compose key) is used when present. */172
X(xkb_compose_table_new_from_locale, 1), X(xkb_compose_table_unref, 1), X(xkb_compose_state_new, 1),173
X(xkb_compose_state_unref, 1), X(xkb_compose_state_feed, 1), X(xkb_compose_state_get_status, 1),174
X(xkb_compose_state_get_utf8, 1), X(xkb_compose_state_get_one_sym, 1), X(xkb_compose_state_reset, 1),175
};176
#undef X178
static int loaded; /* 1 once both libraries are loaded */179
static int have_compose;181
/* Open a library from the environment variable, else by soname. */182
static void *open_library(const char *env, const char *soname, char *err, size_t errn)183
{184
const char *path = getenv(env);185
if (!path || !*path) path = soname;186
void *h = dlopen(path, RTLD_NOW | RTLD_LOCAL);187
if (!h) snprintf(err, errn, "cannot load %s (set %s to its path): %s", path, env, dlerror());188
return h;189
}191
static int bind_syms(void *h, const Sym *syms, size_t n, char *err, size_t errn)192
{193
for (size_t i = 0; i < n; i++) {194
*syms[i].slot = dlsym(h, syms[i].name);195
if (!*syms[i].slot && !syms[i].optional) {196
snprintf(err, errn, "%s is missing from the loaded library", syms[i].name);197
return 0;198
}199
}200
return 1;201
}203
static int load_libraries(char *err, size_t errn)204
{205
if (loaded) return 1;206
void *wl = open_library("SIGIL_UI_LIBWAYLAND_CLIENT", "libwayland-client.so.0", err, errn);207
if (!wl) return 0;208
if (!bind_syms(wl, wayland_syms, sizeof wayland_syms / sizeof *wayland_syms, err, errn)) {209
dlclose(wl);210
return 0;211
}212
void *xkb = open_library("SIGIL_UI_LIBXKBCOMMON", "libxkbcommon.so.0", err, errn);213
if (!xkb) { dlclose(wl); return 0; }214
if (!bind_syms(xkb, xkb_syms, sizeof xkb_syms / sizeof *xkb_syms, err, errn)) {215
dlclose(xkb); dlclose(wl);216
return 0;217
}218
have_compose = xkb_compose_table_new_from_locale && xkb_compose_state_new && xkb_compose_state_feed &&219
xkb_compose_state_get_status && xkb_compose_state_get_utf8 &&220
xkb_compose_state_get_one_sym && xkb_compose_state_reset &&221
xkb_compose_state_unref && xkb_compose_table_unref;222
loaded = 1; /* the libraries stay loaded for the life of the process */223
return 1;224
}226
/* ------------------------------------------------------------ state */228
#define MAX_OUTPUTS 16229
#define MAX_RECTS 32230
#define QUEUE 256231
#define TEXT 32233
enum { ROLE_TOPLEVEL = 0, ROLE_FULLSCREEN = 1 }; /* requested */234
enum { HAS_TOPLEVEL = 0, HAS_LAYER = 1, HAS_XDG_FULLSCREEN = 2, HAS_SUBSURFACE = 3 }; /* obtained */236
enum {237
UI_RESIZE = 1, UI_CLOSE, UI_FRAME, UI_KEY, UI_KEYBOARD_FOCUS,238
UI_POINTER_ENTER, UI_POINTER_LEAVE, UI_MOTION, UI_BUTTON, UI_SCROLL, UI_MODIFIERS,239
UI_FULLSCREEN, UI_FULLSCREEN_STATUS240
};241
enum { KEY_RELEASE = 0, KEY_PRESS = 1, KEY_REPEAT = 2 };242
enum { MOD_SHIFT = 1, MOD_CONTROL = 2, MOD_ALT = 4, MOD_SUPER = 8, MOD_CAPS = 16, MOD_NUM = 32 };244
typedef struct { int32_t x, y, w, h; } Rect;245
/* A list of device rectangles; all = the whole buffer. */246
typedef struct { Rect r[MAX_RECTS]; int n, all; } Damage;248
typedef struct {249
int32_t type, v[10];250
char text[TEXT];251
} Event;253
struct Wl;254
typedef struct DmabufFeedbackState DmabufFeedbackState;256
typedef struct Buffer {257
struct Wl *w;258
struct wl_buffer *buffer;259
uint8_t *data;260
size_t size;261
int width, height, busy, retired;262
Damage stale; /* parts older than the canvas */263
SuiWaylandRelease released;264
void *release_data;265
struct Buffer *next; /* in the retired list */266
} Buffer;268
typedef struct {269
struct Wl *w;270
struct wl_output *output;271
uint32_t global;272
int scale, entered;273
char name[64];274
} Output;276
#define CLIPBOARD_LIMIT (4u * 1024 * 1024)277
#define CLIPBOARD_WRITES 8278
typedef struct Offer {279
struct wl_data_offer *offer;280
int utf8, plain;281
struct Offer *next;282
} Offer;283
typedef struct {284
int fd;285
uint8_t *bytes;286
size_t size, offset;287
int64_t deadline;288
} ClipboardWrite;290
typedef struct CursorState CursorState;291
typedef struct Wl {292
unsigned references;293
int closing, torn_down;294
unsigned connection_depth;295
int close_pending;296
struct Wl *parent, *children, *sibling;297
struct wl_subcompositor *subcompositor;298
struct wl_subsurface *subsurface;299
int alpha;300
uint8_t *alpha_mask;301
size_t alpha_size;302
char *keymap_text;303
size_t keymap_size;304
uint32_t modifier_masks[4];305
uint64_t clipboard_generation;306
SuiWaylandFrame frame_notify;307
void *frame_data;308
struct wl_data_device_manager *data_manager;309
struct wl_data_device *data_device;310
struct wl_data_source *data_source;311
Offer *offers, *selection;312
uint8_t *clipboard;313
size_t clipboard_size;314
uint32_t input_serial;315
ClipboardWrite writes[CLIPBOARD_WRITES];316
struct wl_display *display;317
struct wl_registry *registry;318
struct xdg_activation_v1 *activation;319
uint32_t activation_global;320
uint32_t exporter_global;321
struct wl_proxy *exporter, *exported;322
char *export_handle;323
uint32_t presentation_global, presentation_serial;324
struct wl_proxy *presentation, *presentation_feedback;325
struct wl_callback *presentation_frame;326
enum { PRESENTATION_PENDING = 1, PRESENTATION_VISIBLE, PRESENTATION_FRAME,327
PRESENTATION_DISCARDED } presentation_status;328
struct zwp_linux_dmabuf_v1 *dmabuf;329
uint32_t dmabuf_global, dmabuf_version;330
DmabufFeedbackState *dmabuf_feedback;331
SuiWaylandDmabufImport *dmabuf_imports;332
struct wl_compositor *compositor;333
struct wl_shm *shm;334
struct wl_seat *seat;335
struct wl_keyboard *keyboard;336
struct wl_pointer *pointer;337
struct xdg_wm_base *wm_base;338
struct zxdg_decoration_manager_v1 *decoration_manager;339
struct zxdg_toplevel_decoration_v1 *decoration;340
int server_decorations, decoration_mode, pending_decoration_mode;341
struct zwlr_layer_shell_v1 *layer_shell;342
struct wp_cursor_shape_manager_v1 *cursor_manager;343
struct wp_cursor_shape_device_v1 *cursor_device;344
CursorState *cursor;345
uint32_t pointer_serial;346
struct wl_surface *surface;347
struct xdg_surface *xdg_surface;348
struct xdg_toplevel *toplevel;349
char title[4096];350
struct zwlr_layer_surface_v1 *layer;351
struct wl_callback *frame;352
Output outputs[MAX_OUTPUTS];354
struct xkb_context *xkb;355
struct xkb_keymap *keymap;356
struct xkb_state *keys;357
struct xkb_compose_table *compose_table;358
struct xkb_compose_state *compose;360
Buffer *slot[2];361
Buffer *retired;362
Damage pending; /* presented but not yet committed */364
Event queue[QUEUE];365
int head, count;366
Event current;368
int role; /* HAS_* */369
int width, height; /* logical size */370
int geometry_width, geometry_height; /* last declared xdg window size */371
int scale;372
int preferred_scale; /* from wl_surface.preferred_buffer_scale, 0 if none */373
int committed_scale;374
int configured, mapped, closed, dead;375
int64_t initial_commit_ms;376
unsigned initial_dispatches;377
uint32_t ack_serial;378
int ack_due;379
int pending_w, pending_h; /* from the last toplevel configure */380
int pending_fullscreen, fullscreen;381
pthread_t service_thread;382
SuiFullscreenRequest fullscreen_request;383
int keyboard_focus;384
int pointer_inside;385
int wake; /* a buffer came back while a present waits */387
int32_t repeat_rate, repeat_delay;388
uint32_t held_code;389
int64_t repeat_at;391
double px, py; /* pointer, logical */392
int32_t scroll_x, scroll_y, steps_x, steps_y; /* wl_fixed, this frame */393
int32_t scroll_source, value120_x, value120_y, scroll_stops;394
int32_t scroll_remainder_x, scroll_remainder_y, scroll_remainder_source;395
int scroll_pending;397
int last_n; /* damage rects in the last commit, -1 for all */398
Rect last_box; /* their bounding box */400
char problem[256];401
} Wl;403
static int64_t now_ms(void);404
static int cursor_apply(Wl *w);405
static void cursor_close(Wl *w);406
#include "wayland-dmabuf-feedback.c"407
#include "wayland-dmabuf-import.c"408
#include "wayland-cursor.c"410
static Value wl_tag = SIGIL_UNDEFINED;411
static int dispatch(Wl *w, int64_t timeout_ms);412
static void window_service_configured(Wl *w, int actual);414
static int64_t now_ms(void)415
{416
struct timespec t;417
clock_gettime(CLOCK_MONOTONIC, &t);418
return (int64_t)t.tv_sec * 1000 + t.tv_nsec / 1000000;419
}421
static void trace_initial_configure(Wl *w, const char *status)422
{423
const char *enabled = getenv("SIGIL_UI_TRACE_CONFIGURE");424
if (w->initial_commit_ms && enabled && !strcmp(enabled, "1"))425
fprintf(stderr, "sigil-ui: initial-configure status=%s elapsed_ms=%lld dispatches=%u role=%d\n",426
status, (long long)(now_ms() - w->initial_commit_ms),427
w->initial_dispatches, w->role);428
}430
/* Count pending-event dispatch batches up to and including first configure. */431
static int dispatch_pending(Wl *w)432
{433
if (w->initial_commit_ms && !w->configured) w->initial_dispatches++;434
return wl_display_dispatch_pending(w->display);435
}437
static void fail(Wl *w, const char *what)438
{439
if (!w->dead) {440
int e = w->display ? wl_display_get_error(w->display) : 0;441
if (e) snprintf(w->problem, sizeof w->problem, "%s: %s", what, strerror(e));442
else snprintf(w->problem, sizeof w->problem, "%s", what);443
}444
w->dead = 1;445
}447
/* ------------------------------------------------------------ events */449
static Event *push(Wl *w, int type)450
{451
/* Motion coalesces: a newer position replaces one not yet read. */452
if (type == UI_MOTION && w->count > 0) {453
Event *last = &w->queue[(w->head + w->count - 1) % QUEUE];454
if (last->type == UI_MOTION) return last;455
}456
if (w->count == QUEUE) return NULL; /* the program is not reading; drop */457
Event *e = &w->queue[(w->head + w->count) % QUEUE];458
memset(e, 0, sizeof *e);459
e->type = type;460
w->count++;461
return e;462
}464
/* ------------------------------------------------------------ damage */466
static void damage_clear(Damage *d) { d->n = 0; d->all = 0; }468
static void damage_add(Damage *d, Rect r)469
{470
if (d->all || r.w <= 0 || r.h <= 0) return;471
if (d->n == MAX_RECTS) {472
/* Too many pieces: keep their bounding box. */473
int32_t x0 = r.x, y0 = r.y, x1 = r.x + r.w, y1 = r.y + r.h;474
for (int i = 0; i < d->n; i++) {475
Rect q = d->r[i];476
if (q.x < x0) x0 = q.x;477
if (q.y < y0) y0 = q.y;478
if (q.x + q.w > x1) x1 = q.x + q.w;479
if (q.y + q.h > y1) y1 = q.y + q.h;480
}481
d->r[0] = (Rect){x0, y0, x1 - x0, y1 - y0};482
d->n = 1;483
return;484
}485
d->r[d->n++] = r;486
}488
static void damage_merge(Damage *d, const Damage *s)489
{490
if (s->all) { d->all = 1; d->n = 0; return; }491
for (int i = 0; i < s->n; i++) damage_add(d, s->r[i]);492
}494
static int damage_empty(const Damage *d) { return !d->all && d->n == 0; }496
/* ------------------------------------------------------------ buffers */498
static void buffer_free(Buffer *b)499
{500
if (!b) return;501
if (b->buffer) wl_buffer_destroy(b->buffer);502
if (b->data) munmap(b->data, b->size);503
if (b->released) b->released(b->release_data);504
free(b);505
}507
static void buffer_release(void *data, struct wl_buffer *wb)508
{509
Buffer *b = data;510
Wl *w = b->w;511
b->busy = 0;512
SuiWaylandRelease released = b->released;513
void *release_data = b->release_data;514
b->released = NULL;515
b->release_data = NULL;516
if (b->retired) {517
Buffer **p = &w->retired;518
while (*p && *p != b) p = &(*p)->next;519
if (*p) *p = b->next;520
buffer_free(b);521
if (released) released(release_data);522
return;523
}524
if (!damage_empty(&w->pending)) w->wake = 1;525
if (released) released(release_data);526
}527
static const struct wl_buffer_listener buffer_listener = { .release = buffer_release };529
static Buffer *buffer_new(Wl *w, int width, int height)530
{531
if (width < 1 || height < 1 || width > 16384 || height > 16384) return NULL;532
size_t stride = (size_t)width * 4, size = stride * (size_t)height;533
if (size > INT32_MAX) return NULL;534
int fd = (int)syscall(SYS_memfd_create, "sigil-ui-wayland", 1u /* MFD_CLOEXEC */);535
if (fd < 0) return NULL;536
if (ftruncate(fd, (off_t)size) < 0) { close(fd); return NULL; }537
void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);538
if (data == MAP_FAILED) { close(fd); return NULL; }539
struct wl_shm_pool *pool = wl_shm_create_pool(w->shm, fd, (int32_t)size);540
struct wl_buffer *wb = wl_shm_pool_create_buffer(pool, 0, width, height, (int32_t)stride,541
w->alpha ? WL_SHM_FORMAT_ARGB8888 : WL_SHM_FORMAT_XRGB8888);542
wl_shm_pool_destroy(pool);543
close(fd);544
Buffer *b = calloc(1, sizeof *b);545
if (!b) { wl_buffer_destroy(wb); munmap(data, size); return NULL; }546
*b = (Buffer){ .w = w, .buffer = wb, .data = data, .size = size, .width = width, .height = height };547
b->stale.all = 1;548
wl_buffer_add_listener(wb, &buffer_listener, b);549
return b;550
}552
/* Drop a slot's buffer: now if the compositor has released it, else when553
* it does. Either way it is never written again. */554
static void buffer_retire(Wl *w, Buffer *b)555
{556
if (!b) return;557
if (!b->busy) { buffer_free(b); return; }558
b->retired = 1;559
b->next = w->retired;560
w->retired = b;561
}563
/* ------------------------------------------------------------ size and scale */565
static int device_w(Wl *w) { return w->width * w->scale; }566
static int device_h(Wl *w) { return w->height * w->scale; }568
static void report_resize(Wl *w)569
{570
Event *e = push(w, UI_RESIZE);571
if (e) { e->v[0] = w->width; e->v[1] = w->height; e->v[2] = w->scale; }572
/* Whatever is presented next covers a new buffer size. */573
damage_clear(&w->pending);574
w->pending.all = 1;575
}577
/* The buffer scale: what the compositor prefers, else the largest scale578
* among the outputs the surface is on. */579
static int wanted_scale(Wl *w)580
{581
if (w->preferred_scale > 0) return w->preferred_scale;582
int s = 0;583
for (int i = 0; i < MAX_OUTPUTS; i++)584
if (w->outputs[i].output && w->outputs[i].entered && w->outputs[i].scale > s) s = w->outputs[i].scale;585
return s > 0 ? s : w->scale;586
}588
static void update_scale(Wl *w)589
{590
int s = wanted_scale(w);591
if (s != w->scale) {592
w->scale = s;593
if (w->configured) report_resize(w);594
for (Wl *child = w->children; child; child = child->sibling) {595
child->scale = s;596
report_resize(child);597
}598
if (w->cursor) cursor_apply(w);599
}600
}602
/* ------------------------------------------------------------ listeners */604
static void output_geometry(void *d, struct wl_output *o, int32_t x, int32_t y, int32_t pw, int32_t ph,605
int32_t sub, const char *make, const char *model, int32_t transform) {}606
static void output_mode(void *d, struct wl_output *o, uint32_t flags, int32_t width, int32_t height,607
int32_t refresh) {}608
static void output_done(void *d, struct wl_output *o)609
{610
Output *out = d;611
if (out->w->configured) update_scale(out->w);612
}613
static void output_scale(void *d, struct wl_output *o, int32_t scale)614
{615
Output *out = d;616
out->scale = scale < 1 ? 1 : scale > 8 ? 8 : scale;617
}618
static void output_name(void *d, struct wl_output *o, const char *name)619
{620
Output *out = d;621
snprintf(out->name, sizeof out->name, "%s", name);622
}623
static void output_description(void *d, struct wl_output *o, const char *t) {}624
static const struct wl_output_listener output_listener = {625
.geometry = output_geometry, .mode = output_mode, .done = output_done,626
.scale = output_scale, .name = output_name, .description = output_description,627
};629
static void surface_enter(void *data, struct wl_surface *s, struct wl_output *o)630
{631
Wl *w = data;632
if (!o) return; /* an output removed since */633
for (int i = 0; i < MAX_OUTPUTS; i++) if (w->outputs[i].output == o) w->outputs[i].entered = 1;634
update_scale(w);635
}636
static void surface_leave(void *data, struct wl_surface *s, struct wl_output *o)637
{638
Wl *w = data;639
if (!o) return;640
for (int i = 0; i < MAX_OUTPUTS; i++) if (w->outputs[i].output == o) w->outputs[i].entered = 0;641
update_scale(w);642
}643
static void surface_preferred_scale(void *data, struct wl_surface *s, int32_t scale)644
{645
Wl *w = data;646
w->preferred_scale = scale < 1 ? 1 : scale > 8 ? 8 : scale;647
update_scale(w);648
}649
static void surface_preferred_transform(void *data, struct wl_surface *s, uint32_t t) {}650
static const struct wl_surface_listener surface_listener = {651
.enter = surface_enter, .leave = surface_leave,652
.preferred_buffer_scale = surface_preferred_scale,653
.preferred_buffer_transform = surface_preferred_transform,654
};656
/* Children can complete a resize after the parent. Their old buffers657
* must not enlarge the toplevel's window geometry in the meantime. */658
static void commit_surface(Wl *w)659
{660
if (w->xdg_surface && w->configured &&661
(w->geometry_width != w->width || w->geometry_height != w->height)) {662
xdg_surface_set_window_geometry(w->xdg_surface, 0, 0, w->width, w->height);663
w->geometry_width = w->width;664
w->geometry_height = w->height;665
}666
wl_surface_commit(w->surface);667
}669
/* A configure from the shell: the new logical size, and a serial to ack.670
* A change of size is reported, and acked with the first present at the671
* new size. A configure that changes nothing (focus, tiling state) is672
* acked at once with an empty commit, since a compositor may wait for it. */673
static void configured(Wl *w, uint32_t serial, int width, int height)674
{675
if (w->closed) return;676
if (width > 0 && height > 0 && width <= 16384 && height <= 16384) {677
if (width != w->width || height != w->height) {678
w->width = width;679
w->height = height;680
if (w->configured) report_resize(w);681
}682
}683
w->ack_serial = serial;684
w->ack_due = 1;685
if (!w->configured) {686
trace_initial_configure(w, "configured");687
w->configured = 1;688
w->wake = 1;689
return;690
}691
if (w->mapped && w->committed_scale == w->scale && !w->pending.all) {692
Buffer *b = w->slot[0] ? w->slot[0] : w->slot[1];693
if (b && b->width == device_w(w) && b->height == device_h(w)) {694
if (w->layer) zwlr_layer_surface_v1_ack_configure(w->layer, serial);695
else xdg_surface_ack_configure(w->xdg_surface, serial);696
w->ack_due = 0;697
commit_surface(w);698
}699
}700
}702
static void wm_ping(void *data, struct xdg_wm_base *b, uint32_t serial) { xdg_wm_base_pong(b, serial); }703
static const struct xdg_wm_base_listener wm_listener = { .ping = wm_ping };705
static void xdg_configure(void *data, struct xdg_surface *s, uint32_t serial)706
{707
Wl *w = data;708
if (w->closed || w->dead) return;709
w->decoration_mode = w->pending_decoration_mode;710
configured(w, serial, w->pending_w, w->pending_h);711
window_service_configured(w, w->pending_fullscreen);712
}713
static const struct xdg_surface_listener xdg_surface_listener = { .configure = xdg_configure };715
static void decoration_configure(void *data, struct zxdg_toplevel_decoration_v1 *decoration, uint32_t mode)716
{717
Wl *w = data;718
if (mode != ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE && mode != ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE) {719
fail(w, "the compositor sent an invalid decoration mode");720
return;721
}722
w->pending_decoration_mode = (int)mode;723
}724
static const struct zxdg_toplevel_decoration_v1_listener decoration_listener = { .configure = decoration_configure };726
static void toplevel_configure(void *data, struct xdg_toplevel *t, int32_t width, int32_t height,727
struct wl_array *states)728
{729
Wl *w = data;730
w->pending_w = width; /* 0 lets the client choose: the size stays */731
w->pending_h = height;732
w->pending_fullscreen = 0;733
uint32_t *state;734
wl_array_for_each(state, states)735
if (*state == XDG_TOPLEVEL_STATE_FULLSCREEN) w->pending_fullscreen = 1;736
}737
static void toplevel_close(void *data, struct xdg_toplevel *t)738
{739
Wl *w = data;740
/* Before the first configure there is no application handle to receive741
* the event. End startup; configured windows still deliver UI_CLOSE. */742
if (!w->configured) w->closed = 1;743
push(w, UI_CLOSE);744
}745
static void toplevel_bounds(void *data, struct xdg_toplevel *t, int32_t width, int32_t height) {}746
static void toplevel_caps(void *data, struct xdg_toplevel *t, struct wl_array *caps) {}747
static const struct xdg_toplevel_listener toplevel_listener = {748
.configure = toplevel_configure, .close = toplevel_close,749
.configure_bounds = toplevel_bounds, .wm_capabilities = toplevel_caps,750
};752
static void layer_configure(void *data, struct zwlr_layer_surface_v1 *l, uint32_t serial,753
uint32_t width, uint32_t height)754
{755
configured(data, serial, (int)width, (int)height);756
}757
static void layer_closed(void *data, struct zwlr_layer_surface_v1 *l)758
{759
Wl *w = data;760
w->closed = 1;761
push(w, UI_CLOSE);762
}763
static const struct zwlr_layer_surface_v1_listener layer_listener = {764
.configure = layer_configure, .closed = layer_closed,765
};767
static void presentation_discard(Wl *w)768
{769
if (w->presentation_feedback) wl_proxy_destroy(w->presentation_feedback);770
w->presentation_feedback = NULL;771
w->presentation_frame = NULL;772
w->presentation_status = PRESENTATION_DISCARDED;773
}775
static void presentation_clock(void *data, struct wl_proxy *proxy, uint32_t clock_id)776
{777
(void)data; (void)proxy; (void)clock_id;778
}779
static const struct {780
void (*clock_id)(void *, struct wl_proxy *, uint32_t);781
} presentation_listener = { presentation_clock };783
static void presentation_output(void *data, struct wl_proxy *feedback, struct wl_output *output)784
{785
(void)data; (void)feedback; (void)output;786
}787
static void presentation_visible(void *data, struct wl_proxy *feedback, uint32_t sec_hi, uint32_t sec_lo,788
uint32_t nsec, uint32_t refresh, uint32_t sequence_hi, uint32_t sequence_lo, uint32_t flags)789
{790
(void)feedback; (void)sec_hi; (void)sec_lo; (void)nsec;791
(void)refresh; (void)sequence_hi; (void)sequence_lo; (void)flags;792
Wl *w = data;793
presentation_discard(w);794
w->presentation_status = PRESENTATION_VISIBLE;795
}796
static void presentation_discarded(void *data, struct wl_proxy *feedback)797
{798
(void)feedback;799
presentation_discard(data);800
}801
static const struct {802
void (*sync_output)(void *, struct wl_proxy *, struct wl_output *);803
void (*presented)(void *, struct wl_proxy *, uint32_t, uint32_t, uint32_t, uint32_t,804
uint32_t, uint32_t, uint32_t);805
void (*discarded)(void *, struct wl_proxy *);806
} presentation_feedback_listener = { presentation_output, presentation_visible, presentation_discarded };808
static void presentation_begin(Wl *w)809
{810
presentation_discard(w);811
if (++w->presentation_serial == 0) ++w->presentation_serial;812
w->presentation_status = PRESENTATION_PENDING;813
if (!w->presentation_global) {814
w->presentation_frame = w->frame;815
if (!w->presentation_frame) w->presentation_status = PRESENTATION_DISCARDED;816
return;817
}818
if (!w->presentation) {819
w->presentation = wl_registry_bind(w->registry, w->presentation_global, &presentation_interface, 1);820
if (w->presentation &&821
wl_proxy_add_listener(w->presentation, (void (**)(void))&presentation_listener, w) < 0) {822
wl_proxy_marshal_flags(w->presentation, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);823
w->presentation = NULL;824
}825
}826
if (w->presentation)827
w->presentation_feedback = wl_proxy_marshal_flags(w->presentation, 1, &presentation_feedback_interface,828
1, 0, w->surface, NULL);829
if (!w->presentation_feedback ||830
wl_proxy_add_listener(w->presentation_feedback, (void (**)(void))&presentation_feedback_listener, w) < 0)831
presentation_discard(w);832
}834
static void frame_done(void *data, struct wl_callback *cb, uint32_t time)835
{836
Wl *w = data;837
if (w->presentation_frame == cb) {838
w->presentation_frame = NULL;839
w->presentation_status = PRESENTATION_FRAME;840
}841
wl_callback_destroy(cb);842
w->frame = NULL;843
Event *e = push(w, UI_FRAME);844
if (e) e->v[0] = (int32_t)(time & 0x3fffffff);845
SuiWaylandFrame notify = w->frame_notify;846
void *frame_data = w->frame_data;847
w->frame_notify = NULL;848
w->frame_data = NULL;849
if (notify) notify(frame_data, time, 1);850
}851
static const struct wl_callback_listener frame_listener = { .done = frame_done };853
/* ---- keyboard */855
static int current_mods(Wl *w)856
{857
if (!w->keys) return 0;858
int m = 0;859
if (xkb_state_mod_name_is_active(w->keys, XKB_MOD_NAME_SHIFT, XKB_STATE_MODS_EFFECTIVE) > 0) m |= MOD_SHIFT;860
if (xkb_state_mod_name_is_active(w->keys, XKB_MOD_NAME_CTRL, XKB_STATE_MODS_EFFECTIVE) > 0) m |= MOD_CONTROL;861
if (xkb_state_mod_name_is_active(w->keys, XKB_MOD_NAME_ALT, XKB_STATE_MODS_EFFECTIVE) > 0) m |= MOD_ALT;862
if (xkb_state_mod_name_is_active(w->keys, XKB_MOD_NAME_LOGO, XKB_STATE_MODS_EFFECTIVE) > 0) m |= MOD_SUPER;863
if (xkb_state_mod_name_is_active(w->keys, XKB_MOD_NAME_CAPS, XKB_STATE_MODS_EFFECTIVE) > 0) m |= MOD_CAPS;864
if (xkb_state_mod_name_is_active(w->keys, XKB_MOD_NAME_NUM, XKB_STATE_MODS_EFFECTIVE) > 0) m |= MOD_NUM;865
return m;866
}868
/* Text is what a text field should insert: printable UTF-8 or nothing.869
* Control characters (Return, BackSpace, Ctrl+letter) are dropped; the870
* keysym name says what the key was. */871
static void keep_printable(char *text)872
{873
unsigned char c = (unsigned char)text[0];874
if (c < 0x20 || c == 0x7f) text[0] = 0;875
}877
static void key_event(Wl *w, uint32_t code, int action)878
{879
if (!w->keys) return;880
Event *e = push(w, UI_KEY);881
if (!e) return;882
xkb_keysym_t sym = xkb_state_key_get_one_sym(w->keys, code + 8);883
e->v[0] = action;884
e->v[2] = current_mods(w);885
e->v[3] = (int32_t)code;886
if (action == KEY_RELEASE) {887
e->v[1] = (int32_t)sym;888
return;889
}890
if (w->compose && action == KEY_PRESS &&891
xkb_compose_state_feed(w->compose, sym) == XKB_COMPOSE_FEED_ACCEPTED) {892
switch (xkb_compose_state_get_status(w->compose)) {893
case XKB_COMPOSE_COMPOSING:894
e->v[1] = (int32_t)sym;895
return; /* a dead key: no text yet */896
case XKB_COMPOSE_COMPOSED: {897
xkb_keysym_t s = xkb_compose_state_get_one_sym(w->compose);898
int n = xkb_compose_state_get_utf8(w->compose, e->text, TEXT);899
if (n < 0 || n >= TEXT) e->text[0] = 0;900
xkb_compose_state_reset(w->compose);901
e->v[1] = (int32_t)(s ? s : sym);902
keep_printable(e->text);903
return;904
}905
case XKB_COMPOSE_CANCELLED:906
xkb_compose_state_reset(w->compose);907
e->v[1] = (int32_t)sym;908
return;909
default:910
break;911
}912
}913
e->v[1] = (int32_t)sym;914
int n = xkb_state_key_get_utf8(w->keys, code + 8, e->text, TEXT);915
if (n < 0 || n >= TEXT) e->text[0] = 0;916
keep_printable(e->text);917
}919
static void kb_keymap(void *data, struct wl_keyboard *k, uint32_t format, int32_t fd, uint32_t size)920
{921
Wl *w = data;922
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1 || size == 0 || size > (1u << 22)) { close(fd); return; }923
char *text = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);924
close(fd);925
if (text == MAP_FAILED) return;926
struct xkb_keymap *map = NULL;927
if (text[size - 1] == 0)928
map = xkb_keymap_new_from_string(w->xkb, text, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);929
if (!map) { munmap(text, size); return; }930
struct xkb_state *state = xkb_state_new(map);931
if (!state) { xkb_keymap_unref(map); munmap(text, size); return; }932
char *copy = malloc(size);933
if (copy) {934
memcpy(copy, text, size);935
free(w->keymap_text);936
w->keymap_text = copy;937
w->keymap_size = size;938
}939
munmap(text, size);940
if (w->keys) xkb_state_unref(w->keys);941
if (w->keymap) xkb_keymap_unref(w->keymap);942
w->keymap = map;943
w->keys = state;944
}945
static void kb_enter(void *data, struct wl_keyboard *k, uint32_t serial, struct wl_surface *s, struct wl_array *keys)946
{947
Wl *w = data;948
w->input_serial = serial;949
w->keyboard_focus = 1;950
w->held_code = 0;951
Event *e = push(w, UI_KEYBOARD_FOCUS);952
if (e) e->v[0] = 1;953
}954
static void kb_leave(void *data, struct wl_keyboard *k, uint32_t serial, struct wl_surface *s)955
{956
Wl *w = data;957
w->keyboard_focus = 0;958
w->held_code = 0;959
if (w->compose) xkb_compose_state_reset(w->compose);960
Event *e = push(w, UI_KEYBOARD_FOCUS);961
if (e) e->v[0] = 0;962
}963
static void kb_key(void *data, struct wl_keyboard *k, uint32_t serial, uint32_t time, uint32_t code, uint32_t state)964
{965
Wl *w = data;966
w->input_serial = serial;967
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {968
key_event(w, code, KEY_PRESS);969
int composing = w->compose && xkb_compose_state_get_status(w->compose) == XKB_COMPOSE_COMPOSING;970
if (w->keymap && w->repeat_rate > 0 && !composing && xkb_keymap_key_repeats(w->keymap, code + 8)) {971
w->held_code = code;972
w->repeat_at = now_ms() + w->repeat_delay;973
} else if (composing || !w->keymap || w->repeat_rate <= 0) {974
w->held_code = 0;975
}976
/* A key that does not repeat (a modifier) leaves the held key977
* repeating, as it does in other toolkits. */978
} else {979
if (code == w->held_code) w->held_code = 0;980
key_event(w, code, KEY_RELEASE);981
}982
}983
static void kb_modifiers(void *data, struct wl_keyboard *k, uint32_t serial, uint32_t depressed,984
uint32_t latched, uint32_t locked, uint32_t group)985
{986
Wl *w = data;987
w->modifier_masks[0] = depressed;988
w->modifier_masks[1] = latched;989
w->modifier_masks[2] = locked;990
w->modifier_masks[3] = group;991
if (w->keys) xkb_state_update_mask(w->keys, depressed, latched, locked, 0, 0, group);992
/* This arrives after the modifier key event. Preserve its order relative993
* to pointer events instead of making clients infer state from keys. */994
Event *event = push(w, UI_MODIFIERS);995
if (event) event->v[0] = current_mods(w);996
}997
static void kb_repeat(void *data, struct wl_keyboard *k, int32_t rate, int32_t delay)998
{999
Wl *w = data;1000
w->repeat_rate = rate < 0 ? 0 : rate > 1000 ? 1000 : rate;1001
w->repeat_delay = delay < 0 ? 0 : delay > 5000 ? 5000 : delay;1002
if (w->repeat_rate == 0) w->held_code = 0; /* 0 turns repeat off */1003
}1004
static const struct wl_keyboard_listener keyboard_listener = {1005
.keymap = kb_keymap, .enter = kb_enter, .leave = kb_leave, .key = kb_key,1006
.modifiers = kb_modifiers, .repeat_info = kb_repeat,1007
};1009
/* ---- pointer */1011
static int floor_int(double v) { int i = (int)v; return (v < 0 && i != v) ? i - 1 : i; }1013
static void ptr_enter(void *data, struct wl_pointer *p, uint32_t serial, struct wl_surface *s,1014
wl_fixed_t x, wl_fixed_t y)1015
{1016
Wl *w = data;1017
w->pointer_inside = 1;1018
w->pointer_serial = serial;1019
w->px = wl_fixed_to_double(x);1020
w->py = wl_fixed_to_double(y);1021
if (w->cursor) cursor_apply(w);1022
else if (w->cursor_device)1023
wp_cursor_shape_device_v1_set_shape(w->cursor_device, serial, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);1024
Event *e = push(w, UI_POINTER_ENTER);1025
if (e) { e->v[0] = floor_int(w->px); e->v[1] = floor_int(w->py); }1026
}1027
static void ptr_leave(void *data, struct wl_pointer *p, uint32_t serial, struct wl_surface *s)1028
{1029
Wl *w = data;1030
w->pointer_inside = 0;1031
w->pointer_serial = 0;1032
if (w->cursor) w->cursor->backend = SUI_CURSOR_UNAVAILABLE;1033
push(w, UI_POINTER_LEAVE);1034
}1035
static void ptr_motion(void *data, struct wl_pointer *p, uint32_t time, wl_fixed_t x, wl_fixed_t y)1036
{1037
Wl *w = data;1038
w->px = wl_fixed_to_double(x);1039
w->py = wl_fixed_to_double(y);1040
Event *e = push(w, UI_MOTION);1041
if (e) { e->v[0] = floor_int(w->px); e->v[1] = floor_int(w->py); }1042
}1043
static void ptr_button(void *data, struct wl_pointer *p, uint32_t serial, uint32_t time, uint32_t button,1044
uint32_t state)1045
{1046
Wl *w = data;1047
w->input_serial = serial;1048
Event *e = push(w, UI_BUTTON);1049
if (e) {1050
e->v[0] = (int32_t)button;1051
e->v[1] = state == WL_POINTER_BUTTON_STATE_PRESSED;1052
e->v[2] = floor_int(w->px);1053
e->v[3] = floor_int(w->py);1054
}1055
}1056
static void flush_scroll(Wl *w)1057
{1058
Event *e = w->scroll_pending ? push(w, UI_SCROLL) : NULL;1059
if (e) {1060
e->v[0] = w->scroll_x; e->v[1] = w->scroll_y;1061
e->v[2] = w->steps_x; e->v[3] = w->steps_y;1062
e->v[4] = floor_int(w->px); e->v[5] = floor_int(w->py);1063
e->v[6] = w->scroll_source;1064
e->v[7] = w->value120_x; e->v[8] = w->value120_y;1065
e->v[9] = w->scroll_stops;1066
}1067
w->scroll_x = w->scroll_y = w->steps_x = w->steps_y = 0;1068
w->scroll_source = w->value120_x = w->value120_y = w->scroll_stops = 0;1069
w->scroll_pending = 0;1070
}1071
static void ptr_axis(void *data, struct wl_pointer *p, uint32_t time, uint32_t axis, wl_fixed_t value)1072
{1073
Wl *w = data;1074
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) w->scroll_y += value;1075
else w->scroll_x += value;1076
w->scroll_pending = 1;1077
/* Before seat version 5 there are no frames to group axis events. */1078
if (wl_pointer_get_version(p) < 5) flush_scroll(w);1079
}1080
static void ptr_frame(void *data, struct wl_pointer *p) { flush_scroll(data); }1081
static void ptr_axis_source(void *data, struct wl_pointer *p, uint32_t source)1082
{1083
Wl *w = data;1084
int32_t next = (int32_t)source + 1;1085
if (next != w->scroll_remainder_source)1086
w->scroll_remainder_x = w->scroll_remainder_y = 0;1087
w->scroll_remainder_source = next;1088
/* Zero means that the compositor did not supply a source this frame. */1089
w->scroll_source = next;1090
}1091
static void ptr_axis_stop(void *data, struct wl_pointer *p, uint32_t time, uint32_t axis)1092
{1093
Wl *w = data;1094
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) w->scroll_remainder_y = 0;1095
else w->scroll_remainder_x = 0;1096
w->scroll_stops |= axis == WL_POINTER_AXIS_VERTICAL_SCROLL ? 2 : 1;1097
w->scroll_pending = 1;1098
}1099
static void ptr_axis_value120(void *data, struct wl_pointer *p, uint32_t axis, int32_t value)1100
{1101
Wl *w = data;1102
int vertical = axis == WL_POINTER_AXIS_VERTICAL_SCROLL;1103
int32_t *remainder = vertical ? &w->scroll_remainder_y : &w->scroll_remainder_x;1104
int32_t *steps = vertical ? &w->steps_y : &w->steps_x;1105
/* v8 replaces axis_discrete with value120. Keep the whole-notch1106
* contract for legacy callers, retaining fractions across frames. */1107
if ((value < 0 && *remainder > 0) || (value > 0 && *remainder < 0))1108
*remainder = 0;1109
int64_t total = (int64_t)*remainder + value;1110
*steps += (int32_t)(total / 120);1111
*remainder = (int32_t)(total % 120);1112
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) w->value120_y += value;1113
else w->value120_x += value;1114
w->scroll_pending = 1;1115
}1116
static void ptr_axis_discrete(void *data, struct wl_pointer *p, uint32_t axis, int32_t steps)1117
{1118
Wl *w = data;1119
if (axis == WL_POINTER_AXIS_VERTICAL_SCROLL) w->steps_y += steps;1120
else w->steps_x += steps;1121
w->scroll_pending = 1;1122
}1123
static const struct wl_pointer_listener pointer_listener = {1124
.enter = ptr_enter, .leave = ptr_leave, .motion = ptr_motion, .button = ptr_button,1125
.axis = ptr_axis, .frame = ptr_frame, .axis_source = ptr_axis_source,1126
.axis_stop = ptr_axis_stop, .axis_discrete = ptr_axis_discrete,1127
.axis_value120 = ptr_axis_value120,1128
};1130
#include "window-wayland-services.inc"1132
/* ---- seat and registry */1134
static void seat_caps(void *data, struct wl_seat *seat, uint32_t caps)1135
{1136
Wl *w = data;1137
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !w->keyboard) {1138
w->keyboard = wl_seat_get_keyboard(seat);1139
wl_keyboard_add_listener(w->keyboard, &keyboard_listener, w);1140
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && w->keyboard) {1141
if (wl_keyboard_get_version(w->keyboard) >= 3) wl_keyboard_release(w->keyboard);1142
else wl_keyboard_destroy(w->keyboard);1143
w->keyboard = NULL;1144
w->held_code = 0;1145
if (w->keyboard_focus) {1146
w->keyboard_focus = 0;1147
Event *e = push(w, UI_KEYBOARD_FOCUS);1148
if (e) e->v[0] = 0;1149
}1150
}1151
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !w->pointer) {1152
w->pointer = wl_seat_get_pointer(seat);1153
wl_pointer_add_listener(w->pointer, &pointer_listener, w);1154
if (w->cursor_manager)1155
w->cursor_device = wp_cursor_shape_manager_v1_get_pointer(w->cursor_manager, w->pointer);1156
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && w->pointer) {1157
if (w->cursor_device) { wp_cursor_shape_device_v1_destroy(w->cursor_device); w->cursor_device = NULL; }1158
if (wl_pointer_get_version(w->pointer) >= 3) wl_pointer_release(w->pointer);1159
else wl_pointer_destroy(w->pointer);1160
w->pointer = NULL;1161
w->pointer_serial = 0;1162
if (w->cursor) w->cursor->backend = SUI_CURSOR_UNAVAILABLE;1163
if (w->pointer_inside) {1164
w->pointer_inside = 0;1165
push(w, UI_POINTER_LEAVE);1166
}1167
}1168
}1169
static void seat_name(void *data, struct wl_seat *s, const char *name) {}1170
static const struct wl_seat_listener seat_listener = { .capabilities = seat_caps, .name = seat_name };1172
static uint32_t min_u(uint32_t a, uint32_t b) { return a < b ? a : b; }1174
/* SIGIL_UI_WAYLAND_IGNORE: a comma-separated list of globals to act as if1175
* the compositor did not offer, to exercise the fallbacks (for example1176
* zwlr_layer_shell_v1). */1177
static int ignored(const char *iface)1178
{1179
const char *list = getenv("SIGIL_UI_WAYLAND_IGNORE");1180
size_t n = strlen(iface);1181
while (list && *list) {1182
const char *end = strchr(list, ',');1183
size_t len = end ? (size_t)(end - list) : strlen(list);1184
if (len == n && !strncmp(list, iface, n)) return 1;1185
list = end ? end + 1 : NULL;1186
}1187
return 0;1188
}1190
static void export_close(Wl *w)1191
{1192
if (w->exported)1193
wl_proxy_marshal_flags(w->exported, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);1194
if (w->exporter)1195
wl_proxy_marshal_flags(w->exporter, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);1196
w->exported = w->exporter = NULL;1197
free(w->export_handle);1198
w->export_handle = NULL;1199
}1201
static void export_ready(void *data, struct wl_proxy *exported, const char *handle)1202
{1203
Wl *w = data;1204
if (w->closed || w->exported != exported || w->export_handle || !handle || !*handle) return;1205
w->export_handle = strdup(handle);1206
}1208
static const struct {1209
void (*handle)(void *, struct wl_proxy *, const char *);1210
} export_listener = { .handle = export_ready };1212
static void global(void *data, struct wl_registry *r, uint32_t id, const char *iface, uint32_t version)1213
{1214
Wl *w = data;1215
if (ignored(iface)) return;1216
if (!strcmp(iface, wl_compositor_interface.name) && !w->compositor && version >= 4)1217
w->compositor = wl_registry_bind(r, id, &wl_compositor_interface, min_u(version, 6));1218
else if (!strcmp(iface, wl_subcompositor_interface.name) && !w->subcompositor)1219
w->subcompositor = wl_registry_bind(r, id, &wl_subcompositor_interface, 1);1220
else if (!strcmp(iface, xdg_activation_v1_interface.name) && !w->activation) {1221
w->activation = wl_registry_bind(r, id, &xdg_activation_v1_interface, 1);1222
w->activation_global = id;1223
}1224
else if (!strcmp(iface, exporter_interface.name) && !w->exporter_global)1225
w->exporter_global = id;1226
else if (!strcmp(iface, presentation_interface.name) && !w->presentation_global)1227
w->presentation_global = id;1228
else if (!strcmp(iface, zwp_linux_dmabuf_v1_interface.name) && !w->dmabuf_global) {1229
/* Record availability only; SHM clients never bind this global. */1230
w->dmabuf_global = id;1231
w->dmabuf_version = version;1232
}1233
else if (!strcmp(iface, wl_data_device_manager_interface.name) && !w->data_manager)1234
w->data_manager = wl_registry_bind(r, id, &wl_data_device_manager_interface, min_u(version, 3));1235
else if (!strcmp(iface, wl_shm_interface.name) && !w->shm)1236
w->shm = wl_registry_bind(r, id, &wl_shm_interface, 1);1237
else if (w->server_decorations && !strcmp(iface, zxdg_decoration_manager_v1_interface.name) && !w->decoration_manager)1238
w->decoration_manager = wl_registry_bind(r, id, &zxdg_decoration_manager_v1_interface, 1);1239
else if (!strcmp(iface, wl_seat_interface.name) && !w->seat) {1240
w->seat = wl_registry_bind(r, id, &wl_seat_interface, min_u(version, 8));1241
wl_seat_add_listener(w->seat, &seat_listener, w);1242
} else if (!strcmp(iface, xdg_wm_base_interface.name) && !w->wm_base) {1243
w->wm_base = wl_registry_bind(r, id, &xdg_wm_base_interface, min_u(version, 5));1244
xdg_wm_base_add_listener(w->wm_base, &wm_listener, w);1245
} else if (!strcmp(iface, zwlr_layer_shell_v1_interface.name) && !w->layer_shell)1246
w->layer_shell = wl_registry_bind(r, id, &zwlr_layer_shell_v1_interface, min_u(version, 4));1247
else if (!strcmp(iface, wp_cursor_shape_manager_v1_interface.name) && !w->cursor_manager) {1248
w->cursor_manager = wl_registry_bind(r, id, &wp_cursor_shape_manager_v1_interface, 1);1249
if (w->pointer)1250
w->cursor_device = wp_cursor_shape_manager_v1_get_pointer(w->cursor_manager, w->pointer);1251
}1252
else if (!strcmp(iface, wl_output_interface.name)) {1253
for (int i = 0; i < MAX_OUTPUTS; i++) {1254
Output *o = &w->outputs[i];1255
if (o->output) continue;1256
o->w = w;1257
o->global = id;1258
o->scale = 1;1259
o->entered = 0;1260
o->name[0] = 0;1261
o->output = wl_registry_bind(r, id, &wl_output_interface, min_u(version, 4));1262
wl_output_add_listener(o->output, &output_listener, o);1263
return;1264
}1265
}1266
}1267
static void global_remove(void *data, struct wl_registry *r, uint32_t id)1268
{1269
Wl *w = data;1270
if (w->activation && id == w->activation_global) {1271
xdg_activation_v1_destroy(w->activation);1272
w->activation = NULL;1273
w->activation_global = 0;1274
}1275
if (id == w->exporter_global) {1276
export_close(w);1277
w->exporter_global = 0;1278
}1279
if (id == w->presentation_global) {1280
presentation_discard(w);1281
if (w->presentation) wl_proxy_marshal_flags(w->presentation, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);1282
w->presentation = NULL;1283
w->presentation_global = 0;1284
}1285
if (id == w->dmabuf_global) {1286
dmabuf_imports_revoke_pending(w);1287
dmabuf_feedback_revoke_tree(w);1288
if (w->dmabuf) zwp_linux_dmabuf_v1_destroy(w->dmabuf);1289
w->dmabuf = NULL;1290
w->dmabuf_global = w->dmabuf_version = 0;1291
}1292
for (int i = 0; i < MAX_OUTPUTS; i++) {1293
Output *o = &w->outputs[i];1294
if (o->output && o->global == id) {1295
if (wl_output_get_version(o->output) >= 3) wl_output_release(o->output);1296
else wl_output_destroy(o->output);1297
memset(o, 0, sizeof *o);1298
update_scale(w);1299
}1300
}1301
}1302
static const struct wl_registry_listener registry_listener = { .global = global, .global_remove = global_remove };1304
/* ------------------------------------------------------------ open and close */1306
static void wl_teardown(Wl *w)1307
{1308
if (!w || w->closing || w->torn_down) return;1309
if (!w->parent && w->connection_depth) {1310
/* libwayland still uses the display/queue after a listener returns.1311
* Close becomes visible now, but disconnect waits for the outermost1312
* dispatcher, which retains both the connection and its caller. */1313
w->closed = 1;1314
w->close_pending = 1;1315
return;1316
}1317
/* Notifications may drop every external reference, including when a1318
* parent closes this child. Keep the allocation until teardown returns. */1319
w->references++;1320
w->closing = 1;1321
w->closed = 1;1322
Wl *parent = w->parent;1323
SuiWaylandDmabufImport *canceled_imports = NULL;1324
if (parent) {1325
/* A canceled child notification can close its parent too. Finish1326
* destroying this child's proxies before disconnecting the root. */1327
parent->references++;1328
parent->connection_depth++;1329
Wl **link = &parent->children;1330
while (*link && *link != w) link = &(*link)->sibling;1331
if (*link) *link = w->sibling;1332
}1333
while (w->children) wl_teardown(w->children);1334
dmabuf_feedback_close(w);1335
if (!parent) canceled_imports = dmabuf_imports_close(w);1336
if (w->display) {1337
presentation_discard(w);1338
if (w->presentation) wl_proxy_marshal_flags(w->presentation, 0, NULL, 1, WL_MARSHAL_FLAG_DESTROY);1339
w->presentation = NULL;1340
if (w->frame) wl_callback_destroy(w->frame);1341
w->frame = NULL;1342
SuiWaylandFrame canceled = w->frame_notify;1343
void *frame_data = w->frame_data;1344
w->frame_notify = NULL;1345
w->frame_data = NULL;1346
if (canceled) canceled(frame_data, 0, 0);1347
for (int i = 0; i < 2; i++) { buffer_free(w->slot[i]); w->slot[i] = NULL; }1348
while (w->retired) { Buffer *n = w->retired->next; buffer_free(w->retired); w->retired = n; }1349
if (w->subsurface) wl_subsurface_destroy(w->subsurface);1350
if (parent) {1351
if (w->surface) wl_surface_destroy(w->surface);1352
goto closed;1353
}1354
export_close(w);1355
if (w->layer) zwlr_layer_surface_v1_destroy(w->layer);1356
if (w->decoration) zxdg_toplevel_decoration_v1_destroy(w->decoration);1357
if (w->toplevel) xdg_toplevel_destroy(w->toplevel);1358
if (w->xdg_surface) xdg_surface_destroy(w->xdg_surface);1359
if (w->surface) wl_surface_destroy(w->surface);1360
if (w->data_source) wl_data_source_destroy(w->data_source);1361
while (w->offers) {1362
Offer *next = w->offers->next;1363
wl_data_offer_destroy(w->offers->offer);1364
free(w->offers);1365
w->offers = next;1366
}1367
for (int i = 0; i < CLIPBOARD_WRITES; i++) write_close(&w->writes[i]);1368
if (w->data_device) {1369
if (wl_data_device_get_version(w->data_device) >= 2) wl_data_device_release(w->data_device);1370
else wl_data_device_destroy(w->data_device);1371
}1372
if (w->data_manager) wl_data_device_manager_destroy(w->data_manager);1373
cursor_close(w);1374
if (w->cursor_device) wp_cursor_shape_device_v1_destroy(w->cursor_device);1375
if (w->keyboard) wl_keyboard_destroy(w->keyboard);1376
if (w->pointer) wl_pointer_destroy(w->pointer);1377
for (int i = 0; i < MAX_OUTPUTS; i++) if (w->outputs[i].output) wl_output_destroy(w->outputs[i].output);1378
if (w->cursor_manager) wp_cursor_shape_manager_v1_destroy(w->cursor_manager);1379
if (w->layer_shell) {1380
/* zwlr_layer_shell_v1.destroy exists from version 3 */1381
if (zwlr_layer_shell_v1_get_version(w->layer_shell) >= 3) zwlr_layer_shell_v1_destroy(w->layer_shell);1382
else wl_proxy_destroy((struct wl_proxy *)w->layer_shell);1383
}1384
if (w->activation) xdg_activation_v1_destroy(w->activation);1385
w->activation = NULL;1386
if (w->wm_base) xdg_wm_base_destroy(w->wm_base);1387
if (w->decoration_manager) zxdg_decoration_manager_v1_destroy(w->decoration_manager);1388
if (w->seat) wl_seat_destroy(w->seat);1389
if (w->shm) wl_shm_destroy(w->shm);1390
if (w->dmabuf) zwp_linux_dmabuf_v1_destroy(w->dmabuf);1391
if (w->subcompositor) wl_subcompositor_destroy(w->subcompositor);1392
if (w->compositor) wl_compositor_destroy(w->compositor);1393
if (w->registry) wl_registry_destroy(w->registry);1394
wl_display_flush(w->display);1395
wl_display_disconnect(w->display);1396
w->display = NULL;1397
}1398
dmabuf_imports_disconnected(canceled_imports);1399
if (w->compose) xkb_compose_state_unref(w->compose);1400
if (w->compose_table) xkb_compose_table_unref(w->compose_table);1401
if (w->keys) xkb_state_unref(w->keys);1402
if (w->keymap) xkb_keymap_unref(w->keymap);1403
if (w->xkb) xkb_context_unref(w->xkb);1404
closed:1405
free(w->alpha_mask);1406
free(w->keymap_text);1407
free(w->clipboard);1408
/* What the surface was stays readable after it is closed. */1409
int role = w->role, width = w->width, height = w->height, scale = w->scale;1410
unsigned references = w->references;1411
memset(w, 0, sizeof *w);1412
w->references = references;1413
w->role = role;1414
w->width = width;1415
w->height = height;1416
w->scale = scale;1417
w->dead = 1;1418
w->closed = 1;1419
w->torn_down = 1;1420
if (parent) {1421
parent->connection_depth--;1422
if (!parent->connection_depth && parent->close_pending && !parent->closing) {1423
parent->close_pending = 0;1424
wl_teardown(parent);1425
}1426
sui_wayland_unref(parent);1427
sui_wayland_unref(parent);1428
}1429
sui_wayland_unref(w);1430
}1432
static void wl_finalize(void *data)1433
{1434
Wl *w = data;1435
if (w) { wl_teardown(w); sui_wayland_unref(w); }1436
}1438
static Wl *as_wl(SigilVM *vm, Value v, const char *who)1439
{1440
if (!sigil_is_foreign(v) || sigil_foreign_type(v) != wl_tag) {1441
sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: not a Wayland surface", who);1442
return NULL;1443
}1444
Wl *w = sigil_foreign_data(v);1445
if (!w->display) {1446
sigil__vm_error(vm, SIGIL_ERR_IO, "%s: surface is closed", who);1447
return NULL;1448
}1449
return w;1450
}1452
SuiWaylandSurface *sui_wayland_acquire(SigilVM *vm, Value handle)1453
{1454
Wl *w = as_wl(vm, handle, "wayland native bridge");1455
if (w && !w->dead && !w->closed) { w->references++; return w; }1456
return NULL;1457
}1459
void sui_wayland_unref(SuiWaylandSurface *w)1460
{1461
if (w && --w->references == 0) {1462
if (w->closing) return;1463
if (w->torn_down) free(w);1464
else wl_teardown(w);1465
}1466
}1468
void sui_wayland_close(SuiWaylandSurface *w) { wl_teardown(w); }1470
int sui_wayland_size(SuiWaylandSurface *w, int *width, int *height, int *scale)1471
{1472
if (!w || !w->display || w->dead || w->closed) return 0;1473
if (width) *width = w->width;1474
if (height) *height = w->height;1475
if (scale) *scale = w->scale;1476
return 1;1477
}1479
const char *sui_wayland_export_handle(SuiWaylandSurface *w)1480
{1481
if (!w || !w->display || w->parent || !w->toplevel || !w->exporter_global || w->dead || w->closed)1482
return NULL;1483
if (w->exported) return w->export_handle;1484
if (!w->exporter)1485
w->exporter = wl_registry_bind(w->registry, w->exporter_global, &exporter_interface, 1);1486
if (!w->exporter) return NULL;1487
w->exported = wl_proxy_marshal_flags(w->exporter, 1, &exported_interface, 1, 0, NULL, w->surface);1488
if (w->exported)1489
wl_proxy_add_listener(w->exported, (void (**)(void))&export_listener, w);1490
if (wl_display_flush(w->display) < 0 && errno != EAGAIN)1491
fail(w, "the compositor connection failed");1492
return NULL;1493
}1495
int sui_wayland_fullscreen_state(SuiWaylandSurface *w)1496
{1497
if (!w || !w->display || !w->toplevel || w->parent || w->dead || w->closed) return -1;1498
return w->fullscreen;1499
}1501
int sui_wayland_set_fullscreen(SuiWaylandSurface *w, int fullscreen)1502
{1503
if (sui_wayland_fullscreen_state(w) < 0 || (fullscreen != 0 && fullscreen != 1)) return 0;1504
if (fullscreen) xdg_toplevel_set_fullscreen(w->toplevel, NULL);1505
else xdg_toplevel_unset_fullscreen(w->toplevel);1506
if (wl_display_flush(w->display) < 0 && errno != EAGAIN) {1507
fail(w, "the compositor connection failed");1508
return 0;1509
}1510
return 1;1511
}1513
const char *sui_wayland_keymap(SuiWaylandSurface *w, size_t *size)1514
{1515
while (w && w->parent) w = w->parent;1516
if (size) *size = w ? w->keymap_size : 0;1517
return w ? w->keymap_text : NULL;1518
}1520
int sui_wayland_geometry(SuiWaylandSurface *w, int x, int y, int width, int height)1521
{1522
if (!w || !w->parent || !w->display || w->dead || w->closed ||1523
width < 1 || height < 1 || width > 16384 || height > 16384) return 0;1524
wl_subsurface_set_position(w->subsurface, x, y);1525
if (width != w->width || height != w->height) {1526
w->width = width;1527
w->height = height;1528
report_resize(w);1529
}1530
wl_surface_commit(w->parent->surface);1531
return 1;1532
}1534
void sui_wayland_modifiers(SuiWaylandSurface *w, uint32_t masks[4])1535
{1536
while (w && w->parent) w = w->parent;1537
if (!masks) return;1538
if (w) memcpy(masks, w->modifier_masks, sizeof w->modifier_masks);1539
else memset(masks, 0, sizeof(uint32_t) * 4);1540
}1542
uint64_t sui_wayland_clipboard_generation(SuiWaylandSurface *w, int *has_text)1543
{1544
while (w && w->parent) w = w->parent;1545
if (has_text) *has_text = w && !w->dead && !w->closed &&1546
(w->data_source || (w->selection && (w->selection->utf8 || w->selection->plain)));1547
return w ? w->clipboard_generation : 0;1548
}1550
SuiWaylandSurface *sui_wayland_child(SuiWaylandSurface *parent, int x, int y,1551
int width, int height, int overlay)1552
{1553
if (!parent || !parent->display || parent->dead || parent->closed ||1554
parent->parent || !parent->subcompositor || width < 1 || height < 1 ||1555
width > 16384 || height > 16384) return NULL;1556
Wl *w = calloc(1, sizeof *w);1557
if (!w) return NULL;1558
w->references = 1;1559
w->parent = parent;1560
w->service_thread = parent->service_thread;1561
parent->references++;1562
w->sibling = parent->children;1563
parent->children = w;1564
w->display = parent->display;1565
w->compositor = parent->compositor;1566
w->shm = parent->shm;1567
w->scale = parent->scale;1568
w->width = width;1569
w->height = height;1570
w->configured = 1;1571
w->role = HAS_SUBSURFACE;1572
w->alpha = overlay;1573
w->pending.all = 1;1574
w->surface = wl_compositor_create_surface(w->compositor);1575
w->subsurface = wl_subcompositor_get_subsurface(parent->subcompositor, w->surface, parent->surface);1576
wl_subsurface_set_position(w->subsurface, x, y);1577
wl_subsurface_set_desync(w->subsurface);1578
if (overlay) {1579
struct wl_region *empty = wl_compositor_create_region(w->compositor);1580
wl_surface_set_input_region(w->surface, empty);1581
wl_region_destroy(empty);1582
wl_subsurface_place_above(w->subsurface, parent->surface);1583
} else {1584
wl_subsurface_place_below(w->subsurface, parent->surface);1585
}1586
wl_surface_commit(parent->surface);1587
return w;1588
}1590
static int str_arg(SigilVM *vm, Value v, char *buf, size_t n, int allow_false, const char *who)1591
{1592
if (allow_false && sigil_is_false(v)) { buf[0] = 0; return 1; }1593
if (!sigil_is_string(v)) {1594
sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: expected a string", who);1595
return 0;1596
}1597
size_t len = ((SigilString *)sigil_as_ptr(v))->byte_length;1598
if (len + 1 > n) {1599
sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: string too long", who);1600
return 0;1601
}1602
memcpy(buf, sigil_string_bytes(v), len);1603
buf[len] = 0;1604
return 1;1605
}1607
static int int_arg(SigilVM *vm, Value v, int64_t *out, const char *who)1608
{1609
if (!sigil_is_fixnum(v)) {1610
sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: expected an exact integer", who);1611
return 0;1612
}1613
*out = sigil_as_fixnum(v);1614
return 1;1615
}1617
static int cursor_string_arg(SigilVM *vm, Value value, char *buffer, size_t size,1618
int allow_false, const char *who)1619
{1620
if (!str_arg(vm, value, buffer, size, allow_false, who)) return 0;1621
if (!sigil_is_false(value) &&1622
(strlen(buffer) != ((SigilString *)sigil_as_ptr(value))->byte_length || !*buffer)) {1623
sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: expected nonempty text without NUL", who);1624
return 0;1625
}1626
return 1;1627
}1629
static Value native_wl_cursor_configure(SigilVM *vm, int argc, Value *args)1630
{1631
Wl *w = as_wl(vm, args[0], "wayland-cursor-configure!");1632
char theme[256];1633
int64_t size;1634
if (!w || !cursor_string_arg(vm, args[1], theme, sizeof theme, 1, "wayland-cursor-configure!") ||1635
!int_arg(vm, args[2], &size, "wayland-cursor-configure!")) return DONE;1636
if (size < 0 || size > 128) {1637
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-cursor-configure!: size must be 0..128");1638
return DONE;1639
}1640
return sui_wayland_cursor_configure(w, sigil_is_false(args[1]) ? NULL : theme, (int)size)1641
? SIGIL_TRUE : SIGIL_FALSE;1642
}1644
static Value native_wl_cursor_name(SigilVM *vm, int argc, Value *args)1645
{1646
Wl *w = as_wl(vm, args[0], "wayland-set-cursor!");1647
char name[64];1648
if (!w || !cursor_string_arg(vm, args[1], name, sizeof name, 0, "wayland-set-cursor!")) return DONE;1649
return sui_wayland_cursor_name(w, name) ? SIGIL_TRUE : SIGIL_FALSE;1650
}1652
static Value native_wl_cursor_backend(SigilVM *vm, int argc, Value *args)1653
{1654
Wl *w = as_wl(vm, args[0], "wayland-cursor-backend");1655
return w ? sigil_fixnum(sui_wayland_cursor_backend(w)) : DONE;1656
}1658
static Value native_wl_cursor_image(SigilVM *vm, int argc, Value *args)1659
{1660
Wl *w = as_wl(vm, args[0], "wayland-set-cursor-image!");1661
if (!w) return DONE;1662
if (!sigil_is_bytevector(args[1])) {1663
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-set-cursor-image!: expected ARGB bytes");1664
return DONE;1665
}1666
int64_t values[6];1667
for (int i = 0; i < 6; i++)1668
if (!int_arg(vm, args[i + 2], &values[i], "wayland-set-cursor-image!")) return DONE;1669
int64_t width = values[0], height = values[1], stride = values[2];1670
int64_t x = values[3], y = values[4], scale = values[5];1671
size_t length = sigil_bytevector_length(args[1]);1672
if (width < 1 || width > 512 || height < 1 || height > 512 ||1673
stride < width * 4 || stride > 2048 || stride * height > (int64_t)length ||1674
x < 0 || x >= width || y < 0 || y >= height || scale < 1 || scale > 8 ||1675
width % scale || height % scale) {1676
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-set-cursor-image!: invalid dimensions, stride, hotspot or scale");1677
return DONE;1678
}1679
return sui_wayland_cursor_image(w, sigil_bytevector_data(args[1]), length,1680
(int)width, (int)height, (size_t)stride, (int)x, (int)y, (int)scale) ? SIGIL_TRUE : SIGIL_FALSE;1681
}1683
static Value native_wl_child(SigilVM *vm, int argc, Value *args)1684
{1685
Wl *parent = as_wl(vm, args[0], "wayland-subsurface");1686
int64_t x, y, width, height;1687
if (!parent || !int_arg(vm, args[1], &x, "wayland-subsurface") ||1688
!int_arg(vm, args[2], &y, "wayland-subsurface") ||1689
!int_arg(vm, args[3], &width, "wayland-subsurface") ||1690
!int_arg(vm, args[4], &height, "wayland-subsurface")) return DONE;1691
if (x < -16384 || x > 16384 || y < -16384 || y > 16384 ||1692
width < 1 || width > 16384 || height < 1 || height > 16384) {1693
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-subsurface: invalid position or size");1694
return DONE;1695
}1696
Wl *w = sui_wayland_child(parent, (int)x, (int)y, (int)width, (int)height, !sigil_is_false(args[5]));1697
if (!w) {1698
sigil__vm_error(vm, SIGIL_ERR_IO, "wayland-subsurface: parent is closed or subcompositor is unavailable");1699
return DONE;1700
}1701
Value handle = sigil_make_foreign(vm, wl_tag, w, wl_finalize, sizeof *w);1702
if (sigil_is_undefined(handle)) { wl_teardown(w); sui_wayland_unref(w); }1703
return handle;1704
}1706
static Value native_wl_alpha(SigilVM *vm, int argc, Value *args)1707
{1708
Wl *w = as_wl(vm, args[0], "wayland-set-alpha!");1709
if (!w) return DONE;1710
size_t size = (size_t)device_w(w) * device_h(w);1711
if (!w->alpha || !sigil_is_bytevector(args[1]) || sigil_bytevector_length(args[1]) != size) {1712
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-set-alpha!: expected a transparent surface and one alpha byte per device pixel");1713
return DONE;1714
}1715
uint8_t *mask = malloc(size);1716
if (!mask) { sigil__vm_error(vm, SIGIL_ERR_IO, "wayland-set-alpha!: allocation failed"); return DONE; }1717
memcpy(mask, sigil_bytevector_data(args[1]), size);1718
free(w->alpha_mask);1719
w->alpha_mask = mask;1720
w->alpha_size = size;1721
w->pending.all = 1;1722
return DONE;1723
}1725
static Value native_wl_keymap(SigilVM *vm, int argc, Value *args)1726
{1727
Wl *w = as_wl(vm, args[0], "wayland-keymap");1728
if (!w) return DONE;1729
size_t size;1730
const char *text = sui_wayland_keymap(w, &size);1731
return text && size ? sigil_make_string(vm, text, size - 1) : SIGIL_FALSE;1732
}1734
static Value native_wl_fullscreen_state(SigilVM *vm, int argc, Value *args)1735
{1736
Wl *w = as_wl(vm, args[0], "wayland-surface-fullscreen?");1737
return w ? sigil_bool(sui_wayland_fullscreen_state(w) == 1) : DONE;1738
}1740
static Value native_wl_fullscreen(SigilVM *vm, int argc, Value *args)1741
{1742
Wl *w = as_wl(vm, args[0], "wayland-set-fullscreen!");1743
if (!w) return DONE;1744
if (!sigil_is_bool(args[1])) {1745
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-set-fullscreen!: expected a boolean");1746
return DONE;1747
}1748
return sigil_bool(sui_wayland_set_fullscreen(w, sigil_as_bool(args[1])));1749
}1751
static Value native_wl_title(SigilVM *vm, int argc, Value *args)1752
{1753
Wl *w = as_wl(vm, args[0], "wayland-set-title!");1754
char title[4096];1755
if (!w || !str_arg(vm, args[1], title, sizeof title, 0, "wayland-set-title!")) return DONE;1756
if (memchr(title, 0, ((SigilString *)sigil_as_ptr(args[1]))->byte_length)) {1757
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-set-title!: title contains NUL");1758
return DONE;1759
}1760
if (!w->toplevel || w->closed || w->dead) {1761
sigil__vm_error(vm, SIGIL_ERR_IO, "wayland-set-title!: expected a live toplevel");1762
return DONE;1763
}1764
if (!strcmp(w->title, title)) return SIGIL_FALSE;1765
xdg_toplevel_set_title(w->toplevel, title);1766
memcpy(w->title, title, strlen(title) + 1);1767
if (wl_display_flush(w->display) < 0 && errno != EAGAIN) {1768
fail(w, "the compositor connection failed");1769
sigil__vm_error(vm, SIGIL_ERR_IO, "wayland-set-title!: the compositor connection failed");1770
return DONE;1771
}1772
return SIGIL_TRUE;1773
}1775
static Value native_wl_clipboard_set(SigilVM *vm, int argc, Value *args)1776
{1777
Wl *w = as_wl(vm, args[0], "wayland-clipboard-set!");1778
if (!w) return DONE;1779
if (!sigil_is_string(args[1])) {1780
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-clipboard-set!: expected text");1781
return DONE;1782
}1783
size_t size = ((SigilString *)sigil_as_ptr(args[1]))->byte_length;1784
return sui_wayland_clipboard_set(w, (const uint8_t *)sigil_string_bytes(args[1]), size)1785
? SIGIL_TRUE : SIGIL_FALSE;1786
}1788
static Value native_wl_clipboard_read(SigilVM *vm, int argc, Value *args)1789
{1790
Wl *w = as_wl(vm, args[0], "wayland-clipboard-read");1791
if (!w) return DONE;1792
size_t size;1793
uint8_t *bytes = sui_wayland_clipboard_read(w, &size);1794
if (!bytes) return SIGIL_FALSE;1795
Value result = sigil_make_bytevector(vm, size);1796
if (!sigil_is_undefined(result) && size) memcpy(sigil_bytevector_data(result), bytes, size);1797
free(bytes);1798
return result;1799
}1801
static const char *locale_name(void)1802
{1803
const char *names[] = { "LC_ALL", "LC_CTYPE", "LANG" };1804
for (int i = 0; i < 3; i++) {1805
const char *v = getenv(names[i]);1806
if (v && *v) return v;1807
}1808
return "C";1809
}1811
/* A controller may configure asynchronously, after any number of sync replies.1812
* Keep reading the default queue until configure/close or a monotonic deadline.1813
* Every successful prepare_read is paired with read_events or cancel_read. */1814
static int wait_initial_configure(Wl *w, int timeout_ms)1815
{1816
int64_t deadline = now_ms() + timeout_ms;1817
while ((!w->configured || (w->decoration && !w->decoration_mode)) && !w->closed) {1818
if (wl_display_dispatch_pending(w->display) < 0) return -1;1819
if ((w->configured && (!w->decoration || w->decoration_mode)) || w->closed) break;1820
if (now_ms() >= deadline) return 0;1821
while (wl_display_prepare_read(w->display) != 0) {1822
if (wl_display_dispatch_pending(w->display) < 0) return -1;1823
if ((w->configured && (!w->decoration || w->decoration_mode)) || w->closed) return 1;1824
if (now_ms() >= deadline) return 0;1825
}1826
int flushed = wl_display_flush(w->display);1827
if (flushed < 0 && errno != EAGAIN) {1828
wl_display_cancel_read(w->display);1829
return -1;1830
}1831
int64_t remaining = deadline - now_ms();1832
struct pollfd fd = { wl_display_get_fd(w->display),1833
POLLIN | (flushed < 0 ? POLLOUT : 0), 0 };1834
int ready = poll(&fd, 1, remaining > 0 ? (int)remaining : 0);1835
int poll_error = errno;1836
if (ready > 0 && (fd.revents & POLLIN)) {1837
if (wl_display_read_events(w->display) < 0) return -1;1838
} else {1839
wl_display_cancel_read(w->display);1840
}1841
if (ready < 0 && poll_error != EINTR) return -1;1842
if (ready > 0 && (fd.revents & (POLLERR | POLLHUP | POLLNVAL))1843
&& !(fd.revents & POLLIN)) return -1;1844
}1845
return 1;1846
}1848
/* (%wl-open display role title app-id output width height) => surface1849
* display: a socket name or #f for $WAYLAND_DISPLAY. role: 0 toplevel,1850
* 1 fullscreen (layer-shell when offered, else a fullscreen toplevel).1851
* output: a wl_output name or #f. width, height: the logical size wanted1852
* for a toplevel; a fullscreen surface takes the output's size. */1853
static Value open_surface(SigilVM *vm, int argc, Value *args, int server_decorations)1854
{1855
char display[256], title[512], app_id[256], output[64], err[400];1856
int64_t role, width, height;1857
if (!str_arg(vm, args[0], display, sizeof display, 1, "wayland-open") ||1858
!int_arg(vm, args[1], &role, "wayland-open") ||1859
!str_arg(vm, args[2], title, sizeof title, 0, "wayland-open") ||1860
!str_arg(vm, args[3], app_id, sizeof app_id, 0, "wayland-open") ||1861
!str_arg(vm, args[4], output, sizeof output, 1, "wayland-open") ||1862
!int_arg(vm, args[5], &width, "wayland-open") || !int_arg(vm, args[6], &height, "wayland-open"))1863
return DONE;1864
if ((role != ROLE_TOPLEVEL && role != ROLE_FULLSCREEN) || (server_decorations && role != ROLE_TOPLEVEL)) {1865
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-open: unknown role");1866
return DONE;1867
}1868
if (width < 1 || height < 1 || width > 16384 || height > 16384) {1869
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-open: size %lldx%lld is out of range",1870
(long long)width, (long long)height);1871
return DONE;1872
}1873
if (!load_libraries(err, sizeof err)) {1874
sigil__vm_error(vm, SIGIL_ERR_IO, "wayland-open: %s", err);1875
return DONE;1876
}1877
Wl *w = calloc(1, sizeof *w);1878
if (!w) return SIGIL_FALSE;1879
w->references = 1;1880
w->server_decorations = server_decorations;1881
w->service_thread = pthread_self();1882
w->alpha = !sigil_is_false(args[7]);1883
w->scale = 1;1884
w->repeat_rate = 25;1885
w->repeat_delay = 600;1886
w->width = (int)width;1887
w->height = (int)height;1888
const char *problem = NULL;1890
w->xkb = xkb_context_new(XKB_CONTEXT_NO_FLAGS);1891
if (!w->xkb) { problem = "cannot create an xkbcommon context"; goto fail; }1892
if (have_compose) {1893
w->compose_table = xkb_compose_table_new_from_locale(w->xkb, locale_name(), XKB_COMPOSE_COMPILE_NO_FLAGS);1894
if (w->compose_table) w->compose = xkb_compose_state_new(w->compose_table, XKB_COMPOSE_STATE_NO_FLAGS);1895
}1896
w->display = wl_display_connect(display[0] ? display : NULL);1897
if (!w->display) {1898
snprintf(err, sizeof err, "cannot connect to the compositor at %s: %s",1899
display[0] ? display : (getenv("WAYLAND_DISPLAY") ? getenv("WAYLAND_DISPLAY") : "wayland-0"),1900
strerror(errno));1901
problem = err;1902
goto fail;1903
}1904
w->registry = wl_display_get_registry(w->display);1905
wl_registry_add_listener(w->registry, ®istry_listener, w);1906
/* The first roundtrip lists the globals; the second gets the outputs'1907
* names and scales and the seat's capabilities. */1908
if (wl_display_roundtrip(w->display) < 0 || wl_display_roundtrip(w->display) < 0) {1909
problem = "the compositor connection failed";1910
goto fail;1911
}1912
if (!w->compositor) { problem = "the compositor offers no wl_compositor version 4 or later"; goto fail; }1913
if (!w->shm) { problem = "the compositor offers no wl_shm"; goto fail; }1914
if (w->data_manager && w->seat) {1915
w->data_device = wl_data_device_manager_get_data_device(w->data_manager, w->seat);1916
wl_data_device_add_listener(w->data_device, &device_listener, w);1917
}1918
if (!w->wm_base && !(role == ROLE_FULLSCREEN && w->layer_shell)) {1919
problem = "the compositor offers no xdg_wm_base";1920
goto fail;1921
}1923
Output *target = NULL;1924
if (output[0]) {1925
char names[200] = "";1926
for (int i = 0; i < MAX_OUTPUTS; i++) {1927
Output *o = &w->outputs[i];1928
if (!o->output) continue;1929
if (!strcmp(o->name, output)) target = o;1930
const char *shown = o->name[0] ? o->name : "(unnamed)";1931
if (strlen(names) + strlen(shown) + 3 < sizeof names) {1932
if (names[0]) strcat(names, ", ");1933
strcat(names, shown);1934
}1935
}1936
if (!target) {1937
snprintf(err, sizeof err, "no output named %s (outputs: %s)", output, names[0] ? names : "none");1938
problem = err;1939
goto fail;1940
}1941
}1942
/* First guess at the scale, so the first frame is usually right: the1943
* target output's, else the largest. The surface's enter events or the1944
* compositor's preferred scale correct it after the first frame. */1945
if (target) w->scale = target->scale;1946
else1947
for (int i = 0; i < MAX_OUTPUTS; i++)1948
if (w->outputs[i].output && w->outputs[i].scale > w->scale) w->scale = w->outputs[i].scale;1950
w->surface = wl_compositor_create_surface(w->compositor);1951
wl_surface_add_listener(w->surface, &surface_listener, w);1952
if (role == ROLE_FULLSCREEN && w->layer_shell) {1953
w->role = HAS_LAYER;1954
w->layer = zwlr_layer_shell_v1_get_layer_surface(w->layer_shell, w->surface,1955
target ? target->output : NULL,1956
ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, app_id);1957
zwlr_layer_surface_v1_add_listener(w->layer, &layer_listener, w);1958
zwlr_layer_surface_v1_set_anchor(w->layer,1959
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |1960
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);1961
zwlr_layer_surface_v1_set_size(w->layer, 0, 0);1962
zwlr_layer_surface_v1_set_exclusive_zone(w->layer, -1);1963
zwlr_layer_surface_v1_set_keyboard_interactivity(w->layer,1964
ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE);1965
} else {1966
w->role = role == ROLE_FULLSCREEN ? HAS_XDG_FULLSCREEN : HAS_TOPLEVEL;1967
w->xdg_surface = xdg_wm_base_get_xdg_surface(w->wm_base, w->surface);1968
xdg_surface_add_listener(w->xdg_surface, &xdg_surface_listener, w);1969
w->toplevel = xdg_surface_get_toplevel(w->xdg_surface);1970
xdg_toplevel_add_listener(w->toplevel, &toplevel_listener, w);1971
xdg_toplevel_set_title(w->toplevel, title);1972
memcpy(w->title, title, strlen(title) + 1);1973
xdg_toplevel_set_app_id(w->toplevel, app_id);1974
if (w->decoration_manager) {1975
w->decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(w->decoration_manager, w->toplevel);1976
zxdg_toplevel_decoration_v1_add_listener(w->decoration, &decoration_listener, w);1977
zxdg_toplevel_decoration_v1_set_mode(w->decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);1978
}1979
if (role == ROLE_FULLSCREEN)1980
xdg_toplevel_set_fullscreen(w->toplevel, target ? target->output : NULL);1981
}1982
w->initial_commit_ms = now_ms();1983
commit_surface(w);1984
if (wait_initial_configure(w, 5000) < 0) {1985
trace_initial_configure(w, "connection-failed");1986
problem = "the compositor connection failed";1987
goto fail;1988
}1989
/* Close wins even if configure followed it in the same dispatch batch. */1990
if (w->closed || !w->configured || (w->decoration && !w->decoration_mode)) {1991
trace_initial_configure(w, w->closed ? "closed" : "timeout");1992
problem = w->closed ? "the compositor closed the surface before showing it"1993
: "the compositor did not configure the surface within five seconds";1994
goto fail;1995
}1996
/* What the shell configured, not what the program asked for, is the1997
* size the program starts with: no resize event for it. */1998
damage_clear(&w->pending);1999
w->pending.all = 1;2000
Value handle = sigil_make_foreign(vm, wl_tag, w, wl_finalize, sizeof *w);2001
if (sigil_is_undefined(handle)) { wl_teardown(w); free(w); }2002
return handle;2003
fail:2004
if (problem != err) snprintf(err, sizeof err, "%s", problem ? problem : "failed");2005
wl_teardown(w);2006
free(w);2007
sigil__vm_error(vm, SIGIL_ERR_IO, "wayland-open: %s", err);2008
return DONE;2009
}2011
static Value native_wl_open(SigilVM *vm, int argc, Value *args)2012
{2013
return open_surface(vm, argc, args, 0);2014
}2016
static Value native_wl_open_server(SigilVM *vm, int argc, Value *args)2017
{2018
return open_surface(vm, argc, args, 1);2019
}2021
static Value native_wl_decoration_mode(SigilVM *vm, int argc, Value *args)2022
{2023
Wl *w = as_wl(vm, args[0], "wayland-decoration-mode");2024
return w ? sigil_fixnum(w->decoration_mode) : DONE;2025
}2027
/* (%wl-close! s) */2028
static Value native_wl_close(SigilVM *vm, int argc, Value *args)2029
{2030
if (!sigil_is_foreign(args[0]) || sigil_foreign_type(args[0]) != wl_tag) {2031
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-close!: not a Wayland surface");2032
return DONE;2033
}2034
wl_teardown(sigil_foreign_data(args[0]));2035
return DONE;2036
}2038
/* ------------------------------------------------------------ dispatch */2040
static void repeat_due(Wl *w, int64_t now)2041
{2042
if (!w->held_code || !w->keyboard_focus || w->repeat_rate <= 0) return;2043
int step = 1000 / w->repeat_rate;2044
if (step < 1) step = 1;2045
/* At most a few repeats per dispatch, so a stalled program does not2046
* come back to a burst. */2047
for (int i = 0; i < 4 && now >= w->repeat_at && w->held_code; i++) {2048
key_event(w, w->held_code, KEY_REPEAT);2049
w->repeat_at += step;2050
}2051
if (now >= w->repeat_at) w->repeat_at = now + step;2052
}2054
/* Read and dispatch Wayland events for up to timeout_ms (negative: no2055
* limit), returning as soon as there is an event for the program or a2056
* deferred present can go. Returns the number of queued events, or -1 when2057
* the connection is gone. */2058
static int dispatch_body(Wl *w, Wl *owner, int64_t timeout_ms)2059
{2060
int64_t start = now_ms();2061
for (;;) {2062
if (w->dead || !w->display) return -1;2063
int64_t now = now_ms();2064
window_service_tick(owner);2065
Wl *clipboard_owner = owner;2066
if (dmabuf_imports_advance_fences(clipboard_owner)) w->wake = 1;2067
clipboard_drain(clipboard_owner);2068
/* Read compositor input before synthesizing a due repeat. A release2069
* may already be waiting on the connection after the caller paused. */2070
if (w->closed || owner->close_pending || (w->configured && (w->count > 0 || w->wake))) {2071
w->wake = 0;2072
return w->count;2073
}2074
int64_t wait = timeout_ms < 0 ? -1 : timeout_ms - (now - start);2075
if (timeout_ms >= 0 && wait < 0) wait = 0;2076
wait = sui_fullscreen_wait(&owner->fullscreen_request, now, wait);2077
if (w->held_code && w->keyboard_focus) {2078
int64_t r = w->repeat_at - now;2079
if (r < 0) r = 0;2080
if (wait < 0 || r < wait) wait = r;2081
}2082
while (wl_display_prepare_read(w->display) != 0) {2083
if (dispatch_pending(w) < 0) { fail(w, "dispatch"); return -1; }2084
if (w->dead || !w->display) return -1;2085
if (owner->close_pending) return w->count;2086
if (!w->configured && timeout_ms >= 0 && now_ms() - start >= timeout_ms) return 0;2087
}2088
int flushed = wl_display_flush(w->display);2089
if (flushed < 0 && errno != EAGAIN) {2090
wl_display_cancel_read(w->display);2091
fail(w, "the compositor connection failed");2092
return -1;2093
}2094
if (w->closed || owner->close_pending || (w->configured && (w->count > 0 || w->wake))) {2095
/* A pending dispatch queued something: do not block. */2096
wl_display_cancel_read(w->display);2097
continue;2098
}2099
struct pollfd fds[1 + CLIPBOARD_WRITES + DMABUF_IMPORT_LIMIT] = {2100
{ wl_display_get_fd(w->display), POLLIN | (flushed < 0 ? POLLOUT : 0), 0 }2101
};2102
nfds_t n_fds = 1;2103
for (int i = 0; i < CLIPBOARD_WRITES; i++) {2104
ClipboardWrite *transfer = &clipboard_owner->writes[i];2105
if (!transfer->bytes) continue;2106
fds[n_fds++] = (struct pollfd){ transfer->fd, POLLOUT, 0 };2107
int64_t remaining = transfer->deadline - now_ms();2108
if (remaining < 0) remaining = 0;2109
if (wait < 0 || remaining < wait) wait = remaining;2110
}2111
dmabuf_imports_wait_fences(clipboard_owner, fds, &n_fds, &wait);2112
int ready = poll(fds, n_fds, wait > 1000000 ? 1000000 : (int)wait);2113
if (ready > 0 && (fds[0].revents & POLLIN)) {2114
if (wl_display_read_events(w->display) < 0) { fail(w, "the compositor connection failed"); return -1; }2115
} else {2116
wl_display_cancel_read(w->display);2117
}2118
if (ready < 0 && errno != EINTR) { fail(w, "poll failed"); return -1; }2119
if (ready > 0 && (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) && !(fds[0].revents & POLLIN)) {2120
fail(w, "the compositor hung up");2121
return -1;2122
}2123
if (dispatch_pending(w) < 0) { fail(w, "the compositor connection failed"); return -1; }2124
if (w->dead || !w->display) return -1;2125
if (owner->close_pending) return w->count;2126
if (dmabuf_imports_advance_fences(clipboard_owner)) w->wake = 1;2127
clipboard_drain(clipboard_owner);2128
repeat_due(w, now_ms());2129
window_service_tick(owner);2130
if (w->closed || (w->configured && (w->count > 0 || w->wake))) {2131
w->wake = 0;2132
return w->count;2133
}2134
if (timeout_ms >= 0 && now_ms() - start >= timeout_ms) return 0;2135
}2136
}2138
static int dispatch(Wl *w, int64_t timeout_ms)2139
{2140
if (!w || !w->display) return -1;2141
Wl *owner = w->parent ? w->parent : w;2142
w->references++;2143
owner->references++;2144
owner->connection_depth++;2145
int result = dispatch_body(w, owner, timeout_ms);2146
owner->connection_depth--;2147
if (!owner->connection_depth && owner->close_pending) {2148
owner->close_pending = 0;2149
wl_teardown(owner);2150
}2151
if (w->torn_down) result = 0;2152
sui_wayland_unref(w);2153
sui_wayland_unref(owner);2154
return result;2155
}2157
/* (%wl-dispatch s timeout-ms) => event count, or #f once the connection is gone */2158
static Value native_wl_dispatch(SigilVM *vm, int argc, Value *args)2159
{2160
int64_t timeout;2161
Wl *w = as_wl(vm, args[0], "wayland-dispatch");2162
if (!w || !int_arg(vm, args[1], &timeout, "wayland-dispatch")) return DONE;2163
int n = dispatch(w, timeout);2164
return n < 0 ? SIGIL_FALSE : sigil_fixnum(n);2165
}2167
/* (%wl-next-event! s vec) => type, filling vec (8 slots) with2168
* type and fields; 0 when the queue is empty. The event's text stays2169
* available to %wl-event-text until the next call. */2170
static Value native_wl_next_event(SigilVM *vm, int argc, Value *args)2171
{2172
Wl *w = as_wl(vm, args[0], "wayland-next-event!");2173
if (!w) return DONE;2174
if (!sigil_is_vector(args[1]) || sigil_vector_length(args[1]) < 8) {2175
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-next-event!: expected a vector of 8");2176
return DONE;2177
}2178
if (w->count == 0) { memset(&w->current, 0, sizeof w->current); return sigil_fixnum(0); }2179
w->current = w->queue[w->head];2180
w->head = (w->head + 1) % QUEUE;2181
w->count--;2182
sigil_vector_set(args[1], 0, sigil_fixnum(w->current.type));2183
/* Older callers still receive their original eight-slot event vector. */2184
for (size_t i = 0; i < 10 && i + 1 < sigil_vector_length(args[1]); i++)2185
sigil_vector_set(args[1], i + 1, sigil_fixnum(w->current.v[i]));2186
return sigil_fixnum(w->current.type);2187
}2189
/* (%wl-event-text s) => the text of the last event read */2190
static Value native_wl_event_text(SigilVM *vm, int argc, Value *args)2191
{2192
Wl *w = as_wl(vm, args[0], "wayland-event-text");2193
if (!w) return DONE;2194
return sigil_make_string(vm, w->current.text, strlen(w->current.text));2195
}2197
/* (%wl-keysym-name sym) => the keysym's xkbcommon name */2198
static Value native_wl_keysym_name(SigilVM *vm, int argc, Value *args)2199
{2200
int64_t sym;2201
char name[64];2202
if (!int_arg(vm, args[0], &sym, "wayland-keysym-name")) return DONE;2203
if (!loaded || xkb_keysym_get_name((xkb_keysym_t)sym, name, sizeof name) < 0) name[0] = 0;2204
return sigil_make_string(vm, name, strlen(name));2205
}2207
/* (%wl-info! s vec) fills vec (17 slots): logical width, height, scale,2208
* role obtained (0 toplevel, 1 layer, 2 fullscreen toplevel), keyboard2209
* focus, closed, mapped, buffers in use by the compositor, retired buffers2210
* not yet released, frame callback outstanding, deferred damage waiting,2211
* display fd, then the last commit's damage: its rectangle count (-1 for2212
* the whole buffer) and bounding box x, y, w, h in device pixels. */2213
static Value native_wl_info(SigilVM *vm, int argc, Value *args)2214
{2215
if (!sigil_is_foreign(args[0]) || sigil_foreign_type(args[0]) != wl_tag) {2216
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-info: not a Wayland surface");2217
return DONE;2218
}2219
Wl *w = sigil_foreign_data(args[0]); /* a closed surface too */2220
if (!sigil_is_vector(args[1]) || sigil_vector_length(args[1]) < 17) {2221
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-info: expected a vector of 17");2222
return DONE;2223
}2224
int busy = 0, retired = 0;2225
for (int i = 0; i < 2; i++) if (w->slot[i] && w->slot[i]->busy) busy++;2226
for (Buffer *b = w->retired; b; b = b->next) retired++;2227
int32_t v[17] = { w->width, w->height, w->scale, w->role, w->keyboard_focus, w->closed || w->dead,2228
w->mapped, busy, retired, w->frame != NULL, !damage_empty(&w->pending),2229
w->display ? wl_display_get_fd(w->display) : -1, w->last_n,2230
w->last_box.x, w->last_box.y, w->last_box.w, w->last_box.h };2231
for (int i = 0; i < 17; i++) sigil_vector_set(args[1], i, sigil_fixnum(v[i]));2232
return DONE;2233
}2235
/* (%wl-problem s) => why the connection was lost, or "" */2236
static Value native_wl_problem(SigilVM *vm, int argc, Value *args)2237
{2238
if (!sigil_is_foreign(args[0]) || sigil_foreign_type(args[0]) != wl_tag) {2239
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-problem: not a Wayland surface");2240
return DONE;2241
}2242
Wl *w = sigil_foreign_data(args[0]);2243
return sigil_make_string(vm, w->problem, strlen(w->problem));2244
}2246
/* (%wl-output-count s) and (%wl-output s i vec) => name, filling vec2247
* (2 slots) with the output's scale and whether the surface is on it. */2248
static Value native_wl_output_count(SigilVM *vm, int argc, Value *args)2249
{2250
Wl *w = as_wl(vm, args[0], "wayland-outputs");2251
if (!w) return DONE;2252
int n = 0;2253
for (int i = 0; i < MAX_OUTPUTS; i++) if (w->outputs[i].output) n++;2254
return sigil_fixnum(n);2255
}2256
static Value native_wl_output(SigilVM *vm, int argc, Value *args)2257
{2258
int64_t k;2259
Wl *w = as_wl(vm, args[0], "wayland-outputs");2260
if (!w || !int_arg(vm, args[1], &k, "wayland-outputs")) return DONE;2261
if (!sigil_is_vector(args[2]) || sigil_vector_length(args[2]) < 2) {2262
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-outputs: expected a vector of 2");2263
return DONE;2264
}2265
for (int i = 0, n = 0; i < MAX_OUTPUTS; i++) {2266
Output *o = &w->outputs[i];2267
if (!o->output) continue;2268
if (n++ == k) {2269
sigil_vector_set(args[2], 0, sigil_fixnum(o->scale));2270
sigil_vector_set(args[2], 1, o->entered ? SIGIL_TRUE : SIGIL_FALSE);2271
return sigil_make_string(vm, o->name, strlen(o->name));2272
}2273
}2274
return SIGIL_FALSE;2275
}2277
/* ------------------------------------------------------------ present */2279
static void copy_rect(Buffer *b, const uint8_t *src, int cw, Rect r)2280
{2281
if (r.x < 0) { r.w += r.x; r.x = 0; }2282
if (r.y < 0) { r.h += r.y; r.y = 0; }2283
if (r.x + r.w > b->width) r.w = b->width - r.x;2284
if (r.y + r.h > b->height) r.h = b->height - r.y;2285
if (r.w <= 0 || r.h <= 0) return;2286
for (int j = r.y; j < r.y + r.h; j++) {2287
size_t offset = (size_t)j * cw + r.x;2288
if (!b->w->alpha) {2289
memcpy(b->data + ((size_t)j * b->width + r.x) * 4, src + offset * 4, (size_t)r.w * 4);2290
} else {2291
uint8_t *destination = b->data + ((size_t)j * b->width + r.x) * 4;2292
for (int i = 0; i < r.w; i++) {2293
unsigned a = b->w->alpha_mask && offset + i < b->w->alpha_size2294
? b->w->alpha_mask[offset + i] : 255;2295
for (int channel = 0; channel < 3; channel++)2296
destination[4 * i + channel] = (src[4 * (offset + i) + channel] * a + 127) / 255;2297
destination[4 * i + 3] = a;2298
}2299
}2300
}2301
}2303
static void copy_damage(Buffer *b, const uint8_t *src, int cw, const Damage *d)2304
{2305
if (d->all) {2306
if (b->w->alpha) copy_rect(b, src, cw, (Rect){0, 0, b->width, b->height});2307
else memcpy(b->data, src, b->size);2308
return;2309
}2310
for (int i = 0; i < d->n; i++) copy_rect(b, src, cw, d->r[i]);2311
}2313
/* A free slot, holding a buffer of the current device size (made if2314
* needed), or -1 when the compositor holds both. */2315
static int free_slot(Wl *w)2316
{2317
int dw = device_w(w), dh = device_h(w);2318
/* Wrong-size buffers in either slot are retired first, so a resize2319
* never waits for an old buffer to come back. */2320
for (int i = 0; i < 2; i++) {2321
Buffer *b = w->slot[i];2322
if (b && (b->width != dw || b->height != dh)) { buffer_retire(w, b); w->slot[i] = NULL; }2323
}2324
int pick = -1;2325
for (int i = 0; i < 2 && pick < 0; i++) if (w->slot[i] && !w->slot[i]->busy) pick = i;2326
for (int i = 0; i < 2 && pick < 0; i++) if (!w->slot[i]) pick = i;2327
if (pick < 0) return -1;2328
if (!w->slot[pick]) {2329
w->slot[pick] = buffer_new(w, dw, dh);2330
if (!w->slot[pick]) return -2;2331
}2332
return pick;2333
}2335
/* (%wl-present! s pixels canvas-width rects) => #t when committed, #f when2336
* deferred. rects: a list of (x y w h) in device pixels; '() presents only2337
* what an earlier deferred present left. */2338
static Value present_canvas(SigilVM *vm, Value *args, int tracked)2339
{2340
int64_t cw;2341
Wl *w = as_wl(vm, args[0], "wayland-present!");2342
if (!w) return DONE;2343
if (w->dead) {2344
sigil__vm_error(vm, SIGIL_ERR_IO, "wayland-present!: %s", w->problem[0] ? w->problem : "connection lost");2345
return DONE;2346
}2347
if (!sigil_is_bytevector(args[1])) {2348
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-present!: canvas pixels must be a bytevector");2349
return DONE;2350
}2351
if (!int_arg(vm, args[2], &cw, "wayland-present!")) return DONE;2352
size_t len = sigil_bytevector_length(args[1]);2353
int dw = device_w(w), dh = device_h(w);2354
if (cw <= 0 || len % (size_t)(4 * cw) != 0 || cw != dw || (int64_t)(len / (size_t)(4 * cw)) != dh) {2355
sigil__vm_error(vm, SIGIL_ERR_TYPE,2356
"wayland-present!: canvas is %lldx%lld, the surface is %dx%d (%dx%d at scale %d)",2357
(long long)cw, cw > 0 ? (long long)(len / (size_t)(4 * cw)) : 0LL,2358
dw, dh, w->width, w->height, w->scale);2359
return DONE;2360
}2361
if (w->alpha_mask && w->alpha_size != (size_t)dw * dh) {2362
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-present!: set a new alpha mask after resizing");2363
return DONE;2364
}2365
Damage add = {0};2366
for (Value l = args[3]; !sigil_is_null(l); l = sigil_cdr(l)) {2367
if (!sigil_is_pair(l)) goto bad_rects;2368
Value r = sigil_car(l);2369
int64_t q[4];2370
for (int i = 0; i < 4; i++) {2371
if (!sigil_is_pair(r) || !sigil_is_fixnum(sigil_car(r))) goto bad_rects;2372
q[i] = sigil_as_fixnum(sigil_car(r));2373
r = sigil_cdr(r);2374
}2375
if (!sigil_is_null(r)) goto bad_rects;2376
/* Clip to the buffer. */2377
if (q[0] < 0) { q[2] += q[0]; q[0] = 0; }2378
if (q[1] < 0) { q[3] += q[1]; q[1] = 0; }2379
if (q[0] + q[2] > dw) q[2] = dw - q[0];2380
if (q[1] + q[3] > dh) q[3] = dh - q[1];2381
if (q[2] > 0 && q[3] > 0) damage_add(&add, (Rect){(int32_t)q[0], (int32_t)q[1], (int32_t)q[2], (int32_t)q[3]});2382
}2383
damage_merge(&w->pending, &add);2384
if (damage_empty(&w->pending) && !w->ack_due) return tracked ? SIGIL_FALSE : SIGIL_TRUE;2385
/* One frame in flight, except that a new size is acknowledged at once:2386
* a hidden surface gets no frame callbacks, and the compositor waits2387
* for the acknowledgement. */2388
if (w->closed || (tracked && w->parent) || (w->frame && !w->ack_due)) return SIGIL_FALSE;2389
int k = free_slot(w);2390
if (k == -2) {2391
fail(w, "cannot allocate a shared-memory buffer");2392
sigil__vm_error(vm, SIGIL_ERR_IO, "wayland-present!: %s", w->problem);2393
return DONE;2394
}2395
if (k < 0) return SIGIL_FALSE;2396
Buffer *b = w->slot[k], *other = w->slot[1 - k];2397
const uint8_t *src = sigil_bytevector_data(args[1]);2398
Damage copy = b->stale;2399
damage_merge(©, &w->pending);2400
copy_damage(b, src, (int)cw, ©);2401
damage_clear(&b->stale);2402
if (other) damage_merge(&other->stale, &w->pending);2404
if (w->ack_due) {2405
if (w->layer) zwlr_layer_surface_v1_ack_configure(w->layer, w->ack_serial);2406
else xdg_surface_ack_configure(w->xdg_surface, w->ack_serial);2407
w->ack_due = 0;2408
}2409
if (w->committed_scale != w->scale) {2410
wl_surface_set_buffer_scale(w->surface, w->scale);2411
w->committed_scale = w->scale;2412
}2413
wl_surface_attach(w->surface, b->buffer, 0, 0);2414
if (w->pending.all) {2415
wl_surface_damage_buffer(w->surface, 0, 0, dw, dh);2416
w->last_n = -1;2417
w->last_box = (Rect){0, 0, dw, dh};2418
} else {2419
int32_t x0 = INT32_MAX, y0 = INT32_MAX, x1 = 0, y1 = 0;2420
for (int i = 0; i < w->pending.n; i++) {2421
Rect r = w->pending.r[i];2422
wl_surface_damage_buffer(w->surface, r.x, r.y, r.w, r.h);2423
if (r.x < x0) x0 = r.x;2424
if (r.y < y0) y0 = r.y;2425
if (r.x + r.w > x1) x1 = r.x + r.w;2426
if (r.y + r.h > y1) y1 = r.y + r.h;2427
}2428
w->last_n = w->pending.n;2429
w->last_box = w->pending.n ? (Rect){x0, y0, x1 - x0, y1 - y0} : (Rect){0, 0, 0, 0};2430
}2431
if (w->frame) {2432
if (w->presentation_frame == w->frame) presentation_discard(w);2433
wl_callback_destroy(w->frame);2434
}2435
w->frame = wl_surface_frame(w->surface);2436
wl_callback_add_listener(w->frame, &frame_listener, w);2437
if (tracked) presentation_begin(w);2438
commit_surface(w);2439
b->busy = 1;2440
w->mapped = 1;2441
damage_clear(&w->pending);2442
if (wl_display_flush(w->display) < 0 && errno != EAGAIN) fail(w, "the compositor connection failed");2443
return tracked ? sigil_fixnum(w->presentation_serial) : SIGIL_TRUE;2444
bad_rects:2445
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-present!: damage must be a list of (x y w h) integers");2446
return DONE;2447
}2449
static Value native_wl_present(SigilVM *vm, int argc, Value *args)2450
{2451
return present_canvas(vm, args, 0);2452
}2454
static Value native_wl_present_tracked(SigilVM *vm, int argc, Value *args)2455
{2456
return present_canvas(vm, args, 1);2457
}2459
static Value native_wl_presentation_status(SigilVM *vm, int argc, Value *args)2460
{2461
Wl *w = as_wl(vm, args[0], "wayland-presentation-status");2462
int64_t serial;2463
if (!w || !int_arg(vm, args[1], &serial, "wayland-presentation-status")) return DONE;2464
if (w->closed || w->dead || serial <= 0 || serial != w->presentation_serial)2465
return sigil_fixnum(PRESENTATION_DISCARDED);2466
return sigil_fixnum(w->presentation_status);2467
}2469
/* (%wl-ready? s) => #t when deferred damage waits and could be presented now */2470
static Value native_wl_ready(SigilVM *vm, int argc, Value *args)2471
{2472
Wl *w = as_wl(vm, args[0], "wayland-ready?");2473
if (!w) return DONE;2474
if (w->dead || w->closed || (w->frame && !w->ack_due) || (damage_empty(&w->pending) && !w->ack_due))2475
return SIGIL_FALSE;2476
int dw = device_w(w), dh = device_h(w);2477
for (int i = 0; i < 2; i++) {2478
Buffer *b = w->slot[i];2479
if (!b || !b->busy || b->width != dw || b->height != dh) return SIGIL_TRUE;2480
}2481
return SIGIL_FALSE;2482
}2484
int sui_wayland_present_shm(SuiWaylandSurface *w, const uint8_t *pixels,2485
int width, int height, size_t stride,2486
SuiWaylandRelease released, SuiWaylandFrame frame,2487
void *data)2488
{2489
if (!w || !w->parent || !w->display || w->dead || w->closed || w->frame ||2490
w->parent->dead || w->parent->closed ||2491
width != device_w(w) || height != device_h(w) || stride < (size_t)width * 4 || !pixels)2492
return 0;2493
int slot = free_slot(w);2494
if (slot < 0) return 0;2495
Buffer *b = w->slot[slot];2496
for (int y = 0; y < height; y++)2497
memcpy(b->data + (size_t)y * width * 4, pixels + (size_t)y * stride, (size_t)width * 4);2498
b->released = released;2499
b->release_data = data;2500
b->busy = 1;2501
w->frame_notify = frame;2502
w->frame_data = data;2503
wl_surface_set_buffer_scale(w->surface, w->scale);2504
wl_surface_attach(w->surface, b->buffer, 0, 0);2505
wl_surface_damage_buffer(w->surface, 0, 0, width, height);2506
w->frame = wl_surface_frame(w->surface);2507
wl_callback_add_listener(w->frame, &frame_listener, w);2508
wl_surface_commit(w->surface);2509
w->mapped = 1;2510
damage_clear(&w->pending);2511
if (wl_display_flush(w->display) < 0 && errno != EAGAIN) fail(w, "the compositor connection failed");2512
return 1;2513
}2515
int sui_wayland_present_dmabuf(SuiWaylandSurface *w, SuiWaylandDmabufImport *buffer,2516
SuiWaylandDmabufRelease released, SuiWaylandFrame frame, void *data)2517
{2518
if (!w || !w->parent || !w->display || w->closed || w->dead || w->frame ||2519
!buffer || buffer->owner != w->parent || buffer->busy ||2520
sui_wayland_dmabuf_import_status(buffer) != SUI_DMABUF_IMPORT_READY ||2521
buffer->spec.width != device_w(w) || buffer->spec.height != device_h(w)) return 0;2522
/* Install a buffer listener once, on its first submission. */2523
if (!buffer->listening) {2524
if (wl_buffer_add_listener(buffer->buffer, &dmabuf_import_buffer_listener, buffer) < 0) return 0;2525
buffer->listening = 1;2526
}2527
struct wl_callback *callback = wl_surface_frame(w->surface);2528
if (!callback) return 0;2529
if (wl_callback_add_listener(callback, &frame_listener, w) < 0) {2530
wl_callback_destroy(callback);2531
return 0;2532
}2533
sui_wayland_dmabuf_import_ref(buffer);2534
buffer->busy = 1;2535
buffer->released = released;2536
buffer->release_data = data;2537
w->frame = callback;2538
w->frame_notify = frame;2539
w->frame_data = data;2540
wl_surface_set_buffer_scale(w->surface, w->scale);2541
wl_surface_attach(w->surface, buffer->buffer, 0, 0);2542
wl_surface_damage_buffer(w->surface, 0, 0, buffer->spec.width, buffer->spec.height);2543
wl_surface_commit(w->surface);2544
w->mapped = 1;2545
damage_clear(&w->pending);2546
if (wl_display_flush(w->display) < 0 && errno != EAGAIN) fail(w, "the compositor connection failed");2547
return 1;2548
}2550
void sigil__register_sigil_ui_wayland(SigilVM *vm)2551
{2552
wl_tag = sigil_intern_symbol(vm, "ui-wayland-surface", 18);2553
SigilModule *module = sigil_begin_module(vm, "(sigil ui wayland)");2554
if (!module) return;2555
REGISTER_AND_EXPORT("%wl-open", native_wl_open, SIGIL_ARITY_EXACT(8), "Open a Wayland surface");2556
REGISTER_AND_EXPORT("%wl-open-server-decorated", native_wl_open_server, SIGIL_ARITY_EXACT(8), "Open a surface requesting server decorations");2557
REGISTER_AND_EXPORT("%wl-decoration-mode", native_wl_decoration_mode, SIGIL_ARITY_EXACT(1), "Negotiated toplevel decoration mode");2558
REGISTER_AND_EXPORT("%wl-close!", native_wl_close, SIGIL_ARITY_EXACT(1), "Close a Wayland surface");2559
REGISTER_AND_EXPORT("%wl-dispatch", native_wl_dispatch, SIGIL_ARITY_EXACT(2), "Read compositor events");2560
REGISTER_AND_EXPORT("%wl-next-event!", native_wl_next_event, SIGIL_ARITY_EXACT(2), "Dequeue one event");2561
REGISTER_AND_EXPORT("%wl-event-text", native_wl_event_text, SIGIL_ARITY_EXACT(1), "Text of the last event");2562
REGISTER_AND_EXPORT("%wl-keysym-name", native_wl_keysym_name, SIGIL_ARITY_EXACT(1), "Name of a keysym");2563
REGISTER_AND_EXPORT("%wl-info!", native_wl_info, SIGIL_ARITY_EXACT(2), "Surface state");2564
REGISTER_AND_EXPORT("%wl-problem", native_wl_problem, SIGIL_ARITY_EXACT(1), "Why the connection failed");2565
REGISTER_AND_EXPORT("%wl-output-count", native_wl_output_count, SIGIL_ARITY_EXACT(1), "Number of outputs");2566
REGISTER_AND_EXPORT("%wl-output", native_wl_output, SIGIL_ARITY_EXACT(3), "One output");2567
REGISTER_AND_EXPORT("%wl-present!", native_wl_present, SIGIL_ARITY_EXACT(4), "Present damage from a canvas");2568
REGISTER_AND_EXPORT("%wl-present-tracked!", native_wl_present_tracked, SIGIL_ARITY_EXACT(4), "Track one canvas commit");2569
REGISTER_AND_EXPORT("%wl-presentation-status", native_wl_presentation_status, SIGIL_ARITY_EXACT(2), "Observe the tracked canvas commit");2570
REGISTER_AND_EXPORT("%wl-ready?", native_wl_ready, SIGIL_ARITY_EXACT(1), "Can deferred damage go now");2571
REGISTER_AND_EXPORT("%wl-child", native_wl_child, SIGIL_ARITY_EXACT(6), "Create an embedded subsurface");2572
REGISTER_AND_EXPORT("%wl-cursor-configure!", native_wl_cursor_configure, SIGIL_ARITY_EXACT(3), "Configure explicit cursor theme/size");2573
REGISTER_AND_EXPORT("%wl-cursor-name!", native_wl_cursor_name, SIGIL_ARITY_EXACT(2), "Request a named pointer cursor");2574
REGISTER_AND_EXPORT("%wl-cursor-backend", native_wl_cursor_backend, SIGIL_ARITY_EXACT(1), "Observe the current cursor backend");2575
REGISTER_AND_EXPORT("%wl-cursor-image!", native_wl_cursor_image, SIGIL_ARITY_EXACT(8), "Copy a bounded premultiplied ARGB cursor image");2576
REGISTER_AND_EXPORT("%wl-alpha!", native_wl_alpha, SIGIL_ARITY_EXACT(2), "Set an overlay alpha mask");2577
REGISTER_AND_EXPORT("%wl-keymap", native_wl_keymap, SIGIL_ARITY_EXACT(1), "Compositor keymap text");2578
REGISTER_AND_EXPORT("%wl-title!", native_wl_title, SIGIL_ARITY_EXACT(2), "Update a toplevel title without presenting");2579
REGISTER_AND_EXPORT("%wl-activate!", native_wl_activate, SIGIL_ARITY_EXACT(2), "Submit a toplevel activation token");2580
REGISTER_AND_EXPORT("%wl-fullscreen?", native_wl_fullscreen_state, SIGIL_ARITY_EXACT(1), "Configured xdg-toplevel fullscreen state");2581
REGISTER_AND_EXPORT("%wl-fullscreen!", native_wl_fullscreen, SIGIL_ARITY_EXACT(2), "Request an asynchronous fullscreen transition");2582
REGISTER_AND_EXPORT("%wl-clipboard-set!", native_wl_clipboard_set, SIGIL_ARITY_EXACT(2), "Own the text clipboard");2583
REGISTER_AND_EXPORT("%wl-clipboard-read", native_wl_clipboard_read, SIGIL_ARITY_EXACT(1), "Read clipboard bytes");2584
sigil_end_module(vm);2585
register_window_wayland_services(vm);2586
}