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