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" |
| 36 | #include "protocol/xdg-shell-unstable-v6-server-protocol.h" |
| 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 |
| 287 | weston_desktop_xdg_toplevel_protocol_set_parent(struct wl_client *wl_client, |
| 288 | struct wl_resource *resource, |
| 289 | struct wl_resource *parent_resource) |
| 290 | { |
| 291 | struct weston_desktop_surface *dsurface = |
| 292 | wl_resource_get_user_data(resource); |
| 293 | struct weston_desktop_xdg_toplevel *toplevel = |
| 294 | weston_desktop_surface_get_implementation_data(dsurface); |
| 295 | struct weston_desktop_surface *parent = NULL; |
| 296 | |
| 297 | if (parent_resource != NULL) |
| 298 | parent = wl_resource_get_user_data(parent_resource); |
| 299 | weston_desktop_api_set_parent(toplevel->base.desktop, dsurface, parent); |
| 300 | } |
| 301 | |
| 302 | static void |
| 303 | weston_desktop_xdg_toplevel_protocol_set_title(struct wl_client *wl_client, |
| 304 | struct wl_resource *resource, |
| 305 | const char *title) |
| 306 | { |
| 307 | struct weston_desktop_surface *toplevel = |
| 308 | wl_resource_get_user_data(resource); |
| 309 | |
| 310 | weston_desktop_surface_set_title(toplevel, title); |
| 311 | } |
| 312 | |
| 313 | static void |
| 314 | weston_desktop_xdg_toplevel_protocol_set_app_id(struct wl_client *wl_client, |
| 315 | struct wl_resource *resource, |
| 316 | const char *app_id) |
| 317 | { |
| 318 | struct weston_desktop_surface *toplevel = |
| 319 | wl_resource_get_user_data(resource); |
| 320 | |
| 321 | weston_desktop_surface_set_app_id(toplevel, app_id); |
| 322 | } |
| 323 | |
| 324 | static void |
| 325 | weston_desktop_xdg_toplevel_protocol_show_window_menu(struct wl_client *wl_client, |
| 326 | struct wl_resource *resource, |
| 327 | struct wl_resource *seat_resource, |
| 328 | uint32_t serial, |
| 329 | int32_t x, int32_t y) |
| 330 | { |
| 331 | struct weston_desktop_surface *dsurface = |
| 332 | wl_resource_get_user_data(resource); |
| 333 | struct weston_seat *seat = |
| 334 | wl_resource_get_user_data(seat_resource); |
| 335 | struct weston_desktop_xdg_toplevel *toplevel = |
| 336 | weston_desktop_surface_get_implementation_data(dsurface); |
| 337 | |
| 338 | weston_desktop_api_show_window_menu(toplevel->base.desktop, |
| 339 | dsurface, seat, x, y); |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | weston_desktop_xdg_toplevel_protocol_move(struct wl_client *wl_client, |
| 344 | struct wl_resource *resource, |
| 345 | struct wl_resource *seat_resource, |
| 346 | uint32_t serial) |
| 347 | { |
| 348 | struct weston_desktop_surface *dsurface = |
| 349 | wl_resource_get_user_data(resource); |
| 350 | struct weston_seat *seat = |
| 351 | wl_resource_get_user_data(seat_resource); |
| 352 | struct weston_desktop_xdg_toplevel *toplevel = |
| 353 | weston_desktop_surface_get_implementation_data(dsurface); |
| 354 | |
| 355 | weston_desktop_api_move(toplevel->base.desktop, dsurface, seat, serial); |
| 356 | } |
| 357 | |
| 358 | static void |
| 359 | weston_desktop_xdg_toplevel_protocol_resize(struct wl_client *wl_client, |
| 360 | struct wl_resource *resource, |
| 361 | struct wl_resource *seat_resource, |
| 362 | uint32_t serial, |
| 363 | enum zxdg_toplevel_v6_resize_edge edges) |
| 364 | { |
| 365 | struct weston_desktop_surface *dsurface = |
| 366 | wl_resource_get_user_data(resource); |
| 367 | struct weston_seat *seat = |
| 368 | wl_resource_get_user_data(seat_resource); |
| 369 | struct weston_desktop_xdg_toplevel *toplevel = |
| 370 | weston_desktop_surface_get_implementation_data(dsurface); |
| 371 | |
| 372 | weston_desktop_api_resize(toplevel->base.desktop, |
| 373 | dsurface, seat, serial, edges); |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | weston_desktop_xdg_toplevel_ack_configure(struct weston_desktop_xdg_toplevel *toplevel) |
| 378 | { |
| 379 | toplevel->next_state = toplevel->requested_state; |
| 380 | } |
| 381 | |
| 382 | static void |
| 383 | weston_desktop_xdg_toplevel_protocol_set_min_size(struct wl_client *wl_client, |
| 384 | struct wl_resource *resource, |
| 385 | int32_t width, int32_t height) |
| 386 | { |
| 387 | struct weston_desktop_surface *dsurface = |
| 388 | wl_resource_get_user_data(resource); |
| 389 | struct weston_desktop_xdg_toplevel *toplevel = |
| 390 | weston_desktop_surface_get_implementation_data(dsurface); |
| 391 | |
| 392 | toplevel->next_min_size.width = width; |
| 393 | toplevel->next_min_size.height = height; |
| 394 | } |
| 395 | |
| 396 | static void |
| 397 | weston_desktop_xdg_toplevel_protocol_set_max_size(struct wl_client *wl_client, |
| 398 | struct wl_resource *resource, |
| 399 | int32_t width, int32_t height) |
| 400 | { |
| 401 | struct weston_desktop_surface *dsurface = |
| 402 | wl_resource_get_user_data(resource); |
| 403 | struct weston_desktop_xdg_toplevel *toplevel = |
| 404 | weston_desktop_surface_get_implementation_data(dsurface); |
| 405 | |
| 406 | toplevel->next_max_size.width = width; |
| 407 | toplevel->next_max_size.height = height; |
| 408 | } |
| 409 | |
| 410 | static void |
| 411 | weston_desktop_xdg_toplevel_protocol_set_maximized(struct wl_client *wl_client, |
| 412 | struct wl_resource *resource) |
| 413 | { |
| 414 | struct weston_desktop_surface *dsurface = |
| 415 | wl_resource_get_user_data(resource); |
| 416 | struct weston_desktop_xdg_toplevel *toplevel = |
| 417 | weston_desktop_surface_get_implementation_data(dsurface); |
| 418 | |
| 419 | weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, true); |
| 420 | } |
| 421 | |
| 422 | static void |
| 423 | weston_desktop_xdg_toplevel_protocol_unset_maximized(struct wl_client *wl_client, |
| 424 | struct wl_resource *resource) |
| 425 | { |
| 426 | struct weston_desktop_surface *dsurface = |
| 427 | wl_resource_get_user_data(resource); |
| 428 | struct weston_desktop_xdg_toplevel *toplevel = |
| 429 | weston_desktop_surface_get_implementation_data(dsurface); |
| 430 | |
| 431 | weston_desktop_api_maximized_requested(toplevel->base.desktop, dsurface, false); |
| 432 | } |
| 433 | |
| 434 | static void |
| 435 | weston_desktop_xdg_toplevel_protocol_set_fullscreen(struct wl_client *wl_client, |
| 436 | struct wl_resource *resource, |
| 437 | struct wl_resource *output_resource) |
| 438 | { |
| 439 | struct weston_desktop_surface *dsurface = |
| 440 | wl_resource_get_user_data(resource); |
| 441 | struct weston_desktop_xdg_toplevel *toplevel = |
| 442 | weston_desktop_surface_get_implementation_data(dsurface); |
| 443 | struct weston_output *output = NULL; |
| 444 | |
| 445 | if (output_resource != NULL) |
| 446 | output = wl_resource_get_user_data(output_resource); |
| 447 | |
| 448 | weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface, |
| 449 | true, output); |
| 450 | } |
| 451 | |
| 452 | static void |
| 453 | weston_desktop_xdg_toplevel_protocol_unset_fullscreen(struct wl_client *wl_client, |
| 454 | struct wl_resource *resource) |
| 455 | { |
| 456 | struct weston_desktop_surface *dsurface = |
| 457 | wl_resource_get_user_data(resource); |
| 458 | struct weston_desktop_xdg_toplevel *toplevel = |
| 459 | weston_desktop_surface_get_implementation_data(dsurface); |
| 460 | |
| 461 | weston_desktop_api_fullscreen_requested(toplevel->base.desktop, dsurface, |
| 462 | false, NULL); |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | weston_desktop_xdg_toplevel_protocol_set_minimized(struct wl_client *wl_client, |
| 467 | struct wl_resource *resource) |
| 468 | { |
| 469 | struct weston_desktop_surface *dsurface = |
| 470 | wl_resource_get_user_data(resource); |
| 471 | struct weston_desktop_xdg_toplevel *toplevel = |
| 472 | weston_desktop_surface_get_implementation_data(dsurface); |
| 473 | |
| 474 | weston_desktop_api_minimized_requested(toplevel->base.desktop, dsurface); |
| 475 | } |
| 476 | |
| 477 | static void |
| 478 | weston_desktop_xdg_toplevel_send_configure(struct weston_desktop_xdg_toplevel *toplevel) |
| 479 | { |
| 480 | uint32_t *s; |
| 481 | struct wl_array states; |
| 482 | |
| 483 | wl_array_init(&states); |
| 484 | if (toplevel->requested_state.maximized) { |
| 485 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 486 | *s = ZXDG_TOPLEVEL_V6_STATE_MAXIMIZED; |
| 487 | } |
| 488 | if (toplevel->requested_state.fullscreen) { |
| 489 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 490 | *s = ZXDG_TOPLEVEL_V6_STATE_FULLSCREEN; |
| 491 | } |
| 492 | if (toplevel->requested_state.resizing) { |
| 493 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 494 | *s = ZXDG_TOPLEVEL_V6_STATE_RESIZING; |
| 495 | } |
| 496 | if (toplevel->requested_state.activated) { |
| 497 | s = wl_array_add(&states, sizeof(uint32_t)); |
| 498 | *s = ZXDG_TOPLEVEL_V6_STATE_ACTIVATED; |
| 499 | } |
| 500 | |
| 501 | zxdg_toplevel_v6_send_configure(toplevel->resource, |
| 502 | toplevel->requested_size.width, |
| 503 | toplevel->requested_size.height, |
| 504 | &states); |
| 505 | |
| 506 | wl_array_release(&states); |
| 507 | }; |
| 508 | |
| 509 | static void |
| 510 | weston_desktop_xdg_toplevel_set_maximized(struct weston_desktop_surface *dsurface, |
| 511 | void *user_data, bool maximized) |
| 512 | { |
| 513 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 514 | |
| 515 | if (toplevel->state.maximized == maximized) |
| 516 | return; |
| 517 | |
| 518 | toplevel->requested_state.maximized = maximized; |
| 519 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 520 | } |
| 521 | |
| 522 | static void |
| 523 | weston_desktop_xdg_toplevel_set_fullscreen(struct weston_desktop_surface *dsurface, |
| 524 | void *user_data, bool fullscreen) |
| 525 | { |
| 526 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 527 | |
| 528 | if (toplevel->state.fullscreen == fullscreen) |
| 529 | return; |
| 530 | |
| 531 | toplevel->requested_state.fullscreen = fullscreen; |
| 532 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 533 | } |
| 534 | |
| 535 | static void |
| 536 | weston_desktop_xdg_toplevel_set_resizing(struct weston_desktop_surface *dsurface, |
| 537 | void *user_data, bool resizing) |
| 538 | { |
| 539 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 540 | |
| 541 | if (toplevel->state.resizing == resizing) |
| 542 | return; |
| 543 | |
| 544 | toplevel->requested_state.resizing = resizing; |
| 545 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 546 | } |
| 547 | |
| 548 | static void |
| 549 | weston_desktop_xdg_toplevel_set_activated(struct weston_desktop_surface *dsurface, |
| 550 | void *user_data, bool activated) |
| 551 | { |
| 552 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 553 | |
| 554 | if (toplevel->state.activated == activated) |
| 555 | return; |
| 556 | |
| 557 | toplevel->requested_state.activated = activated; |
| 558 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 559 | } |
| 560 | |
| 561 | static void |
| 562 | weston_desktop_xdg_toplevel_set_size(struct weston_desktop_surface *dsurface, |
| 563 | void *user_data, |
| 564 | int32_t width, int32_t height) |
| 565 | { |
| 566 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 567 | struct weston_surface *wsurface = |
| 568 | weston_desktop_surface_get_surface(toplevel->base.desktop_surface); |
| 569 | |
| 570 | if (wsurface->width == width && wsurface->height == height) |
| 571 | return; |
| 572 | |
| 573 | toplevel->requested_size.width = width; |
| 574 | toplevel->requested_size.height = height; |
| 575 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 576 | } |
| 577 | |
| 578 | static void |
| 579 | weston_desktop_xdg_toplevel_committed(struct weston_desktop_xdg_toplevel *toplevel, |
| 580 | bool new_buffer, int32_t sx, int32_t sy) |
| 581 | { |
| 582 | struct weston_surface *wsurface = |
| 583 | weston_desktop_surface_get_surface(toplevel->base.desktop_surface); |
| 584 | bool reconfigure = false; |
| 585 | |
| 586 | if (!new_buffer && !toplevel->added) { |
| 587 | weston_desktop_api_surface_added(toplevel->base.desktop, |
| 588 | toplevel->base.desktop_surface); |
| 589 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 590 | toplevel->added = true; |
| 591 | return; |
| 592 | } |
| 593 | if (!new_buffer) |
| 594 | return; |
| 595 | |
| 596 | if (toplevel->next_state.maximized || toplevel->next_state.fullscreen) |
| 597 | reconfigure = |
| 598 | ( ( toplevel->requested_size.width != wsurface->width ) || |
| 599 | ( toplevel->requested_size.height != wsurface->height ) ); |
| 600 | |
| 601 | if (reconfigure) { |
| 602 | weston_desktop_xdg_surface_schedule_configure(&toplevel->base); |
| 603 | } else { |
| 604 | toplevel->state = toplevel->next_state; |
| 605 | toplevel->min_size = toplevel->next_min_size; |
| 606 | toplevel->max_size = toplevel->next_max_size; |
| 607 | |
| 608 | weston_desktop_api_committed(toplevel->base.desktop, |
| 609 | toplevel->base.desktop_surface, |
| 610 | sx, sy); |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | static void |
| 615 | weston_desktop_xdg_toplevel_close(struct weston_desktop_xdg_toplevel *toplevel) |
| 616 | { |
| 617 | zxdg_toplevel_v6_send_close(toplevel->resource); |
| 618 | } |
| 619 | |
| 620 | static bool |
| 621 | weston_desktop_xdg_toplevel_get_maximized(struct weston_desktop_surface *dsurface, |
| 622 | void *user_data) |
| 623 | { |
| 624 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 625 | |
| 626 | return toplevel->state.maximized; |
| 627 | } |
| 628 | |
| 629 | static bool |
| 630 | weston_desktop_xdg_toplevel_get_fullscreen(struct weston_desktop_surface *dsurface, |
| 631 | void *user_data) |
| 632 | { |
| 633 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 634 | |
| 635 | return toplevel->state.fullscreen; |
| 636 | } |
| 637 | |
| 638 | static bool |
| 639 | weston_desktop_xdg_toplevel_get_resizing(struct weston_desktop_surface *dsurface, |
| 640 | void *user_data) |
| 641 | { |
| 642 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 643 | |
| 644 | return toplevel->state.resizing; |
| 645 | } |
| 646 | |
| 647 | static bool |
| 648 | weston_desktop_xdg_toplevel_get_activated(struct weston_desktop_surface *dsurface, |
| 649 | void *user_data) |
| 650 | { |
| 651 | struct weston_desktop_xdg_toplevel *toplevel = user_data; |
| 652 | |
| 653 | return toplevel->state.activated; |
| 654 | } |
| 655 | |
| 656 | static void |
| 657 | weston_desktop_xdg_toplevel_destroy(struct weston_desktop_xdg_toplevel *toplevel) |
| 658 | { |
| 659 | if (toplevel->added) |
| 660 | weston_desktop_api_surface_removed(toplevel->base.desktop, |
| 661 | toplevel->base.desktop_surface); |
| 662 | } |
| 663 | |
| 664 | static void |
| 665 | weston_desktop_xdg_toplevel_resource_destroy(struct wl_resource *resource) |
| 666 | { |
| 667 | struct weston_desktop_surface *dsurface = |
| 668 | wl_resource_get_user_data(resource); |
| 669 | |
| 670 | if (dsurface != NULL) |
| 671 | weston_desktop_surface_resource_destroy(resource); |
| 672 | } |
| 673 | |
| 674 | static const struct zxdg_toplevel_v6_interface weston_desktop_xdg_toplevel_implementation = { |
| 675 | .destroy = weston_desktop_destroy_request, |
| 676 | .set_parent = weston_desktop_xdg_toplevel_protocol_set_parent, |
| 677 | .set_title = weston_desktop_xdg_toplevel_protocol_set_title, |
| 678 | .set_app_id = weston_desktop_xdg_toplevel_protocol_set_app_id, |
| 679 | .show_window_menu = weston_desktop_xdg_toplevel_protocol_show_window_menu, |
| 680 | .move = weston_desktop_xdg_toplevel_protocol_move, |
| 681 | .resize = weston_desktop_xdg_toplevel_protocol_resize, |
| 682 | .set_min_size = weston_desktop_xdg_toplevel_protocol_set_min_size, |
| 683 | .set_max_size = weston_desktop_xdg_toplevel_protocol_set_max_size, |
| 684 | .set_maximized = weston_desktop_xdg_toplevel_protocol_set_maximized, |
| 685 | .unset_maximized = weston_desktop_xdg_toplevel_protocol_unset_maximized, |
| 686 | .set_fullscreen = weston_desktop_xdg_toplevel_protocol_set_fullscreen, |
| 687 | .unset_fullscreen = weston_desktop_xdg_toplevel_protocol_unset_fullscreen, |
| 688 | .set_minimized = weston_desktop_xdg_toplevel_protocol_set_minimized, |
| 689 | }; |
| 690 | |
| 691 | static void |
| 692 | weston_desktop_xdg_popup_protocol_grab(struct wl_client *wl_client, |
| 693 | struct wl_resource *resource, |
| 694 | struct wl_resource *seat_resource, |
| 695 | uint32_t serial) |
| 696 | { |
| 697 | struct weston_desktop_surface *dsurface = |
| 698 | wl_resource_get_user_data(resource); |
| 699 | struct weston_desktop_xdg_popup *popup = |
| 700 | weston_desktop_surface_get_implementation_data(dsurface); |
| 701 | struct weston_seat *wseat = wl_resource_get_user_data(seat_resource); |
| 702 | struct weston_desktop_seat *seat = weston_desktop_seat_from_seat(wseat); |
| 703 | struct weston_desktop_surface *topmost; |
| 704 | bool parent_is_toplevel = |
| 705 | popup->parent->role == WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL; |
| 706 | |
| 707 | if (popup->committed) { |
| 708 | wl_resource_post_error(popup->resource, |
| 709 | ZXDG_POPUP_V6_ERROR_INVALID_GRAB, |
| 710 | "xdg_popup already is mapped"); |
| 711 | return; |
| 712 | } |
| 713 | |
| 714 | topmost = weston_desktop_seat_popup_grab_get_topmost_surface(seat); |
| 715 | if ((topmost == NULL && !parent_is_toplevel) || |
| 716 | (topmost != NULL && topmost != popup->parent->desktop_surface)) { |
| 717 | struct weston_desktop_client *client = |
| 718 | weston_desktop_surface_get_client(dsurface); |
| 719 | struct wl_resource *client_resource = |
| 720 | weston_desktop_client_get_resource(client); |
| 721 | |
| 722 | wl_resource_post_error(client_resource, |
| 723 | ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP, |
| 724 | "xdg_popup was not created on the topmost popup"); |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | popup->seat = seat; |
| 729 | weston_desktop_surface_popup_grab(popup->base.desktop_surface, |
| 730 | popup->seat, serial); |
| 731 | } |
| 732 | |
| 733 | static void |
| 734 | weston_desktop_xdg_popup_send_configure(struct weston_desktop_xdg_popup *popup) |
| 735 | { |
| 736 | zxdg_popup_v6_send_configure(popup->resource, |
| 737 | popup->geometry.x, |
| 738 | popup->geometry.y, |
| 739 | popup->geometry.width, |
| 740 | popup->geometry.height); |
| 741 | } |
| 742 | |
| 743 | static void |
| 744 | weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface, |
| 745 | void *user_data); |
| 746 | |
| 747 | static void |
| 748 | weston_desktop_xdg_popup_committed(struct weston_desktop_xdg_popup *popup) |
| 749 | { |
| 750 | if (!popup->committed) |
| 751 | weston_desktop_xdg_surface_schedule_configure(&popup->base); |
| 752 | popup->committed = true; |
| 753 | weston_desktop_xdg_popup_update_position(popup->base.desktop_surface, |
| 754 | popup); |
| 755 | } |
| 756 | |
| 757 | static void |
| 758 | weston_desktop_xdg_popup_update_position(struct weston_desktop_surface *dsurface, |
| 759 | void *user_data) |
| 760 | { |
| 761 | } |
| 762 | |
| 763 | static void |
| 764 | weston_desktop_xdg_popup_close(struct weston_desktop_xdg_popup *popup) |
| 765 | { |
| 766 | zxdg_popup_v6_send_popup_done(popup->resource); |
| 767 | } |
| 768 | |
| 769 | static void |
| 770 | weston_desktop_xdg_popup_destroy(struct weston_desktop_xdg_popup *popup) |
| 771 | { |
| 772 | struct weston_desktop_surface *topmost; |
| 773 | struct weston_desktop_client *client = |
| 774 | weston_desktop_surface_get_client(popup->base.desktop_surface); |
| 775 | |
| 776 | if (!weston_desktop_surface_get_grab(popup->base.desktop_surface)) |
| 777 | return; |
| 778 | |
| 779 | topmost = weston_desktop_seat_popup_grab_get_topmost_surface(popup->seat); |
| 780 | if (topmost != popup->base.desktop_surface) { |
| 781 | struct wl_resource *client_resource = |
| 782 | weston_desktop_client_get_resource(client); |
| 783 | |
| 784 | wl_resource_post_error(client_resource, |
| 785 | ZXDG_SHELL_V6_ERROR_NOT_THE_TOPMOST_POPUP, |
| 786 | "xdg_popup was destroyed while it was not the topmost popup."); |
| 787 | } |
| 788 | |
| 789 | weston_desktop_surface_popup_ungrab(popup->base.desktop_surface, |
| 790 | popup->seat); |
| 791 | } |
| 792 | |
| 793 | static void |
| 794 | weston_desktop_xdg_popup_resource_destroy(struct wl_resource *resource) |
| 795 | { |
| 796 | struct weston_desktop_surface *dsurface = |
| 797 | wl_resource_get_user_data(resource); |
| 798 | |
| 799 | if (dsurface != NULL) |
| 800 | weston_desktop_surface_resource_destroy(resource); |
| 801 | } |
| 802 | |
| 803 | static const struct zxdg_popup_v6_interface weston_desktop_xdg_popup_implementation = { |
| 804 | .destroy = weston_desktop_destroy_request, |
| 805 | .grab = weston_desktop_xdg_popup_protocol_grab, |
| 806 | }; |
| 807 | |
| 808 | static void |
| 809 | weston_desktop_xdg_surface_send_configure(void *user_data) |
| 810 | { |
| 811 | struct weston_desktop_xdg_surface *surface = user_data; |
| 812 | |
| 813 | surface->configure_idle = NULL; |
| 814 | surface->configure_serial = |
| 815 | wl_display_next_serial(weston_desktop_get_display(surface->desktop)); |
| 816 | |
| 817 | switch (surface->role) { |
| 818 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 819 | assert(0 && "not reached"); |
| 820 | break; |
| 821 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 822 | weston_desktop_xdg_toplevel_send_configure((struct weston_desktop_xdg_toplevel *) surface); |
| 823 | break; |
| 824 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 825 | weston_desktop_xdg_popup_send_configure((struct weston_desktop_xdg_popup *) surface); |
| 826 | break; |
| 827 | } |
| 828 | |
| 829 | zxdg_surface_v6_send_configure(surface->resource, surface->configure_serial); |
| 830 | } |
| 831 | |
| 832 | static void |
| 833 | weston_desktop_xdg_surface_schedule_configure(struct weston_desktop_xdg_surface *surface) |
| 834 | { |
| 835 | struct wl_display *display = weston_desktop_get_display(surface->desktop); |
| 836 | struct wl_event_loop *loop = wl_display_get_event_loop(display); |
| 837 | |
| 838 | if (surface->configure_idle != NULL) |
| 839 | return; |
| 840 | surface->configure_idle = |
| 841 | wl_event_loop_add_idle(loop, |
| 842 | weston_desktop_xdg_surface_send_configure, |
| 843 | surface); |
| 844 | } |
| 845 | |
| 846 | static void |
| 847 | weston_desktop_xdg_surface_protocol_get_toplevel(struct wl_client *wl_client, |
| 848 | struct wl_resource *resource, |
| 849 | uint32_t id) |
| 850 | { |
| 851 | struct weston_desktop_surface *dsurface = |
| 852 | wl_resource_get_user_data(resource); |
| 853 | struct weston_surface *wsurface = |
| 854 | weston_desktop_surface_get_surface(dsurface); |
| 855 | struct weston_desktop_xdg_toplevel *toplevel = |
| 856 | weston_desktop_surface_get_implementation_data(dsurface); |
| 857 | |
| 858 | if (weston_surface_set_role(wsurface, weston_desktop_xdg_toplevel_role, |
| 859 | resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0) |
| 860 | return; |
| 861 | |
| 862 | toplevel->resource = |
| 863 | weston_desktop_surface_add_resource(toplevel->base.desktop_surface, |
| 864 | &zxdg_toplevel_v6_interface, |
| 865 | &weston_desktop_xdg_toplevel_implementation, |
| 866 | id, weston_desktop_xdg_toplevel_resource_destroy); |
| 867 | if (toplevel->resource == NULL) |
| 868 | return; |
| 869 | |
| 870 | toplevel->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL; |
| 871 | } |
| 872 | |
| 873 | static void |
| 874 | weston_desktop_xdg_surface_protocol_get_popup(struct wl_client *wl_client, |
| 875 | struct wl_resource *resource, |
| 876 | uint32_t id, |
| 877 | struct wl_resource *parent_resource, |
| 878 | struct wl_resource *positioner_resource) |
| 879 | { |
| 880 | struct weston_desktop_surface *dsurface = |
| 881 | wl_resource_get_user_data(resource); |
| 882 | struct weston_surface *wsurface = |
| 883 | weston_desktop_surface_get_surface(dsurface); |
| 884 | struct weston_desktop_xdg_popup *popup = |
| 885 | weston_desktop_surface_get_implementation_data(dsurface); |
| 886 | struct weston_desktop_surface *parent_surface = |
| 887 | wl_resource_get_user_data(parent_resource); |
| 888 | struct weston_desktop_xdg_surface *parent = |
| 889 | weston_desktop_surface_get_implementation_data(parent_surface); |
| 890 | struct weston_desktop_xdg_positioner *positioner = |
| 891 | wl_resource_get_user_data(positioner_resource); |
| 892 | |
| 893 | if (weston_surface_set_role(wsurface, weston_desktop_xdg_popup_role, |
| 894 | resource, ZXDG_SHELL_V6_ERROR_ROLE) < 0) |
| 895 | return; |
| 896 | |
| 897 | popup->resource = |
| 898 | weston_desktop_surface_add_resource(popup->base.desktop_surface, |
| 899 | &zxdg_popup_v6_interface, |
| 900 | &weston_desktop_xdg_popup_implementation, |
| 901 | id, weston_desktop_xdg_popup_resource_destroy); |
| 902 | if (popup->resource == NULL) |
| 903 | return; |
| 904 | |
| 905 | popup->base.role = WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP; |
| 906 | popup->parent = parent; |
| 907 | |
| 908 | popup->geometry = |
| 909 | weston_desktop_xdg_positioner_get_geometry(positioner, |
| 910 | dsurface, |
| 911 | parent_surface); |
| 912 | |
| 913 | weston_desktop_surface_set_relative_to(popup->base.desktop_surface, |
| 914 | parent_surface, |
| 915 | popup->geometry.x, |
| 916 | popup->geometry.y, |
| 917 | true); |
| 918 | } |
| 919 | |
| 920 | static bool |
| 921 | weston_desktop_xdg_surface_check_role(struct weston_desktop_xdg_surface *surface) |
| 922 | { |
| 923 | struct weston_surface *wsurface = |
| 924 | weston_desktop_surface_get_surface(surface->desktop_surface); |
| 925 | const char *role; |
| 926 | |
| 927 | role = weston_surface_get_role(wsurface); |
| 928 | if (role != NULL && |
| 929 | (role == weston_desktop_xdg_toplevel_role || |
| 930 | role == weston_desktop_xdg_popup_role)) |
| 931 | return true; |
| 932 | |
| 933 | wl_resource_post_error(surface->resource, |
| 934 | ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED, |
| 935 | "xdg_surface must have a role"); |
| 936 | return false; |
| 937 | } |
| 938 | |
| 939 | static void |
| 940 | weston_desktop_xdg_surface_protocol_set_window_geometry(struct wl_client *wl_client, |
| 941 | struct wl_resource *resource, |
| 942 | int32_t x, int32_t y, |
| 943 | int32_t width, int32_t height) |
| 944 | { |
| 945 | struct weston_desktop_surface *dsurface = |
| 946 | wl_resource_get_user_data(resource); |
| 947 | struct weston_desktop_xdg_surface *surface = |
| 948 | weston_desktop_surface_get_implementation_data(dsurface); |
| 949 | |
| 950 | if (!weston_desktop_xdg_surface_check_role(surface)) |
| 951 | return; |
| 952 | |
| 953 | surface->has_next_geometry = true; |
| 954 | surface->next_geometry.x = x; |
| 955 | surface->next_geometry.y = y; |
| 956 | surface->next_geometry.width = width; |
| 957 | surface->next_geometry.height = height; |
| 958 | } |
| 959 | |
| 960 | static void |
| 961 | weston_desktop_xdg_surface_protocol_ack_configure(struct wl_client *wl_client, |
| 962 | struct wl_resource *resource, |
| 963 | uint32_t serial) |
| 964 | { |
| 965 | struct weston_desktop_surface *dsurface = |
| 966 | wl_resource_get_user_data(resource); |
| 967 | struct weston_desktop_xdg_surface *surface = |
| 968 | weston_desktop_surface_get_implementation_data(dsurface); |
| 969 | |
| 970 | if (!weston_desktop_xdg_surface_check_role(surface)) |
| 971 | return; |
| 972 | |
| 973 | if (surface->configure_serial != serial) |
| 974 | return; |
| 975 | |
| 976 | surface->configured = true; |
| 977 | |
| 978 | switch (surface->role) { |
| 979 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 980 | assert(0 && "not reached"); |
| 981 | break; |
| 982 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 983 | weston_desktop_xdg_toplevel_ack_configure((struct weston_desktop_xdg_toplevel *) surface); |
| 984 | break; |
| 985 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 986 | break; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | static void |
| 991 | weston_desktop_xdg_surface_ping(struct weston_desktop_surface *dsurface, |
| 992 | uint32_t serial, void *user_data) |
| 993 | { |
| 994 | struct weston_desktop_client *client = |
| 995 | weston_desktop_surface_get_client(dsurface); |
| 996 | |
| 997 | zxdg_shell_v6_send_ping(weston_desktop_client_get_resource(client), |
| 998 | serial); |
| 999 | } |
| 1000 | |
| 1001 | static void |
| 1002 | weston_desktop_xdg_surface_committed(struct weston_desktop_surface *dsurface, |
| 1003 | void *user_data, bool new_buffer, |
| 1004 | int32_t sx, int32_t sy) |
| 1005 | { |
| 1006 | struct weston_desktop_xdg_surface *surface = user_data; |
| 1007 | |
| 1008 | if (new_buffer && !surface->configured) { |
| 1009 | wl_resource_post_error(surface->resource, |
| 1010 | ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER, |
| 1011 | "xdg_surface has never been configured"); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | if (surface->has_next_geometry) { |
| 1016 | surface->has_next_geometry = false; |
| 1017 | weston_desktop_surface_set_geometry(surface->desktop_surface, |
| 1018 | surface->next_geometry); |
| 1019 | } |
| 1020 | |
| 1021 | switch (surface->role) { |
| 1022 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 1023 | wl_resource_post_error(surface->resource, |
| 1024 | ZXDG_SURFACE_V6_ERROR_NOT_CONSTRUCTED, |
| 1025 | "xdg_surface must have a role"); |
| 1026 | break; |
| 1027 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 1028 | weston_desktop_xdg_toplevel_committed((struct weston_desktop_xdg_toplevel *) surface, new_buffer, sx, sy); |
| 1029 | break; |
| 1030 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 1031 | weston_desktop_xdg_popup_committed((struct weston_desktop_xdg_popup *) surface); |
| 1032 | break; |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | static void |
| 1037 | weston_desktop_xdg_surface_close(struct weston_desktop_surface *dsurface, |
| 1038 | void *user_data) |
| 1039 | { |
| 1040 | struct weston_desktop_xdg_surface *surface = user_data; |
| 1041 | |
| 1042 | switch (surface->role) { |
| 1043 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 1044 | assert(0 && "not reached"); |
| 1045 | break; |
| 1046 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 1047 | weston_desktop_xdg_toplevel_close((struct weston_desktop_xdg_toplevel *) surface); |
| 1048 | break; |
| 1049 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 1050 | weston_desktop_xdg_popup_close((struct weston_desktop_xdg_popup *) surface); |
| 1051 | break; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | weston_desktop_xdg_surface_destroy(struct weston_desktop_surface *dsurface, |
| 1057 | void *user_data) |
| 1058 | { |
| 1059 | struct weston_desktop_xdg_surface *surface = user_data; |
| 1060 | |
| 1061 | switch (surface->role) { |
| 1062 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_NONE: |
| 1063 | break; |
| 1064 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_TOPLEVEL: |
| 1065 | weston_desktop_xdg_toplevel_destroy((struct weston_desktop_xdg_toplevel *) surface); |
| 1066 | break; |
| 1067 | case WESTON_DESKTOP_XDG_SURFACE_ROLE_POPUP: |
| 1068 | weston_desktop_xdg_popup_destroy((struct weston_desktop_xdg_popup *) surface); |
| 1069 | break; |
| 1070 | } |
| 1071 | |
| 1072 | if (surface->configure_idle != NULL) |
| 1073 | wl_event_source_remove(surface->configure_idle); |
| 1074 | |
| 1075 | free(surface); |
| 1076 | } |
| 1077 | |
| 1078 | static const struct zxdg_surface_v6_interface weston_desktop_xdg_surface_implementation = { |
| 1079 | .destroy = weston_desktop_destroy_request, |
| 1080 | .get_toplevel = weston_desktop_xdg_surface_protocol_get_toplevel, |
| 1081 | .get_popup = weston_desktop_xdg_surface_protocol_get_popup, |
| 1082 | .set_window_geometry = weston_desktop_xdg_surface_protocol_set_window_geometry, |
| 1083 | .ack_configure = weston_desktop_xdg_surface_protocol_ack_configure, |
| 1084 | }; |
| 1085 | |
| 1086 | static const struct weston_desktop_surface_implementation weston_desktop_xdg_surface_internal_implementation = { |
| 1087 | /* These are used for toplevel only */ |
| 1088 | .set_maximized = weston_desktop_xdg_toplevel_set_maximized, |
| 1089 | .set_fullscreen = weston_desktop_xdg_toplevel_set_fullscreen, |
| 1090 | .set_resizing = weston_desktop_xdg_toplevel_set_resizing, |
| 1091 | .set_activated = weston_desktop_xdg_toplevel_set_activated, |
| 1092 | .set_size = weston_desktop_xdg_toplevel_set_size, |
| 1093 | |
| 1094 | .get_maximized = weston_desktop_xdg_toplevel_get_maximized, |
| 1095 | .get_fullscreen = weston_desktop_xdg_toplevel_get_fullscreen, |
| 1096 | .get_resizing = weston_desktop_xdg_toplevel_get_resizing, |
| 1097 | .get_activated = weston_desktop_xdg_toplevel_get_activated, |
| 1098 | |
| 1099 | /* These are used for popup only */ |
| 1100 | .update_position = weston_desktop_xdg_popup_update_position, |
| 1101 | |
| 1102 | /* Common API */ |
| 1103 | .committed = weston_desktop_xdg_surface_committed, |
| 1104 | .ping = weston_desktop_xdg_surface_ping, |
| 1105 | .close = weston_desktop_xdg_surface_close, |
| 1106 | |
| 1107 | .destroy = weston_desktop_xdg_surface_destroy, |
| 1108 | }; |
| 1109 | |
| 1110 | static void |
| 1111 | weston_desktop_xdg_shell_protocol_create_positioner(struct wl_client *wl_client, |
| 1112 | struct wl_resource *resource, |
| 1113 | uint32_t id) |
| 1114 | { |
| 1115 | struct weston_desktop_client *client = |
| 1116 | wl_resource_get_user_data(resource); |
| 1117 | struct weston_desktop_xdg_positioner *positioner; |
| 1118 | |
| 1119 | positioner = zalloc(sizeof(struct weston_desktop_xdg_positioner)); |
| 1120 | if (positioner == NULL) { |
| 1121 | wl_client_post_no_memory(wl_client); |
| 1122 | return; |
| 1123 | } |
| 1124 | |
| 1125 | positioner->client = client; |
| 1126 | positioner->desktop = weston_desktop_client_get_desktop(positioner->client); |
| 1127 | |
| 1128 | positioner->resource = |
| 1129 | wl_resource_create(wl_client, |
| 1130 | &zxdg_positioner_v6_interface, |
| 1131 | wl_resource_get_version(resource), id); |
| 1132 | if (positioner->resource == NULL) { |
| 1133 | wl_client_post_no_memory(wl_client); |
| 1134 | free(positioner); |
| 1135 | return; |
| 1136 | } |
| 1137 | wl_resource_set_implementation(positioner->resource, |
| 1138 | &weston_desktop_xdg_positioner_implementation, |
| 1139 | positioner, weston_desktop_xdg_positioner_destroy); |
| 1140 | } |
| 1141 | |
| 1142 | static void |
| 1143 | weston_desktop_xdg_surface_resource_destroy(struct wl_resource *resource) |
| 1144 | { |
| 1145 | struct weston_desktop_surface *dsurface = |
| 1146 | wl_resource_get_user_data(resource); |
| 1147 | |
| 1148 | if (dsurface != NULL) |
| 1149 | weston_desktop_surface_resource_destroy(resource); |
| 1150 | } |
| 1151 | |
| 1152 | static void |
| 1153 | weston_desktop_xdg_shell_protocol_get_xdg_surface(struct wl_client *wl_client, |
| 1154 | struct wl_resource *resource, |
| 1155 | uint32_t id, |
| 1156 | struct wl_resource *surface_resource) |
| 1157 | { |
| 1158 | struct weston_desktop_client *client = |
| 1159 | wl_resource_get_user_data(resource); |
| 1160 | struct weston_surface *wsurface = |
| 1161 | wl_resource_get_user_data(surface_resource); |
| 1162 | struct weston_desktop_xdg_surface *surface; |
| 1163 | |
| 1164 | surface = zalloc(weston_desktop_surface_role_biggest_size); |
| 1165 | if (surface == NULL) { |
| 1166 | wl_client_post_no_memory(wl_client); |
| 1167 | return; |
| 1168 | } |
| 1169 | |
| 1170 | surface->desktop = weston_desktop_client_get_desktop(client); |
| 1171 | surface->surface = wsurface; |
| 1172 | |
| 1173 | surface->desktop_surface = |
| 1174 | weston_desktop_surface_create(surface->desktop, client, |
| 1175 | surface->surface, |
| 1176 | &weston_desktop_xdg_surface_internal_implementation, |
| 1177 | surface); |
| 1178 | if (surface->desktop_surface == NULL) { |
| 1179 | free(surface); |
| 1180 | return; |
| 1181 | } |
| 1182 | |
| 1183 | surface->resource = |
| 1184 | weston_desktop_surface_add_resource(surface->desktop_surface, |
| 1185 | &zxdg_surface_v6_interface, |
| 1186 | &weston_desktop_xdg_surface_implementation, |
| 1187 | id, weston_desktop_xdg_surface_resource_destroy); |
| 1188 | if (surface->resource == NULL) |
| 1189 | return; |
| 1190 | |
| 1191 | if (wsurface->buffer_ref.buffer != NULL) { |
| 1192 | wl_resource_post_error(surface->resource, |
| 1193 | ZXDG_SURFACE_V6_ERROR_UNCONFIGURED_BUFFER, |
| 1194 | "xdg_surface must not have a buffer at creation"); |
| 1195 | return; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | static void |
| 1200 | weston_desktop_xdg_shell_protocol_pong(struct wl_client *wl_client, |
| 1201 | struct wl_resource *resource, |
| 1202 | uint32_t serial) |
| 1203 | { |
| 1204 | struct weston_desktop_client *client = |
| 1205 | wl_resource_get_user_data(resource); |
| 1206 | |
| 1207 | weston_desktop_client_pong(client, serial); |
| 1208 | } |
| 1209 | |
| 1210 | static const struct zxdg_shell_v6_interface weston_desktop_xdg_shell_implementation = { |
| 1211 | .destroy = weston_desktop_destroy_request, |
| 1212 | .create_positioner = weston_desktop_xdg_shell_protocol_create_positioner, |
| 1213 | .get_xdg_surface = weston_desktop_xdg_shell_protocol_get_xdg_surface, |
| 1214 | .pong = weston_desktop_xdg_shell_protocol_pong, |
| 1215 | }; |
| 1216 | |
| 1217 | static void |
| 1218 | weston_desktop_xdg_shell_bind(struct wl_client *client, void *data, |
| 1219 | uint32_t version, uint32_t id) |
| 1220 | { |
| 1221 | struct weston_desktop *desktop = data; |
| 1222 | |
| 1223 | weston_desktop_client_create(desktop, client, NULL, |
| 1224 | &zxdg_shell_v6_interface, |
| 1225 | &weston_desktop_xdg_shell_implementation, |
| 1226 | version, id); |
| 1227 | } |
| 1228 | |
| 1229 | struct wl_global * |
| 1230 | weston_desktop_xdg_shell_v6_create(struct weston_desktop *desktop, struct wl_display *display) |
| 1231 | { |
| 1232 | return wl_global_create(display, &zxdg_shell_v6_interface, |
| 1233 | WD_XDG_SHELL_PROTOCOL_VERSION, desktop, |
| 1234 | weston_desktop_xdg_shell_bind); |
| 1235 | } |