window.c (20007B)
1 /* swc: libswc/window.c 2 * 3 * Copyright (c) 2013 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 "window.h" 25 #include "compositor.h" 26 #include "event.h" 27 #include "internal.h" 28 #include "keyboard.h" 29 #include "layer_shell.h" 30 #include "seat.h" 31 #include "surface.h" 32 #include "swc.h" 33 #include "util.h" 34 #include "view.h" 35 36 #include <stdlib.h> 37 #include <string.h> 38 #include <sys/types.h> 39 40 #define INTERNAL(w) ((struct window *)(w)) 41 42 static const uint32_t def_motion_throttle_ms = 16; 43 44 static const struct swc_window_handler null_handler; 45 46 static bool 47 should_throttle_motion(uint32_t throttle_ms, uint32_t *last_time, uint32_t time) 48 { 49 if (!throttle_ms) { 50 return false; 51 } 52 53 if (*last_time && time - *last_time < throttle_ms) { 54 return true; 55 } 56 57 *last_time = time; 58 return false; 59 } 60 61 static uint32_t 62 clamp_dimension(int32_t value, uint32_t min, uint32_t max) 63 { 64 if (value < 0) { 65 value = 0; 66 } 67 68 if (min && value < min) { 69 value = min; 70 } 71 72 if (max) { 73 if (min && max < min) { 74 max = min; 75 } 76 77 if (value > max) { 78 value = max; 79 } 80 } 81 82 if (value > UINT32_MAX) { 83 value = UINT32_MAX; 84 } 85 86 return value; 87 } 88 89 static void 90 clamp_window_size(const struct window *window, uint32_t *width, 91 uint32_t *height) 92 { 93 *width = 94 clamp_dimension(*width, window->base.min_width, window->base.max_width); 95 *height = clamp_dimension(*height, window->base.min_height, 96 window->base.max_height); 97 } 98 99 void 100 window_pointer_entered(struct window *window) 101 { 102 if (window->handler->entered) { 103 window->handler->entered(window->handler_data); 104 } 105 } 106 107 static void 108 handle_window_enter(struct wl_listener *listener, void *data) 109 { 110 struct event *event = data; 111 struct input_focus_event_data *event_data = event->data; 112 struct window *window; 113 114 if (event->type != INPUT_FOCUS_EVENT_CHANGED) { 115 return; 116 } 117 118 if (!event_data->new || !(window = event_data->new->window)) { 119 return; 120 } 121 122 window_pointer_entered(window); 123 } 124 125 struct wl_listener window_enter_listener = { 126 .notify = handle_window_enter, 127 }; 128 129 static void 130 begin_interaction(struct window_pointer_interaction *interaction, 131 struct button *button) 132 { 133 if (button) { 134 /* Store the serial of the button press so we are able to cancel the 135 * interaction if the window changes from stacked mode. */ 136 interaction->serial = button->press.serial; 137 interaction->original_handler = button->handler; 138 button->handler = &interaction->handler; 139 } else { 140 interaction->original_handler = NULL; 141 } 142 143 interaction->active = true; 144 wl_list_insert(&swc.seat->pointer->handlers, &interaction->handler.link); 145 } 146 147 static void 148 end_interaction(struct window_pointer_interaction *interaction, 149 struct button *button) 150 { 151 if (!interaction->active) { 152 return; 153 } 154 155 if (interaction->original_handler) { 156 if (!button) { 157 button = pointer_get_button(swc.seat->pointer, interaction->serial); 158 159 if (!button) { 160 WARNING("No button with serial %u\n", interaction->serial); 161 goto remove; 162 } 163 } 164 165 interaction->original_handler->button(interaction->original_handler, 166 get_time(), button, 167 WL_POINTER_BUTTON_STATE_RELEASED); 168 } 169 170 remove: 171 interaction->active = false; 172 wl_list_remove(&interaction->handler.link); 173 } 174 175 static void 176 flush(struct window *window) 177 { 178 if (window->move.pending) { 179 if (window->impl->move) { 180 window->impl->move(window, window->move.x, window->move.y); 181 } 182 183 view_move(&window->view->base, window->move.x, window->move.y); 184 window->move.pending = false; 185 } 186 } 187 188 EXPORT void 189 swc_window_set_handler(struct swc_window *base, 190 const struct swc_window_handler *handler, void *data) 191 { 192 struct window *window = INTERNAL(base); 193 194 window->handler = handler; 195 window->handler_data = data; 196 } 197 198 EXPORT void 199 swc_window_set_backdrop(struct swc_window *base, uint32_t margin, 200 void (*hook)(void *data, const struct swc_backdrop *backdrop), void *data) 201 { 202 struct window *window = INTERNAL(base); 203 204 if (window->view) 205 compositor_view_set_backdrop(window->view, margin, hook, data); 206 } 207 208 EXPORT void 209 swc_window_set_z(struct swc_window *base, int32_t z) 210 { 211 struct window *window = INTERNAL(base); 212 213 if (window->view) 214 compositor_view_set_z(window->view, z, true); 215 } 216 217 EXPORT void 218 swc_window_close(struct swc_window *base) 219 { 220 struct window *window = INTERNAL(base); 221 222 if (window->impl->close) { 223 window->impl->close(window); 224 } 225 } 226 227 EXPORT void 228 swc_window_show(struct swc_window *window) 229 { 230 compositor_view_show(INTERNAL(window)->view); 231 } 232 233 EXPORT void 234 swc_window_hide(struct swc_window *window) 235 { 236 compositor_view_hide(INTERNAL(window)->view); 237 } 238 239 EXPORT void 240 swc_window_focus(struct swc_window *base) 241 { 242 struct window *window = INTERNAL(base); 243 struct compositor_view *new = window ? window->view : NULL, 244 *old = swc.seat->keyboard->focus.view; 245 246 if (new == old || layer_shell_hold_keys(new)) { 247 return; 248 } 249 250 /* Focus the new window before unfocusing the old one in case both are X11 251 * windows so the xwl_window implementation can handle this transition 252 * correctly. */ 253 if (window && window->impl->focus) { 254 window->impl->focus(window); 255 } 256 if (old && old->window && old->window->impl->unfocus) { 257 old->window->impl->unfocus(old->window); 258 } 259 260 keyboard_set_focus(swc.seat->keyboard, new); 261 } 262 263 EXPORT void 264 swc_window_set_parent(struct swc_window *base, struct swc_window *parent) 265 { 266 window_set_parent(INTERNAL(base), parent ? INTERNAL(parent) : NULL); 267 } 268 269 EXPORT void 270 swc_window_set_attached(struct swc_window *base, bool attached) 271 { 272 struct window *window = INTERNAL(base); 273 274 if (window->view) { 275 window->view->attached = attached; 276 } 277 } 278 279 EXPORT void 280 swc_window_raise(struct swc_window *base) 281 { 282 struct window *window = INTERNAL(base); 283 284 if (window->view) { 285 raise_window(window->view); 286 } 287 } 288 289 EXPORT void 290 swc_window_set_stacked(struct swc_window *base) 291 { 292 struct window *window = INTERNAL(base); 293 294 flush(window); 295 window->configure.pending = false; 296 window->configure.width = 0; 297 window->configure.height = 0; 298 if (window->impl->set_mode) { 299 window->impl->set_mode(window, WINDOW_MODE_STACKED); 300 } 301 window->mode = WINDOW_MODE_STACKED; 302 } 303 304 EXPORT void 305 swc_window_set_tiled(struct swc_window *base) 306 { 307 struct window *window = INTERNAL(base); 308 309 end_interaction(&window->move.interaction, NULL); 310 end_interaction(&window->resize.interaction, NULL); 311 if (window->impl->set_mode) { 312 window->impl->set_mode(window, WINDOW_MODE_TILED); 313 } 314 window->mode = WINDOW_MODE_TILED; 315 } 316 317 EXPORT void 318 swc_window_set_fullscreen(struct swc_window *base, struct swc_screen *screen) 319 { 320 struct window *window = INTERNAL(base); 321 322 struct swc_rectangle geom; 323 swc_window_get_geometry(base, &geom); 324 325 if (window->mode != WINDOW_MODE_FULLSCREEN) { 326 window->prev.geom = geom; 327 window->prev.mode = window->mode; 328 swc_window_set_geometry(base, &screen->usable_geometry); 329 330 if (window->impl->set_mode) { 331 window->impl->set_mode(window, WINDOW_MODE_FULLSCREEN); 332 } 333 window->mode = WINDOW_MODE_FULLSCREEN; 334 } 335 336 else { 337 swc_window_set_geometry(base, &window->prev.geom); 338 window->mode = window->prev.mode; 339 } 340 } 341 342 EXPORT void 343 swc_window_set_position(struct swc_window *base, int32_t x, int32_t y) 344 { 345 struct window *window = INTERNAL(base); 346 struct swc_rectangle *geometry = &window->view->base.geometry; 347 348 if (x == geometry->x && y == geometry->y) { 349 window->move.pending = false; 350 return; 351 } 352 353 window->move.x = x; 354 window->move.y = y; 355 window->move.pending = true; 356 357 /* If we don't have a configure pending, perform the move now. */ 358 if (!window->configure.pending) { 359 flush(window); 360 } 361 } 362 363 EXPORT void 364 swc_window_set_size(struct swc_window *base, uint32_t width, uint32_t height) 365 { 366 struct window *window = INTERNAL(base); 367 struct swc_rectangle *geom = &window->view->base.geometry; 368 369 clamp_window_size(window, &width, &height); 370 371 if ((window->configure.pending && width == window->configure.width && 372 height == window->configure.height) || 373 (!window->configure.pending && width == geom->width && 374 height == geom->height)) { 375 return; 376 } 377 378 window->impl->configure(window, width, height); 379 380 if (window->mode == WINDOW_MODE_TILED) { 381 window->configure.width = width; 382 window->configure.height = height; 383 window->configure.pending = true; 384 } 385 } 386 387 EXPORT void 388 swc_window_set_geometry(struct swc_window *window, 389 const struct swc_rectangle *geometry) 390 { 391 swc_window_set_size(window, geometry->width, geometry->height); 392 swc_window_set_position(window, geometry->x, geometry->y); 393 } 394 395 EXPORT bool 396 swc_window_get_geometry(const struct swc_window *base, 397 struct swc_rectangle *geometry) 398 { 399 struct window *window = INTERNAL((struct swc_window *)base); 400 401 if (!window || !geometry) { 402 return false; 403 } 404 405 *geometry = window->view->base.geometry; 406 return true; 407 } 408 409 EXPORT void 410 swc_window_set_border(struct swc_window *window, uint32_t inner_border_color, 411 uint32_t inner_border_width, uint32_t outer_border_color, 412 uint32_t outer_border_width) 413 { 414 struct compositor_view *view = INTERNAL(window)->view; 415 416 compositor_view_set_border_color(view, outer_border_color, 417 inner_border_color); 418 compositor_view_set_border_width(view, outer_border_width, 419 inner_border_width); 420 } 421 422 EXPORT void 423 swc_window_set_border_placement(struct swc_window *window, 424 enum swc_border_placement placement) 425 { 426 compositor_view_set_border_frame(INTERNAL(window)->view, 427 placement == SWC_BORDER_FRAME); 428 } 429 430 EXPORT void 431 swc_window_set_decor(struct swc_window *window, const struct swc_decor *decor) 432 { 433 struct compositor_view *view = INTERNAL(window)->view; 434 435 compositor_view_set_decor(view, decor); 436 } 437 438 EXPORT void 439 swc_window_begin_move(struct swc_window *window) 440 { 441 window_begin_move(INTERNAL(window), NULL); 442 } 443 444 EXPORT void 445 swc_window_end_move(struct swc_window *window) 446 { 447 end_interaction(&INTERNAL(window)->move.interaction, NULL); 448 } 449 450 EXPORT void 451 swc_window_begin_resize(struct swc_window *window, uint32_t edges) 452 { 453 window_begin_resize(INTERNAL(window), edges, NULL); 454 } 455 456 EXPORT void 457 swc_window_end_resize(struct swc_window *window) 458 { 459 end_interaction(&INTERNAL(window)->resize.interaction, NULL); 460 } 461 462 static bool 463 move_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, 464 wl_fixed_t fy) 465 { 466 struct window *window = 467 wl_container_of(handler, window, move.interaction.handler); 468 469 if (should_throttle_motion(window->base.motion_throttle_ms, 470 &window->move.last_time, time)) { 471 return true; 472 } 473 474 int32_t x = wl_fixed_to_int(fx) + window->move.offset.x, 475 y = wl_fixed_to_int(fy) + window->move.offset.y; 476 477 view_move(&window->view->base, x, y); 478 return true; 479 } 480 481 static bool 482 resize_motion(struct pointer_handler *handler, uint32_t time, wl_fixed_t fx, 483 wl_fixed_t fy) 484 { 485 struct window *window = 486 wl_container_of(handler, window, resize.interaction.handler); 487 const struct swc_rectangle *geometry = &window->view->base.geometry; 488 uint32_t width = geometry->width, height = geometry->height; 489 490 if (should_throttle_motion(window->base.motion_throttle_ms, 491 &window->resize.last_time, time)) { 492 return true; 493 } 494 495 if (window->resize.edges & SWC_WINDOW_EDGE_LEFT) { 496 width -= wl_fixed_to_int(fx) + window->resize.offset.x - geometry->x; 497 } else if (window->resize.edges & SWC_WINDOW_EDGE_RIGHT) { 498 width = wl_fixed_to_int(fx) + window->resize.offset.x - geometry->x; 499 } 500 501 if (window->resize.edges & SWC_WINDOW_EDGE_TOP) { 502 height -= wl_fixed_to_int(fy) + window->resize.offset.y - geometry->y; 503 } else if (window->resize.edges & SWC_WINDOW_EDGE_BOTTOM) { 504 height = wl_fixed_to_int(fy) + window->resize.offset.y - geometry->y; 505 } 506 507 clamp_window_size(window, &width, &height); 508 window->impl->configure(window, width, height); 509 510 return true; 511 } 512 513 static bool 514 handle_button(struct pointer_handler *handler, uint32_t time, 515 struct button *button, uint32_t state) 516 { 517 struct window_pointer_interaction *interaction = 518 wl_container_of(handler, interaction, handler); 519 520 if (state != WL_POINTER_BUTTON_STATE_RELEASED || 521 !interaction->original_handler) { 522 return false; 523 } 524 525 end_interaction(interaction, button); 526 return true; 527 } 528 529 static void 530 handle_attach(struct view_handler *handler) 531 { 532 struct window *window = wl_container_of(handler, window, view_handler); 533 534 if (window->configure.acknowledged) { 535 flush(window); 536 } 537 window->configure.pending = false; 538 } 539 540 static void 541 handle_resize(struct view_handler *handler, uint32_t old_width, 542 uint32_t old_height) 543 { 544 struct window *window = wl_container_of(handler, window, view_handler); 545 546 if (window->resize.interaction.active && 547 window->resize.edges & (SWC_WINDOW_EDGE_TOP | SWC_WINDOW_EDGE_LEFT)) { 548 const struct swc_rectangle *geometry = &window->view->base.geometry; 549 int32_t x = geometry->x, y = geometry->y; 550 551 if (window->resize.edges & SWC_WINDOW_EDGE_LEFT) { 552 x += old_width - geometry->width; 553 } 554 if (window->resize.edges & SWC_WINDOW_EDGE_TOP) { 555 y += old_height - geometry->height; 556 } 557 558 view_move(&window->view->base, x, y); 559 } 560 561 if (window->handler->size_changed) { 562 window->handler->size_changed(window->handler_data); 563 } 564 } 565 566 static const struct view_handler_impl view_handler_impl = { 567 .attach = handle_attach, 568 .resize = handle_resize, 569 }; 570 571 bool 572 window_initialize(struct window *window, const struct window_impl *impl, 573 struct surface *surface) 574 { 575 DEBUG("Initializing window, %p\n", window); 576 577 window->base.title = NULL; 578 window->base.app_id = NULL; 579 window->base.parent = NULL; 580 581 if (surface->view) { 582 window->view = compositor_view(surface->view); 583 if (!window->view || window->view->window) { 584 return false; 585 } 586 } else { 587 if (!(window->view = compositor_create_view(surface))) { 588 return false; 589 } 590 } 591 592 window->impl = impl; 593 window->handler = &null_handler; 594 window->view_handler.impl = &view_handler_impl; 595 window->view->window = window; 596 window->base.motion_throttle_ms = def_motion_throttle_ms; 597 window->base.min_width = 0; 598 window->base.min_height = 0; 599 window->base.max_width = 0; 600 window->base.max_height = 0; 601 window->managed = false; 602 window->mode = WINDOW_MODE_STACKED; 603 window->move.pending = false; 604 window->move.last_time = 0; 605 window->move.interaction.active = false; 606 window->move.interaction.handler = (struct pointer_handler){ 607 .motion = move_motion, 608 .button = handle_button, 609 }; 610 window->configure.pending = false; 611 window->configure.width = 0; 612 window->configure.height = 0; 613 window->resize.interaction.active = false; 614 window->resize.interaction.handler = (struct pointer_handler){ 615 .motion = resize_motion, 616 .button = handle_button, 617 }; 618 window->resize.last_time = 0; 619 620 wl_list_insert(&window->view->base.handlers, &window->view_handler.link); 621 622 return true; 623 } 624 625 void 626 window_finalize(struct window *window) 627 { 628 DEBUG("Finalizing window, %p\n", window); 629 630 window_unmanage(window); 631 compositor_view_destroy(window->view); 632 free(window->base.title); 633 free(window->base.app_id); 634 } 635 636 void 637 window_manage(struct window *window) 638 { 639 if (window->managed) { 640 return; 641 } 642 643 swc.manager->new_window(&window->base); 644 window->managed = true; 645 } 646 647 void 648 window_unmanage(struct window *window) 649 { 650 if (!window->managed) { 651 return; 652 } 653 654 if (window->handler->destroy) { 655 window->handler->destroy(window->handler_data); 656 } 657 window->handler = &null_handler; 658 window->managed = false; 659 } 660 661 void 662 window_set_title(struct window *window, const char *title, size_t length) 663 { 664 free(window->base.title); 665 window->base.title = strndup(title, length); 666 667 if (window->handler->title_changed) { 668 window->handler->title_changed(window->handler_data); 669 } 670 } 671 672 void 673 window_set_app_id(struct window *window, const char *app_id) 674 { 675 free(window->base.app_id); 676 window->base.app_id = strdup(app_id); 677 678 if (window->handler->app_id_changed) { 679 window->handler->app_id_changed(window->handler_data); 680 } 681 } 682 683 void 684 window_set_modal(struct window *window, bool modal) 685 { 686 if (window->base.modal == modal) { 687 return; 688 } 689 window->base.modal = modal; 690 if (window->handler->modal_changed) { 691 window->handler->modal_changed(window->handler_data); 692 } 693 } 694 695 void 696 window_set_parent(struct window *window, struct window *parent) 697 { 698 struct swc_window *base = parent ? &parent->base : NULL; 699 700 if (window->base.parent == base || window == parent) { 701 return; 702 } 703 704 compositor_view_set_parent(window->view, parent ? parent->view : NULL); 705 window->base.parent = base; 706 if (parent) { 707 raise_window(parent->view); 708 } 709 710 if (window->handler->parent_changed) { 711 window->handler->parent_changed(window->handler_data); 712 } 713 } 714 715 void 716 window_begin_move(struct window *window, struct button *button) 717 { 718 if (window->mode != WINDOW_MODE_STACKED && window->handler->move) { 719 window->handler->move(window->handler_data); 720 } 721 722 if (window->mode != WINDOW_MODE_STACKED || 723 window->move.interaction.active) { 724 return; 725 } 726 727 struct swc_rectangle *geometry = &window->view->base.geometry; 728 int32_t px = wl_fixed_to_int(swc.seat->pointer->x), 729 py = wl_fixed_to_int(swc.seat->pointer->y); 730 731 begin_interaction(&window->move.interaction, button); 732 window->move.last_time = 0; 733 window->move.offset.x = geometry->x - px; 734 window->move.offset.y = geometry->y - py; 735 } 736 737 void 738 window_begin_resize(struct window *window, uint32_t edges, 739 struct button *button) 740 { 741 if (window->mode != WINDOW_MODE_STACKED && window->handler->resize) { 742 window->handler->resize(window->handler_data); 743 } 744 745 if (window->mode != WINDOW_MODE_STACKED || 746 window->resize.interaction.active) { 747 return; 748 } 749 750 struct swc_rectangle *geometry = &window->view->base.geometry; 751 int32_t px = wl_fixed_to_int(swc.seat->pointer->x), 752 py = wl_fixed_to_int(swc.seat->pointer->y); 753 754 begin_interaction(&window->resize.interaction, button); 755 window->resize.last_time = 0; 756 757 if (!edges) { 758 edges |= (px < geometry->x + geometry->width / 2) 759 ? SWC_WINDOW_EDGE_LEFT 760 : SWC_WINDOW_EDGE_RIGHT; 761 edges |= (py < geometry->y + geometry->height / 2) 762 ? SWC_WINDOW_EDGE_TOP 763 : SWC_WINDOW_EDGE_BOTTOM; 764 } 765 766 window->resize.offset.x = 767 geometry->x - px + 768 ((edges & SWC_WINDOW_EDGE_RIGHT) ? geometry->width : 0); 769 window->resize.offset.y = 770 geometry->y - py + 771 ((edges & SWC_WINDOW_EDGE_BOTTOM) ? geometry->height : 0); 772 window->resize.edges = edges; 773 } 774 775 EXPORT pid_t 776 swc_window_get_pid(struct swc_window *base) 777 { 778 struct window *window = INTERNAL(base); 779 struct surface *surface; 780 struct wl_client *client; 781 pid_t pid; 782 uid_t uid; 783 gid_t gid; 784 785 if (!window || !window->view || !window->view->surface) { 786 return 0; 787 } 788 789 surface = window->view->surface; 790 if (!surface->resource) { 791 return 0; 792 } 793 794 client = wl_resource_get_client(surface->resource); 795 wl_client_get_credentials(client, &pid, &uid, &gid); 796 797 return pid; 798 }