blob: 53482a67a2daaa37f0232a5649cc47c2b664f7ad [file] [log] [blame]
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001/*
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 "compositor.h"
35#include "zalloc.h"
36#include "protocol/xdg-shell-unstable-v6-server-protocol.h"
37
38#include "libweston-desktop.h"
39#include "internal.h"
40
41#define WD_XDG_SHELL_PROTOCOL_VERSION 1
42
43static const char *weston_desktop_xdg_toplevel_role = "xdg_toplevel";
44static const char *weston_desktop_xdg_popup_role = "xdg_popup";
45
46struct weston_desktop_xdg_positioner {
47 struct weston_desktop *desktop;
48 struct weston_desktop_client *client;
49 struct wl_resource *resource;
50
51 struct weston_size size;
52 struct weston_geometry anchor_rect;
53 enum zxdg_positioner_v6_anchor anchor;
54 enum zxdg_positioner_v6_gravity gravity;
55 enum zxdg_positioner_v6_constraint_adjustment constraint_adjustment;
56 struct weston_position offset;
57};
58
59enum weston_desktop_xdg_surface_role {
60 WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE,
61 WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL,
62 WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP,
63};
64
65struct weston_desktop_xdg_surface {
66 struct wl_resource *resource;
67 struct weston_desktop *desktop;
68 struct weston_surface *surface;
69 struct weston_desktop_surface *desktop_surface;
70 bool configured;
71 struct wl_event_source *configure_idle;
72 uint32_t configure_serial;
73
74 bool has_next_geometry;
75 struct weston_geometry next_geometry;
76
77 enum weston_desktop_xdg_surface_role role;
78};
79
80struct weston_desktop_xdg_toplevel {
81 struct weston_desktop_xdg_surface base;
82
83 struct wl_resource *resource;
84 bool added;
85 struct weston_size requested_size;
86 struct {
87 bool maximized;
88 bool fullscreen;
89 bool resizing;
90 bool activated;
91 } requested_state, next_state, state;
92 struct weston_size
93 next_max_size, max_size,
94 next_min_size, min_size;
95};
96
97struct weston_desktop_xdg_popup {
98 struct weston_desktop_xdg_surface base;
99
100 struct wl_resource *resource;
101 bool committed;
102 struct weston_desktop_xdg_surface *parent;
103 struct weston_desktop_seat *seat;
104 struct weston_geometry geometry;
105};
106
107#define weston_desktop_surface_role_biggest_size (sizeof(struct weston_desktop_xdg_toplevel))
108
109
110static struct weston_geometry
111weston_desktop_xdg_positioner_get_geometry(struct weston_desktop_xdg_positioner *positioner,
112 struct weston_desktop_surface *dsurface,
113 struct weston_desktop_surface *parent)
114{
115 struct weston_geometry geometry = {
116 .x = positioner->offset.x,
117 .y = positioner->offset.y,
118 .width = positioner->size.width,
119 .height = positioner->size.height,
120 };
121
122 if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP)
123 geometry.y += positioner->anchor_rect.y;
124 else if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)
125 geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height;
126 else
127 geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height / 2;
128
129 if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT)
130 geometry.x += positioner->anchor_rect.x;
131 else if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT)
132 geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width;
133 else
134 geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width / 2;
135
136 if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP)
137 geometry.y -= geometry.height;
138 else if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)
139 geometry.y = geometry.y;
140 else
141 geometry.y -= geometry.height / 2;
142
143 if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT)
144 geometry.x -= geometry.width;
145 else if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT)
146 geometry.x = geometry.x;
147 else
148 geometry.x -= geometry.width / 2;
149
150 if (positioner->constraint_adjustment == ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_NONE)
151 return geometry;
152
153 /* TODO: add compositor policy configuration and the code here */
154
155 return geometry;
156}
157
158static void
159weston_desktop_xdg_positioner_protocol_set_size(struct wl_client *wl_client,
160 struct wl_resource *resource,
161 int32_t width, int32_t height)
162{
163 struct weston_desktop_xdg_positioner *positioner =
164 wl_resource_get_user_data(resource);
165
166 if (width < 1 || height < 1) {
167 wl_resource_post_error(resource,
168 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
169 "width and height must be positives and non-zero");
170 return;
171 }
172
173 positioner->size.width = width;
174 positioner->size.height = height;
175}
176
177static void
178weston_desktop_xdg_positioner_protocol_set_anchor_rect(struct wl_client *wl_client,
179 struct wl_resource *resource,
180 int32_t x, int32_t y,
181 int32_t width, int32_t height)
182{
183 struct weston_desktop_xdg_positioner *positioner =
184 wl_resource_get_user_data(resource);
185
186 if (width < 1 || height < 1) {
187 wl_resource_post_error(resource,
188 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
189 "width and height must be positives and non-zero");
190 return;
191 }
192
193 positioner->anchor_rect.x = x;
194 positioner->anchor_rect.y = y;
195 positioner->anchor_rect.width = width;
196 positioner->anchor_rect.height = height;
197}
198
199static void
200weston_desktop_xdg_positioner_protocol_set_anchor(struct wl_client *wl_client,
201 struct wl_resource *resource,
202 enum zxdg_positioner_v6_anchor anchor)
203{
204 struct weston_desktop_xdg_positioner *positioner =
205 wl_resource_get_user_data(resource);
206
207 if (((anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP ) &&
208 (anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)) ||
209 ((anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT) &&
210 (anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT))) {
211 wl_resource_post_error(resource,
212 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
213 "same-axis values are not allowed");
214 return;
215 }
216
217 positioner->anchor = anchor;
218}
219
220static void
221weston_desktop_xdg_positioner_protocol_set_gravity(struct wl_client *wl_client,
222 struct wl_resource *resource,
223 enum zxdg_positioner_v6_gravity gravity)
224{
225 struct weston_desktop_xdg_positioner *positioner =
226 wl_resource_get_user_data(resource);
227
228 if (((gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP) &&
229 (gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)) ||
230 ((gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT) &&
231 (gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT))) {
232 wl_resource_post_error(resource,
233 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
234 "same-axis values are not allowed");
235 return;
236 }
237
238 positioner->gravity = gravity;
239}
240
241static void
242weston_desktop_xdg_positioner_protocol_set_constraint_adjustment(struct wl_client *wl_client,
243 struct wl_resource *resource,
244 enum zxdg_positioner_v6_constraint_adjustment constraint_adjustment)
245{
246 struct weston_desktop_xdg_positioner *positioner =
247 wl_resource_get_user_data(resource);
248
249 positioner->constraint_adjustment = constraint_adjustment;
250}
251
252static void
253weston_desktop_xdg_positioner_protocol_set_offset(struct wl_client *wl_client,
254 struct wl_resource *resource,
255 int32_t x, int32_t y)
256{
257 struct weston_desktop_xdg_positioner *positioner =
258 wl_resource_get_user_data(resource);
259
260 positioner->offset.x = x;
261 positioner->offset.y = y;
262}
263
264static void
265weston_desktop_xdg_positioner_destroy(struct wl_resource *resource)
266{
267 struct weston_desktop_xdg_positioner *positioner =
268 wl_resource_get_user_data(resource);
269
270 free(positioner);
271}
272
273static const struct zxdg_positioner_v6_interface weston_desktop_xdg_positioner_implementation = {
274 .destroy = weston_desktop_destroy_request,
275 .set_size = weston_desktop_xdg_positioner_protocol_set_size,
276 .set_anchor_rect = weston_desktop_xdg_positioner_protocol_set_anchor_rect,
277 .set_anchor = weston_desktop_xdg_positioner_protocol_set_anchor,
278 .set_gravity = weston_desktop_xdg_positioner_protocol_set_gravity,
279 .set_constraint_adjustment = weston_desktop_xdg_positioner_protocol_set_constraint_adjustment,
280 .set_offset = weston_desktop_xdg_positioner_protocol_set_offset,
281};
282
283static void
284weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface);
285
286static void
287weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client,
288 struct wl_resource *resource,
289 struct wl_resource *parent_resource)
290{
291 struct weston_desktop_surface *dsurface =
292 wl_resource_get_user_data(resource);
293 struct weston_desktop_xdg_toplevel *toplevel =
294 weston_desktop_surface_get_implementation_data(dsurface);
295 struct weston_desktop_surface *parent = NULL;
296
297 if (parent_resource != NULL)
298 parent = wl_resource_get_user_data(parent_resource);
299 weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent);
300}
301
302static void
303weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client,
304 struct wl_resource *resource,
305 const char *title)
306{
307 struct weston_desktop_surface *toplevel =
308 wl_resource_get_user_data(resource);
309
310 weston_desktop_surface_set_title(toplevel, title);
311}
312
313static void
314weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client,
315 struct wl_resource *resource,
316 const char *app_id)
317{
318 struct weston_desktop_surface *toplevel =
319 wl_resource_get_user_data(resource);
320
321 weston_desktop_surface_set_app_id(toplevel, app_id);
322}
323
324static void
325weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client,
326 struct wl_resource *resource,
327 struct wl_resource *seat_resource,
328 uint32_t serial,
329 int32_t x, int32_t y)
330{
331 struct weston_desktop_surface *dsurface =
332 wl_resource_get_user_data(resource);
333 struct weston_seat *seat =
334 wl_resource_get_user_data(seat_resource);
335 struct weston_desktop_xdg_toplevel *toplevel =
336 weston_desktop_surface_get_implementation_data(dsurface);
337
338 weston_desktop_api_show_window_menu(toplevel->base.desktop,
339 dsurface, seat, x, y);
340}
341
342static void
343weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client,
344 struct wl_resource *resource,
345 struct wl_resource *seat_resource,
346 uint32_t serial)
347{
348 struct weston_desktop_surface *dsurface =
349 wl_resource_get_user_data(resource);
350 struct weston_seat *seat =
351 wl_resource_get_user_data(seat_resource);
352 struct weston_desktop_xdg_toplevel *toplevel =
353 weston_desktop_surface_get_implementation_data(dsurface);
354
355 weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial);
356}
357
358static void
359weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client,
360 struct wl_resource *resource,
361 struct wl_resource *seat_resource,
362 uint32_t serial,
363 enum zxdg_toplevel_v6_resize_edge edges)
364{
365 struct weston_desktop_surface *dsurface =
366 wl_resource_get_user_data(resource);
367 struct weston_seat *seat =
368 wl_resource_get_user_data(seat_resource);
369 struct weston_desktop_xdg_toplevel *toplevel =
370 weston_desktop_surface_get_implementation_data(dsurface);
371
372 weston_desktop_api_resize(toplevel->base.desktop,
373 dsurface, seat, serial, edges);
374}
375
376static void
377weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel)
378{
379 toplevel->next_state = toplevel->requested_state;
380}
381
382static void
383weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client,
384 struct wl_resource *resource,
385 int32_t width, int32_t height)
386{
387 struct weston_desktop_surface *dsurface =
388 wl_resource_get_user_data(resource);
389 struct weston_desktop_xdg_toplevel *toplevel =
390 weston_desktop_surface_get_implementation_data(dsurface);
391
392 toplevel->next_min_size.width = width;
393 toplevel->next_min_size.height = height;
394}
395
396static void
397weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client,
398 struct wl_resource *resource,
399 int32_t width, int32_t height)
400{
401 struct weston_desktop_surface *dsurface =
402 wl_resource_get_user_data(resource);
403 struct weston_desktop_xdg_toplevel *toplevel =
404 weston_desktop_surface_get_implementation_data(dsurface);
405
406 toplevel->next_max_size.width = width;
407 toplevel->next_max_size.height = height;
408}
409
410static void
411weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client,
412 struct wl_resource *resource)
413{
414 struct weston_desktop_surface *dsurface =
415 wl_resource_get_user_data(resource);
416 struct weston_desktop_xdg_toplevel *toplevel =
417 weston_desktop_surface_get_implementation_data(dsurface);
418
419 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true);
420}
421
422static void
423weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client,
424 struct wl_resource *resource)
425{
426 struct weston_desktop_surface *dsurface =
427 wl_resource_get_user_data(resource);
428 struct weston_desktop_xdg_toplevel *toplevel =
429 weston_desktop_surface_get_implementation_data(dsurface);
430
431 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false);
432}
433
434static void
435weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client,
436 struct wl_resource *resource,
437 struct wl_resource *output_resource)
438{
439 struct weston_desktop_surface *dsurface =
440 wl_resource_get_user_data(resource);
441 struct weston_desktop_xdg_toplevel *toplevel =
442 weston_desktop_surface_get_implementation_data(dsurface);
443 struct weston_output *output = NULL;
444
445 if (output_resource != NULL)
446 output = wl_resource_get_user_data(output_resource);
447
448 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
449 true, output);
450}
451
452static void
453weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client,
454 struct wl_resource *resource)
455{
456 struct weston_desktop_surface *dsurface =
457 wl_resource_get_user_data(resource);
458 struct weston_desktop_xdg_toplevel *toplevel =
459 weston_desktop_surface_get_implementation_data(dsurface);
460
461 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
462 false, NULL);
463}
464
465static void
466weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client,
467 struct wl_resource *resource)
468{
469 struct weston_desktop_surface *dsurface =
470 wl_resource_get_user_data(resource);
471 struct weston_desktop_xdg_toplevel *toplevel =
472 weston_desktop_surface_get_implementation_data(dsurface);
473
474 weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface);
475}
476
477static void
478weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel)
479{
480 uint32_t *s;
481 struct wl_array states;
482
483 wl_array_init(&states);
484 if (toplevel->requested_state.maximized) {
485 s = wl_array_add(&states, sizeof(uint32_t));
486 *s = ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED;
487 }
488 if (toplevel->requested_state.fullscreen) {
489 s = wl_array_add(&states, sizeof(uint32_t));
490 *s = ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN;
491 }
492 if (toplevel->requested_state.resizing) {
493 s = wl_array_add(&states, sizeof(uint32_t));
494 *s = ZXDG_TOPLEVEL_V6_STATE_RESIZING;
495 }
496 if (toplevel->requested_state.activated) {
497 s = wl_array_add(&states, sizeof(uint32_t));
498 *s = ZXDG_TOPLEVEL_V6_STATE_ACTIVATED;
499 }
500
501 zxdg_toplevel_v6_send_configure(toplevel->resource,
502 toplevel->requested_size.width,
503 toplevel->requested_size.height,
504 &states);
505
506 wl_array_release(&states);
507};
508
509static void
510weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface,
511 void *user_data, bool maximized)
512{
513 struct weston_desktop_xdg_toplevel *toplevel = user_data;
514
515 if (toplevel->state.maximized == maximized)
516 return;
517
518 toplevel->requested_state.maximized = maximized;
519 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
520}
521
522static void
523weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface,
524 void *user_data, bool fullscreen)
525{
526 struct weston_desktop_xdg_toplevel *toplevel = user_data;
527
528 if (toplevel->state.fullscreen == fullscreen)
529 return;
530
531 toplevel->requested_state.fullscreen = fullscreen;
532 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
533}
534
535static void
536weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface,
537 void *user_data, bool resizing)
538{
539 struct weston_desktop_xdg_toplevel *toplevel = user_data;
540
541 if (toplevel->state.resizing == resizing)
542 return;
543
544 toplevel->requested_state.resizing = resizing;
545 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
546}
547
548static void
549weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface,
550 void *user_data, bool activated)
551{
552 struct weston_desktop_xdg_toplevel *toplevel = user_data;
553
554 if (toplevel->state.activated == activated)
555 return;
556
557 toplevel->requested_state.activated = activated;
558 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
559}
560
561static void
562weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface,
563 void *user_data,
564 int32_t width, int32_t height)
565{
566 struct weston_desktop_xdg_toplevel *toplevel = user_data;
567 struct weston_surface *wsurface =
568 weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
569
570 if (wsurface->width == width && wsurface->height == height)
571 return;
572
573 toplevel->requested_size.width = width;
574 toplevel->requested_size.height = height;
575 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
576}
577
578static void
579weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel,
Quentin Glidiccba26e72016-08-15 12:20:22 +0200580 int32_t sx, int32_t sy)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200581{
582 struct weston_surface *wsurface =
583 weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
584 bool reconfigure = false;
585
Quentin Glidiccba26e72016-08-15 12:20:22 +0200586 if (!wsurface->buffer_ref.buffer && !toplevel->added) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200587 weston_desktop_api_surface_added(toplevel->base.desktop,
588 toplevel->base.desktop_surface);
589 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
590 toplevel->added = true;
591 return;
592 }
Quentin Glidiccba26e72016-08-15 12:20:22 +0200593 if (!wsurface->buffer_ref.buffer)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200594 return;
595
596 if (toplevel->next_state.maximized || toplevel->next_state.fullscreen)
597 reconfigure =
598 ( ( toplevel->requested_size.width != wsurface->width ) ||
599 ( toplevel->requested_size.height != wsurface->height ) );
600
601 if (reconfigure) {
602 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
603 } else {
604 toplevel->state = toplevel->next_state;
605 toplevel->min_size = toplevel->next_min_size;
606 toplevel->max_size = toplevel->next_max_size;
607
608 weston_desktop_api_committed(toplevel->base.desktop,
609 toplevel->base.desktop_surface,
610 sx, sy);
611 }
612}
613
614static void
615weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel)
616{
617 zxdg_toplevel_v6_send_close(toplevel->resource);
618}
619
620static bool
621weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface,
622 void *user_data)
623{
624 struct weston_desktop_xdg_toplevel *toplevel = user_data;
625
626 return toplevel->state.maximized;
627}
628
629static bool
630weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface,
631 void *user_data)
632{
633 struct weston_desktop_xdg_toplevel *toplevel = user_data;
634
635 return toplevel->state.fullscreen;
636}
637
638static bool
639weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface,
640 void *user_data)
641{
642 struct weston_desktop_xdg_toplevel *toplevel = user_data;
643
644 return toplevel->state.resizing;
645}
646
647static bool
648weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface,
649 void *user_data)
650{
651 struct weston_desktop_xdg_toplevel *toplevel = user_data;
652
653 return toplevel->state.activated;
654}
655
656static void
657weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel)
658{
659 if (toplevel->added)
660 weston_desktop_api_surface_removed(toplevel->base.desktop,
661 toplevel->base.desktop_surface);
662}
663
664static void
665weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource)
666{
667 struct weston_desktop_surface *dsurface =
668 wl_resource_get_user_data(resource);
669
670 if (dsurface != NULL)
671 weston_desktop_surface_resource_destroy(resource);
672}
673
674static const struct zxdg_toplevel_v6_interface weston_desktop_xdg_toplevel_implementation = {
675 .destroy = weston_desktop_destroy_request,
676 .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent,
677 .set_title = weston_desktop_xdg_toplevel_protocol_set_title,
678 .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id,
679 .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu,
680 .move = weston_desktop_xdg_toplevel_protocol_move,
681 .resize = weston_desktop_xdg_toplevel_protocol_resize,
682 .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size,
683 .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size,
684 .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized,
685 .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized,
686 .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen,
687 .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen,
688 .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized,
689};
690
691static void
692weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client,
693 struct wl_resource *resource,
694 struct wl_resource *seat_resource,
695 uint32_t serial)
696{
697 struct weston_desktop_surface *dsurface =
698 wl_resource_get_user_data(resource);
699 struct weston_desktop_xdg_popup *popup =
700 weston_desktop_surface_get_implementation_data(dsurface);
701 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
702 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
703 struct weston_desktop_surface *topmost;
704 bool parent_is_toplevel =
705 popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
706
707 if (popup->committed) {
708 wl_resource_post_error(popup->resource,
709 ZXDG_POPUP_V6_ERROR_INVALID_GRAB,
710 "xdg_popup already is mapped");
711 return;
712 }
713
714 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
715 if ((topmost == NULL && !parent_is_toplevel) ||
716 (topmost != NULL && topmost != popup->parent->desktop_surface)) {
717 struct weston_desktop_client *client =
718 weston_desktop_surface_get_client(dsurface);
719 struct wl_resource *client_resource =
720 weston_desktop_client_get_resource(client);
721
722 wl_resource_post_error(client_resource,
723 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
724 "xdg_popup was not created on the topmost popup");
725 return;
726 }
727
728 popup->seat = seat;
729 weston_desktop_surface_popup_grab(popup->base.desktop_surface,
730 popup->seat, serial);
731}
732
733static void
734weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup)
735{
736 zxdg_popup_v6_send_configure(popup->resource,
737 popup->geometry.x,
738 popup->geometry.y,
739 popup->geometry.width,
740 popup->geometry.height);
741}
742
743static void
744weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
745 void *user_data);
746
747static void
748weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup)
749{
750 if (!popup->committed)
751 weston_desktop_xdg_surface_schedule_configure(&popup->base);
752 popup->committed = true;
753 weston_desktop_xdg_popup_update_position(popup->base.desktop_surface,
754 popup);
755}
756
757static void
758weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
759 void *user_data)
760{
761}
762
763static void
764weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup)
765{
766 zxdg_popup_v6_send_popup_done(popup->resource);
767}
768
769static void
770weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup)
771{
772 struct weston_desktop_surface *topmost;
773 struct weston_desktop_client *client =
774 weston_desktop_surface_get_client(popup->base.desktop_surface);
775
776 if (!weston_desktop_surface_get_grab(popup->base.desktop_surface))
777 return;
778
779 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
780 if (topmost != popup->base.desktop_surface) {
781 struct wl_resource *client_resource =
782 weston_desktop_client_get_resource(client);
783
784 wl_resource_post_error(client_resource,
785 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
786 "xdg_popup was destroyed while it was not the topmost popup.");
787 }
788
789 weston_desktop_surface_popup_ungrab(popup->base.desktop_surface,
790 popup->seat);
791}
792
793static void
794weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource)
795{
796 struct weston_desktop_surface *dsurface =
797 wl_resource_get_user_data(resource);
798
799 if (dsurface != NULL)
800 weston_desktop_surface_resource_destroy(resource);
801}
802
803static const struct zxdg_popup_v6_interface weston_desktop_xdg_popup_implementation = {
804 .destroy = weston_desktop_destroy_request,
805 .grab = weston_desktop_xdg_popup_protocol_grab,
806};
807
808static void
809weston_desktop_xdg_surface_send_configure(void *user_data)
810{
811 struct weston_desktop_xdg_surface *surface = user_data;
812
813 surface->configure_idle = NULL;
814 surface->configure_serial =
815 wl_display_next_serial(weston_desktop_get_display(surface->desktop));
816
817 switch (surface->role) {
818 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
819 assert(0 && "not reached");
820 break;
821 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
822 weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface);
823 break;
824 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
825 weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface);
826 break;
827 }
828
829 zxdg_surface_v6_send_configure(surface->resource, surface->configure_serial);
830}
831
832static void
833weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface)
834{
835 struct wl_display *display = weston_desktop_get_display(surface->desktop);
836 struct wl_event_loop *loop = wl_display_get_event_loop(display);
837
838 if (surface->configure_idle != NULL)
839 return;
840 surface->configure_idle =
841 wl_event_loop_add_idle(loop,
842 weston_desktop_xdg_surface_send_configure,
843 surface);
844}
845
846static void
847weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client,
848 struct wl_resource *resource,
849 uint32_t id)
850{
851 struct weston_desktop_surface *dsurface =
852 wl_resource_get_user_data(resource);
853 struct weston_surface *wsurface =
854 weston_desktop_surface_get_surface(dsurface);
855 struct weston_desktop_xdg_toplevel *toplevel =
856 weston_desktop_surface_get_implementation_data(dsurface);
857
858 if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role,
859 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
860 return;
861
862 toplevel->resource =
863 weston_desktop_surface_add_resource(toplevel->base.desktop_surface,
864 &zxdg_toplevel_v6_interface,
865 &weston_desktop_xdg_toplevel_implementation,
866 id, weston_desktop_xdg_toplevel_resource_destroy);
867 if (toplevel->resource == NULL)
868 return;
869
870 toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
871}
872
873static void
874weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client,
875 struct wl_resource *resource,
876 uint32_t id,
877 struct wl_resource *parent_resource,
878 struct wl_resource *positioner_resource)
879{
880 struct weston_desktop_surface *dsurface =
881 wl_resource_get_user_data(resource);
882 struct weston_surface *wsurface =
883 weston_desktop_surface_get_surface(dsurface);
884 struct weston_desktop_xdg_popup *popup =
885 weston_desktop_surface_get_implementation_data(dsurface);
886 struct weston_desktop_surface *parent_surface =
887 wl_resource_get_user_data(parent_resource);
888 struct weston_desktop_xdg_surface *parent =
889 weston_desktop_surface_get_implementation_data(parent_surface);
890 struct weston_desktop_xdg_positioner *positioner =
891 wl_resource_get_user_data(positioner_resource);
892
893 if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
894 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
895 return;
896
897 popup->resource =
898 weston_desktop_surface_add_resource(popup->base.desktop_surface,
899 &zxdg_popup_v6_interface,
900 &weston_desktop_xdg_popup_implementation,
901 id, weston_desktop_xdg_popup_resource_destroy);
902 if (popup->resource == NULL)
903 return;
904
905 popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
906 popup->parent = parent;
907
908 popup->geometry =
909 weston_desktop_xdg_positioner_get_geometry(positioner,
910 dsurface,
911 parent_surface);
912
913 weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
914 parent_surface,
915 popup->geometry.x,
916 popup->geometry.y,
917 true);
918}
919
920static bool
921weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
922{
923 struct weston_surface *wsurface =
924 weston_desktop_surface_get_surface(surface->desktop_surface);
925 const char *role;
926
927 role = weston_surface_get_role(wsurface);
928 if (role != NULL &&
929 (role == weston_desktop_xdg_toplevel_role ||
930 role == weston_desktop_xdg_popup_role))
931 return true;
932
933 wl_resource_post_error(surface->resource,
934 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
935 "xdg_surface must have a role");
936 return false;
937}
938
939static void
940weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
941 struct wl_resource *resource,
942 int32_t x, int32_t y,
943 int32_t width, int32_t height)
944{
945 struct weston_desktop_surface *dsurface =
946 wl_resource_get_user_data(resource);
947 struct weston_desktop_xdg_surface *surface =
948 weston_desktop_surface_get_implementation_data(dsurface);
949
950 if (!weston_desktop_xdg_surface_check_role(surface))
951 return;
952
953 surface->has_next_geometry = true;
954 surface->next_geometry.x = x;
955 surface->next_geometry.y = y;
956 surface->next_geometry.width = width;
957 surface->next_geometry.height = height;
958}
959
960static void
961weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
962 struct wl_resource *resource,
963 uint32_t serial)
964{
965 struct weston_desktop_surface *dsurface =
966 wl_resource_get_user_data(resource);
967 struct weston_desktop_xdg_surface *surface =
968 weston_desktop_surface_get_implementation_data(dsurface);
969
970 if (!weston_desktop_xdg_surface_check_role(surface))
971 return;
972
973 if (surface->configure_serial != serial)
974 return;
975
976 surface->configured = true;
977
978 switch (surface->role) {
979 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
980 assert(0 && "not reached");
981 break;
982 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
983 weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface);
984 break;
985 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
986 break;
987 }
988}
989
990static void
991weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
992 uint32_t serial, void *user_data)
993{
994 struct weston_desktop_client *client =
995 weston_desktop_surface_get_client(dsurface);
996
997 zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client),
998 serial);
999}
1000
1001static void
1002weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
1003 void *user_data, bool new_buffer,
1004 int32_t sx, int32_t sy)
1005{
1006 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidiccba26e72016-08-15 12:20:22 +02001007 struct weston_surface *wsurface =
1008 weston_desktop_surface_get_surface (dsurface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001009
Quentin Glidiccba26e72016-08-15 12:20:22 +02001010 if (wsurface->buffer_ref.buffer && !surface->configured) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001011 wl_resource_post_error(surface->resource,
1012 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1013 "xdg_surface has never been configured");
1014 return;
1015 }
1016
1017 if (surface->has_next_geometry) {
1018 surface->has_next_geometry = false;
1019 weston_desktop_surface_set_geometry(surface->desktop_surface,
1020 surface->next_geometry);
1021 }
1022
1023 switch (surface->role) {
1024 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1025 wl_resource_post_error(surface->resource,
1026 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1027 "xdg_surface must have a role");
1028 break;
1029 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidiccba26e72016-08-15 12:20:22 +02001030 weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001031 break;
1032 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1033 weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1034 break;
1035 }
1036}
1037
1038static void
1039weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1040 void *user_data)
1041{
1042 struct weston_desktop_xdg_surface *surface = user_data;
1043
1044 switch (surface->role) {
1045 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1046 assert(0 && "not reached");
1047 break;
1048 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1049 weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1050 break;
1051 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1052 weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1053 break;
1054 }
1055}
1056
1057static void
1058weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1059 void *user_data)
1060{
1061 struct weston_desktop_xdg_surface *surface = user_data;
1062
1063 switch (surface->role) {
1064 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1065 break;
1066 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1067 weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1068 break;
1069 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1070 weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1071 break;
1072 }
1073
1074 if (surface->configure_idle != NULL)
1075 wl_event_source_remove(surface->configure_idle);
1076
1077 free(surface);
1078}
1079
1080static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = {
1081 .destroy = weston_desktop_destroy_request,
1082 .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel,
1083 .get_popup = weston_desktop_xdg_surface_protocol_get_popup,
1084 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1085 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
1086};
1087
1088static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1089 /* These are used for toplevel only */
1090 .set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1091 .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1092 .set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1093 .set_activated = weston_desktop_xdg_toplevel_set_activated,
1094 .set_size = weston_desktop_xdg_toplevel_set_size,
1095
1096 .get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1097 .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1098 .get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1099 .get_activated = weston_desktop_xdg_toplevel_get_activated,
1100
1101 /* These are used for popup only */
1102 .update_position = weston_desktop_xdg_popup_update_position,
1103
1104 /* Common API */
1105 .committed = weston_desktop_xdg_surface_committed,
1106 .ping = weston_desktop_xdg_surface_ping,
1107 .close = weston_desktop_xdg_surface_close,
1108
1109 .destroy = weston_desktop_xdg_surface_destroy,
1110};
1111
1112static void
1113weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1114 struct wl_resource *resource,
1115 uint32_t id)
1116{
1117 struct weston_desktop_client *client =
1118 wl_resource_get_user_data(resource);
1119 struct weston_desktop_xdg_positioner *positioner;
1120
1121 positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1122 if (positioner == NULL) {
1123 wl_client_post_no_memory(wl_client);
1124 return;
1125 }
1126
1127 positioner->client = client;
1128 positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1129
1130 positioner->resource =
1131 wl_resource_create(wl_client,
1132 &zxdg_positioner_v6_interface,
1133 wl_resource_get_version(resource), id);
1134 if (positioner->resource == NULL) {
1135 wl_client_post_no_memory(wl_client);
1136 free(positioner);
1137 return;
1138 }
1139 wl_resource_set_implementation(positioner->resource,
1140 &weston_desktop_xdg_positioner_implementation,
1141 positioner, weston_desktop_xdg_positioner_destroy);
1142}
1143
1144static void
1145weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1146{
1147 struct weston_desktop_surface *dsurface =
1148 wl_resource_get_user_data(resource);
1149
1150 if (dsurface != NULL)
1151 weston_desktop_surface_resource_destroy(resource);
1152}
1153
1154static void
1155weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1156 struct wl_resource *resource,
1157 uint32_t id,
1158 struct wl_resource *surface_resource)
1159{
1160 struct weston_desktop_client *client =
1161 wl_resource_get_user_data(resource);
1162 struct weston_surface *wsurface =
1163 wl_resource_get_user_data(surface_resource);
1164 struct weston_desktop_xdg_surface *surface;
1165
1166 surface = zalloc(weston_desktop_surface_role_biggest_size);
1167 if (surface == NULL) {
1168 wl_client_post_no_memory(wl_client);
1169 return;
1170 }
1171
1172 surface->desktop = weston_desktop_client_get_desktop(client);
1173 surface->surface = wsurface;
1174
1175 surface->desktop_surface =
1176 weston_desktop_surface_create(surface->desktop, client,
1177 surface->surface,
1178 &weston_desktop_xdg_surface_internal_implementation,
1179 surface);
1180 if (surface->desktop_surface == NULL) {
1181 free(surface);
1182 return;
1183 }
1184
1185 surface->resource =
1186 weston_desktop_surface_add_resource(surface->desktop_surface,
1187 &zxdg_surface_v6_interface,
1188 &weston_desktop_xdg_surface_implementation,
1189 id, weston_desktop_xdg_surface_resource_destroy);
1190 if (surface->resource == NULL)
1191 return;
1192
1193 if (wsurface->buffer_ref.buffer != NULL) {
1194 wl_resource_post_error(surface->resource,
1195 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1196 "xdg_surface must not have a buffer at creation");
1197 return;
1198 }
1199}
1200
1201static void
1202weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1203 struct wl_resource *resource,
1204 uint32_t serial)
1205{
1206 struct weston_desktop_client *client =
1207 wl_resource_get_user_data(resource);
1208
1209 weston_desktop_client_pong(client, serial);
1210}
1211
1212static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = {
1213 .destroy = weston_desktop_destroy_request,
1214 .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1215 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1216 .pong = weston_desktop_xdg_shell_protocol_pong,
1217};
1218
1219static void
1220weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1221 uint32_t version, uint32_t id)
1222{
1223 struct weston_desktop *desktop = data;
1224
1225 weston_desktop_client_create(desktop, client, NULL,
1226 &zxdg_shell_v6_interface,
1227 &weston_desktop_xdg_shell_implementation,
1228 version, id);
1229}
1230
1231struct wl_global *
1232weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display)
1233{
1234 return wl_global_create(display, &zxdg_shell_v6_interface,
1235 WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1236 weston_desktop_xdg_shell_bind);
1237}