blob: 002e2523cc9ccc3ecb519dedba005d9e85932c4c [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"
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;
64 bool added;
65 enum weston_desktop_xwayland_surface_state state;
66};
67
68static void
Quentin Glidic955cec02016-08-12 10:41:35 +020069weston_desktop_xwayland_surface_change_state(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +020070 enum weston_desktop_xwayland_surface_state state,
71 struct weston_desktop_surface *parent,
72 int32_t x, int32_t y)
73{
Pekka Paalanene3a582f2016-11-15 14:24:21 +020074 struct weston_surface *wsurface;
Quentin Glidic248dd102016-08-12 10:41:34 +020075 bool to_add = (parent == NULL && state != XWAYLAND);
76
77 assert(state != NONE);
Pekka Paalanena838b782016-11-15 11:43:36 +020078 assert(!parent || state == TRANSIENT);
Quentin Glidic248dd102016-08-12 10:41:34 +020079
Quentin Glidic6384edf2016-08-16 11:42:47 +020080 if (to_add && surface->added) {
81 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +020082 return;
Quentin Glidic6384edf2016-08-16 11:42:47 +020083 }
Quentin Glidic248dd102016-08-12 10:41:34 +020084
Pekka Paalanene3a582f2016-11-15 14:24:21 +020085 wsurface = weston_desktop_surface_get_surface(surface->surface);
86
Quentin Glidic248dd102016-08-12 10:41:34 +020087 if (surface->state != state) {
88 if (surface->state == XWAYLAND) {
Pekka Paalanenbbc749a2016-11-15 12:22:09 +020089 assert(!surface->added);
90
Quentin Glidicf01ecee2016-08-16 10:52:46 +020091 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidicf6636a82016-08-16 10:55:02 +020092 weston_view_destroy(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +020093 surface->view = NULL;
Pekka Paalanene3a582f2016-11-15 14:24:21 +020094 weston_surface_unmap(wsurface);
Quentin Glidic248dd102016-08-12 10:41:34 +020095 }
96
97 if (to_add) {
98 weston_desktop_surface_unset_relative_to(surface->surface);
99 weston_desktop_api_surface_added(surface->desktop,
100 surface->surface);
Pekka Paalanen33268202016-11-15 11:48:42 +0200101 surface->added = true;
Quentin Glidic248dd102016-08-12 10:41:34 +0200102 } else if (surface->added) {
103 weston_desktop_api_surface_removed(surface->desktop,
104 surface->surface);
Pekka Paalanen33268202016-11-15 11:48:42 +0200105 surface->added = false;
Quentin Glidic248dd102016-08-12 10:41:34 +0200106 }
107
108 if (state == XWAYLAND) {
Pekka Paalanenbbc749a2016-11-15 12:22:09 +0200109 assert(!surface->added);
110
Quentin Glidic248dd102016-08-12 10:41:34 +0200111 surface->view =
112 weston_desktop_surface_create_view(surface->surface);
113 weston_layer_entry_insert(&surface->xwayland->layer.view_list,
114 &surface->view->layer_link);
Pekka Paalanene3a582f2016-11-15 14:24:21 +0200115 surface->view->is_mapped = true;
116 wsurface->is_mapped = true;
Quentin Glidic248dd102016-08-12 10:41:34 +0200117 }
118
119 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +0200120 }
121
122 if (parent != NULL)
123 weston_desktop_surface_set_relative_to(surface->surface, parent,
124 x, y, false);
125}
126
127static void
128weston_desktop_xwayland_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +0200129 void *user_data,
Quentin Glidic248dd102016-08-12 10:41:34 +0200130 int32_t sx, int32_t sy)
131{
Quentin Glidic955cec02016-08-12 10:41:35 +0200132 struct weston_desktop_xwayland_surface *surface = user_data;
Pekka Paalanen5f93b9f2017-01-18 15:37:59 +0200133 struct weston_geometry oldgeom;
134
135 assert(dsurface == surface->surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200136
Pekka Paalanen0adb6a72016-11-28 16:37:07 +0200137#ifdef WM_DEBUG
138 weston_log("%s: xwayland surface %p\n", __func__, surface);
139#endif
140
Quentin Glidic248dd102016-08-12 10:41:34 +0200141 if (surface->has_next_geometry) {
Pekka Paalanen5f93b9f2017-01-18 15:37:59 +0200142 oldgeom = weston_desktop_surface_get_geometry(surface->surface);
143 sx -= surface->next_geometry.x - oldgeom.x;
144 sy -= surface->next_geometry.y - oldgeom.x;
145
Quentin Glidic248dd102016-08-12 10:41:34 +0200146 surface->has_next_geometry = false;
147 weston_desktop_surface_set_geometry(surface->surface,
148 surface->next_geometry);
149 }
150
151 if (surface->added)
152 weston_desktop_api_committed(surface->desktop, surface->surface,
153 sx, sy);
154}
155
156static void
157weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200158 void *user_data,
159 int32_t width, int32_t height)
Quentin Glidic248dd102016-08-12 10:41:34 +0200160{
Quentin Glidic955cec02016-08-12 10:41:35 +0200161 struct weston_desktop_xwayland_surface *surface = user_data;
162 struct weston_surface *wsurface =
163 weston_desktop_surface_get_surface(surface->surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200164
Quentin Glidic955cec02016-08-12 10:41:35 +0200165 surface->client_interface->send_configure(wsurface, width, height);
Quentin Glidic248dd102016-08-12 10:41:34 +0200166}
167
168static void
169weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface *dsurface,
170 void *user_data)
171{
Quentin Glidic955cec02016-08-12 10:41:35 +0200172 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200173
174 wl_list_remove(&surface->resource_destroy_listener.link);
175
176 weston_desktop_surface_unset_relative_to(surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200177 if (surface->added)
Quentin Glidic248dd102016-08-12 10:41:34 +0200178 weston_desktop_api_surface_removed(surface->desktop,
179 surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200180 else if (surface->state == XWAYLAND)
Quentin Glidicf01ecee2016-08-16 10:52:46 +0200181 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +0200182
183 free(surface);
184}
185
186static bool
187weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200188 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200189{
Quentin Glidic955cec02016-08-12 10:41:35 +0200190 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200191
192 return surface->state == MAXIMIZED;
193}
194
195static bool
196weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200197 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200198{
Quentin Glidic955cec02016-08-12 10:41:35 +0200199 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200200
201 return surface->state == FULLSCREEN;
202}
203
204static const struct weston_desktop_surface_implementation weston_desktop_xwayland_surface_internal_implementation = {
205 .committed = weston_desktop_xwayland_surface_committed,
206 .set_size = weston_desktop_xwayland_surface_set_size,
207
208 .get_maximized = weston_desktop_xwayland_surface_get_maximized,
209 .get_fullscreen = weston_desktop_xwayland_surface_get_fullscreen,
210
211 .destroy = weston_desktop_xwayland_surface_destroy,
212};
213
214static void
215weston_destop_xwayland_resource_destroyed(struct wl_listener *listener,
216 void *data)
217{
Quentin Glidic955cec02016-08-12 10:41:35 +0200218 struct weston_desktop_xwayland_surface *surface =
Quentin Glidic248dd102016-08-12 10:41:34 +0200219 wl_container_of(listener, surface, resource_destroy_listener);
220
221 weston_desktop_surface_destroy(surface->surface);
222}
223
Quentin Glidic955cec02016-08-12 10:41:35 +0200224static struct weston_desktop_xwayland_surface *
225create_surface(struct weston_desktop_xwayland *xwayland,
226 struct weston_surface *wsurface,
227 const struct weston_xwayland_client_interface *client_interface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200228{
Quentin Glidic955cec02016-08-12 10:41:35 +0200229 struct weston_desktop_xwayland_surface *surface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200230
Quentin Glidic955cec02016-08-12 10:41:35 +0200231 surface = zalloc(sizeof(struct weston_desktop_xwayland_surface));
Quentin Glidic248dd102016-08-12 10:41:34 +0200232 if (surface == NULL)
233 return NULL;
234
235 surface->xwayland = xwayland;
236 surface->desktop = xwayland->desktop;
Quentin Glidic955cec02016-08-12 10:41:35 +0200237 surface->client_interface = client_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200238
239 surface->surface =
240 weston_desktop_surface_create(surface->desktop,
241 xwayland->client, wsurface,
242 &weston_desktop_xwayland_surface_internal_implementation,
243 surface);
244 if (surface->surface == NULL) {
245 free(surface);
246 return NULL;
247 }
248
249 surface->resource_destroy_listener.notify =
250 weston_destop_xwayland_resource_destroyed;
251 wl_resource_add_destroy_listener(wsurface->resource,
252 &surface->resource_destroy_listener);
253
Giulio Camuffof15320f2016-12-08 09:21:08 +0100254 weston_desktop_surface_set_pid(surface->surface, 0);
255
Quentin Glidic248dd102016-08-12 10:41:34 +0200256 return surface;
257}
258
259static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200260set_toplevel(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200261{
262 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
263 0, 0);
264}
265
266static void
Pekka Paalanen37111e12016-11-16 14:03:31 +0200267set_toplevel_with_position(struct weston_desktop_xwayland_surface *surface,
268 int32_t x, int32_t y)
269{
270 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
271 0, 0);
272 weston_desktop_api_set_xwayland_position(surface->desktop,
273 surface->surface, x, y);
274}
275
276static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200277set_parent(struct weston_desktop_xwayland_surface *surface,
278 struct weston_surface *wparent)
Quentin Glidic248dd102016-08-12 10:41:34 +0200279{
280 struct weston_desktop_surface *parent;
281
282 if (!weston_surface_is_desktop_surface(wparent))
283 return;
284
285 parent = weston_surface_get_desktop_surface(wparent);
Quentin Glidic955cec02016-08-12 10:41:35 +0200286 weston_desktop_api_set_parent(surface->desktop, surface->surface, parent);
Quentin Glidic248dd102016-08-12 10:41:34 +0200287}
288
289static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200290set_transient(struct weston_desktop_xwayland_surface *surface,
291 struct weston_surface *wparent, int x, int y)
292{
293 struct weston_desktop_surface *parent;
294
295 if (!weston_surface_is_desktop_surface(wparent))
296 return;
297
298 parent = weston_surface_get_desktop_surface(wparent);
299 weston_desktop_xwayland_surface_change_state(surface, TRANSIENT, parent,
300 x, y);
301}
302
303static void
304set_fullscreen(struct weston_desktop_xwayland_surface *surface,
305 struct weston_output *output)
Quentin Glidic248dd102016-08-12 10:41:34 +0200306{
307 weston_desktop_xwayland_surface_change_state(surface, FULLSCREEN, NULL,
308 0, 0);
309 weston_desktop_api_fullscreen_requested(surface->desktop,
310 surface->surface, true, output);
311}
312
313static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200314set_xwayland(struct weston_desktop_xwayland_surface *surface, int x, int y)
Quentin Glidic248dd102016-08-12 10:41:34 +0200315{
316 weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL,
317 x, y);
Ilia Bozhinov3e5303d2017-06-28 00:08:40 +0300318 weston_view_set_position(surface->view, x, y);
Quentin Glidic248dd102016-08-12 10:41:34 +0200319}
320
321static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200322move(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200323 struct weston_pointer *pointer)
324{
325 if (surface->state == TOPLEVEL ||
326 surface->state == MAXIMIZED ||
327 surface->state == FULLSCREEN)
328 weston_desktop_api_move(surface->desktop, surface->surface,
329 pointer->seat, pointer->grab_serial);
330 return 0;
331}
332
333static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200334resize(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200335 struct weston_pointer *pointer, uint32_t edges)
336{
337 if (surface->state == TOPLEVEL ||
338 surface->state == MAXIMIZED ||
339 surface->state == FULLSCREEN)
340 weston_desktop_api_resize(surface->desktop, surface->surface,
341 pointer->seat, pointer->grab_serial,
342 edges);
343 return 0;
344}
345
346static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200347set_title(struct weston_desktop_xwayland_surface *surface, const char *title)
Quentin Glidic248dd102016-08-12 10:41:34 +0200348{
349 weston_desktop_surface_set_title(surface->surface, title);
350}
351
352static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200353set_window_geometry(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200354 int32_t x, int32_t y, int32_t width, int32_t height)
355{
356 surface->has_next_geometry = true;
357 surface->next_geometry.x = x;
358 surface->next_geometry.y = y;
359 surface->next_geometry.width = width;
360 surface->next_geometry.height = height;
361}
362
363static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200364set_maximized(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200365{
366 weston_desktop_xwayland_surface_change_state(surface, MAXIMIZED, NULL,
367 0, 0);
368 weston_desktop_api_maximized_requested(surface->desktop,
369 surface->surface, true);
370}
371
372static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200373set_pid(struct weston_desktop_xwayland_surface *surface, pid_t pid)
Quentin Glidic248dd102016-08-12 10:41:34 +0200374{
Quentin Glidic955cec02016-08-12 10:41:35 +0200375 weston_desktop_surface_set_pid(surface->surface, pid);
Quentin Glidic248dd102016-08-12 10:41:34 +0200376}
377
Quentin Glidic955cec02016-08-12 10:41:35 +0200378static const struct weston_desktop_xwayland_interface weston_desktop_xwayland_interface = {
379 .create_surface = create_surface,
380 .set_toplevel = set_toplevel,
Pekka Paalanen37111e12016-11-16 14:03:31 +0200381 .set_toplevel_with_position = set_toplevel_with_position,
Quentin Glidic955cec02016-08-12 10:41:35 +0200382 .set_parent = set_parent,
383 .set_transient = set_transient,
384 .set_fullscreen = set_fullscreen,
385 .set_xwayland = set_xwayland,
386 .move = move,
387 .resize = resize,
388 .set_title = set_title,
389 .set_window_geometry = set_window_geometry,
390 .set_maximized = set_maximized,
391 .set_pid = set_pid,
392};
393
Quentin Glidic248dd102016-08-12 10:41:34 +0200394void
395weston_desktop_xwayland_init(struct weston_desktop *desktop)
396{
397 struct weston_compositor *compositor = weston_desktop_get_compositor(desktop);
398 struct weston_desktop_xwayland *xwayland;
399
400 xwayland = zalloc(sizeof(struct weston_desktop_xwayland));
401 if (xwayland == NULL)
402 return;
403
404 xwayland->desktop = desktop;
405 xwayland->client = weston_desktop_client_create(desktop, NULL, NULL, NULL, NULL, 0, 0);
406
Quentin Glidic82681572016-12-17 13:40:51 +0100407 weston_layer_init(&xwayland->layer, compositor);
408 /* We put this layer on top of regular shell surfaces, but hopefully
409 * below any UI the shell would add */
410 weston_layer_set_position(&xwayland->layer,
411 WESTON_LAYER_POSITION_NORMAL + 1);
Quentin Glidic248dd102016-08-12 10:41:34 +0200412
Quentin Glidic955cec02016-08-12 10:41:35 +0200413 compositor->xwayland = xwayland;
414 compositor->xwayland_interface = &weston_desktop_xwayland_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200415}