blob: b32b7812107a7854587947dd04a47301e711fb4f [file] [log] [blame]
Quentin Glidic248dd102016-08-12 10:41:34 +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
31#include <wayland-server.h>
32
33#include "compositor.h"
34#include "zalloc.h"
Daniel Stone7dbb0e12016-11-24 15:30:41 +000035#include "xdg-shell-unstable-v5-server-protocol.h"
Quentin Glidic248dd102016-08-12 10:41:34 +020036
37#include "libweston-desktop.h"
38#include "internal.h"
39
40#define WD_XDG_SHELL_PROTOCOL_VERSION 1
41
Quentin Glidic19d1f6e2017-07-12 09:42:57 +020042struct weston_desktop_xdg_surface_state {
43 bool maximized;
44 bool fullscreen;
45 bool resizing;
46 bool activated;
47};
48
Quentin Glidic248dd102016-08-12 10:41:34 +020049struct weston_desktop_xdg_surface {
50 struct wl_resource *resource;
51 struct weston_desktop_surface *surface;
52 struct weston_desktop *desktop;
Quentin Glidic6967f0e2016-08-18 15:51:38 +020053 bool added;
Quentin Glidic248dd102016-08-12 10:41:34 +020054 struct wl_event_source *add_idle;
55 struct wl_event_source *configure_idle;
56 uint32_t configure_serial;
Quentin Glidic248dd102016-08-12 10:41:34 +020057 struct {
Quentin Glidic19d1f6e2017-07-12 09:42:57 +020058 struct weston_desktop_xdg_surface_state state;
59 struct weston_size size;
60 } pending;
61 struct {
62 struct weston_desktop_xdg_surface_state state;
Quentin Glidicac394a12017-07-12 09:45:43 +020063 struct weston_size size;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +020064 } next;
65 struct {
66 struct weston_desktop_xdg_surface_state state;
67 } current;
Quentin Glidic248dd102016-08-12 10:41:34 +020068 bool has_next_geometry;
69 struct weston_geometry next_geometry;
70};
71
72struct weston_desktop_xdg_popup {
73 struct wl_resource *resource;
74 struct weston_desktop_surface *popup;
75 struct weston_desktop *desktop;
76 struct weston_desktop_seat *seat;
77 struct wl_display *display;
78};
79
80static void
Quentin Glidic6967f0e2016-08-18 15:51:38 +020081weston_desktop_xdg_surface_ensure_added(struct weston_desktop_xdg_surface *surface)
82{
83 if (surface->added)
84 return;
85
86 if (surface->add_idle != NULL)
87 wl_event_source_remove(surface->add_idle);
88 surface->add_idle = NULL;
89 weston_desktop_api_surface_added(surface->desktop, surface->surface);
90 surface->added = true;
91}
92
93static void
Quentin Glidic248dd102016-08-12 10:41:34 +020094weston_desktop_xdg_surface_send_configure(void *data)
95{
96 struct weston_desktop_xdg_surface *surface = data;
97 uint32_t *s;
98 struct wl_array states;
99
100 surface->configure_idle = NULL;
101
102 surface->configure_serial =
103 wl_display_next_serial(weston_desktop_get_display(surface->desktop));
104
105 wl_array_init(&states);
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200106 if (surface->pending.state.maximized) {
Quentin Glidic248dd102016-08-12 10:41:34 +0200107 s = wl_array_add(&states, sizeof(uint32_t));
108 *s = XDG_SURFACE_STATE_MAXIMIZED;
109 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200110 if (surface->pending.state.fullscreen) {
Quentin Glidic248dd102016-08-12 10:41:34 +0200111 s = wl_array_add(&states, sizeof(uint32_t));
112 *s = XDG_SURFACE_STATE_FULLSCREEN;
113 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200114 if (surface->pending.state.resizing) {
Quentin Glidic248dd102016-08-12 10:41:34 +0200115 s = wl_array_add(&states, sizeof(uint32_t));
116 *s = XDG_SURFACE_STATE_RESIZING;
117 }
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200118 if (surface->pending.state.activated) {
Quentin Glidic248dd102016-08-12 10:41:34 +0200119 s = wl_array_add(&states, sizeof(uint32_t));
120 *s = XDG_SURFACE_STATE_ACTIVATED;
121 }
122
123 xdg_surface_send_configure(surface->resource,
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200124 surface->pending.size.width,
125 surface->pending.size.height,
Quentin Glidic248dd102016-08-12 10:41:34 +0200126 &states,
127 surface->configure_serial);
128
129 wl_array_release(&states);
130};
131
Quentin Glidicd51f8262017-04-13 20:25:27 +0200132static bool
133weston_desktop_xdg_surface_state_compare(struct weston_desktop_xdg_surface *surface)
134{
135 struct weston_surface *wsurface =
136 weston_desktop_surface_get_surface(surface->surface);
137
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200138 if (surface->pending.state.activated != surface->current.state.activated)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200139 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200140 if (surface->pending.state.fullscreen != surface->current.state.fullscreen)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200141 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200142 if (surface->pending.state.maximized != surface->current.state.maximized)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200143 return false;
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200144 if (surface->pending.state.resizing != surface->current.state.resizing)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200145 return false;
146
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200147 if (wsurface->width == surface->pending.size.width &&
148 wsurface->height == surface->pending.size.height)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200149 return true;
150
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200151 if (surface->pending.size.width == 0 &&
152 surface->pending.size.height == 0)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200153 return true;
154
155 return false;
156}
157
Quentin Glidic248dd102016-08-12 10:41:34 +0200158static void
Quentin Glidicd51f8262017-04-13 20:25:27 +0200159weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface,
160 bool force)
Quentin Glidic248dd102016-08-12 10:41:34 +0200161{
162 struct wl_display *display = weston_desktop_get_display(surface->desktop);
163 struct wl_event_loop *loop = wl_display_get_event_loop(display);
Quentin Glidic218126d2017-07-11 13:31:36 +0200164 bool pending_same =
Quentin Glidicd51f8262017-04-13 20:25:27 +0200165 !force && weston_desktop_xdg_surface_state_compare(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200166
Quentin Glidicd51f8262017-04-13 20:25:27 +0200167 if (surface->configure_idle != NULL) {
Quentin Glidic218126d2017-07-11 13:31:36 +0200168 if (!pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200169 return;
170
171 wl_event_source_remove(surface->configure_idle);
172 surface->configure_idle = NULL;
173 } else {
Quentin Glidic218126d2017-07-11 13:31:36 +0200174 if (pending_same)
Quentin Glidicd51f8262017-04-13 20:25:27 +0200175 return;
176
177 surface->configure_idle =
178 wl_event_loop_add_idle(loop,
179 weston_desktop_xdg_surface_send_configure,
180 surface);
181 }
Quentin Glidic248dd102016-08-12 10:41:34 +0200182}
183
184static void
185weston_desktop_xdg_surface_set_maximized(struct weston_desktop_surface *dsurface,
186 void *user_data, bool maximized)
187{
188 struct weston_desktop_xdg_surface *surface = user_data;
189
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200190 surface->pending.state.maximized = maximized;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200191 weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic248dd102016-08-12 10:41:34 +0200192}
193
194static void
195weston_desktop_xdg_surface_set_fullscreen(struct weston_desktop_surface *dsurface,
196 void *user_data, bool fullscreen)
197{
198 struct weston_desktop_xdg_surface *surface = user_data;
199
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200200 surface->pending.state.fullscreen = fullscreen;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200201 weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic248dd102016-08-12 10:41:34 +0200202}
203
204static void
205weston_desktop_xdg_surface_set_resizing(struct weston_desktop_surface *dsurface,
206 void *user_data, bool resizing)
207{
208 struct weston_desktop_xdg_surface *surface = user_data;
209
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200210 surface->pending.state.resizing = resizing;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200211 weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic248dd102016-08-12 10:41:34 +0200212}
213
214static void
215weston_desktop_xdg_surface_set_activated(struct weston_desktop_surface *dsurface,
216 void *user_data, bool activated)
217{
218 struct weston_desktop_xdg_surface *surface = user_data;
219
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200220 surface->pending.state.activated = activated;
Quentin Glidicd51f8262017-04-13 20:25:27 +0200221 weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic248dd102016-08-12 10:41:34 +0200222}
223
224static void
225weston_desktop_xdg_surface_set_size(struct weston_desktop_surface *dsurface,
226 void *user_data,
227 int32_t width, int32_t height)
228{
229 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200230
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200231 surface->pending.size.width = width;
232 surface->pending.size.height = height;
Quentin Glidica56b0532016-09-13 10:05:58 +0200233
Quentin Glidicd51f8262017-04-13 20:25:27 +0200234 weston_desktop_xdg_surface_schedule_configure(surface, false);
Quentin Glidic248dd102016-08-12 10:41:34 +0200235}
236
237static void
238weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +0200239 void *user_data,
Quentin Glidic248dd102016-08-12 10:41:34 +0200240 int32_t sx, int32_t sy)
241{
242 struct weston_desktop_xdg_surface *surface = user_data;
243 struct weston_surface *wsurface =
244 weston_desktop_surface_get_surface(surface->surface);
245 bool reconfigure = false;
246
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200247 if (surface->next.state.maximized || surface->next.state.fullscreen)
Quentin Glidicac394a12017-07-12 09:45:43 +0200248 reconfigure = surface->next.size.width != wsurface->width ||
249 surface->next.size.height != wsurface->height;
Quentin Glidic248dd102016-08-12 10:41:34 +0200250
251 if (reconfigure) {
Quentin Glidicd51f8262017-04-13 20:25:27 +0200252 weston_desktop_xdg_surface_schedule_configure(surface, true);
Quentin Glidic248dd102016-08-12 10:41:34 +0200253 } else {
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200254 surface->current.state = surface->next.state;
Quentin Glidic248dd102016-08-12 10:41:34 +0200255 if (surface->has_next_geometry) {
256 surface->has_next_geometry = false;
257 weston_desktop_surface_set_geometry(surface->surface,
258 surface->next_geometry);
259 }
260
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200261 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200262 weston_desktop_api_committed(surface->desktop, surface->surface,
263 sx, sy);
264 }
265}
266
267static void
268weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface,
269 uint32_t serial, void *user_data)
270{
271 struct weston_desktop_client *client =
272 weston_desktop_surface_get_client(dsurface);
273
274 xdg_shell_send_ping(weston_desktop_client_get_resource(client),
275 serial);
276}
277
278static void
279weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface,
280 void *user_data)
281{
282 struct weston_desktop_xdg_surface *surface = user_data;
283
284 xdg_surface_send_close(surface->resource);
285}
286
287static bool
288weston_desktop_xdg_surface_get_maximized(struct weston_desktop_surface *dsurface,
289 void *user_data)
290{
291 struct weston_desktop_xdg_surface *surface = user_data;
292
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200293 return surface->current.state.maximized;
Quentin Glidic248dd102016-08-12 10:41:34 +0200294}
295
296static bool
297weston_desktop_xdg_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
298 void *user_data)
299{
300 struct weston_desktop_xdg_surface *surface = user_data;
301
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200302 return surface->current.state.fullscreen;
Quentin Glidic248dd102016-08-12 10:41:34 +0200303}
304
305static bool
306weston_desktop_xdg_surface_get_resizing(struct weston_desktop_surface *dsurface,
307 void *user_data)
308{
309 struct weston_desktop_xdg_surface *surface = user_data;
310
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200311 return surface->current.state.resizing;
Quentin Glidic248dd102016-08-12 10:41:34 +0200312}
313
314static bool
315weston_desktop_xdg_surface_get_activated(struct weston_desktop_surface *dsurface,
316 void *user_data)
317{
318 struct weston_desktop_xdg_surface *surface = user_data;
319
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200320 return surface->current.state.activated;
Quentin Glidic248dd102016-08-12 10:41:34 +0200321}
322
323static void
324weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface,
325 void *user_data)
326{
327 struct weston_desktop_xdg_surface *surface = user_data;
328
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200329 if (surface->added)
330 weston_desktop_api_surface_removed(surface->desktop,
331 surface->surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200332
333 if (surface->add_idle != NULL)
334 wl_event_source_remove(surface->add_idle);
335
336 if (surface->configure_idle != NULL)
337 wl_event_source_remove(surface->configure_idle);
338
339 free(surface);
340}
341
342static void
343weston_desktop_xdg_surface_protocol_set_parent(struct wl_client *wl_client,
344 struct wl_resource *resource,
345 struct wl_resource *parent_resource)
346{
347 struct weston_desktop_surface *dsurface =
348 wl_resource_get_user_data(resource);
349 struct weston_desktop_xdg_surface *surface =
350 weston_desktop_surface_get_implementation_data(dsurface);
351 struct weston_desktop_surface *parent = NULL;
352
353 if (parent_resource != NULL)
354 parent = wl_resource_get_user_data(parent_resource);
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200355
356 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200357 weston_desktop_api_set_parent(surface->desktop, dsurface, parent);
358}
359
360static void
361weston_desktop_xdg_surface_protocol_set_title(struct wl_client *wl_client,
362 struct wl_resource *resource,
363 const char *title)
364{
365 struct weston_desktop_surface *surface =
366 wl_resource_get_user_data(resource);
367
368 weston_desktop_surface_set_title(surface, title);
369}
370
371static void
372weston_desktop_xdg_surface_protocol_set_app_id(struct wl_client *wl_client,
373 struct wl_resource *resource,
374 const char *app_id)
375{
376 struct weston_desktop_surface *surface =
377 wl_resource_get_user_data(resource);
378
379 weston_desktop_surface_set_app_id(surface, app_id);
380}
381
382static void
383weston_desktop_xdg_surface_protocol_show_window_menu(struct wl_client *wl_client,
384 struct wl_resource *resource,
385 struct wl_resource *seat_resource,
386 uint32_t serial,
387 int32_t x, int32_t y)
388{
389 struct weston_desktop_surface *dsurface =
390 wl_resource_get_user_data(resource);
391 struct weston_seat *seat =
392 wl_resource_get_user_data(seat_resource);
393 struct weston_desktop_xdg_surface *surface =
394 weston_desktop_surface_get_implementation_data(dsurface);
395
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200396 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200397 weston_desktop_api_show_window_menu(surface->desktop, dsurface, seat, x, y);
398}
399
400static void
401weston_desktop_xdg_surface_protocol_move(struct wl_client *wl_client,
402 struct wl_resource *resource,
403 struct wl_resource *seat_resource,
404 uint32_t serial)
405{
406 struct weston_desktop_surface *dsurface =
407 wl_resource_get_user_data(resource);
408 struct weston_seat *seat =
409 wl_resource_get_user_data(seat_resource);
410 struct weston_desktop_xdg_surface *surface =
411 weston_desktop_surface_get_implementation_data(dsurface);
412
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200413 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200414 weston_desktop_api_move(surface->desktop, dsurface, seat, serial);
415}
416
417static void
418weston_desktop_xdg_surface_protocol_resize(struct wl_client *wl_client,
419 struct wl_resource *resource,
420 struct wl_resource *seat_resource,
421 uint32_t serial,
422 enum xdg_surface_resize_edge edges)
423{
424 struct weston_desktop_surface *dsurface =
425 wl_resource_get_user_data(resource);
426 struct weston_seat *seat =
427 wl_resource_get_user_data(seat_resource);
428 struct weston_desktop_xdg_surface *surface =
429 weston_desktop_surface_get_implementation_data(dsurface);
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200430 enum weston_desktop_surface_edge surf_edges =
431 (enum weston_desktop_surface_edge) edges;
Quentin Glidic248dd102016-08-12 10:41:34 +0200432
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200433 weston_desktop_xdg_surface_ensure_added(surface);
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200434 weston_desktop_api_resize(surface->desktop, dsurface, seat, serial, surf_edges);
Quentin Glidic248dd102016-08-12 10:41:34 +0200435}
436
437static void
438weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client,
439 struct wl_resource *resource,
440 uint32_t serial)
441{
442 struct weston_desktop_surface *dsurface =
443 wl_resource_get_user_data(resource);
444 struct weston_desktop_xdg_surface *surface =
445 weston_desktop_surface_get_implementation_data(dsurface);
446
447 if (surface->configure_serial != serial)
448 return;
449
Quentin Glidic19d1f6e2017-07-12 09:42:57 +0200450 surface->next.state = surface->pending.state;
Quentin Glidicac394a12017-07-12 09:45:43 +0200451 surface->next.size = surface->pending.size;
Quentin Glidic248dd102016-08-12 10:41:34 +0200452}
453
454static void
455weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client,
456 struct wl_resource *resource,
457 int32_t x, int32_t y,
458 int32_t width, int32_t height)
459{
460 struct weston_desktop_surface *dsurface =
461 wl_resource_get_user_data(resource);
462 struct weston_desktop_xdg_surface *surface =
463 weston_desktop_surface_get_implementation_data(dsurface);
464
465 surface->has_next_geometry = true;
466 surface->next_geometry.x = x;
467 surface->next_geometry.y = y;
468 surface->next_geometry.width = width;
469 surface->next_geometry.height = height;
470}
471
472static void
473weston_desktop_xdg_surface_protocol_set_maximized(struct wl_client *wl_client,
474 struct wl_resource *resource)
475{
476 struct weston_desktop_surface *dsurface =
477 wl_resource_get_user_data(resource);
478 struct weston_desktop_xdg_surface *surface =
479 weston_desktop_surface_get_implementation_data(dsurface);
480
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200481 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200482 weston_desktop_api_maximized_requested(surface->desktop, dsurface, true);
483}
484
485static void
486weston_desktop_xdg_surface_protocol_unset_maximized(struct wl_client *wl_client,
487 struct wl_resource *resource)
488{
489 struct weston_desktop_surface *dsurface =
490 wl_resource_get_user_data(resource);
491 struct weston_desktop_xdg_surface *surface =
492 weston_desktop_surface_get_implementation_data(dsurface);
493
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200494 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200495 weston_desktop_api_maximized_requested(surface->desktop, dsurface, false);
496}
497
498static void
499weston_desktop_xdg_surface_protocol_set_fullscreen(struct wl_client *wl_client,
500 struct wl_resource *resource,
501 struct wl_resource *output_resource)
502{
503 struct weston_desktop_surface *dsurface =
504 wl_resource_get_user_data(resource);
505 struct weston_desktop_xdg_surface *surface =
506 weston_desktop_surface_get_implementation_data(dsurface);
507 struct weston_output *output = NULL;
508
509 if (output_resource != NULL)
510 output = wl_resource_get_user_data(output_resource);
511
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200512 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200513 weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
514 true, output);
515}
516
517static void
518weston_desktop_xdg_surface_protocol_unset_fullscreen(struct wl_client *wl_client,
519 struct wl_resource *resource)
520{
521 struct weston_desktop_surface *dsurface =
522 wl_resource_get_user_data(resource);
523 struct weston_desktop_xdg_surface *surface =
524 weston_desktop_surface_get_implementation_data(dsurface);
525
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200526 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200527 weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
528 false, NULL);
529}
530
531static void
532weston_desktop_xdg_surface_protocol_set_minimized(struct wl_client *wl_client,
533 struct wl_resource *resource)
534{
535 struct weston_desktop_surface *dsurface =
536 wl_resource_get_user_data(resource);
537 struct weston_desktop_xdg_surface *surface =
538 weston_desktop_surface_get_implementation_data(dsurface);
539
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200540 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200541 weston_desktop_api_minimized_requested(surface->desktop, dsurface);
542}
543
544static const struct xdg_surface_interface weston_desktop_xdg_surface_implementation = {
545 .destroy = weston_desktop_destroy_request,
546 .set_parent = weston_desktop_xdg_surface_protocol_set_parent,
547 .set_title = weston_desktop_xdg_surface_protocol_set_title,
548 .set_app_id = weston_desktop_xdg_surface_protocol_set_app_id,
549 .show_window_menu = weston_desktop_xdg_surface_protocol_show_window_menu,
550 .move = weston_desktop_xdg_surface_protocol_move,
551 .resize = weston_desktop_xdg_surface_protocol_resize,
552 .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure,
553 .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry,
554 .set_maximized = weston_desktop_xdg_surface_protocol_set_maximized,
555 .unset_maximized = weston_desktop_xdg_surface_protocol_unset_maximized,
556 .set_fullscreen = weston_desktop_xdg_surface_protocol_set_fullscreen,
557 .unset_fullscreen = weston_desktop_xdg_surface_protocol_unset_fullscreen,
558 .set_minimized = weston_desktop_xdg_surface_protocol_set_minimized,
559};
560
561static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = {
562 .set_maximized = weston_desktop_xdg_surface_set_maximized,
563 .set_fullscreen = weston_desktop_xdg_surface_set_fullscreen,
564 .set_resizing = weston_desktop_xdg_surface_set_resizing,
565 .set_activated = weston_desktop_xdg_surface_set_activated,
566 .set_size = weston_desktop_xdg_surface_set_size,
567 .committed = weston_desktop_xdg_surface_committed,
568 .ping = weston_desktop_xdg_surface_ping,
569 .close = weston_desktop_xdg_surface_close,
570
571 .get_maximized = weston_desktop_xdg_surface_get_maximized,
572 .get_fullscreen = weston_desktop_xdg_surface_get_fullscreen,
573 .get_resizing = weston_desktop_xdg_surface_get_resizing,
574 .get_activated = weston_desktop_xdg_surface_get_activated,
575
576 .destroy = weston_desktop_xdg_surface_destroy,
577};
578
579static void
580weston_desktop_xdg_popup_close(struct weston_desktop_surface *dsurface,
581 void *user_data)
582{
583 struct weston_desktop_xdg_popup *popup = user_data;
584
585 xdg_popup_send_popup_done(popup->resource);
586}
587
588static void
589weston_desktop_xdg_popup_destroy(struct weston_desktop_surface *dsurface,
590 void *user_data)
591{
592 struct weston_desktop_xdg_popup *popup = user_data;
593 struct weston_desktop_surface *topmost;
594 struct weston_desktop_client *client =
595 weston_desktop_surface_get_client(popup->popup);
596
597 if (!weston_desktop_surface_get_grab(popup->popup))
598 goto end;
599
600 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat);
601 if (topmost != popup->popup) {
602 struct wl_resource *client_resource =
603 weston_desktop_client_get_resource(client);
604
605 wl_resource_post_error(client_resource,
606 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
607 "xdg_popup was destroyed while it was not the topmost popup.");
608 }
609
610 weston_desktop_surface_popup_ungrab(popup->popup, popup->seat);
611
612end:
613 free(popup);
614}
615
616static const struct xdg_popup_interface weston_desktop_xdg_popup_implementation = {
617 .destroy = weston_desktop_destroy_request,
618};
619
620static const struct weston_desktop_surface_implementation weston_desktop_xdg_popup_internal_implementation = {
621 .close = weston_desktop_xdg_popup_close,
622
623 .destroy = weston_desktop_xdg_popup_destroy,
624};
625
626static void
627weston_desktop_xdg_shell_protocol_use_unstable_version(struct wl_client *wl_client,
628 struct wl_resource *resource,
629 int32_t version)
630{
631 if (version > 1) {
632 wl_resource_post_error(resource,
633 1, "xdg_shell version not supported");
634 return;
635 }
636}
637
638static void
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200639weston_desktop_xdg_surface_add_idle_callback(void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200640{
641 struct weston_desktop_xdg_surface *surface = user_data;
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200642
Quentin Glidic248dd102016-08-12 10:41:34 +0200643 surface->add_idle = NULL;
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200644 weston_desktop_xdg_surface_ensure_added(surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200645}
646
647static void
648weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client,
649 struct wl_resource *resource,
650 uint32_t id,
651 struct wl_resource *surface_resource)
652{
653 struct weston_desktop_client *client =
654 wl_resource_get_user_data(resource);
655 struct weston_desktop *desktop =
656 weston_desktop_client_get_desktop(client);
657 struct weston_surface *wsurface =
658 wl_resource_get_user_data(surface_resource);
659 struct weston_desktop_xdg_surface *surface;
660 struct wl_display *display = weston_desktop_get_display(desktop);
661 struct wl_event_loop *loop = wl_display_get_event_loop(display);
662
663 if (weston_surface_set_role(wsurface, "xdg_surface", resource, XDG_SHELL_ERROR_ROLE) < 0)
664 return;
665
666 surface = zalloc(sizeof(struct weston_desktop_xdg_surface));
667 if (surface == NULL) {
668 wl_client_post_no_memory(wl_client);
669 return;
670 }
671
672 surface->desktop = desktop;
673
674 surface->surface =
675 weston_desktop_surface_create(surface->desktop, client,
676 wsurface,
677 &weston_desktop_xdg_surface_internal_implementation,
678 surface);
679 if (surface->surface == NULL) {
680 free(surface);
681 return;
682 }
683
684 surface->resource =
685 weston_desktop_surface_add_resource(surface->surface,
686 &xdg_surface_interface,
687 &weston_desktop_xdg_surface_implementation,
688 id, NULL);
689 if (surface->resource == NULL)
690 return;
691
692 surface->add_idle =
693 wl_event_loop_add_idle(loop,
Quentin Glidic6967f0e2016-08-18 15:51:38 +0200694 weston_desktop_xdg_surface_add_idle_callback,
Quentin Glidic248dd102016-08-12 10:41:34 +0200695 surface);
696}
697
698static void
699weston_desktop_xdg_shell_protocol_get_xdg_popup(struct wl_client *wl_client,
700 struct wl_resource *resource,
701 uint32_t id,
702 struct wl_resource *surface_resource,
703 struct wl_resource *parent_resource,
704 struct wl_resource *seat_resource,
705 uint32_t serial,
706 int32_t x, int32_t y)
707{
708 struct weston_desktop_client *client =
709 wl_resource_get_user_data(resource);
710 struct weston_surface *wsurface =
711 wl_resource_get_user_data(surface_resource);
712 struct weston_surface *wparent =
713 wl_resource_get_user_data(parent_resource);
714 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
715 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
716 struct weston_desktop_surface *parent, *topmost;
717 bool parent_is_popup, parent_is_xdg;
718 struct weston_desktop_xdg_popup *popup;
719
720 if (weston_surface_set_role(wsurface, "xdg_popup", resource, XDG_SHELL_ERROR_ROLE) < 0)
721 return;
722
723 if (!weston_surface_is_desktop_surface(wparent)) {
724 wl_resource_post_error(resource,
725 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
726 "xdg_popup parent was invalid");
727 return;
728 }
729
730 parent = weston_surface_get_desktop_surface(wparent);
731 parent_is_xdg =
732 weston_desktop_surface_has_implementation(parent,
733 &weston_desktop_xdg_surface_internal_implementation);
734 parent_is_popup =
735 weston_desktop_surface_has_implementation(parent,
736 &weston_desktop_xdg_popup_internal_implementation);
737
738 if (!parent_is_xdg && !parent_is_popup) {
739 wl_resource_post_error(resource,
740 XDG_SHELL_ERROR_INVALID_POPUP_PARENT,
741 "xdg_popup parent was invalid");
742 return;
743 }
744
745 topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat);
746 if ((topmost == NULL && parent_is_popup) ||
747 (topmost != NULL && topmost != parent)) {
748 wl_resource_post_error(resource,
749 XDG_SHELL_ERROR_NOT_THE_TOPMOST_POPUP,
750 "xdg_popup was not created on the topmost popup");
751 return;
752 }
753
754 popup = zalloc(sizeof(struct weston_desktop_xdg_popup));
755 if (popup == NULL) {
756 wl_client_post_no_memory(wl_client);
757 return;
758 }
759
760 popup->desktop = weston_desktop_client_get_desktop(client);
761 popup->display = weston_desktop_get_display(popup->desktop);
762 popup->seat = seat;
763
764 popup->popup =
765 weston_desktop_surface_create(popup->desktop, client, wsurface,
766 &weston_desktop_xdg_popup_internal_implementation,
767 popup);
768 if (popup->popup == NULL) {
769 free(popup);
770 return;
771 }
772
773 popup->resource =
774 weston_desktop_surface_add_resource(popup->popup,
775 &xdg_popup_interface,
776 &weston_desktop_xdg_popup_implementation,
777 id, NULL);
778 if (popup->resource == NULL)
779 return;
780
781 weston_desktop_surface_set_relative_to(popup->popup, parent, x, y, false);
782 weston_desktop_surface_popup_grab(popup->popup, popup->seat, serial);
783}
784
785static void
786weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client,
787 struct wl_resource *resource,
788 uint32_t serial)
789{
790 struct weston_desktop_client *client =
791 wl_resource_get_user_data(resource);
792
793 weston_desktop_client_pong(client, serial);
794}
795
796static const struct xdg_shell_interface weston_desktop_xdg_shell_implementation = {
797 .destroy = weston_desktop_destroy_request,
798 .use_unstable_version = weston_desktop_xdg_shell_protocol_use_unstable_version,
799 .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface,
800 .get_xdg_popup = weston_desktop_xdg_shell_protocol_get_xdg_popup,
801 .pong = weston_desktop_xdg_shell_protocol_pong,
802};
803
804static int
805xdg_shell_unversioned_dispatch(const void *implementation,
806 void *_target, uint32_t opcode,
807 const struct wl_message *message,
808 union wl_argument *args)
809{
810 struct wl_resource *resource = _target;
811 struct weston_desktop_client *client =
812 wl_resource_get_user_data(resource);
813
814 if (opcode != 1 /* XDG_SHELL_USE_UNSTABLE_VERSION */) {
815 wl_resource_post_error(resource,
816 WL_DISPLAY_ERROR_INVALID_OBJECT,
817 "must call use_unstable_version first");
818 return 0;
819 }
820
821#define XDG_SERVER_VERSION 5
822
823 if (args[0].i != XDG_SERVER_VERSION) {
824 wl_resource_post_error(resource,
825 WL_DISPLAY_ERROR_INVALID_OBJECT,
826 "incompatible version, server is %d " "client wants %d",
827 XDG_SERVER_VERSION, args[0].i);
828 return 0;
829 }
830
831 wl_resource_set_implementation(resource,
832 &weston_desktop_xdg_shell_implementation,
833 client, implementation);
834
835 return 1;
836}
837
838static void
839weston_desktop_xdg_shell_bind(struct wl_client *client, void *data,
840 uint32_t version, uint32_t id)
841{
842 struct weston_desktop *desktop = data;
843
844 weston_desktop_client_create(desktop, client,
845 xdg_shell_unversioned_dispatch,
846 &xdg_shell_interface, NULL, version, id);
847}
848
849struct wl_global *
850weston_desktop_xdg_shell_v5_create(struct weston_desktop *desktop,
851 struct wl_display *display)
852{
853 return wl_global_create(display,
854 &xdg_shell_interface,
855 WD_XDG_SHELL_PROTOCOL_VERSION,
856 desktop, weston_desktop_xdg_shell_bind);
857}