AtlatestRepositorysigil-ui

sigil-ui / tree / test / waylandconfigure-server.c

1/* Protocol-only compositor fixture: no renderer, input, audio, or GPU access. */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdint.h>
6#include <time.h>
7#include <wayland-server.h>
8#include "configure-xdg-server.h"
9
10static struct wl_display *display;
11static struct wl_resource *shell_surface, *toplevel;
12static struct wl_event_source *timer;
13static const char *mode;
14static int committed, syncs, connected, live_resources;
15static struct wl_resource *wm;
16static struct wl_listener created_listener, destroyed_listener;
17static int64_t traffic_start;
18static int64_t milliseconds(void)
20 struct timespec now;
21 clock_gettime(CLOCK_MONOTONIC, &now);
22 return (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000;
24static void resource_gone(struct wl_resource *resource) { live_resources--; }
25static void cleanup_receipt(void *data)
27 char path[512];
28 snprintf(path, sizeof path, "%s/cleanup", getenv("XDG_RUNTIME_DIR"));
29 FILE *receipt = fopen(path, "w");
30 if (receipt) { fprintf(receipt, "%d\n", live_resources); fclose(receipt); }
32static void client_destroyed(struct wl_listener *listener, void *data)
34 connected = 0;
35 if (timer) wl_event_source_timer_update(timer, 0);
36 wl_event_loop_add_idle(wl_display_get_event_loop(display), cleanup_receipt, NULL);
38static void client_created(struct wl_listener *listener, void *data)
40 connected = 1;
41 destroyed_listener.notify = client_destroyed;
42 wl_client_add_destroy_listener(data, &destroyed_listener);
44static int send_traffic(void *data)
46 if (!connected) return 0;
47 if (milliseconds() - traffic_start >= 7000) {
48 /* Bound a deliberately broken reset-per-loop client too. */
49 xdg_toplevel_send_close(toplevel);
50 } else {
51 xdg_wm_base_send_ping(wm, wl_display_next_serial(display));
52 wl_event_source_timer_update(timer, 100);
53 }
54 wl_display_flush_clients(display);
55 return 0;
58static void destroy_resource(struct wl_client *client, struct wl_resource *resource)
59{ wl_resource_destroy(resource); }
60static void ignore_string(struct wl_client *client, struct wl_resource *resource, const char *value) {}
61static void set_title(struct wl_client *client, struct wl_resource *resource, const char *value)
62{ fprintf(stderr, "title: %s\n", value); }
63static void ignore_serial(struct wl_client *client, struct wl_resource *resource, uint32_t serial) {}
65static void fullscreen_reply(int enabled)
67 fprintf(stderr, "fullscreen request: %d\n", enabled);
68 if (!strcmp(mode, "services-disconnect")) {
69 wl_client_destroy(wl_resource_get_client(toplevel));
70 return;
71 }
72 if (!strcmp(mode, "services-silent")) return;
73 struct wl_array states;
74 wl_array_init(&states);
75 if (!strcmp(mode, "services-refuse")) enabled = !enabled;
76 if (enabled) {
77 uint32_t *state = wl_array_add(&states, sizeof *state);
78 *state = XDG_TOPLEVEL_STATE_FULLSCREEN;
79 }
80 xdg_toplevel_send_configure(toplevel, 640, 480, &states);
81 xdg_surface_send_configure(shell_surface, wl_display_next_serial(display));
82 wl_array_release(&states);
83 wl_display_flush_clients(display);
85static void set_fullscreen(struct wl_client *client, struct wl_resource *resource, struct wl_resource *output)
86{ fullscreen_reply(1); }
87static void unset_fullscreen(struct wl_client *client, struct wl_resource *resource)
88{ fullscreen_reply(0); }
90static int send_initial(void *data)
92 fprintf(stderr, "initial action %s after %d post-commit sync replies\n", mode, syncs);
93 if (!strcmp(mode, "disconnect")) {
94 wl_client_destroy(wl_resource_get_client(shell_surface));
95 return 0;
96 }
97 if (!strcmp(mode, "close") || !strcmp(mode, "close-then-configure")) {
98 xdg_toplevel_send_close(toplevel);
99 }
100 if (strcmp(mode, "close")) {
101 struct wl_array states;
102 wl_array_init(&states);
103 xdg_toplevel_send_configure(toplevel, 640, 480, &states);
104 xdg_surface_send_configure(shell_surface, wl_display_next_serial(display));
105 wl_array_release(&states);
106 }
107 wl_display_flush_clients(display);
108 return 0;
110static void commit_surface(struct wl_client *client, struct wl_resource *surface)
112 if (committed++) return;
113 fprintf(stderr, "surface committed\n");
114 if (!strcmp(mode, "traffic")) {
115 traffic_start = milliseconds();
116 timer = wl_event_loop_add_timer(wl_display_get_event_loop(display), send_traffic, NULL);
117 wl_event_source_timer_update(timer, 10);
118 } else if (strcmp(mode, "never")) {
119 timer = wl_event_loop_add_timer(wl_display_get_event_loop(display), send_initial, NULL);
120 wl_event_source_timer_update(timer, !strcmp(mode, "delay") ? 500 : 50);
121 }
123static const struct wl_surface_interface surface_impl = {
124 .destroy = destroy_resource, .commit = commit_surface
125};
126static void create_surface(struct wl_client *client, struct wl_resource *resource, uint32_t id)
128 struct wl_resource *surface = wl_resource_create(client, &wl_surface_interface, 4, id);
129 live_resources++;
130 wl_resource_set_implementation(surface, &surface_impl, NULL, resource_gone);
132static const struct wl_compositor_interface compositor_impl = { .create_surface = create_surface };
133static void bind_compositor(struct wl_client *client, void *data, uint32_t version, uint32_t id)
135 struct wl_resource *resource = wl_resource_create(client, &wl_compositor_interface, 4, id);
136 live_resources++;
137 wl_resource_set_implementation(resource, &compositor_impl, NULL, resource_gone);
139static const struct xdg_toplevel_interface toplevel_impl = {
140 .destroy = destroy_resource, .set_title = set_title, .set_app_id = ignore_string,
141 .set_fullscreen = set_fullscreen, .unset_fullscreen = unset_fullscreen
142};
143static void get_toplevel(struct wl_client *client, struct wl_resource *resource, uint32_t id)
145 toplevel = wl_resource_create(client, &xdg_toplevel_interface, 1, id);
146 live_resources++;
147 wl_resource_set_implementation(toplevel, &toplevel_impl, NULL, resource_gone);
149static const struct xdg_surface_interface shell_surface_impl = {
150 .destroy = destroy_resource, .get_toplevel = get_toplevel, .ack_configure = ignore_serial
151};
152static void get_shell_surface(struct wl_client *client, struct wl_resource *resource,
153 uint32_t id, struct wl_resource *surface)
155 shell_surface = wl_resource_create(client, &xdg_surface_interface, 1, id);
156 live_resources++;
157 wl_resource_set_implementation(shell_surface, &shell_surface_impl, NULL, resource_gone);
159static const struct xdg_wm_base_interface wm_impl = {
160 .destroy = destroy_resource, .get_xdg_surface = get_shell_surface, .pong = ignore_serial
161};
162static void bind_wm(struct wl_client *client, void *data, uint32_t version, uint32_t id)
164 struct wl_resource *resource = wl_resource_create(client, &xdg_wm_base_interface, 1, id);
165 live_resources++;
166 wl_resource_set_implementation(resource, &wm_impl, NULL, resource_gone);
167 wm = resource;
169static void log_protocol(void *data, enum wl_protocol_logger_type direction,
170 const struct wl_protocol_logger_message *message)
172 if (committed && direction == WL_PROTOCOL_LOGGER_REQUEST &&
173 !strcmp(message->message->name, "sync")) {
174 fprintf(stderr, "post-commit sync %d\n", ++syncs);
175 }
177int main(int argc, char **argv)
179 if (argc != 2) return 2;
180 mode = argv[1];
181 setvbuf(stderr, NULL, _IONBF, 0);
182 display = wl_display_create();
183 if (!display || wl_display_init_shm(display) < 0) return 3;
184 wl_global_create(display, &wl_compositor_interface, 4, NULL, bind_compositor);
185 wl_global_create(display, &xdg_wm_base_interface, 1, NULL, bind_wm);
186 wl_display_add_protocol_logger(display, log_protocol, NULL);
187 created_listener.notify = client_created;
188 wl_display_add_client_created_listener(display, &created_listener);
189 if (wl_display_add_socket(display, "configure-test") < 0) return 4;
190 printf("READY\n");
191 fflush(stdout);
192 wl_display_run(display);
193 wl_display_destroy(display);
194 return 0;