blob: bd68bc6881bd80e054ed375eba81522a9ba19c37 [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{
74 bool to_add = (parent == NULL && state != XWAYLAND);
75
76 assert(state != NONE);
77
Quentin Glidic6384edf2016-08-16 11:42:47 +020078 if (to_add && surface->added) {
79 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +020080 return;
Quentin Glidic6384edf2016-08-16 11:42:47 +020081 }
Quentin Glidic248dd102016-08-12 10:41:34 +020082
83 if (surface->state != state) {
84 if (surface->state == XWAYLAND) {
Quentin Glidicf01ecee2016-08-16 10:52:46 +020085 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidicf6636a82016-08-16 10:55:02 +020086 weston_view_destroy(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +020087 surface->view = NULL;
88 }
89
90 if (to_add) {
91 weston_desktop_surface_unset_relative_to(surface->surface);
92 weston_desktop_api_surface_added(surface->desktop,
93 surface->surface);
94 } else if (surface->added) {
95 weston_desktop_api_surface_removed(surface->desktop,
96 surface->surface);
97 }
98
99 if (state == XWAYLAND) {
100 surface->view =
101 weston_desktop_surface_create_view(surface->surface);
102 weston_layer_entry_insert(&surface->xwayland->layer.view_list,
103 &surface->view->layer_link);
104 weston_view_set_position(surface->view, x, y);
105 }
106
107 surface->state = state;
108 surface->added = to_add;
109 }
110
111 if (parent != NULL)
112 weston_desktop_surface_set_relative_to(surface->surface, parent,
113 x, y, false);
114}
115
116static void
117weston_desktop_xwayland_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +0200118 void *user_data,
Quentin Glidic248dd102016-08-12 10:41:34 +0200119 int32_t sx, int32_t sy)
120{
Quentin Glidic955cec02016-08-12 10:41:35 +0200121 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200122
123 if (surface->has_next_geometry) {
124 surface->has_next_geometry = false;
125 weston_desktop_surface_set_geometry(surface->surface,
126 surface->next_geometry);
127 }
128
129 if (surface->added)
130 weston_desktop_api_committed(surface->desktop, surface->surface,
131 sx, sy);
132}
133
134static void
135weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200136 void *user_data,
137 int32_t width, int32_t height)
Quentin Glidic248dd102016-08-12 10:41:34 +0200138{
Quentin Glidic955cec02016-08-12 10:41:35 +0200139 struct weston_desktop_xwayland_surface *surface = user_data;
140 struct weston_surface *wsurface =
141 weston_desktop_surface_get_surface(surface->surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200142
Quentin Glidic955cec02016-08-12 10:41:35 +0200143 surface->client_interface->send_configure(wsurface, width, height);
Quentin Glidic248dd102016-08-12 10:41:34 +0200144}
145
146static void
147weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface *dsurface,
148 void *user_data)
149{
Quentin Glidic955cec02016-08-12 10:41:35 +0200150 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200151
152 wl_list_remove(&surface->resource_destroy_listener.link);
153
154 weston_desktop_surface_unset_relative_to(surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200155 if (surface->added)
Quentin Glidic248dd102016-08-12 10:41:34 +0200156 weston_desktop_api_surface_removed(surface->desktop,
157 surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200158 else if (surface->state == XWAYLAND)
Quentin Glidicf01ecee2016-08-16 10:52:46 +0200159 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +0200160
161 free(surface);
162}
163
164static bool
165weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200166 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200167{
Quentin Glidic955cec02016-08-12 10:41:35 +0200168 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200169
170 return surface->state == MAXIMIZED;
171}
172
173static bool
174weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200175 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200176{
Quentin Glidic955cec02016-08-12 10:41:35 +0200177 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200178
179 return surface->state == FULLSCREEN;
180}
181
182static const struct weston_desktop_surface_implementation weston_desktop_xwayland_surface_internal_implementation = {
183 .committed = weston_desktop_xwayland_surface_committed,
184 .set_size = weston_desktop_xwayland_surface_set_size,
185
186 .get_maximized = weston_desktop_xwayland_surface_get_maximized,
187 .get_fullscreen = weston_desktop_xwayland_surface_get_fullscreen,
188
189 .destroy = weston_desktop_xwayland_surface_destroy,
190};
191
192static void
193weston_destop_xwayland_resource_destroyed(struct wl_listener *listener,
194 void *data)
195{
Quentin Glidic955cec02016-08-12 10:41:35 +0200196 struct weston_desktop_xwayland_surface *surface =
Quentin Glidic248dd102016-08-12 10:41:34 +0200197 wl_container_of(listener, surface, resource_destroy_listener);
198
199 weston_desktop_surface_destroy(surface->surface);
200}
201
Quentin Glidic955cec02016-08-12 10:41:35 +0200202static struct weston_desktop_xwayland_surface *
203create_surface(struct weston_desktop_xwayland *xwayland,
204 struct weston_surface *wsurface,
205 const struct weston_xwayland_client_interface *client_interface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200206{
Quentin Glidic955cec02016-08-12 10:41:35 +0200207 struct weston_desktop_xwayland_surface *surface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200208
Quentin Glidic955cec02016-08-12 10:41:35 +0200209 surface = zalloc(sizeof(struct weston_desktop_xwayland_surface));
Quentin Glidic248dd102016-08-12 10:41:34 +0200210 if (surface == NULL)
211 return NULL;
212
213 surface->xwayland = xwayland;
214 surface->desktop = xwayland->desktop;
Quentin Glidic955cec02016-08-12 10:41:35 +0200215 surface->client_interface = client_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200216
217 surface->surface =
218 weston_desktop_surface_create(surface->desktop,
219 xwayland->client, wsurface,
220 &weston_desktop_xwayland_surface_internal_implementation,
221 surface);
222 if (surface->surface == NULL) {
223 free(surface);
224 return NULL;
225 }
226
227 surface->resource_destroy_listener.notify =
228 weston_destop_xwayland_resource_destroyed;
229 wl_resource_add_destroy_listener(wsurface->resource,
230 &surface->resource_destroy_listener);
231
232 return surface;
233}
234
235static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200236set_toplevel(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200237{
238 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
239 0, 0);
240}
241
242static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200243set_parent(struct weston_desktop_xwayland_surface *surface,
244 struct weston_surface *wparent)
Quentin Glidic248dd102016-08-12 10:41:34 +0200245{
246 struct weston_desktop_surface *parent;
247
248 if (!weston_surface_is_desktop_surface(wparent))
249 return;
250
251 parent = weston_surface_get_desktop_surface(wparent);
Quentin Glidic955cec02016-08-12 10:41:35 +0200252 weston_desktop_api_set_parent(surface->desktop, surface->surface, parent);
Quentin Glidic248dd102016-08-12 10:41:34 +0200253}
254
255static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200256set_transient(struct weston_desktop_xwayland_surface *surface,
257 struct weston_surface *wparent, int x, int y)
258{
259 struct weston_desktop_surface *parent;
260
261 if (!weston_surface_is_desktop_surface(wparent))
262 return;
263
264 parent = weston_surface_get_desktop_surface(wparent);
265 weston_desktop_xwayland_surface_change_state(surface, TRANSIENT, parent,
266 x, y);
267}
268
269static void
270set_fullscreen(struct weston_desktop_xwayland_surface *surface,
271 struct weston_output *output)
Quentin Glidic248dd102016-08-12 10:41:34 +0200272{
273 weston_desktop_xwayland_surface_change_state(surface, FULLSCREEN, NULL,
274 0, 0);
275 weston_desktop_api_fullscreen_requested(surface->desktop,
276 surface->surface, true, output);
277}
278
279static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200280set_xwayland(struct weston_desktop_xwayland_surface *surface, int x, int y)
Quentin Glidic248dd102016-08-12 10:41:34 +0200281{
282 weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL,
283 x, y);
284}
285
286static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200287move(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200288 struct weston_pointer *pointer)
289{
290 if (surface->state == TOPLEVEL ||
291 surface->state == MAXIMIZED ||
292 surface->state == FULLSCREEN)
293 weston_desktop_api_move(surface->desktop, surface->surface,
294 pointer->seat, pointer->grab_serial);
295 return 0;
296}
297
298static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200299resize(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200300 struct weston_pointer *pointer, uint32_t edges)
301{
302 if (surface->state == TOPLEVEL ||
303 surface->state == MAXIMIZED ||
304 surface->state == FULLSCREEN)
305 weston_desktop_api_resize(surface->desktop, surface->surface,
306 pointer->seat, pointer->grab_serial,
307 edges);
308 return 0;
309}
310
311static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200312set_title(struct weston_desktop_xwayland_surface *surface, const char *title)
Quentin Glidic248dd102016-08-12 10:41:34 +0200313{
314 weston_desktop_surface_set_title(surface->surface, title);
315}
316
317static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200318set_window_geometry(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200319 int32_t x, int32_t y, int32_t width, int32_t height)
320{
321 surface->has_next_geometry = true;
322 surface->next_geometry.x = x;
323 surface->next_geometry.y = y;
324 surface->next_geometry.width = width;
325 surface->next_geometry.height = height;
326}
327
328static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200329set_maximized(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200330{
331 weston_desktop_xwayland_surface_change_state(surface, MAXIMIZED, NULL,
332 0, 0);
333 weston_desktop_api_maximized_requested(surface->desktop,
334 surface->surface, true);
335}
336
337static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200338set_pid(struct weston_desktop_xwayland_surface *surface, pid_t pid)
Quentin Glidic248dd102016-08-12 10:41:34 +0200339{
Quentin Glidic955cec02016-08-12 10:41:35 +0200340 weston_desktop_surface_set_pid(surface->surface, pid);
Quentin Glidic248dd102016-08-12 10:41:34 +0200341}
342
Quentin Glidic955cec02016-08-12 10:41:35 +0200343static const struct weston_desktop_xwayland_interface weston_desktop_xwayland_interface = {
344 .create_surface = create_surface,
345 .set_toplevel = set_toplevel,
346 .set_parent = set_parent,
347 .set_transient = set_transient,
348 .set_fullscreen = set_fullscreen,
349 .set_xwayland = set_xwayland,
350 .move = move,
351 .resize = resize,
352 .set_title = set_title,
353 .set_window_geometry = set_window_geometry,
354 .set_maximized = set_maximized,
355 .set_pid = set_pid,
356};
357
Quentin Glidic248dd102016-08-12 10:41:34 +0200358void
359weston_desktop_xwayland_init(struct weston_desktop *desktop)
360{
361 struct weston_compositor *compositor = weston_desktop_get_compositor(desktop);
362 struct weston_desktop_xwayland *xwayland;
363
364 xwayland = zalloc(sizeof(struct weston_desktop_xwayland));
365 if (xwayland == NULL)
366 return;
367
368 xwayland->desktop = desktop;
369 xwayland->client = weston_desktop_client_create(desktop, NULL, NULL, NULL, NULL, 0, 0);
370
371 weston_layer_init(&xwayland->layer, &compositor->cursor_layer.link);
372
Quentin Glidic955cec02016-08-12 10:41:35 +0200373 compositor->xwayland = xwayland;
374 compositor->xwayland_interface = &weston_desktop_xwayland_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200375}