blob: 41bc4c895c8693cd0e83d8ce55a7cffc806eb471 [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);
Pekka Paalanena838b782016-11-15 11:43:36 +020077 assert(!parent || state == TRANSIENT);
Quentin Glidic248dd102016-08-12 10:41:34 +020078
Quentin Glidic6384edf2016-08-16 11:42:47 +020079 if (to_add && surface->added) {
80 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +020081 return;
Quentin Glidic6384edf2016-08-16 11:42:47 +020082 }
Quentin Glidic248dd102016-08-12 10:41:34 +020083
84 if (surface->state != state) {
85 if (surface->state == XWAYLAND) {
Pekka Paalanenbbc749a2016-11-15 12:22:09 +020086 assert(!surface->added);
87
Quentin Glidicf01ecee2016-08-16 10:52:46 +020088 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidicf6636a82016-08-16 10:55:02 +020089 weston_view_destroy(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +020090 surface->view = NULL;
91 }
92
93 if (to_add) {
94 weston_desktop_surface_unset_relative_to(surface->surface);
95 weston_desktop_api_surface_added(surface->desktop,
96 surface->surface);
Pekka Paalanen33268202016-11-15 11:48:42 +020097 surface->added = true;
Quentin Glidic248dd102016-08-12 10:41:34 +020098 } else if (surface->added) {
99 weston_desktop_api_surface_removed(surface->desktop,
100 surface->surface);
Pekka Paalanen33268202016-11-15 11:48:42 +0200101 surface->added = false;
Quentin Glidic248dd102016-08-12 10:41:34 +0200102 }
103
104 if (state == XWAYLAND) {
Pekka Paalanenbbc749a2016-11-15 12:22:09 +0200105 assert(!surface->added);
106
Quentin Glidic248dd102016-08-12 10:41:34 +0200107 surface->view =
108 weston_desktop_surface_create_view(surface->surface);
109 weston_layer_entry_insert(&surface->xwayland->layer.view_list,
110 &surface->view->layer_link);
111 weston_view_set_position(surface->view, x, y);
112 }
113
114 surface->state = state;
Quentin Glidic248dd102016-08-12 10:41:34 +0200115 }
116
117 if (parent != NULL)
118 weston_desktop_surface_set_relative_to(surface->surface, parent,
119 x, y, false);
120}
121
122static void
123weston_desktop_xwayland_surface_committed(struct weston_desktop_surface *dsurface,
Quentin Glidic003da882016-08-15 12:21:39 +0200124 void *user_data,
Quentin Glidic248dd102016-08-12 10:41:34 +0200125 int32_t sx, int32_t sy)
126{
Quentin Glidic955cec02016-08-12 10:41:35 +0200127 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200128
129 if (surface->has_next_geometry) {
130 surface->has_next_geometry = false;
131 weston_desktop_surface_set_geometry(surface->surface,
132 surface->next_geometry);
133 }
134
135 if (surface->added)
136 weston_desktop_api_committed(surface->desktop, surface->surface,
137 sx, sy);
138}
139
140static void
141weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200142 void *user_data,
143 int32_t width, int32_t height)
Quentin Glidic248dd102016-08-12 10:41:34 +0200144{
Quentin Glidic955cec02016-08-12 10:41:35 +0200145 struct weston_desktop_xwayland_surface *surface = user_data;
146 struct weston_surface *wsurface =
147 weston_desktop_surface_get_surface(surface->surface);
Quentin Glidic248dd102016-08-12 10:41:34 +0200148
Quentin Glidic955cec02016-08-12 10:41:35 +0200149 surface->client_interface->send_configure(wsurface, width, height);
Quentin Glidic248dd102016-08-12 10:41:34 +0200150}
151
152static void
153weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface *dsurface,
154 void *user_data)
155{
Quentin Glidic955cec02016-08-12 10:41:35 +0200156 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200157
158 wl_list_remove(&surface->resource_destroy_listener.link);
159
160 weston_desktop_surface_unset_relative_to(surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200161 if (surface->added)
Quentin Glidic248dd102016-08-12 10:41:34 +0200162 weston_desktop_api_surface_removed(surface->desktop,
163 surface->surface);
Quentin Glidic1714f012016-08-18 16:45:30 +0200164 else if (surface->state == XWAYLAND)
Quentin Glidicf01ecee2016-08-16 10:52:46 +0200165 weston_desktop_surface_unlink_view(surface->view);
Quentin Glidic248dd102016-08-12 10:41:34 +0200166
167 free(surface);
168}
169
170static bool
171weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200172 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200173{
Quentin Glidic955cec02016-08-12 10:41:35 +0200174 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200175
176 return surface->state == MAXIMIZED;
177}
178
179static bool
180weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface *dsurface,
Quentin Glidic955cec02016-08-12 10:41:35 +0200181 void *user_data)
Quentin Glidic248dd102016-08-12 10:41:34 +0200182{
Quentin Glidic955cec02016-08-12 10:41:35 +0200183 struct weston_desktop_xwayland_surface *surface = user_data;
Quentin Glidic248dd102016-08-12 10:41:34 +0200184
185 return surface->state == FULLSCREEN;
186}
187
188static const struct weston_desktop_surface_implementation weston_desktop_xwayland_surface_internal_implementation = {
189 .committed = weston_desktop_xwayland_surface_committed,
190 .set_size = weston_desktop_xwayland_surface_set_size,
191
192 .get_maximized = weston_desktop_xwayland_surface_get_maximized,
193 .get_fullscreen = weston_desktop_xwayland_surface_get_fullscreen,
194
195 .destroy = weston_desktop_xwayland_surface_destroy,
196};
197
198static void
199weston_destop_xwayland_resource_destroyed(struct wl_listener *listener,
200 void *data)
201{
Quentin Glidic955cec02016-08-12 10:41:35 +0200202 struct weston_desktop_xwayland_surface *surface =
Quentin Glidic248dd102016-08-12 10:41:34 +0200203 wl_container_of(listener, surface, resource_destroy_listener);
204
205 weston_desktop_surface_destroy(surface->surface);
206}
207
Quentin Glidic955cec02016-08-12 10:41:35 +0200208static struct weston_desktop_xwayland_surface *
209create_surface(struct weston_desktop_xwayland *xwayland,
210 struct weston_surface *wsurface,
211 const struct weston_xwayland_client_interface *client_interface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200212{
Quentin Glidic955cec02016-08-12 10:41:35 +0200213 struct weston_desktop_xwayland_surface *surface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200214
Quentin Glidic955cec02016-08-12 10:41:35 +0200215 surface = zalloc(sizeof(struct weston_desktop_xwayland_surface));
Quentin Glidic248dd102016-08-12 10:41:34 +0200216 if (surface == NULL)
217 return NULL;
218
219 surface->xwayland = xwayland;
220 surface->desktop = xwayland->desktop;
Quentin Glidic955cec02016-08-12 10:41:35 +0200221 surface->client_interface = client_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200222
223 surface->surface =
224 weston_desktop_surface_create(surface->desktop,
225 xwayland->client, wsurface,
226 &weston_desktop_xwayland_surface_internal_implementation,
227 surface);
228 if (surface->surface == NULL) {
229 free(surface);
230 return NULL;
231 }
232
233 surface->resource_destroy_listener.notify =
234 weston_destop_xwayland_resource_destroyed;
235 wl_resource_add_destroy_listener(wsurface->resource,
236 &surface->resource_destroy_listener);
237
238 return surface;
239}
240
241static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200242set_toplevel(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200243{
244 weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL,
245 0, 0);
246}
247
248static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200249set_parent(struct weston_desktop_xwayland_surface *surface,
250 struct weston_surface *wparent)
Quentin Glidic248dd102016-08-12 10:41:34 +0200251{
252 struct weston_desktop_surface *parent;
253
254 if (!weston_surface_is_desktop_surface(wparent))
255 return;
256
257 parent = weston_surface_get_desktop_surface(wparent);
Quentin Glidic955cec02016-08-12 10:41:35 +0200258 weston_desktop_api_set_parent(surface->desktop, surface->surface, parent);
Quentin Glidic248dd102016-08-12 10:41:34 +0200259}
260
261static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200262set_transient(struct weston_desktop_xwayland_surface *surface,
263 struct weston_surface *wparent, int x, int y)
264{
265 struct weston_desktop_surface *parent;
266
267 if (!weston_surface_is_desktop_surface(wparent))
268 return;
269
270 parent = weston_surface_get_desktop_surface(wparent);
271 weston_desktop_xwayland_surface_change_state(surface, TRANSIENT, parent,
272 x, y);
273}
274
275static void
276set_fullscreen(struct weston_desktop_xwayland_surface *surface,
277 struct weston_output *output)
Quentin Glidic248dd102016-08-12 10:41:34 +0200278{
279 weston_desktop_xwayland_surface_change_state(surface, FULLSCREEN, NULL,
280 0, 0);
281 weston_desktop_api_fullscreen_requested(surface->desktop,
282 surface->surface, true, output);
283}
284
285static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200286set_xwayland(struct weston_desktop_xwayland_surface *surface, int x, int y)
Quentin Glidic248dd102016-08-12 10:41:34 +0200287{
288 weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL,
289 x, y);
290}
291
292static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200293move(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200294 struct weston_pointer *pointer)
295{
296 if (surface->state == TOPLEVEL ||
297 surface->state == MAXIMIZED ||
298 surface->state == FULLSCREEN)
299 weston_desktop_api_move(surface->desktop, surface->surface,
300 pointer->seat, pointer->grab_serial);
301 return 0;
302}
303
304static int
Quentin Glidic955cec02016-08-12 10:41:35 +0200305resize(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200306 struct weston_pointer *pointer, uint32_t edges)
307{
308 if (surface->state == TOPLEVEL ||
309 surface->state == MAXIMIZED ||
310 surface->state == FULLSCREEN)
311 weston_desktop_api_resize(surface->desktop, surface->surface,
312 pointer->seat, pointer->grab_serial,
313 edges);
314 return 0;
315}
316
317static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200318set_title(struct weston_desktop_xwayland_surface *surface, const char *title)
Quentin Glidic248dd102016-08-12 10:41:34 +0200319{
320 weston_desktop_surface_set_title(surface->surface, title);
321}
322
323static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200324set_window_geometry(struct weston_desktop_xwayland_surface *surface,
Quentin Glidic248dd102016-08-12 10:41:34 +0200325 int32_t x, int32_t y, int32_t width, int32_t height)
326{
327 surface->has_next_geometry = true;
328 surface->next_geometry.x = x;
329 surface->next_geometry.y = y;
330 surface->next_geometry.width = width;
331 surface->next_geometry.height = height;
332}
333
334static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200335set_maximized(struct weston_desktop_xwayland_surface *surface)
Quentin Glidic248dd102016-08-12 10:41:34 +0200336{
337 weston_desktop_xwayland_surface_change_state(surface, MAXIMIZED, NULL,
338 0, 0);
339 weston_desktop_api_maximized_requested(surface->desktop,
340 surface->surface, true);
341}
342
343static void
Quentin Glidic955cec02016-08-12 10:41:35 +0200344set_pid(struct weston_desktop_xwayland_surface *surface, pid_t pid)
Quentin Glidic248dd102016-08-12 10:41:34 +0200345{
Quentin Glidic955cec02016-08-12 10:41:35 +0200346 weston_desktop_surface_set_pid(surface->surface, pid);
Quentin Glidic248dd102016-08-12 10:41:34 +0200347}
348
Quentin Glidic955cec02016-08-12 10:41:35 +0200349static const struct weston_desktop_xwayland_interface weston_desktop_xwayland_interface = {
350 .create_surface = create_surface,
351 .set_toplevel = set_toplevel,
352 .set_parent = set_parent,
353 .set_transient = set_transient,
354 .set_fullscreen = set_fullscreen,
355 .set_xwayland = set_xwayland,
356 .move = move,
357 .resize = resize,
358 .set_title = set_title,
359 .set_window_geometry = set_window_geometry,
360 .set_maximized = set_maximized,
361 .set_pid = set_pid,
362};
363
Quentin Glidic248dd102016-08-12 10:41:34 +0200364void
365weston_desktop_xwayland_init(struct weston_desktop *desktop)
366{
367 struct weston_compositor *compositor = weston_desktop_get_compositor(desktop);
368 struct weston_desktop_xwayland *xwayland;
369
370 xwayland = zalloc(sizeof(struct weston_desktop_xwayland));
371 if (xwayland == NULL)
372 return;
373
374 xwayland->desktop = desktop;
375 xwayland->client = weston_desktop_client_create(desktop, NULL, NULL, NULL, NULL, 0, 0);
376
377 weston_layer_init(&xwayland->layer, &compositor->cursor_layer.link);
378
Quentin Glidic955cec02016-08-12 10:41:35 +0200379 compositor->xwayland = xwayland;
380 compositor->xwayland_interface = &weston_desktop_xwayland_interface;
Quentin Glidic248dd102016-08-12 10:41:34 +0200381}