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 void window_pane_timer_callback(int, short, void *);
65 void window_pane_read_callback(struct bufferevent
*, void *);
66 void window_pane_error_callback(struct bufferevent
*, short, void *);
68 RB_GENERATE(winlinks
, winlink
, entry
, winlink_cmp
);
71 winlink_cmp(struct winlink
*wl1
, struct winlink
*wl2
)
73 return (wl1
->idx
- wl2
->idx
);
76 RB_GENERATE(window_pane_tree
, window_pane
, tree_entry
, window_pane_cmp
);
79 window_pane_cmp(struct window_pane
*wp1
, struct window_pane
*wp2
)
81 return (wp1
->id
- wp2
->id
);
85 winlink_find_by_window(struct winlinks
*wwl
, struct window
*w
)
89 RB_FOREACH(wl
, winlinks
, wwl
) {
98 winlink_find_by_index(struct winlinks
*wwl
, int idx
)
106 return (RB_FIND(winlinks
, wwl
, &wl
));
110 winlink_find_by_window_id(struct winlinks
*wwl
, u_int id
)
114 RB_FOREACH(wl
, winlinks
, wwl
) {
115 if (wl
->window
->id
== id
)
122 winlink_next_index(struct winlinks
*wwl
, int idx
)
128 if (winlink_find_by_index(wwl
, i
) == NULL
)
139 winlink_count(struct winlinks
*wwl
)
145 RB_FOREACH(wl
, winlinks
, wwl
)
152 winlink_add(struct winlinks
*wwl
, int idx
)
157 if ((idx
= winlink_next_index(wwl
, -idx
- 1)) == -1)
159 } else if (winlink_find_by_index(wwl
, idx
) != NULL
)
162 wl
= xcalloc(1, sizeof *wl
);
164 RB_INSERT(winlinks
, wwl
, wl
);
170 winlink_set_window(struct winlink
*wl
, struct window
*w
)
177 winlink_remove(struct winlinks
*wwl
, struct winlink
*wl
)
179 struct window
*w
= wl
->window
;
181 RB_REMOVE(winlinks
, wwl
, wl
);
182 free(wl
->status_text
);
186 window_remove_ref(w
);
190 winlink_next(struct winlink
*wl
)
192 return (RB_NEXT(winlinks
, wwl
, wl
));
196 winlink_previous(struct winlink
*wl
)
198 return (RB_PREV(winlinks
, wwl
, wl
));
202 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
205 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
206 wl
= RB_MIN(winlinks
, &s
->windows
);
213 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
216 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
217 wl
= RB_MAX(winlinks
, &s
->windows
);
224 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
229 winlink_stack_remove(stack
, wl
);
230 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
234 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
241 TAILQ_FOREACH(wl2
, stack
, sentry
) {
243 TAILQ_REMOVE(stack
, wl
, sentry
);
250 window_index(struct window
*s
, u_int
*i
)
252 for (*i
= 0; *i
< ARRAY_LENGTH(&windows
); (*i
)++) {
253 if (s
== ARRAY_ITEM(&windows
, *i
))
260 window_find_by_id(u_int id
)
265 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
266 w
= ARRAY_ITEM(&windows
, i
);
274 window_create1(u_int sx
, u_int sy
)
279 w
= xcalloc(1, sizeof *w
);
280 w
->id
= next_window_id
++;
284 TAILQ_INIT(&w
->panes
);
288 w
->layout_root
= NULL
;
293 options_init(&w
->options
, &global_w_options
);
294 if (options_get_number(&w
->options
, "automatic-rename"))
295 queue_window_name(w
);
297 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
298 if (ARRAY_ITEM(&windows
, i
) == NULL
) {
299 ARRAY_SET(&windows
, i
, w
);
303 if (i
== ARRAY_LENGTH(&windows
))
304 ARRAY_ADD(&windows
, w
);
311 window_create(const char *name
, const char *cmd
, const char *shell
,
312 const char *cwd
, struct environ
*env
, struct termios
*tio
,
313 u_int sx
, u_int sy
, u_int hlimit
, char **cause
)
316 struct window_pane
*wp
;
320 w
= window_create1(sx
, sy
);
321 wp
= window_add_pane(w
, hlimit
);
325 prefix
= options_get_string(&w
->options
, "command-prefix");
326 xasprintf(&cmd1
, "%s%s", prefix
, cmd
);
329 if (window_pane_spawn(wp
, cmd1
, shell
, cwd
, env
, tio
, cause
) != 0) {
336 w
->active
= TAILQ_FIRST(&w
->panes
);
338 w
->name
= xstrdup(name
);
339 options_set_number(&w
->options
, "automatic-rename", 0);
341 w
->name
= default_window_name(w
);
347 window_destroy(struct window
*w
)
353 if (window_index(w
, &i
) != 0)
354 fatalx("index not found");
355 ARRAY_SET(&windows
, i
, NULL
);
356 while (!ARRAY_EMPTY(&windows
) && ARRAY_LAST(&windows
) == NULL
)
357 ARRAY_TRUNC(&windows
, 1);
359 if (w
->layout_root
!= NULL
)
362 if (event_initialized(&w
->name_timer
))
363 evtimer_del(&w
->name_timer
);
365 options_free(&w
->options
);
367 window_destroy_panes(w
);
374 window_remove_ref(struct window
*w
)
376 if (w
->references
== 0)
377 fatal("bad reference count");
379 if (w
->references
== 0)
384 window_set_name(struct window
*w
, const char *new_name
)
387 w
->name
= xstrdup(new_name
);
388 notify_window_renamed(w
);
392 window_resize(struct window
*w
, u_int sx
, u_int sy
)
399 window_set_active_pane(struct window
*w
, struct window_pane
*wp
)
405 while (!window_pane_visible(w
->active
)) {
406 w
->active
= TAILQ_PREV(w
->active
, window_panes
, entry
);
407 if (w
->active
== NULL
)
408 w
->active
= TAILQ_LAST(&w
->panes
, window_panes
);
415 window_get_active_at(struct window
*w
, u_int x
, u_int y
)
417 struct window_pane
*wp
;
419 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
420 if (!window_pane_visible(wp
))
422 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
424 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
432 window_set_active_at(struct window
*w
, u_int x
, u_int y
)
434 struct window_pane
*wp
;
436 wp
= window_get_active_at(w
, x
, y
);
437 if (wp
!= NULL
&& wp
!= w
->active
)
438 window_set_active_pane(w
, wp
);
442 window_find_string(struct window
*w
, const char *s
)
449 if (strcasecmp(s
, "top") == 0)
451 else if (strcasecmp(s
, "bottom") == 0)
453 else if (strcasecmp(s
, "left") == 0)
455 else if (strcasecmp(s
, "right") == 0)
457 else if (strcasecmp(s
, "top-left") == 0) {
460 } else if (strcasecmp(s
, "top-right") == 0) {
463 } else if (strcasecmp(s
, "bottom-left") == 0) {
466 } else if (strcasecmp(s
, "bottom-right") == 0) {
472 return (window_get_active_at(w
, x
, y
));
476 window_zoom(struct window_pane
*wp
)
478 struct window
*w
= wp
->window
;
479 struct window_pane
*wp1
;
481 if (w
->flags
& WINDOW_ZOOMED
)
484 if (!window_pane_visible(wp
))
487 if (window_count_panes(w
) == 1)
491 window_set_active_pane(w
, wp
);
493 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
494 wp1
->saved_layout_cell
= wp1
->layout_cell
;
495 wp1
->layout_cell
= NULL
;
498 w
->saved_layout_root
= w
->layout_root
;
500 w
->flags
|= WINDOW_ZOOMED
;
506 window_unzoom(struct window
*w
)
508 struct window_pane
*wp
, *wp1
;
510 if (!(w
->flags
& WINDOW_ZOOMED
))
514 w
->flags
&= ~WINDOW_ZOOMED
;
516 w
->layout_root
= w
->saved_layout_root
;
518 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
519 wp1
->layout_cell
= wp1
->saved_layout_cell
;
520 wp1
->saved_layout_cell
= NULL
;
522 layout_fix_panes(w
, w
->sx
, w
->sy
);
528 window_add_pane(struct window
*w
, u_int hlimit
)
530 struct window_pane
*wp
;
532 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
533 if (TAILQ_EMPTY(&w
->panes
))
534 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
536 TAILQ_INSERT_AFTER(&w
->panes
, w
->active
, wp
, entry
);
541 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
543 if (wp
== w
->active
) {
546 if (w
->active
== NULL
) {
547 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
548 if (w
->active
== NULL
)
549 w
->active
= TAILQ_NEXT(wp
, entry
);
551 } else if (wp
== w
->last
)
554 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
555 window_pane_destroy(wp
);
559 window_pane_at_index(struct window
*w
, u_int idx
)
561 struct window_pane
*wp
;
564 n
= options_get_number(&w
->options
, "pane-base-index");
565 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
574 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
577 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
578 wp
= TAILQ_FIRST(&w
->panes
);
585 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
589 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
590 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
597 window_pane_index(struct window_pane
*wp
, u_int
*i
)
599 struct window_pane
*wq
;
600 struct window
*w
= wp
->window
;
602 *i
= options_get_number(&w
->options
, "pane-base-index");
603 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
614 window_count_panes(struct window
*w
)
616 struct window_pane
*wp
;
620 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
626 window_destroy_panes(struct window
*w
)
628 struct window_pane
*wp
;
630 while (!TAILQ_EMPTY(&w
->panes
)) {
631 wp
= TAILQ_FIRST(&w
->panes
);
632 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
633 window_pane_destroy(wp
);
637 /* Return list of printable window flag symbols. No flags is just a space. */
639 window_printable_flags(struct session
*s
, struct winlink
*wl
)
645 if (wl
->flags
& WINLINK_ACTIVITY
)
647 if (wl
->flags
& WINLINK_BELL
)
649 if (wl
->flags
& WINLINK_CONTENT
)
651 if (wl
->flags
& WINLINK_SILENCE
)
655 if (wl
== TAILQ_FIRST(&s
->lastw
))
657 if (wl
->window
->flags
& WINDOW_ZOOMED
)
662 return (xstrdup(flags
));
665 /* Find pane in global tree by id. */
667 window_pane_find_by_id(u_int id
)
669 struct window_pane wp
;
672 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
676 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
678 struct window_pane
*wp
;
680 wp
= xcalloc(1, sizeof *wp
);
683 wp
->id
= next_window_pane_id
++;
684 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
695 wp
->layout_cell
= NULL
;
705 wp
->pipe_event
= NULL
;
707 wp
->saved_grid
= NULL
;
709 screen_init(&wp
->base
, sx
, sy
, hlimit
);
710 wp
->screen
= &wp
->base
;
718 window_pane_destroy(struct window_pane
*wp
)
720 window_pane_reset_mode(wp
);
722 if (event_initialized(&wp
->changes_timer
))
723 evtimer_del(&wp
->changes_timer
);
726 bufferevent_free(wp
->event
);
732 screen_free(&wp
->base
);
733 if (wp
->saved_grid
!= NULL
)
734 grid_destroy(wp
->saved_grid
);
736 if (wp
->pipe_fd
!= -1) {
737 bufferevent_free(wp
->pipe_event
);
741 RB_REMOVE(window_pane_tree
, &all_window_panes
, wp
);
750 window_pane_spawn(struct window_pane
*wp
, const char *cmd
, const char *shell
,
751 const char *cwd
, struct environ
*env
, struct termios
*tio
, char **cause
)
754 char *argv0
, paneid
[16];
759 bufferevent_free(wp
->event
);
764 wp
->cmd
= xstrdup(cmd
);
768 wp
->shell
= xstrdup(shell
);
772 wp
->cwd
= xstrdup(cwd
);
775 log_debug("spawn: %s -- %s", wp
->shell
, wp
->cmd
);
777 memset(&ws
, 0, sizeof ws
);
778 ws
.ws_col
= screen_size_x(&wp
->base
);
779 ws
.ws_row
= screen_size_y(&wp
->base
);
781 switch (wp
->pid
= forkpty(&wp
->fd
, wp
->tty
, NULL
, &ws
)) {
784 xasprintf(cause
, "%s: %s", cmd
, strerror(errno
));
787 if (chdir(wp
->cwd
) != 0)
790 if (tcgetattr(STDIN_FILENO
, &tio2
) != 0)
791 fatal("tcgetattr failed");
793 memcpy(tio2
.c_cc
, tio
->c_cc
, sizeof tio2
.c_cc
);
794 tio2
.c_cc
[VERASE
] = '\177';
795 if (tcsetattr(STDIN_FILENO
, TCSANOW
, &tio2
) != 0)
796 fatal("tcgetattr failed");
798 closefrom(STDERR_FILENO
+ 1);
800 xsnprintf(paneid
, sizeof paneid
, "%%%u", wp
->id
);
801 environ_set(env
, "TMUX_PANE", paneid
);
807 setenv("SHELL", wp
->shell
, 1);
808 ptr
= strrchr(wp
->shell
, '/');
810 if (*wp
->cmd
!= '\0') {
811 /* Use the command. */
812 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
813 xasprintf(&argv0
, "%s", ptr
+ 1);
815 xasprintf(&argv0
, "%s", wp
->shell
);
816 execl(wp
->shell
, argv0
, "-c", wp
->cmd
, (char *) NULL
);
817 fatal("execl failed");
820 /* No command; fork a login shell. */
821 if (ptr
!= NULL
&& *(ptr
+ 1) != '\0')
822 xasprintf(&argv0
, "-%s", ptr
+ 1);
824 xasprintf(&argv0
, "-%s", wp
->shell
);
825 execl(wp
->shell
, argv0
, (char *) NULL
);
826 fatal("execl failed");
829 setblocking(wp
->fd
, 0);
831 wp
->event
= bufferevent_new(wp
->fd
,
832 window_pane_read_callback
, NULL
, window_pane_error_callback
, wp
);
833 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
839 window_pane_timer_start(struct window_pane
*wp
)
846 evtimer_del(&wp
->changes_timer
);
847 evtimer_set(&wp
->changes_timer
, window_pane_timer_callback
, wp
);
848 evtimer_add(&wp
->changes_timer
, &tv
);
852 window_pane_timer_callback(unused
int fd
, unused
short events
, void *data
)
854 struct window_pane
*wp
= data
;
855 struct window
*w
= wp
->window
;
856 u_int interval
, trigger
;
858 interval
= options_get_number(&w
->options
, "c0-change-interval");
859 trigger
= options_get_number(&w
->options
, "c0-change-trigger");
861 if (wp
->changes_redraw
++ == interval
) {
862 wp
->flags
|= PANE_REDRAW
;
863 wp
->changes_redraw
= 0;
867 if (trigger
== 0 || wp
->changes
< trigger
) {
868 wp
->flags
|= PANE_REDRAW
;
869 wp
->flags
&= ~PANE_DROP
;
871 window_pane_timer_start(wp
);
876 window_pane_read_callback(unused
struct bufferevent
*bufev
, void *data
)
878 struct window_pane
*wp
= data
;
882 new_size
= EVBUFFER_LENGTH(wp
->event
->input
) - wp
->pipe_off
;
883 if (wp
->pipe_fd
!= -1 && new_size
> 0) {
884 new_data
= EVBUFFER_DATA(wp
->event
->input
);
885 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
890 wp
->pipe_off
= EVBUFFER_LENGTH(wp
->event
->input
);
893 * If we get here, we're not outputting anymore, so set the silence
894 * flag on the window.
896 wp
->window
->flags
|= WINDOW_SILENCE
;
897 if (gettimeofday(&wp
->window
->silence_timer
, NULL
) != 0)
898 fatal("gettimeofday failed.");
902 window_pane_error_callback(
903 unused
struct bufferevent
*bufev
, unused
short what
, void *data
)
905 struct window_pane
*wp
= data
;
907 server_destroy_pane(wp
);
911 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
913 if (sx
== wp
->sx
&& sy
== wp
->sy
)
918 screen_resize(&wp
->base
, sx
, sy
, wp
->saved_grid
== NULL
);
919 if (wp
->mode
!= NULL
)
920 wp
->mode
->resize(wp
, sx
, sy
);
924 * Enter alternative screen mode. A copy of the visible screen is saved and the
925 * history is not updated
928 window_pane_alternate_on(struct window_pane
*wp
, struct grid_cell
*gc
,
931 struct screen
*s
= &wp
->base
;
934 if (wp
->saved_grid
!= NULL
)
936 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
938 sx
= screen_size_x(s
);
939 sy
= screen_size_y(s
);
941 wp
->saved_grid
= grid_create(sx
, sy
, 0);
942 grid_duplicate_lines(wp
->saved_grid
, 0, s
->grid
, screen_hsize(s
), sy
);
944 wp
->saved_cx
= s
->cx
;
945 wp
->saved_cy
= s
->cy
;
947 memcpy(&wp
->saved_cell
, gc
, sizeof wp
->saved_cell
);
949 grid_view_clear(s
->grid
, 0, 0, sx
, sy
);
951 wp
->base
.grid
->flags
&= ~GRID_HISTORY
;
953 wp
->flags
|= PANE_REDRAW
;
956 /* Exit alternate screen mode and restore the copied grid. */
958 window_pane_alternate_off(struct window_pane
*wp
, struct grid_cell
*gc
,
961 struct screen
*s
= &wp
->base
;
964 if (wp
->saved_grid
== NULL
)
966 if (!options_get_number(&wp
->window
->options
, "alternate-screen"))
968 sx
= screen_size_x(s
);
969 sy
= screen_size_y(s
);
972 * If the current size is bigger, temporarily resize to the old size
973 * before copying back.
975 if (sy
> wp
->saved_grid
->sy
)
976 screen_resize(s
, sx
, wp
->saved_grid
->sy
, 1);
978 /* Restore the grid, cursor position and cell. */
979 grid_duplicate_lines(s
->grid
, screen_hsize(s
), wp
->saved_grid
, 0, sy
);
981 s
->cx
= wp
->saved_cx
;
982 if (s
->cx
> screen_size_x(s
) - 1)
983 s
->cx
= screen_size_x(s
) - 1;
985 s
->cy
= wp
->saved_cy
;
986 if (s
->cy
> screen_size_y(s
) - 1)
987 s
->cy
= screen_size_y(s
) - 1;
988 memcpy(gc
, &wp
->saved_cell
, sizeof *gc
);
991 * Turn history back on (so resize can use it) and then resize back to
994 wp
->base
.grid
->flags
|= GRID_HISTORY
;
995 if (sy
> wp
->saved_grid
->sy
|| sx
!= wp
->saved_grid
->sx
)
996 screen_resize(s
, sx
, sy
, 1);
998 grid_destroy(wp
->saved_grid
);
999 wp
->saved_grid
= NULL
;
1001 wp
->flags
|= PANE_REDRAW
;
1005 window_pane_set_mode(struct window_pane
*wp
, const struct window_mode
*mode
)
1009 if (wp
->mode
!= NULL
)
1013 if ((s
= wp
->mode
->init(wp
)) != NULL
)
1015 wp
->flags
|= PANE_REDRAW
;
1020 window_pane_reset_mode(struct window_pane
*wp
)
1022 if (wp
->mode
== NULL
)
1028 wp
->screen
= &wp
->base
;
1029 wp
->flags
|= PANE_REDRAW
;
1033 window_pane_key(struct window_pane
*wp
, struct session
*sess
, int key
)
1035 struct window_pane
*wp2
;
1037 if (!window_pane_visible(wp
))
1040 if (wp
->mode
!= NULL
) {
1041 if (wp
->mode
->key
!= NULL
)
1042 wp
->mode
->key(wp
, sess
, key
);
1049 if (options_get_number(&wp
->window
->options
, "synchronize-panes")) {
1050 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1051 if (wp2
== wp
|| wp2
->mode
!= NULL
)
1053 if (wp2
->fd
!= -1 && window_pane_visible(wp2
))
1054 input_key(wp2
, key
);
1061 struct window_pane
*wp
, struct session
*sess
, struct mouse_event
*m
)
1063 if (!window_pane_visible(wp
))
1066 if (m
->x
< wp
->xoff
|| m
->x
>= wp
->xoff
+ wp
->sx
)
1068 if (m
->y
< wp
->yoff
|| m
->y
>= wp
->yoff
+ wp
->sy
)
1073 if (wp
->mode
!= NULL
) {
1074 if (wp
->mode
->mouse
!= NULL
&&
1075 options_get_number(&wp
->window
->options
, "mode-mouse"))
1076 wp
->mode
->mouse(wp
, sess
, m
);
1077 } else if (wp
->fd
!= -1)
1078 input_mouse(wp
, sess
, m
);
1082 window_pane_visible(struct window_pane
*wp
)
1084 struct window
*w
= wp
->window
;
1086 if (wp
->layout_cell
== NULL
)
1088 if (wp
->xoff
>= w
->sx
|| wp
->yoff
>= w
->sy
)
1090 if (wp
->xoff
+ wp
->sx
> w
->sx
|| wp
->yoff
+ wp
->sy
> w
->sy
)
1096 window_pane_search(struct window_pane
*wp
, const char *searchstr
, u_int
*lineno
)
1098 struct screen
*s
= &wp
->base
;
1099 char *newsearchstr
, *line
, *msg
;
1103 xasprintf(&newsearchstr
, "*%s*", searchstr
);
1105 for (i
= 0; i
< screen_size_y(s
); i
++) {
1106 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
1107 if (fnmatch(newsearchstr
, line
, 0) == 0) {
1120 /* Find the pane directly above another. */
1121 struct window_pane
*
1122 window_pane_find_up(struct window_pane
*wp
)
1124 struct window_pane
*wp2
;
1127 if (wp
== NULL
|| !window_pane_visible(wp
))
1132 top
= wp
->window
->sy
+ 1;
1135 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1136 if (!window_pane_visible(wp2
))
1138 if (wp2
->yoff
+ wp2
->sy
+ 1 != top
)
1140 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
1146 /* Find the pane directly below another. */
1147 struct window_pane
*
1148 window_pane_find_down(struct window_pane
*wp
)
1150 struct window_pane
*wp2
;
1153 if (wp
== NULL
|| !window_pane_visible(wp
))
1156 bottom
= wp
->yoff
+ wp
->sy
+ 1;
1157 if (bottom
>= wp
->window
->sy
)
1161 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1162 if (!window_pane_visible(wp2
))
1164 if (wp2
->yoff
!= bottom
)
1166 if (left
>= wp2
->xoff
&& left
<= wp2
->xoff
+ wp2
->sx
)
1173 * Find the pane directly to the left of another, adjacent to the left side and
1174 * containing the top edge.
1176 struct window_pane
*
1177 window_pane_find_left(struct window_pane
*wp
)
1179 struct window_pane
*wp2
;
1182 if (wp
== NULL
|| !window_pane_visible(wp
))
1187 left
= wp
->window
->sx
+ 1;
1190 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1191 if (!window_pane_visible(wp2
))
1193 if (wp2
->xoff
+ wp2
->sx
+ 1 != left
)
1195 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
1202 * Find the pane directly to the right of another, that is adjacent to the
1203 * right edge and including the top edge.
1205 struct window_pane
*
1206 window_pane_find_right(struct window_pane
*wp
)
1208 struct window_pane
*wp2
;
1211 if (wp
== NULL
|| !window_pane_visible(wp
))
1214 right
= wp
->xoff
+ wp
->sx
+ 1;
1215 if (right
>= wp
->window
->sx
)
1219 TAILQ_FOREACH(wp2
, &wp
->window
->panes
, entry
) {
1220 if (!window_pane_visible(wp2
))
1222 if (wp2
->xoff
!= right
)
1224 if (top
>= wp2
->yoff
&& top
<= wp2
->yoff
+ wp2
->sy
)
1230 /* Clear alert flags for a winlink */
1232 winlink_clear_flags(struct winlink
*wl
)
1239 for (i
= 0; i
< ARRAY_LENGTH(&windows
); i
++) {
1240 if ((w
= ARRAY_ITEM(&windows
, i
)) == NULL
)
1243 RB_FOREACH(s
, sessions
, &sessions
) {
1244 if ((wm
= session_has(s
, w
)) == NULL
)
1247 if (wm
->window
!= wl
->window
)
1249 if ((wm
->flags
& WINLINK_ALERTFLAGS
) == 0)
1252 wm
->flags
&= ~WINLINK_ALERTFLAGS
;
1253 server_status_session(s
);
1258 /* Set the grid_cell with fg/bg/attr information when window is in a mode. */
1260 window_mode_attrs(struct grid_cell
*gc
, struct options
*oo
)
1262 memcpy(gc
, &grid_default_cell
, sizeof *gc
);
1263 colour_set_fg(gc
, options_get_number(oo
, "mode-fg"));
1264 colour_set_bg(gc
, options_get_number(oo
, "mode-bg"));
1265 gc
->attr
|= options_get_number(oo
, "mode-attr");