AtlatestRepositorysigil-ui
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
#include "standalone-protocols.h"8
static uint32_t observed_serial, observed_shape;9
static int requests, hidden;10
static uintptr_t attached[16];11
static int attachments;12
static struct wl_proxy *marshal(struct wl_proxy *proxy, uint32_t opcode,13
const struct wl_interface *interface, uint32_t version, uint32_t flags, ...)14
{15
(void)opcode; (void)interface; (void)version; (void)flags;16
if ((uintptr_t)proxy == 5) return (struct wl_proxy *)(uintptr_t)4;17
if ((uintptr_t)proxy == 4) return (struct wl_proxy *)(uintptr_t)3;18
if ((uintptr_t)proxy == 7) {19
if (opcode == WL_SURFACE_ATTACH) {20
va_list args;21
va_start(args, flags);22
assert(attachments < 16);23
attached[attachments++] = (uintptr_t)va_arg(args, struct wl_buffer *);24
va_end(args);25
}26
return NULL;27
}28
va_list args;29
va_start(args, flags);30
observed_serial = va_arg(args, uint32_t);31
if ((uintptr_t)proxy == 3) observed_shape = va_arg(args, uint32_t);32
else { assert((uintptr_t)proxy == 2); hidden = va_arg(args, void *) == NULL; }33
va_end(args);34
requests++;35
return NULL;36
}37
static uint32_t version(struct wl_proxy *proxy) { (void)proxy; return 1; }38
static int flush(struct wl_display *display) { (void)display; return 0; }40
static void *wrong_thread(void *data)41
{42
assert(!sui_wayland_cursor_name(data, "pointer"));43
assert(!sui_wayland_cursor_configure(data, "other", 24));44
return NULL;45
}47
static void queued_old_release(void)48
{49
Wl window = {.service_thread = pthread_self(), .scale = 1,50
.display = (struct wl_display *)(uintptr_t)1,51
.pointer = (struct wl_pointer *)(uintptr_t)2};52
uint8_t first[16], second[16], a[16], b[16];53
memset(first, 0x11, sizeof first);54
memset(second, 0x22, sizeof second);55
assert(sui_wayland_cursor_image(&window, first, sizeof first, 2, 2, 8, 0, 0, 1));56
CursorState *cursor = window.cursor;57
cursor->surface = (struct wl_surface *)(uintptr_t)7;58
cursor->image_buffers[0] = (CursorBuffer){.owner = &window, .buffer = (struct wl_buffer *)(uintptr_t)6,59
.mapped = a, .bytes = 16, .width = 2, .height = 2};60
cursor->image_buffers[1] = (CursorBuffer){.owner = &window, .buffer = (struct wl_buffer *)(uintptr_t)8,61
.mapped = b, .bytes = 16, .width = 2, .height = 2};62
window.pointer_inside = 1;63
window.pointer_serial = 789;64
attachments = 0;65
assert(cursor_apply(&window) && attachments == 1 && attached[0] == 6);66
/* The compositor's first release is queued, but not dispatched yet. */67
assert(sui_wayland_cursor_configure(&window, cursor->theme_name, cursor->size));68
assert(attachments == 2 && attached[1] == 8);69
cursor_buffer_released(&cursor->image_buffers[0], NULL);70
assert(sui_wayland_cursor_image(&window, second, sizeof second, 2, 2, 8, 0, 0, 1));71
assert(attachments == 3 && attached[2] == 6);72
assert(a[0] == 0x22 && b[0] == 0x11 && cursor->image_buffers[1].busy);73
for (int i = 0; i < 2; i++) {74
cursor->image_buffers[i].buffer = NULL;75
cursor->image_buffers[i].mapped = NULL;76
}77
cursor->surface = NULL;78
cursor_close(&window);79
puts("PASS: queued old release cannot free a re-submitted busy image buffer");80
}82
static void legacy_no_cursor_call(const char *library)83
{84
void *available = dlopen(library, RTLD_NOW | RTLD_LOCAL);85
assert(available && dlsym(available, "wl_cursor_theme_load"));86
assert(!setenv("SIGIL_UI_LIBWAYLAND_CURSOR", library, 1));87
Wl window = {.service_thread = pthread_self(), .scale = 1,88
.display = (struct wl_display *)(uintptr_t)1,89
.pointer = (struct wl_pointer *)(uintptr_t)2,90
.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3};91
ptr_enter(&window, window.pointer, 987, NULL, 0, 0);92
assert(!window.cursor && observed_serial == 987);93
assert(observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DEFAULT);94
ptr_leave(&window, window.pointer, 988, NULL);95
dlclose(available);96
puts("PASS: no cursor call retains compositor default with the optional library available");97
}99
static int loads, lookups, loader_miss;100
static const char *expected_theme;101
static int expected_size;102
static char lookup_name[64];103
static struct wl_cursor_image fixture_image = {.width = 16, .height = 16};104
static struct wl_cursor_image *fixture_images[] = {&fixture_image};105
static struct wl_cursor fixture_cursor = {.image_count = 1, .images = fixture_images};107
static struct wl_cursor_theme *load_fixture(const char *theme, int size, struct wl_shm *shm)108
{109
assert(!strcmp(theme, expected_theme) && size == expected_size && shm);110
loads++;111
return loader_miss == 1 ? NULL : (struct wl_cursor_theme *)(uintptr_t)9;112
}113
static void destroy_fixture(struct wl_cursor_theme *theme) { assert((uintptr_t)theme == 9); }114
static struct wl_cursor *get_fixture(struct wl_cursor_theme *theme, const char *name)115
{116
assert((uintptr_t)theme == 9);117
lookups++;118
snprintf(lookup_name, sizeof lookup_name, "%s", name);119
return loader_miss == 2 ? NULL : &fixture_cursor;120
}121
static struct wl_buffer *buffer_fixture(struct wl_cursor_image *image)122
{123
assert(image == &fixture_image);124
return (struct wl_buffer *)(uintptr_t)6;125
}127
static void fallback_policy(void)128
{129
assert(!setenv("SIGIL_UI_LIBWAYLAND_CURSOR", "/nonexistent-cursor-library", 1));130
assert(!setenv("XCURSOR_SIZE", "24", 1));131
Wl window = {.service_thread = pthread_self(), .scale = 1,132
.display = (struct wl_display *)(uintptr_t)1,133
.pointer = (struct wl_pointer *)(uintptr_t)2,134
.shm = (struct wl_shm *)(uintptr_t)10,135
.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3};136
assert(sui_wayland_cursor_name(&window, "pointer"));137
CursorState *cursor = window.cursor;138
cursor->library = (void *)(uintptr_t)1;139
cursor->load = load_fixture;140
cursor->destroy = destroy_fixture;141
cursor->get = get_fixture;142
cursor->buffer = buffer_fixture;143
cursor->surface = (struct wl_surface *)(uintptr_t)7;144
attachments = loads = lookups = loader_miss = 0;145
ptr_enter(&window, window.pointer, 321, NULL, 0, 0);146
assert(cursor->backend == SUI_CURSOR_SHAPE && !loads && !lookups && !attachments);147
assert(observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER);148
assert(sui_wayland_cursor_name(&window, "hand2") && !loads);149
puts("PASS: mapped name with shape bound bypasses the available loader");151
window.cursor_device = NULL;152
expected_theme = "default";153
expected_size = 24;154
assert(sui_wayland_cursor_name(&window, "text"));155
assert(cursor->backend == SUI_CURSOR_THEME && loads == 1 && attachments == 1);156
puts("PASS: shape unbound uses the default loader and its standard fallbacks");158
window.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3;159
expected_theme = "Fixture";160
expected_size = 16;161
assert(sui_wayland_cursor_configure(&window, "Fixture", 16));162
assert(cursor->explicit_configuration && cursor->backend == SUI_CURSOR_THEME && loads == 2);163
expected_theme = "Explicit";164
expected_size = 48;165
assert(sui_wayland_cursor_configure(&window, NULL, 48));166
assert(cursor->explicit_configuration && cursor->backend == SUI_CURSOR_THEME && loads == 3);167
/* Clearing both explicit arguments restores compositor preference. */168
assert(!setenv("XCURSOR_SIZE", "48", 1));169
assert(sui_wayland_cursor_configure(&window, NULL, 0));170
assert(!cursor->explicit_configuration && cursor->backend == SUI_CURSOR_SHAPE && loads == 3);171
puts("PASS: explicit theme or size prefers the loader; omitted arguments restore shape preference");173
expected_theme = "default";174
expected_size = 48;175
assert(sui_wayland_cursor_name(&window, "custom-application-name"));176
assert(cursor->backend == SUI_CURSOR_THEME && loads == 4);177
assert(!strcmp(lookup_name, "custom-application-name"));178
loader_miss = 2;179
int before = requests;180
assert(!sui_wayland_cursor_name(&window, "missing-application-name"));181
assert(cursor->backend == SUI_CURSOR_UNAVAILABLE && requests == before);182
puts("PASS: unmapped names reach the loader unchanged and never become a default shape");184
assert(sui_wayland_cursor_name(&window, "pointer"));185
expected_theme = "MissingCursor";186
expected_size = 24;187
lookups = 0;188
assert(sui_wayland_cursor_configure(&window, "MissingCursor", 24));189
assert(cursor->backend == SUI_CURSOR_SHAPE && lookups == 2);190
window.cursor_device = NULL;191
assert(!sui_wayland_cursor_name(&window, "text"));192
assert(cursor->backend == SUI_CURSOR_UNAVAILABLE);193
loader_miss = 1;194
expected_theme = "MissingTheme";195
window.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3;196
assert(sui_wayland_cursor_configure(&window, "MissingTheme", 24));197
assert(cursor->backend == SUI_CURSOR_SHAPE);198
window.cursor_device = NULL;199
assert(!sui_wayland_cursor_name(&window, "text"));200
assert(cursor->backend == SUI_CURSOR_UNAVAILABLE);201
cursor->library = NULL;202
cursor->surface = NULL;203
cursor_close(&window);204
puts("PASS: loader theme/cursor misses fall back to a mapped shape or return false");205
}207
int main(int argc, char **argv)208
{209
struct rlimit no_core = {0, 0};210
assert(!setrlimit(RLIMIT_CORE, &no_core));211
setvbuf(stdout, NULL, _IONBF, 0);212
assert(!setenv("SIGIL_UI_LIBWAYLAND_CURSOR", "/nonexistent-cursor-library", 1));213
assert(!setenv("XCURSOR_THEME", "Explicit", 1));214
assert(!setenv("XCURSOR_SIZE", "32", 1));215
sui_wl_proxy_marshal_flags = marshal;216
sui_wl_proxy_get_version = version;217
sui_wl_display_flush = flush;218
Wl window = {.service_thread = pthread_self(), .scale = 1,219
.display = (struct wl_display *)(uintptr_t)1,220
.pointer = (struct wl_pointer *)(uintptr_t)2,221
.cursor_device = (struct wp_cursor_shape_device_v1 *)(uintptr_t)3};222
assert(sui_wayland_cursor_name(&window, "pointer"));223
assert(!strcmp(window.cursor->theme_name, "default") && window.cursor->size == 32);224
assert(requests == 0 && sui_wayland_cursor_backend(&window) == SUI_CURSOR_UNAVAILABLE);225
ptr_enter(&window, window.pointer, 123, NULL, 0, 0);226
assert(requests == 1 && observed_serial == 123 && observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER);227
assert(sui_wayland_cursor_backend(&window) == SUI_CURSOR_SHAPE);228
assert(sui_wayland_cursor_name(&window, "text"));229
assert(observed_serial == 123 && observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT);230
assert(sui_wayland_cursor_name(&window, "none") && hidden);231
assert(sui_wayland_cursor_backend(&window) == SUI_CURSOR_HIDDEN);232
ptr_leave(&window, window.pointer, 124, NULL);233
assert(!window.pointer_serial && sui_wayland_cursor_backend(&window) == SUI_CURSOR_UNAVAILABLE);234
int before = requests;235
assert(sui_wayland_cursor_name(&window, "text") && requests == before);236
ptr_enter(&window, window.pointer, 456, NULL, 0, 0);237
assert(observed_serial == 456 && observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT);238
ptr_leave(&window, window.pointer, 457, NULL);239
before = requests;240
ptr_enter(&window, window.pointer, 0, NULL, 0, 0);241
assert(requests == before + 1 && observed_serial == 0);242
assert(observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_TEXT);243
assert(!sui_wayland_cursor_name(&window, "unknown"));244
assert(sui_wayland_cursor_backend(&window) == SUI_CURSOR_UNAVAILABLE);245
assert(sui_wayland_cursor_name(&window, "hand2"));246
assert(observed_shape == WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_POINTER);247
puts("PASS: latest/zero enter serial, deferred state, re-entry, aliases, hidden and shape fallback");249
ptr_leave(&window, window.pointer, 457, NULL);250
window.cursor_device = NULL;251
assert(!setenv("SIGIL_UI_WAYLAND_IGNORE", "", 1));252
global(&window, (struct wl_registry *)(uintptr_t)5, 42, "wp_cursor_shape_manager_v1", 1);253
assert((uintptr_t)window.cursor_manager == 4 && (uintptr_t)window.cursor_device == 3);254
assert(sui_wayland_cursor_configure(&window, "Desktop", 48));255
assert(!strcmp(window.cursor->theme_name, "Desktop") && window.cursor->size == 48);256
assert(!sui_wayland_cursor_configure(&window, "", 24));257
assert(!sui_wayland_cursor_configure(&window, "x", -1));258
assert(!sui_wayland_cursor_configure(&window, "x", 129));259
assert(!setenv("XCURSOR_SIZE", "garbage", 1));260
assert(sui_wayland_cursor_configure(&window, NULL, 0) && window.cursor->size == 24);261
assert(!setenv("XCURSOR_SIZE", "999999999999999999999999999", 1));262
assert(cursor_environment_size() == 24);263
assert(!setenv("XCURSOR_SIZE", "128", 1) && cursor_environment_size() == 128);264
assert(!sui_wayland_cursor_name(&window, ""));265
char long_name[65]; memset(long_name, 'a', sizeof long_name - 1); long_name[64] = 0;266
assert(!sui_wayland_cursor_name(&window, long_name));267
pthread_t thread;268
assert(!pthread_create(&thread, NULL, wrong_thread, &window));269
assert(!pthread_join(thread, NULL));270
puts("PASS: explicit/environment configuration, bounded validation and owning-thread refusal");272
uint8_t image[32];273
for (unsigned i = 0; i < sizeof image; i++) image[i] = (uint8_t)i;274
assert(sui_wayland_cursor_image(&window, image, sizeof image, 2, 2, 16, 1, 1, 1));275
assert(window.cursor->image[0] == 0 && window.cursor->image[8] == 16);276
memset(image, 0xff, sizeof image);277
assert(window.cursor->image[0] == 0 && window.cursor->image[8] == 16);278
assert(!sui_wayland_cursor_image(&window, image, 31, 2, 2, 16, 1, 1, 1));279
assert(!sui_wayland_cursor_image(&window, image, 32, 2, 2, 16, 2, 1, 1));280
assert(!sui_wayland_cursor_image(&window, image, 32, 2, 2, 7, 1, 1, 1));281
assert(!sui_wayland_cursor_image(&window, image, 32, 2, 2, 16, 1, 1, 3));282
assert(!sui_wayland_cursor_image(&window, image, 32, 513, 2, 16, 1, 1, 1));283
assert(!sui_wayland_cursor_image(&window, NULL, 32, 2, 2, 16, 1, 1, 1));284
CursorState *cursor = window.cursor;285
uint8_t mapped[16], latest[16];286
memset(mapped, 0xaa, sizeof mapped);287
memset(latest, 0x11, sizeof latest);288
cursor->image_buffers[0] = (CursorBuffer){.owner = &window,289
.buffer = (struct wl_buffer *)(uintptr_t)6, .mapped = mapped,290
.bytes = sizeof mapped, .width = 2, .height = 2, .busy = 1};291
cursor->image_buffers[1].busy = 1;292
cursor->current_image = 1;293
window.pointer_inside = 1;294
before = requests;295
for (int i = 0; i < 100; i++) {296
latest[0] = (uint8_t)i;297
assert(sui_wayland_cursor_image(&window, latest, sizeof latest, 2, 2, 8, 0, 0, 1));298
}299
assert(requests == before && cursor->image_pending && mapped[0] == 0xaa);300
assert(cursor->image[0] == 99 && cursor->current_image == 1);301
window.pointer_inside = 0;302
cursor_buffer_released(&cursor->image_buffers[0], NULL);303
assert(!cursor->image_buffers[0].busy && cursor->image_pending);304
assert(cursor_image_buffer(&window) == 0 && !cursor->image_pending);305
assert(mapped[0] == 99 && mapped[1] == 0x11 && cursor->image_buffers[1].busy);306
cursor->image_buffers[0].buffer = NULL;307
cursor->image_buffers[0].mapped = NULL;308
cursor->image_buffers[0].bytes = 0;309
cursor->image_buffers[1].busy = 0;310
puts("PASS: two busy buffers retain only the latest update and never overwrite until release");311
assert(sui_wayland_cursor_name(&window, "default") && !window.cursor->image);312
window.closed = 1;313
assert(!sui_wayland_cursor_name(&window, "pointer"));314
assert(!sui_wayland_cursor_configure(&window, "x", 24));315
window.closed = 0;316
cursor_close(&window);317
cursor_close(&window);318
assert(!window.cursor);319
puts("PASS: copied padded-stride images, bounds, replacement and idempotent cleanup");320
queued_old_release();321
fallback_policy();322
legacy_no_cursor_call(argc > 1 ? argv[1] : "libwayland-cursor.so.0");323
return 0;324
}