• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 <libweston/libweston.h>
35 #include <libweston/zalloc.h>
36 #include "xdg-shell-server-protocol.h"
37 
38 #include <libweston-desktop/libweston-desktop.h>
39 #include "internal.h"
40 
41 /************************************************************************************
42  * WARNING: This file implements the stable xdg shell protocol.
43  * Any changes to this file may also need to be added to the xdg-shell-v6.c file which
44  * implements the older unstable xdg shell v6 protocol.
45  ************************************************************************************/
46 
47 #define WD_XDG_SHELL_PROTOCOL_VERSION 1
48 
49 static const char *weston_desktop_xdg_toplevel_role = "xdg_toplevel";
50 static const char *weston_desktop_xdg_popup_role = "xdg_popup";
51 
52 struct weston_desktop_xdg_positioner {
53 	struct weston_desktop *desktop;
54 	struct weston_desktop_client *client;
55 	struct wl_resource *resource;
56 
57 	struct weston_size size;
58 	struct weston_geometry anchor_rect;
59 	enum xdg_positioner_anchor anchor;
60 	enum xdg_positioner_gravity gravity;
61 	enum xdg_positioner_constraint_adjustment constraint_adjustment;
62 	struct weston_position offset;
63 };
64 
65 enum weston_desktop_xdg_surface_role {
66 	WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE,
67 	WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL,
68 	WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP,
69 };
70 
71 struct weston_desktop_xdg_surface {
72 	struct wl_resource *resource;
73 	struct weston_desktop *desktop;
74 	struct weston_surface *surface;
75 	struct weston_desktop_surface *desktop_surface;
76 	bool configured;
77 	struct wl_event_source *configure_idle;
78 	struct wl_list configure_list; /* weston_desktop_xdg_surface_configure::link */
79 
80 	bool has_next_geometry;
81 	struct weston_geometry next_geometry;
82 
83 	enum weston_desktop_xdg_surface_role role;
84 };
85 
86 struct weston_desktop_xdg_surface_configure {
87 	struct wl_list link; /* weston_desktop_xdg_surface::configure_list */
88 	uint32_t serial;
89 };
90 
91 struct weston_desktop_xdg_toplevel_state {
92 	bool maximized;
93 	bool fullscreen;
94 	bool resizing;
95 	bool activated;
96 };
97 
98 struct weston_desktop_xdg_toplevel_configure {
99 	struct weston_desktop_xdg_surface_configure base;
100 	struct weston_desktop_xdg_toplevel_state state;
101 	struct weston_size size;
102 };
103 
104 struct weston_desktop_xdg_toplevel {
105 	struct weston_desktop_xdg_surface base;
106 
107 	struct wl_resource *resource;
108 	bool added;
109 	struct {
110 		struct weston_desktop_xdg_toplevel_state state;
111 		struct weston_size size;
112 	} pending;
113 	struct {
114 		struct weston_desktop_xdg_toplevel_state state;
115 		struct weston_size size;
116 		struct weston_size min_size, max_size;
117 	} next;
118 	struct {
119 		struct weston_desktop_xdg_toplevel_state state;
120 		struct weston_size min_size, max_size;
121 	} current;
122 };
123 
124 struct weston_desktop_xdg_popup {
125 	struct weston_desktop_xdg_surface base;
126 
127 	struct wl_resource *resource;
128 	bool committed;
129 	struct weston_desktop_xdg_surface *parent;
130 	struct weston_desktop_seat *seat;
131 	struct weston_geometry geometry;
132 };
133 
134 #define weston_desktop_surface_role_biggest_size (sizeof(struct weston_desktop_xdg_toplevel))
135 #define weston_desktop_surface_configure_biggest_size (sizeof(struct weston_desktop_xdg_toplevel))
136 
137 
138 static struct weston_geometry
weston_desktop_xdg_positioner_get_geometry(struct weston_desktop_xdg_positioner * positioner,struct weston_desktop_surface * dsurface,struct weston_desktop_surface * parent)139 weston_desktop_xdg_positioner_get_geometry(struct weston_desktop_xdg_positioner *positioner,
140 					   struct weston_desktop_surface *dsurface,
141 					   struct weston_desktop_surface *parent)
142 {
143 	struct weston_geometry geometry = {
144 		.x = positioner->offset.x,
145 		.y = positioner->offset.y,
146 		.width = positioner->size.width,
147 		.height = positioner->size.height,
148 	};
149 
150 	switch (positioner->anchor) {
151 	case XDG_POSITIONER_ANCHOR_TOP:
152 	case XDG_POSITIONER_ANCHOR_TOP_LEFT:
153 	case XDG_POSITIONER_ANCHOR_TOP_RIGHT:
154 		geometry.y += positioner->anchor_rect.y;
155 		break;
156 	case XDG_POSITIONER_ANCHOR_BOTTOM:
157 	case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT:
158 	case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT:
159 		geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height;
160 		break;
161 	default:
162 		geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height / 2;
163 	}
164 
165 	switch (positioner->anchor) {
166 	case XDG_POSITIONER_ANCHOR_LEFT:
167 	case XDG_POSITIONER_ANCHOR_TOP_LEFT:
168 	case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT:
169 		geometry.x += positioner->anchor_rect.x;
170 		break;
171 	case XDG_POSITIONER_ANCHOR_RIGHT:
172 	case XDG_POSITIONER_ANCHOR_TOP_RIGHT:
173 	case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT:
174 		geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width;
175 		break;
176 	default:
177 		geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width / 2;
178 	}
179 
180 	switch (positioner->gravity) {
181 	case XDG_POSITIONER_GRAVITY_TOP:
182 	case XDG_POSITIONER_GRAVITY_TOP_LEFT:
183 	case XDG_POSITIONER_GRAVITY_TOP_RIGHT:
184 		geometry.y -= geometry.height;
185 		break;
186 	case XDG_POSITIONER_GRAVITY_BOTTOM:
187 	case XDG_POSITIONER_GRAVITY_BOTTOM_LEFT:
188 	case XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT:
189 		geometry.y = geometry.y;
190 		break;
191 	default:
192 		geometry.y -= geometry.height / 2;
193 	}
194 
195 	switch (positioner->gravity) {
196 	case XDG_POSITIONER_GRAVITY_LEFT:
197 	case XDG_POSITIONER_GRAVITY_TOP_LEFT:
198 	case XDG_POSITIONER_GRAVITY_BOTTOM_LEFT:
199 		geometry.x -= geometry.width;
200 		break;
201 	case XDG_POSITIONER_GRAVITY_RIGHT:
202 	case XDG_POSITIONER_GRAVITY_TOP_RIGHT:
203 	case XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT:
204 		geometry.x = geometry.x;
205 		break;
206 	default:
207 		geometry.x -= geometry.width / 2;
208 	}
209 
210 	if (positioner->constraint_adjustment == XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_NONE)
211 		return geometry;
212 
213 	/* TODO: add compositor policy configuration and the code here */
214 
215 	return geometry;
216 }
217 
218 static void
weston_desktop_xdg_positioner_protocol_set_size(struct wl_client * wl_client,struct wl_resource * resource,int32_t width,int32_t height)219 weston_desktop_xdg_positioner_protocol_set_size(struct wl_client *wl_client,
220 						struct wl_resource *resource,
221 						int32_t width, int32_t height)
222 {
223 	struct weston_desktop_xdg_positioner *positioner =
224 		wl_resource_get_user_data(resource);
225 
226 	if (width < 1 || height < 1) {
227 		wl_resource_post_error(resource,
228 				       XDG_POSITIONER_ERROR_INVALID_INPUT,
229 				       "width and height must be positives and non-zero");
230 		return;
231 	}
232 
233 	positioner->size.width = width;
234 	positioner->size.height = height;
235 }
236 
237 static void
weston_desktop_xdg_positioner_protocol_set_anchor_rect(struct wl_client * wl_client,struct wl_resource * resource,int32_t x,int32_t y,int32_t width,int32_t height)238 weston_desktop_xdg_positioner_protocol_set_anchor_rect(struct wl_client *wl_client,
239 						       struct wl_resource *resource,
240 						       int32_t x, int32_t y,
241 						       int32_t width, int32_t height)
242 {
243 	struct weston_desktop_xdg_positioner *positioner =
244 		wl_resource_get_user_data(resource);
245 
246 	if (width < 0 || height < 0) {
247 		wl_resource_post_error(resource,
248 				       XDG_POSITIONER_ERROR_INVALID_INPUT,
249 				       "width and height must be non-negative");
250 		return;
251 	}
252 
253 	positioner->anchor_rect.x = x;
254 	positioner->anchor_rect.y = y;
255 	positioner->anchor_rect.width = width;
256 	positioner->anchor_rect.height = height;
257 }
258 
259 static void
weston_desktop_xdg_positioner_protocol_set_anchor(struct wl_client * wl_client,struct wl_resource * resource,enum xdg_positioner_anchor anchor)260 weston_desktop_xdg_positioner_protocol_set_anchor(struct wl_client *wl_client,
261 						  struct wl_resource *resource,
262 						  enum xdg_positioner_anchor anchor)
263 {
264 	struct weston_desktop_xdg_positioner *positioner =
265 		wl_resource_get_user_data(resource);
266 
267 	positioner->anchor = anchor;
268 }
269 
270 static void
weston_desktop_xdg_positioner_protocol_set_gravity(struct wl_client * wl_client,struct wl_resource * resource,enum xdg_positioner_gravity gravity)271 weston_desktop_xdg_positioner_protocol_set_gravity(struct wl_client *wl_client,
272 						   struct wl_resource *resource,
273 						   enum xdg_positioner_gravity gravity)
274 {
275 	struct weston_desktop_xdg_positioner *positioner =
276 		wl_resource_get_user_data(resource);
277 
278 	positioner->gravity = gravity;
279 }
280 
281 static void
weston_desktop_xdg_positioner_protocol_set_constraint_adjustment(struct wl_client * wl_client,struct wl_resource * resource,enum xdg_positioner_constraint_adjustment constraint_adjustment)282 weston_desktop_xdg_positioner_protocol_set_constraint_adjustment(struct wl_client *wl_client,
283 								 struct wl_resource *resource,
284 								 enum xdg_positioner_constraint_adjustment constraint_adjustment)
285 {
286 	struct weston_desktop_xdg_positioner *positioner =
287 		wl_resource_get_user_data(resource);
288 
289 	positioner->constraint_adjustment = constraint_adjustment;
290 }
291 
292 static void
weston_desktop_xdg_positioner_protocol_set_offset(struct wl_client * wl_client,struct wl_resource * resource,int32_t x,int32_t y)293 weston_desktop_xdg_positioner_protocol_set_offset(struct wl_client *wl_client,
294 						  struct wl_resource *resource,
295 						  int32_t x, int32_t y)
296 {
297 	struct weston_desktop_xdg_positioner *positioner =
298 		wl_resource_get_user_data(resource);
299 
300 	positioner->offset.x = x;
301 	positioner->offset.y = y;
302 }
303 
304 static void
weston_desktop_xdg_positioner_destroy(struct wl_resource * resource)305 weston_desktop_xdg_positioner_destroy(struct wl_resource *resource)
306 {
307 	struct weston_desktop_xdg_positioner *positioner =
308 		wl_resource_get_user_data(resource);
309 
310 	free(positioner);
311 }
312 
313 static const struct xdg_positioner_interface weston_desktop_xdg_positioner_implementation = {
314 	.destroy                   = weston_desktop_destroy_request,
315 	.set_size                  = weston_desktop_xdg_positioner_protocol_set_size,
316 	.set_anchor_rect           = weston_desktop_xdg_positioner_protocol_set_anchor_rect,
317 	.set_anchor                = weston_desktop_xdg_positioner_protocol_set_anchor,
318 	.set_gravity               = weston_desktop_xdg_positioner_protocol_set_gravity,
319 	.set_constraint_adjustment = weston_desktop_xdg_positioner_protocol_set_constraint_adjustment,
320 	.set_offset                = weston_desktop_xdg_positioner_protocol_set_offset,
321 };
322 
323 static void
324 weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface);
325 
326 static void
weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel * toplevel)327 weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel *toplevel)
328 {
329 	if (toplevel->added)
330 		return;
331 
332 	weston_desktop_api_surface_added(toplevel->base.desktop,
333 					 toplevel->base.desktop_surface);
334 	weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
335 	toplevel->added = true;
336 }
337 
338 static void
weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * parent_resource)339 weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client,
340 						struct wl_resource *resource,
341 						struct wl_resource *parent_resource)
342 {
343 	struct weston_desktop_surface *dsurface =
344 		wl_resource_get_user_data(resource);
345 	struct weston_desktop_xdg_toplevel *toplevel =
346 		weston_desktop_surface_get_implementation_data(dsurface);
347 	struct weston_desktop_surface *parent = NULL;
348 
349 	if (parent_resource != NULL)
350 		parent = wl_resource_get_user_data(parent_resource);
351 
352 	weston_desktop_xdg_toplevel_ensure_added(toplevel);
353 	weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent);
354 }
355 
356 static void
weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client * wl_client,struct wl_resource * resource,const char * title)357 weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client,
358 					       struct wl_resource *resource,
359 					       const char *title)
360 {
361 	struct weston_desktop_surface *toplevel =
362 		wl_resource_get_user_data(resource);
363 
364 	weston_desktop_surface_set_title(toplevel, title);
365 }
366 
367 static void
weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client * wl_client,struct wl_resource * resource,const char * app_id)368 weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client,
369 						struct wl_resource *resource,
370 						const char *app_id)
371 {
372 	struct weston_desktop_surface *toplevel =
373 		wl_resource_get_user_data(resource);
374 
375 	weston_desktop_surface_set_app_id(toplevel, app_id);
376 }
377 
378 static void
weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial,int32_t x,int32_t y)379 weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client,
380 						      struct wl_resource *resource,
381 						      struct wl_resource *seat_resource,
382 						      uint32_t serial,
383 						      int32_t x, int32_t y)
384 {
385 	struct weston_desktop_surface *dsurface =
386 		wl_resource_get_user_data(resource);
387 	struct weston_seat *seat =
388 		wl_resource_get_user_data(seat_resource);
389 	struct weston_desktop_xdg_toplevel *toplevel =
390 		weston_desktop_surface_get_implementation_data(dsurface);
391 
392 	if (!toplevel->base.configured) {
393 		wl_resource_post_error(toplevel->resource,
394 				       XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
395 				       "Surface has not been configured yet");
396 		return;
397 	}
398 
399 	if (seat == NULL)
400 		return;
401 
402 	weston_desktop_api_show_window_menu(toplevel->base.desktop,
403 					    dsurface, seat, x, y);
404 }
405 
406 static void
weston_desktop_xdg_toplevel_protocol_move(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial)407 weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client,
408 					  struct wl_resource *resource,
409 					  struct wl_resource *seat_resource,
410 					  uint32_t serial)
411 {
412 	struct weston_desktop_surface *dsurface =
413 		wl_resource_get_user_data(resource);
414 	struct weston_seat *seat =
415 		wl_resource_get_user_data(seat_resource);
416 	struct weston_desktop_xdg_toplevel *toplevel =
417 		weston_desktop_surface_get_implementation_data(dsurface);
418 
419 	if (!toplevel->base.configured) {
420 		wl_resource_post_error(toplevel->resource,
421 				       XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
422 				       "Surface has not been configured yet");
423 		return;
424 	}
425 
426 	if (seat == NULL)
427 		return;
428 
429 	weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial);
430 }
431 
432 static void
weston_desktop_xdg_toplevel_protocol_resize(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial,enum xdg_toplevel_resize_edge edges)433 weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client,
434 					    struct wl_resource *resource,
435 					    struct wl_resource *seat_resource,
436 					    uint32_t serial,
437 					    enum xdg_toplevel_resize_edge edges)
438 {
439 	struct weston_desktop_surface *dsurface =
440 		wl_resource_get_user_data(resource);
441 	struct weston_seat *seat =
442 		wl_resource_get_user_data(seat_resource);
443 	struct weston_desktop_xdg_toplevel *toplevel =
444 		weston_desktop_surface_get_implementation_data(dsurface);
445 	enum weston_desktop_surface_edge surf_edges =
446 		(enum weston_desktop_surface_edge) edges;
447 
448 	if (!toplevel->base.configured) {
449 		wl_resource_post_error(toplevel->resource,
450 				       XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
451 				       "Surface has not been configured yet");
452 		return;
453 	}
454 
455 	if (seat == NULL)
456 		return;
457 
458 	weston_desktop_api_resize(toplevel->base.desktop,
459 				  dsurface, seat, serial, surf_edges);
460 }
461 
462 static void
weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel * toplevel,struct weston_desktop_xdg_toplevel_configure * configure)463 weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel,
464 					  struct weston_desktop_xdg_toplevel_configure *configure)
465 {
466 	toplevel->next.state = configure->state;
467 	toplevel->next.size = configure->size;
468 }
469 
470 static void
weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client * wl_client,struct wl_resource * resource,int32_t width,int32_t height)471 weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client,
472 						  struct wl_resource *resource,
473 						  int32_t width, int32_t height)
474 {
475 	struct weston_desktop_surface *dsurface =
476 		wl_resource_get_user_data(resource);
477 	struct weston_desktop_xdg_toplevel *toplevel =
478 		weston_desktop_surface_get_implementation_data(dsurface);
479 
480 	toplevel->next.min_size.width = width;
481 	toplevel->next.min_size.height = height;
482 }
483 
484 static void
weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client * wl_client,struct wl_resource * resource,int32_t width,int32_t height)485 weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client,
486 						  struct wl_resource *resource,
487 						  int32_t width, int32_t height)
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 
494 	toplevel->next.max_size.width = width;
495 	toplevel->next.max_size.height = height;
496 }
497 
498 static void
weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client * wl_client,struct wl_resource * resource)499 weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client,
500 						   struct wl_resource *resource)
501 {
502 	struct weston_desktop_surface *dsurface =
503 		wl_resource_get_user_data(resource);
504 	struct weston_desktop_xdg_toplevel *toplevel =
505 		weston_desktop_surface_get_implementation_data(dsurface);
506 
507 	weston_desktop_xdg_toplevel_ensure_added(toplevel);
508 	weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true);
509 }
510 
511 static void
weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client * wl_client,struct wl_resource * resource)512 weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client,
513 						     struct wl_resource *resource)
514 {
515 	struct weston_desktop_surface *dsurface =
516 		wl_resource_get_user_data(resource);
517 	struct weston_desktop_xdg_toplevel *toplevel =
518 		weston_desktop_surface_get_implementation_data(dsurface);
519 
520 	weston_desktop_xdg_toplevel_ensure_added(toplevel);
521 	weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false);
522 }
523 
524 static void
weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * output_resource)525 weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client,
526 						    struct wl_resource *resource,
527 						    struct wl_resource *output_resource)
528 {
529 	struct weston_desktop_surface *dsurface =
530 		wl_resource_get_user_data(resource);
531 	struct weston_desktop_xdg_toplevel *toplevel =
532 		weston_desktop_surface_get_implementation_data(dsurface);
533 	struct weston_output *output = NULL;
534 
535 	if (output_resource != NULL)
536 		output = weston_head_from_resource(output_resource)->output;
537 
538 	weston_desktop_xdg_toplevel_ensure_added(toplevel);
539 	weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
540 						true, output);
541 }
542 
543 static void
weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client * wl_client,struct wl_resource * resource)544 weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client,
545 						      struct wl_resource *resource)
546 {
547 	struct weston_desktop_surface *dsurface =
548 		wl_resource_get_user_data(resource);
549 	struct weston_desktop_xdg_toplevel *toplevel =
550 		weston_desktop_surface_get_implementation_data(dsurface);
551 
552 	weston_desktop_xdg_toplevel_ensure_added(toplevel);
553 	weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface,
554 						false, NULL);
555 }
556 
557 static void
weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client * wl_client,struct wl_resource * resource)558 weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client,
559 						   struct wl_resource *resource)
560 {
561 	struct weston_desktop_surface *dsurface =
562 		wl_resource_get_user_data(resource);
563 	struct weston_desktop_xdg_toplevel *toplevel =
564 		weston_desktop_surface_get_implementation_data(dsurface);
565 
566 	weston_desktop_xdg_toplevel_ensure_added(toplevel);
567 	weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface);
568 }
569 
570 static void
weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel * toplevel,struct weston_desktop_xdg_toplevel_configure * configure)571 weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel,
572 					   struct weston_desktop_xdg_toplevel_configure *configure)
573 {
574 	uint32_t *s;
575 	struct wl_array states;
576 
577 	configure->state = toplevel->pending.state;
578 	configure->size = toplevel->pending.size;
579 
580 	wl_array_init(&states);
581 	if (toplevel->pending.state.maximized) {
582 		s = wl_array_add(&states, sizeof(uint32_t));
583 		*s = XDG_TOPLEVEL_STATE_MAXIMIZED;
584 	}
585 	if (toplevel->pending.state.fullscreen) {
586 		s = wl_array_add(&states, sizeof(uint32_t));
587 		*s = XDG_TOPLEVEL_STATE_FULLSCREEN;
588 	}
589 	if (toplevel->pending.state.resizing) {
590 		s = wl_array_add(&states, sizeof(uint32_t));
591 		*s = XDG_TOPLEVEL_STATE_RESIZING;
592 	}
593 	if (toplevel->pending.state.activated) {
594 		s = wl_array_add(&states, sizeof(uint32_t));
595 		*s = XDG_TOPLEVEL_STATE_ACTIVATED;
596 	}
597 
598 	xdg_toplevel_send_configure(toplevel->resource,
599 				    toplevel->pending.size.width,
600 				    toplevel->pending.size.height,
601 				    &states);
602 
603 	wl_array_release(&states);
604 };
605 
606 static void
weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface * dsurface,void * user_data,bool maximized)607 weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface,
608 					  void *user_data, bool maximized)
609 {
610 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
611 
612 	toplevel->pending.state.maximized = maximized;
613 	weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
614 }
615 
616 static void
weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface * dsurface,void * user_data,bool fullscreen)617 weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface,
618 					   void *user_data, bool fullscreen)
619 {
620 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
621 
622 	toplevel->pending.state.fullscreen = fullscreen;
623 	weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
624 }
625 
626 static void
weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface * dsurface,void * user_data,bool resizing)627 weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface,
628 					 void *user_data, bool resizing)
629 {
630 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
631 
632 	toplevel->pending.state.resizing = resizing;
633 	weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
634 }
635 
636 static void
weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface * dsurface,void * user_data,bool activated)637 weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface,
638 					  void *user_data, bool activated)
639 {
640 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
641 
642 	toplevel->pending.state.activated = activated;
643 	weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
644 }
645 
646 static void
weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface * dsurface,void * user_data,int32_t width,int32_t height)647 weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface,
648 				     void *user_data,
649 				     int32_t width, int32_t height)
650 {
651 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
652 
653 	toplevel->pending.size.width = width;
654 	toplevel->pending.size.height = height;
655 
656 	weston_desktop_xdg_surface_schedule_configure(&toplevel->base);
657 }
658 
659 static void
weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel * toplevel,int32_t sx,int32_t sy)660 weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel,
661 				      int32_t sx, int32_t sy)
662 {
663 	struct weston_surface *wsurface =
664 		weston_desktop_surface_get_surface(toplevel->base.desktop_surface);
665 
666 	if (!wsurface->buffer_ref.buffer && !toplevel->added) {
667 		weston_desktop_xdg_toplevel_ensure_added(toplevel);
668 		return;
669 	}
670 	if (!wsurface->buffer_ref.buffer)
671 		return;
672 
673 	struct weston_geometry geometry =
674 		weston_desktop_surface_get_geometry(toplevel->base.desktop_surface);
675 
676 	if (toplevel->next.state.maximized &&
677 	    (toplevel->next.size.width != geometry.width ||
678 	     toplevel->next.size.height != geometry.height)) {
679 		struct weston_desktop_client *client =
680 			weston_desktop_surface_get_client(toplevel->base.desktop_surface);
681 		struct wl_resource *client_resource =
682 			weston_desktop_client_get_resource(client);
683 
684 		wl_resource_post_error(client_resource,
685 				       XDG_WM_BASE_ERROR_INVALID_SURFACE_STATE,
686 				       "xdg_surface buffer (%" PRIi32 " x %" PRIi32 ") "
687 				       "does not match the configured maximized state (%" PRIi32 " x %" PRIi32 ")",
688 				       geometry.width, geometry.height,
689 				       toplevel->next.size.width,
690 				       toplevel->next.size.height);
691 		return;
692 	}
693 
694 	if (toplevel->next.state.fullscreen &&
695 	    (toplevel->next.size.width < geometry.width ||
696 	     toplevel->next.size.height < geometry.height)) {
697 		struct weston_desktop_client *client =
698 			weston_desktop_surface_get_client(toplevel->base.desktop_surface);
699 		struct wl_resource *client_resource =
700 			weston_desktop_client_get_resource(client);
701 
702 		wl_resource_post_error(client_resource,
703 				       XDG_WM_BASE_ERROR_INVALID_SURFACE_STATE,
704 				       "xdg_surface buffer (%" PRIi32 " x %" PRIi32 ") "
705 				       "is larger than the configured fullscreen state (%" PRIi32 " x %" PRIi32 ")",
706 				       geometry.width, geometry.height,
707 				       toplevel->next.size.width,
708 				       toplevel->next.size.height);
709 		return;
710 	}
711 
712 	toplevel->current.state = toplevel->next.state;
713 	toplevel->current.min_size = toplevel->next.min_size;
714 	toplevel->current.max_size = toplevel->next.max_size;
715 
716 	weston_desktop_api_committed(toplevel->base.desktop,
717 				     toplevel->base.desktop_surface,
718 				     sx, sy);
719 }
720 
721 static void
weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel * toplevel)722 weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel)
723 {
724 	xdg_toplevel_send_close(toplevel->resource);
725 }
726 
727 static bool
weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface * dsurface,void * user_data)728 weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface,
729 					  void *user_data)
730 {
731 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
732 
733 	return toplevel->current.state.maximized;
734 }
735 
736 static bool
weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface * dsurface,void * user_data)737 weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface,
738 					   void *user_data)
739 {
740 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
741 
742 	return toplevel->current.state.fullscreen;
743 }
744 
745 static bool
weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface * dsurface,void * user_data)746 weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface,
747 					 void *user_data)
748 {
749 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
750 
751 	return toplevel->current.state.resizing;
752 }
753 
754 static bool
weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface * dsurface,void * user_data)755 weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface,
756 					  void *user_data)
757 {
758 	struct weston_desktop_xdg_toplevel *toplevel = user_data;
759 
760 	return toplevel->current.state.activated;
761 }
762 
763 static void
weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel * toplevel)764 weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel)
765 {
766 	if (toplevel->added)
767 		weston_desktop_api_surface_removed(toplevel->base.desktop,
768 						   toplevel->base.desktop_surface);
769 }
770 
771 static void
weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource * resource)772 weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource)
773 {
774 	struct weston_desktop_surface *dsurface =
775 		wl_resource_get_user_data(resource);
776 
777 	if (dsurface != NULL)
778 		weston_desktop_surface_resource_destroy(resource);
779 }
780 
781 static const struct xdg_toplevel_interface weston_desktop_xdg_toplevel_implementation = {
782 	.destroy             = weston_desktop_destroy_request,
783 	.set_parent          = weston_desktop_xdg_toplevel_protocol_set_parent,
784 	.set_title           = weston_desktop_xdg_toplevel_protocol_set_title,
785 	.set_app_id          = weston_desktop_xdg_toplevel_protocol_set_app_id,
786 	.show_window_menu    = weston_desktop_xdg_toplevel_protocol_show_window_menu,
787 	.move                = weston_desktop_xdg_toplevel_protocol_move,
788 	.resize              = weston_desktop_xdg_toplevel_protocol_resize,
789 	.set_min_size        = weston_desktop_xdg_toplevel_protocol_set_min_size,
790 	.set_max_size        = weston_desktop_xdg_toplevel_protocol_set_max_size,
791 	.set_maximized       = weston_desktop_xdg_toplevel_protocol_set_maximized,
792 	.unset_maximized     = weston_desktop_xdg_toplevel_protocol_unset_maximized,
793 	.set_fullscreen      = weston_desktop_xdg_toplevel_protocol_set_fullscreen,
794 	.unset_fullscreen    = weston_desktop_xdg_toplevel_protocol_unset_fullscreen,
795 	.set_minimized       = weston_desktop_xdg_toplevel_protocol_set_minimized,
796 };
797 
798 static void
weston_desktop_xdg_popup_protocol_grab(struct wl_client * wl_client,struct wl_resource * resource,struct wl_resource * seat_resource,uint32_t serial)799 weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client,
800 				       struct wl_resource *resource,
801 				       struct wl_resource *seat_resource,
802 				       uint32_t serial)
803 {
804 	struct weston_desktop_surface *dsurface =
805 		wl_resource_get_user_data(resource);
806 	struct weston_desktop_xdg_popup *popup =
807 		weston_desktop_surface_get_implementation_data(dsurface);
808 	struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
809 	struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
810 	struct weston_desktop_surface *topmost;
811 	bool parent_is_toplevel =
812 		popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
813 
814 	/* Check that if we have a valid wseat we also got a valid desktop seat */
815 	if (wseat != NULL && seat == NULL) {
816 		wl_client_post_no_memory(wl_client);
817 		return;
818 	}
819 
820 	if (popup->committed) {
821 		wl_resource_post_error(popup->resource,
822 				       XDG_POPUP_ERROR_INVALID_GRAB,
823 				       "xdg_popup already is mapped");
824 		return;
825 	}
826 
827 	/* If seat is NULL then get_topmost_surface will return NULL. In
828 	 * combination with setting parent_is_toplevel to TRUE here we will
829 	 * avoid posting an error, and we will instead gracefully fail the
830 	 * grab and dismiss the surface.
831 	 * FIXME: this is a hack because currently we cannot check the topmost
832 	 * parent with a destroyed weston_seat */
833 	if (seat == NULL)
834 		parent_is_toplevel = true;
835 
836 	topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
837 	if ((topmost == NULL && !parent_is_toplevel) ||
838 	    (topmost != NULL && topmost != popup->parent->desktop_surface)) {
839 		struct weston_desktop_client *client =
840 			weston_desktop_surface_get_client(dsurface);
841 		struct wl_resource *client_resource =
842 			weston_desktop_client_get_resource(client);
843 
844 		wl_resource_post_error(client_resource,
845 				       XDG_WM_BASE_ERROR_NOT_THE_TOPMOST_POPUP,
846 				       "xdg_popup was not created on the topmost popup");
847 		return;
848 	}
849 
850 	popup->seat = seat;
851 	weston_desktop_surface_popup_grab(popup->base.desktop_surface,
852 					  popup->seat, serial);
853 }
854 
855 static void
weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup * popup)856 weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup)
857 {
858 	xdg_popup_send_configure(popup->resource,
859 				 popup->geometry.x,
860 				 popup->geometry.y,
861 				 popup->geometry.width,
862 				 popup->geometry.height);
863 }
864 
865 static void
866 weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
867 					 void *user_data);
868 
869 static void
weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup * popup)870 weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup)
871 {
872 	struct weston_surface *wsurface =
873 		weston_desktop_surface_get_surface (popup->base.desktop_surface);
874 	struct weston_view *view;
875 
876 	wl_list_for_each(view, &wsurface->views, surface_link)
877 		weston_view_update_transform(view);
878 
879 	if (!popup->committed)
880 		weston_desktop_xdg_surface_schedule_configure(&popup->base);
881 	popup->committed = true;
882 	weston_desktop_xdg_popup_update_position(popup->base.desktop_surface,
883 						 popup);
884 }
885 
886 static void
weston_desktop_xdg_popup_update_position(struct weston_desktop_surface * dsurface,void * user_data)887 weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface,
888 					 void *user_data)
889 {
890 }
891 
892 static void
weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup * popup)893 weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup)
894 {
895 	xdg_popup_send_popup_done(popup->resource);
896 }
897 
898 static void
weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup * popup)899 weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup)
900 {
901 	struct weston_desktop_surface *topmost;
902 	struct weston_desktop_client *client =
903 		weston_desktop_surface_get_client(popup->base.desktop_surface);
904 
905 	if (!weston_desktop_surface_get_grab(popup->base.desktop_surface))
906 		return;
907 
908 	topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
909 	if (topmost != popup->base.desktop_surface) {
910 		struct wl_resource *client_resource =
911 			weston_desktop_client_get_resource(client);
912 
913 		wl_resource_post_error(client_resource,
914 				       XDG_WM_BASE_ERROR_NOT_THE_TOPMOST_POPUP,
915 				       "xdg_popup was destroyed while it was not the topmost popup.");
916 	}
917 
918 	weston_desktop_surface_popup_ungrab(popup->base.desktop_surface,
919 					    popup->seat);
920 }
921 
922 static void
weston_desktop_xdg_popup_resource_destroy(struct wl_resource * resource)923 weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource)
924 {
925 	struct weston_desktop_surface *dsurface =
926 		wl_resource_get_user_data(resource);
927 
928 	if (dsurface != NULL)
929 		weston_desktop_surface_resource_destroy(resource);
930 }
931 
932 static const struct xdg_popup_interface weston_desktop_xdg_popup_implementation = {
933 	.destroy             = weston_desktop_destroy_request,
934 	.grab                = weston_desktop_xdg_popup_protocol_grab,
935 };
936 
937 static void
weston_desktop_xdg_surface_send_configure(void * user_data)938 weston_desktop_xdg_surface_send_configure(void *user_data)
939 {
940 	struct weston_desktop_xdg_surface *surface = user_data;
941 	struct weston_desktop_xdg_surface_configure *configure;
942 
943 	surface->configure_idle = NULL;
944 
945 	configure = zalloc(weston_desktop_surface_configure_biggest_size);
946 	if (configure == NULL) {
947 		struct weston_desktop_client *client =
948 			weston_desktop_surface_get_client(surface->desktop_surface);
949 		struct wl_client *wl_client =
950 			weston_desktop_client_get_client(client);
951 		wl_client_post_no_memory(wl_client);
952 		return;
953 	}
954 	wl_list_insert(surface->configure_list.prev, &configure->link);
955 	configure->serial =
956 		wl_display_next_serial(weston_desktop_get_display(surface->desktop));
957 
958 	switch (surface->role) {
959 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
960 		assert(0 && "not reached");
961 		break;
962 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
963 		weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface,
964 							   (struct weston_desktop_xdg_toplevel_configure *) configure);
965 		break;
966 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
967 		weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface);
968 		break;
969 	}
970 
971 	xdg_surface_send_configure(surface->resource, configure->serial);
972 }
973 
974 static bool
weston_desktop_xdg_toplevel_state_compare(struct weston_desktop_xdg_toplevel * toplevel)975 weston_desktop_xdg_toplevel_state_compare(struct weston_desktop_xdg_toplevel *toplevel)
976 {
977 	struct {
978 		struct weston_desktop_xdg_toplevel_state state;
979 		struct weston_size size;
980 	} configured;
981 
982 	if (!toplevel->base.configured)
983 		return false;
984 
985 	if (wl_list_empty(&toplevel->base.configure_list)) {
986 		/* Last configure is actually the current state, just use it */
987 		configured.state = toplevel->current.state;
988 		configured.size.width = toplevel->base.surface->width;
989 		configured.size.height = toplevel->base.surface->height;
990 	} else {
991 		struct weston_desktop_xdg_toplevel_configure *configure =
992 			wl_container_of(toplevel->base.configure_list.prev,
993 					configure, base.link);
994 
995 		configured.state = configure->state;
996 		configured.size = configure->size;
997 	}
998 
999 	if (toplevel->pending.state.activated != configured.state.activated)
1000 		return false;
1001 	if (toplevel->pending.state.fullscreen != configured.state.fullscreen)
1002 		return false;
1003 	if (toplevel->pending.state.maximized != configured.state.maximized)
1004 		return false;
1005 	if (toplevel->pending.state.resizing != configured.state.resizing)
1006 		return false;
1007 
1008 	if (toplevel->pending.size.width == configured.size.width &&
1009 	    toplevel->pending.size.height == configured.size.height)
1010 		return true;
1011 
1012 	if (toplevel->pending.size.width == 0 &&
1013 	    toplevel->pending.size.height == 0)
1014 		return true;
1015 
1016 	return false;
1017 }
1018 
1019 static void
weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface * surface)1020 weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface)
1021 {
1022 	struct wl_display *display = weston_desktop_get_display(surface->desktop);
1023 	struct wl_event_loop *loop = wl_display_get_event_loop(display);
1024 	bool pending_same = false;
1025 
1026 	switch (surface->role) {
1027 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1028 		assert(0 && "not reached");
1029 		break;
1030 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1031 		pending_same = weston_desktop_xdg_toplevel_state_compare((struct weston_desktop_xdg_toplevel *) surface);
1032 		break;
1033 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1034 		break;
1035 	}
1036 
1037 	if (surface->configure_idle != NULL) {
1038 		if (!pending_same)
1039 			return;
1040 
1041 		wl_event_source_remove(surface->configure_idle);
1042 		surface->configure_idle = NULL;
1043 	} else {
1044 		if (pending_same)
1045 			return;
1046 
1047 		surface->configure_idle =
1048 			wl_event_loop_add_idle(loop,
1049 					       weston_desktop_xdg_surface_send_configure,
1050 					       surface);
1051 	}
1052 }
1053 
1054 static void
weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id)1055 weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client,
1056 						 struct wl_resource *resource,
1057 						 uint32_t id)
1058 {
1059 	struct weston_desktop_surface *dsurface =
1060 		wl_resource_get_user_data(resource);
1061 	struct weston_surface *wsurface =
1062 		weston_desktop_surface_get_surface(dsurface);
1063 	struct weston_desktop_xdg_toplevel *toplevel =
1064 		weston_desktop_surface_get_implementation_data(dsurface);
1065 
1066 	if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role,
1067 				    resource, XDG_WM_BASE_ERROR_ROLE) < 0)
1068 		return;
1069 
1070 	toplevel->resource =
1071 		weston_desktop_surface_add_resource(toplevel->base.desktop_surface,
1072 						    &xdg_toplevel_interface,
1073 						    &weston_desktop_xdg_toplevel_implementation,
1074 						    id, weston_desktop_xdg_toplevel_resource_destroy);
1075 	if (toplevel->resource == NULL)
1076 		return;
1077 
1078 	toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL;
1079 }
1080 
1081 static void
weston_desktop_xdg_surface_protocol_get_popup(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id,struct wl_resource * parent_resource,struct wl_resource * positioner_resource)1082 weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client,
1083 					      struct wl_resource *resource,
1084 					      uint32_t id,
1085 					      struct wl_resource *parent_resource,
1086 					      struct wl_resource *positioner_resource)
1087 {
1088 	struct weston_desktop_surface *dsurface =
1089 		wl_resource_get_user_data(resource);
1090 	struct weston_surface *wsurface =
1091 		weston_desktop_surface_get_surface(dsurface);
1092 	struct weston_desktop_xdg_popup *popup =
1093 		weston_desktop_surface_get_implementation_data(dsurface);
1094 	struct weston_desktop_surface *parent_surface;
1095 	struct weston_desktop_xdg_surface *parent;
1096 	struct weston_desktop_xdg_positioner *positioner =
1097 		wl_resource_get_user_data(positioner_resource);
1098 
1099 	/* Popup parents are allowed to be non-null, but only if a parent is
1100 	 * specified 'using some other protocol' before committing. Since we
1101 	 * don't support such a protocol yet, clients cannot legitimately
1102 	 * create a popup with a non-null parent. */
1103 	if (!parent_resource) {
1104 		wl_resource_post_error(resource,
1105 				       XDG_WM_BASE_ERROR_INVALID_POPUP_PARENT,
1106 				       "popup parent must be non-null");
1107 		return;
1108 	}
1109 
1110 	parent_surface = wl_resource_get_user_data(parent_resource);
1111 	parent = weston_desktop_surface_get_implementation_data(parent_surface);
1112 
1113 	/* Checking whether the size and anchor rect both have a positive size
1114 	 * is enough to verify both have been correctly set */
1115 	if (positioner->size.width == 0 || positioner->anchor_rect.width == 0 ||
1116 	    positioner->anchor_rect.height == 0) {
1117 		wl_resource_post_error(resource,
1118 				       XDG_WM_BASE_ERROR_INVALID_POSITIONER,
1119 				       "positioner object is not complete");
1120 		return;
1121 	}
1122 
1123 	if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role,
1124 				    resource, XDG_WM_BASE_ERROR_ROLE) < 0)
1125 		return;
1126 
1127 	popup->resource =
1128 		weston_desktop_surface_add_resource(popup->base.desktop_surface,
1129 						    &xdg_popup_interface,
1130 						    &weston_desktop_xdg_popup_implementation,
1131 						    id, weston_desktop_xdg_popup_resource_destroy);
1132 	if (popup->resource == NULL)
1133 		return;
1134 
1135 	popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP;
1136 	popup->parent = parent;
1137 
1138 	popup->geometry =
1139 		weston_desktop_xdg_positioner_get_geometry(positioner,
1140 							   dsurface,
1141 							   parent_surface);
1142 
1143 	weston_desktop_surface_set_relative_to(popup->base.desktop_surface,
1144 					       parent_surface,
1145 					       popup->geometry.x,
1146 					       popup->geometry.y,
1147 					       true);
1148 }
1149 
1150 static bool
weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface * surface)1151 weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface)
1152 {
1153 	struct weston_surface *wsurface =
1154 		weston_desktop_surface_get_surface(surface->desktop_surface);
1155 	const char *role;
1156 
1157 	role = weston_surface_get_role(wsurface);
1158 	if (role != NULL &&
1159 	    (role == weston_desktop_xdg_toplevel_role ||
1160 	     role == weston_desktop_xdg_popup_role))
1161 	     return true;
1162 
1163 	wl_resource_post_error(surface->resource,
1164 			       XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
1165 			       "xdg_surface must have a role");
1166 	return false;
1167 }
1168 
1169 static void
weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client * wl_client,struct wl_resource * resource,int32_t x,int32_t y,int32_t width,int32_t height)1170 weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
1171 							struct wl_resource *resource,
1172 							int32_t x, int32_t y,
1173 							int32_t width, int32_t height)
1174 {
1175 	struct weston_desktop_surface *dsurface =
1176 		wl_resource_get_user_data(resource);
1177 	struct weston_desktop_xdg_surface *surface =
1178 		weston_desktop_surface_get_implementation_data(dsurface);
1179 
1180 	if (!weston_desktop_xdg_surface_check_role(surface))
1181 		return;
1182 
1183 	surface->has_next_geometry = true;
1184 	surface->next_geometry.x = x;
1185 	surface->next_geometry.y = y;
1186 	surface->next_geometry.width = width;
1187 	surface->next_geometry.height = height;
1188 }
1189 
1190 static void
weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client * wl_client,struct wl_resource * resource,uint32_t serial)1191 weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
1192 						  struct wl_resource *resource,
1193 						  uint32_t serial)
1194 {
1195 	struct weston_desktop_surface *dsurface =
1196 		wl_resource_get_user_data(resource);
1197 	struct weston_desktop_xdg_surface *surface =
1198 		weston_desktop_surface_get_implementation_data(dsurface);
1199 	struct weston_desktop_xdg_surface_configure *configure, *temp;
1200 	bool found = false;
1201 
1202 	if (!weston_desktop_xdg_surface_check_role(surface))
1203 		return;
1204 
1205 	wl_list_for_each_safe(configure, temp, &surface->configure_list, link) {
1206 		if (configure->serial < serial) {
1207 			wl_list_remove(&configure->link);
1208 			free(configure);
1209 		} else if (configure->serial == serial) {
1210 			wl_list_remove(&configure->link);
1211 			found = true;
1212 			break;
1213 		} else {
1214 			break;
1215 		}
1216 	}
1217 	if (!found) {
1218 		struct weston_desktop_client *client =
1219 			weston_desktop_surface_get_client(dsurface);
1220 		struct wl_resource *client_resource =
1221 			weston_desktop_client_get_resource(client);
1222 		wl_resource_post_error(client_resource,
1223 				       XDG_WM_BASE_ERROR_INVALID_SURFACE_STATE,
1224 				       "Wrong configure serial: %u", serial);
1225 		return;
1226 	}
1227 
1228 	surface->configured = true;
1229 
1230 	switch (surface->role) {
1231 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1232 		assert(0 && "not reached");
1233 		break;
1234 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1235 		weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface,
1236 							  (struct weston_desktop_xdg_toplevel_configure *) configure);
1237 		break;
1238 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1239 		break;
1240 	}
1241 
1242 	free(configure);
1243 }
1244 
1245 static void
weston_desktop_xdg_surface_ping(struct weston_desktop_surface * dsurface,uint32_t serial,void * user_data)1246 weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
1247 				uint32_t serial, void *user_data)
1248 {
1249 	struct weston_desktop_client *client =
1250 		weston_desktop_surface_get_client(dsurface);
1251 
1252 	xdg_wm_base_send_ping(weston_desktop_client_get_resource(client),
1253 			      serial);
1254 }
1255 
1256 static void
weston_desktop_xdg_surface_committed(struct weston_desktop_surface * dsurface,void * user_data,int32_t sx,int32_t sy)1257 weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
1258 				     void *user_data,
1259 				     int32_t sx, int32_t sy)
1260 {
1261 	struct weston_desktop_xdg_surface *surface = user_data;
1262 	struct weston_surface *wsurface =
1263 		weston_desktop_surface_get_surface (dsurface);
1264 
1265 	if (wsurface->buffer_ref.buffer && !surface->configured) {
1266 		wl_resource_post_error(surface->resource,
1267 				       XDG_SURFACE_ERROR_UNCONFIGURED_BUFFER,
1268 				       "xdg_surface has never been configured");
1269 		return;
1270 	}
1271 
1272 	if (surface->has_next_geometry) {
1273 		surface->has_next_geometry = false;
1274 		weston_desktop_surface_set_geometry(surface->desktop_surface,
1275 						    surface->next_geometry);
1276 	}
1277 
1278 	switch (surface->role) {
1279 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1280 		wl_resource_post_error(surface->resource,
1281 				       XDG_SURFACE_ERROR_NOT_CONSTRUCTED,
1282 				       "xdg_surface must have a role");
1283 		break;
1284 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1285 		weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy);
1286 		break;
1287 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1288 		weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface);
1289 		break;
1290 	}
1291 }
1292 
1293 static void
weston_desktop_xdg_surface_close(struct weston_desktop_surface * dsurface,void * user_data)1294 weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
1295 				 void *user_data)
1296 {
1297 	struct weston_desktop_xdg_surface *surface = user_data;
1298 
1299 	switch (surface->role) {
1300 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1301 		assert(0 && "not reached");
1302 		break;
1303 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1304 		weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface);
1305 		break;
1306 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1307 		weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface);
1308 		break;
1309 	}
1310 }
1311 
1312 static void
weston_desktop_xdg_surface_destroy(struct weston_desktop_surface * dsurface,void * user_data)1313 weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
1314 				   void *user_data)
1315 {
1316 	struct weston_desktop_xdg_surface *surface = user_data;
1317 	struct weston_desktop_xdg_surface_configure *configure, *temp;
1318 
1319 	switch (surface->role) {
1320 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE:
1321 		break;
1322 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL:
1323 		weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface);
1324 		break;
1325 	case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP:
1326 		weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface);
1327 		break;
1328 	}
1329 
1330 	if (surface->configure_idle != NULL)
1331 		wl_event_source_remove(surface->configure_idle);
1332 
1333 	wl_list_for_each_safe(configure, temp, &surface->configure_list, link)
1334 		free(configure);
1335 
1336 	free(surface);
1337 }
1338 
1339 static const struct xdg_surface_interface weston_desktop_xdg_surface_implementation = {
1340 	.destroy             = weston_desktop_destroy_request,
1341 	.get_toplevel        = weston_desktop_xdg_surface_protocol_get_toplevel,
1342 	.get_popup           = weston_desktop_xdg_surface_protocol_get_popup,
1343 	.set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
1344 	.ack_configure       = weston_desktop_xdg_surface_protocol_ack_configure,
1345 };
1346 
1347 static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
1348 	/* These are used for toplevel only */
1349 	.set_maximized = weston_desktop_xdg_toplevel_set_maximized,
1350 	.set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen,
1351 	.set_resizing = weston_desktop_xdg_toplevel_set_resizing,
1352 	.set_activated = weston_desktop_xdg_toplevel_set_activated,
1353 	.set_size = weston_desktop_xdg_toplevel_set_size,
1354 
1355 	.get_maximized = weston_desktop_xdg_toplevel_get_maximized,
1356 	.get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen,
1357 	.get_resizing = weston_desktop_xdg_toplevel_get_resizing,
1358 	.get_activated = weston_desktop_xdg_toplevel_get_activated,
1359 
1360 	/* These are used for popup only */
1361 	.update_position = weston_desktop_xdg_popup_update_position,
1362 
1363 	/* Common API */
1364 	.committed = weston_desktop_xdg_surface_committed,
1365 	.ping = weston_desktop_xdg_surface_ping,
1366 	.close = weston_desktop_xdg_surface_close,
1367 
1368 	.destroy = weston_desktop_xdg_surface_destroy,
1369 };
1370 
1371 static void
weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id)1372 weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client,
1373 						    struct wl_resource *resource,
1374 						    uint32_t id)
1375 {
1376 	struct weston_desktop_client *client =
1377 		wl_resource_get_user_data(resource);
1378 	struct weston_desktop_xdg_positioner *positioner;
1379 
1380 	positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner));
1381 	if (positioner == NULL) {
1382 		wl_client_post_no_memory(wl_client);
1383 		return;
1384 	}
1385 
1386 	positioner->client = client;
1387 	positioner->desktop = weston_desktop_client_get_desktop(positioner->client);
1388 
1389 	positioner->resource =
1390 		wl_resource_create(wl_client,
1391 				   &xdg_positioner_interface,
1392 				   wl_resource_get_version(resource), id);
1393 	if (positioner->resource == NULL) {
1394 		wl_client_post_no_memory(wl_client);
1395 		free(positioner);
1396 		return;
1397 	}
1398 	wl_resource_set_implementation(positioner->resource,
1399 				       &weston_desktop_xdg_positioner_implementation,
1400 				       positioner, weston_desktop_xdg_positioner_destroy);
1401 }
1402 
1403 static void
weston_desktop_xdg_surface_resource_destroy(struct wl_resource * resource)1404 weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource)
1405 {
1406 	struct weston_desktop_surface *dsurface =
1407 		wl_resource_get_user_data(resource);
1408 
1409 	if (dsurface != NULL)
1410 		weston_desktop_surface_resource_destroy(resource);
1411 }
1412 
1413 static void
weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client * wl_client,struct wl_resource * resource,uint32_t id,struct wl_resource * surface_resource)1414 weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
1415 						  struct wl_resource *resource,
1416 						  uint32_t id,
1417 						  struct wl_resource *surface_resource)
1418 {
1419 	struct weston_desktop_client *client =
1420 		wl_resource_get_user_data(resource);
1421 	struct weston_surface *wsurface =
1422 		wl_resource_get_user_data(surface_resource);
1423 	struct weston_desktop_xdg_surface *surface;
1424 
1425 	surface = zalloc(weston_desktop_surface_role_biggest_size);
1426 	if (surface == NULL) {
1427 		wl_client_post_no_memory(wl_client);
1428 		return;
1429 	}
1430 
1431 	surface->desktop = weston_desktop_client_get_desktop(client);
1432 	surface->surface = wsurface;
1433 	wl_list_init(&surface->configure_list);
1434 
1435 	surface->desktop_surface =
1436 		weston_desktop_surface_create(surface->desktop, client,
1437 					      surface->surface,
1438 					      &weston_desktop_xdg_surface_internal_implementation,
1439 					      surface);
1440 	if (surface->desktop_surface == NULL) {
1441 		free(surface);
1442 		return;
1443 	}
1444 
1445 	surface->resource =
1446 		weston_desktop_surface_add_resource(surface->desktop_surface,
1447 						    &xdg_surface_interface,
1448 						    &weston_desktop_xdg_surface_implementation,
1449 						    id, weston_desktop_xdg_surface_resource_destroy);
1450 	if (surface->resource == NULL)
1451 		return;
1452 
1453 	if (wsurface->buffer_ref.buffer != NULL) {
1454 		wl_resource_post_error(surface->resource,
1455 				       XDG_SURFACE_ERROR_UNCONFIGURED_BUFFER,
1456 				       "xdg_surface must not have a buffer at creation");
1457 		return;
1458 	}
1459 }
1460 
1461 static void
weston_desktop_xdg_shell_protocol_pong(struct wl_client * wl_client,struct wl_resource * resource,uint32_t serial)1462 weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
1463 				       struct wl_resource *resource,
1464 				       uint32_t serial)
1465 {
1466 	struct weston_desktop_client *client =
1467 		wl_resource_get_user_data(resource);
1468 
1469 	weston_desktop_client_pong(client, serial);
1470 }
1471 
1472 static const struct xdg_wm_base_interface weston_desktop_xdg_shell_implementation = {
1473 	.destroy = weston_desktop_destroy_request,
1474 	.create_positioner = weston_desktop_xdg_shell_protocol_create_positioner,
1475 	.get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
1476 	.pong = weston_desktop_xdg_shell_protocol_pong,
1477 };
1478 
1479 static void
weston_desktop_xdg_shell_bind(struct wl_client * client,void * data,uint32_t version,uint32_t id)1480 weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
1481 			      uint32_t version, uint32_t id)
1482 {
1483 	struct weston_desktop *desktop = data;
1484 
1485 	weston_desktop_client_create(desktop, client, NULL,
1486 				     &xdg_wm_base_interface,
1487 				     &weston_desktop_xdg_shell_implementation,
1488 				     version, id);
1489 }
1490 
1491 struct wl_global *
weston_desktop_xdg_wm_base_create(struct weston_desktop * desktop,struct wl_display * display)1492 weston_desktop_xdg_wm_base_create(struct weston_desktop *desktop,
1493 				  struct wl_display *display)
1494 {
1495 	return wl_global_create(display, &xdg_wm_base_interface,
1496 				WD_XDG_SHELL_PROTOCOL_VERSION, desktop,
1497 				weston_desktop_xdg_shell_bind);
1498 }
1499