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