diff options
Diffstat (limited to 'sway/desktop/xdg_shell.c')
-rw-r--r-- | sway/desktop/xdg_shell.c | 277 |
1 files changed, 277 insertions, 0 deletions
diff --git a/sway/desktop/xdg_shell.c b/sway/desktop/xdg_shell.c new file mode 100644 index 00000000..48b659f2 --- /dev/null +++ b/sway/desktop/xdg_shell.c | |||
@@ -0,0 +1,277 @@ | |||
1 | #define _POSIX_C_SOURCE 199309L | ||
2 | #include <stdbool.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <wayland-server.h> | ||
5 | #include <wlr/types/wlr_xdg_shell.h> | ||
6 | #include "sway/tree/container.h" | ||
7 | #include "sway/tree/layout.h" | ||
8 | #include "sway/server.h" | ||
9 | #include "sway/tree/view.h" | ||
10 | #include "sway/input/seat.h" | ||
11 | #include "sway/input/input-manager.h" | ||
12 | #include "log.h" | ||
13 | |||
14 | static const struct sway_view_child_impl popup_impl; | ||
15 | |||
16 | static void popup_destroy(struct sway_view_child *child) { | ||
17 | if (!sway_assert(child->impl == &popup_impl, | ||
18 | "Expected an xdg_shell popup")) { | ||
19 | return; | ||
20 | } | ||
21 | struct sway_xdg_popup *popup = (struct sway_xdg_popup *)child; | ||
22 | wl_list_remove(&popup->new_popup.link); | ||
23 | wl_list_remove(&popup->destroy.link); | ||
24 | free(popup); | ||
25 | } | ||
26 | |||
27 | static const struct sway_view_child_impl popup_impl = { | ||
28 | .destroy = popup_destroy, | ||
29 | }; | ||
30 | |||
31 | static struct sway_xdg_popup *popup_create( | ||
32 | struct wlr_xdg_popup *wlr_popup, struct sway_view *view); | ||
33 | |||
34 | static void popup_handle_new_popup(struct wl_listener *listener, void *data) { | ||
35 | struct sway_xdg_popup *popup = | ||
36 | wl_container_of(listener, popup, new_popup); | ||
37 | struct wlr_xdg_popup *wlr_popup = data; | ||
38 | popup_create(wlr_popup, popup->child.view); | ||
39 | } | ||
40 | |||
41 | static void popup_handle_destroy(struct wl_listener *listener, void *data) { | ||
42 | struct sway_xdg_popup *popup = wl_container_of(listener, popup, destroy); | ||
43 | view_child_destroy(&popup->child); | ||
44 | } | ||
45 | |||
46 | static struct sway_xdg_popup *popup_create( | ||
47 | struct wlr_xdg_popup *wlr_popup, struct sway_view *view) { | ||
48 | struct wlr_xdg_surface *xdg_surface = wlr_popup->base; | ||
49 | |||
50 | struct sway_xdg_popup *popup = | ||
51 | calloc(1, sizeof(struct sway_xdg_popup)); | ||
52 | if (popup == NULL) { | ||
53 | return NULL; | ||
54 | } | ||
55 | view_child_init(&popup->child, &popup_impl, view, xdg_surface->surface); | ||
56 | |||
57 | wl_signal_add(&xdg_surface->events.new_popup, &popup->new_popup); | ||
58 | popup->new_popup.notify = popup_handle_new_popup; | ||
59 | wl_signal_add(&xdg_surface->events.destroy, &popup->destroy); | ||
60 | popup->destroy.notify = popup_handle_destroy; | ||
61 | |||
62 | return popup; | ||
63 | } | ||
64 | |||
65 | |||
66 | static struct sway_xdg_shell_view *xdg_shell_view_from_view( | ||
67 | struct sway_view *view) { | ||
68 | if (!sway_assert(view->type == SWAY_VIEW_XDG_SHELL, | ||
69 | "Expected xdg_shell view")) { | ||
70 | return NULL; | ||
71 | } | ||
72 | return (struct sway_xdg_shell_view *)view; | ||
73 | } | ||
74 | |||
75 | static const char *get_prop(struct sway_view *view, enum sway_view_prop prop) { | ||
76 | if (xdg_shell_view_from_view(view) == NULL) { | ||
77 | return NULL; | ||
78 | } | ||
79 | switch (prop) { | ||
80 | case VIEW_PROP_TITLE: | ||
81 | return view->wlr_xdg_surface->toplevel->title; | ||
82 | case VIEW_PROP_APP_ID: | ||
83 | return view->wlr_xdg_surface->toplevel->app_id; | ||
84 | default: | ||
85 | return NULL; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | static void configure(struct sway_view *view, double ox, double oy, int width, | ||
90 | int height) { | ||
91 | struct sway_xdg_shell_view *xdg_shell_view = | ||
92 | xdg_shell_view_from_view(view); | ||
93 | if (xdg_shell_view == NULL) { | ||
94 | return; | ||
95 | } | ||
96 | |||
97 | xdg_shell_view->pending_width = width; | ||
98 | xdg_shell_view->pending_height = height; | ||
99 | wlr_xdg_toplevel_set_size(view->wlr_xdg_surface, width, height); | ||
100 | } | ||
101 | |||
102 | static void set_activated(struct sway_view *view, bool activated) { | ||
103 | if (xdg_shell_view_from_view(view) == NULL) { | ||
104 | return; | ||
105 | } | ||
106 | struct wlr_xdg_surface *surface = view->wlr_xdg_surface; | ||
107 | if (surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) { | ||
108 | wlr_xdg_toplevel_set_activated(surface, activated); | ||
109 | } | ||
110 | } | ||
111 | |||
112 | static void set_fullscreen(struct sway_view *view, bool fullscreen) { | ||
113 | if (xdg_shell_view_from_view(view) == NULL) { | ||
114 | return; | ||
115 | } | ||
116 | struct wlr_xdg_surface *surface = view->wlr_xdg_surface; | ||
117 | wlr_xdg_toplevel_set_fullscreen(surface, fullscreen); | ||
118 | } | ||
119 | |||
120 | static void for_each_surface(struct sway_view *view, | ||
121 | wlr_surface_iterator_func_t iterator, void *user_data) { | ||
122 | if (xdg_shell_view_from_view(view) == NULL) { | ||
123 | return; | ||
124 | } | ||
125 | wlr_xdg_surface_for_each_surface(view->wlr_xdg_surface, iterator, | ||
126 | user_data); | ||
127 | } | ||
128 | |||
129 | static void _close(struct sway_view *view) { | ||
130 | if (xdg_shell_view_from_view(view) == NULL) { | ||
131 | return; | ||
132 | } | ||
133 | struct wlr_xdg_surface *surface = view->wlr_xdg_surface; | ||
134 | if (surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL) { | ||
135 | wlr_xdg_surface_send_close(surface); | ||
136 | } | ||
137 | } | ||
138 | |||
139 | static void destroy(struct sway_view *view) { | ||
140 | struct sway_xdg_shell_view *xdg_shell_view = | ||
141 | xdg_shell_view_from_view(view); | ||
142 | if (xdg_shell_view == NULL) { | ||
143 | return; | ||
144 | } | ||
145 | wl_list_remove(&xdg_shell_view->destroy.link); | ||
146 | wl_list_remove(&xdg_shell_view->map.link); | ||
147 | wl_list_remove(&xdg_shell_view->unmap.link); | ||
148 | wl_list_remove(&xdg_shell_view->request_fullscreen.link); | ||
149 | free(xdg_shell_view); | ||
150 | } | ||
151 | |||
152 | static const struct sway_view_impl view_impl = { | ||
153 | .get_prop = get_prop, | ||
154 | .configure = configure, | ||
155 | .set_activated = set_activated, | ||
156 | .set_fullscreen = set_fullscreen, | ||
157 | .for_each_surface = for_each_surface, | ||
158 | .close = _close, | ||
159 | .destroy = destroy, | ||
160 | }; | ||
161 | |||
162 | static void handle_commit(struct wl_listener *listener, void *data) { | ||
163 | struct sway_xdg_shell_view *xdg_shell_view = | ||
164 | wl_container_of(listener, xdg_shell_view, commit); | ||
165 | struct sway_view *view = &xdg_shell_view->view; | ||
166 | // NOTE: We intentionally discard the view's desired width here | ||
167 | // TODO: Store this for restoration when moving to floating plane | ||
168 | // TODO: Let floating views do whatever | ||
169 | view_update_size(view, xdg_shell_view->pending_width, | ||
170 | xdg_shell_view->pending_height); | ||
171 | view_update_title(view, false); | ||
172 | view_damage_from(view); | ||
173 | } | ||
174 | |||
175 | static void handle_new_popup(struct wl_listener *listener, void *data) { | ||
176 | struct sway_xdg_shell_view *xdg_shell_view = | ||
177 | wl_container_of(listener, xdg_shell_view, new_popup); | ||
178 | struct wlr_xdg_popup *wlr_popup = data; | ||
179 | popup_create(wlr_popup, &xdg_shell_view->view); | ||
180 | } | ||
181 | |||
182 | static void handle_unmap(struct wl_listener *listener, void *data) { | ||
183 | struct sway_xdg_shell_view *xdg_shell_view = | ||
184 | wl_container_of(listener, xdg_shell_view, unmap); | ||
185 | |||
186 | view_unmap(&xdg_shell_view->view); | ||
187 | |||
188 | wl_list_remove(&xdg_shell_view->commit.link); | ||
189 | wl_list_remove(&xdg_shell_view->new_popup.link); | ||
190 | } | ||
191 | |||
192 | static void handle_map(struct wl_listener *listener, void *data) { | ||
193 | struct sway_xdg_shell_view *xdg_shell_view = | ||
194 | wl_container_of(listener, xdg_shell_view, map); | ||
195 | struct sway_view *view = &xdg_shell_view->view; | ||
196 | struct wlr_xdg_surface *xdg_surface = view->wlr_xdg_surface; | ||
197 | |||
198 | view_map(view, view->wlr_xdg_surface->surface); | ||
199 | |||
200 | xdg_shell_view->commit.notify = handle_commit; | ||
201 | wl_signal_add(&xdg_surface->surface->events.commit, | ||
202 | &xdg_shell_view->commit); | ||
203 | |||
204 | xdg_shell_view->new_popup.notify = handle_new_popup; | ||
205 | wl_signal_add(&xdg_surface->events.new_popup, | ||
206 | &xdg_shell_view->new_popup); | ||
207 | |||
208 | if (xdg_surface->toplevel->client_pending.fullscreen) { | ||
209 | view_set_fullscreen(view, true); | ||
210 | } | ||
211 | } | ||
212 | |||
213 | static void handle_destroy(struct wl_listener *listener, void *data) { | ||
214 | struct sway_xdg_shell_view *xdg_shell_view = | ||
215 | wl_container_of(listener, xdg_shell_view, destroy); | ||
216 | view_destroy(&xdg_shell_view->view); | ||
217 | } | ||
218 | |||
219 | static void handle_request_fullscreen(struct wl_listener *listener, void *data) { | ||
220 | struct sway_xdg_shell_view *xdg_shell_view = | ||
221 | wl_container_of(listener, xdg_shell_view, request_fullscreen); | ||
222 | struct wlr_xdg_toplevel_set_fullscreen_event *e = data; | ||
223 | struct wlr_xdg_surface *xdg_surface = | ||
224 | xdg_shell_view->view.wlr_xdg_surface; | ||
225 | |||
226 | if (!sway_assert(xdg_surface->role == WLR_XDG_SURFACE_ROLE_TOPLEVEL, | ||
227 | "xdg_shell requested fullscreen of surface with role %i", | ||
228 | xdg_surface->role)) { | ||
229 | return; | ||
230 | } | ||
231 | if (!xdg_surface->mapped) { | ||
232 | return; | ||
233 | } | ||
234 | |||
235 | view_set_fullscreen(&xdg_shell_view->view, e->fullscreen); | ||
236 | } | ||
237 | |||
238 | void handle_xdg_shell_surface(struct wl_listener *listener, void *data) { | ||
239 | struct sway_server *server = wl_container_of(listener, server, | ||
240 | xdg_shell_surface); | ||
241 | struct wlr_xdg_surface *xdg_surface = data; | ||
242 | |||
243 | if (xdg_surface->role == WLR_XDG_SURFACE_ROLE_POPUP) { | ||
244 | wlr_log(L_DEBUG, "New xdg_shell popup"); | ||
245 | return; | ||
246 | } | ||
247 | |||
248 | wlr_log(L_DEBUG, "New xdg_shell toplevel title='%s' app_id='%s'", | ||
249 | xdg_surface->toplevel->title, xdg_surface->toplevel->app_id); | ||
250 | wlr_xdg_surface_ping(xdg_surface); | ||
251 | wlr_xdg_toplevel_set_maximized(xdg_surface, true); | ||
252 | |||
253 | struct sway_xdg_shell_view *xdg_shell_view = | ||
254 | calloc(1, sizeof(struct sway_xdg_shell_view)); | ||
255 | if (!sway_assert(xdg_shell_view, "Failed to allocate view")) { | ||
256 | return; | ||
257 | } | ||
258 | |||
259 | view_init(&xdg_shell_view->view, SWAY_VIEW_XDG_SHELL, &view_impl); | ||
260 | xdg_shell_view->view.wlr_xdg_surface = xdg_surface; | ||
261 | |||
262 | // TODO: | ||
263 | // - Look up pid and open on appropriate workspace | ||
264 | |||
265 | xdg_shell_view->map.notify = handle_map; | ||
266 | wl_signal_add(&xdg_surface->events.map, &xdg_shell_view->map); | ||
267 | |||
268 | xdg_shell_view->unmap.notify = handle_unmap; | ||
269 | wl_signal_add(&xdg_surface->events.unmap, &xdg_shell_view->unmap); | ||
270 | |||
271 | xdg_shell_view->destroy.notify = handle_destroy; | ||
272 | wl_signal_add(&xdg_surface->events.destroy, &xdg_shell_view->destroy); | ||
273 | |||
274 | xdg_shell_view->request_fullscreen.notify = handle_request_fullscreen; | ||
275 | wl_signal_add(&xdg_surface->toplevel->events.request_fullscreen, | ||
276 | &xdg_shell_view->request_fullscreen); | ||
277 | } | ||