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 "custom-shell-surface.h"
13 #include "gtk-wayland.h"
14 #include "gtk-priv-access.h"
15
16 #include <gtk/gtk.h>
17 #include <gdk/gdk.h>
18 #include <gdk/gdkwayland.h>
19
20 static const char *custom_shell_surface_key = "wayland_custom_shell_surface";
21
22 struct _CustomShellSurfacePrivate
23 {
24 GtkWindow *gtk_window;
25 };
26
27 static void
custom_shell_surface_on_window_destroy(CustomShellSurface * self)28 custom_shell_surface_on_window_destroy (CustomShellSurface *self)
29 {
30 self->virtual->finalize (self);
31 g_free (self->private);
32 g_free (self);
33 }
34
35 static void
custom_shell_surface_on_window_realize(GtkWidget * widget,CustomShellSurface * self)36 custom_shell_surface_on_window_realize (GtkWidget *widget, CustomShellSurface *self)
37 {
38 g_return_if_fail (GTK_WIDGET (self->private->gtk_window) == widget);
39
40 GdkWindow *gdk_window = gtk_widget_get_window (GTK_WIDGET (self->private->gtk_window));
41 g_return_if_fail (gdk_window);
42
43 gtk_priv_access_init (gdk_window);
44 gdk_wayland_window_set_use_custom_surface (gdk_window);
45 }
46
47 static void
custom_shell_surface_on_window_map(GtkWidget * widget,CustomShellSurface * self)48 custom_shell_surface_on_window_map (GtkWidget *widget, CustomShellSurface *self)
49 {
50 g_return_if_fail (GTK_WIDGET (self->private->gtk_window) == widget);
51
52 GdkWindow *gdk_window = gtk_widget_get_window (GTK_WIDGET (self->private->gtk_window));
53 g_return_if_fail (gdk_window);
54
55 struct wl_surface *wl_surface = gdk_wayland_window_get_wl_surface (gdk_window);
56 g_return_if_fail (wl_surface);
57
58 // In some cases (observed when a mate panel has an image background) GDK will attach a buffer just after creating
59 // the surface (see the implementation of gdk_wayland_window_show() for details). Giving the surface a role with a
60 // buffer attached is a protocol violation, so we attach a null buffer. GDK hasn't commited the buffer it may have
61 // attached, so we don't need to commit. If this is removed, test-window-with-initially-attached-buffer should fail.
62 wl_surface_attach (wl_surface, NULL, 0, 0);
63
64 self->virtual->map (self, wl_surface);
65 gdk_window_set_priv_mapped (gdk_window);
66
67 wl_surface_commit (wl_surface);
68 wl_display_roundtrip (gdk_wayland_display_get_wl_display (gdk_display_get_default ()));
69 }
70
71 void
custom_shell_surface_init(CustomShellSurface * self,GtkWindow * gtk_window)72 custom_shell_surface_init (CustomShellSurface *self, GtkWindow *gtk_window)
73 {
74 g_assert (self->virtual); // Subclass should have set this up first
75
76 self->private = g_new0 (CustomShellSurfacePrivate, 1);
77 self->private->gtk_window = gtk_window;
78
79 g_return_if_fail (gtk_window);
80 g_return_if_fail (!gtk_widget_get_mapped (GTK_WIDGET (gtk_window)));
81 g_object_set_data_full (G_OBJECT (gtk_window),
82 custom_shell_surface_key,
83 self,
84 (GDestroyNotify) custom_shell_surface_on_window_destroy);
85 g_signal_connect (gtk_window, "realize", G_CALLBACK (custom_shell_surface_on_window_realize), self);
86 g_signal_connect (gtk_window, "map", G_CALLBACK (custom_shell_surface_on_window_map), self);
87
88 if (gtk_widget_get_realized (GTK_WIDGET (gtk_window))) {
89 // We must be in the process of realizing now
90 custom_shell_surface_on_window_realize (GTK_WIDGET (gtk_window), self);
91 }
92 }
93
94 CustomShellSurface *
gtk_window_get_custom_shell_surface(GtkWindow * gtk_window)95 gtk_window_get_custom_shell_surface (GtkWindow *gtk_window)
96 {
97 if (!gtk_window)
98 return NULL;
99
100 return g_object_get_data (G_OBJECT (gtk_window), custom_shell_surface_key);
101 }
102
103 GtkWindow *
custom_shell_surface_get_gtk_window(CustomShellSurface * self)104 custom_shell_surface_get_gtk_window (CustomShellSurface *self)
105 {
106 g_return_val_if_fail (self, NULL);
107 return self->private->gtk_window;
108 }
109
110 void
custom_shell_surface_get_window_geom(CustomShellSurface * self,GdkRectangle * geom)111 custom_shell_surface_get_window_geom (CustomShellSurface *self, GdkRectangle *geom)
112 {
113 g_return_if_fail (self);
114 // TODO: Store the actual window geometry used
115 *geom = gtk_wayland_get_logical_geom (self->private->gtk_window);
116 }
117
118 void
custom_shell_surface_needs_commit(CustomShellSurface * self)119 custom_shell_surface_needs_commit (CustomShellSurface *self)
120 {
121 if (!self->private->gtk_window)
122 return;
123
124 GdkWindow *gdk_window = gtk_widget_get_window (GTK_WIDGET (self->private->gtk_window));
125
126 if (!gdk_window)
127 return;
128
129 // Hopefully this will trigger a commit
130 // Don't commit directly, as that screws up GTK's internal state
131 // (see https://github.com/wmww/gtk-layer-shell/issues/51)
132 gdk_window_invalidate_rect (gdk_window, NULL, FALSE);
133 }
134
135 void
custom_shell_surface_remap(CustomShellSurface * self)136 custom_shell_surface_remap (CustomShellSurface *self)
137 {
138 GtkWidget *window_widget = GTK_WIDGET (self->private->gtk_window);
139 g_return_if_fail (window_widget);
140 gtk_widget_hide (window_widget);
141 gtk_widget_show (window_widget);
142 }
143