AtlatestRepositorysigil-ui

sigil-ui / tree / test / waylandnative-bridge.c

1/* Native callback ownership under a real private compositor. */
2#include <assert.h>
3#include <sys/resource.h>
4#include <sys/socket.h>
5#include "../../native/wayland.c"
6#include "standalone-protocols.h"
7
8typedef struct { int released, rendered, canceled; } Notifications;
9static void test_release(void *data) { ((Notifications *)data)->released++; }
10static void test_frame(void *data, uint32_t time, int presented)
12 Notifications *notifications = data;
13 if (presented) notifications->rendered++;
14 else notifications->canceled++;
17typedef struct {
18 Notifications notifications;
19 Wl *child;
20} OwnedNotifications;
22static void drop_after_notifications(OwnedNotifications *owned)
24 if (owned->notifications.released && owned->notifications.canceled) {
25 Wl *child = owned->child;
26 owned->child = NULL;
27 sui_wayland_unref(child);
28 }
31static void owned_release(void *data)
33 OwnedNotifications *owned = data;
34 owned->notifications.released++;
35 drop_after_notifications(owned);
38static void owned_frame(void *data, uint32_t time, int presented)
40 OwnedNotifications *owned = data;
41 assert(!presented);
42 owned->notifications.canceled++;
43 drop_after_notifications(owned);
46/* Use the same pipe endpoint handed to wl_data_source.send. A reader can
47 * disappear after a partial write, or leave a full pipe unread. Neither may
48 * kill the process or retain a transfer past its deadline. */
49static void clipboard_failed_peers(Wl *parent)
51 assert(!parent->clipboard);
52 parent->clipboard_size = 131072;
53 parent->clipboard = malloc(parent->clipboard_size);
54 assert(parent->clipboard);
55 memset(parent->clipboard, 'x', parent->clipboard_size);
56 int pipes[2];
57 assert(pipe(pipes) == 0);
58 assert(fcntl(pipes[1], F_SETPIPE_SZ, 4096) >= 0);
59 source_send(parent, NULL, "text/plain;charset=utf-8", pipes[1]);
60 ClipboardWrite *transfer = &parent->writes[0];
61 assert(transfer->bytes && transfer->offset > 0 && transfer->offset < transfer->size);
62 close(pipes[0]);
63 clipboard_drain(parent);
64 assert(!transfer->bytes);
65 assert(fcntl(pipes[1], F_GETFD) == -1 && errno == EBADF);
67 assert(pipe(pipes) == 0);
68 assert(fcntl(pipes[1], F_SETPIPE_SZ, 4096) >= 0);
69 source_send(parent, NULL, "text/plain", pipes[1]);
70 assert(transfer->bytes && transfer->offset > 0 && transfer->offset < transfer->size);
71 int64_t started = now_ms();
72 while (transfer->bytes && now_ms() - started < 2500) {
73 clipboard_drain(parent);
74 usleep(1000);
75 }
76 assert(!transfer->bytes && now_ms() - started >= 1900 && now_ms() - started < 2500);
77 assert(fcntl(pipes[1], F_GETFD) == -1 && errno == EBADF);
78 close(pipes[0]);
79 free(parent->clipboard);
80 parent->clipboard = NULL;
81 parent->clipboard_size = 0;
84#ifndef SUI_TEST_BASELINE
85/* Older implementations used as fault controls lack these additive APIs. */
86static void embedding_state(Wl *parent)
88 /* Configure completes independently of keyboard setup. Direct key
89 * callbacks below require the compositor's keymap to have arrived. */
90 int64_t deadline = now_ms() + 2000;
91 while (!parent->keys && now_ms() < deadline) {
92 parent->head = parent->count = 0;
93 assert(dispatch(parent, 10) >= 0);
94 }
95 assert(parent->keys);
96 Wl *child = sui_wayland_child(parent, 0, 0, 16, 16, 0);
97 assert(child);
98 assert(!sui_wayland_geometry(parent, 0, 0, 32, 16));
99 assert(!sui_wayland_geometry(child, 0, 0, 0, 16));
100 assert(sui_wayland_geometry(child, 8, 12, 32, 16));
101 int width, height, scale;
102 assert(sui_wayland_size(child, &width, &height, &scale));
103 assert(width == 32 && height == 16 && scale == parent->scale);
104 uint32_t previous[4], masks[4];
105 sui_wayland_modifiers(parent, previous);
106 kb_modifiers(parent, NULL, 0, 1, 2, 4, 3);
107 sui_wayland_modifiers(child, masks);
108 assert(masks[0] == 1 && masks[1] == 2 && masks[2] == 4 && masks[3] == 3);
109 kb_modifiers(parent, NULL, 0, previous[0], previous[1], previous[2], previous[3]);
110 /* Real protocol order: modifier keys carry the preceding state. An
111 * ordered modifiers event must bridge that gap before a pointer action. */
112 kb_modifiers(parent, NULL, 0, 0, 0, 0, 0);
113 parent->head = parent->count = 0;
114 kb_key(parent, NULL, 0, 0, 29, WL_KEYBOARD_KEY_STATE_PRESSED);
115 kb_modifiers(parent, NULL, 0, 4, 0, 0, 0);
116 ptr_button(parent, NULL, 0, 0, 272, WL_POINTER_BUTTON_STATE_PRESSED);
117 kb_key(parent, NULL, 0, 0, 29, WL_KEYBOARD_KEY_STATE_RELEASED);
118 kb_modifiers(parent, NULL, 0, 0, 0, 0, 0);
119 ptr_button(parent, NULL, 0, 0, 272, WL_POINTER_BUTTON_STATE_RELEASED);
120 assert(parent->count == 6);
121 assert(parent->queue[0].type == UI_KEY && parent->queue[0].v[2] == 0);
122 assert(parent->queue[1].type == UI_MODIFIERS && parent->queue[1].v[0] == MOD_CONTROL);
123 assert(parent->queue[2].type == UI_BUTTON);
124 assert(parent->queue[3].type == UI_KEY && parent->queue[3].v[2] == MOD_CONTROL);
125 assert(parent->queue[4].type == UI_MODIFIERS && parent->queue[4].v[0] == 0);
126 assert(parent->queue[5].type == UI_BUTTON);
127 kb_modifiers(parent, NULL, 0, previous[0], previous[1], previous[2], previous[3]);
128 uint64_t generation = sui_wayland_clipboard_generation(child, NULL);
129 device_selection(parent, NULL, NULL);
130 int has_text = 1;
131 assert(sui_wayland_clipboard_generation(child, &has_text) == generation + 1 && !has_text);
132 sui_wayland_close(child);
133 assert(!sui_wayland_geometry(child, 0, 0, 32, 16));
134 sui_wayland_unref(child);
136#endif
138int main(int argc, char **argv)
140 struct rlimit no_core = {0, 0};
141 assert(setrlimit(RLIMIT_CORE, &no_core) == 0);
142 SigilVM *vm = sigil_vm_create();
143 assert(vm);
144 sigil__register_sigil_ui_wayland(vm);
145 sigil_eval_string(vm,
146 "(import (sigil ui wayland)) "
147 "(define bridge-parent (%wl-open #f 0 \"Native bridge\" \"sigil-ui-native-test\" #f 640 480 #f))");
148 if (sigil_error_message(vm)) {
149 fprintf(stderr, "%s\n", sigil_error_message(vm));
150 return 1;
151 }
152 Value handle = sigil_lookup_binding(vm, sigil_intern_symbol(vm, "bridge-parent", 13));
153 Wl *parent = sui_wayland_acquire(vm, handle);
154 assert(parent);
155 int width = device_w(parent), height = device_h(parent);
156 Value pixels = sigil_make_bytevector(vm, (size_t)width * height * 4);
157 Value arguments[] = { handle, pixels, sigil_fixnum(width), SIGIL_EMPTY };
158 assert(sigil_is_true(native_wl_present(vm, 4, arguments)));
159 int64_t deadline = now_ms() + 2000;
160 while (parent->frame && now_ms() < deadline) {
161 parent->head = parent->count = 0;
162 assert(dispatch(parent, 10) >= 0);
163 }
164 assert(!parent->frame);
165 if (argc > 1) {
166 int drop_reference = !strcmp(argv[1], "--failed-child-unref");
167 int failed_child = drop_reference || !strcmp(argv[1], "--failed-child");
168 assert(failed_child || !strcmp(argv[1], "--failed-parent"));
169 OwnedNotifications owned = {0};
170 owned.child = sui_wayland_child(parent, 0, 0, 64, 64, 1);
171 assert(owned.child);
172 uint8_t image[64 * 64 * 4];
173 memset(image, 255, sizeof image);
174 assert(sui_wayland_present_shm(owned.child, image, 64, 64, 64 * 4,
175 drop_reference ? test_release : owned_release,
176 drop_reference ? test_frame : owned_frame,
177 drop_reference ? (void *)&owned.notifications : (void *)&owned));
178 /* Break this private client's socket, then reach the production
179 * failure path through dispatch. Failure is distinct from cleanup. */
180 assert(shutdown(wl_display_get_fd(parent->display), SHUT_RDWR) == 0);
181 Wl *failed = failed_child ? owned.child : parent;
182 failed->head = failed->count = 0;
183 assert(dispatch(failed, 0) == -1);
184 assert(failed->dead && failed->display);
185 if (drop_reference) {
186 sui_wayland_unref(owned.child);
187 owned.child = NULL;
188 assert(owned.notifications.released == 1 && owned.notifications.canceled == 1);
189 assert(!parent->children);
190 }
191 sui_wayland_close(parent);
192 assert(!parent->display && !parent->children);
193 assert(!owned.child);
194 assert(owned.notifications.released == 1 && owned.notifications.canceled == 1);
195 sui_wayland_close(parent);
196 sui_wayland_unref(parent);
197 sigil_vm_destroy(vm);
198 puts(drop_reference ? "PASS: final unref of a failed child still completes cleanup"
199 : failed_child ? "PASS: failed child connection still tears down under parent close"
200 : "PASS: failed parent connection still cancels and releases its child");
201 return 0;
202 }
203 Wl *child = sui_wayland_child(parent, 0, 0, 64, 64, 1);
204 assert(child);
205 uint8_t image[64 * 64 * 4];
206 memset(image, 255, sizeof image);
207 Notifications first = {0}, second = {0}, third = {0}, refused = {0};
208 assert(sui_wayland_present_shm(child, image, 64, 64, 64 * 4, test_release, test_frame, &first));
209 assert(!first.released && !first.rendered && !first.canceled);
210 assert(!sui_wayland_present_shm(child, image, 64, 64, 64 * 4, test_release, test_frame, &refused));
211 deadline = now_ms() + 2000;
212 while (!first.rendered && now_ms() < deadline) {
213 parent->head = parent->count = 0;
214 assert(dispatch(parent, 10) >= 0);
215 }
216 assert(first.rendered == 1 && !first.canceled);
217 assert(sui_wayland_present_shm(child, image, 64, 64, 64 * 4, test_release, test_frame, &second));
218 /* The compositor may retain the current buffer until a replacement. */
219 deadline = now_ms() + 2000;
220 while ((!second.rendered || !first.released) && now_ms() < deadline) {
221 parent->head = parent->count = 0;
222 assert(dispatch(parent, 10) >= 0);
223 }
224 assert(first.released == 1 && second.rendered == 1);
225 assert(sui_wayland_present_shm(child, image, 64, 64, 64 * 4, test_release, test_frame, &third));
226 /* Closing before dispatch must cancel the third frame, never present it. */
227 sui_wayland_close(child);
228 assert(first.released == 1 && first.rendered == 1 && !first.canceled);
229 assert(second.released == 1 && second.rendered == 1 && !second.canceled);
230 assert(third.released == 1 && third.canceled == 1 && !third.rendered);
231 assert(!refused.released && !refused.rendered && !refused.canceled);
232 sui_wayland_close(child);
233 assert(third.released == 1 && third.canceled == 1);
234 assert(!sui_wayland_present_shm(child, image, 64, 64, 64 * 4, test_release, test_frame, &refused));
235 sui_wayland_unref(child);
236 /* Final unref without an explicit close also completes teardown. */
237 child = sui_wayland_child(parent, 0, 0, 64, 64, 1);
238 assert(child);
239 Notifications last_reference = {0};
240 assert(sui_wayland_present_shm(child, image, 64, 64, 64 * 4,
241 test_release, test_frame, &last_reference));
242 sui_wayland_unref(child);
243 assert(last_reference.released == 1 && last_reference.canceled == 1);
244 assert(!parent->children);
245#ifndef SUI_TEST_BASELINE
246 embedding_state(parent);
247#endif
248 clipboard_failed_peers(parent);
249 /* The notification context owns the sole child reference. Closing the
250 * parent must survive the final callback dropping it during teardown. */
251 OwnedNotifications owned = {0};
252 owned.child = sui_wayland_child(parent, 0, 0, 64, 64, 1);
253 assert(owned.child);
254 assert(sui_wayland_present_shm(owned.child, image, 64, 64, 64 * 4,
255 owned_release, owned_frame, &owned));
256 sui_wayland_close(parent);
257 assert(!owned.child);
258 assert(owned.notifications.released == 1 && owned.notifications.canceled == 1);
259 assert(!parent->children);
260 sui_wayland_close(parent);
261 sui_wayland_unref(parent);
262 sigil_vm_destroy(vm);
263 puts("PASS: native bridge callbacks, callback-owned teardown and clipboard failed peers");
264 return 0;