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