Codebergdnkl
foot
master
1#include "wayland.h"
3#include <ctype.h>
4#include <errno.h>
5#include <fcntl.h>
6#include <locale.h>
7#include <poll.h>
8#include <stdlib.h>
9#include <string.h>
10#include <unistd.h>
12#include <sys/timerfd.h>
13#include <sys/epoll.h>
15#include <cursor-shape-v1.h>
16#include <wayland-client.h>
17#include <wayland-cursor.h>
18#include <xkbcommon/xkbcommon.h>
19#include <xkbcommon/xkbcommon-keysyms.h>
20#include <xkbcommon/xkbcommon-compose.h>
22#include <tllist.h>
24#define LOG_MODULE "wayland"
25#define LOG_ENABLE_DBG 0
26#include "log.h"
28#include "config.h"
29#include "terminal.h"
30#include "ime.h"
31#include "input.h"
32#include "render.h"
33#include "selection.h"
34#include "shm.h"
35#include "shm-formats.h"
36#include "util.h"
37#include "xmalloc.h"
39static void
40csd_reload_font(struct wl_window *win, float old_scale)
41{
42 struct terminal *term = win->term;
43 const struct config *conf = term->conf;
45 const float scale = term->scale;
47 bool enable_csd = win->csd_mode == CSD_YES && !win->is_fullscreen;
48 if (!enable_csd)
49 return;
50 if (win->csd.font != NULL && scale == old_scale)
51 return;
53 fcft_destroy(win->csd.font);
55 const char *patterns[conf->csd.font.count];
56 for (size_t i = 0; i < conf->csd.font.count; i++)
57 patterns[i] = conf->csd.font.arr[i].pattern;
59 char pixelsize[32];
60 snprintf(pixelsize, sizeof(pixelsize), "pixelsize=%u",
61 (int)roundf(conf->csd.title_height * scale * 1 / 2));
63 LOG_DBG("loading CSD font \"%s:%s\" (old-scale=%.2f, scale=%.2f)",
64 patterns[0], pixelsize, old_scale, scale);
66 win->csd.font = fcft_from_name(conf->csd.font.count, patterns, pixelsize);
67}
69static void
70csd_instantiate(struct wl_window *win)
71{
72 struct wayland *wayl = win->term->wl;
73 xassert(wayl != NULL);
75 for (size_t i = 0; i < CSD_SURF_MINIMIZE; i++) {
76 bool ret = wayl_win_subsurface_new(win, &win->csd.surface[i], true);
77 xassert(ret);
78 }
80 for (size_t i = CSD_SURF_MINIMIZE; i < CSD_SURF_COUNT; i++) {
81 bool ret = wayl_win_subsurface_new_with_custom_parent(
82 win, win->csd.surface[CSD_SURF_TITLE].surface.surf, &win->csd.surface[i],
83 true);
84 xassert(ret);
85 }
87 csd_reload_font(win, -1.);
88}
90static void
91csd_destroy(struct wl_window *win)
92{
93 struct terminal *term = win->term;
95 fcft_destroy(term->window->csd.font);
96 term->window->csd.font = NULL;
98 for (size_t i = 0; i < ALEN(win->csd.surface); i++)
99 wayl_win_subsurface_destroy(&win->csd.surface[i]);
100 shm_purge(term->render.chains.csd);
101}
103static void
104seat_add_data_device(struct seat *seat)
105{
106 if (seat->wayl->data_device_manager == NULL)
107 return;
109 if (seat->data_device != NULL) {
110 /* TODO: destroy old device + clipboard data? */
111 return;
112 }
114 struct wl_data_device *data_device = wl_data_device_manager_get_data_device(
115 seat->wayl->data_device_manager, seat->wl_seat);
117 if (data_device == NULL)
118 return;
120 seat->data_device = data_device;
121 wl_data_device_add_listener(data_device, &data_device_listener, seat);
122}
124static void
125seat_add_primary_selection(struct seat *seat)
126{
127 if (seat->wayl->primary_selection_device_manager == NULL)
128 return;
130 if (seat->primary_selection_device != NULL)
131 return;
133 struct zwp_primary_selection_device_v1 *primary_selection_device
134 = zwp_primary_selection_device_manager_v1_get_device(
135 seat->wayl->primary_selection_device_manager, seat->wl_seat);
137 if (primary_selection_device == NULL)
138 return;
140 seat->primary_selection_device = primary_selection_device;
141 zwp_primary_selection_device_v1_add_listener(
142 primary_selection_device, &primary_selection_device_listener, seat);
143}
145static void
146seat_add_text_input(struct seat *seat)
147{
148#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
149 if (seat->wayl->text_input_manager == NULL)
150 return;
152 struct zwp_text_input_v3 *text_input
153 = zwp_text_input_manager_v3_get_text_input(
154 seat->wayl->text_input_manager, seat->wl_seat);
156 if (text_input == NULL)
157 return;
159 seat->wl_text_input = text_input;
160 zwp_text_input_v3_add_listener(text_input, &text_input_listener, seat);
161#endif
162}
164static void
165seat_add_key_bindings(struct seat *seat)
166{
167 key_binding_new_for_seat(seat->wayl->key_binding_manager, seat);
168}
170static void
171seat_destroy(struct seat *seat)
172{
173 if (seat == NULL)
174 return;
176 tll_free(seat->mouse.buttons);
177 key_binding_remove_seat(seat->wayl->key_binding_manager, seat);
179 if (seat->kbd.xkb_compose_state != NULL)
180 xkb_compose_state_unref(seat->kbd.xkb_compose_state);
181 if (seat->kbd.xkb_compose_table != NULL)
182 xkb_compose_table_unref(seat->kbd.xkb_compose_table);
183 if (seat->kbd.xkb_keymap != NULL)
184 xkb_keymap_unref(seat->kbd.xkb_keymap);
185 if (seat->kbd.xkb_state != NULL)
186 xkb_state_unref(seat->kbd.xkb_state);
187 if (seat->kbd.xkb != NULL)
188 xkb_context_unref(seat->kbd.xkb);
190 if (seat->kbd.repeat.fd >= 0)
191 fdm_del(seat->wayl->fdm, seat->kbd.repeat.fd);
193 if (seat->pointer.theme != NULL)
194 wl_cursor_theme_destroy(seat->pointer.theme);
195 if (seat->pointer.surface.surf != NULL)
196 wl_surface_destroy(seat->pointer.surface.surf);
197 if (seat->pointer.surface.viewport != NULL)
198 wp_viewport_destroy(seat->pointer.surface.viewport);
199 if (seat->pointer.xcursor_callback != NULL)
200 wl_callback_destroy(seat->pointer.xcursor_callback);
202 if (seat->clipboard.data_source != NULL)
203 wl_data_source_destroy(seat->clipboard.data_source);
204 if (seat->clipboard.data_offer != NULL)
205 wl_data_offer_destroy(seat->clipboard.data_offer);
206 if (seat->primary.data_source != NULL)
207 zwp_primary_selection_source_v1_destroy(seat->primary.data_source);
208 if (seat->primary.data_offer != NULL)
209 zwp_primary_selection_offer_v1_destroy(seat->primary.data_offer);
210 if (seat->primary_selection_device != NULL)
211 zwp_primary_selection_device_v1_destroy(seat->primary_selection_device);
212 if (seat->data_device != NULL)
213 wl_data_device_release(seat->data_device);
214 if (seat->pointer.shape_device != NULL)
215 wp_cursor_shape_device_v1_destroy(seat->pointer.shape_device);
216 if (seat->wl_keyboard != NULL)
217 wl_keyboard_release(seat->wl_keyboard);
218 if (seat->wl_pointer != NULL)
219 wl_pointer_release(seat->wl_pointer);
220 if (seat->wl_touch != NULL)
221 wl_touch_release(seat->wl_touch);
223#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
224 if (seat->wl_text_input != NULL)
225 zwp_text_input_v3_destroy(seat->wl_text_input);
226#endif
228 if (seat->wl_seat != NULL)
229 wl_seat_release(seat->wl_seat);
231 ime_reset_pending(seat);
232 free(seat->clipboard.text);
233 free(seat->primary.text);
234 free(seat->pointer.last_custom_xcursor);
235 free(seat->name);
236}
238static void
239shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
240{
241 struct wayland *wayl = data;
243 switch (format) {
244 case WL_SHM_FORMAT_ARGB2101010: wayl->shm_have_argb2101010 = true; break;
245 case WL_SHM_FORMAT_ABGR2101010: wayl->shm_have_abgr2101010 = true; break;
246 case WL_SHM_FORMAT_ABGR16161616: wayl->shm_have_abgr161616 = true; break;
247 }
249#if defined(_DEBUG) && LOG_ENABLE_DBG == 1
250 bool have_description = false;
251 const char c4 = (format >> 24) & 0xff;
252 const char c3 = (format >> 16) & 0xff;
253 const char c2 = (format >> 8) & 0xff;
254 const char c1 = (format >> 0) & 0xff;
256 for (size_t i = 0; i < ALEN(shm_formats); i++) {
257 if (shm_formats[i].format == format) {
258 LOG_DBG("shm: 0x%08x: %c%c%c%c - %s",
259 format,
260 isprint(c1) ? c1 : ' ',
261 isprint(c2) ? c2 : ' ',
262 isprint(c3) ? c3 : ' ',
263 isprint(c4) ? c4 : ' ',
264 shm_formats[i].description);
265 have_description = true;
266 break;
267 }
268 }
270 if (!have_description)
271 LOG_DBG("shm: 0x%08x: %c%c%c%c - unknown",
272 format,
273 isprint(c1) ? c1 : ' ',
274 isprint(c2) ? c2 : ' ',
275 isprint(c3) ? c3 : ' ',
276 isprint(c4) ? c4 : ' ');
277#endif
278}
280static const struct wl_shm_listener shm_listener = {
281 .format = &shm_format,
282};
284static void
285xdg_wm_base_ping(void *data, struct xdg_wm_base *shell, uint32_t serial)
286{
287 LOG_DBG("wm base ping");
288 xdg_wm_base_pong(shell, serial);
289}
291static const struct xdg_wm_base_listener xdg_wm_base_listener = {
292 .ping = &xdg_wm_base_ping,
293};
295static void
296seat_handle_capabilities(void *data, struct wl_seat *wl_seat,
297 enum wl_seat_capability caps)
298{
299 struct seat *seat = data;
300 xassert(seat->wl_seat == wl_seat);
302 LOG_DBG("%s: keyboard=%s, pointer=%s, touch=%s", seat->name,
303 (caps & WL_SEAT_CAPABILITY_KEYBOARD) ? "yes" : "no",
304 (caps & WL_SEAT_CAPABILITY_POINTER) ? "yes" : "no",
305 (caps & WL_SEAT_CAPABILITY_TOUCH) ? "yes" : "no");
307 if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
308 if (seat->wl_keyboard == NULL) {
309 seat->wl_keyboard = wl_seat_get_keyboard(wl_seat);
310 wl_keyboard_add_listener(seat->wl_keyboard, &keyboard_listener, seat);
311 }
312 } else {
313 if (seat->wl_keyboard != NULL) {
314 wl_keyboard_release(seat->wl_keyboard);
315 seat->wl_keyboard = NULL;
316 }
317 }
319 if (caps & WL_SEAT_CAPABILITY_POINTER) {
320 if (seat->wl_pointer == NULL) {
321 xassert(seat->pointer.surface.surf == NULL);
322 seat->pointer.surface.surf =
323 wl_compositor_create_surface(seat->wayl->compositor);
325 if (seat->pointer.surface.surf == NULL) {
326 LOG_ERR("%s: failed to create pointer surface", seat->name);
327 return;
328 }
330 if (seat->wayl->viewporter != NULL) {
331 xassert(seat->pointer.surface.viewport == NULL);
332 seat->pointer.surface.viewport = wp_viewporter_get_viewport(
333 seat->wayl->viewporter, seat->pointer.surface.surf);
335 if (seat->pointer.surface.viewport == NULL) {
336 LOG_ERR("%s: failed to create pointer viewport", seat->name);
337 wl_surface_destroy(seat->pointer.surface.surf);
338 seat->pointer.surface.surf = NULL;
339 return;
340 }
341 }
343 seat->wl_pointer = wl_seat_get_pointer(wl_seat);
344 wl_pointer_add_listener(seat->wl_pointer, &pointer_listener, seat);
346 if (seat->wayl->cursor_shape_manager != NULL) {
347 xassert(seat->pointer.shape_device == NULL);
348 seat->pointer.shape_device = wp_cursor_shape_manager_v1_get_pointer(
349 seat->wayl->cursor_shape_manager, seat->wl_pointer);
350 }
351 }
352 } else {
353 if (seat->wl_pointer != NULL) {
354 if (seat->pointer.shape_device != NULL) {
355 wp_cursor_shape_device_v1_destroy(seat->pointer.shape_device);
356 seat->pointer.shape_device = NULL;
357 }
359 wl_pointer_release(seat->wl_pointer);
360 wl_surface_destroy(seat->pointer.surface.surf);
362 if (seat->pointer.surface.viewport != NULL) {
363 wp_viewport_destroy(seat->pointer.surface.viewport);
364 seat->pointer.surface.viewport = NULL;
365 }
367 if (seat->pointer.theme != NULL)
368 wl_cursor_theme_destroy(seat->pointer.theme);
370 if (seat->wl_touch != NULL &&
371 seat->touch.state == TOUCH_STATE_INHIBITED)
372 {
373 seat->touch.state = TOUCH_STATE_IDLE;
374 }
376 seat->wl_pointer = NULL;
377 seat->pointer.surface.surf = NULL;
378 seat->pointer.theme = NULL;
379 seat->pointer.cursor = NULL;
380 }
381 }
383 if (caps & WL_SEAT_CAPABILITY_TOUCH) {
384 if (seat->wl_touch == NULL) {
385 seat->wl_touch = wl_seat_get_touch(wl_seat);
386 wl_touch_add_listener(seat->wl_touch, &touch_listener, seat);
388 seat->touch.state = TOUCH_STATE_IDLE;
389 }
390 } else {
391 if (seat->wl_touch != NULL) {
392 wl_touch_release(seat->wl_touch);
393 seat->wl_touch = NULL;
394 }
396 seat->touch.state = TOUCH_STATE_INHIBITED;
397 }
398}
400static void
401seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
402{
403 struct seat *seat = data;
404 free(seat->name);
405 seat->name = xstrdup(name);
406}
408static const struct wl_seat_listener seat_listener = {
409 .capabilities = seat_handle_capabilities,
410 .name = seat_handle_name,
411};
413static void
414update_term_for_output_change(struct terminal *term)
415{
416 const float old_scale = term->scale;
417 const float logical_width = term->width / old_scale;
418 const float logical_height = term->height / old_scale;
420 /* Note: order matters! term_update_scale() must come first */
421 bool scale_updated = term_update_scale(term);
422 bool fonts_updated = term_font_dpi_changed(term, old_scale);
423 term_font_subpixel_changed(term);
425 csd_reload_font(term->window, old_scale);
427 enum resize_options resize_opts = RESIZE_KEEP_GRID;
429 if (fonts_updated) {
430 /*
431 * If the fonts have been updated, the cell dimensions have
432 * changed. This requires a "forced" resize, since the surface
433 * buffer dimensions may not have been updated (in which case
434 * render_resize() normally shortcuts and returns early).
435 */
436 resize_opts |= RESIZE_FORCE;
437 } else if (!scale_updated) {
438 /* No need to resize if neither scale nor fonts have changed */
439 return;
440 } else if (term->conf->dpi_aware) {
441 /*
442 * If fonts are sized according to DPI, it is possible for the
443 * cell size to remain the same when display scale
444 * changes. This will not change the surface buffer
445 * dimensions, but will change the logical size of the
446 * window. To ensure that the compositor is made aware o the
447 * proper logical size, force a resize rather than allowing
448 * render_resize() to shortcut the notification if the buffer
449 * dimensions remain the same.
450 */
451 resize_opts |= RESIZE_FORCE;
452 }
454 render_resize(
455 term,
456 (int)roundf(logical_width),
457 (int)roundf(logical_height),
458 resize_opts);
459}
461static void
462update_terms_on_monitor(struct monitor *mon)
463{
464 struct wayland *wayl = mon->wayl;
466 tll_foreach(wayl->terms, it) {
467 struct terminal *term = it->item;
468 if (unlikely(term->shutdown.in_progress))
469 continue;
471 tll_foreach(term->window->on_outputs, it2) {
472 if (it2->item == mon) {
473 update_term_for_output_change(term);
474 break;
475 }
476 }
477 }
478}
480static void
481output_update_ppi(struct monitor *mon)
482{
483 if (mon->dim.mm.width <= 0 || mon->dim.mm.height <= 0)
484 return;
486 double x_inches = mon->dim.mm.width * 0.03937008;
487 double y_inches = mon->dim.mm.height * 0.03937008;
489 const int width = mon->dim.px_real.width;
490 const int height = mon->dim.px_real.height;
492 mon->ppi.real.x = mon->dim.px_real.width / x_inches;
493 mon->ppi.real.y = mon->dim.px_real.height / y_inches;
495 /* The *logical* size is affected by the transform */
496 switch (mon->transform) {
497 case WL_OUTPUT_TRANSFORM_90:
498 case WL_OUTPUT_TRANSFORM_270:
499 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
500 case WL_OUTPUT_TRANSFORM_FLIPPED_270: {
501 int swap = x_inches;
502 x_inches = y_inches;
503 y_inches = swap;
504 break;
505 }
507 case WL_OUTPUT_TRANSFORM_NORMAL:
508 case WL_OUTPUT_TRANSFORM_180:
509 case WL_OUTPUT_TRANSFORM_FLIPPED:
510 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
511 break;
512 }
514 const int scaled_width = mon->dim.px_scaled.width;
515 const int scaled_height = mon->dim.px_scaled.height;
517 mon->ppi.scaled.x = scaled_width / x_inches;
518 mon->ppi.scaled.y = scaled_height / y_inches;
520 const double px_diag_physical = sqrt(pow(width, 2) + pow(height, 2));
521 mon->dpi.physical = width == 0 && height == 0
522 ? 96.
523 : px_diag_physical / mon->inch;
525 const double px_diag_scaled = sqrt(pow(scaled_width, 2) + pow(scaled_height, 2));
526 mon->dpi.scaled = scaled_width == 0 && scaled_height == 0
527 ? 96.
528 : px_diag_scaled / mon->inch * mon->scale;
530 if (mon->dpi.physical > 1000) {
531 if (mon->name != NULL) {
532 LOG_WARN("%s: DPI=%f (physical) is unreasonable, using 96 instead",
533 mon->name, mon->dpi.physical);
534 }
535 mon->dpi.physical = 96;
536 }
538 if (mon->dpi.scaled > 1000) {
539 if (mon->name != NULL) {
540 LOG_WARN("%s: DPI=%f (logical) is unreasonable, using 96 instead",
541 mon->name, mon->dpi.scaled);
542 }
543 mon->dpi.scaled = 96;
544 }
545}
547static void
548output_geometry(void *data, struct wl_output *wl_output, int32_t x, int32_t y,
549 int32_t physical_width, int32_t physical_height,
550 int32_t subpixel, const char *make, const char *model,
551 int32_t transform)
552{
553 struct monitor *mon = data;
555 free(mon->make);
556 free(mon->model);
558 mon->dim.mm.width = physical_width;
559 mon->dim.mm.height = physical_height;
560 mon->inch = sqrt(pow(mon->dim.mm.width, 2) + pow(mon->dim.mm.height, 2)) * 0.03937008;
561 mon->make = make != NULL ? xstrdup(make) : NULL;
562 mon->model = model != NULL ? xstrdup(model) : NULL;
563 mon->subpixel = subpixel;
564 mon->transform = transform;
566 output_update_ppi(mon);
567}
569static void
570output_mode(void *data, struct wl_output *wl_output, uint32_t flags,
571 int32_t width, int32_t height, int32_t refresh)
572{
573 if ((flags & WL_OUTPUT_MODE_CURRENT) == 0)
574 return;
576 struct monitor *mon = data;
577 mon->refresh = (float)refresh / 1000;
578 mon->dim.px_real.width = width;
579 mon->dim.px_real.height = height;
580 output_update_ppi(mon);
581}
583static void
584output_done(void *data, struct wl_output *wl_output)
585{
586 struct monitor *mon = data;
587 update_terms_on_monitor(mon);
588}
590static void
591output_scale(void *data, struct wl_output *wl_output, int32_t factor)
592{
593 struct monitor *mon = data;
594 mon->scale = factor;
595 output_update_ppi(mon);
596}
598#if defined(WL_OUTPUT_NAME_SINCE_VERSION)
599static void
600output_name(void *data, struct wl_output *wl_output, const char *name)
601{
602 struct monitor *mon = data;
603 free(mon->name);
604 mon->name = name != NULL ? xstrdup(name) : NULL;
605}
606#endif
608#if defined(WL_OUTPUT_DESCRIPTION_SINCE_VERSION)
609static void
610output_description(void *data, struct wl_output *wl_output,
611 const char *description)
612{
613 struct monitor *mon = data;
614 free(mon->description);
615 mon->description = description != NULL ? xstrdup(description) : NULL;
616}
617#endif
619static const struct wl_output_listener output_listener = {
620 .geometry = &output_geometry,
621 .mode = &output_mode,
622 .done = &output_done,
623 .scale = &output_scale,
624#if defined(WL_OUTPUT_NAME_SINCE_VERSION)
625 .name = &output_name,
626#endif
627#if defined(WL_OUTPUT_DESCRIPTION_SINCE_VERSION)
628 .description = &output_description,
629#endif
630};
632static void
633xdg_output_handle_logical_position(
634 void *data, struct zxdg_output_v1 *xdg_output, int32_t x, int32_t y)
635{
636 struct monitor *mon = data;
637 mon->x = x;
638 mon->y = y;
639}
641static void
642xdg_output_handle_logical_size(void *data, struct zxdg_output_v1 *xdg_output,
643 int32_t width, int32_t height)
644{
645 struct monitor *mon = data;
646 mon->dim.px_scaled.width = width;
647 mon->dim.px_scaled.height = height;
648 output_update_ppi(mon);
649}
651static void
652xdg_output_handle_done(void *data, struct zxdg_output_v1 *xdg_output)
653{
654 struct monitor *mon = data;
655 update_terms_on_monitor(mon);
656}
658static void
659xdg_output_handle_name(void *data, struct zxdg_output_v1 *xdg_output,
660 const char *name)
661{
662 struct monitor *mon = data;
663 free(mon->name);
664 mon->name = name != NULL ? xstrdup(name) : NULL;
665}
667static void
668xdg_output_handle_description(void *data, struct zxdg_output_v1 *xdg_output,
669 const char *description)
670{
671 struct monitor *mon = data;
672 free(mon->description);
673 mon->description = description != NULL ? xstrdup(description) : NULL;
674}
676static const struct zxdg_output_v1_listener xdg_output_listener = {
677 .logical_position = xdg_output_handle_logical_position,
678 .logical_size = xdg_output_handle_logical_size,
679 .done = xdg_output_handle_done,
680 .name = xdg_output_handle_name,
681 .description = xdg_output_handle_description,
682};
684static void
685clock_id(void *data, struct wp_presentation *wp_presentation, uint32_t clk_id)
686{
687 struct wayland *wayl = data;
688 wayl->presentation_clock_id = clk_id;
689 LOG_DBG("presentation clock ID: %u", clk_id);
690}
692static const struct wp_presentation_listener presentation_listener = {
693 .clock_id = &clock_id,
694};
696static void
697color_manager_create_image_description(struct wayland *wayl)
698{
699 if (!wayl->color_management.have_feat_parametric)
700 return;
702 if (!wayl->color_management.have_primaries_srgb)
703 return;
705 if (!wayl->color_management.have_tf_ext_linear)
706 return;
708 struct wp_image_description_creator_params_v1 *params =
709 wp_color_manager_v1_create_parametric_creator(wayl->color_management.manager);
711 wp_image_description_creator_params_v1_set_tf_named(
712 params, WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_EXT_LINEAR);
714 wp_image_description_creator_params_v1_set_primaries_named(
715 params, WP_COLOR_MANAGER_V1_PRIMARIES_SRGB);
717 wayl->color_management.img_description =
718 wp_image_description_creator_params_v1_create(params);
719}
721static void
722color_manager_supported_intent(void *data,
723 struct wp_color_manager_v1 *wp_color_manager_v1,
724 uint32_t render_intent)
725{
726 struct wayland *wayl = data;
727 if (render_intent == WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL)
728 wayl->color_management.have_intent_perceptual = true;
729}
731static void
732color_manager_supported_feature(void *data,
733 struct wp_color_manager_v1 *wp_color_manager_v1,
734 uint32_t feature)
735{
736 struct wayland *wayl = data;
738 if (feature == WP_COLOR_MANAGER_V1_FEATURE_PARAMETRIC)
739 wayl->color_management.have_feat_parametric = true;
740}
742static void
743color_manager_supported_tf_named(void *data,
744 struct wp_color_manager_v1 *wp_color_manager_v1,
745 uint32_t tf)
746{
747 struct wayland *wayl = data;
748 if (tf == WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_EXT_LINEAR)
749 wayl->color_management.have_tf_ext_linear = true;
750}
752static void
753color_manager_supported_primaries_named(void *data,
754 struct wp_color_manager_v1 *wp_color_manager_v1,
755 uint32_t primaries)
756{
757 struct wayland *wayl = data;
758 if (primaries == WP_COLOR_MANAGER_V1_PRIMARIES_SRGB)
759 wayl->color_management.have_primaries_srgb = true;
760}
762static void
763color_manager_done(void *data,
764 struct wp_color_manager_v1 *wp_color_manager_v1)
765{
766 struct wayland *wayl = data;
767 color_manager_create_image_description(wayl);
768}
770static const struct wp_color_manager_v1_listener color_manager_listener = {
771 .supported_intent = &color_manager_supported_intent,
772 .supported_feature = &color_manager_supported_feature,
773 .supported_primaries_named = &color_manager_supported_primaries_named,
774 .supported_tf_named = &color_manager_supported_tf_named,
775 .done = &color_manager_done,
776};
778static bool
779verify_iface_version(const char *iface, uint32_t version, uint32_t wanted)
780{
781 if (version >= wanted)
782 return true;
784 LOG_ERR("%s: need interface version %u, but compositor only implements %u",
785 iface, wanted, version);
786 return false;
787}
789static void
790surface_enter(void *data, struct wl_surface *wl_surface,
791 struct wl_output *wl_output)
792{
793 struct wl_window *win = data;
794 struct terminal *term = win->term;
796 tll_foreach(term->wl->monitors, it) {
797 if (it->item.output == wl_output) {
798 LOG_DBG("mapped on %s", it->item.name);
799 tll_push_back(term->window->on_outputs, &it->item);
800 update_term_for_output_change(term);
801 return;
802 }
803 }
805 LOG_ERR("mapped on unknown output");
806}
808static void
809surface_leave(void *data, struct wl_surface *wl_surface,
810 struct wl_output *wl_output)
811{
812 struct wl_window *win = data;
813 struct terminal *term = win->term;
815 tll_foreach(term->window->on_outputs, it) {
816 if (it->item->output != wl_output)
817 continue;
819 LOG_DBG("unmapped from %s", it->item->name);
820 tll_remove(term->window->on_outputs, it);
821 update_term_for_output_change(term);
822 return;
823 }
825 LOG_WARN("unmapped from unknown output");
826}
828#if defined(WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION)
829static void
830surface_preferred_buffer_scale(void *data, struct wl_surface *surface,
831 int32_t scale)
832{
833 struct wl_window *win = data;
835 if (win->preferred_buffer_scale == scale)
836 return;
838 LOG_DBG("wl_surface preferred scale: %d -> %d", win->preferred_buffer_scale, scale);
840 win->preferred_buffer_scale = scale;
841 update_term_for_output_change(win->term);
842}
844static void
845surface_preferred_buffer_transform(void *data, struct wl_surface *surface,
846 uint32_t transform)
847{
849}
850#endif
852static const struct wl_surface_listener surface_listener = {
853 .enter = &surface_enter,
854 .leave = &surface_leave,
855#if defined(WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION)
856 .preferred_buffer_scale = &surface_preferred_buffer_scale,
857 .preferred_buffer_transform = &surface_preferred_buffer_transform,
858#endif
859};
861static void
862xdg_toplevel_configure(void *data, struct xdg_toplevel *xdg_toplevel,
863 int32_t width, int32_t height, struct wl_array *states)
864{
865 bool is_activated = false;
866 bool is_fullscreen = false;
867 bool is_maximized = false;
868 bool is_resizing = false;
869 bool is_tiled_top = false;
870 bool is_tiled_bottom = false;
871 bool is_tiled_left = false;
872 bool is_tiled_right = false;
873 bool is_constrained_top = false;
874 bool is_constrained_bottom = false;
875 bool is_constrained_left = false;
876 bool is_constrained_right = false;
877 bool is_suspended = false;
879#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
880 char state_str[2048];
881 int state_chars = 0;
883 static const char *const strings[] = {
884 [XDG_TOPLEVEL_STATE_MAXIMIZED] = "maximized",
885 [XDG_TOPLEVEL_STATE_FULLSCREEN] = "fullscreen",
886 [XDG_TOPLEVEL_STATE_RESIZING] = "resizing",
887 [XDG_TOPLEVEL_STATE_ACTIVATED] = "activated",
888 [XDG_TOPLEVEL_STATE_TILED_LEFT] = "tiled:left",
889 [XDG_TOPLEVEL_STATE_TILED_RIGHT] = "tiled:right",
890 [XDG_TOPLEVEL_STATE_TILED_TOP] = "tiled:top",
891 [XDG_TOPLEVEL_STATE_TILED_BOTTOM] = "tiled:bottom",
892#if defined(XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION) /* wayland-protocols >= 1.32 */
893 [XDG_TOPLEVEL_STATE_SUSPENDED] = "suspended",
894#endif
895#if defined(XDG_TOPLEVEL_STATE_CONSTRAINED_LEFT_SINCE_VERSION)
896 [XDG_TOPLEVEL_STATE_CONSTRAINED_LEFT] = "constrained:left",
897 [XDG_TOPLEVEL_STATE_CONSTRAINED_RIGHT] = "constrained:right",
898 [XDG_TOPLEVEL_STATE_CONSTRAINED_TOP] = "constrained:top",
899 [XDG_TOPLEVEL_STATE_CONSTRAINED_BOTTOM] = "constrained:bottom",
900#endif
901 };
902#endif
904 enum xdg_toplevel_state *state;
905 wl_array_for_each(state, states) {
906 switch (*state) {
907 case XDG_TOPLEVEL_STATE_MAXIMIZED: is_maximized = true; break;
908 case XDG_TOPLEVEL_STATE_FULLSCREEN: is_fullscreen = true; break;
909 case XDG_TOPLEVEL_STATE_RESIZING: is_resizing = true; break;
910 case XDG_TOPLEVEL_STATE_ACTIVATED: is_activated = true; break;
911 case XDG_TOPLEVEL_STATE_TILED_LEFT: is_tiled_left = true; break;
912 case XDG_TOPLEVEL_STATE_TILED_RIGHT: is_tiled_right = true; break;
913 case XDG_TOPLEVEL_STATE_TILED_TOP: is_tiled_top = true; break;
914 case XDG_TOPLEVEL_STATE_TILED_BOTTOM: is_tiled_bottom = true; break;
916#if defined(XDG_TOPLEVEL_STATE_SUSPENDED_SINCE_VERSION)
917 case XDG_TOPLEVEL_STATE_SUSPENDED: is_suspended = true; break;
918#endif
919#if defined(XDG_TOPLEVEL_STATE_CONSTRAINED_LEFT_SINCE_VERSION)
920 case XDG_TOPLEVEL_STATE_CONSTRAINED_LEFT: is_constrained_left = true; break;
921 case XDG_TOPLEVEL_STATE_CONSTRAINED_RIGHT: is_constrained_right = true; break;
922 case XDG_TOPLEVEL_STATE_CONSTRAINED_TOP: is_constrained_top = true; break;
923 case XDG_TOPLEVEL_STATE_CONSTRAINED_BOTTOM: is_constrained_bottom = true; break;
924#endif
925 }
927#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
928 if (*state >= 0 && *state < ALEN(strings)) {
929 state_chars += snprintf(
930 &state_str[state_chars], sizeof(state_str) - state_chars,
931 "%s, ",
932 strings[*state] != NULL ? strings[*state] : "<unknown>");
933 }
934#endif
935 }
937#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
938 if (state_chars > 2)
939 state_str[state_chars - 2] = '\0';
940 else
941 state_str[0] = '\0';
943 LOG_DBG("xdg-toplevel: configure: size=%dx%d, states=[%s]",
944 width, height, state_str);
945#endif
947 /*
948 * Changes done here are ignored until the configure event has
949 * been ack:ed in xdg_surface_configure().
950 *
951 * So, just store the config data and apply it later, in
952 * xdg_surface_configure() after we've ack:ed the event.
953 */
954 struct wl_window *win = data;
956 win->configure.is_activated = is_activated;
957 win->configure.is_suspended = is_suspended;
958 win->configure.is_fullscreen = is_fullscreen;
959 win->configure.is_maximized = is_maximized;
960 win->configure.is_resizing = is_resizing;
961 win->configure.is_tiled_top = is_tiled_top;
962 win->configure.is_tiled_bottom = is_tiled_bottom;
963 win->configure.is_tiled_left = is_tiled_left;
964 win->configure.is_tiled_right = is_tiled_right;
965 win->configure.is_constrained_top = is_constrained_top;
966 win->configure.is_constrained_bottom = is_constrained_bottom;
967 win->configure.is_constrained_left = is_constrained_left;
968 win->configure.is_constrained_right = is_constrained_right;
969 win->configure.width = width;
970 win->configure.height = height;
971}
973static void
974xdg_toplevel_close(void *data, struct xdg_toplevel *xdg_toplevel)
975{
976 struct wl_window *win = data;
977 struct terminal *term = win->term;
978 LOG_DBG("xdg-toplevel: close");
979 term_shutdown(term);
980}
982#if defined(XDG_TOPLEVEL_CONFIGURE_BOUNDS_SINCE_VERSION)
983static void
984xdg_toplevel_configure_bounds(void *data,
985 struct xdg_toplevel *xdg_toplevel,
986 int32_t width, int32_t height)
987{
988 /* TODO: ensure we don't pick a bigger size */
989}
990#endif
992#if defined(XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION)
993static void
994xdg_toplevel_wm_capabilities(void *data,
995 struct xdg_toplevel *xdg_toplevel,
996 struct wl_array *caps)
997{
998 struct wl_window *win = data;
1000 win->wm_capabilities.maximize = false;
1001 win->wm_capabilities.minimize = false;
1003#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
1004 char cap_str[2048];
1005 int cap_chars = 0;
1007 static const char *const strings[] = {
1008 [XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU] = "window-menu",
1009 [XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE] = "maximize",
1010 [XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN] = "fullscreen",
1011 [XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE] = "minimize",
1012 };
1013#endif
1015 enum xdg_toplevel_wm_capabilities *cap;
1016 wl_array_for_each(cap, caps) {
1017 switch (*cap) {
1018 case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE:
1019 win->wm_capabilities.maximize = true;
1020 break;
1022 case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE:
1023 win->wm_capabilities.minimize = true;
1024 break;
1026 case XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU:
1027 case XDG_TOPLEVEL_WM_CAPABILITIES_FULLSCREEN:
1028 break;
1029 }
1031#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
1032 if (*cap >= 0 && *cap < ALEN(strings)) {
1033 cap_chars += snprintf(
1034 &cap_str[cap_chars], sizeof(cap_str) - cap_chars,
1035 "%s, ",
1036 strings[*cap] != NULL ? strings[*cap] : "<unknown>");
1037 }
1038#endif
1039 }
1041#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
1042 if (cap_chars > 2)
1043 cap_str[cap_chars - 2] = '\0';
1044 else
1045 cap_str[0] = '\0';
1047 LOG_DBG("xdg-toplevel: wm-capabilities=[%s]", cap_str);
1048#endif
1049}
1050#endif
1052static const struct xdg_toplevel_listener xdg_toplevel_listener = {
1053 .configure = &xdg_toplevel_configure,
1054 /*.close = */&xdg_toplevel_close, /* epoll-shim defines a macro 'close'... */
1055#if defined(XDG_TOPLEVEL_CONFIGURE_BOUNDS_SINCE_VERSION)
1056 .configure_bounds = &xdg_toplevel_configure_bounds,
1057#endif
1058#if defined(XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION)
1059 .wm_capabilities = xdg_toplevel_wm_capabilities,
1060#endif
1061};
1063static void
1064xdg_surface_configure(void *data, struct xdg_surface *xdg_surface,
1065 uint32_t serial)
1066{
1067 LOG_DBG("xdg-surface: configure");
1069 struct wl_window *win = data;
1070 struct terminal *term = win->term;
1072 if (win->unmapped) {
1073 /*
1074 * https://codeberg.org/dnkl/foot/issues/1249
1075 * https://gitlab.freedesktop.org/wlroots/wlroots/-/issues/3487
1076 * https://gitlab.freedesktop.org/wlroots/wlroots/-/merge_requests/3719
1077 * https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/108
1078 */
1079 return;
1080 }
1082 bool wasnt_configured = !win->is_configured;
1083 bool was_resizing = win->is_resizing;
1084 bool was_fullscreen = win->is_fullscreen;
1085 bool was_suspended = win->is_suspended;
1086 bool csd_was_enabled = win->csd_mode == CSD_YES && !win->is_fullscreen;
1087 int new_width = win->configure.width;
1088 int new_height = win->configure.height;
1090 win->is_configured = true;
1091 win->is_maximized = win->configure.is_maximized;
1092 win->is_fullscreen = win->configure.is_fullscreen;
1093 win->is_resizing = win->configure.is_resizing;
1094 win->is_suspended = win->configure.is_suspended;
1096 win->is_tiled_top = win->configure.is_tiled_top;
1097 win->is_tiled_bottom = win->configure.is_tiled_bottom;
1098 win->is_tiled_left = win->configure.is_tiled_left;
1099 win->is_tiled_right = win->configure.is_tiled_right;
1101 win->is_constrained_top = win->configure.is_constrained_top;
1102 win->is_constrained_bottom = win->configure.is_constrained_bottom;
1103 win->is_constrained_left = win->configure.is_constrained_left;
1104 win->is_constrained_right = win->configure.is_constrained_right;
1106 win->is_tiled = (win->is_tiled_top ||
1107 win->is_tiled_bottom ||
1108 win->is_tiled_left ||
1109 win->is_tiled_right);
1111 win->csd_mode = win->configure.csd_mode;
1113 bool enable_csd = win->csd_mode == CSD_YES && !win->is_fullscreen;
1114 if (!csd_was_enabled && enable_csd)
1115 csd_instantiate(win);
1116 else if (csd_was_enabled && !enable_csd)
1117 csd_destroy(win);
1119 if (enable_csd && new_width > 0 && new_height > 0) {
1120 if (wayl_win_csd_titlebar_visible(win))
1121 new_height -= win->term->conf->csd.title_height;
1123 if (wayl_win_csd_borders_visible(win)) {
1124 new_height -= 2 * win->term->conf->csd.border_width_visible;
1125 new_width -= 2 * win->term->conf->csd.border_width_visible;
1126 }
1127 }
1129 xdg_surface_ack_configure(xdg_surface, serial);
1131 enum resize_options opts = RESIZE_BY_CELLS;
1133#if 1
1134 /*
1135 * TODO: decide if we should do the last "forced" call when ending
1136 * an interactive resize.
1137 *
1138 * Without it, the last TIOCSWINSZ sent to the client will be a
1139 * scheduled one. I.e. there will be a small delay after the user
1140 * has *stopped* resizing, and the client application receives the
1141 * final size.
1142 *
1143 * Note: if we also disable content centering while resizing, then
1144 * the last, forced, resize *is* necessary.
1145 */
1146 if (was_resizing && !win->is_resizing)
1147 opts |= RESIZE_FORCE;
1148#endif
1150 bool resized = render_resize(term, new_width, new_height, opts);
1152 if (win->configure.is_activated)
1153 term_visual_focus_in(term);
1154 else
1155 term_visual_focus_out(term);
1157 if (was_suspended != win->is_suspended && term->visibility_reports)
1158 term_send_visibility_report(term);
1160 /*
1161 * Update opaque region if fullscreen state changed, also need to
1162 * render, since we use different buffer types with and without
1163 * alpha
1164 */
1165 if (was_fullscreen != win->is_fullscreen) {
1166 wayl_win_alpha_changed(win);
1167 render_refresh(term);
1168 }
1170 const bool will_render_soon = resized ||
1171 term->render.refresh.grid ||
1172 term->render.pending.grid;
1174 if (!will_render_soon) {
1175 /*
1176 * If we didn't resize, and aren't refreshing for other
1177 * reasons, we won't be committing a new surface anytime
1178 * soon. Some compositors require a commit in combination with
1179 * an ack - make them happy.
1180 */
1181 wl_surface_commit(win->surface.surf);
1182 }
1184 if (wasnt_configured)
1185 term_window_configured(term);
1186}
1188static const struct xdg_surface_listener xdg_surface_listener = {
1189 .configure = &xdg_surface_configure,
1190};
1192static void
1193xdg_toplevel_decoration_configure(void *data,
1194 struct zxdg_toplevel_decoration_v1 *zxdg_toplevel_decoration_v1,
1195 uint32_t mode)
1196{
1197 struct wl_window *win = data;
1199 xassert(win->term->conf->csd.preferred != CONF_CSD_PREFER_NONE);
1200 switch (mode) {
1201 case ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE:
1202 LOG_INFO("using CSD decorations");
1203 win->configure.csd_mode = CSD_YES;
1204 break;
1206 case ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE:
1207 LOG_INFO("using SSD decorations");
1208 win->configure.csd_mode = CSD_NO;
1209 break;
1211 default:
1212 LOG_ERR("unimplemented: unknown XDG toplevel decoration mode: %u", mode);
1213 break;
1214 }
1215}
1217static const struct zxdg_toplevel_decoration_v1_listener xdg_toplevel_decoration_listener = {
1218 .configure = &xdg_toplevel_decoration_configure,
1219};
1221#if defined(HAVE_EXT_BACKGROUND_EFFECT)
1222static void
1223ext_background_capabilities(
1224 void *data,
1225 struct ext_background_effect_manager_v1 *ext_background_effect_manager_v1,
1226 uint32_t flags)
1227{
1228 struct wayland *wayl = data;
1230 wayl->have_background_blur =
1231 !!(flags & EXT_BACKGROUND_EFFECT_MANAGER_V1_CAPABILITY_BLUR);
1233 LOG_DBG("compositor supports background blur: %s",
1234 wayl->have_background_blur ? "yes" : "no");
1235}
1237static const struct ext_background_effect_manager_v1_listener background_manager_listener = {
1238 .capabilities = &ext_background_capabilities,
1239};
1240#endif /* HAVE_EXT_BACKGROUND_EFFECT */
1242static bool
1243fdm_repeat(struct fdm *fdm, int fd, int events, void *data)
1244{
1245 if (events & EPOLLHUP)
1246 return false;
1248 struct seat *seat = data;
1249 uint64_t expiration_count;
1250 ssize_t ret = read(
1251 seat->kbd.repeat.fd, &expiration_count, sizeof(expiration_count));
1253 if (ret < 0) {
1254 if (errno == EAGAIN)
1255 return true;
1257 LOG_ERRNO("failed to read repeat key from repeat timer fd");
1258 return false;
1259 }
1261 seat->kbd.repeat.dont_re_repeat = true;
1262 for (size_t i = 0; i < expiration_count; i++)
1263 input_repeat(seat, seat->kbd.repeat.key);
1264 seat->kbd.repeat.dont_re_repeat = false;
1265 return true;
1266}
1268static void
1269handle_global(void *data, struct wl_registry *registry,
1270 uint32_t name, const char *interface, uint32_t version)
1271{
1272 LOG_DBG("global: 0x%08x, interface=%s, version=%u", name, interface, version);
1273 struct wayland *wayl = data;
1275 if (streq(interface, wl_compositor_interface.name)) {
1276 const uint32_t required = 4;
1277 if (!verify_iface_version(interface, version, required))
1278 return;
1280#if defined (WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION)
1281 const uint32_t preferred = WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION;
1282#else
1283 const uint32_t preferred = required;
1284#endif
1285 wayl->compositor = wl_registry_bind(
1286 wayl->registry, name, &wl_compositor_interface, min(version, preferred));
1287 }
1289 else if (streq(interface, wl_subcompositor_interface.name)) {
1290 const uint32_t required = 1;
1291 if (!verify_iface_version(interface, version, required))
1292 return;
1294 wayl->sub_compositor = wl_registry_bind(
1295 wayl->registry, name, &wl_subcompositor_interface, required);
1296 }
1298 else if (streq(interface, wl_shm_interface.name)) {
1299 const uint32_t required = 1;
1300 if (!verify_iface_version(interface, version, required))
1301 return;
1303#if defined(WL_SHM_RELEASE_SINCE_VERSION)
1304 const uint32_t preferred = WL_SHM_RELEASE_SINCE_VERSION;
1305#else
1306 const uint32_t preferred = required;
1307#endif
1309 wayl->shm = wl_registry_bind(
1310 wayl->registry, name, &wl_shm_interface, min(version, preferred));
1311 wl_shm_add_listener(wayl->shm, &shm_listener, wayl);
1312#if defined(WL_SHM_RELEASE_SINCE_VERSION)
1313 wayl->use_shm_release = version >= WL_SHM_RELEASE_SINCE_VERSION;
1314#else
1315 wayl->use_shm_release = false;
1316#endif
1317 }
1319 else if (streq(interface, xdg_wm_base_interface.name)) {
1320 const uint32_t required = 1;
1321 if (!verify_iface_version(interface, version, required))
1322 return;
1324 /*
1325 * We *require* version 1, but _can_ use version 2, 5 or 7, if
1326 * available.
1327 *
1328 * Version 2 adds 'tiled' window states. We use this
1329 * information to restore the window size when window is
1330 * un-tiled.
1331 *
1332 * Version 5 adds 'wm_capabilities'. We use this information
1333 * to draw window decorations.
1334 *
1335 * Version 7 adds 'constrained' window states. We use this
1336 * information to determine whether to allow window resize
1337 * (via CSDs) or not.
1338 */
1339#if defined(XDG_TOPLEVEL_STATE_CONSTRAINED_LEFT_SINCE_VERSION)
1340 const uint32_t preferred = XDG_TOPLEVEL_STATE_CONSTRAINED_LEFT_SINCE_VERSION;
1341#elif defined(XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION)
1342 const uint32_t preferred = XDG_TOPLEVEL_WM_CAPABILITIES_SINCE_VERSION;
1343#elif defined(XDG_TOPLEVEL_STATE_TILED_LEFT_SINCE_VERSION)
1344 const uint32_t preferred = XDG_TOPLEVEL_STATE_TILED_LEFT_SINCE_VERSION;
1345#else
1346 const uint32_t preferred = required;
1347#endif
1349 wayl->shell = wl_registry_bind(
1350 wayl->registry, name, &xdg_wm_base_interface, min(version, preferred));
1351 xdg_wm_base_add_listener(wayl->shell, &xdg_wm_base_listener, wayl);
1352 }
1354 else if (streq(interface, zxdg_decoration_manager_v1_interface.name)) {
1355 const uint32_t required = 1;
1356 if (!verify_iface_version(interface, version, required))
1357 return;
1359 wayl->xdg_decoration_manager = wl_registry_bind(
1360 wayl->registry, name, &zxdg_decoration_manager_v1_interface, required);
1361 }
1363 else if (streq(interface, wl_seat_interface.name)) {
1364 const uint32_t required = 5;
1365 if (!verify_iface_version(interface, version, required))
1366 return;
1368#if defined(WL_POINTER_AXIS_VALUE120_SINCE_VERSION)
1369 const uint32_t preferred = WL_POINTER_AXIS_VALUE120_SINCE_VERSION;
1370#else
1371 const uint32_t preferred = required;
1372#endif
1374 int repeat_fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK);
1375 if (repeat_fd == -1) {
1376 LOG_ERRNO("failed to create keyboard repeat timer FD");
1377 return;
1378 }
1380 struct wl_seat *wl_seat = wl_registry_bind(
1381 wayl->registry, name, &wl_seat_interface, min(version, preferred));
1383 tll_push_back(wayl->seats, ((struct seat){
1384 .wayl = wayl,
1385 .wl_seat = wl_seat,
1386 .wl_name = name,
1387 .kbd = {
1388 .repeat = {
1389 .fd = repeat_fd,
1390 },
1391 }}));
1393 struct seat *seat = &tll_back(wayl->seats);
1395 if (!fdm_add(wayl->fdm, repeat_fd, EPOLLIN, &fdm_repeat, seat)) {
1396 close(repeat_fd);
1397 seat->kbd.repeat.fd = -1;
1398 seat_destroy(seat);
1399 return;
1400 }
1402 seat->kbd.xkb = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
1403 if (seat->kbd.xkb != NULL) {
1404 seat->kbd.xkb_compose_table = xkb_compose_table_new_from_locale(
1405 seat->kbd.xkb, setlocale(LC_CTYPE, NULL), XKB_COMPOSE_COMPILE_NO_FLAGS);
1407 if (seat->kbd.xkb_compose_table != NULL) {
1408 seat->kbd.xkb_compose_state = xkb_compose_state_new(
1409 seat->kbd.xkb_compose_table, XKB_COMPOSE_STATE_NO_FLAGS);
1410 } else {
1411 LOG_WARN("failed to instantiate compose table; dead keys (compose) will not work");
1412 }
1413 }
1415 seat_add_data_device(seat);
1416 seat_add_primary_selection(seat);
1417 seat_add_text_input(seat);
1418 seat_add_key_bindings(seat);
1419 wl_seat_add_listener(wl_seat, &seat_listener, seat);
1420 }
1422 else if (streq(interface, zxdg_output_manager_v1_interface.name)) {
1423 const uint32_t required = 1;
1424 if (!verify_iface_version(interface, version, required))
1425 return;
1427 wayl->xdg_output_manager = wl_registry_bind(
1428 wayl->registry, name, &zxdg_output_manager_v1_interface,
1429 min(version, 2));
1431 tll_foreach(wayl->monitors, it) {
1432 struct monitor *mon = &it->item;
1433 mon->xdg = zxdg_output_manager_v1_get_xdg_output(
1434 wayl->xdg_output_manager, mon->output);
1435 zxdg_output_v1_add_listener(mon->xdg, &xdg_output_listener, mon);
1436 }
1437 }
1439 else if (streq(interface, wl_output_interface.name)) {
1440 const uint32_t required = 2;
1441 if (!verify_iface_version(interface, version, required))
1442 return;
1444#if defined(WL_OUTPUT_NAME_SINCE_VERSION)
1445 const uint32_t preferred = WL_OUTPUT_NAME_SINCE_VERSION;
1446#elif defined(WL_OUTPUT_RELEASE_SINCE_VERSION)
1447 const uint32_t preferred = WL_OUTPUT_RELEASE_SINCE_VERSION;
1448#else
1449 const uint32_t preferred = required;
1450#endif
1452 struct wl_output *output = wl_registry_bind(
1453 wayl->registry, name, &wl_output_interface, min(version, preferred));
1455 tll_push_back(
1456 wayl->monitors,
1457 ((struct monitor){.wayl = wayl, .output = output, .wl_name = name,
1458 .scale = 1,
1459 .use_output_release = version >= WL_OUTPUT_RELEASE_SINCE_VERSION}));
1461 struct monitor *mon = &tll_back(wayl->monitors);
1462 wl_output_add_listener(output, &output_listener, mon);
1464 if (wayl->xdg_output_manager != NULL) {
1465 mon->xdg = zxdg_output_manager_v1_get_xdg_output(
1466 wayl->xdg_output_manager, mon->output);
1467 zxdg_output_v1_add_listener(mon->xdg, &xdg_output_listener, mon);
1468 }
1469 }
1471 else if (streq(interface, wl_data_device_manager_interface.name)) {
1472 const uint32_t required = 3;
1473 if (!verify_iface_version(interface, version, required))
1474 return;
1476 wayl->data_device_manager = wl_registry_bind(
1477 wayl->registry, name, &wl_data_device_manager_interface, required);
1479 tll_foreach(wayl->seats, it)
1480 seat_add_data_device(&it->item);
1481 }
1483 else if (streq(interface, zwp_primary_selection_device_manager_v1_interface.name)) {
1484 const uint32_t required = 1;
1485 if (!verify_iface_version(interface, version, required))
1486 return;
1488 wayl->primary_selection_device_manager = wl_registry_bind(
1489 wayl->registry, name,
1490 &zwp_primary_selection_device_manager_v1_interface, required);
1492 tll_foreach(wayl->seats, it)
1493 seat_add_primary_selection(&it->item);
1494 }
1496 else if (streq(interface, wp_presentation_interface.name)) {
1497 if (wayl->presentation_timings) {
1498 const uint32_t required = 1;
1499 if (!verify_iface_version(interface, version, required))
1500 return;
1502 wayl->presentation = wl_registry_bind(
1503 wayl->registry, name, &wp_presentation_interface, required);
1504 wp_presentation_add_listener(
1505 wayl->presentation, &presentation_listener, wayl);
1506 }
1507 }
1509 else if (streq(interface, xdg_activation_v1_interface.name)) {
1510 const uint32_t required = 1;
1511 if (!verify_iface_version(interface, version, required))
1512 return;
1514 wayl->xdg_activation = wl_registry_bind(
1515 wayl->registry, name, &xdg_activation_v1_interface, required);
1516 }
1518 else if (streq(interface, wp_viewporter_interface.name)) {
1519 const uint32_t required = 1;
1520 if (!verify_iface_version(interface, version, required))
1521 return;
1523 wayl->viewporter = wl_registry_bind(
1524 wayl->registry, name, &wp_viewporter_interface, required);
1525 }
1527 else if (streq(interface, wp_fractional_scale_manager_v1_interface.name)) {
1528 const uint32_t required = 1;
1529 if (!verify_iface_version(interface, version, required))
1530 return;
1532 wayl->fractional_scale_manager = wl_registry_bind(
1533 wayl->registry, name,
1534 &wp_fractional_scale_manager_v1_interface, required);
1535 }
1537 else if (streq(interface, wp_cursor_shape_manager_v1_interface.name)) {
1538 const uint32_t required = 1;
1539 if (!verify_iface_version(interface, version, required))
1540 return;
1542#if defined(WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DND_ASK_SINCE_VERSION) /* 1.42 */
1543 const uint32_t preferred = WP_CURSOR_SHAPE_DEVICE_V1_SHAPE_DND_ASK_SINCE_VERSION;
1544#else
1545 const uint32_t preferred = required;
1546#endif
1548 wayl->shape_manager_version = min(version, preferred);
1549 wayl->cursor_shape_manager = wl_registry_bind(
1550 wayl->registry, name, &wp_cursor_shape_manager_v1_interface,
1551 wayl->shape_manager_version);
1552 }
1554 else if (streq(interface, wp_single_pixel_buffer_manager_v1_interface.name)) {
1555 const uint32_t required = 1;
1556 if (!verify_iface_version(interface, version, required))
1557 return;
1559 wayl->single_pixel_manager = wl_registry_bind(
1560 wayl->registry, name,
1561 &wp_single_pixel_buffer_manager_v1_interface, required);
1562 }
1564 else if (streq(interface, xdg_toplevel_icon_manager_v1_interface.name)) {
1565 const uint32_t required = 1;
1566 if (!verify_iface_version(interface, version, required))
1567 return;
1569 wayl->toplevel_icon_manager = wl_registry_bind(
1570 wayl->registry, name, &xdg_toplevel_icon_manager_v1_interface, required);
1571 }
1573 else if (streq(interface, xdg_system_bell_v1_interface.name)) {
1574 const uint32_t required = 1;
1575 if (!verify_iface_version(interface, version, required))
1576 return;
1578 wayl->system_bell = wl_registry_bind(
1579 wayl->registry, name, &xdg_system_bell_v1_interface, required);
1580 }
1582 else if (streq(interface, wp_color_manager_v1_interface.name)) {
1583 const uint32_t required = 1;
1584 if (!verify_iface_version(interface, version, required))
1585 return;
1587 wayl->color_management.manager = wl_registry_bind(
1588 wayl->registry, name, &wp_color_manager_v1_interface, required);
1590 wp_color_manager_v1_add_listener(
1591 wayl->color_management.manager, &color_manager_listener, wayl);
1592 }
1594#if defined(WL_FIXES_INTERFACE)
1595 else if (streq(interface, wl_fixes_interface.name)) {
1596 const uint32_t required = 1;
1597 if (!verify_iface_version(interface, version, required))
1598 return;
1600#if defined(WL_FIXES_ACK_GLOBAL_REMOVE_SINCE_VERSION)
1601 const uint32_t preferred = WL_FIXES_ACK_GLOBAL_REMOVE_SINCE_VERSION;
1602#else
1603 const uint32_t preferred = required;
1604#endif
1606 wayl->fixes_version = min(version, preferred);
1607 wayl->fixes = wl_registry_bind(
1608 wayl->registry, name, &wl_fixes_interface, wayl->fixes_version);
1609 }
1610#endif
1612#if defined(HAVE_XDG_TOPLEVEL_TAG)
1613 else if (streq(interface, xdg_toplevel_tag_manager_v1_interface.name)) {
1614 const uint32_t required = 1;
1615 if (!verify_iface_version(interface, version, required))
1616 return;
1618 wayl->toplevel_tag_manager = wl_registry_bind(
1619 wayl->registry, name, &xdg_toplevel_tag_manager_v1_interface, required);
1620 }
1621#endif
1622#if defined(HAVE_EXT_BACKGROUND_EFFECT)
1623 else if (streq(interface, ext_background_effect_manager_v1_interface.name)) {
1624 const uint32_t required = 1;
1625 if (!verify_iface_version(interface, version, required))
1626 return;
1628 wayl->background_effect_manager = wl_registry_bind(
1629 wayl->registry, name,
1630 &ext_background_effect_manager_v1_interface, required);
1632 ext_background_effect_manager_v1_add_listener(
1633 wayl->background_effect_manager, &background_manager_listener, wayl);
1634 }
1635#endif
1637#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
1638 else if (streq(interface, zwp_text_input_manager_v3_interface.name)) {
1639 const uint32_t required = 1;
1640 if (!verify_iface_version(interface, version, required))
1641 return;
1643 wayl->text_input_manager = wl_registry_bind(
1644 wayl->registry, name, &zwp_text_input_manager_v3_interface, required);
1646 tll_foreach(wayl->seats, it)
1647 seat_add_text_input(&it->item);
1648 }
1649#endif
1651}
1653static void
1654monitor_destroy(struct monitor *mon)
1655{
1656 if (mon->xdg != NULL)
1657 zxdg_output_v1_destroy(mon->xdg);
1658 if (mon->output != NULL) {
1659 if (mon->use_output_release)
1660 wl_output_release(mon->output);
1661 else
1662 wl_output_destroy(mon->output);
1663 }
1664 free(mon->make);
1665 free(mon->model);
1666 free(mon->name);
1667 free(mon->description);
1668}
1670static void
1671handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
1672{
1673 LOG_DBG("global removed: 0x%08x", name);
1675 struct wayland *wayl = data;
1677 /* Check if this is an output */
1678 tll_foreach(wayl->monitors, it) {
1679 struct monitor *mon = &it->item;
1681 if (mon->wl_name != name)
1682 continue;
1684 LOG_INFO("monitor unplugged or disabled: %s", mon->name);
1686 /*
1687 * Update all terminals that are mapped here. On Sway 1.4,
1688 * surfaces are *not* unmapped before the output is removed
1689 */
1691 tll_foreach(wayl->terms, t) {
1692 tll_foreach(t->item->window->on_outputs, o) {
1693 if (o->item->output == mon->output) {
1694 surface_leave(t->item->window, NULL, mon->output);
1695 break;
1696 }
1697 }
1698 }
1700 monitor_destroy(mon);
1701 tll_remove(wayl->monitors, it);
1702 goto ack_remove;
1703 }
1705 /* A seat? */
1706 tll_foreach(wayl->seats, it) {
1707 struct seat *seat = &it->item;
1709 if (seat->wl_name != name)
1710 continue;
1712 LOG_INFO("seat destroyed: %s", seat->name);
1714 if (seat->kbd_focus != NULL) {
1715 LOG_WARN("compositor destroyed seat '%s' "
1716 "without sending a keyboard leave event",
1717 seat->name);
1719 if (seat->wl_keyboard != NULL)
1720 keyboard_listener.leave(
1721 seat, seat->wl_keyboard, -1, seat->kbd_focus->window->surface.surf);
1722 }
1724 if (seat->mouse_focus != NULL) {
1725 LOG_WARN("compositor destroyed seat '%s' "
1726 "without sending a pointer leave event",
1727 seat->name);
1729 if (seat->wl_pointer != NULL)
1730 pointer_listener.leave(
1731 seat, seat->wl_pointer, -1, seat->mouse_focus->window->surface.surf);
1732 }
1734 seat_destroy(seat);
1735 tll_remove(wayl->seats, it);
1736 goto ack_remove;
1737 }
1739 LOG_WARN("unknown global removed: 0x%08x", name);
1741ack_remove:
1742 ;
1743#if defined(WL_FIXES_ACK_GLOBAL_REMOVE_SINCE_VERSION)
1744 if (wayl->fixes != NULL &&
1745 wayl->fixes_version >= WL_FIXES_ACK_GLOBAL_REMOVE_SINCE_VERSION)
1746 {
1747 wl_fixes_ack_global_remove(wayl->fixes, wayl->registry, name);
1748 }
1749#endif
1750}
1752static const struct wl_registry_listener registry_listener = {
1753 .global = &handle_global,
1754 .global_remove = &handle_global_remove,
1755};
1757static void
1758fdm_hook(struct fdm *fdm, void *data)
1759{
1760 struct wayland *wayl = data;
1761 wayl_flush(wayl);
1762}
1764static bool
1765fdm_wayl(struct fdm *fdm, int fd, int events, void *data)
1766{
1767 struct wayland *wayl = data;
1768 int event_count = 0;
1770 if (events & EPOLLIN) {
1771 if (wl_display_read_events(wayl->display) < 0) {
1772 LOG_ERRNO("failed to read events from the Wayland socket");
1773 return false;
1774 }
1776 wl_display_dispatch_pending(wayl->display);
1778 while (wl_display_prepare_read(wayl->display) != 0) {
1779 if (wl_display_dispatch_pending(wayl->display) < 0) {
1780 LOG_ERRNO("failed to dispatch pending Wayland events");
1781 return false;
1782 }
1783 }
1784 }
1786 if (events & EPOLLHUP) {
1787 LOG_WARN("disconnected from Wayland");
1788 /*
1789 * Do *not* call wl_display_cancel_read() here.
1790 *
1791 * Doing so causes later calls to wayl_roundtrip() (called
1792 * from term_destroy() -> wayl_win_destroy()) to hang
1793 * indefinitely.
1794 *
1795 * https://codeberg.org/dnkl/foot/issues/651
1796 */
1797 return false;
1798 }
1800 return event_count != -1;
1801}
1803struct wayland *
1804wayl_init(struct fdm *fdm, struct key_binding_manager *key_binding_manager,
1805 bool presentation_timings)
1806{
1807 struct wayland *wayl = calloc(1, sizeof(*wayl));
1808 if (unlikely(wayl == NULL)) {
1809 LOG_ERRNO("calloc() failed");
1810 return NULL;
1811 }
1813 wayl->fdm = fdm;
1814 wayl->key_binding_manager = key_binding_manager;
1815 wayl->fd = -1;
1816 wayl->presentation_timings = presentation_timings;
1818 if (!fdm_hook_add(fdm, &fdm_hook, wayl, FDM_HOOK_PRIORITY_LOW)) {
1819 LOG_ERR("failed to add FDM hook");
1820 goto out;
1821 }
1823 wayl->display = wl_display_connect(NULL);
1824 if (wayl->display == NULL) {
1825 LOG_ERR("failed to connect to wayland; no compositor running?");
1826 goto out;
1827 }
1829 wayl->registry = wl_display_get_registry(wayl->display);
1830 if (wayl->registry == NULL) {
1831 LOG_ERR("failed to get wayland registry");
1832 goto out;
1833 }
1835 wl_registry_add_listener(wayl->registry, ®istry_listener, wayl);
1836 wl_display_roundtrip(wayl->display);
1838 if (wayl->compositor == NULL) {
1839 LOG_ERR("no compositor");
1840 goto out;
1841 }
1842 if (wayl->sub_compositor == NULL) {
1843 LOG_ERR("no sub compositor");
1844 goto out;
1845 }
1846 if (wayl->shm == NULL) {
1847 LOG_ERR("no shared memory buffers interface");
1848 goto out;
1849 }
1850 if (wayl->shell == NULL) {
1851 LOG_ERR("no XDG shell interface");
1852 goto out;
1853 }
1854 if (wayl->data_device_manager == NULL) {
1855 LOG_ERR("no clipboard available "
1856 "(wl_data_device_manager not implemented by server)");
1857 goto out;
1858 }
1859 if (tll_length(wayl->seats) == 0) {
1860 LOG_ERR("no seats available (wl_seat interface too old?)");
1861 goto out;
1862 }
1863 if (tll_length(wayl->monitors) == 0) {
1864 LOG_ERR("no monitors available");
1865 goto out;
1866 }
1868 if (presentation_timings && wayl->presentation == NULL) {
1869 LOG_ERR("compositor does not implement the presentation time interface");
1870 goto out;
1871 }
1873 if (wayl->primary_selection_device_manager == NULL)
1874 LOG_WARN("compositor does not implement the primary selection interface");
1876 if (wayl->xdg_activation == NULL) {
1877 LOG_WARN(
1878 "compositor does not implement XDG activation, "
1879 "bell.urgent will fall back to coloring the window margins red");
1880 }
1882 if (wayl->fractional_scale_manager == NULL || wayl->viewporter == NULL)
1883 LOG_WARN("compositor does not implement fractional scaling");
1885 if (wayl->cursor_shape_manager == NULL) {
1886 LOG_WARN("compositor does not implement server-side cursors, "
1887 "falling back to client-side cursors");
1888 }
1890 if (wayl->toplevel_icon_manager == NULL) {
1891 LOG_WARN("compositor does not implement the xdg-toplevel-icon protocol");
1892 }
1894#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
1895 if (wayl->text_input_manager == NULL) {
1896 LOG_WARN("text input interface not implemented by compositor; "
1897 "IME will be disabled");
1898 }
1899#endif
1901 /* Trigger listeners registered when handling globals */
1902 wl_display_roundtrip(wayl->display);
1904 tll_foreach(wayl->monitors, it) {
1905 LOG_INFO(
1906 "%s: %dx%d+%dx%d@%dHz %s %.2f\" scale=%d, DPI=%.2f/%.2f (physical/scaled)",
1907 it->item.name, it->item.dim.px_real.width, it->item.dim.px_real.height,
1908 it->item.x, it->item.y, (int)roundf(it->item.refresh),
1909 it->item.model != NULL ? it->item.model : it->item.description,
1910 it->item.inch, it->item.scale,
1911 it->item.dpi.physical, it->item.dpi.scaled);
1912 }
1914 wayl->fd = wl_display_get_fd(wayl->display);
1915 if (fcntl(wayl->fd, F_SETFL, fcntl(wayl->fd, F_GETFL) | O_NONBLOCK) < 0) {
1916 LOG_ERRNO("failed to make Wayland socket non-blocking");
1917 goto out;
1918 }
1920 if (!fdm_add(fdm, wayl->fd, EPOLLIN, &fdm_wayl, wayl))
1921 goto out;
1923 if (wl_display_prepare_read(wayl->display) != 0) {
1924 LOG_ERRNO("failed to prepare for reading wayland events");
1925 goto out;
1926 }
1928 return wayl;
1930out:
1931 if (wayl != NULL)
1932 wayl_destroy(wayl);
1933 return NULL;
1934}
1936void
1937wayl_destroy(struct wayland *wayl)
1938{
1939 if (wayl == NULL)
1940 return;
1942 tll_foreach(wayl->terms, it) {
1943 static bool have_warned = false;
1944 if (!have_warned) {
1945 have_warned = true;
1946 LOG_WARN("there are terminals still running");
1947 term_destroy(it->item);
1948 }
1949 }
1951 tll_free(wayl->terms);
1953 fdm_hook_del(wayl->fdm, &fdm_hook, FDM_HOOK_PRIORITY_LOW);
1955 tll_foreach(wayl->monitors, it) {
1956 monitor_destroy(&it->item);
1957 tll_remove(wayl->monitors, it);
1958 }
1960 tll_foreach(wayl->seats, it) {
1961 seat_destroy(&it->item);
1962 tll_remove(wayl->seats, it);
1963 }
1965#if defined(FOOT_IME_ENABLED) && FOOT_IME_ENABLED
1966 if (wayl->text_input_manager != NULL)
1967 zwp_text_input_manager_v3_destroy(wayl->text_input_manager);
1968#endif
1970#if defined(HAVE_XDG_TOPLEVEL_TAG)
1971 if (wayl->toplevel_tag_manager != NULL)
1972 xdg_toplevel_tag_manager_v1_destroy(wayl->toplevel_tag_manager);
1973#endif
1974#if defined(HAVE_EXT_BACKGROUND_EFFECT)
1975 if (wayl->background_effect_manager != NULL)
1976 ext_background_effect_manager_v1_destroy(wayl->background_effect_manager);
1977#endif
1979 if (wayl->color_management.img_description != NULL)
1980 wp_image_description_v1_destroy(wayl->color_management.img_description);
1981 if (wayl->color_management.manager != NULL)
1982 wp_color_manager_v1_destroy(wayl->color_management.manager);
1983 if (wayl->system_bell != NULL)
1984 xdg_system_bell_v1_destroy(wayl->system_bell);
1985 if (wayl->toplevel_icon_manager != NULL)
1986 xdg_toplevel_icon_manager_v1_destroy(wayl->toplevel_icon_manager);
1987 if (wayl->single_pixel_manager != NULL)
1988 wp_single_pixel_buffer_manager_v1_destroy(wayl->single_pixel_manager);
1989 if (wayl->fractional_scale_manager != NULL)
1990 wp_fractional_scale_manager_v1_destroy(wayl->fractional_scale_manager);
1991 if (wayl->viewporter != NULL)
1992 wp_viewporter_destroy(wayl->viewporter);
1993 if (wayl->cursor_shape_manager != NULL)
1994 wp_cursor_shape_manager_v1_destroy(wayl->cursor_shape_manager);
1995 if (wayl->xdg_activation != NULL)
1996 xdg_activation_v1_destroy(wayl->xdg_activation);
1997 if (wayl->xdg_output_manager != NULL)
1998 zxdg_output_manager_v1_destroy(wayl->xdg_output_manager);
1999 if (wayl->shell != NULL)
2000 xdg_wm_base_destroy(wayl->shell);
2001 if (wayl->xdg_decoration_manager != NULL)
2002 zxdg_decoration_manager_v1_destroy(wayl->xdg_decoration_manager);
2003 if (wayl->presentation != NULL)
2004 wp_presentation_destroy(wayl->presentation);
2005 if (wayl->data_device_manager != NULL)
2006 wl_data_device_manager_destroy(wayl->data_device_manager);
2007 if (wayl->primary_selection_device_manager != NULL)
2008 zwp_primary_selection_device_manager_v1_destroy(wayl->primary_selection_device_manager);
2009 if (wayl->shm != NULL) {
2010#if defined(WL_SHM_RELEASE_SINCE_VERSION)
2011 if (wayl->use_shm_release)
2012 wl_shm_release(wayl->shm);
2013 else
2014#endif
2015 wl_shm_destroy(wayl->shm);
2016 }
2017 if (wayl->sub_compositor != NULL)
2018 wl_subcompositor_destroy(wayl->sub_compositor);
2019 if (wayl->compositor != NULL)
2020 wl_compositor_destroy(wayl->compositor);
2021#if defined(WL_FIXES_INTERFACE)
2022 if (wayl->fixes != NULL)
2023 wl_fixes_destroy(wayl->fixes);
2024#endif
2025 if (wayl->registry != NULL)
2026 wl_registry_destroy(wayl->registry);
2027 if (wayl->fd != -1)
2028 fdm_del_no_close(wayl->fdm, wayl->fd);
2029 if (wayl->display != NULL) {
2030 wayl_flush(wayl);
2031 wl_display_disconnect(wayl->display);
2032 }
2034 free(wayl);
2035}
2037static void
2038fractional_scale_preferred_scale(
2039 void *data, struct wp_fractional_scale_v1 *wp_fractional_scale_v1,
2040 uint32_t scale)
2041{
2042 struct wl_window *win = data;
2044 const float new_scale = (float)scale / 120.;
2046 if (win->scale == new_scale)
2047 return;
2049 LOG_DBG("fractional scale: %.2f -> %.2f", win->scale, new_scale);
2051 win->scale = new_scale;
2052 update_term_for_output_change(win->term);
2053}
2055static const struct wp_fractional_scale_v1_listener fractional_scale_listener = {
2056 .preferred_scale = &fractional_scale_preferred_scale,
2057};
2059struct wl_window *
2060wayl_win_init(struct terminal *term, const char *token)
2061{
2062 struct wayland *wayl = term->wl;
2063 const struct config *conf = term->conf;
2065 struct wl_window *win = calloc(1, sizeof(*win));
2066 if (unlikely(win == NULL)) {
2067 LOG_ERRNO("calloc() failed");
2068 return NULL;
2069 }
2071 win->term = term;
2072 win->csd_mode = CSD_UNKNOWN;
2073 win->csd.move_timeout_fd = -1;
2074 win->resize_timeout_fd = -1;
2075 win->scale = -1.;
2077 win->wm_capabilities.maximize = true;
2078 win->wm_capabilities.minimize = true;
2080 win->surface.surf = wl_compositor_create_surface(wayl->compositor);
2081 if (win->surface.surf == NULL) {
2082 LOG_ERR("failed to create wayland surface");
2083 goto out;
2084 }
2086 wl_surface_add_listener(win->surface.surf, &surface_listener, win);
2088 if (wayl->fractional_scale_manager != NULL && wayl->viewporter != NULL) {
2089 win->surface.viewport = wp_viewporter_get_viewport(wayl->viewporter, win->surface.surf);
2091 win->fractional_scale =
2092 wp_fractional_scale_manager_v1_get_fractional_scale(
2093 wayl->fractional_scale_manager, win->surface.surf);
2094 wp_fractional_scale_v1_add_listener(
2095 win->fractional_scale, &fractional_scale_listener, win);
2096 }
2098#if defined(HAVE_EXT_BACKGROUND_EFFECT)
2099 if (wayl->background_effect_manager != NULL) {
2100 win->surface.background_effect =
2101 ext_background_effect_manager_v1_get_background_effect(
2102 wayl->background_effect_manager, win->surface.surf);
2103 }
2104#endif
2106 wayl_win_alpha_changed(win);
2108 win->xdg_surface = xdg_wm_base_get_xdg_surface(wayl->shell, win->surface.surf);
2109 xdg_surface_add_listener(win->xdg_surface, &xdg_surface_listener, win);
2111 win->xdg_toplevel = xdg_surface_get_toplevel(win->xdg_surface);
2112 xdg_toplevel_add_listener(win->xdg_toplevel, &xdg_toplevel_listener, win);
2114 xdg_toplevel_set_app_id(win->xdg_toplevel, conf->app_id);
2116#if defined(HAVE_XDG_TOPLEVEL_TAG)
2117 if (conf->toplevel_tag != NULL && conf->toplevel_tag[0] != '\0') {
2118 if (wayl->toplevel_tag_manager != NULL) {
2119 xdg_toplevel_tag_manager_v1_set_toplevel_tag(
2120 wayl->toplevel_tag_manager, win->xdg_toplevel, conf->toplevel_tag);
2122 /* TODO: the description is recommended to be the tag, but translated */
2123 xdg_toplevel_tag_manager_v1_set_toplevel_description(
2124 wayl->toplevel_tag_manager, win->xdg_toplevel, conf->toplevel_tag);
2125 } else {
2126 LOG_WARN("compositor does not implement the xdg-toplevel-tag protocol");
2127 }
2128 }
2129#endif
2131 if (wayl->toplevel_icon_manager != NULL) {
2132 const char *app_id =
2133 term->app_id != NULL ? term->app_id : term->conf->app_id;
2135 struct xdg_toplevel_icon_v1 *icon =
2136 xdg_toplevel_icon_manager_v1_create_icon(wayl->toplevel_icon_manager);
2137 xdg_toplevel_icon_v1_set_name(icon, streq(
2138 app_id, "footclient") ? "foot" : app_id);
2139 xdg_toplevel_icon_manager_v1_set_icon(
2140 wayl->toplevel_icon_manager, win->xdg_toplevel, icon);
2141 xdg_toplevel_icon_v1_destroy(icon);
2142 }
2144 if (term->conf->gamma_correct) {
2145 if (wayl->color_management.img_description != NULL) {
2146 xassert(wayl->color_management.manager != NULL);
2148 win->surface.color_management = wp_color_manager_v1_get_surface(
2149 term->wl->color_management.manager, win->surface.surf);
2151 wp_color_management_surface_v1_set_image_description(
2152 win->surface.color_management, wayl->color_management.img_description,
2153 WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL);
2154 } else {
2155 if (wayl->color_management.manager == NULL) {
2156 LOG_WARN(
2157 "gamma-corrected-blending: disabling; "
2158 "compositor does not implement the color-management protocol");
2159 } else {
2160 LOG_WARN(
2161 "gamma-corrected-blending: disabling; "
2162 "compositor does not implement all required color-management features");
2163 LOG_WARN("use e.g. 'wayland-info' and verify the compositor implements:");
2164 LOG_WARN(" - feature: parametric");
2165 LOG_WARN(" - render intent: perceptual");
2166 LOG_WARN(" - TF: ext_linear");
2167 LOG_WARN(" - primaries: sRGB");
2168 }
2169 }
2170 }
2172 if (conf->csd.preferred == CONF_CSD_PREFER_NONE) {
2173 /* User specifically do *not* want decorations */
2174 win->csd_mode = CSD_NO;
2175 LOG_INFO("window decorations disabled by user");
2176 } else if (wayl->xdg_decoration_manager != NULL) {
2177 win->xdg_toplevel_decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(
2178 wayl->xdg_decoration_manager, win->xdg_toplevel);
2180 LOG_INFO("requesting %s decorations",
2181 conf->csd.preferred == CONF_CSD_PREFER_SERVER ? "SSD" : "CSD");
2183 zxdg_toplevel_decoration_v1_set_mode(
2184 win->xdg_toplevel_decoration,
2185 (conf->csd.preferred == CONF_CSD_PREFER_SERVER
2186 ? ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE
2187 : ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE));
2189 zxdg_toplevel_decoration_v1_add_listener(
2190 win->xdg_toplevel_decoration, &xdg_toplevel_decoration_listener, win);
2191 } else {
2192 /* No decoration manager - thus we *must* draw our own decorations */
2193 win->configure.csd_mode = CSD_YES;
2194 LOG_WARN("no decoration manager available - using CSDs unconditionally");
2195 }
2197 wl_surface_commit(win->surface.surf);
2199 /* Complete XDG startup notification */
2200 wayl_activate(wayl, win, token);
2202 if (!wayl_win_subsurface_new(win, &win->overlay, false)) {
2203 LOG_ERR("failed to create overlay surface");
2204 goto out;
2205 }
2207 switch (conf->tweak.render_timer) {
2208 case RENDER_TIMER_OSD:
2209 case RENDER_TIMER_BOTH:
2210 if (!wayl_win_subsurface_new(win, &win->render_timer, false)) {
2211 LOG_ERR("failed to create render timer surface");
2212 goto out;
2213 }
2214 break;
2216 case RENDER_TIMER_NONE:
2217 case RENDER_TIMER_LOG:
2218 break;
2219 }
2221 return win;
2223out:
2224 if (win != NULL)
2225 wayl_win_destroy(win);
2226 return NULL;
2227}
2229void
2230wayl_win_destroy(struct wl_window *win)
2231{
2232 if (win == NULL)
2233 return;
2235 struct terminal *term = win->term;
2237 if (win->csd.move_timeout_fd != -1)
2238 close(win->csd.move_timeout_fd);
2240 /*
2241 * wl_touch has no leave event, so the unmapping below cannot drop touch
2242 * focus the way it drops keyboard and pointer focus. Reset it here, or a
2243 * wl_touch.up/motion dispatched from the roundtrips below runs against
2244 * this window after it has been destroyed.
2245 */
2246 tll_foreach(term->wl->seats, it) {
2247 struct seat *seat = &it->item;
2249 if (seat->touch.surface != NULL &&
2250 wl_surface_get_user_data(seat->touch.surface) == win)
2251 {
2252 seat->touch.state = TOUCH_STATE_IDLE;
2253 seat->touch.surface = NULL;
2254 }
2255 }
2257 /*
2258 * First, unmap all surfaces to trigger things like
2259 * keyboard_leave() and wl_pointer_leave().
2260 *
2261 * This ensures we remove all references to *this* window from the
2262 * global wayland struct (since it no longer has neither keyboard
2263 * nor mouse focus).
2264 */
2266 if (win->render_timer.surface.surf != NULL) {
2267 wl_surface_attach(win->render_timer.surface.surf, NULL, 0, 0);
2268 wl_surface_commit(win->render_timer.surface.surf);
2269 }
2271 if (win->scrollback_indicator.surface.surf != NULL) {
2272 wl_surface_attach(win->scrollback_indicator.surface.surf, NULL, 0, 0);
2273 wl_surface_commit(win->scrollback_indicator.surface.surf);
2274 }
2276 if (win->overlay.surface.surf != NULL) {
2277 wl_surface_attach(win->overlay.surface.surf, NULL, 0, 0);
2278 wl_surface_commit(win->overlay.surface.surf);
2279 }
2281 /* Scrollback search */
2282 if (win->search.surface.surf != NULL) {
2283 wl_surface_attach(win->search.surface.surf, NULL, 0, 0);
2284 wl_surface_commit(win->search.surface.surf);
2285 }
2287 /* URLs */
2288 tll_foreach(win->urls, it) {
2289 wl_surface_attach(it->item.surf.surface.surf, NULL, 0, 0);
2290 wl_surface_commit(it->item.surf.surface.surf);
2291 }
2293 /* CSD */
2294 for (size_t i = 0; i < ALEN(win->csd.surface); i++) {
2295 if (win->csd.surface[i].surface.surf != NULL) {
2296 wl_surface_attach(win->csd.surface[i].surface.surf, NULL, 0, 0);
2297 wl_surface_commit(win->csd.surface[i].surface.surf);
2298 }
2299 }
2301 wayl_roundtrip(win->term->wl);
2303 /* Main window */
2304 win->unmapped = true;
2305 wl_surface_attach(win->surface.surf, NULL, 0, 0);
2306 wl_surface_commit(win->surface.surf);
2307 wayl_roundtrip(win->term->wl);
2309 tll_free(win->on_outputs);
2311 tll_foreach(win->urls, it) {
2312 wayl_win_subsurface_destroy(&it->item.surf);
2313 tll_remove(win->urls, it);
2314 }
2316 render_wait_for_preapply_damage(term);
2318 csd_destroy(win);
2319 wayl_win_subsurface_destroy(&win->search);
2320 wayl_win_subsurface_destroy(&win->scrollback_indicator);
2321 wayl_win_subsurface_destroy(&win->render_timer);
2322 wayl_win_subsurface_destroy(&win->overlay);
2324 shm_purge(term->render.chains.search);
2325 shm_purge(term->render.chains.scrollback_indicator);
2326 shm_purge(term->render.chains.render_timer);
2327 shm_purge(term->render.chains.grid);
2328 shm_purge(term->render.chains.url);
2329 shm_purge(term->render.chains.csd);
2331 tll_foreach(win->xdg_tokens, it) {
2332 xdg_activation_token_v1_destroy(it->item->xdg_token);
2333 free(it->item);
2335 tll_remove(win->xdg_tokens, it);
2336 }
2338#if defined(HAVE_EXT_BACKGROUND_EFFECT)
2339 if (win->surface.background_effect != NULL)
2340 ext_background_effect_surface_v1_destroy(win->surface.background_effect);
2341#endif
2343 if (win->surface.color_management != NULL)
2344 wp_color_management_surface_v1_destroy(win->surface.color_management);
2345 if (win->fractional_scale != NULL)
2346 wp_fractional_scale_v1_destroy(win->fractional_scale);
2347 if (win->surface.viewport != NULL)
2348 wp_viewport_destroy(win->surface.viewport);
2349 if (win->frame_callback != NULL)
2350 wl_callback_destroy(win->frame_callback);
2351 if (win->xdg_toplevel_decoration != NULL)
2352 zxdg_toplevel_decoration_v1_destroy(win->xdg_toplevel_decoration);
2353 if (win->xdg_toplevel != NULL)
2354 xdg_toplevel_destroy(win->xdg_toplevel);
2355 if (win->xdg_surface != NULL)
2356 xdg_surface_destroy(win->xdg_surface);
2357 if (win->surface.surf != NULL)
2358 wl_surface_destroy(win->surface.surf);
2360 wayl_roundtrip(win->term->wl);
2362 if (win->resize_timeout_fd >= 0)
2363 fdm_del(win->term->wl->fdm, win->resize_timeout_fd);
2364 free(win);
2365}
2367bool
2368wayl_reload_xcursor_theme(struct seat *seat, float new_scale)
2369{
2370 if (seat->pointer.theme != NULL && seat->pointer.scale == new_scale) {
2371 /* We already have a theme loaded, and the scale hasn't changed */
2372 return true;
2373 }
2375 if (seat->pointer.theme != NULL) {
2376 xassert(seat->pointer.scale != new_scale);
2377 wl_cursor_theme_destroy(seat->pointer.theme);
2378 seat->pointer.theme = NULL;
2379 seat->pointer.cursor = NULL;
2380 }
2382 if (seat->pointer.shape_device != NULL) {
2383 /* Using server side cursors */
2384 return true;
2385 }
2387 int xcursor_size = 24;
2389 {
2390 const char *env_cursor_size = getenv("XCURSOR_SIZE");
2391 if (env_cursor_size != NULL) {
2392 errno = 0;
2393 char *end;
2394 int size = (int)strtol(env_cursor_size, &end, 10);
2396 if (errno == 0 && *end == '\0' && size > 0)
2397 xcursor_size = size;
2398 else
2399 LOG_WARN("XCURSOR_SIZE '%s' is invalid, defaulting to 24",
2400 env_cursor_size);
2401 }
2402 }
2404 const char *xcursor_theme = getenv("XCURSOR_THEME");
2406 LOG_INFO("cursor theme: %s, size: %d, scale: %.2f",
2407 xcursor_theme ? xcursor_theme : "(null)",
2408 xcursor_size, new_scale);
2410 seat->pointer.theme = wl_cursor_theme_load(
2411 xcursor_theme, xcursor_size * new_scale, seat->wayl->shm);
2413 if (seat->pointer.theme == NULL) {
2414 LOG_ERR("failed to load cursor theme");
2415 return false;
2416 }
2418 seat->pointer.scale = new_scale;
2419 return true;
2420}
2422void
2423wayl_flush(struct wayland *wayl)
2424{
2425 while (true) {
2426 int r = wl_display_flush(wayl->display);
2427 if (r >= 0) {
2428 /* Most likely code path - the flush succeed */
2429 return;
2430 }
2432 if (errno == EINTR) {
2433 /* Unlikely */
2434 continue;
2435 }
2437 if (errno != EAGAIN) {
2438 const int saved_errno = errno;
2440 if (errno == EPIPE) {
2441 wl_display_read_events(wayl->display);
2442 wl_display_dispatch_pending(wayl->display);
2443 }
2445 LOG_ERRNO_P(saved_errno, "failed to flush wayland socket");
2446 return;
2447 }
2449 /* Socket buffer is full - need to wait for it to become
2450 writeable again */
2451 xassert(errno == EAGAIN);
2453 while (true) {
2454 struct pollfd fds[] = {{.fd = wayl->fd, .events = POLLOUT}};
2456 r = poll(fds, sizeof(fds) / sizeof(fds[0]), -1);
2458 if (r < 0) {
2459 if (errno == EINTR)
2460 continue;
2462 LOG_ERRNO("failed to poll");
2463 return;
2464 }
2466 if (fds[0].revents & POLLHUP)
2467 return;
2469 xassert(fds[0].revents & POLLOUT);
2470 break;
2471 }
2472 }
2473}
2475void
2476wayl_roundtrip(struct wayland *wayl)
2477{
2478 wl_display_cancel_read(wayl->display);
2479 if (wl_display_roundtrip(wayl->display) < 0) {
2480 LOG_ERRNO("failed to roundtrip Wayland display");
2481 return;
2482 }
2484 /* I suspect the roundtrip above clears the pending queue, and
2485 * that prepare_read() will always succeed in the first call. But,
2486 * better safe than sorry... */
2488 while (wl_display_prepare_read(wayl->display) != 0) {
2489 if (wl_display_dispatch_pending(wayl->display) < 0) {
2490 LOG_ERRNO("failed to dispatch pending Wayland events");
2491 return;
2492 }
2493 }
2494 wayl_flush(wayl);
2495}
2497static void
2498surface_scale_explicit_width_height(
2499 const struct wl_window *win, const struct wayl_surface *surf,
2500 int width, int height, float scale, bool verify)
2501{
2502 if (term_fractional_scaling(win->term)) {
2503 LOG_DBG("scaling by a factor of %.2f using fractional scaling "
2504 "(width=%d, height=%d) ", scale, width, height);
2506 if (verify) {
2507 if ((int)roundf(scale * (int)roundf(width / scale)) != width) {
2508 BUG("width=%d is not valid with scaling factor %.2f (%d != %d)",
2509 width, scale,
2510 (int)roundf(scale * (int)roundf(width / scale)),
2511 width);
2512 }
2514 if ((int)roundf(scale * (int)roundf(height / scale)) != height) {
2515 BUG("height=%d is not valid with scaling factor %.2f (%d != %d)",
2516 height, scale,
2517 (int)roundf(scale * (int)roundf(height / scale)),
2518 height);
2519 }
2520 }
2522 xassert(surf->viewport != NULL);
2523 wl_surface_set_buffer_scale(surf->surf, 1);
2524 wp_viewport_set_destination(
2525 surf->viewport, roundf(width / scale), roundf(height / scale));
2526 } else {
2527 const char *mode UNUSED = term_preferred_buffer_scale(win->term)
2528 ? "wl_surface.preferred_buffer_scale"
2529 : "legacy mode";
2530 LOG_DBG("scaling by a factor of %.2f using %s "
2531 "(width=%d, height=%d)" , scale, mode, width, height);
2533 xassert(scale == floorf(scale));
2534 const int iscale = (int)floorf(scale);
2536 if (verify) {
2537 if (width % iscale != 0) {
2538 BUG("width=%d is not valid with scaling factor %.2f (%d %% %d != 0)",
2539 width, scale, width, iscale);
2540 }
2542 if (height % iscale != 0) {
2543 BUG("height=%d is not valid with scaling factor %.2f (%d %% %d != 0)",
2544 height, scale, height, iscale);
2545 }
2546 }
2548 wl_surface_set_buffer_scale(surf->surf, iscale);
2549 }
2550}
2552void
2553wayl_surface_scale_explicit_width_height(
2554 const struct wl_window *win, const struct wayl_surface *surf,
2555 int width, int height, float scale)
2556{
2557 surface_scale_explicit_width_height(win, surf, width, height, scale, false);
2558}
2560void
2561wayl_surface_scale(const struct wl_window *win, const struct wayl_surface *surf,
2562 const struct buffer *buf, float scale)
2563{
2564 surface_scale_explicit_width_height(
2565 win, surf, buf->width, buf->height, scale, true);
2566}
2568void
2569wayl_win_scale(struct wl_window *win, const struct buffer *buf)
2570{
2571 const struct terminal *term = win->term;
2572 const float scale = term->scale;
2574 wayl_surface_scale(win, &win->surface, buf, scale);
2575}
2577void
2578wayl_win_alpha_changed(struct wl_window *win)
2579{
2580 struct terminal *term = win->term;
2581 struct wayland *wayl = term->wl;
2583 /*
2584 * When fullscreened, transparency is disabled (see render.c).
2585 * Update the opaque region to match.
2586 */
2587 const bool is_opaque = term->colors.alpha == 0xffff || win->is_fullscreen;
2589 if (is_opaque) {
2590 struct wl_region *region = wl_compositor_create_region(wayl->compositor);
2592 if (region != NULL) {
2593 wl_region_add(region, 0, 0, INT32_MAX, INT32_MAX);
2594 wl_surface_set_opaque_region(win->surface.surf, region);
2595 wl_region_destroy(region);
2596 }
2597 } else
2598 wl_surface_set_opaque_region(win->surface.surf, NULL);
2600#if defined(HAVE_EXT_BACKGROUND_EFFECT)
2601 if (term_theme_get(term)->blur) {
2602 if (wayl->have_background_blur) {
2603 xassert(win->surface.background_effect != NULL);
2605 if (is_opaque) {
2606 /* No transparency, disable blur */
2607 LOG_DBG("disabling background blur");
2608 ext_background_effect_surface_v1_set_blur_region(
2609 win->surface.background_effect, NULL);
2610 } else {
2611 /* We have transparency, enable blur if user has enabled it */
2612 struct wl_region *region = wl_compositor_create_region(wayl->compositor);
2613 if (region != NULL) {
2614 LOG_DBG("enabling background blur");
2616 wl_region_add(region, 0, 0, INT32_MAX, INT32_MAX);
2617 ext_background_effect_surface_v1_set_blur_region(
2618 win->surface.background_effect, region);
2619 wl_region_destroy(region);
2620 }
2621 }
2622 } else {
2623 static bool have_warned = false;
2624 if (!have_warned) {
2625 LOG_WARN("background blur requested, but compositor does not support it");
2626 have_warned = true;
2627 }
2628 }
2629 }
2630#endif /* HAVE_EXT_BACKGROUND_EFFECT */
2631}
2633static void
2634activation_token_for_urgency_done(const char *token, void *data)
2635{
2636 struct wl_window *win = data;
2637 struct wayland *wayl = win->term->wl;
2639 win->urgency_token_is_pending = false;
2640 xdg_activation_v1_activate(wayl->xdg_activation, token, win->surface.surf);
2641}
2643bool
2644wayl_win_set_urgent(struct wl_window *win)
2645{
2646 if (win->urgency_token_is_pending) {
2647 /* We already have a pending token. Don't request another one,
2648 * to avoid flooding the Wayland socket */
2649 return true;
2650 }
2652 bool success = wayl_get_activation_token(
2653 win->term->wl, NULL, 0, win, &activation_token_for_urgency_done, win);
2655 if (success) {
2656 win->urgency_token_is_pending = true;
2657 return true;
2658 }
2660 return false;
2661}
2663bool
2664wayl_win_ring_bell(const struct wl_window *win)
2665{
2666 if (win->term->wl->system_bell == NULL) {
2667 static bool have_warned = false;
2669 if (!have_warned) {
2670 LOG_WARN("compositor does not implement the XDG system bell protocol");
2671 have_warned = true;
2672 }
2674 return false;
2675 }
2677 xdg_system_bell_v1_ring(win->term->wl->system_bell, win->surface.surf);
2678 return true;
2679}
2681bool
2682wayl_win_csd_titlebar_visible(const struct wl_window *win)
2683{
2684 return win->csd_mode == CSD_YES &&
2685 !win->is_fullscreen &&
2686 !(win->is_maximized && win->term->conf->csd.hide_when_maximized);
2687}
2689bool
2690wayl_win_csd_borders_visible(const struct wl_window *win)
2691{
2692 return win->csd_mode == CSD_YES &&
2693 !win->is_fullscreen &&
2694 !win->is_maximized;
2695}
2697bool
2698wayl_win_subsurface_new_with_custom_parent(
2699 struct wl_window *win, struct wl_surface *parent,
2700 struct wayl_sub_surface *surf, bool allow_pointer_input)
2701{
2702 struct wayland *wayl = win->term->wl;
2704 surf->surface.surf = NULL;
2705 surf->surface.viewport = NULL;
2706 surf->sub = NULL;
2708 struct wl_surface *main_surface
2709 = wl_compositor_create_surface(wayl->compositor);
2711 if (main_surface == NULL) {
2712 LOG_ERR("failed to instantiate surface for sub-surface");
2713 return false;
2714 }
2716 surf->surface.color_management = NULL;
2717 if (win->term->conf->gamma_correct &&
2718 wayl->color_management.img_description != NULL)
2719 {
2720 xassert(wayl->color_management.manager != NULL);
2722 surf->surface.color_management = wp_color_manager_v1_get_surface(
2723 wayl->color_management.manager, main_surface);
2725 wp_color_management_surface_v1_set_image_description(
2726 surf->surface.color_management, wayl->color_management.img_description,
2727 WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL);
2728 }
2730 struct wl_subsurface *sub = wl_subcompositor_get_subsurface(
2731 wayl->sub_compositor, main_surface, parent);
2733 if (sub == NULL) {
2734 LOG_ERR("failed to instantiate sub-surface");
2735 wl_surface_destroy(main_surface);
2736 return false;
2737 }
2739 struct wp_viewport *viewport = NULL;
2740 if (wayl->viewporter != NULL) {
2741 viewport = wp_viewporter_get_viewport(wayl->viewporter, main_surface);
2742 if (viewport == NULL) {
2743 LOG_ERR("failed to instantiate viewport for sub-surface");
2744 wl_subsurface_destroy(sub);
2745 wl_surface_destroy(main_surface);
2746 return false;
2747 }
2748 }
2750 wl_surface_set_user_data(main_surface, win);
2751 wl_subsurface_set_sync(sub);
2753 /* Disable pointer and touch events */
2754 if (!allow_pointer_input) {
2755 struct wl_region *empty =
2756 wl_compositor_create_region(wayl->compositor);
2757 wl_surface_set_input_region(main_surface, empty);
2758 wl_region_destroy(empty);
2759 }
2761 surf->surface.surf = main_surface;
2762 surf->sub = sub;
2763 surf->surface.viewport = viewport;
2764 return true;
2765}
2767bool
2768wayl_win_subsurface_new(struct wl_window *win, struct wayl_sub_surface *surf,
2769 bool allow_pointer_input)
2770{
2771 return wayl_win_subsurface_new_with_custom_parent(
2772 win, win->surface.surf, surf, allow_pointer_input);
2773}
2775void
2776wayl_win_subsurface_destroy(struct wayl_sub_surface *surf)
2777{
2778 if (surf == NULL)
2779 return;
2781 if (surf->surface.color_management != NULL) {
2782 wp_color_management_surface_v1_destroy(surf->surface.color_management);
2783 surf->surface.color_management = NULL;
2784 }
2786 if (surf->surface.viewport != NULL) {
2787 wp_viewport_destroy(surf->surface.viewport);
2788 surf->surface.viewport = NULL;
2789 }
2791 if (surf->sub != NULL) {
2792 wl_subsurface_destroy(surf->sub);
2793 surf->sub = NULL;
2794 }
2795 if (surf->surface.surf != NULL) {
2796 wl_surface_destroy(surf->surface.surf);
2797 surf->surface.surf = NULL;
2798 }
2799}
2801static void
2802activation_token_done(void *data, struct xdg_activation_token_v1 *xdg_token,
2803 const char *token)
2804{
2805 LOG_DBG("XDG activation token done: %s", token);
2807 struct xdg_activation_token_context *ctx = data;
2808 struct wl_window *win = ctx->win;
2810 ctx->cb(token, ctx->cb_data);
2812 tll_foreach(win->xdg_tokens, it) {
2813 if (it->item->xdg_token != xdg_token)
2814 continue;
2816 xassert(win == it->item->win);
2818 free(ctx);
2819 xdg_activation_token_v1_destroy(xdg_token);
2820 tll_remove(win->xdg_tokens, it);
2821 return;
2822 }
2824 BUG("activation token not found in list");
2825}
2827static const struct
2828xdg_activation_token_v1_listener activation_token_listener = {
2829 .done = &activation_token_done,
2830};
2832bool
2833wayl_get_activation_token(
2834 struct wayland *wayl, struct seat *seat, uint32_t serial,
2835 struct wl_window *win,
2836 void (*cb)(const char *token, void *data), void *cb_data)
2837{
2838 if (wayl->xdg_activation == NULL)
2839 return false;
2841 struct xdg_activation_token_v1 *token =
2842 xdg_activation_v1_get_activation_token(wayl->xdg_activation);
2844 if (token == NULL) {
2845 LOG_ERR("failed to retrieve XDG activation token");
2846 return false;
2847 }
2849 struct xdg_activation_token_context *ctx = xmalloc(sizeof(*ctx));
2850 *ctx = (struct xdg_activation_token_context){
2851 .win = win,
2852 .xdg_token = token,
2853 .cb = cb,
2854 .cb_data = cb_data,
2855 };
2856 tll_push_back(win->xdg_tokens, ctx);
2858 if (seat != NULL && serial != 0)
2859 xdg_activation_token_v1_set_serial(token, serial, seat->wl_seat);
2861 xdg_activation_token_v1_set_surface(token, win->surface.surf);
2862 xdg_activation_token_v1_add_listener(token, &activation_token_listener, ctx);
2863 xdg_activation_token_v1_commit(token);
2864 return true;
2865}
2867void
2868wayl_activate(struct wayland *wayl, struct wl_window *win, const char *token)
2869{
2870 if (wayl->xdg_activation == NULL)
2871 return;
2873 if (token == NULL)
2874 return;
2876 xdg_activation_v1_activate(wayl->xdg_activation, token, win->surface.surf);
2877}
2879bool
2880wayl_do_linear_blending(const struct wayland *wayl, const struct config *conf)
2881{
2882 return conf->gamma_correct &&
2883 wayl->color_management.img_description != NULL;
2884}
Diagnostics5 git calls · 1.3 s
CallStatusSizeTime01.3 s
discover200178 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB489 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=477 git.response.bytes=10322 git.response.cache=EXPIRED git.response.duration_ms=489 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchtree:0 · deepen 102009.3 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=8830 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=8830 git.pkt.payload.max=8730 git.sideband.band1.bytes=8730 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=14 git.pack.bytes=8730 git.fetch.wide.objects=14 git.fetch.wide.extra_bytes=9909 git.fetch.wide.blobs=0
fetch20018 KiB782 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=782 git.response.bytes=18365 git.response.cache=MISS git.response.duration_ms=782 git.fetch.wants=a47764e4f155037fa1c9745563ff7e5915b30457 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=18365 git.pkt.payload.max=18338 git.sideband.band1.bytes=18338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5e1b5437ed75b65
- Request
- 76521f52…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format