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
, struct window
*w
, 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
);
139 RB_INSERT(winlinks
, wwl
, wl
);
147 winlink_remove(struct winlinks
*wwl
, struct winlink
*wl
)
149 struct window
*w
= wl
->window
;
151 RB_REMOVE(winlinks
, wwl
, wl
);
152 if (wl
->status_text
!= NULL
)
153 xfree(wl
->status_text
);
156 if (w
->references
== 0)
157 fatal("bad reference count");
159 if (w
->references
== 0)
164 winlink_next(struct winlink
*wl
)
166 return (RB_NEXT(winlinks
, wwl
, wl
));
170 winlink_previous(struct winlink
*wl
)
172 return (RB_PREV(winlinks
, wwl
, wl
));
176 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
179 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
180 wl
= RB_MIN(winlinks
, &s
->windows
);
187 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
190 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
191 wl
= RB_MAX(winlinks
, &s
->windows
);
198 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
203 winlink_stack_remove(stack
, wl
);
204 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
208 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
215 TAILQ_FOREACH(wl2
, stack
, sentry
) {
217 TAILQ_REMOVE(stack
, wl
, sentry
);
224 window_index(struct window
*s
, u_int
*i
)
226 for (*i
= 0; *i
< ARRAY_LENGTH(&windows
); (*i
)++) {
227 if (s
== ARRAY_ITEM(&windows
, *i
))
234 window_create1(u_int sx
, u_int sy
)
239 w
= xcalloc(1, sizeof *w
);
243 TAILQ_INIT(&w
->panes
);
247 w
->layout_root
= NULL
;
252 queue_window_name(w
);
254 options_init(&w
->options
, &global_w_options
);
256 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
257 if (ARRAY_ITEM(&windows
, i
) == NULL
) {
258 ARRAY_SET(&windows
, i
, w
);
262 if (i
== ARRAY_LENGTH(&windows
))
263 ARRAY_ADD(&windows
, w
);
270 window_create(const char *name
, const char *cmd
, const char *shell
,
271 const char *cwd
, struct environ
*env
, struct termios
*tio
,
272 u_int sx
, u_int sy
, u_int hlimit
,char **cause
)
275 struct window_pane
*wp
;
277 w
= window_create1(sx
, sy
);
278 wp
= window_add_pane(w
, hlimit
);
280 if (window_pane_spawn(wp
, cmd
, shell
, cwd
, env
, tio
, cause
) != 0) {
284 w
->active
= TAILQ_FIRST(&w
->panes
);
286 w
->name
= xstrdup(name
);
287 options_set_number(&w
->options
, "automatic-rename", 0);
289 w
->name
= default_window_name(w
);
294 window_destroy(struct window
*w
)
298 if (window_index(w
, &i
) != 0)
299 fatalx("index not found");
300 ARRAY_SET(&windows
, i
, NULL
);
301 while (!ARRAY_EMPTY(&windows
) && ARRAY_LAST(&windows
) == NULL
)
302 ARRAY_TRUNC(&windows
, 1);
304 if (w
->layout_root
!= NULL
)
307 evtimer_del(&w
->name_timer
);
309 options_free(&w
->options
);
311 window_destroy_panes(w
);
319 window_resize(struct window
*w
, u_int sx
, u_int sy
)
326 window_set_active_pane(struct window
*w
, struct window_pane
*wp
)
332 while (!window_pane_visible(w
->active
)) {
333 w
->active
= TAILQ_PREV(w
->active
, window_panes
, entry
);
334 if (w
->active
== NULL
)
335 w
->active
= TAILQ_LAST(&w
->panes
, window_panes
);
342 window_set_active_at(struct window
*w
, u_int x
, u_int y
)
344 struct window_pane
*wp
;
346 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
347 if (wp
== w
->active
|| !window_pane_visible(wp
))
349 if (x
< wp
->xoff
|| x
>= wp
->xoff
+ wp
->sx
)
351 if (y
< wp
->yoff
|| y
>= wp
->yoff
+ wp
->sy
)
353 window_set_active_pane(w
, wp
);
359 window_add_pane(struct window
*w
, u_int hlimit
)
361 struct window_pane
*wp
;
363 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
364 if (TAILQ_EMPTY(&w
->panes
))
365 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
367 TAILQ_INSERT_AFTER(&w
->panes
, w
->active
, wp
, entry
);
372 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
374 if (wp
== w
->active
) {
377 if (w
->active
== NULL
) {
378 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
379 if (w
->active
== NULL
)
380 w
->active
= TAILQ_NEXT(wp
, entry
);
382 } else if (wp
== w
->last
)
385 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
386 window_pane_destroy(wp
);
390 window_pane_at_index(struct window
*w
, u_int idx
)
392 struct window_pane
*wp
;
396 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
405 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
408 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
409 wp
= TAILQ_FIRST(&w
->panes
);
416 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
420 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
421 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
428 window_pane_index(struct window
*w
, struct window_pane
*wp
)
430 struct window_pane
*wq
;
434 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
443 window_count_panes(struct window
*w
)
445 struct window_pane
*wp
;
449 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
455 window_destroy_panes(struct window
*w
)
457 struct window_pane
*wp
;
459 while (!TAILQ_EMPTY(&w
->panes
)) {
460 wp
= TAILQ_FIRST(&w
->panes
);
461 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
462 window_pane_destroy(wp
);
466 /* Return list of printable window flag symbols. No flags is just a space. */
468 window_printable_flags(struct session
*s
, struct winlink
*wl
)
474 if (wl
->flags
& WINLINK_ACTIVITY
)
476 if (wl
->flags
& WINLINK_BELL
)
478 if (wl
->flags
& WINLINK_CONTENT
)
480 if (wl
->flags
& WINLINK_SILENCE
)
484 if (wl
== TAILQ_FIRST(&s
->lastw
))
489 return (xstrdup(flags
));
493 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
495 struct window_pane
*wp
;
497 wp
= xcalloc(1, sizeof *wp
);
509 wp
->layout_cell
= NULL
;
519 wp
->pipe_event
= NULL
;
521 wp
->saved_grid
= NULL
;
523 screen_init(&wp
->base
, sx
, sy
, hlimit
);
524 wp
->screen
= &wp
->base
;
532 window_pane_destroy(struct window_pane
*wp
)
534 window_pane_reset_mode(wp
);
538 bufferevent_free(wp
->event
);
543 screen_free(&wp
->base
);
544 if (wp
->saved_grid
!= NULL
)
545 grid_destroy(wp
->saved_grid
);
547 if (wp
->pipe_fd
!= -1) {
549 bufferevent_free(wp
->pipe_event
);
554 if (wp
->shell
!= NULL
)
562 window_pane_spawn(struct window_pane
*wp
, const char *cmd
, const char *shell
,
563 const char *cwd
, struct environ
*env
, struct termios
*tio
, char **cause
)
572 bufferevent_free(wp
->event
);
577 wp
->cmd
= xstrdup(cmd
);
580 if (wp
->shell
!= NULL
)
582 wp
->shell
= xstrdup(shell
);
587 wp
->cwd
= xstrdup(cwd
);
590 memset(&ws
, 0, sizeof ws
);
591 ws
.ws_col
= screen_size_x(&wp
->base
);
592 ws
.ws_row
= screen_size_y(&wp
->base
);
594 switch (wp
->pid
= forkpty(&wp
->fd
, wp
->tty
, NULL
, &ws
)) {
597 xasprintf(cause
, "%s: %s", cmd
, strerror(errno
));
600 if (chdir(wp
->cwd
) != 0)
603 if (tcgetattr(STDIN_FILENO
, &tio2
) != 0)
604 fatal("tcgetattr failed");
606 memcpy(tio2
.c_cc
, tio
->c_cc
, sizeof tio2
.c_cc
);
607 tio2
.c_cc
[VERASE
] = '\177';
608 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tio2
) != 0)
609 fatal("tcgetattr failed");
611 closefrom(STDERR_FILENO
+ 1);
618 if (*wp
->cmd
!= '\0') {
619 /* Set SHELL but only if it is currently not useful. */
620 shell
= getenv("SHELL");
621 if (shell
== NULL
|| *shell
== '\0' || areshell(shell
))
622 setenv("SHELL", wp
->shell
, 1);
624 execl(_PATH_BSHELL
, "sh", "-c", wp
->cmd
, (char *) NULL
);
625 fatal("execl failed");
628 /* No command; fork a login shell. */
629 ptr
= strrchr(wp
->shell
, '/');
630 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
631 xasprintf(&argv0
, "-%s", ptr
+ 1);
633 xasprintf(&argv0
, "-%s", wp
->shell
);
634 setenv("SHELL", wp
->shell
, 1);
635 execl(wp
->shell
, argv0
, (char *) NULL
);
636 fatal("execl failed");
639 setblocking(wp
->fd
, 0);
641 wp
->event
= bufferevent_new(wp
->fd
,
642 window_pane_read_callback
, NULL
, window_pane_error_callback
, wp
);
643 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
650 window_pane_read_callback(unused
struct bufferevent
*bufev
, void *data
)
652 struct window_pane
*wp
= data
;
656 new_size
= EVBUFFER_LENGTH(wp
->event
->input
) - wp
->pipe_off
;
657 if (wp
->pipe_fd
!= -1 && new_size
> 0) {
658 new_data
= EVBUFFER_DATA(wp
->event
->input
);
659 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
664 wp
->pipe_off
= EVBUFFER_LENGTH(wp
->event
->input
);
667 * If we get here, we're not outputting anymore, so set the silence
668 * flag on the window.
670 wp
->window
->flags
|= WINDOW_SILENCE
;
671 if (gettimeofday(&wp
->window
->silence_timer
, NULL
) != 0)
672 fatal("gettimeofday failed.");
677 window_pane_error_callback(
678 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
680 struct window_pane
*wp
= data
;
682 server_destroy_pane(wp
);
686 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
690 if (sx
== wp
->sx
&& sy
== wp
->sy
)
695 memset(&ws
, 0, sizeof ws
);
699 screen_resize(&wp
->base
, sx
, sy
);
700 if (wp
->mode
!= NULL
)
701 wp
->mode
->resize(wp
, sx
, sy
);
703 if (wp
->fd
!= -1 && ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
704 fatal("ioctl failed");
708 * Enter alternative screen mode. A copy of the visible screen is saved and the
709 * history is not updated
712 window_pane_alternate_on(struct window_pane
*wp
, struct grid_cell
*gc
)
714 struct screen
*s
= &wp
->base
;
717 if (wp
->saved_grid
!= NULL
)
719 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
721 sx
= screen_size_x(s
);
722 sy
= screen_size_y(s
);
724 wp
->saved_grid
= grid_create(sx
, sy
, 0);
725 grid_duplicate_lines(wp
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
726 wp
->saved_cx
= s
->cx
;
727 wp
->saved_cy
= s
->cy
;
728 memcpy(&wp
->saved_cell
, gc
, sizeof wp
->saved_cell
);
730 grid_view_clear(s
->grid
, 0, 0, sx
, sy
);
732 wp
->base
.grid
->flags
&= ~GRID_HISTORY
;
734 wp
->flags
|= PANE_REDRAW
;
737 /* Exit alternate screen mode and restore the copied grid. */
739 window_pane_alternate_off(struct window_pane
*wp
, struct grid_cell
*gc
)
741 struct screen
*s
= &wp
->base
;
744 if (wp
->saved_grid
== NULL
)
746 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
748 sx
= screen_size_x(s
);
749 sy
= screen_size_y(s
);
752 * If the current size is bigger, temporarily resize to the old size
753 * before copying back.
755 if (sy
> wp
->saved_grid
->sy
)
756 screen_resize(s
, sx
, wp
->saved_grid
->sy
);
758 /* Restore the grid, cursor position and cell. */
759 grid_duplicate_lines(s
->grid
, screen_hsize(s
), wp
->saved_grid
, 0, sy
);
760 s
->cx
= wp
->saved_cx
;
761 if (s
->cx
> screen_size_x(s
) - 1)
762 s
->cx
= screen_size_x(s
) - 1;
763 s
->cy
= wp
->saved_cy
;
764 if (s
->cy
> screen_size_y(s
) - 1)
765 s
->cy
= screen_size_y(s
) - 1;
766 memcpy(gc
, &wp
->saved_cell
, sizeof *gc
);
769 * Turn history back on (so resize can use it) and then resize back to
772 wp
->base
.grid
->flags
|= GRID_HISTORY
;
773 if (sy
> wp
->saved_grid
->sy
)
774 screen_resize(s
, sx
, sy
);
776 grid_destroy(wp
->saved_grid
);
777 wp
->saved_grid
= NULL
;
779 wp
->flags
|= PANE_REDRAW
;
783 window_pane_set_mode(struct window_pane
*wp
, const struct window_mode
*mode
)
787 if (wp
->mode
!= NULL
)
791 if ((s
= wp
->mode
->init(wp
)) != NULL
)
793 wp
->flags
|= PANE_REDRAW
;
798 window_pane_reset_mode(struct window_pane
*wp
)
800 if (wp
->mode
== NULL
)
806 wp
->screen
= &wp
->base
;
807 wp
->flags
|= PANE_REDRAW
;
811 window_pane_key(struct window_pane
*wp
, struct session
*sess
, int key
)
813 struct window_pane
*wp2
;
815 if (!window_pane_visible(wp
))
818 if (wp
->mode
!= NULL
) {
819 if (wp
->mode
->key
!= NULL
)
820 wp
->mode
->key(wp
, sess
, key
);
827 if (options_get_number(&wp
->window
->options
, "synchronize-panes")) {
828 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
829 if (wp2
== wp
|| wp2
->mode
!= NULL
)
831 if (wp2
->fd
!= -1 && window_pane_visible(wp2
))
839 struct window_pane
*wp
, struct session
*sess
, struct mouse_event
*m
)
841 if (!window_pane_visible(wp
))
844 if (m
->x
< wp
->xoff
|| m
->x
>= wp
->xoff
+ wp
->sx
)
846 if (m
->y
< wp
->yoff
|| m
->y
>= wp
->yoff
+ wp
->sy
)
851 if (wp
->mode
!= NULL
) {
852 if (wp
->mode
->mouse
!= NULL
)
853 wp
->mode
->mouse(wp
, sess
, m
);
854 } else if (wp
->fd
!= -1)
859 window_pane_visible(struct window_pane
*wp
)
861 struct window
*w
= wp
->window
;
863 if (wp
->xoff
>= w
->sx
|| wp
->yoff
>= w
->sy
)
865 if (wp
->xoff
+ wp
->sx
> w
->sx
|| wp
->yoff
+ wp
->sy
> w
->sy
)
871 window_pane_search(struct window_pane
*wp
, const char *searchstr
, u_int
*lineno
)
873 struct screen
*s
= &wp
->base
;
874 char *newsearchstr
, *line
, *msg
;
878 xasprintf(&newsearchstr
, "*%s*", searchstr
);
880 for (i
= 0; i
< screen_size_y(s
); i
++) {
881 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
882 if (fnmatch(newsearchstr
, line
, 0) == 0) {
895 /* Find the pane directly above another. */
897 window_pane_find_up(struct window_pane
*wp
)
899 struct window_pane
*wp2
;
902 if (wp
== NULL
|| !window_pane_visible(wp
))
907 top
= wp
->window
->sy
+ 1;
910 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
911 if (!window_pane_visible(wp2
))
913 if (wp2
->yoff
+ wp2
->sy
+ 1 != top
)
915 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
921 /* Find the pane directly below another. */
923 window_pane_find_down(struct window_pane
*wp
)
925 struct window_pane
*wp2
;
928 if (wp
== NULL
|| !window_pane_visible(wp
))
931 bottom
= wp
->yoff
+ wp
->sy
+ 1;
932 if (bottom
>= wp
->window
->sy
)
936 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
937 if (!window_pane_visible(wp2
))
939 if (wp2
->yoff
!= bottom
)
941 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
948 * Find the pane directly to the left of another, adjacent to the left side and
949 * containing the top edge.
952 window_pane_find_left(struct window_pane
*wp
)
954 struct window_pane
*wp2
;
957 if (wp
== NULL
|| !window_pane_visible(wp
))
962 left
= wp
->window
->sx
+ 1;
965 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
966 if (!window_pane_visible(wp2
))
968 if (wp2
->xoff
+ wp2
->sx
+ 1 != left
)
970 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
977 * Find the pane directly to the right of another, that is adjacent to the
978 * right edge and including the top edge.
981 window_pane_find_right(struct window_pane
*wp
)
983 struct window_pane
*wp2
;
986 if (wp
== NULL
|| !window_pane_visible(wp
))
989 right
= wp
->xoff
+ wp
->sx
+ 1;
990 if (right
>= wp
->window
->sx
)
994 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
995 if (!window_pane_visible(wp2
))
997 if (wp2
->xoff
!= right
)
999 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)