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