blob: cdb65dfca5bd423a91b87bf6d55be3fcb1c56ece [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
Pekka Paalanen3d5d9472019-03-28 16:28:47 +020033#include <libweston/libweston.h>
Pekka Paalanenecbdcfd2019-04-04 14:46:00 +030034#include <libweston/zalloc.h>
Quentin Glidic248dd102016-08-12 10:41:34 +020035
Pekka Paalanen8ebd9812019-04-04 16:02:14 +030036#include <libweston-desktop/libweston-desktop.h>
Quentin Glidic248dd102016-08-12 10:41:34 +020037#include "internal.h"
Quentin Glidic955cec02016-08-12 10:41:35 +020038#include "xwayland/xwayland-internal-interface.h"
Quentin Glidic248dd102016-08-12 10:41:34 +020039
40enum weston_desktop_xwayland_surface_state {
41 NONE,
42 TOPLEVEL,
43 MAXIMIZED,
44 FULLSCREEN,
45 TRANSIENT,
46 XWAYLAND,
47};
48
49struct weston_desktop_xwayland {
50 struct weston_desktop *desktop;
51 struct weston_desktop_client *client;
52 struct weston_layer layer;
53};
54
Quentin Glidic955cec02016-08-12 10:41:35 +020055struct weston_desktop_xwayland_surface {
Quentin Glidic248dd102016-08-12 10:41:34 +020056 struct weston_desktop_xwayland *xwayland;
57 struct weston_desktop *desktop;
58 struct weston_desktop_surface *surface;
59 struct wl_listener resource_destroy_listener;
60 struct weston_view *view;
Quentin Glidic955cec02016-08-12 10:41:35 +020061 const struct weston_xwayland_client_interface *client_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +020062 struct weston_geometry next_geometry;
63 bool has_next_geometry;
Quentin Glidic8e8fa8e2017-12-08 11:10:53 +010064 bool committed;
Quentin Glidic248dd102016-08-12 10:41:34 +020065 bool added;
66 enum weston_desktop_xwayland_surface_state state;
67};
68
69static void
Quentin Glidic955cec02016-08-12 10:41:35 +020070weston_desktop_xwayland_surface_change_state(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +020071 enum weston_desktop_xwayland_surface_state state,
72 struct weston_desktop_surface *parent,
73 int32_t x, int32_t y)
74{
Pekka Paalanene3a582f2016-11-15 14:24:21 +020075 struct weston_surface *wsurface;
Quentin Glidic248dd102016-08-12 10:41:34 +020076 bool to_add = (parent == NULL && state != XWAYLAND);
77
78 assert(state != NONE);
Pekka Paalanena838b782016-11-15 11:43:36 +020079 assert(!parent || state == TRANSIENT);
Quentin Glidic248dd102016-08-12 10:41:34 +020080
Quentin Glidic6384edf2016-08-16 11:42:47 +020081 if (to_add && surface->added) {
82 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +020083 return;
Quentin Glidic6384edf2016-08-16 11:42:47 +020084 }
Quentin Glidic248dd102016-08-12 10:41:34 +020085
Pekka Paalanene3a582f2016-11-15 14:24:21 +020086 wsurface = weston_desktop_surface_get_surface(surface->surface);
87
Quentin Glidic248dd102016-08-12 10:41:34 +020088 if (surface->state != state) {
89 if (surface->state == XWAYLAND) {
Pekka Paalanenbbc749a2016-11-15 12:22:09 +020090 assert(!surface->added);
91
Quentin Glidicf01ecee2016-08-16 10:52:46 +020092 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidicf6636a82016-08-16 10:55:02 +020093 weston_view_destroy(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +020094 surface->view = NULL;
Pekka Paalanene3a582f2016-11-15 14:24:21 +020095 weston_surface_unmap(wsurface);
Quentin Glidic248dd102016-08-12 10:41:34 +020096 }
97
98 if (to_add) {
99 weston_desktop_surface_unset_relative_to(surface->surface);
100 weston_desktop_api_surface_added(surface->desktop,
101 surface->surface);
Pekka Paalanen33268202016-11-15 11:48:42 +0200102 surface->added = true;
Quentin Glidic8e8fa8e2017-12-08 11:10:53 +0100103 if (surface->state == NONE && surface->committed)
104 /* We had a race, and wl_surface.commit() was
105 * faster, just fake a commit to map the
106 * surface */
107 weston_desktop_api_committed(surface->desktop,
108 surface->surface,
109 0, 0);
110
Quentin Glidic248dd102016-08-12 10:41:34 +0200111 } else if (surface->added) {
112 weston_desktop_api_surface_removed(surface->desktop,
113 surface->surface);
Pekka Paalanen33268202016-11-15 11:48:42 +0200114 surface->added = false;
Quentin Glidic248dd102016-08-12 10:41:34 +0200115 }
116
117 if (state == XWAYLAND) {
Pekka Paalanenbbc749a2016-11-15 12:22:09 +0200118 assert(!surface->added);
119
Quentin Glidic248dd102016-08-12 10:41:34 +0200120 surface->view =
121 weston_desktop_surface_create_view(surface->surface);
122 weston_layer_entry_insert(&surface->xwayland->layer.view_list,
123 &surface->view->layer_link);
Pekka Paalanene3a582f2016-11-15 14:24:21 +0200124 surface->view->is_mapped = true;
125 wsurface->is_mapped = true;
Quentin Glidic248dd102016-08-12 10:41:34 +0200126 }
127
128 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +0200129 }
130
131 if (parent != NULL)
132 weston_desktop_surface_set_relative_to(surface->surface, parent,
133 x, y, false);
134}
135
136static void
137weston_desktop_xwayland_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +0200138 void *user_data,
Quentin Glidic248dd102016-08-12 10:41:34 +0200139 int32_t sx, int32_t sy)
140{
Quentin Glidic955cec02016-08-12 10:41:35 +0200141 struct weston_desktop_xwayland_surface *surface = user_data;
Pekka Paalanen5f93b9f2017-01-18 15:37:59 +0200142 struct weston_geometry oldgeom;
143
144 assert(dsurface == surface->surface);
Quentin Glidic8e8fa8e2017-12-08 11:10:53 +0100145 surface->committed = true;
Quentin Glidic248dd102016-08-12 10:41:34 +0200146
Pekka Paalanen0adb6a72016-11-28 16:37:07 +0200147#ifdef WM_DEBUG
148 weston_log("%s: xwayland surface %p\n", __func__, surface);
149#endif
150
Quentin Glidic248dd102016-08-12 10:41:34 +0200151 if (surface->has_next_geometry) {
Pekka Paalanen5f93b9f2017-01-18 15:37:59 +0200152 oldgeom = weston_desktop_surface_get_geometry(surface->surface);
153 sx -= surface->next_geometry.x - oldgeom.x;
154 sy -= surface->next_geometry.y - oldgeom.x;
155
Quentin Glidic248dd102016-08-12 10:41:34 +0200156 surface->has_next_geometry = false;
157 weston_desktop_surface_set_geometry(surface->surface,
158 surface->next_geometry);
159 }
160
161 if (surface->added)
162 weston_desktop_api_committed(surface->desktop, surface->surface,
163 sx, sy);
164}
165
166static void
167weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200168 void *user_data,
169 int32_t width, int32_t height)
Quentin Glidic248dd102016-08-12 10:41:34 +0200170{
Quentin Glidic955cec02016-08-12 10:41:35 +0200171 struct weston_desktop_xwayland_surface *surface = user_data;
172 struct weston_surface *wsurface =
173 weston_desktop_surface_get_surface(surface->surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200174
Quentin Glidic955cec02016-08-12 10:41:35 +0200175 surface->client_interface->send_configure(wsurface, width, height);
Quentin Glidic248dd102016-08-12 10:41:34 +0200176}
177
178static void
179weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface *dsurface,
180 void *user_data)
181{
Quentin Glidic955cec02016-08-12 10:41:35 +0200182 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200183
184 wl_list_remove(&surface->resource_destroy_listener.link);
185
186 weston_desktop_surface_unset_relative_to(surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200187 if (surface->added)
Quentin Glidic248dd102016-08-12 10:41:34 +0200188 weston_desktop_api_surface_removed(surface->desktop,
189 surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200190 else if (surface->state == XWAYLAND)
Quentin Glidicf01ecee2016-08-16 10:52:46 +0200191 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +0200192
193 free(surface);
194}
195
John Goodf33ddd02020-03-22 18:19:38 +0100196static void
197weston_desktop_xwayland_surface_close(struct weston_desktop_surface *dsurface,
198 void *user_data)
199{
200 struct weston_desktop_xwayland_surface *surface = user_data;
201 struct weston_surface *wsurface =
202 weston_desktop_surface_get_surface(surface->surface);
203
204 surface->client_interface->send_close(wsurface);
205}
206
Quentin Glidic248dd102016-08-12 10:41:34 +0200207static bool
208weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200209 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200210{
Quentin Glidic955cec02016-08-12 10:41:35 +0200211 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200212
213 return surface->state == MAXIMIZED;
214}
215
216static bool
217weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200218 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200219{
Quentin Glidic955cec02016-08-12 10:41:35 +0200220 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200221
222 return surface->state == FULLSCREEN;
223}
224
225static const struct weston_desktop_surface_implementation weston_desktop_xwayland_surface_internal_implementation = {
226 .committed = weston_desktop_xwayland_surface_committed,
227 .set_size = weston_desktop_xwayland_surface_set_size,
228
229 .get_maximized = weston_desktop_xwayland_surface_get_maximized,
230 .get_fullscreen = weston_desktop_xwayland_surface_get_fullscreen,
231
232 .destroy = weston_desktop_xwayland_surface_destroy,
John Goodf33ddd02020-03-22 18:19:38 +0100233 .close = weston_desktop_xwayland_surface_close,
Quentin Glidic248dd102016-08-12 10:41:34 +0200234};
235
236static void
237weston_destop_xwayland_resource_destroyed(struct wl_listener *listener,
238 void *data)
239{
Quentin Glidic955cec02016-08-12 10:41:35 +0200240 struct weston_desktop_xwayland_surface *surface =
Quentin Glidic248dd102016-08-12 10:41:34 +0200241 wl_container_of(listener, surface, resource_destroy_listener);
242
243 weston_desktop_surface_destroy(surface->surface);
244}
245
Quentin Glidic955cec02016-08-12 10:41:35 +0200246static struct weston_desktop_xwayland_surface *
247create_surface(struct weston_desktop_xwayland *xwayland,
248 struct weston_surface *wsurface,
249 const struct weston_xwayland_client_interface *client_interface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200250{
Quentin Glidic955cec02016-08-12 10:41:35 +0200251 struct weston_desktop_xwayland_surface *surface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200252
Quentin Glidic955cec02016-08-12 10:41:35 +0200253 surface = zalloc(sizeof(struct weston_desktop_xwayland_surface));
Quentin Glidic248dd102016-08-12 10:41:34 +0200254 if (surface == NULL)
255 return NULL;
256
257 surface->xwayland = xwayland;
258 surface->desktop = xwayland->desktop;
Quentin Glidic955cec02016-08-12 10:41:35 +0200259 surface->client_interface = client_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200260
261 surface->surface =
262 weston_desktop_surface_create(surface->desktop,
263 xwayland->client, wsurface,
264 &weston_desktop_xwayland_surface_internal_implementation,
265 surface);
266 if (surface->surface == NULL) {
267 free(surface);
268 return NULL;
269 }
270
271 surface->resource_destroy_listener.notify =
272 weston_destop_xwayland_resource_destroyed;
273 wl_resource_add_destroy_listener(wsurface->resource,
274 &surface->resource_destroy_listener);
275
Giulio Camuffof15320f2016-12-08 09:21:08 +0100276 weston_desktop_surface_set_pid(surface->surface, 0);
277
Quentin Glidic248dd102016-08-12 10:41:34 +0200278 return surface;
279}
280
281static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200282set_toplevel(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200283{
284 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
285 0, 0);
286}
287
288static void
Pekka Paalanen37111e12016-11-16 14:03:31 +0200289set_toplevel_with_position(struct weston_desktop_xwayland_surface *surface,
290 int32_t x, int32_t y)
291{
292 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
293 0, 0);
294 weston_desktop_api_set_xwayland_position(surface->desktop,
295 surface->surface, x, y);
296}
297
298static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200299set_parent(struct weston_desktop_xwayland_surface *surface,
300 struct weston_surface *wparent)
Quentin Glidic248dd102016-08-12 10:41:34 +0200301{
302 struct weston_desktop_surface *parent;
303
304 if (!weston_surface_is_desktop_surface(wparent))
305 return;
306
307 parent = weston_surface_get_desktop_surface(wparent);
Quentin Glidic955cec02016-08-12 10:41:35 +0200308 weston_desktop_api_set_parent(surface->desktop, surface->surface, parent);
Quentin Glidic248dd102016-08-12 10:41:34 +0200309}
310
311static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200312set_transient(struct weston_desktop_xwayland_surface *surface,
313 struct weston_surface *wparent, int x, int y)
314{
315 struct weston_desktop_surface *parent;
316
317 if (!weston_surface_is_desktop_surface(wparent))
318 return;
319
320 parent = weston_surface_get_desktop_surface(wparent);
321 weston_desktop_xwayland_surface_change_state(surface, TRANSIENT, parent,
322 x, y);
323}
324
325static void
326set_fullscreen(struct weston_desktop_xwayland_surface *surface,
327 struct weston_output *output)
Quentin Glidic248dd102016-08-12 10:41:34 +0200328{
329 weston_desktop_xwayland_surface_change_state(surface, FULLSCREEN, NULL,
330 0, 0);
331 weston_desktop_api_fullscreen_requested(surface->desktop,
332 surface->surface, true, output);
333}
334
335static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200336set_xwayland(struct weston_desktop_xwayland_surface *surface, int x, int y)
Quentin Glidic248dd102016-08-12 10:41:34 +0200337{
338 weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL,
339 x, y);
Ilia Bozhinov3e5303d2017-06-28 00:08:40 +0300340 weston_view_set_position(surface->view, x, y);
Quentin Glidic248dd102016-08-12 10:41:34 +0200341}
342
343static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200344move(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200345 struct weston_pointer *pointer)
346{
347 if (surface->state == TOPLEVEL ||
348 surface->state == MAXIMIZED ||
349 surface->state == FULLSCREEN)
350 weston_desktop_api_move(surface->desktop, surface->surface,
351 pointer->seat, pointer->grab_serial);
352 return 0;
353}
354
355static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200356resize(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200357 struct weston_pointer *pointer, uint32_t edges)
358{
359 if (surface->state == TOPLEVEL ||
360 surface->state == MAXIMIZED ||
361 surface->state == FULLSCREEN)
362 weston_desktop_api_resize(surface->desktop, surface->surface,
363 pointer->seat, pointer->grab_serial,
364 edges);
365 return 0;
366}
367
368static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200369set_title(struct weston_desktop_xwayland_surface *surface, const char *title)
Quentin Glidic248dd102016-08-12 10:41:34 +0200370{
371 weston_desktop_surface_set_title(surface->surface, title);
372}
373
374static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200375set_window_geometry(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200376 int32_t x, int32_t y, int32_t width, int32_t height)
377{
378 surface->has_next_geometry = true;
379 surface->next_geometry.x = x;
380 surface->next_geometry.y = y;
381 surface->next_geometry.width = width;
382 surface->next_geometry.height = height;
383}
384
385static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200386set_maximized(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200387{
388 weston_desktop_xwayland_surface_change_state(surface, MAXIMIZED, NULL,
389 0, 0);
390 weston_desktop_api_maximized_requested(surface->desktop,
391 surface->surface, true);
392}
393
394static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200395set_pid(struct weston_desktop_xwayland_surface *surface, pid_t pid)
Quentin Glidic248dd102016-08-12 10:41:34 +0200396{
Quentin Glidic955cec02016-08-12 10:41:35 +0200397 weston_desktop_surface_set_pid(surface->surface, pid);
Quentin Glidic248dd102016-08-12 10:41:34 +0200398}
399
Quentin Glidic955cec02016-08-12 10:41:35 +0200400static const struct weston_desktop_xwayland_interface weston_desktop_xwayland_interface = {
401 .create_surface = create_surface,
402 .set_toplevel = set_toplevel,
Pekka Paalanen37111e12016-11-16 14:03:31 +0200403 .set_toplevel_with_position = set_toplevel_with_position,
Quentin Glidic955cec02016-08-12 10:41:35 +0200404 .set_parent = set_parent,
405 .set_transient = set_transient,
406 .set_fullscreen = set_fullscreen,
407 .set_xwayland = set_xwayland,
408 .move = move,
409 .resize = resize,
410 .set_title = set_title,
411 .set_window_geometry = set_window_geometry,
412 .set_maximized = set_maximized,
413 .set_pid = set_pid,
414};
415
Quentin Glidic248dd102016-08-12 10:41:34 +0200416void
417weston_desktop_xwayland_init(struct weston_desktop *desktop)
418{
419 struct weston_compositor *compositor = weston_desktop_get_compositor(desktop);
420 struct weston_desktop_xwayland *xwayland;
421
422 xwayland = zalloc(sizeof(struct weston_desktop_xwayland));
423 if (xwayland == NULL)
424 return;
425
426 xwayland->desktop = desktop;
427 xwayland->client = weston_desktop_client_create(desktop, NULL, NULL, NULL, NULL, 0, 0);
428
Quentin Glidic82681572016-12-17 13:40:51 +0100429 weston_layer_init(&xwayland->layer, compositor);
430 /* We put this layer on top of regular shell surfaces, but hopefully
431 * below any UI the shell would add */
432 weston_layer_set_position(&xwayland->layer,
433 WESTON_LAYER_POSITION_NORMAL + 1);
Quentin Glidic248dd102016-08-12 10:41:34 +0200434
Quentin Glidic955cec02016-08-12 10:41:35 +0200435 compositor->xwayland = xwayland;
436 compositor->xwayland_interface = &weston_desktop_xwayland_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200437}
Pekka Paalanene2583ca2021-05-14 16:12:35 +0300438
439void
440weston_desktop_xwayland_fini(struct weston_desktop *desktop)
441{
442 struct weston_compositor *compositor = weston_desktop_get_compositor(desktop);
443 struct weston_desktop_xwayland *xwayland;
444
445 xwayland = compositor->xwayland;
446
447 weston_desktop_client_destroy(xwayland->client);
448 weston_layer_fini(&xwayland->layer);
449 free(xwayland);
450
451 compositor->xwayland = NULL;
452 compositor->xwayland_interface = NULL;
453}