blob: 1344dda02db5163be2b39406024cb56c5dc130d6 [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;
Quentin Glidic749637a2017-07-18 12:59:14 +020072 struct wl_list configure_list; /* weston_desktop_xdg_surface_configure::link */
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +020073
74 bool has_next_geometry;
75 struct weston_geometry next_geometry;
76
77 enum weston_desktop_xdg_surface_role role;
78};
79
Quentin Glidic749637a2017-07-18 12:59:14 +020080struct weston_desktop_xdg_surface_configure {
81 struct wl_list link; /* weston_desktop_xdg_surface::configure_list */
82 uint32_t serial;
83};
84
Quentin Glidic19d1f6e2017-07-12 09:42:57 +020085struct weston_desktop_xdg_toplevel_state {
86 bool maximized;
87 bool fullscreen;
88 bool resizing;
89 bool activated;
90};
91
Quentin Glidic749637a2017-07-18 12:59:14 +020092struct weston_desktop_xdg_toplevel_configure {
93 struct weston_desktop_xdg_surface_configure base;
94 struct weston_desktop_xdg_toplevel_state state;
95 struct weston_size size;
96};
97
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +020098struct weston_desktop_xdg_toplevel {
99 struct weston_desktop_xdg_surface base;
100
101 struct wl_resource *resource;
102 bool added;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200103 struct {
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200104 struct weston_desktop_xdg_toplevel_state state;
105 struct weston_size size;
106 } pending;
107 struct {
108 struct weston_desktop_xdg_toplevel_state state;
Quentin Glidicac394a12017-07-12 09:45:43 +0200109 struct weston_size size;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200110 struct weston_size min_size, max_size;
111 } next;
112 struct {
113 struct weston_desktop_xdg_toplevel_state state;
114 struct weston_size min_size, max_size;
115 } current;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200116};
117
118struct weston_desktop_xdg_popup {
119 struct weston_desktop_xdg_surface base;
120
121 struct wl_resource *resource;
122 bool committed;
123 struct weston_desktop_xdg_surface *parent;
124 struct weston_desktop_seat *seat;
125 struct weston_geometry geometry;
126};
127
128#define weston_desktop_surface_role_biggest_size (sizeof(struct weston_desktop_xdg_toplevel))
Quentin Glidic749637a2017-07-18 12:59:14 +0200129#define weston_desktop_surface_configure_biggest_size (sizeof(struct weston_desktop_xdg_toplevel))
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200130
131
132static struct weston_geometry
133weston_desktop_xdg_positioner_get_geometry(struct weston_desktop_xdg_positioner *positioner,
134 struct weston_desktop_surface *dsurface,
135 struct weston_desktop_surface *parent)
136{
137 struct weston_geometry geometry = {
138 .x = positioner->offset.x,
139 .y = positioner->offset.y,
140 .width = positioner->size.width,
141 .height = positioner->size.height,
142 };
143
144 if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP)
145 geometry.y += positioner->anchor_rect.y;
146 else if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)
147 geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height;
148 else
149 geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height / 2;
150
151 if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT)
152 geometry.x += positioner->anchor_rect.x;
153 else if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT)
154 geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width;
155 else
156 geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width / 2;
157
158 if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP)
159 geometry.y -= geometry.height;
160 else if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)
161 geometry.y = geometry.y;
162 else
163 geometry.y -= geometry.height / 2;
164
165 if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT)
166 geometry.x -= geometry.width;
167 else if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT)
168 geometry.x = geometry.x;
169 else
170 geometry.x -= geometry.width / 2;
171
172 if (positioner->constraint_adjustment == ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_NONE)
173 return geometry;
174
175 /* TODO: add compositor policy configuration and the code here */
176
177 return geometry;
178}
179
180static void
181weston_desktop_xdg_positioner_protocol_set_size(struct wl_client *wl_client,
182 struct wl_resource *resource,
183 int32_t width, int32_t height)
184{
185 struct weston_desktop_xdg_positioner *positioner =
186 wl_resource_get_user_data(resource);
187
188 if (width < 1 || height < 1) {
189 wl_resource_post_error(resource,
190 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
191 "width and height must be positives and non-zero");
192 return;
193 }
194
195 positioner->size.width = width;
196 positioner->size.height = height;
197}
198
199static void
200weston_desktop_xdg_positioner_protocol_set_anchor_rect(struct wl_client *wl_client,
201 struct wl_resource *resource,
202 int32_t x, int32_t y,
203 int32_t width, int32_t height)
204{
205 struct weston_desktop_xdg_positioner *positioner =
206 wl_resource_get_user_data(resource);
207
208 if (width < 1 || height < 1) {
209 wl_resource_post_error(resource,
210 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
211 "width and height must be positives and non-zero");
212 return;
213 }
214
215 positioner->anchor_rect.x = x;
216 positioner->anchor_rect.y = y;
217 positioner->anchor_rect.width = width;
218 positioner->anchor_rect.height = height;
219}
220
221static void
222weston_desktop_xdg_positioner_protocol_set_anchor(struct wl_client *wl_client,
223 struct wl_resource *resource,
224 enum zxdg_positioner_v6_anchor anchor)
225{
226 struct weston_desktop_xdg_positioner *positioner =
227 wl_resource_get_user_data(resource);
228
229 if (((anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP ) &&
230 (anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)) ||
231 ((anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT) &&
232 (anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT))) {
233 wl_resource_post_error(resource,
234 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
235 "same-axis values are not allowed");
236 return;
237 }
238
239 positioner->anchor = anchor;
240}
241
242static void
243weston_desktop_xdg_positioner_protocol_set_gravity(struct wl_client *wl_client,
244 struct wl_resource *resource,
245 enum zxdg_positioner_v6_gravity gravity)
246{
247 struct weston_desktop_xdg_positioner *positioner =
248 wl_resource_get_user_data(resource);
249
250 if (((gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP) &&
251 (gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)) ||
252 ((gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT) &&
253 (gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT))) {
254 wl_resource_post_error(resource,
255 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
256 "same-axis values are not allowed");
257 return;
258 }
259
260 positioner->gravity = gravity;
261}
262
263static void
264weston_desktop_xdg_positioner_protocol_set_constraint_adjustment(struct wl_client *wl_client,
265 struct wl_resource *resource,
266 enum zxdg_positioner_v6_constraint_adjustment constraint_adjustment)
267{
268 struct weston_desktop_xdg_positioner *positioner =
269 wl_resource_get_user_data(resource);
270
271 positioner->constraint_adjustment = constraint_adjustment;
272}
273
274static void
275weston_desktop_xdg_positioner_protocol_set_offset(struct wl_client *wl_client,
276 struct wl_resource *resource,
277 int32_t x, int32_t y)
278{
279 struct weston_desktop_xdg_positioner *positioner =
280 wl_resource_get_user_data(resource);
281
282 positioner->offset.x = x;
283 positioner->offset.y = y;
284}
285
286static void
287weston_desktop_xdg_positioner_destroy(struct wl_resource *resource)
288{
289 struct weston_desktop_xdg_positioner *positioner =
290 wl_resource_get_user_data(resource);
291
292 free(positioner);
293}
294
295static const struct zxdg_positioner_v6_interface weston_desktop_xdg_positioner_implementation = {
296 .destroy = weston_desktop_destroy_request,
297 .set_size = weston_desktop_xdg_positioner_protocol_set_size,
298 .set_anchor_rect = weston_desktop_xdg_positioner_protocol_set_anchor_rect,
299 .set_anchor = weston_desktop_xdg_positioner_protocol_set_anchor,
300 .set_gravity = weston_desktop_xdg_positioner_protocol_set_gravity,
301 .set_constraint_adjustment = weston_desktop_xdg_positioner_protocol_set_constraint_adjustment,
302 .set_offset = weston_desktop_xdg_positioner_protocol_set_offset,
303};
304
305static void
Quentin Glidicd51f8262017-04-13 20:25:27 +0200306weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface,
307 bool force);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200308
309static void
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200310weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel *toplevel)
311{
312 if (toplevel->added)
313 return;
314
315 weston_desktop_api_surface_added(toplevel->base.desktop,
316 toplevel->base.desktop_surface);
Quentin Glidicd51f8262017-04-13 20:25:27 +0200317 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, true);
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200318 toplevel->added = true;
319}
320
321static void
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200322weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client,
323 struct wl_resource *resource,
324 struct wl_resource *parent_resource)
325{
326 struct weston_desktop_surface *dsurface =
327 wl_resource_get_user_data(resource);
328 struct weston_desktop_xdg_toplevel *toplevel =
329 weston_desktop_surface_get_implementation_data(dsurface);
330 struct weston_desktop_surface *parent = NULL;
331
332 if (parent_resource != NULL)
333 parent = wl_resource_get_user_data(parent_resource);
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200334
335 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200336 weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent);
337}
338
339static void
340weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client,
341 struct wl_resource *resource,
342 const char *title)
343{
344 struct weston_desktop_surface *toplevel =
345 wl_resource_get_user_data(resource);
346
347 weston_desktop_surface_set_title(toplevel, title);
348}
349
350static void
351weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client,
352 struct wl_resource *resource,
353 const char *app_id)
354{
355 struct weston_desktop_surface *toplevel =
356 wl_resource_get_user_data(resource);
357
358 weston_desktop_surface_set_app_id(toplevel, app_id);
359}
360
361static void
362weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client,
363 struct wl_resource *resource,
364 struct wl_resource *seat_resource,
365 uint32_t serial,
366 int32_t x, int32_t y)
367{
368 struct weston_desktop_surface *dsurface =
369 wl_resource_get_user_data(resource);
370 struct weston_seat *seat =
371 wl_resource_get_user_data(seat_resource);
372 struct weston_desktop_xdg_toplevel *toplevel =
373 weston_desktop_surface_get_implementation_data(dsurface);
374
Quentin Glidic0abf8902016-09-11 11:34:47 +0200375 if (!toplevel->base.configured) {
376 wl_resource_post_error(toplevel->resource,
377 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
378 "Surface has not been configured yet");
379 return;
380 }
381
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200382 weston_desktop_api_show_window_menu(toplevel->base.desktop,
383 dsurface, seat, x, y);
384}
385
386static void
387weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client,
388 struct wl_resource *resource,
389 struct wl_resource *seat_resource,
390 uint32_t serial)
391{
392 struct weston_desktop_surface *dsurface =
393 wl_resource_get_user_data(resource);
394 struct weston_seat *seat =
395 wl_resource_get_user_data(seat_resource);
396 struct weston_desktop_xdg_toplevel *toplevel =
397 weston_desktop_surface_get_implementation_data(dsurface);
398
Quentin Glidic0abf8902016-09-11 11:34:47 +0200399 if (!toplevel->base.configured) {
400 wl_resource_post_error(toplevel->resource,
401 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
402 "Surface has not been configured yet");
403 return;
404 }
405
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200406 weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial);
407}
408
409static void
410weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client,
411 struct wl_resource *resource,
412 struct wl_resource *seat_resource,
413 uint32_t serial,
414 enum zxdg_toplevel_v6_resize_edge edges)
415{
416 struct weston_desktop_surface *dsurface =
417 wl_resource_get_user_data(resource);
418 struct weston_seat *seat =
419 wl_resource_get_user_data(seat_resource);
420 struct weston_desktop_xdg_toplevel *toplevel =
421 weston_desktop_surface_get_implementation_data(dsurface);
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200422 enum weston_desktop_surface_edge surf_edges =
423 (enum weston_desktop_surface_edge) edges;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200424
Quentin Glidic0abf8902016-09-11 11:34:47 +0200425 if (!toplevel->base.configured) {
426 wl_resource_post_error(toplevel->resource,
427 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
428 "Surface has not been configured yet");
429 return;
430 }
431
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200432 weston_desktop_api_resize(toplevel->base.desktop,
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200433 dsurface, seat, serial, surf_edges);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200434}
435
436static void
Quentin Glidic749637a2017-07-18 12:59:14 +0200437weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel,
438 struct weston_desktop_xdg_toplevel_configure *configure)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200439{
Quentin Glidic749637a2017-07-18 12:59:14 +0200440 toplevel->next.state = configure->state;
441 toplevel->next.size = configure->size;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200442}
443
444static void
445weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client,
446 struct wl_resource *resource,
447 int32_t width, int32_t height)
448{
449 struct weston_desktop_surface *dsurface =
450 wl_resource_get_user_data(resource);
451 struct weston_desktop_xdg_toplevel *toplevel =
452 weston_desktop_surface_get_implementation_data(dsurface);
453
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200454 toplevel->next.min_size.width = width;
455 toplevel->next.min_size.height = height;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200456}
457
458static void
459weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client,
460 struct wl_resource *resource,
461 int32_t width, int32_t height)
462{
463 struct weston_desktop_surface *dsurface =
464 wl_resource_get_user_data(resource);
465 struct weston_desktop_xdg_toplevel *toplevel =
466 weston_desktop_surface_get_implementation_data(dsurface);
467
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200468 toplevel->next.max_size.width = width;
469 toplevel->next.max_size.height = height;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200470}
471
472static void
473weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client,
474 struct wl_resource *resource)
475{
476 struct weston_desktop_surface *dsurface =
477 wl_resource_get_user_data(resource);
478 struct weston_desktop_xdg_toplevel *toplevel =
479 weston_desktop_surface_get_implementation_data(dsurface);
480
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200481 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200482 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true);
483}
484
485static void
486weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client,
487 struct wl_resource *resource)
488{
489 struct weston_desktop_surface *dsurface =
490 wl_resource_get_user_data(resource);
491 struct weston_desktop_xdg_toplevel *toplevel =
492 weston_desktop_surface_get_implementation_data(dsurface);
493
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200494 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200495 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false);
496}
497
498static void
499weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client,
500 struct wl_resource *resource,
501 struct wl_resource *output_resource)
502{
503 struct weston_desktop_surface *dsurface =
504 wl_resource_get_user_data(resource);
505 struct weston_desktop_xdg_toplevel *toplevel =
506 weston_desktop_surface_get_implementation_data(dsurface);
507 struct weston_output *output = NULL;
508
509 if (output_resource != NULL)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +0300510 output = weston_output_from_resource(output_resource);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200511
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200512 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200513 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
514 true, output);
515}
516
517static void
518weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client,
519 struct wl_resource *resource)
520{
521 struct weston_desktop_surface *dsurface =
522 wl_resource_get_user_data(resource);
523 struct weston_desktop_xdg_toplevel *toplevel =
524 weston_desktop_surface_get_implementation_data(dsurface);
525
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200526 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200527 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
528 false, NULL);
529}
530
531static void
532weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client,
533 struct wl_resource *resource)
534{
535 struct weston_desktop_surface *dsurface =
536 wl_resource_get_user_data(resource);
537 struct weston_desktop_xdg_toplevel *toplevel =
538 weston_desktop_surface_get_implementation_data(dsurface);
539
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200540 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200541 weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface);
542}
543
544static void
Quentin Glidic749637a2017-07-18 12:59:14 +0200545weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel,
546 struct weston_desktop_xdg_toplevel_configure *configure)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200547{
548 uint32_t *s;
549 struct wl_array states;
550
Quentin Glidic749637a2017-07-18 12:59:14 +0200551 configure->state = toplevel->pending.state;
552 configure->size = toplevel->pending.size;
553
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200554 wl_array_init(&states);
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200555 if (toplevel->pending.state.maximized) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200556 s = wl_array_add(&states, sizeof(uint32_t));
557 *s = ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED;
558 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200559 if (toplevel->pending.state.fullscreen) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200560 s = wl_array_add(&states, sizeof(uint32_t));
561 *s = ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN;
562 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200563 if (toplevel->pending.state.resizing) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200564 s = wl_array_add(&states, sizeof(uint32_t));
565 *s = ZXDG_TOPLEVEL_V6_STATE_RESIZING;
566 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200567 if (toplevel->pending.state.activated) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200568 s = wl_array_add(&states, sizeof(uint32_t));
569 *s = ZXDG_TOPLEVEL_V6_STATE_ACTIVATED;
570 }
571
572 zxdg_toplevel_v6_send_configure(toplevel->resource,
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200573 toplevel->pending.size.width,
574 toplevel->pending.size.height,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200575 &states);
576
577 wl_array_release(&states);
578};
579
580static void
581weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface,
582 void *user_data, bool maximized)
583{
584 struct weston_desktop_xdg_toplevel *toplevel = user_data;
585
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200586 toplevel->pending.state.maximized = maximized;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200587 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200588}
589
590static void
591weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface,
592 void *user_data, bool fullscreen)
593{
594 struct weston_desktop_xdg_toplevel *toplevel = user_data;
595
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200596 toplevel->pending.state.fullscreen = fullscreen;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200597 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200598}
599
600static void
601weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface,
602 void *user_data, bool resizing)
603{
604 struct weston_desktop_xdg_toplevel *toplevel = user_data;
605
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200606 toplevel->pending.state.resizing = resizing;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200607 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200608}
609
610static void
611weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface,
612 void *user_data, bool activated)
613{
614 struct weston_desktop_xdg_toplevel *toplevel = user_data;
615
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200616 toplevel->pending.state.activated = activated;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200617 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200618}
619
620static void
621weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface,
622 void *user_data,
623 int32_t width, int32_t height)
624{
625 struct weston_desktop_xdg_toplevel *toplevel = user_data;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200626
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200627 toplevel->pending.size.width = width;
628 toplevel->pending.size.height = height;
Quentin Glidica56b0532016-09-13 10:05:58 +0200629
Quentin Glidicd51f8262017-04-13 20:25:27 +0200630 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200631}
632
633static void
634weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel,
Quentin Glidiccba26e72016-08-15 12:20:22 +0200635 int32_t sx, int32_t sy)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200636{
637 struct weston_surface *wsurface =
638 weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200639
Quentin Glidiccba26e72016-08-15 12:20:22 +0200640 if (!wsurface->buffer_ref.buffer && !toplevel->added) {
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200641 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200642 return;
643 }
Quentin Glidiccba26e72016-08-15 12:20:22 +0200644 if (!wsurface->buffer_ref.buffer)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200645 return;
646
Philipp Kerlingc6239022017-07-26 14:02:21 +0200647 struct weston_geometry geometry =
648 weston_desktop_surface_get_geometry(toplevel->base.desktop_surface);
649
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200650 if ((toplevel->next.state.maximized || toplevel->next.state.fullscreen) &&
Philipp Kerlingc6239022017-07-26 14:02:21 +0200651 (toplevel->next.size.width != geometry.width ||
652 toplevel->next.size.height != geometry.height)) {
Quentin Glidicc84423b2017-03-10 11:50:41 +0100653 struct weston_desktop_client *client =
654 weston_desktop_surface_get_client(toplevel->base.desktop_surface);
655 struct wl_resource *client_resource =
656 weston_desktop_client_get_resource(client);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200657
Quentin Glidicc84423b2017-03-10 11:50:41 +0100658 wl_resource_post_error(client_resource,
659 ZXDG_SHELL_V6_ERROR_INVALID_SURFACE_STATE,
660 "xdg_surface buffer does not match the configured state");
661 return;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200662 }
Quentin Glidicc84423b2017-03-10 11:50:41 +0100663
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200664 toplevel->current.state = toplevel->next.state;
665 toplevel->current.min_size = toplevel->next.min_size;
666 toplevel->current.max_size = toplevel->next.max_size;
Quentin Glidicc84423b2017-03-10 11:50:41 +0100667
668 weston_desktop_api_committed(toplevel->base.desktop,
669 toplevel->base.desktop_surface,
670 sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200671}
672
673static void
674weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel)
675{
676 zxdg_toplevel_v6_send_close(toplevel->resource);
677}
678
679static bool
680weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface,
681 void *user_data)
682{
683 struct weston_desktop_xdg_toplevel *toplevel = user_data;
684
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200685 return toplevel->current.state.maximized;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200686}
687
688static bool
689weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface,
690 void *user_data)
691{
692 struct weston_desktop_xdg_toplevel *toplevel = user_data;
693
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200694 return toplevel->current.state.fullscreen;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200695}
696
697static bool
698weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface,
699 void *user_data)
700{
701 struct weston_desktop_xdg_toplevel *toplevel = user_data;
702
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200703 return toplevel->current.state.resizing;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200704}
705
706static bool
707weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface,
708 void *user_data)
709{
710 struct weston_desktop_xdg_toplevel *toplevel = user_data;
711
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200712 return toplevel->current.state.activated;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200713}
714
715static void
716weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel)
717{
718 if (toplevel->added)
719 weston_desktop_api_surface_removed(toplevel->base.desktop,
720 toplevel->base.desktop_surface);
721}
722
723static void
724weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource)
725{
726 struct weston_desktop_surface *dsurface =
727 wl_resource_get_user_data(resource);
728
729 if (dsurface != NULL)
730 weston_desktop_surface_resource_destroy(resource);
731}
732
733static const struct zxdg_toplevel_v6_interface weston_desktop_xdg_toplevel_implementation = {
734 .destroy = weston_desktop_destroy_request,
735 .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent,
736 .set_title = weston_desktop_xdg_toplevel_protocol_set_title,
737 .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id,
738 .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu,
739 .move = weston_desktop_xdg_toplevel_protocol_move,
740 .resize = weston_desktop_xdg_toplevel_protocol_resize,
741 .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size,
742 .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size,
743 .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized,
744 .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized,
745 .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen,
746 .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen,
747 .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized,
748};
749
750static void
751weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client,
752 struct wl_resource *resource,
753 struct wl_resource *seat_resource,
754 uint32_t serial)
755{
756 struct weston_desktop_surface *dsurface =
757 wl_resource_get_user_data(resource);
758 struct weston_desktop_xdg_popup *popup =
759 weston_desktop_surface_get_implementation_data(dsurface);
760 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
761 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
762 struct weston_desktop_surface *topmost;
763 bool parent_is_toplevel =
764 popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
765
766 if (popup->committed) {
767 wl_resource_post_error(popup->resource,
768 ZXDG_POPUP_V6_ERROR_INVALID_GRAB,
769 "xdg_popup already is mapped");
770 return;
771 }
772
773 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
774 if ((topmost == NULL && !parent_is_toplevel) ||
775 (topmost != NULL && topmost != popup->parent->desktop_surface)) {
776 struct weston_desktop_client *client =
777 weston_desktop_surface_get_client(dsurface);
778 struct wl_resource *client_resource =
779 weston_desktop_client_get_resource(client);
780
781 wl_resource_post_error(client_resource,
782 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
783 "xdg_popup was not created on the topmost popup");
784 return;
785 }
786
787 popup->seat = seat;
788 weston_desktop_surface_popup_grab(popup->base.desktop_surface,
789 popup->seat, serial);
790}
791
792static void
793weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup)
794{
795 zxdg_popup_v6_send_configure(popup->resource,
796 popup->geometry.x,
797 popup->geometry.y,
798 popup->geometry.width,
799 popup->geometry.height);
800}
801
802static void
803weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
804 void *user_data);
805
806static void
807weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup)
808{
809 if (!popup->committed)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200810 weston_desktop_xdg_surface_schedule_configure(&popup->base, true);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200811 popup->committed = true;
812 weston_desktop_xdg_popup_update_position(popup->base.desktop_surface,
813 popup);
814}
815
816static void
817weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
818 void *user_data)
819{
820}
821
822static void
823weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup)
824{
825 zxdg_popup_v6_send_popup_done(popup->resource);
826}
827
828static void
829weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup)
830{
831 struct weston_desktop_surface *topmost;
832 struct weston_desktop_client *client =
833 weston_desktop_surface_get_client(popup->base.desktop_surface);
834
835 if (!weston_desktop_surface_get_grab(popup->base.desktop_surface))
836 return;
837
838 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
839 if (topmost != popup->base.desktop_surface) {
840 struct wl_resource *client_resource =
841 weston_desktop_client_get_resource(client);
842
843 wl_resource_post_error(client_resource,
844 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
845 "xdg_popup was destroyed while it was not the topmost popup.");
846 }
847
848 weston_desktop_surface_popup_ungrab(popup->base.desktop_surface,
849 popup->seat);
850}
851
852static void
853weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource)
854{
855 struct weston_desktop_surface *dsurface =
856 wl_resource_get_user_data(resource);
857
858 if (dsurface != NULL)
859 weston_desktop_surface_resource_destroy(resource);
860}
861
862static const struct zxdg_popup_v6_interface weston_desktop_xdg_popup_implementation = {
863 .destroy = weston_desktop_destroy_request,
864 .grab = weston_desktop_xdg_popup_protocol_grab,
865};
866
867static void
868weston_desktop_xdg_surface_send_configure(void *user_data)
869{
870 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidic749637a2017-07-18 12:59:14 +0200871 struct weston_desktop_xdg_surface_configure *configure;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200872
873 surface->configure_idle = NULL;
Quentin Glidic749637a2017-07-18 12:59:14 +0200874
875 configure = zalloc(weston_desktop_surface_configure_biggest_size);
876 if (configure == NULL) {
877 struct weston_desktop_client *client =
878 weston_desktop_surface_get_client(surface->desktop_surface);
879 struct wl_client *wl_client =
880 weston_desktop_client_get_client(client);
881 wl_client_post_no_memory(wl_client);
882 return;
883 }
884 wl_list_insert(surface->configure_list.prev, &configure->link);
885 configure->serial =
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200886 wl_display_next_serial(weston_desktop_get_display(surface->desktop));
887
888 switch (surface->role) {
889 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
890 assert(0 && "not reached");
891 break;
892 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic749637a2017-07-18 12:59:14 +0200893 weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface,
894 (struct weston_desktop_xdg_toplevel_configure *) configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200895 break;
896 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
897 weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface);
898 break;
899 }
900
Quentin Glidic749637a2017-07-18 12:59:14 +0200901 zxdg_surface_v6_send_configure(surface->resource, configure->serial);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200902}
903
Quentin Glidicd51f8262017-04-13 20:25:27 +0200904static bool
905weston_desktop_xdg_toplevel_state_compare(struct weston_desktop_xdg_toplevel *toplevel)
906{
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200907 if (toplevel->pending.state.activated != toplevel->current.state.activated)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200908 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200909 if (toplevel->pending.state.fullscreen != toplevel->current.state.fullscreen)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200910 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200911 if (toplevel->pending.state.maximized != toplevel->current.state.maximized)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200912 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200913 if (toplevel->pending.state.resizing != toplevel->current.state.resizing)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200914 return false;
915
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200916 if (toplevel->base.surface->width == toplevel->pending.size.width &&
917 toplevel->base.surface->height == toplevel->pending.size.height)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200918 return true;
919
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200920 if (toplevel->pending.size.width == 0 &&
921 toplevel->pending.size.height == 0)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200922 return true;
923
924 return false;
925}
926
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200927static void
Quentin Glidicd51f8262017-04-13 20:25:27 +0200928weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface,
929 bool force)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200930{
931 struct wl_display *display = weston_desktop_get_display(surface->desktop);
932 struct wl_event_loop *loop = wl_display_get_event_loop(display);
Quentin Glidic218126d2017-07-11 13:31:36 +0200933 bool pending_same = !force;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200934
Quentin Glidicd51f8262017-04-13 20:25:27 +0200935 switch (surface->role) {
936 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
937 assert(0 && "not reached");
938 break;
939 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic218126d2017-07-11 13:31:36 +0200940 pending_same = pending_same &&
Quentin Glidicd51f8262017-04-13 20:25:27 +0200941 weston_desktop_xdg_toplevel_state_compare((struct weston_desktop_xdg_toplevel *) surface);
942 break;
943 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
944 break;
945 }
946
947 if (surface->configure_idle != NULL) {
Quentin Glidic218126d2017-07-11 13:31:36 +0200948 if (!pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200949 return;
950
951 wl_event_source_remove(surface->configure_idle);
952 surface->configure_idle = NULL;
953 } else {
Quentin Glidic218126d2017-07-11 13:31:36 +0200954 if (pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200955 return;
956
957 surface->configure_idle =
958 wl_event_loop_add_idle(loop,
959 weston_desktop_xdg_surface_send_configure,
960 surface);
961 }
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200962}
963
964static void
965weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client,
966 struct wl_resource *resource,
967 uint32_t id)
968{
969 struct weston_desktop_surface *dsurface =
970 wl_resource_get_user_data(resource);
971 struct weston_surface *wsurface =
972 weston_desktop_surface_get_surface(dsurface);
973 struct weston_desktop_xdg_toplevel *toplevel =
974 weston_desktop_surface_get_implementation_data(dsurface);
975
976 if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role,
977 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
978 return;
979
980 toplevel->resource =
981 weston_desktop_surface_add_resource(toplevel->base.desktop_surface,
982 &zxdg_toplevel_v6_interface,
983 &weston_desktop_xdg_toplevel_implementation,
984 id, weston_desktop_xdg_toplevel_resource_destroy);
985 if (toplevel->resource == NULL)
986 return;
987
988 toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
989}
990
991static void
992weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client,
993 struct wl_resource *resource,
994 uint32_t id,
995 struct wl_resource *parent_resource,
996 struct wl_resource *positioner_resource)
997{
998 struct weston_desktop_surface *dsurface =
999 wl_resource_get_user_data(resource);
1000 struct weston_surface *wsurface =
1001 weston_desktop_surface_get_surface(dsurface);
1002 struct weston_desktop_xdg_popup *popup =
1003 weston_desktop_surface_get_implementation_data(dsurface);
1004 struct weston_desktop_surface *parent_surface =
1005 wl_resource_get_user_data(parent_resource);
1006 struct weston_desktop_xdg_surface *parent =
1007 weston_desktop_surface_get_implementation_data(parent_surface);
1008 struct weston_desktop_xdg_positioner *positioner =
1009 wl_resource_get_user_data(positioner_resource);
1010
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02001011 /* Checking whether the size and anchor rect both have a positive size
1012 * is enough to verify both have been correctly set */
1013 if (positioner->size.width == 0 || positioner->anchor_rect.width == 0) {
1014 wl_resource_post_error(resource,
1015 ZXDG_SHELL_V6_ERROR_INVALID_POSITIONER,
1016 "positioner object is not complete");
1017 return;
1018 }
1019
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001020 if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
1021 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
1022 return;
1023
1024 popup->resource =
1025 weston_desktop_surface_add_resource(popup->base.desktop_surface,
1026 &zxdg_popup_v6_interface,
1027 &weston_desktop_xdg_popup_implementation,
1028 id, weston_desktop_xdg_popup_resource_destroy);
1029 if (popup->resource == NULL)
1030 return;
1031
1032 popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
1033 popup->parent = parent;
1034
1035 popup->geometry =
1036 weston_desktop_xdg_positioner_get_geometry(positioner,
1037 dsurface,
1038 parent_surface);
1039
1040 weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
1041 parent_surface,
1042 popup->geometry.x,
1043 popup->geometry.y,
1044 true);
1045}
1046
1047static bool
1048weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
1049{
1050 struct weston_surface *wsurface =
1051 weston_desktop_surface_get_surface(surface->desktop_surface);
1052 const char *role;
1053
1054 role = weston_surface_get_role(wsurface);
1055 if (role != NULL &&
1056 (role == weston_desktop_xdg_toplevel_role ||
1057 role == weston_desktop_xdg_popup_role))
1058 return true;
1059
1060 wl_resource_post_error(surface->resource,
1061 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1062 "xdg_surface must have a role");
1063 return false;
1064}
1065
1066static void
1067weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
1068 struct wl_resource *resource,
1069 int32_t x, int32_t y,
1070 int32_t width, int32_t height)
1071{
1072 struct weston_desktop_surface *dsurface =
1073 wl_resource_get_user_data(resource);
1074 struct weston_desktop_xdg_surface *surface =
1075 weston_desktop_surface_get_implementation_data(dsurface);
1076
1077 if (!weston_desktop_xdg_surface_check_role(surface))
1078 return;
1079
1080 surface->has_next_geometry = true;
1081 surface->next_geometry.x = x;
1082 surface->next_geometry.y = y;
1083 surface->next_geometry.width = width;
1084 surface->next_geometry.height = height;
1085}
1086
1087static void
1088weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
1089 struct wl_resource *resource,
1090 uint32_t serial)
1091{
1092 struct weston_desktop_surface *dsurface =
1093 wl_resource_get_user_data(resource);
1094 struct weston_desktop_xdg_surface *surface =
1095 weston_desktop_surface_get_implementation_data(dsurface);
Quentin Glidic749637a2017-07-18 12:59:14 +02001096 struct weston_desktop_xdg_surface_configure *configure, *temp;
1097 bool found = false;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001098
1099 if (!weston_desktop_xdg_surface_check_role(surface))
1100 return;
1101
Quentin Glidic749637a2017-07-18 12:59:14 +02001102 wl_list_for_each_safe(configure, temp, &surface->configure_list, link) {
1103 if (configure->serial < serial) {
1104 wl_list_remove(&configure->link);
1105 free(configure);
1106 } else if (configure->serial == serial) {
1107 wl_list_remove(&configure->link);
1108 found = true;
1109 }
1110 break;
1111 }
1112 if (!found) {
1113 struct weston_desktop_client *client =
1114 weston_desktop_surface_get_client(dsurface);
1115 struct wl_resource *client_resource =
1116 weston_desktop_client_get_resource(client);
1117 wl_resource_post_error(client_resource,
1118 ZXDG_SHELL_V6_ERROR_INVALID_SURFACE_STATE,
1119 "Wrong configure serial: %u", serial);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001120 return;
Quentin Glidic749637a2017-07-18 12:59:14 +02001121 }
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001122
1123 surface->configured = true;
1124
1125 switch (surface->role) {
1126 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1127 assert(0 && "not reached");
1128 break;
1129 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic749637a2017-07-18 12:59:14 +02001130 weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface,
1131 (struct weston_desktop_xdg_toplevel_configure *) configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001132 break;
1133 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1134 break;
1135 }
Quentin Glidic749637a2017-07-18 12:59:14 +02001136
1137 free(configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001138}
1139
1140static void
1141weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
1142 uint32_t serial, void *user_data)
1143{
1144 struct weston_desktop_client *client =
1145 weston_desktop_surface_get_client(dsurface);
1146
1147 zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client),
1148 serial);
1149}
1150
1151static void
1152weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +02001153 void *user_data,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001154 int32_t sx, int32_t sy)
1155{
1156 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidiccba26e72016-08-15 12:20:22 +02001157 struct weston_surface *wsurface =
1158 weston_desktop_surface_get_surface (dsurface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001159
Quentin Glidiccba26e72016-08-15 12:20:22 +02001160 if (wsurface->buffer_ref.buffer && !surface->configured) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001161 wl_resource_post_error(surface->resource,
1162 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1163 "xdg_surface has never been configured");
1164 return;
1165 }
1166
1167 if (surface->has_next_geometry) {
1168 surface->has_next_geometry = false;
1169 weston_desktop_surface_set_geometry(surface->desktop_surface,
1170 surface->next_geometry);
1171 }
1172
1173 switch (surface->role) {
1174 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1175 wl_resource_post_error(surface->resource,
1176 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1177 "xdg_surface must have a role");
1178 break;
1179 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidiccba26e72016-08-15 12:20:22 +02001180 weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001181 break;
1182 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1183 weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1184 break;
1185 }
1186}
1187
1188static void
1189weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1190 void *user_data)
1191{
1192 struct weston_desktop_xdg_surface *surface = user_data;
1193
1194 switch (surface->role) {
1195 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1196 assert(0 && "not reached");
1197 break;
1198 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1199 weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1200 break;
1201 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1202 weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1203 break;
1204 }
1205}
1206
1207static void
1208weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1209 void *user_data)
1210{
1211 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidic749637a2017-07-18 12:59:14 +02001212 struct weston_desktop_xdg_surface_configure *configure, *temp;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001213
1214 switch (surface->role) {
1215 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1216 break;
1217 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1218 weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1219 break;
1220 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1221 weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1222 break;
1223 }
1224
1225 if (surface->configure_idle != NULL)
1226 wl_event_source_remove(surface->configure_idle);
1227
Quentin Glidic749637a2017-07-18 12:59:14 +02001228 wl_list_for_each_safe(configure, temp, &surface->configure_list, link)
1229 free(configure);
1230
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001231 free(surface);
1232}
1233
1234static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = {
1235 .destroy = weston_desktop_destroy_request,
1236 .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel,
1237 .get_popup = weston_desktop_xdg_surface_protocol_get_popup,
1238 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1239 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
1240};
1241
1242static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1243 /* These are used for toplevel only */
1244 .set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1245 .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1246 .set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1247 .set_activated = weston_desktop_xdg_toplevel_set_activated,
1248 .set_size = weston_desktop_xdg_toplevel_set_size,
1249
1250 .get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1251 .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1252 .get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1253 .get_activated = weston_desktop_xdg_toplevel_get_activated,
1254
1255 /* These are used for popup only */
1256 .update_position = weston_desktop_xdg_popup_update_position,
1257
1258 /* Common API */
1259 .committed = weston_desktop_xdg_surface_committed,
1260 .ping = weston_desktop_xdg_surface_ping,
1261 .close = weston_desktop_xdg_surface_close,
1262
1263 .destroy = weston_desktop_xdg_surface_destroy,
1264};
1265
1266static void
1267weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1268 struct wl_resource *resource,
1269 uint32_t id)
1270{
1271 struct weston_desktop_client *client =
1272 wl_resource_get_user_data(resource);
1273 struct weston_desktop_xdg_positioner *positioner;
1274
1275 positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1276 if (positioner == NULL) {
1277 wl_client_post_no_memory(wl_client);
1278 return;
1279 }
1280
1281 positioner->client = client;
1282 positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1283
1284 positioner->resource =
1285 wl_resource_create(wl_client,
1286 &zxdg_positioner_v6_interface,
1287 wl_resource_get_version(resource), id);
1288 if (positioner->resource == NULL) {
1289 wl_client_post_no_memory(wl_client);
1290 free(positioner);
1291 return;
1292 }
1293 wl_resource_set_implementation(positioner->resource,
1294 &weston_desktop_xdg_positioner_implementation,
1295 positioner, weston_desktop_xdg_positioner_destroy);
1296}
1297
1298static void
1299weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1300{
1301 struct weston_desktop_surface *dsurface =
1302 wl_resource_get_user_data(resource);
1303
1304 if (dsurface != NULL)
1305 weston_desktop_surface_resource_destroy(resource);
1306}
1307
1308static void
1309weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1310 struct wl_resource *resource,
1311 uint32_t id,
1312 struct wl_resource *surface_resource)
1313{
1314 struct weston_desktop_client *client =
1315 wl_resource_get_user_data(resource);
1316 struct weston_surface *wsurface =
1317 wl_resource_get_user_data(surface_resource);
1318 struct weston_desktop_xdg_surface *surface;
1319
1320 surface = zalloc(weston_desktop_surface_role_biggest_size);
1321 if (surface == NULL) {
1322 wl_client_post_no_memory(wl_client);
1323 return;
1324 }
1325
1326 surface->desktop = weston_desktop_client_get_desktop(client);
1327 surface->surface = wsurface;
1328
1329 surface->desktop_surface =
1330 weston_desktop_surface_create(surface->desktop, client,
1331 surface->surface,
1332 &weston_desktop_xdg_surface_internal_implementation,
1333 surface);
1334 if (surface->desktop_surface == NULL) {
1335 free(surface);
1336 return;
1337 }
1338
1339 surface->resource =
1340 weston_desktop_surface_add_resource(surface->desktop_surface,
1341 &zxdg_surface_v6_interface,
1342 &weston_desktop_xdg_surface_implementation,
1343 id, weston_desktop_xdg_surface_resource_destroy);
1344 if (surface->resource == NULL)
1345 return;
1346
1347 if (wsurface->buffer_ref.buffer != NULL) {
1348 wl_resource_post_error(surface->resource,
1349 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1350 "xdg_surface must not have a buffer at creation");
1351 return;
1352 }
Quentin Glidic749637a2017-07-18 12:59:14 +02001353
1354 wl_list_init(&surface->configure_list);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001355}
1356
1357static void
1358weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1359 struct wl_resource *resource,
1360 uint32_t serial)
1361{
1362 struct weston_desktop_client *client =
1363 wl_resource_get_user_data(resource);
1364
1365 weston_desktop_client_pong(client, serial);
1366}
1367
1368static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = {
1369 .destroy = weston_desktop_destroy_request,
1370 .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1371 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1372 .pong = weston_desktop_xdg_shell_protocol_pong,
1373};
1374
1375static void
1376weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1377 uint32_t version, uint32_t id)
1378{
1379 struct weston_desktop *desktop = data;
1380
1381 weston_desktop_client_create(desktop, client, NULL,
1382 &zxdg_shell_v6_interface,
1383 &weston_desktop_xdg_shell_implementation,
1384 version, id);
1385}
1386
1387struct wl_global *
1388weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display)
1389{
1390 return wl_global_create(display, &zxdg_shell_v6_interface,
1391 WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1392 weston_desktop_xdg_shell_bind);
1393}