blob: 66553f45633f830984edc5acfae398c80af4865c [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 <assert.h>
30
31#include <wayland-server.h>
32
33#include "compositor.h"
34#include "zalloc.h"
35
36#include "libweston-desktop.h"
37#include "internal.h"
38
39#define WD_WL_SHELL_PROTOCOL_VERSION 1
40
41enum weston_desktop_wl_shell_surface_state {
42 NONE,
43 TOPLEVEL,
44 MAXIMIZED,
45 FULLSCREEN,
46 TRANSIENT,
47 POPUP,
48};
49
50struct weston_desktop_wl_shell_surface {
51 struct wl_resource *resource;
52 struct weston_desktop *desktop;
53 struct wl_display *display;
54 struct weston_desktop_surface *surface;
55 struct weston_desktop_surface *parent;
56 bool added;
57 struct weston_desktop_seat *popup_seat;
58 enum weston_desktop_wl_shell_surface_state state;
Giulio Camuffo2dae4d02016-09-28 21:32:11 +020059 struct wl_listener wl_surface_resource_destroy_listener;
Quentin Glidic248dd102016-08-12 10:41:34 +020060};
61
62static void
63weston_desktop_wl_shell_surface_set_size(struct weston_desktop_surface *dsurface,
64 void *user_data,
65 int32_t width, int32_t height)
66{
67 struct weston_desktop_wl_shell_surface *surface = user_data;
68 struct weston_surface *wsurface =
69 weston_desktop_surface_get_surface(surface->surface);
70
Quentin Glidica56b0532016-09-13 10:05:58 +020071 if ((wsurface->width == width && wsurface->height == height) ||
72 (width == 0 && height == 0))
Giulio Camuffo72f68c52016-09-28 21:29:14 +020073 return;
Quentin Glidic248dd102016-08-12 10:41:34 +020074
75 wl_shell_surface_send_configure(surface->resource,
76 WL_SHELL_SURFACE_RESIZE_NONE,
77 width, height);
78}
79
80static void
81weston_desktop_wl_shell_surface_maybe_ungrab(struct weston_desktop_wl_shell_surface *surface)
82{
83 if (surface->state != POPUP ||
84 !weston_desktop_surface_get_grab(surface->surface))
85 return;
86
87 weston_desktop_surface_popup_ungrab(surface->surface,
88 surface->popup_seat);
89 surface->popup_seat = NULL;
90}
91
92static void
93weston_desktop_wl_shell_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +020094 void *user_data,
Quentin Glidic248dd102016-08-12 10:41:34 +020095 int32_t sx, int32_t sy)
96{
97 struct weston_desktop_wl_shell_surface *surface = user_data;
98 struct weston_surface *wsurface =
99 weston_desktop_surface_get_surface(dsurface);
100
101 if (wsurface->buffer_ref.buffer == NULL)
102 weston_desktop_wl_shell_surface_maybe_ungrab(surface);
103
104 if (surface->added)
105 weston_desktop_api_committed(surface->desktop, surface->surface,
106 sx, sy);
107}
108
109static void
110weston_desktop_wl_shell_surface_ping(struct weston_desktop_surface *dsurface,
111 uint32_t serial, void *user_data)
112{
113 struct weston_desktop_wl_shell_surface *surface = user_data;
114
115 wl_shell_surface_send_ping(surface->resource, serial);
116}
117
118static void
119weston_desktop_wl_shell_surface_close(struct weston_desktop_surface *dsurface,
120 void *user_data)
121{
122 struct weston_desktop_wl_shell_surface *surface = user_data;
123
124 if (surface->state == POPUP)
125 wl_shell_surface_send_popup_done(surface->resource);
126}
127
128static bool
129weston_desktop_wl_shell_surface_get_maximized(struct weston_desktop_surface *dsurface,
130 void *user_data)
131{
132 struct weston_desktop_wl_shell_surface *surface = user_data;
133
134 return surface->state == MAXIMIZED;
135}
136
137static bool
138weston_desktop_wl_shell_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
139 void *user_data)
140{
141 struct weston_desktop_wl_shell_surface *surface = user_data;
142
143 return surface->state == FULLSCREEN;
144}
145
146static void
147weston_desktop_wl_shell_change_state(struct weston_desktop_wl_shell_surface *surface,
148 enum weston_desktop_wl_shell_surface_state state,
149 struct weston_desktop_surface *parent,
150 int32_t x, int32_t y)
151{
152 bool to_add = (parent == NULL);
153
154 assert(state != NONE);
155
Quentin Glidic6384edf2016-08-16 11:42:47 +0200156 if (to_add && surface->added) {
157 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +0200158 return;
Quentin Glidic6384edf2016-08-16 11:42:47 +0200159 }
Quentin Glidic248dd102016-08-12 10:41:34 +0200160
161 if (surface->state != state) {
162 if (surface->state == POPUP)
163 weston_desktop_wl_shell_surface_maybe_ungrab(surface);
164
165 if (to_add) {
166 weston_desktop_surface_unset_relative_to(surface->surface);
167 weston_desktop_api_surface_added(surface->desktop,
168 surface->surface);
169 } else if (surface->added) {
170 weston_desktop_api_surface_removed(surface->desktop,
171 surface->surface);
172 }
173
174 surface->state = state;
175 surface->added = to_add;
176 }
177
178 if (parent != NULL)
179 weston_desktop_surface_set_relative_to(surface->surface, parent,
180 x, y, false);
181}
182
183static void
184weston_desktop_wl_shell_surface_destroy(struct weston_desktop_surface *dsurface,
185 void *user_data)
186{
187 struct weston_desktop_wl_shell_surface *surface = user_data;
188
Giulio Camuffo2dae4d02016-09-28 21:32:11 +0200189 wl_list_remove(&surface->wl_surface_resource_destroy_listener.link);
190
Quentin Glidic248dd102016-08-12 10:41:34 +0200191 weston_desktop_wl_shell_surface_maybe_ungrab(surface);
192 weston_desktop_surface_unset_relative_to(surface->surface);
193 if (surface->added)
194 weston_desktop_api_surface_removed(surface->desktop,
195 surface->surface);
196
197 free(surface);
198}
199
200static void
201weston_desktop_wl_shell_surface_protocol_pong(struct wl_client *wl_client,
202 struct wl_resource *resource,
203 uint32_t serial)
204{
205 struct weston_desktop_surface *surface = wl_resource_get_user_data(resource);
206
207 weston_desktop_client_pong(weston_desktop_surface_get_client(surface), serial);
208}
209
210static void
211weston_desktop_wl_shell_surface_protocol_move(struct wl_client *wl_client,
212 struct wl_resource *resource,
213 struct wl_resource *seat_resource,
214 uint32_t serial)
215{
216 struct weston_desktop_surface *dsurface =
217 wl_resource_get_user_data(resource);
218 struct weston_seat *seat =
219 wl_resource_get_user_data(seat_resource);
220 struct weston_desktop_wl_shell_surface *surface =
221 weston_desktop_surface_get_implementation_data(dsurface);
222
223 weston_desktop_api_move(surface->desktop, dsurface, seat, serial);
224}
225
226static void
227weston_desktop_wl_shell_surface_protocol_resize(struct wl_client *wl_client,
228 struct wl_resource *resource,
229 struct wl_resource *seat_resource,
230 uint32_t serial,
231 enum wl_shell_surface_resize edges)
232{
233 struct weston_desktop_surface *dsurface =
234 wl_resource_get_user_data(resource);
235 struct weston_seat *seat = wl_resource_get_user_data(seat_resource);
236 struct weston_desktop_wl_shell_surface *surface =
237 weston_desktop_surface_get_implementation_data(dsurface);
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200238 enum weston_desktop_surface_edge surf_edges =
239 (enum weston_desktop_surface_edge) edges;
Quentin Glidic248dd102016-08-12 10:41:34 +0200240
Armin Krezović4e2fa0a2016-09-10 19:11:21 +0200241 weston_desktop_api_resize(surface->desktop, dsurface, seat, serial, surf_edges);
Quentin Glidic248dd102016-08-12 10:41:34 +0200242}
243
244static void
245weston_desktop_wl_shell_surface_protocol_set_toplevel(struct wl_client *wl_client,
246 struct wl_resource *resource)
247{
248 struct weston_desktop_surface *dsurface =
249 wl_resource_get_user_data(resource);
250 struct weston_desktop_wl_shell_surface *surface =
251 weston_desktop_surface_get_implementation_data(dsurface);
252
253 weston_desktop_wl_shell_change_state(surface, TOPLEVEL, NULL, 0, 0);
254 if (surface->parent == NULL)
255 return;
256 surface->parent = NULL;
257 weston_desktop_api_set_parent(surface->desktop, surface->surface, NULL);
258}
259
260static void
261weston_desktop_wl_shell_surface_protocol_set_transient(struct wl_client *wl_client,
262 struct wl_resource *resource,
263 struct wl_resource *parent_resource,
264 int32_t x, int32_t y,
265 enum wl_shell_surface_transient flags)
266{
267 struct weston_desktop_surface *dsurface =
268 wl_resource_get_user_data(resource);
269 struct weston_surface *wparent =
270 wl_resource_get_user_data(parent_resource);
271 struct weston_desktop_surface *parent;
272 struct weston_desktop_wl_shell_surface *surface =
273 weston_desktop_surface_get_implementation_data(dsurface);
274
275 if (!weston_surface_is_desktop_surface(wparent))
276 return;
277
278 parent = weston_surface_get_desktop_surface(wparent);
279 if (flags & WL_SHELL_SURFACE_TRANSIENT_INACTIVE) {
280 weston_desktop_wl_shell_change_state(surface, TRANSIENT, parent,
281 x, y);
282 } else {
283 weston_desktop_wl_shell_change_state(surface, TOPLEVEL, NULL,
284 0, 0);
285 surface->parent = parent;
286 weston_desktop_api_set_parent(surface->desktop,
287 surface->surface, parent);
288 }
289}
290
291static void
292weston_desktop_wl_shell_surface_protocol_set_fullscreen(struct wl_client *wl_client,
293 struct wl_resource *resource,
294 enum wl_shell_surface_fullscreen_method method,
295 uint32_t framerate,
296 struct wl_resource *output_resource)
297{
298 struct weston_desktop_surface *dsurface =
299 wl_resource_get_user_data(resource);
300 struct weston_desktop_wl_shell_surface *surface =
301 weston_desktop_surface_get_implementation_data(dsurface);
302 struct weston_output *output = NULL;
303
304 if (output_resource != NULL)
Pekka Paalanen9ffb2502017-03-27 15:14:32 +0300305 output = weston_output_from_resource(output_resource);
Quentin Glidic248dd102016-08-12 10:41:34 +0200306
307 weston_desktop_wl_shell_change_state(surface, FULLSCREEN, NULL, 0, 0);
308 weston_desktop_api_fullscreen_requested(surface->desktop, dsurface,
309 true, output);
310}
311
312static void
313weston_desktop_wl_shell_surface_protocol_set_popup(struct wl_client *wl_client,
314 struct wl_resource *resource,
315 struct wl_resource *seat_resource,
316 uint32_t serial,
317 struct wl_resource *parent_resource,
318 int32_t x, int32_t y,
319 enum wl_shell_surface_transient flags)
320{
321 struct weston_desktop_surface *dsurface =
322 wl_resource_get_user_data(resource);
323 struct weston_seat *wseat = wl_resource_get_user_data(seat_resource);
324 struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat);
325 struct weston_surface *parent =
326 wl_resource_get_user_data(parent_resource);
327 struct weston_desktop_surface *parent_surface;
328 struct weston_desktop_wl_shell_surface *surface =
329 weston_desktop_surface_get_implementation_data(dsurface);
330
331 if (seat == NULL) {
332 wl_client_post_no_memory(wl_client);
333 return;
334 }
335
336 if (!weston_surface_is_desktop_surface(parent))
337 return;
338
339 parent_surface = weston_surface_get_desktop_surface(parent);
340
341 weston_desktop_wl_shell_change_state(surface, POPUP,
342 parent_surface, x, y);
343 weston_desktop_surface_popup_grab(surface->surface, seat, serial);
344 surface->popup_seat = seat;
345}
346
347static void
348weston_desktop_wl_shell_surface_protocol_set_maximized(struct wl_client *wl_client,
349 struct wl_resource *resource,
350 struct wl_resource *output_resource)
351{
352 struct weston_desktop_surface *dsurface =
353 wl_resource_get_user_data(resource);
354 struct weston_desktop_wl_shell_surface *surface =
355 weston_desktop_surface_get_implementation_data(dsurface);
356
357 weston_desktop_wl_shell_change_state(surface, MAXIMIZED, NULL, 0, 0);
358 weston_desktop_api_maximized_requested(surface->desktop, dsurface, true);
359}
360
361static void
362weston_desktop_wl_shell_surface_protocol_set_title(struct wl_client *wl_client,
363 struct wl_resource *resource,
364 const char *title)
365{
366 struct weston_desktop_surface *surface =
367 wl_resource_get_user_data(resource);
368
369 weston_desktop_surface_set_title(surface, title);
370}
371
372static void
373weston_desktop_wl_shell_surface_protocol_set_class(struct wl_client *wl_client,
374 struct wl_resource *resource,
375 const char *class_)
376{
377 struct weston_desktop_surface *surface =
378 wl_resource_get_user_data(resource);
379
380 weston_desktop_surface_set_app_id(surface, class_);
381}
382
383
384static const struct wl_shell_surface_interface weston_desktop_wl_shell_surface_implementation = {
385 .pong = weston_desktop_wl_shell_surface_protocol_pong,
386 .move = weston_desktop_wl_shell_surface_protocol_move,
387 .resize = weston_desktop_wl_shell_surface_protocol_resize,
388 .set_toplevel = weston_desktop_wl_shell_surface_protocol_set_toplevel,
389 .set_transient = weston_desktop_wl_shell_surface_protocol_set_transient,
390 .set_fullscreen = weston_desktop_wl_shell_surface_protocol_set_fullscreen,
391 .set_popup = weston_desktop_wl_shell_surface_protocol_set_popup,
392 .set_maximized = weston_desktop_wl_shell_surface_protocol_set_maximized,
393 .set_title = weston_desktop_wl_shell_surface_protocol_set_title,
394 .set_class = weston_desktop_wl_shell_surface_protocol_set_class,
395};
396
397static const struct weston_desktop_surface_implementation weston_desktop_wl_shell_surface_internal_implementation = {
398 .set_size = weston_desktop_wl_shell_surface_set_size,
399 .committed = weston_desktop_wl_shell_surface_committed,
400 .ping = weston_desktop_wl_shell_surface_ping,
401 .close = weston_desktop_wl_shell_surface_close,
402
403 .get_maximized = weston_desktop_wl_shell_surface_get_maximized,
404 .get_fullscreen = weston_desktop_wl_shell_surface_get_fullscreen,
405
406 .destroy = weston_desktop_wl_shell_surface_destroy,
407};
408
409static void
Giulio Camuffo2dae4d02016-09-28 21:32:11 +0200410wl_surface_resource_destroyed(struct wl_listener *listener,
411 void *data)
412{
413 struct weston_desktop_wl_shell_surface *surface =
414 wl_container_of(listener, surface,
415 wl_surface_resource_destroy_listener);
416
417 /* the wl_shell_surface spec says that wl_shell_surfaces are to be
418 * destroyed automatically when the wl_surface is destroyed. */
419 weston_desktop_surface_destroy(surface->surface);
420}
421
422static void
Quentin Glidic248dd102016-08-12 10:41:34 +0200423weston_desktop_wl_shell_protocol_get_shell_surface(struct wl_client *wl_client,
424 struct wl_resource *resource,
425 uint32_t id,
426 struct wl_resource *surface_resource)
427{
428 struct weston_desktop_client *client = wl_resource_get_user_data(resource);
429 struct weston_surface *wsurface = wl_resource_get_user_data(surface_resource);
430 struct weston_desktop_wl_shell_surface *surface;
431
432
433 if (weston_surface_set_role(wsurface, "wl_shell_surface", resource, WL_SHELL_ERROR_ROLE) < 0)
434 return;
435
436 surface = zalloc(sizeof(struct weston_desktop_wl_shell_surface));
437 if (surface == NULL) {
438 wl_client_post_no_memory(wl_client);
439 return;
440 }
441
442 surface->desktop = weston_desktop_client_get_desktop(client);
443 surface->display = weston_desktop_get_display(surface->desktop);
444
445 surface->surface =
446 weston_desktop_surface_create(surface->desktop, client, wsurface,
447 &weston_desktop_wl_shell_surface_internal_implementation,
448 surface);
449 if (surface->surface == NULL) {
450 free(surface);
451 return;
452 }
453
Giulio Camuffo2dae4d02016-09-28 21:32:11 +0200454 surface->wl_surface_resource_destroy_listener.notify =
455 wl_surface_resource_destroyed;
456 wl_resource_add_destroy_listener(wsurface->resource,
457 &surface->wl_surface_resource_destroy_listener);
458
Quentin Glidic248dd102016-08-12 10:41:34 +0200459 surface->resource =
460 weston_desktop_surface_add_resource(surface->surface,
461 &wl_shell_surface_interface,
462 &weston_desktop_wl_shell_surface_implementation,
463 id, NULL);
464}
465
466
467static const struct wl_shell_interface weston_desktop_wl_shell_implementation = {
468 .get_shell_surface = weston_desktop_wl_shell_protocol_get_shell_surface,
469};
470
471static void
472weston_desktop_wl_shell_bind(struct wl_client *client, void *data,
473 uint32_t version, uint32_t id)
474{
475 struct weston_desktop *desktop = data;
476
477 weston_desktop_client_create(desktop, client, NULL, &wl_shell_interface,
478 &weston_desktop_wl_shell_implementation,
479 version, id);
480}
481
482struct wl_global *
483weston_desktop_wl_shell_create(struct weston_desktop *desktop,
484 struct wl_display *display)
485{
486 return wl_global_create(display,
487 &wl_shell_interface,
488 WD_WL_SHELL_PROTOCOL_VERSION, desktop,
489 weston_desktop_wl_shell_bind);
490}