blob: 7d0bd8e46bbbcc3722115c3a02b56d4a3fddde6b [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"
Daniel Stone7dbb0e12016-11-24 15:30:41 +000036#include "xdg-shell-unstable-v6-server-protocol.h"
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +020037
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
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +0200934 /* Checking whether the size and anchor rect both have a positive size
935 * is enough to verify both have been correctly set */
936 if (positioner->size.width == 0 || positioner->anchor_rect.width == 0) {
937 wl_resource_post_error(resource,
938 ZXDG_SHELL_V6_ERROR_INVALID_POSITIONER,
939 "positioner object is not complete");
940 return;
941 }
942
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200943 if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
944 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
945 return;
946
947 popup->resource =
948 weston_desktop_surface_add_resource(popup->base.desktop_surface,
949 &zxdg_popup_v6_interface,
950 &weston_desktop_xdg_popup_implementation,
951 id, weston_desktop_xdg_popup_resource_destroy);
952 if (popup->resource == NULL)
953 return;
954
955 popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
956 popup->parent = parent;
957
958 popup->geometry =
959 weston_desktop_xdg_positioner_get_geometry(positioner,
960 dsurface,
961 parent_surface);
962
963 weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
964 parent_surface,
965 popup->geometry.x,
966 popup->geometry.y,
967 true);
968}
969
970static bool
971weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
972{
973 struct weston_surface *wsurface =
974 weston_desktop_surface_get_surface(surface->desktop_surface);
975 const char *role;
976
977 role = weston_surface_get_role(wsurface);
978 if (role != NULL &&
979 (role == weston_desktop_xdg_toplevel_role ||
980 role == weston_desktop_xdg_popup_role))
981 return true;
982
983 wl_resource_post_error(surface->resource,
984 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
985 "xdg_surface must have a role");
986 return false;
987}
988
989static void
990weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
991 struct wl_resource *resource,
992 int32_t x, int32_t y,
993 int32_t width, int32_t height)
994{
995 struct weston_desktop_surface *dsurface =
996 wl_resource_get_user_data(resource);
997 struct weston_desktop_xdg_surface *surface =
998 weston_desktop_surface_get_implementation_data(dsurface);
999
1000 if (!weston_desktop_xdg_surface_check_role(surface))
1001 return;
1002
1003 surface->has_next_geometry = true;
1004 surface->next_geometry.x = x;
1005 surface->next_geometry.y = y;
1006 surface->next_geometry.width = width;
1007 surface->next_geometry.height = height;
1008}
1009
1010static void
1011weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
1012 struct wl_resource *resource,
1013 uint32_t serial)
1014{
1015 struct weston_desktop_surface *dsurface =
1016 wl_resource_get_user_data(resource);
1017 struct weston_desktop_xdg_surface *surface =
1018 weston_desktop_surface_get_implementation_data(dsurface);
1019
1020 if (!weston_desktop_xdg_surface_check_role(surface))
1021 return;
1022
1023 if (surface->configure_serial != serial)
1024 return;
1025
1026 surface->configured = true;
1027
1028 switch (surface->role) {
1029 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1030 assert(0 && "not reached");
1031 break;
1032 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1033 weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface);
1034 break;
1035 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1036 break;
1037 }
1038}
1039
1040static void
1041weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
1042 uint32_t serial, void *user_data)
1043{
1044 struct weston_desktop_client *client =
1045 weston_desktop_surface_get_client(dsurface);
1046
1047 zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client),
1048 serial);
1049}
1050
1051static void
1052weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +02001053 void *user_data,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001054 int32_t sx, int32_t sy)
1055{
1056 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidiccba26e72016-08-15 12:20:22 +02001057 struct weston_surface *wsurface =
1058 weston_desktop_surface_get_surface (dsurface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001059
Quentin Glidiccba26e72016-08-15 12:20:22 +02001060 if (wsurface->buffer_ref.buffer && !surface->configured) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001061 wl_resource_post_error(surface->resource,
1062 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1063 "xdg_surface has never been configured");
1064 return;
1065 }
1066
1067 if (surface->has_next_geometry) {
1068 surface->has_next_geometry = false;
1069 weston_desktop_surface_set_geometry(surface->desktop_surface,
1070 surface->next_geometry);
1071 }
1072
1073 switch (surface->role) {
1074 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1075 wl_resource_post_error(surface->resource,
1076 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1077 "xdg_surface must have a role");
1078 break;
1079 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidiccba26e72016-08-15 12:20:22 +02001080 weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001081 break;
1082 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1083 weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1084 break;
1085 }
1086}
1087
1088static void
1089weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1090 void *user_data)
1091{
1092 struct weston_desktop_xdg_surface *surface = user_data;
1093
1094 switch (surface->role) {
1095 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1096 assert(0 && "not reached");
1097 break;
1098 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1099 weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1100 break;
1101 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1102 weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1103 break;
1104 }
1105}
1106
1107static void
1108weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1109 void *user_data)
1110{
1111 struct weston_desktop_xdg_surface *surface = user_data;
1112
1113 switch (surface->role) {
1114 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1115 break;
1116 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1117 weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1118 break;
1119 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1120 weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1121 break;
1122 }
1123
1124 if (surface->configure_idle != NULL)
1125 wl_event_source_remove(surface->configure_idle);
1126
1127 free(surface);
1128}
1129
1130static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = {
1131 .destroy = weston_desktop_destroy_request,
1132 .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel,
1133 .get_popup = weston_desktop_xdg_surface_protocol_get_popup,
1134 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1135 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
1136};
1137
1138static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1139 /* These are used for toplevel only */
1140 .set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1141 .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1142 .set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1143 .set_activated = weston_desktop_xdg_toplevel_set_activated,
1144 .set_size = weston_desktop_xdg_toplevel_set_size,
1145
1146 .get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1147 .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1148 .get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1149 .get_activated = weston_desktop_xdg_toplevel_get_activated,
1150
1151 /* These are used for popup only */
1152 .update_position = weston_desktop_xdg_popup_update_position,
1153
1154 /* Common API */
1155 .committed = weston_desktop_xdg_surface_committed,
1156 .ping = weston_desktop_xdg_surface_ping,
1157 .close = weston_desktop_xdg_surface_close,
1158
1159 .destroy = weston_desktop_xdg_surface_destroy,
1160};
1161
1162static void
1163weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1164 struct wl_resource *resource,
1165 uint32_t id)
1166{
1167 struct weston_desktop_client *client =
1168 wl_resource_get_user_data(resource);
1169 struct weston_desktop_xdg_positioner *positioner;
1170
1171 positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1172 if (positioner == NULL) {
1173 wl_client_post_no_memory(wl_client);
1174 return;
1175 }
1176
1177 positioner->client = client;
1178 positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1179
1180 positioner->resource =
1181 wl_resource_create(wl_client,
1182 &zxdg_positioner_v6_interface,
1183 wl_resource_get_version(resource), id);
1184 if (positioner->resource == NULL) {
1185 wl_client_post_no_memory(wl_client);
1186 free(positioner);
1187 return;
1188 }
1189 wl_resource_set_implementation(positioner->resource,
1190 &weston_desktop_xdg_positioner_implementation,
1191 positioner, weston_desktop_xdg_positioner_destroy);
1192}
1193
1194static void
1195weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1196{
1197 struct weston_desktop_surface *dsurface =
1198 wl_resource_get_user_data(resource);
1199
1200 if (dsurface != NULL)
1201 weston_desktop_surface_resource_destroy(resource);
1202}
1203
1204static void
1205weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1206 struct wl_resource *resource,
1207 uint32_t id,
1208 struct wl_resource *surface_resource)
1209{
1210 struct weston_desktop_client *client =
1211 wl_resource_get_user_data(resource);
1212 struct weston_surface *wsurface =
1213 wl_resource_get_user_data(surface_resource);
1214 struct weston_desktop_xdg_surface *surface;
1215
1216 surface = zalloc(weston_desktop_surface_role_biggest_size);
1217 if (surface == NULL) {
1218 wl_client_post_no_memory(wl_client);
1219 return;
1220 }
1221
1222 surface->desktop = weston_desktop_client_get_desktop(client);
1223 surface->surface = wsurface;
1224
1225 surface->desktop_surface =
1226 weston_desktop_surface_create(surface->desktop, client,
1227 surface->surface,
1228 &weston_desktop_xdg_surface_internal_implementation,
1229 surface);
1230 if (surface->desktop_surface == NULL) {
1231 free(surface);
1232 return;
1233 }
1234
1235 surface->resource =
1236 weston_desktop_surface_add_resource(surface->desktop_surface,
1237 &zxdg_surface_v6_interface,
1238 &weston_desktop_xdg_surface_implementation,
1239 id, weston_desktop_xdg_surface_resource_destroy);
1240 if (surface->resource == NULL)
1241 return;
1242
1243 if (wsurface->buffer_ref.buffer != NULL) {
1244 wl_resource_post_error(surface->resource,
1245 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1246 "xdg_surface must not have a buffer at creation");
1247 return;
1248 }
1249}
1250
1251static void
1252weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1253 struct wl_resource *resource,
1254 uint32_t serial)
1255{
1256 struct weston_desktop_client *client =
1257 wl_resource_get_user_data(resource);
1258
1259 weston_desktop_client_pong(client, serial);
1260}
1261
1262static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = {
1263 .destroy = weston_desktop_destroy_request,
1264 .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1265 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1266 .pong = weston_desktop_xdg_shell_protocol_pong,
1267};
1268
1269static void
1270weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1271 uint32_t version, uint32_t id)
1272{
1273 struct weston_desktop *desktop = data;
1274
1275 weston_desktop_client_create(desktop, client, NULL,
1276 &zxdg_shell_v6_interface,
1277 &weston_desktop_xdg_shell_implementation,
1278 version, id);
1279}
1280
1281struct wl_global *
1282weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display)
1283{
1284 return wl_global_create(display, &zxdg_shell_v6_interface,
1285 WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1286 weston_desktop_xdg_shell_bind);
1287}