Merge branch 'obsd-master'
[tmux.git] / window.c
blob84a32393df8d88f9c74ffa3285ddf8ea4e14722a
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
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>
34 #include "tmux.h"
37 * Each window is attached to a number of panes, each of which is a pty. This
38 * file contains code to handle them.
40 * A pane has two buffers attached, these are filled and emptied by the main
41 * server poll loop. Output data is received from pty's in screen format,
42 * translated and returned as a series of escape sequences and strings via
43 * input_parse (in input.c). Input data is received as key codes and written
44 * directly via input_key.
46 * Each pane also has a "virtual" screen (screen.c) which contains the current
47 * state and is redisplayed when the window is reattached to a client.
49 * Windows are stored directly on a global array and wrapped in any number of
50 * winlink structs to be linked onto local session RB trees. A reference count
51 * is maintained and a window removed from the global list and destroyed when
52 * it reaches zero.
55 /* Global window list. */
56 struct windows windows;
58 /* Global panes tree. */
59 struct window_pane_tree all_window_panes;
60 static u_int next_window_pane_id;
61 static u_int next_window_id;
62 static u_int next_active_point;
64 struct window_pane_input_data {
65 struct cmdq_item *item;
66 u_int wp;
67 struct client_file *file;
70 static struct window_pane *window_pane_create(struct window *, u_int, u_int,
71 u_int);
72 static void window_pane_destroy(struct window_pane *);
74 RB_GENERATE(windows, window, entry, window_cmp);
75 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
76 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
78 int
79 window_cmp(struct window *w1, struct window *w2)
81 return (w1->id - w2->id);
84 int
85 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
87 return (wl1->idx - wl2->idx);
90 int
91 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
93 return (wp1->id - wp2->id);
96 struct winlink *
97 winlink_find_by_window(struct winlinks *wwl, struct window *w)
99 struct winlink *wl;
101 RB_FOREACH(wl, winlinks, wwl) {
102 if (wl->window == w)
103 return (wl);
106 return (NULL);
109 struct winlink *
110 winlink_find_by_index(struct winlinks *wwl, int idx)
112 struct winlink wl;
114 if (idx < 0)
115 fatalx("bad index");
117 wl.idx = idx;
118 return (RB_FIND(winlinks, wwl, &wl));
121 struct winlink *
122 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
124 struct winlink *wl;
126 RB_FOREACH(wl, winlinks, wwl) {
127 if (wl->window->id == id)
128 return (wl);
130 return (NULL);
133 static int
134 winlink_next_index(struct winlinks *wwl, int idx)
136 int i;
138 i = idx;
139 do {
140 if (winlink_find_by_index(wwl, i) == NULL)
141 return (i);
142 if (i == INT_MAX)
143 i = 0;
144 else
145 i++;
146 } while (i != idx);
147 return (-1);
150 u_int
151 winlink_count(struct winlinks *wwl)
153 struct winlink *wl;
154 u_int n;
156 n = 0;
157 RB_FOREACH(wl, winlinks, wwl)
158 n++;
160 return (n);
163 struct winlink *
164 winlink_add(struct winlinks *wwl, int idx)
166 struct winlink *wl;
168 if (idx < 0) {
169 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
170 return (NULL);
171 } else if (winlink_find_by_index(wwl, idx) != NULL)
172 return (NULL);
174 wl = xcalloc(1, sizeof *wl);
175 wl->idx = idx;
176 RB_INSERT(winlinks, wwl, wl);
178 return (wl);
181 void
182 winlink_set_window(struct winlink *wl, struct window *w)
184 if (wl->window != NULL) {
185 TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
186 window_remove_ref(wl->window, __func__);
188 TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
189 wl->window = w;
190 window_add_ref(w, __func__);
193 void
194 winlink_remove(struct winlinks *wwl, struct winlink *wl)
196 struct window *w = wl->window;
198 if (w != NULL) {
199 TAILQ_REMOVE(&w->winlinks, wl, wentry);
200 window_remove_ref(w, __func__);
203 RB_REMOVE(winlinks, wwl, wl);
204 free(wl);
207 struct winlink *
208 winlink_next(struct winlink *wl)
210 return (RB_NEXT(winlinks, wwl, wl));
213 struct winlink *
214 winlink_previous(struct winlink *wl)
216 return (RB_PREV(winlinks, wwl, wl));
219 struct winlink *
220 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
222 for (; n > 0; n--) {
223 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
224 wl = RB_MIN(winlinks, &s->windows);
227 return (wl);
230 struct winlink *
231 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
233 for (; n > 0; n--) {
234 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
235 wl = RB_MAX(winlinks, &s->windows);
238 return (wl);
241 void
242 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
244 if (wl == NULL)
245 return;
247 winlink_stack_remove(stack, wl);
248 TAILQ_INSERT_HEAD(stack, wl, sentry);
249 wl->flags |= WINLINK_VISITED;
252 void
253 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
255 if (wl != NULL && (wl->flags & WINLINK_VISITED)) {
256 TAILQ_REMOVE(stack, wl, sentry);
257 wl->flags &= ~WINLINK_VISITED;
261 struct window *
262 window_find_by_id_str(const char *s)
264 const char *errstr;
265 u_int id;
267 if (*s != '@')
268 return (NULL);
270 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
271 if (errstr != NULL)
272 return (NULL);
273 return (window_find_by_id(id));
276 struct window *
277 window_find_by_id(u_int id)
279 struct window w;
281 w.id = id;
282 return (RB_FIND(windows, &windows, &w));
285 void
286 window_update_activity(struct window *w)
288 gettimeofday(&w->activity_time, NULL);
289 alerts_queue(w, WINDOW_ACTIVITY);
292 struct window *
293 window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel)
295 struct window *w;
297 if (xpixel == 0)
298 xpixel = DEFAULT_XPIXEL;
299 if (ypixel == 0)
300 ypixel = DEFAULT_YPIXEL;
302 w = xcalloc(1, sizeof *w);
303 w->name = xstrdup("");
304 w->flags = 0;
306 TAILQ_INIT(&w->panes);
307 TAILQ_INIT(&w->last_panes);
308 w->active = NULL;
310 w->lastlayout = -1;
311 w->layout_root = NULL;
313 w->sx = sx;
314 w->sy = sy;
315 w->manual_sx = sx;
316 w->manual_sy = sy;
317 w->xpixel = xpixel;
318 w->ypixel = ypixel;
320 w->options = options_create(global_w_options);
322 w->references = 0;
323 TAILQ_INIT(&w->winlinks);
325 w->id = next_window_id++;
326 RB_INSERT(windows, &windows, w);
328 window_set_fill_character(w);
329 window_update_activity(w);
331 log_debug("%s: @%u create %ux%u (%ux%u)", __func__, w->id, sx, sy,
332 w->xpixel, w->ypixel);
333 return (w);
336 static void
337 window_destroy(struct window *w)
339 log_debug("window @%u destroyed (%d references)", w->id, w->references);
341 RB_REMOVE(windows, &windows, w);
343 if (w->layout_root != NULL)
344 layout_free_cell(w->layout_root);
345 if (w->saved_layout_root != NULL)
346 layout_free_cell(w->saved_layout_root);
347 free(w->old_layout);
349 window_destroy_panes(w);
351 if (event_initialized(&w->name_event))
352 evtimer_del(&w->name_event);
354 if (event_initialized(&w->alerts_timer))
355 evtimer_del(&w->alerts_timer);
356 if (event_initialized(&w->offset_timer))
357 event_del(&w->offset_timer);
359 options_free(w->options);
360 free(w->fill_character);
362 free(w->name);
363 free(w);
367 window_pane_destroy_ready(struct window_pane *wp)
369 int n;
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);
378 if (~wp->flags & PANE_EXITED)
379 return (0);
380 return (1);
383 void
384 window_add_ref(struct window *w, const char *from)
386 w->references++;
387 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
390 void
391 window_remove_ref(struct window *w, const char *from)
393 w->references--;
394 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
396 if (w->references == 0)
397 window_destroy(w);
400 void
401 window_set_name(struct window *w, const char *new_name)
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);
408 void
409 window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
411 if (xpixel == 0)
412 xpixel = DEFAULT_XPIXEL;
413 if (ypixel == 0)
414 ypixel = DEFAULT_YPIXEL;
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;
427 void
428 window_pane_send_resize(struct window_pane *wp, u_int sx, u_int sy)
430 struct window *w = wp->window;
431 struct winsize ws;
433 if (wp->fd == -1)
434 return;
436 log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, sx, sy);
438 memset(&ws, 0, sizeof ws);
439 ws.ws_col = sx;
440 ws.ws_row = sy;
441 ws.ws_xpixel = w->xpixel * ws.ws_col;
442 ws.ws_ypixel = w->ypixel * ws.ws_row;
443 if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
444 #ifdef __sun
446 * Some versions of Solaris apparently can return an error when
447 * resizing; don't know why this happens, can't reproduce on
448 * other platforms and ignoring it doesn't seem to cause any
449 * issues.
451 if (errno != EINVAL && errno != ENXIO)
452 #endif
453 fatal("ioctl failed");
457 window_has_pane(struct window *w, struct window_pane *wp)
459 struct window_pane *wp1;
461 TAILQ_FOREACH(wp1, &w->panes, entry) {
462 if (wp1 == wp)
463 return (1);
465 return (0);
468 void
469 window_update_focus(struct window *w)
471 if (w != NULL) {
472 log_debug("%s: @%u", __func__, w->id);
473 window_pane_update_focus(w->active);
477 void
478 window_pane_update_focus(struct window_pane *wp)
480 struct client *c;
481 int focused = 0;
483 if (wp != NULL) {
484 if (wp != wp->window->active)
485 focused = 0;
486 else {
487 TAILQ_FOREACH(c, &clients, entry) {
488 if (c->session != NULL &&
489 c->session->attached != 0 &&
490 (c->flags & CLIENT_FOCUSED) &&
491 c->session->curw->window == wp->window) {
492 focused = 1;
493 break;
497 if (!focused && (wp->flags & PANE_FOCUSED)) {
498 log_debug("%s: %%%u focus out", __func__, wp->id);
499 if (wp->base.mode & MODE_FOCUSON)
500 bufferevent_write(wp->event, "\033[O", 3);
501 notify_pane("pane-focus-out", wp);
502 wp->flags &= ~PANE_FOCUSED;
503 } else if (focused && (~wp->flags & PANE_FOCUSED)) {
504 log_debug("%s: %%%u focus in", __func__, wp->id);
505 if (wp->base.mode & MODE_FOCUSON)
506 bufferevent_write(wp->event, "\033[I", 3);
507 notify_pane("pane-focus-in", wp);
508 wp->flags |= PANE_FOCUSED;
509 } else
510 log_debug("%s: %%%u focus unchanged", __func__, wp->id);
515 window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
517 struct window_pane *lastwp;
519 log_debug("%s: pane %%%u", __func__, wp->id);
521 if (wp == w->active)
522 return (0);
523 lastwp = w->active;
525 window_pane_stack_remove(&w->last_panes, wp);
526 window_pane_stack_push(&w->last_panes, lastwp);
528 w->active = wp;
529 w->active->active_point = next_active_point++;
530 w->active->flags |= PANE_CHANGED;
532 if (options_get_number(global_options, "focus-events")) {
533 window_pane_update_focus(lastwp);
534 window_pane_update_focus(w->active);
537 tty_update_window_offset(w);
539 if (notify)
540 notify_window("window-pane-changed", w);
541 return (1);
544 static int
545 window_pane_get_palette(struct window_pane *wp, int c)
547 if (wp == NULL)
548 return (-1);
549 return (colour_palette_get(&wp->palette, c));
552 void
553 window_redraw_active_switch(struct window *w, struct window_pane *wp)
555 struct grid_cell *gc1, *gc2;
556 int c1, c2;
558 if (wp == w->active)
559 return;
561 for (;;) {
563 * If the active and inactive styles or palettes are different,
564 * need to redraw the panes.
566 gc1 = &wp->cached_gc;
567 gc2 = &wp->cached_active_gc;
568 if (!grid_cells_look_equal(gc1, gc2))
569 wp->flags |= PANE_REDRAW;
570 else {
571 c1 = window_pane_get_palette(wp, gc1->fg);
572 c2 = window_pane_get_palette(wp, gc2->fg);
573 if (c1 != c2)
574 wp->flags |= PANE_REDRAW;
575 else {
576 c1 = window_pane_get_palette(wp, gc1->bg);
577 c2 = window_pane_get_palette(wp, gc2->bg);
578 if (c1 != c2)
579 wp->flags |= PANE_REDRAW;
582 if (wp == w->active)
583 break;
584 wp = w->active;
588 struct window_pane *
589 window_get_active_at(struct window *w, u_int x, u_int y)
591 struct window_pane *wp;
593 TAILQ_FOREACH(wp, &w->panes, entry) {
594 if (!window_pane_visible(wp))
595 continue;
596 if (x < wp->xoff || x > wp->xoff + wp->sx)
597 continue;
598 if (y < wp->yoff || y > wp->yoff + wp->sy)
599 continue;
600 return (wp);
602 return (NULL);
605 struct window_pane *
606 window_find_string(struct window *w, const char *s)
608 u_int x, y, top = 0, bottom = w->sy - 1;
609 int status;
611 x = w->sx / 2;
612 y = w->sy / 2;
614 status = options_get_number(w->options, "pane-border-status");
615 if (status == PANE_STATUS_TOP)
616 top++;
617 else if (status == PANE_STATUS_BOTTOM)
618 bottom--;
620 if (strcasecmp(s, "top") == 0)
621 y = top;
622 else if (strcasecmp(s, "bottom") == 0)
623 y = bottom;
624 else if (strcasecmp(s, "left") == 0)
625 x = 0;
626 else if (strcasecmp(s, "right") == 0)
627 x = w->sx - 1;
628 else if (strcasecmp(s, "top-left") == 0) {
629 x = 0;
630 y = top;
631 } else if (strcasecmp(s, "top-right") == 0) {
632 x = w->sx - 1;
633 y = top;
634 } else if (strcasecmp(s, "bottom-left") == 0) {
635 x = 0;
636 y = bottom;
637 } else if (strcasecmp(s, "bottom-right") == 0) {
638 x = w->sx - 1;
639 y = bottom;
640 } else
641 return (NULL);
643 return (window_get_active_at(w, x, y));
647 window_zoom(struct window_pane *wp)
649 struct window *w = wp->window;
650 struct window_pane *wp1;
652 if (w->flags & WINDOW_ZOOMED)
653 return (-1);
655 if (window_count_panes(w) == 1)
656 return (-1);
658 if (w->active != wp)
659 window_set_active_pane(w, wp, 1);
661 TAILQ_FOREACH(wp1, &w->panes, entry) {
662 wp1->saved_layout_cell = wp1->layout_cell;
663 wp1->layout_cell = NULL;
666 w->saved_layout_root = w->layout_root;
667 layout_init(w, wp);
668 w->flags |= WINDOW_ZOOMED;
669 notify_window("window-layout-changed", w);
671 return (0);
675 window_unzoom(struct window *w)
677 struct window_pane *wp;
679 if (!(w->flags & WINDOW_ZOOMED))
680 return (-1);
682 w->flags &= ~WINDOW_ZOOMED;
683 layout_free(w);
684 w->layout_root = w->saved_layout_root;
685 w->saved_layout_root = NULL;
687 TAILQ_FOREACH(wp, &w->panes, entry) {
688 wp->layout_cell = wp->saved_layout_cell;
689 wp->saved_layout_cell = NULL;
691 layout_fix_panes(w, NULL);
692 notify_window("window-layout-changed", w);
694 return (0);
698 window_push_zoom(struct window *w, int always, int flag)
700 log_debug("%s: @%u %d", __func__, w->id,
701 flag && (w->flags & WINDOW_ZOOMED));
702 if (flag && (always || (w->flags & WINDOW_ZOOMED)))
703 w->flags |= WINDOW_WASZOOMED;
704 else
705 w->flags &= ~WINDOW_WASZOOMED;
706 return (window_unzoom(w) == 0);
710 window_pop_zoom(struct window *w)
712 log_debug("%s: @%u %d", __func__, w->id,
713 !!(w->flags & WINDOW_WASZOOMED));
714 if (w->flags & WINDOW_WASZOOMED)
715 return (window_zoom(w->active) == 0);
716 return (0);
719 struct window_pane *
720 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
721 int flags)
723 struct window_pane *wp;
725 if (other == NULL)
726 other = w->active;
728 wp = window_pane_create(w, w->sx, w->sy, hlimit);
729 if (TAILQ_EMPTY(&w->panes)) {
730 log_debug("%s: @%u at start", __func__, w->id);
731 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
732 } else if (flags & SPAWN_BEFORE) {
733 log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
734 if (flags & SPAWN_FULLSIZE)
735 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
736 else
737 TAILQ_INSERT_BEFORE(other, wp, entry);
738 } else {
739 log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
740 if (flags & SPAWN_FULLSIZE)
741 TAILQ_INSERT_TAIL(&w->panes, wp, entry);
742 else
743 TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
745 return (wp);
748 void
749 window_lost_pane(struct window *w, struct window_pane *wp)
751 log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
753 if (wp == marked_pane.wp)
754 server_clear_marked();
756 window_pane_stack_remove(&w->last_panes, wp);
757 if (wp == w->active) {
758 w->active = TAILQ_FIRST(&w->last_panes);
759 if (w->active == NULL) {
760 w->active = TAILQ_PREV(wp, window_panes, entry);
761 if (w->active == NULL)
762 w->active = TAILQ_NEXT(wp, entry);
764 if (w->active != NULL) {
765 window_pane_stack_remove(&w->last_panes, w->active);
766 w->active->flags |= PANE_CHANGED;
767 notify_window("window-pane-changed", w);
768 window_update_focus(w);
773 void
774 window_remove_pane(struct window *w, struct window_pane *wp)
776 window_lost_pane(w, wp);
778 TAILQ_REMOVE(&w->panes, wp, entry);
779 window_pane_destroy(wp);
782 struct window_pane *
783 window_pane_at_index(struct window *w, u_int idx)
785 struct window_pane *wp;
786 u_int n;
788 n = options_get_number(w->options, "pane-base-index");
789 TAILQ_FOREACH(wp, &w->panes, entry) {
790 if (n == idx)
791 return (wp);
792 n++;
794 return (NULL);
797 struct window_pane *
798 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
800 for (; n > 0; n--) {
801 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
802 wp = TAILQ_FIRST(&w->panes);
805 return (wp);
808 struct window_pane *
809 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
810 u_int n)
812 for (; n > 0; n--) {
813 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
814 wp = TAILQ_LAST(&w->panes, window_panes);
817 return (wp);
821 window_pane_index(struct window_pane *wp, u_int *i)
823 struct window_pane *wq;
824 struct window *w = wp->window;
826 *i = options_get_number(w->options, "pane-base-index");
827 TAILQ_FOREACH(wq, &w->panes, entry) {
828 if (wp == wq) {
829 return (0);
831 (*i)++;
834 return (-1);
837 u_int
838 window_count_panes(struct window *w)
840 struct window_pane *wp;
841 u_int n;
843 n = 0;
844 TAILQ_FOREACH(wp, &w->panes, entry)
845 n++;
846 return (n);
849 void
850 window_destroy_panes(struct window *w)
852 struct window_pane *wp;
854 while (!TAILQ_EMPTY(&w->last_panes)) {
855 wp = TAILQ_FIRST(&w->last_panes);
856 window_pane_stack_remove(&w->last_panes, wp);
859 while (!TAILQ_EMPTY(&w->panes)) {
860 wp = TAILQ_FIRST(&w->panes);
861 TAILQ_REMOVE(&w->panes, wp, entry);
862 window_pane_destroy(wp);
866 const char *
867 window_printable_flags(struct winlink *wl, int escape)
869 struct session *s = wl->session;
870 static char flags[32];
871 int pos;
873 pos = 0;
874 if (wl->flags & WINLINK_ACTIVITY) {
875 flags[pos++] = '#';
876 if (escape)
877 flags[pos++] = '#';
879 if (wl->flags & WINLINK_BELL)
880 flags[pos++] = '!';
881 if (wl->flags & WINLINK_SILENCE)
882 flags[pos++] = '~';
883 if (wl == s->curw)
884 flags[pos++] = '*';
885 if (wl == TAILQ_FIRST(&s->lastw))
886 flags[pos++] = '-';
887 if (server_check_marked() && wl == marked_pane.wl)
888 flags[pos++] = 'M';
889 if (wl->window->flags & WINDOW_ZOOMED)
890 flags[pos++] = 'Z';
891 flags[pos] = '\0';
892 return (flags);
895 struct window_pane *
896 window_pane_find_by_id_str(const char *s)
898 const char *errstr;
899 u_int id;
901 if (*s != '%')
902 return (NULL);
904 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
905 if (errstr != NULL)
906 return (NULL);
907 return (window_pane_find_by_id(id));
910 struct window_pane *
911 window_pane_find_by_id(u_int id)
913 struct window_pane wp;
915 wp.id = id;
916 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
919 static struct window_pane *
920 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
922 struct window_pane *wp;
923 char host[HOST_NAME_MAX + 1];
925 wp = xcalloc(1, sizeof *wp);
926 wp->window = w;
927 wp->options = options_create(w->options);
928 wp->flags = PANE_STYLECHANGED;
930 wp->id = next_window_pane_id++;
931 RB_INSERT(window_pane_tree, &all_window_panes, wp);
933 wp->fd = -1;
935 TAILQ_INIT(&wp->modes);
937 TAILQ_INIT (&wp->resize_queue);
939 wp->sx = sx;
940 wp->sy = sy;
942 wp->pipe_fd = -1;
944 colour_palette_init(&wp->palette);
945 colour_palette_from_option(&wp->palette, wp->options);
947 screen_init(&wp->base, sx, sy, hlimit);
948 wp->screen = &wp->base;
949 window_pane_default_cursor(wp);
951 screen_init(&wp->status_screen, 1, 1, 0);
953 if (gethostname(host, sizeof host) == 0)
954 screen_set_title(&wp->base, host);
956 return (wp);
959 static void
960 window_pane_destroy(struct window_pane *wp)
962 struct window_pane_resize *r;
963 struct window_pane_resize *r1;
965 window_pane_reset_mode_all(wp);
966 free(wp->searchstr);
968 if (wp->fd != -1) {
969 #ifdef HAVE_UTEMPTER
970 utempter_remove_record(wp->fd);
971 #endif
972 bufferevent_free(wp->event);
973 close(wp->fd);
975 if (wp->ictx != NULL)
976 input_free(wp->ictx);
978 screen_free(&wp->status_screen);
980 screen_free(&wp->base);
982 if (wp->pipe_fd != -1) {
983 bufferevent_free(wp->pipe_event);
984 close(wp->pipe_fd);
987 if (event_initialized(&wp->resize_timer))
988 event_del(&wp->resize_timer);
989 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
990 TAILQ_REMOVE(&wp->resize_queue, r, entry);
991 free(r);
994 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
996 options_free(wp->options);
997 free((void *)wp->cwd);
998 free(wp->shell);
999 cmd_free_argv(wp->argc, wp->argv);
1000 colour_palette_free(&wp->palette);
1001 free(wp);
1004 static void
1005 window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1007 struct window_pane *wp = data;
1008 struct evbuffer *evb = wp->event->input;
1009 struct window_pane_offset *wpo = &wp->pipe_offset;
1010 size_t size = EVBUFFER_LENGTH(evb);
1011 char *new_data;
1012 size_t new_size;
1013 struct client *c;
1015 if (wp->pipe_fd != -1) {
1016 new_data = window_pane_get_new_data(wp, wpo, &new_size);
1017 if (new_size > 0) {
1018 bufferevent_write(wp->pipe_event, new_data, new_size);
1019 window_pane_update_used_data(wp, wpo, new_size);
1023 log_debug("%%%u has %zu bytes", wp->id, size);
1024 TAILQ_FOREACH(c, &clients, entry) {
1025 if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1026 control_write_output(c, wp);
1028 input_parse_pane(wp);
1029 bufferevent_disable(wp->event, EV_READ);
1032 static void
1033 window_pane_error_callback(__unused struct bufferevent *bufev,
1034 __unused short what, void *data)
1036 struct window_pane *wp = data;
1038 log_debug("%%%u error", wp->id);
1039 wp->flags |= PANE_EXITED;
1041 if (window_pane_destroy_ready(wp))
1042 server_destroy_pane(wp, 1);
1045 void
1046 window_pane_set_event(struct window_pane *wp)
1048 setblocking(wp->fd, 0);
1050 wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
1051 NULL, window_pane_error_callback, wp);
1052 if (wp->event == NULL)
1053 fatalx("out of memory");
1054 wp->ictx = input_init(wp, wp->event, &wp->palette);
1056 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1059 void
1060 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1062 struct window_mode_entry *wme;
1063 struct window_pane_resize *r;
1065 if (sx == wp->sx && sy == wp->sy)
1066 return;
1068 r = xmalloc(sizeof *r);
1069 r->sx = sx;
1070 r->sy = sy;
1071 r->osx = wp->sx;
1072 r->osy = wp->sy;
1073 TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
1075 wp->sx = sx;
1076 wp->sy = sy;
1078 log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1079 screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1081 wme = TAILQ_FIRST(&wp->modes);
1082 if (wme != NULL && wme->mode->resize != NULL)
1083 wme->mode->resize(wme, sx, sy);
1087 window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
1088 const struct window_mode *mode, struct cmd_find_state *fs,
1089 struct args *args)
1091 struct window_mode_entry *wme;
1093 if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1094 return (1);
1096 TAILQ_FOREACH(wme, &wp->modes, entry) {
1097 if (wme->mode == mode)
1098 break;
1100 if (wme != NULL) {
1101 TAILQ_REMOVE(&wp->modes, wme, entry);
1102 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1103 } else {
1104 wme = xcalloc(1, sizeof *wme);
1105 wme->wp = wp;
1106 wme->swp = swp;
1107 wme->mode = mode;
1108 wme->prefix = 1;
1109 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1110 wme->screen = wme->mode->init(wme, fs, args);
1113 wp->screen = wme->screen;
1114 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1116 server_redraw_window_borders(wp->window);
1117 server_status_window(wp->window);
1118 notify_pane("pane-mode-changed", wp);
1120 return (0);
1123 void
1124 window_pane_reset_mode(struct window_pane *wp)
1126 struct window_mode_entry *wme, *next;
1128 if (TAILQ_EMPTY(&wp->modes))
1129 return;
1131 wme = TAILQ_FIRST(&wp->modes);
1132 TAILQ_REMOVE(&wp->modes, wme, entry);
1133 wme->mode->free(wme);
1134 free(wme);
1136 next = TAILQ_FIRST(&wp->modes);
1137 if (next == NULL) {
1138 wp->flags &= ~PANE_UNSEENCHANGES;
1139 log_debug("%s: no next mode", __func__);
1140 wp->screen = &wp->base;
1141 } else {
1142 log_debug("%s: next mode is %s", __func__, next->mode->name);
1143 wp->screen = next->screen;
1144 if (next->mode->resize != NULL)
1145 next->mode->resize(next, wp->sx, wp->sy);
1147 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1149 server_redraw_window_borders(wp->window);
1150 server_status_window(wp->window);
1151 notify_pane("pane-mode-changed", wp);
1154 void
1155 window_pane_reset_mode_all(struct window_pane *wp)
1157 while (!TAILQ_EMPTY(&wp->modes))
1158 window_pane_reset_mode(wp);
1161 static void
1162 window_pane_copy_key(struct window_pane *wp, key_code key)
1164 struct window_pane *loop;
1166 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1167 if (loop != wp &&
1168 TAILQ_EMPTY(&loop->modes) &&
1169 loop->fd != -1 &&
1170 (~loop->flags & PANE_INPUTOFF) &&
1171 window_pane_visible(loop) &&
1172 options_get_number(loop->options, "synchronize-panes"))
1173 input_key_pane(loop, key, NULL);
1178 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1179 struct winlink *wl, key_code key, struct mouse_event *m)
1181 struct window_mode_entry *wme;
1183 if (KEYC_IS_MOUSE(key) && m == NULL)
1184 return (-1);
1186 wme = TAILQ_FIRST(&wp->modes);
1187 if (wme != NULL) {
1188 if (wme->mode->key != NULL && c != NULL) {
1189 key &= ~KEYC_MASK_FLAGS;
1190 wme->mode->key(wme, c, s, wl, key, m);
1192 return (0);
1195 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1196 return (0);
1198 if (input_key_pane(wp, key, m) != 0)
1199 return (-1);
1201 if (KEYC_IS_MOUSE(key))
1202 return (0);
1203 if (options_get_number(wp->options, "synchronize-panes"))
1204 window_pane_copy_key(wp, key);
1205 return (0);
1209 window_pane_visible(struct window_pane *wp)
1211 if (~wp->window->flags & WINDOW_ZOOMED)
1212 return (1);
1213 return (wp == wp->window->active);
1216 u_int
1217 window_pane_search(struct window_pane *wp, const char *term, int regex,
1218 int ignore)
1220 struct screen *s = &wp->base;
1221 regex_t r;
1222 char *new = NULL, *line;
1223 u_int i;
1224 int flags = 0, found;
1225 size_t n;
1227 if (!regex) {
1228 if (ignore)
1229 flags |= FNM_CASEFOLD;
1230 xasprintf(&new, "*%s*", term);
1231 } else {
1232 if (ignore)
1233 flags |= REG_ICASE;
1234 if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
1235 return (0);
1238 for (i = 0; i < screen_size_y(s); i++) {
1239 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1240 for (n = strlen(line); n > 0; n--) {
1241 if (!isspace((u_char)line[n - 1]))
1242 break;
1243 line[n - 1] = '\0';
1245 log_debug("%s: %s", __func__, line);
1246 if (!regex)
1247 found = (fnmatch(new, line, flags) == 0);
1248 else
1249 found = (regexec(&r, line, 0, NULL, 0) == 0);
1250 free(line);
1251 if (found)
1252 break;
1254 if (!regex)
1255 free(new);
1256 else
1257 regfree(&r);
1259 if (i == screen_size_y(s))
1260 return (0);
1261 return (i + 1);
1264 /* Get MRU pane from a list. */
1265 static struct window_pane *
1266 window_pane_choose_best(struct window_pane **list, u_int size)
1268 struct window_pane *next, *best;
1269 u_int i;
1271 if (size == 0)
1272 return (NULL);
1274 best = list[0];
1275 for (i = 1; i < size; i++) {
1276 next = list[i];
1277 if (next->active_point > best->active_point)
1278 best = next;
1280 return (best);
1284 * Find the pane directly above another. We build a list of those adjacent to
1285 * top edge and then choose the best.
1287 struct window_pane *
1288 window_pane_find_up(struct window_pane *wp)
1290 struct window *w;
1291 struct window_pane *next, *best, **list;
1292 u_int edge, left, right, end, size;
1293 int status, found;
1295 if (wp == NULL)
1296 return (NULL);
1297 w = wp->window;
1298 status = options_get_number(w->options, "pane-border-status");
1300 list = NULL;
1301 size = 0;
1303 edge = wp->yoff;
1304 if (status == PANE_STATUS_TOP) {
1305 if (edge == 1)
1306 edge = w->sy + 1;
1307 } else if (status == PANE_STATUS_BOTTOM) {
1308 if (edge == 0)
1309 edge = w->sy;
1310 } else {
1311 if (edge == 0)
1312 edge = w->sy + 1;
1315 left = wp->xoff;
1316 right = wp->xoff + wp->sx;
1318 TAILQ_FOREACH(next, &w->panes, entry) {
1319 if (next == wp)
1320 continue;
1321 if (next->yoff + next->sy + 1 != edge)
1322 continue;
1323 end = next->xoff + next->sx - 1;
1325 found = 0;
1326 if (next->xoff < left && end > right)
1327 found = 1;
1328 else if (next->xoff >= left && next->xoff <= right)
1329 found = 1;
1330 else if (end >= left && end <= right)
1331 found = 1;
1332 if (!found)
1333 continue;
1334 list = xreallocarray(list, size + 1, sizeof *list);
1335 list[size++] = next;
1338 best = window_pane_choose_best(list, size);
1339 free(list);
1340 return (best);
1343 /* Find the pane directly below another. */
1344 struct window_pane *
1345 window_pane_find_down(struct window_pane *wp)
1347 struct window *w;
1348 struct window_pane *next, *best, **list;
1349 u_int edge, left, right, end, size;
1350 int status, found;
1352 if (wp == NULL)
1353 return (NULL);
1354 w = wp->window;
1355 status = options_get_number(w->options, "pane-border-status");
1357 list = NULL;
1358 size = 0;
1360 edge = wp->yoff + wp->sy + 1;
1361 if (status == PANE_STATUS_TOP) {
1362 if (edge >= w->sy)
1363 edge = 1;
1364 } else if (status == PANE_STATUS_BOTTOM) {
1365 if (edge >= w->sy - 1)
1366 edge = 0;
1367 } else {
1368 if (edge >= w->sy)
1369 edge = 0;
1372 left = wp->xoff;
1373 right = wp->xoff + wp->sx;
1375 TAILQ_FOREACH(next, &w->panes, entry) {
1376 if (next == wp)
1377 continue;
1378 if (next->yoff != edge)
1379 continue;
1380 end = next->xoff + next->sx - 1;
1382 found = 0;
1383 if (next->xoff < left && end > right)
1384 found = 1;
1385 else if (next->xoff >= left && next->xoff <= right)
1386 found = 1;
1387 else if (end >= left && end <= right)
1388 found = 1;
1389 if (!found)
1390 continue;
1391 list = xreallocarray(list, size + 1, sizeof *list);
1392 list[size++] = next;
1395 best = window_pane_choose_best(list, size);
1396 free(list);
1397 return (best);
1400 /* Find the pane directly to the left of another. */
1401 struct window_pane *
1402 window_pane_find_left(struct window_pane *wp)
1404 struct window *w;
1405 struct window_pane *next, *best, **list;
1406 u_int edge, top, bottom, end, size;
1407 int found;
1409 if (wp == NULL)
1410 return (NULL);
1411 w = wp->window;
1413 list = NULL;
1414 size = 0;
1416 edge = wp->xoff;
1417 if (edge == 0)
1418 edge = w->sx + 1;
1420 top = wp->yoff;
1421 bottom = wp->yoff + wp->sy;
1423 TAILQ_FOREACH(next, &w->panes, entry) {
1424 if (next == wp)
1425 continue;
1426 if (next->xoff + next->sx + 1 != edge)
1427 continue;
1428 end = next->yoff + next->sy - 1;
1430 found = 0;
1431 if (next->yoff < top && end > bottom)
1432 found = 1;
1433 else if (next->yoff >= top && next->yoff <= bottom)
1434 found = 1;
1435 else if (end >= top && end <= bottom)
1436 found = 1;
1437 if (!found)
1438 continue;
1439 list = xreallocarray(list, size + 1, sizeof *list);
1440 list[size++] = next;
1443 best = window_pane_choose_best(list, size);
1444 free(list);
1445 return (best);
1448 /* Find the pane directly to the right of another. */
1449 struct window_pane *
1450 window_pane_find_right(struct window_pane *wp)
1452 struct window *w;
1453 struct window_pane *next, *best, **list;
1454 u_int edge, top, bottom, end, size;
1455 int found;
1457 if (wp == NULL)
1458 return (NULL);
1459 w = wp->window;
1461 list = NULL;
1462 size = 0;
1464 edge = wp->xoff + wp->sx + 1;
1465 if (edge >= w->sx)
1466 edge = 0;
1468 top = wp->yoff;
1469 bottom = wp->yoff + wp->sy;
1471 TAILQ_FOREACH(next, &w->panes, entry) {
1472 if (next == wp)
1473 continue;
1474 if (next->xoff != edge)
1475 continue;
1476 end = next->yoff + next->sy - 1;
1478 found = 0;
1479 if (next->yoff < top && end > bottom)
1480 found = 1;
1481 else if (next->yoff >= top && next->yoff <= bottom)
1482 found = 1;
1483 else if (end >= top && end <= bottom)
1484 found = 1;
1485 if (!found)
1486 continue;
1487 list = xreallocarray(list, size + 1, sizeof *list);
1488 list[size++] = next;
1491 best = window_pane_choose_best(list, size);
1492 free(list);
1493 return (best);
1496 void
1497 window_pane_stack_push(struct window_panes *stack, struct window_pane *wp)
1499 if (wp != NULL) {
1500 window_pane_stack_remove(stack, wp);
1501 TAILQ_INSERT_HEAD(stack, wp, sentry);
1502 wp->flags |= PANE_VISITED;
1506 void
1507 window_pane_stack_remove(struct window_panes *stack, struct window_pane *wp)
1509 if (wp != NULL && (wp->flags & PANE_VISITED)) {
1510 TAILQ_REMOVE(stack, wp, sentry);
1511 wp->flags &= ~PANE_VISITED;
1515 /* Clear alert flags for a winlink */
1516 void
1517 winlink_clear_flags(struct winlink *wl)
1519 struct winlink *loop;
1521 wl->window->flags &= ~WINDOW_ALERTFLAGS;
1522 TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1523 if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1524 loop->flags &= ~WINLINK_ALERTFLAGS;
1525 server_status_session(loop->session);
1530 /* Shuffle window indexes up. */
1532 winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1534 int idx, last;
1536 if (wl == NULL)
1537 return (-1);
1538 if (before)
1539 idx = wl->idx;
1540 else
1541 idx = wl->idx + 1;
1543 /* Find the next free index. */
1544 for (last = idx; last < INT_MAX; last++) {
1545 if (winlink_find_by_index(&s->windows, last) == NULL)
1546 break;
1548 if (last == INT_MAX)
1549 return (-1);
1551 /* Move everything from last - 1 to idx up a bit. */
1552 for (; last > idx; last--) {
1553 wl = winlink_find_by_index(&s->windows, last - 1);
1554 RB_REMOVE(winlinks, &s->windows, wl);
1555 wl->idx++;
1556 RB_INSERT(winlinks, &s->windows, wl);
1559 return (idx);
1562 static void
1563 window_pane_input_callback(struct client *c, __unused const char *path,
1564 int error, int closed, struct evbuffer *buffer, void *data)
1566 struct window_pane_input_data *cdata = data;
1567 struct window_pane *wp;
1568 u_char *buf = EVBUFFER_DATA(buffer);
1569 size_t len = EVBUFFER_LENGTH(buffer);
1571 wp = window_pane_find_by_id(cdata->wp);
1572 if (cdata->file != NULL && (wp == NULL || c->flags & CLIENT_DEAD)) {
1573 if (wp == NULL) {
1574 c->retval = 1;
1575 c->flags |= CLIENT_EXIT;
1577 file_cancel(cdata->file);
1578 } else if (cdata->file == NULL || closed || error != 0) {
1579 cmdq_continue(cdata->item);
1580 server_client_unref(c);
1581 free(cdata);
1582 } else
1583 input_parse_buffer(wp, buf, len);
1584 evbuffer_drain(buffer, len);
1588 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1589 char **cause)
1591 struct client *c = cmdq_get_client(item);
1592 struct window_pane_input_data *cdata;
1594 if (~wp->flags & PANE_EMPTY) {
1595 *cause = xstrdup("pane is not empty");
1596 return (-1);
1598 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1599 return (1);
1600 if (c->session != NULL)
1601 return (1);
1603 cdata = xmalloc(sizeof *cdata);
1604 cdata->item = item;
1605 cdata->wp = wp->id;
1606 cdata->file = file_read(c, "-", window_pane_input_callback, cdata);
1607 c->references++;
1609 return (0);
1612 void *
1613 window_pane_get_new_data(struct window_pane *wp,
1614 struct window_pane_offset *wpo, size_t *size)
1616 size_t used = wpo->used - wp->base_offset;
1618 *size = EVBUFFER_LENGTH(wp->event->input) - used;
1619 return (EVBUFFER_DATA(wp->event->input) + used);
1622 void
1623 window_pane_update_used_data(struct window_pane *wp,
1624 struct window_pane_offset *wpo, size_t size)
1626 size_t used = wpo->used - wp->base_offset;
1628 if (size > EVBUFFER_LENGTH(wp->event->input) - used)
1629 size = EVBUFFER_LENGTH(wp->event->input) - used;
1630 wpo->used += size;
1633 void
1634 window_set_fill_character(struct window *w)
1636 const char *value;
1637 struct utf8_data *ud;
1639 free(w->fill_character);
1640 w->fill_character = NULL;
1642 value = options_get_string(w->options, "fill-character");
1643 if (*value != '\0' && utf8_isvalid(value)) {
1644 ud = utf8_fromcstr(value);
1645 if (ud != NULL && ud[0].width == 1)
1646 w->fill_character = ud;
1650 void
1651 window_pane_default_cursor(struct window_pane *wp)
1653 struct screen *s = wp->screen;
1654 int c;
1656 c = options_get_number(wp->options, "cursor-colour");
1657 s->default_ccolour = c;
1659 c = options_get_number(wp->options, "cursor-style");
1660 s->default_mode = 0;
1661 screen_set_cursor_style(c, &s->default_cstyle, &s->default_mode);