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_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
181 winlink_stack_remove(stack
, wl
);
182 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
186 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
193 TAILQ_FOREACH(wl2
, stack
, sentry
) {
195 TAILQ_REMOVE(stack
, wl
, sentry
);
202 window_index(struct window
*s
, u_int
*i
)
204 for (*i
= 0; *i
< ARRAY_LENGTH(&windows
); (*i
)++) {
205 if (s
== ARRAY_ITEM(&windows
, *i
))
212 window_create1(u_int sx
, u_int sy
)
217 w
= xcalloc(1, sizeof *w
);
221 TAILQ_INIT(&w
->panes
);
225 w
->layout_root
= NULL
;
230 queue_window_name(w
);
232 options_init(&w
->options
, &global_w_options
);
234 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
235 if (ARRAY_ITEM(&windows
, i
) == NULL
) {
236 ARRAY_SET(&windows
, i
, w
);
240 if (i
== ARRAY_LENGTH(&windows
))
241 ARRAY_ADD(&windows
, w
);
248 window_create(const char *name
, const char *cmd
, const char *shell
,
249 const char *cwd
, struct environ
*env
, struct termios
*tio
,
250 u_int sx
, u_int sy
, u_int hlimit
,char **cause
)
253 struct window_pane
*wp
;
255 w
= window_create1(sx
, sy
);
256 wp
= window_add_pane(w
, hlimit
);
258 if (window_pane_spawn(wp
, cmd
, shell
, cwd
, env
, tio
, cause
) != 0) {
262 w
->active
= TAILQ_FIRST(&w
->panes
);
264 w
->name
= xstrdup(name
);
265 options_set_number(&w
->options
, "automatic-rename", 0);
267 w
->name
= default_window_name(w
);
272 window_destroy(struct window
*w
)
276 if (window_index(w
, &i
) != 0)
277 fatalx("index not found");
278 ARRAY_SET(&windows
, i
, NULL
);
279 while (!ARRAY_EMPTY(&windows
) && ARRAY_LAST(&windows
) == NULL
)
280 ARRAY_TRUNC(&windows
, 1);
282 if (w
->layout_root
!= NULL
)
285 evtimer_del(&w
->name_timer
);
287 options_free(&w
->options
);
289 window_destroy_panes(w
);
297 window_resize(struct window
*w
, u_int sx
, u_int sy
)
304 window_set_active_pane(struct window
*w
, struct window_pane
*wp
)
307 while (!window_pane_visible(w
->active
)) {
308 w
->active
= TAILQ_PREV(w
->active
, window_panes
, entry
);
309 if (w
->active
== NULL
)
310 w
->active
= TAILQ_LAST(&w
->panes
, window_panes
);
317 window_set_active_at(struct window
*w
, u_int x
, u_int y
)
319 struct window_pane
*wp
;
321 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
322 if (!window_pane_visible(wp
))
324 if (x
< wp
->xoff
|| x
>= wp
->xoff
+ wp
->sx
)
326 if (y
< wp
->yoff
|| y
>= wp
->yoff
+ wp
->sy
)
328 window_set_active_pane(w
, wp
);
334 window_add_pane(struct window
*w
, u_int hlimit
)
336 struct window_pane
*wp
;
338 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
339 if (TAILQ_EMPTY(&w
->panes
))
340 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
342 TAILQ_INSERT_AFTER(&w
->panes
, w
->active
, wp
, entry
);
347 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
349 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
350 if (w
->active
== NULL
)
351 w
->active
= TAILQ_NEXT(wp
, entry
);
353 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
354 window_pane_destroy(wp
);
358 window_pane_at_index(struct window
*w
, u_int idx
)
360 struct window_pane
*wp
;
364 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
373 window_pane_index(struct window
*w
, struct window_pane
*wp
)
375 struct window_pane
*wq
;
379 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
388 window_count_panes(struct window
*w
)
390 struct window_pane
*wp
;
394 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
400 window_destroy_panes(struct window
*w
)
402 struct window_pane
*wp
;
404 while (!TAILQ_EMPTY(&w
->panes
)) {
405 wp
= TAILQ_FIRST(&w
->panes
);
406 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
407 window_pane_destroy(wp
);
412 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
414 struct window_pane
*wp
;
416 wp
= xcalloc(1, sizeof *wp
);
428 wp
->layout_cell
= NULL
;
438 wp
->pipe_event
= NULL
;
440 wp
->saved_grid
= NULL
;
442 screen_init(&wp
->base
, sx
, sy
, hlimit
);
443 wp
->screen
= &wp
->base
;
451 window_pane_destroy(struct window_pane
*wp
)
455 bufferevent_free(wp
->event
);
460 window_pane_reset_mode(wp
);
461 screen_free(&wp
->base
);
462 if (wp
->saved_grid
!= NULL
)
463 grid_destroy(wp
->saved_grid
);
465 if (wp
->pipe_fd
!= -1) {
467 bufferevent_free(wp
->pipe_event
);
472 if (wp
->shell
!= NULL
)
480 window_pane_spawn(struct window_pane
*wp
, const char *cmd
, const char *shell
,
481 const char *cwd
, struct environ
*env
, struct termios
*tio
, char **cause
)
491 bufferevent_free(wp
->event
);
496 wp
->cmd
= xstrdup(cmd
);
499 if (wp
->shell
!= NULL
)
501 wp
->shell
= xstrdup(shell
);
506 wp
->cwd
= xstrdup(cwd
);
509 memset(&ws
, 0, sizeof ws
);
510 ws
.ws_col
= screen_size_x(&wp
->base
);
511 ws
.ws_row
= screen_size_y(&wp
->base
);
513 switch (wp
->pid
= forkpty(&wp
->fd
, wp
->tty
, NULL
, &ws
)) {
516 xasprintf(cause
, "%s: %s", cmd
, strerror(errno
));
519 if (chdir(wp
->cwd
) != 0)
522 if (tcgetattr(STDIN_FILENO
, &tio2
) != 0)
523 fatal("tcgetattr failed");
525 memcpy(tio2
.c_cc
, tio
->c_cc
, sizeof tio2
.c_cc
);
526 tio2
.c_cc
[VERASE
] = '\177';
527 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tio2
) != 0)
528 fatal("tcgetattr failed");
535 if (*wp
->cmd
!= '\0') {
536 /* Set SHELL but only if it is currently not useful. */
537 shell
= getenv("SHELL");
538 if (shell
== NULL
|| *shell
== '\0' || areshell(shell
))
539 setenv("SHELL", wp
->shell
, 1);
541 execl(_PATH_BSHELL
, "sh", "-c", wp
->cmd
, (char *) NULL
);
542 fatal("execl failed");
545 /* No command; fork a login shell. */
546 ptr
= strrchr(wp
->shell
, '/');
547 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
548 xasprintf(&argv0
, "-%s", ptr
+ 1);
550 xasprintf(&argv0
, "-%s", wp
->shell
);
551 setenv("SHELL", wp
->shell
, 1);
552 execl(wp
->shell
, argv0
, (char *) NULL
);
553 fatal("execl failed");
556 if ((mode
= fcntl(wp
->fd
, F_GETFL
)) == -1)
557 fatal("fcntl failed");
558 if (fcntl(wp
->fd
, F_SETFL
, mode
|O_NONBLOCK
) == -1)
559 fatal("fcntl failed");
560 if (fcntl(wp
->fd
, F_SETFD
, FD_CLOEXEC
) == -1)
561 fatal("fcntl failed");
562 wp
->event
= bufferevent_new(wp
->fd
,
563 window_pane_read_callback
, NULL
, window_pane_error_callback
, wp
);
564 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
571 window_pane_read_callback(unused
struct bufferevent
*bufev
, void *data
)
573 struct window_pane
*wp
= data
;
577 new_size
= EVBUFFER_LENGTH(wp
->event
->input
) - wp
->pipe_off
;
578 if (wp
->pipe_fd
!= -1 && new_size
> 0) {
579 new_data
= EVBUFFER_DATA(wp
->event
->input
);
580 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
585 wp
->pipe_off
= EVBUFFER_LENGTH(wp
->event
->input
);
590 window_pane_error_callback(
591 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
593 struct window_pane
*wp
= data
;
595 server_destroy_pane(wp
);
599 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
603 if (sx
== wp
->sx
&& sy
== wp
->sy
)
608 memset(&ws
, 0, sizeof ws
);
612 screen_resize(&wp
->base
, sx
, sy
);
613 if (wp
->mode
!= NULL
)
614 wp
->mode
->resize(wp
, sx
, sy
);
616 if (wp
->fd
!= -1 && ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
617 fatal("ioctl failed");
621 * Enter alternative screen mode. A copy of the visible screen is saved and the
622 * history is not updated
625 window_pane_alternate_on(struct window_pane
*wp
, struct grid_cell
*gc
)
627 struct screen
*s
= &wp
->base
;
630 if (wp
->saved_grid
!= NULL
)
632 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
634 sx
= screen_size_x(s
);
635 sy
= screen_size_y(s
);
637 wp
->saved_grid
= grid_create(sx
, sy
, 0);
638 grid_duplicate_lines(wp
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
639 wp
->saved_cx
= s
->cx
;
640 wp
->saved_cy
= s
->cy
;
641 memcpy(&wp
->saved_cell
, gc
, sizeof wp
->saved_cell
);
643 grid_view_clear(s
->grid
, 0, 0, sx
, sy
);
645 wp
->base
.grid
->flags
&= ~GRID_HISTORY
;
647 wp
->flags
|= PANE_REDRAW
;
650 /* Exit alternate screen mode and restore the copied grid. */
652 window_pane_alternate_off(struct window_pane
*wp
, struct grid_cell
*gc
)
654 struct screen
*s
= &wp
->base
;
657 if (wp
->saved_grid
== NULL
)
659 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
661 sx
= screen_size_x(s
);
662 sy
= screen_size_y(s
);
665 * If the current size is bigger, temporarily resize to the old size
666 * before copying back.
668 if (sy
> wp
->saved_grid
->sy
)
669 screen_resize(s
, sx
, wp
->saved_grid
->sy
);
671 /* Restore the grid, cursor position and cell. */
672 grid_duplicate_lines(s
->grid
, screen_hsize(s
), wp
->saved_grid
, 0, sy
);
673 s
->cx
= wp
->saved_cx
;
674 if (s
->cx
> screen_size_x(s
) - 1)
675 s
->cx
= screen_size_x(s
) - 1;
676 s
->cy
= wp
->saved_cy
;
677 if (s
->cy
> screen_size_y(s
) - 1)
678 s
->cy
= screen_size_y(s
) - 1;
679 memcpy(gc
, &wp
->saved_cell
, sizeof *gc
);
682 * Turn history back on (so resize can use it) and then resize back to
685 wp
->base
.grid
->flags
|= GRID_HISTORY
;
686 if (sy
> wp
->saved_grid
->sy
)
687 screen_resize(s
, sx
, sy
);
689 grid_destroy(wp
->saved_grid
);
690 wp
->saved_grid
= NULL
;
692 wp
->flags
|= PANE_REDRAW
;
696 window_pane_set_mode(struct window_pane
*wp
, const struct window_mode
*mode
)
700 if (wp
->mode
!= NULL
)
704 if ((s
= wp
->mode
->init(wp
)) != NULL
)
706 wp
->flags
|= PANE_REDRAW
;
711 window_pane_reset_mode(struct window_pane
*wp
)
713 if (wp
->mode
== NULL
)
719 wp
->screen
= &wp
->base
;
720 wp
->flags
|= PANE_REDRAW
;
724 window_pane_key(struct window_pane
*wp
, struct session
*sess
, int key
)
726 struct window_pane
*wp2
;
728 if (!window_pane_visible(wp
))
731 if (wp
->mode
!= NULL
) {
732 if (wp
->mode
->key
!= NULL
)
733 wp
->mode
->key(wp
, sess
, key
);
740 if (options_get_number(&wp
->window
->options
, "synchronize-panes")) {
741 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
742 if (wp2
== wp
|| wp2
->mode
!= NULL
)
744 if (wp2
->fd
!= -1 && window_pane_visible(wp2
))
752 struct window_pane
*wp
, struct session
*sess
, struct mouse_event
*m
)
754 if (!window_pane_visible(wp
))
757 if (m
->x
< wp
->xoff
|| m
->x
>= wp
->xoff
+ wp
->sx
)
759 if (m
->y
< wp
->yoff
|| m
->y
>= wp
->yoff
+ wp
->sy
)
764 if (wp
->mode
!= NULL
) {
765 if (wp
->mode
->mouse
!= NULL
)
766 wp
->mode
->mouse(wp
, sess
, m
);
767 } else if (wp
->fd
!= -1)
772 window_pane_visible(struct window_pane
*wp
)
774 struct window
*w
= wp
->window
;
776 if (wp
->xoff
>= w
->sx
|| wp
->yoff
>= w
->sy
)
778 if (wp
->xoff
+ wp
->sx
> w
->sx
|| wp
->yoff
+ wp
->sy
> w
->sy
)
784 window_pane_search(struct window_pane
*wp
, const char *searchstr
, u_int
*lineno
)
786 struct screen
*s
= &wp
->base
;
787 char *newsearchstr
, *line
, *msg
;
791 xasprintf(&newsearchstr
, "*%s*", searchstr
);
793 for (i
= 0; i
< screen_size_y(s
); i
++) {
794 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
795 if (fnmatch(newsearchstr
, line
, 0) == 0) {
808 /* Find the pane directly above another. */
810 window_pane_find_up(struct window_pane
*wp
)
812 struct window_pane
*wp2
;
815 if (wp
== NULL
|| !window_pane_visible(wp
))
820 top
= wp
->window
->sy
+ 1;
823 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
824 if (!window_pane_visible(wp2
))
826 if (wp2
->yoff
+ wp2
->sy
+ 1 != top
)
828 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
834 /* Find the pane directly below another. */
836 window_pane_find_down(struct window_pane
*wp
)
838 struct window_pane
*wp2
;
841 if (wp
== NULL
|| !window_pane_visible(wp
))
844 bottom
= wp
->yoff
+ wp
->sy
+ 1;
845 if (bottom
>= wp
->window
->sy
)
849 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
850 if (!window_pane_visible(wp2
))
852 if (wp2
->yoff
!= bottom
)
854 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
861 * Find the pane directly to the left of another, adjacent to the left side and
862 * containing the top edge.
865 window_pane_find_left(struct window_pane
*wp
)
867 struct window_pane
*wp2
;
870 if (wp
== NULL
|| !window_pane_visible(wp
))
875 left
= wp
->window
->sx
+ 1;
878 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
879 if (!window_pane_visible(wp2
))
881 if (wp2
->xoff
+ wp2
->sx
+ 1 != left
)
883 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
890 * Find the pane directly to the right of another, that is adjacent to the
891 * right edge and including the top edge.
894 window_pane_find_right(struct window_pane
*wp
)
896 struct window_pane
*wp2
;
899 if (wp
== NULL
|| !window_pane_visible(wp
))
902 right
= wp
->xoff
+ wp
->sx
+ 1;
903 if (right
>= wp
->window
->sx
)
907 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
908 if (!window_pane_visible(wp2
))
910 if (wp2
->xoff
!= right
)
912 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)