Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 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 "compositor.h" |
| 34 | #include "zalloc.h" |
| 35 | |
| 36 | #include "libweston-desktop.h" |
| 37 | #include "internal.h" |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 38 | #include "xwayland/xwayland-internal-interface.h" |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 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 | |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 55 | struct weston_desktop_xwayland_surface { |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 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; |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 61 | const struct weston_xwayland_client_interface *client_interface; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 62 | struct weston_geometry next_geometry; |
| 63 | bool has_next_geometry; |
| 64 | bool added; |
| 65 | enum weston_desktop_xwayland_surface_state state; |
| 66 | }; |
| 67 | |
| 68 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 69 | weston_desktop_xwayland_surface_change_state(struct weston_desktop_xwayland_surface *surface, |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 70 | 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 | |
| 78 | if (to_add && surface->added) |
| 79 | return; |
| 80 | |
| 81 | if (surface->state != state) { |
| 82 | if (surface->state == XWAYLAND) { |
Quentin Glidic | f01ecee | 2016-08-16 10:52:46 +0200 | [diff] [blame] | 83 | weston_desktop_surface_unlink_view(surface->view); |
Quentin Glidic | f6636a8 | 2016-08-16 10:55:02 +0200 | [diff] [blame^] | 84 | weston_view_destroy(surface->view); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 85 | surface->view = NULL; |
| 86 | } |
| 87 | |
| 88 | if (to_add) { |
| 89 | weston_desktop_surface_unset_relative_to(surface->surface); |
| 90 | weston_desktop_api_surface_added(surface->desktop, |
| 91 | surface->surface); |
| 92 | } else if (surface->added) { |
| 93 | weston_desktop_api_surface_removed(surface->desktop, |
| 94 | surface->surface); |
| 95 | } |
| 96 | |
| 97 | if (state == XWAYLAND) { |
| 98 | surface->view = |
| 99 | weston_desktop_surface_create_view(surface->surface); |
| 100 | weston_layer_entry_insert(&surface->xwayland->layer.view_list, |
| 101 | &surface->view->layer_link); |
| 102 | weston_view_set_position(surface->view, x, y); |
| 103 | } |
| 104 | |
| 105 | surface->state = state; |
| 106 | surface->added = to_add; |
| 107 | } |
| 108 | |
| 109 | if (parent != NULL) |
| 110 | weston_desktop_surface_set_relative_to(surface->surface, parent, |
| 111 | x, y, false); |
| 112 | } |
| 113 | |
| 114 | static void |
| 115 | weston_desktop_xwayland_surface_committed(struct weston_desktop_surface *dsurface, |
Quentin Glidic | 003da88 | 2016-08-15 12:21:39 +0200 | [diff] [blame] | 116 | void *user_data, |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 117 | int32_t sx, int32_t sy) |
| 118 | { |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 119 | struct weston_desktop_xwayland_surface *surface = user_data; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 120 | |
| 121 | if (surface->has_next_geometry) { |
| 122 | surface->has_next_geometry = false; |
| 123 | weston_desktop_surface_set_geometry(surface->surface, |
| 124 | surface->next_geometry); |
| 125 | } |
| 126 | |
| 127 | if (surface->added) |
| 128 | weston_desktop_api_committed(surface->desktop, surface->surface, |
| 129 | sx, sy); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | weston_desktop_xwayland_surface_set_size(struct weston_desktop_surface *dsurface, |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 134 | void *user_data, |
| 135 | int32_t width, int32_t height) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 136 | { |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 137 | struct weston_desktop_xwayland_surface *surface = user_data; |
| 138 | struct weston_surface *wsurface = |
| 139 | weston_desktop_surface_get_surface(surface->surface); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 140 | |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 141 | surface->client_interface->send_configure(wsurface, width, height); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void |
| 145 | weston_desktop_xwayland_surface_destroy(struct weston_desktop_surface *dsurface, |
| 146 | void *user_data) |
| 147 | { |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 148 | struct weston_desktop_xwayland_surface *surface = user_data; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 149 | |
| 150 | wl_list_remove(&surface->resource_destroy_listener.link); |
| 151 | |
| 152 | weston_desktop_surface_unset_relative_to(surface->surface); |
Quentin Glidic | f6636a8 | 2016-08-16 10:55:02 +0200 | [diff] [blame^] | 153 | if (surface->added) { |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 154 | weston_desktop_api_surface_removed(surface->desktop, |
| 155 | surface->surface); |
Quentin Glidic | f6636a8 | 2016-08-16 10:55:02 +0200 | [diff] [blame^] | 156 | } else if (surface->state == XWAYLAND) { |
Quentin Glidic | f01ecee | 2016-08-16 10:52:46 +0200 | [diff] [blame] | 157 | weston_desktop_surface_unlink_view(surface->view); |
Quentin Glidic | f6636a8 | 2016-08-16 10:55:02 +0200 | [diff] [blame^] | 158 | weston_view_destroy(surface->view); |
| 159 | } |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 160 | |
| 161 | free(surface); |
| 162 | } |
| 163 | |
| 164 | static bool |
| 165 | weston_desktop_xwayland_surface_get_maximized(struct weston_desktop_surface *dsurface, |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 166 | void *user_data) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 167 | { |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 168 | struct weston_desktop_xwayland_surface *surface = user_data; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 169 | |
| 170 | return surface->state == MAXIMIZED; |
| 171 | } |
| 172 | |
| 173 | static bool |
| 174 | weston_desktop_xwayland_surface_get_fullscreen(struct weston_desktop_surface *dsurface, |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 175 | void *user_data) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 176 | { |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 177 | struct weston_desktop_xwayland_surface *surface = user_data; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 178 | |
| 179 | return surface->state == FULLSCREEN; |
| 180 | } |
| 181 | |
| 182 | static 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 | |
| 192 | static void |
| 193 | weston_destop_xwayland_resource_destroyed(struct wl_listener *listener, |
| 194 | void *data) |
| 195 | { |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 196 | struct weston_desktop_xwayland_surface *surface = |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 197 | wl_container_of(listener, surface, resource_destroy_listener); |
| 198 | |
| 199 | weston_desktop_surface_destroy(surface->surface); |
| 200 | } |
| 201 | |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 202 | static struct weston_desktop_xwayland_surface * |
| 203 | create_surface(struct weston_desktop_xwayland *xwayland, |
| 204 | struct weston_surface *wsurface, |
| 205 | const struct weston_xwayland_client_interface *client_interface) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 206 | { |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 207 | struct weston_desktop_xwayland_surface *surface; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 208 | |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 209 | surface = zalloc(sizeof(struct weston_desktop_xwayland_surface)); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 210 | if (surface == NULL) |
| 211 | return NULL; |
| 212 | |
| 213 | surface->xwayland = xwayland; |
| 214 | surface->desktop = xwayland->desktop; |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 215 | surface->client_interface = client_interface; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 216 | |
| 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 | |
| 235 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 236 | set_toplevel(struct weston_desktop_xwayland_surface *surface) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 237 | { |
| 238 | weston_desktop_xwayland_surface_change_state(surface, TOPLEVEL, NULL, |
| 239 | 0, 0); |
| 240 | } |
| 241 | |
| 242 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 243 | set_parent(struct weston_desktop_xwayland_surface *surface, |
| 244 | struct weston_surface *wparent) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 245 | { |
| 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 Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 252 | weston_desktop_api_set_parent(surface->desktop, surface->surface, parent); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 256 | set_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 | |
| 269 | static void |
| 270 | set_fullscreen(struct weston_desktop_xwayland_surface *surface, |
| 271 | struct weston_output *output) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 272 | { |
| 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 | |
| 279 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 280 | set_xwayland(struct weston_desktop_xwayland_surface *surface, int x, int y) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 281 | { |
| 282 | weston_desktop_xwayland_surface_change_state(surface, XWAYLAND, NULL, |
| 283 | x, y); |
| 284 | } |
| 285 | |
| 286 | static int |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 287 | move(struct weston_desktop_xwayland_surface *surface, |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 288 | 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 | |
| 298 | static int |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 299 | resize(struct weston_desktop_xwayland_surface *surface, |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 300 | 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 | |
| 311 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 312 | set_title(struct weston_desktop_xwayland_surface *surface, const char *title) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 313 | { |
| 314 | weston_desktop_surface_set_title(surface->surface, title); |
| 315 | } |
| 316 | |
| 317 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 318 | set_window_geometry(struct weston_desktop_xwayland_surface *surface, |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 319 | 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 | |
| 328 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 329 | set_maximized(struct weston_desktop_xwayland_surface *surface) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 330 | { |
| 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 | |
| 337 | static void |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 338 | set_pid(struct weston_desktop_xwayland_surface *surface, pid_t pid) |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 339 | { |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 340 | weston_desktop_surface_set_pid(surface->surface, pid); |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 341 | } |
| 342 | |
Quentin Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 343 | static 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 Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 358 | void |
| 359 | weston_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 Glidic | 955cec0 | 2016-08-12 10:41:35 +0200 | [diff] [blame] | 373 | compositor->xwayland = xwayland; |
| 374 | compositor->xwayland_interface = &weston_desktop_xwayland_interface; |
Quentin Glidic | 248dd10 | 2016-08-12 10:41:34 +0200 | [diff] [blame] | 375 | } |