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