Remove Ns and Li and change Nm to Ic, suggested by jmc.
[tmux-openbsd.git] / window.c
blobc1365993c6b3657dc5a45a72d0dcf26a2faf2e95
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 RB_REMOVE(windows, &windows, w);
345 if (w->layout_root != NULL)
346 layout_free_cell(w->layout_root);
347 if (w->saved_layout_root != NULL)
348 layout_free_cell(w->saved_layout_root);
349 free(w->old_layout);
351 window_destroy_panes(w);
353 if (event_initialized(&w->name_event))
354 evtimer_del(&w->name_event);
356 if (event_initialized(&w->alerts_timer))
357 evtimer_del(&w->alerts_timer);
358 if (event_initialized(&w->offset_timer))
359 event_del(&w->offset_timer);
361 options_free(w->options);
362 free(w->fill_character);
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 fatal("ioctl failed");
450 window_has_pane(struct window *w, struct window_pane *wp)
452 struct window_pane *wp1;
454 TAILQ_FOREACH(wp1, &w->panes, entry) {
455 if (wp1 == wp)
456 return (1);
458 return (0);
461 void
462 window_update_focus(struct window *w)
464 if (w != NULL) {
465 log_debug("%s: @%u", __func__, w->id);
466 window_pane_update_focus(w->active);
470 void
471 window_pane_update_focus(struct window_pane *wp)
473 struct client *c;
474 int focused = 0;
476 if (wp != NULL) {
477 if (wp != wp->window->active)
478 focused = 0;
479 else {
480 TAILQ_FOREACH(c, &clients, entry) {
481 if (c->session != NULL &&
482 c->session->attached != 0 &&
483 (c->flags & CLIENT_FOCUSED) &&
484 c->session->curw->window == wp->window) {
485 focused = 1;
486 break;
490 if (!focused && (wp->flags & PANE_FOCUSED)) {
491 log_debug("%s: %%%u focus out", __func__, wp->id);
492 if (wp->base.mode & MODE_FOCUSON)
493 bufferevent_write(wp->event, "\033[O", 3);
494 notify_pane("pane-focus-out", wp);
495 wp->flags &= ~PANE_FOCUSED;
496 } else if (focused && (~wp->flags & PANE_FOCUSED)) {
497 log_debug("%s: %%%u focus in", __func__, wp->id);
498 if (wp->base.mode & MODE_FOCUSON)
499 bufferevent_write(wp->event, "\033[I", 3);
500 notify_pane("pane-focus-in", wp);
501 wp->flags |= PANE_FOCUSED;
502 } else
503 log_debug("%s: %%%u focus unchanged", __func__, wp->id);
508 window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
510 struct window_pane *lastwp;
512 log_debug("%s: pane %%%u", __func__, wp->id);
514 if (wp == w->active)
515 return (0);
516 lastwp = w->active;
518 window_pane_stack_remove(&w->last_panes, wp);
519 window_pane_stack_push(&w->last_panes, lastwp);
521 w->active = wp;
522 w->active->active_point = next_active_point++;
523 w->active->flags |= PANE_CHANGED;
525 if (options_get_number(global_options, "focus-events")) {
526 window_pane_update_focus(lastwp);
527 window_pane_update_focus(w->active);
530 tty_update_window_offset(w);
532 if (notify)
533 notify_window("window-pane-changed", w);
534 return (1);
537 static int
538 window_pane_get_palette(struct window_pane *wp, int c)
540 if (wp == NULL)
541 return (-1);
542 return (colour_palette_get(&wp->palette, c));
545 void
546 window_redraw_active_switch(struct window *w, struct window_pane *wp)
548 struct grid_cell *gc1, *gc2;
549 int c1, c2;
551 if (wp == w->active)
552 return;
554 for (;;) {
556 * If the active and inactive styles or palettes are different,
557 * need to redraw the panes.
559 gc1 = &wp->cached_gc;
560 gc2 = &wp->cached_active_gc;
561 if (!grid_cells_look_equal(gc1, gc2))
562 wp->flags |= PANE_REDRAW;
563 else {
564 c1 = window_pane_get_palette(wp, gc1->fg);
565 c2 = window_pane_get_palette(wp, gc2->fg);
566 if (c1 != c2)
567 wp->flags |= PANE_REDRAW;
568 else {
569 c1 = window_pane_get_palette(wp, gc1->bg);
570 c2 = window_pane_get_palette(wp, gc2->bg);
571 if (c1 != c2)
572 wp->flags |= PANE_REDRAW;
575 if (wp == w->active)
576 break;
577 wp = w->active;
581 struct window_pane *
582 window_get_active_at(struct window *w, u_int x, u_int y)
584 struct window_pane *wp;
586 TAILQ_FOREACH(wp, &w->panes, entry) {
587 if (!window_pane_visible(wp))
588 continue;
589 if (x < wp->xoff || x > wp->xoff + wp->sx)
590 continue;
591 if (y < wp->yoff || y > wp->yoff + wp->sy)
592 continue;
593 return (wp);
595 return (NULL);
598 struct window_pane *
599 window_find_string(struct window *w, const char *s)
601 u_int x, y, top = 0, bottom = w->sy - 1;
602 int status;
604 x = w->sx / 2;
605 y = w->sy / 2;
607 status = options_get_number(w->options, "pane-border-status");
608 if (status == PANE_STATUS_TOP)
609 top++;
610 else if (status == PANE_STATUS_BOTTOM)
611 bottom--;
613 if (strcasecmp(s, "top") == 0)
614 y = top;
615 else if (strcasecmp(s, "bottom") == 0)
616 y = bottom;
617 else if (strcasecmp(s, "left") == 0)
618 x = 0;
619 else if (strcasecmp(s, "right") == 0)
620 x = w->sx - 1;
621 else if (strcasecmp(s, "top-left") == 0) {
622 x = 0;
623 y = top;
624 } else if (strcasecmp(s, "top-right") == 0) {
625 x = w->sx - 1;
626 y = top;
627 } else if (strcasecmp(s, "bottom-left") == 0) {
628 x = 0;
629 y = bottom;
630 } else if (strcasecmp(s, "bottom-right") == 0) {
631 x = w->sx - 1;
632 y = bottom;
633 } else
634 return (NULL);
636 return (window_get_active_at(w, x, y));
640 window_zoom(struct window_pane *wp)
642 struct window *w = wp->window;
643 struct window_pane *wp1;
645 if (w->flags & WINDOW_ZOOMED)
646 return (-1);
648 if (window_count_panes(w) == 1)
649 return (-1);
651 if (w->active != wp)
652 window_set_active_pane(w, wp, 1);
654 TAILQ_FOREACH(wp1, &w->panes, entry) {
655 wp1->saved_layout_cell = wp1->layout_cell;
656 wp1->layout_cell = NULL;
659 w->saved_layout_root = w->layout_root;
660 layout_init(w, wp);
661 w->flags |= WINDOW_ZOOMED;
662 notify_window("window-layout-changed", w);
664 return (0);
668 window_unzoom(struct window *w)
670 struct window_pane *wp;
672 if (!(w->flags & WINDOW_ZOOMED))
673 return (-1);
675 w->flags &= ~WINDOW_ZOOMED;
676 layout_free(w);
677 w->layout_root = w->saved_layout_root;
678 w->saved_layout_root = NULL;
680 TAILQ_FOREACH(wp, &w->panes, entry) {
681 wp->layout_cell = wp->saved_layout_cell;
682 wp->saved_layout_cell = NULL;
684 layout_fix_panes(w, NULL);
685 notify_window("window-layout-changed", w);
687 return (0);
691 window_push_zoom(struct window *w, int always, int flag)
693 log_debug("%s: @%u %d", __func__, w->id,
694 flag && (w->flags & WINDOW_ZOOMED));
695 if (flag && (always || (w->flags & WINDOW_ZOOMED)))
696 w->flags |= WINDOW_WASZOOMED;
697 else
698 w->flags &= ~WINDOW_WASZOOMED;
699 return (window_unzoom(w) == 0);
703 window_pop_zoom(struct window *w)
705 log_debug("%s: @%u %d", __func__, w->id,
706 !!(w->flags & WINDOW_WASZOOMED));
707 if (w->flags & WINDOW_WASZOOMED)
708 return (window_zoom(w->active) == 0);
709 return (0);
712 struct window_pane *
713 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
714 int flags)
716 struct window_pane *wp;
718 if (other == NULL)
719 other = w->active;
721 wp = window_pane_create(w, w->sx, w->sy, hlimit);
722 if (TAILQ_EMPTY(&w->panes)) {
723 log_debug("%s: @%u at start", __func__, w->id);
724 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
725 } else if (flags & SPAWN_BEFORE) {
726 log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
727 if (flags & SPAWN_FULLSIZE)
728 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
729 else
730 TAILQ_INSERT_BEFORE(other, wp, entry);
731 } else {
732 log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
733 if (flags & SPAWN_FULLSIZE)
734 TAILQ_INSERT_TAIL(&w->panes, wp, entry);
735 else
736 TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
738 return (wp);
741 void
742 window_lost_pane(struct window *w, struct window_pane *wp)
744 log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
746 if (wp == marked_pane.wp)
747 server_clear_marked();
749 window_pane_stack_remove(&w->last_panes, wp);
750 if (wp == w->active) {
751 w->active = TAILQ_FIRST(&w->last_panes);
752 if (w->active == NULL) {
753 w->active = TAILQ_PREV(wp, window_panes, entry);
754 if (w->active == NULL)
755 w->active = TAILQ_NEXT(wp, entry);
757 if (w->active != NULL) {
758 window_pane_stack_remove(&w->last_panes, w->active);
759 w->active->flags |= PANE_CHANGED;
760 notify_window("window-pane-changed", w);
761 window_update_focus(w);
766 void
767 window_remove_pane(struct window *w, struct window_pane *wp)
769 window_lost_pane(w, wp);
771 TAILQ_REMOVE(&w->panes, wp, entry);
772 window_pane_destroy(wp);
775 struct window_pane *
776 window_pane_at_index(struct window *w, u_int idx)
778 struct window_pane *wp;
779 u_int n;
781 n = options_get_number(w->options, "pane-base-index");
782 TAILQ_FOREACH(wp, &w->panes, entry) {
783 if (n == idx)
784 return (wp);
785 n++;
787 return (NULL);
790 struct window_pane *
791 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
793 for (; n > 0; n--) {
794 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
795 wp = TAILQ_FIRST(&w->panes);
798 return (wp);
801 struct window_pane *
802 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
803 u_int n)
805 for (; n > 0; n--) {
806 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
807 wp = TAILQ_LAST(&w->panes, window_panes);
810 return (wp);
814 window_pane_index(struct window_pane *wp, u_int *i)
816 struct window_pane *wq;
817 struct window *w = wp->window;
819 *i = options_get_number(w->options, "pane-base-index");
820 TAILQ_FOREACH(wq, &w->panes, entry) {
821 if (wp == wq) {
822 return (0);
824 (*i)++;
827 return (-1);
830 u_int
831 window_count_panes(struct window *w)
833 struct window_pane *wp;
834 u_int n;
836 n = 0;
837 TAILQ_FOREACH(wp, &w->panes, entry)
838 n++;
839 return (n);
842 void
843 window_destroy_panes(struct window *w)
845 struct window_pane *wp;
847 while (!TAILQ_EMPTY(&w->last_panes)) {
848 wp = TAILQ_FIRST(&w->last_panes);
849 window_pane_stack_remove(&w->last_panes, wp);
852 while (!TAILQ_EMPTY(&w->panes)) {
853 wp = TAILQ_FIRST(&w->panes);
854 TAILQ_REMOVE(&w->panes, wp, entry);
855 window_pane_destroy(wp);
859 const char *
860 window_printable_flags(struct winlink *wl, int escape)
862 struct session *s = wl->session;
863 static char flags[32];
864 int pos;
866 pos = 0;
867 if (wl->flags & WINLINK_ACTIVITY) {
868 flags[pos++] = '#';
869 if (escape)
870 flags[pos++] = '#';
872 if (wl->flags & WINLINK_BELL)
873 flags[pos++] = '!';
874 if (wl->flags & WINLINK_SILENCE)
875 flags[pos++] = '~';
876 if (wl == s->curw)
877 flags[pos++] = '*';
878 if (wl == TAILQ_FIRST(&s->lastw))
879 flags[pos++] = '-';
880 if (server_check_marked() && wl == marked_pane.wl)
881 flags[pos++] = 'M';
882 if (wl->window->flags & WINDOW_ZOOMED)
883 flags[pos++] = 'Z';
884 flags[pos] = '\0';
885 return (flags);
888 struct window_pane *
889 window_pane_find_by_id_str(const char *s)
891 const char *errstr;
892 u_int id;
894 if (*s != '%')
895 return (NULL);
897 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
898 if (errstr != NULL)
899 return (NULL);
900 return (window_pane_find_by_id(id));
903 struct window_pane *
904 window_pane_find_by_id(u_int id)
906 struct window_pane wp;
908 wp.id = id;
909 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
912 static struct window_pane *
913 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
915 struct window_pane *wp;
916 char host[HOST_NAME_MAX + 1];
918 wp = xcalloc(1, sizeof *wp);
919 wp->window = w;
920 wp->options = options_create(w->options);
921 wp->flags = PANE_STYLECHANGED;
923 wp->id = next_window_pane_id++;
924 RB_INSERT(window_pane_tree, &all_window_panes, wp);
926 wp->fd = -1;
928 TAILQ_INIT(&wp->modes);
930 TAILQ_INIT (&wp->resize_queue);
932 wp->sx = sx;
933 wp->sy = sy;
935 wp->pipe_fd = -1;
937 colour_palette_init(&wp->palette);
938 colour_palette_from_option(&wp->palette, wp->options);
940 screen_init(&wp->base, sx, sy, hlimit);
941 wp->screen = &wp->base;
942 window_pane_default_cursor(wp);
944 screen_init(&wp->status_screen, 1, 1, 0);
946 if (gethostname(host, sizeof host) == 0)
947 screen_set_title(&wp->base, host);
949 return (wp);
952 static void
953 window_pane_destroy(struct window_pane *wp)
955 struct window_pane_resize *r;
956 struct window_pane_resize *r1;
958 window_pane_reset_mode_all(wp);
959 free(wp->searchstr);
961 if (wp->fd != -1) {
962 bufferevent_free(wp->event);
963 close(wp->fd);
965 if (wp->ictx != NULL)
966 input_free(wp->ictx);
968 screen_free(&wp->status_screen);
970 screen_free(&wp->base);
972 if (wp->pipe_fd != -1) {
973 bufferevent_free(wp->pipe_event);
974 close(wp->pipe_fd);
977 if (event_initialized(&wp->resize_timer))
978 event_del(&wp->resize_timer);
979 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
980 TAILQ_REMOVE(&wp->resize_queue, r, entry);
981 free(r);
984 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
986 options_free(wp->options);
987 free((void *)wp->cwd);
988 free(wp->shell);
989 cmd_free_argv(wp->argc, wp->argv);
990 colour_palette_free(&wp->palette);
991 free(wp);
994 static void
995 window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
997 struct window_pane *wp = data;
998 struct evbuffer *evb = wp->event->input;
999 struct window_pane_offset *wpo = &wp->pipe_offset;
1000 size_t size = EVBUFFER_LENGTH(evb);
1001 char *new_data;
1002 size_t new_size;
1003 struct client *c;
1005 if (wp->pipe_fd != -1) {
1006 new_data = window_pane_get_new_data(wp, wpo, &new_size);
1007 if (new_size > 0) {
1008 bufferevent_write(wp->pipe_event, new_data, new_size);
1009 window_pane_update_used_data(wp, wpo, new_size);
1013 log_debug("%%%u has %zu bytes", wp->id, size);
1014 TAILQ_FOREACH(c, &clients, entry) {
1015 if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1016 control_write_output(c, wp);
1018 input_parse_pane(wp);
1019 bufferevent_disable(wp->event, EV_READ);
1022 static void
1023 window_pane_error_callback(__unused struct bufferevent *bufev,
1024 __unused short what, void *data)
1026 struct window_pane *wp = data;
1028 log_debug("%%%u error", wp->id);
1029 wp->flags |= PANE_EXITED;
1031 if (window_pane_destroy_ready(wp))
1032 server_destroy_pane(wp, 1);
1035 void
1036 window_pane_set_event(struct window_pane *wp)
1038 setblocking(wp->fd, 0);
1040 wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
1041 NULL, window_pane_error_callback, wp);
1042 if (wp->event == NULL)
1043 fatalx("out of memory");
1044 wp->ictx = input_init(wp, wp->event, &wp->palette);
1046 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1049 void
1050 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1052 struct window_mode_entry *wme;
1053 struct window_pane_resize *r;
1055 if (sx == wp->sx && sy == wp->sy)
1056 return;
1058 r = xmalloc(sizeof *r);
1059 r->sx = sx;
1060 r->sy = sy;
1061 r->osx = wp->sx;
1062 r->osy = wp->sy;
1063 TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
1065 wp->sx = sx;
1066 wp->sy = sy;
1068 log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1069 screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1071 wme = TAILQ_FIRST(&wp->modes);
1072 if (wme != NULL && wme->mode->resize != NULL)
1073 wme->mode->resize(wme, sx, sy);
1077 window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
1078 const struct window_mode *mode, struct cmd_find_state *fs,
1079 struct args *args)
1081 struct window_mode_entry *wme;
1083 if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1084 return (1);
1086 TAILQ_FOREACH(wme, &wp->modes, entry) {
1087 if (wme->mode == mode)
1088 break;
1090 if (wme != NULL) {
1091 TAILQ_REMOVE(&wp->modes, wme, entry);
1092 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1093 } else {
1094 wme = xcalloc(1, sizeof *wme);
1095 wme->wp = wp;
1096 wme->swp = swp;
1097 wme->mode = mode;
1098 wme->prefix = 1;
1099 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1100 wme->screen = wme->mode->init(wme, fs, args);
1103 wp->screen = wme->screen;
1104 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1106 server_redraw_window_borders(wp->window);
1107 server_status_window(wp->window);
1108 notify_pane("pane-mode-changed", wp);
1110 return (0);
1113 void
1114 window_pane_reset_mode(struct window_pane *wp)
1116 struct window_mode_entry *wme, *next;
1118 if (TAILQ_EMPTY(&wp->modes))
1119 return;
1121 wme = TAILQ_FIRST(&wp->modes);
1122 TAILQ_REMOVE(&wp->modes, wme, entry);
1123 wme->mode->free(wme);
1124 free(wme);
1126 next = TAILQ_FIRST(&wp->modes);
1127 if (next == NULL) {
1128 wp->flags &= ~PANE_UNSEENCHANGES;
1129 log_debug("%s: no next mode", __func__);
1130 wp->screen = &wp->base;
1131 } else {
1132 log_debug("%s: next mode is %s", __func__, next->mode->name);
1133 wp->screen = next->screen;
1134 if (next->mode->resize != NULL)
1135 next->mode->resize(next, wp->sx, wp->sy);
1137 wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1139 server_redraw_window_borders(wp->window);
1140 server_status_window(wp->window);
1141 notify_pane("pane-mode-changed", wp);
1144 void
1145 window_pane_reset_mode_all(struct window_pane *wp)
1147 while (!TAILQ_EMPTY(&wp->modes))
1148 window_pane_reset_mode(wp);
1151 static void
1152 window_pane_copy_key(struct window_pane *wp, key_code key)
1154 struct window_pane *loop;
1156 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1157 if (loop != wp &&
1158 TAILQ_EMPTY(&loop->modes) &&
1159 loop->fd != -1 &&
1160 (~loop->flags & PANE_INPUTOFF) &&
1161 window_pane_visible(loop) &&
1162 options_get_number(loop->options, "synchronize-panes"))
1163 input_key_pane(loop, key, NULL);
1168 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1169 struct winlink *wl, key_code key, struct mouse_event *m)
1171 struct window_mode_entry *wme;
1173 if (KEYC_IS_MOUSE(key) && m == NULL)
1174 return (-1);
1176 wme = TAILQ_FIRST(&wp->modes);
1177 if (wme != NULL) {
1178 if (wme->mode->key != NULL && c != NULL) {
1179 key &= ~KEYC_MASK_FLAGS;
1180 wme->mode->key(wme, c, s, wl, key, m);
1182 return (0);
1185 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1186 return (0);
1188 if (input_key_pane(wp, key, m) != 0)
1189 return (-1);
1191 if (KEYC_IS_MOUSE(key))
1192 return (0);
1193 if (options_get_number(wp->options, "synchronize-panes"))
1194 window_pane_copy_key(wp, key);
1195 return (0);
1199 window_pane_visible(struct window_pane *wp)
1201 if (~wp->window->flags & WINDOW_ZOOMED)
1202 return (1);
1203 return (wp == wp->window->active);
1206 u_int
1207 window_pane_search(struct window_pane *wp, const char *term, int regex,
1208 int ignore)
1210 struct screen *s = &wp->base;
1211 regex_t r;
1212 char *new = NULL, *line;
1213 u_int i;
1214 int flags = 0, found;
1215 size_t n;
1217 if (!regex) {
1218 if (ignore)
1219 flags |= FNM_CASEFOLD;
1220 xasprintf(&new, "*%s*", term);
1221 } else {
1222 if (ignore)
1223 flags |= REG_ICASE;
1224 if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
1225 return (0);
1228 for (i = 0; i < screen_size_y(s); i++) {
1229 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1230 for (n = strlen(line); n > 0; n--) {
1231 if (!isspace((u_char)line[n - 1]))
1232 break;
1233 line[n - 1] = '\0';
1235 log_debug("%s: %s", __func__, line);
1236 if (!regex)
1237 found = (fnmatch(new, line, flags) == 0);
1238 else
1239 found = (regexec(&r, line, 0, NULL, 0) == 0);
1240 free(line);
1241 if (found)
1242 break;
1244 if (!regex)
1245 free(new);
1246 else
1247 regfree(&r);
1249 if (i == screen_size_y(s))
1250 return (0);
1251 return (i + 1);
1254 /* Get MRU pane from a list. */
1255 static struct window_pane *
1256 window_pane_choose_best(struct window_pane **list, u_int size)
1258 struct window_pane *next, *best;
1259 u_int i;
1261 if (size == 0)
1262 return (NULL);
1264 best = list[0];
1265 for (i = 1; i < size; i++) {
1266 next = list[i];
1267 if (next->active_point > best->active_point)
1268 best = next;
1270 return (best);
1274 * Find the pane directly above another. We build a list of those adjacent to
1275 * top edge and then choose the best.
1277 struct window_pane *
1278 window_pane_find_up(struct window_pane *wp)
1280 struct window *w;
1281 struct window_pane *next, *best, **list;
1282 u_int edge, left, right, end, size;
1283 int status, found;
1285 if (wp == NULL)
1286 return (NULL);
1287 w = wp->window;
1288 status = options_get_number(w->options, "pane-border-status");
1290 list = NULL;
1291 size = 0;
1293 edge = wp->yoff;
1294 if (status == PANE_STATUS_TOP) {
1295 if (edge == 1)
1296 edge = w->sy + 1;
1297 } else if (status == PANE_STATUS_BOTTOM) {
1298 if (edge == 0)
1299 edge = w->sy;
1300 } else {
1301 if (edge == 0)
1302 edge = w->sy + 1;
1305 left = wp->xoff;
1306 right = wp->xoff + wp->sx;
1308 TAILQ_FOREACH(next, &w->panes, entry) {
1309 if (next == wp)
1310 continue;
1311 if (next->yoff + next->sy + 1 != edge)
1312 continue;
1313 end = next->xoff + next->sx - 1;
1315 found = 0;
1316 if (next->xoff < left && end > right)
1317 found = 1;
1318 else if (next->xoff >= left && next->xoff <= right)
1319 found = 1;
1320 else if (end >= left && end <= right)
1321 found = 1;
1322 if (!found)
1323 continue;
1324 list = xreallocarray(list, size + 1, sizeof *list);
1325 list[size++] = next;
1328 best = window_pane_choose_best(list, size);
1329 free(list);
1330 return (best);
1333 /* Find the pane directly below another. */
1334 struct window_pane *
1335 window_pane_find_down(struct window_pane *wp)
1337 struct window *w;
1338 struct window_pane *next, *best, **list;
1339 u_int edge, left, right, end, size;
1340 int status, found;
1342 if (wp == NULL)
1343 return (NULL);
1344 w = wp->window;
1345 status = options_get_number(w->options, "pane-border-status");
1347 list = NULL;
1348 size = 0;
1350 edge = wp->yoff + wp->sy + 1;
1351 if (status == PANE_STATUS_TOP) {
1352 if (edge >= w->sy)
1353 edge = 1;
1354 } else if (status == PANE_STATUS_BOTTOM) {
1355 if (edge >= w->sy - 1)
1356 edge = 0;
1357 } else {
1358 if (edge >= w->sy)
1359 edge = 0;
1362 left = wp->xoff;
1363 right = wp->xoff + wp->sx;
1365 TAILQ_FOREACH(next, &w->panes, entry) {
1366 if (next == wp)
1367 continue;
1368 if (next->yoff != edge)
1369 continue;
1370 end = next->xoff + next->sx - 1;
1372 found = 0;
1373 if (next->xoff < left && end > right)
1374 found = 1;
1375 else if (next->xoff >= left && next->xoff <= right)
1376 found = 1;
1377 else if (end >= left && end <= right)
1378 found = 1;
1379 if (!found)
1380 continue;
1381 list = xreallocarray(list, size + 1, sizeof *list);
1382 list[size++] = next;
1385 best = window_pane_choose_best(list, size);
1386 free(list);
1387 return (best);
1390 /* Find the pane directly to the left of another. */
1391 struct window_pane *
1392 window_pane_find_left(struct window_pane *wp)
1394 struct window *w;
1395 struct window_pane *next, *best, **list;
1396 u_int edge, top, bottom, end, size;
1397 int found;
1399 if (wp == NULL)
1400 return (NULL);
1401 w = wp->window;
1403 list = NULL;
1404 size = 0;
1406 edge = wp->xoff;
1407 if (edge == 0)
1408 edge = w->sx + 1;
1410 top = wp->yoff;
1411 bottom = wp->yoff + wp->sy;
1413 TAILQ_FOREACH(next, &w->panes, entry) {
1414 if (next == wp)
1415 continue;
1416 if (next->xoff + next->sx + 1 != edge)
1417 continue;
1418 end = next->yoff + next->sy - 1;
1420 found = 0;
1421 if (next->yoff < top && end > bottom)
1422 found = 1;
1423 else if (next->yoff >= top && next->yoff <= bottom)
1424 found = 1;
1425 else if (end >= top && end <= bottom)
1426 found = 1;
1427 if (!found)
1428 continue;
1429 list = xreallocarray(list, size + 1, sizeof *list);
1430 list[size++] = next;
1433 best = window_pane_choose_best(list, size);
1434 free(list);
1435 return (best);
1438 /* Find the pane directly to the right of another. */
1439 struct window_pane *
1440 window_pane_find_right(struct window_pane *wp)
1442 struct window *w;
1443 struct window_pane *next, *best, **list;
1444 u_int edge, top, bottom, end, size;
1445 int found;
1447 if (wp == NULL)
1448 return (NULL);
1449 w = wp->window;
1451 list = NULL;
1452 size = 0;
1454 edge = wp->xoff + wp->sx + 1;
1455 if (edge >= w->sx)
1456 edge = 0;
1458 top = wp->yoff;
1459 bottom = wp->yoff + wp->sy;
1461 TAILQ_FOREACH(next, &w->panes, entry) {
1462 if (next == wp)
1463 continue;
1464 if (next->xoff != edge)
1465 continue;
1466 end = next->yoff + next->sy - 1;
1468 found = 0;
1469 if (next->yoff < top && end > bottom)
1470 found = 1;
1471 else if (next->yoff >= top && next->yoff <= bottom)
1472 found = 1;
1473 else if (end >= top && end <= bottom)
1474 found = 1;
1475 if (!found)
1476 continue;
1477 list = xreallocarray(list, size + 1, sizeof *list);
1478 list[size++] = next;
1481 best = window_pane_choose_best(list, size);
1482 free(list);
1483 return (best);
1486 void
1487 window_pane_stack_push(struct window_panes *stack, struct window_pane *wp)
1489 if (wp != NULL) {
1490 window_pane_stack_remove(stack, wp);
1491 TAILQ_INSERT_HEAD(stack, wp, sentry);
1492 wp->flags |= PANE_VISITED;
1496 void
1497 window_pane_stack_remove(struct window_panes *stack, struct window_pane *wp)
1499 if (wp != NULL && (wp->flags & PANE_VISITED)) {
1500 TAILQ_REMOVE(stack, wp, sentry);
1501 wp->flags &= ~PANE_VISITED;
1505 /* Clear alert flags for a winlink */
1506 void
1507 winlink_clear_flags(struct winlink *wl)
1509 struct winlink *loop;
1511 wl->window->flags &= ~WINDOW_ALERTFLAGS;
1512 TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1513 if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1514 loop->flags &= ~WINLINK_ALERTFLAGS;
1515 server_status_session(loop->session);
1520 /* Shuffle window indexes up. */
1522 winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1524 int idx, last;
1526 if (wl == NULL)
1527 return (-1);
1528 if (before)
1529 idx = wl->idx;
1530 else
1531 idx = wl->idx + 1;
1533 /* Find the next free index. */
1534 for (last = idx; last < INT_MAX; last++) {
1535 if (winlink_find_by_index(&s->windows, last) == NULL)
1536 break;
1538 if (last == INT_MAX)
1539 return (-1);
1541 /* Move everything from last - 1 to idx up a bit. */
1542 for (; last > idx; last--) {
1543 wl = winlink_find_by_index(&s->windows, last - 1);
1544 RB_REMOVE(winlinks, &s->windows, wl);
1545 wl->idx++;
1546 RB_INSERT(winlinks, &s->windows, wl);
1549 return (idx);
1552 static void
1553 window_pane_input_callback(struct client *c, __unused const char *path,
1554 int error, int closed, struct evbuffer *buffer, void *data)
1556 struct window_pane_input_data *cdata = data;
1557 struct window_pane *wp;
1558 u_char *buf = EVBUFFER_DATA(buffer);
1559 size_t len = EVBUFFER_LENGTH(buffer);
1561 wp = window_pane_find_by_id(cdata->wp);
1562 if (cdata->file != NULL && (wp == NULL || c->flags & CLIENT_DEAD)) {
1563 if (wp == NULL) {
1564 c->retval = 1;
1565 c->flags |= CLIENT_EXIT;
1567 file_cancel(cdata->file);
1568 } else if (cdata->file == NULL || closed || error != 0) {
1569 cmdq_continue(cdata->item);
1570 server_client_unref(c);
1571 free(cdata);
1572 } else
1573 input_parse_buffer(wp, buf, len);
1574 evbuffer_drain(buffer, len);
1578 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1579 char **cause)
1581 struct client *c = cmdq_get_client(item);
1582 struct window_pane_input_data *cdata;
1584 if (~wp->flags & PANE_EMPTY) {
1585 *cause = xstrdup("pane is not empty");
1586 return (-1);
1588 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1589 return (1);
1590 if (c->session != NULL)
1591 return (1);
1593 cdata = xmalloc(sizeof *cdata);
1594 cdata->item = item;
1595 cdata->wp = wp->id;
1596 cdata->file = file_read(c, "-", window_pane_input_callback, cdata);
1597 c->references++;
1599 return (0);
1602 void *
1603 window_pane_get_new_data(struct window_pane *wp,
1604 struct window_pane_offset *wpo, size_t *size)
1606 size_t used = wpo->used - wp->base_offset;
1608 *size = EVBUFFER_LENGTH(wp->event->input) - used;
1609 return (EVBUFFER_DATA(wp->event->input) + used);
1612 void
1613 window_pane_update_used_data(struct window_pane *wp,
1614 struct window_pane_offset *wpo, size_t size)
1616 size_t used = wpo->used - wp->base_offset;
1618 if (size > EVBUFFER_LENGTH(wp->event->input) - used)
1619 size = EVBUFFER_LENGTH(wp->event->input) - used;
1620 wpo->used += size;
1623 void
1624 window_set_fill_character(struct window *w)
1626 const char *value;
1627 struct utf8_data *ud;
1629 free(w->fill_character);
1630 w->fill_character = NULL;
1632 value = options_get_string(w->options, "fill-character");
1633 if (*value != '\0' && utf8_isvalid(value)) {
1634 ud = utf8_fromcstr(value);
1635 if (ud != NULL && ud[0].width == 1)
1636 w->fill_character = ud;
1640 void
1641 window_pane_default_cursor(struct window_pane *wp)
1643 struct screen *s = wp->screen;
1644 int c;
1646 c = options_get_number(wp->options, "cursor-colour");
1647 s->default_ccolour = c;
1649 c = options_get_number(wp->options, "cursor-style");
1650 s->default_mode = 0;
1651 screen_set_cursor_style(c, &s->default_cstyle, &s->default_mode);