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