blob: 52f89c045b23ddc80c6b1f0d82cc101451957287 [file] [log] [blame]
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001/*
2 * Copyright © 2010-2012 Intel Corporation
3 * Copyright © 2011-2012 Collabora, Ltd.
4 * Copyright © 2013 Raspberry Pi Foundation
5 * Copyright © 2016 Quentin "Sardem FF7" Glidic
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 */
26
27#include "config.h"
28
29#include <stdbool.h>
30#include <assert.h>
31
32#include <wayland-server.h>
33
34#include "compositor.h"
35#include "zalloc.h"
Daniel Stone7dbb0e12016-11-24 15:30:41 +000036#include "xdg-shell-unstable-v6-server-protocol.h"
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +020037
38#include "libweston-desktop.h"
39#include "internal.h"
40
41#define WD_XDG_SHELL_PROTOCOL_VERSION 1
42
43static const char *weston_desktop_xdg_toplevel_role = "xdg_toplevel";
44static const char *weston_desktop_xdg_popup_role = "xdg_popup";
45
46struct weston_desktop_xdg_positioner {
47 struct weston_desktop *desktop;
48 struct weston_desktop_client *client;
49 struct wl_resource *resource;
50
51 struct weston_size size;
52 struct weston_geometry anchor_rect;
53 enum zxdg_positioner_v6_anchor anchor;
54 enum zxdg_positioner_v6_gravity gravity;
55 enum zxdg_positioner_v6_constraint_adjustment constraint_adjustment;
56 struct weston_position offset;
57};
58
59enum weston_desktop_xdg_surface_role {
60 WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE,
61 WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL,
62 WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP,
63};
64
65struct weston_desktop_xdg_surface {
66 struct wl_resource *resource;
67 struct weston_desktop *desktop;
68 struct weston_surface *surface;
69 struct weston_desktop_surface *desktop_surface;
70 bool configured;
71 struct wl_event_source *configure_idle;
72 uint32_t configure_serial;
73
74 bool has_next_geometry;
75 struct weston_geometry next_geometry;
76
77 enum weston_desktop_xdg_surface_role role;
78};
79
80struct weston_desktop_xdg_toplevel {
81 struct weston_desktop_xdg_surface base;
82
83 struct wl_resource *resource;
84 bool added;
Quentin Glidic218126d2017-07-11 13:31:36 +020085 struct weston_size pending_size;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +020086 struct {
87 bool maximized;
88 bool fullscreen;
89 bool resizing;
90 bool activated;
Quentin Glidic218126d2017-07-11 13:31:36 +020091 } pending_state, next_state, state;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +020092 struct weston_size
93 next_max_size, max_size,
94 next_min_size, min_size;
95};
96
97struct weston_desktop_xdg_popup {
98 struct weston_desktop_xdg_surface base;
99
100 struct wl_resource *resource;
101 bool committed;
102 struct weston_desktop_xdg_surface *parent;
103 struct weston_desktop_seat *seat;
104 struct weston_geometry geometry;
105};
106
107#define weston_desktop_surface_role_biggest_size (sizeof(struct weston_desktop_xdg_toplevel))
108
109
110static struct weston_geometry
111weston_desktop_xdg_positioner_get_geometry(struct weston_desktop_xdg_positioner *positioner,
112 struct weston_desktop_surface *dsurface,
113 struct weston_desktop_surface *parent)
114{
115 struct weston_geometry geometry = {
116 .x = positioner->offset.x,
117 .y = positioner->offset.y,
118 .width = positioner->size.width,
119 .height = positioner->size.height,
120 };
121
122 if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP)
123 geometry.y += positioner->anchor_rect.y;
124 else if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)
125 geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height;
126 else
127 geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height / 2;
128
129 if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT)
130 geometry.x += positioner->anchor_rect.x;
131 else if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT)
132 geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width;
133 else
134 geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width / 2;
135
136 if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP)
137 geometry.y -= geometry.height;
138 else if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)
139 geometry.y = geometry.y;
140 else
141 geometry.y -= geometry.height / 2;
142
143 if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT)
144 geometry.x -= geometry.width;
145 else if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT)
146 geometry.x = geometry.x;
147 else
148 geometry.x -= geometry.width / 2;
149
150 if (positioner->constraint_adjustment == ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_NONE)
151 return geometry;
152
153 /* TODO: add compositor policy configuration and the code here */
154
155 return geometry;
156}
157
158static void
159weston_desktop_xdg_positioner_protocol_set_size(struct wl_client *wl_client,
160 struct wl_resource *resource,
161 int32_t width, int32_t height)
162{
163 struct weston_desktop_xdg_positioner *positioner =
164 wl_resource_get_user_data(resource);
165
166 if (width < 1 || height < 1) {
167 wl_resource_post_error(resource,
168 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
169 "width and height must be positives and non-zero");
170 return;
171 }
172
173 positioner->size.width = width;
174 positioner->size.height = height;
175}
176
177static void
178weston_desktop_xdg_positioner_protocol_set_anchor_rect(struct wl_client *wl_client,
179 struct wl_resource *resource,
180 int32_t x, int32_t y,
181 int32_t width, int32_t height)
182{
183 struct weston_desktop_xdg_positioner *positioner =
184 wl_resource_get_user_data(resource);
185
186 if (width < 1 || height < 1) {
187 wl_resource_post_error(resource,
188 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
189 "width and height must be positives and non-zero");
190 return;
191 }
192
193 positioner->anchor_rect.x = x;
194 positioner->anchor_rect.y = y;
195 positioner->anchor_rect.width = width;
196 positioner->anchor_rect.height = height;
197}
198
199static void
200weston_desktop_xdg_positioner_protocol_set_anchor(struct wl_client *wl_client,
201 struct wl_resource *resource,
202 enum zxdg_positioner_v6_anchor anchor)
203{
204 struct weston_desktop_xdg_positioner *positioner =
205 wl_resource_get_user_data(resource);
206
207 if (((anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP ) &&
208 (anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)) ||
209 ((anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT) &&
210 (anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT))) {
211 wl_resource_post_error(resource,
212 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
213 "same-axis values are not allowed");
214 return;
215 }
216
217 positioner->anchor = anchor;
218}
219
220static void
221weston_desktop_xdg_positioner_protocol_set_gravity(struct wl_client *wl_client,
222 struct wl_resource *resource,
223 enum zxdg_positioner_v6_gravity gravity)
224{
225 struct weston_desktop_xdg_positioner *positioner =
226 wl_resource_get_user_data(resource);
227
228 if (((gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP) &&
229 (gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)) ||
230 ((gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT) &&
231 (gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT))) {
232 wl_resource_post_error(resource,
233 ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT,
234 "same-axis values are not allowed");
235 return;
236 }
237
238 positioner->gravity = gravity;
239}
240
241static void
242weston_desktop_xdg_positioner_protocol_set_constraint_adjustment(struct wl_client *wl_client,
243 struct wl_resource *resource,
244 enum zxdg_positioner_v6_constraint_adjustment constraint_adjustment)
245{
246 struct weston_desktop_xdg_positioner *positioner =
247 wl_resource_get_user_data(resource);
248
249 positioner->constraint_adjustment = constraint_adjustment;
250}
251
252static void
253weston_desktop_xdg_positioner_protocol_set_offset(struct wl_client *wl_client,
254 struct wl_resource *resource,
255 int32_t x, int32_t y)
256{
257 struct weston_desktop_xdg_positioner *positioner =
258 wl_resource_get_user_data(resource);
259
260 positioner->offset.x = x;
261 positioner->offset.y = y;
262}
263
264static void
265weston_desktop_xdg_positioner_destroy(struct wl_resource *resource)
266{
267 struct weston_desktop_xdg_positioner *positioner =
268 wl_resource_get_user_data(resource);
269
270 free(positioner);
271}
272
273static const struct zxdg_positioner_v6_interface weston_desktop_xdg_positioner_implementation = {
274 .destroy = weston_desktop_destroy_request,
275 .set_size = weston_desktop_xdg_positioner_protocol_set_size,
276 .set_anchor_rect = weston_desktop_xdg_positioner_protocol_set_anchor_rect,
277 .set_anchor = weston_desktop_xdg_positioner_protocol_set_anchor,
278 .set_gravity = weston_desktop_xdg_positioner_protocol_set_gravity,
279 .set_constraint_adjustment = weston_desktop_xdg_positioner_protocol_set_constraint_adjustment,
280 .set_offset = weston_desktop_xdg_positioner_protocol_set_offset,
281};
282
283static void
Quentin Glidicd51f8262017-04-13 20:25:27 +0200284weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface,
285 bool force);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200286
287static void
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200288weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel *toplevel)
289{
290 if (toplevel->added)
291 return;
292
293 weston_desktop_api_surface_added(toplevel->base.desktop,
294 toplevel->base.desktop_surface);
Quentin Glidicd51f8262017-04-13 20:25:27 +0200295 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, true);
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200296 toplevel->added = true;
297}
298
299static void
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200300weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client,
301 struct wl_resource *resource,
302 struct wl_resource *parent_resource)
303{
304 struct weston_desktop_surface *dsurface =
305 wl_resource_get_user_data(resource);
306 struct weston_desktop_xdg_toplevel *toplevel =
307 weston_desktop_surface_get_implementation_data(dsurface);
308 struct weston_desktop_surface *parent = NULL;
309
310 if (parent_resource != NULL)
311 parent = wl_resource_get_user_data(parent_resource);
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200312
313 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200314 weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent);
315}
316
317static void
318weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client,
319 struct wl_resource *resource,
320 const char *title)
321{
322 struct weston_desktop_surface *toplevel =
323 wl_resource_get_user_data(resource);
324
325 weston_desktop_surface_set_title(toplevel, title);
326}
327
328static void
329weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client,
330 struct wl_resource *resource,
331 const char *app_id)
332{
333 struct weston_desktop_surface *toplevel =
334 wl_resource_get_user_data(resource);
335
336 weston_desktop_surface_set_app_id(toplevel, app_id);
337}
338
339static void
340weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client,
341 struct wl_resource *resource,
342 struct wl_resource *seat_resource,
343 uint32_t serial,
344 int32_t x, int32_t y)
345{
346 struct weston_desktop_surface *dsurface =
347 wl_resource_get_user_data(resource);
348 struct weston_seat *seat =
349 wl_resource_get_user_data(seat_resource);
350 struct weston_desktop_xdg_toplevel *toplevel =
351 weston_desktop_surface_get_implementation_data(dsurface);
352
Quentin Glidic0abf8902016-09-11 11:34:47 +0200353 if (!toplevel->base.configured) {
354 wl_resource_post_error(toplevel->resource,
355 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
356 "Surface has not been configured yet");
357 return;
358 }
359
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200360 weston_desktop_api_show_window_menu(toplevel->base.desktop,
361 dsurface, seat, x, y);
362}
363
364static void
365weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client,
366 struct wl_resource *resource,
367 struct wl_resource *seat_resource,
368 uint32_t serial)
369{
370 struct weston_desktop_surface *dsurface =
371 wl_resource_get_user_data(resource);
372 struct weston_seat *seat =
373 wl_resource_get_user_data(seat_resource);
374 struct weston_desktop_xdg_toplevel *toplevel =
375 weston_desktop_surface_get_implementation_data(dsurface);
376
Quentin Glidic0abf8902016-09-11 11:34:47 +0200377 if (!toplevel->base.configured) {
378 wl_resource_post_error(toplevel->resource,
379 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
380 "Surface has not been configured yet");
381 return;
382 }
383
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200384 weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial);
385}
386
387static void
388weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client,
389 struct wl_resource *resource,
390 struct wl_resource *seat_resource,
391 uint32_t serial,
392 enum zxdg_toplevel_v6_resize_edge edges)
393{
394 struct weston_desktop_surface *dsurface =
395 wl_resource_get_user_data(resource);
396 struct weston_seat *seat =
397 wl_resource_get_user_data(seat_resource);
398 struct weston_desktop_xdg_toplevel *toplevel =
399 weston_desktop_surface_get_implementation_data(dsurface);
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200400 enum weston_desktop_surface_edge surf_edges =
401 (enum weston_desktop_surface_edge) edges;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200402
Quentin Glidic0abf8902016-09-11 11:34:47 +0200403 if (!toplevel->base.configured) {
404 wl_resource_post_error(toplevel->resource,
405 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
406 "Surface has not been configured yet");
407 return;
408 }
409
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200410 weston_desktop_api_resize(toplevel->base.desktop,
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200411 dsurface, seat, serial, surf_edges);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200412}
413
414static void
415weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel)
416{
Quentin Glidic218126d2017-07-11 13:31:36 +0200417 toplevel->next_state = toplevel->pending_state;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200418}
419
420static void
421weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client,
422 struct wl_resource *resource,
423 int32_t width, int32_t height)
424{
425 struct weston_desktop_surface *dsurface =
426 wl_resource_get_user_data(resource);
427 struct weston_desktop_xdg_toplevel *toplevel =
428 weston_desktop_surface_get_implementation_data(dsurface);
429
430 toplevel->next_min_size.width = width;
431 toplevel->next_min_size.height = height;
432}
433
434static void
435weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client,
436 struct wl_resource *resource,
437 int32_t width, int32_t height)
438{
439 struct weston_desktop_surface *dsurface =
440 wl_resource_get_user_data(resource);
441 struct weston_desktop_xdg_toplevel *toplevel =
442 weston_desktop_surface_get_implementation_data(dsurface);
443
444 toplevel->next_max_size.width = width;
445 toplevel->next_max_size.height = height;
446}
447
448static void
449weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client,
450 struct wl_resource *resource)
451{
452 struct weston_desktop_surface *dsurface =
453 wl_resource_get_user_data(resource);
454 struct weston_desktop_xdg_toplevel *toplevel =
455 weston_desktop_surface_get_implementation_data(dsurface);
456
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200457 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200458 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true);
459}
460
461static void
462weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client,
463 struct wl_resource *resource)
464{
465 struct weston_desktop_surface *dsurface =
466 wl_resource_get_user_data(resource);
467 struct weston_desktop_xdg_toplevel *toplevel =
468 weston_desktop_surface_get_implementation_data(dsurface);
469
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200470 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200471 weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false);
472}
473
474static void
475weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client,
476 struct wl_resource *resource,
477 struct wl_resource *output_resource)
478{
479 struct weston_desktop_surface *dsurface =
480 wl_resource_get_user_data(resource);
481 struct weston_desktop_xdg_toplevel *toplevel =
482 weston_desktop_surface_get_implementation_data(dsurface);
483 struct weston_output *output = NULL;
484
485 if (output_resource != NULL)
486 output = wl_resource_get_user_data(output_resource);
487
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200488 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200489 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
490 true, output);
491}
492
493static void
494weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client,
495 struct wl_resource *resource)
496{
497 struct weston_desktop_surface *dsurface =
498 wl_resource_get_user_data(resource);
499 struct weston_desktop_xdg_toplevel *toplevel =
500 weston_desktop_surface_get_implementation_data(dsurface);
501
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200502 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200503 weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
504 false, NULL);
505}
506
507static void
508weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client,
509 struct wl_resource *resource)
510{
511 struct weston_desktop_surface *dsurface =
512 wl_resource_get_user_data(resource);
513 struct weston_desktop_xdg_toplevel *toplevel =
514 weston_desktop_surface_get_implementation_data(dsurface);
515
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200516 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200517 weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface);
518}
519
520static void
521weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel)
522{
523 uint32_t *s;
524 struct wl_array states;
525
526 wl_array_init(&states);
Quentin Glidic218126d2017-07-11 13:31:36 +0200527 if (toplevel->pending_state.maximized) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200528 s = wl_array_add(&states, sizeof(uint32_t));
529 *s = ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED;
530 }
Quentin Glidic218126d2017-07-11 13:31:36 +0200531 if (toplevel->pending_state.fullscreen) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200532 s = wl_array_add(&states, sizeof(uint32_t));
533 *s = ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN;
534 }
Quentin Glidic218126d2017-07-11 13:31:36 +0200535 if (toplevel->pending_state.resizing) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200536 s = wl_array_add(&states, sizeof(uint32_t));
537 *s = ZXDG_TOPLEVEL_V6_STATE_RESIZING;
538 }
Quentin Glidic218126d2017-07-11 13:31:36 +0200539 if (toplevel->pending_state.activated) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200540 s = wl_array_add(&states, sizeof(uint32_t));
541 *s = ZXDG_TOPLEVEL_V6_STATE_ACTIVATED;
542 }
543
544 zxdg_toplevel_v6_send_configure(toplevel->resource,
Quentin Glidic218126d2017-07-11 13:31:36 +0200545 toplevel->pending_size.width,
546 toplevel->pending_size.height,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200547 &states);
548
549 wl_array_release(&states);
550};
551
552static void
553weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface,
554 void *user_data, bool maximized)
555{
556 struct weston_desktop_xdg_toplevel *toplevel = user_data;
557
Quentin Glidic218126d2017-07-11 13:31:36 +0200558 toplevel->pending_state.maximized = maximized;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200559 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200560}
561
562static void
563weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface,
564 void *user_data, bool fullscreen)
565{
566 struct weston_desktop_xdg_toplevel *toplevel = user_data;
567
Quentin Glidic218126d2017-07-11 13:31:36 +0200568 toplevel->pending_state.fullscreen = fullscreen;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200569 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200570}
571
572static void
573weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface,
574 void *user_data, bool resizing)
575{
576 struct weston_desktop_xdg_toplevel *toplevel = user_data;
577
Quentin Glidic218126d2017-07-11 13:31:36 +0200578 toplevel->pending_state.resizing = resizing;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200579 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200580}
581
582static void
583weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface,
584 void *user_data, bool activated)
585{
586 struct weston_desktop_xdg_toplevel *toplevel = user_data;
587
Quentin Glidic218126d2017-07-11 13:31:36 +0200588 toplevel->pending_state.activated = activated;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200589 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200590}
591
592static void
593weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface,
594 void *user_data,
595 int32_t width, int32_t height)
596{
597 struct weston_desktop_xdg_toplevel *toplevel = user_data;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200598
Quentin Glidic218126d2017-07-11 13:31:36 +0200599 toplevel->pending_size.width = width;
600 toplevel->pending_size.height = height;
Quentin Glidica56b0532016-09-13 10:05:58 +0200601
Quentin Glidicd51f8262017-04-13 20:25:27 +0200602 weston_desktop_xdg_surface_schedule_configure(&toplevel->base, false);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200603}
604
605static void
606weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel,
Quentin Glidiccba26e72016-08-15 12:20:22 +0200607 int32_t sx, int32_t sy)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200608{
609 struct weston_surface *wsurface =
610 weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200611
Quentin Glidiccba26e72016-08-15 12:20:22 +0200612 if (!wsurface->buffer_ref.buffer && !toplevel->added) {
Quentin Glidic3d7e6072016-09-11 11:29:23 +0200613 weston_desktop_xdg_toplevel_ensure_added(toplevel);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200614 return;
615 }
Quentin Glidiccba26e72016-08-15 12:20:22 +0200616 if (!wsurface->buffer_ref.buffer)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200617 return;
618
Quentin Glidicc84423b2017-03-10 11:50:41 +0100619 if ((toplevel->next_state.maximized || toplevel->next_state.fullscreen) &&
Quentin Glidic218126d2017-07-11 13:31:36 +0200620 (toplevel->pending_size.width != wsurface->width ||
621 toplevel->pending_size.height != wsurface->height)) {
Quentin Glidicc84423b2017-03-10 11:50:41 +0100622 struct weston_desktop_client *client =
623 weston_desktop_surface_get_client(toplevel->base.desktop_surface);
624 struct wl_resource *client_resource =
625 weston_desktop_client_get_resource(client);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200626
Quentin Glidicc84423b2017-03-10 11:50:41 +0100627 wl_resource_post_error(client_resource,
628 ZXDG_SHELL_V6_ERROR_INVALID_SURFACE_STATE,
629 "xdg_surface buffer does not match the configured state");
630 return;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200631 }
Quentin Glidicc84423b2017-03-10 11:50:41 +0100632
633 toplevel->state = toplevel->next_state;
634 toplevel->min_size = toplevel->next_min_size;
635 toplevel->max_size = toplevel->next_max_size;
636
637 weston_desktop_api_committed(toplevel->base.desktop,
638 toplevel->base.desktop_surface,
639 sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200640}
641
642static void
643weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel)
644{
645 zxdg_toplevel_v6_send_close(toplevel->resource);
646}
647
648static bool
649weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface,
650 void *user_data)
651{
652 struct weston_desktop_xdg_toplevel *toplevel = user_data;
653
654 return toplevel->state.maximized;
655}
656
657static bool
658weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface,
659 void *user_data)
660{
661 struct weston_desktop_xdg_toplevel *toplevel = user_data;
662
663 return toplevel->state.fullscreen;
664}
665
666static bool
667weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface,
668 void *user_data)
669{
670 struct weston_desktop_xdg_toplevel *toplevel = user_data;
671
672 return toplevel->state.resizing;
673}
674
675static bool
676weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface,
677 void *user_data)
678{
679 struct weston_desktop_xdg_toplevel *toplevel = user_data;
680
681 return toplevel->state.activated;
682}
683
684static void
685weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel)
686{
687 if (toplevel->added)
688 weston_desktop_api_surface_removed(toplevel->base.desktop,
689 toplevel->base.desktop_surface);
690}
691
692static void
693weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource)
694{
695 struct weston_desktop_surface *dsurface =
696 wl_resource_get_user_data(resource);
697
698 if (dsurface != NULL)
699 weston_desktop_surface_resource_destroy(resource);
700}
701
702static const struct zxdg_toplevel_v6_interface weston_desktop_xdg_toplevel_implementation = {
703 .destroy = weston_desktop_destroy_request,
704 .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent,
705 .set_title = weston_desktop_xdg_toplevel_protocol_set_title,
706 .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id,
707 .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu,
708 .move = weston_desktop_xdg_toplevel_protocol_move,
709 .resize = weston_desktop_xdg_toplevel_protocol_resize,
710 .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size,
711 .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size,
712 .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized,
713 .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized,
714 .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen,
715 .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen,
716 .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized,
717};
718
719static void
720weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client,
721 struct wl_resource *resource,
722 struct wl_resource *seat_resource,
723 uint32_t serial)
724{
725 struct weston_desktop_surface *dsurface =
726 wl_resource_get_user_data(resource);
727 struct weston_desktop_xdg_popup *popup =
728 weston_desktop_surface_get_implementation_data(dsurface);
729 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
730 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
731 struct weston_desktop_surface *topmost;
732 bool parent_is_toplevel =
733 popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
734
735 if (popup->committed) {
736 wl_resource_post_error(popup->resource,
737 ZXDG_POPUP_V6_ERROR_INVALID_GRAB,
738 "xdg_popup already is mapped");
739 return;
740 }
741
742 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
743 if ((topmost == NULL && !parent_is_toplevel) ||
744 (topmost != NULL && topmost != popup->parent->desktop_surface)) {
745 struct weston_desktop_client *client =
746 weston_desktop_surface_get_client(dsurface);
747 struct wl_resource *client_resource =
748 weston_desktop_client_get_resource(client);
749
750 wl_resource_post_error(client_resource,
751 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
752 "xdg_popup was not created on the topmost popup");
753 return;
754 }
755
756 popup->seat = seat;
757 weston_desktop_surface_popup_grab(popup->base.desktop_surface,
758 popup->seat, serial);
759}
760
761static void
762weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup)
763{
764 zxdg_popup_v6_send_configure(popup->resource,
765 popup->geometry.x,
766 popup->geometry.y,
767 popup->geometry.width,
768 popup->geometry.height);
769}
770
771static void
772weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
773 void *user_data);
774
775static void
776weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup)
777{
778 if (!popup->committed)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200779 weston_desktop_xdg_surface_schedule_configure(&popup->base, true);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200780 popup->committed = true;
781 weston_desktop_xdg_popup_update_position(popup->base.desktop_surface,
782 popup);
783}
784
785static void
786weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
787 void *user_data)
788{
789}
790
791static void
792weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup)
793{
794 zxdg_popup_v6_send_popup_done(popup->resource);
795}
796
797static void
798weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup)
799{
800 struct weston_desktop_surface *topmost;
801 struct weston_desktop_client *client =
802 weston_desktop_surface_get_client(popup->base.desktop_surface);
803
804 if (!weston_desktop_surface_get_grab(popup->base.desktop_surface))
805 return;
806
807 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
808 if (topmost != popup->base.desktop_surface) {
809 struct wl_resource *client_resource =
810 weston_desktop_client_get_resource(client);
811
812 wl_resource_post_error(client_resource,
813 ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP,
814 "xdg_popup was destroyed while it was not the topmost popup.");
815 }
816
817 weston_desktop_surface_popup_ungrab(popup->base.desktop_surface,
818 popup->seat);
819}
820
821static void
822weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource)
823{
824 struct weston_desktop_surface *dsurface =
825 wl_resource_get_user_data(resource);
826
827 if (dsurface != NULL)
828 weston_desktop_surface_resource_destroy(resource);
829}
830
831static const struct zxdg_popup_v6_interface weston_desktop_xdg_popup_implementation = {
832 .destroy = weston_desktop_destroy_request,
833 .grab = weston_desktop_xdg_popup_protocol_grab,
834};
835
836static void
837weston_desktop_xdg_surface_send_configure(void *user_data)
838{
839 struct weston_desktop_xdg_surface *surface = user_data;
840
841 surface->configure_idle = NULL;
842 surface->configure_serial =
843 wl_display_next_serial(weston_desktop_get_display(surface->desktop));
844
845 switch (surface->role) {
846 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
847 assert(0 && "not reached");
848 break;
849 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
850 weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface);
851 break;
852 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
853 weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface);
854 break;
855 }
856
857 zxdg_surface_v6_send_configure(surface->resource, surface->configure_serial);
858}
859
Quentin Glidicd51f8262017-04-13 20:25:27 +0200860static bool
861weston_desktop_xdg_toplevel_state_compare(struct weston_desktop_xdg_toplevel *toplevel)
862{
Quentin Glidic218126d2017-07-11 13:31:36 +0200863 if (toplevel->pending_state.activated != toplevel->state.activated)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200864 return false;
Quentin Glidic218126d2017-07-11 13:31:36 +0200865 if (toplevel->pending_state.fullscreen != toplevel->state.fullscreen)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200866 return false;
Quentin Glidic218126d2017-07-11 13:31:36 +0200867 if (toplevel->pending_state.maximized != toplevel->state.maximized)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200868 return false;
Quentin Glidic218126d2017-07-11 13:31:36 +0200869 if (toplevel->pending_state.resizing != toplevel->state.resizing)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200870 return false;
871
Quentin Glidic218126d2017-07-11 13:31:36 +0200872 if (toplevel->base.surface->width == toplevel->pending_size.width &&
873 toplevel->base.surface->height == toplevel->pending_size.height)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200874 return true;
875
Quentin Glidic218126d2017-07-11 13:31:36 +0200876 if (toplevel->pending_size.width == 0 &&
877 toplevel->pending_size.height == 0)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200878 return true;
879
880 return false;
881}
882
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200883static void
Quentin Glidicd51f8262017-04-13 20:25:27 +0200884weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface,
885 bool force)
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200886{
887 struct wl_display *display = weston_desktop_get_display(surface->desktop);
888 struct wl_event_loop *loop = wl_display_get_event_loop(display);
Quentin Glidic218126d2017-07-11 13:31:36 +0200889 bool pending_same = !force;
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200890
Quentin Glidicd51f8262017-04-13 20:25:27 +0200891 switch (surface->role) {
892 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
893 assert(0 && "not reached");
894 break;
895 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidic218126d2017-07-11 13:31:36 +0200896 pending_same = pending_same &&
Quentin Glidicd51f8262017-04-13 20:25:27 +0200897 weston_desktop_xdg_toplevel_state_compare((struct weston_desktop_xdg_toplevel *) surface);
898 break;
899 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
900 break;
901 }
902
903 if (surface->configure_idle != NULL) {
Quentin Glidic218126d2017-07-11 13:31:36 +0200904 if (!pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200905 return;
906
907 wl_event_source_remove(surface->configure_idle);
908 surface->configure_idle = NULL;
909 } else {
Quentin Glidic218126d2017-07-11 13:31:36 +0200910 if (pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200911 return;
912
913 surface->configure_idle =
914 wl_event_loop_add_idle(loop,
915 weston_desktop_xdg_surface_send_configure,
916 surface);
917 }
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200918}
919
920static void
921weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client,
922 struct wl_resource *resource,
923 uint32_t id)
924{
925 struct weston_desktop_surface *dsurface =
926 wl_resource_get_user_data(resource);
927 struct weston_surface *wsurface =
928 weston_desktop_surface_get_surface(dsurface);
929 struct weston_desktop_xdg_toplevel *toplevel =
930 weston_desktop_surface_get_implementation_data(dsurface);
931
932 if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role,
933 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
934 return;
935
936 toplevel->resource =
937 weston_desktop_surface_add_resource(toplevel->base.desktop_surface,
938 &zxdg_toplevel_v6_interface,
939 &weston_desktop_xdg_toplevel_implementation,
940 id, weston_desktop_xdg_toplevel_resource_destroy);
941 if (toplevel->resource == NULL)
942 return;
943
944 toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
945}
946
947static void
948weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client,
949 struct wl_resource *resource,
950 uint32_t id,
951 struct wl_resource *parent_resource,
952 struct wl_resource *positioner_resource)
953{
954 struct weston_desktop_surface *dsurface =
955 wl_resource_get_user_data(resource);
956 struct weston_surface *wsurface =
957 weston_desktop_surface_get_surface(dsurface);
958 struct weston_desktop_xdg_popup *popup =
959 weston_desktop_surface_get_implementation_data(dsurface);
960 struct weston_desktop_surface *parent_surface =
961 wl_resource_get_user_data(parent_resource);
962 struct weston_desktop_xdg_surface *parent =
963 weston_desktop_surface_get_implementation_data(parent_surface);
964 struct weston_desktop_xdg_positioner *positioner =
965 wl_resource_get_user_data(positioner_resource);
966
Sjoerd Simonsbe8a6d32016-09-23 09:31:23 +0200967 /* Checking whether the size and anchor rect both have a positive size
968 * is enough to verify both have been correctly set */
969 if (positioner->size.width == 0 || positioner->anchor_rect.width == 0) {
970 wl_resource_post_error(resource,
971 ZXDG_SHELL_V6_ERROR_INVALID_POSITIONER,
972 "positioner object is not complete");
973 return;
974 }
975
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +0200976 if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
977 resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0)
978 return;
979
980 popup->resource =
981 weston_desktop_surface_add_resource(popup->base.desktop_surface,
982 &zxdg_popup_v6_interface,
983 &weston_desktop_xdg_popup_implementation,
984 id, weston_desktop_xdg_popup_resource_destroy);
985 if (popup->resource == NULL)
986 return;
987
988 popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
989 popup->parent = parent;
990
991 popup->geometry =
992 weston_desktop_xdg_positioner_get_geometry(positioner,
993 dsurface,
994 parent_surface);
995
996 weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
997 parent_surface,
998 popup->geometry.x,
999 popup->geometry.y,
1000 true);
1001}
1002
1003static bool
1004weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
1005{
1006 struct weston_surface *wsurface =
1007 weston_desktop_surface_get_surface(surface->desktop_surface);
1008 const char *role;
1009
1010 role = weston_surface_get_role(wsurface);
1011 if (role != NULL &&
1012 (role == weston_desktop_xdg_toplevel_role ||
1013 role == weston_desktop_xdg_popup_role))
1014 return true;
1015
1016 wl_resource_post_error(surface->resource,
1017 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1018 "xdg_surface must have a role");
1019 return false;
1020}
1021
1022static void
1023weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
1024 struct wl_resource *resource,
1025 int32_t x, int32_t y,
1026 int32_t width, int32_t height)
1027{
1028 struct weston_desktop_surface *dsurface =
1029 wl_resource_get_user_data(resource);
1030 struct weston_desktop_xdg_surface *surface =
1031 weston_desktop_surface_get_implementation_data(dsurface);
1032
1033 if (!weston_desktop_xdg_surface_check_role(surface))
1034 return;
1035
1036 surface->has_next_geometry = true;
1037 surface->next_geometry.x = x;
1038 surface->next_geometry.y = y;
1039 surface->next_geometry.width = width;
1040 surface->next_geometry.height = height;
1041}
1042
1043static void
1044weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
1045 struct wl_resource *resource,
1046 uint32_t serial)
1047{
1048 struct weston_desktop_surface *dsurface =
1049 wl_resource_get_user_data(resource);
1050 struct weston_desktop_xdg_surface *surface =
1051 weston_desktop_surface_get_implementation_data(dsurface);
1052
1053 if (!weston_desktop_xdg_surface_check_role(surface))
1054 return;
1055
1056 if (surface->configure_serial != serial)
1057 return;
1058
1059 surface->configured = true;
1060
1061 switch (surface->role) {
1062 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1063 assert(0 && "not reached");
1064 break;
1065 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1066 weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface);
1067 break;
1068 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1069 break;
1070 }
1071}
1072
1073static void
1074weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
1075 uint32_t serial, void *user_data)
1076{
1077 struct weston_desktop_client *client =
1078 weston_desktop_surface_get_client(dsurface);
1079
1080 zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client),
1081 serial);
1082}
1083
1084static void
1085weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +02001086 void *user_data,
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001087 int32_t sx, int32_t sy)
1088{
1089 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidiccba26e72016-08-15 12:20:22 +02001090 struct weston_surface *wsurface =
1091 weston_desktop_surface_get_surface (dsurface);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001092
Quentin Glidiccba26e72016-08-15 12:20:22 +02001093 if (wsurface->buffer_ref.buffer && !surface->configured) {
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001094 wl_resource_post_error(surface->resource,
1095 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1096 "xdg_surface has never been configured");
1097 return;
1098 }
1099
1100 if (surface->has_next_geometry) {
1101 surface->has_next_geometry = false;
1102 weston_desktop_surface_set_geometry(surface->desktop_surface,
1103 surface->next_geometry);
1104 }
1105
1106 switch (surface->role) {
1107 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1108 wl_resource_post_error(surface->resource,
1109 ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED,
1110 "xdg_surface must have a role");
1111 break;
1112 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
Quentin Glidiccba26e72016-08-15 12:20:22 +02001113 weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
Quentin Glidic9c5dd7e2016-08-12 10:41:37 +02001114 break;
1115 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1116 weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1117 break;
1118 }
1119}
1120
1121static void
1122weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1123 void *user_data)
1124{
1125 struct weston_desktop_xdg_surface *surface = user_data;
1126
1127 switch (surface->role) {
1128 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1129 assert(0 && "not reached");
1130 break;
1131 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1132 weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1133 break;
1134 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1135 weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1136 break;
1137 }
1138}
1139
1140static void
1141weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1142 void *user_data)
1143{
1144 struct weston_desktop_xdg_surface *surface = user_data;
1145
1146 switch (surface->role) {
1147 case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1148 break;
1149 case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1150 weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1151 break;
1152 case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1153 weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1154 break;
1155 }
1156
1157 if (surface->configure_idle != NULL)
1158 wl_event_source_remove(surface->configure_idle);
1159
1160 free(surface);
1161}
1162
1163static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = {
1164 .destroy = weston_desktop_destroy_request,
1165 .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel,
1166 .get_popup = weston_desktop_xdg_surface_protocol_get_popup,
1167 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1168 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
1169};
1170
1171static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1172 /* These are used for toplevel only */
1173 .set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1174 .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1175 .set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1176 .set_activated = weston_desktop_xdg_toplevel_set_activated,
1177 .set_size = weston_desktop_xdg_toplevel_set_size,
1178
1179 .get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1180 .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1181 .get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1182 .get_activated = weston_desktop_xdg_toplevel_get_activated,
1183
1184 /* These are used for popup only */
1185 .update_position = weston_desktop_xdg_popup_update_position,
1186
1187 /* Common API */
1188 .committed = weston_desktop_xdg_surface_committed,
1189 .ping = weston_desktop_xdg_surface_ping,
1190 .close = weston_desktop_xdg_surface_close,
1191
1192 .destroy = weston_desktop_xdg_surface_destroy,
1193};
1194
1195static void
1196weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1197 struct wl_resource *resource,
1198 uint32_t id)
1199{
1200 struct weston_desktop_client *client =
1201 wl_resource_get_user_data(resource);
1202 struct weston_desktop_xdg_positioner *positioner;
1203
1204 positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1205 if (positioner == NULL) {
1206 wl_client_post_no_memory(wl_client);
1207 return;
1208 }
1209
1210 positioner->client = client;
1211 positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1212
1213 positioner->resource =
1214 wl_resource_create(wl_client,
1215 &zxdg_positioner_v6_interface,
1216 wl_resource_get_version(resource), id);
1217 if (positioner->resource == NULL) {
1218 wl_client_post_no_memory(wl_client);
1219 free(positioner);
1220 return;
1221 }
1222 wl_resource_set_implementation(positioner->resource,
1223 &weston_desktop_xdg_positioner_implementation,
1224 positioner, weston_desktop_xdg_positioner_destroy);
1225}
1226
1227static void
1228weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1229{
1230 struct weston_desktop_surface *dsurface =
1231 wl_resource_get_user_data(resource);
1232
1233 if (dsurface != NULL)
1234 weston_desktop_surface_resource_destroy(resource);
1235}
1236
1237static void
1238weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1239 struct wl_resource *resource,
1240 uint32_t id,
1241 struct wl_resource *surface_resource)
1242{
1243 struct weston_desktop_client *client =
1244 wl_resource_get_user_data(resource);
1245 struct weston_surface *wsurface =
1246 wl_resource_get_user_data(surface_resource);
1247 struct weston_desktop_xdg_surface *surface;
1248
1249 surface = zalloc(weston_desktop_surface_role_biggest_size);
1250 if (surface == NULL) {
1251 wl_client_post_no_memory(wl_client);
1252 return;
1253 }
1254
1255 surface->desktop = weston_desktop_client_get_desktop(client);
1256 surface->surface = wsurface;
1257
1258 surface->desktop_surface =
1259 weston_desktop_surface_create(surface->desktop, client,
1260 surface->surface,
1261 &weston_desktop_xdg_surface_internal_implementation,
1262 surface);
1263 if (surface->desktop_surface == NULL) {
1264 free(surface);
1265 return;
1266 }
1267
1268 surface->resource =
1269 weston_desktop_surface_add_resource(surface->desktop_surface,
1270 &zxdg_surface_v6_interface,
1271 &weston_desktop_xdg_surface_implementation,
1272 id, weston_desktop_xdg_surface_resource_destroy);
1273 if (surface->resource == NULL)
1274 return;
1275
1276 if (wsurface->buffer_ref.buffer != NULL) {
1277 wl_resource_post_error(surface->resource,
1278 ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER,
1279 "xdg_surface must not have a buffer at creation");
1280 return;
1281 }
1282}
1283
1284static void
1285weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1286 struct wl_resource *resource,
1287 uint32_t serial)
1288{
1289 struct weston_desktop_client *client =
1290 wl_resource_get_user_data(resource);
1291
1292 weston_desktop_client_pong(client, serial);
1293}
1294
1295static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = {
1296 .destroy = weston_desktop_destroy_request,
1297 .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1298 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1299 .pong = weston_desktop_xdg_shell_protocol_pong,
1300};
1301
1302static void
1303weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1304 uint32_t version, uint32_t id)
1305{
1306 struct weston_desktop *desktop = data;
1307
1308 weston_desktop_client_create(desktop, client, NULL,
1309 &zxdg_shell_v6_interface,
1310 &weston_desktop_xdg_shell_implementation,
1311 version, id);
1312}
1313
1314struct wl_global *
1315weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display)
1316{
1317 return wl_global_create(display, &zxdg_shell_v6_interface,
1318 WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1319 weston_desktop_xdg_shell_bind);
1320}