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