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