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