blob: 4f4b453fc1b69103a1f3d5ea9909baa9a40ef8eb [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);
115 weston_view_set_position(surface->view, x, y);
Pekka Paalanene3a582f2016-11-15 14:24:21 +0200116 surface->view->is_mapped = true;
117 wsurface->is_mapped = true;
Quentin Glidic248dd102016-08-12 10:41:34 +0200118 }
119
120 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +0200121 }
122
123 if (parent != NULL)
124 weston_desktop_surface_set_relative_to(surface->surface, parent,
125 x, y, false);
126}
127
128static void
129weston_desktop_xwayland_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +0200130 void *user_data,
Quentin Glidic248dd102016-08-12 10:41:34 +0200131 int32_t sx, int32_t sy)
132{
Quentin Glidic955cec02016-08-12 10:41:35 +0200133 struct weston_desktop_xwayland_surface *surface = user_data;
Pekka Paalanen5f93b9f2017-01-18 15:37:59 +0200134 struct weston_geometry oldgeom;
135
136 assert(dsurface == surface->surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200137
Pekka Paalanen0adb6a72016-11-28 16:37:07 +0200138#ifdef WM_DEBUG
139 weston_log("%s: xwayland surface %p\n", __func__, surface);
140#endif
141
Quentin Glidic248dd102016-08-12 10:41:34 +0200142 if (surface->has_next_geometry) {
Pekka Paalanen5f93b9f2017-01-18 15:37:59 +0200143 oldgeom = weston_desktop_surface_get_geometry(surface->surface);
144 sx -= surface->next_geometry.x - oldgeom.x;
145 sy -= surface->next_geometry.y - oldgeom.x;
146
Quentin Glidic248dd102016-08-12 10:41:34 +0200147 surface->has_next_geometry = false;
148 weston_desktop_surface_set_geometry(surface->surface,
149 surface->next_geometry);
150 }
151
152 if (surface->added)
153 weston_desktop_api_committed(surface->desktop, surface->surface,
154 sx, sy);
155}
156
157static void
158weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200159 void *user_data,
160 int32_t width, int32_t height)
Quentin Glidic248dd102016-08-12 10:41:34 +0200161{
Quentin Glidic955cec02016-08-12 10:41:35 +0200162 struct weston_desktop_xwayland_surface *surface = user_data;
163 struct weston_surface *wsurface =
164 weston_desktop_surface_get_surface(surface->surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200165
Quentin Glidic955cec02016-08-12 10:41:35 +0200166 surface->client_interface->send_configure(wsurface, width, height);
Quentin Glidic248dd102016-08-12 10:41:34 +0200167}
168
169static void
170weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface *dsurface,
171 void *user_data)
172{
Quentin Glidic955cec02016-08-12 10:41:35 +0200173 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200174
175 wl_list_remove(&surface->resource_destroy_listener.link);
176
177 weston_desktop_surface_unset_relative_to(surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200178 if (surface->added)
Quentin Glidic248dd102016-08-12 10:41:34 +0200179 weston_desktop_api_surface_removed(surface->desktop,
180 surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200181 else if (surface->state == XWAYLAND)
Quentin Glidicf01ecee2016-08-16 10:52:46 +0200182 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +0200183
184 free(surface);
185}
186
187static bool
188weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200189 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200190{
Quentin Glidic955cec02016-08-12 10:41:35 +0200191 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200192
193 return surface->state == MAXIMIZED;
194}
195
196static bool
197weston_desktop_xwayland_surface_get_fullscreen(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 == FULLSCREEN;
203}
204
205static const struct weston_desktop_surface_implementation weston_desktop_xwayland_surface_internal_implementation = {
206 .committed = weston_desktop_xwayland_surface_committed,
207 .set_size = weston_desktop_xwayland_surface_set_size,
208
209 .get_maximized = weston_desktop_xwayland_surface_get_maximized,
210 .get_fullscreen = weston_desktop_xwayland_surface_get_fullscreen,
211
212 .destroy = weston_desktop_xwayland_surface_destroy,
213};
214
215static void
216weston_destop_xwayland_resource_destroyed(struct wl_listener *listener,
217 void *data)
218{
Quentin Glidic955cec02016-08-12 10:41:35 +0200219 struct weston_desktop_xwayland_surface *surface =
Quentin Glidic248dd102016-08-12 10:41:34 +0200220 wl_container_of(listener, surface, resource_destroy_listener);
221
222 weston_desktop_surface_destroy(surface->surface);
223}
224
Quentin Glidic955cec02016-08-12 10:41:35 +0200225static struct weston_desktop_xwayland_surface *
226create_surface(struct weston_desktop_xwayland *xwayland,
227 struct weston_surface *wsurface,
228 const struct weston_xwayland_client_interface *client_interface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200229{
Quentin Glidic955cec02016-08-12 10:41:35 +0200230 struct weston_desktop_xwayland_surface *surface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200231
Quentin Glidic955cec02016-08-12 10:41:35 +0200232 surface = zalloc(sizeof(struct weston_desktop_xwayland_surface));
Quentin Glidic248dd102016-08-12 10:41:34 +0200233 if (surface == NULL)
234 return NULL;
235
236 surface->xwayland = xwayland;
237 surface->desktop = xwayland->desktop;
Quentin Glidic955cec02016-08-12 10:41:35 +0200238 surface->client_interface = client_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200239
240 surface->surface =
241 weston_desktop_surface_create(surface->desktop,
242 xwayland->client, wsurface,
243 &weston_desktop_xwayland_surface_internal_implementation,
244 surface);
245 if (surface->surface == NULL) {
246 free(surface);
247 return NULL;
248 }
249
250 surface->resource_destroy_listener.notify =
251 weston_destop_xwayland_resource_destroyed;
252 wl_resource_add_destroy_listener(wsurface->resource,
253 &surface->resource_destroy_listener);
254
Giulio Camuffof15320f2016-12-08 09:21:08 +0100255 weston_desktop_surface_set_pid(surface->surface, 0);
256
Quentin Glidic248dd102016-08-12 10:41:34 +0200257 return surface;
258}
259
260static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200261set_toplevel(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200262{
263 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
264 0, 0);
265}
266
267static void
Pekka Paalanen37111e12016-11-16 14:03:31 +0200268set_toplevel_with_position(struct weston_desktop_xwayland_surface *surface,
269 int32_t x, int32_t y)
270{
271 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
272 0, 0);
273 weston_desktop_api_set_xwayland_position(surface->desktop,
274 surface->surface, x, y);
275}
276
277static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200278set_parent(struct weston_desktop_xwayland_surface *surface,
279 struct weston_surface *wparent)
Quentin Glidic248dd102016-08-12 10:41:34 +0200280{
281 struct weston_desktop_surface *parent;
282
283 if (!weston_surface_is_desktop_surface(wparent))
284 return;
285
286 parent = weston_surface_get_desktop_surface(wparent);
Quentin Glidic955cec02016-08-12 10:41:35 +0200287 weston_desktop_api_set_parent(surface->desktop, surface->surface, parent);
Quentin Glidic248dd102016-08-12 10:41:34 +0200288}
289
290static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200291set_transient(struct weston_desktop_xwayland_surface *surface,
292 struct weston_surface *wparent, int x, int y)
293{
294 struct weston_desktop_surface *parent;
295
296 if (!weston_surface_is_desktop_surface(wparent))
297 return;
298
299 parent = weston_surface_get_desktop_surface(wparent);
300 weston_desktop_xwayland_surface_change_state(surface, TRANSIENT, parent,
301 x, y);
302}
303
304static void
305set_fullscreen(struct weston_desktop_xwayland_surface *surface,
306 struct weston_output *output)
Quentin Glidic248dd102016-08-12 10:41:34 +0200307{
308 weston_desktop_xwayland_surface_change_state(surface, FULLSCREEN, NULL,
309 0, 0);
310 weston_desktop_api_fullscreen_requested(surface->desktop,
311 surface->surface, true, output);
312}
313
314static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200315set_xwayland(struct weston_desktop_xwayland_surface *surface, int x, int y)
Quentin Glidic248dd102016-08-12 10:41:34 +0200316{
317 weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL,
318 x, y);
319}
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}