-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-wlwp.c
More file actions
397 lines (350 loc) · 15.1 KB
/
Copy pathmain-wlwp.c
File metadata and controls
397 lines (350 loc) · 15.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
// Wayland layer-shell front-end -> bin/goo-wlwp. Runs the sim as a desktop
// wallpaper: a wl_surface pinned to the BACKGROUND layer via wlr-layer-shell,
// covering the output, with no keyboard/pointer input. On SIGINT/SIGTERM it tears
// the surface down cleanly, so whatever drew the wallpaper before (swaybg, the
// compositor's own bg, ...) reappears -- no save/restore needed.
//
// Works on any wlroots compositor (sway, hyprland, river, wayfire, ...) and KDE.
// NOT gnome/mutter, which refuses layer-shell. There is no portable wallpaper
// protocol; layer-shell is the broadest one.
// NOTE: EGL must be included before gl.h (sim.h -> buffer.h pulls in glad's gl.h,
// which bundles its own KHR/khrplatform; including EGL afterwards breaks EGLAPIENTRYP).
#include <EGL/egl.h>
#include <wayland-client.h>
#include <wayland-egl.h>
#include "sim.h"
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gl.h> // glad declarations (impl emitted by sim.c)
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h" // layer-shell pulls in xdg types
// ---- wayland / egl state ----
static struct wl_display *display;
static struct wl_compositor *compositor;
static struct zwlr_layer_shell_v1 *layer_shell;
static struct wl_output *output; // first output the compositor advertises
static struct wl_seat *seat;
static struct wl_pointer *pointer;
static struct wl_surface *surface;
static struct zwlr_layer_surface_v1 *layer_surface;
static struct wl_egl_window *egl_window;
static EGLDisplay egl_display = EGL_NO_DISPLAY;
static EGLContext egl_context = EGL_NO_CONTEXT;
static EGLSurface egl_surface = EGL_NO_SURFACE;
static int surf_w = 0, surf_h = 0; // current layer-surface size (logical px)
static bool configured = false; // got the first layer-surface configure
static bool dirty = false; // size changed since last applied -> resize on next frame
static volatile sig_atomic_t running = 1;
// Pointer state (surface-local logical px). The background layer only gets pointer
// events where nothing above it (window / panel) has the cursor -- i.e. over the
// bare desktop -- which is exactly when reacting to the mouse makes sense.
static double mouse_x = 0, mouse_y = 0;
static bool mouse_in = false;
static void on_signal(int sig) {
(void)sig;
running = 0;
}
// ---- layer surface ----
static void ls_configure(void *data, struct zwlr_layer_surface_v1 *ls, uint32_t serial, uint32_t w, uint32_t h) {
(void)data;
zwlr_layer_surface_v1_ack_configure(ls, serial);
if (w > 0 && h > 0 && ((int)w != surf_w || (int)h != surf_h)) {
surf_w = (int)w;
surf_h = (int)h;
dirty = true;
}
configured = true;
}
static void ls_closed(void *data, struct zwlr_layer_surface_v1 *ls) {
(void)data;
(void)ls;
running = 0; // compositor pulled the surface (output gone, etc.) -- exit cleanly
}
static const struct zwlr_layer_surface_v1_listener ls_listener = {
.configure = ls_configure,
.closed = ls_closed,
};
// ---- pointer (bound at wl_pointer v1: enter/leave/motion/button/axis) ----
static void ptr_enter(void *d, struct wl_pointer *p, uint32_t serial, struct wl_surface *s, wl_fixed_t x, wl_fixed_t y) {
(void)d;
(void)p;
(void)serial;
(void)s;
mouse_x = wl_fixed_to_double(x);
mouse_y = wl_fixed_to_double(y);
mouse_in = true;
}
static void ptr_leave(void *d, struct wl_pointer *p, uint32_t serial, struct wl_surface *s) {
(void)d;
(void)p;
(void)serial;
(void)s;
mouse_in = false;
}
static void ptr_motion(void *d, struct wl_pointer *p, uint32_t t, wl_fixed_t x, wl_fixed_t y) {
(void)d;
(void)p;
(void)t;
mouse_x = wl_fixed_to_double(x);
mouse_y = wl_fixed_to_double(y);
}
static void ptr_button(void *d, struct wl_pointer *p, uint32_t serial, uint32_t t, uint32_t b, uint32_t st) {
(void)d;
(void)p;
(void)serial;
(void)t;
(void)b;
(void)st;
}
static void ptr_axis(void *d, struct wl_pointer *p, uint32_t t, uint32_t axis, wl_fixed_t v) {
(void)d;
(void)p;
(void)t;
(void)axis;
(void)v;
}
static const struct wl_pointer_listener ptr_listener = {
.enter = ptr_enter,
.leave = ptr_leave,
.motion = ptr_motion,
.button = ptr_button,
.axis = ptr_axis,
};
// ---- seat ----
static void seat_caps(void *d, struct wl_seat *s, uint32_t caps) {
(void)d;
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !pointer) {
pointer = wl_seat_get_pointer(s);
wl_pointer_add_listener(pointer, &ptr_listener, NULL);
}
}
static void seat_name(void *d, struct wl_seat *s, const char *name) {
(void)d;
(void)s;
(void)name;
}
static const struct wl_seat_listener seat_listener = {
.capabilities = seat_caps,
.name = seat_name,
};
// ---- registry ----
static void reg_global(void *data, struct wl_registry *reg, uint32_t name, const char *iface, uint32_t ver) {
(void)data;
if (strcmp(iface, wl_compositor_interface.name) == 0) {
compositor = wl_registry_bind(reg, name, &wl_compositor_interface, ver < 4 ? ver : 4);
} else if (strcmp(iface, zwlr_layer_shell_v1_interface.name) == 0) {
layer_shell = wl_registry_bind(reg, name, &zwlr_layer_shell_v1_interface, ver < 4 ? ver : 4);
} else if (strcmp(iface, wl_output_interface.name) == 0 && output == NULL) {
// First output only. NOTE: single-monitor -- multi-output would need one
// surface per output; add when someone runs goo-wlwp on a multi-head setup.
output = wl_registry_bind(reg, name, &wl_output_interface, ver < 2 ? ver : 2);
} else if (strcmp(iface, wl_seat_interface.name) == 0 && seat == NULL) {
// Bind at v1 so wl_pointer is v1 -- matches the 5-event listener above (no
// frame/axis_* members to keep in sync with newer pointer versions).
seat = wl_registry_bind(reg, name, &wl_seat_interface, 1);
wl_seat_add_listener(seat, &seat_listener, NULL);
}
}
static void reg_global_remove(void *data, struct wl_registry *reg, uint32_t name) {
(void)data;
(void)reg;
(void)name;
}
static const struct wl_registry_listener reg_listener = {
.global = reg_global,
.global_remove = reg_global_remove,
};
static void die(const char *msg) {
fprintf(stderr, "goo-wlwp: %s\n", msg);
exit(EXIT_FAILURE);
}
// Stand up the EGL desktop-GL context on the wayland surface and load glad.
static void egl_setup(void) {
egl_display = eglGetDisplay((EGLNativeDisplayType)display);
if (egl_display == EGL_NO_DISPLAY)
die("eglGetDisplay failed");
if (!eglInitialize(egl_display, NULL, NULL))
die("eglInitialize failed");
if (!eglBindAPI(EGL_OPENGL_API)) // desktop GL (shaders are #version 330 core), not GLES
die("eglBindAPI(OPENGL) failed");
const EGLint cfg_attr[] = {
EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_BIT,
EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_ALPHA_SIZE,
8,
EGL_NONE,
};
EGLConfig config;
EGLint n = 0;
if (!eglChooseConfig(egl_display, cfg_attr, &config, 1, &n) || n == 0)
die("no matching EGL config (desktop GL)");
const EGLint ctx_attr[] = {
EGL_CONTEXT_MAJOR_VERSION,
3,
EGL_CONTEXT_MINOR_VERSION,
3,
EGL_CONTEXT_OPENGL_PROFILE_MASK,
EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
EGL_NONE,
};
egl_context = eglCreateContext(egl_display, config, EGL_NO_CONTEXT, ctx_attr);
if (egl_context == EGL_NO_CONTEXT)
die("eglCreateContext failed (no 3.3 core?)");
egl_window = wl_egl_window_create(surface, surf_w, surf_h);
if (!egl_window)
die("wl_egl_window_create failed");
egl_surface = eglCreateWindowSurface(egl_display, config, (EGLNativeWindowType)egl_window, NULL);
if (egl_surface == EGL_NO_SURFACE)
die("eglCreateWindowSurface failed");
if (!eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context))
die("eglMakeCurrent failed");
eglSwapInterval(egl_display, vsync ? 1 : 0);
if (!gladLoadGL((GLADloadfunc)eglGetProcAddress))
die("gladLoadGL failed");
// Context-level GL state. Lives here (not in main) so it's re-established when
// the context is recreated by gl_refresh.
}
// Tear down the EGL context + surface (frees the freedreno per-submit leak). Shared
// by gl_refresh and the final exit cleanup.
static void egl_teardown(void) {
eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (egl_surface != EGL_NO_SURFACE)
eglDestroySurface(egl_display, egl_surface);
if (egl_window)
wl_egl_window_destroy(egl_window);
if (egl_context != EGL_NO_CONTEXT)
eglDestroyContext(egl_display, egl_context);
}
// Bring up the GL context and the sim together. snap == NULL generates a fresh
// field (first boot); a snapshot continues an existing sim across a context
// recreate. The single bring-up path for both initial setup and gl_refresh.
static void bringup(const SimSnapshot *snap) {
egl_setup(); // context + surface, glad loaded, GL state enabled
if (snap)
sim_restore(snap); // consumed by the sim_setup below
sim_setup();
}
// Recreate the EGL context to reclaim the freedreno per-submit leak (see debug/).
// The whole sim lives in GL objects tied to the context, so snapshot the sim state,
// tear the context down (which frees the leaked allocations), then bring it back up
// from the snapshot. Costs one frame's hitch; the sim continues uninterrupted.
static void gl_refresh(void) {
SimSnapshot *snap = sim_snapshot(); // read sim state back while the old context lives
egl_teardown();
sim_teardown_cpu(); // free CPU scratch so the bring-up below doesn't leak it
bringup(snap);
sim_snapshot_free(snap);
}
int main(int argc, char **argv) {
parse_args(argc, argv, true, false);
display = wl_display_connect(NULL);
if (!display)
die("cannot connect to a wayland display (is WAYLAND_DISPLAY set?)");
struct wl_registry *registry = wl_display_get_registry(display);
wl_registry_add_listener(registry, ®_listener, NULL);
wl_display_roundtrip(display); // populate globals
if (!compositor)
die("compositor missing wl_compositor");
if (!layer_shell)
die("compositor has no wlr-layer-shell -- wallpaper mode needs a wlroots/KDE compositor (not gnome)");
surface = wl_compositor_create_surface(compositor);
layer_surface = zwlr_layer_shell_v1_get_layer_surface(
layer_shell, surface, output, ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND, "goo");
zwlr_layer_surface_v1_add_listener(layer_surface, &ls_listener, NULL);
// Cover the whole output: anchor all 4 edges, size 0,0 (compositor fills in the
// output size), exclusive zone -1 (ignore other panels' reservations), no input.
zwlr_layer_surface_v1_set_anchor(layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP | ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT);
zwlr_layer_surface_v1_set_size(layer_surface, 0, 0);
zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, -1);
zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE);
wl_surface_commit(surface);
// Block until the first configure so we know the output size before creating the
// egl window. dispatch (not roundtrip) so the configure callback actually runs.
while (!configured && wl_display_dispatch(display) != -1)
;
if (surf_w <= 0 || surf_h <= 0)
die("layer surface configured with no size");
dirty = false;
// Wayland gives logical px; treat the framebuffer as logical (output scale 1).
// NOTE: fractional/hidpi output scale ignored -- on a 2x output the wallpaper
// renders at logical res and the compositor upscales. wire wl_output scale +
// wp_fractional_scale here if it looks soft on hidpi.
sim_set_dims(surf_w, surf_h, surf_w, surf_h);
sim_install_signal_handlers();
bringup(NULL); // first boot: fresh field. same path gl_refresh uses with a snapshot.
signal(SIGINT, on_signal);
signal(SIGTERM, on_signal);
// --core.gl-refresh: periodic GL-context recreation to bound the freedreno leak.
double last_refresh = get_time();
// Mouse repel: track the pointer in sim space and feed its per-frame velocity,
// same as the windowed front-end. Parked sentinel ({-1e9,..}) when the cursor is
// off the wallpaper (over a window) or --core.no-mouse, so the repel never fires.
float prev_mouse[2] = {-1e9f, -1e9f};
int epoch_counter = 0;
while (running) {
if (max_iterations > 0 && epoch_counter >= max_iterations)
break;
// Pump wayland: flush our outgoing requests, then READ the socket (not just
// dispatch already-queued events) so configure/pointer/release events arrive.
// Non-blocking: poll with timeout 0 and only read when there's data ready.
wl_display_dispatch_pending(display);
wl_display_flush(display);
struct pollfd pfd = {.fd = wl_display_get_fd(display), .events = POLLIN};
if (poll(&pfd, 1, 0) > 0 && (pfd.revents & POLLIN)) {
if (wl_display_dispatch(display) == -1)
break;
}
// Periodically recreate the GL context to reclaim the freedreno per-submit
// leak (see debug/). Skipped (gl_refresh_seconds == 0) by default.
if (gl_refresh_seconds > 0 && !sim_transitioning() && get_time() - last_refresh >= gl_refresh_seconds) {
gl_refresh();
last_refresh = get_time();
}
if (dirty) {
wl_egl_window_resize(egl_window, surf_w, surf_h, 0, 0);
sim_resize(surf_w, surf_h, surf_w, surf_h);
dirty = false;
}
float mouse_position[2] = {-1e9f, -1e9f};
float mouse_velocity[2] = {0.0f, 0.0f};
if (!no_mouse && mouse_in) {
mouse_position[0] = (float)mouse_x * mouse_scale;
mouse_position[1] = (float)mouse_y * mouse_scale;
// velocity only if the previous frame was also on the wallpaper -- else the
// delta is the teleport from the parked sentinel (re-entry jump).
if (prev_mouse[0] > -1e8f) {
mouse_velocity[0] = mouse_position[0] - prev_mouse[0];
mouse_velocity[1] = mouse_position[1] - prev_mouse[1];
}
}
prev_mouse[0] = mouse_position[0];
prev_mouse[1] = mouse_position[1];
sim_step(epoch_counter, mouse_position, mouse_velocity);
sim_present();
// eglSwapBuffers commits the wl_surface; vsync (swap interval) paces the loop.
eglSwapBuffers(egl_display, egl_surface);
epoch_counter++;
}
// Clean teardown -> compositor repaints whatever wallpaper was underneath.
glBindVertexArray(0);
sim_teardown_gl();
egl_teardown();
eglTerminate(egl_display);
zwlr_layer_surface_v1_destroy(layer_surface);
wl_surface_destroy(surface);
wl_display_disconnect(display);
return 0;
}