Annotation of src/usr.bin/tmux/window.c, Revision 1.88
1.88 ! nicm 1: /* $OpenBSD: window.c,v 1.87 2012/11/27 20:08:42 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,
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.14 nicm 320: layout_init(w);
1.23 nicm 321: if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
1.1 nicm 322: window_destroy(w);
323: return (NULL);
324: }
325: w->active = TAILQ_FIRST(&w->panes);
326: if (name != NULL) {
327: w->name = xstrdup(name);
328: options_set_number(&w->options, "automatic-rename", 0);
329: } else
330: w->name = default_window_name(w);
331: return (w);
332: }
333:
334: void
335: window_destroy(struct window *w)
336: {
337: u_int i;
338:
339: if (window_index(w, &i) != 0)
340: fatalx("index not found");
341: ARRAY_SET(&windows, i, NULL);
342: while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
343: ARRAY_TRUNC(&windows, 1);
344:
1.14 nicm 345: if (w->layout_root != NULL)
346: layout_free(w);
347:
1.73 nicm 348: if (event_initialized(&w->name_timer))
349: evtimer_del(&w->name_timer);
1.38 nicm 350:
1.1 nicm 351: options_free(&w->options);
352:
353: window_destroy_panes(w);
354:
1.82 nicm 355: free(w->name);
356: free(w);
1.84 nicm 357: }
358:
359: void
360: window_remove_ref(struct window *w)
361: {
362: if (w->references == 0)
363: fatal("bad reference count");
364: w->references--;
365: if (w->references == 0)
366: window_destroy(w);
1.72 nicm 367: }
368:
369: void
370: window_set_name(struct window *w, const char *new_name)
371: {
1.82 nicm 372: free(w->name);
1.72 nicm 373: w->name = xstrdup(new_name);
1.74 nicm 374: notify_window_renamed(w);
1.1 nicm 375: }
376:
1.15 nicm 377: void
1.1 nicm 378: window_resize(struct window *w, u_int sx, u_int sy)
379: {
380: w->sx = sx;
381: w->sy = sy;
382: }
383:
384: void
385: window_set_active_pane(struct window *w, struct window_pane *wp)
386: {
1.59 nicm 387: if (wp == w->active)
388: return;
1.58 nicm 389: w->last = w->active;
1.1 nicm 390: w->active = wp;
1.10 nicm 391: while (!window_pane_visible(w->active)) {
1.1 nicm 392: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 393: if (w->active == NULL)
394: w->active = TAILQ_LAST(&w->panes, window_panes);
395: if (w->active == wp)
396: return;
1.29 nicm 397: }
398: }
399:
1.66 nicm 400: struct window_pane *
401: window_get_active_at(struct window *w, u_int x, u_int y)
1.29 nicm 402: {
403: struct window_pane *wp;
404:
405: TAILQ_FOREACH(wp, &w->panes, entry) {
1.66 nicm 406: if (!window_pane_visible(wp))
1.29 nicm 407: continue;
1.66 nicm 408: if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29 nicm 409: continue;
1.66 nicm 410: if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29 nicm 411: continue;
1.66 nicm 412: return (wp);
413: }
414: return (NULL);
415: }
416:
417: void
418: window_set_active_at(struct window *w, u_int x, u_int y)
419: {
420: struct window_pane *wp;
421:
422: wp = window_get_active_at(w, x, y);
423: if (wp != NULL && wp != w->active)
1.29 nicm 424: window_set_active_pane(w, wp);
1.66 nicm 425: }
426:
427: struct window_pane *
428: window_find_string(struct window *w, const char *s)
429: {
430: u_int x, y;
431:
432: x = w->sx / 2;
433: y = w->sy / 2;
434:
435: if (strcasecmp(s, "top") == 0)
436: y = 0;
437: else if (strcasecmp(s, "bottom") == 0)
438: y = w->sy - 1;
439: else if (strcasecmp(s, "left") == 0)
440: x = 0;
441: else if (strcasecmp(s, "right") == 0)
442: x = w->sx - 1;
443: else if (strcasecmp(s, "top-left") == 0) {
444: x = 0;
445: y = 0;
446: } else if (strcasecmp(s, "top-right") == 0) {
447: x = w->sx - 1;
448: y = 0;
449: } else if (strcasecmp(s, "bottom-left") == 0) {
450: x = 0;
451: y = w->sy - 1;
452: } else if (strcasecmp(s, "bottom-right") == 0) {
453: x = w->sx - 1;
454: y = w->sy - 1;
455: } else
456: return (NULL);
457:
458: return (window_get_active_at(w, x, y));
1.1 nicm 459: }
460:
461: struct window_pane *
1.16 nicm 462: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 463: {
464: struct window_pane *wp;
465:
1.14 nicm 466: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 467: if (TAILQ_EMPTY(&w->panes))
468: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
469: else
470: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
471: return (wp);
472: }
473:
474: void
475: window_remove_pane(struct window *w, struct window_pane *wp)
476: {
1.57 nicm 477: if (wp == w->active) {
1.58 nicm 478: w->active = w->last;
479: w->last = NULL;
480: if (w->active == NULL) {
481: w->active = TAILQ_PREV(wp, window_panes, entry);
482: if (w->active == NULL)
483: w->active = TAILQ_NEXT(wp, entry);
484: }
485: } else if (wp == w->last)
486: w->last = NULL;
1.1 nicm 487:
488: TAILQ_REMOVE(&w->panes, wp, entry);
489: window_pane_destroy(wp);
490: }
491:
492: struct window_pane *
493: window_pane_at_index(struct window *w, u_int idx)
494: {
495: struct window_pane *wp;
496: u_int n;
497:
1.67 nicm 498: n = options_get_number(&w->options, "pane-base-index");
1.1 nicm 499: TAILQ_FOREACH(wp, &w->panes, entry) {
500: if (n == idx)
501: return (wp);
502: n++;
503: }
504: return (NULL);
1.53 nicm 505: }
506:
507: struct window_pane *
508: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
509: {
510: for (; n > 0; n--) {
511: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
512: wp = TAILQ_FIRST(&w->panes);
513: }
514:
515: return (wp);
516: }
517:
518: struct window_pane *
519: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
520: u_int n)
521: {
522: for (; n > 0; n--) {
523: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
524: wp = TAILQ_LAST(&w->panes, window_panes);
525: }
526:
527: return (wp);
1.13 nicm 528: }
529:
1.69 nicm 530: int
531: window_pane_index(struct window_pane *wp, u_int *i)
1.13 nicm 532: {
533: struct window_pane *wq;
1.69 nicm 534: struct window *w = wp->window;
1.13 nicm 535:
1.69 nicm 536: *i = options_get_number(&w->options, "pane-base-index");
1.13 nicm 537: TAILQ_FOREACH(wq, &w->panes, entry) {
1.69 nicm 538: if (wp == wq) {
539: return (0);
540: }
541: (*i)++;
1.13 nicm 542: }
1.69 nicm 543:
544: return (-1);
1.1 nicm 545: }
546:
547: u_int
548: window_count_panes(struct window *w)
549: {
550: struct window_pane *wp;
551: u_int n;
552:
553: n = 0;
554: TAILQ_FOREACH(wp, &w->panes, entry)
555: n++;
556: return (n);
557: }
558:
559: void
560: window_destroy_panes(struct window *w)
561: {
562: struct window_pane *wp;
563:
564: while (!TAILQ_EMPTY(&w->panes)) {
565: wp = TAILQ_FIRST(&w->panes);
566: TAILQ_REMOVE(&w->panes, wp, entry);
567: window_pane_destroy(wp);
568: }
1.61 nicm 569: }
570:
571: /* Return list of printable window flag symbols. No flags is just a space. */
572: char *
573: window_printable_flags(struct session *s, struct winlink *wl)
574: {
575: char flags[BUFSIZ];
576: int pos;
577:
578: pos = 0;
579: if (wl->flags & WINLINK_ACTIVITY)
580: flags[pos++] = '#';
581: if (wl->flags & WINLINK_BELL)
582: flags[pos++] = '!';
583: if (wl->flags & WINLINK_CONTENT)
584: flags[pos++] = '+';
585: if (wl->flags & WINLINK_SILENCE)
586: flags[pos++] = '~';
587: if (wl == s->curw)
588: flags[pos++] = '*';
589: if (wl == TAILQ_FIRST(&s->lastw))
590: flags[pos++] = '-';
591: if (pos == 0)
592: flags[pos++] = ' ';
593: flags[pos] = '\0';
594: return (xstrdup(flags));
1.1 nicm 595: }
596:
1.64 nicm 597: /* Find pane in global tree by id. */
598: struct window_pane *
599: window_pane_find_by_id(u_int id)
600: {
601: struct window_pane wp;
602:
603: wp.id = id;
604: return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
605: }
606:
1.1 nicm 607: struct window_pane *
608: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
609: {
610: struct window_pane *wp;
611:
612: wp = xcalloc(1, sizeof *wp);
613: wp->window = w;
614:
1.71 nicm 615: wp->id = next_window_pane_id++;
1.64 nicm 616: RB_INSERT(window_pane_tree, &all_window_panes, wp);
617:
1.1 nicm 618: wp->cmd = NULL;
1.23 nicm 619: wp->shell = NULL;
1.1 nicm 620: wp->cwd = NULL;
621:
622: wp->fd = -1;
1.37 nicm 623: wp->event = NULL;
1.1 nicm 624:
625: wp->mode = NULL;
1.14 nicm 626:
627: wp->layout_cell = NULL;
1.1 nicm 628:
629: wp->xoff = 0;
1.42 nicm 630: wp->yoff = 0;
1.1 nicm 631:
632: wp->sx = sx;
633: wp->sy = sy;
634:
1.32 nicm 635: wp->pipe_fd = -1;
636: wp->pipe_off = 0;
1.36 nicm 637: wp->pipe_event = NULL;
1.32 nicm 638:
1.9 nicm 639: wp->saved_grid = NULL;
640:
1.1 nicm 641: screen_init(&wp->base, sx, sy, hlimit);
642: wp->screen = &wp->base;
643:
644: input_init(wp);
645:
646: return (wp);
647: }
648:
649: void
650: window_pane_destroy(struct window_pane *wp)
651: {
1.55 nicm 652: window_pane_reset_mode(wp);
653:
1.76 nicm 654: if (event_initialized(&wp->changes_timer))
655: evtimer_del(&wp->changes_timer);
1.75 nicm 656:
1.37 nicm 657: if (wp->fd != -1) {
1.70 nicm 658: bufferevent_free(wp->event);
1.1 nicm 659: close(wp->fd);
1.37 nicm 660: }
1.1 nicm 661:
662: input_free(wp);
663:
664: screen_free(&wp->base);
1.9 nicm 665: if (wp->saved_grid != NULL)
666: grid_destroy(wp->saved_grid);
1.1 nicm 667:
1.32 nicm 668: if (wp->pipe_fd != -1) {
1.70 nicm 669: bufferevent_free(wp->pipe_event);
1.32 nicm 670: close(wp->pipe_fd);
671: }
672:
1.64 nicm 673: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
674:
1.82 nicm 675: free(wp->cwd);
676: free(wp->shell);
677: free(wp->cmd);
678: free(wp);
1.1 nicm 679: }
680:
681: int
1.23 nicm 682: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21 nicm 683: const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1 nicm 684: {
1.47 nicm 685: struct winsize ws;
1.64 nicm 686: char *argv0, paneid[16];
1.47 nicm 687: const char *ptr;
688: struct termios tio2;
1.1 nicm 689:
1.37 nicm 690: if (wp->fd != -1) {
1.70 nicm 691: bufferevent_free(wp->event);
1.1 nicm 692: close(wp->fd);
1.37 nicm 693: }
1.1 nicm 694: if (cmd != NULL) {
1.82 nicm 695: free(wp->cmd);
1.1 nicm 696: wp->cmd = xstrdup(cmd);
697: }
1.23 nicm 698: if (shell != NULL) {
1.82 nicm 699: free(wp->shell);
1.23 nicm 700: wp->shell = xstrdup(shell);
701: }
1.1 nicm 702: if (cwd != NULL) {
1.82 nicm 703: free(wp->cwd);
1.1 nicm 704: wp->cwd = xstrdup(cwd);
705: }
706:
707: memset(&ws, 0, sizeof ws);
708: ws.ws_col = screen_size_x(&wp->base);
709: ws.ws_row = screen_size_y(&wp->base);
710:
1.42 nicm 711: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 712: case -1:
713: wp->fd = -1;
714: xasprintf(cause, "%s: %s", cmd, strerror(errno));
715: return (-1);
716: case 0:
717: if (chdir(wp->cwd) != 0)
718: chdir("/");
1.25 nicm 719:
720: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
721: fatal("tcgetattr failed");
722: if (tio != NULL)
723: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
724: tio2.c_cc[VERASE] = '\177';
725: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
726: fatal("tcgetattr failed");
1.18 nicm 727:
1.56 nicm 728: closefrom(STDERR_FILENO + 1);
729:
1.64 nicm 730: xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
731: environ_set(env, "TMUX_PANE", paneid);
1.47 nicm 732: environ_push(env);
1.18 nicm 733:
1.54 nicm 734: clear_signals(1);
1.1 nicm 735: log_close();
736:
1.80 nicm 737: setenv("SHELL", wp->shell, 1);
738: ptr = strrchr(wp->shell, '/');
739:
1.8 nicm 740: if (*wp->cmd != '\0') {
1.80 nicm 741: /* Use the command. */
742: if (ptr != NULL && *(ptr + 1) != '\0')
743: xasprintf(&argv0, "%s", ptr + 1);
744: else
745: xasprintf(&argv0, "%s", wp->shell);
746: execl(wp->shell, argv0, "-c", wp->cmd, (char *) NULL);
1.8 nicm 747: fatal("execl failed");
748: }
749:
750: /* No command; fork a login shell. */
1.23 nicm 751: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 752: xasprintf(&argv0, "-%s", ptr + 1);
753: else
1.23 nicm 754: xasprintf(&argv0, "-%s", wp->shell);
755: execl(wp->shell, argv0, (char *) NULL);
1.1 nicm 756: fatal("execl failed");
757: }
758:
1.62 nicm 759: setblocking(wp->fd, 0);
760:
1.37 nicm 761: wp->event = bufferevent_new(wp->fd,
762: window_pane_read_callback, NULL, window_pane_error_callback, wp);
763: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 764:
765: return (0);
1.75 nicm 766: }
767:
768: void
769: window_pane_timer_start(struct window_pane *wp)
770: {
771: struct timeval tv;
772:
773: tv.tv_sec = 0;
774: tv.tv_usec = 1000;
775:
776: evtimer_del(&wp->changes_timer);
777: evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp);
778: evtimer_add(&wp->changes_timer, &tv);
779: }
780:
781: void
782: window_pane_timer_callback(unused int fd, unused short events, void *data)
783: {
784: struct window_pane *wp = data;
785: struct window *w = wp->window;
786: u_int interval, trigger;
787:
788: interval = options_get_number(&w->options, "c0-change-interval");
789: trigger = options_get_number(&w->options, "c0-change-trigger");
790:
791: if (wp->changes_redraw++ == interval) {
792: wp->flags |= PANE_REDRAW;
793: wp->changes_redraw = 0;
794:
795: }
796:
797: if (trigger == 0 || wp->changes < trigger) {
798: wp->flags |= PANE_REDRAW;
799: wp->flags &= ~PANE_DROP;
800: } else
801: window_pane_timer_start(wp);
802: wp->changes = 0;
1.1 nicm 803: }
804:
1.41 nicm 805: /* ARGSUSED */
1.15 nicm 806: void
1.37 nicm 807: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
808: {
1.46 nicm 809: struct window_pane *wp = data;
810: char *new_data;
811: size_t new_size;
812:
813: new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
814: if (wp->pipe_fd != -1 && new_size > 0) {
815: new_data = EVBUFFER_DATA(wp->event->input);
816: bufferevent_write(wp->pipe_event, new_data, new_size);
817: }
818:
819: input_parse(wp);
1.37 nicm 820:
1.46 nicm 821: wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.60 nicm 822:
823: /*
824: * If we get here, we're not outputting anymore, so set the silence
825: * flag on the window.
826: */
827: wp->window->flags |= WINDOW_SILENCE;
828: if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
829: fatal("gettimeofday failed.");
1.37 nicm 830: }
831:
1.41 nicm 832: /* ARGSUSED */
1.37 nicm 833: void
834: window_pane_error_callback(
835: unused struct bufferevent *bufev, unused short what, void *data)
836: {
837: struct window_pane *wp = data;
838:
1.39 nicm 839: server_destroy_pane(wp);
1.37 nicm 840: }
841:
842: void
1.1 nicm 843: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
844: {
845: struct winsize ws;
846:
847: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 848: return;
1.1 nicm 849: wp->sx = sx;
850: wp->sy = sy;
851:
852: memset(&ws, 0, sizeof ws);
853: ws.ws_col = sx;
854: ws.ws_row = sy;
855:
856: screen_resize(&wp->base, sx, sy);
857: if (wp->mode != NULL)
858: wp->mode->resize(wp, sx, sy);
859:
860: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
861: fatal("ioctl failed");
1.44 nicm 862: }
863:
864: /*
865: * Enter alternative screen mode. A copy of the visible screen is saved and the
866: * history is not updated
867: */
868: void
1.87 nicm 869: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
870: int cursor)
1.44 nicm 871: {
872: struct screen *s = &wp->base;
873: u_int sx, sy;
874:
875: if (wp->saved_grid != NULL)
876: return;
877: if (!options_get_number(&wp->window->options, "alternate-screen"))
878: return;
879: sx = screen_size_x(s);
880: sy = screen_size_y(s);
881:
882: wp->saved_grid = grid_create(sx, sy, 0);
883: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87 nicm 884: if (cursor) {
885: wp->saved_cx = s->cx;
886: wp->saved_cy = s->cy;
887: }
1.44 nicm 888: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
889:
890: grid_view_clear(s->grid, 0, 0, sx, sy);
891:
892: wp->base.grid->flags &= ~GRID_HISTORY;
893:
894: wp->flags |= PANE_REDRAW;
895: }
896:
897: /* Exit alternate screen mode and restore the copied grid. */
898: void
1.87 nicm 899: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
900: int cursor)
1.44 nicm 901: {
902: struct screen *s = &wp->base;
903: u_int sx, sy;
904:
905: if (wp->saved_grid == NULL)
906: return;
907: if (!options_get_number(&wp->window->options, "alternate-screen"))
908: return;
909: sx = screen_size_x(s);
910: sy = screen_size_y(s);
911:
912: /*
913: * If the current size is bigger, temporarily resize to the old size
914: * before copying back.
915: */
916: if (sy > wp->saved_grid->sy)
917: screen_resize(s, sx, wp->saved_grid->sy);
918:
919: /* Restore the grid, cursor position and cell. */
920: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87 nicm 921: if (cursor)
922: s->cx = wp->saved_cx;
1.44 nicm 923: if (s->cx > screen_size_x(s) - 1)
924: s->cx = screen_size_x(s) - 1;
1.87 nicm 925: if (cursor)
926: s->cy = wp->saved_cy;
1.44 nicm 927: if (s->cy > screen_size_y(s) - 1)
928: s->cy = screen_size_y(s) - 1;
929: memcpy(gc, &wp->saved_cell, sizeof *gc);
930:
931: /*
932: * Turn history back on (so resize can use it) and then resize back to
933: * the current size.
934: */
935: wp->base.grid->flags |= GRID_HISTORY;
936: if (sy > wp->saved_grid->sy)
937: screen_resize(s, sx, sy);
938:
939: grid_destroy(wp->saved_grid);
940: wp->saved_grid = NULL;
941:
942: wp->flags |= PANE_REDRAW;
1.1 nicm 943: }
944:
945: int
946: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
947: {
948: struct screen *s;
949:
1.15 nicm 950: if (wp->mode != NULL)
1.1 nicm 951: return (1);
952: wp->mode = mode;
953:
954: if ((s = wp->mode->init(wp)) != NULL)
955: wp->screen = s;
1.34 nicm 956: wp->flags |= PANE_REDRAW;
1.1 nicm 957: return (0);
958: }
959:
960: void
961: window_pane_reset_mode(struct window_pane *wp)
962: {
963: if (wp->mode == NULL)
964: return;
965:
966: wp->mode->free(wp);
967: wp->mode = NULL;
968:
969: wp->screen = &wp->base;
1.34 nicm 970: wp->flags |= PANE_REDRAW;
1.1 nicm 971: }
972:
973: void
1.51 nicm 974: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1 nicm 975: {
1.27 nicm 976: struct window_pane *wp2;
977:
1.30 nicm 978: if (!window_pane_visible(wp))
1.3 nicm 979: return;
980:
1.1 nicm 981: if (wp->mode != NULL) {
982: if (wp->mode->key != NULL)
1.51 nicm 983: wp->mode->key(wp, sess, key);
1.27 nicm 984: return;
1.30 nicm 985: }
1.27 nicm 986:
1.30 nicm 987: if (wp->fd == -1)
988: return;
1.27 nicm 989: input_key(wp, key);
990: if (options_get_number(&wp->window->options, "synchronize-panes")) {
991: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
992: if (wp2 == wp || wp2->mode != NULL)
993: continue;
994: if (wp2->fd != -1 && window_pane_visible(wp2))
995: input_key(wp2, key);
996: }
997: }
1.1 nicm 998: }
999:
1000: void
1001: window_pane_mouse(
1.51 nicm 1002: struct window_pane *wp, struct session *sess, struct mouse_event *m)
1.1 nicm 1003: {
1.30 nicm 1004: if (!window_pane_visible(wp))
1.3 nicm 1005: return;
1006:
1.31 nicm 1007: if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1 nicm 1008: return;
1.31 nicm 1009: if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1 nicm 1010: return;
1.31 nicm 1011: m->x -= wp->xoff;
1012: m->y -= wp->yoff;
1.1 nicm 1013:
1014: if (wp->mode != NULL) {
1.65 nicm 1015: if (wp->mode->mouse != NULL &&
1016: options_get_number(&wp->window->options, "mode-mouse"))
1.51 nicm 1017: wp->mode->mouse(wp, sess, m);
1.30 nicm 1018: } else if (wp->fd != -1)
1.86 nicm 1019: input_mouse(wp, sess, m);
1.10 nicm 1020: }
1021:
1022: int
1023: window_pane_visible(struct window_pane *wp)
1024: {
1025: struct window *w = wp->window;
1026:
1027: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1028: return (0);
1029: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1030: return (0);
1031: return (1);
1.1 nicm 1032: }
1033:
1034: char *
1.5 nicm 1035: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 1036: {
1.4 nicm 1037: struct screen *s = &wp->base;
1.5 nicm 1038: char *newsearchstr, *line, *msg;
1.4 nicm 1039: u_int i;
1040:
1.5 nicm 1041: msg = NULL;
1042: xasprintf(&newsearchstr, "*%s*", searchstr);
1043:
1.4 nicm 1044: for (i = 0; i < screen_size_y(s); i++) {
1045: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 1046: if (fnmatch(newsearchstr, line, 0) == 0) {
1047: msg = line;
1048: if (lineno != NULL)
1049: *lineno = i;
1050: break;
1051: }
1.82 nicm 1052: free(line);
1.4 nicm 1053: }
1.5 nicm 1054:
1.82 nicm 1055: free(newsearchstr);
1.5 nicm 1056: return (msg);
1.45 nicm 1057: }
1058:
1059: /* Find the pane directly above another. */
1060: struct window_pane *
1061: window_pane_find_up(struct window_pane *wp)
1062: {
1063: struct window_pane *wp2;
1064: u_int left, top;
1065:
1066: if (wp == NULL || !window_pane_visible(wp))
1067: return (NULL);
1068:
1069: top = wp->yoff;
1070: if (top == 0)
1071: top = wp->window->sy + 1;
1072: left = wp->xoff;
1073:
1074: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1075: if (!window_pane_visible(wp2))
1076: continue;
1077: if (wp2->yoff + wp2->sy + 1 != top)
1078: continue;
1079: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1080: return (wp2);
1081: }
1082: return (NULL);
1083: }
1084:
1085: /* Find the pane directly below another. */
1086: struct window_pane *
1087: window_pane_find_down(struct window_pane *wp)
1088: {
1089: struct window_pane *wp2;
1090: u_int left, bottom;
1091:
1092: if (wp == NULL || !window_pane_visible(wp))
1093: return (NULL);
1094:
1095: bottom = wp->yoff + wp->sy + 1;
1096: if (bottom >= wp->window->sy)
1097: bottom = 0;
1098: left = wp->xoff;
1099:
1100: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1101: if (!window_pane_visible(wp2))
1102: continue;
1103: if (wp2->yoff != bottom)
1104: continue;
1105: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1106: return (wp2);
1107: }
1108: return (NULL);
1109: }
1110:
1111: /*
1112: * Find the pane directly to the left of another, adjacent to the left side and
1113: * containing the top edge.
1114: */
1115: struct window_pane *
1116: window_pane_find_left(struct window_pane *wp)
1117: {
1118: struct window_pane *wp2;
1119: u_int left, top;
1120:
1121: if (wp == NULL || !window_pane_visible(wp))
1122: return (NULL);
1123:
1124: left = wp->xoff;
1125: if (left == 0)
1126: left = wp->window->sx + 1;
1127: top = wp->yoff;
1128:
1129: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1130: if (!window_pane_visible(wp2))
1131: continue;
1132: if (wp2->xoff + wp2->sx + 1 != left)
1133: continue;
1134: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1135: return (wp2);
1136: }
1137: return (NULL);
1138: }
1139:
1140: /*
1141: * Find the pane directly to the right of another, that is adjacent to the
1142: * right edge and including the top edge.
1143: */
1144: struct window_pane *
1145: window_pane_find_right(struct window_pane *wp)
1146: {
1147: struct window_pane *wp2;
1148: u_int right, top;
1149:
1150: if (wp == NULL || !window_pane_visible(wp))
1151: return (NULL);
1152:
1153: right = wp->xoff + wp->sx + 1;
1154: if (right >= wp->window->sx)
1155: right = 0;
1156: top = wp->yoff;
1157:
1158: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1159: if (!window_pane_visible(wp2))
1160: continue;
1161: if (wp2->xoff != right)
1162: continue;
1163: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1164: return (wp2);
1165: }
1166: return (NULL);
1.81 nicm 1167: }
1168:
1169: /* Clear alert flags for a winlink */
1170: void
1171: winlink_clear_flags(struct winlink *wl)
1172: {
1173: struct winlink *wm;
1174: struct session *s;
1175: struct window *w;
1176: u_int i;
1177:
1178: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1179: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1180: continue;
1181:
1182: RB_FOREACH(s, sessions, &sessions) {
1183: if ((wm = session_has(s, w)) == NULL)
1184: continue;
1185:
1186: if (wm->window != wl->window)
1187: continue;
1188: if ((wm->flags & WINLINK_ALERTFLAGS) == 0)
1189: continue;
1190:
1191: wm->flags &= ~WINLINK_ALERTFLAGS;
1192: server_status_session(s);
1193: }
1194: }
1.83 nicm 1195: }
1196:
1197: /* Set the grid_cell with fg/bg/attr information when window is in a mode. */
1198: void
1199: window_mode_attrs(struct grid_cell *gc, struct options *oo)
1200: {
1.85 nicm 1201: memcpy(gc, &grid_default_cell, sizeof *gc);
1.83 nicm 1202: colour_set_fg(gc, options_get_number(oo, "mode-fg"));
1203: colour_set_bg(gc, options_get_number(oo, "mode-bg"));
1204: gc->attr |= options_get_number(oo, "mode-attr");
1.1 nicm 1205: }