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