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 /* Global panes tree. */
60 struct window_pane_tree all_window_panes
;
61 u_int next_window_pane_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
);
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
);
83 window_pane_cmp(struct window_pane
*wp1
, struct window_pane
*wp2
)
85 return (wp1
->id
- wp2
->id
);
89 winlink_find_by_window(struct winlinks
*wwl
, struct window
*w
)
93 RB_FOREACH(wl
, winlinks
, wwl
) {
102 winlink_find_by_index(struct winlinks
*wwl
, int idx
)
110 return (RB_FIND(winlinks
, wwl
, &wl
));
114 winlink_find_by_window_id(struct winlinks
*wwl
, u_int id
)
118 RB_FOREACH(wl
, winlinks
, wwl
) {
119 if (wl
->window
->id
== id
)
126 winlink_next_index(struct winlinks
*wwl
, int idx
)
132 if (winlink_find_by_index(wwl
, i
) == NULL
)
143 winlink_count(struct winlinks
*wwl
)
149 RB_FOREACH(wl
, winlinks
, wwl
)
156 winlink_add(struct winlinks
*wwl
, int idx
)
161 if ((idx
= winlink_next_index(wwl
, -idx
- 1)) == -1)
163 } else if (winlink_find_by_index(wwl
, idx
) != NULL
)
166 wl
= xcalloc(1, sizeof *wl
);
168 RB_INSERT(winlinks
, wwl
, wl
);
174 winlink_set_window(struct winlink
*wl
, struct window
*w
)
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
);
190 window_remove_ref(w
);
194 winlink_next(struct winlink
*wl
)
196 return (RB_NEXT(winlinks
, wwl
, wl
));
200 winlink_previous(struct winlink
*wl
)
202 return (RB_PREV(winlinks
, wwl
, wl
));
206 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
209 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
210 wl
= RB_MIN(winlinks
, &s
->windows
);
217 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
220 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
221 wl
= RB_MAX(winlinks
, &s
->windows
);
228 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
233 winlink_stack_remove(stack
, wl
);
234 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
238 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
245 TAILQ_FOREACH(wl2
, stack
, sentry
) {
247 TAILQ_REMOVE(stack
, wl
, sentry
);
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
))
264 window_find_by_id(u_int id
)
269 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
270 w
= ARRAY_ITEM(&windows
, i
);
278 window_create1(u_int sx
, u_int sy
)
283 w
= xcalloc(1, sizeof *w
);
284 w
->id
= next_window_id
++;
288 TAILQ_INIT(&w
->panes
);
292 w
->layout_root
= NULL
;
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
);
307 if (i
== ARRAY_LENGTH(&windows
))
308 ARRAY_ADD(&windows
, w
);
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
)
320 struct window_pane
*wp
;
322 w
= window_create1(sx
, sy
);
323 wp
= window_add_pane(w
, hlimit
);
326 if (window_pane_spawn(wp
, cmd
, shell
, cwd
, env
, tio
, cause
) != 0) {
331 w
->active
= TAILQ_FIRST(&w
->panes
);
333 w
->name
= xstrdup(name
);
334 options_set_number(&w
->options
, "automatic-rename", 0);
336 w
->name
= default_window_name(w
);
342 window_destroy(struct window
*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
)
357 if (event_initialized(&w
->name_timer
))
358 evtimer_del(&w
->name_timer
);
360 options_free(&w
->options
);
362 window_destroy_panes(w
);
369 window_remove_ref(struct window
*w
)
371 if (w
->references
== 0)
372 fatal("bad reference count");
374 if (w
->references
== 0)
379 window_set_name(struct window
*w
, const char *new_name
)
382 w
->name
= xstrdup(new_name
);
383 notify_window_renamed(w
);
387 window_resize(struct window
*w
, u_int sx
, u_int 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.
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
)
410 * Previously active pane, if any, must not be the same as the source
413 lc
= nextwp
->layout_cell
->parent
;
414 if (lc
!= NULL
&& lc
->lastwp
!= NULL
) {
416 if (lastwp
!= wp
&& window_pane_visible(lastwp
))
422 /* Remember previously active pane when changing from wp to nextwp. */
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.
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
) {
448 window_set_active_pane(struct window
*w
, struct window_pane
*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
);
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
))
472 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
474 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
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
);
492 window_find_string(struct window
*w
, const char *s
)
499 if (strcasecmp(s
, "top") == 0)
501 else if (strcasecmp(s
, "bottom") == 0)
503 else if (strcasecmp(s
, "left") == 0)
505 else if (strcasecmp(s
, "right") == 0)
507 else if (strcasecmp(s
, "top-left") == 0) {
510 } else if (strcasecmp(s
, "top-right") == 0) {
513 } else if (strcasecmp(s
, "bottom-left") == 0) {
516 } else if (strcasecmp(s
, "bottom-right") == 0) {
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
)
534 if (!window_pane_visible(wp
))
537 if (window_count_panes(w
) == 1)
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
;
550 w
->flags
|= WINDOW_ZOOMED
;
556 window_unzoom(struct window
*w
)
558 struct window_pane
*wp
;
560 if (!(w
->flags
& WINDOW_ZOOMED
))
563 w
->flags
&= ~WINDOW_ZOOMED
;
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
);
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
);
585 TAILQ_INSERT_AFTER(&w
->panes
, w
->active
, wp
, entry
);
590 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
592 if (wp
== w
->active
) {
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
)
603 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
604 window_pane_destroy(wp
);
608 window_pane_at_index(struct window
*w
, u_int idx
)
610 struct window_pane
*wp
;
613 n
= options_get_number(&w
->options
, "pane-base-index");
614 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
623 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
626 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
627 wp
= TAILQ_FIRST(&w
->panes
);
634 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
638 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
639 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
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
) {
663 window_count_panes(struct window
*w
)
665 struct window_pane
*wp
;
669 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
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. */
688 window_printable_flags(struct session
*s
, struct winlink
*wl
)
694 if (wl
->flags
& WINLINK_ACTIVITY
)
696 if (wl
->flags
& WINLINK_BELL
)
698 if (wl
->flags
& WINLINK_CONTENT
)
700 if (wl
->flags
& WINLINK_SILENCE
)
704 if (wl
== TAILQ_FIRST(&s
->lastw
))
706 if (wl
->window
->flags
& WINDOW_ZOOMED
)
711 return (xstrdup(flags
));
714 /* Find pane in global tree by id. */
716 window_pane_find_by_id(u_int id
)
718 struct window_pane wp
;
721 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
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
);
732 wp
->id
= next_window_pane_id
++;
733 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
744 wp
->layout_cell
= NULL
;
754 wp
->pipe_event
= NULL
;
756 wp
->saved_grid
= NULL
;
758 screen_init(&wp
->base
, sx
, sy
, hlimit
);
759 wp
->screen
= &wp
->base
;
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
);
785 bufferevent_free(wp
->event
);
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
);
800 RB_REMOVE(window_pane_tree
, &all_window_panes
, 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
)
813 char *argv0
, paneid
[16];
818 bufferevent_free(wp
->event
);
823 wp
->cmd
= xstrdup(cmd
);
827 wp
->shell
= xstrdup(shell
);
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
)) {
843 xasprintf(cause
, "%s: %s", cmd
, strerror(errno
));
846 if (fchdir(wp
->cwd
) != 0)
849 if (tcgetattr(STDIN_FILENO
, &tio2
) != 0)
850 fatal("tcgetattr failed");
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
);
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);
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);
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
);
898 window_pane_timer_start(struct window_pane
*wp
)
905 evtimer_del(&wp
->changes_timer
);
906 evtimer_set(&wp
->changes_timer
, window_pane_timer_callback
, wp
);
907 evtimer_add(&wp
->changes_timer
, &tv
);
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
;
930 window_pane_timer_start(wp
);
935 window_pane_read_callback(unused
struct bufferevent
*bufev
, void *data
)
937 struct window_pane
*wp
= data
;
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
);
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.");
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
);
970 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
972 if (sx
== wp
->sx
&& sy
== wp
->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
989 window_pane_alternate_on(struct window_pane
*wp
, struct grid_cell
*gc
,
992 struct screen
*s
= &wp
->base
;
995 if (wp
->saved_grid
!= NULL
)
997 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
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
);
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. */
1019 window_pane_alternate_off(struct window_pane
*wp
, struct grid_cell
*gc
,
1022 struct screen
*s
= &wp
->base
;
1025 if (wp
->saved_grid
== NULL
)
1027 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
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
);
1042 s
->cx
= wp
->saved_cx
;
1043 if (s
->cx
> screen_size_x(s
) - 1)
1044 s
->cx
= screen_size_x(s
) - 1;
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
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
)
1070 if (wp
->mode
!= NULL
)
1074 if ((s
= wp
->mode
->init(wp
)) != NULL
)
1076 wp
->flags
|= PANE_REDRAW
;
1081 window_pane_reset_mode(struct window_pane
*wp
)
1083 if (wp
->mode
== NULL
)
1089 wp
->screen
= &wp
->base
;
1090 wp
->flags
|= PANE_REDRAW
;
1094 window_pane_key(struct window_pane
*wp
, struct session
*sess
, int key
)
1096 struct window_pane
*wp2
;
1098 if (!window_pane_visible(wp
))
1101 if (wp
->mode
!= NULL
) {
1102 if (wp
->mode
->key
!= NULL
)
1103 wp
->mode
->key(wp
, sess
, 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
)
1114 if (wp2
->fd
!= -1 && window_pane_visible(wp2
))
1115 input_key(wp2
, key
);
1122 struct window_pane
*wp
, struct session
*sess
, struct mouse_event
*m
)
1124 if (!window_pane_visible(wp
))
1127 if (m
->x
< wp
->xoff
|| m
->x
>= wp
->xoff
+ wp
->sx
)
1129 if (m
->y
< wp
->yoff
|| m
->y
>= wp
->yoff
+ wp
->sy
)
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
)
1149 if (wp
->xoff
>= w
->sx
|| wp
->yoff
>= w
->sy
)
1151 if (wp
->xoff
+ wp
->sx
> w
->sx
|| wp
->yoff
+ wp
->sy
> w
->sy
)
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
;
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) {
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
;
1188 if (wp
== NULL
|| !window_pane_visible(wp
))
1193 top
= wp
->window
->sy
+ 1;
1196 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1197 if (!window_pane_visible(wp2
))
1199 if (wp2
->yoff
+ wp2
->sy
+ 1 != top
)
1201 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
1202 return (window_pane_active_set(wp
, wp2
));
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
;
1214 if (wp
== NULL
|| !window_pane_visible(wp
))
1217 bottom
= wp
->yoff
+ wp
->sy
+ 1;
1218 if (bottom
>= wp
->window
->sy
)
1222 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1223 if (!window_pane_visible(wp2
))
1225 if (wp2
->yoff
!= bottom
)
1227 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
1228 return (window_pane_active_set(wp
, wp2
));
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
;
1243 if (wp
== NULL
|| !window_pane_visible(wp
))
1248 left
= wp
->window
->sx
+ 1;
1251 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1252 if (!window_pane_visible(wp2
))
1254 if (wp2
->xoff
+ wp2
->sx
+ 1 != left
)
1256 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
1257 return (window_pane_active_set(wp
, wp2
));
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
;
1272 if (wp
== NULL
|| !window_pane_visible(wp
))
1275 right
= wp
->xoff
+ wp
->sx
+ 1;
1276 if (right
>= wp
->window
->sx
)
1280 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1281 if (!window_pane_visible(wp2
))
1283 if (wp2
->xoff
!= right
)
1285 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
1286 return (window_pane_active_set(wp
, wp2
));
1291 /* Clear alert flags for a winlink */
1293 winlink_clear_flags(struct winlink
*wl
)
1300 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
1301 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
1304 RB_FOREACH(s
, sessions
, &sessions
) {
1305 if ((wm
= session_has(s
, w
)) == NULL
)
1308 if (wm
->window
!= wl
->window
)
1310 if ((wm
->flags
& WINLINK_ALERTFLAGS
) == 0)
1313 wm
->flags
&= ~WINLINK_ALERTFLAGS
;
1314 wm
->window
->flags
&= ~WINDOW_ALERTFLAGS
;
1315 server_status_session(s
);