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>
37 * Each window is attached to a number of panes, each of which is a pty. This
38 * file contains code to handle them.
40 * A pane has two buffers attached, these are filled and emptied by the main
41 * server poll loop. Output data is received from pty's in screen format,
42 * translated and returned as a series of escape sequences and strings via
43 * input_parse (in input.c). Input data is received as key codes and written
44 * directly via input_key.
46 * Each pane also has a "virtual" screen (screen.c) which contains the current
47 * state and is redisplayed when the window is reattached to a client.
49 * Windows are stored directly on a global array and wrapped in any number of
50 * winlink structs to be linked onto local session RB trees. A reference count
51 * is maintained and a window removed from the global list and destroyed when
55 /* Global window list. */
56 struct windows windows
;
58 /* Global panes tree. */
59 struct window_pane_tree all_window_panes
;
60 static u_int next_window_pane_id
;
61 static u_int next_window_id
;
62 static u_int next_active_point
;
64 struct window_pane_input_data
{
65 struct cmdq_item
*item
;
67 struct client_file
*file
;
70 static struct window_pane
*window_pane_create(struct window
*, u_int
, u_int
,
72 static void window_pane_destroy(struct window_pane
*);
74 RB_GENERATE(windows
, window
, entry
, window_cmp
);
75 RB_GENERATE(winlinks
, winlink
, entry
, winlink_cmp
);
76 RB_GENERATE(window_pane_tree
, window_pane
, tree_entry
, window_pane_cmp
);
79 window_cmp(struct window
*w1
, struct window
*w2
)
81 return (w1
->id
- w2
->id
);
85 winlink_cmp(struct winlink
*wl1
, struct winlink
*wl2
)
87 return (wl1
->idx
- wl2
->idx
);
91 window_pane_cmp(struct window_pane
*wp1
, struct window_pane
*wp2
)
93 return (wp1
->id
- wp2
->id
);
97 winlink_find_by_window(struct winlinks
*wwl
, struct window
*w
)
101 RB_FOREACH(wl
, winlinks
, wwl
) {
110 winlink_find_by_index(struct winlinks
*wwl
, int idx
)
118 return (RB_FIND(winlinks
, wwl
, &wl
));
122 winlink_find_by_window_id(struct winlinks
*wwl
, u_int id
)
126 RB_FOREACH(wl
, winlinks
, wwl
) {
127 if (wl
->window
->id
== id
)
134 winlink_next_index(struct winlinks
*wwl
, int idx
)
140 if (winlink_find_by_index(wwl
, i
) == NULL
)
151 winlink_count(struct winlinks
*wwl
)
157 RB_FOREACH(wl
, winlinks
, wwl
)
164 winlink_add(struct winlinks
*wwl
, int idx
)
169 if ((idx
= winlink_next_index(wwl
, -idx
- 1)) == -1)
171 } else if (winlink_find_by_index(wwl
, idx
) != NULL
)
174 wl
= xcalloc(1, sizeof *wl
);
176 RB_INSERT(winlinks
, wwl
, wl
);
182 winlink_set_window(struct winlink
*wl
, struct window
*w
)
184 if (wl
->window
!= NULL
) {
185 TAILQ_REMOVE(&wl
->window
->winlinks
, wl
, wentry
);
186 window_remove_ref(wl
->window
, __func__
);
188 TAILQ_INSERT_TAIL(&w
->winlinks
, wl
, wentry
);
190 window_add_ref(w
, __func__
);
194 winlink_remove(struct winlinks
*wwl
, struct winlink
*wl
)
196 struct window
*w
= wl
->window
;
199 TAILQ_REMOVE(&w
->winlinks
, wl
, wentry
);
200 window_remove_ref(w
, __func__
);
203 RB_REMOVE(winlinks
, wwl
, wl
);
208 winlink_next(struct winlink
*wl
)
210 return (RB_NEXT(winlinks
, wwl
, wl
));
214 winlink_previous(struct winlink
*wl
)
216 return (RB_PREV(winlinks
, wwl
, wl
));
220 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
223 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
224 wl
= RB_MIN(winlinks
, &s
->windows
);
231 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
234 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
235 wl
= RB_MAX(winlinks
, &s
->windows
);
242 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
247 winlink_stack_remove(stack
, wl
);
248 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
249 wl
->flags
|= WINLINK_VISITED
;
253 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
255 if (wl
!= NULL
&& (wl
->flags
& WINLINK_VISITED
)) {
256 TAILQ_REMOVE(stack
, wl
, sentry
);
257 wl
->flags
&= ~WINLINK_VISITED
;
262 window_find_by_id_str(const char *s
)
270 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
273 return (window_find_by_id(id
));
277 window_find_by_id(u_int id
)
282 return (RB_FIND(windows
, &windows
, &w
));
286 window_update_activity(struct window
*w
)
288 gettimeofday(&w
->activity_time
, NULL
);
289 alerts_queue(w
, WINDOW_ACTIVITY
);
293 window_create(u_int sx
, u_int sy
, u_int xpixel
, u_int ypixel
)
298 xpixel
= DEFAULT_XPIXEL
;
300 ypixel
= DEFAULT_YPIXEL
;
302 w
= xcalloc(1, sizeof *w
);
303 w
->name
= xstrdup("");
306 TAILQ_INIT(&w
->panes
);
307 TAILQ_INIT(&w
->last_panes
);
311 w
->layout_root
= NULL
;
320 w
->options
= options_create(global_w_options
);
323 TAILQ_INIT(&w
->winlinks
);
325 w
->id
= next_window_id
++;
326 RB_INSERT(windows
, &windows
, w
);
328 window_set_fill_character(w
);
329 window_update_activity(w
);
331 log_debug("%s: @%u create %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
332 w
->xpixel
, w
->ypixel
);
337 window_destroy(struct window
*w
)
339 log_debug("window @%u destroyed (%d references)", w
->id
, w
->references
);
342 RB_REMOVE(windows
, &windows
, w
);
344 if (w
->layout_root
!= NULL
)
345 layout_free_cell(w
->layout_root
);
346 if (w
->saved_layout_root
!= NULL
)
347 layout_free_cell(w
->saved_layout_root
);
350 window_destroy_panes(w
);
352 if (event_initialized(&w
->name_event
))
353 evtimer_del(&w
->name_event
);
355 if (event_initialized(&w
->alerts_timer
))
356 evtimer_del(&w
->alerts_timer
);
357 if (event_initialized(&w
->offset_timer
))
358 event_del(&w
->offset_timer
);
360 options_free(w
->options
);
361 free(w
->fill_character
);
368 window_pane_destroy_ready(struct window_pane
*wp
)
372 if (wp
->pipe_fd
!= -1) {
373 if (EVBUFFER_LENGTH(wp
->pipe_event
->output
) != 0)
375 if (ioctl(wp
->fd
, FIONREAD
, &n
) != -1 && n
> 0)
379 if (~wp
->flags
& PANE_EXITED
)
385 window_add_ref(struct window
*w
, const char *from
)
388 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
392 window_remove_ref(struct window
*w
, const char *from
)
395 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
397 if (w
->references
== 0)
402 window_set_name(struct window
*w
, const char *new_name
)
405 utf8_stravis(&w
->name
, new_name
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
406 notify_window("window-renamed", w
);
410 window_resize(struct window
*w
, u_int sx
, u_int sy
, int xpixel
, int ypixel
)
413 xpixel
= DEFAULT_XPIXEL
;
415 ypixel
= DEFAULT_YPIXEL
;
417 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
418 xpixel
== -1 ? w
->xpixel
: (u_int
)xpixel
,
419 ypixel
== -1 ? w
->ypixel
: (u_int
)ypixel
);
429 window_pane_send_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
431 struct window
*w
= wp
->window
;
437 log_debug("%s: %%%u resize to %u,%u", __func__
, wp
->id
, sx
, sy
);
439 memset(&ws
, 0, sizeof ws
);
442 ws
.ws_xpixel
= w
->xpixel
* ws
.ws_col
;
443 ws
.ws_ypixel
= w
->ypixel
* ws
.ws_row
;
444 if (ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
447 * Some versions of Solaris apparently can return an error when
448 * resizing; don't know why this happens, can't reproduce on
449 * other platforms and ignoring it doesn't seem to cause any
452 if (errno
!= EINVAL
&& errno
!= ENXIO
)
454 fatal("ioctl failed");
458 window_has_pane(struct window
*w
, struct window_pane
*wp
)
460 struct window_pane
*wp1
;
462 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
470 window_update_focus(struct window
*w
)
473 log_debug("%s: @%u", __func__
, w
->id
);
474 window_pane_update_focus(w
->active
);
479 window_pane_update_focus(struct window_pane
*wp
)
485 if (wp
!= wp
->window
->active
)
488 TAILQ_FOREACH(c
, &clients
, entry
) {
489 if (c
->session
!= NULL
&&
490 c
->session
->attached
!= 0 &&
491 (c
->flags
& CLIENT_FOCUSED
) &&
492 c
->session
->curw
->window
== wp
->window
) {
498 if (!focused
&& (wp
->flags
& PANE_FOCUSED
)) {
499 log_debug("%s: %%%u focus out", __func__
, wp
->id
);
500 if (wp
->base
.mode
& MODE_FOCUSON
)
501 bufferevent_write(wp
->event
, "\033[O", 3);
502 notify_pane("pane-focus-out", wp
);
503 wp
->flags
&= ~PANE_FOCUSED
;
504 } else if (focused
&& (~wp
->flags
& PANE_FOCUSED
)) {
505 log_debug("%s: %%%u focus in", __func__
, wp
->id
);
506 if (wp
->base
.mode
& MODE_FOCUSON
)
507 bufferevent_write(wp
->event
, "\033[I", 3);
508 notify_pane("pane-focus-in", wp
);
509 wp
->flags
|= PANE_FOCUSED
;
511 log_debug("%s: %%%u focus unchanged", __func__
, wp
->id
);
516 window_set_active_pane(struct window
*w
, struct window_pane
*wp
, int notify
)
518 struct window_pane
*lastwp
;
520 log_debug("%s: pane %%%u", __func__
, wp
->id
);
526 window_pane_stack_remove(&w
->last_panes
, wp
);
527 window_pane_stack_push(&w
->last_panes
, lastwp
);
530 w
->active
->active_point
= next_active_point
++;
531 w
->active
->flags
|= PANE_CHANGED
;
533 if (options_get_number(global_options
, "focus-events")) {
534 window_pane_update_focus(lastwp
);
535 window_pane_update_focus(w
->active
);
538 tty_update_window_offset(w
);
541 notify_window("window-pane-changed", w
);
546 window_pane_get_palette(struct window_pane
*wp
, int c
)
550 return (colour_palette_get(&wp
->palette
, c
));
554 window_redraw_active_switch(struct window
*w
, struct window_pane
*wp
)
556 struct grid_cell
*gc1
, *gc2
;
564 * If the active and inactive styles or palettes are different,
565 * need to redraw the panes.
567 gc1
= &wp
->cached_gc
;
568 gc2
= &wp
->cached_active_gc
;
569 if (!grid_cells_look_equal(gc1
, gc2
))
570 wp
->flags
|= PANE_REDRAW
;
572 c1
= window_pane_get_palette(wp
, gc1
->fg
);
573 c2
= window_pane_get_palette(wp
, gc2
->fg
);
575 wp
->flags
|= PANE_REDRAW
;
577 c1
= window_pane_get_palette(wp
, gc1
->bg
);
578 c2
= window_pane_get_palette(wp
, gc2
->bg
);
580 wp
->flags
|= PANE_REDRAW
;
590 window_get_active_at(struct window
*w
, u_int x
, u_int y
)
592 struct window_pane
*wp
;
594 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
595 if (!window_pane_visible(wp
))
597 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
599 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
607 window_find_string(struct window
*w
, const char *s
)
609 u_int x
, y
, top
= 0, bottom
= w
->sy
- 1;
615 status
= options_get_number(w
->options
, "pane-border-status");
616 if (status
== PANE_STATUS_TOP
)
618 else if (status
== PANE_STATUS_BOTTOM
)
621 if (strcasecmp(s
, "top") == 0)
623 else if (strcasecmp(s
, "bottom") == 0)
625 else if (strcasecmp(s
, "left") == 0)
627 else if (strcasecmp(s
, "right") == 0)
629 else if (strcasecmp(s
, "top-left") == 0) {
632 } else if (strcasecmp(s
, "top-right") == 0) {
635 } else if (strcasecmp(s
, "bottom-left") == 0) {
638 } else if (strcasecmp(s
, "bottom-right") == 0) {
644 return (window_get_active_at(w
, x
, y
));
648 window_zoom(struct window_pane
*wp
)
650 struct window
*w
= wp
->window
;
651 struct window_pane
*wp1
;
653 if (w
->flags
& WINDOW_ZOOMED
)
656 if (window_count_panes(w
) == 1)
660 window_set_active_pane(w
, wp
, 1);
662 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
663 wp1
->saved_layout_cell
= wp1
->layout_cell
;
664 wp1
->layout_cell
= NULL
;
667 w
->saved_layout_root
= w
->layout_root
;
669 w
->flags
|= WINDOW_ZOOMED
;
670 notify_window("window-layout-changed", w
);
676 window_unzoom(struct window
*w
)
678 struct window_pane
*wp
;
680 if (!(w
->flags
& WINDOW_ZOOMED
))
683 w
->flags
&= ~WINDOW_ZOOMED
;
685 w
->layout_root
= w
->saved_layout_root
;
686 w
->saved_layout_root
= NULL
;
688 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
689 wp
->layout_cell
= wp
->saved_layout_cell
;
690 wp
->saved_layout_cell
= NULL
;
692 layout_fix_panes(w
, NULL
);
693 notify_window("window-layout-changed", w
);
699 window_push_zoom(struct window
*w
, int always
, int flag
)
701 log_debug("%s: @%u %d", __func__
, w
->id
,
702 flag
&& (w
->flags
& WINDOW_ZOOMED
));
703 if (flag
&& (always
|| (w
->flags
& WINDOW_ZOOMED
)))
704 w
->flags
|= WINDOW_WASZOOMED
;
706 w
->flags
&= ~WINDOW_WASZOOMED
;
707 return (window_unzoom(w
) == 0);
711 window_pop_zoom(struct window
*w
)
713 log_debug("%s: @%u %d", __func__
, w
->id
,
714 !!(w
->flags
& WINDOW_WASZOOMED
));
715 if (w
->flags
& WINDOW_WASZOOMED
)
716 return (window_zoom(w
->active
) == 0);
721 window_add_pane(struct window
*w
, struct window_pane
*other
, u_int hlimit
,
724 struct window_pane
*wp
;
729 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
730 if (TAILQ_EMPTY(&w
->panes
)) {
731 log_debug("%s: @%u at start", __func__
, w
->id
);
732 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
733 } else if (flags
& SPAWN_BEFORE
) {
734 log_debug("%s: @%u before %%%u", __func__
, w
->id
, wp
->id
);
735 if (flags
& SPAWN_FULLSIZE
)
736 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
738 TAILQ_INSERT_BEFORE(other
, wp
, entry
);
740 log_debug("%s: @%u after %%%u", __func__
, w
->id
, wp
->id
);
741 if (flags
& SPAWN_FULLSIZE
)
742 TAILQ_INSERT_TAIL(&w
->panes
, wp
, entry
);
744 TAILQ_INSERT_AFTER(&w
->panes
, other
, wp
, entry
);
750 window_lost_pane(struct window
*w
, struct window_pane
*wp
)
752 log_debug("%s: @%u pane %%%u", __func__
, w
->id
, wp
->id
);
754 if (wp
== marked_pane
.wp
)
755 server_clear_marked();
757 window_pane_stack_remove(&w
->last_panes
, wp
);
758 if (wp
== w
->active
) {
759 w
->active
= TAILQ_FIRST(&w
->last_panes
);
760 if (w
->active
== NULL
) {
761 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
762 if (w
->active
== NULL
)
763 w
->active
= TAILQ_NEXT(wp
, entry
);
765 if (w
->active
!= NULL
) {
766 window_pane_stack_remove(&w
->last_panes
, w
->active
);
767 w
->active
->flags
|= PANE_CHANGED
;
768 notify_window("window-pane-changed", w
);
769 window_update_focus(w
);
775 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
777 window_lost_pane(w
, wp
);
779 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
780 window_pane_destroy(wp
);
784 window_pane_at_index(struct window
*w
, u_int idx
)
786 struct window_pane
*wp
;
789 n
= options_get_number(w
->options
, "pane-base-index");
790 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
799 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
802 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
803 wp
= TAILQ_FIRST(&w
->panes
);
810 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
814 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
815 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
822 window_pane_index(struct window_pane
*wp
, u_int
*i
)
824 struct window_pane
*wq
;
825 struct window
*w
= wp
->window
;
827 *i
= options_get_number(w
->options
, "pane-base-index");
828 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
839 window_count_panes(struct window
*w
)
841 struct window_pane
*wp
;
845 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
851 window_destroy_panes(struct window
*w
)
853 struct window_pane
*wp
;
855 while (!TAILQ_EMPTY(&w
->last_panes
)) {
856 wp
= TAILQ_FIRST(&w
->last_panes
);
857 window_pane_stack_remove(&w
->last_panes
, wp
);
860 while (!TAILQ_EMPTY(&w
->panes
)) {
861 wp
= TAILQ_FIRST(&w
->panes
);
862 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
863 window_pane_destroy(wp
);
868 window_printable_flags(struct winlink
*wl
, int escape
)
870 struct session
*s
= wl
->session
;
871 static char flags
[32];
875 if (wl
->flags
& WINLINK_ACTIVITY
) {
880 if (wl
->flags
& WINLINK_BELL
)
882 if (wl
->flags
& WINLINK_SILENCE
)
886 if (wl
== TAILQ_FIRST(&s
->lastw
))
888 if (server_check_marked() && wl
== marked_pane
.wl
)
890 if (wl
->window
->flags
& WINDOW_ZOOMED
)
897 window_pane_find_by_id_str(const char *s
)
905 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
908 return (window_pane_find_by_id(id
));
912 window_pane_find_by_id(u_int id
)
914 struct window_pane wp
;
917 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
920 static struct window_pane
*
921 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
923 struct window_pane
*wp
;
924 char host
[HOST_NAME_MAX
+ 1];
926 wp
= xcalloc(1, sizeof *wp
);
928 wp
->options
= options_create(w
->options
);
929 wp
->flags
= PANE_STYLECHANGED
;
931 wp
->id
= next_window_pane_id
++;
932 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
936 TAILQ_INIT(&wp
->modes
);
938 TAILQ_INIT (&wp
->resize_queue
);
945 colour_palette_init(&wp
->palette
);
946 colour_palette_from_option(&wp
->palette
, wp
->options
);
948 screen_init(&wp
->base
, sx
, sy
, hlimit
);
949 wp
->screen
= &wp
->base
;
950 window_pane_default_cursor(wp
);
952 screen_init(&wp
->status_screen
, 1, 1, 0);
954 if (gethostname(host
, sizeof host
) == 0)
955 screen_set_title(&wp
->base
, host
);
961 window_pane_destroy(struct window_pane
*wp
)
963 struct window_pane_resize
*r
;
964 struct window_pane_resize
*r1
;
966 window_pane_reset_mode_all(wp
);
971 utempter_remove_record(wp
->fd
);
973 bufferevent_free(wp
->event
);
976 if (wp
->ictx
!= NULL
)
977 input_free(wp
->ictx
);
979 screen_free(&wp
->status_screen
);
981 screen_free(&wp
->base
);
983 if (wp
->pipe_fd
!= -1) {
984 bufferevent_free(wp
->pipe_event
);
988 if (event_initialized(&wp
->resize_timer
))
989 event_del(&wp
->resize_timer
);
990 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
991 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
995 RB_REMOVE(window_pane_tree
, &all_window_panes
, wp
);
997 options_free(wp
->options
);
998 free((void *)wp
->cwd
);
1000 cmd_free_argv(wp
->argc
, wp
->argv
);
1001 colour_palette_free(&wp
->palette
);
1006 window_pane_read_callback(__unused
struct bufferevent
*bufev
, void *data
)
1008 struct window_pane
*wp
= data
;
1009 struct evbuffer
*evb
= wp
->event
->input
;
1010 struct window_pane_offset
*wpo
= &wp
->pipe_offset
;
1011 size_t size
= EVBUFFER_LENGTH(evb
);
1016 if (wp
->pipe_fd
!= -1) {
1017 new_data
= window_pane_get_new_data(wp
, wpo
, &new_size
);
1019 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
1020 window_pane_update_used_data(wp
, wpo
, new_size
);
1024 log_debug("%%%u has %zu bytes", wp
->id
, size
);
1025 TAILQ_FOREACH(c
, &clients
, entry
) {
1026 if (c
->session
!= NULL
&& (c
->flags
& CLIENT_CONTROL
))
1027 control_write_output(c
, wp
);
1029 input_parse_pane(wp
);
1030 bufferevent_disable(wp
->event
, EV_READ
);
1034 window_pane_error_callback(__unused
struct bufferevent
*bufev
,
1035 __unused
short what
, void *data
)
1037 struct window_pane
*wp
= data
;
1039 log_debug("%%%u error", wp
->id
);
1040 wp
->flags
|= PANE_EXITED
;
1042 if (window_pane_destroy_ready(wp
))
1043 server_destroy_pane(wp
, 1);
1047 window_pane_set_event(struct window_pane
*wp
)
1049 setblocking(wp
->fd
, 0);
1051 wp
->event
= bufferevent_new(wp
->fd
, window_pane_read_callback
,
1052 NULL
, window_pane_error_callback
, wp
);
1053 if (wp
->event
== NULL
)
1054 fatalx("out of memory");
1055 wp
->ictx
= input_init(wp
, wp
->event
, &wp
->palette
);
1057 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
1061 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
1063 struct window_mode_entry
*wme
;
1064 struct window_pane_resize
*r
;
1066 if (sx
== wp
->sx
&& sy
== wp
->sy
)
1069 r
= xmalloc(sizeof *r
);
1074 TAILQ_INSERT_TAIL (&wp
->resize_queue
, r
, entry
);
1079 log_debug("%s: %%%u resize %ux%u", __func__
, wp
->id
, sx
, sy
);
1080 screen_resize(&wp
->base
, sx
, sy
, wp
->base
.saved_grid
== NULL
);
1082 wme
= TAILQ_FIRST(&wp
->modes
);
1083 if (wme
!= NULL
&& wme
->mode
->resize
!= NULL
)
1084 wme
->mode
->resize(wme
, sx
, sy
);
1088 window_pane_set_mode(struct window_pane
*wp
, struct window_pane
*swp
,
1089 const struct window_mode
*mode
, struct cmd_find_state
*fs
,
1092 struct window_mode_entry
*wme
;
1094 if (!TAILQ_EMPTY(&wp
->modes
) && TAILQ_FIRST(&wp
->modes
)->mode
== mode
)
1097 TAILQ_FOREACH(wme
, &wp
->modes
, entry
) {
1098 if (wme
->mode
== mode
)
1102 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1103 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1105 wme
= xcalloc(1, sizeof *wme
);
1110 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1111 wme
->screen
= wme
->mode
->init(wme
, fs
, args
);
1114 wp
->screen
= wme
->screen
;
1115 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1117 server_redraw_window_borders(wp
->window
);
1118 server_status_window(wp
->window
);
1119 notify_pane("pane-mode-changed", wp
);
1125 window_pane_reset_mode(struct window_pane
*wp
)
1127 struct window_mode_entry
*wme
, *next
;
1129 if (TAILQ_EMPTY(&wp
->modes
))
1132 wme
= TAILQ_FIRST(&wp
->modes
);
1133 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1134 wme
->mode
->free(wme
);
1137 next
= TAILQ_FIRST(&wp
->modes
);
1139 wp
->flags
&= ~PANE_UNSEENCHANGES
;
1140 log_debug("%s: no next mode", __func__
);
1141 wp
->screen
= &wp
->base
;
1143 log_debug("%s: next mode is %s", __func__
, next
->mode
->name
);
1144 wp
->screen
= next
->screen
;
1145 if (next
->mode
->resize
!= NULL
)
1146 next
->mode
->resize(next
, wp
->sx
, wp
->sy
);
1148 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1150 server_redraw_window_borders(wp
->window
);
1151 server_status_window(wp
->window
);
1152 notify_pane("pane-mode-changed", wp
);
1156 window_pane_reset_mode_all(struct window_pane
*wp
)
1158 while (!TAILQ_EMPTY(&wp
->modes
))
1159 window_pane_reset_mode(wp
);
1163 window_pane_copy_key(struct window_pane
*wp
, key_code key
)
1165 struct window_pane
*loop
;
1167 TAILQ_FOREACH(loop
, &wp
->window
->panes
, entry
) {
1169 TAILQ_EMPTY(&loop
->modes
) &&
1171 (~loop
->flags
& PANE_INPUTOFF
) &&
1172 window_pane_visible(loop
) &&
1173 options_get_number(loop
->options
, "synchronize-panes"))
1174 input_key_pane(loop
, key
, NULL
);
1179 window_pane_key(struct window_pane
*wp
, struct client
*c
, struct session
*s
,
1180 struct winlink
*wl
, key_code key
, struct mouse_event
*m
)
1182 struct window_mode_entry
*wme
;
1184 if (KEYC_IS_MOUSE(key
) && m
== NULL
)
1187 wme
= TAILQ_FIRST(&wp
->modes
);
1189 if (wme
->mode
->key
!= NULL
&& c
!= NULL
) {
1190 key
&= ~KEYC_MASK_FLAGS
;
1191 wme
->mode
->key(wme
, c
, s
, wl
, key
, m
);
1196 if (wp
->fd
== -1 || wp
->flags
& PANE_INPUTOFF
)
1199 if (input_key_pane(wp
, key
, m
) != 0)
1202 if (KEYC_IS_MOUSE(key
))
1204 if (options_get_number(wp
->options
, "synchronize-panes"))
1205 window_pane_copy_key(wp
, key
);
1210 window_pane_visible(struct window_pane
*wp
)
1212 if (~wp
->window
->flags
& WINDOW_ZOOMED
)
1214 return (wp
== wp
->window
->active
);
1218 window_pane_search(struct window_pane
*wp
, const char *term
, int regex
,
1221 struct screen
*s
= &wp
->base
;
1223 char *new = NULL
, *line
;
1225 int flags
= 0, found
;
1230 flags
|= FNM_CASEFOLD
;
1231 xasprintf(&new, "*%s*", term
);
1235 if (regcomp(&r
, term
, flags
|REG_EXTENDED
) != 0)
1239 for (i
= 0; i
< screen_size_y(s
); i
++) {
1240 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
1241 for (n
= strlen(line
); n
> 0; n
--) {
1242 if (!isspace((u_char
)line
[n
- 1]))
1246 log_debug("%s: %s", __func__
, line
);
1248 found
= (fnmatch(new, line
, flags
) == 0);
1250 found
= (regexec(&r
, line
, 0, NULL
, 0) == 0);
1260 if (i
== screen_size_y(s
))
1265 /* Get MRU pane from a list. */
1266 static struct window_pane
*
1267 window_pane_choose_best(struct window_pane
**list
, u_int size
)
1269 struct window_pane
*next
, *best
;
1276 for (i
= 1; i
< size
; i
++) {
1278 if (next
->active_point
> best
->active_point
)
1285 * Find the pane directly above another. We build a list of those adjacent to
1286 * top edge and then choose the best.
1288 struct window_pane
*
1289 window_pane_find_up(struct window_pane
*wp
)
1292 struct window_pane
*next
, *best
, **list
;
1293 u_int edge
, left
, right
, end
, size
;
1299 status
= options_get_number(w
->options
, "pane-border-status");
1305 if (status
== PANE_STATUS_TOP
) {
1308 } else if (status
== PANE_STATUS_BOTTOM
) {
1317 right
= wp
->xoff
+ wp
->sx
;
1319 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1322 if (next
->yoff
+ next
->sy
+ 1 != edge
)
1324 end
= next
->xoff
+ next
->sx
- 1;
1327 if (next
->xoff
< left
&& end
> right
)
1329 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1331 else if (end
>= left
&& end
<= right
)
1335 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1336 list
[size
++] = next
;
1339 best
= window_pane_choose_best(list
, size
);
1344 /* Find the pane directly below another. */
1345 struct window_pane
*
1346 window_pane_find_down(struct window_pane
*wp
)
1349 struct window_pane
*next
, *best
, **list
;
1350 u_int edge
, left
, right
, end
, size
;
1356 status
= options_get_number(w
->options
, "pane-border-status");
1361 edge
= wp
->yoff
+ wp
->sy
+ 1;
1362 if (status
== PANE_STATUS_TOP
) {
1365 } else if (status
== PANE_STATUS_BOTTOM
) {
1366 if (edge
>= w
->sy
- 1)
1374 right
= wp
->xoff
+ wp
->sx
;
1376 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1379 if (next
->yoff
!= edge
)
1381 end
= next
->xoff
+ next
->sx
- 1;
1384 if (next
->xoff
< left
&& end
> right
)
1386 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1388 else if (end
>= left
&& end
<= right
)
1392 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1393 list
[size
++] = next
;
1396 best
= window_pane_choose_best(list
, size
);
1401 /* Find the pane directly to the left of another. */
1402 struct window_pane
*
1403 window_pane_find_left(struct window_pane
*wp
)
1406 struct window_pane
*next
, *best
, **list
;
1407 u_int edge
, top
, bottom
, end
, size
;
1422 bottom
= wp
->yoff
+ wp
->sy
;
1424 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1427 if (next
->xoff
+ next
->sx
+ 1 != edge
)
1429 end
= next
->yoff
+ next
->sy
- 1;
1432 if (next
->yoff
< top
&& end
> bottom
)
1434 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1436 else if (end
>= top
&& end
<= bottom
)
1440 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1441 list
[size
++] = next
;
1444 best
= window_pane_choose_best(list
, size
);
1449 /* Find the pane directly to the right of another. */
1450 struct window_pane
*
1451 window_pane_find_right(struct window_pane
*wp
)
1454 struct window_pane
*next
, *best
, **list
;
1455 u_int edge
, top
, bottom
, end
, size
;
1465 edge
= wp
->xoff
+ wp
->sx
+ 1;
1470 bottom
= wp
->yoff
+ wp
->sy
;
1472 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1475 if (next
->xoff
!= edge
)
1477 end
= next
->yoff
+ next
->sy
- 1;
1480 if (next
->yoff
< top
&& end
> bottom
)
1482 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1484 else if (end
>= top
&& end
<= bottom
)
1488 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1489 list
[size
++] = next
;
1492 best
= window_pane_choose_best(list
, size
);
1498 window_pane_stack_push(struct window_panes
*stack
, struct window_pane
*wp
)
1501 window_pane_stack_remove(stack
, wp
);
1502 TAILQ_INSERT_HEAD(stack
, wp
, sentry
);
1503 wp
->flags
|= PANE_VISITED
;
1508 window_pane_stack_remove(struct window_panes
*stack
, struct window_pane
*wp
)
1510 if (wp
!= NULL
&& (wp
->flags
& PANE_VISITED
)) {
1511 TAILQ_REMOVE(stack
, wp
, sentry
);
1512 wp
->flags
&= ~PANE_VISITED
;
1516 /* Clear alert flags for a winlink */
1518 winlink_clear_flags(struct winlink
*wl
)
1520 struct winlink
*loop
;
1522 wl
->window
->flags
&= ~WINDOW_ALERTFLAGS
;
1523 TAILQ_FOREACH(loop
, &wl
->window
->winlinks
, wentry
) {
1524 if ((loop
->flags
& WINLINK_ALERTFLAGS
) != 0) {
1525 loop
->flags
&= ~WINLINK_ALERTFLAGS
;
1526 server_status_session(loop
->session
);
1531 /* Shuffle window indexes up. */
1533 winlink_shuffle_up(struct session
*s
, struct winlink
*wl
, int before
)
1544 /* Find the next free index. */
1545 for (last
= idx
; last
< INT_MAX
; last
++) {
1546 if (winlink_find_by_index(&s
->windows
, last
) == NULL
)
1549 if (last
== INT_MAX
)
1552 /* Move everything from last - 1 to idx up a bit. */
1553 for (; last
> idx
; last
--) {
1554 wl
= winlink_find_by_index(&s
->windows
, last
- 1);
1555 RB_REMOVE(winlinks
, &s
->windows
, wl
);
1557 RB_INSERT(winlinks
, &s
->windows
, wl
);
1564 window_pane_input_callback(struct client
*c
, __unused
const char *path
,
1565 int error
, int closed
, struct evbuffer
*buffer
, void *data
)
1567 struct window_pane_input_data
*cdata
= data
;
1568 struct window_pane
*wp
;
1569 u_char
*buf
= EVBUFFER_DATA(buffer
);
1570 size_t len
= EVBUFFER_LENGTH(buffer
);
1572 wp
= window_pane_find_by_id(cdata
->wp
);
1573 if (cdata
->file
!= NULL
&& (wp
== NULL
|| c
->flags
& CLIENT_DEAD
)) {
1576 c
->flags
|= CLIENT_EXIT
;
1578 file_cancel(cdata
->file
);
1579 } else if (cdata
->file
== NULL
|| closed
|| error
!= 0) {
1580 cmdq_continue(cdata
->item
);
1581 server_client_unref(c
);
1584 input_parse_buffer(wp
, buf
, len
);
1585 evbuffer_drain(buffer
, len
);
1589 window_pane_start_input(struct window_pane
*wp
, struct cmdq_item
*item
,
1592 struct client
*c
= cmdq_get_client(item
);
1593 struct window_pane_input_data
*cdata
;
1595 if (~wp
->flags
& PANE_EMPTY
) {
1596 *cause
= xstrdup("pane is not empty");
1599 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
1601 if (c
->session
!= NULL
)
1604 cdata
= xmalloc(sizeof *cdata
);
1607 cdata
->file
= file_read(c
, "-", window_pane_input_callback
, cdata
);
1614 window_pane_get_new_data(struct window_pane
*wp
,
1615 struct window_pane_offset
*wpo
, size_t *size
)
1617 size_t used
= wpo
->used
- wp
->base_offset
;
1619 *size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1620 return (EVBUFFER_DATA(wp
->event
->input
) + used
);
1624 window_pane_update_used_data(struct window_pane
*wp
,
1625 struct window_pane_offset
*wpo
, size_t size
)
1627 size_t used
= wpo
->used
- wp
->base_offset
;
1629 if (size
> EVBUFFER_LENGTH(wp
->event
->input
) - used
)
1630 size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1635 window_set_fill_character(struct window
*w
)
1638 struct utf8_data
*ud
;
1640 free(w
->fill_character
);
1641 w
->fill_character
= NULL
;
1643 value
= options_get_string(w
->options
, "fill-character");
1644 if (*value
!= '\0' && utf8_isvalid(value
)) {
1645 ud
= utf8_fromcstr(value
);
1646 if (ud
!= NULL
&& ud
[0].width
== 1)
1647 w
->fill_character
= ud
;
1652 window_pane_default_cursor(struct window_pane
*wp
)
1654 struct screen
*s
= wp
->screen
;
1657 c
= options_get_number(wp
->options
, "cursor-colour");
1658 s
->default_ccolour
= c
;
1660 c
= options_get_number(wp
->options
, "cursor-style");
1661 s
->default_mode
= 0;
1662 screen_set_cursor_style(c
, &s
->default_cstyle
, &s
->default_mode
);