Add two new values for the destroy-unattached option to destroy sessions
[tmux-openbsd.git] / window.c
blobc5d00e29fef4b6006a1f199dc6e614fc1276fb52
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>
33 #include <util.h>
34 #include <vis.h>
36 #include "tmux.h"
39 * Each window is attached to a number of panes, each of which is a pty. This
40 * file contains code to handle them.
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.
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.
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.
57 /* Global window list. */
58 struct windows windows;
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;
66 struct window_pane_input_data {
67 struct cmdq_item *item;
68 u_int wp;
69 struct client_file *file;
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 *);
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);
80 int
81 window_cmp(struct window *w1, struct window *w2)
83 return (w1->id - w2->id);
86 int
87 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
89 return (wl1->idx - wl2->idx);
92 int
93 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
95 return (wp1->id - wp2->id);
98 struct winlink *
99 winlink_find_by_window(struct winlinks *wwl, struct window *w)
101 struct winlink *wl;
103 RB_FOREACH(wl, winlinks, wwl) {
104 if (wl->window == w)
105 return (wl);
108 return (NULL);
111 struct winlink *
112 winlink_find_by_index(struct winlinks *wwl, int idx)
114 struct winlink wl;
116 if (idx < 0)
117 fatalx("bad index");
119 wl.idx = idx;
120 return (RB_FIND(winlinks, wwl, &wl));
123 struct winlink *
124 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
126 struct winlink *wl;
128 RB_FOREACH(wl, winlinks, wwl) {
129 if (wl->window->id == id)
130 return (wl);
132 return (NULL);
135 static int
136 winlink_next_index(struct winlinks *wwl, int idx)
138 int i;
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);
152 u_int
153 winlink_count(struct winlinks *wwl)
155 struct winlink *wl;
156 u_int n;
158 n = 0;
159 RB_FOREACH(wl, winlinks, wwl)
160 n++;
162 return (n);
165 struct winlink *
166 winlink_add(struct winlinks *wwl, int idx)
168 struct winlink *wl;
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);
176 wl = xcalloc(1, sizeof *wl);
177 wl->idx = idx;
178 RB_INSERT(winlinks, wwl, wl);
180 return (wl);
183 void
184 winlink_set_window(struct winlink *wl, struct window *w)
186 if (wl->window != NULL) {
187 TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
188 window_remove_ref(wl->window, __func__);
190 TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
191 wl->window = w;
192 window_add_ref(w, __func__);
195 void
196 winlink_remove(struct winlinks *wwl, struct winlink *wl)
198 struct window *w = wl->window;
200 if (w != NULL) {
201 TAILQ_REMOVE(&w->winlinks, wl, wentry);
202 window_remove_ref(w, __func__);
205 RB_REMOVE(winlinks, wwl, wl);
206 free(wl);
209 struct winlink *
210 winlink_next(struct winlink *wl)
212 return (RB_NEXT(winlinks, wwl, wl));
215 struct winlink *
216 winlink_previous(struct winlink *wl)
218 return (RB_PREV(winlinks, wwl, wl));
221 struct winlink *
222 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
224 for (; n > 0; n--) {
225 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
226 wl = RB_MIN(winlinks, &s->windows);
229 return (wl);
232 struct winlink *
233 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
235 for (; n > 0; n--) {
236 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
237 wl = RB_MAX(winlinks, &s->windows);
240 return (wl);
243 void
244 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
246 if (wl == NULL)
247 return;
249 winlink_stack_remove(stack, wl);
250 TAILQ_INSERT_HEAD(stack, wl, sentry);
251 wl->flags |= WINLINK_VISITED;
254 void
255 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
257 if (wl != NULL && (wl->flags & WINLINK_VISITED)) {
258 TAILQ_REMOVE(stack, wl, sentry);
259 wl->flags &= ~WINLINK_VISITED;
263 struct window *
264 window_find_by_id_str(const char *s)
266 const char *errstr;
267 u_int id;
269 if (*s != '@')
270 return (NULL);
272 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
273 if (errstr != NULL)
274 return (NULL);
275 return (window_find_by_id(id));
278 struct window *
279 window_find_by_id(u_int id)
281 struct window w;
283 w.id = id;
284 return (RB_FIND(windows, &windows, &w));
287 void
288 window_update_activity(struct window *w)
290 gettimeofday(&w->activity_time, NULL);
291 alerts_queue(w, WINDOW_ACTIVITY);
294 struct window *
295 window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel)
297 struct window *w;
299 if (xpixel == 0)
300 xpixel = DEFAULT_XPIXEL;
301 if (ypixel == 0)
302 ypixel = DEFAULT_YPIXEL;
304 w = xcalloc(1, sizeof *w);
305 w->name = xstrdup("");
306 w->flags = 0;
308 TAILQ_INIT(&w->panes);
309 TAILQ_INIT(&w->last_panes);
310 w->active = NULL;
312 w->lastlayout = -1;
313 w->layout_root = NULL;
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;
322 w->options = options_create(global_w_options);
324 w->references = 0;
325 TAILQ_INIT(&w->winlinks);
327 w->id = next_window_id++;
328 RB_INSERT(windows, &windows, w);
330 window_set_fill_character(w);
331 window_update_activity(w);
333 log_debug("%s: @%u create %ux%u (%ux%u)", __func__, w->id, sx, sy,
334 w->xpixel, w->ypixel);
335 return (w);
338 static void
339 window_destroy(struct window *w)
341 log_debug("window @%u destroyed (%d references)", w->id, w->references);
343 window_unzoom(w);
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);
363 free(w->fill_character);
365 free(w->name);
366 free(w);
370 window_pane_destroy_ready(struct window_pane *wp)
372 int n;
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);
381 if (~wp->flags & PANE_EXITED)
382 return (0);
383 return (1);
386 void
387 window_add_ref(struct window *w, const char *from)
389 w->references++;
390 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
393 void
394 window_remove_ref(struct window *w, const char *from)
396 w->references--;
397 log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
399 if (w->references == 0)
400 window_destroy(w);
403 void
404 window_set_name(struct window *w, const char *new_name)
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);
411 void
412 window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
414 if (xpixel == 0)
415 xpixel = DEFAULT_XPIXEL;
416 if (ypixel == 0)
417 ypixel = DEFAULT_YPIXEL;
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;
430 void
431 window_pane_send_resize(struct window_pane *wp, u_int sx, u_int sy)
433 struct window *w = wp->window;
434 struct winsize ws;
436 if (wp->fd == -1)
437 return;
439 log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, sx, sy);
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");
451 window_has_pane(struct window *w, struct window_pane *wp)
453 struct window_pane *wp1;
455 TAILQ_FOREACH(wp1, &w->panes, entry) {
456 if (wp1 == wp)
457 return (1);
459 return (0);
462 void
463 window_update_focus(struct window *w)
465 if (w != NULL) {
466 log_debug("%s: @%u", __func__, w->id);
467 window_pane_update_focus(w->active);
471 void
472 window_pane_update_focus(struct window_pane *wp)
474 struct client *c;
475 int focused = 0;
477 if (wp != NULL) {
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;
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);
509 window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
511 struct window_pane *lastwp;
513 log_debug("%s: pane %%%u", __func__, wp->id);
515 if (wp == w->active)
516 return (0);
517 lastwp = w->active;
519 window_pane_stack_remove(&w->last_panes, wp);
520 window_pane_stack_push(&w->last_panes, lastwp);
522 w->active = wp;
523 w->active->active_point = next_active_point++;
524 w->active->flags |= PANE_CHANGED;
526 if (options_get_number(global_options, "focus-events")) {
527 window_pane_update_focus(lastwp);
528 window_pane_update_focus(w->active);
531 tty_update_window_offset(w);
533 if (notify)
534 notify_window("window-pane-changed", w);
535 return (1);
538 static int
539 window_pane_get_palette(struct window_pane *wp, int c)
541 if (wp == NULL)
542 return (-1);
543 return (colour_palette_get(&wp->palette, c));
546 void
547 window_redraw_active_switch(struct window *w, struct window_pane *wp)
549 struct grid_cell *gc1, *gc2;
550 int c1, c2;
552 if (wp == w->active)
553 return;
555 for (;;) {
557 * If the active and inactive styles or palettes are different,
558 * need to redraw the panes.
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;
576 if (wp == w->active)
577 break;
578 wp = w->active;
582 struct window_pane *
583 window_get_active_at(struct window *w, u_int x, u_int y)
585 struct window_pane *wp;
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);
596 return (NULL);
599 struct window_pane *
600 window_find_string(struct window *w, const char *s)
602 u_int x, y, top = 0, bottom = w->sy - 1;
603 int status;
605 x = w->sx / 2;
606 y = w->sy / 2;
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--;
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);
637 return (window_get_active_at(w, x, y));
641 window_zoom(struct window_pane *wp)
643 struct window *w = wp->window;
644 struct window_pane *wp1;
646 if (w->flags & WINDOW_ZOOMED)
647 return (-1);
649 if (window_count_panes(w) == 1)
650 return (-1);
652 if (w->active != wp)
653 window_set_active_pane(w, wp, 1);
655 TAILQ_FOREACH(wp1, &w->panes, entry) {
656 wp1->saved_layout_cell = wp1->layout_cell;
657 wp1->layout_cell = NULL;
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);
665 return (0);
669 window_unzoom(struct window *w)
671 struct window_pane *wp;
673 if (!(w->flags & WINDOW_ZOOMED))
674 return (-1);
676 w->flags &= ~WINDOW_ZOOMED;
677 layout_free(w);
678 w->layout_root = w->saved_layout_root;
679 w->saved_layout_root = NULL;
681 TAILQ_FOREACH(wp, &w->panes, entry) {
682 wp->layout_cell = wp->saved_layout_cell;
683 wp->saved_layout_cell = NULL;
685 layout_fix_panes(w, NULL);
686 notify_window("window-layout-changed", w);
688 return (0);
692 window_push_zoom(struct window *w, int always, int flag)
694 log_debug("%s: @%u %d", __func__, w->id,
695 flag && (w->flags & WINDOW_ZOOMED));
696 if (flag && (always || (w->flags & WINDOW_ZOOMED)))
697 w->flags |= WINDOW_WASZOOMED;
698 else
699 w->flags &= ~WINDOW_WASZOOMED;
700 return (window_unzoom(w) == 0);
704 window_pop_zoom(struct window *w)
706 log_debug("%s: @%u %d", __func__, w->id,
707 !!(w->flags & WINDOW_WASZOOMED));
708 if (w->flags & WINDOW_WASZOOMED)
709 return (window_zoom(w->active) == 0);
710 return (0);
713 struct window_pane *
714 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
715 int flags)
717 struct window_pane *wp;
719 if (other == NULL)
720 other = w->active;
722 wp = window_pane_create(w, w->sx, w->sy, hlimit);
723 if (TAILQ_EMPTY(&w->panes)) {
724 log_debug("%s: @%u at start", __func__, w->id);
725 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
726 } else if (flags & SPAWN_BEFORE) {
727 log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
728 if (flags & SPAWN_FULLSIZE)
729 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
730 else
731 TAILQ_INSERT_BEFORE(other, wp, entry);
732 } else {
733 log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
734 if (flags & SPAWN_FULLSIZE)
735 TAILQ_INSERT_TAIL(&w->panes, wp, entry);
736 else
737 TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
739 return (wp);
742 void
743 window_lost_pane(struct window *w, struct window_pane *wp)
745 log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
747 if (wp == marked_pane.wp)
748 server_clear_marked();
750 window_pane_stack_remove(&w->last_panes, wp);
751 if (wp == w->active) {
752 w->active = TAILQ_FIRST(&w->last_panes);
753 if (w->active == NULL) {
754 w->active = TAILQ_PREV(wp, window_panes, entry);
755 if (w->active == NULL)
756 w->active = TAILQ_NEXT(wp, entry);
758 if (w->active != NULL) {
759 window_pane_stack_remove(&w->last_panes, w->active);
760 w->active->flags |= PANE_CHANGED;
761 notify_window("window-pane-changed", w);
762 window_update_focus(w);
767 void
768 window_remove_pane(struct window *w, struct window_pane *wp)
770 window_lost_pane(w, wp);
772 TAILQ_REMOVE(&w->panes, wp, entry);
773 window_pane_destroy(wp);
776 struct window_pane *
777 window_pane_at_index(struct window *w, u_int idx)
779 struct window_pane *wp;
780 u_int n;
782 n = options_get_number(w->options, "pane-base-index");
783 TAILQ_FOREACH(wp, &w->panes, entry) {
784 if (n == idx)
785 return (wp);
786 n++;
788 return (NULL);
791 struct window_pane *
792 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
794 for (; n > 0; n--) {
795 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
796 wp = TAILQ_FIRST(&w->panes);
799 return (wp);
802 struct window_pane *
803 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
804 u_int n)
806 for (; n > 0; n--) {
807 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
808 wp = TAILQ_LAST(&w->panes, window_panes);
811 return (wp);
815 window_pane_index(struct window_pane *wp, u_int *i)
817 struct window_pane *wq;
818 struct window *w = wp->window;
820 *i = options_get_number(w->options, "pane-base-index");
821 TAILQ_FOREACH(wq, &w->panes, entry) {
822 if (wp == wq) {
823 return (0);
825 (*i)++;
828 return (-1);
831 u_int
832 window_count_panes(struct window *w)
834 struct window_pane *wp;
835 u_int n;
837 n = 0;
838 TAILQ_FOREACH(wp, &w->panes, entry)
839 n++;
840 return (n);
843 void
844 window_destroy_panes(struct window *w)
846 struct window_pane *wp;
848 while (!TAILQ_EMPTY(&w->last_panes)) {
849 wp = TAILQ_FIRST(&w->last_panes);
850 window_pane_stack_remove(&w->last_panes, wp);
853 while (!TAILQ_EMPTY(&w->panes)) {
854 wp = TAILQ_FIRST(&w->panes);
855 TAILQ_REMOVE(&w->panes, wp, entry);
856 window_pane_destroy(wp);
860 const char *
861 window_printable_flags(struct winlink *wl, int escape)
863 struct session *s = wl->session;
864 static char flags[32];
865 int pos;
867 pos = 0;
868 if (wl->flags & WINLINK_ACTIVITY) {
869 flags[pos++] = '#';
870 if (escape)
871 flags[pos++] = '#';
873 if (wl->flags & WINLINK_BELL)
874 flags[pos++] = '!';
875 if (wl->flags & WINLINK_SILENCE)
876 flags[pos++] = '~';
877 if (wl == s->curw)
878 flags[pos++] = '*';
879 if (wl == TAILQ_FIRST(&s->lastw))
880 flags[pos++] = '-';
881 if (server_check_marked() && wl == marked_pane.wl)
882 flags[pos++] = 'M';
883 if (wl->window->flags & WINDOW_ZOOMED)
884 flags[pos++] = 'Z';
885 flags[pos] = '\0';
886 return (flags);
889 struct window_pane *
890 window_pane_find_by_id_str(const char *s)
892 const char *errstr;
893 u_int id;
895 if (*s != '%')
896 return (NULL);
898 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
899 if (errstr != NULL)
900 return (NULL);
901 return (window_pane_find_by_id(id));
904 struct window_pane *
905 window_pane_find_by_id(u_int id)
907 struct window_pane wp;
909 wp.id = id;
910 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
913 static struct window_pane *
914 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
916 struct window_pane *wp;
917 char host[HOST_NAME_MAX + 1];
919 wp = xcalloc(1, sizeof *wp);
920 wp->window = w;
921 wp->options = options_create(w->options);
922 wp->flags = PANE_STYLECHANGED;
924 wp->id = next_window_pane_id++;
925 RB_INSERT(window_pane_tree, &all_window_panes, wp);
927 wp->fd = -1;
929 TAILQ_INIT(&wp->modes);
931 TAILQ_INIT (&wp->resize_queue);
933 wp->sx = sx;
934 wp->sy = sy;
936 wp->pipe_fd = -1;
938 colour_palette_init(&wp->palette);
939 colour_palette_from_option(&wp->palette, wp->options);
941 screen_init(&wp->base, sx, sy, hlimit);
942 wp->screen = &wp->base;
943 window_pane_default_cursor(wp);
945 screen_init(&wp->status_screen, 1, 1, 0);
947 if (gethostname(host, sizeof host) == 0)
948 screen_set_title(&wp->base, host);
950 return (wp);
953 static void
954 window_pane_destroy(struct window_pane *wp)
956 struct window_pane_resize *r;
957 struct window_pane_resize *r1;
959 window_pane_reset_mode_all(wp);
960 free(wp->searchstr);
962 if (wp->fd != -1) {
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 if (wp->event == NULL)
1044 fatalx("out of memory");
1045 wp->ictx = input_init(wp, wp->event, &wp->palette);
1047 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1050 void
1051 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1053 struct window_mode_entry *wme;
1054 struct window_pane_resize *r;
1056 if (sx == wp->sx && sy == wp->sy)
1057 return;
1059 r = xmalloc(sizeof *r);
1060 r->sx = sx;
1061 r->sy = sy;
1062 r->osx = wp->sx;
1063 r->osy = wp->sy;
1064 TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
1066 wp->sx = sx;
1067 wp->sy = sy;
1069 log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1070 screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1072 wme = TAILQ_FIRST(&wp->modes);
1073 if (wme != NULL && wme->mode->resize != NULL)
1074 wme->mode->resize(wme, sx, sy);
1078 window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
1079 const struct window_mode *mode, struct cmd_find_state *fs,
1080 struct args *args)
1082 struct window_mode_entry *wme;
1084 if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1085 return (1);
1087 TAILQ_FOREACH(wme, &wp->modes, entry) {
1088 if (wme->mode == mode)
1089 break;
1091 if (wme != NULL) {
1092 TAILQ_REMOVE(&wp->modes, wme, entry);
1093 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1094 } else {
1095 wme = xcalloc(1, sizeof *wme);
1096 wme->wp = wp;
1097 wme->swp = swp;
1098 wme->mode = mode;
1099 wme->prefix = 1;
1100 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1101 wme->screen = wme->mode->init(wme, fs, args);
1104 wp->screen = wme->screen;
1105 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1107 server_redraw_window_borders(wp->window);
1108 server_status_window(wp->window);
1109 notify_pane("pane-mode-changed", wp);
1111 return (0);
1114 void
1115 window_pane_reset_mode(struct window_pane *wp)
1117 struct window_mode_entry *wme, *next;
1119 if (TAILQ_EMPTY(&wp->modes))
1120 return;
1122 wme = TAILQ_FIRST(&wp->modes);
1123 TAILQ_REMOVE(&wp->modes, wme, entry);
1124 wme->mode->free(wme);
1125 free(wme);
1127 next = TAILQ_FIRST(&wp->modes);
1128 if (next == NULL) {
1129 wp->flags &= ~PANE_UNSEENCHANGES;
1130 log_debug("%s: no next mode", __func__);
1131 wp->screen = &wp->base;
1132 } else {
1133 log_debug("%s: next mode is %s", __func__, next->mode->name);
1134 wp->screen = next->screen;
1135 if (next->mode->resize != NULL)
1136 next->mode->resize(next, wp->sx, wp->sy);
1138 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1140 server_redraw_window_borders(wp->window);
1141 server_status_window(wp->window);
1142 notify_pane("pane-mode-changed", wp);
1145 void
1146 window_pane_reset_mode_all(struct window_pane *wp)
1148 while (!TAILQ_EMPTY(&wp->modes))
1149 window_pane_reset_mode(wp);
1152 static void
1153 window_pane_copy_key(struct window_pane *wp, key_code key)
1155 struct window_pane *loop;
1157 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1158 if (loop != wp &&
1159 TAILQ_EMPTY(&loop->modes) &&
1160 loop->fd != -1 &&
1161 (~loop->flags & PANE_INPUTOFF) &&
1162 window_pane_visible(loop) &&
1163 options_get_number(loop->options, "synchronize-panes"))
1164 input_key_pane(loop, key, NULL);
1169 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1170 struct winlink *wl, key_code key, struct mouse_event *m)
1172 struct window_mode_entry *wme;
1174 if (KEYC_IS_MOUSE(key) && m == NULL)
1175 return (-1);
1177 wme = TAILQ_FIRST(&wp->modes);
1178 if (wme != NULL) {
1179 if (wme->mode->key != NULL && c != NULL) {
1180 key &= ~KEYC_MASK_FLAGS;
1181 wme->mode->key(wme, c, s, wl, key, m);
1183 return (0);
1186 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1187 return (0);
1189 if (input_key_pane(wp, key, m) != 0)
1190 return (-1);
1192 if (KEYC_IS_MOUSE(key))
1193 return (0);
1194 if (options_get_number(wp->options, "synchronize-panes"))
1195 window_pane_copy_key(wp, key);
1196 return (0);
1200 window_pane_visible(struct window_pane *wp)
1202 if (~wp->window->flags & WINDOW_ZOOMED)
1203 return (1);
1204 return (wp == wp->window->active);
1208 window_pane_exited(struct window_pane *wp)
1210 return (wp->fd == -1 || (wp->flags & PANE_EXITED));
1213 u_int
1214 window_pane_search(struct window_pane *wp, const char *term, int regex,
1215 int ignore)
1217 struct screen *s = &wp->base;
1218 regex_t r;
1219 char *new = NULL, *line;
1220 u_int i;
1221 int flags = 0, found;
1222 size_t n;
1224 if (!regex) {
1225 if (ignore)
1226 flags |= FNM_CASEFOLD;
1227 xasprintf(&new, "*%s*", term);
1228 } else {
1229 if (ignore)
1230 flags |= REG_ICASE;
1231 if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
1232 return (0);
1235 for (i = 0; i < screen_size_y(s); i++) {
1236 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1237 for (n = strlen(line); n > 0; n--) {
1238 if (!isspace((u_char)line[n - 1]))
1239 break;
1240 line[n - 1] = '\0';
1242 log_debug("%s: %s", __func__, line);
1243 if (!regex)
1244 found = (fnmatch(new, line, flags) == 0);
1245 else
1246 found = (regexec(&r, line, 0, NULL, 0) == 0);
1247 free(line);
1248 if (found)
1249 break;
1251 if (!regex)
1252 free(new);
1253 else
1254 regfree(&r);
1256 if (i == screen_size_y(s))
1257 return (0);
1258 return (i + 1);
1261 /* Get MRU pane from a list. */
1262 static struct window_pane *
1263 window_pane_choose_best(struct window_pane **list, u_int size)
1265 struct window_pane *next, *best;
1266 u_int i;
1268 if (size == 0)
1269 return (NULL);
1271 best = list[0];
1272 for (i = 1; i < size; i++) {
1273 next = list[i];
1274 if (next->active_point > best->active_point)
1275 best = next;
1277 return (best);
1281 * Find the pane directly above another. We build a list of those adjacent to
1282 * top edge and then choose the best.
1284 struct window_pane *
1285 window_pane_find_up(struct window_pane *wp)
1287 struct window *w;
1288 struct window_pane *next, *best, **list;
1289 u_int edge, left, right, end, size;
1290 int status, found;
1292 if (wp == NULL)
1293 return (NULL);
1294 w = wp->window;
1295 status = options_get_number(w->options, "pane-border-status");
1297 list = NULL;
1298 size = 0;
1300 edge = wp->yoff;
1301 if (status == PANE_STATUS_TOP) {
1302 if (edge == 1)
1303 edge = w->sy + 1;
1304 } else if (status == PANE_STATUS_BOTTOM) {
1305 if (edge == 0)
1306 edge = w->sy;
1307 } else {
1308 if (edge == 0)
1309 edge = w->sy + 1;
1312 left = wp->xoff;
1313 right = wp->xoff + wp->sx;
1315 TAILQ_FOREACH(next, &w->panes, entry) {
1316 if (next == wp)
1317 continue;
1318 if (next->yoff + next->sy + 1 != edge)
1319 continue;
1320 end = next->xoff + next->sx - 1;
1322 found = 0;
1323 if (next->xoff < left && end > right)
1324 found = 1;
1325 else if (next->xoff >= left && next->xoff <= right)
1326 found = 1;
1327 else if (end >= left && end <= right)
1328 found = 1;
1329 if (!found)
1330 continue;
1331 list = xreallocarray(list, size + 1, sizeof *list);
1332 list[size++] = next;
1335 best = window_pane_choose_best(list, size);
1336 free(list);
1337 return (best);
1340 /* Find the pane directly below another. */
1341 struct window_pane *
1342 window_pane_find_down(struct window_pane *wp)
1344 struct window *w;
1345 struct window_pane *next, *best, **list;
1346 u_int edge, left, right, end, size;
1347 int status, found;
1349 if (wp == NULL)
1350 return (NULL);
1351 w = wp->window;
1352 status = options_get_number(w->options, "pane-border-status");
1354 list = NULL;
1355 size = 0;
1357 edge = wp->yoff + wp->sy + 1;
1358 if (status == PANE_STATUS_TOP) {
1359 if (edge >= w->sy)
1360 edge = 1;
1361 } else if (status == PANE_STATUS_BOTTOM) {
1362 if (edge >= w->sy - 1)
1363 edge = 0;
1364 } else {
1365 if (edge >= w->sy)
1366 edge = 0;
1369 left = wp->xoff;
1370 right = wp->xoff + wp->sx;
1372 TAILQ_FOREACH(next, &w->panes, entry) {
1373 if (next == wp)
1374 continue;
1375 if (next->yoff != edge)
1376 continue;
1377 end = next->xoff + next->sx - 1;
1379 found = 0;
1380 if (next->xoff < left && end > right)
1381 found = 1;
1382 else if (next->xoff >= left && next->xoff <= right)
1383 found = 1;
1384 else if (end >= left && end <= right)
1385 found = 1;
1386 if (!found)
1387 continue;
1388 list = xreallocarray(list, size + 1, sizeof *list);
1389 list[size++] = next;
1392 best = window_pane_choose_best(list, size);
1393 free(list);
1394 return (best);
1397 /* Find the pane directly to the left of another. */
1398 struct window_pane *
1399 window_pane_find_left(struct window_pane *wp)
1401 struct window *w;
1402 struct window_pane *next, *best, **list;
1403 u_int edge, top, bottom, end, size;
1404 int found;
1406 if (wp == NULL)
1407 return (NULL);
1408 w = wp->window;
1410 list = NULL;
1411 size = 0;
1413 edge = wp->xoff;
1414 if (edge == 0)
1415 edge = w->sx + 1;
1417 top = wp->yoff;
1418 bottom = wp->yoff + wp->sy;
1420 TAILQ_FOREACH(next, &w->panes, entry) {
1421 if (next == wp)
1422 continue;
1423 if (next->xoff + next->sx + 1 != edge)
1424 continue;
1425 end = next->yoff + next->sy - 1;
1427 found = 0;
1428 if (next->yoff < top && end > bottom)
1429 found = 1;
1430 else if (next->yoff >= top && next->yoff <= bottom)
1431 found = 1;
1432 else if (end >= top && end <= bottom)
1433 found = 1;
1434 if (!found)
1435 continue;
1436 list = xreallocarray(list, size + 1, sizeof *list);
1437 list[size++] = next;
1440 best = window_pane_choose_best(list, size);
1441 free(list);
1442 return (best);
1445 /* Find the pane directly to the right of another. */
1446 struct window_pane *
1447 window_pane_find_right(struct window_pane *wp)
1449 struct window *w;
1450 struct window_pane *next, *best, **list;
1451 u_int edge, top, bottom, end, size;
1452 int found;
1454 if (wp == NULL)
1455 return (NULL);
1456 w = wp->window;
1458 list = NULL;
1459 size = 0;
1461 edge = wp->xoff + wp->sx + 1;
1462 if (edge >= w->sx)
1463 edge = 0;
1465 top = wp->yoff;
1466 bottom = wp->yoff + wp->sy;
1468 TAILQ_FOREACH(next, &w->panes, entry) {
1469 if (next == wp)
1470 continue;
1471 if (next->xoff != edge)
1472 continue;
1473 end = next->yoff + next->sy - 1;
1475 found = 0;
1476 if (next->yoff < top && end > bottom)
1477 found = 1;
1478 else if (next->yoff >= top && next->yoff <= bottom)
1479 found = 1;
1480 else if (end >= top && end <= bottom)
1481 found = 1;
1482 if (!found)
1483 continue;
1484 list = xreallocarray(list, size + 1, sizeof *list);
1485 list[size++] = next;
1488 best = window_pane_choose_best(list, size);
1489 free(list);
1490 return (best);
1493 void
1494 window_pane_stack_push(struct window_panes *stack, struct window_pane *wp)
1496 if (wp != NULL) {
1497 window_pane_stack_remove(stack, wp);
1498 TAILQ_INSERT_HEAD(stack, wp, sentry);
1499 wp->flags |= PANE_VISITED;
1503 void
1504 window_pane_stack_remove(struct window_panes *stack, struct window_pane *wp)
1506 if (wp != NULL && (wp->flags & PANE_VISITED)) {
1507 TAILQ_REMOVE(stack, wp, sentry);
1508 wp->flags &= ~PANE_VISITED;
1512 /* Clear alert flags for a winlink */
1513 void
1514 winlink_clear_flags(struct winlink *wl)
1516 struct winlink *loop;
1518 wl->window->flags &= ~WINDOW_ALERTFLAGS;
1519 TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1520 if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1521 loop->flags &= ~WINLINK_ALERTFLAGS;
1522 server_status_session(loop->session);
1527 /* Shuffle window indexes up. */
1529 winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1531 int idx, last;
1533 if (wl == NULL)
1534 return (-1);
1535 if (before)
1536 idx = wl->idx;
1537 else
1538 idx = wl->idx + 1;
1540 /* Find the next free index. */
1541 for (last = idx; last < INT_MAX; last++) {
1542 if (winlink_find_by_index(&s->windows, last) == NULL)
1543 break;
1545 if (last == INT_MAX)
1546 return (-1);
1548 /* Move everything from last - 1 to idx up a bit. */
1549 for (; last > idx; last--) {
1550 wl = winlink_find_by_index(&s->windows, last - 1);
1551 RB_REMOVE(winlinks, &s->windows, wl);
1552 wl->idx++;
1553 RB_INSERT(winlinks, &s->windows, wl);
1556 return (idx);
1559 static void
1560 window_pane_input_callback(struct client *c, __unused const char *path,
1561 int error, int closed, struct evbuffer *buffer, void *data)
1563 struct window_pane_input_data *cdata = data;
1564 struct window_pane *wp;
1565 u_char *buf = EVBUFFER_DATA(buffer);
1566 size_t len = EVBUFFER_LENGTH(buffer);
1568 wp = window_pane_find_by_id(cdata->wp);
1569 if (cdata->file != NULL && (wp == NULL || c->flags & CLIENT_DEAD)) {
1570 if (wp == NULL) {
1571 c->retval = 1;
1572 c->flags |= CLIENT_EXIT;
1574 file_cancel(cdata->file);
1575 } else if (cdata->file == NULL || closed || error != 0) {
1576 cmdq_continue(cdata->item);
1577 server_client_unref(c);
1578 free(cdata);
1579 } else
1580 input_parse_buffer(wp, buf, len);
1581 evbuffer_drain(buffer, len);
1585 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1586 char **cause)
1588 struct client *c = cmdq_get_client(item);
1589 struct window_pane_input_data *cdata;
1591 if (~wp->flags & PANE_EMPTY) {
1592 *cause = xstrdup("pane is not empty");
1593 return (-1);
1595 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1596 return (1);
1597 if (c->session != NULL)
1598 return (1);
1600 cdata = xmalloc(sizeof *cdata);
1601 cdata->item = item;
1602 cdata->wp = wp->id;
1603 cdata->file = file_read(c, "-", window_pane_input_callback, cdata);
1604 c->references++;
1606 return (0);
1609 void *
1610 window_pane_get_new_data(struct window_pane *wp,
1611 struct window_pane_offset *wpo, size_t *size)
1613 size_t used = wpo->used - wp->base_offset;
1615 *size = EVBUFFER_LENGTH(wp->event->input) - used;
1616 return (EVBUFFER_DATA(wp->event->input) + used);
1619 void
1620 window_pane_update_used_data(struct window_pane *wp,
1621 struct window_pane_offset *wpo, size_t size)
1623 size_t used = wpo->used - wp->base_offset;
1625 if (size > EVBUFFER_LENGTH(wp->event->input) - used)
1626 size = EVBUFFER_LENGTH(wp->event->input) - used;
1627 wpo->used += size;
1630 void
1631 window_set_fill_character(struct window *w)
1633 const char *value;
1634 struct utf8_data *ud;
1636 free(w->fill_character);
1637 w->fill_character = NULL;
1639 value = options_get_string(w->options, "fill-character");
1640 if (*value != '\0' && utf8_isvalid(value)) {
1641 ud = utf8_fromcstr(value);
1642 if (ud != NULL && ud[0].width == 1)
1643 w->fill_character = ud;
1647 void
1648 window_pane_default_cursor(struct window_pane *wp)
1650 struct screen *s = wp->screen;
1651 int c;
1653 c = options_get_number(wp->options, "cursor-colour");
1654 s->default_ccolour = c;
1656 c = options_get_number(wp->options, "cursor-style");
1657 s->default_mode = 0;
1658 screen_set_cursor_style(c, &s->default_cstyle, &s->default_mode);