Annotation of src/usr.bin/tmux/window.c, Revision 1.174
1.174 ! nicm 1: /* $OpenBSD: window.c,v 1.173 2016/10/18 07:38:16 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.14 nicm 772:
773: wp->layout_cell = NULL;
1.1 nicm 774:
775: wp->xoff = 0;
1.42 nicm 776: wp->yoff = 0;
1.1 nicm 777:
778: wp->sx = sx;
779: wp->sy = sy;
780:
1.32 nicm 781: wp->pipe_fd = -1;
782: wp->pipe_off = 0;
1.36 nicm 783: wp->pipe_event = NULL;
1.32 nicm 784:
1.9 nicm 785: wp->saved_grid = NULL;
1.117 nicm 786:
787: memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
1.9 nicm 788:
1.1 nicm 789: screen_init(&wp->base, sx, sy, hlimit);
790: wp->screen = &wp->base;
1.159 nicm 791:
792: screen_init(&wp->status_screen, 1, 1, 0);
1.140 nicm 793:
794: if (gethostname(host, sizeof host) == 0)
795: screen_set_title(&wp->base, host);
1.1 nicm 796:
797: input_init(wp);
798:
799: return (wp);
800: }
801:
1.169 nicm 802: static void
1.1 nicm 803: window_pane_destroy(struct window_pane *wp)
804: {
1.55 nicm 805: window_pane_reset_mode(wp);
806:
1.37 nicm 807: if (wp->fd != -1) {
1.70 nicm 808: bufferevent_free(wp->event);
1.1 nicm 809: close(wp->fd);
1.37 nicm 810: }
1.1 nicm 811:
812: input_free(wp);
813:
814: screen_free(&wp->base);
1.9 nicm 815: if (wp->saved_grid != NULL)
816: grid_destroy(wp->saved_grid);
1.1 nicm 817:
1.32 nicm 818: if (wp->pipe_fd != -1) {
1.70 nicm 819: bufferevent_free(wp->pipe_event);
1.32 nicm 820: close(wp->pipe_fd);
821: }
1.167 nicm 822:
823: if (event_initialized(&wp->resize_timer))
824: event_del(&wp->resize_timer);
1.32 nicm 825:
1.64 nicm 826: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
827:
1.147 nicm 828: free((void *)wp->cwd);
1.82 nicm 829: free(wp->shell);
1.110 nicm 830: cmd_free_argv(wp->argc, wp->argv);
1.82 nicm 831: free(wp);
1.1 nicm 832: }
833:
1.166 nicm 834: static void
835: window_pane_set_watermark(struct window_pane *wp, size_t size)
836: {
837: wp->wmark_hits = 0;
838: wp->wmark_size = size;
839: bufferevent_setwatermark(wp->event, EV_READ, 0, size);
840: }
841:
1.1 nicm 842: int
1.110 nicm 843: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
1.147 nicm 844: const char *path, const char *shell, const char *cwd, struct environ *env,
1.110 nicm 845: struct termios *tio, char **cause)
1.1 nicm 846: {
1.47 nicm 847: struct winsize ws;
1.150 nicm 848: char *argv0, *cmd, **argvp;
1.147 nicm 849: const char *ptr, *first, *home;
1.47 nicm 850: struct termios tio2;
1.110 nicm 851: int i;
1.1 nicm 852:
1.37 nicm 853: if (wp->fd != -1) {
1.70 nicm 854: bufferevent_free(wp->event);
1.1 nicm 855: close(wp->fd);
1.37 nicm 856: }
1.110 nicm 857: if (argc > 0) {
858: cmd_free_argv(wp->argc, wp->argv);
859: wp->argc = argc;
860: wp->argv = cmd_copy_argv(argc, argv);
1.1 nicm 861: }
1.23 nicm 862: if (shell != NULL) {
1.82 nicm 863: free(wp->shell);
1.23 nicm 864: wp->shell = xstrdup(shell);
865: }
1.147 nicm 866: if (cwd != NULL) {
867: free((void *)wp->cwd);
868: wp->cwd = xstrdup(cwd);
1.1 nicm 869: }
1.91 nicm 870:
1.110 nicm 871: cmd = cmd_stringify_argv(wp->argc, wp->argv);
872: log_debug("spawn: %s -- %s", wp->shell, cmd);
873: for (i = 0; i < wp->argc; i++)
874: log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
1.165 nicm 875: environ_log(env, "spawn: ");
1.1 nicm 876:
877: memset(&ws, 0, sizeof ws);
878: ws.ws_col = screen_size_x(&wp->base);
879: ws.ws_row = screen_size_y(&wp->base);
880:
1.42 nicm 881: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 882: case -1:
883: wp->fd = -1;
884: xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110 nicm 885: free(cmd);
1.1 nicm 886: return (-1);
887: case 0:
1.147 nicm 888: if (chdir(wp->cwd) != 0) {
889: if ((home = find_home()) == NULL || chdir(home) != 0)
890: chdir("/");
891: }
1.25 nicm 892:
893: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
894: fatal("tcgetattr failed");
895: if (tio != NULL)
896: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
897: tio2.c_cc[VERASE] = '\177';
898: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
899: fatal("tcgetattr failed");
1.18 nicm 900:
1.56 nicm 901: closefrom(STDERR_FILENO + 1);
902:
1.107 nicm 903: if (path != NULL)
1.150 nicm 904: environ_set(env, "PATH", "%s", path);
905: environ_set(env, "TMUX_PANE", "%%%u", wp->id);
1.47 nicm 906: environ_push(env);
1.18 nicm 907:
1.54 nicm 908: clear_signals(1);
1.1 nicm 909: log_close();
910:
1.80 nicm 911: setenv("SHELL", wp->shell, 1);
912: ptr = strrchr(wp->shell, '/');
913:
1.110 nicm 914: /*
915: * If given one argument, assume it should be passed to sh -c;
916: * with more than one argument, use execvp(). If there is no
917: * arguments, create a login shell.
918: */
919: if (wp->argc > 0) {
920: if (wp->argc != 1) {
921: /* Copy to ensure argv ends in NULL. */
922: argvp = cmd_copy_argv(wp->argc, wp->argv);
923: execvp(argvp[0], argvp);
924: fatal("execvp failed");
925: }
926: first = wp->argv[0];
927:
1.80 nicm 928: if (ptr != NULL && *(ptr + 1) != '\0')
929: xasprintf(&argv0, "%s", ptr + 1);
930: else
931: xasprintf(&argv0, "%s", wp->shell);
1.110 nicm 932: execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8 nicm 933: fatal("execl failed");
934: }
1.23 nicm 935: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 936: xasprintf(&argv0, "-%s", ptr + 1);
937: else
1.23 nicm 938: xasprintf(&argv0, "-%s", wp->shell);
1.110 nicm 939: execl(wp->shell, argv0, (char *)NULL);
1.1 nicm 940: fatal("execl failed");
941: }
942:
1.62 nicm 943: setblocking(wp->fd, 0);
944:
1.110 nicm 945: wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
946: window_pane_error_callback, wp);
1.131 nicm 947:
1.166 nicm 948: window_pane_set_watermark(wp, READ_FAST_SIZE);
1.37 nicm 949: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 950:
1.110 nicm 951: free(cmd);
1.1 nicm 952: return (0);
953: }
954:
1.166 nicm 955: static void
1.149 nicm 956: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37 nicm 957: {
1.131 nicm 958: struct window_pane *wp = data;
959: struct evbuffer *evb = wp->event->input;
1.166 nicm 960: size_t size = EVBUFFER_LENGTH(evb);
1.131 nicm 961: char *new_data;
1.158 nicm 962: size_t new_size;
1.131 nicm 963:
1.166 nicm 964: if (wp->wmark_size == READ_FAST_SIZE) {
965: if (size > READ_FULL_SIZE)
966: wp->wmark_hits++;
967: if (wp->wmark_hits == READ_CHANGE_HITS)
968: window_pane_set_watermark(wp, READ_SLOW_SIZE);
969: } else if (wp->wmark_size == READ_SLOW_SIZE) {
970: if (size < READ_EMPTY_SIZE)
971: wp->wmark_hits++;
972: if (wp->wmark_hits == READ_CHANGE_HITS)
973: window_pane_set_watermark(wp, READ_FAST_SIZE);
974: }
975: log_debug("%%%u has %zu bytes (of %u, %u hits)", wp->id, size,
976: wp->wmark_size, wp->wmark_hits);
1.131 nicm 977:
1.166 nicm 978: new_size = size - wp->pipe_off;
1.46 nicm 979: if (wp->pipe_fd != -1 && new_size > 0) {
1.155 nicm 980: new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
1.46 nicm 981: bufferevent_write(wp->pipe_event, new_data, new_size);
982: }
983:
984: input_parse(wp);
1.37 nicm 985:
1.173 nicm 986: wp->pipe_off = EVBUFFER_LENGTH(evb);
1.37 nicm 987: }
988:
1.166 nicm 989: static void
1.149 nicm 990: window_pane_error_callback(__unused struct bufferevent *bufev,
991: __unused short what, void *data)
1.37 nicm 992: {
993: struct window_pane *wp = data;
994:
1.153 nicm 995: server_destroy_pane(wp, 1);
1.37 nicm 996: }
997:
998: void
1.1 nicm 999: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
1000: {
1001: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 1002: return;
1.1 nicm 1003: wp->sx = sx;
1004: wp->sy = sy;
1005:
1.89 nicm 1006: screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1 nicm 1007: if (wp->mode != NULL)
1008: wp->mode->resize(wp, sx, sy);
1.95 nicm 1009:
1010: wp->flags |= PANE_RESIZE;
1.44 nicm 1011: }
1012:
1013: /*
1014: * Enter alternative screen mode. A copy of the visible screen is saved and the
1015: * history is not updated
1016: */
1017: void
1.87 nicm 1018: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
1019: int cursor)
1.44 nicm 1020: {
1021: struct screen *s = &wp->base;
1022: u_int sx, sy;
1023:
1024: if (wp->saved_grid != NULL)
1025: return;
1.146 nicm 1026: if (!options_get_number(wp->window->options, "alternate-screen"))
1.44 nicm 1027: return;
1028: sx = screen_size_x(s);
1029: sy = screen_size_y(s);
1030:
1031: wp->saved_grid = grid_create(sx, sy, 0);
1032: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87 nicm 1033: if (cursor) {
1034: wp->saved_cx = s->cx;
1035: wp->saved_cy = s->cy;
1036: }
1.44 nicm 1037: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
1038:
1.170 nicm 1039: grid_view_clear(s->grid, 0, 0, sx, sy, 8);
1.44 nicm 1040:
1041: wp->base.grid->flags &= ~GRID_HISTORY;
1042:
1043: wp->flags |= PANE_REDRAW;
1044: }
1045:
1046: /* Exit alternate screen mode and restore the copied grid. */
1047: void
1.87 nicm 1048: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
1049: int cursor)
1.44 nicm 1050: {
1051: struct screen *s = &wp->base;
1052: u_int sx, sy;
1053:
1054: if (wp->saved_grid == NULL)
1055: return;
1.146 nicm 1056: if (!options_get_number(wp->window->options, "alternate-screen"))
1.44 nicm 1057: return;
1058: sx = screen_size_x(s);
1059: sy = screen_size_y(s);
1060:
1061: /*
1062: * If the current size is bigger, temporarily resize to the old size
1063: * before copying back.
1064: */
1065: if (sy > wp->saved_grid->sy)
1.89 nicm 1066: screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44 nicm 1067:
1068: /* Restore the grid, cursor position and cell. */
1069: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87 nicm 1070: if (cursor)
1071: s->cx = wp->saved_cx;
1.44 nicm 1072: if (s->cx > screen_size_x(s) - 1)
1073: s->cx = screen_size_x(s) - 1;
1.87 nicm 1074: if (cursor)
1075: s->cy = wp->saved_cy;
1.44 nicm 1076: if (s->cy > screen_size_y(s) - 1)
1077: s->cy = screen_size_y(s) - 1;
1078: memcpy(gc, &wp->saved_cell, sizeof *gc);
1079:
1080: /*
1081: * Turn history back on (so resize can use it) and then resize back to
1082: * the current size.
1083: */
1084: wp->base.grid->flags |= GRID_HISTORY;
1.89 nicm 1085: if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1086: screen_resize(s, sx, sy, 1);
1.44 nicm 1087:
1088: grid_destroy(wp->saved_grid);
1089: wp->saved_grid = NULL;
1090:
1091: wp->flags |= PANE_REDRAW;
1.1 nicm 1092: }
1093:
1.162 nicm 1094: static void
1095: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
1096: {
1097: struct window_pane *wp = arg;
1098: struct timeval tv = { .tv_sec = 10 };
1099: int n = 0;
1100:
1101: evtimer_del(&wp->modetimer);
1102: evtimer_add(&wp->modetimer, &tv);
1103:
1104: log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
1105:
1106: if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
1107: if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
1108: window_pane_reset_mode(wp);
1109: }
1110: }
1111:
1.1 nicm 1112: int
1113: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1114: {
1115: struct screen *s;
1.162 nicm 1116: struct timeval tv = { .tv_sec = 10 };
1.1 nicm 1117:
1.15 nicm 1118: if (wp->mode != NULL)
1.1 nicm 1119: return (1);
1120: wp->mode = mode;
1121:
1.162 nicm 1122: wp->modelast = time(NULL);
1123: evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
1124: evtimer_add(&wp->modetimer, &tv);
1125:
1.1 nicm 1126: if ((s = wp->mode->init(wp)) != NULL)
1127: wp->screen = s;
1.142 nicm 1128: wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157 nicm 1129:
1130: server_status_window(wp->window);
1.1 nicm 1131: return (0);
1132: }
1133:
1134: void
1135: window_pane_reset_mode(struct window_pane *wp)
1136: {
1137: if (wp->mode == NULL)
1138: return;
1139:
1.162 nicm 1140: evtimer_del(&wp->modetimer);
1141:
1.1 nicm 1142: wp->mode->free(wp);
1143: wp->mode = NULL;
1.168 nicm 1144: wp->modeprefix = 1;
1.1 nicm 1145:
1146: wp->screen = &wp->base;
1.142 nicm 1147: wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157 nicm 1148:
1149: server_status_window(wp->window);
1.1 nicm 1150: }
1151:
1152: void
1.118 nicm 1153: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.148 nicm 1154: key_code key, struct mouse_event *m)
1.1 nicm 1155: {
1.27 nicm 1156: struct window_pane *wp2;
1.3 nicm 1157:
1.118 nicm 1158: if (KEYC_IS_MOUSE(key) && m == NULL)
1159: return;
1160:
1.1 nicm 1161: if (wp->mode != NULL) {
1.162 nicm 1162: wp->modelast = time(NULL);
1.1 nicm 1163: if (wp->mode->key != NULL)
1.118 nicm 1164: wp->mode->key(wp, c, s, key, m);
1.27 nicm 1165: return;
1.30 nicm 1166: }
1.27 nicm 1167:
1.113 nicm 1168: if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30 nicm 1169: return;
1.113 nicm 1170:
1.118 nicm 1171: input_key(wp, key, m);
1172:
1173: if (KEYC_IS_MOUSE(key))
1174: return;
1.146 nicm 1175: if (options_get_number(wp->window->options, "synchronize-panes")) {
1.27 nicm 1176: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1177: if (wp2 == wp || wp2->mode != NULL)
1178: continue;
1.154 nicm 1179: if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF)
1180: continue;
1181: if (window_pane_visible(wp2))
1.118 nicm 1182: input_key(wp2, key, NULL);
1.27 nicm 1183: }
1184: }
1.10 nicm 1185: }
1186:
1187: int
1188: window_pane_visible(struct window_pane *wp)
1189: {
1190: struct window *w = wp->window;
1191:
1.93 nicm 1192: if (wp->layout_cell == NULL)
1193: return (0);
1.10 nicm 1194: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1195: return (0);
1196: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1197: return (0);
1198: return (1);
1.1 nicm 1199: }
1200:
1201: char *
1.108 nicm 1202: window_pane_search(struct window_pane *wp, const char *searchstr,
1203: u_int *lineno)
1.1 nicm 1204: {
1.4 nicm 1205: struct screen *s = &wp->base;
1.5 nicm 1206: char *newsearchstr, *line, *msg;
1.4 nicm 1207: u_int i;
1208:
1.5 nicm 1209: msg = NULL;
1210: xasprintf(&newsearchstr, "*%s*", searchstr);
1211:
1.4 nicm 1212: for (i = 0; i < screen_size_y(s); i++) {
1213: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 1214: if (fnmatch(newsearchstr, line, 0) == 0) {
1215: msg = line;
1216: if (lineno != NULL)
1217: *lineno = i;
1218: break;
1219: }
1.82 nicm 1220: free(line);
1.4 nicm 1221: }
1.5 nicm 1222:
1.82 nicm 1223: free(newsearchstr);
1.5 nicm 1224: return (msg);
1.45 nicm 1225: }
1226:
1.109 nicm 1227: /* Get MRU pane from a list. */
1.166 nicm 1228: static struct window_pane *
1.126 nicm 1229: window_pane_choose_best(struct window_pane **list, u_int size)
1.109 nicm 1230: {
1231: struct window_pane *next, *best;
1232: u_int i;
1233:
1.126 nicm 1234: if (size == 0)
1.109 nicm 1235: return (NULL);
1236:
1.126 nicm 1237: best = list[0];
1238: for (i = 1; i < size; i++) {
1239: next = list[i];
1.109 nicm 1240: if (next->active_point > best->active_point)
1241: best = next;
1242: }
1243: return (best);
1244: }
1245:
1246: /*
1247: * Find the pane directly above another. We build a list of those adjacent to
1248: * top edge and then choose the best.
1249: */
1.45 nicm 1250: struct window_pane *
1251: window_pane_find_up(struct window_pane *wp)
1252: {
1.126 nicm 1253: struct window_pane *next, *best, **list;
1254: u_int edge, left, right, end, size;
1.109 nicm 1255: int found;
1.45 nicm 1256:
1257: if (wp == NULL || !window_pane_visible(wp))
1258: return (NULL);
1.126 nicm 1259:
1260: list = NULL;
1261: size = 0;
1.109 nicm 1262:
1263: edge = wp->yoff;
1264: if (edge == 0)
1265: edge = wp->window->sy + 1;
1.45 nicm 1266:
1267: left = wp->xoff;
1.109 nicm 1268: right = wp->xoff + wp->sx;
1.45 nicm 1269:
1.109 nicm 1270: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1271: if (next == wp || !window_pane_visible(next))
1.45 nicm 1272: continue;
1.109 nicm 1273: if (next->yoff + next->sy + 1 != edge)
1.45 nicm 1274: continue;
1.109 nicm 1275: end = next->xoff + next->sx - 1;
1276:
1277: found = 0;
1278: if (next->xoff < left && end > right)
1279: found = 1;
1280: else if (next->xoff >= left && next->xoff <= right)
1281: found = 1;
1282: else if (end >= left && end <= right)
1283: found = 1;
1.126 nicm 1284: if (!found)
1285: continue;
1286: list = xreallocarray(list, size + 1, sizeof *list);
1287: list[size++] = next;
1.109 nicm 1288: }
1289:
1.126 nicm 1290: best = window_pane_choose_best(list, size);
1291: free(list);
1.109 nicm 1292: return (best);
1.45 nicm 1293: }
1294:
1295: /* Find the pane directly below another. */
1296: struct window_pane *
1297: window_pane_find_down(struct window_pane *wp)
1298: {
1.126 nicm 1299: struct window_pane *next, *best, **list;
1300: u_int edge, left, right, end, size;
1.109 nicm 1301: int found;
1.45 nicm 1302:
1303: if (wp == NULL || !window_pane_visible(wp))
1304: return (NULL);
1.126 nicm 1305:
1306: list = NULL;
1307: size = 0;
1.109 nicm 1308:
1309: edge = wp->yoff + wp->sy + 1;
1310: if (edge >= wp->window->sy)
1311: edge = 0;
1.45 nicm 1312:
1313: left = wp->xoff;
1.109 nicm 1314: right = wp->xoff + wp->sx;
1.45 nicm 1315:
1.109 nicm 1316: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1317: if (next == wp || !window_pane_visible(next))
1.45 nicm 1318: continue;
1.109 nicm 1319: if (next->yoff != edge)
1.45 nicm 1320: continue;
1.109 nicm 1321: end = next->xoff + next->sx - 1;
1322:
1323: found = 0;
1324: if (next->xoff < left && end > right)
1325: found = 1;
1326: else if (next->xoff >= left && next->xoff <= right)
1327: found = 1;
1328: else if (end >= left && end <= right)
1329: found = 1;
1.126 nicm 1330: if (!found)
1331: continue;
1332: list = xreallocarray(list, size + 1, sizeof *list);
1333: list[size++] = next;
1.45 nicm 1334: }
1.109 nicm 1335:
1.126 nicm 1336: best = window_pane_choose_best(list, size);
1337: free(list);
1.109 nicm 1338: return (best);
1.45 nicm 1339: }
1340:
1.109 nicm 1341: /* Find the pane directly to the left of another. */
1.45 nicm 1342: struct window_pane *
1343: window_pane_find_left(struct window_pane *wp)
1344: {
1.126 nicm 1345: struct window_pane *next, *best, **list;
1346: u_int edge, top, bottom, end, size;
1.109 nicm 1347: int found;
1.45 nicm 1348:
1349: if (wp == NULL || !window_pane_visible(wp))
1350: return (NULL);
1.126 nicm 1351:
1352: list = NULL;
1353: size = 0;
1.109 nicm 1354:
1355: edge = wp->xoff;
1356: if (edge == 0)
1357: edge = wp->window->sx + 1;
1.45 nicm 1358:
1359: top = wp->yoff;
1.109 nicm 1360: bottom = wp->yoff + wp->sy;
1.45 nicm 1361:
1.109 nicm 1362: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1363: if (next == wp || !window_pane_visible(next))
1.45 nicm 1364: continue;
1.109 nicm 1365: if (next->xoff + next->sx + 1 != edge)
1.45 nicm 1366: continue;
1.109 nicm 1367: end = next->yoff + next->sy - 1;
1368:
1369: found = 0;
1370: if (next->yoff < top && end > bottom)
1371: found = 1;
1372: else if (next->yoff >= top && next->yoff <= bottom)
1373: found = 1;
1374: else if (end >= top && end <= bottom)
1375: found = 1;
1.126 nicm 1376: if (!found)
1377: continue;
1378: list = xreallocarray(list, size + 1, sizeof *list);
1379: list[size++] = next;
1.45 nicm 1380: }
1.109 nicm 1381:
1.126 nicm 1382: best = window_pane_choose_best(list, size);
1383: free(list);
1.109 nicm 1384: return (best);
1.45 nicm 1385: }
1386:
1.109 nicm 1387: /* Find the pane directly to the right of another. */
1.45 nicm 1388: struct window_pane *
1389: window_pane_find_right(struct window_pane *wp)
1390: {
1.126 nicm 1391: struct window_pane *next, *best, **list;
1392: u_int edge, top, bottom, end, size;
1.109 nicm 1393: int found;
1.45 nicm 1394:
1395: if (wp == NULL || !window_pane_visible(wp))
1396: return (NULL);
1.126 nicm 1397:
1398: list = NULL;
1399: size = 0;
1.109 nicm 1400:
1401: edge = wp->xoff + wp->sx + 1;
1402: if (edge >= wp->window->sx)
1403: edge = 0;
1.45 nicm 1404:
1405: top = wp->yoff;
1.109 nicm 1406: bottom = wp->yoff + wp->sy;
1.45 nicm 1407:
1.109 nicm 1408: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1409: if (next == wp || !window_pane_visible(next))
1.45 nicm 1410: continue;
1.109 nicm 1411: if (next->xoff != edge)
1.45 nicm 1412: continue;
1.109 nicm 1413: end = next->yoff + next->sy - 1;
1414:
1415: found = 0;
1416: if (next->yoff < top && end > bottom)
1417: found = 1;
1418: else if (next->yoff >= top && next->yoff <= bottom)
1419: found = 1;
1420: else if (end >= top && end <= bottom)
1421: found = 1;
1.126 nicm 1422: if (!found)
1423: continue;
1424: list = xreallocarray(list, size + 1, sizeof *list);
1425: list[size++] = next;
1.109 nicm 1426: }
1427:
1.126 nicm 1428: best = window_pane_choose_best(list, size);
1429: free(list);
1.109 nicm 1430: return (best);
1.81 nicm 1431: }
1432:
1433: /* Clear alert flags for a winlink */
1434: void
1435: winlink_clear_flags(struct winlink *wl)
1436: {
1.174 ! nicm 1437: struct winlink *loop;
1.81 nicm 1438:
1.174 ! nicm 1439: wl->window->flags &= ~WINDOW_ALERTFLAGS;
! 1440: TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
! 1441: if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
! 1442: loop->flags &= ~WINLINK_ALERTFLAGS;
! 1443: server_status_session(loop->session);
1.81 nicm 1444: }
1445: }
1.134 nicm 1446: }
1447:
1448: int
1449: winlink_shuffle_up(struct session *s, struct winlink *wl)
1450: {
1451: int idx, last;
1452:
1453: idx = wl->idx + 1;
1454:
1455: /* Find the next free index. */
1456: for (last = idx; last < INT_MAX; last++) {
1457: if (winlink_find_by_index(&s->windows, last) == NULL)
1458: break;
1459: }
1460: if (last == INT_MAX)
1461: return (-1);
1462:
1463: /* Move everything from last - 1 to idx up a bit. */
1464: for (; last > idx; last--) {
1465: wl = winlink_find_by_index(&s->windows, last - 1);
1466: server_link_window(s, wl, s, last, 0, 0, NULL);
1467: server_unlink_window(s, wl);
1468: }
1469:
1470: return (idx);
1.1 nicm 1471: }