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
;
69 struct client_file
*file
;
72 static struct window_pane
*window_pane_create(struct window
*, u_int
, u_int
,
74 static void window_pane_destroy(struct window_pane
*);
76 RB_GENERATE(windows
, window
, entry
, window_cmp
);
77 RB_GENERATE(winlinks
, winlink
, entry
, winlink_cmp
);
78 RB_GENERATE(window_pane_tree
, window_pane
, tree_entry
, window_pane_cmp
);
81 window_cmp(struct window
*w1
, struct window
*w2
)
83 return (w1
->id
- w2
->id
);
87 winlink_cmp(struct winlink
*wl1
, struct winlink
*wl2
)
89 return (wl1
->idx
- wl2
->idx
);
93 window_pane_cmp(struct window_pane
*wp1
, struct window_pane
*wp2
)
95 return (wp1
->id
- wp2
->id
);
99 winlink_find_by_window(struct winlinks
*wwl
, struct window
*w
)
103 RB_FOREACH(wl
, winlinks
, wwl
) {
112 winlink_find_by_index(struct winlinks
*wwl
, int idx
)
120 return (RB_FIND(winlinks
, wwl
, &wl
));
124 winlink_find_by_window_id(struct winlinks
*wwl
, u_int id
)
128 RB_FOREACH(wl
, winlinks
, wwl
) {
129 if (wl
->window
->id
== id
)
136 winlink_next_index(struct winlinks
*wwl
, int idx
)
142 if (winlink_find_by_index(wwl
, i
) == NULL
)
153 winlink_count(struct winlinks
*wwl
)
159 RB_FOREACH(wl
, winlinks
, wwl
)
166 winlink_add(struct winlinks
*wwl
, int idx
)
171 if ((idx
= winlink_next_index(wwl
, -idx
- 1)) == -1)
173 } else if (winlink_find_by_index(wwl
, idx
) != NULL
)
176 wl
= xcalloc(1, sizeof *wl
);
178 RB_INSERT(winlinks
, wwl
, wl
);
184 winlink_set_window(struct winlink
*wl
, struct window
*w
)
186 if (wl
->window
!= NULL
) {
187 TAILQ_REMOVE(&wl
->window
->winlinks
, wl
, wentry
);
188 window_remove_ref(wl
->window
, __func__
);
190 TAILQ_INSERT_TAIL(&w
->winlinks
, wl
, wentry
);
192 window_add_ref(w
, __func__
);
196 winlink_remove(struct winlinks
*wwl
, struct winlink
*wl
)
198 struct window
*w
= wl
->window
;
201 TAILQ_REMOVE(&w
->winlinks
, wl
, wentry
);
202 window_remove_ref(w
, __func__
);
205 RB_REMOVE(winlinks
, wwl
, wl
);
210 winlink_next(struct winlink
*wl
)
212 return (RB_NEXT(winlinks
, wwl
, wl
));
216 winlink_previous(struct winlink
*wl
)
218 return (RB_PREV(winlinks
, wwl
, wl
));
222 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
225 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
226 wl
= RB_MIN(winlinks
, &s
->windows
);
233 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
236 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
237 wl
= RB_MAX(winlinks
, &s
->windows
);
244 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
249 winlink_stack_remove(stack
, wl
);
250 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
254 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
261 TAILQ_FOREACH(wl2
, stack
, sentry
) {
263 TAILQ_REMOVE(stack
, wl
, sentry
);
270 window_find_by_id_str(const char *s
)
278 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
281 return (window_find_by_id(id
));
285 window_find_by_id(u_int id
)
290 return (RB_FIND(windows
, &windows
, &w
));
294 window_update_activity(struct window
*w
)
296 gettimeofday(&w
->activity_time
, NULL
);
297 alerts_queue(w
, WINDOW_ACTIVITY
);
301 window_create(u_int sx
, u_int sy
, u_int xpixel
, u_int ypixel
)
306 xpixel
= DEFAULT_XPIXEL
;
308 ypixel
= DEFAULT_YPIXEL
;
310 w
= xcalloc(1, sizeof *w
);
311 w
->name
= xstrdup("");
314 TAILQ_INIT(&w
->panes
);
318 w
->layout_root
= NULL
;
327 w
->options
= options_create(global_w_options
);
330 TAILQ_INIT(&w
->winlinks
);
332 w
->id
= next_window_id
++;
333 RB_INSERT(windows
, &windows
, w
);
335 window_set_fill_character(w
);
336 window_update_activity(w
);
338 log_debug("%s: @%u create %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
339 w
->xpixel
, w
->ypixel
);
344 window_destroy(struct window
*w
)
346 log_debug("window @%u destroyed (%d references)", w
->id
, w
->references
);
348 RB_REMOVE(windows
, &windows
, w
);
350 if (w
->layout_root
!= NULL
)
351 layout_free_cell(w
->layout_root
);
352 if (w
->saved_layout_root
!= NULL
)
353 layout_free_cell(w
->saved_layout_root
);
356 window_destroy_panes(w
);
358 if (event_initialized(&w
->name_event
))
359 evtimer_del(&w
->name_event
);
361 if (event_initialized(&w
->alerts_timer
))
362 evtimer_del(&w
->alerts_timer
);
363 if (event_initialized(&w
->offset_timer
))
364 event_del(&w
->offset_timer
);
366 options_free(w
->options
);
367 free(w
->fill_character
);
374 window_pane_destroy_ready(struct window_pane
*wp
)
378 if (wp
->pipe_fd
!= -1) {
379 if (EVBUFFER_LENGTH(wp
->pipe_event
->output
) != 0)
381 if (ioctl(wp
->fd
, FIONREAD
, &n
) != -1 && n
> 0)
385 if (~wp
->flags
& PANE_EXITED
)
391 window_add_ref(struct window
*w
, const char *from
)
394 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
398 window_remove_ref(struct window
*w
, const char *from
)
401 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
403 if (w
->references
== 0)
408 window_set_name(struct window
*w
, const char *new_name
)
411 utf8_stravis(&w
->name
, new_name
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
412 notify_window("window-renamed", w
);
416 window_resize(struct window
*w
, u_int sx
, u_int sy
, int xpixel
, int ypixel
)
419 xpixel
= DEFAULT_XPIXEL
;
421 ypixel
= DEFAULT_YPIXEL
;
423 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
424 xpixel
== -1 ? w
->xpixel
: (u_int
)xpixel
,
425 ypixel
== -1 ? w
->ypixel
: (u_int
)ypixel
);
435 window_pane_send_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
437 struct window
*w
= wp
->window
;
443 log_debug("%s: %%%u resize to %u,%u", __func__
, wp
->id
, sx
, sy
);
445 memset(&ws
, 0, sizeof ws
);
448 ws
.ws_xpixel
= w
->xpixel
* ws
.ws_col
;
449 ws
.ws_ypixel
= w
->ypixel
* ws
.ws_row
;
450 if (ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
451 fatal("ioctl failed");
455 window_has_pane(struct window
*w
, struct window_pane
*wp
)
457 struct window_pane
*wp1
;
459 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
467 window_update_focus(struct window
*w
)
470 log_debug("%s: @%u", __func__
, w
->id
);
471 window_pane_update_focus(w
->active
);
476 window_pane_update_focus(struct window_pane
*wp
)
482 if (wp
!= wp
->window
->active
)
485 TAILQ_FOREACH(c
, &clients
, entry
) {
486 if (c
->session
!= NULL
&&
487 c
->session
->attached
!= 0 &&
488 (c
->flags
& CLIENT_FOCUSED
) &&
489 c
->session
->curw
->window
== wp
->window
) {
495 if (!focused
&& (wp
->flags
& PANE_FOCUSED
)) {
496 log_debug("%s: %%%u focus out", __func__
, wp
->id
);
497 if (wp
->base
.mode
& MODE_FOCUSON
)
498 bufferevent_write(wp
->event
, "\033[O", 3);
499 notify_pane("pane-focus-out", wp
);
500 wp
->flags
&= ~PANE_FOCUSED
;
501 } else if (focused
&& (~wp
->flags
& PANE_FOCUSED
)) {
502 log_debug("%s: %%%u focus in", __func__
, wp
->id
);
503 if (wp
->base
.mode
& MODE_FOCUSON
)
504 bufferevent_write(wp
->event
, "\033[I", 3);
505 notify_pane("pane-focus-in", wp
);
506 wp
->flags
|= PANE_FOCUSED
;
508 log_debug("%s: %%%u focus unchanged", __func__
, wp
->id
);
513 window_set_active_pane(struct window
*w
, struct window_pane
*wp
, int notify
)
515 log_debug("%s: pane %%%u", __func__
, wp
->id
);
522 w
->active
->active_point
= next_active_point
++;
523 w
->active
->flags
|= PANE_CHANGED
;
525 if (options_get_number(global_options
, "focus-events")) {
526 window_pane_update_focus(w
->last
);
527 window_pane_update_focus(w
->active
);
530 tty_update_window_offset(w
);
533 notify_window("window-pane-changed", w
);
538 window_pane_get_palette(struct window_pane
*wp
, int c
)
542 return (colour_palette_get(&wp
->palette
, c
));
546 window_redraw_active_switch(struct window
*w
, struct window_pane
*wp
)
548 struct grid_cell
*gc1
, *gc2
;
556 * If the active and inactive styles or palettes are different,
557 * need to redraw the panes.
559 gc1
= &wp
->cached_gc
;
560 gc2
= &wp
->cached_active_gc
;
561 if (!grid_cells_look_equal(gc1
, gc2
))
562 wp
->flags
|= PANE_REDRAW
;
564 c1
= window_pane_get_palette(wp
, gc1
->fg
);
565 c2
= window_pane_get_palette(wp
, gc2
->fg
);
567 wp
->flags
|= PANE_REDRAW
;
569 c1
= window_pane_get_palette(wp
, gc1
->bg
);
570 c2
= window_pane_get_palette(wp
, gc2
->bg
);
572 wp
->flags
|= PANE_REDRAW
;
582 window_get_active_at(struct window
*w
, u_int x
, u_int y
)
584 struct window_pane
*wp
;
586 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
587 if (!window_pane_visible(wp
))
589 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
591 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
599 window_find_string(struct window
*w
, const char *s
)
601 u_int x
, y
, top
= 0, bottom
= w
->sy
- 1;
607 status
= options_get_number(w
->options
, "pane-border-status");
608 if (status
== PANE_STATUS_TOP
)
610 else if (status
== PANE_STATUS_BOTTOM
)
613 if (strcasecmp(s
, "top") == 0)
615 else if (strcasecmp(s
, "bottom") == 0)
617 else if (strcasecmp(s
, "left") == 0)
619 else if (strcasecmp(s
, "right") == 0)
621 else if (strcasecmp(s
, "top-left") == 0) {
624 } else if (strcasecmp(s
, "top-right") == 0) {
627 } else if (strcasecmp(s
, "bottom-left") == 0) {
630 } else if (strcasecmp(s
, "bottom-right") == 0) {
636 return (window_get_active_at(w
, x
, y
));
640 window_zoom(struct window_pane
*wp
)
642 struct window
*w
= wp
->window
;
643 struct window_pane
*wp1
;
645 if (w
->flags
& WINDOW_ZOOMED
)
648 if (window_count_panes(w
) == 1)
652 window_set_active_pane(w
, wp
, 1);
654 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
655 wp1
->saved_layout_cell
= wp1
->layout_cell
;
656 wp1
->layout_cell
= NULL
;
659 w
->saved_layout_root
= w
->layout_root
;
661 w
->flags
|= WINDOW_ZOOMED
;
662 notify_window("window-layout-changed", w
);
668 window_unzoom(struct window
*w
)
670 struct window_pane
*wp
;
672 if (!(w
->flags
& WINDOW_ZOOMED
))
675 w
->flags
&= ~WINDOW_ZOOMED
;
677 w
->layout_root
= w
->saved_layout_root
;
678 w
->saved_layout_root
= NULL
;
680 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
681 wp
->layout_cell
= wp
->saved_layout_cell
;
682 wp
->saved_layout_cell
= NULL
;
684 layout_fix_panes(w
, NULL
);
685 notify_window("window-layout-changed", w
);
691 window_push_zoom(struct window
*w
, int always
, int flag
)
693 log_debug("%s: @%u %d", __func__
, w
->id
,
694 flag
&& (w
->flags
& WINDOW_ZOOMED
));
695 if (flag
&& (always
|| (w
->flags
& WINDOW_ZOOMED
)))
696 w
->flags
|= WINDOW_WASZOOMED
;
698 w
->flags
&= ~WINDOW_WASZOOMED
;
699 return (window_unzoom(w
) == 0);
703 window_pop_zoom(struct window
*w
)
705 log_debug("%s: @%u %d", __func__
, w
->id
,
706 !!(w
->flags
& WINDOW_WASZOOMED
));
707 if (w
->flags
& WINDOW_WASZOOMED
)
708 return (window_zoom(w
->active
) == 0);
713 window_add_pane(struct window
*w
, struct window_pane
*other
, u_int hlimit
,
716 struct window_pane
*wp
;
721 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
722 if (TAILQ_EMPTY(&w
->panes
)) {
723 log_debug("%s: @%u at start", __func__
, w
->id
);
724 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
725 } else if (flags
& SPAWN_BEFORE
) {
726 log_debug("%s: @%u before %%%u", __func__
, w
->id
, wp
->id
);
727 if (flags
& SPAWN_FULLSIZE
)
728 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
730 TAILQ_INSERT_BEFORE(other
, wp
, entry
);
732 log_debug("%s: @%u after %%%u", __func__
, w
->id
, wp
->id
);
733 if (flags
& SPAWN_FULLSIZE
)
734 TAILQ_INSERT_TAIL(&w
->panes
, wp
, entry
);
736 TAILQ_INSERT_AFTER(&w
->panes
, other
, wp
, entry
);
742 window_lost_pane(struct window
*w
, struct window_pane
*wp
)
744 log_debug("%s: @%u pane %%%u", __func__
, w
->id
, wp
->id
);
746 if (wp
== marked_pane
.wp
)
747 server_clear_marked();
749 if (wp
== w
->active
) {
752 if (w
->active
== NULL
) {
753 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
754 if (w
->active
== NULL
)
755 w
->active
= TAILQ_NEXT(wp
, entry
);
757 if (w
->active
!= NULL
) {
758 w
->active
->flags
|= PANE_CHANGED
;
759 notify_window("window-pane-changed", w
);
760 window_update_focus(w
);
762 } else if (wp
== w
->last
)
767 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
769 window_lost_pane(w
, wp
);
771 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
772 window_pane_destroy(wp
);
776 window_pane_at_index(struct window
*w
, u_int idx
)
778 struct window_pane
*wp
;
781 n
= options_get_number(w
->options
, "pane-base-index");
782 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
791 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
794 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
795 wp
= TAILQ_FIRST(&w
->panes
);
802 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
806 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
807 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
814 window_pane_index(struct window_pane
*wp
, u_int
*i
)
816 struct window_pane
*wq
;
817 struct window
*w
= wp
->window
;
819 *i
= options_get_number(w
->options
, "pane-base-index");
820 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
831 window_count_panes(struct window
*w
)
833 struct window_pane
*wp
;
837 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
843 window_destroy_panes(struct window
*w
)
845 struct window_pane
*wp
;
847 while (!TAILQ_EMPTY(&w
->panes
)) {
848 wp
= TAILQ_FIRST(&w
->panes
);
849 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
850 window_pane_destroy(wp
);
855 window_printable_flags(struct winlink
*wl
, int escape
)
857 struct session
*s
= wl
->session
;
858 static char flags
[32];
862 if (wl
->flags
& WINLINK_ACTIVITY
) {
867 if (wl
->flags
& WINLINK_BELL
)
869 if (wl
->flags
& WINLINK_SILENCE
)
873 if (wl
== TAILQ_FIRST(&s
->lastw
))
875 if (server_check_marked() && wl
== marked_pane
.wl
)
877 if (wl
->window
->flags
& WINDOW_ZOOMED
)
884 window_pane_find_by_id_str(const char *s
)
892 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
895 return (window_pane_find_by_id(id
));
899 window_pane_find_by_id(u_int id
)
901 struct window_pane wp
;
904 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
907 static struct window_pane
*
908 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
910 struct window_pane
*wp
;
911 char host
[HOST_NAME_MAX
+ 1];
913 wp
= xcalloc(1, sizeof *wp
);
915 wp
->options
= options_create(w
->options
);
916 wp
->flags
= PANE_STYLECHANGED
;
918 wp
->id
= next_window_pane_id
++;
919 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
923 TAILQ_INIT(&wp
->modes
);
925 TAILQ_INIT (&wp
->resize_queue
);
932 colour_palette_init(&wp
->palette
);
933 colour_palette_from_option(&wp
->palette
, wp
->options
);
935 screen_init(&wp
->base
, sx
, sy
, hlimit
);
936 wp
->screen
= &wp
->base
;
937 window_pane_default_cursor(wp
);
939 screen_init(&wp
->status_screen
, 1, 1, 0);
941 if (gethostname(host
, sizeof host
) == 0)
942 screen_set_title(&wp
->base
, host
);
948 window_pane_destroy(struct window_pane
*wp
)
950 struct window_pane_resize
*r
;
951 struct window_pane_resize
*r1
;
953 window_pane_reset_mode_all(wp
);
957 bufferevent_free(wp
->event
);
960 if (wp
->ictx
!= NULL
)
961 input_free(wp
->ictx
);
963 screen_free(&wp
->status_screen
);
965 screen_free(&wp
->base
);
967 if (wp
->pipe_fd
!= -1) {
968 bufferevent_free(wp
->pipe_event
);
972 if (event_initialized(&wp
->resize_timer
))
973 event_del(&wp
->resize_timer
);
974 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
975 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
979 RB_REMOVE(window_pane_tree
, &all_window_panes
, wp
);
981 options_free(wp
->options
);
982 free((void *)wp
->cwd
);
984 cmd_free_argv(wp
->argc
, wp
->argv
);
985 colour_palette_free(&wp
->palette
);
990 window_pane_read_callback(__unused
struct bufferevent
*bufev
, void *data
)
992 struct window_pane
*wp
= data
;
993 struct evbuffer
*evb
= wp
->event
->input
;
994 struct window_pane_offset
*wpo
= &wp
->pipe_offset
;
995 size_t size
= EVBUFFER_LENGTH(evb
);
1000 if (wp
->pipe_fd
!= -1) {
1001 new_data
= window_pane_get_new_data(wp
, wpo
, &new_size
);
1003 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
1004 window_pane_update_used_data(wp
, wpo
, new_size
);
1008 log_debug("%%%u has %zu bytes", wp
->id
, size
);
1009 TAILQ_FOREACH(c
, &clients
, entry
) {
1010 if (c
->session
!= NULL
&& (c
->flags
& CLIENT_CONTROL
))
1011 control_write_output(c
, wp
);
1013 input_parse_pane(wp
);
1014 bufferevent_disable(wp
->event
, EV_READ
);
1018 window_pane_error_callback(__unused
struct bufferevent
*bufev
,
1019 __unused
short what
, void *data
)
1021 struct window_pane
*wp
= data
;
1023 log_debug("%%%u error", wp
->id
);
1024 wp
->flags
|= PANE_EXITED
;
1026 if (window_pane_destroy_ready(wp
))
1027 server_destroy_pane(wp
, 1);
1031 window_pane_set_event(struct window_pane
*wp
)
1033 setblocking(wp
->fd
, 0);
1035 wp
->event
= bufferevent_new(wp
->fd
, window_pane_read_callback
,
1036 NULL
, window_pane_error_callback
, wp
);
1037 if (wp
->event
== NULL
)
1038 fatalx("out of memory");
1039 wp
->ictx
= input_init(wp
, wp
->event
, &wp
->palette
);
1041 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
1045 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
1047 struct window_mode_entry
*wme
;
1048 struct window_pane_resize
*r
;
1050 if (sx
== wp
->sx
&& sy
== wp
->sy
)
1053 r
= xmalloc(sizeof *r
);
1058 TAILQ_INSERT_TAIL (&wp
->resize_queue
, r
, entry
);
1063 log_debug("%s: %%%u resize %ux%u", __func__
, wp
->id
, sx
, sy
);
1064 screen_resize(&wp
->base
, sx
, sy
, wp
->base
.saved_grid
== NULL
);
1066 wme
= TAILQ_FIRST(&wp
->modes
);
1067 if (wme
!= NULL
&& wme
->mode
->resize
!= NULL
)
1068 wme
->mode
->resize(wme
, sx
, sy
);
1072 window_pane_set_mode(struct window_pane
*wp
, struct window_pane
*swp
,
1073 const struct window_mode
*mode
, struct cmd_find_state
*fs
,
1076 struct window_mode_entry
*wme
;
1078 if (!TAILQ_EMPTY(&wp
->modes
) && TAILQ_FIRST(&wp
->modes
)->mode
== mode
)
1081 TAILQ_FOREACH(wme
, &wp
->modes
, entry
) {
1082 if (wme
->mode
== mode
)
1086 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1087 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1089 wme
= xcalloc(1, sizeof *wme
);
1094 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1095 wme
->screen
= wme
->mode
->init(wme
, fs
, args
);
1098 wp
->screen
= wme
->screen
;
1099 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1101 server_redraw_window_borders(wp
->window
);
1102 server_status_window(wp
->window
);
1103 notify_pane("pane-mode-changed", wp
);
1109 window_pane_reset_mode(struct window_pane
*wp
)
1111 struct window_mode_entry
*wme
, *next
;
1113 if (TAILQ_EMPTY(&wp
->modes
))
1116 wme
= TAILQ_FIRST(&wp
->modes
);
1117 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1118 wme
->mode
->free(wme
);
1121 next
= TAILQ_FIRST(&wp
->modes
);
1123 wp
->flags
&= ~PANE_UNSEENCHANGES
;
1124 log_debug("%s: no next mode", __func__
);
1125 wp
->screen
= &wp
->base
;
1127 log_debug("%s: next mode is %s", __func__
, next
->mode
->name
);
1128 wp
->screen
= next
->screen
;
1129 if (next
->mode
->resize
!= NULL
)
1130 next
->mode
->resize(next
, wp
->sx
, wp
->sy
);
1132 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1134 server_redraw_window_borders(wp
->window
);
1135 server_status_window(wp
->window
);
1136 notify_pane("pane-mode-changed", wp
);
1140 window_pane_reset_mode_all(struct window_pane
*wp
)
1142 while (!TAILQ_EMPTY(&wp
->modes
))
1143 window_pane_reset_mode(wp
);
1147 window_pane_copy_key(struct window_pane
*wp
, key_code key
)
1149 struct window_pane
*loop
;
1151 TAILQ_FOREACH(loop
, &wp
->window
->panes
, entry
) {
1153 TAILQ_EMPTY(&loop
->modes
) &&
1155 (~loop
->flags
& PANE_INPUTOFF
) &&
1156 window_pane_visible(loop
) &&
1157 options_get_number(loop
->options
, "synchronize-panes"))
1158 input_key_pane(loop
, key
, NULL
);
1163 window_pane_key(struct window_pane
*wp
, struct client
*c
, struct session
*s
,
1164 struct winlink
*wl
, key_code key
, struct mouse_event
*m
)
1166 struct window_mode_entry
*wme
;
1168 if (KEYC_IS_MOUSE(key
) && m
== NULL
)
1171 wme
= TAILQ_FIRST(&wp
->modes
);
1173 if (wme
->mode
->key
!= NULL
&& c
!= NULL
) {
1174 key
&= ~KEYC_MASK_FLAGS
;
1175 wme
->mode
->key(wme
, c
, s
, wl
, key
, m
);
1180 if (wp
->fd
== -1 || wp
->flags
& PANE_INPUTOFF
)
1183 if (input_key_pane(wp
, key
, m
) != 0)
1186 if (KEYC_IS_MOUSE(key
))
1188 if (options_get_number(wp
->options
, "synchronize-panes"))
1189 window_pane_copy_key(wp
, key
);
1194 window_pane_visible(struct window_pane
*wp
)
1196 if (~wp
->window
->flags
& WINDOW_ZOOMED
)
1198 return (wp
== wp
->window
->active
);
1202 window_pane_search(struct window_pane
*wp
, const char *term
, int regex
,
1205 struct screen
*s
= &wp
->base
;
1207 char *new = NULL
, *line
;
1209 int flags
= 0, found
;
1214 flags
|= FNM_CASEFOLD
;
1215 xasprintf(&new, "*%s*", term
);
1219 if (regcomp(&r
, term
, flags
|REG_EXTENDED
) != 0)
1223 for (i
= 0; i
< screen_size_y(s
); i
++) {
1224 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
1225 for (n
= strlen(line
); n
> 0; n
--) {
1226 if (!isspace((u_char
)line
[n
- 1]))
1230 log_debug("%s: %s", __func__
, line
);
1232 found
= (fnmatch(new, line
, flags
) == 0);
1234 found
= (regexec(&r
, line
, 0, NULL
, 0) == 0);
1244 if (i
== screen_size_y(s
))
1249 /* Get MRU pane from a list. */
1250 static struct window_pane
*
1251 window_pane_choose_best(struct window_pane
**list
, u_int size
)
1253 struct window_pane
*next
, *best
;
1260 for (i
= 1; i
< size
; i
++) {
1262 if (next
->active_point
> best
->active_point
)
1269 * Find the pane directly above another. We build a list of those adjacent to
1270 * top edge and then choose the best.
1272 struct window_pane
*
1273 window_pane_find_up(struct window_pane
*wp
)
1276 struct window_pane
*next
, *best
, **list
;
1277 u_int edge
, left
, right
, end
, size
;
1283 status
= options_get_number(w
->options
, "pane-border-status");
1289 if (status
== PANE_STATUS_TOP
) {
1292 } else if (status
== PANE_STATUS_BOTTOM
) {
1301 right
= wp
->xoff
+ wp
->sx
;
1303 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1306 if (next
->yoff
+ next
->sy
+ 1 != edge
)
1308 end
= next
->xoff
+ next
->sx
- 1;
1311 if (next
->xoff
< left
&& end
> right
)
1313 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1315 else if (end
>= left
&& end
<= right
)
1319 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1320 list
[size
++] = next
;
1323 best
= window_pane_choose_best(list
, size
);
1328 /* Find the pane directly below another. */
1329 struct window_pane
*
1330 window_pane_find_down(struct window_pane
*wp
)
1333 struct window_pane
*next
, *best
, **list
;
1334 u_int edge
, left
, right
, end
, size
;
1340 status
= options_get_number(w
->options
, "pane-border-status");
1345 edge
= wp
->yoff
+ wp
->sy
+ 1;
1346 if (status
== PANE_STATUS_TOP
) {
1349 } else if (status
== PANE_STATUS_BOTTOM
) {
1350 if (edge
>= w
->sy
- 1)
1358 right
= wp
->xoff
+ wp
->sx
;
1360 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1363 if (next
->yoff
!= edge
)
1365 end
= next
->xoff
+ next
->sx
- 1;
1368 if (next
->xoff
< left
&& end
> right
)
1370 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1372 else if (end
>= left
&& end
<= right
)
1376 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1377 list
[size
++] = next
;
1380 best
= window_pane_choose_best(list
, size
);
1385 /* Find the pane directly to the left of another. */
1386 struct window_pane
*
1387 window_pane_find_left(struct window_pane
*wp
)
1390 struct window_pane
*next
, *best
, **list
;
1391 u_int edge
, top
, bottom
, end
, size
;
1406 bottom
= wp
->yoff
+ wp
->sy
;
1408 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1411 if (next
->xoff
+ next
->sx
+ 1 != edge
)
1413 end
= next
->yoff
+ next
->sy
- 1;
1416 if (next
->yoff
< top
&& end
> bottom
)
1418 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1420 else if (end
>= top
&& end
<= bottom
)
1424 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1425 list
[size
++] = next
;
1428 best
= window_pane_choose_best(list
, size
);
1433 /* Find the pane directly to the right of another. */
1434 struct window_pane
*
1435 window_pane_find_right(struct window_pane
*wp
)
1438 struct window_pane
*next
, *best
, **list
;
1439 u_int edge
, top
, bottom
, end
, size
;
1449 edge
= wp
->xoff
+ wp
->sx
+ 1;
1454 bottom
= wp
->yoff
+ wp
->sy
;
1456 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1459 if (next
->xoff
!= edge
)
1461 end
= next
->yoff
+ next
->sy
- 1;
1464 if (next
->yoff
< top
&& end
> bottom
)
1466 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1468 else if (end
>= top
&& end
<= bottom
)
1472 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1473 list
[size
++] = next
;
1476 best
= window_pane_choose_best(list
, size
);
1481 /* Clear alert flags for a winlink */
1483 winlink_clear_flags(struct winlink
*wl
)
1485 struct winlink
*loop
;
1487 wl
->window
->flags
&= ~WINDOW_ALERTFLAGS
;
1488 TAILQ_FOREACH(loop
, &wl
->window
->winlinks
, wentry
) {
1489 if ((loop
->flags
& WINLINK_ALERTFLAGS
) != 0) {
1490 loop
->flags
&= ~WINLINK_ALERTFLAGS
;
1491 server_status_session(loop
->session
);
1496 /* Shuffle window indexes up. */
1498 winlink_shuffle_up(struct session
*s
, struct winlink
*wl
, int before
)
1509 /* Find the next free index. */
1510 for (last
= idx
; last
< INT_MAX
; last
++) {
1511 if (winlink_find_by_index(&s
->windows
, last
) == NULL
)
1514 if (last
== INT_MAX
)
1517 /* Move everything from last - 1 to idx up a bit. */
1518 for (; last
> idx
; last
--) {
1519 wl
= winlink_find_by_index(&s
->windows
, last
- 1);
1520 RB_REMOVE(winlinks
, &s
->windows
, wl
);
1522 RB_INSERT(winlinks
, &s
->windows
, wl
);
1529 window_pane_input_callback(struct client
*c
, __unused
const char *path
,
1530 int error
, int closed
, struct evbuffer
*buffer
, void *data
)
1532 struct window_pane_input_data
*cdata
= data
;
1533 struct window_pane
*wp
;
1534 u_char
*buf
= EVBUFFER_DATA(buffer
);
1535 size_t len
= EVBUFFER_LENGTH(buffer
);
1537 wp
= window_pane_find_by_id(cdata
->wp
);
1538 if (cdata
->file
!= NULL
&& (wp
== NULL
|| c
->flags
& CLIENT_DEAD
)) {
1541 c
->flags
|= CLIENT_EXIT
;
1543 file_cancel(cdata
->file
);
1544 } else if (cdata
->file
== NULL
|| closed
|| error
!= 0) {
1545 cmdq_continue(cdata
->item
);
1546 server_client_unref(c
);
1549 input_parse_buffer(wp
, buf
, len
);
1550 evbuffer_drain(buffer
, len
);
1554 window_pane_start_input(struct window_pane
*wp
, struct cmdq_item
*item
,
1557 struct client
*c
= cmdq_get_client(item
);
1558 struct window_pane_input_data
*cdata
;
1560 if (~wp
->flags
& PANE_EMPTY
) {
1561 *cause
= xstrdup("pane is not empty");
1564 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
1566 if (c
->session
!= NULL
)
1569 cdata
= xmalloc(sizeof *cdata
);
1572 cdata
->file
= file_read(c
, "-", window_pane_input_callback
, cdata
);
1579 window_pane_get_new_data(struct window_pane
*wp
,
1580 struct window_pane_offset
*wpo
, size_t *size
)
1582 size_t used
= wpo
->used
- wp
->base_offset
;
1584 *size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1585 return (EVBUFFER_DATA(wp
->event
->input
) + used
);
1589 window_pane_update_used_data(struct window_pane
*wp
,
1590 struct window_pane_offset
*wpo
, size_t size
)
1592 size_t used
= wpo
->used
- wp
->base_offset
;
1594 if (size
> EVBUFFER_LENGTH(wp
->event
->input
) - used
)
1595 size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1600 window_set_fill_character(struct window
*w
)
1603 struct utf8_data
*ud
;
1605 free(w
->fill_character
);
1606 w
->fill_character
= NULL
;
1608 value
= options_get_string(w
->options
, "fill-character");
1609 if (*value
!= '\0' && utf8_isvalid(value
)) {
1610 ud
= utf8_fromcstr(value
);
1611 if (ud
!= NULL
&& ud
[0].width
== 1)
1612 w
->fill_character
= ud
;
1617 window_pane_default_cursor(struct window_pane
*wp
)
1619 struct screen
*s
= wp
->screen
;
1622 c
= options_get_number(wp
->options
, "cursor-colour");
1623 s
->default_ccolour
= c
;
1625 c
= options_get_number(wp
->options
, "cursor-style");
1626 s
->default_mode
= 0;
1627 screen_set_cursor_style(c
, &s
->default_cstyle
, &s
->default_mode
);