Annotation of src/usr.bin/tmux/window.c, Revision 1.132
1.132 ! nicm 1: /* $OpenBSD: window.c,v 1.131 2015/05/12 22:40:38 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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:
21: #include <errno.h>
22: #include <fcntl.h>
1.5 nicm 23: #include <fnmatch.h>
1.1 nicm 24: #include <stdint.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #include <termios.h>
28: #include <unistd.h>
29: #include <util.h>
30:
31: #include "tmux.h"
32:
33: /*
1.14 nicm 34: * Each window is attached to a number of panes, each of which is a pty. This
1.1 nicm 35: * file contains code to handle them.
36: *
37: * A pane has two buffers attached, these are filled and emptied by the main
38: * server poll loop. Output data is received from pty's in screen format,
39: * translated and returned as a series of escape sequences and strings via
40: * input_parse (in input.c). Input data is received as key codes and written
41: * directly via input_key.
42: *
43: * Each pane also has a "virtual" screen (screen.c) which contains the current
44: * state and is redisplayed when the window is reattached to a client.
45: *
46: * Windows are stored directly on a global array and wrapped in any number of
47: * winlink structs to be linked onto local session RB trees. A reference count
48: * is maintained and a window removed from the global list and destroyed when
49: * it reaches zero.
50: */
1.124 nicm 51:
1.1 nicm 52: /* Global window list. */
53: struct windows windows;
54:
1.64 nicm 55: /* Global panes tree. */
56: struct window_pane_tree all_window_panes;
1.71 nicm 57: u_int next_window_pane_id;
58: u_int next_window_id;
1.109 nicm 59: u_int next_active_point;
1.100 nicm 60:
1.131 nicm 61: void window_pane_timer_callback(int, short, void *);
1.37 nicm 62: void window_pane_read_callback(struct bufferevent *, void *);
63: void window_pane_error_callback(struct bufferevent *, short, void *);
64:
1.126 nicm 65: struct window_pane *window_pane_choose_best(struct window_pane **, u_int);
1.109 nicm 66:
1.122 nicm 67: RB_GENERATE(windows, window, entry, window_cmp);
68:
69: int
70: window_cmp(struct window *w1, struct window *w2)
71: {
72: return (w1->id - w2->id);
73: }
74:
1.1 nicm 75: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
76:
77: int
78: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
79: {
80: return (wl1->idx - wl2->idx);
1.12 nicm 81: }
82:
1.64 nicm 83: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
84:
85: int
86: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
87: {
88: return (wp1->id - wp2->id);
89: }
90:
1.12 nicm 91: struct winlink *
92: winlink_find_by_window(struct winlinks *wwl, struct window *w)
93: {
94: struct winlink *wl;
95:
96: RB_FOREACH(wl, winlinks, wwl) {
97: if (wl->window == w)
98: return (wl);
99: }
100:
101: return (NULL);
1.1 nicm 102: }
103:
104: struct winlink *
105: winlink_find_by_index(struct winlinks *wwl, int idx)
106: {
107: struct winlink wl;
108:
109: if (idx < 0)
110: fatalx("bad index");
111:
112: wl.idx = idx;
113: return (RB_FIND(winlinks, wwl, &wl));
114: }
115:
1.71 nicm 116: struct winlink *
117: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
118: {
119: struct winlink *wl;
120:
121: RB_FOREACH(wl, winlinks, wwl) {
122: if (wl->window->id == id)
123: return (wl);
124: }
1.78 nicm 125: return (NULL);
1.71 nicm 126: }
127:
1.1 nicm 128: int
1.22 nicm 129: winlink_next_index(struct winlinks *wwl, int idx)
1.1 nicm 130: {
1.22 nicm 131: int i;
1.1 nicm 132:
1.22 nicm 133: i = idx;
134: do {
1.1 nicm 135: if (winlink_find_by_index(wwl, i) == NULL)
136: return (i);
1.22 nicm 137: if (i == INT_MAX)
138: i = 0;
139: else
140: i++;
141: } while (i != idx);
142: return (-1);
1.1 nicm 143: }
144:
145: u_int
146: winlink_count(struct winlinks *wwl)
147: {
148: struct winlink *wl;
149: u_int n;
150:
151: n = 0;
152: RB_FOREACH(wl, winlinks, wwl)
153: n++;
154:
155: return (n);
156: }
157:
158: struct winlink *
1.63 nicm 159: winlink_add(struct winlinks *wwl, int idx)
1.1 nicm 160: {
161: struct winlink *wl;
162:
1.22 nicm 163: if (idx < 0) {
164: if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
165: return (NULL);
166: } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1 nicm 167: return (NULL);
168:
169: wl = xcalloc(1, sizeof *wl);
170: wl->idx = idx;
171: RB_INSERT(winlinks, wwl, wl);
172:
1.63 nicm 173: return (wl);
174: }
175:
176: void
177: winlink_set_window(struct winlink *wl, struct window *w)
178: {
179: wl->window = w;
1.1 nicm 180: w->references++;
181: }
182:
183: void
184: winlink_remove(struct winlinks *wwl, struct winlink *wl)
185: {
186: struct window *w = wl->window;
187:
188: RB_REMOVE(winlinks, wwl, wl);
1.82 nicm 189: free(wl->status_text);
190: free(wl);
1.1 nicm 191:
1.84 nicm 192: if (w != NULL)
193: window_remove_ref(w);
1.1 nicm 194: }
195:
196: struct winlink *
1.41 nicm 197: winlink_next(struct winlink *wl)
1.1 nicm 198: {
199: return (RB_NEXT(winlinks, wwl, wl));
200: }
201:
202: struct winlink *
1.41 nicm 203: winlink_previous(struct winlink *wl)
1.1 nicm 204: {
205: return (RB_PREV(winlinks, wwl, wl));
1.52 nicm 206: }
207:
208: struct winlink *
1.53 nicm 209: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52 nicm 210: {
211: for (; n > 0; n--) {
212: if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53 nicm 213: wl = RB_MIN(winlinks, &s->windows);
1.52 nicm 214: }
215:
216: return (wl);
217: }
218:
219: struct winlink *
1.53 nicm 220: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52 nicm 221: {
222: for (; n > 0; n--) {
223: if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53 nicm 224: wl = RB_MAX(winlinks, &s->windows);
1.52 nicm 225: }
226:
227: return (wl);
1.1 nicm 228: }
229:
230: void
231: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
232: {
233: if (wl == NULL)
234: return;
235:
236: winlink_stack_remove(stack, wl);
1.28 nicm 237: TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1 nicm 238: }
239:
240: void
241: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
242: {
243: struct winlink *wl2;
244:
245: if (wl == NULL)
246: return;
1.42 nicm 247:
1.28 nicm 248: TAILQ_FOREACH(wl2, stack, sentry) {
1.1 nicm 249: if (wl2 == wl) {
1.28 nicm 250: TAILQ_REMOVE(stack, wl, sentry);
1.1 nicm 251: return;
252: }
253: }
254: }
255:
256: struct window *
1.125 nicm 257: window_find_by_id_str(const char *s)
1.123 nicm 258: {
259: const char *errstr;
260: u_int id;
261:
262: if (*s != '@')
263: return (NULL);
264:
265: id = strtonum(s + 1, 0, UINT_MAX, &errstr);
266: if (errstr != NULL)
267: return (NULL);
268: return (window_find_by_id(id));
269: }
270:
271: struct window *
1.71 nicm 272: window_find_by_id(u_int id)
273: {
1.122 nicm 274: struct window w;
1.71 nicm 275:
1.122 nicm 276: w.id = id;
277: return (RB_FIND(windows, &windows, &w));
1.71 nicm 278: }
279:
280: struct window *
1.1 nicm 281: window_create1(u_int sx, u_int sy)
282: {
283: struct window *w;
284:
1.38 nicm 285: w = xcalloc(1, sizeof *w);
1.1 nicm 286: w->name = NULL;
287: w->flags = 0;
288:
289: TAILQ_INIT(&w->panes);
290: w->active = NULL;
1.14 nicm 291:
1.17 nicm 292: w->lastlayout = -1;
1.14 nicm 293: w->layout_root = NULL;
1.42 nicm 294:
1.1 nicm 295: w->sx = sx;
296: w->sy = sy;
297:
1.7 nicm 298: options_init(&w->options, &global_w_options);
1.79 nicm 299: if (options_get_number(&w->options, "automatic-rename"))
300: queue_window_name(w);
1.1 nicm 301:
302: w->references = 0;
303:
1.122 nicm 304: w->id = next_window_id++;
1.129 nicm 305: RB_INSERT(windows, &windows, w);
1.122 nicm 306:
1.1 nicm 307: return (w);
308: }
309:
310: struct window *
1.110 nicm 311: window_create(const char *name, int argc, char **argv, const char *path,
1.107 nicm 312: const char *shell, int cwd, struct environ *env, struct termios *tio,
1.91 nicm 313: u_int sx, u_int sy, u_int hlimit, char **cause)
1.1 nicm 314: {
1.14 nicm 315: struct window *w;
316: struct window_pane *wp;
1.1 nicm 317:
318: w = window_create1(sx, sy);
1.16 nicm 319: wp = window_add_pane(w, hlimit);
1.93 nicm 320: layout_init(w, wp);
1.91 nicm 321:
1.110 nicm 322: if (window_pane_spawn(wp, argc, argv, path, shell, cwd, env, tio,
1.107 nicm 323: cause) != 0) {
1.1 nicm 324: window_destroy(w);
325: return (NULL);
326: }
1.91 nicm 327:
1.1 nicm 328: w->active = TAILQ_FIRST(&w->panes);
329: if (name != NULL) {
330: w->name = xstrdup(name);
331: options_set_number(&w->options, "automatic-rename", 0);
332: } else
333: w->name = default_window_name(w);
1.91 nicm 334:
1.1 nicm 335: return (w);
336: }
337:
338: void
339: window_destroy(struct window *w)
340: {
1.93 nicm 341: window_unzoom(w);
342:
1.122 nicm 343: RB_REMOVE(windows, &windows, w);
1.1 nicm 344:
1.14 nicm 345: if (w->layout_root != NULL)
346: layout_free(w);
1.127 nicm 347: free(w->old_layout);
1.14 nicm 348:
1.73 nicm 349: if (event_initialized(&w->name_timer))
350: evtimer_del(&w->name_timer);
1.38 nicm 351:
1.1 nicm 352: options_free(&w->options);
353:
354: window_destroy_panes(w);
355:
1.82 nicm 356: free(w->name);
357: free(w);
1.84 nicm 358: }
359:
360: void
361: window_remove_ref(struct window *w)
362: {
363: if (w->references == 0)
364: fatal("bad reference count");
365: w->references--;
366: if (w->references == 0)
367: window_destroy(w);
1.72 nicm 368: }
369:
370: void
371: window_set_name(struct window *w, const char *new_name)
372: {
1.82 nicm 373: free(w->name);
1.72 nicm 374: w->name = xstrdup(new_name);
1.74 nicm 375: notify_window_renamed(w);
1.1 nicm 376: }
377:
1.15 nicm 378: void
1.1 nicm 379: window_resize(struct window *w, u_int sx, u_int sy)
380: {
381: w->sx = sx;
382: w->sy = sy;
383: }
384:
1.114 nicm 385: int
1.118 nicm 386: window_has_pane(struct window *w, struct window_pane *wp)
387: {
388: struct window_pane *wp1;
389:
390: TAILQ_FOREACH(wp1, &w->panes, entry) {
391: if (wp1 == wp)
392: return (1);
393: }
394: return (0);
395: }
396:
397: int
1.1 nicm 398: window_set_active_pane(struct window *w, struct window_pane *wp)
399: {
1.59 nicm 400: if (wp == w->active)
1.114 nicm 401: return (0);
1.58 nicm 402: w->last = w->active;
1.1 nicm 403: w->active = wp;
1.10 nicm 404: while (!window_pane_visible(w->active)) {
1.1 nicm 405: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 406: if (w->active == NULL)
407: w->active = TAILQ_LAST(&w->panes, window_panes);
408: if (w->active == wp)
1.114 nicm 409: return (1);
1.29 nicm 410: }
1.109 nicm 411: w->active->active_point = next_active_point++;
1.114 nicm 412: return (1);
1.29 nicm 413: }
414:
1.66 nicm 415: struct window_pane *
416: window_get_active_at(struct window *w, u_int x, u_int y)
1.29 nicm 417: {
418: struct window_pane *wp;
419:
420: TAILQ_FOREACH(wp, &w->panes, entry) {
1.66 nicm 421: if (!window_pane_visible(wp))
1.29 nicm 422: continue;
1.66 nicm 423: if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29 nicm 424: continue;
1.66 nicm 425: if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29 nicm 426: continue;
1.66 nicm 427: return (wp);
428: }
429: return (NULL);
430: }
431:
432: void
433: window_set_active_at(struct window *w, u_int x, u_int y)
434: {
435: struct window_pane *wp;
436:
437: wp = window_get_active_at(w, x, y);
438: if (wp != NULL && wp != w->active)
1.29 nicm 439: window_set_active_pane(w, wp);
1.66 nicm 440: }
441:
442: struct window_pane *
443: window_find_string(struct window *w, const char *s)
444: {
445: u_int x, y;
446:
447: x = w->sx / 2;
448: y = w->sy / 2;
449:
450: if (strcasecmp(s, "top") == 0)
451: y = 0;
452: else if (strcasecmp(s, "bottom") == 0)
453: y = w->sy - 1;
454: else if (strcasecmp(s, "left") == 0)
455: x = 0;
456: else if (strcasecmp(s, "right") == 0)
457: x = w->sx - 1;
458: else if (strcasecmp(s, "top-left") == 0) {
459: x = 0;
460: y = 0;
461: } else if (strcasecmp(s, "top-right") == 0) {
462: x = w->sx - 1;
463: y = 0;
464: } else if (strcasecmp(s, "bottom-left") == 0) {
465: x = 0;
466: y = w->sy - 1;
467: } else if (strcasecmp(s, "bottom-right") == 0) {
468: x = w->sx - 1;
469: y = w->sy - 1;
470: } else
471: return (NULL);
472:
473: return (window_get_active_at(w, x, y));
1.1 nicm 474: }
475:
1.93 nicm 476: int
477: window_zoom(struct window_pane *wp)
478: {
479: struct window *w = wp->window;
480: struct window_pane *wp1;
481:
482: if (w->flags & WINDOW_ZOOMED)
483: return (-1);
484:
485: if (!window_pane_visible(wp))
486: return (-1);
1.94 nicm 487:
488: if (window_count_panes(w) == 1)
489: return (-1);
490:
1.93 nicm 491: if (w->active != wp)
492: window_set_active_pane(w, wp);
493:
494: TAILQ_FOREACH(wp1, &w->panes, entry) {
495: wp1->saved_layout_cell = wp1->layout_cell;
496: wp1->layout_cell = NULL;
497: }
498:
499: w->saved_layout_root = w->layout_root;
500: layout_init(w, wp);
501: w->flags |= WINDOW_ZOOMED;
1.115 nicm 502: notify_window_layout_changed(w);
1.93 nicm 503:
504: return (0);
505: }
506:
507: int
508: window_unzoom(struct window *w)
509: {
1.97 nicm 510: struct window_pane *wp;
1.93 nicm 511:
512: if (!(w->flags & WINDOW_ZOOMED))
513: return (-1);
514:
515: w->flags &= ~WINDOW_ZOOMED;
516: layout_free(w);
517: w->layout_root = w->saved_layout_root;
1.120 nicm 518: w->saved_layout_root = NULL;
1.93 nicm 519:
1.97 nicm 520: TAILQ_FOREACH(wp, &w->panes, entry) {
521: wp->layout_cell = wp->saved_layout_cell;
522: wp->saved_layout_cell = NULL;
1.93 nicm 523: }
524: layout_fix_panes(w, w->sx, w->sy);
1.115 nicm 525: notify_window_layout_changed(w);
1.93 nicm 526:
527: return (0);
528: }
529:
1.1 nicm 530: struct window_pane *
1.16 nicm 531: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 532: {
533: struct window_pane *wp;
534:
1.14 nicm 535: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 536: if (TAILQ_EMPTY(&w->panes))
537: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
538: else
539: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
540: return (wp);
541: }
542:
543: void
1.105 nicm 544: window_lost_pane(struct window *w, struct window_pane *wp)
1.1 nicm 545: {
1.132 ! nicm 546: if (wp == marked_window_pane)
! 547: server_clear_marked();
! 548:
1.57 nicm 549: if (wp == w->active) {
1.58 nicm 550: w->active = w->last;
551: w->last = NULL;
552: if (w->active == NULL) {
553: w->active = TAILQ_PREV(wp, window_panes, entry);
554: if (w->active == NULL)
555: w->active = TAILQ_NEXT(wp, entry);
556: }
557: } else if (wp == w->last)
558: w->last = NULL;
1.105 nicm 559: }
560:
561: void
562: window_remove_pane(struct window *w, struct window_pane *wp)
563: {
564: window_lost_pane(w, wp);
1.1 nicm 565:
566: TAILQ_REMOVE(&w->panes, wp, entry);
567: window_pane_destroy(wp);
568: }
569:
570: struct window_pane *
571: window_pane_at_index(struct window *w, u_int idx)
572: {
573: struct window_pane *wp;
574: u_int n;
575:
1.67 nicm 576: n = options_get_number(&w->options, "pane-base-index");
1.1 nicm 577: TAILQ_FOREACH(wp, &w->panes, entry) {
578: if (n == idx)
579: return (wp);
580: n++;
581: }
582: return (NULL);
1.53 nicm 583: }
584:
585: struct window_pane *
586: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
587: {
588: for (; n > 0; n--) {
589: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
590: wp = TAILQ_FIRST(&w->panes);
591: }
592:
593: return (wp);
594: }
595:
596: struct window_pane *
597: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
598: u_int n)
599: {
600: for (; n > 0; n--) {
601: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
602: wp = TAILQ_LAST(&w->panes, window_panes);
603: }
604:
605: return (wp);
1.13 nicm 606: }
607:
1.69 nicm 608: int
609: window_pane_index(struct window_pane *wp, u_int *i)
1.13 nicm 610: {
611: struct window_pane *wq;
1.69 nicm 612: struct window *w = wp->window;
1.13 nicm 613:
1.69 nicm 614: *i = options_get_number(&w->options, "pane-base-index");
1.13 nicm 615: TAILQ_FOREACH(wq, &w->panes, entry) {
1.69 nicm 616: if (wp == wq) {
617: return (0);
618: }
619: (*i)++;
1.13 nicm 620: }
1.69 nicm 621:
622: return (-1);
1.1 nicm 623: }
624:
625: u_int
626: window_count_panes(struct window *w)
627: {
628: struct window_pane *wp;
629: u_int n;
630:
631: n = 0;
632: TAILQ_FOREACH(wp, &w->panes, entry)
633: n++;
634: return (n);
635: }
636:
637: void
638: window_destroy_panes(struct window *w)
639: {
640: struct window_pane *wp;
641:
642: while (!TAILQ_EMPTY(&w->panes)) {
643: wp = TAILQ_FIRST(&w->panes);
644: TAILQ_REMOVE(&w->panes, wp, entry);
645: window_pane_destroy(wp);
646: }
1.61 nicm 647: }
648:
1.128 nicm 649: /* Retuns the printable flags on a window, empty string if no flags set. */
1.61 nicm 650: char *
651: window_printable_flags(struct session *s, struct winlink *wl)
652: {
1.119 nicm 653: char flags[32];
1.61 nicm 654: int pos;
655:
656: pos = 0;
657: if (wl->flags & WINLINK_ACTIVITY)
658: flags[pos++] = '#';
659: if (wl->flags & WINLINK_BELL)
660: flags[pos++] = '!';
661: if (wl->flags & WINLINK_SILENCE)
662: flags[pos++] = '~';
663: if (wl == s->curw)
664: flags[pos++] = '*';
665: if (wl == TAILQ_FIRST(&s->lastw))
666: flags[pos++] = '-';
1.132 ! nicm 667: if (server_check_marked() && wl == marked_winlink)
! 668: flags[pos++] = 'M';
1.93 nicm 669: if (wl->window->flags & WINDOW_ZOOMED)
670: flags[pos++] = 'Z';
1.61 nicm 671: flags[pos] = '\0';
672: return (xstrdup(flags));
1.1 nicm 673: }
674:
1.123 nicm 675: struct window_pane *
676: window_pane_find_by_id_str(const char *s)
677: {
678: const char *errstr;
679: u_int id;
680:
681: if (*s != '%')
682: return (NULL);
683:
684: id = strtonum(s + 1, 0, UINT_MAX, &errstr);
685: if (errstr != NULL)
686: return (NULL);
687: return (window_pane_find_by_id(id));
688: }
689:
1.64 nicm 690: struct window_pane *
691: window_pane_find_by_id(u_int id)
692: {
693: struct window_pane wp;
694:
695: wp.id = id;
696: return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
697: }
698:
1.1 nicm 699: struct window_pane *
700: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
701: {
702: struct window_pane *wp;
703:
704: wp = xcalloc(1, sizeof *wp);
705: wp->window = w;
706:
1.71 nicm 707: wp->id = next_window_pane_id++;
1.64 nicm 708: RB_INSERT(window_pane_tree, &all_window_panes, wp);
709:
1.110 nicm 710: wp->argc = 0;
711: wp->argv = NULL;
1.23 nicm 712: wp->shell = NULL;
1.99 nicm 713: wp->cwd = -1;
1.1 nicm 714:
715: wp->fd = -1;
1.37 nicm 716: wp->event = NULL;
1.1 nicm 717:
718: wp->mode = NULL;
1.14 nicm 719:
720: wp->layout_cell = NULL;
1.1 nicm 721:
722: wp->xoff = 0;
1.42 nicm 723: wp->yoff = 0;
1.1 nicm 724:
725: wp->sx = sx;
726: wp->sy = sy;
727:
1.32 nicm 728: wp->pipe_fd = -1;
729: wp->pipe_off = 0;
1.36 nicm 730: wp->pipe_event = NULL;
1.32 nicm 731:
1.9 nicm 732: wp->saved_grid = NULL;
1.117 nicm 733:
734: memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
1.9 nicm 735:
1.1 nicm 736: screen_init(&wp->base, sx, sy, hlimit);
737: wp->screen = &wp->base;
738:
739: input_init(wp);
740:
741: return (wp);
742: }
743:
744: void
745: window_pane_destroy(struct window_pane *wp)
746: {
1.55 nicm 747: window_pane_reset_mode(wp);
748:
1.131 nicm 749: if (event_initialized(&wp->timer))
750: evtimer_del(&wp->timer);
751:
1.37 nicm 752: if (wp->fd != -1) {
1.70 nicm 753: bufferevent_free(wp->event);
1.1 nicm 754: close(wp->fd);
1.37 nicm 755: }
1.1 nicm 756:
757: input_free(wp);
758:
759: screen_free(&wp->base);
1.9 nicm 760: if (wp->saved_grid != NULL)
761: grid_destroy(wp->saved_grid);
1.1 nicm 762:
1.32 nicm 763: if (wp->pipe_fd != -1) {
1.70 nicm 764: bufferevent_free(wp->pipe_event);
1.32 nicm 765: close(wp->pipe_fd);
766: }
767:
1.64 nicm 768: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
769:
1.99 nicm 770: close(wp->cwd);
1.82 nicm 771: free(wp->shell);
1.110 nicm 772: cmd_free_argv(wp->argc, wp->argv);
1.82 nicm 773: free(wp);
1.1 nicm 774: }
775:
776: int
1.110 nicm 777: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
778: const char *path, const char *shell, int cwd, struct environ *env,
779: struct termios *tio, char **cause)
1.1 nicm 780: {
1.47 nicm 781: struct winsize ws;
1.110 nicm 782: char *argv0, *cmd, **argvp, paneid[16];
783: const char *ptr, *first;
1.47 nicm 784: struct termios tio2;
1.110 nicm 785: int i;
1.1 nicm 786:
1.37 nicm 787: if (wp->fd != -1) {
1.70 nicm 788: bufferevent_free(wp->event);
1.1 nicm 789: close(wp->fd);
1.37 nicm 790: }
1.110 nicm 791: if (argc > 0) {
792: cmd_free_argv(wp->argc, wp->argv);
793: wp->argc = argc;
794: wp->argv = cmd_copy_argv(argc, argv);
1.1 nicm 795: }
1.23 nicm 796: if (shell != NULL) {
1.82 nicm 797: free(wp->shell);
1.23 nicm 798: wp->shell = xstrdup(shell);
799: }
1.99 nicm 800: if (cwd != -1) {
801: close(wp->cwd);
802: wp->cwd = dup(cwd);
1.1 nicm 803: }
1.91 nicm 804:
1.110 nicm 805: cmd = cmd_stringify_argv(wp->argc, wp->argv);
806: log_debug("spawn: %s -- %s", wp->shell, cmd);
807: for (i = 0; i < wp->argc; i++)
808: log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
1.1 nicm 809:
810: memset(&ws, 0, sizeof ws);
811: ws.ws_col = screen_size_x(&wp->base);
812: ws.ws_row = screen_size_y(&wp->base);
813:
1.42 nicm 814: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 815: case -1:
816: wp->fd = -1;
817: xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110 nicm 818: free(cmd);
1.1 nicm 819: return (-1);
820: case 0:
1.99 nicm 821: if (fchdir(wp->cwd) != 0)
1.1 nicm 822: chdir("/");
1.25 nicm 823:
824: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
825: fatal("tcgetattr failed");
826: if (tio != NULL)
827: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
828: tio2.c_cc[VERASE] = '\177';
829: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
830: fatal("tcgetattr failed");
1.18 nicm 831:
1.56 nicm 832: closefrom(STDERR_FILENO + 1);
833:
1.107 nicm 834: if (path != NULL)
835: environ_set(env, "PATH", path);
1.64 nicm 836: xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
837: environ_set(env, "TMUX_PANE", paneid);
1.47 nicm 838: environ_push(env);
1.18 nicm 839:
1.54 nicm 840: clear_signals(1);
1.1 nicm 841: log_close();
842:
1.80 nicm 843: setenv("SHELL", wp->shell, 1);
844: ptr = strrchr(wp->shell, '/');
845:
1.110 nicm 846: /*
847: * If given one argument, assume it should be passed to sh -c;
848: * with more than one argument, use execvp(). If there is no
849: * arguments, create a login shell.
850: */
851: if (wp->argc > 0) {
852: if (wp->argc != 1) {
853: /* Copy to ensure argv ends in NULL. */
854: argvp = cmd_copy_argv(wp->argc, wp->argv);
855: execvp(argvp[0], argvp);
856: fatal("execvp failed");
857: }
858: first = wp->argv[0];
859:
1.80 nicm 860: if (ptr != NULL && *(ptr + 1) != '\0')
861: xasprintf(&argv0, "%s", ptr + 1);
862: else
863: xasprintf(&argv0, "%s", wp->shell);
1.110 nicm 864: execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8 nicm 865: fatal("execl failed");
866: }
1.23 nicm 867: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 868: xasprintf(&argv0, "-%s", ptr + 1);
869: else
1.23 nicm 870: xasprintf(&argv0, "-%s", wp->shell);
1.110 nicm 871: execl(wp->shell, argv0, (char *)NULL);
1.1 nicm 872: fatal("execl failed");
873: }
874:
1.62 nicm 875: setblocking(wp->fd, 0);
876:
1.110 nicm 877: wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
878: window_pane_error_callback, wp);
1.131 nicm 879:
880: bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
1.37 nicm 881: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 882:
1.110 nicm 883: free(cmd);
1.1 nicm 884: return (0);
885: }
886:
1.15 nicm 887: void
1.131 nicm 888: window_pane_timer_callback(unused int fd, unused short events, void *data)
889: {
890: window_pane_read_callback(NULL, data);
891: }
892:
893: void
1.37 nicm 894: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
895: {
1.131 nicm 896: struct window_pane *wp = data;
897: struct evbuffer *evb = wp->event->input;
898: char *new_data;
899: size_t new_size, available;
900: struct client *c;
901: struct timeval tv;
902:
903: if (event_initialized(&wp->timer))
904: evtimer_del(&wp->timer);
905:
906: log_debug("%%%u has %zu bytes", wp->id, EVBUFFER_LENGTH(evb));
1.46 nicm 907:
1.131 nicm 908: TAILQ_FOREACH(c, &clients, entry) {
909: if (!tty_client_ready(c, wp))
910: continue;
911:
912: available = EVBUFFER_LENGTH(c->tty.event->output);
913: if (available > READ_BACKOFF)
914: goto start_timer;
915: }
916:
917: new_size = EVBUFFER_LENGTH(evb) - wp->pipe_off;
1.46 nicm 918: if (wp->pipe_fd != -1 && new_size > 0) {
1.131 nicm 919: new_data = EVBUFFER_DATA(evb);
1.46 nicm 920: bufferevent_write(wp->pipe_event, new_data, new_size);
921: }
922:
923: input_parse(wp);
1.37 nicm 924:
1.131 nicm 925: wp->pipe_off = EVBUFFER_LENGTH(evb);
1.60 nicm 926:
927: /*
928: * If we get here, we're not outputting anymore, so set the silence
929: * flag on the window.
930: */
931: wp->window->flags |= WINDOW_SILENCE;
932: if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
933: fatal("gettimeofday failed.");
1.131 nicm 934: return;
935:
936: start_timer:
937: log_debug("%%%u backing off (%s %zu > %d)", wp->id, c->ttyname,
938: available, READ_BACKOFF);
939:
940: tv.tv_sec = 0;
941: tv.tv_usec = READ_TIME;
942:
943: evtimer_set(&wp->timer, window_pane_timer_callback, wp);
944: evtimer_add(&wp->timer, &tv);
1.37 nicm 945: }
946:
947: void
1.131 nicm 948: window_pane_error_callback(unused struct bufferevent *bufev, unused short what,
949: void *data)
1.37 nicm 950: {
951: struct window_pane *wp = data;
952:
1.39 nicm 953: server_destroy_pane(wp);
1.37 nicm 954: }
955:
956: void
1.1 nicm 957: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
958: {
959: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 960: return;
1.1 nicm 961: wp->sx = sx;
962: wp->sy = sy;
963:
1.89 nicm 964: screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1 nicm 965: if (wp->mode != NULL)
966: wp->mode->resize(wp, sx, sy);
1.95 nicm 967:
968: wp->flags |= PANE_RESIZE;
1.44 nicm 969: }
970:
971: /*
972: * Enter alternative screen mode. A copy of the visible screen is saved and the
973: * history is not updated
974: */
975: void
1.87 nicm 976: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
977: int cursor)
1.44 nicm 978: {
979: struct screen *s = &wp->base;
980: u_int sx, sy;
981:
982: if (wp->saved_grid != NULL)
983: return;
984: if (!options_get_number(&wp->window->options, "alternate-screen"))
985: return;
986: sx = screen_size_x(s);
987: sy = screen_size_y(s);
988:
989: wp->saved_grid = grid_create(sx, sy, 0);
990: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87 nicm 991: if (cursor) {
992: wp->saved_cx = s->cx;
993: wp->saved_cy = s->cy;
994: }
1.44 nicm 995: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
996:
997: grid_view_clear(s->grid, 0, 0, sx, sy);
998:
999: wp->base.grid->flags &= ~GRID_HISTORY;
1000:
1001: wp->flags |= PANE_REDRAW;
1002: }
1003:
1004: /* Exit alternate screen mode and restore the copied grid. */
1005: void
1.87 nicm 1006: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
1007: int cursor)
1.44 nicm 1008: {
1009: struct screen *s = &wp->base;
1010: u_int sx, sy;
1011:
1012: if (wp->saved_grid == NULL)
1013: return;
1014: if (!options_get_number(&wp->window->options, "alternate-screen"))
1015: return;
1016: sx = screen_size_x(s);
1017: sy = screen_size_y(s);
1018:
1019: /*
1020: * If the current size is bigger, temporarily resize to the old size
1021: * before copying back.
1022: */
1023: if (sy > wp->saved_grid->sy)
1.89 nicm 1024: screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44 nicm 1025:
1026: /* Restore the grid, cursor position and cell. */
1027: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87 nicm 1028: if (cursor)
1029: s->cx = wp->saved_cx;
1.44 nicm 1030: if (s->cx > screen_size_x(s) - 1)
1031: s->cx = screen_size_x(s) - 1;
1.87 nicm 1032: if (cursor)
1033: s->cy = wp->saved_cy;
1.44 nicm 1034: if (s->cy > screen_size_y(s) - 1)
1035: s->cy = screen_size_y(s) - 1;
1036: memcpy(gc, &wp->saved_cell, sizeof *gc);
1037:
1038: /*
1039: * Turn history back on (so resize can use it) and then resize back to
1040: * the current size.
1041: */
1042: wp->base.grid->flags |= GRID_HISTORY;
1.89 nicm 1043: if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1044: screen_resize(s, sx, sy, 1);
1.44 nicm 1045:
1046: grid_destroy(wp->saved_grid);
1047: wp->saved_grid = NULL;
1048:
1049: wp->flags |= PANE_REDRAW;
1.1 nicm 1050: }
1051:
1052: int
1053: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1054: {
1055: struct screen *s;
1056:
1.15 nicm 1057: if (wp->mode != NULL)
1.1 nicm 1058: return (1);
1059: wp->mode = mode;
1060:
1061: if ((s = wp->mode->init(wp)) != NULL)
1062: wp->screen = s;
1.34 nicm 1063: wp->flags |= PANE_REDRAW;
1.1 nicm 1064: return (0);
1065: }
1066:
1067: void
1068: window_pane_reset_mode(struct window_pane *wp)
1069: {
1070: if (wp->mode == NULL)
1071: return;
1072:
1073: wp->mode->free(wp);
1074: wp->mode = NULL;
1075:
1076: wp->screen = &wp->base;
1.34 nicm 1077: wp->flags |= PANE_REDRAW;
1.1 nicm 1078: }
1079:
1080: void
1.118 nicm 1081: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1082: int key, struct mouse_event *m)
1.1 nicm 1083: {
1.27 nicm 1084: struct window_pane *wp2;
1.3 nicm 1085:
1.118 nicm 1086: if (KEYC_IS_MOUSE(key) && m == NULL)
1087: return;
1088:
1.1 nicm 1089: if (wp->mode != NULL) {
1090: if (wp->mode->key != NULL)
1.118 nicm 1091: wp->mode->key(wp, c, s, key, m);
1.27 nicm 1092: return;
1.30 nicm 1093: }
1.27 nicm 1094:
1.113 nicm 1095: if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30 nicm 1096: return;
1.113 nicm 1097:
1.118 nicm 1098: input_key(wp, key, m);
1099:
1100: if (KEYC_IS_MOUSE(key))
1101: return;
1.27 nicm 1102: if (options_get_number(&wp->window->options, "synchronize-panes")) {
1103: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1104: if (wp2 == wp || wp2->mode != NULL)
1105: continue;
1106: if (wp2->fd != -1 && window_pane_visible(wp2))
1.118 nicm 1107: input_key(wp2, key, NULL);
1.27 nicm 1108: }
1109: }
1.10 nicm 1110: }
1111:
1112: int
1113: window_pane_visible(struct window_pane *wp)
1114: {
1115: struct window *w = wp->window;
1116:
1.93 nicm 1117: if (wp->layout_cell == NULL)
1118: return (0);
1.10 nicm 1119: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1120: return (0);
1121: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1122: return (0);
1123: return (1);
1.1 nicm 1124: }
1125:
1126: char *
1.108 nicm 1127: window_pane_search(struct window_pane *wp, const char *searchstr,
1128: u_int *lineno)
1.1 nicm 1129: {
1.4 nicm 1130: struct screen *s = &wp->base;
1.5 nicm 1131: char *newsearchstr, *line, *msg;
1.4 nicm 1132: u_int i;
1133:
1.5 nicm 1134: msg = NULL;
1135: xasprintf(&newsearchstr, "*%s*", searchstr);
1136:
1.4 nicm 1137: for (i = 0; i < screen_size_y(s); i++) {
1138: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 1139: if (fnmatch(newsearchstr, line, 0) == 0) {
1140: msg = line;
1141: if (lineno != NULL)
1142: *lineno = i;
1143: break;
1144: }
1.82 nicm 1145: free(line);
1.4 nicm 1146: }
1.5 nicm 1147:
1.82 nicm 1148: free(newsearchstr);
1.5 nicm 1149: return (msg);
1.45 nicm 1150: }
1151:
1.109 nicm 1152: /* Get MRU pane from a list. */
1153: struct window_pane *
1.126 nicm 1154: window_pane_choose_best(struct window_pane **list, u_int size)
1.109 nicm 1155: {
1156: struct window_pane *next, *best;
1157: u_int i;
1158:
1.126 nicm 1159: if (size == 0)
1.109 nicm 1160: return (NULL);
1161:
1.126 nicm 1162: best = list[0];
1163: for (i = 1; i < size; i++) {
1164: next = list[i];
1.109 nicm 1165: if (next->active_point > best->active_point)
1166: best = next;
1167: }
1168: return (best);
1169: }
1170:
1171: /*
1172: * Find the pane directly above another. We build a list of those adjacent to
1173: * top edge and then choose the best.
1174: */
1.45 nicm 1175: struct window_pane *
1176: window_pane_find_up(struct window_pane *wp)
1177: {
1.126 nicm 1178: struct window_pane *next, *best, **list;
1179: u_int edge, left, right, end, size;
1.109 nicm 1180: int found;
1.45 nicm 1181:
1182: if (wp == NULL || !window_pane_visible(wp))
1183: return (NULL);
1.126 nicm 1184:
1185: list = NULL;
1186: size = 0;
1.109 nicm 1187:
1188: edge = wp->yoff;
1189: if (edge == 0)
1190: edge = wp->window->sy + 1;
1.45 nicm 1191:
1192: left = wp->xoff;
1.109 nicm 1193: right = wp->xoff + wp->sx;
1.45 nicm 1194:
1.109 nicm 1195: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1196: if (next == wp || !window_pane_visible(next))
1.45 nicm 1197: continue;
1.109 nicm 1198: if (next->yoff + next->sy + 1 != edge)
1.45 nicm 1199: continue;
1.109 nicm 1200: end = next->xoff + next->sx - 1;
1201:
1202: found = 0;
1203: if (next->xoff < left && end > right)
1204: found = 1;
1205: else if (next->xoff >= left && next->xoff <= right)
1206: found = 1;
1207: else if (end >= left && end <= right)
1208: found = 1;
1.126 nicm 1209: if (!found)
1210: continue;
1211: list = xreallocarray(list, size + 1, sizeof *list);
1212: list[size++] = next;
1.109 nicm 1213: }
1214:
1.126 nicm 1215: best = window_pane_choose_best(list, size);
1216: free(list);
1.109 nicm 1217: return (best);
1.45 nicm 1218: }
1219:
1220: /* Find the pane directly below another. */
1221: struct window_pane *
1222: window_pane_find_down(struct window_pane *wp)
1223: {
1.126 nicm 1224: struct window_pane *next, *best, **list;
1225: u_int edge, left, right, end, size;
1.109 nicm 1226: int found;
1.45 nicm 1227:
1228: if (wp == NULL || !window_pane_visible(wp))
1229: return (NULL);
1.126 nicm 1230:
1231: list = NULL;
1232: size = 0;
1.109 nicm 1233:
1234: edge = wp->yoff + wp->sy + 1;
1235: if (edge >= wp->window->sy)
1236: edge = 0;
1.45 nicm 1237:
1238: left = wp->xoff;
1.109 nicm 1239: right = wp->xoff + wp->sx;
1.45 nicm 1240:
1.109 nicm 1241: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1242: if (next == wp || !window_pane_visible(next))
1.45 nicm 1243: continue;
1.109 nicm 1244: if (next->yoff != edge)
1.45 nicm 1245: continue;
1.109 nicm 1246: end = next->xoff + next->sx - 1;
1247:
1248: found = 0;
1249: if (next->xoff < left && end > right)
1250: found = 1;
1251: else if (next->xoff >= left && next->xoff <= right)
1252: found = 1;
1253: else if (end >= left && end <= right)
1254: found = 1;
1.126 nicm 1255: if (!found)
1256: continue;
1257: list = xreallocarray(list, size + 1, sizeof *list);
1258: list[size++] = next;
1.45 nicm 1259: }
1.109 nicm 1260:
1.126 nicm 1261: best = window_pane_choose_best(list, size);
1262: free(list);
1.109 nicm 1263: return (best);
1.45 nicm 1264: }
1265:
1.109 nicm 1266: /* Find the pane directly to the left of another. */
1.45 nicm 1267: struct window_pane *
1268: window_pane_find_left(struct window_pane *wp)
1269: {
1.126 nicm 1270: struct window_pane *next, *best, **list;
1271: u_int edge, top, bottom, end, size;
1.109 nicm 1272: int found;
1.45 nicm 1273:
1274: if (wp == NULL || !window_pane_visible(wp))
1275: return (NULL);
1.126 nicm 1276:
1277: list = NULL;
1278: size = 0;
1.109 nicm 1279:
1280: edge = wp->xoff;
1281: if (edge == 0)
1282: edge = wp->window->sx + 1;
1.45 nicm 1283:
1284: top = wp->yoff;
1.109 nicm 1285: bottom = wp->yoff + wp->sy;
1.45 nicm 1286:
1.109 nicm 1287: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1288: if (next == wp || !window_pane_visible(next))
1.45 nicm 1289: continue;
1.109 nicm 1290: if (next->xoff + next->sx + 1 != edge)
1.45 nicm 1291: continue;
1.109 nicm 1292: end = next->yoff + next->sy - 1;
1293:
1294: found = 0;
1295: if (next->yoff < top && end > bottom)
1296: found = 1;
1297: else if (next->yoff >= top && next->yoff <= bottom)
1298: found = 1;
1299: else if (end >= top && end <= bottom)
1300: found = 1;
1.126 nicm 1301: if (!found)
1302: continue;
1303: list = xreallocarray(list, size + 1, sizeof *list);
1304: list[size++] = next;
1.45 nicm 1305: }
1.109 nicm 1306:
1.126 nicm 1307: best = window_pane_choose_best(list, size);
1308: free(list);
1.109 nicm 1309: return (best);
1.45 nicm 1310: }
1311:
1.109 nicm 1312: /* Find the pane directly to the right of another. */
1.45 nicm 1313: struct window_pane *
1314: window_pane_find_right(struct window_pane *wp)
1315: {
1.126 nicm 1316: struct window_pane *next, *best, **list;
1317: u_int edge, top, bottom, end, size;
1.109 nicm 1318: int found;
1.45 nicm 1319:
1320: if (wp == NULL || !window_pane_visible(wp))
1321: return (NULL);
1.126 nicm 1322:
1323: list = NULL;
1324: size = 0;
1.109 nicm 1325:
1326: edge = wp->xoff + wp->sx + 1;
1327: if (edge >= wp->window->sx)
1328: edge = 0;
1.45 nicm 1329:
1330: top = wp->yoff;
1.109 nicm 1331: bottom = wp->yoff + wp->sy;
1.45 nicm 1332:
1.109 nicm 1333: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1334: if (next == wp || !window_pane_visible(next))
1.45 nicm 1335: continue;
1.109 nicm 1336: if (next->xoff != edge)
1.45 nicm 1337: continue;
1.109 nicm 1338: end = next->yoff + next->sy - 1;
1339:
1340: found = 0;
1341: if (next->yoff < top && end > bottom)
1342: found = 1;
1343: else if (next->yoff >= top && next->yoff <= bottom)
1344: found = 1;
1345: else if (end >= top && end <= bottom)
1346: found = 1;
1.126 nicm 1347: if (!found)
1348: continue;
1349: list = xreallocarray(list, size + 1, sizeof *list);
1350: list[size++] = next;
1.109 nicm 1351: }
1352:
1.126 nicm 1353: best = window_pane_choose_best(list, size);
1354: free(list);
1.109 nicm 1355: return (best);
1.81 nicm 1356: }
1357:
1358: /* Clear alert flags for a winlink */
1359: void
1360: winlink_clear_flags(struct winlink *wl)
1361: {
1362: struct session *s;
1.122 nicm 1363: struct winlink *wl_loop;
1.81 nicm 1364:
1.122 nicm 1365: RB_FOREACH(s, sessions, &sessions) {
1366: RB_FOREACH(wl_loop, winlinks, &s->windows) {
1367: if (wl_loop->window != wl->window)
1.81 nicm 1368: continue;
1.122 nicm 1369: if ((wl_loop->flags & WINLINK_ALERTFLAGS) == 0)
1.81 nicm 1370: continue;
1371:
1.122 nicm 1372: wl_loop->flags &= ~WINLINK_ALERTFLAGS;
1373: wl_loop->window->flags &= ~WINDOW_ALERTFLAGS;
1.81 nicm 1374: server_status_session(s);
1375: }
1376: }
1.1 nicm 1377: }