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
;
69 static struct window_pane
*window_pane_create(struct window
*, u_int
, u_int
,
71 static void window_pane_destroy(struct window_pane
*);
73 RB_GENERATE(windows
, window
, entry
, window_cmp
);
74 RB_GENERATE(winlinks
, winlink
, entry
, winlink_cmp
);
75 RB_GENERATE(window_pane_tree
, window_pane
, tree_entry
, window_pane_cmp
);
78 window_cmp(struct window
*w1
, struct window
*w2
)
80 return (w1
->id
- w2
->id
);
84 winlink_cmp(struct winlink
*wl1
, struct winlink
*wl2
)
86 return (wl1
->idx
- wl2
->idx
);
90 window_pane_cmp(struct window_pane
*wp1
, struct window_pane
*wp2
)
92 return (wp1
->id
- wp2
->id
);
96 winlink_find_by_window(struct winlinks
*wwl
, struct window
*w
)
100 RB_FOREACH(wl
, winlinks
, wwl
) {
109 winlink_find_by_index(struct winlinks
*wwl
, int idx
)
117 return (RB_FIND(winlinks
, wwl
, &wl
));
121 winlink_find_by_window_id(struct winlinks
*wwl
, u_int id
)
125 RB_FOREACH(wl
, winlinks
, wwl
) {
126 if (wl
->window
->id
== id
)
133 winlink_next_index(struct winlinks
*wwl
, int idx
)
139 if (winlink_find_by_index(wwl
, i
) == NULL
)
150 winlink_count(struct winlinks
*wwl
)
156 RB_FOREACH(wl
, winlinks
, wwl
)
163 winlink_add(struct winlinks
*wwl
, int idx
)
168 if ((idx
= winlink_next_index(wwl
, -idx
- 1)) == -1)
170 } else if (winlink_find_by_index(wwl
, idx
) != NULL
)
173 wl
= xcalloc(1, sizeof *wl
);
175 RB_INSERT(winlinks
, wwl
, wl
);
181 winlink_set_window(struct winlink
*wl
, struct window
*w
)
183 if (wl
->window
!= NULL
) {
184 TAILQ_REMOVE(&wl
->window
->winlinks
, wl
, wentry
);
185 window_remove_ref(wl
->window
, __func__
);
187 TAILQ_INSERT_TAIL(&w
->winlinks
, wl
, wentry
);
189 window_add_ref(w
, __func__
);
193 winlink_remove(struct winlinks
*wwl
, struct winlink
*wl
)
195 struct window
*w
= wl
->window
;
198 TAILQ_REMOVE(&w
->winlinks
, wl
, wentry
);
199 window_remove_ref(w
, __func__
);
202 RB_REMOVE(winlinks
, wwl
, wl
);
207 winlink_next(struct winlink
*wl
)
209 return (RB_NEXT(winlinks
, wwl
, wl
));
213 winlink_previous(struct winlink
*wl
)
215 return (RB_PREV(winlinks
, wwl
, wl
));
219 winlink_next_by_number(struct winlink
*wl
, struct session
*s
, int n
)
222 if ((wl
= RB_NEXT(winlinks
, wwl
, wl
)) == NULL
)
223 wl
= RB_MIN(winlinks
, &s
->windows
);
230 winlink_previous_by_number(struct winlink
*wl
, struct session
*s
, int n
)
233 if ((wl
= RB_PREV(winlinks
, wwl
, wl
)) == NULL
)
234 wl
= RB_MAX(winlinks
, &s
->windows
);
241 winlink_stack_push(struct winlink_stack
*stack
, struct winlink
*wl
)
246 winlink_stack_remove(stack
, wl
);
247 TAILQ_INSERT_HEAD(stack
, wl
, sentry
);
251 winlink_stack_remove(struct winlink_stack
*stack
, struct winlink
*wl
)
258 TAILQ_FOREACH(wl2
, stack
, sentry
) {
260 TAILQ_REMOVE(stack
, wl
, sentry
);
267 window_find_by_id_str(const char *s
)
275 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
278 return (window_find_by_id(id
));
282 window_find_by_id(u_int id
)
287 return (RB_FIND(windows
, &windows
, &w
));
291 window_update_activity(struct window
*w
)
293 gettimeofday(&w
->activity_time
, NULL
);
294 alerts_queue(w
, WINDOW_ACTIVITY
);
298 window_create(u_int sx
, u_int sy
, u_int xpixel
, u_int ypixel
)
303 xpixel
= DEFAULT_XPIXEL
;
305 ypixel
= DEFAULT_YPIXEL
;
307 w
= xcalloc(1, sizeof *w
);
308 w
->name
= xstrdup("");
311 TAILQ_INIT(&w
->panes
);
315 w
->layout_root
= NULL
;
324 w
->options
= options_create(global_w_options
);
327 TAILQ_INIT(&w
->winlinks
);
329 w
->id
= next_window_id
++;
330 RB_INSERT(windows
, &windows
, w
);
332 window_update_activity(w
);
334 log_debug("%s: @%u create %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
335 w
->xpixel
, w
->ypixel
);
340 window_destroy(struct window
*w
)
342 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
);
369 window_pane_destroy_ready(struct window_pane
*wp
)
373 if (wp
->pipe_fd
!= -1) {
374 if (EVBUFFER_LENGTH(wp
->pipe_event
->output
) != 0)
376 if (ioctl(wp
->fd
, FIONREAD
, &n
) != -1 && n
> 0)
380 if (~wp
->flags
& PANE_EXITED
)
386 window_add_ref(struct window
*w
, const char *from
)
389 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
393 window_remove_ref(struct window
*w
, const char *from
)
396 log_debug("%s: @%u %s, now %d", __func__
, w
->id
, from
, w
->references
);
398 if (w
->references
== 0)
403 window_set_name(struct window
*w
, const char *new_name
)
406 utf8_stravis(&w
->name
, new_name
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
);
407 notify_window("window-renamed", w
);
411 window_resize(struct window
*w
, u_int sx
, u_int sy
, int xpixel
, int ypixel
)
414 xpixel
= DEFAULT_XPIXEL
;
416 ypixel
= DEFAULT_YPIXEL
;
418 log_debug("%s: @%u resize %ux%u (%ux%u)", __func__
, w
->id
, sx
, sy
,
419 xpixel
== -1 ? w
->xpixel
: (u_int
)xpixel
,
420 ypixel
== -1 ? w
->ypixel
: (u_int
)ypixel
);
430 window_pane_send_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
432 struct window
*w
= wp
->window
;
438 log_debug("%s: %%%u resize to %u,%u", __func__
, wp
->id
, sx
, sy
);
440 memset(&ws
, 0, sizeof ws
);
443 ws
.ws_xpixel
= w
->xpixel
* ws
.ws_col
;
444 ws
.ws_ypixel
= w
->ypixel
* ws
.ws_row
;
445 if (ioctl(wp
->fd
, TIOCSWINSZ
, &ws
) == -1)
448 * Some versions of Solaris apparently can return an error when
449 * resizing; don't know why this happens, can't reproduce on
450 * other platforms and ignoring it doesn't seem to cause any
453 if (errno
!= EINVAL
&& errno
!= ENXIO
)
455 fatal("ioctl failed");
459 window_has_pane(struct window
*w
, struct window_pane
*wp
)
461 struct window_pane
*wp1
;
463 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
471 window_update_focus(struct window
*w
)
474 log_debug("%s: @%u", __func__
, w
->id
);
475 window_pane_update_focus(w
->active
);
480 window_pane_update_focus(struct window_pane
*wp
)
486 if (wp
!= wp
->window
->active
)
489 TAILQ_FOREACH(c
, &clients
, entry
) {
490 if (c
->session
!= NULL
&&
491 c
->session
->attached
!= 0 &&
492 (c
->flags
& CLIENT_FOCUSED
) &&
493 c
->session
->curw
->window
== wp
->window
) {
499 if (!focused
&& (wp
->flags
& PANE_FOCUSED
)) {
500 log_debug("%s: %%%u focus out", __func__
, wp
->id
);
501 if (wp
->base
.mode
& MODE_FOCUSON
)
502 bufferevent_write(wp
->event
, "\033[O", 3);
503 notify_pane("pane-focus-out", wp
);
504 wp
->flags
&= ~PANE_FOCUSED
;
505 } else if (focused
&& (~wp
->flags
& PANE_FOCUSED
)) {
506 log_debug("%s: %%%u focus in", __func__
, wp
->id
);
507 if (wp
->base
.mode
& MODE_FOCUSON
)
508 bufferevent_write(wp
->event
, "\033[I", 3);
509 notify_pane("pane-focus-in", wp
);
510 wp
->flags
|= PANE_FOCUSED
;
512 log_debug("%s: %%%u focus unchanged", __func__
, wp
->id
);
517 window_set_active_pane(struct window
*w
, struct window_pane
*wp
, int notify
)
519 log_debug("%s: pane %%%u", __func__
, wp
->id
);
526 w
->active
->active_point
= next_active_point
++;
527 w
->active
->flags
|= PANE_CHANGED
;
529 if (options_get_number(global_options
, "focus-events")) {
530 window_pane_update_focus(w
->last
);
531 window_pane_update_focus(w
->active
);
534 tty_update_window_offset(w
);
537 notify_window("window-pane-changed", w
);
542 window_pane_get_palette(struct window_pane
*wp
, int c
)
546 return (colour_palette_get(&wp
->palette
, c
));
550 window_redraw_active_switch(struct window
*w
, struct window_pane
*wp
)
552 struct grid_cell
*gc1
, *gc2
;
560 * If the active and inactive styles or palettes are different,
561 * need to redraw the panes.
563 gc1
= &wp
->cached_gc
;
564 gc2
= &wp
->cached_active_gc
;
565 if (!grid_cells_look_equal(gc1
, gc2
))
566 wp
->flags
|= PANE_REDRAW
;
568 c1
= window_pane_get_palette(wp
, gc1
->fg
);
569 c2
= window_pane_get_palette(wp
, gc2
->fg
);
571 wp
->flags
|= PANE_REDRAW
;
573 c1
= window_pane_get_palette(wp
, gc1
->bg
);
574 c2
= window_pane_get_palette(wp
, gc2
->bg
);
576 wp
->flags
|= PANE_REDRAW
;
586 window_get_active_at(struct window
*w
, u_int x
, u_int y
)
588 struct window_pane
*wp
;
590 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
591 if (!window_pane_visible(wp
))
593 if (x
< wp
->xoff
|| x
> wp
->xoff
+ wp
->sx
)
595 if (y
< wp
->yoff
|| y
> wp
->yoff
+ wp
->sy
)
603 window_find_string(struct window
*w
, const char *s
)
605 u_int x
, y
, top
= 0, bottom
= w
->sy
- 1;
611 status
= options_get_number(w
->options
, "pane-border-status");
612 if (status
== PANE_STATUS_TOP
)
614 else if (status
== PANE_STATUS_BOTTOM
)
617 if (strcasecmp(s
, "top") == 0)
619 else if (strcasecmp(s
, "bottom") == 0)
621 else if (strcasecmp(s
, "left") == 0)
623 else if (strcasecmp(s
, "right") == 0)
625 else if (strcasecmp(s
, "top-left") == 0) {
628 } else if (strcasecmp(s
, "top-right") == 0) {
631 } else if (strcasecmp(s
, "bottom-left") == 0) {
634 } else if (strcasecmp(s
, "bottom-right") == 0) {
640 return (window_get_active_at(w
, x
, y
));
644 window_zoom(struct window_pane
*wp
)
646 struct window
*w
= wp
->window
;
647 struct window_pane
*wp1
;
649 if (w
->flags
& WINDOW_ZOOMED
)
652 if (window_count_panes(w
) == 1)
656 window_set_active_pane(w
, wp
, 1);
658 TAILQ_FOREACH(wp1
, &w
->panes
, entry
) {
659 wp1
->saved_layout_cell
= wp1
->layout_cell
;
660 wp1
->layout_cell
= NULL
;
663 w
->saved_layout_root
= w
->layout_root
;
665 w
->flags
|= WINDOW_ZOOMED
;
666 notify_window("window-layout-changed", w
);
672 window_unzoom(struct window
*w
)
674 struct window_pane
*wp
;
676 if (!(w
->flags
& WINDOW_ZOOMED
))
679 w
->flags
&= ~WINDOW_ZOOMED
;
681 w
->layout_root
= w
->saved_layout_root
;
682 w
->saved_layout_root
= NULL
;
684 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
685 wp
->layout_cell
= wp
->saved_layout_cell
;
686 wp
->saved_layout_cell
= NULL
;
688 layout_fix_panes(w
, NULL
);
689 notify_window("window-layout-changed", w
);
695 window_push_zoom(struct window
*w
, int always
, int flag
)
697 log_debug("%s: @%u %d", __func__
, w
->id
,
698 flag
&& (w
->flags
& WINDOW_ZOOMED
));
699 if (flag
&& (always
|| (w
->flags
& WINDOW_ZOOMED
)))
700 w
->flags
|= WINDOW_WASZOOMED
;
702 w
->flags
&= ~WINDOW_WASZOOMED
;
703 return (window_unzoom(w
) == 0);
707 window_pop_zoom(struct window
*w
)
709 log_debug("%s: @%u %d", __func__
, w
->id
,
710 !!(w
->flags
& WINDOW_WASZOOMED
));
711 if (w
->flags
& WINDOW_WASZOOMED
)
712 return (window_zoom(w
->active
) == 0);
717 window_add_pane(struct window
*w
, struct window_pane
*other
, u_int hlimit
,
720 struct window_pane
*wp
;
725 wp
= window_pane_create(w
, w
->sx
, w
->sy
, hlimit
);
726 if (TAILQ_EMPTY(&w
->panes
)) {
727 log_debug("%s: @%u at start", __func__
, w
->id
);
728 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
729 } else if (flags
& SPAWN_BEFORE
) {
730 log_debug("%s: @%u before %%%u", __func__
, w
->id
, wp
->id
);
731 if (flags
& SPAWN_FULLSIZE
)
732 TAILQ_INSERT_HEAD(&w
->panes
, wp
, entry
);
734 TAILQ_INSERT_BEFORE(other
, wp
, entry
);
736 log_debug("%s: @%u after %%%u", __func__
, w
->id
, wp
->id
);
737 if (flags
& SPAWN_FULLSIZE
)
738 TAILQ_INSERT_TAIL(&w
->panes
, wp
, entry
);
740 TAILQ_INSERT_AFTER(&w
->panes
, other
, wp
, entry
);
746 window_lost_pane(struct window
*w
, struct window_pane
*wp
)
748 log_debug("%s: @%u pane %%%u", __func__
, w
->id
, wp
->id
);
750 if (wp
== marked_pane
.wp
)
751 server_clear_marked();
753 if (wp
== w
->active
) {
756 if (w
->active
== NULL
) {
757 w
->active
= TAILQ_PREV(wp
, window_panes
, entry
);
758 if (w
->active
== NULL
)
759 w
->active
= TAILQ_NEXT(wp
, entry
);
761 if (w
->active
!= NULL
) {
762 w
->active
->flags
|= PANE_CHANGED
;
763 notify_window("window-pane-changed", w
);
764 window_update_focus(w
);
766 } else if (wp
== w
->last
)
771 window_remove_pane(struct window
*w
, struct window_pane
*wp
)
773 window_lost_pane(w
, wp
);
775 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
776 window_pane_destroy(wp
);
780 window_pane_at_index(struct window
*w
, u_int idx
)
782 struct window_pane
*wp
;
785 n
= options_get_number(w
->options
, "pane-base-index");
786 TAILQ_FOREACH(wp
, &w
->panes
, entry
) {
795 window_pane_next_by_number(struct window
*w
, struct window_pane
*wp
, u_int n
)
798 if ((wp
= TAILQ_NEXT(wp
, entry
)) == NULL
)
799 wp
= TAILQ_FIRST(&w
->panes
);
806 window_pane_previous_by_number(struct window
*w
, struct window_pane
*wp
,
810 if ((wp
= TAILQ_PREV(wp
, window_panes
, entry
)) == NULL
)
811 wp
= TAILQ_LAST(&w
->panes
, window_panes
);
818 window_pane_index(struct window_pane
*wp
, u_int
*i
)
820 struct window_pane
*wq
;
821 struct window
*w
= wp
->window
;
823 *i
= options_get_number(w
->options
, "pane-base-index");
824 TAILQ_FOREACH(wq
, &w
->panes
, entry
) {
835 window_count_panes(struct window
*w
)
837 struct window_pane
*wp
;
841 TAILQ_FOREACH(wp
, &w
->panes
, entry
)
847 window_destroy_panes(struct window
*w
)
849 struct window_pane
*wp
;
851 while (!TAILQ_EMPTY(&w
->panes
)) {
852 wp
= TAILQ_FIRST(&w
->panes
);
853 TAILQ_REMOVE(&w
->panes
, wp
, entry
);
854 window_pane_destroy(wp
);
859 window_printable_flags(struct winlink
*wl
, int escape
)
861 struct session
*s
= wl
->session
;
862 static char flags
[32];
866 if (wl
->flags
& WINLINK_ACTIVITY
) {
871 if (wl
->flags
& WINLINK_BELL
)
873 if (wl
->flags
& WINLINK_SILENCE
)
877 if (wl
== TAILQ_FIRST(&s
->lastw
))
879 if (server_check_marked() && wl
== marked_pane
.wl
)
881 if (wl
->window
->flags
& WINDOW_ZOOMED
)
888 window_pane_find_by_id_str(const char *s
)
896 id
= strtonum(s
+ 1, 0, UINT_MAX
, &errstr
);
899 return (window_pane_find_by_id(id
));
903 window_pane_find_by_id(u_int id
)
905 struct window_pane wp
;
908 return (RB_FIND(window_pane_tree
, &all_window_panes
, &wp
));
911 static struct window_pane
*
912 window_pane_create(struct window
*w
, u_int sx
, u_int sy
, u_int hlimit
)
914 struct window_pane
*wp
;
915 char host
[HOST_NAME_MAX
+ 1];
917 wp
= xcalloc(1, sizeof *wp
);
919 wp
->options
= options_create(w
->options
);
920 wp
->flags
= PANE_STYLECHANGED
;
922 wp
->id
= next_window_pane_id
++;
923 RB_INSERT(window_pane_tree
, &all_window_panes
, wp
);
927 TAILQ_INIT(&wp
->modes
);
929 TAILQ_INIT (&wp
->resize_queue
);
936 colour_palette_init(&wp
->palette
);
937 colour_palette_from_option(&wp
->palette
, wp
->options
);
939 screen_init(&wp
->base
, sx
, sy
, hlimit
);
940 wp
->screen
= &wp
->base
;
942 screen_init(&wp
->status_screen
, 1, 1, 0);
944 if (gethostname(host
, sizeof host
) == 0)
945 screen_set_title(&wp
->base
, host
);
951 window_pane_destroy(struct window_pane
*wp
)
953 struct window_pane_resize
*r
;
954 struct window_pane_resize
*r1
;
956 window_pane_reset_mode_all(wp
);
961 utempter_remove_record(wp
->fd
);
963 bufferevent_free(wp
->event
);
966 if (wp
->ictx
!= NULL
)
967 input_free(wp
->ictx
);
969 screen_free(&wp
->status_screen
);
971 screen_free(&wp
->base
);
973 if (wp
->pipe_fd
!= -1) {
974 bufferevent_free(wp
->pipe_event
);
978 if (event_initialized(&wp
->resize_timer
))
979 event_del(&wp
->resize_timer
);
980 TAILQ_FOREACH_SAFE(r
, &wp
->resize_queue
, entry
, r1
) {
981 TAILQ_REMOVE(&wp
->resize_queue
, r
, entry
);
985 RB_REMOVE(window_pane_tree
, &all_window_panes
, wp
);
987 options_free(wp
->options
);
988 free((void *)wp
->cwd
);
990 cmd_free_argv(wp
->argc
, wp
->argv
);
991 colour_palette_free(&wp
->palette
);
996 window_pane_read_callback(__unused
struct bufferevent
*bufev
, void *data
)
998 struct window_pane
*wp
= data
;
999 struct evbuffer
*evb
= wp
->event
->input
;
1000 struct window_pane_offset
*wpo
= &wp
->pipe_offset
;
1001 size_t size
= EVBUFFER_LENGTH(evb
);
1006 if (wp
->pipe_fd
!= -1) {
1007 new_data
= window_pane_get_new_data(wp
, wpo
, &new_size
);
1009 bufferevent_write(wp
->pipe_event
, new_data
, new_size
);
1010 window_pane_update_used_data(wp
, wpo
, new_size
);
1014 log_debug("%%%u has %zu bytes", wp
->id
, size
);
1015 TAILQ_FOREACH(c
, &clients
, entry
) {
1016 if (c
->session
!= NULL
&& (c
->flags
& CLIENT_CONTROL
))
1017 control_write_output(c
, wp
);
1019 input_parse_pane(wp
);
1020 bufferevent_disable(wp
->event
, EV_READ
);
1024 window_pane_error_callback(__unused
struct bufferevent
*bufev
,
1025 __unused
short what
, void *data
)
1027 struct window_pane
*wp
= data
;
1029 log_debug("%%%u error", wp
->id
);
1030 wp
->flags
|= PANE_EXITED
;
1032 if (window_pane_destroy_ready(wp
))
1033 server_destroy_pane(wp
, 1);
1037 window_pane_set_event(struct window_pane
*wp
)
1039 setblocking(wp
->fd
, 0);
1041 wp
->event
= bufferevent_new(wp
->fd
, window_pane_read_callback
,
1042 NULL
, window_pane_error_callback
, wp
);
1043 wp
->ictx
= input_init(wp
, wp
->event
, &wp
->palette
);
1045 bufferevent_enable(wp
->event
, EV_READ
|EV_WRITE
);
1049 window_pane_resize(struct window_pane
*wp
, u_int sx
, u_int sy
)
1051 struct window_mode_entry
*wme
;
1052 struct window_pane_resize
*r
;
1054 if (sx
== wp
->sx
&& sy
== wp
->sy
)
1057 r
= xmalloc (sizeof *r
);
1062 TAILQ_INSERT_TAIL (&wp
->resize_queue
, r
, entry
);
1067 log_debug("%s: %%%u resize %ux%u", __func__
, wp
->id
, sx
, sy
);
1068 screen_resize(&wp
->base
, sx
, sy
, wp
->base
.saved_grid
== NULL
);
1070 wme
= TAILQ_FIRST(&wp
->modes
);
1071 if (wme
!= NULL
&& wme
->mode
->resize
!= NULL
)
1072 wme
->mode
->resize(wme
, sx
, sy
);
1076 window_pane_set_mode(struct window_pane
*wp
, struct window_pane
*swp
,
1077 const struct window_mode
*mode
, struct cmd_find_state
*fs
,
1080 struct window_mode_entry
*wme
;
1082 if (!TAILQ_EMPTY(&wp
->modes
) && TAILQ_FIRST(&wp
->modes
)->mode
== mode
)
1085 TAILQ_FOREACH(wme
, &wp
->modes
, entry
) {
1086 if (wme
->mode
== mode
)
1090 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1091 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1093 wme
= xcalloc(1, sizeof *wme
);
1098 TAILQ_INSERT_HEAD(&wp
->modes
, wme
, entry
);
1099 wme
->screen
= wme
->mode
->init(wme
, fs
, args
);
1102 wp
->screen
= wme
->screen
;
1103 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1105 server_redraw_window_borders(wp
->window
);
1106 server_status_window(wp
->window
);
1107 notify_pane("pane-mode-changed", wp
);
1113 window_pane_reset_mode(struct window_pane
*wp
)
1115 struct window_mode_entry
*wme
, *next
;
1117 if (TAILQ_EMPTY(&wp
->modes
))
1120 wme
= TAILQ_FIRST(&wp
->modes
);
1121 TAILQ_REMOVE(&wp
->modes
, wme
, entry
);
1122 wme
->mode
->free(wme
);
1125 next
= TAILQ_FIRST(&wp
->modes
);
1127 log_debug("%s: no next mode", __func__
);
1128 wp
->screen
= &wp
->base
;
1130 log_debug("%s: next mode is %s", __func__
, next
->mode
->name
);
1131 wp
->screen
= next
->screen
;
1132 if (next
->mode
->resize
!= NULL
)
1133 next
->mode
->resize(next
, wp
->sx
, wp
->sy
);
1135 wp
->flags
|= (PANE_REDRAW
|PANE_CHANGED
);
1137 server_redraw_window_borders(wp
->window
);
1138 server_status_window(wp
->window
);
1139 notify_pane("pane-mode-changed", wp
);
1143 window_pane_reset_mode_all(struct window_pane
*wp
)
1145 while (!TAILQ_EMPTY(&wp
->modes
))
1146 window_pane_reset_mode(wp
);
1150 window_pane_copy_key(struct window_pane
*wp
, key_code key
)
1152 struct window_pane
*loop
;
1154 TAILQ_FOREACH(loop
, &wp
->window
->panes
, entry
) {
1156 TAILQ_EMPTY(&loop
->modes
) &&
1158 (~loop
->flags
& PANE_INPUTOFF
) &&
1159 window_pane_visible(loop
) &&
1160 options_get_number(loop
->options
, "synchronize-panes"))
1161 input_key_pane(loop
, key
, NULL
);
1166 window_pane_key(struct window_pane
*wp
, struct client
*c
, struct session
*s
,
1167 struct winlink
*wl
, key_code key
, struct mouse_event
*m
)
1169 struct window_mode_entry
*wme
;
1171 if (KEYC_IS_MOUSE(key
) && m
== NULL
)
1174 wme
= TAILQ_FIRST(&wp
->modes
);
1176 if (wme
->mode
->key
!= NULL
&& c
!= NULL
) {
1177 key
&= ~KEYC_MASK_FLAGS
;
1178 wme
->mode
->key(wme
, c
, s
, wl
, key
, m
);
1183 if (wp
->fd
== -1 || wp
->flags
& PANE_INPUTOFF
)
1186 if (input_key_pane(wp
, key
, m
) != 0)
1189 if (KEYC_IS_MOUSE(key
))
1191 if (options_get_number(wp
->options
, "synchronize-panes"))
1192 window_pane_copy_key(wp
, key
);
1197 window_pane_visible(struct window_pane
*wp
)
1199 if (~wp
->window
->flags
& WINDOW_ZOOMED
)
1201 return (wp
== wp
->window
->active
);
1205 window_pane_search(struct window_pane
*wp
, const char *term
, int regex
,
1208 struct screen
*s
= &wp
->base
;
1210 char *new = NULL
, *line
;
1212 int flags
= 0, found
;
1217 flags
|= FNM_CASEFOLD
;
1218 xasprintf(&new, "*%s*", term
);
1222 if (regcomp(&r
, term
, flags
|REG_EXTENDED
) != 0)
1226 for (i
= 0; i
< screen_size_y(s
); i
++) {
1227 line
= grid_view_string_cells(s
->grid
, 0, i
, screen_size_x(s
));
1228 for (n
= strlen(line
); n
> 0; n
--) {
1229 if (!isspace((u_char
)line
[n
- 1]))
1233 log_debug("%s: %s", __func__
, line
);
1235 found
= (fnmatch(new, line
, flags
) == 0);
1237 found
= (regexec(&r
, line
, 0, NULL
, 0) == 0);
1247 if (i
== screen_size_y(s
))
1252 /* Get MRU pane from a list. */
1253 static struct window_pane
*
1254 window_pane_choose_best(struct window_pane
**list
, u_int size
)
1256 struct window_pane
*next
, *best
;
1263 for (i
= 1; i
< size
; i
++) {
1265 if (next
->active_point
> best
->active_point
)
1272 * Find the pane directly above another. We build a list of those adjacent to
1273 * top edge and then choose the best.
1275 struct window_pane
*
1276 window_pane_find_up(struct window_pane
*wp
)
1279 struct window_pane
*next
, *best
, **list
;
1280 u_int edge
, left
, right
, end
, size
;
1286 status
= options_get_number(w
->options
, "pane-border-status");
1292 if (status
== PANE_STATUS_TOP
) {
1295 } else if (status
== PANE_STATUS_BOTTOM
) {
1304 right
= wp
->xoff
+ wp
->sx
;
1306 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1309 if (next
->yoff
+ next
->sy
+ 1 != edge
)
1311 end
= next
->xoff
+ next
->sx
- 1;
1314 if (next
->xoff
< left
&& end
> right
)
1316 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1318 else if (end
>= left
&& end
<= right
)
1322 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1323 list
[size
++] = next
;
1326 best
= window_pane_choose_best(list
, size
);
1331 /* Find the pane directly below another. */
1332 struct window_pane
*
1333 window_pane_find_down(struct window_pane
*wp
)
1336 struct window_pane
*next
, *best
, **list
;
1337 u_int edge
, left
, right
, end
, size
;
1343 status
= options_get_number(w
->options
, "pane-border-status");
1348 edge
= wp
->yoff
+ wp
->sy
+ 1;
1349 if (status
== PANE_STATUS_TOP
) {
1352 } else if (status
== PANE_STATUS_BOTTOM
) {
1353 if (edge
>= w
->sy
- 1)
1361 right
= wp
->xoff
+ wp
->sx
;
1363 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1366 if (next
->yoff
!= edge
)
1368 end
= next
->xoff
+ next
->sx
- 1;
1371 if (next
->xoff
< left
&& end
> right
)
1373 else if (next
->xoff
>= left
&& next
->xoff
<= right
)
1375 else if (end
>= left
&& end
<= right
)
1379 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1380 list
[size
++] = next
;
1383 best
= window_pane_choose_best(list
, size
);
1388 /* Find the pane directly to the left of another. */
1389 struct window_pane
*
1390 window_pane_find_left(struct window_pane
*wp
)
1393 struct window_pane
*next
, *best
, **list
;
1394 u_int edge
, top
, bottom
, end
, size
;
1409 bottom
= wp
->yoff
+ wp
->sy
;
1411 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1414 if (next
->xoff
+ next
->sx
+ 1 != edge
)
1416 end
= next
->yoff
+ next
->sy
- 1;
1419 if (next
->yoff
< top
&& end
> bottom
)
1421 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1423 else if (end
>= top
&& end
<= bottom
)
1427 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1428 list
[size
++] = next
;
1431 best
= window_pane_choose_best(list
, size
);
1436 /* Find the pane directly to the right of another. */
1437 struct window_pane
*
1438 window_pane_find_right(struct window_pane
*wp
)
1441 struct window_pane
*next
, *best
, **list
;
1442 u_int edge
, top
, bottom
, end
, size
;
1452 edge
= wp
->xoff
+ wp
->sx
+ 1;
1457 bottom
= wp
->yoff
+ wp
->sy
;
1459 TAILQ_FOREACH(next
, &w
->panes
, entry
) {
1462 if (next
->xoff
!= edge
)
1464 end
= next
->yoff
+ next
->sy
- 1;
1467 if (next
->yoff
< top
&& end
> bottom
)
1469 else if (next
->yoff
>= top
&& next
->yoff
<= bottom
)
1471 else if (end
>= top
&& end
<= bottom
)
1475 list
= xreallocarray(list
, size
+ 1, sizeof *list
);
1476 list
[size
++] = next
;
1479 best
= window_pane_choose_best(list
, size
);
1484 /* Clear alert flags for a winlink */
1486 winlink_clear_flags(struct winlink
*wl
)
1488 struct winlink
*loop
;
1490 wl
->window
->flags
&= ~WINDOW_ALERTFLAGS
;
1491 TAILQ_FOREACH(loop
, &wl
->window
->winlinks
, wentry
) {
1492 if ((loop
->flags
& WINLINK_ALERTFLAGS
) != 0) {
1493 loop
->flags
&= ~WINLINK_ALERTFLAGS
;
1494 server_status_session(loop
->session
);
1499 /* Shuffle window indexes up. */
1501 winlink_shuffle_up(struct session
*s
, struct winlink
*wl
, int before
)
1512 /* Find the next free index. */
1513 for (last
= idx
; last
< INT_MAX
; last
++) {
1514 if (winlink_find_by_index(&s
->windows
, last
) == NULL
)
1517 if (last
== INT_MAX
)
1520 /* Move everything from last - 1 to idx up a bit. */
1521 for (; last
> idx
; last
--) {
1522 wl
= winlink_find_by_index(&s
->windows
, last
- 1);
1523 RB_REMOVE(winlinks
, &s
->windows
, wl
);
1525 RB_INSERT(winlinks
, &s
->windows
, wl
);
1532 window_pane_input_callback(struct client
*c
, __unused
const char *path
,
1533 int error
, int closed
, struct evbuffer
*buffer
, void *data
)
1535 struct window_pane_input_data
*cdata
= data
;
1536 struct window_pane
*wp
;
1537 u_char
*buf
= EVBUFFER_DATA(buffer
);
1538 size_t len
= EVBUFFER_LENGTH(buffer
);
1540 wp
= window_pane_find_by_id(cdata
->wp
);
1541 if (wp
== NULL
|| closed
|| error
!= 0 || (c
->flags
& CLIENT_DEAD
)) {
1543 c
->flags
|= CLIENT_EXIT
;
1545 evbuffer_drain(buffer
, len
);
1546 cmdq_continue(cdata
->item
);
1548 server_client_unref(c
);
1552 input_parse_buffer(wp
, buf
, len
);
1553 evbuffer_drain(buffer
, len
);
1557 window_pane_start_input(struct window_pane
*wp
, struct cmdq_item
*item
,
1560 struct client
*c
= cmdq_get_client(item
);
1561 struct window_pane_input_data
*cdata
;
1563 if (~wp
->flags
& PANE_EMPTY
) {
1564 *cause
= xstrdup("pane is not empty");
1567 if (c
->flags
& (CLIENT_DEAD
|CLIENT_EXITED
))
1569 if (c
->session
!= NULL
)
1572 cdata
= xmalloc(sizeof *cdata
);
1577 file_read(c
, "-", window_pane_input_callback
, cdata
);
1583 window_pane_get_new_data(struct window_pane
*wp
,
1584 struct window_pane_offset
*wpo
, size_t *size
)
1586 size_t used
= wpo
->used
- wp
->base_offset
;
1588 *size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;
1589 return (EVBUFFER_DATA(wp
->event
->input
) + used
);
1593 window_pane_update_used_data(struct window_pane
*wp
,
1594 struct window_pane_offset
*wpo
, size_t size
)
1596 size_t used
= wpo
->used
- wp
->base_offset
;
1598 if (size
> EVBUFFER_LENGTH(wp
->event
->input
) - used
)
1599 size
= EVBUFFER_LENGTH(wp
->event
->input
) - used
;