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 <stdbool.h>
30 #include <assert.h>
31
32 #include <wayland-server.h>
33
34 #include <libweston/libweston.h>
35 #include <libweston/zalloc.h>
36 #include "xdg-shell-server-protocol.h"
37
38 #include <libweston-desktop/libweston-desktop.h>
39 #include "internal.h"
40
41 /************************************************************************************
42 * WARNING: This file implements the stable xdg shell protocol.
43 * Any changes to this file may also need to be added to the xdg-shell-v6.c file which
44 * implements the older unstable xdg shell v6 protocol.
45 ************************************************************************************/
46
47 #define WD_XDG_SHELL_PROTOCOL_VERSION 1
48
49 static const char *weston_desktop_xdg_toplevel_role = "xdg_toplevel";
50 static const char *weston_desktop_xdg_popup_role = "xdg_popup";
51
52 struct weston_desktop_xdg_positioner {
53 struct weston_desktop *desktop;
54 struct weston_desktop_client *client;
55 struct wl_resource *resource;
56
57 struct weston_size size;
58 struct weston_geometry anchor_rect;
59 enum xdg_positioner_anchor anchor;
60 enum xdg_positioner_gravity gravity;
61 enum xdg_positioner_constraint_adjustment constraint_adjustment;
62 struct weston_position offset;
63 };
64
65 enum weston_desktop_xdg_surface_role {
66 WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE,
67 WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL,
68 WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP,
69 };
70
71 struct weston_desktop_xdg_surface {
72 struct wl_resource *resource;
73 struct weston_desktop *desktop;
74 struct weston_surface *surface;
75 struct weston_desktop_surface *desktop_surface;
76 bool configured;
77 struct wl_event_source *configure_idle;
78 struct wl_list configure_list; /* weston_desktop_xdg_surface_configure::link */
79
80 bool has_next_geometry;
81 struct weston_geometry next_geometry;
82
83 enum weston_desktop_xdg_surface_role role;
84 };
85
86 struct weston_desktop_xdg_surface_configure {
87 struct wl_list link; /* weston_desktop_xdg_surface::configure_list */
88 uint32_t serial;
89 };
90
91 struct weston_desktop_xdg_toplevel_state {
92 bool maximized;
93 bool fullscreen;
94 bool resizing;
95 bool activated;
96 };
97
98 struct weston_desktop_xdg_toplevel_configure {
99 struct weston_desktop_xdg_surface_configure base;
100 struct weston_desktop_xdg_toplevel_state state;
101 struct weston_size size;
102 };
103
104 struct weston_desktop_xdg_toplevel {
105 struct weston_desktop_xdg_surface base;
106
107 struct wl_resource *resource;
108 bool added;
109 struct {
110 struct weston_desktop_xdg_toplevel_state state;
111 struct weston_size size;
112 } pending;
113 struct {
114 struct weston_desktop_xdg_toplevel_state state;
115 struct weston_size size;
116 struct weston_size min_size, max_size;
117 } next;
118 struct {
119 struct weston_desktop_xdg_toplevel_state state;
120 struct weston_size min_size, max_size;
121 } current;
122 };
123
124 struct weston_desktop_xdg_popup {
125 struct weston_desktop_xdg_surface base;
126
127 struct wl_resource *resource;
128 bool committed;
129 struct weston_desktop_xdg_surface *parent;
130 struct weston_desktop_seat *seat;
131 struct weston_geometry geometry;
132 };
133
134 #define weston_desktop_surface_role_biggest_size (sizeof(struct weston_desktop_xdg_toplevel))
135 #define weston_desktop_surface_configure_biggest_size (sizeof(struct weston_desktop_xdg_toplevel))
136
137
138 static struct weston_geometry
weston_desktop_xdg_positioner_get_geometry(struct weston_desktop_xdg_positioner * positioner,struct weston_desktop_surface * dsurface,struct weston_desktop_surface * parent)139 weston_desktop_xdg_positioner_get_geometry(struct weston_desktop_xdg_positioner *positioner,
140 struct weston_desktop_surface *dsurface,
141 struct weston_desktop_surface *parent)
142 {
143 struct weston_geometry geometry = {
144 .x = positioner->offset.x,
145 .y = positioner->offset.y,
146 .width = positioner->size.width,
147 .height = positioner->size.height,
148 };
149
150 switch (positioner->anchor) {
151 case XDG_POSITIONER_ANCHOR_TOP:
152 case XDG_POSITIONER_ANCHOR_TOP_LEFT:
153 case XDG_POSITIONER_ANCHOR_TOP_RIGHT:
154 geometry.y += positioner->anchor_rect.y;
155 break;
156 case XDG_POSITIONER_ANCHOR_BOTTOM:
157 case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT:
158 case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT:
159 geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height;
160 break;
161 default:
162 geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height / 2;
163 }
164
165 switch (positioner->anchor) {
166 case XDG_POSITIONER_ANCHOR_LEFT:
167 case XDG_POSITIONER_ANCHOR_TOP_LEFT:
168 case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT:
169 geometry.x += positioner->anchor_rect.x;
170 break;
171 case XDG_POSITIONER_ANCHOR_RIGHT:
172 case XDG_POSITIONER_ANCHOR_TOP_RIGHT:
173 case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT:
174 geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width;
175 break;
176 default:
177 geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width / 2;
178 }
179
180 switch (positioner->gravity) {
181 case XDG_POSITIONER_GRAVITY_TOP:
182 case XDG_POSITIONER_GRAVITY_TOP_LEFT:
183 case XDG_POSITIONER_GRAVITY_TOP_RIGHT:
184 geometry.y -= geometry.height;
185 break;
186 case XDG_POSITIONER_GRAVITY_BOTTOM:
187 case XDG_POSITIONER_GRAVITY_BOTTOM_LEFT:
188 case XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT:
189 geometry.y = geometry.y;
190 break;
191 default:
192 geometry.y -= geometry.height / 2;
193 }
194
195 switch (positioner->gravity) {
196 case XDG_POSITIONER_GRAVITY_LEFT:
197 case XDG_POSITIONER_GRAVITY_TOP_LEFT:
198 case XDG_POSITIONER_GRAVITY_BOTTOM_LEFT:
199 geometry.x -= geometry.width;
200 break;
201 case XDG_POSITIONER_GRAVITY_RIGHT:
202 case XDG_POSITIONER_GRAVITY_TOP_RIGHT:
203 case XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT:
204 geometry.x = geometry.x;
205 break;
206 default:
207 geometry.x -= geometry.width / 2;
208 }
209
210 if (positioner->constraint_adjustment == XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
211 return geometry;
212
213 /* TODO: add compositor policy configuration and the code here */
214
215 return geometry;
216 }
217
218 static void
weston_desktop_xdg_positioner_protocol_set_size(struct wl_client * wl_client,struct wl_resource * resource,int32_t width,int32_t height)219 weston_desktop_xdg_positioner_protocol_set_size(struct wl_client *wl_client,
220 struct wl_resource *resource,
221 int32_t width, int32_t height)
222 {
223 struct weston_desktop_xdg_positioner *positioner =
224 wl_resource_get_user_data(resource);
225
226 if (width < 1 || height < 1) {
227 wl_resource_post_error(resource,
228 XDG_POSITIONER_ERROR_INVALID_INPUT,
229 "width and height must be positives and non-zero");
230 return;
231 }
232
233 positioner->size.width = width;
234 positioner->size.height = height;
235 }
236
237 static void
weston_desktop_xdg_positioner_protocol_set_anchor_rect(struct wl_client * wl_client,struct wl_resource * resource,int32_t x,int32_t y,int32_t width,int32_t height)238 weston_desktop_xdg_positioner_protocol_set_anchor_rect(struct wl_client *wl_client,
239 struct wl_resource *resource,
240 int32_t x, int32_t y,
241 int32_t width, int32_t height)
242 {
243 struct weston_desktop_xdg_positioner *positioner =
244 wl_resource_get_user_data(resource);
245
246 if (width < 0 || height < 0) {
247 wl_resource_post_error(resource,
248 XDG_POSITIONER_ERROR_INVALID_INPUT,
249 "width and height must be non-negative");
250 return;
251 }
252
253 positioner->anchor_rect.x = x;
254 positioner->anchor_rect.y = y;
255 positioner->anchor_rect.width = width;
256 positioner->anchor_rect.height = height;
257 }
258
259 static void
weston_desktop_xdg_positioner_protocol_set_anchor(struct wl_client * wl_client,struct wl_resource * resource,enum xdg_positioner_anchor anchor)260 weston_desktop_xdg_positioner_protocol_set_anchor(struct wl_client *wl_client,
261 struct wl_resource *resource,
262 enum xdg_positioner_anchor anchor)
263 {
264 struct weston_desktop_xdg_positioner *positioner =
265 wl_resource_get_user_data(resource);
266
267 positioner->anchor = anchor;
268 }
269
270 static void
weston_desktop_xdg_positioner_protocol_set_gravity(struct wl_client * wl_client,struct wl_resource * resource,enum xdg_positioner_gravity gravity)271 weston_desktop_xdg_positioner_protocol_set_gravity(struct wl_client *wl_client,
272 struct wl_resource *resource,
273 enum xdg_positioner_gravity gravity)
274 {
275 struct weston_desktop_xdg_positioner *positioner =
276 wl_resource_get_user_data(resource);
277
278 positioner->gravity = gravity;
279 }
280
281 static void
weston_desktop_xdg_positioner_protocol_set_constraint_adjustment(struct wl_client * wl_client,struct wl_resource * resource,enum xdg_positioner_constraint_adjustment constraint_adjustment)282 weston_desktop_xdg_positioner_protocol_set_constraint_adjustment(struct wl_client *wl_client,
283 struct wl_resource *resource,
284 enum xdg_positioner_constraint_adjustment constraint_adjustment)
285 {
286 struct weston_desktop_xdg_positioner *positioner =
287 wl_resource_get_user_data(resource);
288
289 positioner->constraint_adjustment = constraint_adjustment;
290 }
291
292 static void
weston_desktop_xdg_positioner_protocol_set_offset(struct wl_client * wl_client,struct wl_resource * resource,int32_t x,int32_t y)293 weston_desktop_xdg_positioner_protocol_set_offset(struct wl_client *wl_client,
294 struct wl_resource *resource,
295 int32_t x, int32_t y)
296 {
297 struct weston_desktop_xdg_positioner *positioner =
298 wl_resource_get_user_data(resource);
299
300 positioner->offset.x = x;
301 positioner->offset.y = y;
302 }
303
304 static void
weston_desktop_xdg_positioner_destroy(struct wl_resource * resource)305 weston_desktop_xdg_positioner_destroy(struct wl_resource *resource)
306 {
307 struct weston_desktop_xdg_positioner *positioner =
308 wl_resource_get_user_data(resource);
309
310 free(positioner);
311 }
312
313 static const struct xdg_positioner_interface weston_desktop_xdg_positioner_implementation = {
314 .destroy = weston_desktop_destroy_request,
315 .set_size = weston_desktop_xdg_positioner_protocol_set_size,
316 .set_anchor_rect = weston_desktop_xdg_positioner_protocol_set_anchor_rect,
317 .set_anchor = weston_desktop_xdg_positioner_protocol_set_anchor,
318 .set_gravity = weston_desktop_xdg_positioner_protocol_set_gravity,
319 .set_constraint_adjustment = weston_desktop_xdg_positioner_protocol_set_constraint_adjustment,
320 .set_offset = weston_desktop_xdg_positioner_protocol_set_offset,
321 };
322
323 static void
324 weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface);
325
326 static void
weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel * toplevel)327 weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel *toplevel)
328 {
329 if (toplevel->added)
330 return;
331
332 weston_desktop_api_surface_added(toplevel->base.desktop,
333 toplevel->base.desktop_surface);
334 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
335 toplevel->added = true;
336 }
337
338 static void
weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * parent_resource)339 weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client,
340 struct wl_resource *resource,
341 struct wl_resource *parent_resource)
342 {
343 struct weston_desktop_surface *dsurface =
344 wl_resource_get_user_data(resource);
345 struct weston_desktop_xdg_toplevel *toplevel =
346 weston_desktop_surface_get_implementation_data(dsurface);
347 struct weston_desktop_surface *parent = NULL;
348
349 if (parent_resource != NULL)
350 parent = wl_resource_get_user_data(parent_resource);
351
352 weston_desktop_xdg_toplevel_ensure_added(toplevel);
353 weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent);
354 }
355
356 static void
weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client * wl_client,struct wl_resource * resource,const char * title)357 weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client,
358 struct wl_resource *resource,
359 const char *title)
360 {
361 struct weston_desktop_surface *toplevel =
362 wl_resource_get_user_data(resource);
363
364 weston_desktop_surface_set_title(toplevel, title);
365 }
366
367 static void
weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client * wl_client,struct wl_resource * resource,const char * app_id)368 weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client,
369 struct wl_resource *resource,
370 const char *app_id)
371 {
372 struct weston_desktop_surface *toplevel =
373 wl_resource_get_user_data(resource);
374
375 weston_desktop_surface_set_app_id(toplevel, app_id);
376 }
377
378 static void
weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial,int32_t x,int32_t y)379 weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client,
380 struct wl_resource *resource,
381 struct wl_resource *seat_resource,
382 uint32_t serial,
383 int32_t x, int32_t y)
384 {
385 struct weston_desktop_surface *dsurface =
386 wl_resource_get_user_data(resource);
387 struct weston_seat *seat =
388 wl_resource_get_user_data(seat_resource);
389 struct weston_desktop_xdg_toplevel *toplevel =
390 weston_desktop_surface_get_implementation_data(dsurface);
391
392 if (!toplevel->base.configured) {
393 wl_resource_post_error(toplevel->resource,
394 XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
395 "Surface has not been configured yet");
396 return;
397 }
398
399 if (seat == NULL)
400 return;
401
402 weston_desktop_api_show_window_menu(toplevel->base.desktop,
403 dsurface, seat, x, y);
404 }
405
406 static void
weston_desktop_xdg_toplevel_protocol_move(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial)407 weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client,
408 struct wl_resource *resource,
409 struct wl_resource *seat_resource,
410 uint32_t serial)
411 {
412 struct weston_desktop_surface *dsurface =
413 wl_resource_get_user_data(resource);
414 struct weston_seat *seat =
415 wl_resource_get_user_data(seat_resource);
416 struct weston_desktop_xdg_toplevel *toplevel =
417 weston_desktop_surface_get_implementation_data(dsurface);
418
419 if (!toplevel->base.configured) {
420 wl_resource_post_error(toplevel->resource,
421 XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
422 "Surface has not been configured yet");
423 return;
424 }
425
426 if (seat == NULL)
427 return;
428
429 weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial);
430 }
431
432 static void
weston_desktop_xdg_toplevel_protocol_resize(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial,enum xdg_toplevel_resize_edge edges)433 weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client,
434 struct wl_resource *resource,
435 struct wl_resource *seat_resource,
436 uint32_t serial,
437 enum xdg_toplevel_resize_edge edges)
438 {
439 struct weston_desktop_surface *dsurface =
440 wl_resource_get_user_data(resource);
441 struct weston_seat *seat =
442 wl_resource_get_user_data(seat_resource);
443 struct weston_desktop_xdg_toplevel *toplevel =
444 weston_desktop_surface_get_implementation_data(dsurface);
445 enum weston_desktop_surface_edge surf_edges =
446 (enum weston_desktop_surface_edge) edges;
447
448 if (!toplevel->base.configured) {
449 wl_resource_post_error(toplevel->resource,
450 XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
451 "Surface has not been configured yet");
452 return;
453 }
454
455 if (seat == NULL)
456 return;
457
458 weston_desktop_api_resize(toplevel->base.desktop,
459 dsurface, seat, serial, surf_edges);
460 }
461
462 static void
weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel * toplevel,struct weston_desktop_xdg_toplevel_configure * configure)463 weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel,
464 struct weston_desktop_xdg_toplevel_configure *configure)
465 {
466 toplevel->next.state = configure->state;
467 toplevel->next.size = configure->size;
468 }
469
470 static void
weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client * wl_client,struct wl_resource * resource,int32_t width,int32_t height)471 weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client,
472 struct wl_resource *resource,
473 int32_t width, int32_t height)
474 {
475 struct weston_desktop_surface *dsurface =
476 wl_resource_get_user_data(resource);
477 struct weston_desktop_xdg_toplevel *toplevel =
478 weston_desktop_surface_get_implementation_data(dsurface);
479
480 toplevel->next.min_size.width = width;
481 toplevel->next.min_size.height = height;
482 }
483
484 static void
weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client * wl_client,struct wl_resource * resource,int32_t width,int32_t height)485 weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client,
486 struct wl_resource *resource,
487 int32_t width, int32_t height)
488 {
489 struct weston_desktop_surface *dsurface =
490 wl_resource_get_user_data(resource);
491 struct weston_desktop_xdg_toplevel *toplevel =
492 weston_desktop_surface_get_implementation_data(dsurface);
493
494 toplevel->next.max_size.width = width;
495 toplevel->next.max_size.height = height;
496 }
497
498 static void
weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client * wl_client,struct wl_resource * resource)499 weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client,
500 struct wl_resource *resource)
501 {
502 struct weston_desktop_surface *dsurface =
503 wl_resource_get_user_data(resource);
504 struct weston_desktop_xdg_toplevel *toplevel =
505 weston_desktop_surface_get_implementation_data(dsurface);
506
507 weston_desktop_xdg_toplevel_ensure_added(toplevel);
508 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true);
509 }
510
511 static void
weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client * wl_client,struct wl_resource * resource)512 weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client,
513 struct wl_resource *resource)
514 {
515 struct weston_desktop_surface *dsurface =
516 wl_resource_get_user_data(resource);
517 struct weston_desktop_xdg_toplevel *toplevel =
518 weston_desktop_surface_get_implementation_data(dsurface);
519
520 weston_desktop_xdg_toplevel_ensure_added(toplevel);
521 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false);
522 }
523
524 static void
weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * output_resource)525 weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client,
526 struct wl_resource *resource,
527 struct wl_resource *output_resource)
528 {
529 struct weston_desktop_surface *dsurface =
530 wl_resource_get_user_data(resource);
531 struct weston_desktop_xdg_toplevel *toplevel =
532 weston_desktop_surface_get_implementation_data(dsurface);
533 struct weston_output *output = NULL;
534
535 if (output_resource != NULL)
536 output = weston_head_from_resource(output_resource)->output;
537
538 weston_desktop_xdg_toplevel_ensure_added(toplevel);
539 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
540 true, output);
541 }
542
543 static void
weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client * wl_client,struct wl_resource * resource)544 weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client,
545 struct wl_resource *resource)
546 {
547 struct weston_desktop_surface *dsurface =
548 wl_resource_get_user_data(resource);
549 struct weston_desktop_xdg_toplevel *toplevel =
550 weston_desktop_surface_get_implementation_data(dsurface);
551
552 weston_desktop_xdg_toplevel_ensure_added(toplevel);
553 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
554 false, NULL);
555 }
556
557 static void
weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client * wl_client,struct wl_resource * resource)558 weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client,
559 struct wl_resource *resource)
560 {
561 struct weston_desktop_surface *dsurface =
562 wl_resource_get_user_data(resource);
563 struct weston_desktop_xdg_toplevel *toplevel =
564 weston_desktop_surface_get_implementation_data(dsurface);
565
566 weston_desktop_xdg_toplevel_ensure_added(toplevel);
567 weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface);
568 }
569
570 static void
weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel * toplevel,struct weston_desktop_xdg_toplevel_configure * configure)571 weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel,
572 struct weston_desktop_xdg_toplevel_configure *configure)
573 {
574 uint32_t *s;
575 struct wl_array states;
576
577 configure->state = toplevel->pending.state;
578 configure->size = toplevel->pending.size;
579
580 wl_array_init(&states);
581 if (toplevel->pending.state.maximized) {
582 s = wl_array_add(&states, sizeof(uint32_t));
583 *s = XDG_TOPLEVEL_STATE_MAXIMIZED;
584 }
585 if (toplevel->pending.state.fullscreen) {
586 s = wl_array_add(&states, sizeof(uint32_t));
587 *s = XDG_TOPLEVEL_STATE_FULLSCREEN;
588 }
589 if (toplevel->pending.state.resizing) {
590 s = wl_array_add(&states, sizeof(uint32_t));
591 *s = XDG_TOPLEVEL_STATE_RESIZING;
592 }
593 if (toplevel->pending.state.activated) {
594 s = wl_array_add(&states, sizeof(uint32_t));
595 *s = XDG_TOPLEVEL_STATE_ACTIVATED;
596 }
597
598 xdg_toplevel_send_configure(toplevel->resource,
599 toplevel->pending.size.width,
600 toplevel->pending.size.height,
601 &states);
602
603 wl_array_release(&states);
604 };
605
606 static void
weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface * dsurface,void * user_data,bool maximized)607 weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface,
608 void *user_data, bool maximized)
609 {
610 struct weston_desktop_xdg_toplevel *toplevel = user_data;
611
612 toplevel->pending.state.maximized = maximized;
613 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
614 }
615
616 static void
weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface * dsurface,void * user_data,bool fullscreen)617 weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface,
618 void *user_data, bool fullscreen)
619 {
620 struct weston_desktop_xdg_toplevel *toplevel = user_data;
621
622 toplevel->pending.state.fullscreen = fullscreen;
623 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
624 }
625
626 static void
weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface * dsurface,void * user_data,bool resizing)627 weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface,
628 void *user_data, bool resizing)
629 {
630 struct weston_desktop_xdg_toplevel *toplevel = user_data;
631
632 toplevel->pending.state.resizing = resizing;
633 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
634 }
635
636 static void
weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface * dsurface,void * user_data,bool activated)637 weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface,
638 void *user_data, bool activated)
639 {
640 struct weston_desktop_xdg_toplevel *toplevel = user_data;
641
642 toplevel->pending.state.activated = activated;
643 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
644 }
645
646 static void
weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface * dsurface,void * user_data,int32_t width,int32_t height)647 weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface,
648 void *user_data,
649 int32_t width, int32_t height)
650 {
651 struct weston_desktop_xdg_toplevel *toplevel = user_data;
652
653 toplevel->pending.size.width = width;
654 toplevel->pending.size.height = height;
655
656 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
657 }
658
659 static void
weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel * toplevel,int32_t sx,int32_t sy)660 weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel,
661 int32_t sx, int32_t sy)
662 {
663 struct weston_surface *wsurface =
664 weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
665
666 if (!wsurface->buffer_ref.buffer && !toplevel->added) {
667 weston_desktop_xdg_toplevel_ensure_added(toplevel);
668 return;
669 }
670 if (!wsurface->buffer_ref.buffer)
671 return;
672
673 struct weston_geometry geometry =
674 weston_desktop_surface_get_geometry(toplevel->base.desktop_surface);
675
676 if ((toplevel->next.state.maximized || toplevel->next.state.fullscreen) &&
677 (toplevel->next.size.width != geometry.width ||
678 toplevel->next.size.height != geometry.height)) {
679 struct weston_desktop_client *client =
680 weston_desktop_surface_get_client(toplevel->base.desktop_surface);
681 struct wl_resource *client_resource =
682 weston_desktop_client_get_resource(client);
683
684 wl_resource_post_error(client_resource,
685 XDG_WM_BASE_ERROR_INVALID_SURFACE_STATE,
686 "xdg_surface buffer does not match the configured state");
687 return;
688 }
689
690 toplevel->current.state = toplevel->next.state;
691 toplevel->current.min_size = toplevel->next.min_size;
692 toplevel->current.max_size = toplevel->next.max_size;
693
694 weston_desktop_api_committed(toplevel->base.desktop,
695 toplevel->base.desktop_surface,
696 sx, sy);
697 }
698
699 static void
weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel * toplevel)700 weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel)
701 {
702 xdg_toplevel_send_close(toplevel->resource);
703 }
704
705 static bool
weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface * dsurface,void * user_data)706 weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface,
707 void *user_data)
708 {
709 struct weston_desktop_xdg_toplevel *toplevel = user_data;
710
711 return toplevel->current.state.maximized;
712 }
713
714 static bool
weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface * dsurface,void * user_data)715 weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface,
716 void *user_data)
717 {
718 struct weston_desktop_xdg_toplevel *toplevel = user_data;
719
720 return toplevel->current.state.fullscreen;
721 }
722
723 static bool
weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface * dsurface,void * user_data)724 weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface,
725 void *user_data)
726 {
727 struct weston_desktop_xdg_toplevel *toplevel = user_data;
728
729 return toplevel->current.state.resizing;
730 }
731
732 static bool
weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface * dsurface,void * user_data)733 weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface,
734 void *user_data)
735 {
736 struct weston_desktop_xdg_toplevel *toplevel = user_data;
737
738 return toplevel->current.state.activated;
739 }
740
741 static void
weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel * toplevel)742 weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel)
743 {
744 if (toplevel->added)
745 weston_desktop_api_surface_removed(toplevel->base.desktop,
746 toplevel->base.desktop_surface);
747 }
748
749 static void
weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource * resource)750 weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource)
751 {
752 struct weston_desktop_surface *dsurface =
753 wl_resource_get_user_data(resource);
754
755 if (dsurface != NULL)
756 weston_desktop_surface_resource_destroy(resource);
757 }
758
759 static const struct xdg_toplevel_interface weston_desktop_xdg_toplevel_implementation = {
760 .destroy = weston_desktop_destroy_request,
761 .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent,
762 .set_title = weston_desktop_xdg_toplevel_protocol_set_title,
763 .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id,
764 .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu,
765 .move = weston_desktop_xdg_toplevel_protocol_move,
766 .resize = weston_desktop_xdg_toplevel_protocol_resize,
767 .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size,
768 .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size,
769 .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized,
770 .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized,
771 .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen,
772 .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen,
773 .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized,
774 };
775
776 static void
weston_desktop_xdg_popup_protocol_grab(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial)777 weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client,
778 struct wl_resource *resource,
779 struct wl_resource *seat_resource,
780 uint32_t serial)
781 {
782 struct weston_desktop_surface *dsurface =
783 wl_resource_get_user_data(resource);
784 struct weston_desktop_xdg_popup *popup =
785 weston_desktop_surface_get_implementation_data(dsurface);
786 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
787 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
788 struct weston_desktop_surface *topmost;
789 bool parent_is_toplevel =
790 popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
791
792 /* Check that if we have a valid wseat we also got a valid desktop seat */
793 if (wseat != NULL && seat == NULL) {
794 wl_client_post_no_memory(wl_client);
795 return;
796 }
797
798 if (popup->committed) {
799 wl_resource_post_error(popup->resource,
800 XDG_POPUP_ERROR_INVALID_GRAB,
801 "xdg_popup already is mapped");
802 return;
803 }
804
805 /* If seat is NULL then get_topmost_surface will return NULL. In
806 * combination with setting parent_is_toplevel to TRUE here we will
807 * avoid posting an error, and we will instead gracefully fail the
808 * grab and dismiss the surface.
809 * FIXME: this is a hack because currently we cannot check the topmost
810 * parent with a destroyed weston_seat */
811 if (seat == NULL)
812 parent_is_toplevel = true;
813
814 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
815 if ((topmost == NULL && !parent_is_toplevel) ||
816 (topmost != NULL && topmost != popup->parent->desktop_surface)) {
817 struct weston_desktop_client *client =
818 weston_desktop_surface_get_client(dsurface);
819 struct wl_resource *client_resource =
820 weston_desktop_client_get_resource(client);
821
822 wl_resource_post_error(client_resource,
823 XDG_WM_BASE_ERROR_NOT_THE_TOPMOST_POPUP,
824 "xdg_popup was not created on the topmost popup");
825 return;
826 }
827
828 popup->seat = seat;
829 weston_desktop_surface_popup_grab(popup->base.desktop_surface,
830 popup->seat, serial);
831 }
832
833 static void
weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup * popup)834 weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup)
835 {
836 xdg_popup_send_configure(popup->resource,
837 popup->geometry.x,
838 popup->geometry.y,
839 popup->geometry.width,
840 popup->geometry.height);
841 }
842
843 static void
844 weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
845 void *user_data);
846
847 static void
weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup * popup)848 weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup)
849 {
850 struct weston_surface *wsurface =
851 weston_desktop_surface_get_surface (popup->base.desktop_surface);
852 struct weston_view *view;
853
854 wl_list_for_each(view, &wsurface->views, surface_link)
855 weston_view_update_transform(view);
856
857 if (!popup->committed)
858 weston_desktop_xdg_surface_schedule_configure(&popup->base);
859 popup->committed = true;
860 weston_desktop_xdg_popup_update_position(popup->base.desktop_surface,
861 popup);
862 }
863
864 static void
weston_desktop_xdg_popup_update_position(struct weston_desktop_surface * dsurface,void * user_data)865 weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
866 void *user_data)
867 {
868 }
869
870 static void
weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup * popup)871 weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup)
872 {
873 xdg_popup_send_popup_done(popup->resource);
874 }
875
876 static void
weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup * popup)877 weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup)
878 {
879 struct weston_desktop_surface *topmost;
880 struct weston_desktop_client *client =
881 weston_desktop_surface_get_client(popup->base.desktop_surface);
882
883 if (!weston_desktop_surface_get_grab(popup->base.desktop_surface))
884 return;
885
886 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
887 if (topmost != popup->base.desktop_surface) {
888 struct wl_resource *client_resource =
889 weston_desktop_client_get_resource(client);
890
891 wl_resource_post_error(client_resource,
892 XDG_WM_BASE_ERROR_NOT_THE_TOPMOST_POPUP,
893 "xdg_popup was destroyed while it was not the topmost popup.");
894 }
895
896 weston_desktop_surface_popup_ungrab(popup->base.desktop_surface,
897 popup->seat);
898 }
899
900 static void
weston_desktop_xdg_popup_resource_destroy(struct wl_resource * resource)901 weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource)
902 {
903 struct weston_desktop_surface *dsurface =
904 wl_resource_get_user_data(resource);
905
906 if (dsurface != NULL)
907 weston_desktop_surface_resource_destroy(resource);
908 }
909
910 static const struct xdg_popup_interface weston_desktop_xdg_popup_implementation = {
911 .destroy = weston_desktop_destroy_request,
912 .grab = weston_desktop_xdg_popup_protocol_grab,
913 };
914
915 static void
weston_desktop_xdg_surface_send_configure(void * user_data)916 weston_desktop_xdg_surface_send_configure(void *user_data)
917 {
918 struct weston_desktop_xdg_surface *surface = user_data;
919 struct weston_desktop_xdg_surface_configure *configure;
920
921 surface->configure_idle = NULL;
922
923 configure = zalloc(weston_desktop_surface_configure_biggest_size);
924 if (configure == NULL) {
925 struct weston_desktop_client *client =
926 weston_desktop_surface_get_client(surface->desktop_surface);
927 struct wl_client *wl_client =
928 weston_desktop_client_get_client(client);
929 wl_client_post_no_memory(wl_client);
930 return;
931 }
932 wl_list_insert(surface->configure_list.prev, &configure->link);
933 configure->serial =
934 wl_display_next_serial(weston_desktop_get_display(surface->desktop));
935
936 switch (surface->role) {
937 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
938 assert(0 && "not reached");
939 break;
940 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
941 weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface,
942 (struct weston_desktop_xdg_toplevel_configure *) configure);
943 break;
944 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
945 weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface);
946 break;
947 }
948
949 xdg_surface_send_configure(surface->resource, configure->serial);
950 }
951
952 static bool
weston_desktop_xdg_toplevel_state_compare(struct weston_desktop_xdg_toplevel * toplevel)953 weston_desktop_xdg_toplevel_state_compare(struct weston_desktop_xdg_toplevel *toplevel)
954 {
955 struct {
956 struct weston_desktop_xdg_toplevel_state state;
957 struct weston_size size;
958 } configured;
959
960 if (!toplevel->base.configured)
961 return false;
962
963 if (wl_list_empty(&toplevel->base.configure_list)) {
964 /* Last configure is actually the current state, just use it */
965 configured.state = toplevel->current.state;
966 configured.size.width = toplevel->base.surface->width;
967 configured.size.height = toplevel->base.surface->height;
968 } else {
969 struct weston_desktop_xdg_toplevel_configure *configure =
970 wl_container_of(toplevel->base.configure_list.prev,
971 configure, base.link);
972
973 configured.state = configure->state;
974 configured.size = configure->size;
975 }
976
977 if (toplevel->pending.state.activated != configured.state.activated)
978 return false;
979 if (toplevel->pending.state.fullscreen != configured.state.fullscreen)
980 return false;
981 if (toplevel->pending.state.maximized != configured.state.maximized)
982 return false;
983 if (toplevel->pending.state.resizing != configured.state.resizing)
984 return false;
985
986 if (toplevel->pending.size.width == configured.size.width &&
987 toplevel->pending.size.height == configured.size.height)
988 return true;
989
990 if (toplevel->pending.size.width == 0 &&
991 toplevel->pending.size.height == 0)
992 return true;
993
994 return false;
995 }
996
997 static void
weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface * surface)998 weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface)
999 {
1000 struct wl_display *display = weston_desktop_get_display(surface->desktop);
1001 struct wl_event_loop *loop = wl_display_get_event_loop(display);
1002 bool pending_same = false;
1003
1004 switch (surface->role) {
1005 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1006 assert(0 && "not reached");
1007 break;
1008 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1009 pending_same = weston_desktop_xdg_toplevel_state_compare((struct weston_desktop_xdg_toplevel *) surface);
1010 break;
1011 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1012 break;
1013 }
1014
1015 if (surface->configure_idle != NULL) {
1016 if (!pending_same)
1017 return;
1018
1019 wl_event_source_remove(surface->configure_idle);
1020 surface->configure_idle = NULL;
1021 } else {
1022 if (pending_same)
1023 return;
1024
1025 surface->configure_idle =
1026 wl_event_loop_add_idle(loop,
1027 weston_desktop_xdg_surface_send_configure,
1028 surface);
1029 }
1030 }
1031
1032 static void
weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id)1033 weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client,
1034 struct wl_resource *resource,
1035 uint32_t id)
1036 {
1037 struct weston_desktop_surface *dsurface =
1038 wl_resource_get_user_data(resource);
1039 struct weston_surface *wsurface =
1040 weston_desktop_surface_get_surface(dsurface);
1041 struct weston_desktop_xdg_toplevel *toplevel =
1042 weston_desktop_surface_get_implementation_data(dsurface);
1043
1044 if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role,
1045 resource, XDG_WM_BASE_ERROR_ROLE) < 0)
1046 return;
1047
1048 toplevel->resource =
1049 weston_desktop_surface_add_resource(toplevel->base.desktop_surface,
1050 &xdg_toplevel_interface,
1051 &weston_desktop_xdg_toplevel_implementation,
1052 id, weston_desktop_xdg_toplevel_resource_destroy);
1053 if (toplevel->resource == NULL)
1054 return;
1055
1056 toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
1057 }
1058
1059 static void
weston_desktop_xdg_surface_protocol_get_popup(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id,struct wl_resource * parent_resource,struct wl_resource * positioner_resource)1060 weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client,
1061 struct wl_resource *resource,
1062 uint32_t id,
1063 struct wl_resource *parent_resource,
1064 struct wl_resource *positioner_resource)
1065 {
1066 struct weston_desktop_surface *dsurface =
1067 wl_resource_get_user_data(resource);
1068 struct weston_surface *wsurface =
1069 weston_desktop_surface_get_surface(dsurface);
1070 struct weston_desktop_xdg_popup *popup =
1071 weston_desktop_surface_get_implementation_data(dsurface);
1072 struct weston_desktop_surface *parent_surface;
1073 struct weston_desktop_xdg_surface *parent;
1074 struct weston_desktop_xdg_positioner *positioner =
1075 wl_resource_get_user_data(positioner_resource);
1076
1077 /* Popup parents are allowed to be non-null, but only if a parent is
1078 * specified 'using some other protocol' before committing. Since we
1079 * don't support such a protocol yet, clients cannot legitimately
1080 * create a popup with a non-null parent. */
1081 if (!parent_resource) {
1082 wl_resource_post_error(resource,
1083 XDG_WM_BASE_ERROR_INVALID_POPUP_PARENT,
1084 "popup parent must be non-null");
1085 return;
1086 }
1087
1088 parent_surface = wl_resource_get_user_data(parent_resource);
1089 parent = weston_desktop_surface_get_implementation_data(parent_surface);
1090
1091 /* Checking whether the size and anchor rect both have a positive size
1092 * is enough to verify both have been correctly set */
1093 if (positioner->size.width == 0 || positioner->anchor_rect.width == 0 ||
1094 positioner->anchor_rect.height == 0) {
1095 wl_resource_post_error(resource,
1096 XDG_WM_BASE_ERROR_INVALID_POSITIONER,
1097 "positioner object is not complete");
1098 return;
1099 }
1100
1101 if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
1102 resource, XDG_WM_BASE_ERROR_ROLE) < 0)
1103 return;
1104
1105 popup->resource =
1106 weston_desktop_surface_add_resource(popup->base.desktop_surface,
1107 &xdg_popup_interface,
1108 &weston_desktop_xdg_popup_implementation,
1109 id, weston_desktop_xdg_popup_resource_destroy);
1110 if (popup->resource == NULL)
1111 return;
1112
1113 popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
1114 popup->parent = parent;
1115
1116 popup->geometry =
1117 weston_desktop_xdg_positioner_get_geometry(positioner,
1118 dsurface,
1119 parent_surface);
1120
1121 weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
1122 parent_surface,
1123 popup->geometry.x,
1124 popup->geometry.y,
1125 true);
1126 }
1127
1128 static bool
weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface * surface)1129 weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
1130 {
1131 struct weston_surface *wsurface =
1132 weston_desktop_surface_get_surface(surface->desktop_surface);
1133 const char *role;
1134
1135 role = weston_surface_get_role(wsurface);
1136 if (role != NULL &&
1137 (role == weston_desktop_xdg_toplevel_role ||
1138 role == weston_desktop_xdg_popup_role))
1139 return true;
1140
1141 wl_resource_post_error(surface->resource,
1142 XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
1143 "xdg_surface must have a role");
1144 return false;
1145 }
1146
1147 static void
weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client * wl_client,struct wl_resource * resource,int32_t x,int32_t y,int32_t width,int32_t height)1148 weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
1149 struct wl_resource *resource,
1150 int32_t x, int32_t y,
1151 int32_t width, int32_t height)
1152 {
1153 struct weston_desktop_surface *dsurface =
1154 wl_resource_get_user_data(resource);
1155 struct weston_desktop_xdg_surface *surface =
1156 weston_desktop_surface_get_implementation_data(dsurface);
1157
1158 if (!weston_desktop_xdg_surface_check_role(surface))
1159 return;
1160
1161 surface->has_next_geometry = true;
1162 surface->next_geometry.x = x;
1163 surface->next_geometry.y = y;
1164 surface->next_geometry.width = width;
1165 surface->next_geometry.height = height;
1166 }
1167
1168 static void
weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client * wl_client,struct wl_resource * resource,uint32_t serial)1169 weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
1170 struct wl_resource *resource,
1171 uint32_t serial)
1172 {
1173 struct weston_desktop_surface *dsurface =
1174 wl_resource_get_user_data(resource);
1175 struct weston_desktop_xdg_surface *surface =
1176 weston_desktop_surface_get_implementation_data(dsurface);
1177 struct weston_desktop_xdg_surface_configure *configure, *temp;
1178 bool found = false;
1179
1180 if (!weston_desktop_xdg_surface_check_role(surface))
1181 return;
1182
1183 wl_list_for_each_safe(configure, temp, &surface->configure_list, link) {
1184 if (configure->serial < serial) {
1185 wl_list_remove(&configure->link);
1186 free(configure);
1187 } else if (configure->serial == serial) {
1188 wl_list_remove(&configure->link);
1189 found = true;
1190 break;
1191 } else {
1192 break;
1193 }
1194 }
1195 if (!found) {
1196 struct weston_desktop_client *client =
1197 weston_desktop_surface_get_client(dsurface);
1198 struct wl_resource *client_resource =
1199 weston_desktop_client_get_resource(client);
1200 wl_resource_post_error(client_resource,
1201 XDG_WM_BASE_ERROR_INVALID_SURFACE_STATE,
1202 "Wrong configure serial: %u", serial);
1203 return;
1204 }
1205
1206 surface->configured = true;
1207
1208 switch (surface->role) {
1209 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1210 assert(0 && "not reached");
1211 break;
1212 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1213 weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface,
1214 (struct weston_desktop_xdg_toplevel_configure *) configure);
1215 break;
1216 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1217 break;
1218 }
1219
1220 free(configure);
1221 }
1222
1223 static void
weston_desktop_xdg_surface_ping(struct weston_desktop_surface * dsurface,uint32_t serial,void * user_data)1224 weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
1225 uint32_t serial, void *user_data)
1226 {
1227 struct weston_desktop_client *client =
1228 weston_desktop_surface_get_client(dsurface);
1229
1230 xdg_wm_base_send_ping(weston_desktop_client_get_resource(client),
1231 serial);
1232 }
1233
1234 static void
weston_desktop_xdg_surface_committed(struct weston_desktop_surface * dsurface,void * user_data,int32_t sx,int32_t sy)1235 weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
1236 void *user_data,
1237 int32_t sx, int32_t sy)
1238 {
1239 struct weston_desktop_xdg_surface *surface = user_data;
1240 struct weston_surface *wsurface =
1241 weston_desktop_surface_get_surface (dsurface);
1242
1243 if (wsurface->buffer_ref.buffer && !surface->configured) {
1244 wl_resource_post_error(surface->resource,
1245 XDG_SURFACE_ERROR_UNCONFIGURED_BUFFER,
1246 "xdg_surface has never been configured");
1247 return;
1248 }
1249
1250 if (surface->has_next_geometry) {
1251 surface->has_next_geometry = false;
1252 weston_desktop_surface_set_geometry(surface->desktop_surface,
1253 surface->next_geometry);
1254 }
1255
1256 switch (surface->role) {
1257 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1258 wl_resource_post_error(surface->resource,
1259 XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
1260 "xdg_surface must have a role");
1261 break;
1262 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1263 weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
1264 break;
1265 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1266 weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1267 break;
1268 }
1269 }
1270
1271 static void
weston_desktop_xdg_surface_close(struct weston_desktop_surface * dsurface,void * user_data)1272 weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1273 void *user_data)
1274 {
1275 struct weston_desktop_xdg_surface *surface = user_data;
1276
1277 switch (surface->role) {
1278 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1279 assert(0 && "not reached");
1280 break;
1281 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1282 weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1283 break;
1284 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1285 weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1286 break;
1287 }
1288 }
1289
1290 static void
weston_desktop_xdg_surface_destroy(struct weston_desktop_surface * dsurface,void * user_data)1291 weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1292 void *user_data)
1293 {
1294 struct weston_desktop_xdg_surface *surface = user_data;
1295 struct weston_desktop_xdg_surface_configure *configure, *temp;
1296
1297 switch (surface->role) {
1298 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1299 break;
1300 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1301 weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1302 break;
1303 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1304 weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1305 break;
1306 }
1307
1308 if (surface->configure_idle != NULL)
1309 wl_event_source_remove(surface->configure_idle);
1310
1311 wl_list_for_each_safe(configure, temp, &surface->configure_list, link)
1312 free(configure);
1313
1314 free(surface);
1315 }
1316
1317 static const struct xdg_surface_interface weston_desktop_xdg_surface_implementation = {
1318 .destroy = weston_desktop_destroy_request,
1319 .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel,
1320 .get_popup = weston_desktop_xdg_surface_protocol_get_popup,
1321 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1322 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
1323 };
1324
1325 static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1326 /* These are used for toplevel only */
1327 .set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1328 .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1329 .set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1330 .set_activated = weston_desktop_xdg_toplevel_set_activated,
1331 .set_size = weston_desktop_xdg_toplevel_set_size,
1332
1333 .get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1334 .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1335 .get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1336 .get_activated = weston_desktop_xdg_toplevel_get_activated,
1337
1338 /* These are used for popup only */
1339 .update_position = weston_desktop_xdg_popup_update_position,
1340
1341 /* Common API */
1342 .committed = weston_desktop_xdg_surface_committed,
1343 .ping = weston_desktop_xdg_surface_ping,
1344 .close = weston_desktop_xdg_surface_close,
1345
1346 .destroy = weston_desktop_xdg_surface_destroy,
1347 };
1348
1349 static void
weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id)1350 weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1351 struct wl_resource *resource,
1352 uint32_t id)
1353 {
1354 struct weston_desktop_client *client =
1355 wl_resource_get_user_data(resource);
1356 struct weston_desktop_xdg_positioner *positioner;
1357
1358 positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1359 if (positioner == NULL) {
1360 wl_client_post_no_memory(wl_client);
1361 return;
1362 }
1363
1364 positioner->client = client;
1365 positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1366
1367 positioner->resource =
1368 wl_resource_create(wl_client,
1369 &xdg_positioner_interface,
1370 wl_resource_get_version(resource), id);
1371 if (positioner->resource == NULL) {
1372 wl_client_post_no_memory(wl_client);
1373 free(positioner);
1374 return;
1375 }
1376 wl_resource_set_implementation(positioner->resource,
1377 &weston_desktop_xdg_positioner_implementation,
1378 positioner, weston_desktop_xdg_positioner_destroy);
1379 }
1380
1381 static void
weston_desktop_xdg_surface_resource_destroy(struct wl_resource * resource)1382 weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1383 {
1384 struct weston_desktop_surface *dsurface =
1385 wl_resource_get_user_data(resource);
1386
1387 if (dsurface != NULL)
1388 weston_desktop_surface_resource_destroy(resource);
1389 }
1390
1391 static void
weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id,struct wl_resource * surface_resource)1392 weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1393 struct wl_resource *resource,
1394 uint32_t id,
1395 struct wl_resource *surface_resource)
1396 {
1397 struct weston_desktop_client *client =
1398 wl_resource_get_user_data(resource);
1399 struct weston_surface *wsurface =
1400 wl_resource_get_user_data(surface_resource);
1401 struct weston_desktop_xdg_surface *surface;
1402
1403 surface = zalloc(weston_desktop_surface_role_biggest_size);
1404 if (surface == NULL) {
1405 wl_client_post_no_memory(wl_client);
1406 return;
1407 }
1408
1409 surface->desktop = weston_desktop_client_get_desktop(client);
1410 surface->surface = wsurface;
1411 wl_list_init(&surface->configure_list);
1412
1413 surface->desktop_surface =
1414 weston_desktop_surface_create(surface->desktop, client,
1415 surface->surface,
1416 &weston_desktop_xdg_surface_internal_implementation,
1417 surface);
1418 if (surface->desktop_surface == NULL) {
1419 free(surface);
1420 return;
1421 }
1422
1423 surface->resource =
1424 weston_desktop_surface_add_resource(surface->desktop_surface,
1425 &xdg_surface_interface,
1426 &weston_desktop_xdg_surface_implementation,
1427 id, weston_desktop_xdg_surface_resource_destroy);
1428 if (surface->resource == NULL)
1429 return;
1430
1431 if (wsurface->buffer_ref.buffer != NULL) {
1432 wl_resource_post_error(surface->resource,
1433 XDG_SURFACE_ERROR_UNCONFIGURED_BUFFER,
1434 "xdg_surface must not have a buffer at creation");
1435 return;
1436 }
1437 }
1438
1439 static void
weston_desktop_xdg_shell_protocol_pong(struct wl_client * wl_client,struct wl_resource * resource,uint32_t serial)1440 weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1441 struct wl_resource *resource,
1442 uint32_t serial)
1443 {
1444 struct weston_desktop_client *client =
1445 wl_resource_get_user_data(resource);
1446
1447 weston_desktop_client_pong(client, serial);
1448 }
1449
1450 static const struct xdg_wm_base_interface weston_desktop_xdg_shell_implementation = {
1451 .destroy = weston_desktop_destroy_request,
1452 .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1453 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1454 .pong = weston_desktop_xdg_shell_protocol_pong,
1455 };
1456
1457 static void
weston_desktop_xdg_shell_bind(struct wl_client * client,void * data,uint32_t version,uint32_t id)1458 weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1459 uint32_t version, uint32_t id)
1460 {
1461 struct weston_desktop *desktop = data;
1462
1463 weston_desktop_client_create(desktop, client, NULL,
1464 &xdg_wm_base_interface,
1465 &weston_desktop_xdg_shell_implementation,
1466 version, id);
1467 }
1468
1469 struct wl_global *
weston_desktop_xdg_wm_base_create(struct weston_desktop * desktop,struct wl_display * display)1470 weston_desktop_xdg_wm_base_create(struct weston_desktop *desktop,
1471 struct wl_display *display)
1472 {
1473 return wl_global_create(display, &xdg_wm_base_interface,
1474 WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1475 weston_desktop_xdg_shell_bind);
1476 }
1477