1 /* $OpenBSD: window.c,v 1.267 2020/12/15 08:31:50 nicm Exp $ */ 2 3 /* 4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 15 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 16 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include <sys/ioctl.h> 21 22 #include <ctype.h> 23 #include <errno.h> 24 #include <fcntl.h> 25 #include <fnmatch.h> 26 #include <regex.h> 27 #include <signal.h> 28 #include <stdint.h> 29 #include <stdlib.h> 30 #include <string.h> 31 #include <time.h> 32 #include <unistd.h> 33 #include <util.h> 34 #include <vis.h> 35 36 #include "tmux.h" 37 38 /* 39 * Each window is attached to a number of panes, each of which is a pty. This 40 * file contains code to handle them. 41 * 42 * A pane has two buffers attached, these are filled and emptied by the main 43 * server poll loop. Output data is received from pty's in screen format, 44 * translated and returned as a series of escape sequences and strings via 45 * input_parse (in input.c). Input data is received as key codes and written 46 * directly via input_key. 47 * 48 * Each pane also has a "virtual" screen (screen.c) which contains the current 49 * state and is redisplayed when the window is reattached to a client. 50 * 51 * Windows are stored directly on a global array and wrapped in any number of 52 * winlink structs to be linked onto local session RB trees. A reference count 53 * is maintained and a window removed from the global list and destroyed when 54 * it reaches zero. 55 */ 56 57 /* Global window list. */ 58 struct windows windows; 59 60 /* Global panes tree. */ 61 struct window_pane_tree all_window_panes; 62 static u_int next_window_pane_id; 63 static u_int next_window_id; 64 static u_int next_active_point; 65 66 /* List of window modes. */ 67 const struct window_mode *all_window_modes[] = { 68 &window_buffer_mode, 69 &window_client_mode, 70 &window_clock_mode, 71 &window_copy_mode, 72 &window_tree_mode, 73 &window_view_mode, 74 NULL 75 }; 76 77 struct window_pane_input_data { 78 struct cmdq_item *item; 79 u_int wp; 80 }; 81 82 static struct window_pane *window_pane_create(struct window *, u_int, u_int, 83 u_int); 84 static void window_pane_destroy(struct window_pane *); 85 86 RB_GENERATE(windows, window, entry, window_cmp); 87 RB_GENERATE(winlinks, winlink, entry, winlink_cmp); 88 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp); 89 90 int 91 window_cmp(struct window *w1, struct window *w2) 92 { 93 return (w1->id - w2->id); 94 } 95 96 int 97 winlink_cmp(struct winlink *wl1, struct winlink *wl2) 98 { 99 return (wl1->idx - wl2->idx); 100 } 101 102 int 103 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2) 104 { 105 return (wp1->id - wp2->id); 106 } 107 108 struct winlink * 109 winlink_find_by_window(struct winlinks *wwl, struct window *w) 110 { 111 struct winlink *wl; 112 113 RB_FOREACH(wl, winlinks, wwl) { 114 if (wl->window == w) 115 return (wl); 116 } 117 118 return (NULL); 119 } 120 121 struct winlink * 122 winlink_find_by_index(struct winlinks *wwl, int idx) 123 { 124 struct winlink wl; 125 126 if (idx < 0) 127 fatalx("bad index"); 128 129 wl.idx = idx; 130 return (RB_FIND(winlinks, wwl, &wl)); 131 } 132 133 struct winlink * 134 winlink_find_by_window_id(struct winlinks *wwl, u_int id) 135 { 136 struct winlink *wl; 137 138 RB_FOREACH(wl, winlinks, wwl) { 139 if (wl->window->id == id) 140 return (wl); 141 } 142 return (NULL); 143 } 144 145 static int 146 winlink_next_index(struct winlinks *wwl, int idx) 147 { 148 int i; 149 150 i = idx; 151 do { 152 if (winlink_find_by_index(wwl, i) == NULL) 153 return (i); 154 if (i == INT_MAX) 155 i = 0; 156 else 157 i++; 158 } while (i != idx); 159 return (-1); 160 } 161 162 u_int 163 winlink_count(struct winlinks *wwl) 164 { 165 struct winlink *wl; 166 u_int n; 167 168 n = 0; 169 RB_FOREACH(wl, winlinks, wwl) 170 n++; 171 172 return (n); 173 } 174 175 struct winlink * 176 winlink_add(struct winlinks *wwl, int idx) 177 { 178 struct winlink *wl; 179 180 if (idx < 0) { 181 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1) 182 return (NULL); 183 } else if (winlink_find_by_index(wwl, idx) != NULL) 184 return (NULL); 185 186 wl = xcalloc(1, sizeof *wl); 187 wl->idx = idx; 188 RB_INSERT(winlinks, wwl, wl); 189 190 return (wl); 191 } 192 193 void 194 winlink_set_window(struct winlink *wl, struct window *w) 195 { 196 if (wl->window != NULL) { 197 TAILQ_REMOVE(&wl->window->winlinks, wl, wentry); 198 window_remove_ref(wl->window, __func__); 199 } 200 TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry); 201 wl->window = w; 202 window_add_ref(w, __func__); 203 } 204 205 void 206 winlink_remove(struct winlinks *wwl, struct winlink *wl) 207 { 208 struct window *w = wl->window; 209 210 if (w != NULL) { 211 TAILQ_REMOVE(&w->winlinks, wl, wentry); 212 window_remove_ref(w, __func__); 213 } 214 215 RB_REMOVE(winlinks, wwl, wl); 216 free(wl); 217 } 218 219 struct winlink * 220 winlink_next(struct winlink *wl) 221 { 222 return (RB_NEXT(winlinks, wwl, wl)); 223 } 224 225 struct winlink * 226 winlink_previous(struct winlink *wl) 227 { 228 return (RB_PREV(winlinks, wwl, wl)); 229 } 230 231 struct winlink * 232 winlink_next_by_number(struct winlink *wl, struct session *s, int n) 233 { 234 for (; n > 0; n--) { 235 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL) 236 wl = RB_MIN(winlinks, &s->windows); 237 } 238 239 return (wl); 240 } 241 242 struct winlink * 243 winlink_previous_by_number(struct winlink *wl, struct session *s, int n) 244 { 245 for (; n > 0; n--) { 246 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL) 247 wl = RB_MAX(winlinks, &s->windows); 248 } 249 250 return (wl); 251 } 252 253 void 254 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl) 255 { 256 if (wl == NULL) 257 return; 258 259 winlink_stack_remove(stack, wl); 260 TAILQ_INSERT_HEAD(stack, wl, sentry); 261 } 262 263 void 264 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl) 265 { 266 struct winlink *wl2; 267 268 if (wl == NULL) 269 return; 270 271 TAILQ_FOREACH(wl2, stack, sentry) { 272 if (wl2 == wl) { 273 TAILQ_REMOVE(stack, wl, sentry); 274 return; 275 } 276 } 277 } 278 279 struct window * 280 window_find_by_id_str(const char *s) 281 { 282 const char *errstr; 283 u_int id; 284 285 if (*s != '@') 286 return (NULL); 287 288 id = strtonum(s + 1, 0, UINT_MAX, &errstr); 289 if (errstr != NULL) 290 return (NULL); 291 return (window_find_by_id(id)); 292 } 293 294 struct window * 295 window_find_by_id(u_int id) 296 { 297 struct window w; 298 299 w.id = id; 300 return (RB_FIND(windows, &windows, &w)); 301 } 302 303 void 304 window_update_activity(struct window *w) 305 { 306 gettimeofday(&w->activity_time, NULL); 307 alerts_queue(w, WINDOW_ACTIVITY); 308 } 309 310 struct window * 311 window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel) 312 { 313 struct window *w; 314 315 if (xpixel == 0) 316 xpixel = DEFAULT_XPIXEL; 317 if (ypixel == 0) 318 ypixel = DEFAULT_YPIXEL; 319 320 w = xcalloc(1, sizeof *w); 321 w->name = xstrdup(""); 322 w->flags = 0; 323 324 TAILQ_INIT(&w->panes); 325 w->active = NULL; 326 327 w->lastlayout = -1; 328 w->layout_root = NULL; 329 330 w->sx = sx; 331 w->sy = sy; 332 w->xpixel = xpixel; 333 w->ypixel = ypixel; 334 335 w->options = options_create(global_w_options); 336 337 w->references = 0; 338 TAILQ_INIT(&w->winlinks); 339 340 w->id = next_window_id++; 341 RB_INSERT(windows, &windows, w); 342 343 window_update_activity(w); 344 345 return (w); 346 } 347 348 static void 349 window_destroy(struct window *w) 350 { 351 log_debug("window @%u destroyed (%d references)", w->id, w->references); 352 353 RB_REMOVE(windows, &windows, w); 354 355 if (w->layout_root != NULL) 356 layout_free_cell(w->layout_root); 357 if (w->saved_layout_root != NULL) 358 layout_free_cell(w->saved_layout_root); 359 free(w->old_layout); 360 361 window_destroy_panes(w); 362 363 if (event_initialized(&w->name_event)) 364 evtimer_del(&w->name_event); 365 366 if (event_initialized(&w->alerts_timer)) 367 evtimer_del(&w->alerts_timer); 368 if (event_initialized(&w->offset_timer)) 369 event_del(&w->offset_timer); 370 371 options_free(w->options); 372 373 free(w->name); 374 free(w); 375 } 376 377 int 378 window_pane_destroy_ready(struct window_pane *wp) 379 { 380 int n; 381 382 if (wp->pipe_fd != -1) { 383 if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0) 384 return (0); 385 if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0) 386 return (0); 387 } 388 389 if (~wp->flags & PANE_EXITED) 390 return (0); 391 return (1); 392 } 393 394 void 395 window_add_ref(struct window *w, const char *from) 396 { 397 w->references++; 398 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references); 399 } 400 401 void 402 window_remove_ref(struct window *w, const char *from) 403 { 404 w->references--; 405 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references); 406 407 if (w->references == 0) 408 window_destroy(w); 409 } 410 411 void 412 window_set_name(struct window *w, const char *new_name) 413 { 414 free(w->name); 415 utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL); 416 notify_window("window-renamed", w); 417 } 418 419 void 420 window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel) 421 { 422 if (xpixel == 0) 423 xpixel = DEFAULT_XPIXEL; 424 if (ypixel == 0) 425 ypixel = DEFAULT_YPIXEL; 426 427 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__, w->id, sx, sy, 428 xpixel == -1 ? w->xpixel : (u_int)xpixel, 429 ypixel == -1 ? w->ypixel : (u_int)ypixel); 430 w->sx = sx; 431 w->sy = sy; 432 if (xpixel != -1) 433 w->xpixel = xpixel; 434 if (ypixel != -1) 435 w->ypixel = ypixel; 436 } 437 438 void 439 window_pane_send_resize(struct window_pane *wp, int force) 440 { 441 struct window *w = wp->window; 442 struct winsize ws; 443 u_int sy; 444 445 if (wp->fd == -1) 446 return; 447 448 if (!force) 449 sy = wp->sy; 450 else if (wp->sy <= 1) 451 sy = wp->sy + 1; 452 else 453 sy = wp->sy - 1; 454 log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, wp->sx, sy); 455 456 memset(&ws, 0, sizeof ws); 457 ws.ws_col = wp->sx; 458 ws.ws_row = sy; 459 ws.ws_xpixel = w->xpixel * ws.ws_col; 460 ws.ws_ypixel = w->ypixel * ws.ws_row; 461 if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1) 462 fatal("ioctl failed"); 463 } 464 465 int 466 window_has_pane(struct window *w, struct window_pane *wp) 467 { 468 struct window_pane *wp1; 469 470 TAILQ_FOREACH(wp1, &w->panes, entry) { 471 if (wp1 == wp) 472 return (1); 473 } 474 return (0); 475 } 476 477 int 478 window_set_active_pane(struct window *w, struct window_pane *wp, int notify) 479 { 480 log_debug("%s: pane %%%u", __func__, wp->id); 481 482 if (wp == w->active) 483 return (0); 484 w->last = w->active; 485 486 w->active = wp; 487 w->active->active_point = next_active_point++; 488 w->active->flags |= PANE_CHANGED; 489 490 tty_update_window_offset(w); 491 492 if (notify) 493 notify_window("window-pane-changed", w); 494 return (1); 495 } 496 497 void 498 window_redraw_active_switch(struct window *w, struct window_pane *wp) 499 { 500 struct grid_cell *gc1, *gc2; 501 int c1, c2; 502 503 if (wp == w->active) 504 return; 505 506 for (;;) { 507 /* 508 * If the active and inactive styles or palettes are different, 509 * need to redraw the panes. 510 */ 511 gc1 = &wp->cached_gc; 512 gc2 = &wp->cached_active_gc; 513 if (!grid_cells_look_equal(gc1, gc2)) 514 wp->flags |= PANE_REDRAW; 515 else { 516 c1 = window_pane_get_palette(wp, gc1->fg); 517 c2 = window_pane_get_palette(wp, gc2->fg); 518 if (c1 != c2) 519 wp->flags |= PANE_REDRAW; 520 else { 521 c1 = window_pane_get_palette(wp, gc1->bg); 522 c2 = window_pane_get_palette(wp, gc2->bg); 523 if (c1 != c2) 524 wp->flags |= PANE_REDRAW; 525 } 526 } 527 if (wp == w->active) 528 break; 529 wp = w->active; 530 } 531 } 532 533 struct window_pane * 534 window_get_active_at(struct window *w, u_int x, u_int y) 535 { 536 struct window_pane *wp; 537 538 TAILQ_FOREACH(wp, &w->panes, entry) { 539 if (!window_pane_visible(wp)) 540 continue; 541 if (x < wp->xoff || x > wp->xoff + wp->sx) 542 continue; 543 if (y < wp->yoff || y > wp->yoff + wp->sy) 544 continue; 545 return (wp); 546 } 547 return (NULL); 548 } 549 550 struct window_pane * 551 window_find_string(struct window *w, const char *s) 552 { 553 u_int x, y, top = 0, bottom = w->sy - 1; 554 int status; 555 556 x = w->sx / 2; 557 y = w->sy / 2; 558 559 status = options_get_number(w->options, "pane-border-status"); 560 if (status == PANE_STATUS_TOP) 561 top++; 562 else if (status == PANE_STATUS_BOTTOM) 563 bottom--; 564 565 if (strcasecmp(s, "top") == 0) 566 y = top; 567 else if (strcasecmp(s, "bottom") == 0) 568 y = bottom; 569 else if (strcasecmp(s, "left") == 0) 570 x = 0; 571 else if (strcasecmp(s, "right") == 0) 572 x = w->sx - 1; 573 else if (strcasecmp(s, "top-left") == 0) { 574 x = 0; 575 y = top; 576 } else if (strcasecmp(s, "top-right") == 0) { 577 x = w->sx - 1; 578 y = top; 579 } else if (strcasecmp(s, "bottom-left") == 0) { 580 x = 0; 581 y = bottom; 582 } else if (strcasecmp(s, "bottom-right") == 0) { 583 x = w->sx - 1; 584 y = bottom; 585 } else 586 return (NULL); 587 588 return (window_get_active_at(w, x, y)); 589 } 590 591 int 592 window_zoom(struct window_pane *wp) 593 { 594 struct window *w = wp->window; 595 struct window_pane *wp1; 596 597 if (w->flags & WINDOW_ZOOMED) 598 return (-1); 599 600 if (window_count_panes(w) == 1) 601 return (-1); 602 603 if (w->active != wp) 604 window_set_active_pane(w, wp, 1); 605 606 TAILQ_FOREACH(wp1, &w->panes, entry) { 607 wp1->saved_layout_cell = wp1->layout_cell; 608 wp1->layout_cell = NULL; 609 } 610 611 w->saved_layout_root = w->layout_root; 612 layout_init(w, wp); 613 w->flags |= WINDOW_ZOOMED; 614 notify_window("window-layout-changed", w); 615 616 return (0); 617 } 618 619 int 620 window_unzoom(struct window *w) 621 { 622 struct window_pane *wp; 623 624 if (!(w->flags & WINDOW_ZOOMED)) 625 return (-1); 626 627 w->flags &= ~WINDOW_ZOOMED; 628 layout_free(w); 629 w->layout_root = w->saved_layout_root; 630 w->saved_layout_root = NULL; 631 632 TAILQ_FOREACH(wp, &w->panes, entry) { 633 wp->layout_cell = wp->saved_layout_cell; 634 wp->saved_layout_cell = NULL; 635 } 636 layout_fix_panes(w); 637 notify_window("window-layout-changed", w); 638 639 return (0); 640 } 641 642 int 643 window_push_zoom(struct window *w, int flag) 644 { 645 log_debug("%s: @%u %d", __func__, w->id, 646 flag && (w->flags & WINDOW_ZOOMED)); 647 if (flag && (w->flags & WINDOW_ZOOMED)) 648 w->flags |= WINDOW_WASZOOMED; 649 else 650 w->flags &= ~WINDOW_WASZOOMED; 651 return (window_unzoom(w) == 0); 652 } 653 654 int 655 window_pop_zoom(struct window *w) 656 { 657 log_debug("%s: @%u %d", __func__, w->id, 658 !!(w->flags & WINDOW_WASZOOMED)); 659 if (w->flags & WINDOW_WASZOOMED) 660 return (window_zoom(w->active) == 0); 661 return (0); 662 } 663 664 struct window_pane * 665 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit, 666 int flags) 667 { 668 struct window_pane *wp; 669 670 if (other == NULL) 671 other = w->active; 672 673 wp = window_pane_create(w, w->sx, w->sy, hlimit); 674 if (TAILQ_EMPTY(&w->panes)) { 675 log_debug("%s: @%u at start", __func__, w->id); 676 TAILQ_INSERT_HEAD(&w->panes, wp, entry); 677 } else if (flags & SPAWN_BEFORE) { 678 log_debug("%s: @%u before %%%u", __func__, w->id, wp->id); 679 if (flags & SPAWN_FULLSIZE) 680 TAILQ_INSERT_HEAD(&w->panes, wp, entry); 681 else 682 TAILQ_INSERT_BEFORE(other, wp, entry); 683 } else { 684 log_debug("%s: @%u after %%%u", __func__, w->id, wp->id); 685 if (flags & SPAWN_FULLSIZE) 686 TAILQ_INSERT_TAIL(&w->panes, wp, entry); 687 else 688 TAILQ_INSERT_AFTER(&w->panes, other, wp, entry); 689 } 690 return (wp); 691 } 692 693 void 694 window_lost_pane(struct window *w, struct window_pane *wp) 695 { 696 log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id); 697 698 if (wp == marked_pane.wp) 699 server_clear_marked(); 700 701 if (wp == w->active) { 702 w->active = w->last; 703 w->last = NULL; 704 if (w->active == NULL) { 705 w->active = TAILQ_PREV(wp, window_panes, entry); 706 if (w->active == NULL) 707 w->active = TAILQ_NEXT(wp, entry); 708 } 709 if (w->active != NULL) { 710 w->active->flags |= PANE_CHANGED; 711 notify_window("window-pane-changed", w); 712 } 713 } else if (wp == w->last) 714 w->last = NULL; 715 } 716 717 void 718 window_remove_pane(struct window *w, struct window_pane *wp) 719 { 720 window_lost_pane(w, wp); 721 722 TAILQ_REMOVE(&w->panes, wp, entry); 723 window_pane_destroy(wp); 724 } 725 726 struct window_pane * 727 window_pane_at_index(struct window *w, u_int idx) 728 { 729 struct window_pane *wp; 730 u_int n; 731 732 n = options_get_number(w->options, "pane-base-index"); 733 TAILQ_FOREACH(wp, &w->panes, entry) { 734 if (n == idx) 735 return (wp); 736 n++; 737 } 738 return (NULL); 739 } 740 741 struct window_pane * 742 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n) 743 { 744 for (; n > 0; n--) { 745 if ((wp = TAILQ_NEXT(wp, entry)) == NULL) 746 wp = TAILQ_FIRST(&w->panes); 747 } 748 749 return (wp); 750 } 751 752 struct window_pane * 753 window_pane_previous_by_number(struct window *w, struct window_pane *wp, 754 u_int n) 755 { 756 for (; n > 0; n--) { 757 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL) 758 wp = TAILQ_LAST(&w->panes, window_panes); 759 } 760 761 return (wp); 762 } 763 764 int 765 window_pane_index(struct window_pane *wp, u_int *i) 766 { 767 struct window_pane *wq; 768 struct window *w = wp->window; 769 770 *i = options_get_number(w->options, "pane-base-index"); 771 TAILQ_FOREACH(wq, &w->panes, entry) { 772 if (wp == wq) { 773 return (0); 774 } 775 (*i)++; 776 } 777 778 return (-1); 779 } 780 781 u_int 782 window_count_panes(struct window *w) 783 { 784 struct window_pane *wp; 785 u_int n; 786 787 n = 0; 788 TAILQ_FOREACH(wp, &w->panes, entry) 789 n++; 790 return (n); 791 } 792 793 void 794 window_destroy_panes(struct window *w) 795 { 796 struct window_pane *wp; 797 798 while (!TAILQ_EMPTY(&w->panes)) { 799 wp = TAILQ_FIRST(&w->panes); 800 TAILQ_REMOVE(&w->panes, wp, entry); 801 window_pane_destroy(wp); 802 } 803 } 804 805 const char * 806 window_printable_flags(struct winlink *wl) 807 { 808 struct session *s = wl->session; 809 static char flags[32]; 810 int pos; 811 812 pos = 0; 813 if (wl->flags & WINLINK_ACTIVITY) 814 flags[pos++] = '#'; 815 if (wl->flags & WINLINK_BELL) 816 flags[pos++] = '!'; 817 if (wl->flags & WINLINK_SILENCE) 818 flags[pos++] = '~'; 819 if (wl == s->curw) 820 flags[pos++] = '*'; 821 if (wl == TAILQ_FIRST(&s->lastw)) 822 flags[pos++] = '-'; 823 if (server_check_marked() && wl == marked_pane.wl) 824 flags[pos++] = 'M'; 825 if (wl->window->flags & WINDOW_ZOOMED) 826 flags[pos++] = 'Z'; 827 flags[pos] = '\0'; 828 return (flags); 829 } 830 831 struct window_pane * 832 window_pane_find_by_id_str(const char *s) 833 { 834 const char *errstr; 835 u_int id; 836 837 if (*s != '%') 838 return (NULL); 839 840 id = strtonum(s + 1, 0, UINT_MAX, &errstr); 841 if (errstr != NULL) 842 return (NULL); 843 return (window_pane_find_by_id(id)); 844 } 845 846 struct window_pane * 847 window_pane_find_by_id(u_int id) 848 { 849 struct window_pane wp; 850 851 wp.id = id; 852 return (RB_FIND(window_pane_tree, &all_window_panes, &wp)); 853 } 854 855 static struct window_pane * 856 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit) 857 { 858 struct window_pane *wp; 859 char host[HOST_NAME_MAX + 1]; 860 861 wp = xcalloc(1, sizeof *wp); 862 wp->window = w; 863 wp->options = options_create(w->options); 864 wp->flags = PANE_STYLECHANGED; 865 866 wp->id = next_window_pane_id++; 867 RB_INSERT(window_pane_tree, &all_window_panes, wp); 868 869 wp->argc = 0; 870 wp->argv = NULL; 871 wp->shell = NULL; 872 wp->cwd = NULL; 873 874 wp->fd = -1; 875 wp->event = NULL; 876 877 wp->fg = 8; 878 wp->bg = 8; 879 880 TAILQ_INIT(&wp->modes); 881 882 wp->layout_cell = NULL; 883 884 wp->xoff = 0; 885 wp->yoff = 0; 886 887 wp->sx = sx; 888 wp->sy = sy; 889 890 wp->pipe_fd = -1; 891 wp->pipe_event = NULL; 892 893 screen_init(&wp->base, sx, sy, hlimit); 894 wp->screen = &wp->base; 895 896 screen_init(&wp->status_screen, 1, 1, 0); 897 898 if (gethostname(host, sizeof host) == 0) 899 screen_set_title(&wp->base, host); 900 901 return (wp); 902 } 903 904 static void 905 window_pane_destroy(struct window_pane *wp) 906 { 907 window_pane_reset_mode_all(wp); 908 free(wp->searchstr); 909 910 if (wp->fd != -1) { 911 bufferevent_free(wp->event); 912 close(wp->fd); 913 } 914 if (wp->ictx != NULL) 915 input_free(wp->ictx); 916 917 screen_free(&wp->status_screen); 918 919 screen_free(&wp->base); 920 921 if (wp->pipe_fd != -1) { 922 bufferevent_free(wp->pipe_event); 923 close(wp->pipe_fd); 924 } 925 926 if (event_initialized(&wp->resize_timer)) 927 event_del(&wp->resize_timer); 928 if (event_initialized(&wp->force_timer)) 929 event_del(&wp->force_timer); 930 931 RB_REMOVE(window_pane_tree, &all_window_panes, wp); 932 933 options_free(wp->options); 934 free((void *)wp->cwd); 935 free(wp->shell); 936 cmd_free_argv(wp->argc, wp->argv); 937 free(wp->palette); 938 free(wp); 939 } 940 941 static void 942 window_pane_read_callback(__unused struct bufferevent *bufev, void *data) 943 { 944 struct window_pane *wp = data; 945 struct evbuffer *evb = wp->event->input; 946 struct window_pane_offset *wpo = &wp->pipe_offset; 947 size_t size = EVBUFFER_LENGTH(evb); 948 char *new_data; 949 size_t new_size; 950 struct client *c; 951 952 if (wp->pipe_fd != -1) { 953 new_data = window_pane_get_new_data(wp, wpo, &new_size); 954 if (new_size > 0) { 955 bufferevent_write(wp->pipe_event, new_data, new_size); 956 window_pane_update_used_data(wp, wpo, new_size); 957 } 958 } 959 960 log_debug("%%%u has %zu bytes", wp->id, size); 961 TAILQ_FOREACH(c, &clients, entry) { 962 if (c->session != NULL && (c->flags & CLIENT_CONTROL)) 963 control_write_output(c, wp); 964 } 965 input_parse_pane(wp); 966 bufferevent_disable(wp->event, EV_READ); 967 } 968 969 static void 970 window_pane_error_callback(__unused struct bufferevent *bufev, 971 __unused short what, void *data) 972 { 973 struct window_pane *wp = data; 974 975 log_debug("%%%u error", wp->id); 976 wp->flags |= PANE_EXITED; 977 978 if (window_pane_destroy_ready(wp)) 979 server_destroy_pane(wp, 1); 980 } 981 982 void 983 window_pane_set_event(struct window_pane *wp) 984 { 985 setblocking(wp->fd, 0); 986 987 wp->event = bufferevent_new(wp->fd, window_pane_read_callback, 988 NULL, window_pane_error_callback, wp); 989 wp->ictx = input_init(wp, wp->event); 990 991 bufferevent_enable(wp->event, EV_READ|EV_WRITE); 992 } 993 994 void 995 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy) 996 { 997 struct window_mode_entry *wme; 998 999 if (sx == wp->sx && sy == wp->sy) 1000 return; 1001 wp->sx = sx; 1002 wp->sy = sy; 1003 1004 log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy); 1005 screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL); 1006 1007 wme = TAILQ_FIRST(&wp->modes); 1008 if (wme != NULL && wme->mode->resize != NULL) 1009 wme->mode->resize(wme, sx, sy); 1010 1011 /* 1012 * If the pane has already been resized, set the force flag and make 1013 * the application resize twice to force it to redraw. 1014 */ 1015 if (wp->flags & PANE_RESIZE) 1016 wp->flags |= PANE_RESIZEFORCE; 1017 wp->flags |= PANE_RESIZE; 1018 } 1019 1020 void 1021 window_pane_set_palette(struct window_pane *wp, u_int n, int colour) 1022 { 1023 if (n > 0xff) 1024 return; 1025 1026 if (wp->palette == NULL) 1027 wp->palette = xcalloc(0x100, sizeof *wp->palette); 1028 1029 wp->palette[n] = colour; 1030 wp->flags |= PANE_REDRAW; 1031 } 1032 1033 void 1034 window_pane_unset_palette(struct window_pane *wp, u_int n) 1035 { 1036 if (n > 0xff || wp->palette == NULL) 1037 return; 1038 1039 wp->palette[n] = 0; 1040 wp->flags |= PANE_REDRAW; 1041 } 1042 1043 void 1044 window_pane_reset_palette(struct window_pane *wp) 1045 { 1046 if (wp->palette == NULL) 1047 return; 1048 1049 free(wp->palette); 1050 wp->palette = NULL; 1051 wp->flags |= PANE_REDRAW; 1052 } 1053 1054 int 1055 window_pane_get_palette(struct window_pane *wp, int c) 1056 { 1057 int new; 1058 1059 if (wp == NULL || wp->palette == NULL) 1060 return (-1); 1061 1062 new = -1; 1063 if (c < 8) 1064 new = wp->palette[c]; 1065 else if (c >= 90 && c <= 97) 1066 new = wp->palette[8 + c - 90]; 1067 else if (c & COLOUR_FLAG_256) 1068 new = wp->palette[c & ~COLOUR_FLAG_256]; 1069 if (new == 0) 1070 return (-1); 1071 return (new); 1072 } 1073 1074 int 1075 window_pane_set_mode(struct window_pane *wp, struct window_pane *swp, 1076 const struct window_mode *mode, struct cmd_find_state *fs, 1077 struct args *args) 1078 { 1079 struct window_mode_entry *wme; 1080 1081 if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode) 1082 return (1); 1083 1084 TAILQ_FOREACH(wme, &wp->modes, entry) { 1085 if (wme->mode == mode) 1086 break; 1087 } 1088 if (wme != NULL) { 1089 TAILQ_REMOVE(&wp->modes, wme, entry); 1090 TAILQ_INSERT_HEAD(&wp->modes, wme, entry); 1091 } else { 1092 wme = xcalloc(1, sizeof *wme); 1093 wme->wp = wp; 1094 wme->swp = swp; 1095 wme->mode = mode; 1096 wme->prefix = 1; 1097 TAILQ_INSERT_HEAD(&wp->modes, wme, entry); 1098 wme->screen = wme->mode->init(wme, fs, args); 1099 } 1100 1101 wp->screen = wme->screen; 1102 wp->flags |= (PANE_REDRAW|PANE_CHANGED); 1103 1104 server_redraw_window_borders(wp->window); 1105 server_status_window(wp->window); 1106 notify_pane("pane-mode-changed", wp); 1107 1108 return (0); 1109 } 1110 1111 void 1112 window_pane_reset_mode(struct window_pane *wp) 1113 { 1114 struct window_mode_entry *wme, *next; 1115 1116 if (TAILQ_EMPTY(&wp->modes)) 1117 return; 1118 1119 wme = TAILQ_FIRST(&wp->modes); 1120 TAILQ_REMOVE(&wp->modes, wme, entry); 1121 wme->mode->free(wme); 1122 free(wme); 1123 1124 next = TAILQ_FIRST(&wp->modes); 1125 if (next == NULL) { 1126 log_debug("%s: no next mode", __func__); 1127 wp->screen = &wp->base; 1128 } else { 1129 log_debug("%s: next mode is %s", __func__, next->mode->name); 1130 wp->screen = next->screen; 1131 if (next->mode->resize != NULL) 1132 next->mode->resize(next, wp->sx, wp->sy); 1133 } 1134 wp->flags |= (PANE_REDRAW|PANE_CHANGED); 1135 1136 server_redraw_window_borders(wp->window); 1137 server_status_window(wp->window); 1138 notify_pane("pane-mode-changed", wp); 1139 } 1140 1141 void 1142 window_pane_reset_mode_all(struct window_pane *wp) 1143 { 1144 while (!TAILQ_EMPTY(&wp->modes)) 1145 window_pane_reset_mode(wp); 1146 } 1147 1148 static void 1149 window_pane_copy_key(struct window_pane *wp, key_code key) 1150 { 1151 struct window_pane *loop; 1152 1153 TAILQ_FOREACH(loop, &wp->window->panes, entry) { 1154 if (loop != wp && 1155 TAILQ_EMPTY(&loop->modes) && 1156 loop->fd != -1 && 1157 (~loop->flags & PANE_INPUTOFF) && 1158 window_pane_visible(loop) && 1159 options_get_number(loop->options, "synchronize-panes")) 1160 input_key_pane(loop, key, NULL); 1161 } 1162 } 1163 1164 int 1165 window_pane_key(struct window_pane *wp, struct client *c, struct session *s, 1166 struct winlink *wl, key_code key, struct mouse_event *m) 1167 { 1168 struct window_mode_entry *wme; 1169 1170 if (KEYC_IS_MOUSE(key) && m == NULL) 1171 return (-1); 1172 1173 wme = TAILQ_FIRST(&wp->modes); 1174 if (wme != NULL) { 1175 if (wme->mode->key != NULL && c != NULL) { 1176 key &= ~KEYC_MASK_FLAGS; 1177 wme->mode->key(wme, c, s, wl, key, m); 1178 } 1179 return (0); 1180 } 1181 1182 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF) 1183 return (0); 1184 1185 if (input_key_pane(wp, key, m) != 0) 1186 return (-1); 1187 1188 if (KEYC_IS_MOUSE(key)) 1189 return (0); 1190 if (options_get_number(wp->options, "synchronize-panes")) 1191 window_pane_copy_key(wp, key); 1192 return (0); 1193 } 1194 1195 int 1196 window_pane_visible(struct window_pane *wp) 1197 { 1198 if (~wp->window->flags & WINDOW_ZOOMED) 1199 return (1); 1200 return (wp == wp->window->active); 1201 } 1202 1203 u_int 1204 window_pane_search(struct window_pane *wp, const char *term, int regex, 1205 int ignore) 1206 { 1207 struct screen *s = &wp->base; 1208 regex_t r; 1209 char *new = NULL, *line; 1210 u_int i; 1211 int flags = 0, found; 1212 size_t n; 1213 1214 if (!regex) { 1215 if (ignore) 1216 flags |= FNM_CASEFOLD; 1217 xasprintf(&new, "*%s*", term); 1218 } else { 1219 if (ignore) 1220 flags |= REG_ICASE; 1221 if (regcomp(&r, term, flags|REG_EXTENDED) != 0) 1222 return (0); 1223 } 1224 1225 for (i = 0; i < screen_size_y(s); i++) { 1226 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s)); 1227 for (n = strlen(line); n > 0; n--) { 1228 if (!isspace((u_char)line[n - 1])) 1229 break; 1230 line[n - 1] = '\0'; 1231 } 1232 log_debug("%s: %s", __func__, line); 1233 if (!regex) 1234 found = (fnmatch(new, line, flags) == 0); 1235 else 1236 found = (regexec(&r, line, 0, NULL, 0) == 0); 1237 free(line); 1238 if (found) 1239 break; 1240 } 1241 if (!regex) 1242 free(new); 1243 else 1244 regfree(&r); 1245 1246 if (i == screen_size_y(s)) 1247 return (0); 1248 return (i + 1); 1249 } 1250 1251 /* Get MRU pane from a list. */ 1252 static struct window_pane * 1253 window_pane_choose_best(struct window_pane **list, u_int size) 1254 { 1255 struct window_pane *next, *best; 1256 u_int i; 1257 1258 if (size == 0) 1259 return (NULL); 1260 1261 best = list[0]; 1262 for (i = 1; i < size; i++) { 1263 next = list[i]; 1264 if (next->active_point > best->active_point) 1265 best = next; 1266 } 1267 return (best); 1268 } 1269 1270 /* 1271 * Find the pane directly above another. We build a list of those adjacent to 1272 * top edge and then choose the best. 1273 */ 1274 struct window_pane * 1275 window_pane_find_up(struct window_pane *wp) 1276 { 1277 struct window *w; 1278 struct window_pane *next, *best, **list; 1279 u_int edge, left, right, end, size; 1280 int status, found; 1281 1282 if (wp == NULL) 1283 return (NULL); 1284 w = wp->window; 1285 status = options_get_number(w->options, "pane-border-status"); 1286 1287 list = NULL; 1288 size = 0; 1289 1290 edge = wp->yoff; 1291 if (status == PANE_STATUS_TOP) { 1292 if (edge == 1) 1293 edge = w->sy + 1; 1294 } else if (status == PANE_STATUS_BOTTOM) { 1295 if (edge == 0) 1296 edge = w->sy; 1297 } else { 1298 if (edge == 0) 1299 edge = w->sy + 1; 1300 } 1301 1302 left = wp->xoff; 1303 right = wp->xoff + wp->sx; 1304 1305 TAILQ_FOREACH(next, &w->panes, entry) { 1306 if (next == wp) 1307 continue; 1308 if (next->yoff + next->sy + 1 != edge) 1309 continue; 1310 end = next->xoff + next->sx - 1; 1311 1312 found = 0; 1313 if (next->xoff < left && end > right) 1314 found = 1; 1315 else if (next->xoff >= left && next->xoff <= right) 1316 found = 1; 1317 else if (end >= left && end <= right) 1318 found = 1; 1319 if (!found) 1320 continue; 1321 list = xreallocarray(list, size + 1, sizeof *list); 1322 list[size++] = next; 1323 } 1324 1325 best = window_pane_choose_best(list, size); 1326 free(list); 1327 return (best); 1328 } 1329 1330 /* Find the pane directly below another. */ 1331 struct window_pane * 1332 window_pane_find_down(struct window_pane *wp) 1333 { 1334 struct window *w; 1335 struct window_pane *next, *best, **list; 1336 u_int edge, left, right, end, size; 1337 int status, found; 1338 1339 if (wp == NULL) 1340 return (NULL); 1341 w = wp->window; 1342 status = options_get_number(w->options, "pane-border-status"); 1343 1344 list = NULL; 1345 size = 0; 1346 1347 edge = wp->yoff + wp->sy + 1; 1348 if (status == PANE_STATUS_TOP) { 1349 if (edge >= w->sy) 1350 edge = 1; 1351 } else if (status == PANE_STATUS_BOTTOM) { 1352 if (edge >= w->sy - 1) 1353 edge = 0; 1354 } else { 1355 if (edge >= w->sy) 1356 edge = 0; 1357 } 1358 1359 left = wp->xoff; 1360 right = wp->xoff + wp->sx; 1361 1362 TAILQ_FOREACH(next, &w->panes, entry) { 1363 if (next == wp) 1364 continue; 1365 if (next->yoff != edge) 1366 continue; 1367 end = next->xoff + next->sx - 1; 1368 1369 found = 0; 1370 if (next->xoff < left && end > right) 1371 found = 1; 1372 else if (next->xoff >= left && next->xoff <= right) 1373 found = 1; 1374 else if (end >= left && end <= right) 1375 found = 1; 1376 if (!found) 1377 continue; 1378 list = xreallocarray(list, size + 1, sizeof *list); 1379 list[size++] = next; 1380 } 1381 1382 best = window_pane_choose_best(list, size); 1383 free(list); 1384 return (best); 1385 } 1386 1387 /* Find the pane directly to the left of another. */ 1388 struct window_pane * 1389 window_pane_find_left(struct window_pane *wp) 1390 { 1391 struct window *w; 1392 struct window_pane *next, *best, **list; 1393 u_int edge, top, bottom, end, size; 1394 int found; 1395 1396 if (wp == NULL) 1397 return (NULL); 1398 w = wp->window; 1399 1400 list = NULL; 1401 size = 0; 1402 1403 edge = wp->xoff; 1404 if (edge == 0) 1405 edge = w->sx + 1; 1406 1407 top = wp->yoff; 1408 bottom = wp->yoff + wp->sy; 1409 1410 TAILQ_FOREACH(next, &w->panes, entry) { 1411 if (next == wp) 1412 continue; 1413 if (next->xoff + next->sx + 1 != edge) 1414 continue; 1415 end = next->yoff + next->sy - 1; 1416 1417 found = 0; 1418 if (next->yoff < top && end > bottom) 1419 found = 1; 1420 else if (next->yoff >= top && next->yoff <= bottom) 1421 found = 1; 1422 else if (end >= top && end <= bottom) 1423 found = 1; 1424 if (!found) 1425 continue; 1426 list = xreallocarray(list, size + 1, sizeof *list); 1427 list[size++] = next; 1428 } 1429 1430 best = window_pane_choose_best(list, size); 1431 free(list); 1432 return (best); 1433 } 1434 1435 /* Find the pane directly to the right of another. */ 1436 struct window_pane * 1437 window_pane_find_right(struct window_pane *wp) 1438 { 1439 struct window *w; 1440 struct window_pane *next, *best, **list; 1441 u_int edge, top, bottom, end, size; 1442 int found; 1443 1444 if (wp == NULL) 1445 return (NULL); 1446 w = wp->window; 1447 1448 list = NULL; 1449 size = 0; 1450 1451 edge = wp->xoff + wp->sx + 1; 1452 if (edge >= w->sx) 1453 edge = 0; 1454 1455 top = wp->yoff; 1456 bottom = wp->yoff + wp->sy; 1457 1458 TAILQ_FOREACH(next, &w->panes, entry) { 1459 if (next == wp) 1460 continue; 1461 if (next->xoff != edge) 1462 continue; 1463 end = next->yoff + next->sy - 1; 1464 1465 found = 0; 1466 if (next->yoff < top && end > bottom) 1467 found = 1; 1468 else if (next->yoff >= top && next->yoff <= bottom) 1469 found = 1; 1470 else if (end >= top && end <= bottom) 1471 found = 1; 1472 if (!found) 1473 continue; 1474 list = xreallocarray(list, size + 1, sizeof *list); 1475 list[size++] = next; 1476 } 1477 1478 best = window_pane_choose_best(list, size); 1479 free(list); 1480 return (best); 1481 } 1482 1483 /* Clear alert flags for a winlink */ 1484 void 1485 winlink_clear_flags(struct winlink *wl) 1486 { 1487 struct winlink *loop; 1488 1489 wl->window->flags &= ~WINDOW_ALERTFLAGS; 1490 TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) { 1491 if ((loop->flags & WINLINK_ALERTFLAGS) != 0) { 1492 loop->flags &= ~WINLINK_ALERTFLAGS; 1493 server_status_session(loop->session); 1494 } 1495 } 1496 } 1497 1498 /* Shuffle window indexes up. */ 1499 int 1500 winlink_shuffle_up(struct session *s, struct winlink *wl, int before) 1501 { 1502 int idx, last; 1503 1504 if (wl == NULL) 1505 return (-1); 1506 if (before) 1507 idx = wl->idx; 1508 else 1509 idx = wl->idx + 1; 1510 1511 /* Find the next free index. */ 1512 for (last = idx; last < INT_MAX; last++) { 1513 if (winlink_find_by_index(&s->windows, last) == NULL) 1514 break; 1515 } 1516 if (last == INT_MAX) 1517 return (-1); 1518 1519 /* Move everything from last - 1 to idx up a bit. */ 1520 for (; last > idx; last--) { 1521 wl = winlink_find_by_index(&s->windows, last - 1); 1522 RB_REMOVE(winlinks, &s->windows, wl); 1523 wl->idx++; 1524 RB_INSERT(winlinks, &s->windows, wl); 1525 } 1526 1527 return (idx); 1528 } 1529 1530 static void 1531 window_pane_input_callback(struct client *c, __unused const char *path, 1532 int error, int closed, struct evbuffer *buffer, void *data) 1533 { 1534 struct window_pane_input_data *cdata = data; 1535 struct window_pane *wp; 1536 u_char *buf = EVBUFFER_DATA(buffer); 1537 size_t len = EVBUFFER_LENGTH(buffer); 1538 1539 wp = window_pane_find_by_id(cdata->wp); 1540 if (wp == NULL || closed || error != 0 || c->flags & CLIENT_DEAD) { 1541 if (wp == NULL) 1542 c->flags |= CLIENT_EXIT; 1543 1544 evbuffer_drain(buffer, len); 1545 cmdq_continue(cdata->item); 1546 1547 server_client_unref(c); 1548 free(cdata); 1549 return; 1550 } 1551 input_parse_buffer(wp, buf, len); 1552 evbuffer_drain(buffer, len); 1553 } 1554 1555 int 1556 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item, 1557 char **cause) 1558 { 1559 struct client *c = cmdq_get_client(item); 1560 struct window_pane_input_data *cdata; 1561 1562 if (~wp->flags & PANE_EMPTY) { 1563 *cause = xstrdup("pane is not empty"); 1564 return (-1); 1565 } 1566 1567 cdata = xmalloc(sizeof *cdata); 1568 cdata->item = item; 1569 cdata->wp = wp->id; 1570 1571 c->references++; 1572 file_read(c, "-", window_pane_input_callback, cdata); 1573 1574 return (0); 1575 } 1576 1577 void * 1578 window_pane_get_new_data(struct window_pane *wp, 1579 struct window_pane_offset *wpo, size_t *size) 1580 { 1581 size_t used = wpo->used - wp->base_offset; 1582 1583 *size = EVBUFFER_LENGTH(wp->event->input) - used; 1584 return (EVBUFFER_DATA(wp->event->input) + used); 1585 } 1586 1587 void 1588 window_pane_update_used_data(struct window_pane *wp, 1589 struct window_pane_offset *wpo, size_t size) 1590 { 1591 size_t used = wpo->used - wp->base_offset; 1592 1593 if (size > EVBUFFER_LENGTH(wp->event->input) - used) 1594 size = EVBUFFER_LENGTH(wp->event->input) - used; 1595 wpo->used += size; 1596 } 1597