blob: 4db3748b76bfc06bc4bce937f7b8e122b40aa088 [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 Glidic6914c802017-08-28 20:12:01 +0200306weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200307
308static void
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200309weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel *toplevel)
310{
311 if (toplevel->added)
312 return;
313
314 weston_desktop_api_surface_added(toplevel->base.desktop,
315 toplevel->base.desktop_surface);
Quentin Glidic6914c802017-08-28 20:12:01 +0200316 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200317 toplevel->added = true;
318}
319
320static void
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200321weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client,
322 struct wl_resource *resource,
323 struct wl_resource *parent_resource)
324{
325 struct weston_desktop_surface *dsurface =
326 wl_resource_get_user_data(resource);
327 struct weston_desktop_xdg_toplevel *toplevel =
328 weston_desktop_surface_get_implementation_data(dsurface);
329 struct weston_desktop_surface *parent = NULL;
330
331 if (parent_resource != NULL)
332 parent = wl_resource_get_user_data(parent_resource);
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200333
334 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200335 weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent);
336}
337
338static void
339weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client,
340 struct wl_resource *resource,
341 const char *title)
342{
343 struct weston_desktop_surface *toplevel =
344 wl_resource_get_user_data(resource);
345
346 weston_desktop_surface_set_title(toplevel, title);
347}
348
349static void
350weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client,
351 struct wl_resource *resource,
352 const char *app_id)
353{
354 struct weston_desktop_surface *toplevel =
355 wl_resource_get_user_data(resource);
356
357 weston_desktop_surface_set_app_id(toplevel, app_id);
358}
359
360static void
361weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client,
362 struct wl_resource *resource,
363 struct wl_resource *seat_resource,
364 uint32_t serial,
365 int32_t x, int32_t y)
366{
367 struct weston_desktop_surface *dsurface =
368 wl_resource_get_user_data(resource);
369 struct weston_seat *seat =
370 wl_resource_get_user_data(seat_resource);
371 struct weston_desktop_xdg_toplevel *toplevel =
372 weston_desktop_surface_get_implementation_data(dsurface);
373
Quentin Glidic0abf8902016-09-11 11:34:47 +0200374 if (!toplevel->base.configured) {
375 wl_resource_post_error(toplevel->resource,
376 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
377 "Surface has not been configured yet");
378 return;
379 }
380
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200381 weston_desktop_api_show_window_menu(toplevel->base.desktop,
382 dsurface, seat, x, y);
383}
384
385static void
386weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client,
387 struct wl_resource *resource,
388 struct wl_resource *seat_resource,
389 uint32_t serial)
390{
391 struct weston_desktop_surface *dsurface =
392 wl_resource_get_user_data(resource);
393 struct weston_seat *seat =
394 wl_resource_get_user_data(seat_resource);
395 struct weston_desktop_xdg_toplevel *toplevel =
396 weston_desktop_surface_get_implementation_data(dsurface);
397
Quentin Glidic0abf8902016-09-11 11:34:47 +0200398 if (!toplevel->base.configured) {
399 wl_resource_post_error(toplevel->resource,
400 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
401 "Surface has not been configured yet");
402 return;
403 }
404
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200405 weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial);
406}
407
408static void
409weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client,
410 struct wl_resource *resource,
411 struct wl_resource *seat_resource,
412 uint32_t serial,
413 enum zxdg_toplevel_v6_resize_edge edges)
414{
415 struct weston_desktop_surface *dsurface =
416 wl_resource_get_user_data(resource);
417 struct weston_seat *seat =
418 wl_resource_get_user_data(seat_resource);
419 struct weston_desktop_xdg_toplevel *toplevel =
420 weston_desktop_surface_get_implementation_data(dsurface);
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200421 enum weston_desktop_surface_edge surf_edges =
422 (enum weston_desktop_surface_edge) edges;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200423
Quentin Glidic0abf8902016-09-11 11:34:47 +0200424 if (!toplevel->base.configured) {
425 wl_resource_post_error(toplevel->resource,
426 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
427 "Surface has not been configured yet");
428 return;
429 }
430
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200431 weston_desktop_api_resize(toplevel->base.desktop,
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200432 dsurface, seat, serial, surf_edges);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200433}
434
435static void
Quentin Glidic749637a2017-07-18 12:59:14 +0200436weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel,
437 struct weston_desktop_xdg_toplevel_configure *configure)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200438{
Quentin Glidic749637a2017-07-18 12:59:14 +0200439 toplevel->next.state = configure->state;
440 toplevel->next.size = configure->size;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200441}
442
443static void
444weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client,
445 struct wl_resource *resource,
446 int32_t width, int32_t height)
447{
448 struct weston_desktop_surface *dsurface =
449 wl_resource_get_user_data(resource);
450 struct weston_desktop_xdg_toplevel *toplevel =
451 weston_desktop_surface_get_implementation_data(dsurface);
452
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200453 toplevel->next.min_size.width = width;
454 toplevel->next.min_size.height = height;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200455}
456
457static void
458weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client,
459 struct wl_resource *resource,
460 int32_t width, int32_t height)
461{
462 struct weston_desktop_surface *dsurface =
463 wl_resource_get_user_data(resource);
464 struct weston_desktop_xdg_toplevel *toplevel =
465 weston_desktop_surface_get_implementation_data(dsurface);
466
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200467 toplevel->next.max_size.width = width;
468 toplevel->next.max_size.height = height;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200469}
470
471static void
472weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client,
473 struct wl_resource *resource)
474{
475 struct weston_desktop_surface *dsurface =
476 wl_resource_get_user_data(resource);
477 struct weston_desktop_xdg_toplevel *toplevel =
478 weston_desktop_surface_get_implementation_data(dsurface);
479
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200480 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200481 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true);
482}
483
484static void
485weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client,
486 struct wl_resource *resource)
487{
488 struct weston_desktop_surface *dsurface =
489 wl_resource_get_user_data(resource);
490 struct weston_desktop_xdg_toplevel *toplevel =
491 weston_desktop_surface_get_implementation_data(dsurface);
492
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200493 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200494 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false);
495}
496
497static void
498weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client,
499 struct wl_resource *resource,
500 struct wl_resource *output_resource)
501{
502 struct weston_desktop_surface *dsurface =
503 wl_resource_get_user_data(resource);
504 struct weston_desktop_xdg_toplevel *toplevel =
505 weston_desktop_surface_get_implementation_data(dsurface);
506 struct weston_output *output = NULL;
507
508 if (output_resource != NULL)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +0300509 output = weston_output_from_resource(output_resource);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200510
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200511 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200512 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
513 true, output);
514}
515
516static void
517weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client,
518 struct wl_resource *resource)
519{
520 struct weston_desktop_surface *dsurface =
521 wl_resource_get_user_data(resource);
522 struct weston_desktop_xdg_toplevel *toplevel =
523 weston_desktop_surface_get_implementation_data(dsurface);
524
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200525 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200526 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
527 false, NULL);
528}
529
530static void
531weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client,
532 struct wl_resource *resource)
533{
534 struct weston_desktop_surface *dsurface =
535 wl_resource_get_user_data(resource);
536 struct weston_desktop_xdg_toplevel *toplevel =
537 weston_desktop_surface_get_implementation_data(dsurface);
538
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200539 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200540 weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface);
541}
542
543static void
Quentin Glidic749637a2017-07-18 12:59:14 +0200544weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel,
545 struct weston_desktop_xdg_toplevel_configure *configure)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200546{
547 uint32_t *s;
548 struct wl_array states;
549
Quentin Glidic749637a2017-07-18 12:59:14 +0200550 configure->state = toplevel->pending.state;
551 configure->size = toplevel->pending.size;
552
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200553 wl_array_init(&states);
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200554 if (toplevel->pending.state.maximized) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200555 s = wl_array_add(&states, sizeof(uint32_t));
556 *s = ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED;
557 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200558 if (toplevel->pending.state.fullscreen) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200559 s = wl_array_add(&states, sizeof(uint32_t));
560 *s = ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN;
561 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200562 if (toplevel->pending.state.resizing) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200563 s = wl_array_add(&states, sizeof(uint32_t));
564 *s = ZXDG_TOPLEVEL_V6_STATE_RESIZING;
565 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200566 if (toplevel->pending.state.activated) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200567 s = wl_array_add(&states, sizeof(uint32_t));
568 *s = ZXDG_TOPLEVEL_V6_STATE_ACTIVATED;
569 }
570
571 zxdg_toplevel_v6_send_configure(toplevel->resource,
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200572 toplevel->pending.size.width,
573 toplevel->pending.size.height,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200574 &states);
575
576 wl_array_release(&states);
577};
578
579static void
580weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface,
581 void *user_data, bool maximized)
582{
583 struct weston_desktop_xdg_toplevel *toplevel = user_data;
584
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200585 toplevel->pending.state.maximized = maximized;
Quentin Glidic6914c802017-08-28 20:12:01 +0200586 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200587}
588
589static void
590weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface,
591 void *user_data, bool fullscreen)
592{
593 struct weston_desktop_xdg_toplevel *toplevel = user_data;
594
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200595 toplevel->pending.state.fullscreen = fullscreen;
Quentin Glidic6914c802017-08-28 20:12:01 +0200596 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200597}
598
599static void
600weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface,
601 void *user_data, bool resizing)
602{
603 struct weston_desktop_xdg_toplevel *toplevel = user_data;
604
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200605 toplevel->pending.state.resizing = resizing;
Quentin Glidic6914c802017-08-28 20:12:01 +0200606 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200607}
608
609static void
610weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface,
611 void *user_data, bool activated)
612{
613 struct weston_desktop_xdg_toplevel *toplevel = user_data;
614
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200615 toplevel->pending.state.activated = activated;
Quentin Glidic6914c802017-08-28 20:12:01 +0200616 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200617}
618
619static void
620weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface,
621 void *user_data,
622 int32_t width, int32_t height)
623{
624 struct weston_desktop_xdg_toplevel *toplevel = user_data;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200625
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200626 toplevel->pending.size.width = width;
627 toplevel->pending.size.height = height;
Quentin Glidica56b0532016-09-13 10:05:58 +0200628
Quentin Glidic6914c802017-08-28 20:12:01 +0200629 weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200630}
631
632static void
633weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel,
Quentin Glidiccba26e72016-08-15 12:20:22 +0200634 int32_t sx, int32_t sy)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200635{
636 struct weston_surface *wsurface =
637 weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200638
Quentin Glidiccba26e72016-08-15 12:20:22 +0200639 if (!wsurface->buffer_ref.buffer && !toplevel->added) {
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200640 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200641 return;
642 }
Quentin Glidiccba26e72016-08-15 12:20:22 +0200643 if (!wsurface->buffer_ref.buffer)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200644 return;
645
Philipp Kerlingc6239022017-07-26 14:02:21 +0200646 struct weston_geometry geometry =
647 weston_desktop_surface_get_geometry(toplevel->base.desktop_surface);
648
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200649 if ((toplevel->next.state.maximized || toplevel->next.state.fullscreen) &&
Philipp Kerlingc6239022017-07-26 14:02:21 +0200650 (toplevel->next.size.width != geometry.width ||
651 toplevel->next.size.height != geometry.height)) {
Quentin Glidicc84423b2017-03-10 11:50:41 +0100652 struct weston_desktop_client *client =
653 weston_desktop_surface_get_client(toplevel->base.desktop_surface);
654 struct wl_resource *client_resource =
655 weston_desktop_client_get_resource(client);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200656
Quentin Glidicc84423b2017-03-10 11:50:41 +0100657 wl_resource_post_error(client_resource,
658 ZXDG_SHELL_V6_ERROR_INVALID_SURFACE_STATE,
659 "xdg_surface buffer does not match the configured state");
660 return;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200661 }
Quentin Glidicc84423b2017-03-10 11:50:41 +0100662
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200663 toplevel->current.state = toplevel->next.state;
664 toplevel->current.min_size = toplevel->next.min_size;
665 toplevel->current.max_size = toplevel->next.max_size;
Quentin Glidicc84423b2017-03-10 11:50:41 +0100666
667 weston_desktop_api_committed(toplevel->base.desktop,
668 toplevel->base.desktop_surface,
669 sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200670}
671
672static void
673weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel)
674{
675 zxdg_toplevel_v6_send_close(toplevel->resource);
676}
677
678static bool
679weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface,
680 void *user_data)
681{
682 struct weston_desktop_xdg_toplevel *toplevel = user_data;
683
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200684 return toplevel->current.state.maximized;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200685}
686
687static bool
688weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface,
689 void *user_data)
690{
691 struct weston_desktop_xdg_toplevel *toplevel = user_data;
692
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200693 return toplevel->current.state.fullscreen;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200694}
695
696static bool
697weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface,
698 void *user_data)
699{
700 struct weston_desktop_xdg_toplevel *toplevel = user_data;
701
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200702 return toplevel->current.state.resizing;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200703}
704
705static bool
706weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface,
707 void *user_data)
708{
709 struct weston_desktop_xdg_toplevel *toplevel = user_data;
710
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200711 return toplevel->current.state.activated;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200712}
713
714static void
715weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel)
716{
717 if (toplevel->added)
718 weston_desktop_api_surface_removed(toplevel->base.desktop,
719 toplevel->base.desktop_surface);
720}
721
722static void
723weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource)
724{
725 struct weston_desktop_surface *dsurface =
726 wl_resource_get_user_data(resource);
727
728 if (dsurface != NULL)
729 weston_desktop_surface_resource_destroy(resource);
730}
731
732static const struct zxdg_toplevel_v6_interface weston_desktop_xdg_toplevel_implementation = {
733 .destroy = weston_desktop_destroy_request,
734 .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent,
735 .set_title = weston_desktop_xdg_toplevel_protocol_set_title,
736 .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id,
737 .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu,
738 .move = weston_desktop_xdg_toplevel_protocol_move,
739 .resize = weston_desktop_xdg_toplevel_protocol_resize,
740 .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size,
741 .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size,
742 .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized,
743 .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized,
744 .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen,
745 .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen,
746 .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized,
747};
748
749static void
750weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client,
751 struct wl_resource *resource,
752 struct wl_resource *seat_resource,
753 uint32_t serial)
754{
755 struct weston_desktop_surface *dsurface =
756 wl_resource_get_user_data(resource);
757 struct weston_desktop_xdg_popup *popup =
758 weston_desktop_surface_get_implementation_data(dsurface);
759 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
760 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
761 struct weston_desktop_surface *topmost;
762 bool parent_is_toplevel =
763 popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
764
765 if (popup->committed) {
766 wl_resource_post_error(popup->resource,
767 ZXDG_POPUP_V6_ERROR_INVALID_GRAB,
768 "xdg_popup already is mapped");
769 return;
770 }
771
772 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
773 if ((topmost == NULL && !parent_is_toplevel) ||
774 (topmost != NULL && topmost != popup->parent->desktop_surface)) {
775 struct weston_desktop_client *client =
776 weston_desktop_surface_get_client(dsurface);
777 struct wl_resource *client_resource =
778 weston_desktop_client_get_resource(client);
779
780 wl_resource_post_error(client_resource,
781 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
782 "xdg_popup was not created on the topmost popup");
783 return;
784 }
785
786 popup->seat = seat;
787 weston_desktop_surface_popup_grab(popup->base.desktop_surface,
788 popup->seat, serial);
789}
790
791static void
792weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup)
793{
794 zxdg_popup_v6_send_configure(popup->resource,
795 popup->geometry.x,
796 popup->geometry.y,
797 popup->geometry.width,
798 popup->geometry.height);
799}
800
801static void
802weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
803 void *user_data);
804
805static void
806weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup)
807{
808 if (!popup->committed)
Quentin Glidic6914c802017-08-28 20:12:01 +0200809 weston_desktop_xdg_surface_schedule_configure(&popup->base);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200810 popup->committed = true;
811 weston_desktop_xdg_popup_update_position(popup->base.desktop_surface,
812 popup);
813}
814
815static void
816weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
817 void *user_data)
818{
819}
820
821static void
822weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup)
823{
824 zxdg_popup_v6_send_popup_done(popup->resource);
825}
826
827static void
828weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup)
829{
830 struct weston_desktop_surface *topmost;
831 struct weston_desktop_client *client =
832 weston_desktop_surface_get_client(popup->base.desktop_surface);
833
834 if (!weston_desktop_surface_get_grab(popup->base.desktop_surface))
835 return;
836
837 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
838 if (topmost != popup->base.desktop_surface) {
839 struct wl_resource *client_resource =
840 weston_desktop_client_get_resource(client);
841
842 wl_resource_post_error(client_resource,
843 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
844 "xdg_popup was destroyed while it was not the topmost popup.");
845 }
846
847 weston_desktop_surface_popup_ungrab(popup->base.desktop_surface,
848 popup->seat);
849}
850
851static void
852weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource)
853{
854 struct weston_desktop_surface *dsurface =
855 wl_resource_get_user_data(resource);
856
857 if (dsurface != NULL)
858 weston_desktop_surface_resource_destroy(resource);
859}
860
861static const struct zxdg_popup_v6_interface weston_desktop_xdg_popup_implementation = {
862 .destroy = weston_desktop_destroy_request,
863 .grab = weston_desktop_xdg_popup_protocol_grab,
864};
865
866static void
867weston_desktop_xdg_surface_send_configure(void *user_data)
868{
869 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidic749637a2017-07-18 12:59:14 +0200870 struct weston_desktop_xdg_surface_configure *configure;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200871
872 surface->configure_idle = NULL;
Quentin Glidic749637a2017-07-18 12:59:14 +0200873
874 configure = zalloc(weston_desktop_surface_configure_biggest_size);
875 if (configure == NULL) {
876 struct weston_desktop_client *client =
877 weston_desktop_surface_get_client(surface->desktop_surface);
878 struct wl_client *wl_client =
879 weston_desktop_client_get_client(client);
880 wl_client_post_no_memory(wl_client);
881 return;
882 }
883 wl_list_insert(surface->configure_list.prev, &configure->link);
884 configure->serial =
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200885 wl_display_next_serial(weston_desktop_get_display(surface->desktop));
886
887 switch (surface->role) {
888 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
889 assert(0 && "not reached");
890 break;
891 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic749637a2017-07-18 12:59:14 +0200892 weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface,
893 (struct weston_desktop_xdg_toplevel_configure *) configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200894 break;
895 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
896 weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface);
897 break;
898 }
899
Quentin Glidic749637a2017-07-18 12:59:14 +0200900 zxdg_surface_v6_send_configure(surface->resource, configure->serial);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200901}
902
Quentin Glidicd51f8262017-04-13 20:25:27 +0200903static bool
904weston_desktop_xdg_toplevel_state_compare(struct weston_desktop_xdg_toplevel *toplevel)
905{
Quentin Glidicefade282017-10-17 16:14:49 +0200906 struct {
907 struct weston_desktop_xdg_toplevel_state state;
908 struct weston_size size;
909 } configured;
910
Quentin Glidic6914c802017-08-28 20:12:01 +0200911 if (!toplevel->base.configured)
912 return false;
913
Quentin Glidicefade282017-10-17 16:14:49 +0200914 if (wl_list_empty(&toplevel->base.configure_list)) {
915 /* Last configure is actually the current state, just use it */
916 configured.state = toplevel->current.state;
917 configured.size.width = toplevel->base.surface->width;
918 configured.size.height = toplevel->base.surface->height;
919 } else {
920 struct weston_desktop_xdg_toplevel_configure *configure =
921 wl_container_of(toplevel->base.configure_list.prev,
922 configure, base.link);
923
924 configured.state = configure->state;
925 configured.size = configure->size;
926 }
927
928 if (toplevel->pending.state.activated != configured.state.activated)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200929 return false;
Quentin Glidicefade282017-10-17 16:14:49 +0200930 if (toplevel->pending.state.fullscreen != configured.state.fullscreen)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200931 return false;
Quentin Glidicefade282017-10-17 16:14:49 +0200932 if (toplevel->pending.state.maximized != configured.state.maximized)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200933 return false;
Quentin Glidicefade282017-10-17 16:14:49 +0200934 if (toplevel->pending.state.resizing != configured.state.resizing)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200935 return false;
936
Quentin Glidicefade282017-10-17 16:14:49 +0200937 if (toplevel->pending.size.width == configured.size.width &&
938 toplevel->pending.size.height == configured.size.height)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200939 return true;
940
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200941 if (toplevel->pending.size.width == 0 &&
942 toplevel->pending.size.height == 0)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200943 return true;
944
945 return false;
946}
947
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200948static void
Quentin Glidic6914c802017-08-28 20:12:01 +0200949weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200950{
951 struct wl_display *display = weston_desktop_get_display(surface->desktop);
952 struct wl_event_loop *loop = wl_display_get_event_loop(display);
Quentin Glidic6914c802017-08-28 20:12:01 +0200953 bool pending_same = false;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200954
Quentin Glidicd51f8262017-04-13 20:25:27 +0200955 switch (surface->role) {
956 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
957 assert(0 && "not reached");
958 break;
959 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic6914c802017-08-28 20:12:01 +0200960 pending_same = weston_desktop_xdg_toplevel_state_compare((struct weston_desktop_xdg_toplevel *) surface);
Quentin Glidicd51f8262017-04-13 20:25:27 +0200961 break;
962 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
963 break;
964 }
965
966 if (surface->configure_idle != NULL) {
Quentin Glidic218126d2017-07-11 13:31:36 +0200967 if (!pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200968 return;
969
970 wl_event_source_remove(surface->configure_idle);
971 surface->configure_idle = NULL;
972 } else {
Quentin Glidic218126d2017-07-11 13:31:36 +0200973 if (pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200974 return;
975
976 surface->configure_idle =
977 wl_event_loop_add_idle(loop,
978 weston_desktop_xdg_surface_send_configure,
979 surface);
980 }
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200981}
982
983static void
984weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client,
985 struct wl_resource *resource,
986 uint32_t id)
987{
988 struct weston_desktop_surface *dsurface =
989 wl_resource_get_user_data(resource);
990 struct weston_surface *wsurface =
991 weston_desktop_surface_get_surface(dsurface);
992 struct weston_desktop_xdg_toplevel *toplevel =
993 weston_desktop_surface_get_implementation_data(dsurface);
994
995 if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role,
996 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
997 return;
998
999 toplevel->resource =
1000 weston_desktop_surface_add_resource(toplevel->base.desktop_surface,
1001 &zxdg_toplevel_v6_interface,
1002 &weston_desktop_xdg_toplevel_implementation,
1003 id, weston_desktop_xdg_toplevel_resource_destroy);
1004 if (toplevel->resource == NULL)
1005 return;
1006
1007 toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
1008}
1009
1010static void
1011weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client,
1012 struct wl_resource *resource,
1013 uint32_t id,
1014 struct wl_resource *parent_resource,
1015 struct wl_resource *positioner_resource)
1016{
1017 struct weston_desktop_surface *dsurface =
1018 wl_resource_get_user_data(resource);
1019 struct weston_surface *wsurface =
1020 weston_desktop_surface_get_surface(dsurface);
1021 struct weston_desktop_xdg_popup *popup =
1022 weston_desktop_surface_get_implementation_data(dsurface);
1023 struct weston_desktop_surface *parent_surface =
1024 wl_resource_get_user_data(parent_resource);
1025 struct weston_desktop_xdg_surface *parent =
1026 weston_desktop_surface_get_implementation_data(parent_surface);
1027 struct weston_desktop_xdg_positioner *positioner =
1028 wl_resource_get_user_data(positioner_resource);
1029
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02001030 /* Checking whether the size and anchor rect both have a positive size
1031 * is enough to verify both have been correctly set */
1032 if (positioner->size.width == 0 || positioner->anchor_rect.width == 0) {
1033 wl_resource_post_error(resource,
1034 ZXDG_SHELL_V6_ERROR_INVALID_POSITIONER,
1035 "positioner object is not complete");
1036 return;
1037 }
1038
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001039 if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
1040 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
1041 return;
1042
1043 popup->resource =
1044 weston_desktop_surface_add_resource(popup->base.desktop_surface,
1045 &zxdg_popup_v6_interface,
1046 &weston_desktop_xdg_popup_implementation,
1047 id, weston_desktop_xdg_popup_resource_destroy);
1048 if (popup->resource == NULL)
1049 return;
1050
1051 popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
1052 popup->parent = parent;
1053
1054 popup->geometry =
1055 weston_desktop_xdg_positioner_get_geometry(positioner,
1056 dsurface,
1057 parent_surface);
1058
1059 weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
1060 parent_surface,
1061 popup->geometry.x,
1062 popup->geometry.y,
1063 true);
1064}
1065
1066static bool
1067weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
1068{
1069 struct weston_surface *wsurface =
1070 weston_desktop_surface_get_surface(surface->desktop_surface);
1071 const char *role;
1072
1073 role = weston_surface_get_role(wsurface);
1074 if (role != NULL &&
1075 (role == weston_desktop_xdg_toplevel_role ||
1076 role == weston_desktop_xdg_popup_role))
1077 return true;
1078
1079 wl_resource_post_error(surface->resource,
1080 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1081 "xdg_surface must have a role");
1082 return false;
1083}
1084
1085static void
1086weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
1087 struct wl_resource *resource,
1088 int32_t x, int32_t y,
1089 int32_t width, int32_t height)
1090{
1091 struct weston_desktop_surface *dsurface =
1092 wl_resource_get_user_data(resource);
1093 struct weston_desktop_xdg_surface *surface =
1094 weston_desktop_surface_get_implementation_data(dsurface);
1095
1096 if (!weston_desktop_xdg_surface_check_role(surface))
1097 return;
1098
1099 surface->has_next_geometry = true;
1100 surface->next_geometry.x = x;
1101 surface->next_geometry.y = y;
1102 surface->next_geometry.width = width;
1103 surface->next_geometry.height = height;
1104}
1105
1106static void
1107weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
1108 struct wl_resource *resource,
1109 uint32_t serial)
1110{
1111 struct weston_desktop_surface *dsurface =
1112 wl_resource_get_user_data(resource);
1113 struct weston_desktop_xdg_surface *surface =
1114 weston_desktop_surface_get_implementation_data(dsurface);
Quentin Glidic749637a2017-07-18 12:59:14 +02001115 struct weston_desktop_xdg_surface_configure *configure, *temp;
1116 bool found = false;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001117
1118 if (!weston_desktop_xdg_surface_check_role(surface))
1119 return;
1120
Quentin Glidic749637a2017-07-18 12:59:14 +02001121 wl_list_for_each_safe(configure, temp, &surface->configure_list, link) {
1122 if (configure->serial < serial) {
1123 wl_list_remove(&configure->link);
1124 free(configure);
1125 } else if (configure->serial == serial) {
1126 wl_list_remove(&configure->link);
1127 found = true;
Derek Foremane3715522017-07-26 14:35:58 -05001128 break;
1129 } else {
1130 break;
Quentin Glidic749637a2017-07-18 12:59:14 +02001131 }
Quentin Glidic749637a2017-07-18 12:59:14 +02001132 }
1133 if (!found) {
1134 struct weston_desktop_client *client =
1135 weston_desktop_surface_get_client(dsurface);
1136 struct wl_resource *client_resource =
1137 weston_desktop_client_get_resource(client);
1138 wl_resource_post_error(client_resource,
1139 ZXDG_SHELL_V6_ERROR_INVALID_SURFACE_STATE,
1140 "Wrong configure serial: %u", serial);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001141 return;
Quentin Glidic749637a2017-07-18 12:59:14 +02001142 }
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001143
1144 surface->configured = true;
1145
1146 switch (surface->role) {
1147 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1148 assert(0 && "not reached");
1149 break;
1150 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic749637a2017-07-18 12:59:14 +02001151 weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface,
1152 (struct weston_desktop_xdg_toplevel_configure *) configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001153 break;
1154 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1155 break;
1156 }
Quentin Glidic749637a2017-07-18 12:59:14 +02001157
1158 free(configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001159}
1160
1161static void
1162weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
1163 uint32_t serial, void *user_data)
1164{
1165 struct weston_desktop_client *client =
1166 weston_desktop_surface_get_client(dsurface);
1167
1168 zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client),
1169 serial);
1170}
1171
1172static void
1173weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +02001174 void *user_data,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001175 int32_t sx, int32_t sy)
1176{
1177 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidiccba26e72016-08-15 12:20:22 +02001178 struct weston_surface *wsurface =
1179 weston_desktop_surface_get_surface (dsurface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001180
Quentin Glidiccba26e72016-08-15 12:20:22 +02001181 if (wsurface->buffer_ref.buffer && !surface->configured) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001182 wl_resource_post_error(surface->resource,
1183 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1184 "xdg_surface has never been configured");
1185 return;
1186 }
1187
1188 if (surface->has_next_geometry) {
1189 surface->has_next_geometry = false;
1190 weston_desktop_surface_set_geometry(surface->desktop_surface,
1191 surface->next_geometry);
1192 }
1193
1194 switch (surface->role) {
1195 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1196 wl_resource_post_error(surface->resource,
1197 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1198 "xdg_surface must have a role");
1199 break;
1200 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidiccba26e72016-08-15 12:20:22 +02001201 weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001202 break;
1203 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1204 weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1205 break;
1206 }
1207}
1208
1209static void
1210weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1211 void *user_data)
1212{
1213 struct weston_desktop_xdg_surface *surface = user_data;
1214
1215 switch (surface->role) {
1216 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1217 assert(0 && "not reached");
1218 break;
1219 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1220 weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1221 break;
1222 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1223 weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1224 break;
1225 }
1226}
1227
1228static void
1229weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1230 void *user_data)
1231{
1232 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidic749637a2017-07-18 12:59:14 +02001233 struct weston_desktop_xdg_surface_configure *configure, *temp;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001234
1235 switch (surface->role) {
1236 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1237 break;
1238 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1239 weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1240 break;
1241 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1242 weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1243 break;
1244 }
1245
1246 if (surface->configure_idle != NULL)
1247 wl_event_source_remove(surface->configure_idle);
1248
Quentin Glidic749637a2017-07-18 12:59:14 +02001249 wl_list_for_each_safe(configure, temp, &surface->configure_list, link)
1250 free(configure);
1251
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001252 free(surface);
1253}
1254
1255static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = {
1256 .destroy = weston_desktop_destroy_request,
1257 .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel,
1258 .get_popup = weston_desktop_xdg_surface_protocol_get_popup,
1259 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1260 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
1261};
1262
1263static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1264 /* These are used for toplevel only */
1265 .set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1266 .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1267 .set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1268 .set_activated = weston_desktop_xdg_toplevel_set_activated,
1269 .set_size = weston_desktop_xdg_toplevel_set_size,
1270
1271 .get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1272 .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1273 .get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1274 .get_activated = weston_desktop_xdg_toplevel_get_activated,
1275
1276 /* These are used for popup only */
1277 .update_position = weston_desktop_xdg_popup_update_position,
1278
1279 /* Common API */
1280 .committed = weston_desktop_xdg_surface_committed,
1281 .ping = weston_desktop_xdg_surface_ping,
1282 .close = weston_desktop_xdg_surface_close,
1283
1284 .destroy = weston_desktop_xdg_surface_destroy,
1285};
1286
1287static void
1288weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1289 struct wl_resource *resource,
1290 uint32_t id)
1291{
1292 struct weston_desktop_client *client =
1293 wl_resource_get_user_data(resource);
1294 struct weston_desktop_xdg_positioner *positioner;
1295
1296 positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1297 if (positioner == NULL) {
1298 wl_client_post_no_memory(wl_client);
1299 return;
1300 }
1301
1302 positioner->client = client;
1303 positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1304
1305 positioner->resource =
1306 wl_resource_create(wl_client,
1307 &zxdg_positioner_v6_interface,
1308 wl_resource_get_version(resource), id);
1309 if (positioner->resource == NULL) {
1310 wl_client_post_no_memory(wl_client);
1311 free(positioner);
1312 return;
1313 }
1314 wl_resource_set_implementation(positioner->resource,
1315 &weston_desktop_xdg_positioner_implementation,
1316 positioner, weston_desktop_xdg_positioner_destroy);
1317}
1318
1319static void
1320weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1321{
1322 struct weston_desktop_surface *dsurface =
1323 wl_resource_get_user_data(resource);
1324
1325 if (dsurface != NULL)
1326 weston_desktop_surface_resource_destroy(resource);
1327}
1328
1329static void
1330weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1331 struct wl_resource *resource,
1332 uint32_t id,
1333 struct wl_resource *surface_resource)
1334{
1335 struct weston_desktop_client *client =
1336 wl_resource_get_user_data(resource);
1337 struct weston_surface *wsurface =
1338 wl_resource_get_user_data(surface_resource);
1339 struct weston_desktop_xdg_surface *surface;
1340
1341 surface = zalloc(weston_desktop_surface_role_biggest_size);
1342 if (surface == NULL) {
1343 wl_client_post_no_memory(wl_client);
1344 return;
1345 }
1346
1347 surface->desktop = weston_desktop_client_get_desktop(client);
1348 surface->surface = wsurface;
1349
1350 surface->desktop_surface =
1351 weston_desktop_surface_create(surface->desktop, client,
1352 surface->surface,
1353 &weston_desktop_xdg_surface_internal_implementation,
1354 surface);
1355 if (surface->desktop_surface == NULL) {
1356 free(surface);
1357 return;
1358 }
1359
1360 surface->resource =
1361 weston_desktop_surface_add_resource(surface->desktop_surface,
1362 &zxdg_surface_v6_interface,
1363 &weston_desktop_xdg_surface_implementation,
1364 id, weston_desktop_xdg_surface_resource_destroy);
1365 if (surface->resource == NULL)
1366 return;
1367
1368 if (wsurface->buffer_ref.buffer != NULL) {
1369 wl_resource_post_error(surface->resource,
1370 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1371 "xdg_surface must not have a buffer at creation");
1372 return;
1373 }
Quentin Glidic749637a2017-07-18 12:59:14 +02001374
1375 wl_list_init(&surface->configure_list);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001376}
1377
1378static void
1379weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1380 struct wl_resource *resource,
1381 uint32_t serial)
1382{
1383 struct weston_desktop_client *client =
1384 wl_resource_get_user_data(resource);
1385
1386 weston_desktop_client_pong(client, serial);
1387}
1388
1389static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = {
1390 .destroy = weston_desktop_destroy_request,
1391 .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1392 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1393 .pong = weston_desktop_xdg_shell_protocol_pong,
1394};
1395
1396static void
1397weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1398 uint32_t version, uint32_t id)
1399{
1400 struct weston_desktop *desktop = data;
1401
1402 weston_desktop_client_create(desktop, client, NULL,
1403 &zxdg_shell_v6_interface,
1404 &weston_desktop_xdg_shell_implementation,
1405 version, id);
1406}
1407
1408struct wl_global *
1409weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display)
1410{
1411 return wl_global_create(display, &zxdg_shell_v6_interface,
1412 WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1413 weston_desktop_xdg_shell_bind);
1414}