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