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