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>
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
56 /* Global window list. */
57 struct windows windows
;
59 void window_pane_read_callback(struct bufferevent
*, void *);
60 void window_pane_error_callback(struct bufferevent
*, short, void *);
62 RB_GENERATE(winlinks
, winlink
, entry
, winlink_cmp
);
65 winlink_cmp(struct winlink
*wl1
, struct winlink
*wl2
)
67 return (wl1
->idx
- wl2
->idx
);
71 winlink_find_by_window(struct winlinks
*wwl
, struct window
*w
)
75 RB_FOREACH(wl
, winlinks
, wwl
) {
84 winlink_find_by_index(struct winlinks
*wwl
, int idx
)
92 return (RB_FIND(winlinks
, wwl
, &wl
));
96 winlink_next_index(struct winlinks
*wwl
, int idx
)
102 if (winlink_find_by_index(wwl
, i
) == NULL
)
113 winlink_count(struct winlinks
*wwl
)
119 RB_FOREACH(wl
, winlinks
, wwl
)
126 winlink_add(struct winlinks
*wwl
, int idx
)
131 if ((idx
= winlink_next_index(wwl
, -idx
- 1)) == -1)
133 } else if (winlink_find_by_index(wwl
, idx
) != NULL
)
136 wl
= xcalloc(1, sizeof *wl
);
138 RB_INSERT(winlinks
, wwl
, wl
);
144 winlink_set_window(struct winlink
*wl
, struct window
*w
)
151 winlink_remove(struct winlinks
*wwl
, struct winlink
*wl
)
153 struct window
*w
= wl
->window
;
155 RB_REMOVE(winlinks
, wwl
, wl
);
156 if (wl
->status_text
!= NULL
)
157 xfree(wl
->status_text
);
161 if (w
->references
== 0)
162 fatal("bad reference count");
164 if (w
->references
== 0)
170 winlink_next(struct winlink
*wl
)
172 return (RB_NEXT(winlinks
, wwl
, wl
));
176 winlink_previous(struct winlink
*wl
)
178 return (RB_PREV(winlinks
, wwl
, wl
));
182 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
185 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
186 wl
= RB_MIN(winlinks
, &s
->windows
);
193 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
196 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
197 wl
= RB_MAX(winlinks
, &s
->windows
);
204 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
209 winlink_stack_remove(stack
, wl
);
210 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
214 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
221 TAILQ_FOREACH(wl2
, stack
, sentry
) {
223 TAILQ_REMOVE(stack
, wl
, sentry
);
230 window_index(struct window
*s
, u_int
*i
)
232 for (*i
= 0; *i
< ARRAY_LENGTH(&windows
); (*i
)++) {
233 if (s
== ARRAY_ITEM(&windows
, *i
))
240 window_create1(u_int sx
, u_int sy
)
245 w
= xcalloc(1, sizeof *w
);
249 TAILQ_INIT(&w
->panes
);
253 w
->layout_root
= NULL
;
258 queue_window_name(w
);
260 options_init(&w
->options
, &global_w_options
);
262 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
263 if (ARRAY_ITEM(&windows
, i
) == NULL
) {
264 ARRAY_SET(&windows
, i
, w
);
268 if (i
== ARRAY_LENGTH(&windows
))
269 ARRAY_ADD(&windows
, w
);
276 window_create(const char *name
, const char *cmd
, const char *shell
,
277 const char *cwd
, struct environ
*env
, struct termios
*tio
,
278 u_int sx
, u_int sy
, u_int hlimit
,char **cause
)
281 struct window_pane
*wp
;
283 w
= window_create1(sx
, sy
);
284 wp
= window_add_pane(w
, hlimit
);
286 if (window_pane_spawn(wp
, cmd
, shell
, cwd
, env
, tio
, cause
) != 0) {
290 w
->active
= TAILQ_FIRST(&w
->panes
);
292 w
->name
= xstrdup(name
);
293 options_set_number(&w
->options
, "automatic-rename", 0);
295 w
->name
= default_window_name(w
);
300 window_destroy(struct window
*w
)
304 if (window_index(w
, &i
) != 0)
305 fatalx("index not found");
306 ARRAY_SET(&windows
, i
, NULL
);
307 while (!ARRAY_EMPTY(&windows
) && ARRAY_LAST(&windows
) == NULL
)
308 ARRAY_TRUNC(&windows
, 1);
310 if (w
->layout_root
!= NULL
)
313 evtimer_del(&w
->name_timer
);
315 options_free(&w
->options
);
317 window_destroy_panes(w
);
325 window_resize(struct window
*w
, u_int sx
, u_int sy
)
332 window_set_active_pane(struct window
*w
, struct window_pane
*wp
)
338 while (!window_pane_visible(w
->active
)) {
339 w
->active
= TAILQ_PREV(w
->active
, window_panes
, entry
);
340 if (w
->active
== NULL
)
341 w
->active
= TAILQ_LAST(&w
->panes
, window_panes
);
348 window_set_active_at(struct window
*w
, u_int x
, u_int y
)
350 struct window_pane
*wp
;
352 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
353 if (wp
== w
->active
|| !window_pane_visible(wp
))
355 if (x
< wp
->xoff
|| x
>= wp
->xoff
+ wp
->sx
)
357 if (y
< wp
->yoff
|| y
>= wp
->yoff
+ wp
->sy
)
359 window_set_active_pane(w
, wp
);
365 window_add_pane(struct window
*w
, u_int hlimit
)
367 struct window_pane
*wp
;
369 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
370 if (TAILQ_EMPTY(&w
->panes
))
371 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
373 TAILQ_INSERT_AFTER(&w
->panes
, w
->active
, wp
, entry
);
378 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
380 if (wp
== w
->active
) {
383 if (w
->active
== NULL
) {
384 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
385 if (w
->active
== NULL
)
386 w
->active
= TAILQ_NEXT(wp
, entry
);
388 } else if (wp
== w
->last
)
391 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
392 window_pane_destroy(wp
);
396 window_pane_at_index(struct window
*w
, u_int idx
)
398 struct window_pane
*wp
;
402 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
411 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
414 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
415 wp
= TAILQ_FIRST(&w
->panes
);
422 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
426 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
427 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
434 window_pane_index(struct window
*w
, struct window_pane
*wp
)
436 struct window_pane
*wq
;
440 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
449 window_count_panes(struct window
*w
)
451 struct window_pane
*wp
;
455 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
461 window_destroy_panes(struct window
*w
)
463 struct window_pane
*wp
;
465 while (!TAILQ_EMPTY(&w
->panes
)) {
466 wp
= TAILQ_FIRST(&w
->panes
);
467 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
468 window_pane_destroy(wp
);
472 /* Return list of printable window flag symbols. No flags is just a space. */
474 window_printable_flags(struct session
*s
, struct winlink
*wl
)
480 if (wl
->flags
& WINLINK_ACTIVITY
)
482 if (wl
->flags
& WINLINK_BELL
)
484 if (wl
->flags
& WINLINK_CONTENT
)
486 if (wl
->flags
& WINLINK_SILENCE
)
490 if (wl
== TAILQ_FIRST(&s
->lastw
))
495 return (xstrdup(flags
));
499 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
501 struct window_pane
*wp
;
503 wp
= xcalloc(1, sizeof *wp
);
515 wp
->layout_cell
= NULL
;
525 wp
->pipe_event
= NULL
;
527 wp
->saved_grid
= NULL
;
529 screen_init(&wp
->base
, sx
, sy
, hlimit
);
530 wp
->screen
= &wp
->base
;
538 window_pane_destroy(struct window_pane
*wp
)
540 window_pane_reset_mode(wp
);
544 bufferevent_free(wp
->event
);
549 screen_free(&wp
->base
);
550 if (wp
->saved_grid
!= NULL
)
551 grid_destroy(wp
->saved_grid
);
553 if (wp
->pipe_fd
!= -1) {
555 bufferevent_free(wp
->pipe_event
);
560 if (wp
->shell
!= NULL
)
568 window_pane_spawn(struct window_pane
*wp
, const char *cmd
, const char *shell
,
569 const char *cwd
, struct environ
*env
, struct termios
*tio
, char **cause
)
578 bufferevent_free(wp
->event
);
583 wp
->cmd
= xstrdup(cmd
);
586 if (wp
->shell
!= NULL
)
588 wp
->shell
= xstrdup(shell
);
593 wp
->cwd
= xstrdup(cwd
);
596 memset(&ws
, 0, sizeof ws
);
597 ws
.ws_col
= screen_size_x(&wp
->base
);
598 ws
.ws_row
= screen_size_y(&wp
->base
);
600 switch (wp
->pid
= forkpty(&wp
->fd
, wp
->tty
, NULL
, &ws
)) {
603 xasprintf(cause
, "%s: %s", cmd
, strerror(errno
));
606 if (chdir(wp
->cwd
) != 0)
609 if (tcgetattr(STDIN_FILENO
, &tio2
) != 0)
610 fatal("tcgetattr failed");
612 memcpy(tio2
.c_cc
, tio
->c_cc
, sizeof tio2
.c_cc
);
613 tio2
.c_cc
[VERASE
] = '\177';
614 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tio2
) != 0)
615 fatal("tcgetattr failed");
617 closefrom(STDERR_FILENO
+ 1);
624 if (*wp
->cmd
!= '\0') {
625 /* Set SHELL but only if it is currently not useful. */
626 shell
= getenv("SHELL");
627 if (shell
== NULL
|| *shell
== '\0' || areshell(shell
))
628 setenv("SHELL", wp
->shell
, 1);
630 execl(_PATH_BSHELL
, "sh", "-c", wp
->cmd
, (char *) NULL
);
631 fatal("execl failed");
634 /* No command; fork a login shell. */
635 ptr
= strrchr(wp
->shell
, '/');
636 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
637 xasprintf(&argv0
, "-%s", ptr
+ 1);
639 xasprintf(&argv0
, "-%s", wp
->shell
);
640 setenv("SHELL", wp
->shell
, 1);
641 execl(wp
->shell
, argv0
, (char *) NULL
);
642 fatal("execl failed");
645 setblocking(wp
->fd
, 0);
647 wp
->event
= bufferevent_new(wp
->fd
,
648 window_pane_read_callback
, NULL
, window_pane_error_callback
, wp
);
649 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
656 window_pane_read_callback(unused
struct bufferevent
*bufev
, void *data
)
658 struct window_pane
*wp
= data
;
662 new_size
= EVBUFFER_LENGTH(wp
->event
->input
) - wp
->pipe_off
;
663 if (wp
->pipe_fd
!= -1 && new_size
> 0) {
664 new_data
= EVBUFFER_DATA(wp
->event
->input
);
665 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
670 wp
->pipe_off
= EVBUFFER_LENGTH(wp
->event
->input
);
673 * If we get here, we're not outputting anymore, so set the silence
674 * flag on the window.
676 wp
->window
->flags
|= WINDOW_SILENCE
;
677 if (gettimeofday(&wp
->window
->silence_timer
, NULL
) != 0)
678 fatal("gettimeofday failed.");
683 window_pane_error_callback(
684 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
686 struct window_pane
*wp
= data
;
688 server_destroy_pane(wp
);
692 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
696 if (sx
== wp
->sx
&& sy
== wp
->sy
)
701 memset(&ws
, 0, sizeof ws
);
705 screen_resize(&wp
->base
, sx
, sy
);
706 if (wp
->mode
!= NULL
)
707 wp
->mode
->resize(wp
, sx
, sy
);
709 if (wp
->fd
!= -1 && ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
710 fatal("ioctl failed");
714 * Enter alternative screen mode. A copy of the visible screen is saved and the
715 * history is not updated
718 window_pane_alternate_on(struct window_pane
*wp
, struct grid_cell
*gc
)
720 struct screen
*s
= &wp
->base
;
723 if (wp
->saved_grid
!= NULL
)
725 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
727 sx
= screen_size_x(s
);
728 sy
= screen_size_y(s
);
730 wp
->saved_grid
= grid_create(sx
, sy
, 0);
731 grid_duplicate_lines(wp
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
732 wp
->saved_cx
= s
->cx
;
733 wp
->saved_cy
= s
->cy
;
734 memcpy(&wp
->saved_cell
, gc
, sizeof wp
->saved_cell
);
736 grid_view_clear(s
->grid
, 0, 0, sx
, sy
);
738 wp
->base
.grid
->flags
&= ~GRID_HISTORY
;
740 wp
->flags
|= PANE_REDRAW
;
743 /* Exit alternate screen mode and restore the copied grid. */
745 window_pane_alternate_off(struct window_pane
*wp
, struct grid_cell
*gc
)
747 struct screen
*s
= &wp
->base
;
750 if (wp
->saved_grid
== NULL
)
752 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
754 sx
= screen_size_x(s
);
755 sy
= screen_size_y(s
);
758 * If the current size is bigger, temporarily resize to the old size
759 * before copying back.
761 if (sy
> wp
->saved_grid
->sy
)
762 screen_resize(s
, sx
, wp
->saved_grid
->sy
);
764 /* Restore the grid, cursor position and cell. */
765 grid_duplicate_lines(s
->grid
, screen_hsize(s
), wp
->saved_grid
, 0, sy
);
766 s
->cx
= wp
->saved_cx
;
767 if (s
->cx
> screen_size_x(s
) - 1)
768 s
->cx
= screen_size_x(s
) - 1;
769 s
->cy
= wp
->saved_cy
;
770 if (s
->cy
> screen_size_y(s
) - 1)
771 s
->cy
= screen_size_y(s
) - 1;
772 memcpy(gc
, &wp
->saved_cell
, sizeof *gc
);
775 * Turn history back on (so resize can use it) and then resize back to
778 wp
->base
.grid
->flags
|= GRID_HISTORY
;
779 if (sy
> wp
->saved_grid
->sy
)
780 screen_resize(s
, sx
, sy
);
782 grid_destroy(wp
->saved_grid
);
783 wp
->saved_grid
= NULL
;
785 wp
->flags
|= PANE_REDRAW
;
789 window_pane_set_mode(struct window_pane
*wp
, const struct window_mode
*mode
)
793 if (wp
->mode
!= NULL
)
797 if ((s
= wp
->mode
->init(wp
)) != NULL
)
799 wp
->flags
|= PANE_REDRAW
;
804 window_pane_reset_mode(struct window_pane
*wp
)
806 if (wp
->mode
== NULL
)
812 wp
->screen
= &wp
->base
;
813 wp
->flags
|= PANE_REDRAW
;
817 window_pane_key(struct window_pane
*wp
, struct session
*sess
, int key
)
819 struct window_pane
*wp2
;
821 if (!window_pane_visible(wp
))
824 if (wp
->mode
!= NULL
) {
825 if (wp
->mode
->key
!= NULL
)
826 wp
->mode
->key(wp
, sess
, key
);
833 if (options_get_number(&wp
->window
->options
, "synchronize-panes")) {
834 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
835 if (wp2
== wp
|| wp2
->mode
!= NULL
)
837 if (wp2
->fd
!= -1 && window_pane_visible(wp2
))
845 struct window_pane
*wp
, struct session
*sess
, struct mouse_event
*m
)
847 if (!window_pane_visible(wp
))
850 if (m
->x
< wp
->xoff
|| m
->x
>= wp
->xoff
+ wp
->sx
)
852 if (m
->y
< wp
->yoff
|| m
->y
>= wp
->yoff
+ wp
->sy
)
857 if (wp
->mode
!= NULL
) {
858 if (wp
->mode
->mouse
!= NULL
)
859 wp
->mode
->mouse(wp
, sess
, m
);
860 } else if (wp
->fd
!= -1)
865 window_pane_visible(struct window_pane
*wp
)
867 struct window
*w
= wp
->window
;
869 if (wp
->xoff
>= w
->sx
|| wp
->yoff
>= w
->sy
)
871 if (wp
->xoff
+ wp
->sx
> w
->sx
|| wp
->yoff
+ wp
->sy
> w
->sy
)
877 window_pane_search(struct window_pane
*wp
, const char *searchstr
, u_int
*lineno
)
879 struct screen
*s
= &wp
->base
;
880 char *newsearchstr
, *line
, *msg
;
884 xasprintf(&newsearchstr
, "*%s*", searchstr
);
886 for (i
= 0; i
< screen_size_y(s
); i
++) {
887 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
888 if (fnmatch(newsearchstr
, line
, 0) == 0) {
901 /* Find the pane directly above another. */
903 window_pane_find_up(struct window_pane
*wp
)
905 struct window_pane
*wp2
;
908 if (wp
== NULL
|| !window_pane_visible(wp
))
913 top
= wp
->window
->sy
+ 1;
916 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
917 if (!window_pane_visible(wp2
))
919 if (wp2
->yoff
+ wp2
->sy
+ 1 != top
)
921 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
927 /* Find the pane directly below another. */
929 window_pane_find_down(struct window_pane
*wp
)
931 struct window_pane
*wp2
;
934 if (wp
== NULL
|| !window_pane_visible(wp
))
937 bottom
= wp
->yoff
+ wp
->sy
+ 1;
938 if (bottom
>= wp
->window
->sy
)
942 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
943 if (!window_pane_visible(wp2
))
945 if (wp2
->yoff
!= bottom
)
947 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
954 * Find the pane directly to the left of another, adjacent to the left side and
955 * containing the top edge.
958 window_pane_find_left(struct window_pane
*wp
)
960 struct window_pane
*wp2
;
963 if (wp
== NULL
|| !window_pane_visible(wp
))
968 left
= wp
->window
->sx
+ 1;
971 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
972 if (!window_pane_visible(wp2
))
974 if (wp2
->xoff
+ wp2
->sx
+ 1 != left
)
976 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
983 * Find the pane directly to the right of another, that is adjacent to the
984 * right edge and including the top edge.
987 window_pane_find_right(struct window_pane
*wp
)
989 struct window_pane
*wp2
;
992 if (wp
== NULL
|| !window_pane_visible(wp
))
995 right
= wp
->xoff
+ wp
->sx
+ 1;
996 if (right
>= wp
->window
->sx
)
1000 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1001 if (!window_pane_visible(wp2
))
1003 if (wp2
->xoff
!= right
)
1005 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)