Annotation of src/usr.bin/tmux/window.c, Revision 1.107
1.107 ! nicm 1: /* $OpenBSD: window.c,v 1.106 2014/04/17 11:38:35 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:
21: #include <errno.h>
22: #include <fcntl.h>
1.5 nicm 23: #include <fnmatch.h>
1.1 nicm 24: #include <stdint.h>
25: #include <stdlib.h>
26: #include <string.h>
27: #include <termios.h>
28: #include <unistd.h>
29: #include <util.h>
30:
31: #include "tmux.h"
32:
33: /*
1.14 nicm 34: * Each window is attached to a number of panes, each of which is a pty. This
1.1 nicm 35: * file contains code to handle them.
36: *
37: * A pane has two buffers attached, these are filled and emptied by the main
38: * server poll loop. Output data is received from pty's in screen format,
39: * translated and returned as a series of escape sequences and strings via
40: * input_parse (in input.c). Input data is received as key codes and written
41: * directly via input_key.
42: *
43: * Each pane also has a "virtual" screen (screen.c) which contains the current
44: * state and is redisplayed when the window is reattached to a client.
45: *
46: * Windows are stored directly on a global array and wrapped in any number of
47: * winlink structs to be linked onto local session RB trees. A reference count
48: * is maintained and a window removed from the global list and destroyed when
49: * it reaches zero.
50: */
51:
52: /* Global window list. */
53: struct windows windows;
54:
1.64 nicm 55: /* Global panes tree. */
56: struct window_pane_tree all_window_panes;
1.71 nicm 57: u_int next_window_pane_id;
58: u_int next_window_id;
1.64 nicm 59:
1.100 nicm 60: struct window_pane *window_pane_active_set(struct window_pane *,
61: struct window_pane *);
62: void window_pane_active_lost(struct window_pane *, struct window_pane *);
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.107 ! nicm 311: window_create(const char *name, const char *cmd, const char *path,
! 312: const char *shell, int 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.1 nicm 317:
318: w = window_create1(sx, sy);
1.16 nicm 319: wp = window_add_pane(w, hlimit);
1.93 nicm 320: layout_init(w, wp);
1.91 nicm 321:
1.107 ! nicm 322: if (window_pane_spawn(wp, cmd, path, shell, cwd, env, tio,
! 323: cause) != 0) {
1.1 nicm 324: window_destroy(w);
325: return (NULL);
326: }
1.91 nicm 327:
1.1 nicm 328: w->active = TAILQ_FIRST(&w->panes);
329: if (name != NULL) {
330: w->name = xstrdup(name);
331: options_set_number(&w->options, "automatic-rename", 0);
332: } else
333: w->name = default_window_name(w);
1.91 nicm 334:
1.1 nicm 335: return (w);
336: }
337:
338: void
339: window_destroy(struct window *w)
340: {
341: u_int i;
342:
1.93 nicm 343: window_unzoom(w);
344:
1.1 nicm 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:
1.82 nicm 361: free(w->name);
362: free(w);
1.84 nicm 363: }
364:
365: void
366: window_remove_ref(struct window *w)
367: {
368: if (w->references == 0)
369: fatal("bad reference count");
370: w->references--;
371: if (w->references == 0)
372: window_destroy(w);
1.72 nicm 373: }
374:
375: void
376: window_set_name(struct window *w, const char *new_name)
377: {
1.82 nicm 378: free(w->name);
1.72 nicm 379: w->name = xstrdup(new_name);
1.74 nicm 380: notify_window_renamed(w);
1.1 nicm 381: }
382:
1.15 nicm 383: void
1.1 nicm 384: window_resize(struct window *w, u_int sx, u_int sy)
385: {
386: w->sx = sx;
387: w->sy = sy;
388: }
389:
1.100 nicm 390: /*
391: * Restore previously active pane when changing from wp to nextwp. The intended
392: * pane is in nextwp and it returns the previously focused pane.
393: */
394: struct window_pane *
395: window_pane_active_set(struct window_pane *wp, struct window_pane *nextwp)
396: {
397: struct layout_cell *lc;
398: struct window_pane *lastwp;
399:
400: /* Target pane's parent must not be an ancestor of source pane. */
401: for (lc = wp->layout_cell->parent; lc != NULL; lc = lc->parent) {
402: if (lc == nextwp->layout_cell->parent)
403: return (nextwp);
404: }
405:
406: /*
407: * Previously active pane, if any, must not be the same as the source
408: * pane.
409: */
1.102 nicm 410: lc = nextwp->layout_cell->parent;
411: if (lc != NULL && lc->lastwp != NULL) {
412: lastwp = lc->lastwp;
1.100 nicm 413: if (lastwp != wp && window_pane_visible(lastwp))
414: return (lastwp);
415: }
416: return (nextwp);
417: }
418:
419: /* Remember previously active pane when changing from wp to nextwp. */
420: void
421: window_pane_active_lost(struct window_pane *wp, struct window_pane *nextwp)
422: {
1.103 nicm 423: struct layout_cell *lc, *lc2, *lcparent;
424:
425: /* Get the parent cell. */
426: lcparent = nextwp->layout_cell->parent;
427: if (lcparent == NULL)
428: return;
1.100 nicm 429:
430: /* Save the target pane in its parent. */
1.103 nicm 431: lcparent->lastwp = nextwp;
1.100 nicm 432:
433: /*
434: * Save the source pane in all of its parents up to, but not including,
435: * the common ancestor of itself and the target panes.
436: */
437: if (wp == NULL)
438: return;
439: for (lc = wp->layout_cell->parent; lc != NULL; lc = lc->parent) {
1.103 nicm 440: for (lc2 = lcparent; lc2 != NULL; lc2 = lc2->parent) {
1.100 nicm 441: if (lc == lc2)
442: return;
443: }
444: lc->lastwp = wp;
445: }
446: }
447:
1.1 nicm 448: void
449: window_set_active_pane(struct window *w, struct window_pane *wp)
450: {
1.59 nicm 451: if (wp == w->active)
452: return;
1.58 nicm 453: w->last = w->active;
1.1 nicm 454: w->active = wp;
1.100 nicm 455: window_pane_active_lost(w->last, wp);
1.10 nicm 456: while (!window_pane_visible(w->active)) {
1.1 nicm 457: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 458: if (w->active == NULL)
459: w->active = TAILQ_LAST(&w->panes, window_panes);
460: if (w->active == wp)
461: return;
1.29 nicm 462: }
463: }
464:
1.66 nicm 465: struct window_pane *
466: window_get_active_at(struct window *w, u_int x, u_int y)
1.29 nicm 467: {
468: struct window_pane *wp;
469:
470: TAILQ_FOREACH(wp, &w->panes, entry) {
1.66 nicm 471: if (!window_pane_visible(wp))
1.29 nicm 472: continue;
1.66 nicm 473: if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29 nicm 474: continue;
1.66 nicm 475: if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29 nicm 476: continue;
1.66 nicm 477: return (wp);
478: }
479: return (NULL);
480: }
481:
482: void
483: window_set_active_at(struct window *w, u_int x, u_int y)
484: {
485: struct window_pane *wp;
486:
487: wp = window_get_active_at(w, x, y);
488: if (wp != NULL && wp != w->active)
1.29 nicm 489: window_set_active_pane(w, wp);
1.66 nicm 490: }
491:
492: struct window_pane *
493: window_find_string(struct window *w, const char *s)
494: {
495: u_int x, y;
496:
497: x = w->sx / 2;
498: y = w->sy / 2;
499:
500: if (strcasecmp(s, "top") == 0)
501: y = 0;
502: else if (strcasecmp(s, "bottom") == 0)
503: y = w->sy - 1;
504: else if (strcasecmp(s, "left") == 0)
505: x = 0;
506: else if (strcasecmp(s, "right") == 0)
507: x = w->sx - 1;
508: else if (strcasecmp(s, "top-left") == 0) {
509: x = 0;
510: y = 0;
511: } else if (strcasecmp(s, "top-right") == 0) {
512: x = w->sx - 1;
513: y = 0;
514: } else if (strcasecmp(s, "bottom-left") == 0) {
515: x = 0;
516: y = w->sy - 1;
517: } else if (strcasecmp(s, "bottom-right") == 0) {
518: x = w->sx - 1;
519: y = w->sy - 1;
520: } else
521: return (NULL);
522:
523: return (window_get_active_at(w, x, y));
1.1 nicm 524: }
525:
1.93 nicm 526: int
527: window_zoom(struct window_pane *wp)
528: {
529: struct window *w = wp->window;
530: struct window_pane *wp1;
531:
532: if (w->flags & WINDOW_ZOOMED)
533: return (-1);
534:
535: if (!window_pane_visible(wp))
536: return (-1);
1.94 nicm 537:
538: if (window_count_panes(w) == 1)
539: return (-1);
540:
1.93 nicm 541: if (w->active != wp)
542: window_set_active_pane(w, wp);
543:
544: TAILQ_FOREACH(wp1, &w->panes, entry) {
545: wp1->saved_layout_cell = wp1->layout_cell;
546: wp1->layout_cell = NULL;
547: }
548:
549: w->saved_layout_root = w->layout_root;
550: layout_init(w, wp);
551: w->flags |= WINDOW_ZOOMED;
552:
553: return (0);
554: }
555:
556: int
557: window_unzoom(struct window *w)
558: {
1.97 nicm 559: struct window_pane *wp;
1.93 nicm 560:
561: if (!(w->flags & WINDOW_ZOOMED))
562: return (-1);
563:
564: w->flags &= ~WINDOW_ZOOMED;
565: layout_free(w);
566: w->layout_root = w->saved_layout_root;
567:
1.97 nicm 568: TAILQ_FOREACH(wp, &w->panes, entry) {
569: wp->layout_cell = wp->saved_layout_cell;
570: wp->saved_layout_cell = NULL;
1.93 nicm 571: }
572: layout_fix_panes(w, w->sx, w->sy);
573:
574: return (0);
575: }
576:
1.1 nicm 577: struct window_pane *
1.16 nicm 578: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 579: {
580: struct window_pane *wp;
581:
1.14 nicm 582: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 583: if (TAILQ_EMPTY(&w->panes))
584: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
585: else
586: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
587: return (wp);
588: }
589:
590: void
1.105 nicm 591: window_lost_pane(struct window *w, struct window_pane *wp)
1.1 nicm 592: {
1.57 nicm 593: if (wp == w->active) {
1.58 nicm 594: w->active = w->last;
595: w->last = NULL;
596: if (w->active == NULL) {
597: w->active = TAILQ_PREV(wp, window_panes, entry);
598: if (w->active == NULL)
599: w->active = TAILQ_NEXT(wp, entry);
600: }
601: } else if (wp == w->last)
602: w->last = NULL;
1.105 nicm 603: }
604:
605: void
606: window_remove_pane(struct window *w, struct window_pane *wp)
607: {
608: window_lost_pane(w, wp);
1.1 nicm 609:
610: TAILQ_REMOVE(&w->panes, wp, entry);
611: window_pane_destroy(wp);
612: }
613:
614: struct window_pane *
615: window_pane_at_index(struct window *w, u_int idx)
616: {
617: struct window_pane *wp;
618: u_int n;
619:
1.67 nicm 620: n = options_get_number(&w->options, "pane-base-index");
1.1 nicm 621: TAILQ_FOREACH(wp, &w->panes, entry) {
622: if (n == idx)
623: return (wp);
624: n++;
625: }
626: return (NULL);
1.53 nicm 627: }
628:
629: struct window_pane *
630: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
631: {
632: for (; n > 0; n--) {
633: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
634: wp = TAILQ_FIRST(&w->panes);
635: }
636:
637: return (wp);
638: }
639:
640: struct window_pane *
641: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
642: u_int n)
643: {
644: for (; n > 0; n--) {
645: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
646: wp = TAILQ_LAST(&w->panes, window_panes);
647: }
648:
649: return (wp);
1.13 nicm 650: }
651:
1.69 nicm 652: int
653: window_pane_index(struct window_pane *wp, u_int *i)
1.13 nicm 654: {
655: struct window_pane *wq;
1.69 nicm 656: struct window *w = wp->window;
1.13 nicm 657:
1.69 nicm 658: *i = options_get_number(&w->options, "pane-base-index");
1.13 nicm 659: TAILQ_FOREACH(wq, &w->panes, entry) {
1.69 nicm 660: if (wp == wq) {
661: return (0);
662: }
663: (*i)++;
1.13 nicm 664: }
1.69 nicm 665:
666: return (-1);
1.1 nicm 667: }
668:
669: u_int
670: window_count_panes(struct window *w)
671: {
672: struct window_pane *wp;
673: u_int n;
674:
675: n = 0;
676: TAILQ_FOREACH(wp, &w->panes, entry)
677: n++;
678: return (n);
679: }
680:
681: void
682: window_destroy_panes(struct window *w)
683: {
684: struct window_pane *wp;
685:
686: while (!TAILQ_EMPTY(&w->panes)) {
687: wp = TAILQ_FIRST(&w->panes);
688: TAILQ_REMOVE(&w->panes, wp, entry);
689: window_pane_destroy(wp);
690: }
1.61 nicm 691: }
692:
693: /* Return list of printable window flag symbols. No flags is just a space. */
694: char *
695: window_printable_flags(struct session *s, struct winlink *wl)
696: {
697: char flags[BUFSIZ];
698: int pos;
699:
700: pos = 0;
701: if (wl->flags & WINLINK_ACTIVITY)
702: flags[pos++] = '#';
703: if (wl->flags & WINLINK_BELL)
704: flags[pos++] = '!';
705: if (wl->flags & WINLINK_SILENCE)
706: flags[pos++] = '~';
707: if (wl == s->curw)
708: flags[pos++] = '*';
709: if (wl == TAILQ_FIRST(&s->lastw))
710: flags[pos++] = '-';
1.93 nicm 711: if (wl->window->flags & WINDOW_ZOOMED)
712: flags[pos++] = 'Z';
1.61 nicm 713: if (pos == 0)
714: flags[pos++] = ' ';
715: flags[pos] = '\0';
716: return (xstrdup(flags));
1.1 nicm 717: }
718:
1.64 nicm 719: /* Find pane in global tree by id. */
720: struct window_pane *
721: window_pane_find_by_id(u_int id)
722: {
723: struct window_pane wp;
724:
725: wp.id = id;
726: return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
727: }
728:
1.1 nicm 729: struct window_pane *
730: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
731: {
732: struct window_pane *wp;
733:
734: wp = xcalloc(1, sizeof *wp);
735: wp->window = w;
736:
1.71 nicm 737: wp->id = next_window_pane_id++;
1.64 nicm 738: RB_INSERT(window_pane_tree, &all_window_panes, wp);
739:
1.1 nicm 740: wp->cmd = NULL;
1.23 nicm 741: wp->shell = NULL;
1.99 nicm 742: wp->cwd = -1;
1.1 nicm 743:
744: wp->fd = -1;
1.37 nicm 745: wp->event = NULL;
1.1 nicm 746:
747: wp->mode = NULL;
1.14 nicm 748:
749: wp->layout_cell = NULL;
1.1 nicm 750:
751: wp->xoff = 0;
1.42 nicm 752: wp->yoff = 0;
1.1 nicm 753:
754: wp->sx = sx;
755: wp->sy = sy;
756:
1.32 nicm 757: wp->pipe_fd = -1;
758: wp->pipe_off = 0;
1.36 nicm 759: wp->pipe_event = NULL;
1.32 nicm 760:
1.9 nicm 761: wp->saved_grid = NULL;
762:
1.1 nicm 763: screen_init(&wp->base, sx, sy, hlimit);
764: wp->screen = &wp->base;
765:
766: input_init(wp);
767:
768: return (wp);
769: }
770:
771: void
772: window_pane_destroy(struct window_pane *wp)
773: {
1.100 nicm 774: struct window_pane *wp2;
775:
776: /* Forget removed pane in all layout cells that remember it. */
777: RB_FOREACH(wp2, window_pane_tree, &all_window_panes) {
778: if (wp2->layout_cell != NULL &&
779: wp2->layout_cell->parent != NULL &&
780: wp2->layout_cell->parent->lastwp == wp)
781: wp2->layout_cell->parent->lastwp = NULL;
782: }
783:
1.55 nicm 784: window_pane_reset_mode(wp);
785:
1.76 nicm 786: if (event_initialized(&wp->changes_timer))
787: evtimer_del(&wp->changes_timer);
1.75 nicm 788:
1.37 nicm 789: if (wp->fd != -1) {
1.70 nicm 790: bufferevent_free(wp->event);
1.1 nicm 791: close(wp->fd);
1.37 nicm 792: }
1.1 nicm 793:
794: input_free(wp);
795:
796: screen_free(&wp->base);
1.9 nicm 797: if (wp->saved_grid != NULL)
798: grid_destroy(wp->saved_grid);
1.1 nicm 799:
1.32 nicm 800: if (wp->pipe_fd != -1) {
1.70 nicm 801: bufferevent_free(wp->pipe_event);
1.32 nicm 802: close(wp->pipe_fd);
803: }
804:
1.64 nicm 805: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
806:
1.99 nicm 807: close(wp->cwd);
1.82 nicm 808: free(wp->shell);
809: free(wp->cmd);
810: free(wp);
1.1 nicm 811: }
812:
813: int
1.107 ! nicm 814: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *path,
! 815: const char *shell, int cwd, struct environ *env, struct termios *tio,
! 816: char **cause)
1.1 nicm 817: {
1.47 nicm 818: struct winsize ws;
1.64 nicm 819: char *argv0, paneid[16];
1.47 nicm 820: const char *ptr;
821: struct termios tio2;
1.1 nicm 822:
1.37 nicm 823: if (wp->fd != -1) {
1.70 nicm 824: bufferevent_free(wp->event);
1.1 nicm 825: close(wp->fd);
1.37 nicm 826: }
1.1 nicm 827: if (cmd != NULL) {
1.82 nicm 828: free(wp->cmd);
1.1 nicm 829: wp->cmd = xstrdup(cmd);
830: }
1.23 nicm 831: if (shell != NULL) {
1.82 nicm 832: free(wp->shell);
1.23 nicm 833: wp->shell = xstrdup(shell);
834: }
1.99 nicm 835: if (cwd != -1) {
836: close(wp->cwd);
837: wp->cwd = dup(cwd);
1.1 nicm 838: }
1.91 nicm 839:
840: log_debug("spawn: %s -- %s", wp->shell, wp->cmd);
1.1 nicm 841:
842: memset(&ws, 0, sizeof ws);
843: ws.ws_col = screen_size_x(&wp->base);
844: ws.ws_row = screen_size_y(&wp->base);
845:
1.42 nicm 846: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 847: case -1:
848: wp->fd = -1;
849: xasprintf(cause, "%s: %s", cmd, strerror(errno));
850: return (-1);
851: case 0:
1.99 nicm 852: if (fchdir(wp->cwd) != 0)
1.1 nicm 853: chdir("/");
1.25 nicm 854:
855: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
856: fatal("tcgetattr failed");
857: if (tio != NULL)
858: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
859: tio2.c_cc[VERASE] = '\177';
860: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
861: fatal("tcgetattr failed");
1.18 nicm 862:
1.56 nicm 863: closefrom(STDERR_FILENO + 1);
864:
1.107 ! nicm 865: if (path != NULL)
! 866: environ_set(env, "PATH", path);
1.64 nicm 867: xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
868: environ_set(env, "TMUX_PANE", paneid);
1.47 nicm 869: environ_push(env);
1.18 nicm 870:
1.54 nicm 871: clear_signals(1);
1.1 nicm 872: log_close();
873:
1.80 nicm 874: setenv("SHELL", wp->shell, 1);
875: ptr = strrchr(wp->shell, '/');
876:
1.8 nicm 877: if (*wp->cmd != '\0') {
1.80 nicm 878: /* Use the command. */
879: if (ptr != NULL && *(ptr + 1) != '\0')
880: xasprintf(&argv0, "%s", ptr + 1);
881: else
882: xasprintf(&argv0, "%s", wp->shell);
883: execl(wp->shell, argv0, "-c", wp->cmd, (char *) NULL);
1.8 nicm 884: fatal("execl failed");
885: }
886:
887: /* No command; fork a login shell. */
1.23 nicm 888: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 889: xasprintf(&argv0, "-%s", ptr + 1);
890: else
1.23 nicm 891: xasprintf(&argv0, "-%s", wp->shell);
892: execl(wp->shell, argv0, (char *) NULL);
1.1 nicm 893: fatal("execl failed");
894: }
895:
1.62 nicm 896: setblocking(wp->fd, 0);
897:
1.37 nicm 898: wp->event = bufferevent_new(wp->fd,
899: window_pane_read_callback, NULL, window_pane_error_callback, wp);
900: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 901:
902: return (0);
1.75 nicm 903: }
904:
905: void
906: window_pane_timer_start(struct window_pane *wp)
907: {
908: struct timeval tv;
909:
910: tv.tv_sec = 0;
911: tv.tv_usec = 1000;
912:
913: evtimer_del(&wp->changes_timer);
914: evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp);
915: evtimer_add(&wp->changes_timer, &tv);
916: }
917:
918: void
919: window_pane_timer_callback(unused int fd, unused short events, void *data)
920: {
921: struct window_pane *wp = data;
922: struct window *w = wp->window;
923: u_int interval, trigger;
924:
925: interval = options_get_number(&w->options, "c0-change-interval");
926: trigger = options_get_number(&w->options, "c0-change-trigger");
927:
928: if (wp->changes_redraw++ == interval) {
929: wp->flags |= PANE_REDRAW;
930: wp->changes_redraw = 0;
931:
932: }
933:
934: if (trigger == 0 || wp->changes < trigger) {
935: wp->flags |= PANE_REDRAW;
936: wp->flags &= ~PANE_DROP;
937: } else
938: window_pane_timer_start(wp);
939: wp->changes = 0;
1.1 nicm 940: }
941:
1.15 nicm 942: void
1.37 nicm 943: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
944: {
1.46 nicm 945: struct window_pane *wp = data;
946: char *new_data;
947: size_t new_size;
948:
949: new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
950: if (wp->pipe_fd != -1 && new_size > 0) {
951: new_data = EVBUFFER_DATA(wp->event->input);
952: bufferevent_write(wp->pipe_event, new_data, new_size);
953: }
954:
955: input_parse(wp);
1.37 nicm 956:
1.46 nicm 957: wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.60 nicm 958:
959: /*
960: * If we get here, we're not outputting anymore, so set the silence
961: * flag on the window.
962: */
963: wp->window->flags |= WINDOW_SILENCE;
964: if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
965: fatal("gettimeofday failed.");
1.37 nicm 966: }
967:
968: void
969: window_pane_error_callback(
970: unused struct bufferevent *bufev, unused short what, void *data)
971: {
972: struct window_pane *wp = data;
973:
1.39 nicm 974: server_destroy_pane(wp);
1.37 nicm 975: }
976:
977: void
1.1 nicm 978: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
979: {
980: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 981: return;
1.1 nicm 982: wp->sx = sx;
983: wp->sy = sy;
984:
1.89 nicm 985: screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1 nicm 986: if (wp->mode != NULL)
987: wp->mode->resize(wp, sx, sy);
1.95 nicm 988:
989: wp->flags |= PANE_RESIZE;
1.44 nicm 990: }
991:
992: /*
993: * Enter alternative screen mode. A copy of the visible screen is saved and the
994: * history is not updated
995: */
996: void
1.87 nicm 997: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
998: int cursor)
1.44 nicm 999: {
1000: struct screen *s = &wp->base;
1001: u_int sx, sy;
1002:
1003: if (wp->saved_grid != NULL)
1004: return;
1005: if (!options_get_number(&wp->window->options, "alternate-screen"))
1006: return;
1007: sx = screen_size_x(s);
1008: sy = screen_size_y(s);
1009:
1010: wp->saved_grid = grid_create(sx, sy, 0);
1011: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87 nicm 1012: if (cursor) {
1013: wp->saved_cx = s->cx;
1014: wp->saved_cy = s->cy;
1015: }
1.44 nicm 1016: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
1017:
1018: grid_view_clear(s->grid, 0, 0, sx, sy);
1019:
1020: wp->base.grid->flags &= ~GRID_HISTORY;
1021:
1022: wp->flags |= PANE_REDRAW;
1023: }
1024:
1025: /* Exit alternate screen mode and restore the copied grid. */
1026: void
1.87 nicm 1027: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
1028: int cursor)
1.44 nicm 1029: {
1030: struct screen *s = &wp->base;
1031: u_int sx, sy;
1032:
1033: if (wp->saved_grid == NULL)
1034: return;
1035: if (!options_get_number(&wp->window->options, "alternate-screen"))
1036: return;
1037: sx = screen_size_x(s);
1038: sy = screen_size_y(s);
1039:
1040: /*
1041: * If the current size is bigger, temporarily resize to the old size
1042: * before copying back.
1043: */
1044: if (sy > wp->saved_grid->sy)
1.89 nicm 1045: screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44 nicm 1046:
1047: /* Restore the grid, cursor position and cell. */
1048: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87 nicm 1049: if (cursor)
1050: s->cx = wp->saved_cx;
1.44 nicm 1051: if (s->cx > screen_size_x(s) - 1)
1052: s->cx = screen_size_x(s) - 1;
1.87 nicm 1053: if (cursor)
1054: s->cy = wp->saved_cy;
1.44 nicm 1055: if (s->cy > screen_size_y(s) - 1)
1056: s->cy = screen_size_y(s) - 1;
1057: memcpy(gc, &wp->saved_cell, sizeof *gc);
1058:
1059: /*
1060: * Turn history back on (so resize can use it) and then resize back to
1061: * the current size.
1062: */
1063: wp->base.grid->flags |= GRID_HISTORY;
1.89 nicm 1064: if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1065: screen_resize(s, sx, sy, 1);
1.44 nicm 1066:
1067: grid_destroy(wp->saved_grid);
1068: wp->saved_grid = NULL;
1069:
1070: wp->flags |= PANE_REDRAW;
1.1 nicm 1071: }
1072:
1073: int
1074: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1075: {
1076: struct screen *s;
1077:
1.15 nicm 1078: if (wp->mode != NULL)
1.1 nicm 1079: return (1);
1080: wp->mode = mode;
1081:
1082: if ((s = wp->mode->init(wp)) != NULL)
1083: wp->screen = s;
1.34 nicm 1084: wp->flags |= PANE_REDRAW;
1.1 nicm 1085: return (0);
1086: }
1087:
1088: void
1089: window_pane_reset_mode(struct window_pane *wp)
1090: {
1091: if (wp->mode == NULL)
1092: return;
1093:
1094: wp->mode->free(wp);
1095: wp->mode = NULL;
1096:
1097: wp->screen = &wp->base;
1.34 nicm 1098: wp->flags |= PANE_REDRAW;
1.1 nicm 1099: }
1100:
1101: void
1.51 nicm 1102: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1 nicm 1103: {
1.27 nicm 1104: struct window_pane *wp2;
1105:
1.30 nicm 1106: if (!window_pane_visible(wp))
1.3 nicm 1107: return;
1108:
1.1 nicm 1109: if (wp->mode != NULL) {
1110: if (wp->mode->key != NULL)
1.51 nicm 1111: wp->mode->key(wp, sess, key);
1.27 nicm 1112: return;
1.30 nicm 1113: }
1.27 nicm 1114:
1.30 nicm 1115: if (wp->fd == -1)
1116: return;
1.27 nicm 1117: input_key(wp, key);
1118: if (options_get_number(&wp->window->options, "synchronize-panes")) {
1119: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1120: if (wp2 == wp || wp2->mode != NULL)
1121: continue;
1122: if (wp2->fd != -1 && window_pane_visible(wp2))
1123: input_key(wp2, key);
1124: }
1125: }
1.1 nicm 1126: }
1127:
1128: void
1129: window_pane_mouse(
1.51 nicm 1130: struct window_pane *wp, struct session *sess, struct mouse_event *m)
1.1 nicm 1131: {
1.30 nicm 1132: if (!window_pane_visible(wp))
1.3 nicm 1133: return;
1134:
1.31 nicm 1135: if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1 nicm 1136: return;
1.31 nicm 1137: if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1 nicm 1138: return;
1.31 nicm 1139: m->x -= wp->xoff;
1140: m->y -= wp->yoff;
1.1 nicm 1141:
1142: if (wp->mode != NULL) {
1.65 nicm 1143: if (wp->mode->mouse != NULL &&
1144: options_get_number(&wp->window->options, "mode-mouse"))
1.51 nicm 1145: wp->mode->mouse(wp, sess, m);
1.30 nicm 1146: } else if (wp->fd != -1)
1.86 nicm 1147: input_mouse(wp, sess, m);
1.10 nicm 1148: }
1149:
1150: int
1151: window_pane_visible(struct window_pane *wp)
1152: {
1153: struct window *w = wp->window;
1154:
1.93 nicm 1155: if (wp->layout_cell == NULL)
1156: return (0);
1.10 nicm 1157: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1158: return (0);
1159: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1160: return (0);
1161: return (1);
1.1 nicm 1162: }
1163:
1164: char *
1.5 nicm 1165: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 1166: {
1.4 nicm 1167: struct screen *s = &wp->base;
1.5 nicm 1168: char *newsearchstr, *line, *msg;
1.4 nicm 1169: u_int i;
1170:
1.5 nicm 1171: msg = NULL;
1172: xasprintf(&newsearchstr, "*%s*", searchstr);
1173:
1.4 nicm 1174: for (i = 0; i < screen_size_y(s); i++) {
1175: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 1176: if (fnmatch(newsearchstr, line, 0) == 0) {
1177: msg = line;
1178: if (lineno != NULL)
1179: *lineno = i;
1180: break;
1181: }
1.82 nicm 1182: free(line);
1.4 nicm 1183: }
1.5 nicm 1184:
1.82 nicm 1185: free(newsearchstr);
1.5 nicm 1186: return (msg);
1.45 nicm 1187: }
1188:
1189: /* Find the pane directly above another. */
1190: struct window_pane *
1191: window_pane_find_up(struct window_pane *wp)
1192: {
1193: struct window_pane *wp2;
1194: u_int left, top;
1195:
1196: if (wp == NULL || !window_pane_visible(wp))
1197: return (NULL);
1198:
1199: top = wp->yoff;
1200: if (top == 0)
1201: top = wp->window->sy + 1;
1202: left = wp->xoff;
1203:
1204: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1205: if (!window_pane_visible(wp2))
1206: continue;
1207: if (wp2->yoff + wp2->sy + 1 != top)
1208: continue;
1209: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1.100 nicm 1210: return (window_pane_active_set(wp, wp2));
1.45 nicm 1211: }
1212: return (NULL);
1213: }
1214:
1215: /* Find the pane directly below another. */
1216: struct window_pane *
1217: window_pane_find_down(struct window_pane *wp)
1218: {
1219: struct window_pane *wp2;
1220: u_int left, bottom;
1221:
1222: if (wp == NULL || !window_pane_visible(wp))
1223: return (NULL);
1224:
1225: bottom = wp->yoff + wp->sy + 1;
1226: if (bottom >= wp->window->sy)
1227: bottom = 0;
1228: left = wp->xoff;
1229:
1230: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1231: if (!window_pane_visible(wp2))
1232: continue;
1233: if (wp2->yoff != bottom)
1234: continue;
1235: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1.100 nicm 1236: return (window_pane_active_set(wp, wp2));
1.45 nicm 1237: }
1238: return (NULL);
1239: }
1240:
1241: /*
1242: * Find the pane directly to the left of another, adjacent to the left side and
1243: * containing the top edge.
1244: */
1245: struct window_pane *
1246: window_pane_find_left(struct window_pane *wp)
1247: {
1248: struct window_pane *wp2;
1249: u_int left, top;
1250:
1251: if (wp == NULL || !window_pane_visible(wp))
1252: return (NULL);
1253:
1254: left = wp->xoff;
1255: if (left == 0)
1256: left = wp->window->sx + 1;
1257: top = wp->yoff;
1258:
1259: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1260: if (!window_pane_visible(wp2))
1261: continue;
1262: if (wp2->xoff + wp2->sx + 1 != left)
1263: continue;
1264: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1.100 nicm 1265: return (window_pane_active_set(wp, wp2));
1.45 nicm 1266: }
1267: return (NULL);
1268: }
1269:
1270: /*
1271: * Find the pane directly to the right of another, that is adjacent to the
1272: * right edge and including the top edge.
1273: */
1274: struct window_pane *
1275: window_pane_find_right(struct window_pane *wp)
1276: {
1277: struct window_pane *wp2;
1278: u_int right, top;
1279:
1280: if (wp == NULL || !window_pane_visible(wp))
1281: return (NULL);
1282:
1283: right = wp->xoff + wp->sx + 1;
1284: if (right >= wp->window->sx)
1285: right = 0;
1286: top = wp->yoff;
1287:
1288: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1289: if (!window_pane_visible(wp2))
1290: continue;
1291: if (wp2->xoff != right)
1292: continue;
1293: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1.100 nicm 1294: return (window_pane_active_set(wp, wp2));
1.45 nicm 1295: }
1296: return (NULL);
1.81 nicm 1297: }
1298:
1299: /* Clear alert flags for a winlink */
1300: void
1301: winlink_clear_flags(struct winlink *wl)
1302: {
1303: struct winlink *wm;
1304: struct session *s;
1305: struct window *w;
1306: u_int i;
1307:
1308: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1309: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1310: continue;
1311:
1312: RB_FOREACH(s, sessions, &sessions) {
1313: if ((wm = session_has(s, w)) == NULL)
1314: continue;
1315:
1316: if (wm->window != wl->window)
1317: continue;
1318: if ((wm->flags & WINLINK_ALERTFLAGS) == 0)
1319: continue;
1320:
1321: wm->flags &= ~WINLINK_ALERTFLAGS;
1.98 nicm 1322: wm->window->flags &= ~WINDOW_ALERTFLAGS;
1.81 nicm 1323: server_status_session(s);
1324: }
1325: }
1.1 nicm 1326: }