blob: 552b3306da6f0b491c10eac0b6073d5cbb3f25e5 [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
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200287weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel *toplevel)
288{
289 if (toplevel->added)
290 return;
291
292 weston_desktop_api_surface_added(toplevel->base.desktop,
293 toplevel->base.desktop_surface);
294 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
295 toplevel->added = true;
296}
297
298static void
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200299weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client,
300 struct wl_resource *resource,
301 struct wl_resource *parent_resource)
302{
303 struct weston_desktop_surface *dsurface =
304 wl_resource_get_user_data(resource);
305 struct weston_desktop_xdg_toplevel *toplevel =
306 weston_desktop_surface_get_implementation_data(dsurface);
307 struct weston_desktop_surface *parent = NULL;
308
309 if (parent_resource != NULL)
310 parent = wl_resource_get_user_data(parent_resource);
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200311
312 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200313 weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent);
314}
315
316static void
317weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client,
318 struct wl_resource *resource,
319 const char *title)
320{
321 struct weston_desktop_surface *toplevel =
322 wl_resource_get_user_data(resource);
323
324 weston_desktop_surface_set_title(toplevel, title);
325}
326
327static void
328weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client,
329 struct wl_resource *resource,
330 const char *app_id)
331{
332 struct weston_desktop_surface *toplevel =
333 wl_resource_get_user_data(resource);
334
335 weston_desktop_surface_set_app_id(toplevel, app_id);
336}
337
338static void
339weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client,
340 struct wl_resource *resource,
341 struct wl_resource *seat_resource,
342 uint32_t serial,
343 int32_t x, int32_t y)
344{
345 struct weston_desktop_surface *dsurface =
346 wl_resource_get_user_data(resource);
347 struct weston_seat *seat =
348 wl_resource_get_user_data(seat_resource);
349 struct weston_desktop_xdg_toplevel *toplevel =
350 weston_desktop_surface_get_implementation_data(dsurface);
351
Quentin Glidic0abf8902016-09-11 11:34:47 +0200352 if (!toplevel->base.configured) {
353 wl_resource_post_error(toplevel->resource,
354 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
355 "Surface has not been configured yet");
356 return;
357 }
358
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200359 weston_desktop_api_show_window_menu(toplevel->base.desktop,
360 dsurface, seat, x, y);
361}
362
363static void
364weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client,
365 struct wl_resource *resource,
366 struct wl_resource *seat_resource,
367 uint32_t serial)
368{
369 struct weston_desktop_surface *dsurface =
370 wl_resource_get_user_data(resource);
371 struct weston_seat *seat =
372 wl_resource_get_user_data(seat_resource);
373 struct weston_desktop_xdg_toplevel *toplevel =
374 weston_desktop_surface_get_implementation_data(dsurface);
375
Quentin Glidic0abf8902016-09-11 11:34:47 +0200376 if (!toplevel->base.configured) {
377 wl_resource_post_error(toplevel->resource,
378 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
379 "Surface has not been configured yet");
380 return;
381 }
382
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200383 weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial);
384}
385
386static void
387weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client,
388 struct wl_resource *resource,
389 struct wl_resource *seat_resource,
390 uint32_t serial,
391 enum zxdg_toplevel_v6_resize_edge edges)
392{
393 struct weston_desktop_surface *dsurface =
394 wl_resource_get_user_data(resource);
395 struct weston_seat *seat =
396 wl_resource_get_user_data(seat_resource);
397 struct weston_desktop_xdg_toplevel *toplevel =
398 weston_desktop_surface_get_implementation_data(dsurface);
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200399 enum weston_desktop_surface_edge surf_edges =
400 (enum weston_desktop_surface_edge) edges;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200401
Quentin Glidic0abf8902016-09-11 11:34:47 +0200402 if (!toplevel->base.configured) {
403 wl_resource_post_error(toplevel->resource,
404 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
405 "Surface has not been configured yet");
406 return;
407 }
408
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200409 weston_desktop_api_resize(toplevel->base.desktop,
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200410 dsurface, seat, serial, surf_edges);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200411}
412
413static void
414weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel)
415{
416 toplevel->next_state = toplevel->requested_state;
417}
418
419static void
420weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client,
421 struct wl_resource *resource,
422 int32_t width, int32_t height)
423{
424 struct weston_desktop_surface *dsurface =
425 wl_resource_get_user_data(resource);
426 struct weston_desktop_xdg_toplevel *toplevel =
427 weston_desktop_surface_get_implementation_data(dsurface);
428
429 toplevel->next_min_size.width = width;
430 toplevel->next_min_size.height = height;
431}
432
433static void
434weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client,
435 struct wl_resource *resource,
436 int32_t width, int32_t height)
437{
438 struct weston_desktop_surface *dsurface =
439 wl_resource_get_user_data(resource);
440 struct weston_desktop_xdg_toplevel *toplevel =
441 weston_desktop_surface_get_implementation_data(dsurface);
442
443 toplevel->next_max_size.width = width;
444 toplevel->next_max_size.height = height;
445}
446
447static void
448weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client,
449 struct wl_resource *resource)
450{
451 struct weston_desktop_surface *dsurface =
452 wl_resource_get_user_data(resource);
453 struct weston_desktop_xdg_toplevel *toplevel =
454 weston_desktop_surface_get_implementation_data(dsurface);
455
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200456 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200457 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true);
458}
459
460static void
461weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client,
462 struct wl_resource *resource)
463{
464 struct weston_desktop_surface *dsurface =
465 wl_resource_get_user_data(resource);
466 struct weston_desktop_xdg_toplevel *toplevel =
467 weston_desktop_surface_get_implementation_data(dsurface);
468
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200469 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200470 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false);
471}
472
473static void
474weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client,
475 struct wl_resource *resource,
476 struct wl_resource *output_resource)
477{
478 struct weston_desktop_surface *dsurface =
479 wl_resource_get_user_data(resource);
480 struct weston_desktop_xdg_toplevel *toplevel =
481 weston_desktop_surface_get_implementation_data(dsurface);
482 struct weston_output *output = NULL;
483
484 if (output_resource != NULL)
485 output = wl_resource_get_user_data(output_resource);
486
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200487 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200488 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
489 true, output);
490}
491
492static void
493weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client,
494 struct wl_resource *resource)
495{
496 struct weston_desktop_surface *dsurface =
497 wl_resource_get_user_data(resource);
498 struct weston_desktop_xdg_toplevel *toplevel =
499 weston_desktop_surface_get_implementation_data(dsurface);
500
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200501 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200502 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
503 false, NULL);
504}
505
506static void
507weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client,
508 struct wl_resource *resource)
509{
510 struct weston_desktop_surface *dsurface =
511 wl_resource_get_user_data(resource);
512 struct weston_desktop_xdg_toplevel *toplevel =
513 weston_desktop_surface_get_implementation_data(dsurface);
514
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200515 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200516 weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface);
517}
518
519static void
520weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel)
521{
522 uint32_t *s;
523 struct wl_array states;
524
525 wl_array_init(&states);
526 if (toplevel->requested_state.maximized) {
527 s = wl_array_add(&states, sizeof(uint32_t));
528 *s = ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED;
529 }
530 if (toplevel->requested_state.fullscreen) {
531 s = wl_array_add(&states, sizeof(uint32_t));
532 *s = ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN;
533 }
534 if (toplevel->requested_state.resizing) {
535 s = wl_array_add(&states, sizeof(uint32_t));
536 *s = ZXDG_TOPLEVEL_V6_STATE_RESIZING;
537 }
538 if (toplevel->requested_state.activated) {
539 s = wl_array_add(&states, sizeof(uint32_t));
540 *s = ZXDG_TOPLEVEL_V6_STATE_ACTIVATED;
541 }
542
543 zxdg_toplevel_v6_send_configure(toplevel->resource,
544 toplevel->requested_size.width,
545 toplevel->requested_size.height,
546 &states);
547
548 wl_array_release(&states);
549};
550
551static void
552weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface,
553 void *user_data, bool maximized)
554{
555 struct weston_desktop_xdg_toplevel *toplevel = user_data;
556
557 if (toplevel->state.maximized == maximized)
558 return;
559
560 toplevel->requested_state.maximized = maximized;
561 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
562}
563
564static void
565weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface,
566 void *user_data, bool fullscreen)
567{
568 struct weston_desktop_xdg_toplevel *toplevel = user_data;
569
570 if (toplevel->state.fullscreen == fullscreen)
571 return;
572
573 toplevel->requested_state.fullscreen = fullscreen;
574 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
575}
576
577static void
578weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface,
579 void *user_data, bool resizing)
580{
581 struct weston_desktop_xdg_toplevel *toplevel = user_data;
582
583 if (toplevel->state.resizing == resizing)
584 return;
585
586 toplevel->requested_state.resizing = resizing;
587 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
588}
589
590static void
591weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface,
592 void *user_data, bool activated)
593{
594 struct weston_desktop_xdg_toplevel *toplevel = user_data;
595
596 if (toplevel->state.activated == activated)
597 return;
598
599 toplevel->requested_state.activated = activated;
600 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
601}
602
603static void
604weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface,
605 void *user_data,
606 int32_t width, int32_t height)
607{
608 struct weston_desktop_xdg_toplevel *toplevel = user_data;
609 struct weston_surface *wsurface =
610 weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
611
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200612 toplevel->requested_size.width = width;
613 toplevel->requested_size.height = height;
Quentin Glidica56b0532016-09-13 10:05:58 +0200614
615 if ((wsurface->width == width && wsurface->height == height) ||
616 (width == 0 && height == 0))
617 return;
618
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200619 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
620}
621
622static void
623weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel,
Quentin Glidiccba26e72016-08-15 12:20:22 +0200624 int32_t sx, int32_t sy)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200625{
626 struct weston_surface *wsurface =
627 weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
628 bool reconfigure = false;
629
Quentin Glidiccba26e72016-08-15 12:20:22 +0200630 if (!wsurface->buffer_ref.buffer && !toplevel->added) {
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200631 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200632 return;
633 }
Quentin Glidiccba26e72016-08-15 12:20:22 +0200634 if (!wsurface->buffer_ref.buffer)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200635 return;
636
637 if (toplevel->next_state.maximized || toplevel->next_state.fullscreen)
638 reconfigure =
639 ( ( toplevel->requested_size.width != wsurface->width ) ||
640 ( toplevel->requested_size.height != wsurface->height ) );
641
642 if (reconfigure) {
643 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
644 } else {
645 toplevel->state = toplevel->next_state;
646 toplevel->min_size = toplevel->next_min_size;
647 toplevel->max_size = toplevel->next_max_size;
648
649 weston_desktop_api_committed(toplevel->base.desktop,
650 toplevel->base.desktop_surface,
651 sx, sy);
652 }
653}
654
655static void
656weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel)
657{
658 zxdg_toplevel_v6_send_close(toplevel->resource);
659}
660
661static bool
662weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface,
663 void *user_data)
664{
665 struct weston_desktop_xdg_toplevel *toplevel = user_data;
666
667 return toplevel->state.maximized;
668}
669
670static bool
671weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface,
672 void *user_data)
673{
674 struct weston_desktop_xdg_toplevel *toplevel = user_data;
675
676 return toplevel->state.fullscreen;
677}
678
679static bool
680weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface,
681 void *user_data)
682{
683 struct weston_desktop_xdg_toplevel *toplevel = user_data;
684
685 return toplevel->state.resizing;
686}
687
688static bool
689weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface,
690 void *user_data)
691{
692 struct weston_desktop_xdg_toplevel *toplevel = user_data;
693
694 return toplevel->state.activated;
695}
696
697static void
698weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel)
699{
700 if (toplevel->added)
701 weston_desktop_api_surface_removed(toplevel->base.desktop,
702 toplevel->base.desktop_surface);
703}
704
705static void
706weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource)
707{
708 struct weston_desktop_surface *dsurface =
709 wl_resource_get_user_data(resource);
710
711 if (dsurface != NULL)
712 weston_desktop_surface_resource_destroy(resource);
713}
714
715static const struct zxdg_toplevel_v6_interface weston_desktop_xdg_toplevel_implementation = {
716 .destroy = weston_desktop_destroy_request,
717 .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent,
718 .set_title = weston_desktop_xdg_toplevel_protocol_set_title,
719 .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id,
720 .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu,
721 .move = weston_desktop_xdg_toplevel_protocol_move,
722 .resize = weston_desktop_xdg_toplevel_protocol_resize,
723 .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size,
724 .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size,
725 .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized,
726 .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized,
727 .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen,
728 .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen,
729 .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized,
730};
731
732static void
733weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client,
734 struct wl_resource *resource,
735 struct wl_resource *seat_resource,
736 uint32_t serial)
737{
738 struct weston_desktop_surface *dsurface =
739 wl_resource_get_user_data(resource);
740 struct weston_desktop_xdg_popup *popup =
741 weston_desktop_surface_get_implementation_data(dsurface);
742 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
743 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
744 struct weston_desktop_surface *topmost;
745 bool parent_is_toplevel =
746 popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
747
748 if (popup->committed) {
749 wl_resource_post_error(popup->resource,
750 ZXDG_POPUP_V6_ERROR_INVALID_GRAB,
751 "xdg_popup already is mapped");
752 return;
753 }
754
755 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
756 if ((topmost == NULL && !parent_is_toplevel) ||
757 (topmost != NULL && topmost != popup->parent->desktop_surface)) {
758 struct weston_desktop_client *client =
759 weston_desktop_surface_get_client(dsurface);
760 struct wl_resource *client_resource =
761 weston_desktop_client_get_resource(client);
762
763 wl_resource_post_error(client_resource,
764 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
765 "xdg_popup was not created on the topmost popup");
766 return;
767 }
768
769 popup->seat = seat;
770 weston_desktop_surface_popup_grab(popup->base.desktop_surface,
771 popup->seat, serial);
772}
773
774static void
775weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup)
776{
777 zxdg_popup_v6_send_configure(popup->resource,
778 popup->geometry.x,
779 popup->geometry.y,
780 popup->geometry.width,
781 popup->geometry.height);
782}
783
784static void
785weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
786 void *user_data);
787
788static void
789weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup)
790{
791 if (!popup->committed)
792 weston_desktop_xdg_surface_schedule_configure(&popup->base);
793 popup->committed = true;
794 weston_desktop_xdg_popup_update_position(popup->base.desktop_surface,
795 popup);
796}
797
798static void
799weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
800 void *user_data)
801{
802}
803
804static void
805weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup)
806{
807 zxdg_popup_v6_send_popup_done(popup->resource);
808}
809
810static void
811weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup)
812{
813 struct weston_desktop_surface *topmost;
814 struct weston_desktop_client *client =
815 weston_desktop_surface_get_client(popup->base.desktop_surface);
816
817 if (!weston_desktop_surface_get_grab(popup->base.desktop_surface))
818 return;
819
820 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
821 if (topmost != popup->base.desktop_surface) {
822 struct wl_resource *client_resource =
823 weston_desktop_client_get_resource(client);
824
825 wl_resource_post_error(client_resource,
826 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
827 "xdg_popup was destroyed while it was not the topmost popup.");
828 }
829
830 weston_desktop_surface_popup_ungrab(popup->base.desktop_surface,
831 popup->seat);
832}
833
834static void
835weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource)
836{
837 struct weston_desktop_surface *dsurface =
838 wl_resource_get_user_data(resource);
839
840 if (dsurface != NULL)
841 weston_desktop_surface_resource_destroy(resource);
842}
843
844static const struct zxdg_popup_v6_interface weston_desktop_xdg_popup_implementation = {
845 .destroy = weston_desktop_destroy_request,
846 .grab = weston_desktop_xdg_popup_protocol_grab,
847};
848
849static void
850weston_desktop_xdg_surface_send_configure(void *user_data)
851{
852 struct weston_desktop_xdg_surface *surface = user_data;
853
854 surface->configure_idle = NULL;
855 surface->configure_serial =
856 wl_display_next_serial(weston_desktop_get_display(surface->desktop));
857
858 switch (surface->role) {
859 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
860 assert(0 && "not reached");
861 break;
862 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
863 weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface);
864 break;
865 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
866 weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface);
867 break;
868 }
869
870 zxdg_surface_v6_send_configure(surface->resource, surface->configure_serial);
871}
872
873static void
874weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface)
875{
876 struct wl_display *display = weston_desktop_get_display(surface->desktop);
877 struct wl_event_loop *loop = wl_display_get_event_loop(display);
878
879 if (surface->configure_idle != NULL)
880 return;
881 surface->configure_idle =
882 wl_event_loop_add_idle(loop,
883 weston_desktop_xdg_surface_send_configure,
884 surface);
885}
886
887static void
888weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client,
889 struct wl_resource *resource,
890 uint32_t id)
891{
892 struct weston_desktop_surface *dsurface =
893 wl_resource_get_user_data(resource);
894 struct weston_surface *wsurface =
895 weston_desktop_surface_get_surface(dsurface);
896 struct weston_desktop_xdg_toplevel *toplevel =
897 weston_desktop_surface_get_implementation_data(dsurface);
898
899 if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role,
900 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
901 return;
902
903 toplevel->resource =
904 weston_desktop_surface_add_resource(toplevel->base.desktop_surface,
905 &zxdg_toplevel_v6_interface,
906 &weston_desktop_xdg_toplevel_implementation,
907 id, weston_desktop_xdg_toplevel_resource_destroy);
908 if (toplevel->resource == NULL)
909 return;
910
911 toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
912}
913
914static void
915weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client,
916 struct wl_resource *resource,
917 uint32_t id,
918 struct wl_resource *parent_resource,
919 struct wl_resource *positioner_resource)
920{
921 struct weston_desktop_surface *dsurface =
922 wl_resource_get_user_data(resource);
923 struct weston_surface *wsurface =
924 weston_desktop_surface_get_surface(dsurface);
925 struct weston_desktop_xdg_popup *popup =
926 weston_desktop_surface_get_implementation_data(dsurface);
927 struct weston_desktop_surface *parent_surface =
928 wl_resource_get_user_data(parent_resource);
929 struct weston_desktop_xdg_surface *parent =
930 weston_desktop_surface_get_implementation_data(parent_surface);
931 struct weston_desktop_xdg_positioner *positioner =
932 wl_resource_get_user_data(positioner_resource);
933
934 if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
935 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
936 return;
937
938 popup->resource =
939 weston_desktop_surface_add_resource(popup->base.desktop_surface,
940 &zxdg_popup_v6_interface,
941 &weston_desktop_xdg_popup_implementation,
942 id, weston_desktop_xdg_popup_resource_destroy);
943 if (popup->resource == NULL)
944 return;
945
946 popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
947 popup->parent = parent;
948
949 popup->geometry =
950 weston_desktop_xdg_positioner_get_geometry(positioner,
951 dsurface,
952 parent_surface);
953
954 weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
955 parent_surface,
956 popup->geometry.x,
957 popup->geometry.y,
958 true);
959}
960
961static bool
962weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
963{
964 struct weston_surface *wsurface =
965 weston_desktop_surface_get_surface(surface->desktop_surface);
966 const char *role;
967
968 role = weston_surface_get_role(wsurface);
969 if (role != NULL &&
970 (role == weston_desktop_xdg_toplevel_role ||
971 role == weston_desktop_xdg_popup_role))
972 return true;
973
974 wl_resource_post_error(surface->resource,
975 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
976 "xdg_surface must have a role");
977 return false;
978}
979
980static void
981weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
982 struct wl_resource *resource,
983 int32_t x, int32_t y,
984 int32_t width, int32_t height)
985{
986 struct weston_desktop_surface *dsurface =
987 wl_resource_get_user_data(resource);
988 struct weston_desktop_xdg_surface *surface =
989 weston_desktop_surface_get_implementation_data(dsurface);
990
991 if (!weston_desktop_xdg_surface_check_role(surface))
992 return;
993
994 surface->has_next_geometry = true;
995 surface->next_geometry.x = x;
996 surface->next_geometry.y = y;
997 surface->next_geometry.width = width;
998 surface->next_geometry.height = height;
999}
1000
1001static void
1002weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
1003 struct wl_resource *resource,
1004 uint32_t serial)
1005{
1006 struct weston_desktop_surface *dsurface =
1007 wl_resource_get_user_data(resource);
1008 struct weston_desktop_xdg_surface *surface =
1009 weston_desktop_surface_get_implementation_data(dsurface);
1010
1011 if (!weston_desktop_xdg_surface_check_role(surface))
1012 return;
1013
1014 if (surface->configure_serial != serial)
1015 return;
1016
1017 surface->configured = true;
1018
1019 switch (surface->role) {
1020 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1021 assert(0 && "not reached");
1022 break;
1023 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1024 weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface);
1025 break;
1026 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1027 break;
1028 }
1029}
1030
1031static void
1032weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
1033 uint32_t serial, void *user_data)
1034{
1035 struct weston_desktop_client *client =
1036 weston_desktop_surface_get_client(dsurface);
1037
1038 zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client),
1039 serial);
1040}
1041
1042static void
1043weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +02001044 void *user_data,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001045 int32_t sx, int32_t sy)
1046{
1047 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidiccba26e72016-08-15 12:20:22 +02001048 struct weston_surface *wsurface =
1049 weston_desktop_surface_get_surface (dsurface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001050
Quentin Glidiccba26e72016-08-15 12:20:22 +02001051 if (wsurface->buffer_ref.buffer && !surface->configured) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001052 wl_resource_post_error(surface->resource,
1053 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1054 "xdg_surface has never been configured");
1055 return;
1056 }
1057
1058 if (surface->has_next_geometry) {
1059 surface->has_next_geometry = false;
1060 weston_desktop_surface_set_geometry(surface->desktop_surface,
1061 surface->next_geometry);
1062 }
1063
1064 switch (surface->role) {
1065 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1066 wl_resource_post_error(surface->resource,
1067 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1068 "xdg_surface must have a role");
1069 break;
1070 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidiccba26e72016-08-15 12:20:22 +02001071 weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001072 break;
1073 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1074 weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1075 break;
1076 }
1077}
1078
1079static void
1080weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1081 void *user_data)
1082{
1083 struct weston_desktop_xdg_surface *surface = user_data;
1084
1085 switch (surface->role) {
1086 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1087 assert(0 && "not reached");
1088 break;
1089 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1090 weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1091 break;
1092 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1093 weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1094 break;
1095 }
1096}
1097
1098static void
1099weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1100 void *user_data)
1101{
1102 struct weston_desktop_xdg_surface *surface = user_data;
1103
1104 switch (surface->role) {
1105 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1106 break;
1107 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1108 weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1109 break;
1110 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1111 weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1112 break;
1113 }
1114
1115 if (surface->configure_idle != NULL)
1116 wl_event_source_remove(surface->configure_idle);
1117
1118 free(surface);
1119}
1120
1121static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = {
1122 .destroy = weston_desktop_destroy_request,
1123 .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel,
1124 .get_popup = weston_desktop_xdg_surface_protocol_get_popup,
1125 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1126 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
1127};
1128
1129static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1130 /* These are used for toplevel only */
1131 .set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1132 .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1133 .set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1134 .set_activated = weston_desktop_xdg_toplevel_set_activated,
1135 .set_size = weston_desktop_xdg_toplevel_set_size,
1136
1137 .get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1138 .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1139 .get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1140 .get_activated = weston_desktop_xdg_toplevel_get_activated,
1141
1142 /* These are used for popup only */
1143 .update_position = weston_desktop_xdg_popup_update_position,
1144
1145 /* Common API */
1146 .committed = weston_desktop_xdg_surface_committed,
1147 .ping = weston_desktop_xdg_surface_ping,
1148 .close = weston_desktop_xdg_surface_close,
1149
1150 .destroy = weston_desktop_xdg_surface_destroy,
1151};
1152
1153static void
1154weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1155 struct wl_resource *resource,
1156 uint32_t id)
1157{
1158 struct weston_desktop_client *client =
1159 wl_resource_get_user_data(resource);
1160 struct weston_desktop_xdg_positioner *positioner;
1161
1162 positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1163 if (positioner == NULL) {
1164 wl_client_post_no_memory(wl_client);
1165 return;
1166 }
1167
1168 positioner->client = client;
1169 positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1170
1171 positioner->resource =
1172 wl_resource_create(wl_client,
1173 &zxdg_positioner_v6_interface,
1174 wl_resource_get_version(resource), id);
1175 if (positioner->resource == NULL) {
1176 wl_client_post_no_memory(wl_client);
1177 free(positioner);
1178 return;
1179 }
1180 wl_resource_set_implementation(positioner->resource,
1181 &weston_desktop_xdg_positioner_implementation,
1182 positioner, weston_desktop_xdg_positioner_destroy);
1183}
1184
1185static void
1186weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1187{
1188 struct weston_desktop_surface *dsurface =
1189 wl_resource_get_user_data(resource);
1190
1191 if (dsurface != NULL)
1192 weston_desktop_surface_resource_destroy(resource);
1193}
1194
1195static void
1196weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1197 struct wl_resource *resource,
1198 uint32_t id,
1199 struct wl_resource *surface_resource)
1200{
1201 struct weston_desktop_client *client =
1202 wl_resource_get_user_data(resource);
1203 struct weston_surface *wsurface =
1204 wl_resource_get_user_data(surface_resource);
1205 struct weston_desktop_xdg_surface *surface;
1206
1207 surface = zalloc(weston_desktop_surface_role_biggest_size);
1208 if (surface == NULL) {
1209 wl_client_post_no_memory(wl_client);
1210 return;
1211 }
1212
1213 surface->desktop = weston_desktop_client_get_desktop(client);
1214 surface->surface = wsurface;
1215
1216 surface->desktop_surface =
1217 weston_desktop_surface_create(surface->desktop, client,
1218 surface->surface,
1219 &weston_desktop_xdg_surface_internal_implementation,
1220 surface);
1221 if (surface->desktop_surface == NULL) {
1222 free(surface);
1223 return;
1224 }
1225
1226 surface->resource =
1227 weston_desktop_surface_add_resource(surface->desktop_surface,
1228 &zxdg_surface_v6_interface,
1229 &weston_desktop_xdg_surface_implementation,
1230 id, weston_desktop_xdg_surface_resource_destroy);
1231 if (surface->resource == NULL)
1232 return;
1233
1234 if (wsurface->buffer_ref.buffer != NULL) {
1235 wl_resource_post_error(surface->resource,
1236 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1237 "xdg_surface must not have a buffer at creation");
1238 return;
1239 }
1240}
1241
1242static void
1243weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1244 struct wl_resource *resource,
1245 uint32_t serial)
1246{
1247 struct weston_desktop_client *client =
1248 wl_resource_get_user_data(resource);
1249
1250 weston_desktop_client_pong(client, serial);
1251}
1252
1253static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = {
1254 .destroy = weston_desktop_destroy_request,
1255 .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1256 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1257 .pong = weston_desktop_xdg_shell_protocol_pong,
1258};
1259
1260static void
1261weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1262 uint32_t version, uint32_t id)
1263{
1264 struct weston_desktop *desktop = data;
1265
1266 weston_desktop_client_create(desktop, client, NULL,
1267 &zxdg_shell_v6_interface,
1268 &weston_desktop_xdg_shell_implementation,
1269 version, id);
1270}
1271
1272struct wl_global *
1273weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display)
1274{
1275 return wl_global_create(display, &zxdg_shell_v6_interface,
1276 WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1277 weston_desktop_xdg_shell_bind);
1278}