Annotation of src/usr.bin/tmux/window.c, Revision 1.105
1.105 ! nicm 1: /* $OpenBSD: window.c,v 1.104 2014/04/17 07:36:45 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: */
1.102 nicm 413: lc = nextwp->layout_cell->parent;
414: if (lc != NULL && lc->lastwp != NULL) {
415: lastwp = lc->lastwp;
1.100 nicm 416: if (lastwp != wp && window_pane_visible(lastwp))
417: return (lastwp);
418: }
419: return (nextwp);
420: }
421:
422: /* Remember previously active pane when changing from wp to nextwp. */
423: void
424: window_pane_active_lost(struct window_pane *wp, struct window_pane *nextwp)
425: {
1.103 nicm 426: struct layout_cell *lc, *lc2, *lcparent;
427:
428: /* Get the parent cell. */
429: lcparent = nextwp->layout_cell->parent;
430: if (lcparent == NULL)
431: return;
1.100 nicm 432:
433: /* Save the target pane in its parent. */
1.103 nicm 434: lcparent->lastwp = nextwp;
1.100 nicm 435:
436: /*
437: * Save the source pane in all of its parents up to, but not including,
438: * the common ancestor of itself and the target panes.
439: */
440: if (wp == NULL)
441: return;
442: for (lc = wp->layout_cell->parent; lc != NULL; lc = lc->parent) {
1.103 nicm 443: for (lc2 = lcparent; lc2 != NULL; lc2 = lc2->parent) {
1.100 nicm 444: if (lc == lc2)
445: return;
446: }
447: lc->lastwp = wp;
448: }
449: }
450:
1.1 nicm 451: void
452: window_set_active_pane(struct window *w, struct window_pane *wp)
453: {
1.59 nicm 454: if (wp == w->active)
455: return;
1.58 nicm 456: w->last = w->active;
1.1 nicm 457: w->active = wp;
1.100 nicm 458: window_pane_active_lost(w->last, wp);
1.10 nicm 459: while (!window_pane_visible(w->active)) {
1.1 nicm 460: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 461: if (w->active == NULL)
462: w->active = TAILQ_LAST(&w->panes, window_panes);
463: if (w->active == wp)
464: return;
1.29 nicm 465: }
466: }
467:
1.66 nicm 468: struct window_pane *
469: window_get_active_at(struct window *w, u_int x, u_int y)
1.29 nicm 470: {
471: struct window_pane *wp;
472:
473: TAILQ_FOREACH(wp, &w->panes, entry) {
1.66 nicm 474: if (!window_pane_visible(wp))
1.29 nicm 475: continue;
1.66 nicm 476: if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29 nicm 477: continue;
1.66 nicm 478: if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29 nicm 479: continue;
1.66 nicm 480: return (wp);
481: }
482: return (NULL);
483: }
484:
485: void
486: window_set_active_at(struct window *w, u_int x, u_int y)
487: {
488: struct window_pane *wp;
489:
490: wp = window_get_active_at(w, x, y);
491: if (wp != NULL && wp != w->active)
1.29 nicm 492: window_set_active_pane(w, wp);
1.66 nicm 493: }
494:
495: struct window_pane *
496: window_find_string(struct window *w, const char *s)
497: {
498: u_int x, y;
499:
500: x = w->sx / 2;
501: y = w->sy / 2;
502:
503: if (strcasecmp(s, "top") == 0)
504: y = 0;
505: else if (strcasecmp(s, "bottom") == 0)
506: y = w->sy - 1;
507: else if (strcasecmp(s, "left") == 0)
508: x = 0;
509: else if (strcasecmp(s, "right") == 0)
510: x = w->sx - 1;
511: else if (strcasecmp(s, "top-left") == 0) {
512: x = 0;
513: y = 0;
514: } else if (strcasecmp(s, "top-right") == 0) {
515: x = w->sx - 1;
516: y = 0;
517: } else if (strcasecmp(s, "bottom-left") == 0) {
518: x = 0;
519: y = w->sy - 1;
520: } else if (strcasecmp(s, "bottom-right") == 0) {
521: x = w->sx - 1;
522: y = w->sy - 1;
523: } else
524: return (NULL);
525:
526: return (window_get_active_at(w, x, y));
1.1 nicm 527: }
528:
1.93 nicm 529: int
530: window_zoom(struct window_pane *wp)
531: {
532: struct window *w = wp->window;
533: struct window_pane *wp1;
534:
535: if (w->flags & WINDOW_ZOOMED)
536: return (-1);
537:
538: if (!window_pane_visible(wp))
539: return (-1);
1.94 nicm 540:
541: if (window_count_panes(w) == 1)
542: return (-1);
543:
1.93 nicm 544: if (w->active != wp)
545: window_set_active_pane(w, wp);
546:
547: TAILQ_FOREACH(wp1, &w->panes, entry) {
548: wp1->saved_layout_cell = wp1->layout_cell;
549: wp1->layout_cell = NULL;
550: }
551:
552: w->saved_layout_root = w->layout_root;
553: layout_init(w, wp);
554: w->flags |= WINDOW_ZOOMED;
555:
556: return (0);
557: }
558:
559: int
560: window_unzoom(struct window *w)
561: {
1.97 nicm 562: struct window_pane *wp;
1.93 nicm 563:
564: if (!(w->flags & WINDOW_ZOOMED))
565: return (-1);
566:
567: w->flags &= ~WINDOW_ZOOMED;
568: layout_free(w);
569: w->layout_root = w->saved_layout_root;
570:
1.97 nicm 571: TAILQ_FOREACH(wp, &w->panes, entry) {
572: wp->layout_cell = wp->saved_layout_cell;
573: wp->saved_layout_cell = NULL;
1.93 nicm 574: }
575: layout_fix_panes(w, w->sx, w->sy);
576:
577: return (0);
578: }
579:
1.1 nicm 580: struct window_pane *
1.16 nicm 581: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 582: {
583: struct window_pane *wp;
584:
1.14 nicm 585: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 586: if (TAILQ_EMPTY(&w->panes))
587: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
588: else
589: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
590: return (wp);
591: }
592:
593: void
1.105 ! nicm 594: window_lost_pane(struct window *w, struct window_pane *wp)
1.1 nicm 595: {
1.57 nicm 596: if (wp == w->active) {
1.58 nicm 597: w->active = w->last;
598: w->last = NULL;
599: if (w->active == NULL) {
600: w->active = TAILQ_PREV(wp, window_panes, entry);
601: if (w->active == NULL)
602: w->active = TAILQ_NEXT(wp, entry);
603: }
604: } else if (wp == w->last)
605: w->last = NULL;
1.105 ! nicm 606: }
! 607:
! 608: void
! 609: window_remove_pane(struct window *w, struct window_pane *wp)
! 610: {
! 611: window_lost_pane(w, wp);
1.1 nicm 612:
613: TAILQ_REMOVE(&w->panes, wp, entry);
614: window_pane_destroy(wp);
615: }
616:
617: struct window_pane *
618: window_pane_at_index(struct window *w, u_int idx)
619: {
620: struct window_pane *wp;
621: u_int n;
622:
1.67 nicm 623: n = options_get_number(&w->options, "pane-base-index");
1.1 nicm 624: TAILQ_FOREACH(wp, &w->panes, entry) {
625: if (n == idx)
626: return (wp);
627: n++;
628: }
629: return (NULL);
1.53 nicm 630: }
631:
632: struct window_pane *
633: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
634: {
635: for (; n > 0; n--) {
636: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
637: wp = TAILQ_FIRST(&w->panes);
638: }
639:
640: return (wp);
641: }
642:
643: struct window_pane *
644: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
645: u_int n)
646: {
647: for (; n > 0; n--) {
648: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
649: wp = TAILQ_LAST(&w->panes, window_panes);
650: }
651:
652: return (wp);
1.13 nicm 653: }
654:
1.69 nicm 655: int
656: window_pane_index(struct window_pane *wp, u_int *i)
1.13 nicm 657: {
658: struct window_pane *wq;
1.69 nicm 659: struct window *w = wp->window;
1.13 nicm 660:
1.69 nicm 661: *i = options_get_number(&w->options, "pane-base-index");
1.13 nicm 662: TAILQ_FOREACH(wq, &w->panes, entry) {
1.69 nicm 663: if (wp == wq) {
664: return (0);
665: }
666: (*i)++;
1.13 nicm 667: }
1.69 nicm 668:
669: return (-1);
1.1 nicm 670: }
671:
672: u_int
673: window_count_panes(struct window *w)
674: {
675: struct window_pane *wp;
676: u_int n;
677:
678: n = 0;
679: TAILQ_FOREACH(wp, &w->panes, entry)
680: n++;
681: return (n);
682: }
683:
684: void
685: window_destroy_panes(struct window *w)
686: {
687: struct window_pane *wp;
688:
689: while (!TAILQ_EMPTY(&w->panes)) {
690: wp = TAILQ_FIRST(&w->panes);
691: TAILQ_REMOVE(&w->panes, wp, entry);
692: window_pane_destroy(wp);
693: }
1.61 nicm 694: }
695:
696: /* Return list of printable window flag symbols. No flags is just a space. */
697: char *
698: window_printable_flags(struct session *s, struct winlink *wl)
699: {
700: char flags[BUFSIZ];
701: int pos;
702:
703: pos = 0;
704: if (wl->flags & WINLINK_ACTIVITY)
705: flags[pos++] = '#';
706: if (wl->flags & WINLINK_BELL)
707: flags[pos++] = '!';
708: if (wl->flags & WINLINK_SILENCE)
709: flags[pos++] = '~';
710: if (wl == s->curw)
711: flags[pos++] = '*';
712: if (wl == TAILQ_FIRST(&s->lastw))
713: flags[pos++] = '-';
1.93 nicm 714: if (wl->window->flags & WINDOW_ZOOMED)
715: flags[pos++] = 'Z';
1.61 nicm 716: if (pos == 0)
717: flags[pos++] = ' ';
718: flags[pos] = '\0';
719: return (xstrdup(flags));
1.1 nicm 720: }
721:
1.64 nicm 722: /* Find pane in global tree by id. */
723: struct window_pane *
724: window_pane_find_by_id(u_int id)
725: {
726: struct window_pane wp;
727:
728: wp.id = id;
729: return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
730: }
731:
1.1 nicm 732: struct window_pane *
733: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
734: {
735: struct window_pane *wp;
736:
737: wp = xcalloc(1, sizeof *wp);
738: wp->window = w;
739:
1.71 nicm 740: wp->id = next_window_pane_id++;
1.64 nicm 741: RB_INSERT(window_pane_tree, &all_window_panes, wp);
742:
1.1 nicm 743: wp->cmd = NULL;
1.23 nicm 744: wp->shell = NULL;
1.99 nicm 745: wp->cwd = -1;
1.1 nicm 746:
747: wp->fd = -1;
1.37 nicm 748: wp->event = NULL;
1.1 nicm 749:
750: wp->mode = NULL;
1.14 nicm 751:
752: wp->layout_cell = NULL;
1.1 nicm 753:
754: wp->xoff = 0;
1.42 nicm 755: wp->yoff = 0;
1.1 nicm 756:
757: wp->sx = sx;
758: wp->sy = sy;
759:
1.32 nicm 760: wp->pipe_fd = -1;
761: wp->pipe_off = 0;
1.36 nicm 762: wp->pipe_event = NULL;
1.32 nicm 763:
1.9 nicm 764: wp->saved_grid = NULL;
765:
1.1 nicm 766: screen_init(&wp->base, sx, sy, hlimit);
767: wp->screen = &wp->base;
768:
769: input_init(wp);
770:
771: return (wp);
772: }
773:
774: void
775: window_pane_destroy(struct window_pane *wp)
776: {
1.100 nicm 777: struct window_pane *wp2;
778:
779: /* Forget removed pane in all layout cells that remember it. */
780: RB_FOREACH(wp2, window_pane_tree, &all_window_panes) {
781: if (wp2->layout_cell != NULL &&
782: wp2->layout_cell->parent != NULL &&
783: wp2->layout_cell->parent->lastwp == wp)
784: wp2->layout_cell->parent->lastwp = NULL;
785: }
786:
1.55 nicm 787: window_pane_reset_mode(wp);
788:
1.76 nicm 789: if (event_initialized(&wp->changes_timer))
790: evtimer_del(&wp->changes_timer);
1.75 nicm 791:
1.37 nicm 792: if (wp->fd != -1) {
1.70 nicm 793: bufferevent_free(wp->event);
1.1 nicm 794: close(wp->fd);
1.37 nicm 795: }
1.1 nicm 796:
797: input_free(wp);
798:
799: screen_free(&wp->base);
1.9 nicm 800: if (wp->saved_grid != NULL)
801: grid_destroy(wp->saved_grid);
1.1 nicm 802:
1.32 nicm 803: if (wp->pipe_fd != -1) {
1.70 nicm 804: bufferevent_free(wp->pipe_event);
1.32 nicm 805: close(wp->pipe_fd);
806: }
807:
1.64 nicm 808: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
809:
1.99 nicm 810: close(wp->cwd);
1.82 nicm 811: free(wp->shell);
812: free(wp->cmd);
813: free(wp);
1.1 nicm 814: }
815:
816: int
1.23 nicm 817: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.99 nicm 818: int cwd, struct environ *env, struct termios *tio, char **cause)
1.1 nicm 819: {
1.47 nicm 820: struct winsize ws;
1.64 nicm 821: char *argv0, paneid[16];
1.47 nicm 822: const char *ptr;
823: struct termios tio2;
1.1 nicm 824:
1.37 nicm 825: if (wp->fd != -1) {
1.70 nicm 826: bufferevent_free(wp->event);
1.1 nicm 827: close(wp->fd);
1.37 nicm 828: }
1.1 nicm 829: if (cmd != NULL) {
1.82 nicm 830: free(wp->cmd);
1.1 nicm 831: wp->cmd = xstrdup(cmd);
832: }
1.23 nicm 833: if (shell != NULL) {
1.82 nicm 834: free(wp->shell);
1.23 nicm 835: wp->shell = xstrdup(shell);
836: }
1.99 nicm 837: if (cwd != -1) {
838: close(wp->cwd);
839: wp->cwd = dup(cwd);
1.1 nicm 840: }
1.91 nicm 841:
842: log_debug("spawn: %s -- %s", wp->shell, wp->cmd);
1.1 nicm 843:
844: memset(&ws, 0, sizeof ws);
845: ws.ws_col = screen_size_x(&wp->base);
846: ws.ws_row = screen_size_y(&wp->base);
847:
1.42 nicm 848: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 849: case -1:
850: wp->fd = -1;
851: xasprintf(cause, "%s: %s", cmd, strerror(errno));
852: return (-1);
853: case 0:
1.99 nicm 854: if (fchdir(wp->cwd) != 0)
1.1 nicm 855: chdir("/");
1.25 nicm 856:
857: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
858: fatal("tcgetattr failed");
859: if (tio != NULL)
860: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
861: tio2.c_cc[VERASE] = '\177';
862: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
863: fatal("tcgetattr failed");
1.18 nicm 864:
1.56 nicm 865: closefrom(STDERR_FILENO + 1);
866:
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: }