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