1 /*
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 <libweston/libweston.h>
34 #include <libweston/zalloc.h>
35
36 #include <libweston-desktop/libweston-desktop.h>
37 #include "internal.h"
38 #include "xwayland/xwayland-internal-interface.h"
39
40 enum weston_desktop_xwayland_surface_state {
41 NONE,
42 TOPLEVEL,
43 MAXIMIZED,
44 FULLSCREEN,
45 TRANSIENT,
46 XWAYLAND,
47 };
48
49 struct weston_desktop_xwayland {
50 struct weston_desktop *desktop;
51 struct weston_desktop_client *client;
52 struct weston_layer layer;
53 };
54
55 struct weston_desktop_xwayland_surface {
56 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;
61 const struct weston_xwayland_client_interface *client_interface;
62 struct weston_geometry next_geometry;
63 bool has_next_geometry;
64 bool committed;
65 bool added;
66 enum weston_desktop_xwayland_surface_state state;
67 };
68
69 static void
weston_desktop_xwayland_surface_change_state(struct weston_desktop_xwayland_surface * surface,enum weston_desktop_xwayland_surface_state state,struct weston_desktop_surface * parent,int32_t x,int32_t y)70 weston_desktop_xwayland_surface_change_state(struct weston_desktop_xwayland_surface *surface,
71 enum weston_desktop_xwayland_surface_state state,
72 struct weston_desktop_surface *parent,
73 int32_t x, int32_t y)
74 {
75 struct weston_surface *wsurface;
76 bool to_add = (parent == NULL && state != XWAYLAND);
77
78 assert(state != NONE);
79 assert(!parent || state == TRANSIENT);
80
81 if (to_add && surface->added) {
82 surface->state = state;
83 return;
84 }
85
86 wsurface = weston_desktop_surface_get_surface(surface->surface);
87
88 if (surface->state != state) {
89 if (surface->state == XWAYLAND) {
90 assert(!surface->added);
91
92 weston_desktop_surface_unlink_view(surface->view);
93 weston_view_destroy(surface->view);
94 surface->view = NULL;
95 weston_surface_unmap(wsurface);
96 }
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);
102 surface->added = true;
103 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
111 } else if (surface->added) {
112 weston_desktop_api_surface_removed(surface->desktop,
113 surface->surface);
114 surface->added = false;
115 }
116
117 if (state == XWAYLAND) {
118 assert(!surface->added);
119
120 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);
124 surface->view->is_mapped = true;
125 wsurface->is_mapped = true;
126 }
127
128 surface->state = state;
129 }
130
131 if (parent != NULL)
132 weston_desktop_surface_set_relative_to(surface->surface, parent,
133 x, y, false);
134 }
135
136 static void
weston_desktop_xwayland_surface_committed(struct weston_desktop_surface * dsurface,void * user_data,int32_t sx,int32_t sy)137 weston_desktop_xwayland_surface_committed(struct weston_desktop_surface *dsurface,
138 void *user_data,
139 int32_t sx, int32_t sy)
140 {
141 struct weston_desktop_xwayland_surface *surface = user_data;
142 struct weston_geometry oldgeom;
143
144 assert(dsurface == surface->surface);
145 surface->committed = true;
146
147 #ifdef WM_DEBUG
148 weston_log("%s: xwayland surface %p\n", __func__, surface);
149 #endif
150
151 if (surface->has_next_geometry) {
152 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
156 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
166 static void
weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface * dsurface,void * user_data,int32_t width,int32_t height)167 weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface *dsurface,
168 void *user_data,
169 int32_t width, int32_t height)
170 {
171 struct weston_desktop_xwayland_surface *surface = user_data;
172 struct weston_surface *wsurface =
173 weston_desktop_surface_get_surface(surface->surface);
174
175 surface->client_interface->send_configure(wsurface, width, height);
176 }
177
178 static void
weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface * dsurface,void * user_data)179 weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface *dsurface,
180 void *user_data)
181 {
182 struct weston_desktop_xwayland_surface *surface = user_data;
183
184 wl_list_remove(&surface->resource_destroy_listener.link);
185
186 weston_desktop_surface_unset_relative_to(surface->surface);
187 if (surface->added)
188 weston_desktop_api_surface_removed(surface->desktop,
189 surface->surface);
190 else if (surface->state == XWAYLAND)
191 weston_desktop_surface_unlink_view(surface->view);
192
193 free(surface);
194 }
195
196 static bool
weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface * dsurface,void * user_data)197 weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface,
198 void *user_data)
199 {
200 struct weston_desktop_xwayland_surface *surface = user_data;
201
202 return surface->state == MAXIMIZED;
203 }
204
205 static bool
weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface * dsurface,void * user_data)206 weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
207 void *user_data)
208 {
209 struct weston_desktop_xwayland_surface *surface = user_data;
210
211 return surface->state == FULLSCREEN;
212 }
213
214 static 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
224 static void
weston_destop_xwayland_resource_destroyed(struct wl_listener * listener,void * data)225 weston_destop_xwayland_resource_destroyed(struct wl_listener *listener,
226 void *data)
227 {
228 struct weston_desktop_xwayland_surface *surface =
229 wl_container_of(listener, surface, resource_destroy_listener);
230
231 weston_desktop_surface_destroy(surface->surface);
232 }
233
234 static struct weston_desktop_xwayland_surface *
create_surface(struct weston_desktop_xwayland * xwayland,struct weston_surface * wsurface,const struct weston_xwayland_client_interface * client_interface)235 create_surface(struct weston_desktop_xwayland *xwayland,
236 struct weston_surface *wsurface,
237 const struct weston_xwayland_client_interface *client_interface)
238 {
239 struct weston_desktop_xwayland_surface *surface;
240
241 surface = zalloc(sizeof(struct weston_desktop_xwayland_surface));
242 if (surface == NULL)
243 return NULL;
244
245 surface->xwayland = xwayland;
246 surface->desktop = xwayland->desktop;
247 surface->client_interface = client_interface;
248
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
264 weston_desktop_surface_set_pid(surface->surface, 0);
265
266 return surface;
267 }
268
269 static void
set_toplevel(struct weston_desktop_xwayland_surface * surface)270 set_toplevel(struct weston_desktop_xwayland_surface *surface)
271 {
272 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
273 0, 0);
274 }
275
276 static void
set_toplevel_with_position(struct weston_desktop_xwayland_surface * surface,int32_t x,int32_t y)277 set_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
286 static void
set_parent(struct weston_desktop_xwayland_surface * surface,struct weston_surface * wparent)287 set_parent(struct weston_desktop_xwayland_surface *surface,
288 struct weston_surface *wparent)
289 {
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);
296 weston_desktop_api_set_parent(surface->desktop, surface->surface, parent);
297 }
298
299 static void
set_transient(struct weston_desktop_xwayland_surface * surface,struct weston_surface * wparent,int x,int y)300 set_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
313 static void
set_fullscreen(struct weston_desktop_xwayland_surface * surface,struct weston_output * output)314 set_fullscreen(struct weston_desktop_xwayland_surface *surface,
315 struct weston_output *output)
316 {
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
323 static void
set_xwayland(struct weston_desktop_xwayland_surface * surface,int x,int y)324 set_xwayland(struct weston_desktop_xwayland_surface *surface, int x, int y)
325 {
326 weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL,
327 x, y);
328 weston_view_set_position(surface->view, x, y);
329 }
330
331 static int
move(struct weston_desktop_xwayland_surface * surface,struct weston_pointer * pointer)332 move(struct weston_desktop_xwayland_surface *surface,
333 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
343 static int
resize(struct weston_desktop_xwayland_surface * surface,struct weston_pointer * pointer,uint32_t edges)344 resize(struct weston_desktop_xwayland_surface *surface,
345 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
356 static void
set_title(struct weston_desktop_xwayland_surface * surface,const char * title)357 set_title(struct weston_desktop_xwayland_surface *surface, const char *title)
358 {
359 weston_desktop_surface_set_title(surface->surface, title);
360 }
361
362 static void
set_window_geometry(struct weston_desktop_xwayland_surface * surface,int32_t x,int32_t y,int32_t width,int32_t height)363 set_window_geometry(struct weston_desktop_xwayland_surface *surface,
364 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
373 static void
set_maximized(struct weston_desktop_xwayland_surface * surface)374 set_maximized(struct weston_desktop_xwayland_surface *surface)
375 {
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
382 static void
set_pid(struct weston_desktop_xwayland_surface * surface,pid_t pid)383 set_pid(struct weston_desktop_xwayland_surface *surface, pid_t pid)
384 {
385 weston_desktop_surface_set_pid(surface->surface, pid);
386 }
387
388 static const struct weston_desktop_xwayland_interface weston_desktop_xwayland_interface = {
389 .create_surface = create_surface,
390 .set_toplevel = set_toplevel,
391 .set_toplevel_with_position = set_toplevel_with_position,
392 .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
404 void
weston_desktop_xwayland_init(struct weston_desktop * desktop)405 weston_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
417 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);
422
423 compositor->xwayland = xwayland;
424 compositor->xwayland_interface = &weston_desktop_xwayland_interface;
425 }
426