Fix user hooks (which are strings not arrays).
[tmux.git] / window.c
blobfcc61c2a66c1c759a6d93cc6bfec7608e85708ff
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;
69 static struct window_pane *window_pane_create(struct window *, u_int, u_int,
70 u_int);
71 static void window_pane_destroy(struct window_pane *);
73 RB_GENERATE(windows, window, entry, window_cmp);
74 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
75 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
77 int
78 window_cmp(struct window *w1, struct window *w2)
80 return (w1->id - w2->id);
83 int
84 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
86 return (wl1->idx - wl2->idx);
89 int
90 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
92 return (wp1->id - wp2->id);
95 struct winlink *
96 winlink_find_by_window(struct winlinks *wwl, struct window *w)
98 struct winlink *wl;
100 RB_FOREACH(wl, winlinks, wwl) {
101 if (wl->window == w)
102 return (wl);
105 return (NULL);
108 struct winlink *
109 winlink_find_by_index(struct winlinks *wwl, int idx)
111 struct winlink wl;
113 if (idx < 0)
114 fatalx("bad index");
116 wl.idx = idx;
117 return (RB_FIND(winlinks, wwl, &wl));
120 struct winlink *
121 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
123 struct winlink *wl;
125 RB_FOREACH(wl, winlinks, wwl) {
126 if (wl->window->id == id)
127 return (wl);
129 return (NULL);
132 static int
133 winlink_next_index(struct winlinks *wwl, int idx)
135 int i;
137 i = idx;
138 do {
139 if (winlink_find_by_index(wwl, i) == NULL)
140 return (i);
141 if (i == INT_MAX)
142 i = 0;
143 else
144 i++;
145 } while (i != idx);
146 return (-1);
149 u_int
150 winlink_count(struct winlinks *wwl)
152 struct winlink *wl;
153 u_int n;
155 n = 0;
156 RB_FOREACH(wl, winlinks, wwl)
157 n++;
159 return (n);
162 struct winlink *
163 winlink_add(struct winlinks *wwl, int idx)
165 struct winlink *wl;
167 if (idx < 0) {
168 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
169 return (NULL);
170 } else if (winlink_find_by_index(wwl, idx) != NULL)
171 return (NULL);
173 wl = xcalloc(1, sizeof *wl);
174 wl->idx = idx;
175 RB_INSERT(winlinks, wwl, wl);
177 return (wl);
180 void
181 winlink_set_window(struct winlink *wl, struct window *w)
183 if (wl->window != NULL) {
184 TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
185 window_remove_ref(wl->window, __func__);
187 TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
188 wl->window = w;
189 window_add_ref(w, __func__);
192 void
193 winlink_remove(struct winlinks *wwl, struct winlink *wl)
195 struct window *w = wl->window;
197 if (w != NULL) {
198 TAILQ_REMOVE(&w->winlinks, wl, wentry);
199 window_remove_ref(w, __func__);
202 RB_REMOVE(winlinks, wwl, wl);
203 free(wl);
206 struct winlink *
207 winlink_next(struct winlink *wl)
209 return (RB_NEXT(winlinks, wwl, wl));
212 struct winlink *
213 winlink_previous(struct winlink *wl)
215 return (RB_PREV(winlinks, wwl, wl));
218 struct winlink *
219 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
221 for (; n > 0; n--) {
222 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
223 wl = RB_MIN(winlinks, &s->windows);
226 return (wl);
229 struct winlink *
230 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
232 for (; n > 0; n--) {
233 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
234 wl = RB_MAX(winlinks, &s->windows);
237 return (wl);
240 void
241 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
243 if (wl == NULL)
244 return;
246 winlink_stack_remove(stack, wl);
247 TAILQ_INSERT_HEAD(stack, wl, sentry);
250 void
251 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
253 struct winlink *wl2;
255 if (wl == NULL)
256 return;
258 TAILQ_FOREACH(wl2, stack, sentry) {
259 if (wl2 == wl) {
260 TAILQ_REMOVE(stack, wl, sentry);
261 return;
266 struct window *
267 window_find_by_id_str(const char *s)
269 const char *errstr;
270 u_int id;
272 if (*s != '@')
273 return (NULL);
275 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
276 if (errstr != NULL)
277 return (NULL);
278 return (window_find_by_id(id));
281 struct window *
282 window_find_by_id(u_int id)
284 struct window w;
286 w.id = id;
287 return (RB_FIND(windows, &windows, &w));
290 void
291 window_update_activity(struct window *w)
293 gettimeofday(&w->activity_time, NULL);
294 alerts_queue(w, WINDOW_ACTIVITY);
297 struct window *
298 window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel)
300 struct window *w;
302 if (xpixel == 0)
303 xpixel = DEFAULT_XPIXEL;
304 if (ypixel == 0)
305 ypixel = DEFAULT_YPIXEL;
307 w = xcalloc(1, sizeof *w);
308 w->name = xstrdup("");
309 w->flags = 0;
311 TAILQ_INIT(&w->panes);
312 w->active = NULL;
314 w->lastlayout = -1;
315 w->layout_root = NULL;
317 w->sx = sx;
318 w->sy = sy;
319 w->manual_sx = sx;
320 w->manual_sy = sy;
321 w->xpixel = xpixel;
322 w->ypixel = ypixel;
324 w->options = options_create(global_w_options);
326 w->references = 0;
327 TAILQ_INIT(&w->winlinks);
329 w->id = next_window_id++;
330 RB_INSERT(windows, &windows, w);
332 window_update_activity(w);
334 log_debug("%s: @%u create %ux%u (%ux%u)", __func__, w->id, sx, sy,
335 w->xpixel, w->ypixel);
336 return (w);
339 static void
340 window_destroy(struct window *w)
342 log_debug("window @%u destroyed (%d references)", w->id, w->references);
344 RB_REMOVE(windows, &windows, w);
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);
352 window_destroy_panes(w);
354 if (event_initialized(&w->name_event))
355 evtimer_del(&w->name_event);
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);
362 options_free(w->options);
364 free(w->name);
365 free(w);
369 window_pane_destroy_ready(struct window_pane *wp)
371 int n;
373 if (wp->pipe_fd != -1) {
374 if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
375 return (0);
376 if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0)
377 return (0);
380 if (~wp->flags & PANE_EXITED)
381 return (0);
382 return (1);
385 void
386 window_add_ref(struct window *w, const char *from)
388 w->references++;
389 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
392 void
393 window_remove_ref(struct window *w, const char *from)
395 w->references--;
396 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
398 if (w->references == 0)
399 window_destroy(w);
402 void
403 window_set_name(struct window *w, const char *new_name)
405 free(w->name);
406 utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
407 notify_window("window-renamed", w);
410 void
411 window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
413 if (xpixel == 0)
414 xpixel = DEFAULT_XPIXEL;
415 if (ypixel == 0)
416 ypixel = DEFAULT_YPIXEL;
418 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__, w->id, sx, sy,
419 xpixel == -1 ? w->xpixel : (u_int)xpixel,
420 ypixel == -1 ? w->ypixel : (u_int)ypixel);
421 w->sx = sx;
422 w->sy = sy;
423 if (xpixel != -1)
424 w->xpixel = xpixel;
425 if (ypixel != -1)
426 w->ypixel = ypixel;
429 void
430 window_pane_send_resize(struct window_pane *wp, u_int sx, u_int sy)
432 struct window *w = wp->window;
433 struct winsize ws;
435 if (wp->fd == -1)
436 return;
438 log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, sx, sy);
440 memset(&ws, 0, sizeof ws);
441 ws.ws_col = sx;
442 ws.ws_row = sy;
443 ws.ws_xpixel = w->xpixel * ws.ws_col;
444 ws.ws_ypixel = w->ypixel * ws.ws_row;
445 if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
446 #ifdef __sun
448 * Some versions of Solaris apparently can return an error when
449 * resizing; don't know why this happens, can't reproduce on
450 * other platforms and ignoring it doesn't seem to cause any
451 * issues.
453 if (errno != EINVAL && errno != ENXIO)
454 #endif
455 fatal("ioctl failed");
459 window_has_pane(struct window *w, struct window_pane *wp)
461 struct window_pane *wp1;
463 TAILQ_FOREACH(wp1, &w->panes, entry) {
464 if (wp1 == wp)
465 return (1);
467 return (0);
470 void
471 window_update_focus(struct window *w)
473 if (w != NULL) {
474 log_debug("%s: @%u", __func__, w->id);
475 window_pane_update_focus(w->active);
479 void
480 window_pane_update_focus(struct window_pane *wp)
482 struct client *c;
483 int focused = 0;
485 if (wp != NULL) {
486 if (wp != wp->window->active)
487 focused = 0;
488 else {
489 TAILQ_FOREACH(c, &clients, entry) {
490 if (c->session != NULL &&
491 c->session->attached != 0 &&
492 (c->flags & CLIENT_FOCUSED) &&
493 c->session->curw->window == wp->window) {
494 focused = 1;
495 break;
499 if (!focused && (wp->flags & PANE_FOCUSED)) {
500 log_debug("%s: %%%u focus out", __func__, wp->id);
501 if (wp->base.mode & MODE_FOCUSON)
502 bufferevent_write(wp->event, "\033[O", 3);
503 notify_pane("pane-focus-out", wp);
504 wp->flags &= ~PANE_FOCUSED;
505 } else if (focused && (~wp->flags & PANE_FOCUSED)) {
506 log_debug("%s: %%%u focus in", __func__, wp->id);
507 if (wp->base.mode & MODE_FOCUSON)
508 bufferevent_write(wp->event, "\033[I", 3);
509 notify_pane("pane-focus-in", wp);
510 wp->flags |= PANE_FOCUSED;
511 } else
512 log_debug("%s: %%%u focus unchanged", __func__, wp->id);
517 window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
519 log_debug("%s: pane %%%u", __func__, wp->id);
521 if (wp == w->active)
522 return (0);
523 w->last = w->active;
525 w->active = wp;
526 w->active->active_point = next_active_point++;
527 w->active->flags |= PANE_CHANGED;
529 if (options_get_number(global_options, "focus-events")) {
530 window_pane_update_focus(w->last);
531 window_pane_update_focus(w->active);
534 tty_update_window_offset(w);
536 if (notify)
537 notify_window("window-pane-changed", w);
538 return (1);
541 static int
542 window_pane_get_palette(struct window_pane *wp, int c)
544 if (wp == NULL)
545 return (-1);
546 return (colour_palette_get(&wp->palette, c));
549 void
550 window_redraw_active_switch(struct window *w, struct window_pane *wp)
552 struct grid_cell *gc1, *gc2;
553 int c1, c2;
555 if (wp == w->active)
556 return;
558 for (;;) {
560 * If the active and inactive styles or palettes are different,
561 * need to redraw the panes.
563 gc1 = &wp->cached_gc;
564 gc2 = &wp->cached_active_gc;
565 if (!grid_cells_look_equal(gc1, gc2))
566 wp->flags |= PANE_REDRAW;
567 else {
568 c1 = window_pane_get_palette(wp, gc1->fg);
569 c2 = window_pane_get_palette(wp, gc2->fg);
570 if (c1 != c2)
571 wp->flags |= PANE_REDRAW;
572 else {
573 c1 = window_pane_get_palette(wp, gc1->bg);
574 c2 = window_pane_get_palette(wp, gc2->bg);
575 if (c1 != c2)
576 wp->flags |= PANE_REDRAW;
579 if (wp == w->active)
580 break;
581 wp = w->active;
585 struct window_pane *
586 window_get_active_at(struct window *w, u_int x, u_int y)
588 struct window_pane *wp;
590 TAILQ_FOREACH(wp, &w->panes, entry) {
591 if (!window_pane_visible(wp))
592 continue;
593 if (x < wp->xoff || x > wp->xoff + wp->sx)
594 continue;
595 if (y < wp->yoff || y > wp->yoff + wp->sy)
596 continue;
597 return (wp);
599 return (NULL);
602 struct window_pane *
603 window_find_string(struct window *w, const char *s)
605 u_int x, y, top = 0, bottom = w->sy - 1;
606 int status;
608 x = w->sx / 2;
609 y = w->sy / 2;
611 status = options_get_number(w->options, "pane-border-status");
612 if (status == PANE_STATUS_TOP)
613 top++;
614 else if (status == PANE_STATUS_BOTTOM)
615 bottom--;
617 if (strcasecmp(s, "top") == 0)
618 y = top;
619 else if (strcasecmp(s, "bottom") == 0)
620 y = bottom;
621 else if (strcasecmp(s, "left") == 0)
622 x = 0;
623 else if (strcasecmp(s, "right") == 0)
624 x = w->sx - 1;
625 else if (strcasecmp(s, "top-left") == 0) {
626 x = 0;
627 y = top;
628 } else if (strcasecmp(s, "top-right") == 0) {
629 x = w->sx - 1;
630 y = top;
631 } else if (strcasecmp(s, "bottom-left") == 0) {
632 x = 0;
633 y = bottom;
634 } else if (strcasecmp(s, "bottom-right") == 0) {
635 x = w->sx - 1;
636 y = bottom;
637 } else
638 return (NULL);
640 return (window_get_active_at(w, x, y));
644 window_zoom(struct window_pane *wp)
646 struct window *w = wp->window;
647 struct window_pane *wp1;
649 if (w->flags & WINDOW_ZOOMED)
650 return (-1);
652 if (window_count_panes(w) == 1)
653 return (-1);
655 if (w->active != wp)
656 window_set_active_pane(w, wp, 1);
658 TAILQ_FOREACH(wp1, &w->panes, entry) {
659 wp1->saved_layout_cell = wp1->layout_cell;
660 wp1->layout_cell = NULL;
663 w->saved_layout_root = w->layout_root;
664 layout_init(w, wp);
665 w->flags |= WINDOW_ZOOMED;
666 notify_window("window-layout-changed", w);
668 return (0);
672 window_unzoom(struct window *w)
674 struct window_pane *wp;
676 if (!(w->flags & WINDOW_ZOOMED))
677 return (-1);
679 w->flags &= ~WINDOW_ZOOMED;
680 layout_free(w);
681 w->layout_root = w->saved_layout_root;
682 w->saved_layout_root = NULL;
684 TAILQ_FOREACH(wp, &w->panes, entry) {
685 wp->layout_cell = wp->saved_layout_cell;
686 wp->saved_layout_cell = NULL;
688 layout_fix_panes(w, NULL);
689 notify_window("window-layout-changed", w);
691 return (0);
695 window_push_zoom(struct window *w, int always, int flag)
697 log_debug("%s: @%u %d", __func__, w->id,
698 flag && (w->flags & WINDOW_ZOOMED));
699 if (flag && (always || (w->flags & WINDOW_ZOOMED)))
700 w->flags |= WINDOW_WASZOOMED;
701 else
702 w->flags &= ~WINDOW_WASZOOMED;
703 return (window_unzoom(w) == 0);
707 window_pop_zoom(struct window *w)
709 log_debug("%s: @%u %d", __func__, w->id,
710 !!(w->flags & WINDOW_WASZOOMED));
711 if (w->flags & WINDOW_WASZOOMED)
712 return (window_zoom(w->active) == 0);
713 return (0);
716 struct window_pane *
717 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
718 int flags)
720 struct window_pane *wp;
722 if (other == NULL)
723 other = w->active;
725 wp = window_pane_create(w, w->sx, w->sy, hlimit);
726 if (TAILQ_EMPTY(&w->panes)) {
727 log_debug("%s: @%u at start", __func__, w->id);
728 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
729 } else if (flags & SPAWN_BEFORE) {
730 log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
731 if (flags & SPAWN_FULLSIZE)
732 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
733 else
734 TAILQ_INSERT_BEFORE(other, wp, entry);
735 } else {
736 log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
737 if (flags & SPAWN_FULLSIZE)
738 TAILQ_INSERT_TAIL(&w->panes, wp, entry);
739 else
740 TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
742 return (wp);
745 void
746 window_lost_pane(struct window *w, struct window_pane *wp)
748 log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
750 if (wp == marked_pane.wp)
751 server_clear_marked();
753 if (wp == w->active) {
754 w->active = w->last;
755 w->last = NULL;
756 if (w->active == NULL) {
757 w->active = TAILQ_PREV(wp, window_panes, entry);
758 if (w->active == NULL)
759 w->active = TAILQ_NEXT(wp, entry);
761 if (w->active != NULL) {
762 w->active->flags |= PANE_CHANGED;
763 notify_window("window-pane-changed", w);
764 window_update_focus(w);
766 } else if (wp == w->last)
767 w->last = NULL;
770 void
771 window_remove_pane(struct window *w, struct window_pane *wp)
773 window_lost_pane(w, wp);
775 TAILQ_REMOVE(&w->panes, wp, entry);
776 window_pane_destroy(wp);
779 struct window_pane *
780 window_pane_at_index(struct window *w, u_int idx)
782 struct window_pane *wp;
783 u_int n;
785 n = options_get_number(w->options, "pane-base-index");
786 TAILQ_FOREACH(wp, &w->panes, entry) {
787 if (n == idx)
788 return (wp);
789 n++;
791 return (NULL);
794 struct window_pane *
795 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
797 for (; n > 0; n--) {
798 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
799 wp = TAILQ_FIRST(&w->panes);
802 return (wp);
805 struct window_pane *
806 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
807 u_int n)
809 for (; n > 0; n--) {
810 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
811 wp = TAILQ_LAST(&w->panes, window_panes);
814 return (wp);
818 window_pane_index(struct window_pane *wp, u_int *i)
820 struct window_pane *wq;
821 struct window *w = wp->window;
823 *i = options_get_number(w->options, "pane-base-index");
824 TAILQ_FOREACH(wq, &w->panes, entry) {
825 if (wp == wq) {
826 return (0);
828 (*i)++;
831 return (-1);
834 u_int
835 window_count_panes(struct window *w)
837 struct window_pane *wp;
838 u_int n;
840 n = 0;
841 TAILQ_FOREACH(wp, &w->panes, entry)
842 n++;
843 return (n);
846 void
847 window_destroy_panes(struct window *w)
849 struct window_pane *wp;
851 while (!TAILQ_EMPTY(&w->panes)) {
852 wp = TAILQ_FIRST(&w->panes);
853 TAILQ_REMOVE(&w->panes, wp, entry);
854 window_pane_destroy(wp);
858 const char *
859 window_printable_flags(struct winlink *wl, int escape)
861 struct session *s = wl->session;
862 static char flags[32];
863 int pos;
865 pos = 0;
866 if (wl->flags & WINLINK_ACTIVITY) {
867 flags[pos++] = '#';
868 if (escape)
869 flags[pos++] = '#';
871 if (wl->flags & WINLINK_BELL)
872 flags[pos++] = '!';
873 if (wl->flags & WINLINK_SILENCE)
874 flags[pos++] = '~';
875 if (wl == s->curw)
876 flags[pos++] = '*';
877 if (wl == TAILQ_FIRST(&s->lastw))
878 flags[pos++] = '-';
879 if (server_check_marked() && wl == marked_pane.wl)
880 flags[pos++] = 'M';
881 if (wl->window->flags & WINDOW_ZOOMED)
882 flags[pos++] = 'Z';
883 flags[pos] = '\0';
884 return (flags);
887 struct window_pane *
888 window_pane_find_by_id_str(const char *s)
890 const char *errstr;
891 u_int id;
893 if (*s != '%')
894 return (NULL);
896 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
897 if (errstr != NULL)
898 return (NULL);
899 return (window_pane_find_by_id(id));
902 struct window_pane *
903 window_pane_find_by_id(u_int id)
905 struct window_pane wp;
907 wp.id = id;
908 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
911 static struct window_pane *
912 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
914 struct window_pane *wp;
915 char host[HOST_NAME_MAX + 1];
917 wp = xcalloc(1, sizeof *wp);
918 wp->window = w;
919 wp->options = options_create(w->options);
920 wp->flags = PANE_STYLECHANGED;
922 wp->id = next_window_pane_id++;
923 RB_INSERT(window_pane_tree, &all_window_panes, wp);
925 wp->fd = -1;
927 TAILQ_INIT(&wp->modes);
929 TAILQ_INIT (&wp->resize_queue);
931 wp->sx = sx;
932 wp->sy = sy;
934 wp->pipe_fd = -1;
936 colour_palette_init(&wp->palette);
937 colour_palette_from_option(&wp->palette, wp->options);
939 screen_init(&wp->base, sx, sy, hlimit);
940 wp->screen = &wp->base;
942 screen_init(&wp->status_screen, 1, 1, 0);
944 if (gethostname(host, sizeof host) == 0)
945 screen_set_title(&wp->base, host);
947 return (wp);
950 static void
951 window_pane_destroy(struct window_pane *wp)
953 struct window_pane_resize *r;
954 struct window_pane_resize *r1;
956 window_pane_reset_mode_all(wp);
957 free(wp->searchstr);
959 if (wp->fd != -1) {
960 #ifdef HAVE_UTEMPTER
961 utempter_remove_record(wp->fd);
962 #endif
963 bufferevent_free(wp->event);
964 close(wp->fd);
966 if (wp->ictx != NULL)
967 input_free(wp->ictx);
969 screen_free(&wp->status_screen);
971 screen_free(&wp->base);
973 if (wp->pipe_fd != -1) {
974 bufferevent_free(wp->pipe_event);
975 close(wp->pipe_fd);
978 if (event_initialized(&wp->resize_timer))
979 event_del(&wp->resize_timer);
980 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
981 TAILQ_REMOVE(&wp->resize_queue, r, entry);
982 free(r);
985 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
987 options_free(wp->options);
988 free((void *)wp->cwd);
989 free(wp->shell);
990 cmd_free_argv(wp->argc, wp->argv);
991 colour_palette_free(&wp->palette);
992 free(wp);
995 static void
996 window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
998 struct window_pane *wp = data;
999 struct evbuffer *evb = wp->event->input;
1000 struct window_pane_offset *wpo = &wp->pipe_offset;
1001 size_t size = EVBUFFER_LENGTH(evb);
1002 char *new_data;
1003 size_t new_size;
1004 struct client *c;
1006 if (wp->pipe_fd != -1) {
1007 new_data = window_pane_get_new_data(wp, wpo, &new_size);
1008 if (new_size > 0) {
1009 bufferevent_write(wp->pipe_event, new_data, new_size);
1010 window_pane_update_used_data(wp, wpo, new_size);
1014 log_debug("%%%u has %zu bytes", wp->id, size);
1015 TAILQ_FOREACH(c, &clients, entry) {
1016 if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1017 control_write_output(c, wp);
1019 input_parse_pane(wp);
1020 bufferevent_disable(wp->event, EV_READ);
1023 static void
1024 window_pane_error_callback(__unused struct bufferevent *bufev,
1025 __unused short what, void *data)
1027 struct window_pane *wp = data;
1029 log_debug("%%%u error", wp->id);
1030 wp->flags |= PANE_EXITED;
1032 if (window_pane_destroy_ready(wp))
1033 server_destroy_pane(wp, 1);
1036 void
1037 window_pane_set_event(struct window_pane *wp)
1039 setblocking(wp->fd, 0);
1041 wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
1042 NULL, window_pane_error_callback, wp);
1043 wp->ictx = input_init(wp, wp->event, &wp->palette);
1045 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1048 void
1049 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1051 struct window_mode_entry *wme;
1052 struct window_pane_resize *r;
1054 if (sx == wp->sx && sy == wp->sy)
1055 return;
1057 r = xmalloc (sizeof *r);
1058 r->sx = sx;
1059 r->sy = sy;
1060 r->osx = wp->sx;
1061 r->osy = wp->sy;
1062 TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
1064 wp->sx = sx;
1065 wp->sy = sy;
1067 log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1068 screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1070 wme = TAILQ_FIRST(&wp->modes);
1071 if (wme != NULL && wme->mode->resize != NULL)
1072 wme->mode->resize(wme, sx, sy);
1076 window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
1077 const struct window_mode *mode, struct cmd_find_state *fs,
1078 struct args *args)
1080 struct window_mode_entry *wme;
1082 if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1083 return (1);
1085 TAILQ_FOREACH(wme, &wp->modes, entry) {
1086 if (wme->mode == mode)
1087 break;
1089 if (wme != NULL) {
1090 TAILQ_REMOVE(&wp->modes, wme, entry);
1091 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1092 } else {
1093 wme = xcalloc(1, sizeof *wme);
1094 wme->wp = wp;
1095 wme->swp = swp;
1096 wme->mode = mode;
1097 wme->prefix = 1;
1098 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1099 wme->screen = wme->mode->init(wme, fs, args);
1102 wp->screen = wme->screen;
1103 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1105 server_redraw_window_borders(wp->window);
1106 server_status_window(wp->window);
1107 notify_pane("pane-mode-changed", wp);
1109 return (0);
1112 void
1113 window_pane_reset_mode(struct window_pane *wp)
1115 struct window_mode_entry *wme, *next;
1117 if (TAILQ_EMPTY(&wp->modes))
1118 return;
1120 wme = TAILQ_FIRST(&wp->modes);
1121 TAILQ_REMOVE(&wp->modes, wme, entry);
1122 wme->mode->free(wme);
1123 free(wme);
1125 next = TAILQ_FIRST(&wp->modes);
1126 if (next == NULL) {
1127 log_debug("%s: no next mode", __func__);
1128 wp->screen = &wp->base;
1129 } else {
1130 log_debug("%s: next mode is %s", __func__, next->mode->name);
1131 wp->screen = next->screen;
1132 if (next->mode->resize != NULL)
1133 next->mode->resize(next, wp->sx, wp->sy);
1135 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1137 server_redraw_window_borders(wp->window);
1138 server_status_window(wp->window);
1139 notify_pane("pane-mode-changed", wp);
1142 void
1143 window_pane_reset_mode_all(struct window_pane *wp)
1145 while (!TAILQ_EMPTY(&wp->modes))
1146 window_pane_reset_mode(wp);
1149 static void
1150 window_pane_copy_key(struct window_pane *wp, key_code key)
1152 struct window_pane *loop;
1154 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1155 if (loop != wp &&
1156 TAILQ_EMPTY(&loop->modes) &&
1157 loop->fd != -1 &&
1158 (~loop->flags & PANE_INPUTOFF) &&
1159 window_pane_visible(loop) &&
1160 options_get_number(loop->options, "synchronize-panes"))
1161 input_key_pane(loop, key, NULL);
1166 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1167 struct winlink *wl, key_code key, struct mouse_event *m)
1169 struct window_mode_entry *wme;
1171 if (KEYC_IS_MOUSE(key) && m == NULL)
1172 return (-1);
1174 wme = TAILQ_FIRST(&wp->modes);
1175 if (wme != NULL) {
1176 if (wme->mode->key != NULL && c != NULL) {
1177 key &= ~KEYC_MASK_FLAGS;
1178 wme->mode->key(wme, c, s, wl, key, m);
1180 return (0);
1183 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1184 return (0);
1186 if (input_key_pane(wp, key, m) != 0)
1187 return (-1);
1189 if (KEYC_IS_MOUSE(key))
1190 return (0);
1191 if (options_get_number(wp->options, "synchronize-panes"))
1192 window_pane_copy_key(wp, key);
1193 return (0);
1197 window_pane_visible(struct window_pane *wp)
1199 if (~wp->window->flags & WINDOW_ZOOMED)
1200 return (1);
1201 return (wp == wp->window->active);
1204 u_int
1205 window_pane_search(struct window_pane *wp, const char *term, int regex,
1206 int ignore)
1208 struct screen *s = &wp->base;
1209 regex_t r;
1210 char *new = NULL, *line;
1211 u_int i;
1212 int flags = 0, found;
1213 size_t n;
1215 if (!regex) {
1216 if (ignore)
1217 flags |= FNM_CASEFOLD;
1218 xasprintf(&new, "*%s*", term);
1219 } else {
1220 if (ignore)
1221 flags |= REG_ICASE;
1222 if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
1223 return (0);
1226 for (i = 0; i < screen_size_y(s); i++) {
1227 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1228 for (n = strlen(line); n > 0; n--) {
1229 if (!isspace((u_char)line[n - 1]))
1230 break;
1231 line[n - 1] = '\0';
1233 log_debug("%s: %s", __func__, line);
1234 if (!regex)
1235 found = (fnmatch(new, line, flags) == 0);
1236 else
1237 found = (regexec(&r, line, 0, NULL, 0) == 0);
1238 free(line);
1239 if (found)
1240 break;
1242 if (!regex)
1243 free(new);
1244 else
1245 regfree(&r);
1247 if (i == screen_size_y(s))
1248 return (0);
1249 return (i + 1);
1252 /* Get MRU pane from a list. */
1253 static struct window_pane *
1254 window_pane_choose_best(struct window_pane **list, u_int size)
1256 struct window_pane *next, *best;
1257 u_int i;
1259 if (size == 0)
1260 return (NULL);
1262 best = list[0];
1263 for (i = 1; i < size; i++) {
1264 next = list[i];
1265 if (next->active_point > best->active_point)
1266 best = next;
1268 return (best);
1272 * Find the pane directly above another. We build a list of those adjacent to
1273 * top edge and then choose the best.
1275 struct window_pane *
1276 window_pane_find_up(struct window_pane *wp)
1278 struct window *w;
1279 struct window_pane *next, *best, **list;
1280 u_int edge, left, right, end, size;
1281 int status, found;
1283 if (wp == NULL)
1284 return (NULL);
1285 w = wp->window;
1286 status = options_get_number(w->options, "pane-border-status");
1288 list = NULL;
1289 size = 0;
1291 edge = wp->yoff;
1292 if (status == PANE_STATUS_TOP) {
1293 if (edge == 1)
1294 edge = w->sy + 1;
1295 } else if (status == PANE_STATUS_BOTTOM) {
1296 if (edge == 0)
1297 edge = w->sy;
1298 } else {
1299 if (edge == 0)
1300 edge = w->sy + 1;
1303 left = wp->xoff;
1304 right = wp->xoff + wp->sx;
1306 TAILQ_FOREACH(next, &w->panes, entry) {
1307 if (next == wp)
1308 continue;
1309 if (next->yoff + next->sy + 1 != edge)
1310 continue;
1311 end = next->xoff + next->sx - 1;
1313 found = 0;
1314 if (next->xoff < left && end > right)
1315 found = 1;
1316 else if (next->xoff >= left && next->xoff <= right)
1317 found = 1;
1318 else if (end >= left && end <= right)
1319 found = 1;
1320 if (!found)
1321 continue;
1322 list = xreallocarray(list, size + 1, sizeof *list);
1323 list[size++] = next;
1326 best = window_pane_choose_best(list, size);
1327 free(list);
1328 return (best);
1331 /* Find the pane directly below another. */
1332 struct window_pane *
1333 window_pane_find_down(struct window_pane *wp)
1335 struct window *w;
1336 struct window_pane *next, *best, **list;
1337 u_int edge, left, right, end, size;
1338 int status, found;
1340 if (wp == NULL)
1341 return (NULL);
1342 w = wp->window;
1343 status = options_get_number(w->options, "pane-border-status");
1345 list = NULL;
1346 size = 0;
1348 edge = wp->yoff + wp->sy + 1;
1349 if (status == PANE_STATUS_TOP) {
1350 if (edge >= w->sy)
1351 edge = 1;
1352 } else if (status == PANE_STATUS_BOTTOM) {
1353 if (edge >= w->sy - 1)
1354 edge = 0;
1355 } else {
1356 if (edge >= w->sy)
1357 edge = 0;
1360 left = wp->xoff;
1361 right = wp->xoff + wp->sx;
1363 TAILQ_FOREACH(next, &w->panes, entry) {
1364 if (next == wp)
1365 continue;
1366 if (next->yoff != edge)
1367 continue;
1368 end = next->xoff + next->sx - 1;
1370 found = 0;
1371 if (next->xoff < left && end > right)
1372 found = 1;
1373 else if (next->xoff >= left && next->xoff <= right)
1374 found = 1;
1375 else if (end >= left && end <= right)
1376 found = 1;
1377 if (!found)
1378 continue;
1379 list = xreallocarray(list, size + 1, sizeof *list);
1380 list[size++] = next;
1383 best = window_pane_choose_best(list, size);
1384 free(list);
1385 return (best);
1388 /* Find the pane directly to the left of another. */
1389 struct window_pane *
1390 window_pane_find_left(struct window_pane *wp)
1392 struct window *w;
1393 struct window_pane *next, *best, **list;
1394 u_int edge, top, bottom, end, size;
1395 int found;
1397 if (wp == NULL)
1398 return (NULL);
1399 w = wp->window;
1401 list = NULL;
1402 size = 0;
1404 edge = wp->xoff;
1405 if (edge == 0)
1406 edge = w->sx + 1;
1408 top = wp->yoff;
1409 bottom = wp->yoff + wp->sy;
1411 TAILQ_FOREACH(next, &w->panes, entry) {
1412 if (next == wp)
1413 continue;
1414 if (next->xoff + next->sx + 1 != edge)
1415 continue;
1416 end = next->yoff + next->sy - 1;
1418 found = 0;
1419 if (next->yoff < top && end > bottom)
1420 found = 1;
1421 else if (next->yoff >= top && next->yoff <= bottom)
1422 found = 1;
1423 else if (end >= top && end <= bottom)
1424 found = 1;
1425 if (!found)
1426 continue;
1427 list = xreallocarray(list, size + 1, sizeof *list);
1428 list[size++] = next;
1431 best = window_pane_choose_best(list, size);
1432 free(list);
1433 return (best);
1436 /* Find the pane directly to the right of another. */
1437 struct window_pane *
1438 window_pane_find_right(struct window_pane *wp)
1440 struct window *w;
1441 struct window_pane *next, *best, **list;
1442 u_int edge, top, bottom, end, size;
1443 int found;
1445 if (wp == NULL)
1446 return (NULL);
1447 w = wp->window;
1449 list = NULL;
1450 size = 0;
1452 edge = wp->xoff + wp->sx + 1;
1453 if (edge >= w->sx)
1454 edge = 0;
1456 top = wp->yoff;
1457 bottom = wp->yoff + wp->sy;
1459 TAILQ_FOREACH(next, &w->panes, entry) {
1460 if (next == wp)
1461 continue;
1462 if (next->xoff != edge)
1463 continue;
1464 end = next->yoff + next->sy - 1;
1466 found = 0;
1467 if (next->yoff < top && end > bottom)
1468 found = 1;
1469 else if (next->yoff >= top && next->yoff <= bottom)
1470 found = 1;
1471 else if (end >= top && end <= bottom)
1472 found = 1;
1473 if (!found)
1474 continue;
1475 list = xreallocarray(list, size + 1, sizeof *list);
1476 list[size++] = next;
1479 best = window_pane_choose_best(list, size);
1480 free(list);
1481 return (best);
1484 /* Clear alert flags for a winlink */
1485 void
1486 winlink_clear_flags(struct winlink *wl)
1488 struct winlink *loop;
1490 wl->window->flags &= ~WINDOW_ALERTFLAGS;
1491 TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1492 if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1493 loop->flags &= ~WINLINK_ALERTFLAGS;
1494 server_status_session(loop->session);
1499 /* Shuffle window indexes up. */
1501 winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1503 int idx, last;
1505 if (wl == NULL)
1506 return (-1);
1507 if (before)
1508 idx = wl->idx;
1509 else
1510 idx = wl->idx + 1;
1512 /* Find the next free index. */
1513 for (last = idx; last < INT_MAX; last++) {
1514 if (winlink_find_by_index(&s->windows, last) == NULL)
1515 break;
1517 if (last == INT_MAX)
1518 return (-1);
1520 /* Move everything from last - 1 to idx up a bit. */
1521 for (; last > idx; last--) {
1522 wl = winlink_find_by_index(&s->windows, last - 1);
1523 RB_REMOVE(winlinks, &s->windows, wl);
1524 wl->idx++;
1525 RB_INSERT(winlinks, &s->windows, wl);
1528 return (idx);
1531 static void
1532 window_pane_input_callback(struct client *c, __unused const char *path,
1533 int error, int closed, struct evbuffer *buffer, void *data)
1535 struct window_pane_input_data *cdata = data;
1536 struct window_pane *wp;
1537 u_char *buf = EVBUFFER_DATA(buffer);
1538 size_t len = EVBUFFER_LENGTH(buffer);
1540 wp = window_pane_find_by_id(cdata->wp);
1541 if (wp == NULL || closed || error != 0 || (c->flags & CLIENT_DEAD)) {
1542 if (wp == NULL)
1543 c->flags |= CLIENT_EXIT;
1545 evbuffer_drain(buffer, len);
1546 cmdq_continue(cdata->item);
1548 server_client_unref(c);
1549 free(cdata);
1550 return;
1552 input_parse_buffer(wp, buf, len);
1553 evbuffer_drain(buffer, len);
1557 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1558 char **cause)
1560 struct client *c = cmdq_get_client(item);
1561 struct window_pane_input_data *cdata;
1563 if (~wp->flags & PANE_EMPTY) {
1564 *cause = xstrdup("pane is not empty");
1565 return (-1);
1567 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1568 return (1);
1569 if (c->session != NULL)
1570 return (1);
1572 cdata = xmalloc(sizeof *cdata);
1573 cdata->item = item;
1574 cdata->wp = wp->id;
1576 c->references++;
1577 file_read(c, "-", window_pane_input_callback, cdata);
1579 return (0);
1582 void *
1583 window_pane_get_new_data(struct window_pane *wp,
1584 struct window_pane_offset *wpo, size_t *size)
1586 size_t used = wpo->used - wp->base_offset;
1588 *size = EVBUFFER_LENGTH(wp->event->input) - used;
1589 return (EVBUFFER_DATA(wp->event->input) + used);
1592 void
1593 window_pane_update_used_data(struct window_pane *wp,
1594 struct window_pane_offset *wpo, size_t size)
1596 size_t used = wpo->used - wp->base_offset;
1598 if (size > EVBUFFER_LENGTH(wp->event->input) - used)
1599 size = EVBUFFER_LENGTH(wp->event->input) - used;
1600 wpo->used += size;