4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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>
39 * Each window is attached to a number of panes, each of which is a pty. This
40 * file contains code to handle them.
42 * A pane has two buffers attached, these are filled and emptied by the main
43 * server poll loop. Output data is received from pty's in screen format,
44 * translated and returned as a series of escape sequences and strings via
45 * input_parse (in input.c). Input data is received as key codes and written
46 * directly via input_key.
48 * Each pane also has a "virtual" screen (screen.c) which contains the current
49 * state and is redisplayed when the window is reattached to a client.
51 * Windows are stored directly on a global array and wrapped in any number of
52 * winlink structs to be linked onto local session RB trees. A reference count
53 * is maintained and a window removed from the global list and destroyed when
57 /* Global window list. */
58 struct windows windows
;
60 /* Global panes tree. */
61 struct window_pane_tree all_window_panes
;
62 static u_int next_window_pane_id
;
63 static u_int next_window_id
;
64 static u_int next_active_point
;
66 struct window_pane_input_data
{
67 struct cmdq_item
*item
;
71 static struct window_pane
*window_pane_create(struct window
*, u_int
, u_int
,
73 static void window_pane_destroy(struct window_pane
*);
75 RB_GENERATE(windows
, window
, entry
, window_cmp
);
76 RB_GENERATE(winlinks
, winlink
, entry
, winlink_cmp
);
77 RB_GENERATE(window_pane_tree
, window_pane
, tree_entry
, window_pane_cmp
);
80 window_cmp(struct window
*w1
, struct window
*w2
)
82 return (w1
->id
- w2
->id
);
86 winlink_cmp(struct winlink
*wl1
, struct winlink
*wl2
)
88 return (wl1
->idx
- wl2
->idx
);
92 window_pane_cmp(struct window_pane
*wp1
, struct window_pane
*wp2
)
94 return (wp1
->id
- wp2
->id
);
98 winlink_find_by_window(struct winlinks
*wwl
, struct window
*w
)
102 RB_FOREACH(wl
, winlinks
, wwl
) {
111 winlink_find_by_index(struct winlinks
*wwl
, int idx
)
119 return (RB_FIND(winlinks
, wwl
, &wl
));
123 winlink_find_by_window_id(struct winlinks
*wwl
, u_int id
)
127 RB_FOREACH(wl
, winlinks
, wwl
) {
128 if (wl
->window
->id
== id
)
135 winlink_next_index(struct winlinks
*wwl
, int idx
)
141 if (winlink_find_by_index(wwl
, i
) == NULL
)
152 winlink_count(struct winlinks
*wwl
)
158 RB_FOREACH(wl
, winlinks
, wwl
)
165 winlink_add(struct winlinks
*wwl
, int idx
)
170 if ((idx
= winlink_next_index(wwl
, -idx
- 1)) == -1)
172 } else if (winlink_find_by_index(wwl
, idx
) != NULL
)
175 wl
= xcalloc(1, sizeof *wl
);
177 RB_INSERT(winlinks
, wwl
, wl
);
183 winlink_set_window(struct winlink
*wl
, struct window
*w
)
185 if (wl
->window
!= NULL
) {
186 TAILQ_REMOVE(&wl
->window
->winlinks
, wl
, wentry
);
187 window_remove_ref(wl
->window
, __func__
);
189 TAILQ_INSERT_TAIL(&w
->winlinks
, wl
, wentry
);
191 window_add_ref(w
, __func__
);
195 winlink_remove(struct winlinks
*wwl
, struct winlink
*wl
)
197 struct window
*w
= wl
->window
;
200 TAILQ_REMOVE(&w
->winlinks
, wl
, wentry
);
201 window_remove_ref(w
, __func__
);
204 RB_REMOVE(winlinks
, wwl
, wl
);
209 winlink_next(struct winlink
*wl
)
211 return (RB_NEXT(winlinks
, wwl
, wl
));
215 winlink_previous(struct winlink
*wl
)
217 return (RB_PREV(winlinks
, wwl
, wl
));
221 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
224 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
225 wl
= RB_MIN(winlinks
, &s
->windows
);
232 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
235 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
236 wl
= RB_MAX(winlinks
, &s
->windows
);
243 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
248 winlink_stack_remove(stack
, wl
);
249 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
253 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
260 TAILQ_FOREACH(wl2
, stack
, sentry
) {
262 TAILQ_REMOVE(stack
, wl
, sentry
);
269 window_find_by_id_str(const char *s
)
277 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
280 return (window_find_by_id(id
));
284 window_find_by_id(u_int id
)
289 return (RB_FIND(windows
, &windows
, &w
));
293 window_update_activity(struct window
*w
)
295 gettimeofday(&w
->activity_time
, NULL
);
296 alerts_queue(w
, WINDOW_ACTIVITY
);
300 window_create(u_int sx
, u_int sy
, u_int xpixel
, u_int ypixel
)
305 xpixel
= DEFAULT_XPIXEL
;
307 ypixel
= DEFAULT_YPIXEL
;
309 w
= xcalloc(1, sizeof *w
);
310 w
->name
= xstrdup("");
313 TAILQ_INIT(&w
->panes
);
317 w
->layout_root
= NULL
;
326 w
->options
= options_create(global_w_options
);
329 TAILQ_INIT(&w
->winlinks
);
331 w
->id
= next_window_id
++;
332 RB_INSERT(windows
, &windows
, w
);
334 window_set_fill_character(w
);
335 window_update_activity(w
);
337 log_debug("%s: @%u create %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
338 w
->xpixel
, w
->ypixel
);
343 window_destroy(struct window
*w
)
345 log_debug("window @%u destroyed (%d references)", w
->id
, w
->references
);
347 RB_REMOVE(windows
, &windows
, w
);
349 if (w
->layout_root
!= NULL
)
350 layout_free_cell(w
->layout_root
);
351 if (w
->saved_layout_root
!= NULL
)
352 layout_free_cell(w
->saved_layout_root
);
355 window_destroy_panes(w
);
357 if (event_initialized(&w
->name_event
))
358 evtimer_del(&w
->name_event
);
360 if (event_initialized(&w
->alerts_timer
))
361 evtimer_del(&w
->alerts_timer
);
362 if (event_initialized(&w
->offset_timer
))
363 event_del(&w
->offset_timer
);
365 options_free(w
->options
);
366 free(w
->fill_character
);
373 window_pane_destroy_ready(struct window_pane
*wp
)
377 if (wp
->pipe_fd
!= -1) {
378 if (EVBUFFER_LENGTH(wp
->pipe_event
->output
) != 0)
380 if (ioctl(wp
->fd
, FIONREAD
, &n
) != -1 && n
> 0)
384 if (~wp
->flags
& PANE_EXITED
)
390 window_add_ref(struct window
*w
, const char *from
)
393 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
397 window_remove_ref(struct window
*w
, const char *from
)
400 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
402 if (w
->references
== 0)
407 window_set_name(struct window
*w
, const char *new_name
)
410 utf8_stravis(&w
->name
, new_name
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
411 notify_window("window-renamed", w
);
415 window_resize(struct window
*w
, u_int sx
, u_int sy
, int xpixel
, int ypixel
)
418 xpixel
= DEFAULT_XPIXEL
;
420 ypixel
= DEFAULT_YPIXEL
;
422 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
423 xpixel
== -1 ? w
->xpixel
: (u_int
)xpixel
,
424 ypixel
== -1 ? w
->ypixel
: (u_int
)ypixel
);
434 window_pane_send_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
436 struct window
*w
= wp
->window
;
442 log_debug("%s: %%%u resize to %u,%u", __func__
, wp
->id
, sx
, sy
);
444 memset(&ws
, 0, sizeof ws
);
447 ws
.ws_xpixel
= w
->xpixel
* ws
.ws_col
;
448 ws
.ws_ypixel
= w
->ypixel
* ws
.ws_row
;
449 if (ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
450 fatal("ioctl failed");
454 window_has_pane(struct window
*w
, struct window_pane
*wp
)
456 struct window_pane
*wp1
;
458 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
466 window_update_focus(struct window
*w
)
469 log_debug("%s: @%u", __func__
, w
->id
);
470 window_pane_update_focus(w
->active
);
475 window_pane_update_focus(struct window_pane
*wp
)
481 if (wp
!= wp
->window
->active
)
484 TAILQ_FOREACH(c
, &clients
, entry
) {
485 if (c
->session
!= NULL
&&
486 c
->session
->attached
!= 0 &&
487 (c
->flags
& CLIENT_FOCUSED
) &&
488 c
->session
->curw
->window
== wp
->window
) {
494 if (!focused
&& (wp
->flags
& PANE_FOCUSED
)) {
495 log_debug("%s: %%%u focus out", __func__
, wp
->id
);
496 if (wp
->base
.mode
& MODE_FOCUSON
)
497 bufferevent_write(wp
->event
, "\033[O", 3);
498 notify_pane("pane-focus-out", wp
);
499 wp
->flags
&= ~PANE_FOCUSED
;
500 } else if (focused
&& (~wp
->flags
& PANE_FOCUSED
)) {
501 log_debug("%s: %%%u focus in", __func__
, wp
->id
);
502 if (wp
->base
.mode
& MODE_FOCUSON
)
503 bufferevent_write(wp
->event
, "\033[I", 3);
504 notify_pane("pane-focus-in", wp
);
505 wp
->flags
|= PANE_FOCUSED
;
507 log_debug("%s: %%%u focus unchanged", __func__
, wp
->id
);
512 window_set_active_pane(struct window
*w
, struct window_pane
*wp
, int notify
)
514 log_debug("%s: pane %%%u", __func__
, wp
->id
);
521 w
->active
->active_point
= next_active_point
++;
522 w
->active
->flags
|= PANE_CHANGED
;
524 if (options_get_number(global_options
, "focus-events")) {
525 window_pane_update_focus(w
->last
);
526 window_pane_update_focus(w
->active
);
529 tty_update_window_offset(w
);
532 notify_window("window-pane-changed", w
);
537 window_pane_get_palette(struct window_pane
*wp
, int c
)
541 return (colour_palette_get(&wp
->palette
, c
));
545 window_redraw_active_switch(struct window
*w
, struct window_pane
*wp
)
547 struct grid_cell
*gc1
, *gc2
;
555 * If the active and inactive styles or palettes are different,
556 * need to redraw the panes.
558 gc1
= &wp
->cached_gc
;
559 gc2
= &wp
->cached_active_gc
;
560 if (!grid_cells_look_equal(gc1
, gc2
))
561 wp
->flags
|= PANE_REDRAW
;
563 c1
= window_pane_get_palette(wp
, gc1
->fg
);
564 c2
= window_pane_get_palette(wp
, gc2
->fg
);
566 wp
->flags
|= PANE_REDRAW
;
568 c1
= window_pane_get_palette(wp
, gc1
->bg
);
569 c2
= window_pane_get_palette(wp
, gc2
->bg
);
571 wp
->flags
|= PANE_REDRAW
;
581 window_get_active_at(struct window
*w
, u_int x
, u_int y
)
583 struct window_pane
*wp
;
585 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
586 if (!window_pane_visible(wp
))
588 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
590 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
598 window_find_string(struct window
*w
, const char *s
)
600 u_int x
, y
, top
= 0, bottom
= w
->sy
- 1;
606 status
= options_get_number(w
->options
, "pane-border-status");
607 if (status
== PANE_STATUS_TOP
)
609 else if (status
== PANE_STATUS_BOTTOM
)
612 if (strcasecmp(s
, "top") == 0)
614 else if (strcasecmp(s
, "bottom") == 0)
616 else if (strcasecmp(s
, "left") == 0)
618 else if (strcasecmp(s
, "right") == 0)
620 else if (strcasecmp(s
, "top-left") == 0) {
623 } else if (strcasecmp(s
, "top-right") == 0) {
626 } else if (strcasecmp(s
, "bottom-left") == 0) {
629 } else if (strcasecmp(s
, "bottom-right") == 0) {
635 return (window_get_active_at(w
, x
, y
));
639 window_zoom(struct window_pane
*wp
)
641 struct window
*w
= wp
->window
;
642 struct window_pane
*wp1
;
644 if (w
->flags
& WINDOW_ZOOMED
)
647 if (window_count_panes(w
) == 1)
651 window_set_active_pane(w
, wp
, 1);
653 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
654 wp1
->saved_layout_cell
= wp1
->layout_cell
;
655 wp1
->layout_cell
= NULL
;
658 w
->saved_layout_root
= w
->layout_root
;
660 w
->flags
|= WINDOW_ZOOMED
;
661 notify_window("window-layout-changed", w
);
667 window_unzoom(struct window
*w
)
669 struct window_pane
*wp
;
671 if (!(w
->flags
& WINDOW_ZOOMED
))
674 w
->flags
&= ~WINDOW_ZOOMED
;
676 w
->layout_root
= w
->saved_layout_root
;
677 w
->saved_layout_root
= NULL
;
679 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
680 wp
->layout_cell
= wp
->saved_layout_cell
;
681 wp
->saved_layout_cell
= NULL
;
683 layout_fix_panes(w
, NULL
);
684 notify_window("window-layout-changed", w
);
690 window_push_zoom(struct window
*w
, int always
, int flag
)
692 log_debug("%s: @%u %d", __func__
, w
->id
,
693 flag
&& (w
->flags
& WINDOW_ZOOMED
));
694 if (flag
&& (always
|| (w
->flags
& WINDOW_ZOOMED
)))
695 w
->flags
|= WINDOW_WASZOOMED
;
697 w
->flags
&= ~WINDOW_WASZOOMED
;
698 return (window_unzoom(w
) == 0);
702 window_pop_zoom(struct window
*w
)
704 log_debug("%s: @%u %d", __func__
, w
->id
,
705 !!(w
->flags
& WINDOW_WASZOOMED
));
706 if (w
->flags
& WINDOW_WASZOOMED
)
707 return (window_zoom(w
->active
) == 0);
712 window_add_pane(struct window
*w
, struct window_pane
*other
, u_int hlimit
,
715 struct window_pane
*wp
;
720 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
721 if (TAILQ_EMPTY(&w
->panes
)) {
722 log_debug("%s: @%u at start", __func__
, w
->id
);
723 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
724 } else if (flags
& SPAWN_BEFORE
) {
725 log_debug("%s: @%u before %%%u", __func__
, w
->id
, wp
->id
);
726 if (flags
& SPAWN_FULLSIZE
)
727 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
729 TAILQ_INSERT_BEFORE(other
, wp
, entry
);
731 log_debug("%s: @%u after %%%u", __func__
, w
->id
, wp
->id
);
732 if (flags
& SPAWN_FULLSIZE
)
733 TAILQ_INSERT_TAIL(&w
->panes
, wp
, entry
);
735 TAILQ_INSERT_AFTER(&w
->panes
, other
, wp
, entry
);
741 window_lost_pane(struct window
*w
, struct window_pane
*wp
)
743 log_debug("%s: @%u pane %%%u", __func__
, w
->id
, wp
->id
);
745 if (wp
== marked_pane
.wp
)
746 server_clear_marked();
748 if (wp
== w
->active
) {
751 if (w
->active
== NULL
) {
752 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
753 if (w
->active
== NULL
)
754 w
->active
= TAILQ_NEXT(wp
, entry
);
756 if (w
->active
!= NULL
) {
757 w
->active
->flags
|= PANE_CHANGED
;
758 notify_window("window-pane-changed", w
);
759 window_update_focus(w
);
761 } else if (wp
== w
->last
)
766 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
768 window_lost_pane(w
, wp
);
770 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
771 window_pane_destroy(wp
);
775 window_pane_at_index(struct window
*w
, u_int idx
)
777 struct window_pane
*wp
;
780 n
= options_get_number(w
->options
, "pane-base-index");
781 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
790 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
793 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
794 wp
= TAILQ_FIRST(&w
->panes
);
801 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
805 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
806 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
813 window_pane_index(struct window_pane
*wp
, u_int
*i
)
815 struct window_pane
*wq
;
816 struct window
*w
= wp
->window
;
818 *i
= options_get_number(w
->options
, "pane-base-index");
819 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
830 window_count_panes(struct window
*w
)
832 struct window_pane
*wp
;
836 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
842 window_destroy_panes(struct window
*w
)
844 struct window_pane
*wp
;
846 while (!TAILQ_EMPTY(&w
->panes
)) {
847 wp
= TAILQ_FIRST(&w
->panes
);
848 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
849 window_pane_destroy(wp
);
854 window_printable_flags(struct winlink
*wl
, int escape
)
856 struct session
*s
= wl
->session
;
857 static char flags
[32];
861 if (wl
->flags
& WINLINK_ACTIVITY
) {
866 if (wl
->flags
& WINLINK_BELL
)
868 if (wl
->flags
& WINLINK_SILENCE
)
872 if (wl
== TAILQ_FIRST(&s
->lastw
))
874 if (server_check_marked() && wl
== marked_pane
.wl
)
876 if (wl
->window
->flags
& WINDOW_ZOOMED
)
883 window_pane_find_by_id_str(const char *s
)
891 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
894 return (window_pane_find_by_id(id
));
898 window_pane_find_by_id(u_int id
)
900 struct window_pane wp
;
903 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
906 static struct window_pane
*
907 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
909 struct window_pane
*wp
;
910 char host
[HOST_NAME_MAX
+ 1];
912 wp
= xcalloc(1, sizeof *wp
);
914 wp
->options
= options_create(w
->options
);
915 wp
->flags
= PANE_STYLECHANGED
;
917 wp
->id
= next_window_pane_id
++;
918 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
922 TAILQ_INIT(&wp
->modes
);
924 TAILQ_INIT (&wp
->resize_queue
);
931 colour_palette_init(&wp
->palette
);
932 colour_palette_from_option(&wp
->palette
, wp
->options
);
934 screen_init(&wp
->base
, sx
, sy
, hlimit
);
935 wp
->screen
= &wp
->base
;
936 window_pane_default_cursor(wp
);
938 screen_init(&wp
->status_screen
, 1, 1, 0);
940 if (gethostname(host
, sizeof host
) == 0)
941 screen_set_title(&wp
->base
, host
);
947 window_pane_destroy(struct window_pane
*wp
)
949 struct window_pane_resize
*r
;
950 struct window_pane_resize
*r1
;
952 window_pane_reset_mode_all(wp
);
956 bufferevent_free(wp
->event
);
959 if (wp
->ictx
!= NULL
)
960 input_free(wp
->ictx
);
962 screen_free(&wp
->status_screen
);
964 screen_free(&wp
->base
);
966 if (wp
->pipe_fd
!= -1) {
967 bufferevent_free(wp
->pipe_event
);
971 if (event_initialized(&wp
->resize_timer
))
972 event_del(&wp
->resize_timer
);
973 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
974 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
978 RB_REMOVE(window_pane_tree
, &all_window_panes
, wp
);
980 options_free(wp
->options
);
981 free((void *)wp
->cwd
);
983 cmd_free_argv(wp
->argc
, wp
->argv
);
984 colour_palette_free(&wp
->palette
);
989 window_pane_read_callback(__unused
struct bufferevent
*bufev
, void *data
)
991 struct window_pane
*wp
= data
;
992 struct evbuffer
*evb
= wp
->event
->input
;
993 struct window_pane_offset
*wpo
= &wp
->pipe_offset
;
994 size_t size
= EVBUFFER_LENGTH(evb
);
999 if (wp
->pipe_fd
!= -1) {
1000 new_data
= window_pane_get_new_data(wp
, wpo
, &new_size
);
1002 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
1003 window_pane_update_used_data(wp
, wpo
, new_size
);
1007 log_debug("%%%u has %zu bytes", wp
->id
, size
);
1008 TAILQ_FOREACH(c
, &clients
, entry
) {
1009 if (c
->session
!= NULL
&& (c
->flags
& CLIENT_CONTROL
))
1010 control_write_output(c
, wp
);
1012 input_parse_pane(wp
);
1013 bufferevent_disable(wp
->event
, EV_READ
);
1017 window_pane_error_callback(__unused
struct bufferevent
*bufev
,
1018 __unused
short what
, void *data
)
1020 struct window_pane
*wp
= data
;
1022 log_debug("%%%u error", wp
->id
);
1023 wp
->flags
|= PANE_EXITED
;
1025 if (window_pane_destroy_ready(wp
))
1026 server_destroy_pane(wp
, 1);
1030 window_pane_set_event(struct window_pane
*wp
)
1032 setblocking(wp
->fd
, 0);
1034 wp
->event
= bufferevent_new(wp
->fd
, window_pane_read_callback
,
1035 NULL
, window_pane_error_callback
, wp
);
1036 if (wp
->event
== NULL
)
1037 fatalx("out of memory");
1038 wp
->ictx
= input_init(wp
, wp
->event
, &wp
->palette
);
1040 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
1044 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
1046 struct window_mode_entry
*wme
;
1047 struct window_pane_resize
*r
;
1049 if (sx
== wp
->sx
&& sy
== wp
->sy
)
1052 r
= xmalloc(sizeof *r
);
1057 TAILQ_INSERT_TAIL (&wp
->resize_queue
, r
, entry
);
1062 log_debug("%s: %%%u resize %ux%u", __func__
, wp
->id
, sx
, sy
);
1063 screen_resize(&wp
->base
, sx
, sy
, wp
->base
.saved_grid
== NULL
);
1065 wme
= TAILQ_FIRST(&wp
->modes
);
1066 if (wme
!= NULL
&& wme
->mode
->resize
!= NULL
)
1067 wme
->mode
->resize(wme
, sx
, sy
);
1071 window_pane_set_mode(struct window_pane
*wp
, struct window_pane
*swp
,
1072 const struct window_mode
*mode
, struct cmd_find_state
*fs
,
1075 struct window_mode_entry
*wme
;
1077 if (!TAILQ_EMPTY(&wp
->modes
) && TAILQ_FIRST(&wp
->modes
)->mode
== mode
)
1080 TAILQ_FOREACH(wme
, &wp
->modes
, entry
) {
1081 if (wme
->mode
== mode
)
1085 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1086 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1088 wme
= xcalloc(1, sizeof *wme
);
1093 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1094 wme
->screen
= wme
->mode
->init(wme
, fs
, args
);
1097 wp
->screen
= wme
->screen
;
1098 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1100 server_redraw_window_borders(wp
->window
);
1101 server_status_window(wp
->window
);
1102 notify_pane("pane-mode-changed", wp
);
1108 window_pane_reset_mode(struct window_pane
*wp
)
1110 struct window_mode_entry
*wme
, *next
;
1112 if (TAILQ_EMPTY(&wp
->modes
))
1115 wme
= TAILQ_FIRST(&wp
->modes
);
1116 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1117 wme
->mode
->free(wme
);
1120 next
= TAILQ_FIRST(&wp
->modes
);
1122 log_debug("%s: no next mode", __func__
);
1123 wp
->screen
= &wp
->base
;
1125 log_debug("%s: next mode is %s", __func__
, next
->mode
->name
);
1126 wp
->screen
= next
->screen
;
1127 if (next
->mode
->resize
!= NULL
)
1128 next
->mode
->resize(next
, wp
->sx
, wp
->sy
);
1130 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1132 server_redraw_window_borders(wp
->window
);
1133 server_status_window(wp
->window
);
1134 notify_pane("pane-mode-changed", wp
);
1138 window_pane_reset_mode_all(struct window_pane
*wp
)
1140 while (!TAILQ_EMPTY(&wp
->modes
))
1141 window_pane_reset_mode(wp
);
1145 window_pane_copy_key(struct window_pane
*wp
, key_code key
)
1147 struct window_pane
*loop
;
1149 TAILQ_FOREACH(loop
, &wp
->window
->panes
, entry
) {
1151 TAILQ_EMPTY(&loop
->modes
) &&
1153 (~loop
->flags
& PANE_INPUTOFF
) &&
1154 window_pane_visible(loop
) &&
1155 options_get_number(loop
->options
, "synchronize-panes"))
1156 input_key_pane(loop
, key
, NULL
);
1161 window_pane_key(struct window_pane
*wp
, struct client
*c
, struct session
*s
,
1162 struct winlink
*wl
, key_code key
, struct mouse_event
*m
)
1164 struct window_mode_entry
*wme
;
1166 if (KEYC_IS_MOUSE(key
) && m
== NULL
)
1169 wme
= TAILQ_FIRST(&wp
->modes
);
1171 if (wme
->mode
->key
!= NULL
&& c
!= NULL
) {
1172 key
&= ~KEYC_MASK_FLAGS
;
1173 wme
->mode
->key(wme
, c
, s
, wl
, key
, m
);
1178 if (wp
->fd
== -1 || wp
->flags
& PANE_INPUTOFF
)
1181 if (input_key_pane(wp
, key
, m
) != 0)
1184 if (KEYC_IS_MOUSE(key
))
1186 if (options_get_number(wp
->options
, "synchronize-panes"))
1187 window_pane_copy_key(wp
, key
);
1192 window_pane_visible(struct window_pane
*wp
)
1194 if (~wp
->window
->flags
& WINDOW_ZOOMED
)
1196 return (wp
== wp
->window
->active
);
1200 window_pane_search(struct window_pane
*wp
, const char *term
, int regex
,
1203 struct screen
*s
= &wp
->base
;
1205 char *new = NULL
, *line
;
1207 int flags
= 0, found
;
1212 flags
|= FNM_CASEFOLD
;
1213 xasprintf(&new, "*%s*", term
);
1217 if (regcomp(&r
, term
, flags
|REG_EXTENDED
) != 0)
1221 for (i
= 0; i
< screen_size_y(s
); i
++) {
1222 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
1223 for (n
= strlen(line
); n
> 0; n
--) {
1224 if (!isspace((u_char
)line
[n
- 1]))
1228 log_debug("%s: %s", __func__
, line
);
1230 found
= (fnmatch(new, line
, flags
) == 0);
1232 found
= (regexec(&r
, line
, 0, NULL
, 0) == 0);
1242 if (i
== screen_size_y(s
))
1247 /* Get MRU pane from a list. */
1248 static struct window_pane
*
1249 window_pane_choose_best(struct window_pane
**list
, u_int size
)
1251 struct window_pane
*next
, *best
;
1258 for (i
= 1; i
< size
; i
++) {
1260 if (next
->active_point
> best
->active_point
)
1267 * Find the pane directly above another. We build a list of those adjacent to
1268 * top edge and then choose the best.
1270 struct window_pane
*
1271 window_pane_find_up(struct window_pane
*wp
)
1274 struct window_pane
*next
, *best
, **list
;
1275 u_int edge
, left
, right
, end
, size
;
1281 status
= options_get_number(w
->options
, "pane-border-status");
1287 if (status
== PANE_STATUS_TOP
) {
1290 } else if (status
== PANE_STATUS_BOTTOM
) {
1299 right
= wp
->xoff
+ wp
->sx
;
1301 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1304 if (next
->yoff
+ next
->sy
+ 1 != edge
)
1306 end
= next
->xoff
+ next
->sx
- 1;
1309 if (next
->xoff
< left
&& end
> right
)
1311 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1313 else if (end
>= left
&& end
<= right
)
1317 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1318 list
[size
++] = next
;
1321 best
= window_pane_choose_best(list
, size
);
1326 /* Find the pane directly below another. */
1327 struct window_pane
*
1328 window_pane_find_down(struct window_pane
*wp
)
1331 struct window_pane
*next
, *best
, **list
;
1332 u_int edge
, left
, right
, end
, size
;
1338 status
= options_get_number(w
->options
, "pane-border-status");
1343 edge
= wp
->yoff
+ wp
->sy
+ 1;
1344 if (status
== PANE_STATUS_TOP
) {
1347 } else if (status
== PANE_STATUS_BOTTOM
) {
1348 if (edge
>= w
->sy
- 1)
1356 right
= wp
->xoff
+ wp
->sx
;
1358 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1361 if (next
->yoff
!= edge
)
1363 end
= next
->xoff
+ next
->sx
- 1;
1366 if (next
->xoff
< left
&& end
> right
)
1368 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1370 else if (end
>= left
&& end
<= right
)
1374 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1375 list
[size
++] = next
;
1378 best
= window_pane_choose_best(list
, size
);
1383 /* Find the pane directly to the left of another. */
1384 struct window_pane
*
1385 window_pane_find_left(struct window_pane
*wp
)
1388 struct window_pane
*next
, *best
, **list
;
1389 u_int edge
, top
, bottom
, end
, size
;
1404 bottom
= wp
->yoff
+ wp
->sy
;
1406 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1409 if (next
->xoff
+ next
->sx
+ 1 != edge
)
1411 end
= next
->yoff
+ next
->sy
- 1;
1414 if (next
->yoff
< top
&& end
> bottom
)
1416 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1418 else if (end
>= top
&& end
<= bottom
)
1422 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1423 list
[size
++] = next
;
1426 best
= window_pane_choose_best(list
, size
);
1431 /* Find the pane directly to the right of another. */
1432 struct window_pane
*
1433 window_pane_find_right(struct window_pane
*wp
)
1436 struct window_pane
*next
, *best
, **list
;
1437 u_int edge
, top
, bottom
, end
, size
;
1447 edge
= wp
->xoff
+ wp
->sx
+ 1;
1452 bottom
= wp
->yoff
+ wp
->sy
;
1454 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1457 if (next
->xoff
!= edge
)
1459 end
= next
->yoff
+ next
->sy
- 1;
1462 if (next
->yoff
< top
&& end
> bottom
)
1464 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1466 else if (end
>= top
&& end
<= bottom
)
1470 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1471 list
[size
++] = next
;
1474 best
= window_pane_choose_best(list
, size
);
1479 /* Clear alert flags for a winlink */
1481 winlink_clear_flags(struct winlink
*wl
)
1483 struct winlink
*loop
;
1485 wl
->window
->flags
&= ~WINDOW_ALERTFLAGS
;
1486 TAILQ_FOREACH(loop
, &wl
->window
->winlinks
, wentry
) {
1487 if ((loop
->flags
& WINLINK_ALERTFLAGS
) != 0) {
1488 loop
->flags
&= ~WINLINK_ALERTFLAGS
;
1489 server_status_session(loop
->session
);
1494 /* Shuffle window indexes up. */
1496 winlink_shuffle_up(struct session
*s
, struct winlink
*wl
, int before
)
1507 /* Find the next free index. */
1508 for (last
= idx
; last
< INT_MAX
; last
++) {
1509 if (winlink_find_by_index(&s
->windows
, last
) == NULL
)
1512 if (last
== INT_MAX
)
1515 /* Move everything from last - 1 to idx up a bit. */
1516 for (; last
> idx
; last
--) {
1517 wl
= winlink_find_by_index(&s
->windows
, last
- 1);
1518 RB_REMOVE(winlinks
, &s
->windows
, wl
);
1520 RB_INSERT(winlinks
, &s
->windows
, wl
);
1527 window_pane_input_callback(struct client
*c
, __unused
const char *path
,
1528 int error
, int closed
, struct evbuffer
*buffer
, void *data
)
1530 struct window_pane_input_data
*cdata
= data
;
1531 struct window_pane
*wp
;
1532 u_char
*buf
= EVBUFFER_DATA(buffer
);
1533 size_t len
= EVBUFFER_LENGTH(buffer
);
1535 wp
= window_pane_find_by_id(cdata
->wp
);
1536 if (wp
== NULL
|| closed
|| error
!= 0 || (c
->flags
& CLIENT_DEAD
)) {
1538 c
->flags
|= CLIENT_EXIT
;
1540 evbuffer_drain(buffer
, len
);
1541 cmdq_continue(cdata
->item
);
1543 server_client_unref(c
);
1547 input_parse_buffer(wp
, buf
, len
);
1548 evbuffer_drain(buffer
, len
);
1552 window_pane_start_input(struct window_pane
*wp
, struct cmdq_item
*item
,
1555 struct client
*c
= cmdq_get_client(item
);
1556 struct window_pane_input_data
*cdata
;
1558 if (~wp
->flags
& PANE_EMPTY
) {
1559 *cause
= xstrdup("pane is not empty");
1562 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
1564 if (c
->session
!= NULL
)
1567 cdata
= xmalloc(sizeof *cdata
);
1572 file_read(c
, "-", window_pane_input_callback
, cdata
);
1578 window_pane_get_new_data(struct window_pane
*wp
,
1579 struct window_pane_offset
*wpo
, size_t *size
)
1581 size_t used
= wpo
->used
- wp
->base_offset
;
1583 *size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1584 return (EVBUFFER_DATA(wp
->event
->input
) + used
);
1588 window_pane_update_used_data(struct window_pane
*wp
,
1589 struct window_pane_offset
*wpo
, size_t size
)
1591 size_t used
= wpo
->used
- wp
->base_offset
;
1593 if (size
> EVBUFFER_LENGTH(wp
->event
->input
) - used
)
1594 size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1599 window_set_fill_character(struct window
*w
)
1602 struct utf8_data
*ud
;
1604 free(w
->fill_character
);
1605 w
->fill_character
= NULL
;
1607 value
= options_get_string(w
->options
, "fill-character");
1608 if (*value
!= '\0' && utf8_isvalid(value
)) {
1609 ud
= utf8_fromcstr(value
);
1610 if (ud
!= NULL
&& ud
[0].width
== 1)
1611 w
->fill_character
= ud
;
1616 window_pane_default_cursor(struct window_pane
*wp
)
1618 struct screen
*s
= wp
->screen
;
1621 c
= options_get_number(wp
->options
, "cursor-colour");
1622 s
->default_ccolour
= c
;
1624 c
= options_get_number(wp
->options
, "cursor-style");
1625 s
->default_mode
= 0;
1626 screen_set_cursor_style(c
, &s
->default_cstyle
, &s
->default_mode
);