blob: de5d3e0585b32aa43cc169e6bfdd592d2b5f7a75 [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)
510 output = wl_resource_get_user_data(output_resource);
511
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
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200647 if ((toplevel->next.state.maximized || toplevel->next.state.fullscreen) &&
Quentin Glidicac394a12017-07-12 09:45:43 +0200648 (toplevel->next.size.width != wsurface->width ||
649 toplevel->next.size.height != wsurface->height)) {
Quentin Glidicc84423b2017-03-10 11:50:41 +0100650 struct weston_desktop_client *client =
651 weston_desktop_surface_get_client(toplevel->base.desktop_surface);
652 struct wl_resource *client_resource =
653 weston_desktop_client_get_resource(client);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200654
Quentin Glidicc84423b2017-03-10 11:50:41 +0100655 wl_resource_post_error(client_resource,
656 ZXDG_SHELL_V6_ERROR_INVALID_SURFACE_STATE,
657 "xdg_surface buffer does not match the configured state");
658 return;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200659 }
Quentin Glidicc84423b2017-03-10 11:50:41 +0100660
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200661 toplevel->current.state = toplevel->next.state;
662 toplevel->current.min_size = toplevel->next.min_size;
663 toplevel->current.max_size = toplevel->next.max_size;
Quentin Glidicc84423b2017-03-10 11:50:41 +0100664
665 weston_desktop_api_committed(toplevel->base.desktop,
666 toplevel->base.desktop_surface,
667 sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200668}
669
670static void
671weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel)
672{
673 zxdg_toplevel_v6_send_close(toplevel->resource);
674}
675
676static bool
677weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface,
678 void *user_data)
679{
680 struct weston_desktop_xdg_toplevel *toplevel = user_data;
681
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200682 return toplevel->current.state.maximized;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200683}
684
685static bool
686weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface,
687 void *user_data)
688{
689 struct weston_desktop_xdg_toplevel *toplevel = user_data;
690
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200691 return toplevel->current.state.fullscreen;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200692}
693
694static bool
695weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface,
696 void *user_data)
697{
698 struct weston_desktop_xdg_toplevel *toplevel = user_data;
699
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200700 return toplevel->current.state.resizing;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200701}
702
703static bool
704weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface,
705 void *user_data)
706{
707 struct weston_desktop_xdg_toplevel *toplevel = user_data;
708
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200709 return toplevel->current.state.activated;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200710}
711
712static void
713weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel)
714{
715 if (toplevel->added)
716 weston_desktop_api_surface_removed(toplevel->base.desktop,
717 toplevel->base.desktop_surface);
718}
719
720static void
721weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource)
722{
723 struct weston_desktop_surface *dsurface =
724 wl_resource_get_user_data(resource);
725
726 if (dsurface != NULL)
727 weston_desktop_surface_resource_destroy(resource);
728}
729
730static const struct zxdg_toplevel_v6_interface weston_desktop_xdg_toplevel_implementation = {
731 .destroy = weston_desktop_destroy_request,
732 .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent,
733 .set_title = weston_desktop_xdg_toplevel_protocol_set_title,
734 .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id,
735 .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu,
736 .move = weston_desktop_xdg_toplevel_protocol_move,
737 .resize = weston_desktop_xdg_toplevel_protocol_resize,
738 .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size,
739 .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size,
740 .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized,
741 .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized,
742 .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen,
743 .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen,
744 .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized,
745};
746
747static void
748weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client,
749 struct wl_resource *resource,
750 struct wl_resource *seat_resource,
751 uint32_t serial)
752{
753 struct weston_desktop_surface *dsurface =
754 wl_resource_get_user_data(resource);
755 struct weston_desktop_xdg_popup *popup =
756 weston_desktop_surface_get_implementation_data(dsurface);
757 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
758 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
759 struct weston_desktop_surface *topmost;
760 bool parent_is_toplevel =
761 popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
762
763 if (popup->committed) {
764 wl_resource_post_error(popup->resource,
765 ZXDG_POPUP_V6_ERROR_INVALID_GRAB,
766 "xdg_popup already is mapped");
767 return;
768 }
769
770 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
771 if ((topmost == NULL && !parent_is_toplevel) ||
772 (topmost != NULL && topmost != popup->parent->desktop_surface)) {
773 struct weston_desktop_client *client =
774 weston_desktop_surface_get_client(dsurface);
775 struct wl_resource *client_resource =
776 weston_desktop_client_get_resource(client);
777
778 wl_resource_post_error(client_resource,
779 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
780 "xdg_popup was not created on the topmost popup");
781 return;
782 }
783
784 popup->seat = seat;
785 weston_desktop_surface_popup_grab(popup->base.desktop_surface,
786 popup->seat, serial);
787}
788
789static void
790weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup)
791{
792 zxdg_popup_v6_send_configure(popup->resource,
793 popup->geometry.x,
794 popup->geometry.y,
795 popup->geometry.width,
796 popup->geometry.height);
797}
798
799static void
800weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
801 void *user_data);
802
803static void
804weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup)
805{
806 if (!popup->committed)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200807 weston_desktop_xdg_surface_schedule_configure(&popup->base, true);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200808 popup->committed = true;
809 weston_desktop_xdg_popup_update_position(popup->base.desktop_surface,
810 popup);
811}
812
813static void
814weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
815 void *user_data)
816{
817}
818
819static void
820weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup)
821{
822 zxdg_popup_v6_send_popup_done(popup->resource);
823}
824
825static void
826weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup)
827{
828 struct weston_desktop_surface *topmost;
829 struct weston_desktop_client *client =
830 weston_desktop_surface_get_client(popup->base.desktop_surface);
831
832 if (!weston_desktop_surface_get_grab(popup->base.desktop_surface))
833 return;
834
835 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
836 if (topmost != popup->base.desktop_surface) {
837 struct wl_resource *client_resource =
838 weston_desktop_client_get_resource(client);
839
840 wl_resource_post_error(client_resource,
841 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
842 "xdg_popup was destroyed while it was not the topmost popup.");
843 }
844
845 weston_desktop_surface_popup_ungrab(popup->base.desktop_surface,
846 popup->seat);
847}
848
849static void
850weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource)
851{
852 struct weston_desktop_surface *dsurface =
853 wl_resource_get_user_data(resource);
854
855 if (dsurface != NULL)
856 weston_desktop_surface_resource_destroy(resource);
857}
858
859static const struct zxdg_popup_v6_interface weston_desktop_xdg_popup_implementation = {
860 .destroy = weston_desktop_destroy_request,
861 .grab = weston_desktop_xdg_popup_protocol_grab,
862};
863
864static void
865weston_desktop_xdg_surface_send_configure(void *user_data)
866{
867 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidic749637a2017-07-18 12:59:14 +0200868 struct weston_desktop_xdg_surface_configure *configure;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200869
870 surface->configure_idle = NULL;
Quentin Glidic749637a2017-07-18 12:59:14 +0200871
872 configure = zalloc(weston_desktop_surface_configure_biggest_size);
873 if (configure == NULL) {
874 struct weston_desktop_client *client =
875 weston_desktop_surface_get_client(surface->desktop_surface);
876 struct wl_client *wl_client =
877 weston_desktop_client_get_client(client);
878 wl_client_post_no_memory(wl_client);
879 return;
880 }
881 wl_list_insert(surface->configure_list.prev, &configure->link);
882 configure->serial =
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200883 wl_display_next_serial(weston_desktop_get_display(surface->desktop));
884
885 switch (surface->role) {
886 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
887 assert(0 && "not reached");
888 break;
889 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic749637a2017-07-18 12:59:14 +0200890 weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface,
891 (struct weston_desktop_xdg_toplevel_configure *) configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200892 break;
893 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
894 weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface);
895 break;
896 }
897
Quentin Glidic749637a2017-07-18 12:59:14 +0200898 zxdg_surface_v6_send_configure(surface->resource, configure->serial);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200899}
900
Quentin Glidicd51f8262017-04-13 20:25:27 +0200901static bool
902weston_desktop_xdg_toplevel_state_compare(struct weston_desktop_xdg_toplevel *toplevel)
903{
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200904 if (toplevel->pending.state.activated != toplevel->current.state.activated)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200905 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200906 if (toplevel->pending.state.fullscreen != toplevel->current.state.fullscreen)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200907 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200908 if (toplevel->pending.state.maximized != toplevel->current.state.maximized)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200909 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200910 if (toplevel->pending.state.resizing != toplevel->current.state.resizing)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200911 return false;
912
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200913 if (toplevel->base.surface->width == toplevel->pending.size.width &&
914 toplevel->base.surface->height == toplevel->pending.size.height)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200915 return true;
916
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200917 if (toplevel->pending.size.width == 0 &&
918 toplevel->pending.size.height == 0)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200919 return true;
920
921 return false;
922}
923
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200924static void
Quentin Glidicd51f8262017-04-13 20:25:27 +0200925weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface,
926 bool force)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200927{
928 struct wl_display *display = weston_desktop_get_display(surface->desktop);
929 struct wl_event_loop *loop = wl_display_get_event_loop(display);
Quentin Glidic218126d2017-07-11 13:31:36 +0200930 bool pending_same = !force;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200931
Quentin Glidicd51f8262017-04-13 20:25:27 +0200932 switch (surface->role) {
933 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
934 assert(0 && "not reached");
935 break;
936 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic218126d2017-07-11 13:31:36 +0200937 pending_same = pending_same &&
Quentin Glidicd51f8262017-04-13 20:25:27 +0200938 weston_desktop_xdg_toplevel_state_compare((struct weston_desktop_xdg_toplevel *) surface);
939 break;
940 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
941 break;
942 }
943
944 if (surface->configure_idle != NULL) {
Quentin Glidic218126d2017-07-11 13:31:36 +0200945 if (!pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200946 return;
947
948 wl_event_source_remove(surface->configure_idle);
949 surface->configure_idle = NULL;
950 } else {
Quentin Glidic218126d2017-07-11 13:31:36 +0200951 if (pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200952 return;
953
954 surface->configure_idle =
955 wl_event_loop_add_idle(loop,
956 weston_desktop_xdg_surface_send_configure,
957 surface);
958 }
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200959}
960
961static void
962weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client,
963 struct wl_resource *resource,
964 uint32_t id)
965{
966 struct weston_desktop_surface *dsurface =
967 wl_resource_get_user_data(resource);
968 struct weston_surface *wsurface =
969 weston_desktop_surface_get_surface(dsurface);
970 struct weston_desktop_xdg_toplevel *toplevel =
971 weston_desktop_surface_get_implementation_data(dsurface);
972
973 if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role,
974 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
975 return;
976
977 toplevel->resource =
978 weston_desktop_surface_add_resource(toplevel->base.desktop_surface,
979 &zxdg_toplevel_v6_interface,
980 &weston_desktop_xdg_toplevel_implementation,
981 id, weston_desktop_xdg_toplevel_resource_destroy);
982 if (toplevel->resource == NULL)
983 return;
984
985 toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
986}
987
988static void
989weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client,
990 struct wl_resource *resource,
991 uint32_t id,
992 struct wl_resource *parent_resource,
993 struct wl_resource *positioner_resource)
994{
995 struct weston_desktop_surface *dsurface =
996 wl_resource_get_user_data(resource);
997 struct weston_surface *wsurface =
998 weston_desktop_surface_get_surface(dsurface);
999 struct weston_desktop_xdg_popup *popup =
1000 weston_desktop_surface_get_implementation_data(dsurface);
1001 struct weston_desktop_surface *parent_surface =
1002 wl_resource_get_user_data(parent_resource);
1003 struct weston_desktop_xdg_surface *parent =
1004 weston_desktop_surface_get_implementation_data(parent_surface);
1005 struct weston_desktop_xdg_positioner *positioner =
1006 wl_resource_get_user_data(positioner_resource);
1007
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +02001008 /* Checking whether the size and anchor rect both have a positive size
1009 * is enough to verify both have been correctly set */
1010 if (positioner->size.width == 0 || positioner->anchor_rect.width == 0) {
1011 wl_resource_post_error(resource,
1012 ZXDG_SHELL_V6_ERROR_INVALID_POSITIONER,
1013 "positioner object is not complete");
1014 return;
1015 }
1016
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001017 if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
1018 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
1019 return;
1020
1021 popup->resource =
1022 weston_desktop_surface_add_resource(popup->base.desktop_surface,
1023 &zxdg_popup_v6_interface,
1024 &weston_desktop_xdg_popup_implementation,
1025 id, weston_desktop_xdg_popup_resource_destroy);
1026 if (popup->resource == NULL)
1027 return;
1028
1029 popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
1030 popup->parent = parent;
1031
1032 popup->geometry =
1033 weston_desktop_xdg_positioner_get_geometry(positioner,
1034 dsurface,
1035 parent_surface);
1036
1037 weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
1038 parent_surface,
1039 popup->geometry.x,
1040 popup->geometry.y,
1041 true);
1042}
1043
1044static bool
1045weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
1046{
1047 struct weston_surface *wsurface =
1048 weston_desktop_surface_get_surface(surface->desktop_surface);
1049 const char *role;
1050
1051 role = weston_surface_get_role(wsurface);
1052 if (role != NULL &&
1053 (role == weston_desktop_xdg_toplevel_role ||
1054 role == weston_desktop_xdg_popup_role))
1055 return true;
1056
1057 wl_resource_post_error(surface->resource,
1058 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1059 "xdg_surface must have a role");
1060 return false;
1061}
1062
1063static void
1064weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
1065 struct wl_resource *resource,
1066 int32_t x, int32_t y,
1067 int32_t width, int32_t height)
1068{
1069 struct weston_desktop_surface *dsurface =
1070 wl_resource_get_user_data(resource);
1071 struct weston_desktop_xdg_surface *surface =
1072 weston_desktop_surface_get_implementation_data(dsurface);
1073
1074 if (!weston_desktop_xdg_surface_check_role(surface))
1075 return;
1076
1077 surface->has_next_geometry = true;
1078 surface->next_geometry.x = x;
1079 surface->next_geometry.y = y;
1080 surface->next_geometry.width = width;
1081 surface->next_geometry.height = height;
1082}
1083
1084static void
1085weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
1086 struct wl_resource *resource,
1087 uint32_t serial)
1088{
1089 struct weston_desktop_surface *dsurface =
1090 wl_resource_get_user_data(resource);
1091 struct weston_desktop_xdg_surface *surface =
1092 weston_desktop_surface_get_implementation_data(dsurface);
Quentin Glidic749637a2017-07-18 12:59:14 +02001093 struct weston_desktop_xdg_surface_configure *configure, *temp;
1094 bool found = false;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001095
1096 if (!weston_desktop_xdg_surface_check_role(surface))
1097 return;
1098
Quentin Glidic749637a2017-07-18 12:59:14 +02001099 wl_list_for_each_safe(configure, temp, &surface->configure_list, link) {
1100 if (configure->serial < serial) {
1101 wl_list_remove(&configure->link);
1102 free(configure);
1103 } else if (configure->serial == serial) {
1104 wl_list_remove(&configure->link);
1105 found = true;
1106 }
1107 break;
1108 }
1109 if (!found) {
1110 struct weston_desktop_client *client =
1111 weston_desktop_surface_get_client(dsurface);
1112 struct wl_resource *client_resource =
1113 weston_desktop_client_get_resource(client);
1114 wl_resource_post_error(client_resource,
1115 ZXDG_SHELL_V6_ERROR_INVALID_SURFACE_STATE,
1116 "Wrong configure serial: %u", serial);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001117 return;
Quentin Glidic749637a2017-07-18 12:59:14 +02001118 }
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001119
1120 surface->configured = true;
1121
1122 switch (surface->role) {
1123 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1124 assert(0 && "not reached");
1125 break;
1126 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic749637a2017-07-18 12:59:14 +02001127 weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface,
1128 (struct weston_desktop_xdg_toplevel_configure *) configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001129 break;
1130 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1131 break;
1132 }
Quentin Glidic749637a2017-07-18 12:59:14 +02001133
1134 free(configure);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001135}
1136
1137static void
1138weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
1139 uint32_t serial, void *user_data)
1140{
1141 struct weston_desktop_client *client =
1142 weston_desktop_surface_get_client(dsurface);
1143
1144 zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client),
1145 serial);
1146}
1147
1148static void
1149weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +02001150 void *user_data,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001151 int32_t sx, int32_t sy)
1152{
1153 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidiccba26e72016-08-15 12:20:22 +02001154 struct weston_surface *wsurface =
1155 weston_desktop_surface_get_surface (dsurface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001156
Quentin Glidiccba26e72016-08-15 12:20:22 +02001157 if (wsurface->buffer_ref.buffer && !surface->configured) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001158 wl_resource_post_error(surface->resource,
1159 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1160 "xdg_surface has never been configured");
1161 return;
1162 }
1163
1164 if (surface->has_next_geometry) {
1165 surface->has_next_geometry = false;
1166 weston_desktop_surface_set_geometry(surface->desktop_surface,
1167 surface->next_geometry);
1168 }
1169
1170 switch (surface->role) {
1171 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1172 wl_resource_post_error(surface->resource,
1173 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1174 "xdg_surface must have a role");
1175 break;
1176 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidiccba26e72016-08-15 12:20:22 +02001177 weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001178 break;
1179 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1180 weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1181 break;
1182 }
1183}
1184
1185static void
1186weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1187 void *user_data)
1188{
1189 struct weston_desktop_xdg_surface *surface = user_data;
1190
1191 switch (surface->role) {
1192 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1193 assert(0 && "not reached");
1194 break;
1195 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1196 weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1197 break;
1198 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1199 weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1200 break;
1201 }
1202}
1203
1204static void
1205weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1206 void *user_data)
1207{
1208 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidic749637a2017-07-18 12:59:14 +02001209 struct weston_desktop_xdg_surface_configure *configure, *temp;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001210
1211 switch (surface->role) {
1212 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1213 break;
1214 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1215 weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1216 break;
1217 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1218 weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1219 break;
1220 }
1221
1222 if (surface->configure_idle != NULL)
1223 wl_event_source_remove(surface->configure_idle);
1224
Quentin Glidic749637a2017-07-18 12:59:14 +02001225 wl_list_for_each_safe(configure, temp, &surface->configure_list, link)
1226 free(configure);
1227
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001228 free(surface);
1229}
1230
1231static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = {
1232 .destroy = weston_desktop_destroy_request,
1233 .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel,
1234 .get_popup = weston_desktop_xdg_surface_protocol_get_popup,
1235 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1236 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
1237};
1238
1239static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1240 /* These are used for toplevel only */
1241 .set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1242 .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1243 .set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1244 .set_activated = weston_desktop_xdg_toplevel_set_activated,
1245 .set_size = weston_desktop_xdg_toplevel_set_size,
1246
1247 .get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1248 .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1249 .get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1250 .get_activated = weston_desktop_xdg_toplevel_get_activated,
1251
1252 /* These are used for popup only */
1253 .update_position = weston_desktop_xdg_popup_update_position,
1254
1255 /* Common API */
1256 .committed = weston_desktop_xdg_surface_committed,
1257 .ping = weston_desktop_xdg_surface_ping,
1258 .close = weston_desktop_xdg_surface_close,
1259
1260 .destroy = weston_desktop_xdg_surface_destroy,
1261};
1262
1263static void
1264weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1265 struct wl_resource *resource,
1266 uint32_t id)
1267{
1268 struct weston_desktop_client *client =
1269 wl_resource_get_user_data(resource);
1270 struct weston_desktop_xdg_positioner *positioner;
1271
1272 positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1273 if (positioner == NULL) {
1274 wl_client_post_no_memory(wl_client);
1275 return;
1276 }
1277
1278 positioner->client = client;
1279 positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1280
1281 positioner->resource =
1282 wl_resource_create(wl_client,
1283 &zxdg_positioner_v6_interface,
1284 wl_resource_get_version(resource), id);
1285 if (positioner->resource == NULL) {
1286 wl_client_post_no_memory(wl_client);
1287 free(positioner);
1288 return;
1289 }
1290 wl_resource_set_implementation(positioner->resource,
1291 &weston_desktop_xdg_positioner_implementation,
1292 positioner, weston_desktop_xdg_positioner_destroy);
1293}
1294
1295static void
1296weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1297{
1298 struct weston_desktop_surface *dsurface =
1299 wl_resource_get_user_data(resource);
1300
1301 if (dsurface != NULL)
1302 weston_desktop_surface_resource_destroy(resource);
1303}
1304
1305static void
1306weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1307 struct wl_resource *resource,
1308 uint32_t id,
1309 struct wl_resource *surface_resource)
1310{
1311 struct weston_desktop_client *client =
1312 wl_resource_get_user_data(resource);
1313 struct weston_surface *wsurface =
1314 wl_resource_get_user_data(surface_resource);
1315 struct weston_desktop_xdg_surface *surface;
1316
1317 surface = zalloc(weston_desktop_surface_role_biggest_size);
1318 if (surface == NULL) {
1319 wl_client_post_no_memory(wl_client);
1320 return;
1321 }
1322
1323 surface->desktop = weston_desktop_client_get_desktop(client);
1324 surface->surface = wsurface;
1325
1326 surface->desktop_surface =
1327 weston_desktop_surface_create(surface->desktop, client,
1328 surface->surface,
1329 &weston_desktop_xdg_surface_internal_implementation,
1330 surface);
1331 if (surface->desktop_surface == NULL) {
1332 free(surface);
1333 return;
1334 }
1335
1336 surface->resource =
1337 weston_desktop_surface_add_resource(surface->desktop_surface,
1338 &zxdg_surface_v6_interface,
1339 &weston_desktop_xdg_surface_implementation,
1340 id, weston_desktop_xdg_surface_resource_destroy);
1341 if (surface->resource == NULL)
1342 return;
1343
1344 if (wsurface->buffer_ref.buffer != NULL) {
1345 wl_resource_post_error(surface->resource,
1346 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1347 "xdg_surface must not have a buffer at creation");
1348 return;
1349 }
Quentin Glidic749637a2017-07-18 12:59:14 +02001350
1351 wl_list_init(&surface->configure_list);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001352}
1353
1354static void
1355weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1356 struct wl_resource *resource,
1357 uint32_t serial)
1358{
1359 struct weston_desktop_client *client =
1360 wl_resource_get_user_data(resource);
1361
1362 weston_desktop_client_pong(client, serial);
1363}
1364
1365static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = {
1366 .destroy = weston_desktop_destroy_request,
1367 .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1368 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1369 .pong = weston_desktop_xdg_shell_protocol_pong,
1370};
1371
1372static void
1373weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1374 uint32_t version, uint32_t id)
1375{
1376 struct weston_desktop *desktop = data;
1377
1378 weston_desktop_client_create(desktop, client, NULL,
1379 &zxdg_shell_v6_interface,
1380 &weston_desktop_xdg_shell_implementation,
1381 version, id);
1382}
1383
1384struct wl_global *
1385weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display)
1386{
1387 return wl_global_create(display, &zxdg_shell_v6_interface,
1388 WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1389 weston_desktop_xdg_shell_bind);
1390}