1 /*
2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
5 * Copyright © 2016 Quentin "Sardem FF7" Glidic
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27 #include "config.h"
28
29 #include <assert.h>
30
31 #include <wayland-server.h>
32
33 #include <libweston/libweston.h>
34 #include <libweston/zalloc.h>
35
36 #include <libweston-desktop/libweston-desktop.h>
37 #include "internal.h"
38
39 #define WD_WL_SHELL_PROTOCOL_VERSION 1
40
41 enum weston_desktop_wl_shell_surface_state {
42 NONE,
43 TOPLEVEL,
44 MAXIMIZED,
45 FULLSCREEN,
46 TRANSIENT,
47 POPUP,
48 };
49
50 struct weston_desktop_wl_shell_surface {
51 struct wl_resource *resource;
52 struct weston_desktop *desktop;
53 struct wl_display *display;
54 struct weston_desktop_surface *surface;
55 struct weston_desktop_surface *parent;
56 bool added;
57 struct weston_desktop_seat *popup_seat;
58 enum weston_desktop_wl_shell_surface_state state;
59 struct wl_listener wl_surface_resource_destroy_listener;
60 };
61
62 static void
weston_desktop_wl_shell_surface_set_size(struct weston_desktop_surface * dsurface,void * user_data,int32_t width,int32_t height)63 weston_desktop_wl_shell_surface_set_size(struct weston_desktop_surface *dsurface,
64 void *user_data,
65 int32_t width, int32_t height)
66 {
67 struct weston_desktop_wl_shell_surface *surface = user_data;
68 struct weston_surface *wsurface =
69 weston_desktop_surface_get_surface(surface->surface);
70
71 if ((wsurface->width == width && wsurface->height == height) ||
72 (width == 0 && height == 0))
73 return;
74
75 wl_shell_surface_send_configure(surface->resource,
76 WL_SHELL_SURFACE_RESIZE_NONE,
77 width, height);
78 }
79
80 static void
weston_desktop_wl_shell_surface_maybe_ungrab(struct weston_desktop_wl_shell_surface * surface)81 weston_desktop_wl_shell_surface_maybe_ungrab(struct weston_desktop_wl_shell_surface *surface)
82 {
83 if (surface->state != POPUP ||
84 !weston_desktop_surface_get_grab(surface->surface))
85 return;
86
87 weston_desktop_surface_popup_ungrab(surface->surface,
88 surface->popup_seat);
89 surface->popup_seat = NULL;
90 }
91
92 static void
weston_desktop_wl_shell_surface_committed(struct weston_desktop_surface * dsurface,void * user_data,int32_t sx,int32_t sy)93 weston_desktop_wl_shell_surface_committed(struct weston_desktop_surface *dsurface,
94 void *user_data,
95 int32_t sx, int32_t sy)
96 {
97 struct weston_desktop_wl_shell_surface *surface = user_data;
98 struct weston_surface *wsurface =
99 weston_desktop_surface_get_surface(dsurface);
100
101 if (wsurface->buffer_ref.buffer == NULL)
102 weston_desktop_wl_shell_surface_maybe_ungrab(surface);
103
104 if (surface->added)
105 weston_desktop_api_committed(surface->desktop, surface->surface,
106 sx, sy);
107 }
108
109 static void
weston_desktop_wl_shell_surface_ping(struct weston_desktop_surface * dsurface,uint32_t serial,void * user_data)110 weston_desktop_wl_shell_surface_ping(struct weston_desktop_surface *dsurface,
111 uint32_t serial, void *user_data)
112 {
113 struct weston_desktop_wl_shell_surface *surface = user_data;
114
115 wl_shell_surface_send_ping(surface->resource, serial);
116 }
117
118 static void
weston_desktop_wl_shell_surface_close(struct weston_desktop_surface * dsurface,void * user_data)119 weston_desktop_wl_shell_surface_close(struct weston_desktop_surface *dsurface,
120 void *user_data)
121 {
122 struct weston_desktop_wl_shell_surface *surface = user_data;
123
124 if (surface->state == POPUP)
125 wl_shell_surface_send_popup_done(surface->resource);
126 }
127
128 static bool
weston_desktop_wl_shell_surface_get_maximized(struct weston_desktop_surface * dsurface,void * user_data)129 weston_desktop_wl_shell_surface_get_maximized(struct weston_desktop_surface *dsurface,
130 void *user_data)
131 {
132 struct weston_desktop_wl_shell_surface *surface = user_data;
133
134 return surface->state == MAXIMIZED;
135 }
136
137 static bool
weston_desktop_wl_shell_surface_get_fullscreen(struct weston_desktop_surface * dsurface,void * user_data)138 weston_desktop_wl_shell_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
139 void *user_data)
140 {
141 struct weston_desktop_wl_shell_surface *surface = user_data;
142
143 return surface->state == FULLSCREEN;
144 }
145
146 static void
weston_desktop_wl_shell_change_state(struct weston_desktop_wl_shell_surface * surface,enum weston_desktop_wl_shell_surface_state state,struct weston_desktop_surface * parent,int32_t x,int32_t y)147 weston_desktop_wl_shell_change_state(struct weston_desktop_wl_shell_surface *surface,
148 enum weston_desktop_wl_shell_surface_state state,
149 struct weston_desktop_surface *parent,
150 int32_t x, int32_t y)
151 {
152 bool to_add = (parent == NULL);
153
154 assert(state != NONE);
155
156 if (to_add && surface->added) {
157 surface->state = state;
158 return;
159 }
160
161 if (surface->state != state) {
162 if (surface->state == POPUP)
163 weston_desktop_wl_shell_surface_maybe_ungrab(surface);
164
165 if (to_add) {
166 weston_desktop_surface_unset_relative_to(surface->surface);
167 weston_desktop_api_surface_added(surface->desktop,
168 surface->surface);
169 } else if (surface->added) {
170 weston_desktop_api_surface_removed(surface->desktop,
171 surface->surface);
172 }
173
174 surface->state = state;
175 surface->added = to_add;
176 }
177
178 if (parent != NULL)
179 weston_desktop_surface_set_relative_to(surface->surface, parent,
180 x, y, false);
181 }
182
183 static void
weston_desktop_wl_shell_surface_destroy(struct weston_desktop_surface * dsurface,void * user_data)184 weston_desktop_wl_shell_surface_destroy(struct weston_desktop_surface *dsurface,
185 void *user_data)
186 {
187 struct weston_desktop_wl_shell_surface *surface = user_data;
188
189 wl_list_remove(&surface->wl_surface_resource_destroy_listener.link);
190
191 weston_desktop_wl_shell_surface_maybe_ungrab(surface);
192 weston_desktop_surface_unset_relative_to(surface->surface);
193 if (surface->added)
194 weston_desktop_api_surface_removed(surface->desktop,
195 surface->surface);
196
197 free(surface);
198 }
199
200 static void
weston_desktop_wl_shell_surface_protocol_pong(struct wl_client * wl_client,struct wl_resource * resource,uint32_t serial)201 weston_desktop_wl_shell_surface_protocol_pong(struct wl_client *wl_client,
202 struct wl_resource *resource,
203 uint32_t serial)
204 {
205 struct weston_desktop_surface *surface = wl_resource_get_user_data(resource);
206
207 weston_desktop_client_pong(weston_desktop_surface_get_client(surface), serial);
208 }
209
210 static void
weston_desktop_wl_shell_surface_protocol_move(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial)211 weston_desktop_wl_shell_surface_protocol_move(struct wl_client *wl_client,
212 struct wl_resource *resource,
213 struct wl_resource *seat_resource,
214 uint32_t serial)
215 {
216 struct weston_desktop_surface *dsurface =
217 wl_resource_get_user_data(resource);
218 struct weston_seat *seat =
219 wl_resource_get_user_data(seat_resource);
220 struct weston_desktop_wl_shell_surface *surface =
221 weston_desktop_surface_get_implementation_data(dsurface);
222
223 if (seat == NULL)
224 return;
225
226 weston_desktop_api_move(surface->desktop, dsurface, seat, serial);
227 }
228
229 static void
weston_desktop_wl_shell_surface_protocol_resize(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial,enum wl_shell_surface_resize edges)230 weston_desktop_wl_shell_surface_protocol_resize(struct wl_client *wl_client,
231 struct wl_resource *resource,
232 struct wl_resource *seat_resource,
233 uint32_t serial,
234 enum wl_shell_surface_resize edges)
235 {
236 struct weston_desktop_surface *dsurface =
237 wl_resource_get_user_data(resource);
238 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
239 struct weston_desktop_wl_shell_surface *surface =
240 weston_desktop_surface_get_implementation_data(dsurface);
241 enum weston_desktop_surface_edge surf_edges =
242 (enum weston_desktop_surface_edge) edges;
243
244 if (seat == NULL)
245 return;
246
247 weston_desktop_api_resize(surface->desktop, dsurface, seat, serial, surf_edges);
248 }
249
250 static void
weston_desktop_wl_shell_surface_protocol_set_toplevel(struct wl_client * wl_client,struct wl_resource * resource)251 weston_desktop_wl_shell_surface_protocol_set_toplevel(struct wl_client *wl_client,
252 struct wl_resource *resource)
253 {
254 struct weston_desktop_surface *dsurface =
255 wl_resource_get_user_data(resource);
256 struct weston_desktop_wl_shell_surface *surface =
257 weston_desktop_surface_get_implementation_data(dsurface);
258
259 weston_desktop_wl_shell_change_state(surface, TOPLEVEL, NULL, 0, 0);
260 if (surface->parent == NULL)
261 return;
262 surface->parent = NULL;
263 weston_desktop_api_set_parent(surface->desktop, surface->surface, NULL);
264 }
265
266 static void
weston_desktop_wl_shell_surface_protocol_set_transient(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * parent_resource,int32_t x,int32_t y,enum wl_shell_surface_transient flags)267 weston_desktop_wl_shell_surface_protocol_set_transient(struct wl_client *wl_client,
268 struct wl_resource *resource,
269 struct wl_resource *parent_resource,
270 int32_t x, int32_t y,
271 enum wl_shell_surface_transient flags)
272 {
273 struct weston_desktop_surface *dsurface =
274 wl_resource_get_user_data(resource);
275 struct weston_surface *wparent =
276 wl_resource_get_user_data(parent_resource);
277 struct weston_desktop_surface *parent;
278 struct weston_desktop_wl_shell_surface *surface =
279 weston_desktop_surface_get_implementation_data(dsurface);
280
281 if (!weston_surface_is_desktop_surface(wparent))
282 return;
283
284 parent = weston_surface_get_desktop_surface(wparent);
285 if (flags & WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
286 weston_desktop_wl_shell_change_state(surface, TRANSIENT, parent,
287 x, y);
288 } else {
289 weston_desktop_wl_shell_change_state(surface, TOPLEVEL, NULL,
290 0, 0);
291 surface->parent = parent;
292 weston_desktop_api_set_parent(surface->desktop,
293 surface->surface, parent);
294 }
295 }
296
297 static void
weston_desktop_wl_shell_surface_protocol_set_fullscreen(struct wl_client * wl_client,struct wl_resource * resource,enum wl_shell_surface_fullscreen_method method,uint32_t framerate,struct wl_resource * output_resource)298 weston_desktop_wl_shell_surface_protocol_set_fullscreen(struct wl_client *wl_client,
299 struct wl_resource *resource,
300 enum wl_shell_surface_fullscreen_method method,
301 uint32_t framerate,
302 struct wl_resource *output_resource)
303 {
304 struct weston_desktop_surface *dsurface =
305 wl_resource_get_user_data(resource);
306 struct weston_desktop_wl_shell_surface *surface =
307 weston_desktop_surface_get_implementation_data(dsurface);
308 struct weston_output *output = NULL;
309
310 if (output_resource != NULL)
311 output = weston_head_from_resource(output_resource)->output;
312
313 weston_desktop_wl_shell_change_state(surface, FULLSCREEN, NULL, 0, 0);
314 weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
315 true, output);
316 }
317
318 static void
weston_desktop_wl_shell_surface_protocol_set_popup(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial,struct wl_resource * parent_resource,int32_t x,int32_t y,enum wl_shell_surface_transient flags)319 weston_desktop_wl_shell_surface_protocol_set_popup(struct wl_client *wl_client,
320 struct wl_resource *resource,
321 struct wl_resource *seat_resource,
322 uint32_t serial,
323 struct wl_resource *parent_resource,
324 int32_t x, int32_t y,
325 enum wl_shell_surface_transient flags)
326 {
327 struct weston_desktop_surface *dsurface =
328 wl_resource_get_user_data(resource);
329 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
330 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
331 struct weston_surface *parent =
332 wl_resource_get_user_data(parent_resource);
333 struct weston_desktop_surface *parent_surface;
334 struct weston_desktop_wl_shell_surface *surface =
335 weston_desktop_surface_get_implementation_data(dsurface);
336
337 /* Check that if we have a valid wseat we also got a valid desktop seat */
338 if (wseat != NULL && seat == NULL) {
339 wl_client_post_no_memory(wl_client);
340 return;
341 }
342
343 if (!weston_surface_is_desktop_surface(parent))
344 return;
345
346 parent_surface = weston_surface_get_desktop_surface(parent);
347
348 weston_desktop_wl_shell_change_state(surface, POPUP,
349 parent_surface, x, y);
350 weston_desktop_surface_popup_grab(surface->surface, seat, serial);
351 surface->popup_seat = seat;
352 }
353
354 static void
weston_desktop_wl_shell_surface_protocol_set_maximized(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * output_resource)355 weston_desktop_wl_shell_surface_protocol_set_maximized(struct wl_client *wl_client,
356 struct wl_resource *resource,
357 struct wl_resource *output_resource)
358 {
359 struct weston_desktop_surface *dsurface =
360 wl_resource_get_user_data(resource);
361 struct weston_desktop_wl_shell_surface *surface =
362 weston_desktop_surface_get_implementation_data(dsurface);
363
364 weston_desktop_wl_shell_change_state(surface, MAXIMIZED, NULL, 0, 0);
365 weston_desktop_api_maximized_requested(surface->desktop, dsurface, true);
366 }
367
368 static void
weston_desktop_wl_shell_surface_protocol_set_title(struct wl_client * wl_client,struct wl_resource * resource,const char * title)369 weston_desktop_wl_shell_surface_protocol_set_title(struct wl_client *wl_client,
370 struct wl_resource *resource,
371 const char *title)
372 {
373 struct weston_desktop_surface *surface =
374 wl_resource_get_user_data(resource);
375
376 weston_desktop_surface_set_title(surface, title);
377 }
378
379 static void
weston_desktop_wl_shell_surface_protocol_set_class(struct wl_client * wl_client,struct wl_resource * resource,const char * class_)380 weston_desktop_wl_shell_surface_protocol_set_class(struct wl_client *wl_client,
381 struct wl_resource *resource,
382 const char *class_)
383 {
384 struct weston_desktop_surface *surface =
385 wl_resource_get_user_data(resource);
386
387 weston_desktop_surface_set_app_id(surface, class_);
388 }
389
390
391 static const struct wl_shell_surface_interface weston_desktop_wl_shell_surface_implementation = {
392 .pong = weston_desktop_wl_shell_surface_protocol_pong,
393 .move = weston_desktop_wl_shell_surface_protocol_move,
394 .resize = weston_desktop_wl_shell_surface_protocol_resize,
395 .set_toplevel = weston_desktop_wl_shell_surface_protocol_set_toplevel,
396 .set_transient = weston_desktop_wl_shell_surface_protocol_set_transient,
397 .set_fullscreen = weston_desktop_wl_shell_surface_protocol_set_fullscreen,
398 .set_popup = weston_desktop_wl_shell_surface_protocol_set_popup,
399 .set_maximized = weston_desktop_wl_shell_surface_protocol_set_maximized,
400 .set_title = weston_desktop_wl_shell_surface_protocol_set_title,
401 .set_class = weston_desktop_wl_shell_surface_protocol_set_class,
402 };
403
404 static const struct weston_desktop_surface_implementation weston_desktop_wl_shell_surface_internal_implementation = {
405 .set_size = weston_desktop_wl_shell_surface_set_size,
406 .committed = weston_desktop_wl_shell_surface_committed,
407 .ping = weston_desktop_wl_shell_surface_ping,
408 .close = weston_desktop_wl_shell_surface_close,
409
410 .get_maximized = weston_desktop_wl_shell_surface_get_maximized,
411 .get_fullscreen = weston_desktop_wl_shell_surface_get_fullscreen,
412
413 .destroy = weston_desktop_wl_shell_surface_destroy,
414 };
415
416 static void
wl_surface_resource_destroyed(struct wl_listener * listener,void * data)417 wl_surface_resource_destroyed(struct wl_listener *listener,
418 void *data)
419 {
420 struct weston_desktop_wl_shell_surface *surface =
421 wl_container_of(listener, surface,
422 wl_surface_resource_destroy_listener);
423
424 /* the wl_shell_surface spec says that wl_shell_surfaces are to be
425 * destroyed automatically when the wl_surface is destroyed. */
426 weston_desktop_surface_destroy(surface->surface);
427 }
428
429 static void
weston_desktop_wl_shell_protocol_get_shell_surface(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id,struct wl_resource * surface_resource)430 weston_desktop_wl_shell_protocol_get_shell_surface(struct wl_client *wl_client,
431 struct wl_resource *resource,
432 uint32_t id,
433 struct wl_resource *surface_resource)
434 {
435 struct weston_desktop_client *client = wl_resource_get_user_data(resource);
436 struct weston_surface *wsurface = wl_resource_get_user_data(surface_resource);
437 struct weston_desktop_wl_shell_surface *surface;
438
439
440 if (weston_surface_set_role(wsurface, "wl_shell_surface", resource, WL_SHELL_ERROR_ROLE) < 0)
441 return;
442
443 surface = zalloc(sizeof(struct weston_desktop_wl_shell_surface));
444 if (surface == NULL) {
445 wl_client_post_no_memory(wl_client);
446 return;
447 }
448
449 surface->desktop = weston_desktop_client_get_desktop(client);
450 surface->display = weston_desktop_get_display(surface->desktop);
451
452 surface->surface =
453 weston_desktop_surface_create(surface->desktop, client, wsurface,
454 &weston_desktop_wl_shell_surface_internal_implementation,
455 surface);
456 if (surface->surface == NULL) {
457 free(surface);
458 return;
459 }
460
461 surface->wl_surface_resource_destroy_listener.notify =
462 wl_surface_resource_destroyed;
463 wl_resource_add_destroy_listener(wsurface->resource,
464 &surface->wl_surface_resource_destroy_listener);
465
466 surface->resource =
467 weston_desktop_surface_add_resource(surface->surface,
468 &wl_shell_surface_interface,
469 &weston_desktop_wl_shell_surface_implementation,
470 id, NULL);
471 }
472
473
474 static const struct wl_shell_interface weston_desktop_wl_shell_implementation = {
475 .get_shell_surface = weston_desktop_wl_shell_protocol_get_shell_surface,
476 };
477
478 static void
weston_desktop_wl_shell_bind(struct wl_client * client,void * data,uint32_t version,uint32_t id)479 weston_desktop_wl_shell_bind(struct wl_client *client, void *data,
480 uint32_t version, uint32_t id)
481 {
482 struct weston_desktop *desktop = data;
483
484 weston_desktop_client_create(desktop, client, NULL, &wl_shell_interface,
485 &weston_desktop_wl_shell_implementation,
486 version, id);
487 }
488
489 struct wl_global *
weston_desktop_wl_shell_create(struct weston_desktop * desktop,struct wl_display * display)490 weston_desktop_wl_shell_create(struct weston_desktop *desktop,
491 struct wl_display *display)
492 {
493 return wl_global_create(display,
494 &wl_shell_interface,
495 WD_WL_SHELL_PROTOCOL_VERSION, desktop,
496 weston_desktop_wl_shell_bind);
497 }
498