1 /* This entire file is licensed under MIT
2  *
3  * Copyright 2020 William Wold
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6  *
7  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10  */
11 
12 #include "xdg-popup-surface.h"
13 
14 #include "custom-shell-surface.h"
15 #include "gtk-wayland.h"
16 #include "simple-conversions.h"
17 #include "gtk-priv-access.h"
18 
19 #include "xdg-shell-client.h"
20 
21 #include <gtk/gtk.h>
22 #include <gdk/gdk.h>
23 #include <gdk/gdkwayland.h>
24 
25 struct _XdgPopupSurface
26 {
27     CustomShellSurface super;
28 
29     XdgPopupPosition position;
30 
31     GdkRectangle cached_allocation;
32     GdkRectangle geom;
33 
34     // These can be NULL
35     struct xdg_surface *xdg_surface;
36     struct xdg_popup *xdg_popup;
37 };
38 
39 static void
xdg_surface_handle_configure(void * data,struct xdg_surface * _xdg_surface,uint32_t serial)40 xdg_surface_handle_configure (void *data,
41                               struct xdg_surface *_xdg_surface,
42                               uint32_t serial)
43 {
44     XdgPopupSurface *self = data;
45     (void)_xdg_surface;
46 
47     xdg_surface_ack_configure (self->xdg_surface, serial);
48 }
49 
50 static const struct xdg_surface_listener xdg_surface_listener = {
51     .configure = xdg_surface_handle_configure,
52 };
53 
54 static void
xdg_popup_handle_configure(void * data,struct xdg_popup * _xdg_popup,int32_t _x,int32_t _y,int32_t width,int32_t height)55 xdg_popup_handle_configure (void *data,
56                             struct xdg_popup *_xdg_popup,
57                             int32_t _x,
58                             int32_t _y,
59                             int32_t width,
60                             int32_t height)
61 {
62     XdgPopupSurface *self = data;
63     (void)_xdg_popup;
64     (void)_x;
65     (void)_y;
66 
67     g_return_if_fail(width >= 0 && height >= 0); // Protocol error
68 
69     // Technically this should not be applied until we get a xdg_surface.configure
70     GtkWindow *gtk_window = custom_shell_surface_get_gtk_window ((CustomShellSurface *)self);
71     gtk_window_resize (gtk_window, width, height);
72     GdkWindow *gdk_window = gtk_widget_get_window (GTK_WIDGET (gtk_window));
73     g_return_if_fail (gdk_window);
74     // calculating the correct values is hard, but we're not required to provide them
75     g_signal_emit_by_name (gdk_window, "moved-to-rect", NULL, NULL, FALSE, FALSE);
76 }
77 
78 static void
xdg_popup_handle_popup_done(void * data,struct xdg_popup * _xdg_popup)79 xdg_popup_handle_popup_done (void *data,
80                              struct xdg_popup *_xdg_popup)
81 {
82     (void)_xdg_popup;
83 
84     XdgPopupSurface *self = data;
85     GtkWindow *gtk_window = custom_shell_surface_get_gtk_window ((CustomShellSurface *)self);
86     gtk_widget_unmap (GTK_WIDGET (gtk_window));
87 }
88 
89 static const struct xdg_popup_listener xdg_popup_listener = {
90     .configure = xdg_popup_handle_configure,
91     .popup_done = xdg_popup_handle_popup_done,
92 };
93 
94 static void
xdg_popup_surface_get_anchor_rect(XdgPopupSurface * self,GdkRectangle * rect)95 xdg_popup_surface_get_anchor_rect (XdgPopupSurface *self, GdkRectangle *rect)
96 {
97     // The anchor rect is given relative to the actual top-left of the parent GDK window surface
98     // We need it realative to the logical geometry of the transient-for window, which may be sevel layers up
99     *rect = self->position.rect;
100     // It is a protocol error for size to be <= 0
101     rect->width = MAX (rect->width, 1);
102     rect->height = MAX (rect->height, 1);
103     GdkWindow *parent_window = self->position.transient_for_gdk_window;
104     CustomShellSurface *transient_for_shell_surface = self->position.transient_for_shell_surface;
105     GtkWidget *transient_for_widget = GTK_WIDGET (custom_shell_surface_get_gtk_window (transient_for_shell_surface));
106     GdkWindow *transient_for_window = gtk_widget_get_window (transient_for_widget);
107     g_return_if_fail (parent_window);
108     g_return_if_fail (transient_for_window);
109     // Traverse up to the transient-for window adding each window's position relative to it's parent along the way
110     while (parent_window && parent_window != transient_for_window) {
111         gint x, y;
112         gdk_window_get_position (parent_window, &x, &y);
113         rect->x += x;
114         rect->y += y;
115         parent_window = gdk_window_get_effective_parent (parent_window);
116     }
117     if (parent_window != transient_for_window) {
118         g_warning ("Could not find position of child window %p relative to parent window %p",
119                    (void *)self->position.transient_for_gdk_window,
120                    (void *)transient_for_window);
121     }
122     // Subtract the transient-for window's logical top-left
123     GdkRectangle transient_for_geom =
124         transient_for_shell_surface->virtual->get_logical_geom (transient_for_shell_surface);
125     rect->x -= transient_for_geom.x;
126     rect->y -= transient_for_geom.y;
127 }
128 
129 static void
xdg_popup_surface_maybe_grab(XdgPopupSurface * self,GdkWindow * gdk_window)130 xdg_popup_surface_maybe_grab (XdgPopupSurface *self, GdkWindow *gdk_window)
131 {
132     GdkSeat *grab_gdk_seat = gdk_window_get_priv_grab_seat (gdk_window);
133     if (!grab_gdk_seat) {
134         // If we really wanted a seat we could get the default one
135         // but grab_gdk_seat being null is an indication we should not grab
136         return;
137     }
138 
139     struct wl_seat *grab_wl_seat = gdk_wayland_seat_get_wl_seat (grab_gdk_seat);
140     if (!grab_wl_seat)
141         return; // unlikely
142 
143     uint32_t serial = gdk_window_get_priv_latest_serial (grab_gdk_seat);
144 
145     // serial might be 0, but the compositor might not care; YOLO
146     xdg_popup_grab(self->xdg_popup, grab_wl_seat, serial);
147 }
148 
149 static void
xdg_popup_surface_map(CustomShellSurface * super,struct wl_surface * wl_surface)150 xdg_popup_surface_map (CustomShellSurface *super, struct wl_surface *wl_surface)
151 {
152     XdgPopupSurface *self = (XdgPopupSurface *)super;
153 
154     g_return_if_fail (!self->xdg_popup);
155     g_return_if_fail (!self->xdg_surface);
156 
157     GtkWindow *gtk_window = custom_shell_surface_get_gtk_window (super);
158     GdkWindow *gdk_window = gtk_widget_get_window (GTK_WIDGET (gtk_window));
159     g_return_if_fail (gdk_window);
160     GdkRectangle rect;
161     xdg_popup_surface_get_anchor_rect (self, &rect);
162     struct xdg_wm_base *xdg_wm_base_global = gtk_wayland_get_xdg_wm_base_global ();
163     g_return_if_fail (xdg_wm_base_global);
164     struct xdg_positioner *positioner = xdg_wm_base_create_positioner (xdg_wm_base_global);
165     self->geom = gtk_wayland_get_logical_geom (gtk_window);
166     enum xdg_positioner_anchor anchor = gdk_gravity_get_xdg_positioner_anchor(self->position.rect_anchor);
167     enum xdg_positioner_gravity gravity = gdk_gravity_get_xdg_positioner_gravity(self->position.window_anchor);
168     enum xdg_positioner_constraint_adjustment constraint_adjustment =
169         gdk_anchor_hints_get_xdg_positioner_constraint_adjustment (self->position.anchor_hints);
170     xdg_positioner_set_size (positioner, self->geom.width, self->geom.height);
171     xdg_positioner_set_anchor_rect (positioner, rect.x, rect.y, rect.width, rect.height);
172     xdg_positioner_set_offset (positioner, self->position.rect_anchor_d.x, self->position.rect_anchor_d.y);
173     xdg_positioner_set_anchor (positioner, anchor);
174     xdg_positioner_set_gravity (positioner, gravity);
175     xdg_positioner_set_constraint_adjustment (positioner, constraint_adjustment);
176 
177     self->xdg_surface = xdg_wm_base_get_xdg_surface (xdg_wm_base_global, wl_surface);
178     g_return_if_fail (self->xdg_surface);
179     xdg_surface_add_listener (self->xdg_surface, &xdg_surface_listener, self);
180 
181     CustomShellSurface *transient_for_shell_surface = self->position.transient_for_shell_surface;
182     self->xdg_popup = transient_for_shell_surface->virtual->get_popup (transient_for_shell_surface,
183                                                                        self->xdg_surface,
184                                                                        positioner);
185     g_return_if_fail (self->xdg_popup);
186     xdg_popup_add_listener (self->xdg_popup, &xdg_popup_listener, self);
187 
188     xdg_positioner_destroy (positioner);
189 
190     xdg_popup_surface_maybe_grab (self, gdk_window);
191     xdg_surface_set_window_geometry (self->xdg_surface,
192                                      self->geom.x,
193                                      self->geom.y,
194                                      self->geom.width,
195                                      self->geom.height);
196 }
197 
198 static void
xdg_popup_surface_unmap(CustomShellSurface * super)199 xdg_popup_surface_unmap (CustomShellSurface *super)
200 {
201     XdgPopupSurface *self = (XdgPopupSurface *)super;
202 
203     if (self->xdg_popup) {
204         xdg_popup_destroy (self->xdg_popup);
205         self->xdg_popup = NULL;
206     }
207 
208     if (self->xdg_surface) {
209         xdg_surface_destroy (self->xdg_surface);
210         self->xdg_surface = NULL;
211     }
212 }
213 
214 static void
xdg_popup_surface_finalize(CustomShellSurface * super)215 xdg_popup_surface_finalize (CustomShellSurface *super)
216 {
217     xdg_popup_surface_unmap (super);
218 }
219 
220 static struct xdg_popup *
xdg_popup_surface_get_popup(CustomShellSurface * super,struct xdg_surface * popup_xdg_surface,struct xdg_positioner * positioner)221 xdg_popup_surface_get_popup (CustomShellSurface *super,
222                              struct xdg_surface *popup_xdg_surface,
223                              struct xdg_positioner *positioner)
224 {
225     XdgPopupSurface *self = (XdgPopupSurface *)super;
226 
227     if (!self->xdg_surface) {
228         g_critical ("xdg_popup_surface_get_popup () called when the xdg surface wayland object has not yet been created");
229         return NULL;
230     }
231 
232     return xdg_surface_get_popup (popup_xdg_surface, self->xdg_surface, positioner);
233 }
234 
235 static GdkRectangle
xdg_popup_surface_get_logical_geom(CustomShellSurface * super)236 xdg_popup_surface_get_logical_geom (CustomShellSurface *super)
237 {
238     XdgPopupSurface *self = (XdgPopupSurface *)super;
239     return self->geom;
240 }
241 
242 static const CustomShellSurfaceVirtual xdg_popup_surface_virtual = {
243     .map = xdg_popup_surface_map,
244     .unmap = xdg_popup_surface_unmap,
245     .finalize = xdg_popup_surface_finalize,
246     .get_popup = xdg_popup_surface_get_popup,
247     .get_logical_geom = xdg_popup_surface_get_logical_geom,
248 };
249 
250 static void
xdg_popup_surface_on_size_allocate(GtkWidget * _widget,GdkRectangle * allocation,XdgPopupSurface * self)251 xdg_popup_surface_on_size_allocate (GtkWidget *_widget,
252                                     GdkRectangle *allocation,
253                                     XdgPopupSurface *self)
254 {
255     (void)_widget;
256 
257     if (self->xdg_surface && !gdk_rectangle_equal (&self->cached_allocation, allocation)) {
258         self->cached_allocation = *allocation;
259         // allocation only used for catching duplicate calls. To get the correct geom we need to check something else
260         GtkWindow *gtk_window = custom_shell_surface_get_gtk_window ((CustomShellSurface *)self);
261         self->geom = gtk_wayland_get_logical_geom (gtk_window);
262         xdg_surface_set_window_geometry (self->xdg_surface,
263                                          self->geom.x,
264                                          self->geom.y,
265                                          self->geom.width,
266                                          self->geom.height);
267     }
268 }
269 
270 XdgPopupSurface *
xdg_popup_surface_new(GtkWindow * gtk_window,XdgPopupPosition const * position)271 xdg_popup_surface_new (GtkWindow *gtk_window, XdgPopupPosition const* position)
272 {
273     XdgPopupSurface *self = g_new0 (XdgPopupSurface, 1);
274     g_assert (gtk_window);
275     g_assert (position);
276     self->super.virtual = &xdg_popup_surface_virtual;
277     custom_shell_surface_init ((CustomShellSurface *)self, gtk_window);
278 
279     self->position = *position;
280     self->cached_allocation = (GdkRectangle) {
281         .x = 0,
282         .y = 0,
283         .width = 0,
284         .height = 0,
285     };
286     self->xdg_surface = NULL;
287     self->xdg_popup = NULL;
288 
289     g_signal_connect (gtk_window, "size-allocate", G_CALLBACK (xdg_popup_surface_on_size_allocate), self);
290 
291     return self;
292 }
293 
294 void
xdg_popup_surface_update_position(XdgPopupSurface * self,XdgPopupPosition const * position)295 xdg_popup_surface_update_position (XdgPopupSurface *self, XdgPopupPosition const* position)
296 {
297     self->position = *position;
298     // Don't bother trying to remap. It's not needed and breaks shit
299 }
300 
301 XdgPopupSurface *
custom_shell_surface_get_xdg_popup(CustomShellSurface * shell_surface)302 custom_shell_surface_get_xdg_popup (CustomShellSurface *shell_surface)
303 {
304     if (shell_surface && shell_surface->virtual == &xdg_popup_surface_virtual)
305         return (XdgPopupSurface *)shell_surface;
306     else
307         return NULL;
308 }
309