Annotation of src/usr.bin/tmux/window.c, Revision 1.137
1.137 ! nicm 1: /* $OpenBSD: window.c,v 1.136 2015/08/28 07:49:24 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:
280: struct window *
1.1 nicm 281: window_create1(u_int sx, u_int sy)
282: {
283: struct window *w;
284:
1.38 nicm 285: w = xcalloc(1, sizeof *w);
1.1 nicm 286: w->name = NULL;
287: w->flags = 0;
288:
289: TAILQ_INIT(&w->panes);
290: w->active = NULL;
1.14 nicm 291:
1.17 nicm 292: w->lastlayout = -1;
1.14 nicm 293: w->layout_root = NULL;
1.42 nicm 294:
1.1 nicm 295: w->sx = sx;
296: w->sy = sy;
1.133 nicm 297:
298: if (gettimeofday(&w->activity_time, NULL) != 0)
299: fatal("gettimeofday failed");
1.1 nicm 300:
1.7 nicm 301: options_init(&w->options, &global_w_options);
1.79 nicm 302: if (options_get_number(&w->options, "automatic-rename"))
303: queue_window_name(w);
1.1 nicm 304:
305: w->references = 0;
306:
1.122 nicm 307: w->id = next_window_id++;
1.129 nicm 308: RB_INSERT(windows, &windows, w);
1.122 nicm 309:
1.1 nicm 310: return (w);
311: }
312:
313: struct window *
1.110 nicm 314: window_create(const char *name, int argc, char **argv, const char *path,
1.107 nicm 315: const char *shell, int cwd, struct environ *env, struct termios *tio,
1.91 nicm 316: u_int sx, u_int sy, u_int hlimit, char **cause)
1.1 nicm 317: {
1.14 nicm 318: struct window *w;
319: struct window_pane *wp;
1.1 nicm 320:
321: w = window_create1(sx, sy);
1.16 nicm 322: wp = window_add_pane(w, hlimit);
1.93 nicm 323: layout_init(w, wp);
1.91 nicm 324:
1.110 nicm 325: if (window_pane_spawn(wp, argc, argv, path, shell, cwd, env, tio,
1.107 nicm 326: cause) != 0) {
1.1 nicm 327: window_destroy(w);
328: return (NULL);
329: }
1.91 nicm 330:
1.1 nicm 331: w->active = TAILQ_FIRST(&w->panes);
332: if (name != NULL) {
333: w->name = xstrdup(name);
334: options_set_number(&w->options, "automatic-rename", 0);
335: } else
336: w->name = default_window_name(w);
1.91 nicm 337:
1.1 nicm 338: return (w);
339: }
340:
341: void
342: window_destroy(struct window *w)
343: {
1.122 nicm 344: RB_REMOVE(windows, &windows, w);
1.1 nicm 345:
1.14 nicm 346: if (w->layout_root != NULL)
1.135 nicm 347: layout_free_cell(w->layout_root);
348: if (w->saved_layout_root != NULL)
349: layout_free_cell(w->saved_layout_root);
1.127 nicm 350: free(w->old_layout);
1.14 nicm 351:
1.73 nicm 352: if (event_initialized(&w->name_timer))
353: evtimer_del(&w->name_timer);
1.38 nicm 354:
1.1 nicm 355: options_free(&w->options);
356:
357: window_destroy_panes(w);
358:
1.82 nicm 359: free(w->name);
360: free(w);
1.84 nicm 361: }
362:
363: void
364: window_remove_ref(struct window *w)
365: {
366: if (w->references == 0)
367: fatal("bad reference count");
368: w->references--;
369: if (w->references == 0)
370: window_destroy(w);
1.72 nicm 371: }
372:
373: void
374: window_set_name(struct window *w, const char *new_name)
375: {
1.82 nicm 376: free(w->name);
1.72 nicm 377: w->name = xstrdup(new_name);
1.74 nicm 378: notify_window_renamed(w);
1.1 nicm 379: }
380:
1.15 nicm 381: void
1.1 nicm 382: window_resize(struct window *w, u_int sx, u_int sy)
383: {
384: w->sx = sx;
385: w->sy = sy;
386: }
387:
1.114 nicm 388: int
1.118 nicm 389: window_has_pane(struct window *w, struct window_pane *wp)
390: {
391: struct window_pane *wp1;
392:
393: TAILQ_FOREACH(wp1, &w->panes, entry) {
394: if (wp1 == wp)
395: return (1);
396: }
397: return (0);
398: }
399:
400: int
1.1 nicm 401: window_set_active_pane(struct window *w, struct window_pane *wp)
402: {
1.59 nicm 403: if (wp == w->active)
1.114 nicm 404: return (0);
1.58 nicm 405: w->last = w->active;
1.1 nicm 406: w->active = wp;
1.10 nicm 407: while (!window_pane_visible(w->active)) {
1.1 nicm 408: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 409: if (w->active == NULL)
410: w->active = TAILQ_LAST(&w->panes, window_panes);
411: if (w->active == wp)
1.114 nicm 412: return (1);
1.29 nicm 413: }
1.109 nicm 414: w->active->active_point = next_active_point++;
1.136 nicm 415: w->active->flags |= PANE_CHANGED;
1.114 nicm 416: return (1);
1.29 nicm 417: }
418:
1.66 nicm 419: struct window_pane *
420: window_get_active_at(struct window *w, u_int x, u_int y)
1.29 nicm 421: {
422: struct window_pane *wp;
423:
424: TAILQ_FOREACH(wp, &w->panes, entry) {
1.66 nicm 425: if (!window_pane_visible(wp))
1.29 nicm 426: continue;
1.66 nicm 427: if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29 nicm 428: continue;
1.66 nicm 429: if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29 nicm 430: continue;
1.66 nicm 431: return (wp);
432: }
433: return (NULL);
434: }
435:
436: void
437: window_set_active_at(struct window *w, u_int x, u_int y)
438: {
439: struct window_pane *wp;
440:
441: wp = window_get_active_at(w, x, y);
442: if (wp != NULL && wp != w->active)
1.29 nicm 443: window_set_active_pane(w, wp);
1.66 nicm 444: }
445:
446: struct window_pane *
447: window_find_string(struct window *w, const char *s)
448: {
449: u_int x, y;
450:
451: x = w->sx / 2;
452: y = w->sy / 2;
453:
454: if (strcasecmp(s, "top") == 0)
455: y = 0;
456: else if (strcasecmp(s, "bottom") == 0)
457: y = w->sy - 1;
458: else if (strcasecmp(s, "left") == 0)
459: x = 0;
460: else if (strcasecmp(s, "right") == 0)
461: x = w->sx - 1;
462: else if (strcasecmp(s, "top-left") == 0) {
463: x = 0;
464: y = 0;
465: } else if (strcasecmp(s, "top-right") == 0) {
466: x = w->sx - 1;
467: y = 0;
468: } else if (strcasecmp(s, "bottom-left") == 0) {
469: x = 0;
470: y = w->sy - 1;
471: } else if (strcasecmp(s, "bottom-right") == 0) {
472: x = w->sx - 1;
473: y = w->sy - 1;
474: } else
475: return (NULL);
476:
477: return (window_get_active_at(w, x, y));
1.1 nicm 478: }
479:
1.93 nicm 480: int
481: window_zoom(struct window_pane *wp)
482: {
483: struct window *w = wp->window;
484: struct window_pane *wp1;
485:
486: if (w->flags & WINDOW_ZOOMED)
487: return (-1);
488:
489: if (!window_pane_visible(wp))
490: return (-1);
1.94 nicm 491:
492: if (window_count_panes(w) == 1)
493: return (-1);
494:
1.93 nicm 495: if (w->active != wp)
496: window_set_active_pane(w, wp);
497:
498: TAILQ_FOREACH(wp1, &w->panes, entry) {
499: wp1->saved_layout_cell = wp1->layout_cell;
500: wp1->layout_cell = NULL;
501: }
502:
503: w->saved_layout_root = w->layout_root;
504: layout_init(w, wp);
505: w->flags |= WINDOW_ZOOMED;
1.115 nicm 506: notify_window_layout_changed(w);
1.93 nicm 507:
508: return (0);
509: }
510:
511: int
512: window_unzoom(struct window *w)
513: {
1.97 nicm 514: struct window_pane *wp;
1.93 nicm 515:
516: if (!(w->flags & WINDOW_ZOOMED))
517: return (-1);
518:
519: w->flags &= ~WINDOW_ZOOMED;
520: layout_free(w);
521: w->layout_root = w->saved_layout_root;
1.120 nicm 522: w->saved_layout_root = NULL;
1.93 nicm 523:
1.97 nicm 524: TAILQ_FOREACH(wp, &w->panes, entry) {
525: wp->layout_cell = wp->saved_layout_cell;
526: wp->saved_layout_cell = NULL;
1.93 nicm 527: }
528: layout_fix_panes(w, w->sx, w->sy);
1.115 nicm 529: notify_window_layout_changed(w);
1.93 nicm 530:
531: return (0);
532: }
533:
1.1 nicm 534: struct window_pane *
1.16 nicm 535: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 536: {
537: struct window_pane *wp;
538:
1.14 nicm 539: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 540: if (TAILQ_EMPTY(&w->panes))
541: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
542: else
543: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
544: return (wp);
545: }
546:
547: void
1.105 nicm 548: window_lost_pane(struct window *w, struct window_pane *wp)
1.1 nicm 549: {
1.132 nicm 550: if (wp == marked_window_pane)
551: server_clear_marked();
552:
1.57 nicm 553: if (wp == w->active) {
1.58 nicm 554: w->active = w->last;
555: w->last = NULL;
556: if (w->active == NULL) {
557: w->active = TAILQ_PREV(wp, window_panes, entry);
558: if (w->active == NULL)
559: w->active = TAILQ_NEXT(wp, entry);
560: }
561: } else if (wp == w->last)
562: w->last = NULL;
1.105 nicm 563: }
564:
565: void
566: window_remove_pane(struct window *w, struct window_pane *wp)
567: {
568: window_lost_pane(w, wp);
1.1 nicm 569:
570: TAILQ_REMOVE(&w->panes, wp, entry);
571: window_pane_destroy(wp);
572: }
573:
574: struct window_pane *
575: window_pane_at_index(struct window *w, u_int idx)
576: {
577: struct window_pane *wp;
578: u_int n;
579:
1.67 nicm 580: n = options_get_number(&w->options, "pane-base-index");
1.1 nicm 581: TAILQ_FOREACH(wp, &w->panes, entry) {
582: if (n == idx)
583: return (wp);
584: n++;
585: }
586: return (NULL);
1.53 nicm 587: }
588:
589: struct window_pane *
590: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
591: {
592: for (; n > 0; n--) {
593: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
594: wp = TAILQ_FIRST(&w->panes);
595: }
596:
597: return (wp);
598: }
599:
600: struct window_pane *
601: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
602: u_int n)
603: {
604: for (; n > 0; n--) {
605: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
606: wp = TAILQ_LAST(&w->panes, window_panes);
607: }
608:
609: return (wp);
1.13 nicm 610: }
611:
1.69 nicm 612: int
613: window_pane_index(struct window_pane *wp, u_int *i)
1.13 nicm 614: {
615: struct window_pane *wq;
1.69 nicm 616: struct window *w = wp->window;
1.13 nicm 617:
1.69 nicm 618: *i = options_get_number(&w->options, "pane-base-index");
1.13 nicm 619: TAILQ_FOREACH(wq, &w->panes, entry) {
1.69 nicm 620: if (wp == wq) {
621: return (0);
622: }
623: (*i)++;
1.13 nicm 624: }
1.69 nicm 625:
626: return (-1);
1.1 nicm 627: }
628:
629: u_int
630: window_count_panes(struct window *w)
631: {
632: struct window_pane *wp;
633: u_int n;
634:
635: n = 0;
636: TAILQ_FOREACH(wp, &w->panes, entry)
637: n++;
638: return (n);
639: }
640:
641: void
642: window_destroy_panes(struct window *w)
643: {
644: struct window_pane *wp;
645:
646: while (!TAILQ_EMPTY(&w->panes)) {
647: wp = TAILQ_FIRST(&w->panes);
648: TAILQ_REMOVE(&w->panes, wp, entry);
649: window_pane_destroy(wp);
650: }
1.61 nicm 651: }
652:
1.128 nicm 653: /* Retuns the printable flags on a window, empty string if no flags set. */
1.61 nicm 654: char *
655: window_printable_flags(struct session *s, struct winlink *wl)
656: {
1.119 nicm 657: char flags[32];
1.61 nicm 658: int pos;
659:
660: pos = 0;
661: if (wl->flags & WINLINK_ACTIVITY)
662: flags[pos++] = '#';
663: if (wl->flags & WINLINK_BELL)
664: flags[pos++] = '!';
665: if (wl->flags & WINLINK_SILENCE)
666: flags[pos++] = '~';
667: if (wl == s->curw)
668: flags[pos++] = '*';
669: if (wl == TAILQ_FIRST(&s->lastw))
670: flags[pos++] = '-';
1.132 nicm 671: if (server_check_marked() && wl == marked_winlink)
672: flags[pos++] = 'M';
1.93 nicm 673: if (wl->window->flags & WINDOW_ZOOMED)
674: flags[pos++] = 'Z';
1.61 nicm 675: flags[pos] = '\0';
676: return (xstrdup(flags));
1.1 nicm 677: }
678:
1.123 nicm 679: struct window_pane *
680: window_pane_find_by_id_str(const char *s)
681: {
682: const char *errstr;
683: u_int id;
684:
685: if (*s != '%')
686: return (NULL);
687:
688: id = strtonum(s + 1, 0, UINT_MAX, &errstr);
689: if (errstr != NULL)
690: return (NULL);
691: return (window_pane_find_by_id(id));
692: }
693:
1.64 nicm 694: struct window_pane *
695: window_pane_find_by_id(u_int id)
696: {
697: struct window_pane wp;
698:
699: wp.id = id;
700: return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
701: }
702:
1.1 nicm 703: struct window_pane *
704: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
705: {
706: struct window_pane *wp;
707:
708: wp = xcalloc(1, sizeof *wp);
709: wp->window = w;
710:
1.71 nicm 711: wp->id = next_window_pane_id++;
1.64 nicm 712: RB_INSERT(window_pane_tree, &all_window_panes, wp);
713:
1.110 nicm 714: wp->argc = 0;
715: wp->argv = NULL;
1.23 nicm 716: wp->shell = NULL;
1.99 nicm 717: wp->cwd = -1;
1.1 nicm 718:
719: wp->fd = -1;
1.37 nicm 720: wp->event = NULL;
1.1 nicm 721:
722: wp->mode = NULL;
1.14 nicm 723:
724: wp->layout_cell = NULL;
1.1 nicm 725:
726: wp->xoff = 0;
1.42 nicm 727: wp->yoff = 0;
1.1 nicm 728:
729: wp->sx = sx;
730: wp->sy = sy;
731:
1.32 nicm 732: wp->pipe_fd = -1;
733: wp->pipe_off = 0;
1.36 nicm 734: wp->pipe_event = NULL;
1.32 nicm 735:
1.9 nicm 736: wp->saved_grid = NULL;
1.117 nicm 737:
738: memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
1.9 nicm 739:
1.1 nicm 740: screen_init(&wp->base, sx, sy, hlimit);
741: wp->screen = &wp->base;
742:
743: input_init(wp);
744:
745: return (wp);
746: }
747:
748: void
749: window_pane_destroy(struct window_pane *wp)
750: {
1.55 nicm 751: window_pane_reset_mode(wp);
752:
1.131 nicm 753: if (event_initialized(&wp->timer))
754: evtimer_del(&wp->timer);
755:
1.37 nicm 756: if (wp->fd != -1) {
1.70 nicm 757: bufferevent_free(wp->event);
1.1 nicm 758: close(wp->fd);
1.37 nicm 759: }
1.1 nicm 760:
761: input_free(wp);
762:
763: screen_free(&wp->base);
1.9 nicm 764: if (wp->saved_grid != NULL)
765: grid_destroy(wp->saved_grid);
1.1 nicm 766:
1.32 nicm 767: if (wp->pipe_fd != -1) {
1.70 nicm 768: bufferevent_free(wp->pipe_event);
1.32 nicm 769: close(wp->pipe_fd);
770: }
771:
1.64 nicm 772: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
773:
1.99 nicm 774: close(wp->cwd);
1.82 nicm 775: free(wp->shell);
1.110 nicm 776: cmd_free_argv(wp->argc, wp->argv);
1.82 nicm 777: free(wp);
1.1 nicm 778: }
779:
780: int
1.110 nicm 781: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
782: const char *path, const char *shell, int cwd, struct environ *env,
783: struct termios *tio, char **cause)
1.1 nicm 784: {
1.47 nicm 785: struct winsize ws;
1.110 nicm 786: char *argv0, *cmd, **argvp, paneid[16];
787: const char *ptr, *first;
1.47 nicm 788: struct termios tio2;
1.110 nicm 789: int i;
1.1 nicm 790:
1.37 nicm 791: if (wp->fd != -1) {
1.70 nicm 792: bufferevent_free(wp->event);
1.1 nicm 793: close(wp->fd);
1.37 nicm 794: }
1.110 nicm 795: if (argc > 0) {
796: cmd_free_argv(wp->argc, wp->argv);
797: wp->argc = argc;
798: wp->argv = cmd_copy_argv(argc, argv);
1.1 nicm 799: }
1.23 nicm 800: if (shell != NULL) {
1.82 nicm 801: free(wp->shell);
1.23 nicm 802: wp->shell = xstrdup(shell);
803: }
1.99 nicm 804: if (cwd != -1) {
805: close(wp->cwd);
806: wp->cwd = dup(cwd);
1.1 nicm 807: }
1.91 nicm 808:
1.110 nicm 809: cmd = cmd_stringify_argv(wp->argc, wp->argv);
810: log_debug("spawn: %s -- %s", wp->shell, cmd);
811: for (i = 0; i < wp->argc; i++)
812: log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
1.1 nicm 813:
814: memset(&ws, 0, sizeof ws);
815: ws.ws_col = screen_size_x(&wp->base);
816: ws.ws_row = screen_size_y(&wp->base);
817:
1.42 nicm 818: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 819: case -1:
820: wp->fd = -1;
821: xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110 nicm 822: free(cmd);
1.1 nicm 823: return (-1);
824: case 0:
1.99 nicm 825: if (fchdir(wp->cwd) != 0)
1.1 nicm 826: chdir("/");
1.25 nicm 827:
828: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
829: fatal("tcgetattr failed");
830: if (tio != NULL)
831: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
832: tio2.c_cc[VERASE] = '\177';
833: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
834: fatal("tcgetattr failed");
1.18 nicm 835:
1.56 nicm 836: closefrom(STDERR_FILENO + 1);
837:
1.107 nicm 838: if (path != NULL)
839: environ_set(env, "PATH", path);
1.64 nicm 840: xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
841: environ_set(env, "TMUX_PANE", paneid);
1.47 nicm 842: environ_push(env);
1.18 nicm 843:
1.54 nicm 844: clear_signals(1);
1.1 nicm 845: log_close();
846:
1.80 nicm 847: setenv("SHELL", wp->shell, 1);
848: ptr = strrchr(wp->shell, '/');
849:
1.110 nicm 850: /*
851: * If given one argument, assume it should be passed to sh -c;
852: * with more than one argument, use execvp(). If there is no
853: * arguments, create a login shell.
854: */
855: if (wp->argc > 0) {
856: if (wp->argc != 1) {
857: /* Copy to ensure argv ends in NULL. */
858: argvp = cmd_copy_argv(wp->argc, wp->argv);
859: execvp(argvp[0], argvp);
860: fatal("execvp failed");
861: }
862: first = wp->argv[0];
863:
1.80 nicm 864: if (ptr != NULL && *(ptr + 1) != '\0')
865: xasprintf(&argv0, "%s", ptr + 1);
866: else
867: xasprintf(&argv0, "%s", wp->shell);
1.110 nicm 868: execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8 nicm 869: fatal("execl failed");
870: }
1.23 nicm 871: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 872: xasprintf(&argv0, "-%s", ptr + 1);
873: else
1.23 nicm 874: xasprintf(&argv0, "-%s", wp->shell);
1.110 nicm 875: execl(wp->shell, argv0, (char *)NULL);
1.1 nicm 876: fatal("execl failed");
877: }
878:
1.62 nicm 879: setblocking(wp->fd, 0);
880:
1.110 nicm 881: wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
882: window_pane_error_callback, wp);
1.131 nicm 883:
884: bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
1.37 nicm 885: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 886:
1.110 nicm 887: free(cmd);
1.1 nicm 888: return (0);
889: }
890:
1.15 nicm 891: void
1.131 nicm 892: window_pane_timer_callback(unused int fd, unused short events, void *data)
893: {
894: window_pane_read_callback(NULL, data);
895: }
896:
897: void
1.37 nicm 898: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
899: {
1.131 nicm 900: struct window_pane *wp = data;
901: struct evbuffer *evb = wp->event->input;
902: char *new_data;
903: size_t new_size, available;
904: struct client *c;
905: struct timeval tv;
906:
907: if (event_initialized(&wp->timer))
908: evtimer_del(&wp->timer);
909:
910: log_debug("%%%u has %zu bytes", wp->id, EVBUFFER_LENGTH(evb));
1.46 nicm 911:
1.131 nicm 912: TAILQ_FOREACH(c, &clients, entry) {
913: if (!tty_client_ready(c, wp))
914: continue;
915:
916: available = EVBUFFER_LENGTH(c->tty.event->output);
917: if (available > READ_BACKOFF)
918: goto start_timer;
919: }
920:
921: new_size = EVBUFFER_LENGTH(evb) - wp->pipe_off;
1.46 nicm 922: if (wp->pipe_fd != -1 && new_size > 0) {
1.131 nicm 923: new_data = EVBUFFER_DATA(evb);
1.46 nicm 924: bufferevent_write(wp->pipe_event, new_data, new_size);
925: }
926:
927: input_parse(wp);
1.37 nicm 928:
1.131 nicm 929: wp->pipe_off = EVBUFFER_LENGTH(evb);
1.60 nicm 930:
931: /*
932: * If we get here, we're not outputting anymore, so set the silence
933: * flag on the window.
934: */
935: wp->window->flags |= WINDOW_SILENCE;
936: if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
1.137 ! nicm 937: fatal("gettimeofday failed");
1.131 nicm 938: return;
939:
940: start_timer:
941: log_debug("%%%u backing off (%s %zu > %d)", wp->id, c->ttyname,
942: available, READ_BACKOFF);
943:
944: tv.tv_sec = 0;
945: tv.tv_usec = READ_TIME;
946:
947: evtimer_set(&wp->timer, window_pane_timer_callback, wp);
948: evtimer_add(&wp->timer, &tv);
1.37 nicm 949: }
950:
951: void
1.131 nicm 952: window_pane_error_callback(unused struct bufferevent *bufev, unused short what,
953: void *data)
1.37 nicm 954: {
955: struct window_pane *wp = data;
956:
1.39 nicm 957: server_destroy_pane(wp);
1.37 nicm 958: }
959:
960: void
1.1 nicm 961: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
962: {
963: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 964: return;
1.1 nicm 965: wp->sx = sx;
966: wp->sy = sy;
967:
1.89 nicm 968: screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1 nicm 969: if (wp->mode != NULL)
970: wp->mode->resize(wp, sx, sy);
1.95 nicm 971:
972: wp->flags |= PANE_RESIZE;
1.44 nicm 973: }
974:
975: /*
976: * Enter alternative screen mode. A copy of the visible screen is saved and the
977: * history is not updated
978: */
979: void
1.87 nicm 980: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
981: int cursor)
1.44 nicm 982: {
983: struct screen *s = &wp->base;
984: u_int sx, sy;
985:
986: if (wp->saved_grid != NULL)
987: return;
988: if (!options_get_number(&wp->window->options, "alternate-screen"))
989: return;
990: sx = screen_size_x(s);
991: sy = screen_size_y(s);
992:
993: wp->saved_grid = grid_create(sx, sy, 0);
994: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87 nicm 995: if (cursor) {
996: wp->saved_cx = s->cx;
997: wp->saved_cy = s->cy;
998: }
1.44 nicm 999: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
1000:
1001: grid_view_clear(s->grid, 0, 0, sx, sy);
1002:
1003: wp->base.grid->flags &= ~GRID_HISTORY;
1004:
1005: wp->flags |= PANE_REDRAW;
1006: }
1007:
1008: /* Exit alternate screen mode and restore the copied grid. */
1009: void
1.87 nicm 1010: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
1011: int cursor)
1.44 nicm 1012: {
1013: struct screen *s = &wp->base;
1014: u_int sx, sy;
1015:
1016: if (wp->saved_grid == NULL)
1017: return;
1018: if (!options_get_number(&wp->window->options, "alternate-screen"))
1019: return;
1020: sx = screen_size_x(s);
1021: sy = screen_size_y(s);
1022:
1023: /*
1024: * If the current size is bigger, temporarily resize to the old size
1025: * before copying back.
1026: */
1027: if (sy > wp->saved_grid->sy)
1.89 nicm 1028: screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44 nicm 1029:
1030: /* Restore the grid, cursor position and cell. */
1031: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87 nicm 1032: if (cursor)
1033: s->cx = wp->saved_cx;
1.44 nicm 1034: if (s->cx > screen_size_x(s) - 1)
1035: s->cx = screen_size_x(s) - 1;
1.87 nicm 1036: if (cursor)
1037: s->cy = wp->saved_cy;
1.44 nicm 1038: if (s->cy > screen_size_y(s) - 1)
1039: s->cy = screen_size_y(s) - 1;
1040: memcpy(gc, &wp->saved_cell, sizeof *gc);
1041:
1042: /*
1043: * Turn history back on (so resize can use it) and then resize back to
1044: * the current size.
1045: */
1046: wp->base.grid->flags |= GRID_HISTORY;
1.89 nicm 1047: if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1048: screen_resize(s, sx, sy, 1);
1.44 nicm 1049:
1050: grid_destroy(wp->saved_grid);
1051: wp->saved_grid = NULL;
1052:
1053: wp->flags |= PANE_REDRAW;
1.1 nicm 1054: }
1055:
1056: int
1057: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1058: {
1059: struct screen *s;
1060:
1.15 nicm 1061: if (wp->mode != NULL)
1.1 nicm 1062: return (1);
1063: wp->mode = mode;
1064:
1065: if ((s = wp->mode->init(wp)) != NULL)
1066: wp->screen = s;
1.34 nicm 1067: wp->flags |= PANE_REDRAW;
1.1 nicm 1068: return (0);
1069: }
1070:
1071: void
1072: window_pane_reset_mode(struct window_pane *wp)
1073: {
1074: if (wp->mode == NULL)
1075: return;
1076:
1077: wp->mode->free(wp);
1078: wp->mode = NULL;
1079:
1080: wp->screen = &wp->base;
1.34 nicm 1081: wp->flags |= PANE_REDRAW;
1.1 nicm 1082: }
1083:
1084: void
1.118 nicm 1085: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1086: int key, struct mouse_event *m)
1.1 nicm 1087: {
1.27 nicm 1088: struct window_pane *wp2;
1.3 nicm 1089:
1.118 nicm 1090: if (KEYC_IS_MOUSE(key) && m == NULL)
1091: return;
1092:
1.1 nicm 1093: if (wp->mode != NULL) {
1094: if (wp->mode->key != NULL)
1.118 nicm 1095: wp->mode->key(wp, c, s, key, m);
1.27 nicm 1096: return;
1.30 nicm 1097: }
1.27 nicm 1098:
1.113 nicm 1099: if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30 nicm 1100: return;
1.113 nicm 1101:
1.118 nicm 1102: input_key(wp, key, m);
1103:
1104: if (KEYC_IS_MOUSE(key))
1105: return;
1.27 nicm 1106: if (options_get_number(&wp->window->options, "synchronize-panes")) {
1107: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1108: if (wp2 == wp || wp2->mode != NULL)
1109: continue;
1110: if (wp2->fd != -1 && window_pane_visible(wp2))
1.118 nicm 1111: input_key(wp2, key, NULL);
1.27 nicm 1112: }
1113: }
1.10 nicm 1114: }
1115:
1116: int
1117: window_pane_visible(struct window_pane *wp)
1118: {
1119: struct window *w = wp->window;
1120:
1.93 nicm 1121: if (wp->layout_cell == NULL)
1122: return (0);
1.10 nicm 1123: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1124: return (0);
1125: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1126: return (0);
1127: return (1);
1.1 nicm 1128: }
1129:
1130: char *
1.108 nicm 1131: window_pane_search(struct window_pane *wp, const char *searchstr,
1132: u_int *lineno)
1.1 nicm 1133: {
1.4 nicm 1134: struct screen *s = &wp->base;
1.5 nicm 1135: char *newsearchstr, *line, *msg;
1.4 nicm 1136: u_int i;
1137:
1.5 nicm 1138: msg = NULL;
1139: xasprintf(&newsearchstr, "*%s*", searchstr);
1140:
1.4 nicm 1141: for (i = 0; i < screen_size_y(s); i++) {
1142: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 1143: if (fnmatch(newsearchstr, line, 0) == 0) {
1144: msg = line;
1145: if (lineno != NULL)
1146: *lineno = i;
1147: break;
1148: }
1.82 nicm 1149: free(line);
1.4 nicm 1150: }
1.5 nicm 1151:
1.82 nicm 1152: free(newsearchstr);
1.5 nicm 1153: return (msg);
1.45 nicm 1154: }
1155:
1.109 nicm 1156: /* Get MRU pane from a list. */
1157: struct window_pane *
1.126 nicm 1158: window_pane_choose_best(struct window_pane **list, u_int size)
1.109 nicm 1159: {
1160: struct window_pane *next, *best;
1161: u_int i;
1162:
1.126 nicm 1163: if (size == 0)
1.109 nicm 1164: return (NULL);
1165:
1.126 nicm 1166: best = list[0];
1167: for (i = 1; i < size; i++) {
1168: next = list[i];
1.109 nicm 1169: if (next->active_point > best->active_point)
1170: best = next;
1171: }
1172: return (best);
1173: }
1174:
1175: /*
1176: * Find the pane directly above another. We build a list of those adjacent to
1177: * top edge and then choose the best.
1178: */
1.45 nicm 1179: struct window_pane *
1180: window_pane_find_up(struct window_pane *wp)
1181: {
1.126 nicm 1182: struct window_pane *next, *best, **list;
1183: u_int edge, left, right, end, size;
1.109 nicm 1184: int found;
1.45 nicm 1185:
1186: if (wp == NULL || !window_pane_visible(wp))
1187: return (NULL);
1.126 nicm 1188:
1189: list = NULL;
1190: size = 0;
1.109 nicm 1191:
1192: edge = wp->yoff;
1193: if (edge == 0)
1194: edge = wp->window->sy + 1;
1.45 nicm 1195:
1196: left = wp->xoff;
1.109 nicm 1197: right = wp->xoff + wp->sx;
1.45 nicm 1198:
1.109 nicm 1199: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1200: if (next == wp || !window_pane_visible(next))
1.45 nicm 1201: continue;
1.109 nicm 1202: if (next->yoff + next->sy + 1 != edge)
1.45 nicm 1203: continue;
1.109 nicm 1204: end = next->xoff + next->sx - 1;
1205:
1206: found = 0;
1207: if (next->xoff < left && end > right)
1208: found = 1;
1209: else if (next->xoff >= left && next->xoff <= right)
1210: found = 1;
1211: else if (end >= left && end <= right)
1212: found = 1;
1.126 nicm 1213: if (!found)
1214: continue;
1215: list = xreallocarray(list, size + 1, sizeof *list);
1216: list[size++] = next;
1.109 nicm 1217: }
1218:
1.126 nicm 1219: best = window_pane_choose_best(list, size);
1220: free(list);
1.109 nicm 1221: return (best);
1.45 nicm 1222: }
1223:
1224: /* Find the pane directly below another. */
1225: struct window_pane *
1226: window_pane_find_down(struct window_pane *wp)
1227: {
1.126 nicm 1228: struct window_pane *next, *best, **list;
1229: u_int edge, left, right, end, size;
1.109 nicm 1230: int found;
1.45 nicm 1231:
1232: if (wp == NULL || !window_pane_visible(wp))
1233: return (NULL);
1.126 nicm 1234:
1235: list = NULL;
1236: size = 0;
1.109 nicm 1237:
1238: edge = wp->yoff + wp->sy + 1;
1239: if (edge >= wp->window->sy)
1240: edge = 0;
1.45 nicm 1241:
1242: left = wp->xoff;
1.109 nicm 1243: right = wp->xoff + wp->sx;
1.45 nicm 1244:
1.109 nicm 1245: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1246: if (next == wp || !window_pane_visible(next))
1.45 nicm 1247: continue;
1.109 nicm 1248: if (next->yoff != edge)
1.45 nicm 1249: continue;
1.109 nicm 1250: end = next->xoff + next->sx - 1;
1251:
1252: found = 0;
1253: if (next->xoff < left && end > right)
1254: found = 1;
1255: else if (next->xoff >= left && next->xoff <= right)
1256: found = 1;
1257: else if (end >= left && end <= right)
1258: found = 1;
1.126 nicm 1259: if (!found)
1260: continue;
1261: list = xreallocarray(list, size + 1, sizeof *list);
1262: list[size++] = next;
1.45 nicm 1263: }
1.109 nicm 1264:
1.126 nicm 1265: best = window_pane_choose_best(list, size);
1266: free(list);
1.109 nicm 1267: return (best);
1.45 nicm 1268: }
1269:
1.109 nicm 1270: /* Find the pane directly to the left of another. */
1.45 nicm 1271: struct window_pane *
1272: window_pane_find_left(struct window_pane *wp)
1273: {
1.126 nicm 1274: struct window_pane *next, *best, **list;
1275: u_int edge, top, bottom, end, size;
1.109 nicm 1276: int found;
1.45 nicm 1277:
1278: if (wp == NULL || !window_pane_visible(wp))
1279: return (NULL);
1.126 nicm 1280:
1281: list = NULL;
1282: size = 0;
1.109 nicm 1283:
1284: edge = wp->xoff;
1285: if (edge == 0)
1286: edge = wp->window->sx + 1;
1.45 nicm 1287:
1288: top = wp->yoff;
1.109 nicm 1289: bottom = wp->yoff + wp->sy;
1.45 nicm 1290:
1.109 nicm 1291: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1292: if (next == wp || !window_pane_visible(next))
1.45 nicm 1293: continue;
1.109 nicm 1294: if (next->xoff + next->sx + 1 != edge)
1.45 nicm 1295: continue;
1.109 nicm 1296: end = next->yoff + next->sy - 1;
1297:
1298: found = 0;
1299: if (next->yoff < top && end > bottom)
1300: found = 1;
1301: else if (next->yoff >= top && next->yoff <= bottom)
1302: found = 1;
1303: else if (end >= top && end <= bottom)
1304: found = 1;
1.126 nicm 1305: if (!found)
1306: continue;
1307: list = xreallocarray(list, size + 1, sizeof *list);
1308: list[size++] = next;
1.45 nicm 1309: }
1.109 nicm 1310:
1.126 nicm 1311: best = window_pane_choose_best(list, size);
1312: free(list);
1.109 nicm 1313: return (best);
1.45 nicm 1314: }
1315:
1.109 nicm 1316: /* Find the pane directly to the right of another. */
1.45 nicm 1317: struct window_pane *
1318: window_pane_find_right(struct window_pane *wp)
1319: {
1.126 nicm 1320: struct window_pane *next, *best, **list;
1321: u_int edge, top, bottom, end, size;
1.109 nicm 1322: int found;
1.45 nicm 1323:
1324: if (wp == NULL || !window_pane_visible(wp))
1325: return (NULL);
1.126 nicm 1326:
1327: list = NULL;
1328: size = 0;
1.109 nicm 1329:
1330: edge = wp->xoff + wp->sx + 1;
1331: if (edge >= wp->window->sx)
1332: edge = 0;
1.45 nicm 1333:
1334: top = wp->yoff;
1.109 nicm 1335: bottom = wp->yoff + wp->sy;
1.45 nicm 1336:
1.109 nicm 1337: TAILQ_FOREACH(next, &wp->window->panes, entry) {
1338: if (next == wp || !window_pane_visible(next))
1.45 nicm 1339: continue;
1.109 nicm 1340: if (next->xoff != edge)
1.45 nicm 1341: continue;
1.109 nicm 1342: end = next->yoff + next->sy - 1;
1343:
1344: found = 0;
1345: if (next->yoff < top && end > bottom)
1346: found = 1;
1347: else if (next->yoff >= top && next->yoff <= bottom)
1348: found = 1;
1349: else if (end >= top && end <= bottom)
1350: found = 1;
1.126 nicm 1351: if (!found)
1352: continue;
1353: list = xreallocarray(list, size + 1, sizeof *list);
1354: list[size++] = next;
1.109 nicm 1355: }
1356:
1.126 nicm 1357: best = window_pane_choose_best(list, size);
1358: free(list);
1.109 nicm 1359: return (best);
1.81 nicm 1360: }
1361:
1362: /* Clear alert flags for a winlink */
1363: void
1364: winlink_clear_flags(struct winlink *wl)
1365: {
1366: struct session *s;
1.122 nicm 1367: struct winlink *wl_loop;
1.81 nicm 1368:
1.122 nicm 1369: RB_FOREACH(s, sessions, &sessions) {
1370: RB_FOREACH(wl_loop, winlinks, &s->windows) {
1371: if (wl_loop->window != wl->window)
1.81 nicm 1372: continue;
1.122 nicm 1373: if ((wl_loop->flags & WINLINK_ALERTFLAGS) == 0)
1.81 nicm 1374: continue;
1375:
1.122 nicm 1376: wl_loop->flags &= ~WINLINK_ALERTFLAGS;
1377: wl_loop->window->flags &= ~WINDOW_ALERTFLAGS;
1.81 nicm 1378: server_status_session(s);
1379: }
1380: }
1.134 nicm 1381: }
1382:
1383: int
1384: winlink_shuffle_up(struct session *s, struct winlink *wl)
1385: {
1386: int idx, last;
1387:
1388: idx = wl->idx + 1;
1389:
1390: /* Find the next free index. */
1391: for (last = idx; last < INT_MAX; last++) {
1392: if (winlink_find_by_index(&s->windows, last) == NULL)
1393: break;
1394: }
1395: if (last == INT_MAX)
1396: return (-1);
1397:
1398: /* Move everything from last - 1 to idx up a bit. */
1399: for (; last > idx; last--) {
1400: wl = winlink_find_by_index(&s->windows, last - 1);
1401: server_link_window(s, wl, s, last, 0, 0, NULL);
1402: server_unlink_window(s, wl);
1403: }
1404:
1405: return (idx);
1.1 nicm 1406: }