shell.patch (32787B)
1 Subject: shells and windows 2 3 diff --git a/libswc/layer_shell.c b/libswc/layer_shell.c 4 index 194ed01..f167c8f 100644 5 --- a/libswc/layer_shell.c 6 +++ b/libswc/layer_shell.c 7 @@ -9,6 +9,7 @@ 8 #include "surface.h" 9 #include "util.h" 10 #include "view.h" 11 +#include "xdg_shell.h" 12 13 #include "wlr-layer-shell-unstable-v1-server-protocol.h" 14 #include <assert.h> 15 @@ -35,6 +36,8 @@ struct layer_surface { 16 uint32_t desired_width, desired_height; 17 } current, pending; 18 uint32_t configure_serial; 19 + /* screen size the last configure was computed from */ 20 + uint32_t screen_width, screen_height; 21 bool configured; 22 bool mapped; 23 }; 24 @@ -153,22 +156,22 @@ update_usable_geometry(struct layer_surface *surface) 25 static void 26 restack_layer(struct layer_surface *surface) 27 { 28 - uint32_t stack_layer = STACK_LAYER_NORMAL; 29 + int32_t z = STACK_LAYER_NORMAL; 30 bool always_top = false; 31 32 switch (surface->current.layer) { 33 case ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND: 34 - stack_layer = STACK_LAYER_BACKGROUND; 35 + z = STACK_LAYER_BACKGROUND; 36 break; 37 case ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM: 38 - stack_layer = STACK_LAYER_BOTTOM; 39 + z = STACK_LAYER_BOTTOM; 40 break; 41 case ZWLR_LAYER_SHELL_V1_LAYER_TOP: 42 - stack_layer = STACK_LAYER_TOP; 43 + z = STACK_LAYER_TOP; 44 always_top = true; 45 break; 46 case ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY: 47 - stack_layer = STACK_LAYER_OVERLAY; 48 + z = STACK_LAYER_OVERLAY; 49 always_top = true; 50 break; 51 default: 52 @@ -176,13 +179,25 @@ restack_layer(struct layer_surface *surface) 53 } 54 55 surface->view->always_top = always_top; 56 - compositor_view_set_stack_layer(surface->view, stack_layer, true); 57 + compositor_view_set_z(surface->view, z, true); 58 +} 59 + 60 +/* zone 0 surfaces keep clear of reserved zones, others span the screen */ 61 +static const struct swc_rectangle * 62 +area_of(const struct layer_surface *surface) 63 +{ 64 + const struct swc_rectangle *usable = &surface->screen->base.usable_geometry; 65 + 66 + if (surface->current.exclusive_zone == 0 && usable->width && usable->height) { 67 + return usable; 68 + } 69 + return &surface->screen->base.geometry; 70 } 71 72 static void 73 update_position(struct layer_surface *surface) 74 { 75 - struct swc_rectangle *screen = &surface->screen->base.geometry; 76 + const struct swc_rectangle *screen = area_of(surface); 77 struct swc_rectangle *view = &surface->view->base.geometry; 78 int32_t x, y; 79 80 @@ -229,16 +244,18 @@ send_configure(struct layer_surface *surface) 81 width = configure_size(surface->current.desired_width, surface->current.anchor, 82 ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT, 83 ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT, 84 - surface->screen->base.geometry.width, 85 + area_of(surface)->width, 86 surface->current.margin.left, 87 surface->current.margin.right); 88 height = 89 configure_size(surface->current.desired_height, surface->current.anchor, 90 ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP, 91 ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM, 92 - surface->screen->base.geometry.height, 93 + area_of(surface)->height, 94 surface->current.margin.top, 95 surface->current.margin.bottom); 96 + surface->screen_width = surface->screen->base.geometry.width; 97 + surface->screen_height = surface->screen->base.geometry.height; 98 surface->configure_serial = wl_display_next_serial(swc.display); 99 zwlr_layer_surface_v1_send_configure(surface->resource, 100 surface->configure_serial, width, 101 @@ -308,9 +325,12 @@ static void 102 get_popup(struct wl_client *client, struct wl_resource *resource, 103 struct wl_resource *popup) 104 { 105 + struct layer_surface *surface = wl_resource_get_user_data(resource); 106 + 107 (void)client; 108 - (void)resource; 109 - (void)popup; 110 + if (surface->view) { 111 + xdg_popup_set_parent(popup, surface->view->surface); 112 + } 113 } 114 115 static void 116 @@ -375,11 +395,13 @@ handle_attach(struct view_handler *handler) 117 118 if (mapped) { 119 update_position(surface); 120 - restack_layer(surface); 121 + /* raise on map only, or it covers its own popups */ 122 if (!surface->mapped) { 123 + restack_layer(surface); 124 compositor_view_show(surface->view); 125 - if (surface->current.keyboard_interactivity != 126 - ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_NONE) { 127 + /* on demand waits for a press, see layer_shell_pressed */ 128 + if (surface->current.keyboard_interactivity == 129 + ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE) { 130 keyboard_set_focus(swc.seat->keyboard, surface->view); 131 } 132 } 133 @@ -432,6 +454,28 @@ handle_surface_commit(struct wl_listener *listener, void *data) 134 } 135 } 136 137 +static void modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, 138 + pixman_region32_t *usable); 139 + 140 +void 141 +layer_shell_usable_changed(struct screen *screen) 142 +{ 143 + struct screen_modifier *modifier; 144 + struct layer_surface *surface; 145 + 146 + wl_list_for_each(modifier, &screen->modifiers, link) 147 + { 148 + if (modifier->modify != modify) { 149 + continue; 150 + } 151 + surface = wl_container_of(modifier, surface, modifier); 152 + if (surface->configured && surface->current.exclusive_zone == 0) { 153 + send_configure(surface); 154 + update_position(surface); 155 + } 156 + } 157 +} 158 + 159 static void 160 modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, 161 pixman_region32_t *usable) 162 @@ -443,6 +487,13 @@ modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, 163 .y2 = geom->y + geom->height}; 164 int32_t size; 165 166 + /* every usable-area pass visits us, so notice a size change here */ 167 + if (surface->configured && (geom->width != surface->screen_width || 168 + geom->height != surface->screen_height)) { 169 + send_configure(surface); 170 + update_position(surface); 171 + } 172 + 173 if (!surface->mapped) { 174 pixman_region32_reset(usable, &box); 175 return; 176 @@ -632,6 +683,28 @@ bind_layer_shell(struct wl_client *client, void *data, uint32_t version, 177 wl_resource_set_implementation(resource, &layer_shell_impl, NULL, NULL); 178 } 179 180 +void 181 +layer_shell_pressed(struct compositor_view *view) 182 +{ 183 + struct view_handler *handler; 184 + struct layer_surface *surface; 185 + 186 + if (!view) { 187 + return; 188 + } 189 + wl_list_for_each(handler, &view->base.handlers, link) { 190 + if (handler->impl != &view_handler_impl) { 191 + continue; 192 + } 193 + surface = wl_container_of(handler, surface, view_handler); 194 + if (surface->current.keyboard_interactivity == 195 + ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_ON_DEMAND) { 196 + keyboard_set_focus(swc.seat->keyboard, view); 197 + } 198 + return; 199 + } 200 +} 201 + 202 struct wl_global * 203 layer_shell_create(struct wl_display *display) 204 { 205 diff --git a/libswc/layer_shell.h b/libswc/layer_shell.h 206 index 76e5432..138db1a 100644 207 --- a/libswc/layer_shell.h 208 +++ b/libswc/layer_shell.h 209 @@ -4,7 +4,18 @@ 210 struct wl_display; 211 struct wl_global; 212 213 +struct screen; 214 + 215 struct wl_global * 216 layer_shell_create(struct wl_display *display); 217 218 +/* usable area changed, lay out the surfaces that depend on it */ 219 +void 220 +layer_shell_usable_changed(struct screen *screen); 221 + 222 +/* a press on view gives on-demand layer surfaces the keyboard */ 223 +struct compositor_view; 224 +void 225 +layer_shell_pressed(struct compositor_view *view); 226 + 227 #endif 228 diff --git a/libswc/panel.c b/libswc/panel.c 229 index 5434d38..09add87 100644 230 --- a/libswc/panel.c 231 +++ b/libswc/panel.c 232 @@ -47,9 +47,26 @@ struct panel { 233 uint32_t edge; 234 uint32_t offset, strut_size; 235 uint32_t y_offset; 236 + /* length last sent */ 237 + uint32_t length; 238 bool docked; 239 }; 240 241 +static uint32_t 242 +edge_length(struct screen *screen, uint32_t edge) 243 +{ 244 + switch (edge) { 245 + case SWC_PANEL_EDGE_TOP: 246 + case SWC_PANEL_EDGE_BOTTOM: 247 + return screen->base.geometry.width; 248 + case SWC_PANEL_EDGE_LEFT: 249 + case SWC_PANEL_EDGE_RIGHT: 250 + return screen->base.geometry.height; 251 + default: 252 + return 0; 253 + } 254 +} 255 + 256 static void 257 update_position(struct panel *panel) 258 { 259 @@ -95,16 +112,7 @@ dock(struct wl_client *client, struct wl_resource *resource, uint32_t edge, 260 screen = wl_container_of(swc.screens.next, screen, link); 261 } 262 263 - switch (edge) { 264 - case SWC_PANEL_EDGE_TOP: 265 - case SWC_PANEL_EDGE_BOTTOM: 266 - length = screen->base.geometry.width; 267 - break; 268 - case SWC_PANEL_EDGE_LEFT: 269 - case SWC_PANEL_EDGE_RIGHT: 270 - length = screen->base.geometry.height; 271 - break; 272 - default: 273 + if (!(length = edge_length(screen, edge))) { 274 return; 275 } 276 277 @@ -115,6 +123,7 @@ dock(struct wl_client *client, struct wl_resource *resource, uint32_t edge, 278 279 panel->screen = screen; 280 panel->edge = edge; 281 + panel->length = length; 282 panel->docked = true; 283 284 update_position(panel); 285 @@ -196,6 +205,13 @@ modify(struct screen_modifier *modifier, const struct swc_rectangle *geom, 286 287 assert(panel->docked); 288 289 + /* every usable-area pass visits us, so notice a size change here */ 290 + if (edge_length(panel->screen, panel->edge) != panel->length) { 291 + panel->length = edge_length(panel->screen, panel->edge); 292 + update_position(panel); 293 + swc_panel_send_docked(panel->resource, panel->length); 294 + } 295 + 296 DEBUG("Original geometry { x1: %d, y1: %d, x2: %d, y2: %d }\n", box.x1, 297 box.y1, box.x2, box.y2); 298 299 diff --git a/libswc/window.c b/libswc/window.c 300 index 19a9897..0efd250 100644 301 --- a/libswc/window.c 302 +++ b/libswc/window.c 303 @@ -95,6 +95,14 @@ clamp_window_size(const struct window *window, uint32_t *width, 304 window->base.max_height); 305 } 306 307 +void 308 +window_pointer_entered(struct window *window) 309 +{ 310 + if (window->handler->entered) { 311 + window->handler->entered(window->handler_data); 312 + } 313 +} 314 + 315 static void 316 handle_window_enter(struct wl_listener *listener, void *data) 317 { 318 @@ -110,9 +118,7 @@ handle_window_enter(struct wl_listener *listener, void *data) 319 return; 320 } 321 322 - if (window->handler->entered) { 323 - window->handler->entered(window->handler_data); 324 - } 325 + window_pointer_entered(window); 326 } 327 328 struct wl_listener window_enter_listener = { 329 @@ -188,6 +194,25 @@ swc_window_set_handler(struct swc_window *base, 330 window->handler_data = data; 331 } 332 333 +EXPORT void 334 +swc_window_set_backdrop(struct swc_window *base, uint32_t margin, 335 + void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data) 336 +{ 337 + struct window *window = INTERNAL(base); 338 + 339 + if (window->view) 340 + compositor_view_set_backdrop(window->view, margin, hook, data); 341 +} 342 + 343 +EXPORT void 344 +swc_window_set_z(struct swc_window *base, int32_t z) 345 +{ 346 + struct window *window = INTERNAL(base); 347 + 348 + if (window->view) 349 + compositor_view_set_z(window->view, z, true); 350 +} 351 + 352 EXPORT void 353 swc_window_close(struct swc_window *base) 354 { 355 @@ -234,6 +259,32 @@ swc_window_focus(struct swc_window *base) 356 keyboard_set_focus(swc.seat->keyboard, new); 357 } 358 359 +EXPORT void 360 +swc_window_set_parent(struct swc_window *base, struct swc_window *parent) 361 +{ 362 + window_set_parent(INTERNAL(base), parent ? INTERNAL(parent) : NULL); 363 +} 364 + 365 +EXPORT void 366 +swc_window_set_attached(struct swc_window *base, bool attached) 367 +{ 368 + struct window *window = INTERNAL(base); 369 + 370 + if (window->view) { 371 + window->view->attached = attached; 372 + } 373 +} 374 + 375 +EXPORT void 376 +swc_window_raise(struct swc_window *base) 377 +{ 378 + struct window *window = INTERNAL(base); 379 + 380 + if (window->view) { 381 + raise_window(window->view); 382 + } 383 +} 384 + 385 EXPORT void 386 swc_window_set_stacked(struct swc_window *base) 387 { 388 @@ -367,6 +418,14 @@ swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, 389 inner_border_width); 390 } 391 392 +EXPORT void 393 +swc_window_set_border_placement(struct swc_window *window, 394 + enum swc_border_placement placement) 395 +{ 396 + compositor_view_set_border_frame(INTERNAL(window)->view, 397 + placement == SWC_BORDER_FRAME); 398 +} 399 + 400 EXPORT void 401 swc_window_set_decor(struct swc_window *window, const struct swc_decor *decor) 402 { 403 @@ -497,6 +556,10 @@ handle_resize(struct view_handler *handler, uint32_t old_width, 404 405 view_move(&window->view->base, x, y); 406 } 407 + 408 + if (window->handler->size_changed) { 409 + window->handler->size_changed(window->handler_data); 410 + } 411 } 412 413 static const struct view_handler_impl view_handler_impl = { 414 @@ -616,15 +679,32 @@ window_set_app_id(struct window *window, const char *app_id) 415 } 416 } 417 418 +void 419 +window_set_modal(struct window *window, bool modal) 420 +{ 421 + if (window->base.modal == modal) { 422 + return; 423 + } 424 + window->base.modal = modal; 425 + if (window->handler->modal_changed) { 426 + window->handler->modal_changed(window->handler_data); 427 + } 428 +} 429 + 430 void 431 window_set_parent(struct window *window, struct window *parent) 432 { 433 - if (window->base.parent == &parent->base) { 434 + struct swc_window *base = parent ? &parent->base : NULL; 435 + 436 + if (window->base.parent == base || window == parent) { 437 return; 438 } 439 440 - compositor_view_set_parent(window->view, parent->view); 441 - window->base.parent = &parent->base; 442 + compositor_view_set_parent(window->view, parent ? parent->view : NULL); 443 + window->base.parent = base; 444 + if (parent) { 445 + raise_window(parent->view); 446 + } 447 448 if (window->handler->parent_changed) { 449 window->handler->parent_changed(window->handler_data); 450 diff --git a/libswc/window.h b/libswc/window.h 451 index f1eadde..d987307 100644 452 --- a/libswc/window.h 453 +++ b/libswc/window.h 454 @@ -95,6 +95,11 @@ struct window_impl { 455 456 extern struct wl_listener window_enter_listener; 457 458 +/* The pointer is over the window as much as over its surface: its handler is 459 + * told, as when the surface is entered. */ 460 +void 461 +window_pointer_entered(struct window *window); 462 + 463 bool 464 window_initialize(struct window *window, const struct window_impl *impl, 465 struct surface *surface); 466 @@ -111,6 +116,8 @@ window_set_app_id(struct window *window, const char *app_id); 467 void 468 window_set_parent(struct window *window, struct window *parent); 469 void 470 +window_set_modal(struct window *window, bool modal); 471 +void 472 window_begin_move(struct window *window, struct button *button); 473 void 474 window_begin_resize(struct window *window, uint32_t edges, 475 diff --git a/libswc/xdg_dialog.c b/libswc/xdg_dialog.c 476 new file mode 100644 477 index 0000000..25233b9 478 --- /dev/null 479 +++ b/libswc/xdg_dialog.c 480 @@ -0,0 +1,117 @@ 481 +/* xdg-dialog-v1 */ 482 + 483 +#include "xdg_dialog.h" 484 +#include "util.h" 485 +#include "window.h" 486 +#include "xdg_shell.h" 487 + 488 +#include <stdlib.h> 489 +#include <wayland-server.h> 490 +#include "xdg-dialog-v1-server-protocol.h" 491 + 492 +struct dialog { 493 + struct wl_resource *resource; 494 + struct wl_resource *toplevel; 495 + struct wl_listener toplevel_destroy; 496 +}; 497 + 498 +static void 499 +set(struct wl_resource *resource, bool modal) 500 +{ 501 + struct dialog *dialog = wl_resource_get_user_data(resource); 502 + struct window *window; 503 + 504 + if (dialog->toplevel && (window = xdg_toplevel_window(dialog->toplevel))) { 505 + window_set_modal(window, modal); 506 + } 507 +} 508 + 509 +static void 510 +set_modal(struct wl_client *client, struct wl_resource *resource) 511 +{ 512 + (void)client; 513 + set(resource, true); 514 +} 515 + 516 +static void 517 +unset_modal(struct wl_client *client, struct wl_resource *resource) 518 +{ 519 + (void)client; 520 + set(resource, false); 521 +} 522 + 523 +static const struct xdg_dialog_v1_interface dialog_impl = { 524 + .destroy = destroy_resource, 525 + .set_modal = set_modal, 526 + .unset_modal = unset_modal, 527 +}; 528 + 529 +static void 530 +handle_toplevel_destroy(struct wl_listener *listener, void *data) 531 +{ 532 + struct dialog *dialog = wl_container_of(listener, dialog, toplevel_destroy); 533 + 534 + (void)data; 535 + dialog->toplevel = NULL; 536 +} 537 + 538 +static void 539 +destroy_dialog(struct wl_resource *resource) 540 +{ 541 + struct dialog *dialog = wl_resource_get_user_data(resource); 542 + 543 + /* no longer a dialog: stop blocking */ 544 + set(resource, false); 545 + if (dialog->toplevel) { 546 + wl_list_remove(&dialog->toplevel_destroy.link); 547 + } 548 + free(dialog); 549 +} 550 + 551 +static void 552 +get_xdg_dialog(struct wl_client *client, struct wl_resource *resource, uint32_t id, 553 + struct wl_resource *toplevel) 554 +{ 555 + struct dialog *dialog = calloc(1, sizeof *dialog); 556 + 557 + if (!dialog) { 558 + wl_resource_post_no_memory(resource); 559 + return; 560 + } 561 + dialog->resource = wl_resource_create(client, &xdg_dialog_v1_interface, 562 + wl_resource_get_version(resource), id); 563 + if (!dialog->resource) { 564 + free(dialog); 565 + wl_resource_post_no_memory(resource); 566 + return; 567 + } 568 + dialog->toplevel = toplevel; 569 + dialog->toplevel_destroy.notify = handle_toplevel_destroy; 570 + wl_resource_add_destroy_listener(toplevel, &dialog->toplevel_destroy); 571 + wl_resource_set_implementation(dialog->resource, &dialog_impl, dialog, destroy_dialog); 572 +} 573 + 574 +static const struct xdg_wm_dialog_v1_interface manager_impl = { 575 + .destroy = destroy_resource, 576 + .get_xdg_dialog = get_xdg_dialog, 577 +}; 578 + 579 +static void 580 +bind_manager(struct wl_client *client, void *data, uint32_t version, uint32_t id) 581 +{ 582 + struct wl_resource *resource; 583 + 584 + (void)data; 585 + resource = wl_resource_create(client, &xdg_wm_dialog_v1_interface, version, id); 586 + if (!resource) { 587 + wl_client_post_no_memory(client); 588 + return; 589 + } 590 + wl_resource_set_implementation(resource, &manager_impl, NULL, NULL); 591 +} 592 + 593 +struct wl_global * 594 +xdg_dialog_manager_create(struct wl_display *display) 595 +{ 596 + return wl_global_create(display, &xdg_wm_dialog_v1_interface, 1, NULL, &bind_manager); 597 +} 598 diff --git a/libswc/xdg_dialog.h b/libswc/xdg_dialog.h 599 new file mode 100644 600 index 0000000..134b329 601 --- /dev/null 602 +++ b/libswc/xdg_dialog.h 603 @@ -0,0 +1,11 @@ 604 +/* xdg-dialog-v1 */ 605 + 606 +#ifndef SWC_XDG_DIALOG_H 607 +#define SWC_XDG_DIALOG_H 608 + 609 +struct wl_display; 610 + 611 +struct wl_global * 612 +xdg_dialog_manager_create(struct wl_display *display); 613 + 614 +#endif 615 diff --git a/libswc/xdg_shell.c b/libswc/xdg_shell.c 616 index c5d6986..1e327ad 100644 617 --- a/libswc/xdg_shell.c 618 +++ b/libswc/xdg_shell.c 619 @@ -24,6 +24,8 @@ 620 #include "xdg_shell.h" 621 #include "compositor.h" 622 #include "internal.h" 623 +#include "pointer.h" 624 +#include "screen.h" 625 #include "seat.h" 626 #include "surface.h" 627 #include "util.h" 628 @@ -63,8 +65,76 @@ struct xdg_popup { 629 struct xdg_surface *xdg_surface; 630 struct xdg_positioner positioner; 631 struct compositor_view *view; 632 + struct wl_list grab_link; /* in grabs while it holds a grab */ 633 + bool grabbing; 634 }; 635 636 +/* popups holding a grab, newest last. a press elsewhere dismisses them newest first */ 637 +static struct wl_list grabs = {&grabs, &grabs}; 638 + 639 +static bool popup_grab_button(struct pointer_handler *handler, uint32_t time, 640 + struct button *button, uint32_t state); 641 + 642 +static struct pointer_handler popup_grab_handler = { 643 + .button = popup_grab_button, 644 +}; 645 + 646 +static void 647 +grab_end(struct xdg_popup *popup) 648 +{ 649 + if (!popup->grabbing) { 650 + return; 651 + } 652 + wl_list_remove(&popup->grab_link); 653 + popup->grabbing = false; 654 + if (wl_list_empty(&grabs)) { 655 + wl_list_remove(&popup_grab_handler.link); 656 + wl_list_init(&popup_grab_handler.link); 657 + } 658 +} 659 + 660 +static bool 661 +popup_grab_button(struct pointer_handler *handler, uint32_t time, 662 + struct button *button, uint32_t state) 663 +{ 664 + struct xdg_popup *top, *popup, *tmp; 665 + struct wl_client *focus = swc.seat->pointer->focus.client; 666 + 667 + if (state != WL_POINTER_BUTTON_STATE_PRESSED || wl_list_empty(&grabs)) { 668 + return false; 669 + } 670 + top = wl_container_of(grabs.prev, top, grab_link); 671 + if (focus && focus == wl_resource_get_client(top->resource)) { 672 + return false; 673 + } 674 + wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { 675 + xdg_popup_send_popup_done(popup->resource); 676 + grab_end(popup); 677 + } 678 + return true; 679 +} 680 + 681 +/* Keyboard focus went to client: a menu is a grab, and a grab ends when focus 682 + * leaves what grabbed it, as menus do on every desktop. A press elsewhere 683 + * is the other way it ends (popup_grab_button). */ 684 +void 685 +xdg_popups_focus_changed(struct wl_client *client) 686 +{ 687 + struct xdg_popup *top, *popup, *tmp; 688 + 689 + if (wl_list_empty(&grabs)) { 690 + return; 691 + } 692 + top = wl_container_of(grabs.prev, top, grab_link); 693 + if (client && client == wl_resource_get_client(top->resource)) { 694 + return; 695 + } 696 + wl_list_for_each_reverse_safe (popup, tmp, &grabs, grab_link) { 697 + xdg_popup_send_popup_done(popup->resource); 698 + grab_end(popup); 699 + } 700 +} 701 + 702 /* xdg_positioner */ 703 static void 704 destroy_positioner(struct wl_resource *resource) 705 @@ -269,23 +339,29 @@ remove_state(struct xdg_toplevel *toplevel, uint32_t state) 706 return false; 707 } 708 709 -static uint32_t 710 +static void 711 send_configure(struct xdg_toplevel *toplevel, int32_t width, int32_t height) 712 { 713 uint32_t serial = wl_display_next_serial(swc.display); 714 715 - if (width < 0) { 716 - width = toplevel->window.configure.width; 717 - } 718 - if (height < 0) { 719 - height = toplevel->window.configure.height; 720 - } 721 - 722 xdg_toplevel_send_configure(toplevel->resource, width, height, 723 &toplevel->states); 724 xdg_surface_send_configure(toplevel->xdg_surface->resource, serial); 725 + toplevel->xdg_surface->configure_serial = serial; 726 +} 727 728 - return serial; 729 +static void 730 +send_state(struct xdg_toplevel *toplevel) 731 +{ 732 + struct window *window = &toplevel->window; 733 + struct swc_rectangle *geometry = &window->view->base.geometry; 734 + 735 + /* a window not yet mapped has no geometry: it keeps the size it was 736 + * last asked for rather than being told to choose its own */ 737 + if (window->configure.pending || !geometry->width || !geometry->height) 738 + send_configure(toplevel, window->configure.width, window->configure.height); 739 + else 740 + send_configure(toplevel, geometry->width, geometry->height); 741 } 742 743 static void 744 @@ -294,33 +370,25 @@ configure(struct window *window, uint32_t width, uint32_t height) 745 struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); 746 747 window->configure.acknowledged = false; 748 - toplevel->xdg_surface->configure_serial = 749 - send_configure(toplevel, width, height); 750 + send_configure(toplevel, width, height); 751 } 752 753 static void 754 focus(struct window *window) 755 { 756 struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); 757 - uint32_t width = window->view->base.geometry.width; 758 - uint32_t height = window->view->base.geometry.height; 759 760 add_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); 761 - /* dont send 0x0 on focus change */ 762 - send_configure(toplevel, width ? (int32_t)width : -1, 763 - height ? (int32_t)height : -1); 764 + send_state(toplevel); 765 } 766 767 static void 768 unfocus(struct window *window) 769 { 770 struct xdg_toplevel *toplevel = wl_container_of(window, toplevel, window); 771 - uint32_t width = window->view->base.geometry.width; 772 - uint32_t height = window->view->base.geometry.height; 773 774 remove_state(toplevel, XDG_TOPLEVEL_STATE_ACTIVATED); 775 - send_configure(toplevel, width ? (int32_t)width : -1, 776 - height ? (int32_t)height : -1); 777 + send_state(toplevel); 778 } 779 780 static void 781 @@ -354,7 +422,7 @@ set_mode(struct window *window, unsigned mode) 782 break; 783 } 784 785 - send_configure(toplevel, -1, -1); 786 + send_state(toplevel); 787 } 788 789 static const struct window_impl toplevel_window_impl = { 790 @@ -431,12 +499,23 @@ static void 791 set_max_size(struct wl_client *client, struct wl_resource *resource, 792 int32_t width, int32_t height) 793 { 794 + struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 795 + 796 + (void)client; 797 + /* 0 is no limit */ 798 + toplevel->window.base.max_width = width > 0 ? (uint32_t)width : 0; 799 + toplevel->window.base.max_height = height > 0 ? (uint32_t)height : 0; 800 } 801 802 static void 803 set_min_size(struct wl_client *client, struct wl_resource *resource, 804 int32_t width, int32_t height) 805 { 806 + struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 807 + 808 + (void)client; 809 + toplevel->window.base.min_width = width > 0 ? (uint32_t)width : 1; 810 + toplevel->window.base.min_height = height > 0 ? (uint32_t)height : 1; 811 } 812 813 static void 814 @@ -453,11 +532,21 @@ static void 815 set_fullscreen(struct wl_client *client, struct wl_resource *resource, 816 struct wl_resource *output) 817 { 818 + struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 819 + struct window *window = &toplevel->window; 820 + 821 + if (window->handler->fullscreen) 822 + window->handler->fullscreen(window->handler_data, true); 823 } 824 825 static void 826 unset_fullscreen(struct wl_client *client, struct wl_resource *resource) 827 { 828 + struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 829 + struct window *window = &toplevel->window; 830 + 831 + if (window->handler->fullscreen) 832 + window->handler->fullscreen(window->handler_data, false); 833 } 834 835 static void 836 @@ -482,6 +571,14 @@ static const struct xdg_toplevel_interface toplevel_impl = { 837 .set_minimized = set_minimized, 838 }; 839 840 +struct window * 841 +xdg_toplevel_window(struct wl_resource *resource) 842 +{ 843 + struct xdg_toplevel *toplevel = wl_resource_get_user_data(resource); 844 + 845 + return toplevel ? &toplevel->window : NULL; 846 +} 847 + 848 static struct xdg_toplevel * 849 xdg_toplevel_new(struct wl_client *client, uint32_t version, uint32_t id, 850 struct xdg_surface *xdg_surface) 851 @@ -526,6 +623,7 @@ destroy_popup(struct wl_resource *resource) 852 { 853 struct xdg_popup *popup = wl_resource_get_user_data(resource); 854 855 + grab_end(popup); 856 compositor_view_destroy(popup->view); 857 free(popup); 858 } 859 @@ -534,6 +632,17 @@ static void 860 grab(struct wl_client *client, struct wl_resource *resource, 861 struct wl_resource *seat, uint32_t serial) 862 { 863 + struct xdg_popup *popup = wl_resource_get_user_data(resource); 864 + 865 + if (popup->grabbing) { 866 + return; 867 + } 868 + if (wl_list_empty(&grabs)) { 869 + /* before bindings and clients */ 870 + wl_list_insert(&swc.seat->pointer->handlers, &popup_grab_handler.link); 871 + } 872 + wl_list_insert(grabs.prev, &popup->grab_link); 873 + popup->grabbing = true; 874 } 875 876 static const struct xdg_popup_interface popup_impl = { 877 @@ -541,6 +650,141 @@ static const struct xdg_popup_interface popup_impl = { 878 .grab = grab, 879 }; 880 881 +/* mirror an anchor or gravity, the enums share values */ 882 +static uint32_t 883 +mirror_y(uint32_t v) 884 +{ 885 + static const uint32_t to[] = {0, 2, 1, 3, 4, 6, 5, 8, 7}; 886 + 887 + return v < 9 ? to[v] : v; 888 +} 889 + 890 +static uint32_t 891 +mirror_x(uint32_t v) 892 +{ 893 + static const uint32_t to[] = {0, 1, 2, 4, 3, 7, 8, 5, 6}; 894 + 895 + return v < 9 ? to[v] : v; 896 +} 897 + 898 +/* screen at x, y, else the first */ 899 +static struct swc_rectangle * 900 +screen_at(int32_t x, int32_t y) 901 +{ 902 + struct screen *screen, *first = NULL; 903 + 904 + wl_list_for_each (screen, &swc.screens, link) { 905 + struct swc_rectangle *g = &screen->base.geometry; 906 + 907 + if (!first) { 908 + first = screen; 909 + } 910 + if (x >= g->x && y >= g->y && x < g->x + (int32_t)g->width && y < g->y + (int32_t)g->height) { 911 + return g; 912 + } 913 + } 914 + return first ? &first->base.geometry : NULL; 915 +} 916 + 917 +/* popup position kept on screen: flip, then slide, as the positioner allows */ 918 +static struct swc_rectangle 919 +constrained_position(struct xdg_positioner *positioner, int32_t px, int32_t py) 920 +{ 921 + struct swc_rectangle r = calculate_position(positioner), f; 922 + struct swc_rectangle *g = screen_at(px + r.x, py + r.y); 923 + struct xdg_positioner flipped; 924 + 925 + if (!g) { 926 + return r; 927 + } 928 + if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_Y && 929 + py + r.y + r.height > g->y + (int32_t)g->height) { 930 + flipped = *positioner; 931 + flipped.anchor = mirror_y(flipped.anchor); 932 + flipped.gravity = mirror_y(flipped.gravity); 933 + flipped.offset_y = -flipped.offset_y; 934 + f = calculate_position(&flipped); 935 + if (py + f.y >= g->y) { 936 + r.y = f.y; 937 + } 938 + } 939 + if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_FLIP_X && 940 + px + r.x + r.width > g->x + (int32_t)g->width) { 941 + flipped = *positioner; 942 + flipped.anchor = mirror_x(flipped.anchor); 943 + flipped.gravity = mirror_x(flipped.gravity); 944 + flipped.offset_x = -flipped.offset_x; 945 + f = calculate_position(&flipped); 946 + if (px + f.x >= g->x) { 947 + r.x = f.x; 948 + } 949 + } 950 + if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y) { 951 + if (py + r.y + r.height > g->y + (int32_t)g->height) { 952 + r.y = g->y + (int32_t)g->height - r.height - py; 953 + } 954 + if (py + r.y < g->y) { 955 + r.y = g->y - py; 956 + } 957 + } 958 + if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X) { 959 + if (px + r.x + r.width > g->x + (int32_t)g->width) { 960 + r.x = g->x + (int32_t)g->width - r.width - px; 961 + } 962 + if (px + r.x < g->x) { 963 + r.x = g->x - px; 964 + } 965 + } 966 + /* cut what still does not fit and tell the client */ 967 + if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_Y && 968 + py + r.y + r.height > g->y + (int32_t)g->height) { 969 + r.height = g->y + (int32_t)g->height - py - r.y; 970 + } 971 + if (positioner->constraint & XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_RESIZE_X && 972 + px + r.x + r.width > g->x + (int32_t)g->width) { 973 + r.width = g->x + (int32_t)g->width - px - r.x; 974 + } 975 + return r; 976 +} 977 + 978 +/* place popup against its parent and tell the client */ 979 +static bool 980 +xdg_popup_place(struct xdg_popup *popup, struct compositor_view *parent_view) 981 +{ 982 + uint32_t serial = wl_display_next_serial(swc.display); 983 + struct swc_rectangle rect; 984 + 985 + if (!parent_view) { 986 + return false; 987 + } 988 + rect = constrained_position(&popup->positioner, parent_view->base.geometry.x, 989 + parent_view->base.geometry.y); 990 + compositor_view_set_parent(popup->view, parent_view); 991 + /* above the parent, in a layer of normal at least */ 992 + popup->view->always_top = parent_view->always_top; 993 + compositor_view_set_z(popup->view, parent_view->z > STACK_LAYER_NORMAL ? parent_view->z : STACK_LAYER_NORMAL, 994 + true); 995 + view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, 996 + parent_view->base.geometry.y + rect.y); 997 + xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, 998 + rect.height); 999 + xdg_surface_send_configure(popup->xdg_surface->resource, serial); 1000 + return true; 1001 +} 1002 + 1003 +void 1004 +xdg_popup_set_parent(struct wl_resource *resource, struct surface *parent) 1005 +{ 1006 + struct xdg_popup *popup; 1007 + 1008 + if (!resource || !wl_resource_instance_of(resource, &xdg_popup_interface, &popup_impl)) { 1009 + return; 1010 + } 1011 + popup = wl_resource_get_user_data(resource); 1012 + xdg_popup_place(popup, compositor_view(parent->view)); 1013 +} 1014 + 1015 +/* a popup with no parent waits for one, as layer surface popups do */ 1016 static struct xdg_popup * 1017 xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, 1018 struct xdg_surface *xdg_surface, struct xdg_surface *parent, 1019 @@ -548,11 +792,9 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, 1020 { 1021 struct xdg_popup *popup; 1022 struct compositor_view *parent_view = 1023 - compositor_view(parent->surface->view); 1024 - uint32_t serial = wl_display_next_serial(swc.display); 1025 - struct swc_rectangle rect; 1026 + parent ? compositor_view(parent->surface->view) : NULL; 1027 1028 - if (!parent_view) { 1029 + if (parent && !parent_view) { 1030 goto error0; 1031 } 1032 popup = malloc(sizeof(*popup)); 1033 @@ -561,6 +803,7 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, 1034 } 1035 popup->xdg_surface = xdg_surface; 1036 popup->positioner = *positioner; 1037 + popup->grabbing = false; 1038 popup->resource = 1039 wl_resource_create(client, &xdg_popup_interface, version, id); 1040 if (!popup->resource) { 1041 @@ -575,14 +818,9 @@ xdg_popup_new(struct wl_client *client, uint32_t version, uint32_t id, 1042 } 1043 wl_resource_set_implementation(popup->resource, &popup_impl, popup, 1044 &destroy_popup); 1045 - 1046 - rect = calculate_position(positioner); 1047 - compositor_view_set_parent(popup->view, parent_view); 1048 - view_move(&popup->view->base, parent_view->base.geometry.x + rect.x, 1049 - parent_view->base.geometry.y + rect.y); 1050 - xdg_popup_send_configure(popup->resource, rect.x, rect.y, rect.width, 1051 - rect.height); 1052 - xdg_surface_send_configure(xdg_surface->resource, serial); 1053 + if (parent_view) { 1054 + xdg_popup_place(popup, parent_view); 1055 + } 1056 1057 return popup; 1058 1059 @@ -630,7 +868,8 @@ get_popup(struct wl_client *client, struct wl_resource *resource, uint32_t id, 1060 struct wl_resource *positioner_resource) 1061 { 1062 struct xdg_surface *xdg_surface = wl_resource_get_user_data(resource); 1063 - struct xdg_surface *parent = wl_resource_get_user_data(parent_resource); 1064 + struct xdg_surface *parent = 1065 + parent_resource ? wl_resource_get_user_data(parent_resource) : NULL; 1066 struct xdg_positioner *positioner = 1067 wl_resource_get_user_data(positioner_resource); 1068 struct xdg_popup *popup; 1069 diff --git a/libswc/xdg_shell.h b/libswc/xdg_shell.h 1070 index e7bfaa7..de6997b 100644 1071 --- a/libswc/xdg_shell.h 1072 +++ b/libswc/xdg_shell.h 1073 @@ -25,8 +25,26 @@ 1074 #define SWC_XDG_SHELL_H 1075 1076 struct wl_display; 1077 +struct wl_resource; 1078 +struct window; 1079 1080 struct wl_global * 1081 xdg_shell_create(struct wl_display *display); 1082 1083 +struct surface; 1084 + 1085 +/* give a parentless popup its parent and place it */ 1086 +void 1087 +xdg_popup_set_parent(struct wl_resource *popup, struct surface *parent); 1088 + 1089 +/* keyboard focus moved to client (NULL for none): grabbing popups end unless 1090 + * it is theirs */ 1091 +struct wl_client; 1092 +void 1093 +xdg_popups_focus_changed(struct wl_client *client); 1094 + 1095 +/* window of an xdg_toplevel */ 1096 +struct window * 1097 +xdg_toplevel_window(struct wl_resource *resource); 1098 + 1099 #endif