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
);
251 wl
->flags
|= WINLINK_VISITED
;
255 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
257 if (wl
!= NULL
&& (wl
->flags
& WINLINK_VISITED
)) {
258 TAILQ_REMOVE(stack
, wl
, sentry
);
259 wl
->flags
&= ~WINLINK_VISITED
;
264 window_find_by_id_str(const char *s
)
272 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
275 return (window_find_by_id(id
));
279 window_find_by_id(u_int id
)
284 return (RB_FIND(windows
, &windows
, &w
));
288 window_update_activity(struct window
*w
)
290 gettimeofday(&w
->activity_time
, NULL
);
291 alerts_queue(w
, WINDOW_ACTIVITY
);
295 window_create(u_int sx
, u_int sy
, u_int xpixel
, u_int ypixel
)
300 xpixel
= DEFAULT_XPIXEL
;
302 ypixel
= DEFAULT_YPIXEL
;
304 w
= xcalloc(1, sizeof *w
);
305 w
->name
= xstrdup("");
308 TAILQ_INIT(&w
->panes
);
309 TAILQ_INIT(&w
->last_panes
);
313 w
->layout_root
= NULL
;
322 w
->options
= options_create(global_w_options
);
325 TAILQ_INIT(&w
->winlinks
);
327 w
->id
= next_window_id
++;
328 RB_INSERT(windows
, &windows
, w
);
330 window_set_fill_character(w
);
331 window_update_activity(w
);
333 log_debug("%s: @%u create %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
334 w
->xpixel
, w
->ypixel
);
339 window_destroy(struct window
*w
)
341 log_debug("window @%u destroyed (%d references)", w
->id
, w
->references
);
344 RB_REMOVE(windows
, &windows
, w
);
346 if (w
->layout_root
!= NULL
)
347 layout_free_cell(w
->layout_root
);
348 if (w
->saved_layout_root
!= NULL
)
349 layout_free_cell(w
->saved_layout_root
);
352 window_destroy_panes(w
);
354 if (event_initialized(&w
->name_event
))
355 evtimer_del(&w
->name_event
);
357 if (event_initialized(&w
->alerts_timer
))
358 evtimer_del(&w
->alerts_timer
);
359 if (event_initialized(&w
->offset_timer
))
360 event_del(&w
->offset_timer
);
362 options_free(w
->options
);
363 free(w
->fill_character
);
370 window_pane_destroy_ready(struct window_pane
*wp
)
374 if (wp
->pipe_fd
!= -1) {
375 if (EVBUFFER_LENGTH(wp
->pipe_event
->output
) != 0)
377 if (ioctl(wp
->fd
, FIONREAD
, &n
) != -1 && n
> 0)
381 if (~wp
->flags
& PANE_EXITED
)
387 window_add_ref(struct window
*w
, const char *from
)
390 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
394 window_remove_ref(struct window
*w
, const char *from
)
397 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
399 if (w
->references
== 0)
404 window_set_name(struct window
*w
, const char *new_name
)
407 utf8_stravis(&w
->name
, new_name
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
408 notify_window("window-renamed", w
);
412 window_resize(struct window
*w
, u_int sx
, u_int sy
, int xpixel
, int ypixel
)
415 xpixel
= DEFAULT_XPIXEL
;
417 ypixel
= DEFAULT_YPIXEL
;
419 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
420 xpixel
== -1 ? w
->xpixel
: (u_int
)xpixel
,
421 ypixel
== -1 ? w
->ypixel
: (u_int
)ypixel
);
431 window_pane_send_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
433 struct window
*w
= wp
->window
;
439 log_debug("%s: %%%u resize to %u,%u", __func__
, wp
->id
, sx
, sy
);
441 memset(&ws
, 0, sizeof ws
);
444 ws
.ws_xpixel
= w
->xpixel
* ws
.ws_col
;
445 ws
.ws_ypixel
= w
->ypixel
* ws
.ws_row
;
446 if (ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
447 fatal("ioctl failed");
451 window_has_pane(struct window
*w
, struct window_pane
*wp
)
453 struct window_pane
*wp1
;
455 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
463 window_update_focus(struct window
*w
)
466 log_debug("%s: @%u", __func__
, w
->id
);
467 window_pane_update_focus(w
->active
);
472 window_pane_update_focus(struct window_pane
*wp
)
478 if (wp
!= wp
->window
->active
)
481 TAILQ_FOREACH(c
, &clients
, entry
) {
482 if (c
->session
!= NULL
&&
483 c
->session
->attached
!= 0 &&
484 (c
->flags
& CLIENT_FOCUSED
) &&
485 c
->session
->curw
->window
== wp
->window
) {
491 if (!focused
&& (wp
->flags
& PANE_FOCUSED
)) {
492 log_debug("%s: %%%u focus out", __func__
, wp
->id
);
493 if (wp
->base
.mode
& MODE_FOCUSON
)
494 bufferevent_write(wp
->event
, "\033[O", 3);
495 notify_pane("pane-focus-out", wp
);
496 wp
->flags
&= ~PANE_FOCUSED
;
497 } else if (focused
&& (~wp
->flags
& PANE_FOCUSED
)) {
498 log_debug("%s: %%%u focus in", __func__
, wp
->id
);
499 if (wp
->base
.mode
& MODE_FOCUSON
)
500 bufferevent_write(wp
->event
, "\033[I", 3);
501 notify_pane("pane-focus-in", wp
);
502 wp
->flags
|= PANE_FOCUSED
;
504 log_debug("%s: %%%u focus unchanged", __func__
, wp
->id
);
509 window_set_active_pane(struct window
*w
, struct window_pane
*wp
, int notify
)
511 struct window_pane
*lastwp
;
513 log_debug("%s: pane %%%u", __func__
, wp
->id
);
519 window_pane_stack_remove(&w
->last_panes
, wp
);
520 window_pane_stack_push(&w
->last_panes
, lastwp
);
523 w
->active
->active_point
= next_active_point
++;
524 w
->active
->flags
|= PANE_CHANGED
;
526 if (options_get_number(global_options
, "focus-events")) {
527 window_pane_update_focus(lastwp
);
528 window_pane_update_focus(w
->active
);
531 tty_update_window_offset(w
);
534 notify_window("window-pane-changed", w
);
539 window_pane_get_palette(struct window_pane
*wp
, int c
)
543 return (colour_palette_get(&wp
->palette
, c
));
547 window_redraw_active_switch(struct window
*w
, struct window_pane
*wp
)
549 struct grid_cell
*gc1
, *gc2
;
557 * If the active and inactive styles or palettes are different,
558 * need to redraw the panes.
560 gc1
= &wp
->cached_gc
;
561 gc2
= &wp
->cached_active_gc
;
562 if (!grid_cells_look_equal(gc1
, gc2
))
563 wp
->flags
|= PANE_REDRAW
;
565 c1
= window_pane_get_palette(wp
, gc1
->fg
);
566 c2
= window_pane_get_palette(wp
, gc2
->fg
);
568 wp
->flags
|= PANE_REDRAW
;
570 c1
= window_pane_get_palette(wp
, gc1
->bg
);
571 c2
= window_pane_get_palette(wp
, gc2
->bg
);
573 wp
->flags
|= PANE_REDRAW
;
583 window_get_active_at(struct window
*w
, u_int x
, u_int y
)
585 struct window_pane
*wp
;
587 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
588 if (!window_pane_visible(wp
))
590 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
592 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
600 window_find_string(struct window
*w
, const char *s
)
602 u_int x
, y
, top
= 0, bottom
= w
->sy
- 1;
608 status
= options_get_number(w
->options
, "pane-border-status");
609 if (status
== PANE_STATUS_TOP
)
611 else if (status
== PANE_STATUS_BOTTOM
)
614 if (strcasecmp(s
, "top") == 0)
616 else if (strcasecmp(s
, "bottom") == 0)
618 else if (strcasecmp(s
, "left") == 0)
620 else if (strcasecmp(s
, "right") == 0)
622 else if (strcasecmp(s
, "top-left") == 0) {
625 } else if (strcasecmp(s
, "top-right") == 0) {
628 } else if (strcasecmp(s
, "bottom-left") == 0) {
631 } else if (strcasecmp(s
, "bottom-right") == 0) {
637 return (window_get_active_at(w
, x
, y
));
641 window_zoom(struct window_pane
*wp
)
643 struct window
*w
= wp
->window
;
644 struct window_pane
*wp1
;
646 if (w
->flags
& WINDOW_ZOOMED
)
649 if (window_count_panes(w
) == 1)
653 window_set_active_pane(w
, wp
, 1);
655 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
656 wp1
->saved_layout_cell
= wp1
->layout_cell
;
657 wp1
->layout_cell
= NULL
;
660 w
->saved_layout_root
= w
->layout_root
;
662 w
->flags
|= WINDOW_ZOOMED
;
663 notify_window("window-layout-changed", w
);
669 window_unzoom(struct window
*w
, int notify
)
671 struct window_pane
*wp
;
673 if (!(w
->flags
& WINDOW_ZOOMED
))
676 w
->flags
&= ~WINDOW_ZOOMED
;
678 w
->layout_root
= w
->saved_layout_root
;
679 w
->saved_layout_root
= NULL
;
681 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
682 wp
->layout_cell
= wp
->saved_layout_cell
;
683 wp
->saved_layout_cell
= NULL
;
685 layout_fix_panes(w
, NULL
);
688 notify_window("window-layout-changed", w
);
694 window_push_zoom(struct window
*w
, int always
, int flag
)
696 log_debug("%s: @%u %d", __func__
, w
->id
,
697 flag
&& (w
->flags
& WINDOW_ZOOMED
));
698 if (flag
&& (always
|| (w
->flags
& WINDOW_ZOOMED
)))
699 w
->flags
|= WINDOW_WASZOOMED
;
701 w
->flags
&= ~WINDOW_WASZOOMED
;
702 return (window_unzoom(w
, 1) == 0);
706 window_pop_zoom(struct window
*w
)
708 log_debug("%s: @%u %d", __func__
, w
->id
,
709 !!(w
->flags
& WINDOW_WASZOOMED
));
710 if (w
->flags
& WINDOW_WASZOOMED
)
711 return (window_zoom(w
->active
) == 0);
716 window_add_pane(struct window
*w
, struct window_pane
*other
, u_int hlimit
,
719 struct window_pane
*wp
;
724 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
725 if (TAILQ_EMPTY(&w
->panes
)) {
726 log_debug("%s: @%u at start", __func__
, w
->id
);
727 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
728 } else if (flags
& SPAWN_BEFORE
) {
729 log_debug("%s: @%u before %%%u", __func__
, w
->id
, wp
->id
);
730 if (flags
& SPAWN_FULLSIZE
)
731 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
733 TAILQ_INSERT_BEFORE(other
, wp
, entry
);
735 log_debug("%s: @%u after %%%u", __func__
, w
->id
, wp
->id
);
736 if (flags
& SPAWN_FULLSIZE
)
737 TAILQ_INSERT_TAIL(&w
->panes
, wp
, entry
);
739 TAILQ_INSERT_AFTER(&w
->panes
, other
, wp
, entry
);
745 window_lost_pane(struct window
*w
, struct window_pane
*wp
)
747 log_debug("%s: @%u pane %%%u", __func__
, w
->id
, wp
->id
);
749 if (wp
== marked_pane
.wp
)
750 server_clear_marked();
752 window_pane_stack_remove(&w
->last_panes
, wp
);
753 if (wp
== w
->active
) {
754 w
->active
= TAILQ_FIRST(&w
->last_panes
);
755 if (w
->active
== NULL
) {
756 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
757 if (w
->active
== NULL
)
758 w
->active
= TAILQ_NEXT(wp
, entry
);
760 if (w
->active
!= NULL
) {
761 window_pane_stack_remove(&w
->last_panes
, w
->active
);
762 w
->active
->flags
|= PANE_CHANGED
;
763 notify_window("window-pane-changed", w
);
764 window_update_focus(w
);
770 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
772 window_lost_pane(w
, wp
);
774 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
775 window_pane_destroy(wp
);
779 window_pane_at_index(struct window
*w
, u_int idx
)
781 struct window_pane
*wp
;
784 n
= options_get_number(w
->options
, "pane-base-index");
785 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
794 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
797 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
798 wp
= TAILQ_FIRST(&w
->panes
);
805 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
809 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
810 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
817 window_pane_index(struct window_pane
*wp
, u_int
*i
)
819 struct window_pane
*wq
;
820 struct window
*w
= wp
->window
;
822 *i
= options_get_number(w
->options
, "pane-base-index");
823 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
834 window_count_panes(struct window
*w
)
836 struct window_pane
*wp
;
840 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
846 window_destroy_panes(struct window
*w
)
848 struct window_pane
*wp
;
850 while (!TAILQ_EMPTY(&w
->last_panes
)) {
851 wp
= TAILQ_FIRST(&w
->last_panes
);
852 window_pane_stack_remove(&w
->last_panes
, wp
);
855 while (!TAILQ_EMPTY(&w
->panes
)) {
856 wp
= TAILQ_FIRST(&w
->panes
);
857 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
858 window_pane_destroy(wp
);
863 window_printable_flags(struct winlink
*wl
, int escape
)
865 struct session
*s
= wl
->session
;
866 static char flags
[32];
870 if (wl
->flags
& WINLINK_ACTIVITY
) {
875 if (wl
->flags
& WINLINK_BELL
)
877 if (wl
->flags
& WINLINK_SILENCE
)
881 if (wl
== TAILQ_FIRST(&s
->lastw
))
883 if (server_check_marked() && wl
== marked_pane
.wl
)
885 if (wl
->window
->flags
& WINDOW_ZOOMED
)
892 window_pane_find_by_id_str(const char *s
)
900 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
903 return (window_pane_find_by_id(id
));
907 window_pane_find_by_id(u_int id
)
909 struct window_pane wp
;
912 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
915 static struct window_pane
*
916 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
918 struct window_pane
*wp
;
919 char host
[HOST_NAME_MAX
+ 1];
921 wp
= xcalloc(1, sizeof *wp
);
923 wp
->options
= options_create(w
->options
);
924 wp
->flags
= PANE_STYLECHANGED
;
926 wp
->id
= next_window_pane_id
++;
927 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
931 TAILQ_INIT(&wp
->modes
);
933 TAILQ_INIT (&wp
->resize_queue
);
940 colour_palette_init(&wp
->palette
);
941 colour_palette_from_option(&wp
->palette
, wp
->options
);
943 screen_init(&wp
->base
, sx
, sy
, hlimit
);
944 wp
->screen
= &wp
->base
;
945 window_pane_default_cursor(wp
);
947 screen_init(&wp
->status_screen
, 1, 1, 0);
949 if (gethostname(host
, sizeof host
) == 0)
950 screen_set_title(&wp
->base
, host
);
956 window_pane_destroy(struct window_pane
*wp
)
958 struct window_pane_resize
*r
;
959 struct window_pane_resize
*r1
;
961 window_pane_reset_mode_all(wp
);
965 bufferevent_free(wp
->event
);
968 if (wp
->ictx
!= NULL
)
969 input_free(wp
->ictx
);
971 screen_free(&wp
->status_screen
);
973 screen_free(&wp
->base
);
975 if (wp
->pipe_fd
!= -1) {
976 bufferevent_free(wp
->pipe_event
);
980 if (event_initialized(&wp
->resize_timer
))
981 event_del(&wp
->resize_timer
);
982 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
983 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
987 RB_REMOVE(window_pane_tree
, &all_window_panes
, wp
);
989 options_free(wp
->options
);
990 free((void *)wp
->cwd
);
992 cmd_free_argv(wp
->argc
, wp
->argv
);
993 colour_palette_free(&wp
->palette
);
998 window_pane_read_callback(__unused
struct bufferevent
*bufev
, void *data
)
1000 struct window_pane
*wp
= data
;
1001 struct evbuffer
*evb
= wp
->event
->input
;
1002 struct window_pane_offset
*wpo
= &wp
->pipe_offset
;
1003 size_t size
= EVBUFFER_LENGTH(evb
);
1008 if (wp
->pipe_fd
!= -1) {
1009 new_data
= window_pane_get_new_data(wp
, wpo
, &new_size
);
1011 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
1012 window_pane_update_used_data(wp
, wpo
, new_size
);
1016 log_debug("%%%u has %zu bytes", wp
->id
, size
);
1017 TAILQ_FOREACH(c
, &clients
, entry
) {
1018 if (c
->session
!= NULL
&& (c
->flags
& CLIENT_CONTROL
))
1019 control_write_output(c
, wp
);
1021 input_parse_pane(wp
);
1022 bufferevent_disable(wp
->event
, EV_READ
);
1026 window_pane_error_callback(__unused
struct bufferevent
*bufev
,
1027 __unused
short what
, void *data
)
1029 struct window_pane
*wp
= data
;
1031 log_debug("%%%u error", wp
->id
);
1032 wp
->flags
|= PANE_EXITED
;
1034 if (window_pane_destroy_ready(wp
))
1035 server_destroy_pane(wp
, 1);
1039 window_pane_set_event(struct window_pane
*wp
)
1041 setblocking(wp
->fd
, 0);
1043 wp
->event
= bufferevent_new(wp
->fd
, window_pane_read_callback
,
1044 NULL
, window_pane_error_callback
, wp
);
1045 if (wp
->event
== NULL
)
1046 fatalx("out of memory");
1047 wp
->ictx
= input_init(wp
, wp
->event
, &wp
->palette
);
1049 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
1053 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
1055 struct window_mode_entry
*wme
;
1056 struct window_pane_resize
*r
;
1058 if (sx
== wp
->sx
&& sy
== wp
->sy
)
1061 r
= xmalloc(sizeof *r
);
1066 TAILQ_INSERT_TAIL (&wp
->resize_queue
, r
, entry
);
1071 log_debug("%s: %%%u resize %ux%u", __func__
, wp
->id
, sx
, sy
);
1072 screen_resize(&wp
->base
, sx
, sy
, wp
->base
.saved_grid
== NULL
);
1074 wme
= TAILQ_FIRST(&wp
->modes
);
1075 if (wme
!= NULL
&& wme
->mode
->resize
!= NULL
)
1076 wme
->mode
->resize(wme
, sx
, sy
);
1080 window_pane_set_mode(struct window_pane
*wp
, struct window_pane
*swp
,
1081 const struct window_mode
*mode
, struct cmd_find_state
*fs
,
1084 struct window_mode_entry
*wme
;
1086 if (!TAILQ_EMPTY(&wp
->modes
) && TAILQ_FIRST(&wp
->modes
)->mode
== mode
)
1089 TAILQ_FOREACH(wme
, &wp
->modes
, entry
) {
1090 if (wme
->mode
== mode
)
1094 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1095 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1097 wme
= xcalloc(1, sizeof *wme
);
1102 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1103 wme
->screen
= wme
->mode
->init(wme
, fs
, args
);
1106 wp
->screen
= wme
->screen
;
1107 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1109 server_redraw_window_borders(wp
->window
);
1110 server_status_window(wp
->window
);
1111 notify_pane("pane-mode-changed", wp
);
1117 window_pane_reset_mode(struct window_pane
*wp
)
1119 struct window_mode_entry
*wme
, *next
;
1121 if (TAILQ_EMPTY(&wp
->modes
))
1124 wme
= TAILQ_FIRST(&wp
->modes
);
1125 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1126 wme
->mode
->free(wme
);
1129 next
= TAILQ_FIRST(&wp
->modes
);
1131 wp
->flags
&= ~PANE_UNSEENCHANGES
;
1132 log_debug("%s: no next mode", __func__
);
1133 wp
->screen
= &wp
->base
;
1135 log_debug("%s: next mode is %s", __func__
, next
->mode
->name
);
1136 wp
->screen
= next
->screen
;
1137 if (next
->mode
->resize
!= NULL
)
1138 next
->mode
->resize(next
, wp
->sx
, wp
->sy
);
1140 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1142 server_redraw_window_borders(wp
->window
);
1143 server_status_window(wp
->window
);
1144 notify_pane("pane-mode-changed", wp
);
1148 window_pane_reset_mode_all(struct window_pane
*wp
)
1150 while (!TAILQ_EMPTY(&wp
->modes
))
1151 window_pane_reset_mode(wp
);
1155 window_pane_copy_key(struct window_pane
*wp
, key_code key
)
1157 struct window_pane
*loop
;
1159 TAILQ_FOREACH(loop
, &wp
->window
->panes
, entry
) {
1161 TAILQ_EMPTY(&loop
->modes
) &&
1163 (~loop
->flags
& PANE_INPUTOFF
) &&
1164 window_pane_visible(loop
) &&
1165 options_get_number(loop
->options
, "synchronize-panes"))
1166 input_key_pane(loop
, key
, NULL
);
1171 window_pane_key(struct window_pane
*wp
, struct client
*c
, struct session
*s
,
1172 struct winlink
*wl
, key_code key
, struct mouse_event
*m
)
1174 struct window_mode_entry
*wme
;
1176 if (KEYC_IS_MOUSE(key
) && m
== NULL
)
1179 wme
= TAILQ_FIRST(&wp
->modes
);
1181 if (wme
->mode
->key
!= NULL
&& c
!= NULL
) {
1182 key
&= ~KEYC_MASK_FLAGS
;
1183 wme
->mode
->key(wme
, c
, s
, wl
, key
, m
);
1188 if (wp
->fd
== -1 || wp
->flags
& PANE_INPUTOFF
)
1191 if (input_key_pane(wp
, key
, m
) != 0)
1194 if (KEYC_IS_MOUSE(key
))
1196 if (options_get_number(wp
->options
, "synchronize-panes"))
1197 window_pane_copy_key(wp
, key
);
1202 window_pane_visible(struct window_pane
*wp
)
1204 if (~wp
->window
->flags
& WINDOW_ZOOMED
)
1206 return (wp
== wp
->window
->active
);
1210 window_pane_exited(struct window_pane
*wp
)
1212 return (wp
->fd
== -1 || (wp
->flags
& PANE_EXITED
));
1216 window_pane_search(struct window_pane
*wp
, const char *term
, int regex
,
1219 struct screen
*s
= &wp
->base
;
1221 char *new = NULL
, *line
;
1223 int flags
= 0, found
;
1228 flags
|= FNM_CASEFOLD
;
1229 xasprintf(&new, "*%s*", term
);
1233 if (regcomp(&r
, term
, flags
|REG_EXTENDED
) != 0)
1237 for (i
= 0; i
< screen_size_y(s
); i
++) {
1238 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
1239 for (n
= strlen(line
); n
> 0; n
--) {
1240 if (!isspace((u_char
)line
[n
- 1]))
1244 log_debug("%s: %s", __func__
, line
);
1246 found
= (fnmatch(new, line
, flags
) == 0);
1248 found
= (regexec(&r
, line
, 0, NULL
, 0) == 0);
1258 if (i
== screen_size_y(s
))
1263 /* Get MRU pane from a list. */
1264 static struct window_pane
*
1265 window_pane_choose_best(struct window_pane
**list
, u_int size
)
1267 struct window_pane
*next
, *best
;
1274 for (i
= 1; i
< size
; i
++) {
1276 if (next
->active_point
> best
->active_point
)
1283 * Find the pane directly above another. We build a list of those adjacent to
1284 * top edge and then choose the best.
1286 struct window_pane
*
1287 window_pane_find_up(struct window_pane
*wp
)
1290 struct window_pane
*next
, *best
, **list
;
1291 u_int edge
, left
, right
, end
, size
;
1297 status
= options_get_number(w
->options
, "pane-border-status");
1303 if (status
== PANE_STATUS_TOP
) {
1306 } else if (status
== PANE_STATUS_BOTTOM
) {
1315 right
= wp
->xoff
+ wp
->sx
;
1317 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1320 if (next
->yoff
+ next
->sy
+ 1 != edge
)
1322 end
= next
->xoff
+ next
->sx
- 1;
1325 if (next
->xoff
< left
&& end
> right
)
1327 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1329 else if (end
>= left
&& end
<= right
)
1333 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1334 list
[size
++] = next
;
1337 best
= window_pane_choose_best(list
, size
);
1342 /* Find the pane directly below another. */
1343 struct window_pane
*
1344 window_pane_find_down(struct window_pane
*wp
)
1347 struct window_pane
*next
, *best
, **list
;
1348 u_int edge
, left
, right
, end
, size
;
1354 status
= options_get_number(w
->options
, "pane-border-status");
1359 edge
= wp
->yoff
+ wp
->sy
+ 1;
1360 if (status
== PANE_STATUS_TOP
) {
1363 } else if (status
== PANE_STATUS_BOTTOM
) {
1364 if (edge
>= w
->sy
- 1)
1372 right
= wp
->xoff
+ wp
->sx
;
1374 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1377 if (next
->yoff
!= edge
)
1379 end
= next
->xoff
+ next
->sx
- 1;
1382 if (next
->xoff
< left
&& end
> right
)
1384 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1386 else if (end
>= left
&& end
<= right
)
1390 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1391 list
[size
++] = next
;
1394 best
= window_pane_choose_best(list
, size
);
1399 /* Find the pane directly to the left of another. */
1400 struct window_pane
*
1401 window_pane_find_left(struct window_pane
*wp
)
1404 struct window_pane
*next
, *best
, **list
;
1405 u_int edge
, top
, bottom
, end
, size
;
1420 bottom
= wp
->yoff
+ wp
->sy
;
1422 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1425 if (next
->xoff
+ next
->sx
+ 1 != edge
)
1427 end
= next
->yoff
+ next
->sy
- 1;
1430 if (next
->yoff
< top
&& end
> bottom
)
1432 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1434 else if (end
>= top
&& end
<= bottom
)
1438 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1439 list
[size
++] = next
;
1442 best
= window_pane_choose_best(list
, size
);
1447 /* Find the pane directly to the right of another. */
1448 struct window_pane
*
1449 window_pane_find_right(struct window_pane
*wp
)
1452 struct window_pane
*next
, *best
, **list
;
1453 u_int edge
, top
, bottom
, end
, size
;
1463 edge
= wp
->xoff
+ wp
->sx
+ 1;
1468 bottom
= wp
->yoff
+ wp
->sy
;
1470 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1473 if (next
->xoff
!= edge
)
1475 end
= next
->yoff
+ next
->sy
- 1;
1478 if (next
->yoff
< top
&& end
> bottom
)
1480 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1482 else if (end
>= top
&& end
<= bottom
)
1486 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1487 list
[size
++] = next
;
1490 best
= window_pane_choose_best(list
, size
);
1496 window_pane_stack_push(struct window_panes
*stack
, struct window_pane
*wp
)
1499 window_pane_stack_remove(stack
, wp
);
1500 TAILQ_INSERT_HEAD(stack
, wp
, sentry
);
1501 wp
->flags
|= PANE_VISITED
;
1506 window_pane_stack_remove(struct window_panes
*stack
, struct window_pane
*wp
)
1508 if (wp
!= NULL
&& (wp
->flags
& PANE_VISITED
)) {
1509 TAILQ_REMOVE(stack
, wp
, sentry
);
1510 wp
->flags
&= ~PANE_VISITED
;
1514 /* Clear alert flags for a winlink */
1516 winlink_clear_flags(struct winlink
*wl
)
1518 struct winlink
*loop
;
1520 wl
->window
->flags
&= ~WINDOW_ALERTFLAGS
;
1521 TAILQ_FOREACH(loop
, &wl
->window
->winlinks
, wentry
) {
1522 if ((loop
->flags
& WINLINK_ALERTFLAGS
) != 0) {
1523 loop
->flags
&= ~WINLINK_ALERTFLAGS
;
1524 server_status_session(loop
->session
);
1529 /* Shuffle window indexes up. */
1531 winlink_shuffle_up(struct session
*s
, struct winlink
*wl
, int before
)
1542 /* Find the next free index. */
1543 for (last
= idx
; last
< INT_MAX
; last
++) {
1544 if (winlink_find_by_index(&s
->windows
, last
) == NULL
)
1547 if (last
== INT_MAX
)
1550 /* Move everything from last - 1 to idx up a bit. */
1551 for (; last
> idx
; last
--) {
1552 wl
= winlink_find_by_index(&s
->windows
, last
- 1);
1553 RB_REMOVE(winlinks
, &s
->windows
, wl
);
1555 RB_INSERT(winlinks
, &s
->windows
, wl
);
1562 window_pane_input_callback(struct client
*c
, __unused
const char *path
,
1563 int error
, int closed
, struct evbuffer
*buffer
, void *data
)
1565 struct window_pane_input_data
*cdata
= data
;
1566 struct window_pane
*wp
;
1567 u_char
*buf
= EVBUFFER_DATA(buffer
);
1568 size_t len
= EVBUFFER_LENGTH(buffer
);
1570 wp
= window_pane_find_by_id(cdata
->wp
);
1571 if (cdata
->file
!= NULL
&& (wp
== NULL
|| c
->flags
& CLIENT_DEAD
)) {
1574 c
->flags
|= CLIENT_EXIT
;
1576 file_cancel(cdata
->file
);
1577 } else if (cdata
->file
== NULL
|| closed
|| error
!= 0) {
1578 cmdq_continue(cdata
->item
);
1579 server_client_unref(c
);
1582 input_parse_buffer(wp
, buf
, len
);
1583 evbuffer_drain(buffer
, len
);
1587 window_pane_start_input(struct window_pane
*wp
, struct cmdq_item
*item
,
1590 struct client
*c
= cmdq_get_client(item
);
1591 struct window_pane_input_data
*cdata
;
1593 if (~wp
->flags
& PANE_EMPTY
) {
1594 *cause
= xstrdup("pane is not empty");
1597 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
1599 if (c
->session
!= NULL
)
1602 cdata
= xmalloc(sizeof *cdata
);
1605 cdata
->file
= file_read(c
, "-", window_pane_input_callback
, cdata
);
1612 window_pane_get_new_data(struct window_pane
*wp
,
1613 struct window_pane_offset
*wpo
, size_t *size
)
1615 size_t used
= wpo
->used
- wp
->base_offset
;
1617 *size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1618 return (EVBUFFER_DATA(wp
->event
->input
) + used
);
1622 window_pane_update_used_data(struct window_pane
*wp
,
1623 struct window_pane_offset
*wpo
, size_t size
)
1625 size_t used
= wpo
->used
- wp
->base_offset
;
1627 if (size
> EVBUFFER_LENGTH(wp
->event
->input
) - used
)
1628 size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1633 window_set_fill_character(struct window
*w
)
1636 struct utf8_data
*ud
;
1638 free(w
->fill_character
);
1639 w
->fill_character
= NULL
;
1641 value
= options_get_string(w
->options
, "fill-character");
1642 if (*value
!= '\0' && utf8_isvalid(value
)) {
1643 ud
= utf8_fromcstr(value
);
1644 if (ud
!= NULL
&& ud
[0].width
== 1)
1645 w
->fill_character
= ud
;
1650 window_pane_default_cursor(struct window_pane
*wp
)
1652 struct screen
*s
= wp
->screen
;
1655 c
= options_get_number(wp
->options
, "cursor-colour");
1656 s
->default_ccolour
= c
;
1658 c
= options_get_number(wp
->options
, "cursor-style");
1659 s
->default_mode
= 0;
1660 screen_set_cursor_style(c
, &s
->default_cstyle
, &s
->default_mode
);