xdg_shell.c (30298B)
1 /* swc: libswc/xdg_shell.c 2 * 3 * Copyright (c) 2014, 2018 Michael Forney 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 */ 23 24 #include "xdg_shell.h" 25 #include "compositor.h" 26 #include "internal.h" 27 #include "pointer.h" 28 #include "screen.h" 29 #include "seat.h" 30 #include "surface.h" 31 #include "util.h" 32 #include "window.h" 33 34 #include "xdg-shell-server-protocol.h" 35 #include <assert.h> 36 #include <stdlib.h> 37 #include <wayland-server.h> 38 39 struct xdg_surface { 40 struct wl_resource *resource, *role; 41 struct surface *surface; 42 struct wl_listener surface_destroy_listener, role_destroy_listener; 43 uint32_t configure_serial; 44 }; 45 46 struct xdg_positioner { 47 int32_t width, height; 48 int32_t anchor_x, anchor_y; 49 int32_t anchor_width, anchor_height; 50 enum xdg_positioner_anchor anchor; 51 enum xdg_positioner_gravity gravity; 52 enum xdg_positioner_constraint_adjustment constraint; 53 int32_t offset_x, offset_y; 54 }; 55 56 struct xdg_toplevel { 57 struct window window; 58 struct wl_resource *resource; 59 struct wl_array states; 60 struct xdg_surface *xdg_surface; 61 }; 62 63 struct xdg_popup { 64 struct wl_resource *resource; 65 struct xdg_surface *xdg_surface; 66 struct xdg_positioner positioner; 67 struct compositor_view *view; 68 struct wl_list grab_link; /* in grabs while it holds a grab */ 69 bool grabbing; 70 }; 71 72 /* popups holding a grab, newest last. a press elsewhere dismisses them newest first */ 73 static struct wl_list grabs = {&grabs, &grabs}; 74 75 static bool popup_grab_button(struct pointer_handler *handler, uint32_t time, 76 struct button *button, uint32_t state); 77 78 static struct pointer_handler popup_grab_handler = { 79 .button = popup_grab_button, 80 }; 81 82 static void 83 grab_end(struct xdg_popup *popup) 84 { 85 if (!popup->grabbing) { 86 return; 87 } 88 wl_list_remove(&popup->grab_link); 89 popup->grabbing = false; 90 if (wl_list_empty(&grabs)) { 91 wl_list_remove(&popup_grab_handler.link); 92 wl_list_init(&popup_grab_handler.link); 93 } 94 } 95 96 static bool 97 popup_grab_button(struct pointer_handler *handler, uint32_t time, 98 struct button *button, uint32_t state) 99 { 100 struct xdg_popup *top, *popup, *tmp; 101 struct wl_client *focus = swc.seat->pointer->focus.client; 102 103 if (state != WL_POINTER_BUTTON_STATE_PRESSED || wl_list_empty(&grabs)) { 104 return false; 105 } 106 top = wl_container_of(grabs.prev, top, grab_link); 107 if (focus && focus == wl_resource_get_client(top->resource)) { 108 return false; 109 } 110 wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { 111 xdg_popup_send_popup_done(popup->resource); 112 grab_end(popup); 113 } 114 return true; 115 } 116 117 /* Keyboard focus went to client: a menu is a grab, and a grab ends when focus 118 * leaves what grabbed it, as menus do on every desktop. A press elsewhere 119 * is the other way it ends (popup_grab_button). */ 120 void 121 xdg_popups_focus_changed(struct wl_client *client) 122 { 123 struct xdg_popup *top, *popup, *tmp; 124 125 if (wl_list_empty(&grabs)) { 126 return; 127 } 128 top = wl_container_of(grabs.prev, top, grab_link); 129 if (client && client == wl_resource_get_client(top->resource)) { 130 return; 131 } 132 wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { 133 xdg_popup_send_popup_done(popup->resource); 134 grab_end(popup); 135 } 136 } 137 138 /* xdg_positioner */ 139 static void 140 destroy_positioner(struct wl_resource *resource) 141 { 142 struct xdg_positioner *positioner = wl_resource_get_user_data(resource); 143 144 free(positioner); 145 } 146 147 static void 148 set_size(struct wl_client *client, struct wl_resource *resource, int32_t width, 149 int32_t height) 150 { 151 struct xdg_positioner *positioner = wl_resource_get_user_data(resource); 152 153 if (width <= 0 || height <= 0) { 154 wl_resource_post_error(resource, XDG_POSITIONER_ERROR_INVALID_INPUT, 155 "invalid size"); 156 return; 157 } 158 positioner->width = width; 159 positioner->height = height; 160 } 161 162 static void 163 set_anchor_rect(struct wl_client *client, struct wl_resource *resource, 164 int32_t x, int32_t y, int32_t width, int32_t height) 165 { 166 struct xdg_positioner *positioner = wl_resource_get_user_data(resource); 167 168 if (width <= 0 || height <= 0) { 169 wl_resource_post_error(resource, XDG_POSITIONER_ERROR_INVALID_INPUT, 170 "invalid anchor size"); 171 return; 172 } 173 positioner->anchor_x = x; 174 positioner->anchor_y = y; 175 positioner->anchor_width = width; 176 positioner->anchor_height = height; 177 } 178 179 static void 180 set_anchor(struct wl_client *client, struct wl_resource *resource, 181 uint32_t anchor) 182 { 183 struct xdg_positioner *positioner = wl_resource_get_user_data(resource); 184 185 positioner->anchor = anchor; 186 } 187 188 static void 189 set_gravity(struct wl_client *client, struct wl_resource *resource, 190 uint32_t gravity) 191 { 192 struct xdg_positioner *positioner = wl_resource_get_user_data(resource); 193 194 positioner->gravity = gravity; 195 } 196 197 static void 198 set_constraint_adjustment(struct wl_client *client, 199 struct wl_resource *resource, uint32_t constraint) 200 { 201 struct xdg_positioner *positioner = wl_resource_get_user_data(resource); 202 203 positioner->constraint = constraint; 204 } 205 206 static void 207 set_offset(struct wl_client *client, struct wl_resource *resource, int32_t x, 208 int32_t y) 209 { 210 struct xdg_positioner *positioner = wl_resource_get_user_data(resource); 211 212 positioner->offset_x = x; 213 positioner->offset_y = y; 214 } 215 216 static const struct xdg_positioner_interface positioner_impl = { 217 .destroy = destroy_resource, 218 .set_size = set_size, 219 .set_anchor_rect = set_anchor_rect, 220 .set_anchor = set_anchor, 221 .set_gravity = set_gravity, 222 .set_constraint_adjustment = set_constraint_adjustment, 223 .set_offset = set_offset, 224 }; 225 226 static struct swc_rectangle 227 calculate_position(struct xdg_positioner *positioner) 228 { 229 struct swc_rectangle r = { 230 .x = positioner->offset_x, 231 .y = positioner->offset_y, 232 .width = positioner->width, 233 .height = positioner->height, 234 }; 235 236 switch (positioner->anchor) { 237 case XDG_POSITIONER_ANCHOR_TOP: 238 case XDG_POSITIONER_ANCHOR_TOP_LEFT: 239 case XDG_POSITIONER_ANCHOR_TOP_RIGHT: 240 r.y += positioner->anchor_y; 241 break; 242 case XDG_POSITIONER_ANCHOR_BOTTOM: 243 case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT: 244 case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT: 245 r.y += positioner->anchor_y + positioner->anchor_height; 246 break; 247 default: 248 r.y += positioner->anchor_y + positioner->anchor_height / 2; 249 } 250 switch (positioner->anchor) { 251 case XDG_POSITIONER_ANCHOR_LEFT: 252 case XDG_POSITIONER_ANCHOR_TOP_LEFT: 253 case XDG_POSITIONER_ANCHOR_BOTTOM_LEFT: 254 r.x += positioner->anchor_x; 255 break; 256 case XDG_POSITIONER_ANCHOR_RIGHT: 257 case XDG_POSITIONER_ANCHOR_TOP_RIGHT: 258 case XDG_POSITIONER_ANCHOR_BOTTOM_RIGHT: 259 r.x += positioner->anchor_x + positioner->anchor_width; 260 break; 261 default: 262 r.x += positioner->anchor_x + positioner->anchor_width / 2; 263 } 264 265 switch (positioner->gravity) { 266 case XDG_POSITIONER_GRAVITY_TOP: 267 case XDG_POSITIONER_GRAVITY_TOP_LEFT: 268 case XDG_POSITIONER_GRAVITY_TOP_RIGHT: 269 r.y -= r.height; 270 break; 271 case XDG_POSITIONER_GRAVITY_BOTTOM: 272 case XDG_POSITIONER_GRAVITY_BOTTOM_LEFT: 273 case XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT: 274 break; 275 default: 276 r.y -= r.height / 2; 277 } 278 switch (positioner->gravity) { 279 case XDG_POSITIONER_GRAVITY_LEFT: 280 case XDG_POSITIONER_GRAVITY_TOP_LEFT: 281 case XDG_POSITIONER_GRAVITY_BOTTOM_LEFT: 282 r.x -= r.width; 283 break; 284 case XDG_POSITIONER_GRAVITY_RIGHT: 285 case XDG_POSITIONER_GRAVITY_TOP_RIGHT: 286 case XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT: 287 break; 288 default: 289 r.x -= r.width / 2; 290 } 291 292 return r; 293 } 294 295 /* xdg_toplevel */ 296 static void 297 destroy_toplevel(struct wl_resource *resource) 298 { 299 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 300 301 window_finalize(&toplevel->window); 302 free(toplevel); 303 } 304 305 static bool 306 add_state(struct xdg_toplevel *toplevel, uint32_t state) 307 { 308 uint32_t *current_state; 309 310 wl_array_for_each(current_state, &toplevel->states) 311 { 312 if (*current_state == state) { 313 return false; 314 } 315 } 316 317 if (!(current_state = wl_array_add(&toplevel->states, sizeof(state)))) { 318 WARNING("xdg_toplevel: Failed to allocate new state\n"); 319 return false; 320 } 321 322 *current_state = state; 323 return true; 324 } 325 326 static bool 327 remove_state(struct xdg_toplevel *toplevel, uint32_t state) 328 { 329 uint32_t *current_state; 330 331 wl_array_for_each(current_state, &toplevel->states) 332 { 333 if (*current_state == state) { 334 array_remove(&toplevel->states, current_state, sizeof(state)); 335 return true; 336 } 337 } 338 339 return false; 340 } 341 342 static void 343 send_configure(struct xdg_toplevel *toplevel, int32_t width, int32_t height) 344 { 345 uint32_t serial = wl_display_next_serial(swc.display); 346 347 xdg_toplevel_send_configure(toplevel->resource, width, height, 348 &toplevel->states); 349 xdg_surface_send_configure(toplevel->xdg_surface->resource, serial); 350 toplevel->xdg_surface->configure_serial = serial; 351 } 352 353 static void 354 send_state(struct xdg_toplevel *toplevel) 355 { 356 struct window *window = &toplevel->window; 357 struct swc_rectangle *geometry = &window->view->base.geometry; 358 359 /* a window not yet mapped has no geometry: it keeps the size it was 360 * last asked for rather than being told to choose its own */ 361 if (window->configure.pending || !geometry->width || !geometry->height) 362 send_configure(toplevel, window->configure.width, window->configure.height); 363 else 364 send_configure(toplevel, geometry->width, geometry->height); 365 } 366 367 void 368 xdg_toplevel_send_state(struct wl_resource *resource) 369 { 370 send_state(wl_resource_get_user_data(resource)); 371 } 372 373 static void 374 configure(struct window *window, uint32_t width, uint32_t height) 375 { 376 struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); 377 378 window->configure.acknowledged = false; 379 send_configure(toplevel, width, height); 380 } 381 382 static void 383 focus(struct window *window) 384 { 385 struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); 386 387 add_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); 388 send_state(toplevel); 389 } 390 391 static void 392 unfocus(struct window *window) 393 { 394 struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); 395 396 remove_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); 397 send_state(toplevel); 398 } 399 400 static void 401 close_(struct window *window) 402 { 403 struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); 404 405 xdg_toplevel_send_close(toplevel->resource); 406 } 407 408 static void 409 set_mode(struct window *window, unsigned mode) 410 { 411 struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); 412 413 switch (window->mode) { 414 case WINDOW_MODE_TILED: 415 remove_state(toplevel, XDG_TOPLEVEL_STATE_MAXIMIZED); 416 break; 417 case WINDOW_MODE_FULLSCREEN: 418 remove_state(toplevel, XDG_TOPLEVEL_STATE_FULLSCREEN); 419 break; 420 } 421 422 switch (mode) { 423 case WINDOW_MODE_TILED: 424 add_state(toplevel, XDG_TOPLEVEL_STATE_MAXIMIZED); 425 break; 426 case WINDOW_MODE_FULLSCREEN: 427 add_state(toplevel, XDG_TOPLEVEL_STATE_FULLSCREEN); 428 break; 429 } 430 431 send_state(toplevel); 432 } 433 434 static const struct window_impl toplevel_window_impl = { 435 .configure = configure, 436 .focus = focus, 437 .unfocus = unfocus, 438 .close = close_, 439 .set_mode = set_mode, 440 }; 441 442 static void 443 set_parent(struct wl_client *client, struct wl_resource *resource, 444 struct wl_resource *parent_resource) 445 { 446 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource), 447 *parent = NULL; 448 449 if (parent_resource) { 450 parent = wl_resource_get_user_data(parent_resource); 451 } 452 window_set_parent(&toplevel->window, parent ? &parent->window : NULL); 453 } 454 455 static void 456 set_title(struct wl_client *client, struct wl_resource *resource, 457 const char *title) 458 { 459 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 460 window_set_title(&toplevel->window, title, -1); 461 } 462 463 static void 464 set_app_id(struct wl_client *client, struct wl_resource *resource, 465 const char *app_id) 466 { 467 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 468 window_set_app_id(&toplevel->window, app_id); 469 } 470 471 static void 472 show_window_menu(struct wl_client *client, struct wl_resource *resource, 473 struct wl_resource *seat, uint32_t serial, int32_t x, 474 int32_t y) 475 { 476 } 477 478 static void 479 move(struct wl_client *client, struct wl_resource *resource, 480 struct wl_resource *seat, uint32_t serial) 481 { 482 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 483 struct button *button; 484 485 button = pointer_get_button(swc.seat->pointer, serial); 486 if (button) { 487 window_begin_move(&toplevel->window, button); 488 } 489 } 490 491 static void 492 resize(struct wl_client *client, struct wl_resource *resource, 493 struct wl_resource *seat, uint32_t serial, uint32_t edges) 494 { 495 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 496 struct button *button; 497 498 button = pointer_get_button(swc.seat->pointer, serial); 499 if (button) { 500 window_begin_resize(&toplevel->window, edges, button); 501 } 502 } 503 504 static void 505 set_max_size(struct wl_client *client, struct wl_resource *resource, 506 int32_t width, int32_t height) 507 { 508 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 509 510 (void)client; 511 /* 0 is no limit */ 512 toplevel->window.base.max_width = width > 0 ? (uint32_t)width : 0; 513 toplevel->window.base.max_height = height > 0 ? (uint32_t)height : 0; 514 } 515 516 static void 517 set_min_size(struct wl_client *client, struct wl_resource *resource, 518 int32_t width, int32_t height) 519 { 520 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 521 522 (void)client; 523 toplevel->window.base.min_width = width > 0 ? (uint32_t)width : 1; 524 toplevel->window.base.min_height = height > 0 ? (uint32_t)height : 1; 525 } 526 527 static void 528 set_maximized(struct wl_client *client, struct wl_resource *resource) 529 { 530 } 531 532 static void 533 unset_maximized(struct wl_client *client, struct wl_resource *resource) 534 { 535 } 536 537 static void 538 set_fullscreen(struct wl_client *client, struct wl_resource *resource, 539 struct wl_resource *output) 540 { 541 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 542 struct window *window = &toplevel->window; 543 544 if (window->handler->fullscreen) 545 window->handler->fullscreen(window->handler_data, true); 546 } 547 548 static void 549 unset_fullscreen(struct wl_client *client, struct wl_resource *resource) 550 { 551 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 552 struct window *window = &toplevel->window; 553 554 if (window->handler->fullscreen) 555 window->handler->fullscreen(window->handler_data, false); 556 } 557 558 static void 559 set_minimized(struct wl_client *client, struct wl_resource *resource) 560 { 561 } 562 563 static const struct xdg_toplevel_interface toplevel_impl = { 564 .destroy = destroy_resource, 565 .set_parent = set_parent, 566 .set_title = set_title, 567 .set_app_id = set_app_id, 568 .show_window_menu = show_window_menu, 569 .move = move, 570 .resize = resize, 571 .set_max_size = set_max_size, 572 .set_min_size = set_min_size, 573 .set_maximized = set_maximized, 574 .unset_maximized = unset_maximized, 575 .set_fullscreen = set_fullscreen, 576 .unset_fullscreen = unset_fullscreen, 577 .set_minimized = set_minimized, 578 }; 579 580 struct window * 581 xdg_toplevel_window(struct wl_resource *resource) 582 { 583 struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 584 585 return toplevel ? &toplevel->window : NULL; 586 } 587 588 static struct xdg_toplevel * 589 xdg_toplevel_new(struct wl_client *client, uint32_t version, uint32_t id, 590 struct xdg_surface *xdg_surface) 591 { 592 struct xdg_toplevel *toplevel; 593 594 toplevel = malloc(sizeof(*toplevel)); 595 if (!toplevel) { 596 goto error0; 597 } 598 toplevel->xdg_surface = xdg_surface; 599 toplevel->resource = 600 wl_resource_create(client, &xdg_toplevel_interface, version, id); 601 if (!toplevel->resource) { 602 goto error1; 603 } 604 if (!surface_set_role(xdg_surface->surface, toplevel->resource)) { 605 goto error2; 606 } 607 if (!window_initialize(&toplevel->window, &toplevel_window_impl, 608 xdg_surface->surface)) { 609 goto error2; 610 } 611 wl_array_init(&toplevel->states); 612 wl_resource_set_implementation(toplevel->resource, &toplevel_impl, toplevel, 613 &destroy_toplevel); 614 window_manage(&toplevel->window); 615 616 return toplevel; 617 618 error2: 619 wl_resource_destroy(toplevel->resource); 620 free(toplevel); 621 error1: 622 error0: 623 return NULL; 624 } 625 626 /* xdg_popup */ 627 static void 628 destroy_popup(struct wl_resource *resource) 629 { 630 struct xdg_popup *popup = wl_resource_get_user_data(resource); 631 632 grab_end(popup); 633 compositor_view_destroy(popup->view); 634 free(popup); 635 } 636 637 static void 638 grab(struct wl_client *client, struct wl_resource *resource, 639 struct wl_resource *seat, uint32_t serial) 640 { 641 struct xdg_popup *popup = wl_resource_get_user_data(resource); 642 643 if (popup->grabbing) { 644 return; 645 } 646 if (wl_list_empty(&grabs)) { 647 /* before bindings and clients */ 648 wl_list_insert(&swc.seat->pointer->handlers, &popup_grab_handler.link); 649 } 650 wl_list_insert(grabs.prev, &popup->grab_link); 651 popup->grabbing = true; 652 } 653 654 static const struct xdg_popup_interface popup_impl = { 655 .destroy = destroy_resource, 656 .grab = grab, 657 }; 658 659 /* mirror an anchor or gravity, the enums share values */ 660 static uint32_t 661 mirror_y(uint32_t v) 662 { 663 static const uint32_t to[] = {0, 2, 1, 3, 4, 6, 5, 8, 7}; 664 665 return v < 9 ? to[v] : v; 666 } 667 668 static uint32_t 669 mirror_x(uint32_t v) 670 { 671 static const uint32_t to[] = {0, 1, 2, 4, 3, 7, 8, 5, 6}; 672 673 return v < 9 ? to[v] : v; 674 } 675 676 /* screen at x, y, else the first */ 677 static struct swc_rectangle * 678 screen_at(int32_t x, int32_t y) 679 { 680 struct screen *screen, *first = NULL; 681 682 wl_list_for_each (screen, &swc.screens, link) { 683 struct swc_rectangle *g = &screen->base.geometry; 684 685 if (!first) { 686 first = screen; 687 } 688 if (x >= g->x && y >= g->y && x < g->x + (int32_t)g->width && y < g->y + (int32_t)g->height) { 689 return g; 690 } 691 } 692 return first ? &first->base.geometry : NULL; 693 } 694 695 /* popup position kept on screen: flip, then slide, as the positioner allows */ 696 static struct swc_rectangle 697 constrained_position(struct xdg_positioner *positioner, int32_t px, int32_t py) 698 { 699 struct swc_rectangle r = calculate_position(positioner), f; 700 struct swc_rectangle *g = screen_at(px + r.x, py + r.y); 701 struct xdg_positioner flipped; 702 703 if (!g) { 704 return r; 705 } 706 if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y && 707 py + r.y + r.height > g->y + (int32_t)g->height) { 708 flipped = *positioner; 709 flipped.anchor = mirror_y(flipped.anchor); 710 flipped.gravity = mirror_y(flipped.gravity); 711 flipped.offset_y = -flipped.offset_y; 712 f = calculate_position(&flipped); 713 if (py + f.y >= g->y) { 714 r.y = f.y; 715 } 716 } 717 if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X && 718 px + r.x + r.width > g->x + (int32_t)g->width) { 719 flipped = *positioner; 720 flipped.anchor = mirror_x(flipped.anchor); 721 flipped.gravity = mirror_x(flipped.gravity); 722 flipped.offset_x = -flipped.offset_x; 723 f = calculate_position(&flipped); 724 if (px + f.x >= g->x) { 725 r.x = f.x; 726 } 727 } 728 if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y) { 729 if (py + r.y + r.height > g->y + (int32_t)g->height) { 730 r.y = g->y + (int32_t)g->height - r.height - py; 731 } 732 if (py + r.y < g->y) { 733 r.y = g->y - py; 734 } 735 } 736 if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X) { 737 if (px + r.x + r.width > g->x + (int32_t)g->width) { 738 r.x = g->x + (int32_t)g->width - r.width - px; 739 } 740 if (px + r.x < g->x) { 741 r.x = g->x - px; 742 } 743 } 744 /* cut what still does not fit and tell the client */ 745 if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y && 746 py + r.y + r.height > g->y + (int32_t)g->height) { 747 r.height = g->y + (int32_t)g->height - py - r.y; 748 } 749 if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X && 750 px + r.x + r.width > g->x + (int32_t)g->width) { 751 r.width = g->x + (int32_t)g->width - px - r.x; 752 } 753 return r; 754 } 755 756 /* place popup against its parent and tell the client */ 757 static bool 758 xdg_popup_place(struct xdg_popup *popup, struct compositor_view *parent_view) 759 { 760 uint32_t serial = wl_display_next_serial(swc.display); 761 struct swc_rectangle rect; 762 763 if (!parent_view) { 764 return false; 765 } 766 rect = constrained_position(&popup->positioner, parent_view->base.geometry.x, 767 parent_view->base.geometry.y); 768 compositor_view_set_parent(popup->view, parent_view); 769 /* above the parent, in a layer of normal at least */ 770 popup->view->always_top = parent_view->always_top; 771 compositor_view_set_z(popup->view, parent_view->z > STACK_LAYER_NORMAL ? parent_view->z : STACK_LAYER_NORMAL, 772 true); 773 view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, 774 parent_view->base.geometry.y + rect.y); 775 xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, 776 rect.height); 777 xdg_surface_send_configure(popup->xdg_surface->resource, serial); 778 return true; 779 } 780 781 void 782 xdg_popup_set_parent(struct wl_resource *resource, struct surface *parent) 783 { 784 struct xdg_popup *popup; 785 786 if (!resource || !wl_resource_instance_of(resource, &xdg_popup_interface, &popup_impl)) { 787 return; 788 } 789 popup = wl_resource_get_user_data(resource); 790 xdg_popup_place(popup, compositor_view(parent->view)); 791 } 792 793 /* a popup with no parent waits for one, as layer surface popups do */ 794 static struct xdg_popup * 795 xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, 796 struct xdg_surface *xdg_surface, struct xdg_surface *parent, 797 struct xdg_positioner *positioner) 798 { 799 struct xdg_popup *popup; 800 struct compositor_view *parent_view = 801 parent ? compositor_view(parent->surface->view) : NULL; 802 803 if (parent && !parent_view) { 804 goto error0; 805 } 806 popup = malloc(sizeof(*popup)); 807 if (!popup) { 808 goto error0; 809 } 810 popup->xdg_surface = xdg_surface; 811 popup->positioner = *positioner; 812 popup->grabbing = false; 813 popup->resource = 814 wl_resource_create(client, &xdg_popup_interface, version, id); 815 if (!popup->resource) { 816 goto error1; 817 } 818 if (!surface_set_role(xdg_surface->surface, popup->resource)) { 819 goto error2; 820 } 821 popup->view = compositor_create_view(xdg_surface->surface); 822 if (!popup->view) { 823 goto error3; 824 } 825 wl_resource_set_implementation(popup->resource, &popup_impl, popup, 826 &destroy_popup); 827 if (parent_view) { 828 xdg_popup_place(popup, parent_view); 829 } 830 831 return popup; 832 833 error3: 834 wl_resource_destroy(popup->resource); 835 error2: 836 free(popup); 837 error1: 838 error0: 839 return NULL; 840 } 841 842 /* xdg_surface */ 843 static void 844 get_toplevel(struct wl_client *client, struct wl_resource *resource, 845 uint32_t id) 846 { 847 struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); 848 struct xdg_toplevel *toplevel; 849 850 if (xdg_surface->role) { 851 wl_resource_post_error(resource, XDG_WM_BASE_ERROR_ROLE, 852 "surface already has a role"); 853 return; 854 } 855 if (xdg_surface->surface->role) { 856 wl_resource_post_error(resource, XDG_WM_BASE_ERROR_ROLE, 857 "surface already has a role"); 858 return; 859 } 860 toplevel = xdg_toplevel_new(client, wl_resource_get_version(resource), id, 861 xdg_surface); 862 if (!toplevel) { 863 wl_client_post_no_memory(client); 864 return; 865 } 866 xdg_surface->role = toplevel->resource; 867 wl_resource_add_destroy_listener(xdg_surface->role, 868 &xdg_surface->role_destroy_listener); 869 } 870 871 static void 872 get_popup(struct wl_client *client, struct wl_resource *resource, uint32_t id, 873 struct wl_resource *parent_resource, 874 struct wl_resource *positioner_resource) 875 { 876 struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); 877 struct xdg_surface *parent = 878 parent_resource ? wl_resource_get_user_data(parent_resource) : NULL; 879 struct xdg_positioner *positioner = 880 wl_resource_get_user_data(positioner_resource); 881 struct xdg_popup *popup; 882 883 if (xdg_surface->role) { 884 wl_resource_post_error(resource, XDG_WM_BASE_ERROR_ROLE, 885 "surface already has a role"); 886 return; 887 } 888 if (xdg_surface->surface->role) { 889 wl_resource_post_error(resource, XDG_WM_BASE_ERROR_ROLE, 890 "surface already has a role"); 891 return; 892 } 893 popup = xdg_popup_new(client, wl_resource_get_version(resource), id, 894 xdg_surface, parent, positioner); 895 if (!popup) { 896 wl_client_post_no_memory(client); 897 return; 898 } 899 xdg_surface->role = popup->resource; 900 wl_resource_add_destroy_listener(xdg_surface->role, 901 &xdg_surface->role_destroy_listener); 902 } 903 904 static void 905 ack_configure(struct wl_client *client, struct wl_resource *resource, 906 uint32_t serial) 907 { 908 struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); 909 struct window *window; 910 911 if (!xdg_surface->role) { 912 return; 913 } 914 window = wl_resource_get_user_data(xdg_surface->role); 915 if (window && serial == xdg_surface->configure_serial) { 916 window->configure.acknowledged = true; 917 } 918 } 919 920 static void 921 set_window_geometry(struct wl_client *client, struct wl_resource *resource, 922 int32_t x, int32_t y, int32_t width, int32_t height) 923 { 924 (void)client; 925 struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); 926 struct surface *surface = xdg_surface->surface; 927 928 if (width <= 0 || height <= 0) { 929 surface->has_window_geometry = false; 930 return; 931 } 932 933 surface->has_window_geometry = true; 934 surface->window_x = x; 935 surface->window_y = y; 936 surface->window_width = width; 937 surface->window_height = height; 938 if (!surface->window_geometry_applied && surface->view && 939 (x != 0 || y != 0)) { 940 struct swc_rectangle *geom = &surface->view->geometry; 941 view_move(surface->view, geom->x - x, geom->y - y); 942 surface->window_geometry_applied = true; 943 } 944 } 945 946 static const struct xdg_surface_interface xdg_surface_impl = { 947 .destroy = destroy_resource, 948 .get_toplevel = get_toplevel, 949 .get_popup = get_popup, 950 .ack_configure = ack_configure, 951 .set_window_geometry = set_window_geometry, 952 }; 953 954 static void 955 handle_surface_destroy(struct wl_listener *listener, void *data) 956 { 957 struct xdg_surface *xdg_surface = 958 wl_container_of(listener, xdg_surface, surface_destroy_listener); 959 960 wl_resource_destroy(xdg_surface->resource); 961 } 962 963 static void 964 handle_role_destroy(struct wl_listener *listener, void *data) 965 { 966 struct xdg_surface *xdg_surface = 967 wl_container_of(listener, xdg_surface, role_destroy_listener); 968 969 xdg_surface->role = NULL; 970 } 971 972 static void 973 destroy_xdg_surface(struct wl_resource *resource) 974 { 975 struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); 976 977 wl_list_remove(&xdg_surface->surface_destroy_listener.link); 978 if (xdg_surface->role) { 979 wl_resource_destroy(xdg_surface->role); 980 } 981 free(xdg_surface); 982 } 983 984 static struct xdg_surface * 985 xdg_surface_new(struct wl_client *client, uint32_t version, uint32_t id, 986 struct surface *surface) 987 { 988 struct xdg_surface *xdg_surface; 989 990 xdg_surface = malloc(sizeof(*xdg_surface)); 991 if (!xdg_surface) { 992 goto error0; 993 } 994 xdg_surface->resource = 995 wl_resource_create(client, &xdg_surface_interface, version, id); 996 if (!xdg_surface->resource) { 997 goto error1; 998 } 999 xdg_surface->surface = surface; 1000 xdg_surface->surface_destroy_listener.notify = &handle_surface_destroy; 1001 xdg_surface->role = NULL; 1002 xdg_surface->role_destroy_listener.notify = &handle_role_destroy; 1003 wl_resource_add_destroy_listener(surface->resource, 1004 &xdg_surface->surface_destroy_listener); 1005 wl_resource_set_implementation(xdg_surface->resource, &xdg_surface_impl, 1006 xdg_surface, destroy_xdg_surface); 1007 1008 return xdg_surface; 1009 1010 error1: 1011 free(xdg_surface); 1012 error0: 1013 return NULL; 1014 } 1015 1016 /* xdg_shell */ 1017 static void 1018 create_positioner(struct wl_client *client, struct wl_resource *resource, 1019 uint32_t id) 1020 { 1021 struct xdg_positioner *positioner; 1022 struct wl_resource *positioner_resource; 1023 uint32_t version; 1024 1025 positioner = calloc(1, sizeof(*positioner)); 1026 if (!positioner) { 1027 goto error0; 1028 } 1029 1030 version = wl_resource_get_version(resource); 1031 positioner_resource = 1032 wl_resource_create(client, &xdg_positioner_interface, version, id); 1033 if (!positioner_resource) { 1034 goto error1; 1035 } 1036 wl_resource_set_implementation(positioner_resource, &positioner_impl, 1037 positioner, &destroy_positioner); 1038 return; 1039 1040 error1: 1041 free(positioner); 1042 error0: 1043 wl_resource_post_no_memory(resource); 1044 } 1045 1046 static void 1047 get_xdg_surface(struct wl_client *client, struct wl_resource *resource, 1048 uint32_t id, struct wl_resource *surface_resource) 1049 { 1050 struct xdg_surface *xdg_surface; 1051 struct surface *surface = wl_resource_get_user_data(surface_resource); 1052 1053 xdg_surface = 1054 xdg_surface_new(client, wl_resource_get_version(resource), id, surface); 1055 if (!xdg_surface) { 1056 wl_client_post_no_memory(client); 1057 } 1058 } 1059 1060 static void 1061 pong(struct wl_client *client, struct wl_resource *resource, uint32_t serial) 1062 { 1063 } 1064 1065 static const struct xdg_wm_base_interface wm_base_impl = { 1066 .destroy = destroy_resource, 1067 .create_positioner = create_positioner, 1068 .get_xdg_surface = get_xdg_surface, 1069 .pong = pong, 1070 }; 1071 1072 static void 1073 bind_wm_base(struct wl_client *client, void *data, uint32_t version, 1074 uint32_t id) 1075 { 1076 struct wl_resource *resource; 1077 1078 resource = wl_resource_create(client, &xdg_wm_base_interface, version, id); 1079 if (!resource) { 1080 wl_client_post_no_memory(client); 1081 return; 1082 } 1083 wl_resource_set_implementation(resource, &wm_base_impl, NULL, NULL); 1084 } 1085 1086 struct wl_global * 1087 xdg_shell_create(struct wl_display *display) 1088 { 1089 return wl_global_create(display, &xdg_wm_base_interface, 1, NULL, 1090 &bind_wm_base); 1091 }