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