1 /* $OpenBSD: window.c,v 1.296 2024/11/05 09:41:17 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 = PANE_SCROLLBARS_WIDTH;
601 else
602 sb_w = 0;
603
604 if (sb_pos == PANE_SCROLLBARS_LEFT) {
605 xoff = wp->xoff - sb_w;
606 sx = wp->sx + sb_w;
607 } else { /* sb_pos == PANE_SCROLLBARS_RIGHT */
608 xoff = wp->xoff;
609 sx = wp->sx + sb_w;
610 }
611 if (x < xoff || x > xoff + sx)
612 continue;
613 if (y < wp->yoff || y > wp->yoff + wp->sy)
614 continue;
615 return (wp);
616 }
617 return (NULL);
618 }
619
620 struct window_pane *
window_find_string(struct window * w,const char * s)621 window_find_string(struct window *w, const char *s)
622 {
623 u_int x, y, top = 0, bottom = w->sy - 1;
624 int status;
625
626 x = w->sx / 2;
627 y = w->sy / 2;
628
629 status = options_get_number(w->options, "pane-border-status");
630 if (status == PANE_STATUS_TOP)
631 top++;
632 else if (status == PANE_STATUS_BOTTOM)
633 bottom--;
634
635 if (strcasecmp(s, "top") == 0)
636 y = top;
637 else if (strcasecmp(s, "bottom") == 0)
638 y = bottom;
639 else if (strcasecmp(s, "left") == 0)
640 x = 0;
641 else if (strcasecmp(s, "right") == 0)
642 x = w->sx - 1;
643 else if (strcasecmp(s, "top-left") == 0) {
644 x = 0;
645 y = top;
646 } else if (strcasecmp(s, "top-right") == 0) {
647 x = w->sx - 1;
648 y = top;
649 } else if (strcasecmp(s, "bottom-left") == 0) {
650 x = 0;
651 y = bottom;
652 } else if (strcasecmp(s, "bottom-right") == 0) {
653 x = w->sx - 1;
654 y = bottom;
655 } else
656 return (NULL);
657
658 return (window_get_active_at(w, x, y));
659 }
660
661 int
window_zoom(struct window_pane * wp)662 window_zoom(struct window_pane *wp)
663 {
664 struct window *w = wp->window;
665 struct window_pane *wp1;
666
667 if (w->flags & WINDOW_ZOOMED)
668 return (-1);
669
670 if (window_count_panes(w) == 1)
671 return (-1);
672
673 if (w->active != wp)
674 window_set_active_pane(w, wp, 1);
675
676 TAILQ_FOREACH(wp1, &w->panes, entry) {
677 wp1->saved_layout_cell = wp1->layout_cell;
678 wp1->layout_cell = NULL;
679 }
680
681 w->saved_layout_root = w->layout_root;
682 layout_init(w, wp);
683 w->flags |= WINDOW_ZOOMED;
684 notify_window("window-layout-changed", w);
685
686 return (0);
687 }
688
689 int
window_unzoom(struct window * w,int notify)690 window_unzoom(struct window *w, int notify)
691 {
692 struct window_pane *wp;
693
694 if (!(w->flags & WINDOW_ZOOMED))
695 return (-1);
696
697 w->flags &= ~WINDOW_ZOOMED;
698 layout_free(w);
699 w->layout_root = w->saved_layout_root;
700 w->saved_layout_root = NULL;
701
702 TAILQ_FOREACH(wp, &w->panes, entry) {
703 wp->layout_cell = wp->saved_layout_cell;
704 wp->saved_layout_cell = NULL;
705 }
706 layout_fix_panes(w, NULL);
707
708 if (notify)
709 notify_window("window-layout-changed", w);
710
711 return (0);
712 }
713
714 int
window_push_zoom(struct window * w,int always,int flag)715 window_push_zoom(struct window *w, int always, int flag)
716 {
717 log_debug("%s: @%u %d", __func__, w->id,
718 flag && (w->flags & WINDOW_ZOOMED));
719 if (flag && (always || (w->flags & WINDOW_ZOOMED)))
720 w->flags |= WINDOW_WASZOOMED;
721 else
722 w->flags &= ~WINDOW_WASZOOMED;
723 return (window_unzoom(w, 1) == 0);
724 }
725
726 int
window_pop_zoom(struct window * w)727 window_pop_zoom(struct window *w)
728 {
729 log_debug("%s: @%u %d", __func__, w->id,
730 !!(w->flags & WINDOW_WASZOOMED));
731 if (w->flags & WINDOW_WASZOOMED)
732 return (window_zoom(w->active) == 0);
733 return (0);
734 }
735
736 struct window_pane *
window_add_pane(struct window * w,struct window_pane * other,u_int hlimit,int flags)737 window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
738 int flags)
739 {
740 struct window_pane *wp;
741
742 if (other == NULL)
743 other = w->active;
744
745 wp = window_pane_create(w, w->sx, w->sy, hlimit);
746 if (TAILQ_EMPTY(&w->panes)) {
747 log_debug("%s: @%u at start", __func__, w->id);
748 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
749 } else if (flags & SPAWN_BEFORE) {
750 log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
751 if (flags & SPAWN_FULLSIZE)
752 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
753 else
754 TAILQ_INSERT_BEFORE(other, wp, entry);
755 } else {
756 log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
757 if (flags & SPAWN_FULLSIZE)
758 TAILQ_INSERT_TAIL(&w->panes, wp, entry);
759 else
760 TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
761 }
762 return (wp);
763 }
764
765 void
window_lost_pane(struct window * w,struct window_pane * wp)766 window_lost_pane(struct window *w, struct window_pane *wp)
767 {
768 log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
769
770 if (wp == marked_pane.wp)
771 server_clear_marked();
772
773 window_pane_stack_remove(&w->last_panes, wp);
774 if (wp == w->active) {
775 w->active = TAILQ_FIRST(&w->last_panes);
776 if (w->active == NULL) {
777 w->active = TAILQ_PREV(wp, window_panes, entry);
778 if (w->active == NULL)
779 w->active = TAILQ_NEXT(wp, entry);
780 }
781 if (w->active != NULL) {
782 window_pane_stack_remove(&w->last_panes, w->active);
783 w->active->flags |= PANE_CHANGED;
784 notify_window("window-pane-changed", w);
785 window_update_focus(w);
786 }
787 }
788 }
789
790 void
window_remove_pane(struct window * w,struct window_pane * wp)791 window_remove_pane(struct window *w, struct window_pane *wp)
792 {
793 window_lost_pane(w, wp);
794
795 TAILQ_REMOVE(&w->panes, wp, entry);
796 window_pane_destroy(wp);
797 }
798
799 struct window_pane *
window_pane_at_index(struct window * w,u_int idx)800 window_pane_at_index(struct window *w, u_int idx)
801 {
802 struct window_pane *wp;
803 u_int n;
804
805 n = options_get_number(w->options, "pane-base-index");
806 TAILQ_FOREACH(wp, &w->panes, entry) {
807 if (n == idx)
808 return (wp);
809 n++;
810 }
811 return (NULL);
812 }
813
814 struct window_pane *
window_pane_next_by_number(struct window * w,struct window_pane * wp,u_int n)815 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
816 {
817 for (; n > 0; n--) {
818 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
819 wp = TAILQ_FIRST(&w->panes);
820 }
821
822 return (wp);
823 }
824
825 struct window_pane *
window_pane_previous_by_number(struct window * w,struct window_pane * wp,u_int n)826 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
827 u_int n)
828 {
829 for (; n > 0; n--) {
830 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
831 wp = TAILQ_LAST(&w->panes, window_panes);
832 }
833
834 return (wp);
835 }
836
837 int
window_pane_index(struct window_pane * wp,u_int * i)838 window_pane_index(struct window_pane *wp, u_int *i)
839 {
840 struct window_pane *wq;
841 struct window *w = wp->window;
842
843 *i = options_get_number(w->options, "pane-base-index");
844 TAILQ_FOREACH(wq, &w->panes, entry) {
845 if (wp == wq) {
846 return (0);
847 }
848 (*i)++;
849 }
850
851 return (-1);
852 }
853
854 u_int
window_count_panes(struct window * w)855 window_count_panes(struct window *w)
856 {
857 struct window_pane *wp;
858 u_int n;
859
860 n = 0;
861 TAILQ_FOREACH(wp, &w->panes, entry)
862 n++;
863 return (n);
864 }
865
866 void
window_destroy_panes(struct window * w)867 window_destroy_panes(struct window *w)
868 {
869 struct window_pane *wp;
870
871 while (!TAILQ_EMPTY(&w->last_panes)) {
872 wp = TAILQ_FIRST(&w->last_panes);
873 window_pane_stack_remove(&w->last_panes, wp);
874 }
875
876 while (!TAILQ_EMPTY(&w->panes)) {
877 wp = TAILQ_FIRST(&w->panes);
878 TAILQ_REMOVE(&w->panes, wp, entry);
879 window_pane_destroy(wp);
880 }
881 }
882
883 const char *
window_printable_flags(struct winlink * wl,int escape)884 window_printable_flags(struct winlink *wl, int escape)
885 {
886 struct session *s = wl->session;
887 static char flags[32];
888 int pos;
889
890 pos = 0;
891 if (wl->flags & WINLINK_ACTIVITY) {
892 flags[pos++] = '#';
893 if (escape)
894 flags[pos++] = '#';
895 }
896 if (wl->flags & WINLINK_BELL)
897 flags[pos++] = '!';
898 if (wl->flags & WINLINK_SILENCE)
899 flags[pos++] = '~';
900 if (wl == s->curw)
901 flags[pos++] = '*';
902 if (wl == TAILQ_FIRST(&s->lastw))
903 flags[pos++] = '-';
904 if (server_check_marked() && wl == marked_pane.wl)
905 flags[pos++] = 'M';
906 if (wl->window->flags & WINDOW_ZOOMED)
907 flags[pos++] = 'Z';
908 flags[pos] = '\0';
909 return (flags);
910 }
911
912 struct window_pane *
window_pane_find_by_id_str(const char * s)913 window_pane_find_by_id_str(const char *s)
914 {
915 const char *errstr;
916 u_int id;
917
918 if (*s != '%')
919 return (NULL);
920
921 id = strtonum(s + 1, 0, UINT_MAX, &errstr);
922 if (errstr != NULL)
923 return (NULL);
924 return (window_pane_find_by_id(id));
925 }
926
927 struct window_pane *
window_pane_find_by_id(u_int id)928 window_pane_find_by_id(u_int id)
929 {
930 struct window_pane wp;
931
932 wp.id = id;
933 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
934 }
935
936 static struct window_pane *
window_pane_create(struct window * w,u_int sx,u_int sy,u_int hlimit)937 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
938 {
939 struct window_pane *wp;
940 char host[HOST_NAME_MAX + 1];
941
942 wp = xcalloc(1, sizeof *wp);
943 wp->window = w;
944 wp->options = options_create(w->options);
945 wp->flags = PANE_STYLECHANGED;
946
947 wp->id = next_window_pane_id++;
948 RB_INSERT(window_pane_tree, &all_window_panes, wp);
949
950 wp->fd = -1;
951
952 TAILQ_INIT(&wp->modes);
953
954 TAILQ_INIT (&wp->resize_queue);
955
956 wp->sx = sx;
957 wp->sy = sy;
958
959 wp->pipe_fd = -1;
960
961 wp->control_bg = -1;
962 wp->control_fg = -1;
963
964 colour_palette_init(&wp->palette);
965 colour_palette_from_option(&wp->palette, wp->options);
966
967 screen_init(&wp->base, sx, sy, hlimit);
968 wp->screen = &wp->base;
969 window_pane_default_cursor(wp);
970
971 screen_init(&wp->status_screen, 1, 1, 0);
972
973 if (gethostname(host, sizeof host) == 0)
974 screen_set_title(&wp->base, host);
975
976 return (wp);
977 }
978
979 static void
window_pane_destroy(struct window_pane * wp)980 window_pane_destroy(struct window_pane *wp)
981 {
982 struct window_pane_resize *r;
983 struct window_pane_resize *r1;
984
985 window_pane_reset_mode_all(wp);
986 free(wp->searchstr);
987
988 if (wp->fd != -1) {
989 bufferevent_free(wp->event);
990 close(wp->fd);
991 }
992 if (wp->ictx != NULL)
993 input_free(wp->ictx);
994
995 screen_free(&wp->status_screen);
996
997 screen_free(&wp->base);
998
999 if (wp->pipe_fd != -1) {
1000 bufferevent_free(wp->pipe_event);
1001 close(wp->pipe_fd);
1002 }
1003
1004 if (event_initialized(&wp->resize_timer))
1005 event_del(&wp->resize_timer);
1006 TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
1007 TAILQ_REMOVE(&wp->resize_queue, r, entry);
1008 free(r);
1009 }
1010
1011 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
1012
1013 options_free(wp->options);
1014 free((void *)wp->cwd);
1015 free(wp->shell);
1016 cmd_free_argv(wp->argc, wp->argv);
1017 colour_palette_free(&wp->palette);
1018 free(wp);
1019 }
1020
1021 static void
window_pane_read_callback(__unused struct bufferevent * bufev,void * data)1022 window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1023 {
1024 struct window_pane *wp = data;
1025 struct evbuffer *evb = wp->event->input;
1026 struct window_pane_offset *wpo = &wp->pipe_offset;
1027 size_t size = EVBUFFER_LENGTH(evb);
1028 char *new_data;
1029 size_t new_size;
1030 struct client *c;
1031
1032 if (wp->pipe_fd != -1) {
1033 new_data = window_pane_get_new_data(wp, wpo, &new_size);
1034 if (new_size > 0) {
1035 bufferevent_write(wp->pipe_event, new_data, new_size);
1036 window_pane_update_used_data(wp, wpo, new_size);
1037 }
1038 }
1039
1040 log_debug("%%%u has %zu bytes", wp->id, size);
1041 TAILQ_FOREACH(c, &clients, entry) {
1042 if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1043 control_write_output(c, wp);
1044 }
1045 input_parse_pane(wp);
1046 bufferevent_disable(wp->event, EV_READ);
1047 }
1048
1049 static void
window_pane_error_callback(__unused struct bufferevent * bufev,__unused short what,void * data)1050 window_pane_error_callback(__unused struct bufferevent *bufev,
1051 __unused short what, void *data)
1052 {
1053 struct window_pane *wp = data;
1054
1055 log_debug("%%%u error", wp->id);
1056 wp->flags |= PANE_EXITED;
1057
1058 if (window_pane_destroy_ready(wp))
1059 server_destroy_pane(wp, 1);
1060 }
1061
1062 void
window_pane_set_event(struct window_pane * wp)1063 window_pane_set_event(struct window_pane *wp)
1064 {
1065 setblocking(wp->fd, 0);
1066
1067 wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
1068 NULL, window_pane_error_callback, wp);
1069 if (wp->event == NULL)
1070 fatalx("out of memory");
1071 wp->ictx = input_init(wp, wp->event, &wp->palette);
1072
1073 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1074 }
1075
1076 void
window_pane_resize(struct window_pane * wp,u_int sx,u_int sy)1077 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1078 {
1079 struct window_mode_entry *wme;
1080 struct window_pane_resize *r;
1081
1082 if (sx == wp->sx && sy == wp->sy)
1083 return;
1084
1085 r = xmalloc(sizeof *r);
1086 r->sx = sx;
1087 r->sy = sy;
1088 r->osx = wp->sx;
1089 r->osy = wp->sy;
1090 TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
1091
1092 wp->sx = sx;
1093 wp->sy = sy;
1094
1095 log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1096 screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1097
1098 wme = TAILQ_FIRST(&wp->modes);
1099 if (wme != NULL && wme->mode->resize != NULL)
1100 wme->mode->resize(wme, sx, sy);
1101 }
1102
1103 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)1104 window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
1105 const struct window_mode *mode, struct cmd_find_state *fs,
1106 struct args *args)
1107 {
1108 struct window_mode_entry *wme;
1109 struct window *w = wp->window;
1110
1111 if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1112 return (1);
1113
1114 TAILQ_FOREACH(wme, &wp->modes, entry) {
1115 if (wme->mode == mode)
1116 break;
1117 }
1118 if (wme != NULL) {
1119 TAILQ_REMOVE(&wp->modes, wme, entry);
1120 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1121 } else {
1122 wme = xcalloc(1, sizeof *wme);
1123 wme->wp = wp;
1124 wme->swp = swp;
1125 wme->mode = mode;
1126 wme->prefix = 1;
1127 TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1128 wme->screen = wme->mode->init(wme, fs, args);
1129 }
1130 wp->screen = wme->screen;
1131
1132 wp->flags |= (PANE_REDRAW|PANE_REDRAWSCROLLBAR|PANE_CHANGED);
1133 layout_fix_panes(w, NULL);
1134
1135 server_redraw_window_borders(wp->window);
1136 server_status_window(wp->window);
1137 notify_pane("pane-mode-changed", wp);
1138
1139 return (0);
1140 }
1141
1142 void
window_pane_reset_mode(struct window_pane * wp)1143 window_pane_reset_mode(struct window_pane *wp)
1144 {
1145 struct window_mode_entry *wme, *next;
1146 struct window *w = wp->window;
1147
1148 if (TAILQ_EMPTY(&wp->modes))
1149 return;
1150
1151 wme = TAILQ_FIRST(&wp->modes);
1152 TAILQ_REMOVE(&wp->modes, wme, entry);
1153 wme->mode->free(wme);
1154 free(wme);
1155
1156 next = TAILQ_FIRST(&wp->modes);
1157 if (next == NULL) {
1158 wp->flags &= ~PANE_UNSEENCHANGES;
1159 log_debug("%s: no next mode", __func__);
1160 wp->screen = &wp->base;
1161 } else {
1162 log_debug("%s: next mode is %s", __func__, next->mode->name);
1163 wp->screen = next->screen;
1164 if (next->mode->resize != NULL)
1165 next->mode->resize(next, wp->sx, wp->sy);
1166 }
1167
1168 wp->flags |= (PANE_REDRAW|PANE_REDRAWSCROLLBAR|PANE_CHANGED);
1169 layout_fix_panes(w, NULL);
1170
1171 server_redraw_window_borders(wp->window);
1172 server_status_window(wp->window);
1173 notify_pane("pane-mode-changed", wp);
1174 }
1175
1176 void
window_pane_reset_mode_all(struct window_pane * wp)1177 window_pane_reset_mode_all(struct window_pane *wp)
1178 {
1179 while (!TAILQ_EMPTY(&wp->modes))
1180 window_pane_reset_mode(wp);
1181 }
1182
1183 static void
window_pane_copy_paste(struct window_pane * wp,char * buf,size_t len)1184 window_pane_copy_paste(struct window_pane *wp, char *buf, size_t len)
1185 {
1186 struct window_pane *loop;
1187
1188 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1189 if (loop != wp &&
1190 TAILQ_EMPTY(&loop->modes) &&
1191 loop->fd != -1 &&
1192 (~loop->flags & PANE_INPUTOFF) &&
1193 window_pane_visible(loop) &&
1194 options_get_number(loop->options, "synchronize-panes")) {
1195 log_debug("%s: %.*s", __func__, (int)len, buf);
1196 bufferevent_write(loop->event, buf, len);
1197 }
1198 }
1199 }
1200
1201 static void
window_pane_copy_key(struct window_pane * wp,key_code key)1202 window_pane_copy_key(struct window_pane *wp, key_code key)
1203 {
1204 struct window_pane *loop;
1205
1206 TAILQ_FOREACH(loop, &wp->window->panes, entry) {
1207 if (loop != wp &&
1208 TAILQ_EMPTY(&loop->modes) &&
1209 loop->fd != -1 &&
1210 (~loop->flags & PANE_INPUTOFF) &&
1211 window_pane_visible(loop) &&
1212 options_get_number(loop->options, "synchronize-panes"))
1213 input_key_pane(loop, key, NULL);
1214 }
1215 }
1216
1217 void
window_pane_paste(struct window_pane * wp,char * buf,size_t len)1218 window_pane_paste(struct window_pane *wp, char *buf, size_t len)
1219 {
1220 if (!TAILQ_EMPTY(&wp->modes))
1221 return;
1222
1223 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1224 return;
1225
1226 log_debug("%s: %.*s", __func__, (int)len, buf);
1227 bufferevent_write(wp->event, buf, len);
1228
1229 if (options_get_number(wp->options, "synchronize-panes"))
1230 window_pane_copy_paste(wp, buf, len);
1231 }
1232
1233 int
window_pane_key(struct window_pane * wp,struct client * c,struct session * s,struct winlink * wl,key_code key,struct mouse_event * m)1234 window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1235 struct winlink *wl, key_code key, struct mouse_event *m)
1236 {
1237 struct window_mode_entry *wme;
1238
1239 if (KEYC_IS_MOUSE(key) && m == NULL)
1240 return (-1);
1241
1242 wme = TAILQ_FIRST(&wp->modes);
1243 if (wme != NULL) {
1244 if (wme->mode->key != NULL && c != NULL) {
1245 key &= ~KEYC_MASK_FLAGS;
1246 wme->mode->key(wme, c, s, wl, key, m);
1247 }
1248 return (0);
1249 }
1250
1251 if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1252 return (0);
1253
1254 if (input_key_pane(wp, key, m) != 0)
1255 return (-1);
1256
1257 if (KEYC_IS_MOUSE(key))
1258 return (0);
1259 if (options_get_number(wp->options, "synchronize-panes"))
1260 window_pane_copy_key(wp, key);
1261 return (0);
1262 }
1263
1264 int
window_pane_visible(struct window_pane * wp)1265 window_pane_visible(struct window_pane *wp)
1266 {
1267 if (~wp->window->flags & WINDOW_ZOOMED)
1268 return (1);
1269 return (wp == wp->window->active);
1270 }
1271
1272 int
window_pane_exited(struct window_pane * wp)1273 window_pane_exited(struct window_pane *wp)
1274 {
1275 return (wp->fd == -1 || (wp->flags & PANE_EXITED));
1276 }
1277
1278 u_int
window_pane_search(struct window_pane * wp,const char * term,int regex,int ignore)1279 window_pane_search(struct window_pane *wp, const char *term, int regex,
1280 int ignore)
1281 {
1282 struct screen *s = &wp->base;
1283 regex_t r;
1284 char *new = NULL, *line;
1285 u_int i;
1286 int flags = 0, found;
1287 size_t n;
1288
1289 if (!regex) {
1290 if (ignore)
1291 flags |= FNM_CASEFOLD;
1292 xasprintf(&new, "*%s*", term);
1293 } else {
1294 if (ignore)
1295 flags |= REG_ICASE;
1296 if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
1297 return (0);
1298 }
1299
1300 for (i = 0; i < screen_size_y(s); i++) {
1301 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1302 for (n = strlen(line); n > 0; n--) {
1303 if (!isspace((u_char)line[n - 1]))
1304 break;
1305 line[n - 1] = '\0';
1306 }
1307 log_debug("%s: %s", __func__, line);
1308 if (!regex)
1309 found = (fnmatch(new, line, flags) == 0);
1310 else
1311 found = (regexec(&r, line, 0, NULL, 0) == 0);
1312 free(line);
1313 if (found)
1314 break;
1315 }
1316 if (!regex)
1317 free(new);
1318 else
1319 regfree(&r);
1320
1321 if (i == screen_size_y(s))
1322 return (0);
1323 return (i + 1);
1324 }
1325
1326 /* Get MRU pane from a list. */
1327 static struct window_pane *
window_pane_choose_best(struct window_pane ** list,u_int size)1328 window_pane_choose_best(struct window_pane **list, u_int size)
1329 {
1330 struct window_pane *next, *best;
1331 u_int i;
1332
1333 if (size == 0)
1334 return (NULL);
1335
1336 best = list[0];
1337 for (i = 1; i < size; i++) {
1338 next = list[i];
1339 if (next->active_point > best->active_point)
1340 best = next;
1341 }
1342 return (best);
1343 }
1344
1345 /*
1346 * Find the pane directly above another. We build a list of those adjacent to
1347 * top edge and then choose the best.
1348 */
1349 struct window_pane *
window_pane_find_up(struct window_pane * wp)1350 window_pane_find_up(struct window_pane *wp)
1351 {
1352 struct window *w;
1353 struct window_pane *next, *best, **list;
1354 u_int edge, left, right, end, size;
1355 int status, found;
1356
1357 if (wp == NULL)
1358 return (NULL);
1359 w = wp->window;
1360 status = options_get_number(w->options, "pane-border-status");
1361
1362 list = NULL;
1363 size = 0;
1364
1365 edge = wp->yoff;
1366 if (status == PANE_STATUS_TOP) {
1367 if (edge == 1)
1368 edge = w->sy + 1;
1369 } else if (status == PANE_STATUS_BOTTOM) {
1370 if (edge == 0)
1371 edge = w->sy;
1372 } else {
1373 if (edge == 0)
1374 edge = w->sy + 1;
1375 }
1376
1377 left = wp->xoff;
1378 right = wp->xoff + wp->sx;
1379
1380 TAILQ_FOREACH(next, &w->panes, entry) {
1381 if (next == wp)
1382 continue;
1383 if (next->yoff + next->sy + 1 != edge)
1384 continue;
1385 end = next->xoff + next->sx - 1;
1386
1387 found = 0;
1388 if (next->xoff < left && end > right)
1389 found = 1;
1390 else if (next->xoff >= left && next->xoff <= right)
1391 found = 1;
1392 else if (end >= left && end <= right)
1393 found = 1;
1394 if (!found)
1395 continue;
1396 list = xreallocarray(list, size + 1, sizeof *list);
1397 list[size++] = next;
1398 }
1399
1400 best = window_pane_choose_best(list, size);
1401 free(list);
1402 return (best);
1403 }
1404
1405 /* Find the pane directly below another. */
1406 struct window_pane *
window_pane_find_down(struct window_pane * wp)1407 window_pane_find_down(struct window_pane *wp)
1408 {
1409 struct window *w;
1410 struct window_pane *next, *best, **list;
1411 u_int edge, left, right, end, size;
1412 int status, found;
1413
1414 if (wp == NULL)
1415 return (NULL);
1416 w = wp->window;
1417 status = options_get_number(w->options, "pane-border-status");
1418
1419 list = NULL;
1420 size = 0;
1421
1422 edge = wp->yoff + wp->sy + 1;
1423 if (status == PANE_STATUS_TOP) {
1424 if (edge >= w->sy)
1425 edge = 1;
1426 } else if (status == PANE_STATUS_BOTTOM) {
1427 if (edge >= w->sy - 1)
1428 edge = 0;
1429 } else {
1430 if (edge >= w->sy)
1431 edge = 0;
1432 }
1433
1434 left = wp->xoff;
1435 right = wp->xoff + wp->sx;
1436
1437 TAILQ_FOREACH(next, &w->panes, entry) {
1438 if (next == wp)
1439 continue;
1440 if (next->yoff != edge)
1441 continue;
1442 end = next->xoff + next->sx - 1;
1443
1444 found = 0;
1445 if (next->xoff < left && end > right)
1446 found = 1;
1447 else if (next->xoff >= left && next->xoff <= right)
1448 found = 1;
1449 else if (end >= left && end <= right)
1450 found = 1;
1451 if (!found)
1452 continue;
1453 list = xreallocarray(list, size + 1, sizeof *list);
1454 list[size++] = next;
1455 }
1456
1457 best = window_pane_choose_best(list, size);
1458 free(list);
1459 return (best);
1460 }
1461
1462 /* Find the pane directly to the left of another. */
1463 struct window_pane *
window_pane_find_left(struct window_pane * wp)1464 window_pane_find_left(struct window_pane *wp)
1465 {
1466 struct window *w;
1467 struct window_pane *next, *best, **list;
1468 u_int edge, top, bottom, end, size;
1469 int found;
1470
1471 if (wp == NULL)
1472 return (NULL);
1473 w = wp->window;
1474
1475 list = NULL;
1476 size = 0;
1477
1478 edge = wp->xoff;
1479 if (edge == 0)
1480 edge = w->sx + 1;
1481
1482 top = wp->yoff;
1483 bottom = wp->yoff + wp->sy;
1484
1485 TAILQ_FOREACH(next, &w->panes, entry) {
1486 if (next == wp)
1487 continue;
1488 if (next->xoff + next->sx + 1 != edge)
1489 continue;
1490 end = next->yoff + next->sy - 1;
1491
1492 found = 0;
1493 if (next->yoff < top && end > bottom)
1494 found = 1;
1495 else if (next->yoff >= top && next->yoff <= bottom)
1496 found = 1;
1497 else if (end >= top && end <= bottom)
1498 found = 1;
1499 if (!found)
1500 continue;
1501 list = xreallocarray(list, size + 1, sizeof *list);
1502 list[size++] = next;
1503 }
1504
1505 best = window_pane_choose_best(list, size);
1506 free(list);
1507 return (best);
1508 }
1509
1510 /* Find the pane directly to the right of another. */
1511 struct window_pane *
window_pane_find_right(struct window_pane * wp)1512 window_pane_find_right(struct window_pane *wp)
1513 {
1514 struct window *w;
1515 struct window_pane *next, *best, **list;
1516 u_int edge, top, bottom, end, size;
1517 int found;
1518
1519 if (wp == NULL)
1520 return (NULL);
1521 w = wp->window;
1522
1523 list = NULL;
1524 size = 0;
1525
1526 edge = wp->xoff + wp->sx + 1;
1527 if (edge >= w->sx)
1528 edge = 0;
1529
1530 top = wp->yoff;
1531 bottom = wp->yoff + wp->sy;
1532
1533 TAILQ_FOREACH(next, &w->panes, entry) {
1534 if (next == wp)
1535 continue;
1536 if (next->xoff != edge)
1537 continue;
1538 end = next->yoff + next->sy - 1;
1539
1540 found = 0;
1541 if (next->yoff < top && end > bottom)
1542 found = 1;
1543 else if (next->yoff >= top && next->yoff <= bottom)
1544 found = 1;
1545 else if (end >= top && end <= bottom)
1546 found = 1;
1547 if (!found)
1548 continue;
1549 list = xreallocarray(list, size + 1, sizeof *list);
1550 list[size++] = next;
1551 }
1552
1553 best = window_pane_choose_best(list, size);
1554 free(list);
1555 return (best);
1556 }
1557
1558 void
window_pane_stack_push(struct window_panes * stack,struct window_pane * wp)1559 window_pane_stack_push(struct window_panes *stack, struct window_pane *wp)
1560 {
1561 if (wp != NULL) {
1562 window_pane_stack_remove(stack, wp);
1563 TAILQ_INSERT_HEAD(stack, wp, sentry);
1564 wp->flags |= PANE_VISITED;
1565 }
1566 }
1567
1568 void
window_pane_stack_remove(struct window_panes * stack,struct window_pane * wp)1569 window_pane_stack_remove(struct window_panes *stack, struct window_pane *wp)
1570 {
1571 if (wp != NULL && (wp->flags & PANE_VISITED)) {
1572 TAILQ_REMOVE(stack, wp, sentry);
1573 wp->flags &= ~PANE_VISITED;
1574 }
1575 }
1576
1577 /* Clear alert flags for a winlink */
1578 void
winlink_clear_flags(struct winlink * wl)1579 winlink_clear_flags(struct winlink *wl)
1580 {
1581 struct winlink *loop;
1582
1583 wl->window->flags &= ~WINDOW_ALERTFLAGS;
1584 TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
1585 if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
1586 loop->flags &= ~WINLINK_ALERTFLAGS;
1587 server_status_session(loop->session);
1588 }
1589 }
1590 }
1591
1592 /* Shuffle window indexes up. */
1593 int
winlink_shuffle_up(struct session * s,struct winlink * wl,int before)1594 winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1595 {
1596 int idx, last;
1597
1598 if (wl == NULL)
1599 return (-1);
1600 if (before)
1601 idx = wl->idx;
1602 else
1603 idx = wl->idx + 1;
1604
1605 /* Find the next free index. */
1606 for (last = idx; last < INT_MAX; last++) {
1607 if (winlink_find_by_index(&s->windows, last) == NULL)
1608 break;
1609 }
1610 if (last == INT_MAX)
1611 return (-1);
1612
1613 /* Move everything from last - 1 to idx up a bit. */
1614 for (; last > idx; last--) {
1615 wl = winlink_find_by_index(&s->windows, last - 1);
1616 RB_REMOVE(winlinks, &s->windows, wl);
1617 wl->idx++;
1618 RB_INSERT(winlinks, &s->windows, wl);
1619 }
1620
1621 return (idx);
1622 }
1623
1624 static void
window_pane_input_callback(struct client * c,__unused const char * path,int error,int closed,struct evbuffer * buffer,void * data)1625 window_pane_input_callback(struct client *c, __unused const char *path,
1626 int error, int closed, struct evbuffer *buffer, void *data)
1627 {
1628 struct window_pane_input_data *cdata = data;
1629 struct window_pane *wp;
1630 u_char *buf = EVBUFFER_DATA(buffer);
1631 size_t len = EVBUFFER_LENGTH(buffer);
1632
1633 wp = window_pane_find_by_id(cdata->wp);
1634 if (cdata->file != NULL && (wp == NULL || c->flags & CLIENT_DEAD)) {
1635 if (wp == NULL) {
1636 c->retval = 1;
1637 c->flags |= CLIENT_EXIT;
1638 }
1639 file_cancel(cdata->file);
1640 } else if (cdata->file == NULL || closed || error != 0) {
1641 cmdq_continue(cdata->item);
1642 server_client_unref(c);
1643 free(cdata);
1644 } else
1645 input_parse_buffer(wp, buf, len);
1646 evbuffer_drain(buffer, len);
1647 }
1648
1649 int
window_pane_start_input(struct window_pane * wp,struct cmdq_item * item,char ** cause)1650 window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
1651 char **cause)
1652 {
1653 struct client *c = cmdq_get_client(item);
1654 struct window_pane_input_data *cdata;
1655
1656 if (~wp->flags & PANE_EMPTY) {
1657 *cause = xstrdup("pane is not empty");
1658 return (-1);
1659 }
1660 if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
1661 return (1);
1662 if (c->session != NULL)
1663 return (1);
1664
1665 cdata = xmalloc(sizeof *cdata);
1666 cdata->item = item;
1667 cdata->wp = wp->id;
1668 cdata->file = file_read(c, "-", window_pane_input_callback, cdata);
1669 c->references++;
1670
1671 return (0);
1672 }
1673
1674 void *
window_pane_get_new_data(struct window_pane * wp,struct window_pane_offset * wpo,size_t * size)1675 window_pane_get_new_data(struct window_pane *wp,
1676 struct window_pane_offset *wpo, size_t *size)
1677 {
1678 size_t used = wpo->used - wp->base_offset;
1679
1680 *size = EVBUFFER_LENGTH(wp->event->input) - used;
1681 return (EVBUFFER_DATA(wp->event->input) + used);
1682 }
1683
1684 void
window_pane_update_used_data(struct window_pane * wp,struct window_pane_offset * wpo,size_t size)1685 window_pane_update_used_data(struct window_pane *wp,
1686 struct window_pane_offset *wpo, size_t size)
1687 {
1688 size_t used = wpo->used - wp->base_offset;
1689
1690 if (size > EVBUFFER_LENGTH(wp->event->input) - used)
1691 size = EVBUFFER_LENGTH(wp->event->input) - used;
1692 wpo->used += size;
1693 }
1694
1695 void
window_set_fill_character(struct window * w)1696 window_set_fill_character(struct window *w)
1697 {
1698 const char *value;
1699 struct utf8_data *ud;
1700
1701 free(w->fill_character);
1702 w->fill_character = NULL;
1703
1704 value = options_get_string(w->options, "fill-character");
1705 if (*value != '\0' && utf8_isvalid(value)) {
1706 ud = utf8_fromcstr(value);
1707 if (ud != NULL && ud[0].width == 1)
1708 w->fill_character = ud;
1709 }
1710 }
1711
1712 void
window_pane_default_cursor(struct window_pane * wp)1713 window_pane_default_cursor(struct window_pane *wp)
1714 {
1715 screen_set_default_cursor(wp->screen, wp->options);
1716 }
1717
1718 int
window_pane_mode(struct window_pane * wp)1719 window_pane_mode(struct window_pane *wp)
1720 {
1721 if (TAILQ_FIRST(&wp->modes) != NULL) {
1722 if (TAILQ_FIRST(&wp->modes)->mode == &window_copy_mode)
1723 return (WINDOW_PANE_COPY_MODE);
1724 if (TAILQ_FIRST(&wp->modes)->mode == &window_view_mode)
1725 return (WINDOW_PANE_VIEW_MODE);
1726 }
1727 return (WINDOW_PANE_NO_MODE);
1728 }
1729