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