Change terminal-overrides to a server option (now that we have them), it
[tmux-openbsd.git] / window.c
blob842a5c6376de2dfacd8d9215209fe8c7730d87bc
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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 <errno.h>
23 #include <fcntl.h>
24 #include <fnmatch.h>
25 #include <paths.h>
26 #include <pwd.h>
27 #include <signal.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <termios.h>
32 #include <unistd.h>
33 #include <util.h>
35 #include "tmux.h"
38 * Each window is attached to a number of panes, each of which is a pty. This
39 * file contains code to handle them.
41 * A pane has two buffers attached, these are filled and emptied by the main
42 * server poll loop. Output data is received from pty's in screen format,
43 * translated and returned as a series of escape sequences and strings via
44 * input_parse (in input.c). Input data is received as key codes and written
45 * directly via input_key.
47 * Each pane also has a "virtual" screen (screen.c) which contains the current
48 * state and is redisplayed when the window is reattached to a client.
50 * Windows are stored directly on a global array and wrapped in any number of
51 * winlink structs to be linked onto local session RB trees. A reference count
52 * is maintained and a window removed from the global list and destroyed when
53 * it reaches zero.
56 /* Global window list. */
57 struct windows windows;
59 /* Global panes tree. */
60 struct window_pane_tree all_window_panes;
61 u_int next_window_pane_id;
62 u_int next_window_id;
64 struct window_pane *window_pane_active_set(struct window_pane *,
65 struct window_pane *);
66 void window_pane_active_lost(struct window_pane *, struct window_pane *);
68 void window_pane_timer_callback(int, short, void *);
69 void window_pane_read_callback(struct bufferevent *, void *);
70 void window_pane_error_callback(struct bufferevent *, short, void *);
72 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
74 int
75 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
77 return (wl1->idx - wl2->idx);
80 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
82 int
83 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
85 return (wp1->id - wp2->id);
88 struct winlink *
89 winlink_find_by_window(struct winlinks *wwl, struct window *w)
91 struct winlink *wl;
93 RB_FOREACH(wl, winlinks, wwl) {
94 if (wl->window == w)
95 return (wl);
98 return (NULL);
101 struct winlink *
102 winlink_find_by_index(struct winlinks *wwl, int idx)
104 struct winlink wl;
106 if (idx < 0)
107 fatalx("bad index");
109 wl.idx = idx;
110 return (RB_FIND(winlinks, wwl, &wl));
113 struct winlink *
114 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
116 struct winlink *wl;
118 RB_FOREACH(wl, winlinks, wwl) {
119 if (wl->window->id == id)
120 return (wl);
122 return (NULL);
126 winlink_next_index(struct winlinks *wwl, int idx)
128 int i;
130 i = idx;
131 do {
132 if (winlink_find_by_index(wwl, i) == NULL)
133 return (i);
134 if (i == INT_MAX)
135 i = 0;
136 else
137 i++;
138 } while (i != idx);
139 return (-1);
142 u_int
143 winlink_count(struct winlinks *wwl)
145 struct winlink *wl;
146 u_int n;
148 n = 0;
149 RB_FOREACH(wl, winlinks, wwl)
150 n++;
152 return (n);
155 struct winlink *
156 winlink_add(struct winlinks *wwl, int idx)
158 struct winlink *wl;
160 if (idx < 0) {
161 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
162 return (NULL);
163 } else if (winlink_find_by_index(wwl, idx) != NULL)
164 return (NULL);
166 wl = xcalloc(1, sizeof *wl);
167 wl->idx = idx;
168 RB_INSERT(winlinks, wwl, wl);
170 return (wl);
173 void
174 winlink_set_window(struct winlink *wl, struct window *w)
176 wl->window = w;
177 w->references++;
180 void
181 winlink_remove(struct winlinks *wwl, struct winlink *wl)
183 struct window *w = wl->window;
185 RB_REMOVE(winlinks, wwl, wl);
186 free(wl->status_text);
187 free(wl);
189 if (w != NULL)
190 window_remove_ref(w);
193 struct winlink *
194 winlink_next(struct winlink *wl)
196 return (RB_NEXT(winlinks, wwl, wl));
199 struct winlink *
200 winlink_previous(struct winlink *wl)
202 return (RB_PREV(winlinks, wwl, wl));
205 struct winlink *
206 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
208 for (; n > 0; n--) {
209 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
210 wl = RB_MIN(winlinks, &s->windows);
213 return (wl);
216 struct winlink *
217 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
219 for (; n > 0; n--) {
220 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
221 wl = RB_MAX(winlinks, &s->windows);
224 return (wl);
227 void
228 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
230 if (wl == NULL)
231 return;
233 winlink_stack_remove(stack, wl);
234 TAILQ_INSERT_HEAD(stack, wl, sentry);
237 void
238 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
240 struct winlink *wl2;
242 if (wl == NULL)
243 return;
245 TAILQ_FOREACH(wl2, stack, sentry) {
246 if (wl2 == wl) {
247 TAILQ_REMOVE(stack, wl, sentry);
248 return;
254 window_index(struct window *s, u_int *i)
256 for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
257 if (s == ARRAY_ITEM(&windows, *i))
258 return (0);
260 return (-1);
263 struct window *
264 window_find_by_id(u_int id)
266 struct window *w;
267 u_int i;
269 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
270 w = ARRAY_ITEM(&windows, i);
271 if (w->id == id)
272 return (w);
274 return (NULL);
277 struct window *
278 window_create1(u_int sx, u_int sy)
280 struct window *w;
281 u_int i;
283 w = xcalloc(1, sizeof *w);
284 w->id = next_window_id++;
285 w->name = NULL;
286 w->flags = 0;
288 TAILQ_INIT(&w->panes);
289 w->active = NULL;
291 w->lastlayout = -1;
292 w->layout_root = NULL;
294 w->sx = sx;
295 w->sy = sy;
297 options_init(&w->options, &global_w_options);
298 if (options_get_number(&w->options, "automatic-rename"))
299 queue_window_name(w);
301 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
302 if (ARRAY_ITEM(&windows, i) == NULL) {
303 ARRAY_SET(&windows, i, w);
304 break;
307 if (i == ARRAY_LENGTH(&windows))
308 ARRAY_ADD(&windows, w);
309 w->references = 0;
311 return (w);
314 struct window *
315 window_create(const char *name, const char *cmd, const char *shell,
316 int cwd, struct environ *env, struct termios *tio,
317 u_int sx, u_int sy, u_int hlimit, char **cause)
319 struct window *w;
320 struct window_pane *wp;
322 w = window_create1(sx, sy);
323 wp = window_add_pane(w, hlimit);
324 layout_init(w, wp);
326 if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
327 window_destroy(w);
328 return (NULL);
331 w->active = TAILQ_FIRST(&w->panes);
332 if (name != NULL) {
333 w->name = xstrdup(name);
334 options_set_number(&w->options, "automatic-rename", 0);
335 } else
336 w->name = default_window_name(w);
338 return (w);
341 void
342 window_destroy(struct window *w)
344 u_int i;
346 window_unzoom(w);
348 if (window_index(w, &i) != 0)
349 fatalx("index not found");
350 ARRAY_SET(&windows, i, NULL);
351 while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
352 ARRAY_TRUNC(&windows, 1);
354 if (w->layout_root != NULL)
355 layout_free(w);
357 if (event_initialized(&w->name_timer))
358 evtimer_del(&w->name_timer);
360 options_free(&w->options);
362 window_destroy_panes(w);
364 free(w->name);
365 free(w);
368 void
369 window_remove_ref(struct window *w)
371 if (w->references == 0)
372 fatal("bad reference count");
373 w->references--;
374 if (w->references == 0)
375 window_destroy(w);
378 void
379 window_set_name(struct window *w, const char *new_name)
381 free(w->name);
382 w->name = xstrdup(new_name);
383 notify_window_renamed(w);
386 void
387 window_resize(struct window *w, u_int sx, u_int sy)
389 w->sx = sx;
390 w->sy = sy;
394 * Restore previously active pane when changing from wp to nextwp. The intended
395 * pane is in nextwp and it returns the previously focused pane.
397 struct window_pane *
398 window_pane_active_set(struct window_pane *wp, struct window_pane *nextwp)
400 struct layout_cell *lc;
401 struct window_pane *lastwp;
403 /* Target pane's parent must not be an ancestor of source pane. */
404 for (lc = wp->layout_cell->parent; lc != NULL; lc = lc->parent) {
405 if (lc == nextwp->layout_cell->parent)
406 return (nextwp);
410 * Previously active pane, if any, must not be the same as the source
411 * pane.
413 lc = nextwp->layout_cell->parent;
414 if (lc != NULL && lc->lastwp != NULL) {
415 lastwp = lc->lastwp;
416 if (lastwp != wp && window_pane_visible(lastwp))
417 return (lastwp);
419 return (nextwp);
422 /* Remember previously active pane when changing from wp to nextwp. */
423 void
424 window_pane_active_lost(struct window_pane *wp, struct window_pane *nextwp)
426 struct layout_cell *lc, *lc2;
428 /* Save the target pane in its parent. */
429 nextwp->layout_cell->parent->lastwp = nextwp;
432 * Save the source pane in all of its parents up to, but not including,
433 * the common ancestor of itself and the target panes.
435 if (wp == NULL)
436 return;
437 for (lc = wp->layout_cell->parent; lc != NULL; lc = lc->parent) {
438 lc2 = nextwp->layout_cell->parent;
439 for (; lc2 != NULL; lc2 = lc2->parent) {
440 if (lc == lc2)
441 return;
443 lc->lastwp = wp;
447 void
448 window_set_active_pane(struct window *w, struct window_pane *wp)
450 if (wp == w->active)
451 return;
452 w->last = w->active;
453 w->active = wp;
454 window_pane_active_lost(w->last, wp);
455 while (!window_pane_visible(w->active)) {
456 w->active = TAILQ_PREV(w->active, window_panes, entry);
457 if (w->active == NULL)
458 w->active = TAILQ_LAST(&w->panes, window_panes);
459 if (w->active == wp)
460 return;
464 struct window_pane *
465 window_get_active_at(struct window *w, u_int x, u_int y)
467 struct window_pane *wp;
469 TAILQ_FOREACH(wp, &w->panes, entry) {
470 if (!window_pane_visible(wp))
471 continue;
472 if (x < wp->xoff || x > wp->xoff + wp->sx)
473 continue;
474 if (y < wp->yoff || y > wp->yoff + wp->sy)
475 continue;
476 return (wp);
478 return (NULL);
481 void
482 window_set_active_at(struct window *w, u_int x, u_int y)
484 struct window_pane *wp;
486 wp = window_get_active_at(w, x, y);
487 if (wp != NULL && wp != w->active)
488 window_set_active_pane(w, wp);
491 struct window_pane *
492 window_find_string(struct window *w, const char *s)
494 u_int x, y;
496 x = w->sx / 2;
497 y = w->sy / 2;
499 if (strcasecmp(s, "top") == 0)
500 y = 0;
501 else if (strcasecmp(s, "bottom") == 0)
502 y = w->sy - 1;
503 else if (strcasecmp(s, "left") == 0)
504 x = 0;
505 else if (strcasecmp(s, "right") == 0)
506 x = w->sx - 1;
507 else if (strcasecmp(s, "top-left") == 0) {
508 x = 0;
509 y = 0;
510 } else if (strcasecmp(s, "top-right") == 0) {
511 x = w->sx - 1;
512 y = 0;
513 } else if (strcasecmp(s, "bottom-left") == 0) {
514 x = 0;
515 y = w->sy - 1;
516 } else if (strcasecmp(s, "bottom-right") == 0) {
517 x = w->sx - 1;
518 y = w->sy - 1;
519 } else
520 return (NULL);
522 return (window_get_active_at(w, x, y));
526 window_zoom(struct window_pane *wp)
528 struct window *w = wp->window;
529 struct window_pane *wp1;
531 if (w->flags & WINDOW_ZOOMED)
532 return (-1);
534 if (!window_pane_visible(wp))
535 return (-1);
537 if (window_count_panes(w) == 1)
538 return (-1);
540 if (w->active != wp)
541 window_set_active_pane(w, wp);
543 TAILQ_FOREACH(wp1, &w->panes, entry) {
544 wp1->saved_layout_cell = wp1->layout_cell;
545 wp1->layout_cell = NULL;
548 w->saved_layout_root = w->layout_root;
549 layout_init(w, wp);
550 w->flags |= WINDOW_ZOOMED;
552 return (0);
556 window_unzoom(struct window *w)
558 struct window_pane *wp;
560 if (!(w->flags & WINDOW_ZOOMED))
561 return (-1);
563 w->flags &= ~WINDOW_ZOOMED;
564 layout_free(w);
565 w->layout_root = w->saved_layout_root;
567 TAILQ_FOREACH(wp, &w->panes, entry) {
568 wp->layout_cell = wp->saved_layout_cell;
569 wp->saved_layout_cell = NULL;
571 layout_fix_panes(w, w->sx, w->sy);
573 return (0);
576 struct window_pane *
577 window_add_pane(struct window *w, u_int hlimit)
579 struct window_pane *wp;
581 wp = window_pane_create(w, w->sx, w->sy, hlimit);
582 if (TAILQ_EMPTY(&w->panes))
583 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
584 else
585 TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
586 return (wp);
589 void
590 window_remove_pane(struct window *w, struct window_pane *wp)
592 if (wp == w->active) {
593 w->active = w->last;
594 w->last = NULL;
595 if (w->active == NULL) {
596 w->active = TAILQ_PREV(wp, window_panes, entry);
597 if (w->active == NULL)
598 w->active = TAILQ_NEXT(wp, entry);
600 } else if (wp == w->last)
601 w->last = NULL;
603 TAILQ_REMOVE(&w->panes, wp, entry);
604 window_pane_destroy(wp);
607 struct window_pane *
608 window_pane_at_index(struct window *w, u_int idx)
610 struct window_pane *wp;
611 u_int n;
613 n = options_get_number(&w->options, "pane-base-index");
614 TAILQ_FOREACH(wp, &w->panes, entry) {
615 if (n == idx)
616 return (wp);
617 n++;
619 return (NULL);
622 struct window_pane *
623 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
625 for (; n > 0; n--) {
626 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
627 wp = TAILQ_FIRST(&w->panes);
630 return (wp);
633 struct window_pane *
634 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
635 u_int n)
637 for (; n > 0; n--) {
638 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
639 wp = TAILQ_LAST(&w->panes, window_panes);
642 return (wp);
646 window_pane_index(struct window_pane *wp, u_int *i)
648 struct window_pane *wq;
649 struct window *w = wp->window;
651 *i = options_get_number(&w->options, "pane-base-index");
652 TAILQ_FOREACH(wq, &w->panes, entry) {
653 if (wp == wq) {
654 return (0);
656 (*i)++;
659 return (-1);
662 u_int
663 window_count_panes(struct window *w)
665 struct window_pane *wp;
666 u_int n;
668 n = 0;
669 TAILQ_FOREACH(wp, &w->panes, entry)
670 n++;
671 return (n);
674 void
675 window_destroy_panes(struct window *w)
677 struct window_pane *wp;
679 while (!TAILQ_EMPTY(&w->panes)) {
680 wp = TAILQ_FIRST(&w->panes);
681 TAILQ_REMOVE(&w->panes, wp, entry);
682 window_pane_destroy(wp);
686 /* Return list of printable window flag symbols. No flags is just a space. */
687 char *
688 window_printable_flags(struct session *s, struct winlink *wl)
690 char flags[BUFSIZ];
691 int pos;
693 pos = 0;
694 if (wl->flags & WINLINK_ACTIVITY)
695 flags[pos++] = '#';
696 if (wl->flags & WINLINK_BELL)
697 flags[pos++] = '!';
698 if (wl->flags & WINLINK_CONTENT)
699 flags[pos++] = '+';
700 if (wl->flags & WINLINK_SILENCE)
701 flags[pos++] = '~';
702 if (wl == s->curw)
703 flags[pos++] = '*';
704 if (wl == TAILQ_FIRST(&s->lastw))
705 flags[pos++] = '-';
706 if (wl->window->flags & WINDOW_ZOOMED)
707 flags[pos++] = 'Z';
708 if (pos == 0)
709 flags[pos++] = ' ';
710 flags[pos] = '\0';
711 return (xstrdup(flags));
714 /* Find pane in global tree by id. */
715 struct window_pane *
716 window_pane_find_by_id(u_int id)
718 struct window_pane wp;
720 wp.id = id;
721 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
724 struct window_pane *
725 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
727 struct window_pane *wp;
729 wp = xcalloc(1, sizeof *wp);
730 wp->window = w;
732 wp->id = next_window_pane_id++;
733 RB_INSERT(window_pane_tree, &all_window_panes, wp);
735 wp->cmd = NULL;
736 wp->shell = NULL;
737 wp->cwd = -1;
739 wp->fd = -1;
740 wp->event = NULL;
742 wp->mode = NULL;
744 wp->layout_cell = NULL;
746 wp->xoff = 0;
747 wp->yoff = 0;
749 wp->sx = sx;
750 wp->sy = sy;
752 wp->pipe_fd = -1;
753 wp->pipe_off = 0;
754 wp->pipe_event = NULL;
756 wp->saved_grid = NULL;
758 screen_init(&wp->base, sx, sy, hlimit);
759 wp->screen = &wp->base;
761 input_init(wp);
763 return (wp);
766 void
767 window_pane_destroy(struct window_pane *wp)
769 struct window_pane *wp2;
771 /* Forget removed pane in all layout cells that remember it. */
772 RB_FOREACH(wp2, window_pane_tree, &all_window_panes) {
773 if (wp2->layout_cell != NULL &&
774 wp2->layout_cell->parent != NULL &&
775 wp2->layout_cell->parent->lastwp == wp)
776 wp2->layout_cell->parent->lastwp = NULL;
779 window_pane_reset_mode(wp);
781 if (event_initialized(&wp->changes_timer))
782 evtimer_del(&wp->changes_timer);
784 if (wp->fd != -1) {
785 bufferevent_free(wp->event);
786 close(wp->fd);
789 input_free(wp);
791 screen_free(&wp->base);
792 if (wp->saved_grid != NULL)
793 grid_destroy(wp->saved_grid);
795 if (wp->pipe_fd != -1) {
796 bufferevent_free(wp->pipe_event);
797 close(wp->pipe_fd);
800 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
802 close(wp->cwd);
803 free(wp->shell);
804 free(wp->cmd);
805 free(wp);
809 window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
810 int cwd, struct environ *env, struct termios *tio, char **cause)
812 struct winsize ws;
813 char *argv0, paneid[16];
814 const char *ptr;
815 struct termios tio2;
817 if (wp->fd != -1) {
818 bufferevent_free(wp->event);
819 close(wp->fd);
821 if (cmd != NULL) {
822 free(wp->cmd);
823 wp->cmd = xstrdup(cmd);
825 if (shell != NULL) {
826 free(wp->shell);
827 wp->shell = xstrdup(shell);
829 if (cwd != -1) {
830 close(wp->cwd);
831 wp->cwd = dup(cwd);
834 log_debug("spawn: %s -- %s", wp->shell, wp->cmd);
836 memset(&ws, 0, sizeof ws);
837 ws.ws_col = screen_size_x(&wp->base);
838 ws.ws_row = screen_size_y(&wp->base);
840 switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
841 case -1:
842 wp->fd = -1;
843 xasprintf(cause, "%s: %s", cmd, strerror(errno));
844 return (-1);
845 case 0:
846 if (fchdir(wp->cwd) != 0)
847 chdir("/");
849 if (tcgetattr(STDIN_FILENO, &tio2) != 0)
850 fatal("tcgetattr failed");
851 if (tio != NULL)
852 memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
853 tio2.c_cc[VERASE] = '\177';
854 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
855 fatal("tcgetattr failed");
857 closefrom(STDERR_FILENO + 1);
859 xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
860 environ_set(env, "TMUX_PANE", paneid);
861 environ_push(env);
863 clear_signals(1);
864 log_close();
866 setenv("SHELL", wp->shell, 1);
867 ptr = strrchr(wp->shell, '/');
869 if (*wp->cmd != '\0') {
870 /* Use the command. */
871 if (ptr != NULL && *(ptr + 1) != '\0')
872 xasprintf(&argv0, "%s", ptr + 1);
873 else
874 xasprintf(&argv0, "%s", wp->shell);
875 execl(wp->shell, argv0, "-c", wp->cmd, (char *) NULL);
876 fatal("execl failed");
879 /* No command; fork a login shell. */
880 if (ptr != NULL && *(ptr + 1) != '\0')
881 xasprintf(&argv0, "-%s", ptr + 1);
882 else
883 xasprintf(&argv0, "-%s", wp->shell);
884 execl(wp->shell, argv0, (char *) NULL);
885 fatal("execl failed");
888 setblocking(wp->fd, 0);
890 wp->event = bufferevent_new(wp->fd,
891 window_pane_read_callback, NULL, window_pane_error_callback, wp);
892 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
894 return (0);
897 void
898 window_pane_timer_start(struct window_pane *wp)
900 struct timeval tv;
902 tv.tv_sec = 0;
903 tv.tv_usec = 1000;
905 evtimer_del(&wp->changes_timer);
906 evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp);
907 evtimer_add(&wp->changes_timer, &tv);
910 void
911 window_pane_timer_callback(unused int fd, unused short events, void *data)
913 struct window_pane *wp = data;
914 struct window *w = wp->window;
915 u_int interval, trigger;
917 interval = options_get_number(&w->options, "c0-change-interval");
918 trigger = options_get_number(&w->options, "c0-change-trigger");
920 if (wp->changes_redraw++ == interval) {
921 wp->flags |= PANE_REDRAW;
922 wp->changes_redraw = 0;
926 if (trigger == 0 || wp->changes < trigger) {
927 wp->flags |= PANE_REDRAW;
928 wp->flags &= ~PANE_DROP;
929 } else
930 window_pane_timer_start(wp);
931 wp->changes = 0;
934 void
935 window_pane_read_callback(unused struct bufferevent *bufev, void *data)
937 struct window_pane *wp = data;
938 char *new_data;
939 size_t new_size;
941 new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
942 if (wp->pipe_fd != -1 && new_size > 0) {
943 new_data = EVBUFFER_DATA(wp->event->input);
944 bufferevent_write(wp->pipe_event, new_data, new_size);
947 input_parse(wp);
949 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
952 * If we get here, we're not outputting anymore, so set the silence
953 * flag on the window.
955 wp->window->flags |= WINDOW_SILENCE;
956 if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
957 fatal("gettimeofday failed.");
960 void
961 window_pane_error_callback(
962 unused struct bufferevent *bufev, unused short what, void *data)
964 struct window_pane *wp = data;
966 server_destroy_pane(wp);
969 void
970 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
972 if (sx == wp->sx && sy == wp->sy)
973 return;
974 wp->sx = sx;
975 wp->sy = sy;
977 screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
978 if (wp->mode != NULL)
979 wp->mode->resize(wp, sx, sy);
981 wp->flags |= PANE_RESIZE;
985 * Enter alternative screen mode. A copy of the visible screen is saved and the
986 * history is not updated
988 void
989 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
990 int cursor)
992 struct screen *s = &wp->base;
993 u_int sx, sy;
995 if (wp->saved_grid != NULL)
996 return;
997 if (!options_get_number(&wp->window->options, "alternate-screen"))
998 return;
999 sx = screen_size_x(s);
1000 sy = screen_size_y(s);
1002 wp->saved_grid = grid_create(sx, sy, 0);
1003 grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1004 if (cursor) {
1005 wp->saved_cx = s->cx;
1006 wp->saved_cy = s->cy;
1008 memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
1010 grid_view_clear(s->grid, 0, 0, sx, sy);
1012 wp->base.grid->flags &= ~GRID_HISTORY;
1014 wp->flags |= PANE_REDRAW;
1017 /* Exit alternate screen mode and restore the copied grid. */
1018 void
1019 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
1020 int cursor)
1022 struct screen *s = &wp->base;
1023 u_int sx, sy;
1025 if (wp->saved_grid == NULL)
1026 return;
1027 if (!options_get_number(&wp->window->options, "alternate-screen"))
1028 return;
1029 sx = screen_size_x(s);
1030 sy = screen_size_y(s);
1033 * If the current size is bigger, temporarily resize to the old size
1034 * before copying back.
1036 if (sy > wp->saved_grid->sy)
1037 screen_resize(s, sx, wp->saved_grid->sy, 1);
1039 /* Restore the grid, cursor position and cell. */
1040 grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1041 if (cursor)
1042 s->cx = wp->saved_cx;
1043 if (s->cx > screen_size_x(s) - 1)
1044 s->cx = screen_size_x(s) - 1;
1045 if (cursor)
1046 s->cy = wp->saved_cy;
1047 if (s->cy > screen_size_y(s) - 1)
1048 s->cy = screen_size_y(s) - 1;
1049 memcpy(gc, &wp->saved_cell, sizeof *gc);
1052 * Turn history back on (so resize can use it) and then resize back to
1053 * the current size.
1055 wp->base.grid->flags |= GRID_HISTORY;
1056 if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1057 screen_resize(s, sx, sy, 1);
1059 grid_destroy(wp->saved_grid);
1060 wp->saved_grid = NULL;
1062 wp->flags |= PANE_REDRAW;
1066 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1068 struct screen *s;
1070 if (wp->mode != NULL)
1071 return (1);
1072 wp->mode = mode;
1074 if ((s = wp->mode->init(wp)) != NULL)
1075 wp->screen = s;
1076 wp->flags |= PANE_REDRAW;
1077 return (0);
1080 void
1081 window_pane_reset_mode(struct window_pane *wp)
1083 if (wp->mode == NULL)
1084 return;
1086 wp->mode->free(wp);
1087 wp->mode = NULL;
1089 wp->screen = &wp->base;
1090 wp->flags |= PANE_REDRAW;
1093 void
1094 window_pane_key(struct window_pane *wp, struct session *sess, int key)
1096 struct window_pane *wp2;
1098 if (!window_pane_visible(wp))
1099 return;
1101 if (wp->mode != NULL) {
1102 if (wp->mode->key != NULL)
1103 wp->mode->key(wp, sess, key);
1104 return;
1107 if (wp->fd == -1)
1108 return;
1109 input_key(wp, key);
1110 if (options_get_number(&wp->window->options, "synchronize-panes")) {
1111 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1112 if (wp2 == wp || wp2->mode != NULL)
1113 continue;
1114 if (wp2->fd != -1 && window_pane_visible(wp2))
1115 input_key(wp2, key);
1120 void
1121 window_pane_mouse(
1122 struct window_pane *wp, struct session *sess, struct mouse_event *m)
1124 if (!window_pane_visible(wp))
1125 return;
1127 if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1128 return;
1129 if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1130 return;
1131 m->x -= wp->xoff;
1132 m->y -= wp->yoff;
1134 if (wp->mode != NULL) {
1135 if (wp->mode->mouse != NULL &&
1136 options_get_number(&wp->window->options, "mode-mouse"))
1137 wp->mode->mouse(wp, sess, m);
1138 } else if (wp->fd != -1)
1139 input_mouse(wp, sess, m);
1143 window_pane_visible(struct window_pane *wp)
1145 struct window *w = wp->window;
1147 if (wp->layout_cell == NULL)
1148 return (0);
1149 if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1150 return (0);
1151 if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1152 return (0);
1153 return (1);
1156 char *
1157 window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1159 struct screen *s = &wp->base;
1160 char *newsearchstr, *line, *msg;
1161 u_int i;
1163 msg = NULL;
1164 xasprintf(&newsearchstr, "*%s*", searchstr);
1166 for (i = 0; i < screen_size_y(s); i++) {
1167 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1168 if (fnmatch(newsearchstr, line, 0) == 0) {
1169 msg = line;
1170 if (lineno != NULL)
1171 *lineno = i;
1172 break;
1174 free(line);
1177 free(newsearchstr);
1178 return (msg);
1181 /* Find the pane directly above another. */
1182 struct window_pane *
1183 window_pane_find_up(struct window_pane *wp)
1185 struct window_pane *wp2;
1186 u_int left, top;
1188 if (wp == NULL || !window_pane_visible(wp))
1189 return (NULL);
1191 top = wp->yoff;
1192 if (top == 0)
1193 top = wp->window->sy + 1;
1194 left = wp->xoff;
1196 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1197 if (!window_pane_visible(wp2))
1198 continue;
1199 if (wp2->yoff + wp2->sy + 1 != top)
1200 continue;
1201 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1202 return (window_pane_active_set(wp, wp2));
1204 return (NULL);
1207 /* Find the pane directly below another. */
1208 struct window_pane *
1209 window_pane_find_down(struct window_pane *wp)
1211 struct window_pane *wp2;
1212 u_int left, bottom;
1214 if (wp == NULL || !window_pane_visible(wp))
1215 return (NULL);
1217 bottom = wp->yoff + wp->sy + 1;
1218 if (bottom >= wp->window->sy)
1219 bottom = 0;
1220 left = wp->xoff;
1222 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1223 if (!window_pane_visible(wp2))
1224 continue;
1225 if (wp2->yoff != bottom)
1226 continue;
1227 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1228 return (window_pane_active_set(wp, wp2));
1230 return (NULL);
1234 * Find the pane directly to the left of another, adjacent to the left side and
1235 * containing the top edge.
1237 struct window_pane *
1238 window_pane_find_left(struct window_pane *wp)
1240 struct window_pane *wp2;
1241 u_int left, top;
1243 if (wp == NULL || !window_pane_visible(wp))
1244 return (NULL);
1246 left = wp->xoff;
1247 if (left == 0)
1248 left = wp->window->sx + 1;
1249 top = wp->yoff;
1251 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1252 if (!window_pane_visible(wp2))
1253 continue;
1254 if (wp2->xoff + wp2->sx + 1 != left)
1255 continue;
1256 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1257 return (window_pane_active_set(wp, wp2));
1259 return (NULL);
1263 * Find the pane directly to the right of another, that is adjacent to the
1264 * right edge and including the top edge.
1266 struct window_pane *
1267 window_pane_find_right(struct window_pane *wp)
1269 struct window_pane *wp2;
1270 u_int right, top;
1272 if (wp == NULL || !window_pane_visible(wp))
1273 return (NULL);
1275 right = wp->xoff + wp->sx + 1;
1276 if (right >= wp->window->sx)
1277 right = 0;
1278 top = wp->yoff;
1280 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1281 if (!window_pane_visible(wp2))
1282 continue;
1283 if (wp2->xoff != right)
1284 continue;
1285 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1286 return (window_pane_active_set(wp, wp2));
1288 return (NULL);
1291 /* Clear alert flags for a winlink */
1292 void
1293 winlink_clear_flags(struct winlink *wl)
1295 struct winlink *wm;
1296 struct session *s;
1297 struct window *w;
1298 u_int i;
1300 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1301 if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1302 continue;
1304 RB_FOREACH(s, sessions, &sessions) {
1305 if ((wm = session_has(s, w)) == NULL)
1306 continue;
1308 if (wm->window != wl->window)
1309 continue;
1310 if ((wm->flags & WINLINK_ALERTFLAGS) == 0)
1311 continue;
1313 wm->flags &= ~WINLINK_ALERTFLAGS;
1314 wm->window->flags &= ~WINDOW_ALERTFLAGS;
1315 server_status_session(s);