blob: ee319b31b13fc5d6fc69ce86aee163bf673f37d4 [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
36#include "libweston-desktop.h"
37#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
196static bool
197weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200198 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200199{
Quentin Glidic955cec02016-08-12 10:41:35 +0200200 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200201
202 return surface->state == MAXIMIZED;
203}
204
205static bool
206weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200207 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200208{
Quentin Glidic955cec02016-08-12 10:41:35 +0200209 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200210
211 return surface->state == FULLSCREEN;
212}
213
214static const struct weston_desktop_surface_implementation weston_desktop_xwayland_surface_internal_implementation = {
215 .committed = weston_desktop_xwayland_surface_committed,
216 .set_size = weston_desktop_xwayland_surface_set_size,
217
218 .get_maximized = weston_desktop_xwayland_surface_get_maximized,
219 .get_fullscreen = weston_desktop_xwayland_surface_get_fullscreen,
220
221 .destroy = weston_desktop_xwayland_surface_destroy,
222};
223
224static void
225weston_destop_xwayland_resource_destroyed(struct wl_listener *listener,
226 void *data)
227{
Quentin Glidic955cec02016-08-12 10:41:35 +0200228 struct weston_desktop_xwayland_surface *surface =
Quentin Glidic248dd102016-08-12 10:41:34 +0200229 wl_container_of(listener, surface, resource_destroy_listener);
230
231 weston_desktop_surface_destroy(surface->surface);
232}
233
Quentin Glidic955cec02016-08-12 10:41:35 +0200234static struct weston_desktop_xwayland_surface *
235create_surface(struct weston_desktop_xwayland *xwayland,
236 struct weston_surface *wsurface,
237 const struct weston_xwayland_client_interface *client_interface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200238{
Quentin Glidic955cec02016-08-12 10:41:35 +0200239 struct weston_desktop_xwayland_surface *surface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200240
Quentin Glidic955cec02016-08-12 10:41:35 +0200241 surface = zalloc(sizeof(struct weston_desktop_xwayland_surface));
Quentin Glidic248dd102016-08-12 10:41:34 +0200242 if (surface == NULL)
243 return NULL;
244
245 surface->xwayland = xwayland;
246 surface->desktop = xwayland->desktop;
Quentin Glidic955cec02016-08-12 10:41:35 +0200247 surface->client_interface = client_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200248
249 surface->surface =
250 weston_desktop_surface_create(surface->desktop,
251 xwayland->client, wsurface,
252 &weston_desktop_xwayland_surface_internal_implementation,
253 surface);
254 if (surface->surface == NULL) {
255 free(surface);
256 return NULL;
257 }
258
259 surface->resource_destroy_listener.notify =
260 weston_destop_xwayland_resource_destroyed;
261 wl_resource_add_destroy_listener(wsurface->resource,
262 &surface->resource_destroy_listener);
263
Giulio Camuffof15320f2016-12-08 09:21:08 +0100264 weston_desktop_surface_set_pid(surface->surface, 0);
265
Quentin Glidic248dd102016-08-12 10:41:34 +0200266 return surface;
267}
268
269static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200270set_toplevel(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200271{
272 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
273 0, 0);
274}
275
276static void
Pekka Paalanen37111e12016-11-16 14:03:31 +0200277set_toplevel_with_position(struct weston_desktop_xwayland_surface *surface,
278 int32_t x, int32_t y)
279{
280 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
281 0, 0);
282 weston_desktop_api_set_xwayland_position(surface->desktop,
283 surface->surface, x, y);
284}
285
286static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200287set_parent(struct weston_desktop_xwayland_surface *surface,
288 struct weston_surface *wparent)
Quentin Glidic248dd102016-08-12 10:41:34 +0200289{
290 struct weston_desktop_surface *parent;
291
292 if (!weston_surface_is_desktop_surface(wparent))
293 return;
294
295 parent = weston_surface_get_desktop_surface(wparent);
Quentin Glidic955cec02016-08-12 10:41:35 +0200296 weston_desktop_api_set_parent(surface->desktop, surface->surface, parent);
Quentin Glidic248dd102016-08-12 10:41:34 +0200297}
298
299static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200300set_transient(struct weston_desktop_xwayland_surface *surface,
301 struct weston_surface *wparent, int x, int y)
302{
303 struct weston_desktop_surface *parent;
304
305 if (!weston_surface_is_desktop_surface(wparent))
306 return;
307
308 parent = weston_surface_get_desktop_surface(wparent);
309 weston_desktop_xwayland_surface_change_state(surface, TRANSIENT, parent,
310 x, y);
311}
312
313static void
314set_fullscreen(struct weston_desktop_xwayland_surface *surface,
315 struct weston_output *output)
Quentin Glidic248dd102016-08-12 10:41:34 +0200316{
317 weston_desktop_xwayland_surface_change_state(surface, FULLSCREEN, NULL,
318 0, 0);
319 weston_desktop_api_fullscreen_requested(surface->desktop,
320 surface->surface, true, output);
321}
322
323static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200324set_xwayland(struct weston_desktop_xwayland_surface *surface, int x, int y)
Quentin Glidic248dd102016-08-12 10:41:34 +0200325{
326 weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL,
327 x, y);
Ilia Bozhinov3e5303d2017-06-28 00:08:40 +0300328 weston_view_set_position(surface->view, x, y);
Quentin Glidic248dd102016-08-12 10:41:34 +0200329}
330
331static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200332move(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200333 struct weston_pointer *pointer)
334{
335 if (surface->state == TOPLEVEL ||
336 surface->state == MAXIMIZED ||
337 surface->state == FULLSCREEN)
338 weston_desktop_api_move(surface->desktop, surface->surface,
339 pointer->seat, pointer->grab_serial);
340 return 0;
341}
342
343static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200344resize(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200345 struct weston_pointer *pointer, uint32_t edges)
346{
347 if (surface->state == TOPLEVEL ||
348 surface->state == MAXIMIZED ||
349 surface->state == FULLSCREEN)
350 weston_desktop_api_resize(surface->desktop, surface->surface,
351 pointer->seat, pointer->grab_serial,
352 edges);
353 return 0;
354}
355
356static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200357set_title(struct weston_desktop_xwayland_surface *surface, const char *title)
Quentin Glidic248dd102016-08-12 10:41:34 +0200358{
359 weston_desktop_surface_set_title(surface->surface, title);
360}
361
362static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200363set_window_geometry(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200364 int32_t x, int32_t y, int32_t width, int32_t height)
365{
366 surface->has_next_geometry = true;
367 surface->next_geometry.x = x;
368 surface->next_geometry.y = y;
369 surface->next_geometry.width = width;
370 surface->next_geometry.height = height;
371}
372
373static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200374set_maximized(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200375{
376 weston_desktop_xwayland_surface_change_state(surface, MAXIMIZED, NULL,
377 0, 0);
378 weston_desktop_api_maximized_requested(surface->desktop,
379 surface->surface, true);
380}
381
382static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200383set_pid(struct weston_desktop_xwayland_surface *surface, pid_t pid)
Quentin Glidic248dd102016-08-12 10:41:34 +0200384{
Quentin Glidic955cec02016-08-12 10:41:35 +0200385 weston_desktop_surface_set_pid(surface->surface, pid);
Quentin Glidic248dd102016-08-12 10:41:34 +0200386}
387
Quentin Glidic955cec02016-08-12 10:41:35 +0200388static const struct weston_desktop_xwayland_interface weston_desktop_xwayland_interface = {
389 .create_surface = create_surface,
390 .set_toplevel = set_toplevel,
Pekka Paalanen37111e12016-11-16 14:03:31 +0200391 .set_toplevel_with_position = set_toplevel_with_position,
Quentin Glidic955cec02016-08-12 10:41:35 +0200392 .set_parent = set_parent,
393 .set_transient = set_transient,
394 .set_fullscreen = set_fullscreen,
395 .set_xwayland = set_xwayland,
396 .move = move,
397 .resize = resize,
398 .set_title = set_title,
399 .set_window_geometry = set_window_geometry,
400 .set_maximized = set_maximized,
401 .set_pid = set_pid,
402};
403
Quentin Glidic248dd102016-08-12 10:41:34 +0200404void
405weston_desktop_xwayland_init(struct weston_desktop *desktop)
406{
407 struct weston_compositor *compositor = weston_desktop_get_compositor(desktop);
408 struct weston_desktop_xwayland *xwayland;
409
410 xwayland = zalloc(sizeof(struct weston_desktop_xwayland));
411 if (xwayland == NULL)
412 return;
413
414 xwayland->desktop = desktop;
415 xwayland->client = weston_desktop_client_create(desktop, NULL, NULL, NULL, NULL, 0, 0);
416
Quentin Glidic82681572016-12-17 13:40:51 +0100417 weston_layer_init(&xwayland->layer, compositor);
418 /* We put this layer on top of regular shell surfaces, but hopefully
419 * below any UI the shell would add */
420 weston_layer_set_position(&xwayland->layer,
421 WESTON_LAYER_POSITION_NORMAL + 1);
Quentin Glidic248dd102016-08-12 10:41:34 +0200422
Quentin Glidic955cec02016-08-12 10:41:35 +0200423 compositor->xwayland = xwayland;
424 compositor->xwayland_interface = &weston_desktop_xwayland_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200425}