xref: /openbsd-src/usr.bin/tmux/window.c (revision 4b70baf6e17fc8b27fc1f7fa7929335753fa94c3)
1 /* $OpenBSD: window.c,v 1.228 2019/05/03 20:44:24 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
5  *
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.
9  *
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.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <fnmatch.h>
25 #include <signal.h>
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <util.h>
32 #include <vis.h>
33 
34 #include "tmux.h"
35 
36 /*
37  * Each window is attached to a number of panes, each of which is a pty. This
38  * file contains code to handle them.
39  *
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.
45  *
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.
48  *
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
52  * it reaches zero.
53  */
54 
55 /* Global window list. */
56 struct windows windows;
57 
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;
63 
64 /* List of window modes. */
65 const struct window_mode *all_window_modes[] = {
66 	&window_buffer_mode,
67 	&window_client_mode,
68 	&window_clock_mode,
69 	&window_copy_mode,
70 	&window_tree_mode,
71 	&window_view_mode,
72 	NULL
73 };
74 
75 struct window_pane_input_data {
76 	struct cmdq_item	*item;
77 	u_int			 wp;
78 };
79 
80 static struct window_pane *window_pane_create(struct window *, u_int, u_int,
81 		    u_int);
82 static void	window_pane_destroy(struct window_pane *);
83 
84 RB_GENERATE(windows, window, entry, window_cmp);
85 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
86 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
87 
88 int
89 window_cmp(struct window *w1, struct window *w2)
90 {
91 	return (w1->id - w2->id);
92 }
93 
94 int
95 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
96 {
97 	return (wl1->idx - wl2->idx);
98 }
99 
100 int
101 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
102 {
103 	return (wp1->id - wp2->id);
104 }
105 
106 struct winlink *
107 winlink_find_by_window(struct winlinks *wwl, struct window *w)
108 {
109 	struct winlink	*wl;
110 
111 	RB_FOREACH(wl, winlinks, wwl) {
112 		if (wl->window == w)
113 			return (wl);
114 	}
115 
116 	return (NULL);
117 }
118 
119 struct winlink *
120 winlink_find_by_index(struct winlinks *wwl, int idx)
121 {
122 	struct winlink	wl;
123 
124 	if (idx < 0)
125 		fatalx("bad index");
126 
127 	wl.idx = idx;
128 	return (RB_FIND(winlinks, wwl, &wl));
129 }
130 
131 struct winlink *
132 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
133 {
134 	struct winlink *wl;
135 
136 	RB_FOREACH(wl, winlinks, wwl) {
137 		if (wl->window->id == id)
138 			return (wl);
139 	}
140 	return (NULL);
141 }
142 
143 static int
144 winlink_next_index(struct winlinks *wwl, int idx)
145 {
146 	int	i;
147 
148 	i = idx;
149 	do {
150 		if (winlink_find_by_index(wwl, i) == NULL)
151 			return (i);
152 		if (i == INT_MAX)
153 			i = 0;
154 		else
155 			i++;
156 	} while (i != idx);
157 	return (-1);
158 }
159 
160 u_int
161 winlink_count(struct winlinks *wwl)
162 {
163 	struct winlink	*wl;
164 	u_int		 n;
165 
166 	n = 0;
167 	RB_FOREACH(wl, winlinks, wwl)
168 		n++;
169 
170 	return (n);
171 }
172 
173 struct winlink *
174 winlink_add(struct winlinks *wwl, int idx)
175 {
176 	struct winlink	*wl;
177 
178 	if (idx < 0) {
179 		if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
180 			return (NULL);
181 	} else if (winlink_find_by_index(wwl, idx) != NULL)
182 		return (NULL);
183 
184 	wl = xcalloc(1, sizeof *wl);
185 	wl->idx = idx;
186 	RB_INSERT(winlinks, wwl, wl);
187 
188 	return (wl);
189 }
190 
191 void
192 winlink_set_window(struct winlink *wl, struct window *w)
193 {
194 	if (wl->window != NULL) {
195 		TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
196 		window_remove_ref(wl->window, __func__);
197 	}
198 	TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
199 	wl->window = w;
200 	window_add_ref(w, __func__);
201 }
202 
203 void
204 winlink_remove(struct winlinks *wwl, struct winlink *wl)
205 {
206 	struct window	*w = wl->window;
207 
208 	if (w != NULL) {
209 		TAILQ_REMOVE(&w->winlinks, wl, wentry);
210 		window_remove_ref(w, __func__);
211 	}
212 
213 	RB_REMOVE(winlinks, wwl, wl);
214 	free(wl);
215 }
216 
217 struct winlink *
218 winlink_next(struct winlink *wl)
219 {
220 	return (RB_NEXT(winlinks, wwl, wl));
221 }
222 
223 struct winlink *
224 winlink_previous(struct winlink *wl)
225 {
226 	return (RB_PREV(winlinks, wwl, wl));
227 }
228 
229 struct winlink *
230 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
231 {
232 	for (; n > 0; n--) {
233 		if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
234 			wl = RB_MIN(winlinks, &s->windows);
235 	}
236 
237 	return (wl);
238 }
239 
240 struct winlink *
241 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
242 {
243 	for (; n > 0; n--) {
244 		if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
245 			wl = RB_MAX(winlinks, &s->windows);
246 	}
247 
248 	return (wl);
249 }
250 
251 void
252 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
253 {
254 	if (wl == NULL)
255 		return;
256 
257 	winlink_stack_remove(stack, wl);
258 	TAILQ_INSERT_HEAD(stack, wl, sentry);
259 }
260 
261 void
262 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
263 {
264 	struct winlink	*wl2;
265 
266 	if (wl == NULL)
267 		return;
268 
269 	TAILQ_FOREACH(wl2, stack, sentry) {
270 		if (wl2 == wl) {
271 			TAILQ_REMOVE(stack, wl, sentry);
272 			return;
273 		}
274 	}
275 }
276 
277 struct window *
278 window_find_by_id_str(const char *s)
279 {
280 	const char	*errstr;
281 	u_int		 id;
282 
283 	if (*s != '@')
284 		return (NULL);
285 
286 	id = strtonum(s + 1, 0, UINT_MAX, &errstr);
287 	if (errstr != NULL)
288 		return (NULL);
289 	return (window_find_by_id(id));
290 }
291 
292 struct window *
293 window_find_by_id(u_int id)
294 {
295 	struct window	w;
296 
297 	w.id = id;
298 	return (RB_FIND(windows, &windows, &w));
299 }
300 
301 void
302 window_update_activity(struct window *w)
303 {
304 	gettimeofday(&w->activity_time, NULL);
305 	alerts_queue(w, WINDOW_ACTIVITY);
306 }
307 
308 struct window *
309 window_create(u_int sx, u_int sy)
310 {
311 	struct window	*w;
312 
313 	w = xcalloc(1, sizeof *w);
314 	w->name = NULL;
315 	w->flags = WINDOW_STYLECHANGED;
316 
317 	TAILQ_INIT(&w->panes);
318 	w->active = NULL;
319 
320 	w->lastlayout = -1;
321 	w->layout_root = NULL;
322 
323 	w->sx = sx;
324 	w->sy = sy;
325 
326 	w->options = options_create(global_w_options);
327 
328 	w->references = 0;
329 	TAILQ_INIT(&w->winlinks);
330 
331 	w->id = next_window_id++;
332 	RB_INSERT(windows, &windows, w);
333 
334 	window_update_activity(w);
335 
336 	return (w);
337 }
338 
339 void
340 window_destroy(struct window *w)
341 {
342 	log_debug("window @%u destroyed (%d references)", w->id, w->references);
343 
344 	RB_REMOVE(windows, &windows, w);
345 
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);
350 	free(w->old_layout);
351 
352 	window_destroy_panes(w);
353 
354 	if (event_initialized(&w->name_event))
355 		evtimer_del(&w->name_event);
356 
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);
361 
362 	options_free(w->options);
363 
364 	free(w->name);
365 	free(w);
366 }
367 
368 int
369 window_pane_destroy_ready(struct window_pane *wp)
370 {
371 	int	n;
372 
373 	if (wp->pipe_fd != -1) {
374 		if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
375 			return (0);
376 		if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0)
377 			return (0);
378 	}
379 
380 	if (~wp->flags & PANE_EXITED)
381 		return (0);
382 	return (1);
383 }
384 
385 void
386 window_add_ref(struct window *w, const char *from)
387 {
388 	w->references++;
389 	log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
390 }
391 
392 void
393 window_remove_ref(struct window *w, const char *from)
394 {
395 	w->references--;
396 	log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
397 
398 	if (w->references == 0)
399 		window_destroy(w);
400 }
401 
402 void
403 window_set_name(struct window *w, const char *new_name)
404 {
405 	free(w->name);
406 	utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
407 	notify_window("window-renamed", w);
408 }
409 
410 void
411 window_resize(struct window *w, u_int sx, u_int sy)
412 {
413 	w->sx = sx;
414 	w->sy = sy;
415 }
416 
417 int
418 window_has_pane(struct window *w, struct window_pane *wp)
419 {
420 	struct window_pane	*wp1;
421 
422 	TAILQ_FOREACH(wp1, &w->panes, entry) {
423 		if (wp1 == wp)
424 			return (1);
425 	}
426 	return (0);
427 }
428 
429 int
430 window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
431 {
432 	log_debug("%s: pane %%%u", __func__, wp->id);
433 
434 	if (wp == w->active)
435 		return (0);
436 	w->last = w->active;
437 
438 	w->active = wp;
439 	w->active->active_point = next_active_point++;
440 	w->active->flags |= PANE_CHANGED;
441 
442 	tty_update_window_offset(w);
443 
444 	if (notify)
445 		notify_window("window-pane-changed", w);
446 	return (1);
447 }
448 
449 void
450 window_redraw_active_switch(struct window *w, struct window_pane *wp)
451 {
452 	struct style	*sy;
453 
454 	if (wp == w->active)
455 		return;
456 
457 	/*
458 	 * If window-style and window-active-style are the same, we don't need
459 	 * to redraw panes when switching active panes.
460 	 */
461 	sy = options_get_style(w->options, "window-active-style");
462 	if (style_equal(sy, options_get_style(w->options, "window-style")))
463 		return;
464 
465 	/*
466 	 * If the now active or inactive pane do not have a custom style or if
467 	 * the palette is different, they need to be redrawn.
468 	 */
469 	if (window_pane_get_palette(w->active, w->active->style.gc.fg) != -1 ||
470 	    window_pane_get_palette(w->active, w->active->style.gc.bg) != -1 ||
471 	    style_is_default(&w->active->style))
472 		w->active->flags |= PANE_REDRAW;
473 	if (window_pane_get_palette(wp, wp->style.gc.fg) != -1 ||
474 	    window_pane_get_palette(wp, wp->style.gc.bg) != -1 ||
475 	    style_is_default(&wp->style))
476 		wp->flags |= PANE_REDRAW;
477 }
478 
479 struct window_pane *
480 window_get_active_at(struct window *w, u_int x, u_int y)
481 {
482 	struct window_pane	*wp;
483 
484 	TAILQ_FOREACH(wp, &w->panes, entry) {
485 		if (!window_pane_visible(wp))
486 			continue;
487 		if (x < wp->xoff || x > wp->xoff + wp->sx)
488 			continue;
489 		if (y < wp->yoff || y > wp->yoff + wp->sy)
490 			continue;
491 		return (wp);
492 	}
493 	return (NULL);
494 }
495 
496 struct window_pane *
497 window_find_string(struct window *w, const char *s)
498 {
499 	u_int	x, y;
500 
501 	x = w->sx / 2;
502 	y = w->sy / 2;
503 
504 	if (strcasecmp(s, "top") == 0)
505 		y = 0;
506 	else if (strcasecmp(s, "bottom") == 0)
507 		y = w->sy - 1;
508 	else if (strcasecmp(s, "left") == 0)
509 		x = 0;
510 	else if (strcasecmp(s, "right") == 0)
511 		x = w->sx - 1;
512 	else if (strcasecmp(s, "top-left") == 0) {
513 		x = 0;
514 		y = 0;
515 	} else if (strcasecmp(s, "top-right") == 0) {
516 		x = w->sx - 1;
517 		y = 0;
518 	} else if (strcasecmp(s, "bottom-left") == 0) {
519 		x = 0;
520 		y = w->sy - 1;
521 	} else if (strcasecmp(s, "bottom-right") == 0) {
522 		x = w->sx - 1;
523 		y = w->sy - 1;
524 	} else
525 		return (NULL);
526 
527 	return (window_get_active_at(w, x, y));
528 }
529 
530 int
531 window_zoom(struct window_pane *wp)
532 {
533 	struct window		*w = wp->window;
534 	struct window_pane	*wp1;
535 
536 	if (w->flags & WINDOW_ZOOMED)
537 		return (-1);
538 
539 	if (window_count_panes(w) == 1)
540 		return (-1);
541 
542 	if (w->active != wp)
543 		window_set_active_pane(w, wp, 1);
544 
545 	TAILQ_FOREACH(wp1, &w->panes, entry) {
546 		wp1->saved_layout_cell = wp1->layout_cell;
547 		wp1->layout_cell = NULL;
548 	}
549 
550 	w->saved_layout_root = w->layout_root;
551 	layout_init(w, wp);
552 	w->flags |= WINDOW_ZOOMED;
553 	notify_window("window-layout-changed", w);
554 
555 	return (0);
556 }
557 
558 int
559 window_unzoom(struct window *w)
560 {
561 	struct window_pane	*wp;
562 
563 	if (!(w->flags & WINDOW_ZOOMED))
564 		return (-1);
565 
566 	w->flags &= ~WINDOW_ZOOMED;
567 	layout_free(w);
568 	w->layout_root = w->saved_layout_root;
569 	w->saved_layout_root = NULL;
570 
571 	TAILQ_FOREACH(wp, &w->panes, entry) {
572 		wp->layout_cell = wp->saved_layout_cell;
573 		wp->saved_layout_cell = NULL;
574 	}
575 	layout_fix_panes(w);
576 	notify_window("window-layout-changed", w);
577 
578 	return (0);
579 }
580 
581 struct window_pane *
582 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
583     int flags)
584 {
585 	struct window_pane	*wp;
586 
587 	if (other == NULL)
588 		other = w->active;
589 
590 	wp = window_pane_create(w, w->sx, w->sy, hlimit);
591 	if (TAILQ_EMPTY(&w->panes)) {
592 		log_debug("%s: @%u at start", __func__, w->id);
593 		TAILQ_INSERT_HEAD(&w->panes, wp, entry);
594 	} else if (flags & SPAWN_BEFORE) {
595 		log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
596 		if (flags & SPAWN_FULLSIZE)
597 			TAILQ_INSERT_HEAD(&w->panes, wp, entry);
598 		else
599 			TAILQ_INSERT_BEFORE(other, wp, entry);
600 	} else {
601 		log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
602 		if (flags & SPAWN_FULLSIZE)
603 			TAILQ_INSERT_TAIL(&w->panes, wp, entry);
604 		else
605 			TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
606 	}
607 	return (wp);
608 }
609 
610 void
611 window_lost_pane(struct window *w, struct window_pane *wp)
612 {
613 	log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
614 
615 	if (wp == marked_pane.wp)
616 		server_clear_marked();
617 
618 	if (wp == w->active) {
619 		w->active = w->last;
620 		w->last = NULL;
621 		if (w->active == NULL) {
622 			w->active = TAILQ_PREV(wp, window_panes, entry);
623 			if (w->active == NULL)
624 				w->active = TAILQ_NEXT(wp, entry);
625 		}
626 		if (w->active != NULL) {
627 			w->active->flags |= PANE_CHANGED;
628 			notify_window("window-pane-changed", w);
629 		}
630 	} else if (wp == w->last)
631 		w->last = NULL;
632 }
633 
634 void
635 window_remove_pane(struct window *w, struct window_pane *wp)
636 {
637 	window_lost_pane(w, wp);
638 
639 	TAILQ_REMOVE(&w->panes, wp, entry);
640 	window_pane_destroy(wp);
641 }
642 
643 struct window_pane *
644 window_pane_at_index(struct window *w, u_int idx)
645 {
646 	struct window_pane	*wp;
647 	u_int			 n;
648 
649 	n = options_get_number(w->options, "pane-base-index");
650 	TAILQ_FOREACH(wp, &w->panes, entry) {
651 		if (n == idx)
652 			return (wp);
653 		n++;
654 	}
655 	return (NULL);
656 }
657 
658 struct window_pane *
659 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
660 {
661 	for (; n > 0; n--) {
662 		if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
663 			wp = TAILQ_FIRST(&w->panes);
664 	}
665 
666 	return (wp);
667 }
668 
669 struct window_pane *
670 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
671     u_int n)
672 {
673 	for (; n > 0; n--) {
674 		if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
675 			wp = TAILQ_LAST(&w->panes, window_panes);
676 	}
677 
678 	return (wp);
679 }
680 
681 int
682 window_pane_index(struct window_pane *wp, u_int *i)
683 {
684 	struct window_pane	*wq;
685 	struct window		*w = wp->window;
686 
687 	*i = options_get_number(w->options, "pane-base-index");
688 	TAILQ_FOREACH(wq, &w->panes, entry) {
689 		if (wp == wq) {
690 			return (0);
691 		}
692 		(*i)++;
693 	}
694 
695 	return (-1);
696 }
697 
698 u_int
699 window_count_panes(struct window *w)
700 {
701 	struct window_pane	*wp;
702 	u_int			 n;
703 
704 	n = 0;
705 	TAILQ_FOREACH(wp, &w->panes, entry)
706 		n++;
707 	return (n);
708 }
709 
710 void
711 window_destroy_panes(struct window *w)
712 {
713 	struct window_pane	*wp;
714 
715 	while (!TAILQ_EMPTY(&w->panes)) {
716 		wp = TAILQ_FIRST(&w->panes);
717 		TAILQ_REMOVE(&w->panes, wp, entry);
718 		window_pane_destroy(wp);
719 	}
720 }
721 
722 const char *
723 window_printable_flags(struct winlink *wl)
724 {
725 	struct session	*s = wl->session;
726 	static char	 flags[32];
727 	int		 pos;
728 
729 	pos = 0;
730 	if (wl->flags & WINLINK_ACTIVITY)
731 		flags[pos++] = '#';
732 	if (wl->flags & WINLINK_BELL)
733 		flags[pos++] = '!';
734 	if (wl->flags & WINLINK_SILENCE)
735 		flags[pos++] = '~';
736 	if (wl == s->curw)
737 		flags[pos++] = '*';
738 	if (wl == TAILQ_FIRST(&s->lastw))
739 		flags[pos++] = '-';
740 	if (server_check_marked() && wl == marked_pane.wl)
741 		flags[pos++] = 'M';
742 	if (wl->window->flags & WINDOW_ZOOMED)
743 		flags[pos++] = 'Z';
744 	flags[pos] = '\0';
745 	return (flags);
746 }
747 
748 struct window_pane *
749 window_pane_find_by_id_str(const char *s)
750 {
751 	const char	*errstr;
752 	u_int		 id;
753 
754 	if (*s != '%')
755 		return (NULL);
756 
757 	id = strtonum(s + 1, 0, UINT_MAX, &errstr);
758 	if (errstr != NULL)
759 		return (NULL);
760 	return (window_pane_find_by_id(id));
761 }
762 
763 struct window_pane *
764 window_pane_find_by_id(u_int id)
765 {
766 	struct window_pane	wp;
767 
768 	wp.id = id;
769 	return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
770 }
771 
772 static struct window_pane *
773 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
774 {
775 	struct window_pane	*wp;
776 	char			 host[HOST_NAME_MAX + 1];
777 
778 	wp = xcalloc(1, sizeof *wp);
779 	wp->window = w;
780 
781 	wp->id = next_window_pane_id++;
782 	RB_INSERT(window_pane_tree, &all_window_panes, wp);
783 
784 	wp->argc = 0;
785 	wp->argv = NULL;
786 	wp->shell = NULL;
787 	wp->cwd = NULL;
788 
789 	wp->fd = -1;
790 	wp->event = NULL;
791 
792 	TAILQ_INIT(&wp->modes);
793 
794 	wp->layout_cell = NULL;
795 
796 	wp->xoff = 0;
797 	wp->yoff = 0;
798 
799 	wp->sx = wp->osx = sx;
800 	wp->sy = wp->osx = sy;
801 
802 	wp->pipe_fd = -1;
803 	wp->pipe_off = 0;
804 	wp->pipe_event = NULL;
805 
806 	wp->saved_grid = NULL;
807 
808 	style_set(&wp->style, &grid_default_cell);
809 
810 	screen_init(&wp->base, sx, sy, hlimit);
811 	wp->screen = &wp->base;
812 
813 	screen_init(&wp->status_screen, 1, 1, 0);
814 
815 	if (gethostname(host, sizeof host) == 0)
816 		screen_set_title(&wp->base, host);
817 
818 	input_init(wp);
819 
820 	return (wp);
821 }
822 
823 static void
824 window_pane_destroy(struct window_pane *wp)
825 {
826 	window_pane_reset_mode_all(wp);
827 	free(wp->searchstr);
828 
829 	if (wp->fd != -1) {
830 		bufferevent_free(wp->event);
831 		close(wp->fd);
832 	}
833 
834 	input_free(wp);
835 
836 	screen_free(&wp->status_screen);
837 
838 	screen_free(&wp->base);
839 	if (wp->saved_grid != NULL)
840 		grid_destroy(wp->saved_grid);
841 
842 	if (wp->pipe_fd != -1) {
843 		bufferevent_free(wp->pipe_event);
844 		close(wp->pipe_fd);
845 	}
846 
847 	if (event_initialized(&wp->resize_timer))
848 		event_del(&wp->resize_timer);
849 
850 	RB_REMOVE(window_pane_tree, &all_window_panes, wp);
851 
852 	free((void *)wp->cwd);
853 	free(wp->shell);
854 	cmd_free_argv(wp->argc, wp->argv);
855 	free(wp->palette);
856 	free(wp);
857 }
858 
859 static void
860 window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
861 {
862 	struct window_pane	*wp = data;
863 	struct evbuffer		*evb = wp->event->input;
864 	size_t			 size = EVBUFFER_LENGTH(evb);
865 	char			*new_data;
866 	size_t			 new_size;
867 
868 	new_size = size - wp->pipe_off;
869 	if (wp->pipe_fd != -1 && new_size > 0) {
870 		new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
871 		bufferevent_write(wp->pipe_event, new_data, new_size);
872 	}
873 
874 	log_debug("%%%u has %zu bytes", wp->id, size);
875 	input_parse(wp);
876 
877 	wp->pipe_off = EVBUFFER_LENGTH(evb);
878 }
879 
880 static void
881 window_pane_error_callback(__unused struct bufferevent *bufev,
882     __unused short what, void *data)
883 {
884 	struct window_pane *wp = data;
885 
886 	log_debug("%%%u error", wp->id);
887 	wp->flags |= PANE_EXITED;
888 
889 	if (window_pane_destroy_ready(wp))
890 		server_destroy_pane(wp, 1);
891 }
892 
893 void
894 window_pane_set_event(struct window_pane *wp)
895 {
896 	setblocking(wp->fd, 0);
897 
898 	wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
899 	    NULL, window_pane_error_callback, wp);
900 
901 	bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
902 	bufferevent_enable(wp->event, EV_READ|EV_WRITE);
903 }
904 
905 void
906 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
907 {
908 	struct window_mode_entry	*wme;
909 
910 	if (sx == wp->sx && sy == wp->sy)
911 		return;
912 	wp->sx = sx;
913 	wp->sy = sy;
914 
915 	screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
916 
917 	wme = TAILQ_FIRST(&wp->modes);
918 	if (wme != NULL && wme->mode->resize != NULL)
919 		wme->mode->resize(wme, sx, sy);
920 
921 	wp->flags |= PANE_RESIZE;
922 }
923 
924 /*
925  * Enter alternative screen mode. A copy of the visible screen is saved and the
926  * history is not updated
927  */
928 void
929 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
930     int cursor)
931 {
932 	struct screen	*s = &wp->base;
933 	u_int		 sx, sy;
934 
935 	if (wp->saved_grid != NULL)
936 		return;
937 	if (!options_get_number(wp->window->options, "alternate-screen"))
938 		return;
939 	sx = screen_size_x(s);
940 	sy = screen_size_y(s);
941 
942 	wp->saved_grid = grid_create(sx, sy, 0);
943 	grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
944 	if (cursor) {
945 		wp->saved_cx = s->cx;
946 		wp->saved_cy = s->cy;
947 	}
948 	memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
949 
950 	grid_view_clear(s->grid, 0, 0, sx, sy, 8);
951 
952 	wp->base.grid->flags &= ~GRID_HISTORY;
953 
954 	wp->flags |= PANE_REDRAW;
955 }
956 
957 /* Exit alternate screen mode and restore the copied grid. */
958 void
959 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
960     int cursor)
961 {
962 	struct screen	*s = &wp->base;
963 	u_int		 sx, sy;
964 
965 	if (wp->saved_grid == NULL)
966 		return;
967 	if (!options_get_number(wp->window->options, "alternate-screen"))
968 		return;
969 	sx = screen_size_x(s);
970 	sy = screen_size_y(s);
971 
972 	/*
973 	 * If the current size is bigger, temporarily resize to the old size
974 	 * before copying back.
975 	 */
976 	if (sy > wp->saved_grid->sy)
977 		screen_resize(s, sx, wp->saved_grid->sy, 1);
978 
979 	/* Restore the grid, cursor position and cell. */
980 	grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
981 	if (cursor)
982 		s->cx = wp->saved_cx;
983 	if (s->cx > screen_size_x(s) - 1)
984 		s->cx = screen_size_x(s) - 1;
985 	if (cursor)
986 		s->cy = wp->saved_cy;
987 	if (s->cy > screen_size_y(s) - 1)
988 		s->cy = screen_size_y(s) - 1;
989 	memcpy(gc, &wp->saved_cell, sizeof *gc);
990 
991 	/*
992 	 * Turn history back on (so resize can use it) and then resize back to
993 	 * the current size.
994 	 */
995 	wp->base.grid->flags |= GRID_HISTORY;
996 	if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
997 		screen_resize(s, sx, sy, 1);
998 
999 	grid_destroy(wp->saved_grid);
1000 	wp->saved_grid = NULL;
1001 
1002 	wp->flags |= PANE_REDRAW;
1003 }
1004 
1005 void
1006 window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
1007 {
1008 	if (n > 0xff)
1009 		return;
1010 
1011 	if (wp->palette == NULL)
1012 		wp->palette = xcalloc(0x100, sizeof *wp->palette);
1013 
1014 	wp->palette[n] = colour;
1015 	wp->flags |= PANE_REDRAW;
1016 }
1017 
1018 void
1019 window_pane_unset_palette(struct window_pane *wp, u_int n)
1020 {
1021 	if (n > 0xff || wp->palette == NULL)
1022 		return;
1023 
1024 	wp->palette[n] = 0;
1025 	wp->flags |= PANE_REDRAW;
1026 }
1027 
1028 void
1029 window_pane_reset_palette(struct window_pane *wp)
1030 {
1031 	if (wp->palette == NULL)
1032 		return;
1033 
1034 	free(wp->palette);
1035 	wp->palette = NULL;
1036 	wp->flags |= PANE_REDRAW;
1037 }
1038 
1039 int
1040 window_pane_get_palette(struct window_pane *wp, int c)
1041 {
1042 	int	new;
1043 
1044 	if (wp == NULL || wp->palette == NULL)
1045 		return (-1);
1046 
1047 	new = -1;
1048 	if (c < 8)
1049 		new = wp->palette[c];
1050 	else if (c >= 90 && c <= 97)
1051 		new = wp->palette[8 + c - 90];
1052 	else if (c & COLOUR_FLAG_256)
1053 		new = wp->palette[c & ~COLOUR_FLAG_256];
1054 	if (new == 0)
1055 		return (-1);
1056 	return (new);
1057 }
1058 
1059 static void
1060 window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
1061 {
1062 	struct window_pane	*wp = arg;
1063 	struct timeval		 tv = { .tv_sec = 10 };
1064 	int			 n = 0;
1065 
1066 	evtimer_del(&wp->modetimer);
1067 	evtimer_add(&wp->modetimer, &tv);
1068 
1069 	log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
1070 
1071 	if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
1072 		if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
1073 			window_pane_reset_mode_all(wp);
1074 	}
1075 }
1076 
1077 int
1078 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode,
1079     struct cmd_find_state *fs, struct args *args)
1080 {
1081 	struct timeval			 tv = { .tv_sec = 10 };
1082 	struct window_mode_entry	*wme;
1083 
1084 	if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1085 		return (1);
1086 
1087 	wp->modelast = time(NULL);
1088 	if (TAILQ_EMPTY(&wp->modes)) {
1089 		evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
1090 		evtimer_add(&wp->modetimer, &tv);
1091 	}
1092 
1093 	TAILQ_FOREACH(wme, &wp->modes, entry) {
1094 		if (wme->mode == mode)
1095 			break;
1096 	}
1097 	if (wme != NULL) {
1098 		TAILQ_REMOVE(&wp->modes, wme, entry);
1099 		TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1100 	} else {
1101 		wme = xcalloc(1, sizeof *wme);
1102 		wme->wp = wp;
1103 		wme->mode = mode;
1104 		wme->prefix = 1;
1105 		TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1106 		wme->screen = wme->mode->init(wme, fs, args);
1107 	}
1108 
1109 	wp->screen = wme->screen;
1110 	wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1111 
1112 	server_status_window(wp->window);
1113 	notify_pane("pane-mode-changed", wp);
1114 
1115 	return (0);
1116 }
1117 
1118 void
1119 window_pane_reset_mode(struct window_pane *wp)
1120 {
1121 	struct window_mode_entry	*wme, *next;
1122 
1123 	if (TAILQ_EMPTY(&wp->modes))
1124 		return;
1125 
1126 	wme = TAILQ_FIRST(&wp->modes);
1127 	TAILQ_REMOVE(&wp->modes, wme, entry);
1128 	wme->mode->free(wme);
1129 	free(wme);
1130 
1131 	next = TAILQ_FIRST(&wp->modes);
1132 	if (next == NULL) {
1133 		log_debug("%s: no next mode", __func__);
1134 		evtimer_del(&wp->modetimer);
1135 		wp->screen = &wp->base;
1136 	} else {
1137 		log_debug("%s: next mode is %s", __func__, next->mode->name);
1138 		wp->screen = next->screen;
1139 		if (next != NULL && next->mode->resize != NULL)
1140 			next->mode->resize(next, wp->sx, wp->sy);
1141 	}
1142 	wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1143 
1144 	server_status_window(wp->window);
1145 	notify_pane("pane-mode-changed", wp);
1146 }
1147 
1148 void
1149 window_pane_reset_mode_all(struct window_pane *wp)
1150 {
1151 	while (!TAILQ_EMPTY(&wp->modes))
1152 		window_pane_reset_mode(wp);
1153 }
1154 
1155 void
1156 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1157     struct winlink *wl, key_code key, struct mouse_event *m)
1158 {
1159 	struct window_mode_entry	*wme;
1160 	struct window_pane		*wp2;
1161 
1162 	if (KEYC_IS_MOUSE(key) && m == NULL)
1163 		return;
1164 
1165 	wme = TAILQ_FIRST(&wp->modes);
1166 	if (wme != NULL) {
1167 		wp->modelast = time(NULL);
1168 		if (wme->mode->key != NULL)
1169 			wme->mode->key(wme, c, s, wl, (key & ~KEYC_XTERM), m);
1170 		return;
1171 	}
1172 
1173 	if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1174 		return;
1175 
1176 	input_key(wp, key, m);
1177 
1178 	if (KEYC_IS_MOUSE(key))
1179 		return;
1180 	if (options_get_number(wp->window->options, "synchronize-panes")) {
1181 		TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1182 			if (wp2 != wp &&
1183 			    TAILQ_EMPTY(&wp2->modes) &&
1184 			    wp2->fd != -1 &&
1185 			    (~wp2->flags & PANE_INPUTOFF) &&
1186 			    window_pane_visible(wp2))
1187 				input_key(wp2, key, NULL);
1188 		}
1189 	}
1190 }
1191 
1192 int
1193 window_pane_visible(struct window_pane *wp)
1194 {
1195 	if (~wp->window->flags & WINDOW_ZOOMED)
1196 		return (1);
1197 	return (wp == wp->window->active);
1198 }
1199 
1200 u_int
1201 window_pane_search(struct window_pane *wp, const char *searchstr)
1202 {
1203 	struct screen	*s = &wp->base;
1204 	char		*newsearchstr, *line;
1205 	u_int		 i;
1206 
1207 	xasprintf(&newsearchstr, "*%s*", searchstr);
1208 
1209 	for (i = 0; i < screen_size_y(s); i++) {
1210 		line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1211 		if (fnmatch(newsearchstr, line, 0) == 0) {
1212 			free(line);
1213 			break;
1214 		}
1215 		free(line);
1216 	}
1217 
1218 	free(newsearchstr);
1219 	if (i == screen_size_y(s))
1220 		return (0);
1221 	return (i + 1);
1222 }
1223 
1224 /* Get MRU pane from a list. */
1225 static struct window_pane *
1226 window_pane_choose_best(struct window_pane **list, u_int size)
1227 {
1228 	struct window_pane	*next, *best;
1229 	u_int			 i;
1230 
1231 	if (size == 0)
1232 		return (NULL);
1233 
1234 	best = list[0];
1235 	for (i = 1; i < size; i++) {
1236 		next = list[i];
1237 		if (next->active_point > best->active_point)
1238 			best = next;
1239 	}
1240 	return (best);
1241 }
1242 
1243 /*
1244  * Find the pane directly above another. We build a list of those adjacent to
1245  * top edge and then choose the best.
1246  */
1247 struct window_pane *
1248 window_pane_find_up(struct window_pane *wp)
1249 {
1250 	struct window_pane	*next, *best, **list;
1251 	u_int			 edge, left, right, end, size;
1252 	int			 status, found;
1253 
1254 	if (wp == NULL)
1255 		return (NULL);
1256 	status = options_get_number(wp->window->options, "pane-border-status");
1257 
1258 	list = NULL;
1259 	size = 0;
1260 
1261 	edge = wp->yoff;
1262 	if (edge == (status == 1 ? 1 : 0))
1263 		edge = wp->window->sy + 1 - (status == 2 ? 1 : 0);
1264 
1265 	left = wp->xoff;
1266 	right = wp->xoff + wp->sx;
1267 
1268 	TAILQ_FOREACH(next, &wp->window->panes, entry) {
1269 		if (next == wp)
1270 			continue;
1271 		if (next->yoff + next->sy + 1 != edge)
1272 			continue;
1273 		end = next->xoff + next->sx - 1;
1274 
1275 		found = 0;
1276 		if (next->xoff < left && end > right)
1277 			found = 1;
1278 		else if (next->xoff >= left && next->xoff <= right)
1279 			found = 1;
1280 		else if (end >= left && end <= right)
1281 			found = 1;
1282 		if (!found)
1283 			continue;
1284 		list = xreallocarray(list, size + 1, sizeof *list);
1285 		list[size++] = next;
1286 	}
1287 
1288 	best = window_pane_choose_best(list, size);
1289 	free(list);
1290 	return (best);
1291 }
1292 
1293 /* Find the pane directly below another. */
1294 struct window_pane *
1295 window_pane_find_down(struct window_pane *wp)
1296 {
1297 	struct window_pane	*next, *best, **list;
1298 	u_int			 edge, left, right, end, size;
1299 	int			 status, found;
1300 
1301 	if (wp == NULL)
1302 		return (NULL);
1303 	status = options_get_number(wp->window->options, "pane-border-status");
1304 
1305 	list = NULL;
1306 	size = 0;
1307 
1308 	edge = wp->yoff + wp->sy + 1;
1309 	if (edge >= wp->window->sy - (status == 2 ? 1 : 0))
1310 		edge = (status == 1 ? 1 : 0);
1311 
1312 	left = wp->xoff;
1313 	right = wp->xoff + wp->sx;
1314 
1315 	TAILQ_FOREACH(next, &wp->window->panes, entry) {
1316 		if (next == wp)
1317 			continue;
1318 		if (next->yoff != edge)
1319 			continue;
1320 		end = next->xoff + next->sx - 1;
1321 
1322 		found = 0;
1323 		if (next->xoff < left && end > right)
1324 			found = 1;
1325 		else if (next->xoff >= left && next->xoff <= right)
1326 			found = 1;
1327 		else if (end >= left && end <= right)
1328 			found = 1;
1329 		if (!found)
1330 			continue;
1331 		list = xreallocarray(list, size + 1, sizeof *list);
1332 		list[size++] = next;
1333 	}
1334 
1335 	best = window_pane_choose_best(list, size);
1336 	free(list);
1337 	return (best);
1338 }
1339 
1340 /* Find the pane directly to the left of another. */
1341 struct window_pane *
1342 window_pane_find_left(struct window_pane *wp)
1343 {
1344 	struct window_pane	*next, *best, **list;
1345 	u_int			 edge, top, bottom, end, size;
1346 	int			 found;
1347 
1348 	if (wp == NULL)
1349 		return (NULL);
1350 
1351 	list = NULL;
1352 	size = 0;
1353 
1354 	edge = wp->xoff;
1355 	if (edge == 0)
1356 		edge = wp->window->sx + 1;
1357 
1358 	top = wp->yoff;
1359 	bottom = wp->yoff + wp->sy;
1360 
1361 	TAILQ_FOREACH(next, &wp->window->panes, entry) {
1362 		if (next == wp)
1363 			continue;
1364 		if (next->xoff + next->sx + 1 != edge)
1365 			continue;
1366 		end = next->yoff + next->sy - 1;
1367 
1368 		found = 0;
1369 		if (next->yoff < top && end > bottom)
1370 			found = 1;
1371 		else if (next->yoff >= top && next->yoff <= bottom)
1372 			found = 1;
1373 		else if (end >= top && end <= bottom)
1374 			found = 1;
1375 		if (!found)
1376 			continue;
1377 		list = xreallocarray(list, size + 1, sizeof *list);
1378 		list[size++] = next;
1379 	}
1380 
1381 	best = window_pane_choose_best(list, size);
1382 	free(list);
1383 	return (best);
1384 }
1385 
1386 /* Find the pane directly to the right of another. */
1387 struct window_pane *
1388 window_pane_find_right(struct window_pane *wp)
1389 {
1390 	struct window_pane	*next, *best, **list;
1391 	u_int			 edge, top, bottom, end, size;
1392 	int			 found;
1393 
1394 	if (wp == NULL)
1395 		return (NULL);
1396 
1397 	list = NULL;
1398 	size = 0;
1399 
1400 	edge = wp->xoff + wp->sx + 1;
1401 	if (edge >= wp->window->sx)
1402 		edge = 0;
1403 
1404 	top = wp->yoff;
1405 	bottom = wp->yoff + wp->sy;
1406 
1407 	TAILQ_FOREACH(next, &wp->window->panes, entry) {
1408 		if (next == wp)
1409 			continue;
1410 		if (next->xoff != edge)
1411 			continue;
1412 		end = next->yoff + next->sy - 1;
1413 
1414 		found = 0;
1415 		if (next->yoff < top && end > bottom)
1416 			found = 1;
1417 		else if (next->yoff >= top && next->yoff <= bottom)
1418 			found = 1;
1419 		else if (end >= top && end <= bottom)
1420 			found = 1;
1421 		if (!found)
1422 			continue;
1423 		list = xreallocarray(list, size + 1, sizeof *list);
1424 		list[size++] = next;
1425 	}
1426 
1427 	best = window_pane_choose_best(list, size);
1428 	free(list);
1429 	return (best);
1430 }
1431 
1432 /* Clear alert flags for a winlink */
1433 void
1434 winlink_clear_flags(struct winlink *wl)
1435 {
1436 	struct winlink	*loop;
1437 
1438 	wl->window->flags &= ~WINDOW_ALERTFLAGS;
1439 	TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1440 		if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1441 			loop->flags &= ~WINLINK_ALERTFLAGS;
1442 			server_status_session(loop->session);
1443 		}
1444 	}
1445 }
1446 
1447 /* Shuffle window indexes up. */
1448 int
1449 winlink_shuffle_up(struct session *s, struct winlink *wl)
1450 {
1451 	int	 idx, last;
1452 
1453 	if (wl == NULL)
1454 		return (-1);
1455 	idx = wl->idx + 1;
1456 
1457 	/* Find the next free index. */
1458 	for (last = idx; last < INT_MAX; last++) {
1459 		if (winlink_find_by_index(&s->windows, last) == NULL)
1460 			break;
1461 	}
1462 	if (last == INT_MAX)
1463 		return (-1);
1464 
1465 	/* Move everything from last - 1 to idx up a bit. */
1466 	for (; last > idx; last--) {
1467 		wl = winlink_find_by_index(&s->windows, last - 1);
1468 		server_link_window(s, wl, s, last, 0, 0, NULL);
1469 		server_unlink_window(s, wl);
1470 	}
1471 
1472 	return (idx);
1473 }
1474 
1475 static void
1476 window_pane_input_callback(struct client *c, int closed, void *data)
1477 {
1478 	struct window_pane_input_data	*cdata = data;
1479 	struct window_pane		*wp;
1480 
1481 	wp = window_pane_find_by_id(cdata->wp);
1482 	if (wp == NULL || closed || c->flags & CLIENT_DEAD) {
1483 		c->stdin_callback = NULL;
1484 		server_client_unref(c);
1485 
1486 		cdata->item->flags &= ~CMDQ_WAITING;
1487 		free(cdata);
1488 
1489 		return;
1490 	}
1491 
1492 	if (evbuffer_add_buffer(wp->event->input, c->stdin_data) != 0)
1493 		evbuffer_drain(c->stdin_data, EVBUFFER_LENGTH(c->stdin_data));
1494 	input_parse(wp);
1495 }
1496 
1497 int
1498 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1499     char **cause)
1500 {
1501 	struct client			*c = item->client;
1502 	struct window_pane_input_data	*cdata;
1503 
1504 	if (~wp->flags & PANE_EMPTY) {
1505 		*cause = xstrdup("pane is not empty");
1506 		return (-1);
1507 	}
1508 
1509 	cdata = xmalloc(sizeof *cdata);
1510 	cdata->item = item;
1511 	cdata->wp = wp->id;
1512 
1513 	return (server_set_stdin_callback(c, window_pane_input_callback, cdata,
1514 	    cause));
1515 }
1516