Commitef937ba5Recorded11 Nov 2026Repositorysigil-ui

Support themed and image cursors on Wayland connections

Changed
 NEWS.md                                |  18 +++++++++
 README.md                              |   7 ++++
 docs/wayland-cursors.md                |  90 ++++++++++++++++++++++++++++++++++++++++++
 native/wayland-bridge.h                |  20 ++++++++++
 native/wayland-cursor.c                | 332 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 native/wayland.c                       |  91 +++++++++++++++++++++++++++++++++++++++++-
 native/wayland/cursor.h                |  96 ++++++++++++++++++++++++++++++++++++++++++++
 scripts/make-cursor-fixture.sgl        |  38 ++++++++++++++++++
 scripts/test-wayland-cursor-native.sgl |  20 ++++++++++
 src/sigil/ui/wayland.sgl               |  36 +++++++++++++++++
 test/wayland/cursor-native.c           | 323 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/wayland/live.sgl                  | 137 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/wayland/run                       |   7 +++-
 13 files changed, 1212 insertions(+), 3 deletions(-)
Diff
NEWS.mdmodified
@@ -1,3 +1,21 @@
+1
# Unreleased
+2
+3
- Wayland cursors accept explicit theme/size and named or copied ARGB image
+4
requests on the owned connection. Requests retain desired state across
+5
pointer re-entry and use the current enter serial. Existing applications
+6
retain compositor-default behavior until making a cursor call. A name alone
+7
prefers its mapped compositor shape, then the default library loader when
+8
no shape is available. Explicit theme or size configuration prefers the
+9
configured library loader, then a mapped compositor shape. Unmapped names
+10
reach the loader unchanged; a miss without a mapped shape returns false.
+11
libwayland-cursor combines theme, inheritance, default and builtin fallback;
+12
builtin images cannot be distinguished from theme images. Themes use frame
+13
one. Applications select desktop preferences; explicit size without a theme
+14
reads XCURSOR_THEME, and omitted size reads XCURSOR_SIZE. Omitting both
+15
configure arguments restores compositor-shape preference. Busy copied-image
+16
buffers are never re-attached or overwritten, including unchanged
+17
configuration and re-entry.
+18
19
# sigil-ui 0.2.3
20
21
A text fallback release.
README.mdmodified
@@ -1,5 +1,12 @@
1
# sigil-ui
2
+3
Wayland windows support [named and image cursors](docs/wayland-cursors.md)
+4
through their owned connection. Named requests prefer compositor shapes;
+5
explicit theme/size configuration prefers optional library images. The loader
+6
combines theme, default and builtin fallback without identifying their origin.
+7
Applications select desktop theme preferences. Existing applications retain
+8
compositor-default behavior until making a cursor call.
+9
10
A retained-mode UI library for Sigil, with a software renderer and offscreen,
11
Linux framebuffer, DRM, Wayland and macOS AppKit surfaces. It draws rectangles,
12
bitmap text and alpha masks into XRGB8888 pixel buffers, and can save renders as
docs/wayland-cursors.mdadded
@@ -0,0 +1,90 @@
+1
# Wayland cursors
+2
+3
The Wayland window owns cursor protocol objects and the latest pointer-enter
+4
serial. Requests never dispatch or create another display connection. Desired
+5
state survives pointer leave/re-entry; updates outside pointer focus are stored
+6
for the next entry. A subsurface request affects the entire shared connection.
+7
Embedding applications must route requests from their active view and reset
+8
the cursor when the pointer moves over chrome.
+9
+10
Cursor configuration is opt-in. Until the first configure, name or image
+11
request, pointer entry keeps the existing compositor-default shape behavior;
+12
it does not load a theme or choose a client-side cursor size.
+13
+14
```scheme
+15
(wayland-cursor-configure! surface theme: "Adwaita" size: 24)
+16
(wayland-set-cursor! surface "pointer")
+17
(wayland-set-cursor! surface "text")
+18
(wayland-set-cursor! surface "none")
+19
```
+20
+21
Theme names contain at most 255 UTF-8 bytes and no NUL. Explicit logical size is
+22
1..128. An explicit size without a theme uses `XCURSOR_THEME` or default.
+23
Omitting the size or using 0 reads a valid
+24
`XCURSOR_SIZE` in 1..128, otherwise 24. Desktop preference selection belongs to the
+25
application. Naming a theme or a positive size marks the configuration explicit.
+26
Calling configure with both arguments omitted clears that preference again.
+27
Its library fallback then uses the default theme and the environment-selected
+28
size; mapped names continue to prefer the compositor.
+29
libwayland-cursor honors `XCURSOR_PATH` and its standard combined theme,
+30
inheritance, default-theme and builtin fallback. Its public API cannot identify
+31
which source supplied an image; backend `theme` includes builtin images.
+32
It is loaded optionally by soname;
+33
`SIGIL_UI_LIBWAYLAND_CURSOR` selects an explicit library path, like the existing
+34
Wayland/XKB library overrides. A window still opens when this library is absent.
+35
+36
Named requests use CSS cursor names and conventional aliases such as left_ptr,
+37
hand2 and xterm. Selection follows this order:
+38
+39
1. After explicit theme or size configuration, try the configured library
+40
loader, then the name's mapped compositor shape if the protocol is bound.
+41
2. With a name alone, prefer its mapped compositor shape. The compositor
+42
supplies the user's desktop theme. If the protocol is absent or the name
+43
has no shape mapping, try the library's default theme and standard fallbacks.
+44
3. If neither path supplies the cursor, return false. Unmapped names reach the
+45
loader unchanged and are never replaced with the compositor's default shape.
+46
+47
No separate decoder or builtin data is used. A missing optional library still
+48
permits mapped compositor shapes. Image requests keep their copied-image path,
+49
and `none` explicitly hides the cursor. On a miss,
+50
`wayland-cursor-backend` reports unavailable. Missing optional support emits no
+51
startup warning. A deferred request outside pointer focus returns true and
+52
reports unavailable until entry. Theme animation currently uses its first image.
+53
+54
Theme images reload when the output scale changes. Their physical size is
+55
requested at logical size times output scale. If an image's dimensions do not
+56
divide that scale, it is presented at buffer scale 1 to avoid a Wayland protocol
+57
error; such a theme may appear larger. Theme decoding/allocation belongs to
+58
libwayland-cursor and is separate from the copied-image bounds below.
+59
+60
`wayland-set-cursor-image!` accepts premultiplied native ARGB8888 bytes, width
+61
and height, with optional stride, hotspot-x, hotspot-y and scale. Stride defaults
+62
to width times 4. Dimensions are 1..512, stride is at most 2048 bytes, and scale
+63
is 1..8 and must divide both dimensions. The hotspot is in device pixels and
+64
must be strictly inside the image. The supplied bytevector must contain all
+65
stride times height bytes. At most 1 MiB is copied; later caller mutation is safe.
+66
Two reusable SHM buffers are retained. Busy buffers are never overwritten or
+67
re-attached, including unchanged configuration, scale updates and re-entry;
+68
if both are busy, only the latest pending pixel copy is kept and submitted
+69
after a release. Replacing desired state discards its old pending copy. Closing releases all cursor
+70
images, theme, surface and optional library ownership.
+71
+72
`wayland-cursor-backend` observes unavailable, theme, shape, image or hidden
+73
without dispatching. The corresponding native operations are declared in
+74
native/wayland-bridge.h and reject calls from the wrong connection thread.
+75
+76
`scripts/test-wayland-cursor-native.sgl` drives the native serial/ownership and
+77
validation controls. The live Wayland suite uses owned Xcursor files from
+78
`scripts/make-cursor-fixture.sgl`, captures the actual compositor cursor with
+79
grim, and checks theme/custom-image/hidden/re-entry behavior at scales 1/2.
+80
It also exercises compositor-shape fallback with the optional library absent,
+81
three custom-image updates without dispatch, and custom-image re-entry.
+82
The native gate reproduces a queued old release between unchanged
+83
configuration and replacement, and verifies that an application making no
+84
cursor call keeps the old default with the optional library available.
+85
It also controls shape-bound/unbound, explicit theme/size, unmapped-name and
+86
loader-miss branches. The live suite checks explicit images with shape support
+87
bound, inherited default-theme images without shape support, and an unmapped
+88
owned cursor with exact captured pixels.
+89
The existing test/wayland/run prepares these fixtures; custom runners supply
+90
`SIGIL_UI_TEST_CURSOR_ROOT`. Tests do not read or change an installed theme.
native/wayland-bridge.hmodified
@@ -107,6 +107,26 @@ SuiWaylandSurface *sui_wayland_child(SuiWaylandSurface *parent, int x, int y,
107
int width, int height, int overlay);
108
void sui_wayland_close(SuiWaylandSurface *surface);
109
int sui_wayland_size(SuiWaylandSurface *surface, int *width, int *height, int *scale);
+110
/* Cursor requests affect the shared connection, including requests from a
+111
* child. They copy desired state, never dispatch, and apply only with a live
+112
* pointer-enter serial. Calls must use the connection's owning thread.
+113
* A name alone prefers its mapped compositor shape, then the default loader.
+114
* An explicit theme or positive size prefers the configured loader, then a
+115
* mapped shape. NULL theme with size 0 clears that explicit preference.
+116
* For an explicit size without a theme, NULL selects XCURSOR_THEME/default;
+117
* size 0 selects valid XCURSOR_SIZE/24. Size is 1..128 logical pixels.
+118
* The loader uses frame one; builtin and theme origins are indistinguishable.
+119
* Images are premultiplied native ARGB8888, at most 512x512/1 MiB, copied now;
+120
* image scale is 1..8 and divides both dimensions; hotspots are device pixels
+121
* strictly inside the image.
+122
* False reports invalid/closed/wrong-thread or currently unavailable output.
+123
* True outside pointer focus means accepted for the next pointer entry. */
+124
int sui_wayland_cursor_configure(SuiWaylandSurface *surface, const char *theme, int size);
+125
int sui_wayland_cursor_name(SuiWaylandSurface *surface, const char *name);
+126
int sui_wayland_cursor_image(SuiWaylandSurface *surface, const uint8_t *bytes,
+127
size_t length, int width, int height, size_t stride, int hotspot_x, int hotspot_y, int scale);
+128
enum { SUI_CURSOR_UNAVAILABLE, SUI_CURSOR_THEME, SUI_CURSOR_SHAPE, SUI_CURSOR_IMAGE, SUI_CURSOR_HIDDEN };
+129
int sui_wayland_cursor_backend(SuiWaylandSurface *surface);
130
/* Requests never dispatch or claim compositor acceptance. State is 0/1 only
131
* after xdg_surface.configure; -1 means no live xdg-toplevel. */
132
int sui_wayland_set_fullscreen(SuiWaylandSurface *surface, int fullscreen);
native/wayland-cursor.cadded
@@ -0,0 +1,332 @@
+1
/* Cursor protocol objects belong to the window connection. Theme decoding
+2
* uses optional libwayland-cursor; no toolkit or display connection is added.
+3
* The vendored public ABI is Wayland 1.23.0 cursor/wayland-cursor.h (MIT). */
+4
#include "wayland/cursor.h"
+5
+6
typedef struct {
+7
Wl *owner;
+8
struct wl_buffer *buffer;
+9
void *mapped;
+10
size_t bytes;
+11
int width, height, busy;
+12
} CursorBuffer;
+13
+14
struct CursorState {
+15
void *library;
+16
struct wl_cursor_theme *(*load)(const char *, int, struct wl_shm *);
+17
void (*destroy)(struct wl_cursor_theme *);
+18
struct wl_cursor *(*get)(struct wl_cursor_theme *, const char *);
+19
struct wl_buffer *(*buffer)(struct wl_cursor_image *);
+20
struct wl_cursor_theme *theme;
+21
struct wl_surface *surface;
+22
CursorBuffer image_buffers[2];
+23
char theme_name[256], name[64];
+24
int size, theme_scale, backend, explicit_configuration;
+25
int image_width, image_height, image_x, image_y, image_scale;
+26
uint8_t *image;
+27
int image_pending, current_image;
+28
};
+29
+30
typedef struct { const char *name, *alias; uint32_t shape; } CursorName;
+31
#define CURSOR(name, alias, shape) { name, alias, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_##shape }
+32
static const CursorName cursor_names[] = {
+33
CURSOR("default", "left_ptr", DEFAULT), CURSOR("context-menu", "context-menu", CONTEXT_MENU),
+34
CURSOR("help", "question_arrow", HELP), CURSOR("pointer", "hand2", POINTER),
+35
CURSOR("progress", "left_ptr_watch", PROGRESS), CURSOR("wait", "watch", WAIT),
+36
CURSOR("cell", "plus", CELL), CURSOR("crosshair", "crosshair", CROSSHAIR),
+37
CURSOR("text", "xterm", TEXT), CURSOR("vertical-text", "vertical-text", VERTICAL_TEXT),
+38
CURSOR("alias", "dnd-link", ALIAS), CURSOR("copy", "dnd-copy", COPY),
+39
CURSOR("move", "fleur", MOVE), CURSOR("no-drop", "dnd-none", NO_DROP),
+40
CURSOR("not-allowed", "crossed_circle", NOT_ALLOWED), CURSOR("grab", "openhand", GRAB),
+41
CURSOR("grabbing", "closedhand", GRABBING), CURSOR("e-resize", "right_side", E_RESIZE),
+42
CURSOR("n-resize", "top_side", N_RESIZE), CURSOR("ne-resize", "top_right_corner", NE_RESIZE),
+43
CURSOR("nw-resize", "top_left_corner", NW_RESIZE), CURSOR("s-resize", "bottom_side", S_RESIZE),
+44
CURSOR("se-resize", "bottom_right_corner", SE_RESIZE), CURSOR("sw-resize", "bottom_left_corner", SW_RESIZE),
+45
CURSOR("w-resize", "left_side", W_RESIZE), CURSOR("ew-resize", "sb_h_double_arrow", EW_RESIZE),
+46
CURSOR("ns-resize", "sb_v_double_arrow", NS_RESIZE), CURSOR("nesw-resize", "fd_double_arrow", NESW_RESIZE),
+47
CURSOR("nwse-resize", "bd_double_arrow", NWSE_RESIZE), CURSOR("col-resize", "h_double_arrow", COL_RESIZE),
+48
CURSOR("row-resize", "v_double_arrow", ROW_RESIZE), CURSOR("all-scroll", "fleur", ALL_SCROLL),
+49
CURSOR("zoom-in", "zoom-in", ZOOM_IN), CURSOR("zoom-out", "zoom-out", ZOOM_OUT)
+50
};
+51
#undef CURSOR
+52
+53
static const CursorName *cursor_name(const char *name)
+54
{
+55
for (size_t i = 0; i < sizeof cursor_names / sizeof *cursor_names; i++)
+56
if (!strcmp(name, cursor_names[i].name) || !strcmp(name, cursor_names[i].alias)) return &cursor_names[i];
+57
return NULL;
+58
}
+59
+60
static Wl *cursor_owner(Wl *w)
+61
{
+62
if (!w || !w->display || w->dead || w->closed) return NULL;
+63
while (w->parent) w = w->parent;
+64
return !w->dead && !w->closed && pthread_equal(w->service_thread, pthread_self()) ? w : NULL;
+65
}
+66
+67
static int cursor_environment_size(void)
+68
{
+69
const char *text = getenv("XCURSOR_SIZE");
+70
if (!text || !*text) return 24;
+71
char *end;
+72
errno = 0;
+73
long size = strtol(text, &end, 10);
+74
return !errno && !*end && size > 0 && size <= 128 ? (int)size : 24;
+75
}
+76
+77
static CursorState *cursor_state(Wl *w)
+78
{
+79
if (w->cursor) return w->cursor;
+80
CursorState *cursor = calloc(1, sizeof *cursor);
+81
if (!cursor) return NULL;
+82
strcpy(cursor->theme_name, "default");
+83
strcpy(cursor->name, "default");
+84
cursor->size = cursor_environment_size();
+85
cursor->current_image = -1;
+86
for (int i = 0; i < 2; i++) cursor->image_buffers[i].owner = w;
+87
const char *library = getenv("SIGIL_UI_LIBWAYLAND_CURSOR");
+88
cursor->library = dlopen(library && *library ? library : "libwayland-cursor.so.0", RTLD_NOW | RTLD_LOCAL);
+89
if (cursor->library) {
+90
cursor->load = dlsym(cursor->library, "wl_cursor_theme_load");
+91
cursor->destroy = dlsym(cursor->library, "wl_cursor_theme_destroy");
+92
cursor->get = dlsym(cursor->library, "wl_cursor_theme_get_cursor");
+93
cursor->buffer = dlsym(cursor->library, "wl_cursor_image_get_buffer");
+94
if (!cursor->load || !cursor->destroy || !cursor->get || !cursor->buffer) {
+95
dlclose(cursor->library);
+96
cursor->library = NULL;
+97
}
+98
}
+99
w->cursor = cursor;
+100
return cursor;
+101
}
+102
+103
static int cursor_show(Wl *w, struct wl_buffer *buffer, int width, int height, int x, int y, int scale)
+104
{
+105
CursorState *cursor = w->cursor;
+106
/* Theme sizes need not divide the output scale (including builtins).
+107
* Never submit a buffer that would raise invalid_size at commit. */
+108
if (width % scale || height % scale) scale = 1;
+109
if (!cursor->surface) cursor->surface = wl_compositor_create_surface(w->compositor);
+110
if (!cursor->surface) return 0;
+111
wl_pointer_set_cursor(w->pointer, w->pointer_serial, cursor->surface, x / scale, y / scale);
+112
wl_surface_set_buffer_scale(cursor->surface, scale);
+113
wl_surface_attach(cursor->surface, buffer, 0, 0);
+114
wl_surface_damage(cursor->surface, 0, 0, (width + scale - 1) / scale, (height + scale - 1) / scale);
+115
wl_surface_commit(cursor->surface);
+116
return 1;
+117
}
+118
+119
static void cursor_buffer_close(CursorBuffer *buffer)
+120
{
+121
if (buffer->buffer) wl_buffer_destroy(buffer->buffer);
+122
if (buffer->mapped) munmap(buffer->mapped, buffer->bytes);
+123
buffer->buffer = NULL;
+124
buffer->mapped = NULL;
+125
buffer->bytes = 0;
+126
buffer->busy = 0;
+127
}
+128
+129
static void cursor_buffer_released(void *data, struct wl_buffer *proxy)
+130
{
+131
(void)proxy;
+132
CursorBuffer *buffer = data;
+133
buffer->busy = 0;
+134
Wl *w = buffer->owner;
+135
if (w->cursor && w->cursor->image_pending) cursor_apply(w);
+136
}
+137
static const struct wl_buffer_listener cursor_buffer_listener = {cursor_buffer_released};
+138
+139
static int cursor_buffer_open(Wl *w, CursorBuffer *buffer, int width, int height)
+140
{
+141
size_t size = (size_t)width * height * 4;
+142
int fd = (int)syscall(SYS_memfd_create, "sigil-ui-cursor", 1u);
+143
if (fd < 0) return 0;
+144
if (ftruncate(fd, (off_t)size) < 0) { close(fd); return 0; }
+145
void *mapped = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+146
if (mapped == MAP_FAILED) { close(fd); return 0; }
+147
struct wl_shm_pool *pool = wl_shm_create_pool(w->shm, fd, (int32_t)size);
+148
if (pool) {
+149
buffer->buffer = wl_shm_pool_create_buffer(pool, 0, width, height, width * 4, WL_SHM_FORMAT_ARGB8888);
+150
wl_shm_pool_destroy(pool);
+151
}
+152
close(fd);
+153
if (!buffer->buffer) { munmap(mapped, size); return 0; }
+154
buffer->mapped = mapped;
+155
buffer->bytes = size;
+156
buffer->width = width;
+157
buffer->height = height;
+158
if (wl_buffer_add_listener(buffer->buffer, &cursor_buffer_listener, buffer) < 0) {
+159
cursor_buffer_close(buffer);
+160
return 0;
+161
}
+162
return 1;
+163
}
+164
+165
/* At most two protocol buffers and one latest pending copy. Never rewrite a
+166
* busy buffer or allocate another slot while the compositor holds both. */
+167
static int cursor_image_buffer(Wl *w)
+168
{
+169
CursorState *cursor = w->cursor;
+170
if (!cursor->image_pending && cursor->current_image >= 0 &&
+171
!cursor->image_buffers[cursor->current_image].busy) return cursor->current_image;
+172
/* An older release may already be queued. A busy slot gets exactly one
+173
* submission until that release is dispatched, even for unchanged data. */
+174
cursor->image_pending = 1;
+175
for (int i = 0; i < 2; i++) {
+176
CursorBuffer *buffer = &cursor->image_buffers[i];
+177
if (buffer->busy) continue;
+178
if (buffer->buffer && (buffer->width != cursor->image_width || buffer->height != cursor->image_height))
+179
cursor_buffer_close(buffer);
+180
if (!buffer->buffer && !cursor_buffer_open(w, buffer, cursor->image_width, cursor->image_height)) return -2;
+181
memcpy(buffer->mapped, cursor->image, buffer->bytes);
+182
cursor->image_pending = 0;
+183
cursor->current_image = i;
+184
return i;
+185
}
+186
return -1;
+187
}
+188
+189
static int cursor_apply(Wl *w)
+190
{
+191
if (!(w = cursor_owner(w))) return 0;
+192
CursorState *cursor = w->cursor;
+193
if (!cursor) return 1;
+194
cursor->backend = SUI_CURSOR_UNAVAILABLE;
+195
/* Serial zero is valid after the protocol counter wraps. Focus is the
+196
* validity bit; never infer it from the serial's numeric value. */
+197
if (!w->pointer_inside || !w->pointer) return 1;
+198
if (!strcmp(cursor->name, "none")) {
+199
wl_pointer_set_cursor(w->pointer, w->pointer_serial, NULL, 0, 0);
+200
cursor->backend = SUI_CURSOR_HIDDEN;
+201
} else if (cursor->image) {
+202
int slot = cursor_image_buffer(w);
+203
if (slot == -1) {
+204
cursor->backend = cursor->current_image >= 0 ? SUI_CURSOR_IMAGE : SUI_CURSOR_UNAVAILABLE;
+205
return 1;
+206
}
+207
if (slot < 0 || !cursor_show(w, cursor->image_buffers[slot].buffer,
+208
cursor->image_width, cursor->image_height, cursor->image_x, cursor->image_y, cursor->image_scale)) return 0;
+209
cursor->image_buffers[slot].busy = 1;
+210
cursor->backend = SUI_CURSOR_IMAGE;
+211
} else {
+212
const CursorName *name = cursor_name(cursor->name);
+213
/* A name alone lets the compositor apply desktop preferences. Only
+214
* explicit theme/size configuration gives library images priority. */
+215
if (cursor->explicit_configuration || !name || !w->cursor_device) {
+216
int scale = w->scale > 0 && w->scale <= 8 ? w->scale : 1;
+217
if (cursor->theme && cursor->theme_scale != scale) {
+218
cursor->destroy(cursor->theme);
+219
cursor->theme = NULL;
+220
}
+221
if (!cursor->theme && cursor->library && w->shm) {
+222
cursor->theme = cursor->load(cursor->explicit_configuration ? cursor->theme_name : "default",
+223
cursor->size * scale, w->shm);
+224
cursor->theme_scale = scale;
+225
}
+226
if (cursor->theme) {
+227
struct wl_cursor *image = cursor->get(cursor->theme, name ? name->name : cursor->name);
+228
if (!image && name) image = cursor->get(cursor->theme, name->alias);
+229
if (image && image->image_count && image->images && image->images[0]) {
+230
struct wl_cursor_image *first = image->images[0];
+231
if (first->width && first->height && first->width <= 1024 && first->height <= 1024 &&
+232
first->hotspot_x < first->width && first->hotspot_y < first->height) {
+233
struct wl_buffer *buffer = cursor->buffer(first);
+234
if (buffer && cursor_show(w, buffer, first->width, first->height,
+235
first->hotspot_x, first->hotspot_y, scale))
+236
cursor->backend = SUI_CURSOR_THEME;
+237
}
+238
}
+239
}
+240
}
+241
if (cursor->backend == SUI_CURSOR_UNAVAILABLE && name && w->cursor_device) {
+242
wp_cursor_shape_device_v1_set_shape(w->cursor_device, w->pointer_serial, name->shape);
+243
cursor->backend = SUI_CURSOR_SHAPE;
+244
}
+245
if (cursor->backend == SUI_CURSOR_UNAVAILABLE) return 0;
+246
}
+247
return wl_display_flush(w->display) >= 0 || errno == EAGAIN;
+248
}
+249
+250
static void cursor_drop_image(CursorState *cursor)
+251
{
+252
free(cursor->image);
+253
cursor->image = NULL;
+254
cursor->image_pending = 0;
+255
}
+256
+257
int sui_wayland_cursor_configure(Wl *w, const char *theme, int size)
+258
{
+259
if (!(w = cursor_owner(w)) || size < 0 || size > 128 ||
+260
(theme && (!*theme || strlen(theme) >= 256))) return 0;
+261
CursorState *cursor = cursor_state(w);
+262
if (!cursor) return 0;
+263
int explicit_configuration = theme != NULL || size > 0;
+264
if (!theme) {
+265
theme = getenv("XCURSOR_THEME");
+266
if (!theme || !*theme || strlen(theme) >= sizeof cursor->theme_name) theme = "default";
+267
}
+268
if (!size) size = cursor_environment_size();
+269
if (size != cursor->size || strcmp(theme, cursor->theme_name) ||
+270
explicit_configuration != cursor->explicit_configuration) {
+271
if (cursor->theme) cursor->destroy(cursor->theme);
+272
cursor->theme = NULL;
+273
cursor->size = size;
+274
if (strcmp(theme, cursor->theme_name)) strcpy(cursor->theme_name, theme);
+275
}
+276
cursor->explicit_configuration = explicit_configuration;
+277
return cursor_apply(w);
+278
}
+279
+280
int sui_wayland_cursor_name(Wl *w, const char *name)
+281
{
+282
if (!(w = cursor_owner(w)) || !name || !*name || strlen(name) >= 64) return 0;
+283
CursorState *cursor = cursor_state(w);
+284
if (!cursor) return 0;
+285
cursor_drop_image(cursor);
+286
cursor->current_image = -1;
+287
strcpy(cursor->name, name);
+288
return cursor_apply(w);
+289
}
+290
+291
int sui_wayland_cursor_image(Wl *w, const uint8_t *bytes, size_t length,
+292
int width, int height, size_t stride, int x, int y, int scale)
+293
{
+294
if (!(w = cursor_owner(w)) || !bytes || width < 1 || height < 1 || width > 512 || height > 512 ||
+295
stride < (size_t)width * 4 || stride > 2048 || stride * height > length ||
+296
x < 0 || x >= width || y < 0 || y >= height || scale < 1 || scale > 8 ||
+297
width % scale || height % scale) return 0;
+298
size_t row = (size_t)width * 4;
+299
uint8_t *copy = malloc(row * height);
+300
if (!copy) return 0;
+301
for (int y = 0; y < height; y++) memcpy(copy + row * y, bytes + stride * y, row);
+302
CursorState *cursor = cursor_state(w);
+303
if (!cursor) { free(copy); return 0; }
+304
cursor_drop_image(cursor);
+305
strcpy(cursor->name, "image");
+306
cursor->image = copy;
+307
cursor->image_pending = 1;
+308
cursor->image_width = width;
+309
cursor->image_height = height;
+310
cursor->image_x = x;
+311
cursor->image_y = y;
+312
cursor->image_scale = scale;
+313
return cursor_apply(w);
+314
}
+315
+316
int sui_wayland_cursor_backend(Wl *w)
+317
{
+318
return (w = cursor_owner(w)) && w->cursor ? w->cursor->backend : SUI_CURSOR_UNAVAILABLE;
+319
}
+320
+321
static void cursor_close(Wl *w)
+322
{
+323
CursorState *cursor = w->cursor;
+324
if (!cursor) return;
+325
cursor_drop_image(cursor);
+326
for (int i = 0; i < 2; i++) cursor_buffer_close(&cursor->image_buffers[i]);
+327
if (cursor->theme) cursor->destroy(cursor->theme);
+328
if (cursor->surface) wl_surface_destroy(cursor->surface);
+329
if (cursor->library) dlclose(cursor->library);
+330
free(cursor);
+331
w->cursor = NULL;
+332
}
native/wayland.cmodified
@@ -255,6 +255,7 @@ typedef struct {
255
int64_t deadline;
256
} ClipboardWrite;
257
+258
typedef struct CursorState CursorState;
259
typedef struct Wl {
260
unsigned references;
261
int closing, torn_down;
@@ -298,6 +299,8 @@ typedef struct Wl {
299
struct zwlr_layer_shell_v1 *layer_shell;
300
struct wp_cursor_shape_manager_v1 *cursor_manager;
301
struct wp_cursor_shape_device_v1 *cursor_device;
+302
CursorState *cursor;
+303
uint32_t pointer_serial;
304
struct wl_surface *surface;
305
struct xdg_surface *xdg_surface;
306
struct xdg_toplevel *toplevel;
@@ -356,8 +359,11 @@ typedef struct Wl {
359
} Wl;
360
361
static int64_t now_ms(void);
+362
static int cursor_apply(Wl *w);
+363
static void cursor_close(Wl *w);
364
#include "wayland-dmabuf-feedback.c"
365
#include "wayland-dmabuf-import.c"
+366
#include "wayland-cursor.c"
367
368
static Value wl_tag = SIGIL_UNDEFINED;
369
static int dispatch(Wl *w, int64_t timeout_ms);
@@ -547,6 +553,7 @@ static void update_scale(Wl *w)
553
child->scale = s;
554
report_resize(child);
555
}
+556
if (w->cursor) cursor_apply(w);
557
}
558
}
559
@@ -895,9 +902,11 @@ static void ptr_enter(void *data, struct wl_pointer *p, uint32_t serial, struct
902
{
903
Wl *w = data;
904
w->pointer_inside = 1;
+905
w->pointer_serial = serial;
906
w->px = wl_fixed_to_double(x);
907
w->py = wl_fixed_to_double(y);
−900
if (w->cursor_device)
+908
if (w->cursor) cursor_apply(w);
+909
else if (w->cursor_device)
910
wp_cursor_shape_device_v1_set_shape(w->cursor_device, serial, WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
911
Event *e = push(w, UI_POINTER_ENTER);
912
if (e) { e->v[0] = floor_int(w->px); e->v[1] = floor_int(w->py); }
@@ -906,6 +915,8 @@ static void ptr_leave(void *data, struct wl_pointer *p, uint32_t serial, struct
915
{
916
Wl *w = data;
917
w->pointer_inside = 0;
+918
w->pointer_serial = 0;
+919
if (w->cursor) w->cursor->backend = SUI_CURSOR_UNAVAILABLE;
920
push(w, UI_POINTER_LEAVE);
921
}
922
static void ptr_motion(void *data, struct wl_pointer *p, uint32_t time, wl_fixed_t x, wl_fixed_t y)
@@ -1034,6 +1045,8 @@ static void seat_caps(void *data, struct wl_seat *seat, uint32_t caps)
1045
if (wl_pointer_get_version(w->pointer) >= 3) wl_pointer_release(w->pointer);
1046
else wl_pointer_destroy(w->pointer);
1047
w->pointer = NULL;
+1048
w->pointer_serial = 0;
+1049
if (w->cursor) w->cursor->backend = SUI_CURSOR_UNAVAILABLE;
1050
if (w->pointer_inside) {
1051
w->pointer_inside = 0;
1052
push(w, UI_POINTER_LEAVE);
@@ -1088,8 +1101,11 @@ static void global(void *data, struct wl_registry *r, uint32_t id, const char *i
1101
xdg_wm_base_add_listener(w->wm_base, &wm_listener, w);
1102
} else if (!strcmp(iface, zwlr_layer_shell_v1_interface.name) && !w->layer_shell)
1103
w->layer_shell = wl_registry_bind(r, id, &zwlr_layer_shell_v1_interface, min_u(version, 4));
−1091
else if (!strcmp(iface, wp_cursor_shape_manager_v1_interface.name) && !w->cursor_manager)
+1104
else if (!strcmp(iface, wp_cursor_shape_manager_v1_interface.name) && !w->cursor_manager) {
1105
w->cursor_manager = wl_registry_bind(r, id, &wp_cursor_shape_manager_v1_interface, 1);
+1106
if (w->pointer)
+1107
w->cursor_device = wp_cursor_shape_manager_v1_get_pointer(w->cursor_manager, w->pointer);
+1108
}
1109
else if (!strcmp(iface, wl_output_interface.name)) {
1110
for (int i = 0; i < MAX_OUTPUTS; i++) {
1111
Output *o = &w->outputs[i];
@@ -1192,6 +1208,7 @@ static void wl_teardown(Wl *w)
1208
else wl_data_device_destroy(w->data_device);
1209
}
1210
if (w->data_manager) wl_data_device_manager_destroy(w->data_manager);
+1211
cursor_close(w);
1212
if (w->cursor_device) wp_cursor_shape_device_v1_destroy(w->cursor_device);
1213
if (w->keyboard) wl_keyboard_destroy(w->keyboard);
1214
if (w->pointer) wl_pointer_destroy(w->pointer);
@@ -1417,6 +1434,72 @@ static int int_arg(SigilVM *vm, Value v, int64_t *out, const char *who)
1434
return 1;
1435
}
1436
+1437
static int cursor_string_arg(SigilVM *vm, Value value, char *buffer, size_t size,
+1438
int allow_false, const char *who)
+1439
{
+1440
if (!str_arg(vm, value, buffer, size, allow_false, who)) return 0;
+1441
if (!sigil_is_false(value) &&
+1442
(strlen(buffer) != ((SigilString *)sigil_as_ptr(value))->byte_length || !*buffer)) {
+1443
sigil__vm_error(vm, SIGIL_ERR_TYPE, "%s: expected nonempty text without NUL", who);
+1444
return 0;
+1445
}
+1446
return 1;
+1447
}
+1448
+1449
static Value native_wl_cursor_configure(SigilVM *vm, int argc, Value *args)
+1450
{
+1451
Wl *w = as_wl(vm, args[0], "wayland-cursor-configure!");
+1452
char theme[256];
+1453
int64_t size;
+1454
if (!w || !cursor_string_arg(vm, args[1], theme, sizeof theme, 1, "wayland-cursor-configure!") ||
+1455
!int_arg(vm, args[2], &size, "wayland-cursor-configure!")) return DONE;
+1456
if (size < 0 || size > 128) {
+1457
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-cursor-configure!: size must be 0..128");
+1458
return DONE;
+1459
}
+1460
return sui_wayland_cursor_configure(w, sigil_is_false(args[1]) ? NULL : theme, (int)size)
+1461
? SIGIL_TRUE : SIGIL_FALSE;
+1462
}
+1463
+1464
static Value native_wl_cursor_name(SigilVM *vm, int argc, Value *args)
+1465
{
+1466
Wl *w = as_wl(vm, args[0], "wayland-set-cursor!");
+1467
char name[64];
+1468
if (!w || !cursor_string_arg(vm, args[1], name, sizeof name, 0, "wayland-set-cursor!")) return DONE;
+1469
return sui_wayland_cursor_name(w, name) ? SIGIL_TRUE : SIGIL_FALSE;
+1470
}
+1471
+1472
static Value native_wl_cursor_backend(SigilVM *vm, int argc, Value *args)
+1473
{
+1474
Wl *w = as_wl(vm, args[0], "wayland-cursor-backend");
+1475
return w ? sigil_fixnum(sui_wayland_cursor_backend(w)) : DONE;
+1476
}
+1477
+1478
static Value native_wl_cursor_image(SigilVM *vm, int argc, Value *args)
+1479
{
+1480
Wl *w = as_wl(vm, args[0], "wayland-set-cursor-image!");
+1481
if (!w) return DONE;
+1482
if (!sigil_is_bytevector(args[1])) {
+1483
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-set-cursor-image!: expected ARGB bytes");
+1484
return DONE;
+1485
}
+1486
int64_t values[6];
+1487
for (int i = 0; i < 6; i++)
+1488
if (!int_arg(vm, args[i + 2], &values[i], "wayland-set-cursor-image!")) return DONE;
+1489
int64_t width = values[0], height = values[1], stride = values[2];
+1490
int64_t x = values[3], y = values[4], scale = values[5];
+1491
size_t length = sigil_bytevector_length(args[1]);
+1492
if (width < 1 || width > 512 || height < 1 || height > 512 ||
+1493
stride < width * 4 || stride > 2048 || stride * height > (int64_t)length ||
+1494
x < 0 || x >= width || y < 0 || y >= height || scale < 1 || scale > 8 ||
+1495
width % scale || height % scale) {
+1496
sigil__vm_error(vm, SIGIL_ERR_TYPE, "wayland-set-cursor-image!: invalid dimensions, stride, hotspot or scale");
+1497
return DONE;
+1498
}
+1499
return sui_wayland_cursor_image(w, sigil_bytevector_data(args[1]), length,
+1500
(int)width, (int)height, (size_t)stride, (int)x, (int)y, (int)scale) ? SIGIL_TRUE : SIGIL_FALSE;
+1501
}
+1502
1503
static Value native_wl_child(SigilVM *vm, int argc, Value *args)
1504
{
1505
Wl *parent = as_wl(vm, args[0], "wayland-subsurface");
@@ -2280,6 +2363,10 @@ void sigil__register_sigil_ui_wayland(SigilVM *vm)
2363
REGISTER_AND_EXPORT("%wl-present!", native_wl_present, SIGIL_ARITY_EXACT(4), "Present damage from a canvas");
2364
REGISTER_AND_EXPORT("%wl-ready?", native_wl_ready, SIGIL_ARITY_EXACT(1), "Can deferred damage go now");
2365
REGISTER_AND_EXPORT("%wl-child", native_wl_child, SIGIL_ARITY_EXACT(6), "Create an embedded subsurface");
+2366
REGISTER_AND_EXPORT("%wl-cursor-configure!", native_wl_cursor_configure, SIGIL_ARITY_EXACT(3), "Configure explicit cursor theme/size");
+2367
REGISTER_AND_EXPORT("%wl-cursor-name!", native_wl_cursor_name, SIGIL_ARITY_EXACT(2), "Request a named pointer cursor");
+2368
REGISTER_AND_EXPORT("%wl-cursor-backend", native_wl_cursor_backend, SIGIL_ARITY_EXACT(1), "Observe the current cursor backend");
+2369
REGISTER_AND_EXPORT("%wl-cursor-image!", native_wl_cursor_image, SIGIL_ARITY_EXACT(8), "Copy a bounded premultiplied ARGB cursor image");
2370
REGISTER_AND_EXPORT("%wl-alpha!", native_wl_alpha, SIGIL_ARITY_EXACT(2), "Set an overlay alpha mask");
2371
REGISTER_AND_EXPORT("%wl-keymap", native_wl_keymap, SIGIL_ARITY_EXACT(1), "Compositor keymap text");
2372
REGISTER_AND_EXPORT("%wl-title!", native_wl_title, SIGIL_ARITY_EXACT(2), "Update a toplevel title without presenting");
native/wayland/cursor.hadded
@@ -0,0 +1,96 @@
+1
/*
+2
* Copyright © 2012 Intel Corporation
+3
*
+4
* Permission is hereby granted, free of charge, to any person obtaining
+5
* a copy of this software and associated documentation files (the
+6
* "Software"), to deal in the Software without restriction, including
+7
* without limitation the rights to use, copy, modify, merge, publish,
+8
* distribute, sublicense, and/or sell copies of the Software, and to
+9
* permit persons to whom the Software is furnished to do so, subject to
+10
* the following conditions:
+11
*
+12
* The above copyright notice and this permission notice (including the
+13
* next paragraph) shall be included in all copies or substantial
+14
* portions of the Software.
+15
*
+16
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+17
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+18
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+19
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+20
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+21
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+22
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+23
* SOFTWARE.
+24
*/
+25
+26
#ifndef WAYLAND_CURSOR_H
+27
#define WAYLAND_CURSOR_H
+28
+29
#include <stdint.h>
+30
+31
#ifdef __cplusplus
+32
extern "C" {
+33
#endif
+34
+35
struct wl_cursor_theme;
+36
struct wl_buffer;
+37
struct wl_shm;
+38
+39
/** A still image part of a cursor
+40
*
+41
* Use `wl_cursor_image_get_buffer()` to get the corresponding `struct
+42
* wl_buffer` to attach to your `struct wl_surface`. */
+43
struct wl_cursor_image {
+44
/** Actual width */
+45
uint32_t width;
+46
+47
/** Actual height */
+48
uint32_t height;
+49
+50
/** Hot spot x (must be inside image) */
+51
uint32_t hotspot_x;
+52
+53
/** Hot spot y (must be inside image) */
+54
uint32_t hotspot_y;
+55
+56
/** Animation delay to next frame (ms) */
+57
uint32_t delay;
+58
};
+59
+60
/** A cursor, as returned by `wl_cursor_theme_get_cursor()` */
+61
struct wl_cursor {
+62
/** How many images there are in this cursor’s animation */
+63
unsigned int image_count;
+64
+65
/** The array of still images composing this animation */
+66
struct wl_cursor_image **images;
+67
+68
/** The name of this cursor */
+69
char *name;
+70
};
+71
+72
struct wl_cursor_theme *
+73
wl_cursor_theme_load(const char *name, int size, struct wl_shm *shm);
+74
+75
void
+76
wl_cursor_theme_destroy(struct wl_cursor_theme *theme);
+77
+78
struct wl_cursor *
+79
wl_cursor_theme_get_cursor(struct wl_cursor_theme *theme,
+80
const char *name);
+81
+82
struct wl_buffer *
+83
wl_cursor_image_get_buffer(struct wl_cursor_image *image);
+84
+85
int
+86
wl_cursor_frame(struct wl_cursor *cursor, uint32_t time);
+87
+88
int
+89
wl_cursor_frame_and_duration(struct wl_cursor *cursor, uint32_t time,
+90
uint32_t *duration);
+91
+92
#ifdef __cplusplus
+93
}
+94
#endif
+95
+96
#endif
scripts/make-cursor-fixture.sgladded
@@ -0,0 +1,38 @@
+1
#!/usr/bin/env sigil
+2
;;; Usage: sigil scripts/make-cursor-fixture.sgl DIRECTORY
+3
;;; Generates owned opaque test cursors; no installed theme is read or changed.
+4
(import (sigil shell prelude) (sigil fs))
+5
(define root (arg 1))
+6
(define destination (path root "Fixture/cursors"))
+7
(when (or (file-exists? (path root "Fixture")) (file-exists? (path root "default")))
+8
(error "Refusing to replace cursor fixture"))
+9
(ensure-directory destination)
+10
(ensure-directory (path root "default"))
+11
(write-text (path root "default/index.theme") "[Icon Theme]\nInherits=Fixture\n")
+12
(define (u32! bytes offset value)
+13
(each i '(0 1 2 3)
+14
(bytevector-u8-set! bytes (+ offset i) (modulo (quotient value (expt 256 i)) 256))))
+15
(each entry '(("left_ptr" . 4278190335) ("hand2" . 4294901760) ("xterm" . 4278255360)
+16
("unmapped-fixture" . 4294902015))
+17
(define lengths (map (lambda (size) (+ 36 (* size size 4))) '(16 32)))
+18
(define bytes (make-bytevector (+ 40 (apply + lengths)) 0))
+19
(u32! bytes 0 #x72756358)
+20
(u32! bytes 4 16)
+21
(u32! bytes 8 #x10000)
+22
(u32! bytes 12 2)
+23
(let draw ((sizes '(16 32)) (toc 16) (chunk 40))
+24
(unless (null? sizes)
+25
(define size (car sizes))
+26
(u32! bytes toc #xfffd0002)
+27
(u32! bytes (+ toc 4) size)
+28
(u32! bytes (+ toc 8) chunk)
+29
(each pair (list (cons 0 36) (cons 4 #xfffd0002) (cons 8 size) (cons 12 1)
+30
(cons 16 size) (cons 20 size))
+31
(u32! bytes (+ chunk (car pair)) (cdr pair)))
+32
(let paint ((i 0))
+33
(when (< i (* size size))
+34
(u32! bytes (+ chunk 36 (* i 4)) (cdr entry))
+35
(paint (+ i 1))))
+36
(draw (cdr sizes) (+ toc 12) (+ chunk 36 (* size size 4)))))
+37
(write-file-bytes (path destination (car entry)) bytes))
+38
(println "Created owned cursor fixture ~a" root)
scripts/test-wayland-cursor-native.sgladded
@@ -0,0 +1,20 @@
+1
#!/usr/bin/env sigil
+2
;;; Usage: sigil scripts/test-wayland-cursor-native.sgl HEADERS RUNTIME_ARCHIVE ALLOCATOR_OBJECT EVIDENCE_DIRECTORY [CURSOR_LIBRARY]
+3
;;; CC selects the C compiler (default cc); no compositor is required.
+4
(import (sigil shell prelude))
+5
(define compiler
+6
(let ((configured (getenv "CC")))
+7
(if configured
+8
(if (command-exists? configured) configured (error "CC is not executable" configured))
+9
(begin (requires (tools cc)) (tool-path 'cc)))))
+10
(define root (realpath (path (script-dir) "..")))
+11
(define evidence (arg 4))
+12
(ensure-directory evidence)
+13
(define executable (path evidence "cursor-native"))
+14
($ ,compiler -std=c11 -D_GNU_SOURCE -O1 -g -Wall -Wextra -Werror -Wno-unused-parameter
+15
,(str "-I" (arg 1)) ,(str "-I" (path root "native"))
+16
,(str "-I" (path root "native/wayland")) ,(str "-I" (path root "native/wayland/include"))
+17
,(path root "test/wayland/cursor-native.c") ,(arg 2) ,(arg 3)
+18
-lm -ldl -lpthread -o ,executable timeout: 60)
+19
(define library (arg 5 default: #f))
+20
($ ,executable ,@(if library (list library) '()) timeout: 15)
src/sigil/ui/wayland.sglmodified
@@ -73,6 +73,7 @@
73
wayland-surface-focused? wayland-surface-closed? wayland-surface-fd
74
wayland-surface-outputs wayland-surface-buffer-state
75
open-wayland-subsurface wayland-set-alpha! wayland-keymap wayland-native-handle
+76
wayland-cursor-configure! wayland-set-cursor! wayland-set-cursor-image! wayland-cursor-backend
77
wayland-clipboard-set! wayland-clipboard-read
78
wayland-set-title! wayland-decoration-mode
79
wayland-set-fullscreen! wayland-surface-fullscreen?
@@ -102,6 +103,11 @@
103
(: any? any? any? any? any? any? -> any?))
104
(define-native (%wl-alpha! s mask) (: any? any? -> any?))
105
(define-native (%wl-keymap s) (: any? -> any?))
+106
(define-native (%wl-cursor-configure! s theme size) (: any? any? any? -> any?))
+107
(define-native (%wl-cursor-name! s name) (: any? any? -> any?))
+108
(define-native (%wl-cursor-backend s) (: any? -> any?))
+109
(define-native (%wl-cursor-image! s pixels width height stride x y scale)
+110
(: any? any? any? any? any? any? any? any? -> any?))
111
(define-native (%wl-title! s title) (: any? any? -> any?))
112
(define-native (%wl-fullscreen! s fullscreen?) (: any? any? -> any?))
113
(define-native (%wl-fullscreen? s) (: any? -> any?))
@@ -134,6 +140,36 @@
140
(define (wayland-set-alpha! overlay mask)
141
(%wl-alpha! (wayland-surface-handle overlay) mask))
142
+143
;;; Explicit theme and logical size, copied now; neither call dispatches.
+144
;;; Explicit size without theme reads XCURSOR_THEME/default; omitted size
+145
;;; reads XCURSOR_SIZE/24.
+146
;;; Naming either prefers library images. Omitting both restores shape
+147
;;; preference, with the default loader used when no shape is available.
+148
;;; Desktop preference selection belongs to the application.
+149
(define (wayland-cursor-configure! surface (keys: (theme #f) (size 0)))
+150
(%wl-cursor-configure! (wayland-surface-handle surface) theme size))
+151
+152
;;; Stores the desired name across pointer leave/re-entry. "none" hides
+153
;;; the cursor. Names prefer compositor shapes until explicit theme/size
+154
;;; configuration. Unmapped names go unchanged to the optional loader;
+155
;;; missing images without a mapped shape return #f. Themes use frame one.
+156
(define (wayland-set-cursor! surface name)
+157
(%wl-cursor-name! (wayland-surface-handle surface) name))
+158
+159
;;; Copies at most 512x512 premultiplied native ARGB8888 pixels now.
+160
;;; Scale is 1..8 and must divide both dimensions. Hotspots are device
+161
;;; pixels strictly inside the image; stride defaults to packed rows.
+162
(define (wayland-set-cursor-image! surface pixels width height
+163
(keys: (stride (* width 4)) (hotspot-x 0) (hotspot-y 0) (scale 1)))
+164
(%wl-cursor-image! (wayland-surface-handle surface) pixels width height stride
+165
hotspot-x hotspot-y scale))
+166
+167
;;; Observes output without dispatching: unavailable, theme, shape, image
+168
;;; or hidden. Outside pointer focus it is unavailable until re-entry.
+169
(define (wayland-cursor-backend surface)
+170
(vector-ref '#(unavailable theme shape image hidden)
+171
(%wl-cursor-backend (wayland-surface-handle surface))))
+172
173
;;; Update metadata without presenting or committing the surface. Returns
174
;;; true for a changed title, false for an identical one. UTF-8 titles may
175
;;; contain at most 4095 bytes and no NUL. Requires a live xdg-toplevel.
test/wayland/cursor-native.cadded
@@ -0,0 +1,323 @@
+1
/* Native cursor ownership and serial controls; no compositor is required. */
+2
#include <assert.h>
+3
#include <stdarg.h>
+4
#include <sys/resource.h>
+5
#include "../../native/wayland.c"
+6
+7
static uint32_t observed_serial, observed_shape;
+8
static int requests, hidden;
+9
static uintptr_t attached[16];
+10
static int attachments;
+11
static struct wl_proxy *marshal(struct wl_proxy *proxy, uint32_t opcode,
+12
const struct wl_interface *interface, uint32_t version, uint32_t flags, ...)
+13
{
+14
(void)opcode; (void)interface; (void)version; (void)flags;
+15
if ((uintptr_t)proxy == 5) return (struct wl_proxy *)(uintptr_t)4;
+16
if ((uintptr_t)proxy == 4) return (struct wl_proxy *)(uintptr_t)3;
+17
if ((uintptr_t)proxy == 7) {
+18
if (opcode == WL_SURFACE_ATTACH) {
+19
va_list args;
+20
va_start(args, flags);
+21
assert(attachments < 16);
+22
attached[attachments++] = (uintptr_t)va_arg(args, struct wl_buffer *);
+23
va_end(args);
+24
}
+25
return NULL;
+26
}
+27
va_list args;
+28
va_start(args, flags);
+29
observed_serial = va_arg(args, uint32_t);
+30
if ((uintptr_t)proxy == 3) observed_shape = va_arg(args, uint32_t);
+31
else { assert((uintptr_t)proxy == 2); hidden = va_arg(args, void *) == NULL; }
+32
va_end(args);
+33
requests++;
+34
return NULL;
+35
}
+36
static uint32_t version(struct wl_proxy *proxy) { (void)proxy; return 1; }
+37
static int flush(struct wl_display *display) { (void)display; return 0; }
+38
+39
static void *wrong_thread(void *data)
+40
{
+41
assert(!sui_wayland_cursor_name(data, "pointer"));
+42
assert(!sui_wayland_cursor_configure(data, "other", 24));
+43
return NULL;
+44
}
+45
+46
static void queued_old_release(void)
+47
{
+48
Wl window = {.service_thread = pthread_self(), .scale = 1,
+49
.display = (struct wl_display *)(uintptr_t)1,
+50
.pointer = (struct wl_pointer *)(uintptr_t)2};
+51
uint8_t first[16], second[16], a[16], b[16];
+52
memset(first, 0x11, sizeof first);
+53
memset(second, 0x22, sizeof second);
+54
assert(sui_wayland_cursor_image(&window, first, sizeof first, 2, 2, 8, 0, 0, 1));
+55
CursorState *cursor = window.cursor;
+56
cursor->surface = (struct wl_surface *)(uintptr_t)7;
+57
cursor->image_buffers[0] = (CursorBuffer){.owner = &window, .buffer = (struct wl_buffer *)(uintptr_t)6,
+58
.mapped = a, .bytes = 16, .width = 2, .height = 2};
+59
cursor->image_buffers[1] = (CursorBuffer){.owner = &window, .buffer = (struct wl_buffer *)(uintptr_t)8,
+60
.mapped = b, .bytes = 16, .width = 2, .height = 2};
+61
window.pointer_inside = 1;
+62
window.pointer_serial = 789;
+63
attachments = 0;
+64
assert(cursor_apply(&window) && attachments == 1 && attached[0] == 6);
+65
/* The compositor's first release is queued, but not dispatched yet. */
+66
assert(sui_wayland_cursor_configure(&window, cursor->theme_name, cursor->size));
+67
assert(attachments == 2 && attached[1] == 8);
+68
cursor_buffer_released(&cursor->image_buffers[0], NULL);
+69
assert(sui_wayland_cursor_image(&window, second, sizeof second, 2, 2, 8, 0, 0, 1));
+70
assert(attachments == 3 && attached[2] == 6);
+71
assert(a[0] == 0x22 && b[0] == 0x11 && cursor->image_buffers[1].busy);
+72
for (int i = 0; i < 2; i++) {
+73
cursor->image_buffers[i].buffer = NULL;
+74
cursor->image_buffers[i].mapped = NULL;
+75
}
+76
cursor->surface = NULL;
+77
cursor_close(&window);
+78
puts("PASS: queued old release cannot free a re-submitted busy image buffer");
+79
}
+80
+81
static void legacy_no_cursor_call(const char *library)
+82
{
+83
void *available = dlopen(library, RTLD_NOW | RTLD_LOCAL);
+84
assert(available && dlsym(available, "wl_cursor_theme_load"));
+85
assert(!setenv("SIGIL_UI_LIBWAYLAND_CURSOR", library, 1));
+86
Wl window = {.service_thread = pthread_self(), .scale = 1,
+87
.display = (struct wl_display *)(uintptr_t)1,
+88
.pointer = (struct wl_pointer *)(uintptr_t)2,
+89
.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3};
+90
ptr_enter(&window, window.pointer, 987, NULL, 0, 0);
+91
assert(!window.cursor && observed_serial == 987);
+92
assert(observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);
+93
ptr_leave(&window, window.pointer, 988, NULL);
+94
dlclose(available);
+95
puts("PASS: no cursor call retains compositor default with the optional library available");
+96
}
+97
+98
static int loads, lookups, loader_miss;
+99
static const char *expected_theme;
+100
static int expected_size;
+101
static char lookup_name[64];
+102
static struct wl_cursor_image fixture_image = {.width = 16, .height = 16};
+103
static struct wl_cursor_image *fixture_images[] = {&fixture_image};
+104
static struct wl_cursor fixture_cursor = {.image_count = 1, .images = fixture_images};
+105
+106
static struct wl_cursor_theme *load_fixture(const char *theme, int size, struct wl_shm *shm)
+107
{
+108
assert(!strcmp(theme, expected_theme) && size == expected_size && shm);
+109
loads++;
+110
return loader_miss == 1 ? NULL : (struct wl_cursor_theme *)(uintptr_t)9;
+111
}
+112
static void destroy_fixture(struct wl_cursor_theme *theme) { assert((uintptr_t)theme == 9); }
+113
static struct wl_cursor *get_fixture(struct wl_cursor_theme *theme, const char *name)
+114
{
+115
assert((uintptr_t)theme == 9);
+116
lookups++;
+117
snprintf(lookup_name, sizeof lookup_name, "%s", name);
+118
return loader_miss == 2 ? NULL : &fixture_cursor;
+119
}
+120
static struct wl_buffer *buffer_fixture(struct wl_cursor_image *image)
+121
{
+122
assert(image == &fixture_image);
+123
return (struct wl_buffer *)(uintptr_t)6;
+124
}
+125
+126
static void fallback_policy(void)
+127
{
+128
assert(!setenv("SIGIL_UI_LIBWAYLAND_CURSOR", "/nonexistent-cursor-library", 1));
+129
assert(!setenv("XCURSOR_SIZE", "24", 1));
+130
Wl window = {.service_thread = pthread_self(), .scale = 1,
+131
.display = (struct wl_display *)(uintptr_t)1,
+132
.pointer = (struct wl_pointer *)(uintptr_t)2,
+133
.shm = (struct wl_shm *)(uintptr_t)10,
+134
.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3};
+135
assert(sui_wayland_cursor_name(&window, "pointer"));
+136
CursorState *cursor = window.cursor;
+137
cursor->library = (void *)(uintptr_t)1;
+138
cursor->load = load_fixture;
+139
cursor->destroy = destroy_fixture;
+140
cursor->get = get_fixture;
+141
cursor->buffer = buffer_fixture;
+142
cursor->surface = (struct wl_surface *)(uintptr_t)7;
+143
attachments = loads = lookups = loader_miss = 0;
+144
ptr_enter(&window, window.pointer, 321, NULL, 0, 0);
+145
assert(cursor->backend == SUI_CURSOR_SHAPE && !loads && !lookups && !attachments);
+146
assert(observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER);
+147
assert(sui_wayland_cursor_name(&window, "hand2") && !loads);
+148
puts("PASS: mapped name with shape bound bypasses the available loader");
+149
+150
window.cursor_device = NULL;
+151
expected_theme = "default";
+152
expected_size = 24;
+153
assert(sui_wayland_cursor_name(&window, "text"));
+154
assert(cursor->backend == SUI_CURSOR_THEME && loads == 1 && attachments == 1);
+155
puts("PASS: shape unbound uses the default loader and its standard fallbacks");
+156
+157
window.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3;
+158
expected_theme = "Fixture";
+159
expected_size = 16;
+160
assert(sui_wayland_cursor_configure(&window, "Fixture", 16));
+161
assert(cursor->explicit_configuration && cursor->backend == SUI_CURSOR_THEME && loads == 2);
+162
expected_theme = "Explicit";
+163
expected_size = 48;
+164
assert(sui_wayland_cursor_configure(&window, NULL, 48));
+165
assert(cursor->explicit_configuration && cursor->backend == SUI_CURSOR_THEME && loads == 3);
+166
/* Clearing both explicit arguments restores compositor preference. */
+167
assert(!setenv("XCURSOR_SIZE", "48", 1));
+168
assert(sui_wayland_cursor_configure(&window, NULL, 0));
+169
assert(!cursor->explicit_configuration && cursor->backend == SUI_CURSOR_SHAPE && loads == 3);
+170
puts("PASS: explicit theme or size prefers the loader; omitted arguments restore shape preference");
+171
+172
expected_theme = "default";
+173
expected_size = 48;
+174
assert(sui_wayland_cursor_name(&window, "custom-application-name"));
+175
assert(cursor->backend == SUI_CURSOR_THEME && loads == 4);
+176
assert(!strcmp(lookup_name, "custom-application-name"));
+177
loader_miss = 2;
+178
int before = requests;
+179
assert(!sui_wayland_cursor_name(&window, "missing-application-name"));
+180
assert(cursor->backend == SUI_CURSOR_UNAVAILABLE && requests == before);
+181
puts("PASS: unmapped names reach the loader unchanged and never become a default shape");
+182
+183
assert(sui_wayland_cursor_name(&window, "pointer"));
+184
expected_theme = "MissingCursor";
+185
expected_size = 24;
+186
lookups = 0;
+187
assert(sui_wayland_cursor_configure(&window, "MissingCursor", 24));
+188
assert(cursor->backend == SUI_CURSOR_SHAPE && lookups == 2);
+189
window.cursor_device = NULL;
+190
assert(!sui_wayland_cursor_name(&window, "text"));
+191
assert(cursor->backend == SUI_CURSOR_UNAVAILABLE);
+192
loader_miss = 1;
+193
expected_theme = "MissingTheme";
+194
window.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3;
+195
assert(sui_wayland_cursor_configure(&window, "MissingTheme", 24));
+196
assert(cursor->backend == SUI_CURSOR_SHAPE);
+197
window.cursor_device = NULL;
+198
assert(!sui_wayland_cursor_name(&window, "text"));
+199
assert(cursor->backend == SUI_CURSOR_UNAVAILABLE);
+200
cursor->library = NULL;
+201
cursor->surface = NULL;
+202
cursor_close(&window);
+203
puts("PASS: loader theme/cursor misses fall back to a mapped shape or return false");
+204
}
+205
+206
int main(int argc, char **argv)
+207
{
+208
struct rlimit no_core = {0, 0};
+209
assert(!setrlimit(RLIMIT_CORE, &no_core));
+210
setvbuf(stdout, NULL, _IONBF, 0);
+211
assert(!setenv("SIGIL_UI_LIBWAYLAND_CURSOR", "/nonexistent-cursor-library", 1));
+212
assert(!setenv("XCURSOR_THEME", "Explicit", 1));
+213
assert(!setenv("XCURSOR_SIZE", "32", 1));
+214
sui_wl_proxy_marshal_flags = marshal;
+215
sui_wl_proxy_get_version = version;
+216
sui_wl_display_flush = flush;
+217
Wl window = {.service_thread = pthread_self(), .scale = 1,
+218
.display = (struct wl_display *)(uintptr_t)1,
+219
.pointer = (struct wl_pointer *)(uintptr_t)2,
+220
.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3};
+221
assert(sui_wayland_cursor_name(&window, "pointer"));
+222
assert(!strcmp(window.cursor->theme_name, "default") && window.cursor->size == 32);
+223
assert(requests == 0 && sui_wayland_cursor_backend(&window) == SUI_CURSOR_UNAVAILABLE);
+224
ptr_enter(&window, window.pointer, 123, NULL, 0, 0);
+225
assert(requests == 1 && observed_serial == 123 && observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER);
+226
assert(sui_wayland_cursor_backend(&window) == SUI_CURSOR_SHAPE);
+227
assert(sui_wayland_cursor_name(&window, "text"));
+228
assert(observed_serial == 123 && observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT);
+229
assert(sui_wayland_cursor_name(&window, "none") && hidden);
+230
assert(sui_wayland_cursor_backend(&window) == SUI_CURSOR_HIDDEN);
+231
ptr_leave(&window, window.pointer, 124, NULL);
+232
assert(!window.pointer_serial && sui_wayland_cursor_backend(&window) == SUI_CURSOR_UNAVAILABLE);
+233
int before = requests;
+234
assert(sui_wayland_cursor_name(&window, "text") && requests == before);
+235
ptr_enter(&window, window.pointer, 456, NULL, 0, 0);
+236
assert(observed_serial == 456 && observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT);
+237
ptr_leave(&window, window.pointer, 457, NULL);
+238
before = requests;
+239
ptr_enter(&window, window.pointer, 0, NULL, 0, 0);
+240
assert(requests == before + 1 && observed_serial == 0);
+241
assert(observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT);
+242
assert(!sui_wayland_cursor_name(&window, "unknown"));
+243
assert(sui_wayland_cursor_backend(&window) == SUI_CURSOR_UNAVAILABLE);
+244
assert(sui_wayland_cursor_name(&window, "hand2"));
+245
assert(observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER);
+246
puts("PASS: latest/zero enter serial, deferred state, re-entry, aliases, hidden and shape fallback");
+247
+248
ptr_leave(&window, window.pointer, 457, NULL);
+249
window.cursor_device = NULL;
+250
assert(!setenv("SIGIL_UI_WAYLAND_IGNORE", "", 1));
+251
global(&window, (struct wl_registry *)(uintptr_t)5, 42, "wp_cursor_shape_manager_v1", 1);
+252
assert((uintptr_t)window.cursor_manager == 4 && (uintptr_t)window.cursor_device == 3);
+253
assert(sui_wayland_cursor_configure(&window, "Desktop", 48));
+254
assert(!strcmp(window.cursor->theme_name, "Desktop") && window.cursor->size == 48);
+255
assert(!sui_wayland_cursor_configure(&window, "", 24));
+256
assert(!sui_wayland_cursor_configure(&window, "x", -1));
+257
assert(!sui_wayland_cursor_configure(&window, "x", 129));
+258
assert(!setenv("XCURSOR_SIZE", "garbage", 1));
+259
assert(sui_wayland_cursor_configure(&window, NULL, 0) && window.cursor->size == 24);
+260
assert(!setenv("XCURSOR_SIZE", "999999999999999999999999999", 1));
+261
assert(cursor_environment_size() == 24);
+262
assert(!setenv("XCURSOR_SIZE", "128", 1) && cursor_environment_size() == 128);
+263
assert(!sui_wayland_cursor_name(&window, ""));
+264
char long_name[65]; memset(long_name, 'a', sizeof long_name - 1); long_name[64] = 0;
+265
assert(!sui_wayland_cursor_name(&window, long_name));
+266
pthread_t thread;
+267
assert(!pthread_create(&thread, NULL, wrong_thread, &window));
+268
assert(!pthread_join(thread, NULL));
+269
puts("PASS: explicit/environment configuration, bounded validation and owning-thread refusal");
+270
+271
uint8_t image[32];
+272
for (unsigned i = 0; i < sizeof image; i++) image[i] = (uint8_t)i;
+273
assert(sui_wayland_cursor_image(&window, image, sizeof image, 2, 2, 16, 1, 1, 1));
+274
assert(window.cursor->image[0] == 0 && window.cursor->image[8] == 16);
+275
memset(image, 0xff, sizeof image);
+276
assert(window.cursor->image[0] == 0 && window.cursor->image[8] == 16);
+277
assert(!sui_wayland_cursor_image(&window, image, 31, 2, 2, 16, 1, 1, 1));
+278
assert(!sui_wayland_cursor_image(&window, image, 32, 2, 2, 16, 2, 1, 1));
+279
assert(!sui_wayland_cursor_image(&window, image, 32, 2, 2, 7, 1, 1, 1));
+280
assert(!sui_wayland_cursor_image(&window, image, 32, 2, 2, 16, 1, 1, 3));
+281
assert(!sui_wayland_cursor_image(&window, image, 32, 513, 2, 16, 1, 1, 1));
+282
assert(!sui_wayland_cursor_image(&window, NULL, 32, 2, 2, 16, 1, 1, 1));
+283
CursorState *cursor = window.cursor;
+284
uint8_t mapped[16], latest[16];
+285
memset(mapped, 0xaa, sizeof mapped);
+286
memset(latest, 0x11, sizeof latest);
+287
cursor->image_buffers[0] = (CursorBuffer){.owner = &window,
+288
.buffer = (struct wl_buffer *)(uintptr_t)6, .mapped = mapped,
+289
.bytes = sizeof mapped, .width = 2, .height = 2, .busy = 1};
+290
cursor->image_buffers[1].busy = 1;
+291
cursor->current_image = 1;
+292
window.pointer_inside = 1;
+293
before = requests;
+294
for (int i = 0; i < 100; i++) {
+295
latest[0] = (uint8_t)i;
+296
assert(sui_wayland_cursor_image(&window, latest, sizeof latest, 2, 2, 8, 0, 0, 1));
+297
}
+298
assert(requests == before && cursor->image_pending && mapped[0] == 0xaa);
+299
assert(cursor->image[0] == 99 && cursor->current_image == 1);
+300
window.pointer_inside = 0;
+301
cursor_buffer_released(&cursor->image_buffers[0], NULL);
+302
assert(!cursor->image_buffers[0].busy && cursor->image_pending);
+303
assert(cursor_image_buffer(&window) == 0 && !cursor->image_pending);
+304
assert(mapped[0] == 99 && mapped[1] == 0x11 && cursor->image_buffers[1].busy);
+305
cursor->image_buffers[0].buffer = NULL;
+306
cursor->image_buffers[0].mapped = NULL;
+307
cursor->image_buffers[0].bytes = 0;
+308
cursor->image_buffers[1].busy = 0;
+309
puts("PASS: two busy buffers retain only the latest update and never overwrite until release");
+310
assert(sui_wayland_cursor_name(&window, "default") && !window.cursor->image);
+311
window.closed = 1;
+312
assert(!sui_wayland_cursor_name(&window, "pointer"));
+313
assert(!sui_wayland_cursor_configure(&window, "x", 24));
+314
window.closed = 0;
+315
cursor_close(&window);
+316
cursor_close(&window);
+317
assert(!window.cursor);
+318
puts("PASS: copied padded-stride images, bounds, replacement and idempotent cleanup");
+319
queued_old_release();
+320
fallback_policy();
+321
legacy_no_cursor_call(argc > 1 ? argv[1] : "libwayland-cursor.so.0");
+322
return 0;
+323
}
test/wayland/live.sglmodified
@@ -15,6 +15,10 @@
15
16
(define output (need "SIGIL_UI_TEST_OUTPUT"))
17
(define shots (need "SIGIL_UI_TEST_SHOTS"))
+18
(define original-cursor-library (getenv "SIGIL_UI_LIBWAYLAND_CURSOR"))
+19
(define original-cursor-path (getenv "XCURSOR_PATH"))
+20
(define original-cursor-theme (getenv "XCURSOR_THEME"))
+21
(define original-cursor-size (getenv "XCURSOR_SIZE"))
22
23
;; ------------------------------------------------------------ tools
24
@@ -120,6 +124,24 @@
124
(process-output->string "grim" "-t" "ppm" "-o" output path)
125
(parse-ppm (read-file-bytes path))))
126
+127
(define (cursor-screenshot)
+128
(set! shot-count (+ shot-count 1))
+129
(let ((path (string-append shots "/cursor-" (n->s shot-count) ".ppm")))
+130
(process-output->string "grim" "-c" "-t" "ppm" "-o" output path)
+131
(process-output->string "grim" "-c" "-t" "png" "-o" output
+132
(string-append shots "/cursor-" (n->s shot-count) ".png"))
+133
(parse-ppm (read-file-bytes path))))
+134
+135
(define (expect-cursor s color extent)
+136
(pump s 100)
+137
(let ((expected (make-canvas 1280 800 #x111111)))
+138
(when color (canvas-fill! expected 300 200 extent extent color))
+139
(let ((matches? (region-matches? (cursor-screenshot) 290 190 96 96 expected 290 190)))
+140
(unless matches?
+141
(display "Cursor capture mismatch (color extent): ")
+142
(write (list color extent)) (newline))
+143
(assert-true matches?))))
+144
145
(define (parse-ppm bv)
146
;; P6, then width, height and maxval separated by whitespace, then one
147
;; whitespace byte and the pixels.
@@ -201,6 +223,10 @@
223
(set! live '())
224
(set! inbox '())
225
(setenv! "SIGIL_UI_WAYLAND_IGNORE" "")
+226
(setenv! "SIGIL_UI_LIBWAYLAND_CURSOR" original-cursor-library)
+227
(setenv! "XCURSOR_PATH" original-cursor-path)
+228
(setenv! "XCURSOR_THEME" original-cursor-theme)
+229
(setenv! "XCURSOR_SIZE" original-cursor-size)
230
(set-scale! "1")
231
(sway "input type:keyboard repeat_delay 600")
232
(sway "input type:keyboard repeat_rate 25")
@@ -434,6 +460,117 @@
460
(loop (append events (wayland-window-dispatch s left)))))))
461
(close! s))))
462
+463
(test-group "cursor"
+464
(live-test "explicit theme, named and copied image cursors render at both scales and survive re-entry"
+465
(setenv! "SIGIL_UI_WAYLAND_IGNORE" "wp_cursor_shape_manager_v1")
+466
(setenv! "XCURSOR_PATH" (need "SIGIL_UI_TEST_CURSOR_ROOT"))
+467
(for-each (lambda (scale)
+468
(set-scale! (n->s scale))
+469
(let ((s (open-toplevel)))
+470
(define (reenter!)
+471
(let ((other (open-toplevel)))
+472
(drive! "move" "1000" "200")
+473
(pump other 100)
+474
(pump s 100)
+475
(expect 'unavailable (wayland-cursor-backend s))
+476
(close! other))
+477
(pump s 100)
+478
(show! s (make-canvas (wayland-surface-device-width s) (wayland-surface-device-height s) #x111111))
+479
(drive! "move" "300" "200")
+480
(pump s 100))
+481
(assert-true (wayland-cursor-configure! s theme: "Fixture" size: 16))
+482
(show! s (make-canvas 1280 800 #x111111))
+483
(drive! "move" "300" "200")
+484
(pump s 100)
+485
(for-each (lambda (entry)
+486
(assert-true (wayland-set-cursor! s (car entry)))
+487
(expect 'theme (wayland-cursor-backend s))
+488
(expect-cursor s (cdr entry) (* 16 scale)))
+489
'(("default" . #x0000ff) ("pointer" . #xff0000) ("text" . #x00ff00)))
+490
;; Copy now: later mutation of the caller's pixels cannot change it.
+491
(let* ((side (* 8 scale)) (pixels (make-bytevector (* side side 4) 255)))
+492
(define (fill-color! red green blue)
+493
(let fill ((i 0))
+494
(when (< i (bytevector-length pixels))
+495
(bytevector-u8-set! pixels i blue)
+496
(bytevector-u8-set! pixels (+ i 1) green)
+497
(bytevector-u8-set! pixels (+ i 2) red)
+498
(fill (+ i 4)))))
+499
;; Match the output's device scale so the compositor does not
+500
;; resample a lower-resolution source at the cursor's edges.
+501
;; Three updates without dispatch fill both slots, then retain
+502
;; only the latest yellow image until a buffer release arrives.
+503
(fill-color! 0 0 255)
+504
(assert-true (wayland-set-cursor-image! s pixels side side scale: scale))
+505
(fill-color! 255 0 0)
+506
(assert-true (wayland-set-cursor-image! s pixels side side scale: scale))
+507
(fill-color! 255 255 0)
+508
(assert-true (wayland-set-cursor-image! s pixels side side scale: scale))
+509
(bytevector-u8-set! pixels 1 0)
+510
(expect 'image (wayland-cursor-backend s))
+511
(expect-cursor s #xffff00 (* 8 scale)))
+512
(reenter!)
+513
(expect 'image (wayland-cursor-backend s))
+514
(expect-cursor s #xffff00 (* 8 scale))
+515
(assert-true (wayland-set-cursor! s "none"))
+516
(expect 'hidden (wayland-cursor-backend s))
+517
(expect-cursor s #f 0)
+518
(assert-true (wayland-set-cursor! s "text"))
+519
(reenter!)
+520
(expect 'theme (wayland-cursor-backend s))
+521
(expect-cursor s #x00ff00 (* 16 scale))
+522
(assert-error (wayland-set-cursor! s ""))
+523
(assert-error (wayland-set-cursor! s (string #\a (integer->char 0) #\b)))
+524
(assert-error (wayland-cursor-configure! s size: 129))
+525
(assert-error (wayland-set-cursor-image! s (make-bytevector 4 0) 2 2))
+526
(close! s)
+527
(assert-error (wayland-set-cursor! s "pointer")))) '(1 2))
+528
(set-scale! "1"))
+529
+530
(live-test "cursor policy selects compositor shapes, explicit themes and inherited default images"
+531
(setenv! "XCURSOR_PATH" (need "SIGIL_UI_TEST_CURSOR_ROOT"))
+532
(setenv! "XCURSOR_THEME" "Fixture")
+533
(setenv! "XCURSOR_SIZE" "16")
+534
(let ((s (open-toplevel)))
+535
(show! s (make-canvas 1280 800 #x111111))
+536
(drive! "move" "300" "200")
+537
(pump s 100)
+538
(assert-true (wayland-set-cursor! s "pointer"))
+539
(expect 'shape (wayland-cursor-backend s))
+540
(assert-true (wayland-cursor-configure! s theme: "Fixture" size: 16))
+541
(expect 'theme (wayland-cursor-backend s))
+542
(expect-cursor s #xff0000 16)
+543
(assert-true (wayland-cursor-configure! s))
+544
(expect 'shape (wayland-cursor-backend s))
+545
(assert-true (wayland-set-cursor! s "unmapped-fixture"))
+546
(expect 'theme (wayland-cursor-backend s))
+547
(expect-cursor s #xff00ff 16)
+548
(assert-false (wayland-set-cursor! s "missing-fixture-cursor"))
+549
(expect 'unavailable (wayland-cursor-backend s))
+550
(close! s))
+551
(setenv! "SIGIL_UI_WAYLAND_IGNORE" "wp_cursor_shape_manager_v1")
+552
(let ((s (open-toplevel)))
+553
(show! s (make-canvas 1280 800 #x111111))
+554
(drive! "move" "300" "200")
+555
(pump s 100)
+556
(assert-true (wayland-set-cursor! s "text"))
+557
(expect 'theme (wayland-cursor-backend s))
+558
(expect-cursor s #x00ff00 16)
+559
(close! s)))
+560
+561
(live-test "missing optional cursor library preserves compositor pointer and text shapes"
+562
(setenv! "SIGIL_UI_LIBWAYLAND_CURSOR" "/nonexistent-cursor-library")
+563
(let ((s (open-toplevel)))
+564
(drive! "move" "300" "200")
+565
(pump s 100)
+566
(assert-true (wayland-set-cursor! s "pointer"))
+567
(expect 'shape (wayland-cursor-backend s))
+568
(assert-true (wayland-set-cursor! s "text"))
+569
(expect 'shape (wayland-cursor-backend s))
+570
(pump s 100)
+571
(assert-false (wayland-surface-closed? s))
+572
(close! s))))
+573
574
(test-group "scale"
575
(live-test "scale 2: buffer scale follows the output, logical size halves, pixels are exact"
576
(let ((s (open-toplevel)))
test/wayland/runmodified
@@ -30,8 +30,11 @@ for dir in ${GUIX_ENVIRONMENT:+"$GUIX_ENVIRONMENT/lib"} "$HOME/.guix-home/profil
30
if [ -z "${SIGIL_UI_LIBXKBCOMMON:-}" ] && [ -e "$dir/libxkbcommon.so.0" ]; then
31
SIGIL_UI_LIBXKBCOMMON="$dir/libxkbcommon.so.0"
32
fi
+33
if [ -z "${SIGIL_UI_LIBWAYLAND_CURSOR:-}" ] && [ -e "$dir/libwayland-cursor.so.0" ]; then
+34
SIGIL_UI_LIBWAYLAND_CURSOR="$dir/libwayland-cursor.so.0"
+35
fi
36
done
−34
export SIGIL_UI_LIBWAYLAND_CLIENT SIGIL_UI_LIBXKBCOMMON
+37
export SIGIL_UI_LIBWAYLAND_CLIENT SIGIL_UI_LIBXKBCOMMON SIGIL_UI_LIBWAYLAND_CURSOR
38
39
# A short runtime directory: a Wayland socket path must fit in 108 bytes.
40
runtime=$(mktemp -d /tmp/sigil-ui-wl.XXXXXX)
@@ -91,6 +94,8 @@ done
94
95
export XDG_RUNTIME_DIR="$runtime" WAYLAND_DISPLAY="$socket" SWAYSOCK="$sock"
96
export SIGIL_UI_TEST_OUTPUT=HEADLESS-1 SIGIL_UI_TEST_SHOTS="$runtime"
+97
"$SIGIL" "$root/scripts/make-cursor-fixture.sgl" "$runtime/cursor-fixtures"
+98
export SIGIL_UI_TEST_CURSOR_ROOT="$runtime/cursor-fixtures"
99
echo "run: sway $(sway --version | cut -d' ' -f3) headless at $runtime/$socket"
100
cd "$root"
101
status=0