Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +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 <stdbool.h> |
| 30 | #include <assert.h> |
| 31 | |
| 32 | #include <wayland-server.h> |
| 33 | |
| 34 | #include "compositor.h" |
| 35 | #include "zalloc.h" |
Daniel Stone | 7dbb0e1 | 2016-11-24 15:30:41 +0000 | [diff] [blame] | 36 | #include "xdg-shell-unstable-v6-server-protocol.h" |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 37 | |
| 38 | #include "libweston-desktop.h" |
| 39 | #include "internal.h" |
| 40 | |
| 41 | #define WD_XDG_SHELL_PROTOCOL_VERSION 1 |
| 42 | |
| 43 | static const char *weston_desktop_xdg_toplevel_role = "xdg_toplevel"; |
| 44 | static const char *weston_desktop_xdg_popup_role = "xdg_popup"; |
| 45 | |
| 46 | struct weston_desktop_xdg_positioner { |
| 47 | struct weston_desktop *desktop; |
| 48 | struct weston_desktop_client *client; |
| 49 | struct wl_resource *resource; |
| 50 | |
| 51 | struct weston_size size; |
| 52 | struct weston_geometry anchor_rect; |
| 53 | enum zxdg_positioner_v6_anchor anchor; |
| 54 | enum zxdg_positioner_v6_gravity gravity; |
| 55 | enum zxdg_positioner_v6_constraint_adjustment constraint_adjustment; |
| 56 | struct weston_position offset; |
| 57 | }; |
| 58 | |
| 59 | enum weston_desktop_xdg_surface_role { |
| 60 | WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE, |
| 61 | WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL, |
| 62 | WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP, |
| 63 | }; |
| 64 | |
| 65 | struct weston_desktop_xdg_surface { |
| 66 | struct wl_resource *resource; |
| 67 | struct weston_desktop *desktop; |
| 68 | struct weston_surface *surface; |
| 69 | struct weston_desktop_surface *desktop_surface; |
| 70 | bool configured; |
| 71 | struct wl_event_source *configure_idle; |
| 72 | uint32_t configure_serial; |
| 73 | |
| 74 | bool has_next_geometry; |
| 75 | struct weston_geometry next_geometry; |
| 76 | |
| 77 | enum weston_desktop_xdg_surface_role role; |
| 78 | }; |
| 79 | |
| 80 | struct weston_desktop_xdg_toplevel { |
| 81 | struct weston_desktop_xdg_surface base; |
| 82 | |
| 83 | struct wl_resource *resource; |
| 84 | bool added; |
| 85 | struct weston_size requested_size; |
| 86 | struct { |
| 87 | bool maximized; |
| 88 | bool fullscreen; |
| 89 | bool resizing; |
| 90 | bool activated; |
| 91 | } requested_state, next_state, state; |
| 92 | struct weston_size |
| 93 | next_max_size, max_size, |
| 94 | next_min_size, min_size; |
| 95 | }; |
| 96 | |
| 97 | struct weston_desktop_xdg_popup { |
| 98 | struct weston_desktop_xdg_surface base; |
| 99 | |
| 100 | struct wl_resource *resource; |
| 101 | bool committed; |
| 102 | struct weston_desktop_xdg_surface *parent; |
| 103 | struct weston_desktop_seat *seat; |
| 104 | struct weston_geometry geometry; |
| 105 | }; |
| 106 | |
| 107 | #define weston_desktop_surface_role_biggest_size (sizeof(struct weston_desktop_xdg_toplevel)) |
| 108 | |
| 109 | |
| 110 | static struct weston_geometry |
| 111 | weston_desktop_xdg_positioner_get_geometry(struct weston_desktop_xdg_positioner *positioner, |
| 112 | struct weston_desktop_surface *dsurface, |
| 113 | struct weston_desktop_surface *parent) |
| 114 | { |
| 115 | struct weston_geometry geometry = { |
| 116 | .x = positioner->offset.x, |
| 117 | .y = positioner->offset.y, |
| 118 | .width = positioner->size.width, |
| 119 | .height = positioner->size.height, |
| 120 | }; |
| 121 | |
| 122 | if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP) |
| 123 | geometry.y += positioner->anchor_rect.y; |
| 124 | else if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM) |
| 125 | geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height; |
| 126 | else |
| 127 | geometry.y += positioner->anchor_rect.y + positioner->anchor_rect.height / 2; |
| 128 | |
| 129 | if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT) |
| 130 | geometry.x += positioner->anchor_rect.x; |
| 131 | else if (positioner->anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT) |
| 132 | geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width; |
| 133 | else |
| 134 | geometry.x += positioner->anchor_rect.x + positioner->anchor_rect.width / 2; |
| 135 | |
| 136 | if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP) |
| 137 | geometry.y -= geometry.height; |
| 138 | else if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM) |
| 139 | geometry.y = geometry.y; |
| 140 | else |
| 141 | geometry.y -= geometry.height / 2; |
| 142 | |
| 143 | if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT) |
| 144 | geometry.x -= geometry.width; |
| 145 | else if (positioner->gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT) |
| 146 | geometry.x = geometry.x; |
| 147 | else |
| 148 | geometry.x -= geometry.width / 2; |
| 149 | |
| 150 | if (positioner->constraint_adjustment == ZXDG_POSITIONER_V6_CONSTRAINT_ADJUSTMENT_NONE) |
| 151 | return geometry; |
| 152 | |
| 153 | /* TODO: add compositor policy configuration and the code here */ |
| 154 | |
| 155 | return geometry; |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | weston_desktop_xdg_positioner_protocol_set_size(struct wl_client *wl_client, |
| 160 | struct wl_resource *resource, |
| 161 | int32_t width, int32_t height) |
| 162 | { |
| 163 | struct weston_desktop_xdg_positioner *positioner = |
| 164 | wl_resource_get_user_data(resource); |
| 165 | |
| 166 | if (width < 1 || height < 1) { |
| 167 | wl_resource_post_error(resource, |
| 168 | ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, |
| 169 | "width and height must be positives and non-zero"); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | positioner->size.width = width; |
| 174 | positioner->size.height = height; |
| 175 | } |
| 176 | |
| 177 | static void |
| 178 | weston_desktop_xdg_positioner_protocol_set_anchor_rect(struct wl_client *wl_client, |
| 179 | struct wl_resource *resource, |
| 180 | int32_t x, int32_t y, |
| 181 | int32_t width, int32_t height) |
| 182 | { |
| 183 | struct weston_desktop_xdg_positioner *positioner = |
| 184 | wl_resource_get_user_data(resource); |
| 185 | |
| 186 | if (width < 1 || height < 1) { |
| 187 | wl_resource_post_error(resource, |
| 188 | ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, |
| 189 | "width and height must be positives and non-zero"); |
| 190 | return; |
| 191 | } |
| 192 | |
| 193 | positioner->anchor_rect.x = x; |
| 194 | positioner->anchor_rect.y = y; |
| 195 | positioner->anchor_rect.width = width; |
| 196 | positioner->anchor_rect.height = height; |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | weston_desktop_xdg_positioner_protocol_set_anchor(struct wl_client *wl_client, |
| 201 | struct wl_resource *resource, |
| 202 | enum zxdg_positioner_v6_anchor anchor) |
| 203 | { |
| 204 | struct weston_desktop_xdg_positioner *positioner = |
| 205 | wl_resource_get_user_data(resource); |
| 206 | |
| 207 | if (((anchor & ZXDG_POSITIONER_V6_ANCHOR_TOP ) && |
| 208 | (anchor & ZXDG_POSITIONER_V6_ANCHOR_BOTTOM)) || |
| 209 | ((anchor & ZXDG_POSITIONER_V6_ANCHOR_LEFT) && |
| 210 | (anchor & ZXDG_POSITIONER_V6_ANCHOR_RIGHT))) { |
| 211 | wl_resource_post_error(resource, |
| 212 | ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, |
| 213 | "same-axis values are not allowed"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | positioner->anchor = anchor; |
| 218 | } |
| 219 | |
| 220 | static void |
| 221 | weston_desktop_xdg_positioner_protocol_set_gravity(struct wl_client *wl_client, |
| 222 | struct wl_resource *resource, |
| 223 | enum zxdg_positioner_v6_gravity gravity) |
| 224 | { |
| 225 | struct weston_desktop_xdg_positioner *positioner = |
| 226 | wl_resource_get_user_data(resource); |
| 227 | |
| 228 | if (((gravity & ZXDG_POSITIONER_V6_GRAVITY_TOP) && |
| 229 | (gravity & ZXDG_POSITIONER_V6_GRAVITY_BOTTOM)) || |
| 230 | ((gravity & ZXDG_POSITIONER_V6_GRAVITY_LEFT) && |
| 231 | (gravity & ZXDG_POSITIONER_V6_GRAVITY_RIGHT))) { |
| 232 | wl_resource_post_error(resource, |
| 233 | ZXDG_POSITIONER_V6_ERROR_INVALID_INPUT, |
| 234 | "same-axis values are not allowed"); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | positioner->gravity = gravity; |
| 239 | } |
| 240 | |
| 241 | static void |
| 242 | weston_desktop_xdg_positioner_protocol_set_constraint_adjustment(struct wl_client *wl_client, |
| 243 | struct wl_resource *resource, |
| 244 | enum zxdg_positioner_v6_constraint_adjustment constraint_adjustment) |
| 245 | { |
| 246 | struct weston_desktop_xdg_positioner *positioner = |
| 247 | wl_resource_get_user_data(resource); |
| 248 | |
| 249 | positioner->constraint_adjustment = constraint_adjustment; |
| 250 | } |
| 251 | |
| 252 | static void |
| 253 | weston_desktop_xdg_positioner_protocol_set_offset(struct wl_client *wl_client, |
| 254 | struct wl_resource *resource, |
| 255 | int32_t x, int32_t y) |
| 256 | { |
| 257 | struct weston_desktop_xdg_positioner *positioner = |
| 258 | wl_resource_get_user_data(resource); |
| 259 | |
| 260 | positioner->offset.x = x; |
| 261 | positioner->offset.y = y; |
| 262 | } |
| 263 | |
| 264 | static void |
| 265 | weston_desktop_xdg_positioner_destroy(struct wl_resource *resource) |
| 266 | { |
| 267 | struct weston_desktop_xdg_positioner *positioner = |
| 268 | wl_resource_get_user_data(resource); |
| 269 | |
| 270 | free(positioner); |
| 271 | } |
| 272 | |
| 273 | static const struct zxdg_positioner_v6_interface weston_desktop_xdg_positioner_implementation = { |
| 274 | .destroy = weston_desktop_destroy_request, |
| 275 | .set_size = weston_desktop_xdg_positioner_protocol_set_size, |
| 276 | .set_anchor_rect = weston_desktop_xdg_positioner_protocol_set_anchor_rect, |
| 277 | .set_anchor = weston_desktop_xdg_positioner_protocol_set_anchor, |
| 278 | .set_gravity = weston_desktop_xdg_positioner_protocol_set_gravity, |
| 279 | .set_constraint_adjustment = weston_desktop_xdg_positioner_protocol_set_constraint_adjustment, |
| 280 | .set_offset = weston_desktop_xdg_positioner_protocol_set_offset, |
| 281 | }; |
| 282 | |
| 283 | static void |
| 284 | weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface); |
| 285 | |
| 286 | static void |
Quentin Glidic | 3d7e607 | 2016-09-11 11:29:23 +0200 | [diff] [blame] | 287 | weston_desktop_xdg_toplevel_ensure_added(struct weston_desktop_xdg_toplevel *toplevel) |
| 288 | { |
| 289 | if (toplevel->added) |
| 290 | return; |
| 291 | |
| 292 | weston_desktop_api_surface_added(toplevel->base.desktop, |
| 293 | toplevel->base.desktop_surface); |
| 294 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 295 | toplevel->added = true; |
| 296 | } |
| 297 | |
| 298 | static void |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 299 | weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client, |
| 300 | struct wl_resource *resource, |
| 301 | struct wl_resource *parent_resource) |
| 302 | { |
| 303 | struct weston_desktop_surface *dsurface = |
| 304 | wl_resource_get_user_data(resource); |
| 305 | struct weston_desktop_xdg_toplevel *toplevel = |
| 306 | weston_desktop_surface_get_implementation_data(dsurface); |
| 307 | struct weston_desktop_surface *parent = NULL; |
| 308 | |
| 309 | if (parent_resource != NULL) |
| 310 | parent = wl_resource_get_user_data(parent_resource); |
Quentin Glidic | 3d7e607 | 2016-09-11 11:29:23 +0200 | [diff] [blame] | 311 | |
| 312 | weston_desktop_xdg_toplevel_ensure_added(toplevel); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 313 | weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent); |
| 314 | } |
| 315 | |
| 316 | static void |
| 317 | weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client, |
| 318 | struct wl_resource *resource, |
| 319 | const char *title) |
| 320 | { |
| 321 | struct weston_desktop_surface *toplevel = |
| 322 | wl_resource_get_user_data(resource); |
| 323 | |
| 324 | weston_desktop_surface_set_title(toplevel, title); |
| 325 | } |
| 326 | |
| 327 | static void |
| 328 | weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client, |
| 329 | struct wl_resource *resource, |
| 330 | const char *app_id) |
| 331 | { |
| 332 | struct weston_desktop_surface *toplevel = |
| 333 | wl_resource_get_user_data(resource); |
| 334 | |
| 335 | weston_desktop_surface_set_app_id(toplevel, app_id); |
| 336 | } |
| 337 | |
| 338 | static void |
| 339 | weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client, |
| 340 | struct wl_resource *resource, |
| 341 | struct wl_resource *seat_resource, |
| 342 | uint32_t serial, |
| 343 | int32_t x, int32_t y) |
| 344 | { |
| 345 | struct weston_desktop_surface *dsurface = |
| 346 | wl_resource_get_user_data(resource); |
| 347 | struct weston_seat *seat = |
| 348 | wl_resource_get_user_data(seat_resource); |
| 349 | struct weston_desktop_xdg_toplevel *toplevel = |
| 350 | weston_desktop_surface_get_implementation_data(dsurface); |
| 351 | |
Quentin Glidic | 0abf890 | 2016-09-11 11:34:47 +0200 | [diff] [blame] | 352 | if (!toplevel->base.configured) { |
| 353 | wl_resource_post_error(toplevel->resource, |
| 354 | ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED, |
| 355 | "Surface has not been configured yet"); |
| 356 | return; |
| 357 | } |
| 358 | |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 359 | weston_desktop_api_show_window_menu(toplevel->base.desktop, |
| 360 | dsurface, seat, x, y); |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client, |
| 365 | struct wl_resource *resource, |
| 366 | struct wl_resource *seat_resource, |
| 367 | uint32_t serial) |
| 368 | { |
| 369 | struct weston_desktop_surface *dsurface = |
| 370 | wl_resource_get_user_data(resource); |
| 371 | struct weston_seat *seat = |
| 372 | wl_resource_get_user_data(seat_resource); |
| 373 | struct weston_desktop_xdg_toplevel *toplevel = |
| 374 | weston_desktop_surface_get_implementation_data(dsurface); |
| 375 | |
Quentin Glidic | 0abf890 | 2016-09-11 11:34:47 +0200 | [diff] [blame] | 376 | if (!toplevel->base.configured) { |
| 377 | wl_resource_post_error(toplevel->resource, |
| 378 | ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED, |
| 379 | "Surface has not been configured yet"); |
| 380 | return; |
| 381 | } |
| 382 | |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 383 | weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial); |
| 384 | } |
| 385 | |
| 386 | static void |
| 387 | weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client, |
| 388 | struct wl_resource *resource, |
| 389 | struct wl_resource *seat_resource, |
| 390 | uint32_t serial, |
| 391 | enum zxdg_toplevel_v6_resize_edge edges) |
| 392 | { |
| 393 | struct weston_desktop_surface *dsurface = |
| 394 | wl_resource_get_user_data(resource); |
| 395 | struct weston_seat *seat = |
| 396 | wl_resource_get_user_data(seat_resource); |
| 397 | struct weston_desktop_xdg_toplevel *toplevel = |
| 398 | weston_desktop_surface_get_implementation_data(dsurface); |
Armin Krezović | 4e2fa0a | 2016-09-10 19:11:21 +0200 | [diff] [blame] | 399 | enum weston_desktop_surface_edge surf_edges = |
| 400 | (enum weston_desktop_surface_edge) edges; |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 401 | |
Quentin Glidic | 0abf890 | 2016-09-11 11:34:47 +0200 | [diff] [blame] | 402 | if (!toplevel->base.configured) { |
| 403 | wl_resource_post_error(toplevel->resource, |
| 404 | ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED, |
| 405 | "Surface has not been configured yet"); |
| 406 | return; |
| 407 | } |
| 408 | |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 409 | weston_desktop_api_resize(toplevel->base.desktop, |
Armin Krezović | 4e2fa0a | 2016-09-10 19:11:21 +0200 | [diff] [blame] | 410 | dsurface, seat, serial, surf_edges); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void |
| 414 | weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel) |
| 415 | { |
| 416 | toplevel->next_state = toplevel->requested_state; |
| 417 | } |
| 418 | |
| 419 | static void |
| 420 | weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client, |
| 421 | struct wl_resource *resource, |
| 422 | int32_t width, int32_t height) |
| 423 | { |
| 424 | struct weston_desktop_surface *dsurface = |
| 425 | wl_resource_get_user_data(resource); |
| 426 | struct weston_desktop_xdg_toplevel *toplevel = |
| 427 | weston_desktop_surface_get_implementation_data(dsurface); |
| 428 | |
| 429 | toplevel->next_min_size.width = width; |
| 430 | toplevel->next_min_size.height = height; |
| 431 | } |
| 432 | |
| 433 | static void |
| 434 | weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client, |
| 435 | struct wl_resource *resource, |
| 436 | int32_t width, int32_t height) |
| 437 | { |
| 438 | struct weston_desktop_surface *dsurface = |
| 439 | wl_resource_get_user_data(resource); |
| 440 | struct weston_desktop_xdg_toplevel *toplevel = |
| 441 | weston_desktop_surface_get_implementation_data(dsurface); |
| 442 | |
| 443 | toplevel->next_max_size.width = width; |
| 444 | toplevel->next_max_size.height = height; |
| 445 | } |
| 446 | |
| 447 | static void |
| 448 | weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client, |
| 449 | struct wl_resource *resource) |
| 450 | { |
| 451 | struct weston_desktop_surface *dsurface = |
| 452 | wl_resource_get_user_data(resource); |
| 453 | struct weston_desktop_xdg_toplevel *toplevel = |
| 454 | weston_desktop_surface_get_implementation_data(dsurface); |
| 455 | |
Quentin Glidic | 3d7e607 | 2016-09-11 11:29:23 +0200 | [diff] [blame] | 456 | weston_desktop_xdg_toplevel_ensure_added(toplevel); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 457 | weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true); |
| 458 | } |
| 459 | |
| 460 | static void |
| 461 | weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client, |
| 462 | struct wl_resource *resource) |
| 463 | { |
| 464 | struct weston_desktop_surface *dsurface = |
| 465 | wl_resource_get_user_data(resource); |
| 466 | struct weston_desktop_xdg_toplevel *toplevel = |
| 467 | weston_desktop_surface_get_implementation_data(dsurface); |
| 468 | |
Quentin Glidic | 3d7e607 | 2016-09-11 11:29:23 +0200 | [diff] [blame] | 469 | weston_desktop_xdg_toplevel_ensure_added(toplevel); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 470 | weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false); |
| 471 | } |
| 472 | |
| 473 | static void |
| 474 | weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client, |
| 475 | struct wl_resource *resource, |
| 476 | struct wl_resource *output_resource) |
| 477 | { |
| 478 | struct weston_desktop_surface *dsurface = |
| 479 | wl_resource_get_user_data(resource); |
| 480 | struct weston_desktop_xdg_toplevel *toplevel = |
| 481 | weston_desktop_surface_get_implementation_data(dsurface); |
| 482 | struct weston_output *output = NULL; |
| 483 | |
| 484 | if (output_resource != NULL) |
| 485 | output = wl_resource_get_user_data(output_resource); |
| 486 | |
Quentin Glidic | 3d7e607 | 2016-09-11 11:29:23 +0200 | [diff] [blame] | 487 | weston_desktop_xdg_toplevel_ensure_added(toplevel); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 488 | weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface, |
| 489 | true, output); |
| 490 | } |
| 491 | |
| 492 | static void |
| 493 | weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client, |
| 494 | struct wl_resource *resource) |
| 495 | { |
| 496 | struct weston_desktop_surface *dsurface = |
| 497 | wl_resource_get_user_data(resource); |
| 498 | struct weston_desktop_xdg_toplevel *toplevel = |
| 499 | weston_desktop_surface_get_implementation_data(dsurface); |
| 500 | |
Quentin Glidic | 3d7e607 | 2016-09-11 11:29:23 +0200 | [diff] [blame] | 501 | weston_desktop_xdg_toplevel_ensure_added(toplevel); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 502 | weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface, |
| 503 | false, NULL); |
| 504 | } |
| 505 | |
| 506 | static void |
| 507 | weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client, |
| 508 | struct wl_resource *resource) |
| 509 | { |
| 510 | struct weston_desktop_surface *dsurface = |
| 511 | wl_resource_get_user_data(resource); |
| 512 | struct weston_desktop_xdg_toplevel *toplevel = |
| 513 | weston_desktop_surface_get_implementation_data(dsurface); |
| 514 | |
Quentin Glidic | 3d7e607 | 2016-09-11 11:29:23 +0200 | [diff] [blame] | 515 | weston_desktop_xdg_toplevel_ensure_added(toplevel); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 516 | weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface); |
| 517 | } |
| 518 | |
| 519 | static void |
| 520 | weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel) |
| 521 | { |
| 522 | uint32_t *s; |
| 523 | struct wl_array states; |
| 524 | |
| 525 | wl_array_init(&states); |
| 526 | if (toplevel->requested_state.maximized) { |
| 527 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 528 | *s = ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED; |
| 529 | } |
| 530 | if (toplevel->requested_state.fullscreen) { |
| 531 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 532 | *s = ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN; |
| 533 | } |
| 534 | if (toplevel->requested_state.resizing) { |
| 535 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 536 | *s = ZXDG_TOPLEVEL_V6_STATE_RESIZING; |
| 537 | } |
| 538 | if (toplevel->requested_state.activated) { |
| 539 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 540 | *s = ZXDG_TOPLEVEL_V6_STATE_ACTIVATED; |
| 541 | } |
| 542 | |
| 543 | zxdg_toplevel_v6_send_configure(toplevel->resource, |
| 544 | toplevel->requested_size.width, |
| 545 | toplevel->requested_size.height, |
| 546 | &states); |
| 547 | |
| 548 | wl_array_release(&states); |
| 549 | }; |
| 550 | |
| 551 | static void |
| 552 | weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface, |
| 553 | void *user_data, bool maximized) |
| 554 | { |
| 555 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 556 | |
| 557 | if (toplevel->state.maximized == maximized) |
| 558 | return; |
| 559 | |
| 560 | toplevel->requested_state.maximized = maximized; |
| 561 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 562 | } |
| 563 | |
| 564 | static void |
| 565 | weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface, |
| 566 | void *user_data, bool fullscreen) |
| 567 | { |
| 568 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 569 | |
| 570 | if (toplevel->state.fullscreen == fullscreen) |
| 571 | return; |
| 572 | |
| 573 | toplevel->requested_state.fullscreen = fullscreen; |
| 574 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 575 | } |
| 576 | |
| 577 | static void |
| 578 | weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface, |
| 579 | void *user_data, bool resizing) |
| 580 | { |
| 581 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 582 | |
| 583 | if (toplevel->state.resizing == resizing) |
| 584 | return; |
| 585 | |
| 586 | toplevel->requested_state.resizing = resizing; |
| 587 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 588 | } |
| 589 | |
| 590 | static void |
| 591 | weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface, |
| 592 | void *user_data, bool activated) |
| 593 | { |
| 594 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 595 | |
| 596 | if (toplevel->state.activated == activated) |
| 597 | return; |
| 598 | |
| 599 | toplevel->requested_state.activated = activated; |
| 600 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 601 | } |
| 602 | |
| 603 | static void |
| 604 | weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface, |
| 605 | void *user_data, |
| 606 | int32_t width, int32_t height) |
| 607 | { |
| 608 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 609 | struct weston_surface *wsurface = |
| 610 | weston_desktop_surface_get_surface(toplevel->base.desktop_surface); |
| 611 | |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 612 | toplevel->requested_size.width = width; |
| 613 | toplevel->requested_size.height = height; |
Quentin Glidic | a56b053 | 2016-09-13 10:05:58 +0200 | [diff] [blame] | 614 | |
| 615 | if ((wsurface->width == width && wsurface->height == height) || |
| 616 | (width == 0 && height == 0)) |
| 617 | return; |
| 618 | |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 619 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 620 | } |
| 621 | |
| 622 | static void |
| 623 | weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel, |
Quentin Glidic | cba26e7 | 2016-08-15 12:20:22 +0200 | [diff] [blame] | 624 | int32_t sx, int32_t sy) |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 625 | { |
| 626 | struct weston_surface *wsurface = |
| 627 | weston_desktop_surface_get_surface(toplevel->base.desktop_surface); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 628 | |
Quentin Glidic | cba26e7 | 2016-08-15 12:20:22 +0200 | [diff] [blame] | 629 | if (!wsurface->buffer_ref.buffer && !toplevel->added) { |
Quentin Glidic | 3d7e607 | 2016-09-11 11:29:23 +0200 | [diff] [blame] | 630 | weston_desktop_xdg_toplevel_ensure_added(toplevel); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 631 | return; |
| 632 | } |
Quentin Glidic | cba26e7 | 2016-08-15 12:20:22 +0200 | [diff] [blame] | 633 | if (!wsurface->buffer_ref.buffer) |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 634 | return; |
| 635 | |
Quentin Glidic | c84423b | 2017-03-10 11:50:41 +0100 | [diff] [blame^] | 636 | if ((toplevel->next_state.maximized || toplevel->next_state.fullscreen) && |
| 637 | (toplevel->requested_size.width != wsurface->width || |
| 638 | toplevel->requested_size.height != wsurface->height)) { |
| 639 | struct weston_desktop_client *client = |
| 640 | weston_desktop_surface_get_client(toplevel->base.desktop_surface); |
| 641 | struct wl_resource *client_resource = |
| 642 | weston_desktop_client_get_resource(client); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 643 | |
Quentin Glidic | c84423b | 2017-03-10 11:50:41 +0100 | [diff] [blame^] | 644 | wl_resource_post_error(client_resource, |
| 645 | ZXDG_SHELL_V6_ERROR_INVALID_SURFACE_STATE, |
| 646 | "xdg_surface buffer does not match the configured state"); |
| 647 | return; |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 648 | } |
Quentin Glidic | c84423b | 2017-03-10 11:50:41 +0100 | [diff] [blame^] | 649 | |
| 650 | toplevel->state = toplevel->next_state; |
| 651 | toplevel->min_size = toplevel->next_min_size; |
| 652 | toplevel->max_size = toplevel->next_max_size; |
| 653 | |
| 654 | weston_desktop_api_committed(toplevel->base.desktop, |
| 655 | toplevel->base.desktop_surface, |
| 656 | sx, sy); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | static void |
| 660 | weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel) |
| 661 | { |
| 662 | zxdg_toplevel_v6_send_close(toplevel->resource); |
| 663 | } |
| 664 | |
| 665 | static bool |
| 666 | weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface, |
| 667 | void *user_data) |
| 668 | { |
| 669 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 670 | |
| 671 | return toplevel->state.maximized; |
| 672 | } |
| 673 | |
| 674 | static bool |
| 675 | weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface, |
| 676 | void *user_data) |
| 677 | { |
| 678 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 679 | |
| 680 | return toplevel->state.fullscreen; |
| 681 | } |
| 682 | |
| 683 | static bool |
| 684 | weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface, |
| 685 | void *user_data) |
| 686 | { |
| 687 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 688 | |
| 689 | return toplevel->state.resizing; |
| 690 | } |
| 691 | |
| 692 | static bool |
| 693 | weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface, |
| 694 | void *user_data) |
| 695 | { |
| 696 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 697 | |
| 698 | return toplevel->state.activated; |
| 699 | } |
| 700 | |
| 701 | static void |
| 702 | weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel) |
| 703 | { |
| 704 | if (toplevel->added) |
| 705 | weston_desktop_api_surface_removed(toplevel->base.desktop, |
| 706 | toplevel->base.desktop_surface); |
| 707 | } |
| 708 | |
| 709 | static void |
| 710 | weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource) |
| 711 | { |
| 712 | struct weston_desktop_surface *dsurface = |
| 713 | wl_resource_get_user_data(resource); |
| 714 | |
| 715 | if (dsurface != NULL) |
| 716 | weston_desktop_surface_resource_destroy(resource); |
| 717 | } |
| 718 | |
| 719 | static const struct zxdg_toplevel_v6_interface weston_desktop_xdg_toplevel_implementation = { |
| 720 | .destroy = weston_desktop_destroy_request, |
| 721 | .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent, |
| 722 | .set_title = weston_desktop_xdg_toplevel_protocol_set_title, |
| 723 | .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id, |
| 724 | .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu, |
| 725 | .move = weston_desktop_xdg_toplevel_protocol_move, |
| 726 | .resize = weston_desktop_xdg_toplevel_protocol_resize, |
| 727 | .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size, |
| 728 | .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size, |
| 729 | .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized, |
| 730 | .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized, |
| 731 | .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen, |
| 732 | .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen, |
| 733 | .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized, |
| 734 | }; |
| 735 | |
| 736 | static void |
| 737 | weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client, |
| 738 | struct wl_resource *resource, |
| 739 | struct wl_resource *seat_resource, |
| 740 | uint32_t serial) |
| 741 | { |
| 742 | struct weston_desktop_surface *dsurface = |
| 743 | wl_resource_get_user_data(resource); |
| 744 | struct weston_desktop_xdg_popup *popup = |
| 745 | weston_desktop_surface_get_implementation_data(dsurface); |
| 746 | struct weston_seat *wseat = wl_resource_get_user_data(seat_resource); |
| 747 | struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat); |
| 748 | struct weston_desktop_surface *topmost; |
| 749 | bool parent_is_toplevel = |
| 750 | popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL; |
| 751 | |
| 752 | if (popup->committed) { |
| 753 | wl_resource_post_error(popup->resource, |
| 754 | ZXDG_POPUP_V6_ERROR_INVALID_GRAB, |
| 755 | "xdg_popup already is mapped"); |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat); |
| 760 | if ((topmost == NULL && !parent_is_toplevel) || |
| 761 | (topmost != NULL && topmost != popup->parent->desktop_surface)) { |
| 762 | struct weston_desktop_client *client = |
| 763 | weston_desktop_surface_get_client(dsurface); |
| 764 | struct wl_resource *client_resource = |
| 765 | weston_desktop_client_get_resource(client); |
| 766 | |
| 767 | wl_resource_post_error(client_resource, |
| 768 | ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP, |
| 769 | "xdg_popup was not created on the topmost popup"); |
| 770 | return; |
| 771 | } |
| 772 | |
| 773 | popup->seat = seat; |
| 774 | weston_desktop_surface_popup_grab(popup->base.desktop_surface, |
| 775 | popup->seat, serial); |
| 776 | } |
| 777 | |
| 778 | static void |
| 779 | weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup) |
| 780 | { |
| 781 | zxdg_popup_v6_send_configure(popup->resource, |
| 782 | popup->geometry.x, |
| 783 | popup->geometry.y, |
| 784 | popup->geometry.width, |
| 785 | popup->geometry.height); |
| 786 | } |
| 787 | |
| 788 | static void |
| 789 | weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface, |
| 790 | void *user_data); |
| 791 | |
| 792 | static void |
| 793 | weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup) |
| 794 | { |
| 795 | if (!popup->committed) |
| 796 | weston_desktop_xdg_surface_schedule_configure(&popup->base); |
| 797 | popup->committed = true; |
| 798 | weston_desktop_xdg_popup_update_position(popup->base.desktop_surface, |
| 799 | popup); |
| 800 | } |
| 801 | |
| 802 | static void |
| 803 | weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface, |
| 804 | void *user_data) |
| 805 | { |
| 806 | } |
| 807 | |
| 808 | static void |
| 809 | weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup) |
| 810 | { |
| 811 | zxdg_popup_v6_send_popup_done(popup->resource); |
| 812 | } |
| 813 | |
| 814 | static void |
| 815 | weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup) |
| 816 | { |
| 817 | struct weston_desktop_surface *topmost; |
| 818 | struct weston_desktop_client *client = |
| 819 | weston_desktop_surface_get_client(popup->base.desktop_surface); |
| 820 | |
| 821 | if (!weston_desktop_surface_get_grab(popup->base.desktop_surface)) |
| 822 | return; |
| 823 | |
| 824 | topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat); |
| 825 | if (topmost != popup->base.desktop_surface) { |
| 826 | struct wl_resource *client_resource = |
| 827 | weston_desktop_client_get_resource(client); |
| 828 | |
| 829 | wl_resource_post_error(client_resource, |
| 830 | ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP, |
| 831 | "xdg_popup was destroyed while it was not the topmost popup."); |
| 832 | } |
| 833 | |
| 834 | weston_desktop_surface_popup_ungrab(popup->base.desktop_surface, |
| 835 | popup->seat); |
| 836 | } |
| 837 | |
| 838 | static void |
| 839 | weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource) |
| 840 | { |
| 841 | struct weston_desktop_surface *dsurface = |
| 842 | wl_resource_get_user_data(resource); |
| 843 | |
| 844 | if (dsurface != NULL) |
| 845 | weston_desktop_surface_resource_destroy(resource); |
| 846 | } |
| 847 | |
| 848 | static const struct zxdg_popup_v6_interface weston_desktop_xdg_popup_implementation = { |
| 849 | .destroy = weston_desktop_destroy_request, |
| 850 | .grab = weston_desktop_xdg_popup_protocol_grab, |
| 851 | }; |
| 852 | |
| 853 | static void |
| 854 | weston_desktop_xdg_surface_send_configure(void *user_data) |
| 855 | { |
| 856 | struct weston_desktop_xdg_surface *surface = user_data; |
| 857 | |
| 858 | surface->configure_idle = NULL; |
| 859 | surface->configure_serial = |
| 860 | wl_display_next_serial(weston_desktop_get_display(surface->desktop)); |
| 861 | |
| 862 | switch (surface->role) { |
| 863 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 864 | assert(0 && "not reached"); |
| 865 | break; |
| 866 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 867 | weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface); |
| 868 | break; |
| 869 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 870 | weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface); |
| 871 | break; |
| 872 | } |
| 873 | |
| 874 | zxdg_surface_v6_send_configure(surface->resource, surface->configure_serial); |
| 875 | } |
| 876 | |
| 877 | static void |
| 878 | weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface) |
| 879 | { |
| 880 | struct wl_display *display = weston_desktop_get_display(surface->desktop); |
| 881 | struct wl_event_loop *loop = wl_display_get_event_loop(display); |
| 882 | |
| 883 | if (surface->configure_idle != NULL) |
| 884 | return; |
| 885 | surface->configure_idle = |
| 886 | wl_event_loop_add_idle(loop, |
| 887 | weston_desktop_xdg_surface_send_configure, |
| 888 | surface); |
| 889 | } |
| 890 | |
| 891 | static void |
| 892 | weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client, |
| 893 | struct wl_resource *resource, |
| 894 | uint32_t id) |
| 895 | { |
| 896 | struct weston_desktop_surface *dsurface = |
| 897 | wl_resource_get_user_data(resource); |
| 898 | struct weston_surface *wsurface = |
| 899 | weston_desktop_surface_get_surface(dsurface); |
| 900 | struct weston_desktop_xdg_toplevel *toplevel = |
| 901 | weston_desktop_surface_get_implementation_data(dsurface); |
| 902 | |
| 903 | if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role, |
| 904 | resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0) |
| 905 | return; |
| 906 | |
| 907 | toplevel->resource = |
| 908 | weston_desktop_surface_add_resource(toplevel->base.desktop_surface, |
| 909 | &zxdg_toplevel_v6_interface, |
| 910 | &weston_desktop_xdg_toplevel_implementation, |
| 911 | id, weston_desktop_xdg_toplevel_resource_destroy); |
| 912 | if (toplevel->resource == NULL) |
| 913 | return; |
| 914 | |
| 915 | toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL; |
| 916 | } |
| 917 | |
| 918 | static void |
| 919 | weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client, |
| 920 | struct wl_resource *resource, |
| 921 | uint32_t id, |
| 922 | struct wl_resource *parent_resource, |
| 923 | struct wl_resource *positioner_resource) |
| 924 | { |
| 925 | struct weston_desktop_surface *dsurface = |
| 926 | wl_resource_get_user_data(resource); |
| 927 | struct weston_surface *wsurface = |
| 928 | weston_desktop_surface_get_surface(dsurface); |
| 929 | struct weston_desktop_xdg_popup *popup = |
| 930 | weston_desktop_surface_get_implementation_data(dsurface); |
| 931 | struct weston_desktop_surface *parent_surface = |
| 932 | wl_resource_get_user_data(parent_resource); |
| 933 | struct weston_desktop_xdg_surface *parent = |
| 934 | weston_desktop_surface_get_implementation_data(parent_surface); |
| 935 | struct weston_desktop_xdg_positioner *positioner = |
| 936 | wl_resource_get_user_data(positioner_resource); |
| 937 | |
Sjoerd Simons | be8a6d3 | 2016-09-23 09:31:23 +0200 | [diff] [blame] | 938 | /* Checking whether the size and anchor rect both have a positive size |
| 939 | * is enough to verify both have been correctly set */ |
| 940 | if (positioner->size.width == 0 || positioner->anchor_rect.width == 0) { |
| 941 | wl_resource_post_error(resource, |
| 942 | ZXDG_SHELL_V6_ERROR_INVALID_POSITIONER, |
| 943 | "positioner object is not complete"); |
| 944 | return; |
| 945 | } |
| 946 | |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 947 | if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role, |
| 948 | resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0) |
| 949 | return; |
| 950 | |
| 951 | popup->resource = |
| 952 | weston_desktop_surface_add_resource(popup->base.desktop_surface, |
| 953 | &zxdg_popup_v6_interface, |
| 954 | &weston_desktop_xdg_popup_implementation, |
| 955 | id, weston_desktop_xdg_popup_resource_destroy); |
| 956 | if (popup->resource == NULL) |
| 957 | return; |
| 958 | |
| 959 | popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP; |
| 960 | popup->parent = parent; |
| 961 | |
| 962 | popup->geometry = |
| 963 | weston_desktop_xdg_positioner_get_geometry(positioner, |
| 964 | dsurface, |
| 965 | parent_surface); |
| 966 | |
| 967 | weston_desktop_surface_set_relative_to(popup->base.desktop_surface, |
| 968 | parent_surface, |
| 969 | popup->geometry.x, |
| 970 | popup->geometry.y, |
| 971 | true); |
| 972 | } |
| 973 | |
| 974 | static bool |
| 975 | weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface) |
| 976 | { |
| 977 | struct weston_surface *wsurface = |
| 978 | weston_desktop_surface_get_surface(surface->desktop_surface); |
| 979 | const char *role; |
| 980 | |
| 981 | role = weston_surface_get_role(wsurface); |
| 982 | if (role != NULL && |
| 983 | (role == weston_desktop_xdg_toplevel_role || |
| 984 | role == weston_desktop_xdg_popup_role)) |
| 985 | return true; |
| 986 | |
| 987 | wl_resource_post_error(surface->resource, |
| 988 | ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED, |
| 989 | "xdg_surface must have a role"); |
| 990 | return false; |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client, |
| 995 | struct wl_resource *resource, |
| 996 | int32_t x, int32_t y, |
| 997 | int32_t width, int32_t height) |
| 998 | { |
| 999 | struct weston_desktop_surface *dsurface = |
| 1000 | wl_resource_get_user_data(resource); |
| 1001 | struct weston_desktop_xdg_surface *surface = |
| 1002 | weston_desktop_surface_get_implementation_data(dsurface); |
| 1003 | |
| 1004 | if (!weston_desktop_xdg_surface_check_role(surface)) |
| 1005 | return; |
| 1006 | |
| 1007 | surface->has_next_geometry = true; |
| 1008 | surface->next_geometry.x = x; |
| 1009 | surface->next_geometry.y = y; |
| 1010 | surface->next_geometry.width = width; |
| 1011 | surface->next_geometry.height = height; |
| 1012 | } |
| 1013 | |
| 1014 | static void |
| 1015 | weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client, |
| 1016 | struct wl_resource *resource, |
| 1017 | uint32_t serial) |
| 1018 | { |
| 1019 | struct weston_desktop_surface *dsurface = |
| 1020 | wl_resource_get_user_data(resource); |
| 1021 | struct weston_desktop_xdg_surface *surface = |
| 1022 | weston_desktop_surface_get_implementation_data(dsurface); |
| 1023 | |
| 1024 | if (!weston_desktop_xdg_surface_check_role(surface)) |
| 1025 | return; |
| 1026 | |
| 1027 | if (surface->configure_serial != serial) |
| 1028 | return; |
| 1029 | |
| 1030 | surface->configured = true; |
| 1031 | |
| 1032 | switch (surface->role) { |
| 1033 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 1034 | assert(0 && "not reached"); |
| 1035 | break; |
| 1036 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 1037 | weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface); |
| 1038 | break; |
| 1039 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 1040 | break; |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | static void |
| 1045 | weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface, |
| 1046 | uint32_t serial, void *user_data) |
| 1047 | { |
| 1048 | struct weston_desktop_client *client = |
| 1049 | weston_desktop_surface_get_client(dsurface); |
| 1050 | |
| 1051 | zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client), |
| 1052 | serial); |
| 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface, |
Quentin Glidic | 003da88 | 2016-08-15 12:21:39 +0200 | [diff] [blame] | 1057 | void *user_data, |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 1058 | int32_t sx, int32_t sy) |
| 1059 | { |
| 1060 | struct weston_desktop_xdg_surface *surface = user_data; |
Quentin Glidic | cba26e7 | 2016-08-15 12:20:22 +0200 | [diff] [blame] | 1061 | struct weston_surface *wsurface = |
| 1062 | weston_desktop_surface_get_surface (dsurface); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 1063 | |
Quentin Glidic | cba26e7 | 2016-08-15 12:20:22 +0200 | [diff] [blame] | 1064 | if (wsurface->buffer_ref.buffer && !surface->configured) { |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 1065 | wl_resource_post_error(surface->resource, |
| 1066 | ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER, |
| 1067 | "xdg_surface has never been configured"); |
| 1068 | return; |
| 1069 | } |
| 1070 | |
| 1071 | if (surface->has_next_geometry) { |
| 1072 | surface->has_next_geometry = false; |
| 1073 | weston_desktop_surface_set_geometry(surface->desktop_surface, |
| 1074 | surface->next_geometry); |
| 1075 | } |
| 1076 | |
| 1077 | switch (surface->role) { |
| 1078 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 1079 | wl_resource_post_error(surface->resource, |
| 1080 | ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED, |
| 1081 | "xdg_surface must have a role"); |
| 1082 | break; |
| 1083 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
Quentin Glidic | cba26e7 | 2016-08-15 12:20:22 +0200 | [diff] [blame] | 1084 | weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, sx, sy); |
Quentin Glidic | 9c5dd7e | 2016-08-12 10:41:37 +0200 | [diff] [blame] | 1085 | break; |
| 1086 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 1087 | weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface); |
| 1088 | break; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | static void |
| 1093 | weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface, |
| 1094 | void *user_data) |
| 1095 | { |
| 1096 | struct weston_desktop_xdg_surface *surface = user_data; |
| 1097 | |
| 1098 | switch (surface->role) { |
| 1099 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 1100 | assert(0 && "not reached"); |
| 1101 | break; |
| 1102 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 1103 | weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface); |
| 1104 | break; |
| 1105 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 1106 | weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface); |
| 1107 | break; |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | static void |
| 1112 | weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface, |
| 1113 | void *user_data) |
| 1114 | { |
| 1115 | struct weston_desktop_xdg_surface *surface = user_data; |
| 1116 | |
| 1117 | switch (surface->role) { |
| 1118 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 1119 | break; |
| 1120 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 1121 | weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface); |
| 1122 | break; |
| 1123 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 1124 | weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface); |
| 1125 | break; |
| 1126 | } |
| 1127 | |
| 1128 | if (surface->configure_idle != NULL) |
| 1129 | wl_event_source_remove(surface->configure_idle); |
| 1130 | |
| 1131 | free(surface); |
| 1132 | } |
| 1133 | |
| 1134 | static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = { |
| 1135 | .destroy = weston_desktop_destroy_request, |
| 1136 | .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel, |
| 1137 | .get_popup = weston_desktop_xdg_surface_protocol_get_popup, |
| 1138 | .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry, |
| 1139 | .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure, |
| 1140 | }; |
| 1141 | |
| 1142 | static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = { |
| 1143 | /* These are used for toplevel only */ |
| 1144 | .set_maximized = weston_desktop_xdg_toplevel_set_maximized, |
| 1145 | .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen, |
| 1146 | .set_resizing = weston_desktop_xdg_toplevel_set_resizing, |
| 1147 | .set_activated = weston_desktop_xdg_toplevel_set_activated, |
| 1148 | .set_size = weston_desktop_xdg_toplevel_set_size, |
| 1149 | |
| 1150 | .get_maximized = weston_desktop_xdg_toplevel_get_maximized, |
| 1151 | .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen, |
| 1152 | .get_resizing = weston_desktop_xdg_toplevel_get_resizing, |
| 1153 | .get_activated = weston_desktop_xdg_toplevel_get_activated, |
| 1154 | |
| 1155 | /* These are used for popup only */ |
| 1156 | .update_position = weston_desktop_xdg_popup_update_position, |
| 1157 | |
| 1158 | /* Common API */ |
| 1159 | .committed = weston_desktop_xdg_surface_committed, |
| 1160 | .ping = weston_desktop_xdg_surface_ping, |
| 1161 | .close = weston_desktop_xdg_surface_close, |
| 1162 | |
| 1163 | .destroy = weston_desktop_xdg_surface_destroy, |
| 1164 | }; |
| 1165 | |
| 1166 | static void |
| 1167 | weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client, |
| 1168 | struct wl_resource *resource, |
| 1169 | uint32_t id) |
| 1170 | { |
| 1171 | struct weston_desktop_client *client = |
| 1172 | wl_resource_get_user_data(resource); |
| 1173 | struct weston_desktop_xdg_positioner *positioner; |
| 1174 | |
| 1175 | positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner)); |
| 1176 | if (positioner == NULL) { |
| 1177 | wl_client_post_no_memory(wl_client); |
| 1178 | return; |
| 1179 | } |
| 1180 | |
| 1181 | positioner->client = client; |
| 1182 | positioner->desktop = weston_desktop_client_get_desktop(positioner->client); |
| 1183 | |
| 1184 | positioner->resource = |
| 1185 | wl_resource_create(wl_client, |
| 1186 | &zxdg_positioner_v6_interface, |
| 1187 | wl_resource_get_version(resource), id); |
| 1188 | if (positioner->resource == NULL) { |
| 1189 | wl_client_post_no_memory(wl_client); |
| 1190 | free(positioner); |
| 1191 | return; |
| 1192 | } |
| 1193 | wl_resource_set_implementation(positioner->resource, |
| 1194 | &weston_desktop_xdg_positioner_implementation, |
| 1195 | positioner, weston_desktop_xdg_positioner_destroy); |
| 1196 | } |
| 1197 | |
| 1198 | static void |
| 1199 | weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource) |
| 1200 | { |
| 1201 | struct weston_desktop_surface *dsurface = |
| 1202 | wl_resource_get_user_data(resource); |
| 1203 | |
| 1204 | if (dsurface != NULL) |
| 1205 | weston_desktop_surface_resource_destroy(resource); |
| 1206 | } |
| 1207 | |
| 1208 | static void |
| 1209 | weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client, |
| 1210 | struct wl_resource *resource, |
| 1211 | uint32_t id, |
| 1212 | struct wl_resource *surface_resource) |
| 1213 | { |
| 1214 | struct weston_desktop_client *client = |
| 1215 | wl_resource_get_user_data(resource); |
| 1216 | struct weston_surface *wsurface = |
| 1217 | wl_resource_get_user_data(surface_resource); |
| 1218 | struct weston_desktop_xdg_surface *surface; |
| 1219 | |
| 1220 | surface = zalloc(weston_desktop_surface_role_biggest_size); |
| 1221 | if (surface == NULL) { |
| 1222 | wl_client_post_no_memory(wl_client); |
| 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | surface->desktop = weston_desktop_client_get_desktop(client); |
| 1227 | surface->surface = wsurface; |
| 1228 | |
| 1229 | surface->desktop_surface = |
| 1230 | weston_desktop_surface_create(surface->desktop, client, |
| 1231 | surface->surface, |
| 1232 | &weston_desktop_xdg_surface_internal_implementation, |
| 1233 | surface); |
| 1234 | if (surface->desktop_surface == NULL) { |
| 1235 | free(surface); |
| 1236 | return; |
| 1237 | } |
| 1238 | |
| 1239 | surface->resource = |
| 1240 | weston_desktop_surface_add_resource(surface->desktop_surface, |
| 1241 | &zxdg_surface_v6_interface, |
| 1242 | &weston_desktop_xdg_surface_implementation, |
| 1243 | id, weston_desktop_xdg_surface_resource_destroy); |
| 1244 | if (surface->resource == NULL) |
| 1245 | return; |
| 1246 | |
| 1247 | if (wsurface->buffer_ref.buffer != NULL) { |
| 1248 | wl_resource_post_error(surface->resource, |
| 1249 | ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER, |
| 1250 | "xdg_surface must not have a buffer at creation"); |
| 1251 | return; |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | static void |
| 1256 | weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client, |
| 1257 | struct wl_resource *resource, |
| 1258 | uint32_t serial) |
| 1259 | { |
| 1260 | struct weston_desktop_client *client = |
| 1261 | wl_resource_get_user_data(resource); |
| 1262 | |
| 1263 | weston_desktop_client_pong(client, serial); |
| 1264 | } |
| 1265 | |
| 1266 | static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = { |
| 1267 | .destroy = weston_desktop_destroy_request, |
| 1268 | .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner, |
| 1269 | .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface, |
| 1270 | .pong = weston_desktop_xdg_shell_protocol_pong, |
| 1271 | }; |
| 1272 | |
| 1273 | static void |
| 1274 | weston_desktop_xdg_shell_bind(struct wl_client *client, void *data, |
| 1275 | uint32_t version, uint32_t id) |
| 1276 | { |
| 1277 | struct weston_desktop *desktop = data; |
| 1278 | |
| 1279 | weston_desktop_client_create(desktop, client, NULL, |
| 1280 | &zxdg_shell_v6_interface, |
| 1281 | &weston_desktop_xdg_shell_implementation, |
| 1282 | version, id); |
| 1283 | } |
| 1284 | |
| 1285 | struct wl_global * |
| 1286 | weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display) |
| 1287 | { |
| 1288 | return wl_global_create(display, &zxdg_shell_v6_interface, |
| 1289 | WD_XDG_SHELL_PROTOCOL_VERSION, desktop, |
| 1290 | weston_desktop_xdg_shell_bind); |
| 1291 | } |