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_update_activity(w
);
336 log_debug("%s: @%u create %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
337 w
->xpixel
, w
->ypixel
);
342 window_destroy(struct window
*w
)
344 log_debug("window @%u destroyed (%d references)", w
->id
, w
->references
);
346 RB_REMOVE(windows
, &windows
, w
);
348 if (w
->layout_root
!= NULL
)
349 layout_free_cell(w
->layout_root
);
350 if (w
->saved_layout_root
!= NULL
)
351 layout_free_cell(w
->saved_layout_root
);
354 window_destroy_panes(w
);
356 if (event_initialized(&w
->name_event
))
357 evtimer_del(&w
->name_event
);
359 if (event_initialized(&w
->alerts_timer
))
360 evtimer_del(&w
->alerts_timer
);
361 if (event_initialized(&w
->offset_timer
))
362 event_del(&w
->offset_timer
);
364 options_free(w
->options
);
371 window_pane_destroy_ready(struct window_pane
*wp
)
375 if (wp
->pipe_fd
!= -1) {
376 if (EVBUFFER_LENGTH(wp
->pipe_event
->output
) != 0)
378 if (ioctl(wp
->fd
, FIONREAD
, &n
) != -1 && n
> 0)
382 if (~wp
->flags
& PANE_EXITED
)
388 window_add_ref(struct window
*w
, const char *from
)
391 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
395 window_remove_ref(struct window
*w
, const char *from
)
398 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
400 if (w
->references
== 0)
405 window_set_name(struct window
*w
, const char *new_name
)
408 utf8_stravis(&w
->name
, new_name
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
409 notify_window("window-renamed", w
);
413 window_resize(struct window
*w
, u_int sx
, u_int sy
, int xpixel
, int ypixel
)
416 xpixel
= DEFAULT_XPIXEL
;
418 ypixel
= DEFAULT_YPIXEL
;
420 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
421 xpixel
== -1 ? w
->xpixel
: (u_int
)xpixel
,
422 ypixel
== -1 ? w
->ypixel
: (u_int
)ypixel
);
432 window_pane_send_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
434 struct window
*w
= wp
->window
;
440 log_debug("%s: %%%u resize to %u,%u", __func__
, wp
->id
, sx
, sy
);
442 memset(&ws
, 0, sizeof ws
);
445 ws
.ws_xpixel
= w
->xpixel
* ws
.ws_col
;
446 ws
.ws_ypixel
= w
->ypixel
* ws
.ws_row
;
447 if (ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
448 fatal("ioctl failed");
452 window_has_pane(struct window
*w
, struct window_pane
*wp
)
454 struct window_pane
*wp1
;
456 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
464 window_update_focus(struct window
*w
)
467 log_debug("%s: @%u", __func__
, w
->id
);
468 window_pane_update_focus(w
->active
);
473 window_pane_update_focus(struct window_pane
*wp
)
479 if (wp
!= wp
->window
->active
)
482 TAILQ_FOREACH(c
, &clients
, entry
) {
483 if (c
->session
!= NULL
&&
484 c
->session
->attached
!= 0 &&
485 (c
->flags
& CLIENT_FOCUSED
) &&
486 c
->session
->curw
->window
== wp
->window
) {
492 if (!focused
&& (wp
->flags
& PANE_FOCUSED
)) {
493 log_debug("%s: %%%u focus out", __func__
, wp
->id
);
494 if (wp
->base
.mode
& MODE_FOCUSON
)
495 bufferevent_write(wp
->event
, "\033[O", 3);
496 notify_pane("pane-focus-out", wp
);
497 wp
->flags
&= ~PANE_FOCUSED
;
498 } else if (focused
&& (~wp
->flags
& PANE_FOCUSED
)) {
499 log_debug("%s: %%%u focus in", __func__
, wp
->id
);
500 if (wp
->base
.mode
& MODE_FOCUSON
)
501 bufferevent_write(wp
->event
, "\033[I", 3);
502 notify_pane("pane-focus-in", wp
);
503 wp
->flags
|= PANE_FOCUSED
;
505 log_debug("%s: %%%u focus unchanged", __func__
, wp
->id
);
510 window_set_active_pane(struct window
*w
, struct window_pane
*wp
, int notify
)
512 log_debug("%s: pane %%%u", __func__
, wp
->id
);
519 w
->active
->active_point
= next_active_point
++;
520 w
->active
->flags
|= PANE_CHANGED
;
522 if (options_get_number(global_options
, "focus-events")) {
523 window_pane_update_focus(w
->last
);
524 window_pane_update_focus(w
->active
);
527 tty_update_window_offset(w
);
530 notify_window("window-pane-changed", w
);
535 window_pane_get_palette(struct window_pane
*wp
, int c
)
539 return (colour_palette_get(&wp
->palette
, c
));
543 window_redraw_active_switch(struct window
*w
, struct window_pane
*wp
)
545 struct grid_cell
*gc1
, *gc2
;
553 * If the active and inactive styles or palettes are different,
554 * need to redraw the panes.
556 gc1
= &wp
->cached_gc
;
557 gc2
= &wp
->cached_active_gc
;
558 if (!grid_cells_look_equal(gc1
, gc2
))
559 wp
->flags
|= PANE_REDRAW
;
561 c1
= window_pane_get_palette(wp
, gc1
->fg
);
562 c2
= window_pane_get_palette(wp
, gc2
->fg
);
564 wp
->flags
|= PANE_REDRAW
;
566 c1
= window_pane_get_palette(wp
, gc1
->bg
);
567 c2
= window_pane_get_palette(wp
, gc2
->bg
);
569 wp
->flags
|= PANE_REDRAW
;
579 window_get_active_at(struct window
*w
, u_int x
, u_int y
)
581 struct window_pane
*wp
;
583 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
584 if (!window_pane_visible(wp
))
586 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
588 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
596 window_find_string(struct window
*w
, const char *s
)
598 u_int x
, y
, top
= 0, bottom
= w
->sy
- 1;
604 status
= options_get_number(w
->options
, "pane-border-status");
605 if (status
== PANE_STATUS_TOP
)
607 else if (status
== PANE_STATUS_BOTTOM
)
610 if (strcasecmp(s
, "top") == 0)
612 else if (strcasecmp(s
, "bottom") == 0)
614 else if (strcasecmp(s
, "left") == 0)
616 else if (strcasecmp(s
, "right") == 0)
618 else if (strcasecmp(s
, "top-left") == 0) {
621 } else if (strcasecmp(s
, "top-right") == 0) {
624 } else if (strcasecmp(s
, "bottom-left") == 0) {
627 } else if (strcasecmp(s
, "bottom-right") == 0) {
633 return (window_get_active_at(w
, x
, y
));
637 window_zoom(struct window_pane
*wp
)
639 struct window
*w
= wp
->window
;
640 struct window_pane
*wp1
;
642 if (w
->flags
& WINDOW_ZOOMED
)
645 if (window_count_panes(w
) == 1)
649 window_set_active_pane(w
, wp
, 1);
651 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
652 wp1
->saved_layout_cell
= wp1
->layout_cell
;
653 wp1
->layout_cell
= NULL
;
656 w
->saved_layout_root
= w
->layout_root
;
658 w
->flags
|= WINDOW_ZOOMED
;
659 notify_window("window-layout-changed", w
);
665 window_unzoom(struct window
*w
)
667 struct window_pane
*wp
;
669 if (!(w
->flags
& WINDOW_ZOOMED
))
672 w
->flags
&= ~WINDOW_ZOOMED
;
674 w
->layout_root
= w
->saved_layout_root
;
675 w
->saved_layout_root
= NULL
;
677 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
678 wp
->layout_cell
= wp
->saved_layout_cell
;
679 wp
->saved_layout_cell
= NULL
;
681 layout_fix_panes(w
, NULL
);
682 notify_window("window-layout-changed", w
);
688 window_push_zoom(struct window
*w
, int always
, int flag
)
690 log_debug("%s: @%u %d", __func__
, w
->id
,
691 flag
&& (w
->flags
& WINDOW_ZOOMED
));
692 if (flag
&& (always
|| (w
->flags
& WINDOW_ZOOMED
)))
693 w
->flags
|= WINDOW_WASZOOMED
;
695 w
->flags
&= ~WINDOW_WASZOOMED
;
696 return (window_unzoom(w
) == 0);
700 window_pop_zoom(struct window
*w
)
702 log_debug("%s: @%u %d", __func__
, w
->id
,
703 !!(w
->flags
& WINDOW_WASZOOMED
));
704 if (w
->flags
& WINDOW_WASZOOMED
)
705 return (window_zoom(w
->active
) == 0);
710 window_add_pane(struct window
*w
, struct window_pane
*other
, u_int hlimit
,
713 struct window_pane
*wp
;
718 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
719 if (TAILQ_EMPTY(&w
->panes
)) {
720 log_debug("%s: @%u at start", __func__
, w
->id
);
721 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
722 } else if (flags
& SPAWN_BEFORE
) {
723 log_debug("%s: @%u before %%%u", __func__
, w
->id
, wp
->id
);
724 if (flags
& SPAWN_FULLSIZE
)
725 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
727 TAILQ_INSERT_BEFORE(other
, wp
, entry
);
729 log_debug("%s: @%u after %%%u", __func__
, w
->id
, wp
->id
);
730 if (flags
& SPAWN_FULLSIZE
)
731 TAILQ_INSERT_TAIL(&w
->panes
, wp
, entry
);
733 TAILQ_INSERT_AFTER(&w
->panes
, other
, wp
, entry
);
739 window_lost_pane(struct window
*w
, struct window_pane
*wp
)
741 log_debug("%s: @%u pane %%%u", __func__
, w
->id
, wp
->id
);
743 if (wp
== marked_pane
.wp
)
744 server_clear_marked();
746 if (wp
== w
->active
) {
749 if (w
->active
== NULL
) {
750 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
751 if (w
->active
== NULL
)
752 w
->active
= TAILQ_NEXT(wp
, entry
);
754 if (w
->active
!= NULL
) {
755 w
->active
->flags
|= PANE_CHANGED
;
756 notify_window("window-pane-changed", w
);
757 window_update_focus(w
);
759 } else if (wp
== w
->last
)
764 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
766 window_lost_pane(w
, wp
);
768 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
769 window_pane_destroy(wp
);
773 window_pane_at_index(struct window
*w
, u_int idx
)
775 struct window_pane
*wp
;
778 n
= options_get_number(w
->options
, "pane-base-index");
779 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
788 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
791 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
792 wp
= TAILQ_FIRST(&w
->panes
);
799 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
803 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
804 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
811 window_pane_index(struct window_pane
*wp
, u_int
*i
)
813 struct window_pane
*wq
;
814 struct window
*w
= wp
->window
;
816 *i
= options_get_number(w
->options
, "pane-base-index");
817 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
828 window_count_panes(struct window
*w
)
830 struct window_pane
*wp
;
834 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
840 window_destroy_panes(struct window
*w
)
842 struct window_pane
*wp
;
844 while (!TAILQ_EMPTY(&w
->panes
)) {
845 wp
= TAILQ_FIRST(&w
->panes
);
846 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
847 window_pane_destroy(wp
);
852 window_printable_flags(struct winlink
*wl
, int escape
)
854 struct session
*s
= wl
->session
;
855 static char flags
[32];
859 if (wl
->flags
& WINLINK_ACTIVITY
) {
864 if (wl
->flags
& WINLINK_BELL
)
866 if (wl
->flags
& WINLINK_SILENCE
)
870 if (wl
== TAILQ_FIRST(&s
->lastw
))
872 if (server_check_marked() && wl
== marked_pane
.wl
)
874 if (wl
->window
->flags
& WINDOW_ZOOMED
)
881 window_pane_find_by_id_str(const char *s
)
889 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
892 return (window_pane_find_by_id(id
));
896 window_pane_find_by_id(u_int id
)
898 struct window_pane wp
;
901 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
904 static struct window_pane
*
905 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
907 struct window_pane
*wp
;
908 char host
[HOST_NAME_MAX
+ 1];
910 wp
= xcalloc(1, sizeof *wp
);
912 wp
->options
= options_create(w
->options
);
913 wp
->flags
= PANE_STYLECHANGED
;
915 wp
->id
= next_window_pane_id
++;
916 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
920 TAILQ_INIT(&wp
->modes
);
922 TAILQ_INIT (&wp
->resize_queue
);
929 colour_palette_init(&wp
->palette
);
930 colour_palette_from_option(&wp
->palette
, wp
->options
);
932 screen_init(&wp
->base
, sx
, sy
, hlimit
);
933 wp
->screen
= &wp
->base
;
935 screen_init(&wp
->status_screen
, 1, 1, 0);
937 if (gethostname(host
, sizeof host
) == 0)
938 screen_set_title(&wp
->base
, host
);
944 window_pane_destroy(struct window_pane
*wp
)
946 struct window_pane_resize
*r
;
947 struct window_pane_resize
*r1
;
949 window_pane_reset_mode_all(wp
);
953 bufferevent_free(wp
->event
);
956 if (wp
->ictx
!= NULL
)
957 input_free(wp
->ictx
);
959 screen_free(&wp
->status_screen
);
961 screen_free(&wp
->base
);
963 if (wp
->pipe_fd
!= -1) {
964 bufferevent_free(wp
->pipe_event
);
968 if (event_initialized(&wp
->resize_timer
))
969 event_del(&wp
->resize_timer
);
970 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
971 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
975 RB_REMOVE(window_pane_tree
, &all_window_panes
, wp
);
977 options_free(wp
->options
);
978 free((void *)wp
->cwd
);
980 cmd_free_argv(wp
->argc
, wp
->argv
);
981 colour_palette_free(&wp
->palette
);
986 window_pane_read_callback(__unused
struct bufferevent
*bufev
, void *data
)
988 struct window_pane
*wp
= data
;
989 struct evbuffer
*evb
= wp
->event
->input
;
990 struct window_pane_offset
*wpo
= &wp
->pipe_offset
;
991 size_t size
= EVBUFFER_LENGTH(evb
);
996 if (wp
->pipe_fd
!= -1) {
997 new_data
= window_pane_get_new_data(wp
, wpo
, &new_size
);
999 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
1000 window_pane_update_used_data(wp
, wpo
, new_size
);
1004 log_debug("%%%u has %zu bytes", wp
->id
, size
);
1005 TAILQ_FOREACH(c
, &clients
, entry
) {
1006 if (c
->session
!= NULL
&& (c
->flags
& CLIENT_CONTROL
))
1007 control_write_output(c
, wp
);
1009 input_parse_pane(wp
);
1010 bufferevent_disable(wp
->event
, EV_READ
);
1014 window_pane_error_callback(__unused
struct bufferevent
*bufev
,
1015 __unused
short what
, void *data
)
1017 struct window_pane
*wp
= data
;
1019 log_debug("%%%u error", wp
->id
);
1020 wp
->flags
|= PANE_EXITED
;
1022 if (window_pane_destroy_ready(wp
))
1023 server_destroy_pane(wp
, 1);
1027 window_pane_set_event(struct window_pane
*wp
)
1029 setblocking(wp
->fd
, 0);
1031 wp
->event
= bufferevent_new(wp
->fd
, window_pane_read_callback
,
1032 NULL
, window_pane_error_callback
, wp
);
1033 wp
->ictx
= input_init(wp
, wp
->event
, &wp
->palette
);
1035 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
1039 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
1041 struct window_mode_entry
*wme
;
1042 struct window_pane_resize
*r
;
1044 if (sx
== wp
->sx
&& sy
== wp
->sy
)
1047 r
= xmalloc (sizeof *r
);
1052 TAILQ_INSERT_TAIL (&wp
->resize_queue
, r
, entry
);
1057 log_debug("%s: %%%u resize %ux%u", __func__
, wp
->id
, sx
, sy
);
1058 screen_resize(&wp
->base
, sx
, sy
, wp
->base
.saved_grid
== NULL
);
1060 wme
= TAILQ_FIRST(&wp
->modes
);
1061 if (wme
!= NULL
&& wme
->mode
->resize
!= NULL
)
1062 wme
->mode
->resize(wme
, sx
, sy
);
1066 window_pane_set_mode(struct window_pane
*wp
, struct window_pane
*swp
,
1067 const struct window_mode
*mode
, struct cmd_find_state
*fs
,
1070 struct window_mode_entry
*wme
;
1072 if (!TAILQ_EMPTY(&wp
->modes
) && TAILQ_FIRST(&wp
->modes
)->mode
== mode
)
1075 TAILQ_FOREACH(wme
, &wp
->modes
, entry
) {
1076 if (wme
->mode
== mode
)
1080 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1081 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1083 wme
= xcalloc(1, sizeof *wme
);
1088 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1089 wme
->screen
= wme
->mode
->init(wme
, fs
, args
);
1092 wp
->screen
= wme
->screen
;
1093 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1095 server_redraw_window_borders(wp
->window
);
1096 server_status_window(wp
->window
);
1097 notify_pane("pane-mode-changed", wp
);
1103 window_pane_reset_mode(struct window_pane
*wp
)
1105 struct window_mode_entry
*wme
, *next
;
1107 if (TAILQ_EMPTY(&wp
->modes
))
1110 wme
= TAILQ_FIRST(&wp
->modes
);
1111 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1112 wme
->mode
->free(wme
);
1115 next
= TAILQ_FIRST(&wp
->modes
);
1117 log_debug("%s: no next mode", __func__
);
1118 wp
->screen
= &wp
->base
;
1120 log_debug("%s: next mode is %s", __func__
, next
->mode
->name
);
1121 wp
->screen
= next
->screen
;
1122 if (next
->mode
->resize
!= NULL
)
1123 next
->mode
->resize(next
, wp
->sx
, wp
->sy
);
1125 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1127 server_redraw_window_borders(wp
->window
);
1128 server_status_window(wp
->window
);
1129 notify_pane("pane-mode-changed", wp
);
1133 window_pane_reset_mode_all(struct window_pane
*wp
)
1135 while (!TAILQ_EMPTY(&wp
->modes
))
1136 window_pane_reset_mode(wp
);
1140 window_pane_copy_key(struct window_pane
*wp
, key_code key
)
1142 struct window_pane
*loop
;
1144 TAILQ_FOREACH(loop
, &wp
->window
->panes
, entry
) {
1146 TAILQ_EMPTY(&loop
->modes
) &&
1148 (~loop
->flags
& PANE_INPUTOFF
) &&
1149 window_pane_visible(loop
) &&
1150 options_get_number(loop
->options
, "synchronize-panes"))
1151 input_key_pane(loop
, key
, NULL
);
1156 window_pane_key(struct window_pane
*wp
, struct client
*c
, struct session
*s
,
1157 struct winlink
*wl
, key_code key
, struct mouse_event
*m
)
1159 struct window_mode_entry
*wme
;
1161 if (KEYC_IS_MOUSE(key
) && m
== NULL
)
1164 wme
= TAILQ_FIRST(&wp
->modes
);
1166 if (wme
->mode
->key
!= NULL
&& c
!= NULL
) {
1167 key
&= ~KEYC_MASK_FLAGS
;
1168 wme
->mode
->key(wme
, c
, s
, wl
, key
, m
);
1173 if (wp
->fd
== -1 || wp
->flags
& PANE_INPUTOFF
)
1176 if (input_key_pane(wp
, key
, m
) != 0)
1179 if (KEYC_IS_MOUSE(key
))
1181 if (options_get_number(wp
->options
, "synchronize-panes"))
1182 window_pane_copy_key(wp
, key
);
1187 window_pane_visible(struct window_pane
*wp
)
1189 if (~wp
->window
->flags
& WINDOW_ZOOMED
)
1191 return (wp
== wp
->window
->active
);
1195 window_pane_search(struct window_pane
*wp
, const char *term
, int regex
,
1198 struct screen
*s
= &wp
->base
;
1200 char *new = NULL
, *line
;
1202 int flags
= 0, found
;
1207 flags
|= FNM_CASEFOLD
;
1208 xasprintf(&new, "*%s*", term
);
1212 if (regcomp(&r
, term
, flags
|REG_EXTENDED
) != 0)
1216 for (i
= 0; i
< screen_size_y(s
); i
++) {
1217 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
1218 for (n
= strlen(line
); n
> 0; n
--) {
1219 if (!isspace((u_char
)line
[n
- 1]))
1223 log_debug("%s: %s", __func__
, line
);
1225 found
= (fnmatch(new, line
, flags
) == 0);
1227 found
= (regexec(&r
, line
, 0, NULL
, 0) == 0);
1237 if (i
== screen_size_y(s
))
1242 /* Get MRU pane from a list. */
1243 static struct window_pane
*
1244 window_pane_choose_best(struct window_pane
**list
, u_int size
)
1246 struct window_pane
*next
, *best
;
1253 for (i
= 1; i
< size
; i
++) {
1255 if (next
->active_point
> best
->active_point
)
1262 * Find the pane directly above another. We build a list of those adjacent to
1263 * top edge and then choose the best.
1265 struct window_pane
*
1266 window_pane_find_up(struct window_pane
*wp
)
1269 struct window_pane
*next
, *best
, **list
;
1270 u_int edge
, left
, right
, end
, size
;
1276 status
= options_get_number(w
->options
, "pane-border-status");
1282 if (status
== PANE_STATUS_TOP
) {
1285 } else if (status
== PANE_STATUS_BOTTOM
) {
1294 right
= wp
->xoff
+ wp
->sx
;
1296 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1299 if (next
->yoff
+ next
->sy
+ 1 != edge
)
1301 end
= next
->xoff
+ next
->sx
- 1;
1304 if (next
->xoff
< left
&& end
> right
)
1306 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1308 else if (end
>= left
&& end
<= right
)
1312 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1313 list
[size
++] = next
;
1316 best
= window_pane_choose_best(list
, size
);
1321 /* Find the pane directly below another. */
1322 struct window_pane
*
1323 window_pane_find_down(struct window_pane
*wp
)
1326 struct window_pane
*next
, *best
, **list
;
1327 u_int edge
, left
, right
, end
, size
;
1333 status
= options_get_number(w
->options
, "pane-border-status");
1338 edge
= wp
->yoff
+ wp
->sy
+ 1;
1339 if (status
== PANE_STATUS_TOP
) {
1342 } else if (status
== PANE_STATUS_BOTTOM
) {
1343 if (edge
>= w
->sy
- 1)
1351 right
= wp
->xoff
+ wp
->sx
;
1353 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1356 if (next
->yoff
!= edge
)
1358 end
= next
->xoff
+ next
->sx
- 1;
1361 if (next
->xoff
< left
&& end
> right
)
1363 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1365 else if (end
>= left
&& end
<= right
)
1369 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1370 list
[size
++] = next
;
1373 best
= window_pane_choose_best(list
, size
);
1378 /* Find the pane directly to the left of another. */
1379 struct window_pane
*
1380 window_pane_find_left(struct window_pane
*wp
)
1383 struct window_pane
*next
, *best
, **list
;
1384 u_int edge
, top
, bottom
, end
, size
;
1399 bottom
= wp
->yoff
+ wp
->sy
;
1401 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1404 if (next
->xoff
+ next
->sx
+ 1 != edge
)
1406 end
= next
->yoff
+ next
->sy
- 1;
1409 if (next
->yoff
< top
&& end
> bottom
)
1411 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1413 else if (end
>= top
&& end
<= bottom
)
1417 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1418 list
[size
++] = next
;
1421 best
= window_pane_choose_best(list
, size
);
1426 /* Find the pane directly to the right of another. */
1427 struct window_pane
*
1428 window_pane_find_right(struct window_pane
*wp
)
1431 struct window_pane
*next
, *best
, **list
;
1432 u_int edge
, top
, bottom
, end
, size
;
1442 edge
= wp
->xoff
+ wp
->sx
+ 1;
1447 bottom
= wp
->yoff
+ wp
->sy
;
1449 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1452 if (next
->xoff
!= edge
)
1454 end
= next
->yoff
+ next
->sy
- 1;
1457 if (next
->yoff
< top
&& end
> bottom
)
1459 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1461 else if (end
>= top
&& end
<= bottom
)
1465 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1466 list
[size
++] = next
;
1469 best
= window_pane_choose_best(list
, size
);
1474 /* Clear alert flags for a winlink */
1476 winlink_clear_flags(struct winlink
*wl
)
1478 struct winlink
*loop
;
1480 wl
->window
->flags
&= ~WINDOW_ALERTFLAGS
;
1481 TAILQ_FOREACH(loop
, &wl
->window
->winlinks
, wentry
) {
1482 if ((loop
->flags
& WINLINK_ALERTFLAGS
) != 0) {
1483 loop
->flags
&= ~WINLINK_ALERTFLAGS
;
1484 server_status_session(loop
->session
);
1489 /* Shuffle window indexes up. */
1491 winlink_shuffle_up(struct session
*s
, struct winlink
*wl
, int before
)
1502 /* Find the next free index. */
1503 for (last
= idx
; last
< INT_MAX
; last
++) {
1504 if (winlink_find_by_index(&s
->windows
, last
) == NULL
)
1507 if (last
== INT_MAX
)
1510 /* Move everything from last - 1 to idx up a bit. */
1511 for (; last
> idx
; last
--) {
1512 wl
= winlink_find_by_index(&s
->windows
, last
- 1);
1513 RB_REMOVE(winlinks
, &s
->windows
, wl
);
1515 RB_INSERT(winlinks
, &s
->windows
, wl
);
1522 window_pane_input_callback(struct client
*c
, __unused
const char *path
,
1523 int error
, int closed
, struct evbuffer
*buffer
, void *data
)
1525 struct window_pane_input_data
*cdata
= data
;
1526 struct window_pane
*wp
;
1527 u_char
*buf
= EVBUFFER_DATA(buffer
);
1528 size_t len
= EVBUFFER_LENGTH(buffer
);
1530 wp
= window_pane_find_by_id(cdata
->wp
);
1531 if (wp
== NULL
|| closed
|| error
!= 0 || (c
->flags
& CLIENT_DEAD
)) {
1533 c
->flags
|= CLIENT_EXIT
;
1535 evbuffer_drain(buffer
, len
);
1536 cmdq_continue(cdata
->item
);
1538 server_client_unref(c
);
1542 input_parse_buffer(wp
, buf
, len
);
1543 evbuffer_drain(buffer
, len
);
1547 window_pane_start_input(struct window_pane
*wp
, struct cmdq_item
*item
,
1550 struct client
*c
= cmdq_get_client(item
);
1551 struct window_pane_input_data
*cdata
;
1553 if (~wp
->flags
& PANE_EMPTY
) {
1554 *cause
= xstrdup("pane is not empty");
1557 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
1559 if (c
->session
!= NULL
)
1562 cdata
= xmalloc(sizeof *cdata
);
1567 file_read(c
, "-", window_pane_input_callback
, cdata
);
1573 window_pane_get_new_data(struct window_pane
*wp
,
1574 struct window_pane_offset
*wpo
, size_t *size
)
1576 size_t used
= wpo
->used
- wp
->base_offset
;
1578 *size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1579 return (EVBUFFER_DATA(wp
->event
->input
) + used
);
1583 window_pane_update_used_data(struct window_pane
*wp
,
1584 struct window_pane_offset
*wpo
, size_t size
)
1586 size_t used
= wpo
->used
- wp
->base_offset
;
1588 if (size
> EVBUFFER_LENGTH(wp
->event
->input
) - used
)
1589 size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;