Blender V5.3
GHOST_WindowWayland.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
11#include "GHOST_Types.hh"
12#include "GHOST_WaylandUtils.hh"
14#include "GHOST_utildefines.hh"
15
16#include "GHOST_Event.hh"
17
18#include "GHOST_ContextNone.hh"
19#ifdef WITH_OPENGL_BACKEND
20# include "GHOST_ContextEGL.hh"
21#endif
22#ifdef WITH_VULKAN_BACKEND
23# include "GHOST_ContextVK.hh"
24#endif
25
26#include <wayland-client-protocol.h>
27
28#ifdef WITH_OPENGL_BACKEND
29# ifdef WITH_GHOST_WAYLAND_DYNLOAD
30# include <wayland_dynload_egl.h>
31# endif
32# include <wayland-egl.h>
33#endif
34
35#include <algorithm> /* For `std::ranges::find`. */
36
37/* Generated by `wayland-scanner`. */
38#include <color-management-v1-client-protocol.h>
39#include <fractional-scale-v1-client-protocol.h>
40#include <viewporter-client-protocol.h>
41#include <xdg-activation-v1-client-protocol.h>
42#include <xdg-decoration-unstable-v1-client-protocol.h>
43#include <xdg-shell-client-protocol.h>
44
45#include <atomic>
46#include <optional>
47
48#include <cstring> /* For `memcpy`. */
49#include <sys/mman.h> /* For `munmap`. */
50
51/* Logging, use `ghost.wl.*` prefix. */
52#include "CLG_log.h"
53
54/* Disable, as this can "lock" the GUI even with the *pending* version of dispatch is used. */
55#if 0
70# define USE_CURSOR_IMMEDIATE_DISPATCH
71#endif
72
73static const xdg_activation_token_v1_listener *xdg_activation_listener_get();
74
75static constexpr size_t base_dpi = 96;
76
77#ifdef WITH_GHOST_CSD
78
79struct GWL_WindowCSD {
80 GHOST_CSD_Elem csd_elems[GHOST_kCSDType_NUM] = {};
81 int csd_elems_num = 0;
82
86 GHOST_CSD_EventState event_state = {{0}};
87
97 wl_surface *margin_surface = nullptr;
98 wl_subsurface *margin_subsurface = nullptr;
99 wp_viewport *margin_viewport = nullptr;
100 wl_buffer *margin_buffer = nullptr;
101
107 int32_t margin_size_local = 0;
108 int32_t margin_window_size_local[2] = {0, 0};
109};
110
111#endif /* WITH_GHOST_CSD */
112
114 xdg_surface *surface = nullptr;
115 zxdg_toplevel_decoration_v1 *toplevel_decor = nullptr;
116 xdg_toplevel *toplevel = nullptr;
117 enum zxdg_toplevel_decoration_v1_mode mode = (enum zxdg_toplevel_decoration_v1_mode)0;
118
123 struct {
125 bool ack_configure = false;
127 uint32_t ack_configure_serial = 0;
129
133 int initial_bounds[2] = {0, 0};
134};
135
137{
138 if (decor->toplevel_decor) {
139 zxdg_toplevel_decoration_v1_destroy(decor->toplevel_decor);
140 }
141 xdg_toplevel_destroy(decor->toplevel);
142 xdg_surface_destroy(decor->surface);
143 delete decor;
144}
145
146/* -------------------------------------------------------------------- */
149
150static void gwl_round_int_by(int *value_p, const int round_value)
151{
152 GHOST_ASSERT(round_value > 0, "Invalid rounding value!");
153 *value_p = (*value_p / round_value) * round_value;
154}
155
156static void gwl_round_int2_by(int value_p[2], const int round_value)
157{
158 GHOST_ASSERT(round_value > 0, "Invalid rounding value!");
159 value_p[0] = (value_p[0] / round_value) * round_value;
160 value_p[1] = (value_p[1] / round_value) * round_value;
161}
162
166static bool gwl_round_int_test(const int value, const int round_value)
167{
168 return value == ((value / round_value) * round_value);
169}
170
172
173/* -------------------------------------------------------------------- */
176
178 bool is_fractional = false;
184 wl_fixed_t scale = 0;
185};
186
188 wl_fixed_t value)
189{
190 if (scale_params.is_fractional) {
191 return (value * scale_params.scale) / FRACTIONAL_DENOMINATOR;
192 }
193 return value * scale_params.scale;
194}
196 wl_fixed_t value)
197{
198 if (scale_params.is_fractional) {
199 return (value * FRACTIONAL_DENOMINATOR) / scale_params.scale;
200 }
201 return value / scale_params.scale;
202}
203
204int gwl_window_scale_int_to(const GWL_WindowScaleParams &scale_params, const int value)
205{
206 return wl_fixed_to_int(gwl_window_scale_wl_fixed_to(scale_params, wl_fixed_from_int(value)));
207}
208int gwl_window_scale_int_from(const GWL_WindowScaleParams &scale_params, const int value)
209{
210 return wl_fixed_to_int(gwl_window_scale_wl_fixed_from(scale_params, wl_fixed_from_int(value)));
211}
212
213static uint divide_ceil_uint(const uint x, const uint d)
214{
215 return ((x + d) - 1) / d;
216}
217
219 const int logical_size,
220 int *r_buffer_scale)
221{
222 /* The buffer scale must be an integer, so use ceiling-division to round up.
223 * This is a no-op for integer scales but ensures fractional scales
224 * (e.g. 1.5x) produce a buffer whose logical size matches what the compositor requested. */
225 const int buffer_scale = divide_ceil_uint(gwl_window_scale_int_to(scale_params, logical_size),
226 logical_size);
227 *r_buffer_scale = buffer_scale;
228 return logical_size * buffer_scale;
229}
230
232
233/* -------------------------------------------------------------------- */
236
238{
239 cg->free_fn(cg);
240}
241
243{
244 if (*cg == nullptr) {
245 return;
246 }
248 *cg = nullptr;
249}
250
252 GHOST_SystemWayland *system)
253{
254 return system->cursor_shape_custom_set(cg);
255}
256
259{
260#ifdef USE_EVENT_BACKGROUND_THREAD
261 GHOST_ASSERT(system->main_thread_id == std::this_thread::get_id(), "Only from main thread!");
262#endif
263
264 if (shape == GHOST_kStandardCursorCustom) {
265 const GHOST_TSuccess ok = gwl_window_cursor_custom_load(*cg, system);
266 if (ok == GHOST_kSuccess) {
267 return ok;
268 }
270 system->cursor_shape_set(shape);
271 return GHOST_kFailure;
272 }
273
274 return system->cursor_shape_set(shape);
275}
276
278
279/* -------------------------------------------------------------------- */
282
283#ifdef USE_EVENT_BACKGROUND_THREAD
284
320# define PENDING_NUM (PENDING_WINDOW_CURSOR_SHAPE_REFRESH + 1)
321
322#endif /* USE_EVENT_BACKGROUND_THREAD */
323
350
352
354 struct {
355 wl_surface *surface = nullptr;
356 } wl;
357
359 struct {
360 wp_viewport *viewport = nullptr;
361
366 wp_fractional_scale_v1 *fractional_scale_handle = nullptr;
367 wp_color_management_surface_v1 *color_management_surface = nullptr;
368 } wp;
369
371 struct {
373 xdg_activation_token_v1 *activation_token = nullptr;
375
376 struct {
377#ifdef WITH_OPENGL_BACKEND
378 wl_egl_window *egl_window = nullptr;
379#endif
380#ifdef WITH_VULKAN_BACKEND
381 GHOST_ContextVK_WindowInfo *vulkan_window_info = nullptr;
382#endif
384
388
395 std::vector<GWL_Output *> outputs;
396
399
400#ifdef WITH_GHOST_CSD
402 GWL_WindowCSD *xdg_csd = nullptr;
408 uint32_t wm_capabilities_mask = ~uint32_t(0);
409#endif
410
417
418#ifdef USE_EVENT_BACKGROUND_THREAD
424#endif
425
427
428 std::string title;
429
430 bool is_dialog = false;
431
433 bool is_init = false;
434
436 bool is_valid_setup = false;
437
440
441#ifdef USE_EVENT_BACKGROUND_THREAD
446 std::atomic<bool> pending_actions[PENDING_NUM] = {false};
447#endif /* USE_EVENT_BACKGROUND_THREAD */
448};
449
451{
452#ifdef WITH_OPENGL_BACKEND
453 if (win->ghost_context_type == GHOST_kDrawingContextTypeOpenGL) {
454 /* Null on window initialization. */
455 if (win->backend.egl_window) {
456 wl_egl_window_resize(win->backend.egl_window, UNPACK2(size), 0, 0);
457 }
458 }
459#endif
460#ifdef WITH_VULKAN_BACKEND
461 if (win->ghost_context_type == GHOST_kDrawingContextTypeVulkan) {
462 /* FIXME: unlike EGL, the underlying surface is not resized
463 * which is needed in situations where the buffer scale changes
464 * (moving the window to an output with a different scale for e.g.).
465 * Failing to do so can exit with a protocol error, see: #148243. */
466 win->backend.vulkan_window_info->size[0] = size[0];
467 win->backend.vulkan_window_info->size[1] = size[1];
468 }
469#endif
470}
471
472static void gwl_window_title_set(GWL_Window *win, const char *title)
473{
474 GWL_XDG_Decor_Window &decor = *win->xdg_decor;
475 xdg_toplevel_set_title(decor.toplevel, title);
476 win->title = title;
477}
478
480{
481 if (win->frame.is_fullscreen) {
483 }
484 if (win->frame.is_maximized) {
486 }
488}
489
490static bool gwl_window_state_set_for_xdg(xdg_toplevel *toplevel,
492 const GHOST_TWindowState state_current)
493{
494 switch (state) {
496 /* Unset states. */
497 switch (state_current) {
499 xdg_toplevel_unset_maximized(toplevel);
500 break;
501 }
503 xdg_toplevel_unset_fullscreen(toplevel);
504 break;
505 }
506 default: {
507 break;
508 }
509 }
510 break;
512 /* Unset fullscreen before maximizing (required by the XDG shell spec). */
513 if (state_current == GHOST_kWindowStateFullScreen) {
514 xdg_toplevel_unset_fullscreen(toplevel);
515 }
516 xdg_toplevel_set_maximized(toplevel);
517 break;
518 }
520 xdg_toplevel_set_minimized(toplevel);
521 break;
522 }
524 /* Unset maximized before going fullscreen (required by the XDG shell spec). */
525 if (state_current == GHOST_kWindowStateMaximized) {
526 xdg_toplevel_unset_maximized(toplevel);
527 }
528 xdg_toplevel_set_fullscreen(toplevel, nullptr);
529 break;
530 }
531 }
532 return true;
533}
534
536{
537 const GHOST_TWindowState state_current = gwl_window_state_get(win);
538 return gwl_window_state_set_for_xdg(win->xdg_decor->toplevel, state, state_current);
539}
540
542
543/* -------------------------------------------------------------------- */
549
554static int gwl_window_fractional_to_viewport(const GWL_WindowFrame &frame, const int value)
555{
556 GHOST_ASSERT(frame.fractional_scale != 0, "Not fractional or called before initialized!");
557 return (value * frame.fractional_scale) / FRACTIONAL_DENOMINATOR;
558}
559
564static int gwl_window_fractional_from_viewport(const GWL_WindowFrame &frame, const int value)
565{
566 GHOST_ASSERT(frame.fractional_scale != 0, "Not fractional or called before initialized!");
567 return (value * FRACTIONAL_DENOMINATOR) / frame.fractional_scale;
568}
569
570/* NOTE: rounded versions are needed for window-frame dimensions conversions.
571 * (rounding is part of the WAYLAND spec). All other conversions such as cursor coordinates
572 * can used simple integer division as rounding is not defined in this case. */
573
574static int gwl_window_fractional_to_viewport_round(const GWL_WindowFrame &frame, const int value)
575{
576 GHOST_ASSERT(frame.fractional_scale != 0, "Not fractional or called before initialized!");
577 return lroundf(double(value * frame.fractional_scale) / double(FRACTIONAL_DENOMINATOR));
578}
579
580static int gwl_window_fractional_from_viewport_round(const GWL_WindowFrame &frame, const int value)
581{
582 GHOST_ASSERT(frame.fractional_scale != 0, "Not fractional or called before initialized!");
583 return lroundf(double(value * FRACTIONAL_DENOMINATOR) / double(frame.fractional_scale));
584}
585
586#ifdef WITH_GHOST_CSD
593static int32_t gwl_window_physical_to_surface_local(const GWL_Window *win, const int32_t value)
594{
595 if (win->frame.fractional_scale) {
597 }
598 return value / win->frame.buffer_scale;
599}
600#endif /* WITH_GHOST_CSD */
601
603 bool *r_surface_needs_commit,
604 bool *r_surface_needs_buffer_scale)
605{
606 if (win->wp.viewport != nullptr) {
607 return false;
608 }
609 wp_viewporter *viewporter = win->ghost_system->wp_viewporter_get();
610 if (viewporter == nullptr) {
611 return false;
612 }
613 win->wp.viewport = wp_viewporter_get_viewport(viewporter, win->wl.surface);
614 if (win->wp.viewport == nullptr) {
615 return false;
616 }
617
618 /* Set the buffer scale to 1 since a viewport will be used. */
619 if (win->frame.buffer_scale != 1) {
620 win->frame.buffer_scale = 1;
621
622 if (r_surface_needs_buffer_scale) {
623 *r_surface_needs_buffer_scale = true;
624 }
625 else {
626 wl_surface_set_buffer_scale(win->wl.surface, win->frame.buffer_scale);
627 }
628
629 if (r_surface_needs_commit) {
630 *r_surface_needs_commit = true;
631 }
632 else {
633 wl_surface_commit(win->wl.surface);
634 }
635 }
636
637 return true;
638}
639
641 bool *r_surface_needs_commit,
642 bool *r_surface_needs_buffer_scale)
643{
644 if (win->wp.viewport == nullptr) {
645 return false;
646 }
647
648 wp_viewport_destroy(win->wp.viewport);
649 win->wp.viewport = nullptr;
650
651 GHOST_ASSERT(win->frame.buffer_scale == 1, "Unexpected scale!");
652 if (win->frame_pending.buffer_scale != win->frame.buffer_scale) {
654
655 if (r_surface_needs_buffer_scale) {
656 *r_surface_needs_buffer_scale = true;
657 }
658 else {
659 wl_surface_set_buffer_scale(win->wl.surface, win->frame.buffer_scale);
660 }
661
662 if (r_surface_needs_commit) {
663 *r_surface_needs_commit = true;
664 }
665 else {
666 wl_surface_commit(win->wl.surface);
667 }
668 }
669 return true;
670}
671
673{
674 if (win->wp.viewport == nullptr) {
675 return false;
676 }
677
678 /* Setting `wp_viewport_set_source` isn't necessary as an unset value is ensured on creation
679 * and documented to use the entire buffer, further this can crash with NVIDIA, see: #117531. */
680
681 wp_viewport_set_destination(
682 win->wp.viewport,
685 return true;
686}
687
689
690/* -------------------------------------------------------------------- */
693
701{
702 GHOST_SystemWayland *system = win->ghost_system;
703 xdg_activation_v1 *activation_manager = system->xdg_activation_manager_get();
704 if (activation_manager == nullptr) [[unlikely]] {
705 return;
706 }
707
708 if (win->xdg.activation_token) {
709 /* We're about to overwrite this with a new request. */
710 xdg_activation_token_v1_destroy(win->xdg.activation_token);
711 }
712 win->xdg.activation_token = xdg_activation_v1_get_activation_token(activation_manager);
713
714 xdg_activation_token_v1_add_listener(
715 win->xdg.activation_token, xdg_activation_listener_get(), win);
716
717 xdg_activation_token_v1_set_app_id(win->xdg.activation_token,
719
720 /* The serial of the input device requesting activation. */
721 {
722 uint32_t serial = 0;
723 wl_seat *seat = system->wl_seat_active_get_with_input_serial(serial);
724 if (seat) {
725 xdg_activation_token_v1_set_serial(win->xdg.activation_token, serial, seat);
726 }
727 }
728
729 /* The surface of the window requesting activation. */
730 {
731 GHOST_WindowWayland *ghost_window_active = static_cast<GHOST_WindowWayland *>(
732 system->getWindowManager()->getActiveWindow());
733 if (ghost_window_active) {
734 wl_surface *surface = ghost_window_active->wl_surface_get();
735 if (surface) {
736 xdg_activation_token_v1_set_surface(win->xdg.activation_token, surface);
737 }
738 }
739 }
740
741 xdg_activation_token_v1_commit(win->xdg.activation_token);
742}
743
745
746/* -------------------------------------------------------------------- */
749
751 GWL_Window *win, bool *r_surface_needs_commit, bool *r_surface_needs_buffer_scale)
752{
755 gwl_window_viewport_set(win, r_surface_needs_commit, r_surface_needs_buffer_scale);
757 }
758 else {
759 if (win->wp.viewport) {
760 gwl_window_viewport_unset(win, r_surface_needs_commit, r_surface_needs_buffer_scale);
761 }
762 else {
764 if (r_surface_needs_buffer_scale) {
765 *r_surface_needs_buffer_scale = true;
766 }
767 else {
768 wl_surface_set_buffer_scale(win->wl.surface, win->frame.buffer_scale);
769 }
770 if (r_surface_needs_commit) {
771 *r_surface_needs_commit = true;
772 }
773 else {
774 wl_surface_commit(win->wl.surface);
775 }
776 }
777 }
778}
779
781 bool *r_surface_needs_commit,
782 bool *r_surface_needs_buffer_scale)
783{
786 {
787 return;
788 }
790 win, r_surface_needs_commit, r_surface_needs_buffer_scale);
791}
792
794 bool *r_surface_needs_commit,
795 bool *r_surface_needs_resize_for_backend,
796 bool *r_surface_needs_buffer_scale)
797{
798 if (win->frame_pending.size[0] == 0 || win->frame_pending.size[1] == 0) {
799 return;
800 }
801
802 win->frame.size[0] = win->frame_pending.size[0];
803 win->frame.size[1] = win->frame_pending.size[1];
804
807 {
809 win, r_surface_needs_commit, r_surface_needs_buffer_scale);
810 }
811 else {
813 }
814
815 if (r_surface_needs_resize_for_backend) {
816 *r_surface_needs_resize_for_backend = true;
817 }
818 else {
820 }
821
823
824 win->frame_pending.size[0] = 0;
825 win->frame_pending.size[1] = 0;
826}
827
829
830#ifdef USE_EVENT_BACKGROUND_THREAD
831
833{
834 win->pending_actions[int(type)].store(true);
836}
837
839{
840 /* Ensure pending actions always use the state when the function starts
841 * because one actions may trigger other pending actions an in that case
842 * exact behavior depends on the order functions are called here.
843 * Without this, configuring the frame will trigger the surface
844 * commit immediately instead of the next time pending actions are handled. */
845 bool actions[PENDING_NUM];
846 for (size_t i = 0; i < ARRAY_SIZE(actions); i++) {
847 actions[i] = win->pending_actions[i].exchange(false);
848 }
849
850 if (actions[PENDING_WINDOW_FRAME_CONFIGURE]) {
852 }
855 /* Force postponing scale update to ensure all scale information has been taken into account
856 * before the actual update is performed. Failing to do so tends to cause flickering. */
857 actions[PENDING_OUTPUT_SCALE_UPDATE] = false;
858 }
859 if (actions[PENDING_OUTPUT_SCALE_UPDATE]) {
861 }
862 if (actions[PENDING_WINDOW_SURFACE_COMMIT]) {
863 wl_surface_commit(win->wl.surface);
864 }
866 /* Best-effort refresh: failure is acceptable when pending (e.g. cursor theme unavailable). */
869 }
870}
871
872#endif /* USE_EVENT_BACKGROUND_THREAD */
873
874#ifdef WITH_GHOST_CSD
876static void gwl_window_csd_elems_update(GWL_Window *win)
877{
878 GWL_WindowCSD *xdg_csd = win->xdg_csd;
879 GHOST_SystemWayland *system = win->ghost_system;
880 const GHOST_CSD_Params &params = system->getWindowCSD();
881 const GHOST_CSD_Layout &button_layout = system->getWindowCSD_Layout();
882
883 const int32_t fractional_scale[2] = {
885 win->ghost_window->getDPIHint(),
886 };
887 xdg_csd->csd_elems_num = params.layout_callback(win->frame.size,
888 fractional_scale,
890 &button_layout,
891 xdg_csd->csd_elems);
892}
893
898static void gwl_window_csd_layout_apply(GWL_Window *win)
899{
900 const uint32_t type_mask = win->wm_capabilities_mask;
901 const GHOST_CSD_Layout &layout_base = win->ghost_system->preferences_csd_button_layout_get();
902 GHOST_CSD_Layout layout = {};
903
904 int dst = 0;
905 for (int src = 0; src < layout_base.buttons_num; src++) {
906 GHOST_TCSD_Type type = layout_base.buttons[src];
907 if (type_mask & (1 << type)) {
908 layout.buttons[dst++] = type;
909 }
910 }
911 layout.buttons_num = dst;
912 win->ghost_system->setWindowCSD_Layout(layout);
913}
914
923static void gwl_window_csd_geometry_update(GWL_Window *win)
924{
925 if (win->xdg_decor == nullptr) {
926 return;
927 }
928 xdg_surface_set_window_geometry(win->xdg_decor->surface,
929 0,
930 0,
931 gwl_window_physical_to_surface_local(win, win->frame.size[0]),
932 gwl_window_physical_to_surface_local(win, win->frame.size[1]));
933}
934
941static void gwl_window_csd_margin_update(GWL_Window *win)
942{
943 GWL_WindowCSD *xdg_csd = win->xdg_csd;
944 GHOST_SystemWayland *system = win->ghost_system;
945
946 /* Zero hides the margin: no border exists for full-screen & maximized windows,
947 * a zero size means it's disabled (also accounting for rounding at extreme scales). */
948 int32_t margin_local = 0;
951 const GHOST_CSD_Params &params = system->getWindowCSD();
952 const int32_t margin_physical = (params.resize_margin_size * win->ghost_window->getDPIHint()) /
954 margin_local = gwl_window_physical_to_surface_local(win, margin_physical);
955 }
956
957 if (margin_local <= 0) {
958 if (xdg_csd->margin_surface) {
959 /* Unmap: detach the buffer so the (otherwise unused) surface stops accepting input. */
960 wl_surface_attach(xdg_csd->margin_surface, nullptr, 0, 0);
961 wl_surface_commit(xdg_csd->margin_surface);
962 }
963 return;
964 }
965
966 if (xdg_csd->margin_surface == nullptr) {
967 wl_subcompositor *subcompositor = system->wl_subcompositor_get();
968 wp_viewporter *viewporter = system->wp_viewporter_get();
969 if (subcompositor == nullptr || viewporter == nullptr) {
970 /* No support for the invisible margin on this compositor, in that case there is no way to
971 * resize from the window borders. Very unlikely in practice, resizing remains possible via
972 * the compositor's own shortcuts, so accept the limitation. */
973 return;
974 }
975
976 xdg_csd->margin_buffer = ghost_wl_buffer_create_transparent_pixel(system->wl_shm_get());
977 if (xdg_csd->margin_buffer == nullptr) [[unlikely]] {
978 return;
979 }
980
981 xdg_csd->margin_surface = wl_compositor_create_surface(system->wl_compositor_get());
982 ghost_wl_surface_tag_csd_margin(xdg_csd->margin_surface);
983 wl_surface_set_user_data(xdg_csd->margin_surface, win->ghost_window);
984
985 xdg_csd->margin_subsurface = wl_subcompositor_get_subsurface(
986 subcompositor, xdg_csd->margin_surface, win->wl.surface);
987 /* De-synchronize so the margin's state (buffer & viewport) applies on its own
988 * commit, it's not tied to the main surface's frame timing. The position is the
989 * exception, #wl_subsurface_set_position only applies on the parents commit,
990 * in practice the parent commits on every size & state change. */
991 wl_subsurface_set_desync(xdg_csd->margin_subsurface);
992 wl_subsurface_place_below(xdg_csd->margin_subsurface, win->wl.surface);
993
994 xdg_csd->margin_viewport = wp_viewporter_get_viewport(viewporter, xdg_csd->margin_surface);
995 }
996
997 const int32_t window_size_local[2] = {
998 gwl_window_physical_to_surface_local(win, win->frame.size[0]),
999 gwl_window_physical_to_surface_local(win, win->frame.size[1]),
1000 };
1001
1002 xdg_csd->margin_size_local = margin_local;
1003 xdg_csd->margin_window_size_local[0] = window_size_local[0];
1004 xdg_csd->margin_window_size_local[1] = window_size_local[1];
1005
1006 wl_surface_attach(xdg_csd->margin_surface, xdg_csd->margin_buffer, 0, 0);
1007 wl_subsurface_set_position(xdg_csd->margin_subsurface, -margin_local, -margin_local);
1008 wp_viewport_set_destination(xdg_csd->margin_viewport,
1009 window_size_local[0] + (margin_local * 2),
1010 window_size_local[1] + (margin_local * 2));
1011 wl_surface_commit(xdg_csd->margin_surface);
1012}
1013
1021static void gwl_window_csd_opaque_region_update(GWL_Window *win)
1022{
1023 GHOST_SystemWayland *system = win->ghost_system;
1024
1025 const int32_t size_local[2] = {
1026 gwl_window_physical_to_surface_local(win, win->frame.size[0]),
1027 gwl_window_physical_to_surface_local(win, win->frame.size[1]),
1028 };
1029 if (size_local[0] <= 0 || size_local[1] <= 0) [[unlikely]] {
1030 return;
1031 }
1032
1033 int32_t radius_local = 0;
1035 if (win->ghost_window->hasAlpha() &&
1037 {
1038 const GHOST_CSD_Params &params = system->getWindowCSD();
1039 const int32_t radius_physical = (params.corner_radius * win->ghost_window->getDPIHint()) /
1041 radius_local = gwl_window_physical_to_surface_local(win, radius_physical);
1042 }
1043
1044 wl_region *region = wl_compositor_create_region(system->wl_compositor_get());
1045 if (region == nullptr) [[unlikely]] {
1046 return;
1047 }
1048
1049 wl_region_add(region, 0, 0, UNPACK2(size_local));
1050
1051 if (radius_local > 0) {
1052 /* Cut out the corners that are actually rounded. A corner squares off when either of the
1053 * edges meeting there is tiled, this must match #WM_window_csd_draw_corner_mask.
1054 * Coordinates are Y-down (the top of the window is zero). */
1056 const struct {
1057 int32_t xy[2];
1058 int edges;
1059 } corners[] = {
1060 {
1061 {0, 0},
1063 },
1064 {
1065 {size_local[0] - radius_local, 0},
1067 },
1068 {
1069 {0, size_local[1] - radius_local},
1071 },
1072 {
1073 {size_local[0] - radius_local, size_local[1] - radius_local},
1075 },
1076 };
1077 for (const auto &corner : corners) {
1078 if (tiled & corner.edges) {
1079 continue;
1080 }
1081 wl_region_subtract(region, UNPACK2(corner.xy), radius_local, radius_local);
1082 }
1083 }
1084
1085 wl_surface_set_opaque_region(win->wl.surface, region);
1086 /* The compositor copies the region, it doesn't take ownership. */
1087 wl_region_destroy(region);
1088}
1089#endif /* WITH_GHOST_CSD */
1090
1096{
1097#ifdef USE_EVENT_BACKGROUND_THREAD
1098 GHOST_ASSERT(win->ghost_system->main_thread_id == std::this_thread::get_id(),
1099 "Only from main thread!");
1100
1101#endif
1102
1103#ifdef WITH_GHOST_CSD
1104 const bool state_changed = ((win->frame_pending.is_fullscreen != win->frame.is_fullscreen) ||
1107#endif
1108
1109 const bool dpi_changed = win->frame_pending.fractional_scale != win->frame.fractional_scale;
1110 const bool scale_changed = dpi_changed ||
1112 bool surface_needs_commit = false;
1113 bool surface_needs_resize_for_backend = false;
1114 bool surface_needs_buffer_scale = false;
1115
1116 if (win->frame_pending.size[0] != 0 && win->frame_pending.size[1] != 0) {
1117 if ((win->frame.size[0] != win->frame_pending.size[0]) ||
1118 (win->frame.size[1] != win->frame_pending.size[1]))
1119 {
1121 &surface_needs_commit,
1122 &surface_needs_resize_for_backend,
1123 &surface_needs_buffer_scale);
1124 }
1125 }
1126
1129 win, &surface_needs_commit, &surface_needs_buffer_scale);
1130 }
1131 else {
1132 if (win->frame_pending.buffer_scale != win->frame.buffer_scale) {
1134 surface_needs_buffer_scale = true;
1135 }
1136 }
1137
1138 if (surface_needs_resize_for_backend) {
1140 }
1141
1142 if (surface_needs_buffer_scale) {
1143 wl_surface_set_buffer_scale(win->wl.surface, win->frame.buffer_scale);
1144 }
1145
1146 if (win->xdg_decor) {
1147 GWL_XDG_Decor_Window &decor = *win->xdg_decor;
1148 if (decor.pending.ack_configure) {
1149 xdg_surface_ack_configure(decor.surface, decor.pending.ack_configure_serial);
1150 /* The XDG spec states a commit event is required after ACK configure. */
1151 surface_needs_commit = true;
1152
1153 decor.pending.ack_configure = false;
1154 decor.pending.ack_configure_serial = 0;
1155
1156 decor.initial_configure_seen = true;
1157 }
1158 }
1159
1160 if (surface_needs_commit) {
1161#ifdef USE_EVENT_BACKGROUND_THREAD
1162 /* Postponing the commit avoids flickering when moving between monitors of different scale. */
1164#else
1165 wl_surface_commit(win->wl.surface);
1166#endif
1167 }
1168
1169 if (dpi_changed) {
1170 GHOST_SystemWayland *system = win->ghost_system;
1171 system->pushEvent(std::make_unique<GHOST_Event>(
1173 }
1174
1175 if (win->frame.is_active != win->frame_pending.is_active) {
1176 if (win->frame_pending.is_active) {
1177 win->ghost_window->activate();
1178 }
1179 else {
1180 win->ghost_window->deactivate();
1181 }
1182 }
1183 else if (false) {
1184 /* Disabled, this can happen during debugging
1185 * when the window changed while the process has been paused. */
1187 win->frame.is_active ==
1189 "GHOST internal active state does not match WAYLAND!");
1190 }
1191
1192 win->frame_pending.size[0] = win->frame.size[0];
1193 win->frame_pending.size[1] = win->frame.size[1];
1194
1195 win->frame = win->frame_pending;
1196
1197 /* Update the toplevel icon at the new scale (after the frame is fully synced). */
1198 if (scale_changed && win->xdg_decor) {
1200 }
1201
1202 /* Signal not to apply the scale unless it's configured. */
1203 win->frame_pending.size[0] = 0;
1204 win->frame_pending.size[1] = 0;
1205
1206 /* Run last to account for the updated window.
1207 * Note that we could attempt to minimize when this runs however as `layout_callback`
1208 * is expected to be low overhead (since it runs on redraw), there isn't any advantage,
1209 * further, failure to run is likely to result in strange bugs (window & decoration mismatch).
1210 *
1211 * For reference re-running is needed on:
1212 * - DPI change.
1213 * - Frame size change.
1214 * - Frame state change.
1215 *
1216 * But not needed on activate/deactivate. */
1217#ifdef WITH_GHOST_CSD
1218 if (GWL_WindowCSD *xdg_csd = win->xdg_csd) {
1221 xdg_csd->csd_elems_num = 0;
1222 }
1223 else {
1224 gwl_window_csd_elems_update(win);
1225
1226 if (state_changed) {
1227 /* NOTE(@ideasman42) This is not technically correct because after the
1228 * re-configure the pointer could be over some other element of the window,
1229 * however the down side would be fairly unlikely to redo.
1230 * An example would be: double click to maximize, with the title-bar
1231 * already at the top of the output, then (without moving the pointer),
1232 * double click on the title-bar again. This would fail to un-maximize.
1233 * Given the issue is quite obscure and not actually part of a "reasonable" use case.
1234 * I'm going to accept the limitation. */
1235 win->ghost_window->csd_elem_active_type_set(GHOST_kCSDTypeBody);
1236 }
1237 }
1238
1239 /* Must run on all state change so the "Decor" redraw event is always sent. */
1240 if (state_changed) {
1241 /* In cases where geometry of the window doesn't change, we need to make
1242 * sure that the decor gets redrawn to correctly show the new state we are in. */
1244 }
1245
1246 gwl_window_csd_geometry_update(win);
1247 gwl_window_csd_margin_update(win);
1248 gwl_window_csd_opaque_region_update(win);
1249 }
1250#endif /* WITH_GHOST_CSD */
1251}
1252
1253[[maybe_unused]] static void gwl_window_frame_update_from_pending(GWL_Window *win)
1254{
1255#ifdef USE_EVENT_BACKGROUND_THREAD
1256 std::lock_guard lock_frame_guard{win->frame_pending_mutex};
1257#endif
1259}
1260
1262
1263/* -------------------------------------------------------------------- */
1266
1270static int output_scale_cmp(const GWL_Output *output_a, const GWL_Output *output_b)
1271{
1272 if (output_a->scale < output_b->scale) {
1273 return -1;
1274 }
1275 if (output_a->scale > output_b->scale) {
1276 return 1;
1277 }
1278 if (output_a->has_scale_fractional || output_b->has_scale_fractional) {
1279 const int scale_fractional_a = output_a->has_scale_fractional ?
1280 output_a->scale_fractional :
1281 (output_a->scale * FRACTIONAL_DENOMINATOR);
1282 const int scale_fractional_b = output_b->has_scale_fractional ?
1283 output_b->scale_fractional :
1284 (output_b->scale * FRACTIONAL_DENOMINATOR);
1285 if (scale_fractional_a < scale_fractional_b) {
1286 return -1;
1287 }
1288 if (scale_fractional_a > scale_fractional_b) {
1289 return 1;
1290 }
1291 }
1292 return 0;
1293}
1294
1295static int outputs_max_scale_or_default(const std::span<GWL_Output *const> outputs,
1296 const int32_t scale_default,
1297 int *r_scale_fractional)
1298{
1299 const GWL_Output *output_max = nullptr;
1300 for (const GWL_Output *reg_output : outputs) {
1301 if (!output_max || (output_scale_cmp(output_max, reg_output) == -1)) {
1302 output_max = reg_output;
1303 }
1304 }
1305
1306 if (output_max) {
1307 if (r_scale_fractional) {
1308 *r_scale_fractional = output_max->has_scale_fractional ?
1309 output_max->scale_fractional :
1310 (output_max->scale * FRACTIONAL_DENOMINATOR);
1311 }
1312 return output_max->scale;
1313 }
1314 if (r_scale_fractional) {
1315 *r_scale_fractional = scale_default * FRACTIONAL_DENOMINATOR;
1316 }
1317 return scale_default;
1318}
1319
1320static int outputs_uniform_scale_or_default(const std::span<GWL_Output *const> outputs,
1321 const int32_t scale_default,
1322 int *r_scale_fractional)
1323{
1324 const GWL_Output *output_uniform = nullptr;
1325 for (const GWL_Output *reg_output : outputs) {
1326 if (!output_uniform) {
1327 output_uniform = reg_output;
1328 }
1329 else if (output_scale_cmp(output_uniform, reg_output) != 0) {
1330 /* Non-uniform. */
1331 output_uniform = nullptr;
1332 break;
1333 }
1334 }
1335
1336 if (output_uniform) {
1337 if (r_scale_fractional) {
1338 *r_scale_fractional = output_uniform->has_scale_fractional ?
1339 output_uniform->scale_fractional :
1340 (output_uniform->scale * FRACTIONAL_DENOMINATOR);
1341 }
1342 return output_uniform->scale;
1343 }
1344 if (r_scale_fractional) {
1345 *r_scale_fractional = scale_default * FRACTIONAL_DENOMINATOR;
1346 }
1347 return scale_default;
1348}
1349
1351
1352/* -------------------------------------------------------------------- */
1355
1356static CLG_LogRef LOG_WL_XDG_TOPLEVEL = {"ghost.wl.handle.xdg_toplevel"};
1357#define LOG (&LOG_WL_XDG_TOPLEVEL)
1358
1359static void xdg_toplevel_handle_configure(void *data,
1360 xdg_toplevel * /*xdg_toplevel*/,
1361 const int32_t width,
1362 const int32_t height,
1363 wl_array *states)
1364{
1365 /* TODO: log `states`, not urgent. */
1366 CLOG_DEBUG(LOG, "configure (size=[%d, %d])", width, height);
1367
1368 GWL_Window *win = static_cast<GWL_Window *>(data);
1369
1370#ifdef USE_EVENT_BACKGROUND_THREAD
1371 std::lock_guard lock_frame_guard{win->frame_pending_mutex};
1372#endif
1373
1374 const int32_t size[2] = {width, height};
1375 for (int i = 0; i < 2; i++) {
1376 if (size[i] == 0) {
1377 /* Values may be zero, in this case the client should choose. */
1378 continue;
1379 }
1380 win->frame_pending.size[i] = win->frame.fractional_scale ?
1382 (size[i] * win->frame.buffer_scale);
1383 }
1384
1385 win->frame_pending.is_maximized = false;
1386 win->frame_pending.is_fullscreen = false;
1387 win->frame_pending.is_active = false;
1388
1389 int tiled_edges = GHOST_kWindowTiledNone;
1390 enum xdg_toplevel_state *state;
1391 WL_ARRAY_FOR_EACH (state, states) {
1392 switch (*state) {
1393 case XDG_TOPLEVEL_STATE_MAXIMIZED:
1394 win->frame_pending.is_maximized = true;
1395 break;
1396 case XDG_TOPLEVEL_STATE_FULLSCREEN:
1397 win->frame_pending.is_fullscreen = true;
1398 break;
1399 case XDG_TOPLEVEL_STATE_ACTIVATED:
1400 win->frame_pending.is_active = true;
1401 break;
1402 /* Only sent by compositors supporting `xdg_shell` version 2 or newer. */
1403 case XDG_TOPLEVEL_STATE_TILED_LEFT:
1404 tiled_edges |= GHOST_kWindowTiledLeft;
1405 break;
1406 case XDG_TOPLEVEL_STATE_TILED_RIGHT:
1407 tiled_edges |= GHOST_kWindowTiledRight;
1408 break;
1409 case XDG_TOPLEVEL_STATE_TILED_TOP:
1410 tiled_edges |= GHOST_kWindowTiledTop;
1411 break;
1412 case XDG_TOPLEVEL_STATE_TILED_BOTTOM:
1413 tiled_edges |= GHOST_kWindowTiledBottom;
1414 break;
1415 default:
1416 break;
1417 }
1418 }
1420}
1421
1422static void xdg_toplevel_handle_close(void *data, xdg_toplevel * /*xdg_toplevel*/)
1423{
1424 CLOG_DEBUG(LOG, "close");
1425
1426 GWL_Window *win = static_cast<GWL_Window *>(data);
1427
1428 win->ghost_window->close();
1429}
1430
1432 xdg_toplevel * /*xdg_toplevel*/,
1433 const int32_t width,
1434 const int32_t height)
1435{
1436 /* Only available in interface version 4. */
1437 CLOG_DEBUG(LOG, "configure_bounds (size=[%d, %d])", width, height);
1438
1439 /* No need to lock as this only runs on window creation. */
1440 GWL_Window *win = static_cast<GWL_Window *>(data);
1441 GWL_XDG_Decor_Window &decor = *win->xdg_decor;
1442 if (decor.initial_configure_seen == false) {
1443 decor.initial_bounds[0] = width;
1444 decor.initial_bounds[1] = height;
1445 }
1446}
1448 xdg_toplevel * /*xdg_toplevel*/,
1449 wl_array *capabilities)
1450{
1451 /* Only available in interface version 5. */
1452 CLOG_DEBUG(LOG, "wm_capabilities");
1453
1454#ifdef WITH_GHOST_CSD
1455 /* Build a bit-mask of supported button types. Close is always supported. */
1456 uint32_t type_mask = (1 << GHOST_kCSDTypeButtonClose) | (1 << GHOST_kCSDTypeTitlebar);
1457 uint32_t *cap;
1458 WL_ARRAY_FOR_EACH (cap, capabilities) {
1459 switch (*cap) {
1460 case XDG_TOPLEVEL_WM_CAPABILITIES_WINDOW_MENU:
1461 type_mask |= 1 << GHOST_kCSDTypeButtonMenu;
1462 break;
1463 case XDG_TOPLEVEL_WM_CAPABILITIES_MAXIMIZE:
1464 type_mask |= 1 << GHOST_kCSDTypeButtonMaximize;
1465 break;
1466 case XDG_TOPLEVEL_WM_CAPABILITIES_MINIMIZE:
1467 type_mask |= 1 << GHOST_kCSDTypeButtonMinimize;
1468 break;
1469 }
1470 }
1471
1472 GWL_Window *win = static_cast<GWL_Window *>(data);
1473 win->wm_capabilities_mask = type_mask;
1474 if (win->xdg_csd) {
1475 gwl_window_csd_layout_apply(win);
1476 }
1477
1478 /* NOTE(@ideasman42): don't trigger a redraw here.
1479 * In practice this callback runs on newly created windows,
1480 * so an event such as #GHOST_kEventWindowUpdateDecor is likely
1481 * to add an extra redraw for newly created windows. */
1482
1483#else
1484 (void)data;
1485 (void)capabilities;
1486#endif
1487}
1488
1490 /*configure*/ xdg_toplevel_handle_configure,
1491 /*close*/ xdg_toplevel_handle_close,
1492 /*configure_bounds*/ xdg_toplevel_handle_configure_bounds,
1493 /*wm_capabilities*/ xdg_toplevel_handle_wm_capabilities,
1494};
1495
1496#undef LOG
1497
1499
1500/* -------------------------------------------------------------------- */
1505
1506static void xdg_activation_handle_done(void *data,
1507 xdg_activation_token_v1 *xdg_activation_token_v1,
1508 const char *token)
1509{
1510 GWL_Window *win = static_cast<GWL_Window *>(data);
1511 if (xdg_activation_token_v1 != win->xdg.activation_token) {
1512 return;
1513 }
1514
1515 GHOST_SystemWayland *system = win->ghost_system;
1516 xdg_activation_v1 *activation_manager = system->xdg_activation_manager_get();
1517 xdg_activation_v1_activate(activation_manager, token, win->wl.surface);
1518 xdg_activation_token_v1_destroy(win->xdg.activation_token);
1519 win->xdg.activation_token = nullptr;
1520}
1521
1522static const xdg_activation_token_v1_listener xdg_activation_listener = {
1524};
1525
1526static const xdg_activation_token_v1_listener *xdg_activation_listener_get()
1527{
1529}
1530
1532
1533/* -------------------------------------------------------------------- */
1538
1539static CLG_LogRef LOG_WL_FRACTIONAL_SCALE = {"ghost.wl.handle.fractional_scale"};
1540#define LOG (&LOG_WL_FRACTIONAL_SCALE)
1541
1543 void *data, wp_fractional_scale_v1 * /*wp_fractional_scale_v1*/, uint preferred_scale)
1544{
1545#ifdef USE_EVENT_BACKGROUND_THREAD
1546 std::lock_guard lock_frame_guard{static_cast<GWL_Window *>(data)->frame_pending_mutex};
1547#endif
1549 "preferred_scale (preferred_scale=%.6f)",
1550 double(preferred_scale) / FRACTIONAL_DENOMINATOR);
1551
1552 GWL_Window *win = static_cast<GWL_Window *>(data);
1553
1554 if (win->frame_pending.fractional_scale_preferred != int(preferred_scale)) {
1555 win->frame_pending.fractional_scale_preferred = preferred_scale;
1557 }
1558}
1559
1560static const wp_fractional_scale_v1_listener wp_fractional_scale_listener = {
1562};
1563
1564#undef LOG
1565
1567
1568/* -------------------------------------------------------------------- */
1571
1572static CLG_LogRef LOG_WL_IMAGE_DESCRIPTION = {"ghost.wl.handle.image_description"};
1573#define LOG (&LOG_WL_IMAGE_DESCRIPTION)
1574
1579
1580static void image_description_handle_failed(void *data,
1581 wp_image_description_v1 * /*image_description*/,
1582 uint32_t cause,
1583 const char *msg)
1584{
1585 CLOG_WARN(LOG, "failed (cause=%u): %s", cause, msg ? msg : "");
1586 static_cast<GWL_ImageDescriptionWait *>(data)->done = true;
1587}
1588
1589static void image_description_handle_ready(void *data,
1590 wp_image_description_v1 * /*image_description*/,
1591 uint32_t identity)
1592{
1593 CLOG_INFO(LOG, "ready (identity=%u)", identity);
1594 GWL_ImageDescriptionWait *wait = static_cast<GWL_ImageDescriptionWait *>(data);
1595 wait->done = true;
1596 wait->ready = true;
1597}
1598
1599#ifdef WP_IMAGE_DESCRIPTION_V1_READY2_SINCE_VERSION
1600static void image_description_handle_ready2(void *data,
1601 wp_image_description_v1 * /*image_description*/,
1602 uint32_t identity_lo,
1603 uint32_t identity_hi)
1604{
1605 CLOG_INFO(LOG, "ready2 (identity_lo=%u, identity_hi=%u)", identity_lo, identity_hi);
1607 wait->done = true;
1608 wait->ready = true;
1609}
1610#endif /* WP_IMAGE_DESCRIPTION_V1_READY2_SINCE_VERSION */
1611
1612static const wp_image_description_v1_listener image_description_listener = {
1615#ifdef WP_IMAGE_DESCRIPTION_V1_READY2_SINCE_VERSION
1616 /*ready2*/ image_description_handle_ready2,
1617#endif
1618};
1619
1620#undef LOG
1621
1623
1624/* -------------------------------------------------------------------- */
1627
1628static CLG_LogRef LOG_WL_XDG_TOPLEVEL_DECORATION = {"ghost.wl.handle.xdg_toplevel_decoration"};
1629#define LOG (&LOG_WL_XDG_TOPLEVEL_DECORATION)
1630
1632 void *data, zxdg_toplevel_decoration_v1 * /*zxdg_toplevel_decoration_v1*/, const uint32_t mode)
1633{
1634 CLOG_DEBUG(LOG, "configure (mode=%u)", mode);
1635
1636 GWL_Window *win = static_cast<GWL_Window *>(data);
1637
1638 win->xdg_decor->mode = (zxdg_toplevel_decoration_v1_mode)mode;
1639}
1640
1641static const zxdg_toplevel_decoration_v1_listener xdg_toplevel_decoration_v1_listener = {
1643};
1644
1645#undef LOG
1646
1648
1649/* -------------------------------------------------------------------- */
1652
1653static CLG_LogRef LOG_WL_XDG_SURFACE = {"ghost.wl.handle.xdg_surface"};
1654#define LOG (&LOG_WL_XDG_SURFACE)
1655
1656static void xdg_surface_handle_configure(void *data,
1657 xdg_surface *xdg_surface,
1658 const uint32_t serial)
1659{
1660 GWL_Window *win = static_cast<GWL_Window *>(data);
1661
1662 if (win->xdg_decor->surface != xdg_surface) {
1663 CLOG_DEBUG(LOG, "configure (skipped)");
1664 return;
1665 }
1666 CLOG_DEBUG(LOG, "configure");
1667
1668#ifdef USE_EVENT_BACKGROUND_THREAD
1669 std::lock_guard lock_frame_guard{static_cast<GWL_Window *>(data)->frame_pending_mutex};
1670#endif
1671 win->xdg_decor->pending.ack_configure = true;
1672 win->xdg_decor->pending.ack_configure_serial = serial;
1673
1674#ifdef USE_EVENT_BACKGROUND_THREAD
1675 const GHOST_SystemWayland *system = win->ghost_system;
1676 const bool is_main_thread = system->main_thread_id == std::this_thread::get_id();
1677 if (!is_main_thread) {
1678 /* NOTE(@ideasman42): this only gets one redraw,
1679 * I could not find a case where this causes problems. */
1681 }
1682 else
1683#endif
1684 {
1686 }
1687}
1688
1692
1693#undef LOG
1694
1696
1697/* -------------------------------------------------------------------- */
1700
1701static CLG_LogRef LOG_WL_SURFACE = {"ghost.wl.handle.surface"};
1702#define LOG (&LOG_WL_SURFACE)
1703
1704static void surface_handle_enter(void *data, wl_surface * /*wl_surface*/, wl_output *wl_output)
1705{
1706 if (!ghost_wl_output_own(wl_output)) {
1707 CLOG_DEBUG(LOG, "enter (skipped)");
1708 return;
1709 }
1710 CLOG_DEBUG(LOG, "enter");
1711
1712 GWL_Output *reg_output = ghost_wl_output_user_data(wl_output);
1713 GHOST_WindowWayland *win = static_cast<GHOST_WindowWayland *>(data);
1714 if (win->outputs_enter(reg_output)) {
1716 }
1717}
1718
1719static void surface_handle_leave(void *data, wl_surface * /*wl_surface*/, wl_output *wl_output)
1720{
1721 if (!ghost_wl_output_own(wl_output)) {
1722 CLOG_DEBUG(LOG, "leave (skipped)");
1723 return;
1724 }
1725 CLOG_DEBUG(LOG, "leave");
1726
1727 GWL_Output *reg_output = ghost_wl_output_user_data(wl_output);
1728 GHOST_WindowWayland *win = static_cast<GHOST_WindowWayland *>(data);
1729 if (win->outputs_leave(reg_output)) {
1731 }
1732}
1733
1734#if defined(WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION) && \
1735 defined(WL_SURFACE_PREFERRED_BUFFER_TRANSFORM_SINCE_VERSION)
1736static void surface_handle_preferred_buffer_scale(void * /*data*/,
1737 wl_surface * /*wl_surface*/,
1738 const int32_t factor)
1739{
1740 /* Only available in interface version 6. */
1741 CLOG_DEBUG(LOG, "handle_preferred_buffer_scale (factor=%d)", factor);
1742}
1743
1744static void surface_handle_preferred_buffer_transform(void * /*data*/,
1745 wl_surface * /*wl_surface*/,
1746 const uint32_t transform)
1747{
1748 /* Only available in interface version 6. */
1749 CLOG_DEBUG(LOG, "handle_preferred_buffer_transform (transform=%u)", transform);
1750}
1751#endif /* WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION && \
1752 * WL_SURFACE_PREFERRED_BUFFER_TRANSFORM_SINCE_VERSION */
1753
1755 /*enter*/ surface_handle_enter,
1756 /*leave*/ surface_handle_leave,
1757#if defined(WL_SURFACE_PREFERRED_BUFFER_SCALE_SINCE_VERSION) && \
1758 defined(WL_SURFACE_PREFERRED_BUFFER_TRANSFORM_SINCE_VERSION)
1759 /*preferred_buffer_scale*/ surface_handle_preferred_buffer_scale,
1760 /*preferred_buffer_transform*/ surface_handle_preferred_buffer_transform,
1761#endif
1762};
1763
1764#undef LOG
1765
1767
1768/* -------------------------------------------------------------------- */
1773
1775 const char *title,
1776 const int32_t /*left*/,
1777 const int32_t /*top*/,
1778 const uint32_t width,
1779 const uint32_t height,
1781 const GHOST_IWindow *parent_window,
1782 const GHOST_TDrawingContextType type,
1783 const bool is_dialog,
1784 const GHOST_ContextParams &context_params,
1785 const bool exclusive,
1786 const GHOST_GPUDevice &preferred_device)
1787 : GHOST_Window(width, height, state, context_params, exclusive),
1788 system_(system),
1789 window_(new GWL_Window),
1790 preferred_device_(preferred_device)
1791{
1792#ifdef USE_EVENT_BACKGROUND_THREAD
1793 std::lock_guard lock_server_guard{*system->server_mutex};
1794#endif
1795
1796 window_->ghost_window = this;
1797 window_->ghost_system = system;
1798 window_->ghost_context_type = type;
1799
1800 wl_display *display = system->wl_display_get();
1801
1802 /* NOTE(@ideasman42): The scale set here to avoid flickering on startup.
1803 * When all monitors use the same scale (which is quite common) there aren't any problems.
1804 *
1805 * When monitors have different scales there may still be a visible window resize on startup.
1806 * Ideally it would be possible to know the scale this window will use however that's only
1807 * known once #surface_enter callback runs (which isn't guaranteed to run at all).
1808 *
1809 * Using the maximum scale is best as it results in the window first being smaller,
1810 * avoiding a large window flashing before it's made smaller.
1811 *
1812 * For fractional scaling the buffer will eventually be 1. Setting it to 1 now
1813 * (to avoid window size rounding and buffer size switching) has some down-sides.
1814 * It means the window will be drawn larger for a moment then smaller once fractional scaling
1815 * is detected and enabled. Unfortunately, it doesn't seem possible to receive the
1816 * #wp_fractional_scale_v1_listener::preferred_scale information before the window is created
1817 * So leave the buffer scaled up because there is no *guarantee* the fractional scaling support
1818 * will run which could result in an incorrect buffer scale. */
1819 int scale_fractional_from_output;
1820 const int buffer_scale_from_output = outputs_uniform_scale_or_default(
1821 system_->outputs_get(), 0, &scale_fractional_from_output);
1822
1823 window_->frame.size[0] = int32_t(width);
1824 window_->frame.size[1] = int32_t(height);
1825
1826 window_->is_dialog = is_dialog;
1827
1828 /* Window surfaces. */
1829 window_->wl.surface = wl_compositor_create_surface(system_->wl_compositor_get());
1830 ghost_wl_surface_tag(window_->wl.surface);
1831
1832 wl_surface_add_listener(window_->wl.surface, &wl_surface_listener, window_);
1833
1834 wp_fractional_scale_manager_v1 *fractional_scale_manager =
1836 if (fractional_scale_manager) {
1837 window_->wp.fractional_scale_handle = wp_fractional_scale_manager_v1_get_fractional_scale(
1838 fractional_scale_manager, window_->wl.surface);
1839 wp_fractional_scale_v1_add_listener(
1840 window_->wp.fractional_scale_handle, &wp_fractional_scale_listener, window_);
1841 }
1842
1843 /* NOTE: The limit is in points (not pixels) so Hi-DPI will limit to larger number of pixels.
1844 * This has the advantage that the size limit is the same when moving the window between monitors
1845 * with different scales set. If it was important to limit in pixels it could be re-calculated
1846 * when the `window_->frame.buffer_scale` changed. */
1847 const int32_t size_min[2] = {320, 240};
1848
1849 const char *xdg_app_id = GHOST_SystemWayland::xdg_app_id_get();
1850
1851 {
1852 window_->xdg_decor = new GWL_XDG_Decor_Window;
1853 GWL_XDG_Decor_Window &decor = *window_->xdg_decor;
1854 decor.surface = xdg_wm_base_get_xdg_surface(system_->xdg_decor_shell_get(),
1855 window_->wl.surface);
1856 decor.toplevel = xdg_surface_get_toplevel(decor.surface);
1857
1858 xdg_toplevel_set_min_size(decor.toplevel, UNPACK2(size_min));
1859 xdg_toplevel_set_app_id(decor.toplevel, xdg_app_id);
1860
1861 xdg_surface_add_listener(decor.surface, &xdg_surface_listener, window_);
1862 xdg_toplevel_add_listener(decor.toplevel, &xdg_toplevel_listener, window_);
1863
1864 if (parent_window && is_dialog) {
1865 GWL_XDG_Decor_Window &decor_parent =
1866 *dynamic_cast<const GHOST_WindowWayland *>(parent_window)->window_->xdg_decor;
1867 xdg_toplevel_set_parent(decor.toplevel, decor_parent.toplevel);
1868 }
1869 }
1870
1871 gwl_window_title_set(window_, title);
1872
1873 wl_surface_set_user_data(window_->wl.surface, this);
1874
1875 /* Commit & dispatch until the initial configure has been seen.
1876 * Failure exits with an error, simply prevent an eternal loop. */
1877 const auto wayland_dispatch_until_configure_seen_or_error_fn = [&]() {
1878 GWL_XDG_Decor_Window &decor = *window_->xdg_decor;
1880 wl_display_flush(display);
1881 wl_display_dispatch(display);
1882 }
1883 };
1884
1885 /* NOTE(@ideasman42): when the display is dispatched in a loop
1886 * starting full-screen intermittently creates windows the wrong size for GNOME (46 .. 49.3).
1887 * The problem is reasonably involved - in short, creating the windows instance early
1888 * causes the full-screen dimensions to be set too early, pushing the window off the monitor
1889 * when it *is* set to it's correct size, see: #153796 for details.
1890 *
1891 * The down side of *not* running this loop is we may not get the correct output scale,
1892 * which could cause the UI to refresh at a different UI-scale on startup. */
1893 const bool configure_deferred = state == GHOST_kWindowStateFullScreen;
1894
1895 /* NOTE: the method used for XDG initialization (using `initial_configure_seen`)
1896 * follows the method used in SDL 3.16. */
1897 {
1898 GWL_XDG_Decor_Window &decor = *window_->xdg_decor;
1899
1900 if (system_->xdg_decor_manager_get()) {
1901 const bool use_window_frame = system_->use_window_frame_get();
1902 decor.toplevel_decor = zxdg_decoration_manager_v1_get_toplevel_decoration(
1903 system_->xdg_decor_manager_get(), decor.toplevel);
1904 zxdg_toplevel_decoration_v1_add_listener(
1906 /* Request client side decorations as a way of disabling decorations
1907 * or to use client side decorations. */
1908 const uint32_t mode = (use_window_frame && system_->use_window_frame_csd_get() == false) ?
1909 ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE :
1910 ZXDG_TOPLEVEL_DECORATION_V1_MODE_CLIENT_SIDE;
1911 zxdg_toplevel_decoration_v1_set_mode(decor.toplevel_decor, mode);
1912 }
1913
1914 if (!configure_deferred) {
1915 wl_surface_commit(window_->wl.surface);
1916
1917 wayland_dispatch_until_configure_seen_or_error_fn();
1918 }
1919 }
1920#ifdef WITH_GHOST_CSD
1921 if (window_->xdg_decor && system_->use_window_frame_csd_get()) {
1922 window_->xdg_csd = new GWL_WindowCSD;
1923 /* Apply capabilities received while dispatching the initial configure. */
1924 gwl_window_csd_layout_apply(window_);
1925 /* Rounded corners require the compositor to blend the surface. */
1926 want_context_params_.use_alpha = system_->getWindowCSD().corner_radius > 0;
1927 }
1928#endif
1929
1930 /* If the scale is known early, setup the window scale.
1931 * Otherwise accept an unsightly flicker once the outputs scale can be found. */
1932 int early_buffer_scale = 0;
1933 int early_fractional_scale = 0;
1934
1935 if (const int test_fractional_scale =
1936 fractional_scale_manager ? (window_->frame_pending.fractional_scale_preferred ?
1937 window_->frame_pending.fractional_scale_preferred :
1938 scale_fractional_from_output) :
1939 0)
1940 {
1941 if (!gwl_round_int_test(test_fractional_scale, FRACTIONAL_DENOMINATOR)) {
1942 early_fractional_scale = test_fractional_scale;
1943 }
1944 else {
1945 /* Rounded, use simple integer buffer scaling. */
1946 early_buffer_scale = test_fractional_scale / FRACTIONAL_DENOMINATOR;
1947 early_fractional_scale = 0;
1948 }
1949 }
1950 else if (buffer_scale_from_output) {
1951 early_buffer_scale = buffer_scale_from_output;
1952 }
1953
1954 if (early_fractional_scale != 0) {
1955 /* Fractional scale is known. */
1956
1957 window_->frame.fractional_scale_preferred = early_fractional_scale;
1958 window_->frame.fractional_scale = early_fractional_scale;
1959 window_->frame.buffer_scale = 1;
1960
1961 window_->frame_pending.fractional_scale_preferred = early_fractional_scale;
1962 window_->frame_pending.fractional_scale = early_fractional_scale;
1963 window_->frame_pending.buffer_scale = 1;
1964
1965 /* The scale is considered initialized now. */
1966 window_->frame_pending.is_scale_init = true;
1967
1968 /* Always commit and set the scale. */
1969 bool surface_needs_commit_dummy = false, surface_needs_buffer_scale_dummy = false;
1971 window_, &surface_needs_commit_dummy, &surface_needs_buffer_scale_dummy);
1972 }
1973 else if (early_buffer_scale != 0) {
1974 /* Non-fractional scale is known. */
1975
1976 /* No fractional scale, simple initialization. */
1977 window_->frame.buffer_scale = early_buffer_scale;
1978 window_->frame_pending.buffer_scale = early_buffer_scale;
1979
1980 /* The scale is considered initialized now. */
1981 window_->frame_pending.is_scale_init = true;
1982
1983 /* The window surface must be rounded to the scale,
1984 * failing to do so causes the WAYLAND-server to close the window immediately. */
1985 gwl_round_int2_by(window_->frame.size, window_->frame.buffer_scale);
1986 }
1987 else {
1988 /* Scale isn't known (the windows size may flicker when #outputs_changed_update_scale runs). */
1989 window_->frame.buffer_scale = 1;
1990 window_->frame_pending.buffer_scale = 1;
1991 GHOST_ASSERT(window_->frame_pending.is_scale_init == false,
1992 "An initialized scale is not expected");
1993 }
1994
1995 if (window_->frame_pending.is_scale_init) {
1996 /* If the output scale changes here it means the scale settings were not properly set. */
1998 "Fractional scale was not properly initialized");
1999 }
2000
2001 wl_surface_set_buffer_scale(window_->wl.surface, window_->frame.buffer_scale);
2002
2003 /* Set the toplevel icon (after the scale is initialized). */
2004 if (window_->xdg_decor) {
2005 system_->xdg_toplevel_icon_update(this, window_->xdg_decor->toplevel);
2006 }
2007
2008 /* Apply Bounds.
2009 * Important to run after the buffer scale is known & before the buffer is created. */
2010 {
2011 const GWL_XDG_Decor_Window &decor = *window_->xdg_decor;
2012 if (decor.initial_bounds[0] && decor.initial_bounds[1]) {
2013 window_->frame.size[0] = std::min(window_->frame.size[0],
2014 decor.initial_bounds[0] * window_->frame.buffer_scale);
2015 window_->frame.size[1] = std::min(window_->frame.size[1],
2016 decor.initial_bounds[1] * window_->frame.buffer_scale);
2017 }
2018 }
2019
2020 /* It's a part of the XDG spec that a window has been configured (via ACK) before a
2021 * buffer is attached, GNOME accepts this but WLROOTS based compositors fail to start. */
2022 if (configure_deferred) {
2023 /* Without the commit, the ACK won't be triggered. */
2024 wl_surface_commit(window_->wl.surface);
2025
2026 wayland_dispatch_until_configure_seen_or_error_fn();
2027 }
2028
2029/* Postpone binding the buffer until after it's decor has been configured:
2030 * - Ensure the window is sized properly (with XDG window decorations), see: #113059.
2031 * - Avoids flickering on startup.
2032 */
2033#ifdef WITH_OPENGL_BACKEND
2034 if (type == GHOST_kDrawingContextTypeOpenGL) {
2035 window_->backend.egl_window = wl_egl_window_create(
2036 window_->wl.surface, int(window_->frame.size[0]), int(window_->frame.size[1]));
2037 }
2038#endif
2039#ifdef WITH_VULKAN_BACKEND
2040 if (type == GHOST_kDrawingContextTypeVulkan) {
2041 window_->backend.vulkan_window_info = new GHOST_ContextVK_WindowInfo;
2042 window_->backend.vulkan_window_info->size[0] = window_->frame.size[0];
2043 window_->backend.vulkan_window_info->size[1] = window_->frame.size[1];
2044 }
2045#endif
2046
2047 /* Drawing context. */
2049 /* This can happen when repeatedly creating windows, see #123096.
2050 * In this case #GHOST_WindowWayland::getValid will return false. */
2051 GHOST_PRINT("Failed to create drawing context" << std::endl);
2052 }
2053 else {
2054 window_->is_valid_setup = true;
2055 }
2056
2057 if (window_->is_valid_setup == false) {
2058 /* Don't attempt to setup the window if there is no context.
2059 * This window is considered invalid and will be removed. */
2060 }
2061 else {
2062 gwl_window_state_set(window_, state);
2063 }
2064
2065 /* Color management */
2066 wp_color_manager_v1 *color_manager = nullptr;
2067#ifdef WITH_VULKAN_BACKEND
2068 if (type == GHOST_kDrawingContextTypeVulkan &&
2069 GHOST_ContextVK::supportsWaylandColorManagement() == GHOST_kSuccess)
2070 {
2071 color_manager = system->wp_color_manager_get();
2072 }
2073#endif
2074 if (color_manager) {
2075 wp_image_description_v1 *image_description = nullptr;
2076
2078 wp_image_description_creator_params_v1 *image_creator_params =
2079 wp_color_manager_v1_create_parametric_creator(color_manager);
2080 wp_image_description_creator_params_v1_set_tf_named(
2081 image_creator_params, WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_EXT_LINEAR);
2082 wp_image_description_creator_params_v1_set_primaries_named(
2083 image_creator_params, WP_COLOR_MANAGER_V1_PRIMARIES_SRGB);
2084
2085 image_description = wp_image_description_creator_params_v1_create(image_creator_params);
2086 hdr_info_.hdr_enabled = true;
2089 }
2091 /* Create an image description compatible with MS-Windows specific definition of scRGB.
2092 *
2093 * NOTE: This code-path is being used by NVIDIA 595 and higher.
2094 *
2095 * - Uses sRGB (BT.709) color primaries and white point
2096 * - Transfer characteristics is linear
2097 * - Value range is extended
2098 * - Value of 1.0 corresponds to 80 cd/m^2
2099 * - Reference white value is unknown but should be assumed to be 2.5375.
2100 *
2101 * Note: EGL_EXT_gl_colorspace_scrgb_linear definition differs from Windows-scRGB by using
2102 * R=G=B=1.0 as the reference white level, while Windows-scRGB reference white level is
2103 * unknown or varies. However, it seems probable that Windows implements both
2104 * EGL_EXT_gl_colorspace_scrgb_linear and Vulkan VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT as
2105 * Windows-scRGB.
2106 *
2107 * https://wayland.app/protocols/color-management-v1#wp_color_manager_v1:request:create_windows_scrgb
2108 */
2109 image_description = wp_color_manager_v1_create_windows_scrgb(color_manager);
2110 hdr_info_.hdr_enabled = true;
2112 hdr_info_.sdr_white_level = 2.5375f;
2113 }
2114
2115 if (image_description) {
2116 /* Wait for ready/failed on a dedicated queue to avoid re-entering other listeners.
2117 * Using a not-yet-ready description could cause a fatal protocol error. */
2118 wl_display *display = system->wl_display_get();
2119 wl_event_queue *event_queue = system->wp_color_manager_queue_get();
2120 wl_proxy_set_queue(reinterpret_cast<wl_proxy *>(image_description), event_queue);
2121
2122 GWL_ImageDescriptionWait wait = {false, false};
2123 wp_image_description_v1_add_listener(image_description, &image_description_listener, &wait);
2124 while (!wait.done) {
2125 if (wl_display_dispatch_queue(display, event_queue) == -1) {
2126 break;
2127 }
2128 }
2129
2130 if (wait.ready) {
2131 window_->wp.color_management_surface = wp_color_manager_v1_get_surface(
2132 color_manager, window_->wl.surface);
2133 wp_color_management_surface_v1_set_image_description(
2134 window_->wp.color_management_surface,
2135 image_description,
2136 WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL);
2137 }
2138 else {
2139 hdr_info_.hdr_enabled = false;
2142 }
2143 wp_image_description_v1_destroy(image_description);
2144 }
2145 }
2146
2147#ifdef WITH_GHOST_CSD
2148 if (window_->xdg_csd) {
2149 /* Whether the corners can be cut away is only known once the context exists, the first window
2150 * frame should reflect that. Without this the compositor would keep treating the corners as
2151 * opaque, showing them as solid blocks. */
2152 gwl_window_csd_opaque_region_update(window_);
2153 }
2154#endif
2155
2156 /* Commit after setting the buffer.
2157 * While postponing until after the buffer drawing is context is set
2158 * isn't essential, it reduces flickering. */
2159 wl_surface_commit(window_->wl.surface);
2160
2161#ifdef WITH_OPENGL_BACKEND
2162 if (type == GHOST_kDrawingContextTypeOpenGL) {
2163 /* NOTE(@ideasman42): Set the swap interval to 0 (disable VSync) to prevent blocking.
2164 * This was reported for SDL in 2021 so it may be good to revisit this decision
2165 * at some point since forcing the VSync setting seems heavy-handed,
2166 * especially if the issue gets resolved up-stream.
2167 *
2168 * For reference: https://github.com/libsdl-org/SDL/issues/4335
2169 * From the report the compositor causing problems was GNOME's Mutter. */
2170 setSwapInterval(0);
2171 }
2172#endif
2173
2174 window_->is_init = true;
2175}
2176
2178{
2179#ifdef USE_EVENT_BACKGROUND_THREAD
2180 std::lock_guard lock_server_guard{*system_->server_mutex};
2181#endif
2182
2184
2185#ifdef WITH_OPENGL_BACKEND
2186 if (window_->ghost_context_type == GHOST_kDrawingContextTypeOpenGL) {
2187 wl_egl_window_destroy(window_->backend.egl_window);
2188 }
2189#endif
2190#ifdef WITH_VULKAN_BACKEND
2191 if (window_->ghost_context_type == GHOST_kDrawingContextTypeVulkan) {
2192 delete window_->backend.vulkan_window_info;
2193 }
2194#endif
2195
2196 gwl_xdg_window_icon_free(window_->xdg_icon);
2197
2198 if (window_->xdg.activation_token) {
2199 xdg_activation_token_v1_destroy(window_->xdg.activation_token);
2200 window_->xdg.activation_token = nullptr;
2201 }
2202
2203 if (window_->wp.fractional_scale_handle) {
2204 wp_fractional_scale_v1_destroy(window_->wp.fractional_scale_handle);
2205 window_->wp.fractional_scale_handle = nullptr;
2206 }
2207
2208 if (window_->wp.viewport) {
2209 wp_viewport_destroy(window_->wp.viewport);
2210 window_->wp.viewport = nullptr;
2211 }
2212
2213 if (window_->wp.color_management_surface) {
2214 wp_color_management_surface_v1_destroy(window_->wp.color_management_surface);
2215 window_->wp.color_management_surface = nullptr;
2216 }
2217
2218 {
2219 gwl_xdg_decor_window_destroy(window_->xdg_decor);
2220 }
2221
2222#ifdef WITH_GHOST_CSD
2223 if (window_->xdg_csd) {
2224 GWL_WindowCSD &xdg_csd = *window_->xdg_csd;
2225 if (xdg_csd.margin_buffer) {
2226 wl_buffer_destroy(xdg_csd.margin_buffer);
2227 }
2228 if (xdg_csd.margin_viewport) {
2229 wp_viewport_destroy(xdg_csd.margin_viewport);
2230 }
2231 if (xdg_csd.margin_subsurface) {
2232 wl_subsurface_destroy(xdg_csd.margin_subsurface);
2233 }
2234 if (xdg_csd.margin_surface) {
2235 system_->window_surface_unref(xdg_csd.margin_surface);
2236 wl_surface_destroy(xdg_csd.margin_surface);
2237 }
2238 delete window_->xdg_csd;
2239 window_->xdg_csd = nullptr;
2240 }
2241#endif
2242
2243 /* Clear any pointers to this window. This is needed because there are no guarantees
2244 * that flushing the display will the "leave" handlers before handling events. */
2245 system_->window_surface_unref(window_->wl.surface);
2246
2247 wl_surface_destroy(window_->wl.surface);
2248
2249 /* NOTE(@ideasman42): Flushing will often run the appropriate handlers event
2250 * (#wl_surface_listener.leave in particular) to avoid attempted access to the freed surfaces.
2251 * This is not fool-proof though, hence the call to #window_surface_unref, see: #99078. */
2252 wl_display_flush(system_->wl_display_get());
2253
2254 if (window_->cursor_generator) {
2255 gwl_window_cursor_custom_free(window_->cursor_generator);
2256 }
2257
2258 delete window_;
2259}
2260
2261#ifdef USE_EVENT_BACKGROUND_THREAD
2263{
2264 GHOST_ASSERT(system_->main_thread_id == std::this_thread::get_id(), "Only from main thread!");
2266}
2267#endif /* USE_EVENT_BACKGROUND_THREAD */
2268
2270{
2271 return system_->cursor_shape_check(cursor_shape);
2272}
2273
2275{
2276#ifdef USE_EVENT_BACKGROUND_THREAD
2277 std::lock_guard lock_server_guard{*system_->server_mutex};
2278#endif
2279
2280 GHOST_Rect bounds_buf;
2281 const GHOST_Rect *bounds = nullptr;
2283 if (getCursorGrabBounds(bounds_buf) == GHOST_kFailure) {
2284 getClientBounds(bounds_buf);
2285 }
2286 bounds = &bounds_buf;
2287 }
2288
2289 if (system_->window_cursor_grab_set(mode,
2292 bounds,
2294 window_->wl.surface,
2295 this->scale_params_get(),
2296 this->window_->ghost_context_type))
2297 {
2298 return GHOST_kSuccess;
2299 }
2300 return GHOST_kFailure;
2301}
2302
2304{
2305#ifdef USE_EVENT_BACKGROUND_THREAD
2306 std::lock_guard lock_server_guard{*system_->server_mutex};
2307#endif
2308
2309 const bool is_active = this == static_cast<const GHOST_WindowWayland *>(
2310 system_->getWindowManager()->getActiveWindow());
2311 gwl_window_cursor_custom_clear(&window_->cursor_generator);
2312 cursor_shape_ = shape;
2313
2314 GHOST_TSuccess ok;
2315 if (is_active) {
2316 ok = system_->cursor_shape_set(cursor_shape_);
2317 GHOST_TSuccess ok_test = ok;
2318 if (ok == GHOST_kFailure) {
2319 /* Failed, try again with the default cursor. */
2321 ok_test = system_->cursor_shape_set(cursor_shape_);
2322 }
2323
2324 wl_display *display = system_->wl_display_get();
2325#ifdef USE_CURSOR_IMMEDIATE_DISPATCH
2326 if (ok == GHOST_kSuccess || ok_test == GHOST_kSuccess) {
2327 wl_display_flush(display);
2329 }
2330 else
2331#endif /* USE_CURSOR_IMMEDIATE_DISPATCH */
2332 if (ok_test == GHOST_kFailure) {
2333 /* For the cursor to display when the event queue isn't being handled. */
2334 wl_display_flush(display);
2335 }
2336 }
2337 else {
2338 /* Set later when activating the window. */
2339 ok = system_->cursor_shape_check(shape);
2340 if (ok == GHOST_kFailure) {
2342 }
2343 }
2344 return ok;
2345}
2346
2348{
2349#ifdef USE_EVENT_BACKGROUND_THREAD
2350 std::lock_guard lock_server_guard{*system_->server_mutex};
2351#endif
2352 return system_->cursor_grab_use_software_display_get(cursor_grab_);
2353}
2354
2356 GHOST_CursorGenerator *cursor_generator)
2357{
2358 /* Before this, all logic is just setting up the cursor. */
2359#ifdef USE_EVENT_BACKGROUND_THREAD
2360 std::lock_guard lock_server_guard{*system_->server_mutex};
2361#endif
2363 if (window_->cursor_generator) {
2364 gwl_window_cursor_custom_free(window_->cursor_generator);
2365 }
2366 window_->cursor_generator = cursor_generator;
2367
2369
2370 /* Let refresh handle applying the changes. */
2371 if (success == GHOST_kSuccess) {
2372 wl_display *display = system_->wl_display_get();
2373 /* For the cursor to display when the event queue isn't being handled. */
2374 wl_display_flush(display);
2375#ifdef USE_CURSOR_IMMEDIATE_DISPATCH
2377#endif
2378 }
2379 return success;
2380}
2381
2383 const uint8_t *mask,
2384 const int size[2],
2385 const int hot_spot[2],
2386 const bool can_invert_color)
2387{
2388 /* This is no longer needed as all cursors are generated on demand. */
2389 GHOST_ASSERT(false, "All cursors must be generated!");
2390 (void)bitmap;
2391 (void)mask;
2392 (void)size;
2393 (void)hot_spot;
2394 (void)can_invert_color;
2395
2396 return GHOST_kFailure;
2397}
2398
2400{
2401#ifdef USE_EVENT_BACKGROUND_THREAD
2402 std::lock_guard lock_server_guard{*system_->server_mutex};
2403#endif
2404 return system_->cursor_bitmap_get(bitmap);
2405}
2406
2408{
2409 return GHOST_Window::getValid() && window_->is_valid_setup;
2410}
2411
2412void GHOST_WindowWayland::setTitle(const char *title)
2413{
2414#ifdef USE_EVENT_BACKGROUND_THREAD
2415 std::lock_guard lock_server_guard{*system_->server_mutex};
2416#endif
2417 gwl_window_title_set(window_, title);
2418}
2419
2421{
2422 /* No need to lock `server_mutex` (WAYLAND never changes this). */
2423 return window_->title.empty() ? "untitled" : window_->title;
2424}
2425
2427{
2428 getClientBounds(bounds);
2429}
2430
2432{
2433 /* No need to lock `server_mutex` (WAYLAND never changes this in a thread). */
2434 bounds.set(0, 0, UNPACK2(window_->frame.size));
2435}
2436
2438{
2439 return setClientSize(width, uint32_t(window_->frame.size[1]));
2440}
2441
2443{
2444 return setClientSize(uint32_t(window_->frame.size[0]), height);
2445}
2446
2447GHOST_TSuccess GHOST_WindowWayland::setClientSize(const uint32_t width, const uint32_t height)
2448{
2449#ifdef USE_EVENT_BACKGROUND_THREAD
2450 std::lock_guard lock_server_guard{*system_->server_mutex};
2451 std::lock_guard lock_frame_guard{window_->frame_pending_mutex};
2452#endif
2453
2454 GWL_WindowFrame &frame_pending = window_->frame_pending;
2455
2456 frame_pending.size[0] = width;
2457 frame_pending.size[1] = height;
2458 gwl_round_int2_by(frame_pending.size, frame_pending.buffer_scale);
2459
2460 gwl_window_frame_pending_size_set(window_, nullptr, nullptr, nullptr);
2461
2462 return GHOST_kSuccess;
2463}
2464
2466 const int32_t inY,
2467 int32_t &outX,
2468 int32_t &outY) const
2469{
2470 outX = inX;
2471 outY = inY;
2472}
2473
2475 const int32_t inY,
2476 int32_t &outX,
2477 int32_t &outY) const
2478{
2479 outX = inX;
2480 outY = inY;
2481}
2482
2484{
2485 return window_->frame.tiled_edges;
2486}
2487
2489{
2490 /* Early out if use of DPI scale is disabled. */
2491 if (!system_->native_pixel_) {
2492 return base_dpi;
2493 }
2494
2495 /* No need to lock `server_mutex`
2496 * (`outputs_changed_update_scale` never changes values in a non-main thread). */
2497
2498 if (window_->frame.fractional_scale) {
2499 return gwl_window_fractional_to_viewport(window_->frame, base_dpi);
2500 }
2501
2502 return window_->frame.buffer_scale * base_dpi;
2503}
2504
2506{
2507#ifdef USE_EVENT_BACKGROUND_THREAD
2508 std::lock_guard lock_server_guard{*system_->server_mutex};
2509#endif
2510 const GHOST_TSuccess ok = system_->cursor_visibility_set(visible);
2511 if (ok == GHOST_kSuccess) {
2512 wl_display *display = system_->wl_display_get();
2513 /* For the cursor to display when the event queue isn't being handled. */
2514 wl_display_flush(display);
2515#ifdef USE_CURSOR_IMMEDIATE_DISPATCH
2517#endif
2518 }
2519 return ok;
2520}
2521
2523{
2524#ifdef USE_EVENT_BACKGROUND_THREAD
2525 std::lock_guard lock_server_guard{*system_->server_mutex};
2526#endif
2528}
2529
2531{
2532#ifdef USE_EVENT_BACKGROUND_THREAD
2533 std::lock_guard lock_server_guard{*system_->server_mutex};
2534#endif
2535 return gwl_window_state_get(window_);
2536}
2537
2542
2544{
2545 /* NOTE(@ideasman42): only activation is supported (on X11 & Cocoa for example)
2546 * both activation and raising is performed. Since WAYLAND only supports activation,
2547 * do that as the compositor will likely raise the window as well.
2548 * Although it's not ideal that raising does something but lowering a window is ignored. */
2549 if (order == GHOST_kWindowOrderTop) {
2550 gwl_window_activate(window_);
2551 }
2552
2553 return GHOST_kSuccess;
2554}
2555
2557{
2558 return window_->is_dialog;
2559}
2560
2561GHOST_Context *GHOST_WindowWayland::newDrawingContext(GHOST_TDrawingContextType type)
2562{
2563 switch (type) {
2566 return context;
2567 }
2568
2569#ifdef WITH_VULKAN_BACKEND
2570 case GHOST_kDrawingContextTypeVulkan: {
2571 GHOST_ContextVK *context = new GHOST_ContextVK(want_context_params_,
2572 GHOST_kVulkanPlatformWayland,
2573 0,
2574 nullptr,
2575 window_->wl.surface,
2576 system_->wl_display_get(),
2577 window_->backend.vulkan_window_info,
2578 1,
2579 1,
2580 preferred_device_,
2581 &hdr_info_);
2582 if (context->initializeDrawingContext()) {
2583 return context;
2584 }
2585 delete context;
2586 return nullptr;
2587 }
2588#endif
2589
2590#ifdef WITH_OPENGL_BACKEND
2591 case GHOST_kDrawingContextTypeOpenGL: {
2592 for (int minor = 6; minor >= 3; --minor) {
2593 GHOST_Context *context = new GHOST_ContextEGL(
2594 system_,
2596 EGLNativeWindowType(window_->backend.egl_window),
2597 EGLNativeDisplayType(system_->wl_display_get()),
2598 EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
2599 4,
2600 minor,
2602 (want_context_params_.is_debug ? EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR : 0),
2604 EGL_OPENGL_API);
2605
2606 if (context->initializeDrawingContext()) {
2607 return context;
2608 }
2609 delete context;
2610 }
2611 return nullptr;
2612 }
2613#endif
2614
2615 default:
2616 /* Unsupported backend. */
2617 return nullptr;
2618 }
2619}
2620
2621#ifdef WITH_INPUT_IME
2622
2623void GHOST_WindowWayland::beginIME(
2624 const int32_t x, const int32_t y, const int32_t w, const int32_t h, const bool completed)
2625{
2626 /* WAYLAND anchors the rectangle at its top-left. */
2627 system_->ime_begin(this, x, y - h, w, h, completed);
2628}
2629
2630void GHOST_WindowWayland::endIME()
2631{
2632 system_->ime_end(this);
2633}
2634
2635#endif
2636
2638
2639/* -------------------------------------------------------------------- */
2644
2646{
2647 return window_->frame.buffer_scale;
2648}
2649
2651{
2652 if (icon.buffer) {
2653 wl_buffer_destroy(icon.buffer);
2654 munmap(icon.buffer_data, icon.buffer_size);
2655 icon = {};
2656 }
2657}
2658
2660{
2661 return window_->xdg_icon;
2662}
2663
2665{
2666 /* NOTE(@ideasman42): This could be kept initialized,
2667 * since it's such a small struct it's not so important. */
2668 GWL_WindowScaleParams *scale_params = &window_->scale_params;
2669 scale_params->is_fractional = (window_->frame.fractional_scale != 0);
2670 scale_params->scale = scale_params->is_fractional ? window_->frame.fractional_scale :
2671 window_->frame.buffer_scale;
2672 return *scale_params;
2673}
2674
2675wl_fixed_t GHOST_WindowWayland::wl_fixed_from_window(wl_fixed_t value) const
2676{
2677 if (window_->frame.fractional_scale) {
2678 return gwl_window_fractional_from_viewport(window_->frame, value);
2679 }
2680 return value / window_->frame.buffer_scale;
2681}
2682
2683wl_fixed_t GHOST_WindowWayland::wl_fixed_to_window(wl_fixed_t value) const
2684{
2685 if (window_->frame.fractional_scale) {
2686 return gwl_window_fractional_to_viewport(window_->frame, value);
2687 }
2688 return value * window_->frame.buffer_scale;
2689}
2690
2692{
2693 return window_->wl.surface;
2694}
2695
2696const std::span<GWL_Output *const> GHOST_WindowWayland::outputs_get() const
2697{
2698 return window_->outputs;
2699}
2700
2702
2703/* -------------------------------------------------------------------- */
2708
2710{
2711 return system_->pushEvent_maybe_pending(
2712 std::make_unique<GHOST_Event>(system_->getMilliSeconds(), GHOST_kEventWindowClose, this));
2713}
2714
2716{
2717 /* When first initializing from the main thread, activation is called directly,
2718 * otherwise activation is performed when processing pending events. */
2719#ifdef USE_EVENT_BACKGROUND_THREAD
2720 const bool is_main_thread = system_->main_thread_id == std::this_thread::get_id();
2721 if (is_main_thread)
2722#endif
2723 {
2724 /* This can run while the window being initialized.
2725 * In this case, skip setting the window active but add the event, see: #120465. */
2726 if (window_->is_init) {
2727 if (system_->getWindowManager()->setActiveWindow(this) == GHOST_kFailure) {
2728 return GHOST_kFailure;
2729 }
2730 }
2731 }
2732 const GHOST_TSuccess success = system_->pushEvent_maybe_pending(
2733 std::make_unique<GHOST_Event>(system_->getMilliSeconds(), GHOST_kEventWindowActivate, this));
2734 return success;
2735}
2736
2738{
2739 /* When first initializing from the main thread, deactivation is called directly,
2740 * otherwise deactivation is performed when processing pending events. */
2741#ifdef USE_EVENT_BACKGROUND_THREAD
2742 const bool is_main_thread = system_->main_thread_id == std::this_thread::get_id();
2743 if (is_main_thread)
2744#endif
2745 {
2746 /* See code comments for #GHOST_WindowWayland::activate. */
2747 if (window_->is_init) {
2748 system_->getWindowManager()->setWindowInactive(this);
2749 }
2750 }
2751 const GHOST_TSuccess success = system_->pushEvent_maybe_pending(std::make_unique<GHOST_Event>(
2752 system_->getMilliSeconds(), GHOST_kEventWindowDeactivate, this));
2753 return success;
2754}
2755
2757{
2758 return system_->pushEvent_maybe_pending(
2759 std::make_unique<GHOST_Event>(system_->getMilliSeconds(), GHOST_kEventWindowSize, this));
2760}
2761
2763{
2764 /* NOTE: we want to `swapBuffers`, however this may run from a thread and
2765 * when this windows OpenGL context is not active, so send and update event instead. */
2766 return system_->pushEvent_maybe_pending(std::make_unique<GHOST_Event>(
2767 system_->getMilliSeconds(), GHOST_kEventWindowUpdateDecor, this));
2768}
2769
2771
2772/* -------------------------------------------------------------------- */
2777
2779{
2780#ifdef USE_EVENT_BACKGROUND_THREAD
2781 if (system_->main_thread_id != std::this_thread::get_id()) {
2783 return GHOST_kSuccess;
2784 }
2785#endif
2786 return gwl_window_cursor_shape_refresh(cursor_shape_, window_->cursor_generator, system_);
2787}
2788
2790{
2791#ifdef USE_EVENT_BACKGROUND_THREAD
2792
2793 /* NOTE: if deferring causes problems, it could be isolated to the first scale initialization
2794 * See: #GWL_WindowFrame::is_scale_init. */
2796#else
2798#endif
2799}
2800
2802{
2803 /* NOTE: a current limitation is that a change in monitor scale while using --no-native-pixels
2804 * will scale the window content up/down. */
2805 if (!system_->native_pixel_) {
2806 return false;
2807 }
2808
2809#ifdef USE_EVENT_BACKGROUND_THREAD
2810 if (system_->main_thread_id != std::this_thread::get_id()) {
2812 return false;
2813 }
2814#endif
2815 int fractional_scale_next = -1;
2816 int fractional_scale_from_output = 0;
2817
2818 int scale_next = outputs_max_scale_or_default(outputs_get(), 0, &fractional_scale_from_output);
2819
2820 if (scale_next == 0) [[unlikely]] {
2821 return false;
2822 }
2823
2824#ifdef USE_EVENT_BACKGROUND_THREAD
2825 std::lock_guard lock_frame_guard{window_->frame_pending_mutex};
2826#endif
2827
2828 if (window_->wp.fractional_scale_handle) {
2829 /* Let the #wp_fractional_scale_v1_listener::preferred_scale callback handle
2830 * changes to the windows scale. */
2831 if (window_->frame_pending.fractional_scale_preferred != 0) {
2832 fractional_scale_next = window_->frame_pending.fractional_scale_preferred;
2833 scale_next = fractional_scale_next / FRACTIONAL_DENOMINATOR;
2834 }
2835 }
2836
2837 if (fractional_scale_next == -1) {
2838 fractional_scale_next = fractional_scale_from_output;
2839 scale_next = fractional_scale_next / FRACTIONAL_DENOMINATOR;
2840 }
2841
2842 bool changed = false;
2843
2844 const bool is_fractional_prev = window_->frame.fractional_scale != 0;
2845 const bool is_fractional_next = (fractional_scale_next % FRACTIONAL_DENOMINATOR) != 0;
2846
2847 /* When non-fractional, never use fractional scaling! */
2848 window_->frame_pending.fractional_scale = is_fractional_next ? fractional_scale_next : 0;
2849 window_->frame_pending.buffer_scale = is_fractional_next ?
2850 1 :
2851 fractional_scale_next / FRACTIONAL_DENOMINATOR;
2852
2853 const int fractional_scale_prev = window_->frame.fractional_scale ?
2854 window_->frame.fractional_scale :
2855 window_->frame.buffer_scale * FRACTIONAL_DENOMINATOR;
2856 const int scale_prev = fractional_scale_prev / FRACTIONAL_DENOMINATOR;
2857
2858 /* Resizing implies updating. */
2859 bool do_frame_resize = false;
2860 bool do_frame_update = false;
2861
2862 if (window_->frame_pending.is_scale_init == false) {
2863 window_->frame_pending.is_scale_init = true;
2864
2865 /* NOTE(@ideasman42): Needed because new windows are created at their previous pixel-dimensions
2866 * as the window doesn't save it's DPI. Restore the window size under the assumption it's
2867 * opening on the same monitor so a window keeps it's previous size on a users system.
2868 *
2869 * To support anything more sophisticated, windows would need to be created with a scale
2870 * argument (representing the scale used when the window was stored, for example). */
2871
2872 /* Leave `window_->frame_pending` as-is, so changes are detected and updates are applied. */
2873 do_frame_resize = false;
2874 do_frame_update = true;
2875
2876 /* If the buffer scale changes, the window size (and underlying buffer-size)
2877 * must always be a multiple of the buffer size. Resizing ensures this is the case.
2878 * See replies to #135764 for details.
2879 *
2880 * NOTE: We could skip resize if the current window size is a multiple of the buffer scale,
2881 * avoids this as it will result in unpredictable behavior based on single pixel differences
2882 * in window size. */
2883 if (window_->frame_pending.buffer_scale != window_->frame.buffer_scale) {
2884 do_frame_resize = true;
2885 }
2886 }
2887 else {
2888 /* Test if the scale changed. */
2889 if ((fractional_scale_prev != fractional_scale_next) ||
2890 (window_->frame_pending.buffer_scale != window_->frame.buffer_scale))
2891 {
2892 do_frame_resize = true;
2893 }
2894 }
2895
2896 if (do_frame_resize) {
2897 /* Resize the window failing to do so results in severe flickering with a
2898 * multi-monitor setup when multiple monitors have different scales.
2899 *
2900 * NOTE: some flickering is still possible even when resizing this
2901 * happens when dragging the right hand side of the title-bar in KDE
2902 * as expanding changed the size on the RHS, this may be up to the compositor to fix. */
2903 for (size_t i = 0; i < ARRAY_SIZE(window_->frame_pending.size); i++) {
2904 const int value = window_->frame_pending.size[i] ? window_->frame_pending.size[i] :
2905 window_->frame.size[i];
2906 if (is_fractional_prev || is_fractional_next) {
2907 window_->frame_pending.size[i] = lroundf((value * double(fractional_scale_next)) /
2908 double(fractional_scale_prev));
2909 }
2910 else {
2911 window_->frame_pending.size[i] = (value * scale_next) / scale_prev;
2912 }
2913 if (window_->frame_pending.buffer_scale > 1) {
2914 gwl_round_int_by(&window_->frame_pending.size[i], window_->frame_pending.buffer_scale);
2915 }
2916 }
2917 do_frame_update = true;
2918 }
2919
2920 if (do_frame_update) {
2922 changed = true;
2923 }
2924
2925 return changed;
2926}
2927
2929{
2930 std::vector<GWL_Output *> &outputs = window_->outputs;
2931 if (std::ranges::find(outputs, output) != outputs.end()) {
2932 return false;
2933 }
2934 outputs.push_back(output);
2935 return true;
2936}
2937
2939{
2940 return std::erase(window_->outputs, output) > 0;
2941}
2942
2943#ifdef USE_EVENT_BACKGROUND_THREAD
2944
2946{
2947 /* Caller must lock `server_mutex`, while individual actions could lock,
2948 * it's simpler to lock once when handling all window actions. */
2949 GWL_Window *win = window_;
2950 GHOST_ASSERT(win->ghost_system->main_thread_id == std::this_thread::get_id(),
2951 "Run from main thread!");
2952
2954}
2955
2956#endif /* USE_EVENT_BACKGROUND_THREAD */
2957
2958#ifdef WITH_GHOST_CSD
2959
2960xdg_toplevel *GHOST_WindowWayland::xdg_toplevel_get()
2961{
2962 if (window_->xdg_decor) {
2963 return window_->xdg_decor->toplevel;
2964 }
2965 return nullptr;
2966}
2967
2968GHOST_TWindowState GHOST_WindowWayland::xdg_toplevel_state_get()
2969{
2970 /* This mainly exists for the moment for CSD buttons.
2971 * #getState can't be used as it causes a deadlock. */
2972 return gwl_window_state_get(window_);
2973}
2974
2975GHOST_TCSD_Type GHOST_WindowWayland::csd_elem_active_type_get() const
2976{
2977 GHOST_ASSERT(this->system_->use_window_frame_csd_get(), "caller must ensure");
2978 return window_->xdg_csd->active_type;
2979}
2980void GHOST_WindowWayland::csd_elem_active_type_set(GHOST_TCSD_Type type)
2981{
2982 GHOST_ASSERT(this->system_->use_window_frame_csd_get(), "caller must ensure");
2983 window_->xdg_csd->active_type = type;
2984}
2985const GHOST_CSD_Elem *GHOST_WindowWayland::csd_layout(int *r_num)
2986{
2987 GHOST_ASSERT(this->system_->use_window_frame_csd_get(), "caller must ensure");
2988 *r_num = window_->xdg_csd->csd_elems_num;
2989 return window_->xdg_csd->csd_elems;
2990}
2991
2992void GHOST_WindowWayland::csd_layout_refresh()
2993{
2994 if (!window_->xdg_csd) {
2995 return;
2996 }
2997 gwl_window_csd_layout_apply(window_);
2999 gwl_window_csd_elems_update(window_);
3000 }
3001 this->notify_decor_redraw();
3002}
3003
3004GHOST_CSD_EventState &GHOST_WindowWayland::csd_eventstate_get()
3005{
3006 GHOST_ASSERT(this->system_->use_window_frame_csd_get(), "caller must ensure");
3007 return window_->xdg_csd->event_state;
3008}
3009
3010void GHOST_WindowWayland::csd_margin_geometry_get(int32_t &r_margin_size,
3011 int32_t r_window_size[2]) const
3012{
3013 GHOST_ASSERT(this->system_->use_window_frame_csd_get(), "caller must ensure");
3014 const GWL_WindowCSD *xdg_csd = window_->xdg_csd;
3015 r_margin_size = xdg_csd->margin_size_local;
3016 r_window_size[0] = xdg_csd->margin_window_size_local[0];
3017 r_window_size[1] = xdg_csd->margin_window_size_local[1];
3018}
3019
3020#endif /* WITH_GHOST_CSD */
3021
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
#define CLOG_INFO(clg_ref,...)
Definition CLG_log.h:190
#define GHOST_OPENGL_EGL_CONTEXT_FLAGS
#define GHOST_OPENGL_EGL_RESET_NOTIFICATION_STRATEGY
#define wl_display
#define wl_surface
#define GHOST_ASSERT(x, info)
#define GHOST_PRINT(x)
GWL_Output * ghost_wl_output_user_data(wl_output *wl_output)
void ghost_wl_surface_tag(wl_surface *wl_surface)
bool ghost_wl_display_report_error_if_set(wl_display *display)
bool ghost_wl_output_own(const wl_output *wl_output)
wl_buffer * ghost_wl_buffer_create_transparent_pixel(wl_shm *shm)
wl_fixed_t gwl_window_scale_wl_fixed_to(const GWL_WindowScaleParams &scale_params, wl_fixed_t value)
wl_fixed_t gwl_window_scale_wl_fixed_from(const GWL_WindowScaleParams &scale_params, wl_fixed_t value)
int gwl_window_scale_int_to(const GWL_WindowScaleParams &scale_params, int value)
#define FRACTIONAL_DENOMINATOR
int gwl_window_scale_buffer_size_to(const GWL_WindowScaleParams &scale_params, int logical_size, int *r_buffer_scale)
int gwl_window_scale_int_from(const GWL_WindowScaleParams &scale_params, int value)
#define GHOST_CSD_DPI_FRACTIONAL_BASE
GHOST_TWindowState
@ GHOST_kWindowStateMinimized
@ GHOST_kWindowStateMaximized
@ GHOST_kWindowStateNormal
@ GHOST_kWindowStateFullScreen
GHOST_TStandardCursor
@ GHOST_kStandardCursorCustom
@ GHOST_kStandardCursorDefault
@ GHOST_kEventWindowClose
@ GHOST_kEventWindowSize
@ GHOST_kEventWindowActivate
@ GHOST_kEventWindowUpdateDecor
@ GHOST_kEventWindowDeactivate
@ GHOST_kEventWindowDPIHintChanged
GHOST_TCSD_Type
@ GHOST_kCSDTypeBody
@ GHOST_kCSDTypeButtonClose
@ GHOST_kCSDTypeButtonMinimize
@ GHOST_kCSDTypeButtonMaximize
@ GHOST_kCSDTypeButtonMenu
@ GHOST_kCSDTypeTitlebar
GHOST_TDrawingContextType
@ GHOST_kDrawingContextTypeNone
GHOST_TWindowOrder
@ GHOST_kWindowOrderTop
GHOST_TSuccess
@ GHOST_kFailure
@ GHOST_kSuccess
GHOST_TGrabCursorMode
@ GHOST_kGrabWrap
#define GHOST_kCSDType_NUM
GHOST_TWindowTiledFlag
@ GHOST_kWindowTiledRight
@ GHOST_kWindowTiledNone
@ GHOST_kWindowTiledLeft
@ GHOST_kWindowTiledBottom
@ GHOST_kWindowTiledTop
#define WL_ARRAY_FOR_EACH(pos, array)
static void wp_fractional_scale_handle_preferred_scale(void *data, wp_fractional_scale_v1 *, uint preferred_scale)
static bool gwl_round_int_test(const int value, const int round_value)
static GHOST_TSuccess gwl_window_cursor_shape_refresh(GHOST_TStandardCursor shape, const GHOST_CursorGenerator *cg, GHOST_SystemWayland *system)
eGWL_PendingWindowActions
@ PENDING_WINDOW_FRAME_CONFIGURE
@ PENDING_OUTPUT_SCALE_UPDATE_DEFERRED
@ PENDING_WINDOW_CURSOR_SHAPE_REFRESH
@ PENDING_WINDOW_SURFACE_COMMIT
@ PENDING_OUTPUT_SCALE_UPDATE
static CLG_LogRef LOG_WL_IMAGE_DESCRIPTION
void gwl_xdg_window_icon_free(GWL_XDG_WindowIcon &icon)
static void gwl_window_cursor_custom_clear(GHOST_CursorGenerator **cg)
static void gwl_round_int2_by(int value_p[2], const int round_value)
static uint divide_ceil_uint(const uint x, const uint d)
static void surface_handle_leave(void *data, wl_surface *, wl_output *wl_output)
static void gwl_window_frame_pending_fractional_scale_set_notest(GWL_Window *win, bool *r_surface_needs_commit, bool *r_surface_needs_buffer_scale)
static CLG_LogRef LOG_WL_XDG_TOPLEVEL_DECORATION
static void gwl_window_frame_update_from_pending_no_lock(GWL_Window *win)
static int gwl_window_fractional_from_viewport(const GWL_WindowFrame &frame, const int value)
static bool gwl_window_viewport_unset(GWL_Window *win, bool *r_surface_needs_commit, bool *r_surface_needs_buffer_scale)
static int outputs_uniform_scale_or_default(const std::span< GWL_Output *const > outputs, const int32_t scale_default, int *r_scale_fractional)
static GHOST_TWindowState gwl_window_state_get(const GWL_Window *win)
#define PENDING_NUM
static const wp_image_description_v1_listener image_description_listener
static void xdg_toplevel_handle_configure(void *data, xdg_toplevel *, const int32_t width, const int32_t height, wl_array *states)
static CLG_LogRef LOG_WL_XDG_SURFACE
static void gwl_window_frame_pending_fractional_scale_set(GWL_Window *win, bool *r_surface_needs_commit, bool *r_surface_needs_buffer_scale)
static void xdg_surface_handle_configure(void *data, xdg_surface *xdg_surface, const uint32_t serial)
static int gwl_window_fractional_to_viewport_round(const GWL_WindowFrame &frame, const int value)
static void gwl_window_frame_update_from_pending(GWL_Window *win)
static const wp_fractional_scale_v1_listener wp_fractional_scale_listener
static CLG_LogRef LOG_WL_SURFACE
static const xdg_toplevel_listener xdg_toplevel_listener
static const zxdg_toplevel_decoration_v1_listener xdg_toplevel_decoration_v1_listener
static GHOST_TSuccess gwl_window_cursor_custom_load(const GHOST_CursorGenerator &cg, GHOST_SystemWayland *system)
static bool gwl_window_state_set(GWL_Window *win, const GHOST_TWindowState state)
static void image_description_handle_ready(void *data, wp_image_description_v1 *, uint32_t identity)
static void gwl_window_frame_pending_size_set(GWL_Window *win, bool *r_surface_needs_commit, bool *r_surface_needs_resize_for_backend, bool *r_surface_needs_buffer_scale)
static bool gwl_window_state_set_for_xdg(xdg_toplevel *toplevel, const GHOST_TWindowState state, const GHOST_TWindowState state_current)
static void gwl_xdg_decor_window_destroy(GWL_XDG_Decor_Window *decor)
static void xdg_toplevel_handle_wm_capabilities(void *data, xdg_toplevel *, wl_array *capabilities)
static CLG_LogRef LOG_WL_FRACTIONAL_SCALE
static void gwl_window_activate(GWL_Window *win)
static void gwl_window_resize_for_backend(GWL_Window *win, const int32_t size[2])
static constexpr size_t base_dpi
static void gwl_window_pending_actions_handle(GWL_Window *win)
static int outputs_max_scale_or_default(const std::span< GWL_Output *const > outputs, const int32_t scale_default, int *r_scale_fractional)
static const xdg_surface_listener xdg_surface_listener
static int gwl_window_fractional_from_viewport_round(const GWL_WindowFrame &frame, const int value)
static int output_scale_cmp(const GWL_Output *output_a, const GWL_Output *output_b)
static void image_description_handle_failed(void *data, wp_image_description_v1 *, uint32_t cause, const char *msg)
static void gwl_round_int_by(int *value_p, const int round_value)
static CLG_LogRef LOG_WL_XDG_TOPLEVEL
static bool gwl_window_viewport_size_update(GWL_Window *win)
static void xdg_toplevel_handle_configure_bounds(void *data, xdg_toplevel *, const int32_t width, const int32_t height)
static void xdg_activation_handle_done(void *data, xdg_activation_token_v1 *xdg_activation_token_v1, const char *token)
static const xdg_activation_token_v1_listener xdg_activation_listener
static void gwl_window_pending_actions_tag(GWL_Window *win, enum eGWL_PendingWindowActions type)
static void surface_handle_enter(void *data, wl_surface *, wl_output *wl_output)
static int gwl_window_fractional_to_viewport(const GWL_WindowFrame &frame, const int value)
static bool gwl_window_viewport_set(GWL_Window *win, bool *r_surface_needs_commit, bool *r_surface_needs_buffer_scale)
static void gwl_window_cursor_custom_free(GHOST_CursorGenerator *cg)
static void gwl_window_title_set(GWL_Window *win, const char *title)
static void xdg_toplevel_decoration_handle_configure(void *data, zxdg_toplevel_decoration_v1 *, const uint32_t mode)
static const xdg_activation_token_v1_listener * xdg_activation_listener_get()
static const wl_surface_listener wl_surface_listener
static void xdg_toplevel_handle_close(void *data, xdg_toplevel *)
void gwl_xdg_window_icon_free(GWL_XDG_WindowIcon &icon)
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
#define UNPACK2(a)
#define ELEM(...)
SIMD_FORCE_INLINE btVector3 transform(const btVector3 &point) const
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define output
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
GHOST_ContextEGL(const GHOST_System *const system, const GHOST_ContextParams &context_params, EGLNativeWindowType nativeWindow, EGLNativeDisplayType nativeDisplay, EGLint contextProfileMask, EGLint contextMajorVersion, EGLint contextMinorVersion, EGLint contextFlags, EGLint contextResetNotificationStrategy, EGLenum api)
GHOST_ContextNone(const GHOST_ContextParams &context_params)
GHOST_ContextVK(const GHOST_ContextParams &context_params, GHOST_TVulkanPlatformType platform, Window window, Display *display, wl_surface *wayland_surface, wl_display *wayland_display, const GHOST_ContextVK_WindowInfo *wayland_window_info, int contextMajorVersion, int contextMinorVersion, const GHOST_GPUDevice &preferred_device, const GHOST_WindowHDRInfo *hdr_info_=nullptr)
virtual void set(int32_t l, int32_t t, int32_t r, int32_t b)
static const char * xdg_app_id_get()
bool supports_color_manager_extended_srgb_linear() const
struct wl_event_queue * wp_color_manager_queue_get()
void xdg_toplevel_icon_update(GHOST_WindowWayland *window, struct xdg_toplevel *toplevel)
bool supports_color_manager_feature_windows_scrgb() const
uint64_t getMilliSeconds() const override
std::thread::id main_thread_id
struct wl_display * wl_display_get()
struct wl_compositor * wl_compositor_get()
struct wp_fractional_scale_manager_v1 * wp_fractional_scale_manager_get()
struct wl_subcompositor * wl_subcompositor_get()
struct wl_shm * wl_shm_get() const
GHOST_TSuccess cursor_shape_set(GHOST_TStandardCursor shape)
struct wp_viewporter * wp_viewporter_get()
struct wl_seat * wl_seat_active_get_with_input_serial(uint32_t &serial)
GHOST_TSuccess cursor_shape_custom_set(const GHOST_CursorGenerator &cg)
struct wp_color_manager_v1 * wp_color_manager_get()
std::atomic< bool > has_pending_actions_for_window
struct xdg_activation_v1 * xdg_activation_manager_get()
GHOST_WindowManager * getWindowManager() const
const GHOST_CSD_Params & getWindowCSD() const
const GHOST_CSD_Layout & getWindowCSD_Layout() const override
GHOST_TSuccess pushEvent(std::unique_ptr< const GHOST_IEvent > event)
void setWindowCSD_Layout(const GHOST_CSD_Layout &csd_layout)
GHOST_IWindow * getActiveWindow() const
const struct GWL_WindowScaleParams & scale_params_get() const
GHOST_TSuccess setClientSize(uint32_t width, uint32_t height) override
bool outputs_leave(GWL_Output *output)
GHOST_TSuccess setClientWidth(uint32_t width) override
wl_fixed_t wl_fixed_to_window(wl_fixed_t value) const
GHOST_TSuccess setOrder(GHOST_TWindowOrder order) override
GHOST_TSuccess setWindowCursorGrab(GHOST_TGrabCursorMode mode) override
GHOST_TSuccess hasCursorShape(GHOST_TStandardCursor cursor_shape) override
GHOST_TSuccess setState(GHOST_TWindowState state) override
GHOST_TSuccess setWindowCursorVisibility(bool visible) override
GHOST_WindowWayland(GHOST_SystemWayland *system, const char *title, int32_t left, int32_t top, uint32_t width, uint32_t height, GHOST_TWindowState state, const GHOST_IWindow *parent_window, GHOST_TDrawingContextType type, const bool is_dialog, const GHOST_ContextParams &context_params, const bool exclusive, const GHOST_GPUDevice &preferred_device)
GHOST_TWindowState getState() const override
bool getCursorGrabUseSoftwareDisplay() override
bool isDialog() const override
struct wl_surface * wl_surface_get() const
GHOST_TSuccess notify_decor_redraw()
bool getValid() const override
wl_fixed_t wl_fixed_from_window(wl_fixed_t value) const
std::string getTitle() const override
void getClientBounds(GHOST_Rect &bounds) const override
const std::span< GWL_Output *const > outputs_get() const
uint16_t getDPIHint() override
void clientToScreen(int32_t inX, int32_t inY, int32_t &outX, int32_t &outY) const override
GHOST_TSuccess swapBufferRelease() override
GHOST_TSuccess setClientHeight(uint32_t height) override
GHOST_TSuccess cursor_shape_refresh()
void screenToClient(int32_t inX, int32_t inY, int32_t &outX, int32_t &outY) const override
bool outputs_enter(GWL_Output *output)
GHOST_TSuccess setWindowCustomCursorShape(const uint8_t *bitmap, const uint8_t *mask, const int size[2], const int hot_spot[2], bool can_invert_color) override
GHOST_TSuccess invalidate() override
GWL_XDG_WindowIcon & gwl_xdg_icon_get()
GHOST_TSuccess setWindowCursorShape(GHOST_TStandardCursor shape) override
GHOST_TSuccess setWindowCustomCursorGenerator(GHOST_CursorGenerator *cursor_generator) override
void setTitle(const char *title) override
void getWindowBounds(GHOST_Rect &bounds) const override
GHOST_TWindowTiledFlag getTiledEdges() const override
GHOST_TSuccess getCursorBitmap(GHOST_CursorBitmapRef *bitmap) override
virtual GHOST_TSuccess getCursorGrabBounds(GHOST_Rect &bounds) const
GHOST_ContextParams want_context_params_
GHOST_TSuccess swapBufferRelease() override
GHOST_TStandardCursor cursor_shape_
GHOST_TAxisFlag cursor_grab_axis_
GHOST_TStandardCursor getCursorShape() const override
GHOST_TSuccess setDrawingContextType(GHOST_TDrawingContextType type) override
int32_t cursor_grab_init_pos_[2]
GHOST_TSuccess setSwapInterval(int interval) override
GHOST_TSuccess releaseNativeHandles()
bool getValid() const override
GHOST_Window(uint32_t width, uint32_t height, GHOST_TWindowState state, const GHOST_ContextParams &context_params, const bool exclusive=false)
GHOST_WindowHDRInfo hdr_info_
GHOST_TGrabCursorMode cursor_grab_
bool hasAlpha() const override
const int state
#define LOG(level)
Definition log.h:97
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
std::array< VecBase< T, 3 >, 8 > corners(const Bounds< VecBase< T, 3 > > &bounds)
int context(const bContext *C, const char *member, bContextDataResult *result)
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
GHOST_TCSD_Type buttons[5]
int32_t(*) layout_callback(const int32_t window_size[2], const int32_t fractional_scale[2], GHOST_TWindowState windowstate, const struct GHOST_CSD_Layout *csd_layout, GHOST_CSD_Elem *csd_elems)
void(*) free_fn(struct GHOST_CursorGenerator *cursor_generator)
GHOST_TWindowTiledFlag tiled_edges
wp_color_management_surface_v1 * color_management_surface
wp_viewport * viewport
wp_fractional_scale_v1 * fractional_scale_handle
xdg_activation_token_v1 * activation_token
GHOST_SystemWayland * ghost_system
std::atomic< bool > pending_actions[PENDING_NUM]
GWL_XDG_Decor_Window * xdg_decor
GWL_WindowScaleParams scale_params
struct GWL_Window::@042130253023266106323023200262152144123366214227 xdg
struct GWL_Window::@063046005070270072365331175015153324252156224275 wp
std::mutex frame_pending_mutex
struct GWL_Window::@301133305237256027042255364215230134332217040376 backend
GWL_WindowFrame frame_pending
GWL_WindowFrame frame
GWL_XDG_WindowIcon xdg_icon
struct GWL_Window::@136307353141236064335106064300222257023101227000 wl
GHOST_WindowWayland * ghost_window
GHOST_CursorGenerator * cursor_generator
std::vector< GWL_Output * > outputs
GHOST_TDrawingContextType ghost_context_type
zxdg_toplevel_decoration_v1 * toplevel_decor
struct GWL_XDG_Decor_Window::@016247100160304020072271262017253272164041351323 pending
enum zxdg_toplevel_decoration_v1_mode mode
struct wl_buffer * buffer
#define wl_display_dispatch_queue(...)
#define wl_display_dispatch_pending(...)
#define wl_display_dispatch(...)
#define wl_display_flush(...)
#define wl_proxy_set_queue(...)
#define wl_egl_window_resize(...)
#define wl_egl_window_create(...)
#define wl_egl_window_destroy(...)