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>
35 * Each window is attached to a number of panes, each of which is a pty. This
36 * file contains code to handle them.
38 * A pane has two buffers attached, these are filled and emptied by the main
39 * server poll loop. Output data is received from pty's in screen format,
40 * translated and returned as a series of escape sequences and strings via
41 * input_parse (in input.c). Input data is received as key codes and written
42 * directly via input_key.
44 * Each pane also has a "virtual" screen (screen.c) which contains the current
45 * state and is redisplayed when the window is reattached to a client.
47 * Windows are stored directly on a global array and wrapped in any number of
48 * winlink structs to be linked onto local session RB trees. A reference count
49 * is maintained and a window removed from the global list and destroyed when
53 /* Global window list. */
54 struct windows windows
;
56 /* Global panes tree. */
57 struct window_pane_tree all_window_panes
;
58 u_int next_window_pane_id
;
61 struct window_pane
*window_pane_active_set(struct window_pane
*,
62 struct window_pane
*);
63 void window_pane_active_lost(struct window_pane
*, struct window_pane
*);
65 void window_pane_timer_callback(int, short, void *);
66 void window_pane_read_callback(struct bufferevent
*, void *);
67 void window_pane_error_callback(struct bufferevent
*, short, void *);
69 RB_GENERATE(winlinks
, winlink
, entry
, winlink_cmp
);
72 winlink_cmp(struct winlink
*wl1
, struct winlink
*wl2
)
74 return (wl1
->idx
- wl2
->idx
);
77 RB_GENERATE(window_pane_tree
, window_pane
, tree_entry
, window_pane_cmp
);
80 window_pane_cmp(struct window_pane
*wp1
, struct window_pane
*wp2
)
82 return (wp1
->id
- wp2
->id
);
86 winlink_find_by_window(struct winlinks
*wwl
, struct window
*w
)
90 RB_FOREACH(wl
, winlinks
, wwl
) {
99 winlink_find_by_index(struct winlinks
*wwl
, int idx
)
107 return (RB_FIND(winlinks
, wwl
, &wl
));
111 winlink_find_by_window_id(struct winlinks
*wwl
, u_int id
)
115 RB_FOREACH(wl
, winlinks
, wwl
) {
116 if (wl
->window
->id
== id
)
123 winlink_next_index(struct winlinks
*wwl
, int idx
)
129 if (winlink_find_by_index(wwl
, i
) == NULL
)
140 winlink_count(struct winlinks
*wwl
)
146 RB_FOREACH(wl
, winlinks
, wwl
)
153 winlink_add(struct winlinks
*wwl
, int idx
)
158 if ((idx
= winlink_next_index(wwl
, -idx
- 1)) == -1)
160 } else if (winlink_find_by_index(wwl
, idx
) != NULL
)
163 wl
= xcalloc(1, sizeof *wl
);
165 RB_INSERT(winlinks
, wwl
, wl
);
171 winlink_set_window(struct winlink
*wl
, struct window
*w
)
178 winlink_remove(struct winlinks
*wwl
, struct winlink
*wl
)
180 struct window
*w
= wl
->window
;
182 RB_REMOVE(winlinks
, wwl
, wl
);
183 free(wl
->status_text
);
187 window_remove_ref(w
);
191 winlink_next(struct winlink
*wl
)
193 return (RB_NEXT(winlinks
, wwl
, wl
));
197 winlink_previous(struct winlink
*wl
)
199 return (RB_PREV(winlinks
, wwl
, wl
));
203 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
206 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
207 wl
= RB_MIN(winlinks
, &s
->windows
);
214 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
217 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
218 wl
= RB_MAX(winlinks
, &s
->windows
);
225 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
230 winlink_stack_remove(stack
, wl
);
231 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
235 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
242 TAILQ_FOREACH(wl2
, stack
, sentry
) {
244 TAILQ_REMOVE(stack
, wl
, sentry
);
251 window_index(struct window
*s
, u_int
*i
)
253 for (*i
= 0; *i
< ARRAY_LENGTH(&windows
); (*i
)++) {
254 if (s
== ARRAY_ITEM(&windows
, *i
))
261 window_find_by_id(u_int id
)
266 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
267 w
= ARRAY_ITEM(&windows
, i
);
275 window_create1(u_int sx
, u_int sy
)
280 w
= xcalloc(1, sizeof *w
);
281 w
->id
= next_window_id
++;
285 TAILQ_INIT(&w
->panes
);
289 w
->layout_root
= NULL
;
294 options_init(&w
->options
, &global_w_options
);
295 if (options_get_number(&w
->options
, "automatic-rename"))
296 queue_window_name(w
);
298 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
299 if (ARRAY_ITEM(&windows
, i
) == NULL
) {
300 ARRAY_SET(&windows
, i
, w
);
304 if (i
== ARRAY_LENGTH(&windows
))
305 ARRAY_ADD(&windows
, w
);
312 window_create(const char *name
, const char *cmd
, const char *shell
,
313 int cwd
, struct environ
*env
, struct termios
*tio
,
314 u_int sx
, u_int sy
, u_int hlimit
, char **cause
)
317 struct window_pane
*wp
;
319 w
= window_create1(sx
, sy
);
320 wp
= window_add_pane(w
, hlimit
);
323 if (window_pane_spawn(wp
, cmd
, shell
, cwd
, env
, tio
, cause
) != 0) {
328 w
->active
= TAILQ_FIRST(&w
->panes
);
330 w
->name
= xstrdup(name
);
331 options_set_number(&w
->options
, "automatic-rename", 0);
333 w
->name
= default_window_name(w
);
339 window_destroy(struct window
*w
)
345 if (window_index(w
, &i
) != 0)
346 fatalx("index not found");
347 ARRAY_SET(&windows
, i
, NULL
);
348 while (!ARRAY_EMPTY(&windows
) && ARRAY_LAST(&windows
) == NULL
)
349 ARRAY_TRUNC(&windows
, 1);
351 if (w
->layout_root
!= NULL
)
354 if (event_initialized(&w
->name_timer
))
355 evtimer_del(&w
->name_timer
);
357 options_free(&w
->options
);
359 window_destroy_panes(w
);
366 window_remove_ref(struct window
*w
)
368 if (w
->references
== 0)
369 fatal("bad reference count");
371 if (w
->references
== 0)
376 window_set_name(struct window
*w
, const char *new_name
)
379 w
->name
= xstrdup(new_name
);
380 notify_window_renamed(w
);
384 window_resize(struct window
*w
, u_int sx
, u_int sy
)
391 * Restore previously active pane when changing from wp to nextwp. The intended
392 * pane is in nextwp and it returns the previously focused pane.
395 window_pane_active_set(struct window_pane
*wp
, struct window_pane
*nextwp
)
397 struct layout_cell
*lc
;
398 struct window_pane
*lastwp
;
400 /* Target pane's parent must not be an ancestor of source pane. */
401 for (lc
= wp
->layout_cell
->parent
; lc
!= NULL
; lc
= lc
->parent
) {
402 if (lc
== nextwp
->layout_cell
->parent
)
407 * Previously active pane, if any, must not be the same as the source
410 lc
= nextwp
->layout_cell
->parent
;
411 if (lc
!= NULL
&& lc
->lastwp
!= NULL
) {
413 if (lastwp
!= wp
&& window_pane_visible(lastwp
))
419 /* Remember previously active pane when changing from wp to nextwp. */
421 window_pane_active_lost(struct window_pane
*wp
, struct window_pane
*nextwp
)
423 struct layout_cell
*lc
, *lc2
;
425 /* Save the target pane in its parent. */
426 nextwp
->layout_cell
->parent
->lastwp
= nextwp
;
429 * Save the source pane in all of its parents up to, but not including,
430 * the common ancestor of itself and the target panes.
434 for (lc
= wp
->layout_cell
->parent
; lc
!= NULL
; lc
= lc
->parent
) {
435 lc2
= nextwp
->layout_cell
->parent
;
436 for (; lc2
!= NULL
; lc2
= lc2
->parent
) {
445 window_set_active_pane(struct window
*w
, struct window_pane
*wp
)
451 window_pane_active_lost(w
->last
, wp
);
452 while (!window_pane_visible(w
->active
)) {
453 w
->active
= TAILQ_PREV(w
->active
, window_panes
, entry
);
454 if (w
->active
== NULL
)
455 w
->active
= TAILQ_LAST(&w
->panes
, window_panes
);
462 window_get_active_at(struct window
*w
, u_int x
, u_int y
)
464 struct window_pane
*wp
;
466 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
467 if (!window_pane_visible(wp
))
469 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
471 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
479 window_set_active_at(struct window
*w
, u_int x
, u_int y
)
481 struct window_pane
*wp
;
483 wp
= window_get_active_at(w
, x
, y
);
484 if (wp
!= NULL
&& wp
!= w
->active
)
485 window_set_active_pane(w
, wp
);
489 window_find_string(struct window
*w
, const char *s
)
496 if (strcasecmp(s
, "top") == 0)
498 else if (strcasecmp(s
, "bottom") == 0)
500 else if (strcasecmp(s
, "left") == 0)
502 else if (strcasecmp(s
, "right") == 0)
504 else if (strcasecmp(s
, "top-left") == 0) {
507 } else if (strcasecmp(s
, "top-right") == 0) {
510 } else if (strcasecmp(s
, "bottom-left") == 0) {
513 } else if (strcasecmp(s
, "bottom-right") == 0) {
519 return (window_get_active_at(w
, x
, y
));
523 window_zoom(struct window_pane
*wp
)
525 struct window
*w
= wp
->window
;
526 struct window_pane
*wp1
;
528 if (w
->flags
& WINDOW_ZOOMED
)
531 if (!window_pane_visible(wp
))
534 if (window_count_panes(w
) == 1)
538 window_set_active_pane(w
, wp
);
540 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
541 wp1
->saved_layout_cell
= wp1
->layout_cell
;
542 wp1
->layout_cell
= NULL
;
545 w
->saved_layout_root
= w
->layout_root
;
547 w
->flags
|= WINDOW_ZOOMED
;
553 window_unzoom(struct window
*w
)
555 struct window_pane
*wp
;
557 if (!(w
->flags
& WINDOW_ZOOMED
))
560 w
->flags
&= ~WINDOW_ZOOMED
;
562 w
->layout_root
= w
->saved_layout_root
;
564 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
565 wp
->layout_cell
= wp
->saved_layout_cell
;
566 wp
->saved_layout_cell
= NULL
;
568 layout_fix_panes(w
, w
->sx
, w
->sy
);
574 window_add_pane(struct window
*w
, u_int hlimit
)
576 struct window_pane
*wp
;
578 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
579 if (TAILQ_EMPTY(&w
->panes
))
580 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
582 TAILQ_INSERT_AFTER(&w
->panes
, w
->active
, wp
, entry
);
587 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
589 if (wp
== w
->active
) {
592 if (w
->active
== NULL
) {
593 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
594 if (w
->active
== NULL
)
595 w
->active
= TAILQ_NEXT(wp
, entry
);
597 } else if (wp
== w
->last
)
600 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
601 window_pane_destroy(wp
);
605 window_pane_at_index(struct window
*w
, u_int idx
)
607 struct window_pane
*wp
;
610 n
= options_get_number(&w
->options
, "pane-base-index");
611 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
620 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
623 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
624 wp
= TAILQ_FIRST(&w
->panes
);
631 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
635 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
636 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
643 window_pane_index(struct window_pane
*wp
, u_int
*i
)
645 struct window_pane
*wq
;
646 struct window
*w
= wp
->window
;
648 *i
= options_get_number(&w
->options
, "pane-base-index");
649 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
660 window_count_panes(struct window
*w
)
662 struct window_pane
*wp
;
666 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
672 window_destroy_panes(struct window
*w
)
674 struct window_pane
*wp
;
676 while (!TAILQ_EMPTY(&w
->panes
)) {
677 wp
= TAILQ_FIRST(&w
->panes
);
678 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
679 window_pane_destroy(wp
);
683 /* Return list of printable window flag symbols. No flags is just a space. */
685 window_printable_flags(struct session
*s
, struct winlink
*wl
)
691 if (wl
->flags
& WINLINK_ACTIVITY
)
693 if (wl
->flags
& WINLINK_BELL
)
695 if (wl
->flags
& WINLINK_CONTENT
)
697 if (wl
->flags
& WINLINK_SILENCE
)
701 if (wl
== TAILQ_FIRST(&s
->lastw
))
703 if (wl
->window
->flags
& WINDOW_ZOOMED
)
708 return (xstrdup(flags
));
711 /* Find pane in global tree by id. */
713 window_pane_find_by_id(u_int id
)
715 struct window_pane wp
;
718 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
722 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
724 struct window_pane
*wp
;
726 wp
= xcalloc(1, sizeof *wp
);
729 wp
->id
= next_window_pane_id
++;
730 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
741 wp
->layout_cell
= NULL
;
751 wp
->pipe_event
= NULL
;
753 wp
->saved_grid
= NULL
;
755 screen_init(&wp
->base
, sx
, sy
, hlimit
);
756 wp
->screen
= &wp
->base
;
764 window_pane_destroy(struct window_pane
*wp
)
766 struct window_pane
*wp2
;
768 /* Forget removed pane in all layout cells that remember it. */
769 RB_FOREACH(wp2
, window_pane_tree
, &all_window_panes
) {
770 if (wp2
->layout_cell
!= NULL
&&
771 wp2
->layout_cell
->parent
!= NULL
&&
772 wp2
->layout_cell
->parent
->lastwp
== wp
)
773 wp2
->layout_cell
->parent
->lastwp
= NULL
;
776 window_pane_reset_mode(wp
);
778 if (event_initialized(&wp
->changes_timer
))
779 evtimer_del(&wp
->changes_timer
);
782 bufferevent_free(wp
->event
);
788 screen_free(&wp
->base
);
789 if (wp
->saved_grid
!= NULL
)
790 grid_destroy(wp
->saved_grid
);
792 if (wp
->pipe_fd
!= -1) {
793 bufferevent_free(wp
->pipe_event
);
797 RB_REMOVE(window_pane_tree
, &all_window_panes
, wp
);
799 utmp_destroy(wp
->utmp
);
807 window_pane_spawn(struct window_pane
*wp
, const char *cmd
, const char *shell
,
808 int cwd
, struct environ
*env
, struct termios
*tio
, char **cause
)
811 char *argv0
, paneid
[16];
816 bufferevent_free(wp
->event
);
821 wp
->cmd
= xstrdup(cmd
);
825 wp
->shell
= xstrdup(shell
);
832 log_debug("spawn: %s -- %s", wp
->shell
, wp
->cmd
);
834 memset(&ws
, 0, sizeof ws
);
835 ws
.ws_col
= screen_size_x(&wp
->base
);
836 ws
.ws_row
= screen_size_y(&wp
->base
);
838 switch (wp
->pid
= forkpty(&wp
->fd
, wp
->tty
, NULL
, &ws
)) {
841 xasprintf(cause
, "%s: %s", cmd
, strerror(errno
));
844 if (fchdir(wp
->cwd
) != 0)
847 if (tcgetattr(STDIN_FILENO
, &tio2
) != 0)
848 fatal("tcgetattr failed");
850 memcpy(tio2
.c_cc
, tio
->c_cc
, sizeof tio2
.c_cc
);
851 tio2
.c_cc
[VERASE
] = '\177';
853 if (options_get_number(&wp
->window
->options
, "utf8"))
854 tio2
.c_iflag
|= IUTF8
;
856 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tio2
) != 0)
857 fatal("tcgetattr failed");
859 closefrom(STDERR_FILENO
+ 1);
861 xsnprintf(paneid
, sizeof paneid
, "%%%u", wp
->id
);
862 environ_set(env
, "TMUX_PANE", paneid
);
868 setenv("SHELL", wp
->shell
, 1);
869 ptr
= strrchr(wp
->shell
, '/');
871 if (*wp
->cmd
!= '\0') {
872 /* Use the command. */
873 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
874 xasprintf(&argv0
, "%s", ptr
+ 1);
876 xasprintf(&argv0
, "%s", wp
->shell
);
877 execl(wp
->shell
, argv0
, "-c", wp
->cmd
, (char *) NULL
);
878 fatal("execl failed");
881 /* No command; fork a login shell. */
882 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
883 xasprintf(&argv0
, "-%s", ptr
+ 1);
885 xasprintf(&argv0
, "-%s", wp
->shell
);
886 execl(wp
->shell
, argv0
, (char *) NULL
);
887 fatal("execl failed");
890 setblocking(wp
->fd
, 0);
892 wp
->event
= bufferevent_new(wp
->fd
,
893 window_pane_read_callback
, NULL
, window_pane_error_callback
, wp
);
894 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
896 wp
->utmp
= utmp_create(wp
->tty
);
902 window_pane_timer_start(struct window_pane
*wp
)
909 evtimer_del(&wp
->changes_timer
);
910 evtimer_set(&wp
->changes_timer
, window_pane_timer_callback
, wp
);
911 evtimer_add(&wp
->changes_timer
, &tv
);
915 window_pane_timer_callback(unused
int fd
, unused
short events
, void *data
)
917 struct window_pane
*wp
= data
;
918 struct window
*w
= wp
->window
;
919 u_int interval
, trigger
;
921 interval
= options_get_number(&w
->options
, "c0-change-interval");
922 trigger
= options_get_number(&w
->options
, "c0-change-trigger");
924 if (wp
->changes_redraw
++ == interval
) {
925 wp
->flags
|= PANE_REDRAW
;
926 wp
->changes_redraw
= 0;
930 if (trigger
== 0 || wp
->changes
< trigger
) {
931 wp
->flags
|= PANE_REDRAW
;
932 wp
->flags
&= ~PANE_DROP
;
934 window_pane_timer_start(wp
);
939 window_pane_read_callback(unused
struct bufferevent
*bufev
, void *data
)
941 struct window_pane
*wp
= data
;
945 new_size
= EVBUFFER_LENGTH(wp
->event
->input
) - wp
->pipe_off
;
946 if (wp
->pipe_fd
!= -1 && new_size
> 0) {
947 new_data
= (char *)EVBUFFER_DATA(wp
->event
->input
);
948 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
953 wp
->pipe_off
= EVBUFFER_LENGTH(wp
->event
->input
);
956 * If we get here, we're not outputting anymore, so set the silence
957 * flag on the window.
959 wp
->window
->flags
|= WINDOW_SILENCE
;
960 if (gettimeofday(&wp
->window
->silence_timer
, NULL
) != 0)
961 fatal("gettimeofday failed.");
965 window_pane_error_callback(
966 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
968 struct window_pane
*wp
= data
;
970 server_destroy_pane(wp
);
974 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
976 if (sx
== wp
->sx
&& sy
== wp
->sy
)
981 screen_resize(&wp
->base
, sx
, sy
, wp
->saved_grid
== NULL
);
982 if (wp
->mode
!= NULL
)
983 wp
->mode
->resize(wp
, sx
, sy
);
985 wp
->flags
|= PANE_RESIZE
;
989 * Enter alternative screen mode. A copy of the visible screen is saved and the
990 * history is not updated
993 window_pane_alternate_on(struct window_pane
*wp
, struct grid_cell
*gc
,
996 struct screen
*s
= &wp
->base
;
999 if (wp
->saved_grid
!= NULL
)
1001 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
1003 sx
= screen_size_x(s
);
1004 sy
= screen_size_y(s
);
1006 wp
->saved_grid
= grid_create(sx
, sy
, 0);
1007 grid_duplicate_lines(wp
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
1009 wp
->saved_cx
= s
->cx
;
1010 wp
->saved_cy
= s
->cy
;
1012 memcpy(&wp
->saved_cell
, gc
, sizeof wp
->saved_cell
);
1014 grid_view_clear(s
->grid
, 0, 0, sx
, sy
);
1016 wp
->base
.grid
->flags
&= ~GRID_HISTORY
;
1018 wp
->flags
|= PANE_REDRAW
;
1021 /* Exit alternate screen mode and restore the copied grid. */
1023 window_pane_alternate_off(struct window_pane
*wp
, struct grid_cell
*gc
,
1026 struct screen
*s
= &wp
->base
;
1029 if (wp
->saved_grid
== NULL
)
1031 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
1033 sx
= screen_size_x(s
);
1034 sy
= screen_size_y(s
);
1037 * If the current size is bigger, temporarily resize to the old size
1038 * before copying back.
1040 if (sy
> wp
->saved_grid
->sy
)
1041 screen_resize(s
, sx
, wp
->saved_grid
->sy
, 1);
1043 /* Restore the grid, cursor position and cell. */
1044 grid_duplicate_lines(s
->grid
, screen_hsize(s
), wp
->saved_grid
, 0, sy
);
1046 s
->cx
= wp
->saved_cx
;
1047 if (s
->cx
> screen_size_x(s
) - 1)
1048 s
->cx
= screen_size_x(s
) - 1;
1050 s
->cy
= wp
->saved_cy
;
1051 if (s
->cy
> screen_size_y(s
) - 1)
1052 s
->cy
= screen_size_y(s
) - 1;
1053 memcpy(gc
, &wp
->saved_cell
, sizeof *gc
);
1056 * Turn history back on (so resize can use it) and then resize back to
1059 wp
->base
.grid
->flags
|= GRID_HISTORY
;
1060 if (sy
> wp
->saved_grid
->sy
|| sx
!= wp
->saved_grid
->sx
)
1061 screen_resize(s
, sx
, sy
, 1);
1063 grid_destroy(wp
->saved_grid
);
1064 wp
->saved_grid
= NULL
;
1066 wp
->flags
|= PANE_REDRAW
;
1070 window_pane_set_mode(struct window_pane
*wp
, const struct window_mode
*mode
)
1074 if (wp
->mode
!= NULL
)
1078 if ((s
= wp
->mode
->init(wp
)) != NULL
)
1080 wp
->flags
|= PANE_REDRAW
;
1085 window_pane_reset_mode(struct window_pane
*wp
)
1087 if (wp
->mode
== NULL
)
1093 wp
->screen
= &wp
->base
;
1094 wp
->flags
|= PANE_REDRAW
;
1098 window_pane_key(struct window_pane
*wp
, struct session
*sess
, int key
)
1100 struct window_pane
*wp2
;
1102 if (!window_pane_visible(wp
))
1105 if (wp
->mode
!= NULL
) {
1106 if (wp
->mode
->key
!= NULL
)
1107 wp
->mode
->key(wp
, sess
, key
);
1114 if (options_get_number(&wp
->window
->options
, "synchronize-panes")) {
1115 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1116 if (wp2
== wp
|| wp2
->mode
!= NULL
)
1118 if (wp2
->fd
!= -1 && window_pane_visible(wp2
))
1119 input_key(wp2
, key
);
1126 struct window_pane
*wp
, struct session
*sess
, struct mouse_event
*m
)
1128 if (!window_pane_visible(wp
))
1131 if (m
->x
< wp
->xoff
|| m
->x
>= wp
->xoff
+ wp
->sx
)
1133 if (m
->y
< wp
->yoff
|| m
->y
>= wp
->yoff
+ wp
->sy
)
1138 if (wp
->mode
!= NULL
) {
1139 if (wp
->mode
->mouse
!= NULL
&&
1140 options_get_number(&wp
->window
->options
, "mode-mouse"))
1141 wp
->mode
->mouse(wp
, sess
, m
);
1142 } else if (wp
->fd
!= -1)
1143 input_mouse(wp
, sess
, m
);
1147 window_pane_visible(struct window_pane
*wp
)
1149 struct window
*w
= wp
->window
;
1151 if (wp
->layout_cell
== NULL
)
1153 if (wp
->xoff
>= w
->sx
|| wp
->yoff
>= w
->sy
)
1155 if (wp
->xoff
+ wp
->sx
> w
->sx
|| wp
->yoff
+ wp
->sy
> w
->sy
)
1161 window_pane_search(struct window_pane
*wp
, const char *searchstr
, u_int
*lineno
)
1163 struct screen
*s
= &wp
->base
;
1164 char *newsearchstr
, *line
, *msg
;
1168 xasprintf(&newsearchstr
, "*%s*", searchstr
);
1170 for (i
= 0; i
< screen_size_y(s
); i
++) {
1171 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
1172 if (fnmatch(newsearchstr
, line
, 0) == 0) {
1185 /* Find the pane directly above another. */
1186 struct window_pane
*
1187 window_pane_find_up(struct window_pane
*wp
)
1189 struct window_pane
*wp2
;
1192 if (wp
== NULL
|| !window_pane_visible(wp
))
1197 top
= wp
->window
->sy
+ 1;
1200 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1201 if (!window_pane_visible(wp2
))
1203 if (wp2
->yoff
+ wp2
->sy
+ 1 != top
)
1205 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
1206 return (window_pane_active_set(wp
, wp2
));
1211 /* Find the pane directly below another. */
1212 struct window_pane
*
1213 window_pane_find_down(struct window_pane
*wp
)
1215 struct window_pane
*wp2
;
1218 if (wp
== NULL
|| !window_pane_visible(wp
))
1221 bottom
= wp
->yoff
+ wp
->sy
+ 1;
1222 if (bottom
>= wp
->window
->sy
)
1226 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1227 if (!window_pane_visible(wp2
))
1229 if (wp2
->yoff
!= bottom
)
1231 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
1232 return (window_pane_active_set(wp
, wp2
));
1238 * Find the pane directly to the left of another, adjacent to the left side and
1239 * containing the top edge.
1241 struct window_pane
*
1242 window_pane_find_left(struct window_pane
*wp
)
1244 struct window_pane
*wp2
;
1247 if (wp
== NULL
|| !window_pane_visible(wp
))
1252 left
= wp
->window
->sx
+ 1;
1255 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1256 if (!window_pane_visible(wp2
))
1258 if (wp2
->xoff
+ wp2
->sx
+ 1 != left
)
1260 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
1261 return (window_pane_active_set(wp
, wp2
));
1267 * Find the pane directly to the right of another, that is adjacent to the
1268 * right edge and including the top edge.
1270 struct window_pane
*
1271 window_pane_find_right(struct window_pane
*wp
)
1273 struct window_pane
*wp2
;
1276 if (wp
== NULL
|| !window_pane_visible(wp
))
1279 right
= wp
->xoff
+ wp
->sx
+ 1;
1280 if (right
>= wp
->window
->sx
)
1284 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1285 if (!window_pane_visible(wp2
))
1287 if (wp2
->xoff
!= right
)
1289 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
1290 return (window_pane_active_set(wp
, wp2
));
1295 /* Clear alert flags for a winlink */
1297 winlink_clear_flags(struct winlink
*wl
)
1304 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
1305 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
1308 RB_FOREACH(s
, sessions
, &sessions
) {
1309 if ((wm
= session_has(s
, w
)) == NULL
)
1312 if (wm
->window
!= wl
->window
)
1314 if ((wm
->flags
& WINLINK_ALERTFLAGS
) == 0)
1317 wm
->flags
&= ~WINLINK_ALERTFLAGS
;
1318 wm
->window
->flags
&= ~WINDOW_ALERTFLAGS
;
1319 server_status_session(s
);