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