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