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