Annotation of src/usr.bin/tmux/window.c, Revision 1.104
1.104 ! nicm 1: /* $OpenBSD: window.c,v 1.103 2014/03/31 21:41:07 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
594: window_remove_pane(struct window *w, struct window_pane *wp)
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.1 nicm 606:
607: TAILQ_REMOVE(&w->panes, wp, entry);
608: window_pane_destroy(wp);
609: }
610:
611: struct window_pane *
612: window_pane_at_index(struct window *w, u_int idx)
613: {
614: struct window_pane *wp;
615: u_int n;
616:
1.67 nicm 617: n = options_get_number(&w->options, "pane-base-index");
1.1 nicm 618: TAILQ_FOREACH(wp, &w->panes, entry) {
619: if (n == idx)
620: return (wp);
621: n++;
622: }
623: return (NULL);
1.53 nicm 624: }
625:
626: struct window_pane *
627: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
628: {
629: for (; n > 0; n--) {
630: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
631: wp = TAILQ_FIRST(&w->panes);
632: }
633:
634: return (wp);
635: }
636:
637: struct window_pane *
638: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
639: u_int n)
640: {
641: for (; n > 0; n--) {
642: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
643: wp = TAILQ_LAST(&w->panes, window_panes);
644: }
645:
646: return (wp);
1.13 nicm 647: }
648:
1.69 nicm 649: int
650: window_pane_index(struct window_pane *wp, u_int *i)
1.13 nicm 651: {
652: struct window_pane *wq;
1.69 nicm 653: struct window *w = wp->window;
1.13 nicm 654:
1.69 nicm 655: *i = options_get_number(&w->options, "pane-base-index");
1.13 nicm 656: TAILQ_FOREACH(wq, &w->panes, entry) {
1.69 nicm 657: if (wp == wq) {
658: return (0);
659: }
660: (*i)++;
1.13 nicm 661: }
1.69 nicm 662:
663: return (-1);
1.1 nicm 664: }
665:
666: u_int
667: window_count_panes(struct window *w)
668: {
669: struct window_pane *wp;
670: u_int n;
671:
672: n = 0;
673: TAILQ_FOREACH(wp, &w->panes, entry)
674: n++;
675: return (n);
676: }
677:
678: void
679: window_destroy_panes(struct window *w)
680: {
681: struct window_pane *wp;
682:
683: while (!TAILQ_EMPTY(&w->panes)) {
684: wp = TAILQ_FIRST(&w->panes);
685: TAILQ_REMOVE(&w->panes, wp, entry);
686: window_pane_destroy(wp);
687: }
1.61 nicm 688: }
689:
690: /* Return list of printable window flag symbols. No flags is just a space. */
691: char *
692: window_printable_flags(struct session *s, struct winlink *wl)
693: {
694: char flags[BUFSIZ];
695: int pos;
696:
697: pos = 0;
698: if (wl->flags & WINLINK_ACTIVITY)
699: flags[pos++] = '#';
700: if (wl->flags & WINLINK_BELL)
701: flags[pos++] = '!';
702: if (wl->flags & WINLINK_SILENCE)
703: flags[pos++] = '~';
704: if (wl == s->curw)
705: flags[pos++] = '*';
706: if (wl == TAILQ_FIRST(&s->lastw))
707: flags[pos++] = '-';
1.93 nicm 708: if (wl->window->flags & WINDOW_ZOOMED)
709: flags[pos++] = 'Z';
1.61 nicm 710: if (pos == 0)
711: flags[pos++] = ' ';
712: flags[pos] = '\0';
713: return (xstrdup(flags));
1.1 nicm 714: }
715:
1.64 nicm 716: /* Find pane in global tree by id. */
717: struct window_pane *
718: window_pane_find_by_id(u_int id)
719: {
720: struct window_pane wp;
721:
722: wp.id = id;
723: return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
724: }
725:
1.1 nicm 726: struct window_pane *
727: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
728: {
729: struct window_pane *wp;
730:
731: wp = xcalloc(1, sizeof *wp);
732: wp->window = w;
733:
1.71 nicm 734: wp->id = next_window_pane_id++;
1.64 nicm 735: RB_INSERT(window_pane_tree, &all_window_panes, wp);
736:
1.1 nicm 737: wp->cmd = NULL;
1.23 nicm 738: wp->shell = NULL;
1.99 nicm 739: wp->cwd = -1;
1.1 nicm 740:
741: wp->fd = -1;
1.37 nicm 742: wp->event = NULL;
1.1 nicm 743:
744: wp->mode = NULL;
1.14 nicm 745:
746: wp->layout_cell = NULL;
1.1 nicm 747:
748: wp->xoff = 0;
1.42 nicm 749: wp->yoff = 0;
1.1 nicm 750:
751: wp->sx = sx;
752: wp->sy = sy;
753:
1.32 nicm 754: wp->pipe_fd = -1;
755: wp->pipe_off = 0;
1.36 nicm 756: wp->pipe_event = NULL;
1.32 nicm 757:
1.9 nicm 758: wp->saved_grid = NULL;
759:
1.1 nicm 760: screen_init(&wp->base, sx, sy, hlimit);
761: wp->screen = &wp->base;
762:
763: input_init(wp);
764:
765: return (wp);
766: }
767:
768: void
769: window_pane_destroy(struct window_pane *wp)
770: {
1.100 nicm 771: struct window_pane *wp2;
772:
773: /* Forget removed pane in all layout cells that remember it. */
774: RB_FOREACH(wp2, window_pane_tree, &all_window_panes) {
775: if (wp2->layout_cell != NULL &&
776: wp2->layout_cell->parent != NULL &&
777: wp2->layout_cell->parent->lastwp == wp)
778: wp2->layout_cell->parent->lastwp = NULL;
779: }
780:
1.55 nicm 781: window_pane_reset_mode(wp);
782:
1.76 nicm 783: if (event_initialized(&wp->changes_timer))
784: evtimer_del(&wp->changes_timer);
1.75 nicm 785:
1.37 nicm 786: if (wp->fd != -1) {
1.70 nicm 787: bufferevent_free(wp->event);
1.1 nicm 788: close(wp->fd);
1.37 nicm 789: }
1.1 nicm 790:
791: input_free(wp);
792:
793: screen_free(&wp->base);
1.9 nicm 794: if (wp->saved_grid != NULL)
795: grid_destroy(wp->saved_grid);
1.1 nicm 796:
1.32 nicm 797: if (wp->pipe_fd != -1) {
1.70 nicm 798: bufferevent_free(wp->pipe_event);
1.32 nicm 799: close(wp->pipe_fd);
800: }
801:
1.64 nicm 802: RB_REMOVE(window_pane_tree, &all_window_panes, wp);
803:
1.99 nicm 804: close(wp->cwd);
1.82 nicm 805: free(wp->shell);
806: free(wp->cmd);
807: free(wp);
1.1 nicm 808: }
809:
810: int
1.23 nicm 811: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.99 nicm 812: int cwd, struct environ *env, struct termios *tio, char **cause)
1.1 nicm 813: {
1.47 nicm 814: struct winsize ws;
1.64 nicm 815: char *argv0, paneid[16];
1.47 nicm 816: const char *ptr;
817: struct termios tio2;
1.1 nicm 818:
1.37 nicm 819: if (wp->fd != -1) {
1.70 nicm 820: bufferevent_free(wp->event);
1.1 nicm 821: close(wp->fd);
1.37 nicm 822: }
1.1 nicm 823: if (cmd != NULL) {
1.82 nicm 824: free(wp->cmd);
1.1 nicm 825: wp->cmd = xstrdup(cmd);
826: }
1.23 nicm 827: if (shell != NULL) {
1.82 nicm 828: free(wp->shell);
1.23 nicm 829: wp->shell = xstrdup(shell);
830: }
1.99 nicm 831: if (cwd != -1) {
832: close(wp->cwd);
833: wp->cwd = dup(cwd);
1.1 nicm 834: }
1.91 nicm 835:
836: log_debug("spawn: %s -- %s", wp->shell, wp->cmd);
1.1 nicm 837:
838: memset(&ws, 0, sizeof ws);
839: ws.ws_col = screen_size_x(&wp->base);
840: ws.ws_row = screen_size_y(&wp->base);
841:
1.42 nicm 842: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 843: case -1:
844: wp->fd = -1;
845: xasprintf(cause, "%s: %s", cmd, strerror(errno));
846: return (-1);
847: case 0:
1.99 nicm 848: if (fchdir(wp->cwd) != 0)
1.1 nicm 849: chdir("/");
1.25 nicm 850:
851: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
852: fatal("tcgetattr failed");
853: if (tio != NULL)
854: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
855: tio2.c_cc[VERASE] = '\177';
856: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
857: fatal("tcgetattr failed");
1.18 nicm 858:
1.56 nicm 859: closefrom(STDERR_FILENO + 1);
860:
1.64 nicm 861: xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
862: environ_set(env, "TMUX_PANE", paneid);
1.47 nicm 863: environ_push(env);
1.18 nicm 864:
1.54 nicm 865: clear_signals(1);
1.1 nicm 866: log_close();
867:
1.80 nicm 868: setenv("SHELL", wp->shell, 1);
869: ptr = strrchr(wp->shell, '/');
870:
1.8 nicm 871: if (*wp->cmd != '\0') {
1.80 nicm 872: /* Use the command. */
873: if (ptr != NULL && *(ptr + 1) != '\0')
874: xasprintf(&argv0, "%s", ptr + 1);
875: else
876: xasprintf(&argv0, "%s", wp->shell);
877: execl(wp->shell, argv0, "-c", wp->cmd, (char *) NULL);
1.8 nicm 878: fatal("execl failed");
879: }
880:
881: /* No command; fork a login shell. */
1.23 nicm 882: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 883: xasprintf(&argv0, "-%s", ptr + 1);
884: else
1.23 nicm 885: xasprintf(&argv0, "-%s", wp->shell);
886: execl(wp->shell, argv0, (char *) NULL);
1.1 nicm 887: fatal("execl failed");
888: }
889:
1.62 nicm 890: setblocking(wp->fd, 0);
891:
1.37 nicm 892: wp->event = bufferevent_new(wp->fd,
893: window_pane_read_callback, NULL, window_pane_error_callback, wp);
894: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 895:
896: return (0);
1.75 nicm 897: }
898:
899: void
900: window_pane_timer_start(struct window_pane *wp)
901: {
902: struct timeval tv;
903:
904: tv.tv_sec = 0;
905: tv.tv_usec = 1000;
906:
907: evtimer_del(&wp->changes_timer);
908: evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp);
909: evtimer_add(&wp->changes_timer, &tv);
910: }
911:
912: void
913: window_pane_timer_callback(unused int fd, unused short events, void *data)
914: {
915: struct window_pane *wp = data;
916: struct window *w = wp->window;
917: u_int interval, trigger;
918:
919: interval = options_get_number(&w->options, "c0-change-interval");
920: trigger = options_get_number(&w->options, "c0-change-trigger");
921:
922: if (wp->changes_redraw++ == interval) {
923: wp->flags |= PANE_REDRAW;
924: wp->changes_redraw = 0;
925:
926: }
927:
928: if (trigger == 0 || wp->changes < trigger) {
929: wp->flags |= PANE_REDRAW;
930: wp->flags &= ~PANE_DROP;
931: } else
932: window_pane_timer_start(wp);
933: wp->changes = 0;
1.1 nicm 934: }
935:
1.15 nicm 936: void
1.37 nicm 937: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
938: {
1.46 nicm 939: struct window_pane *wp = data;
940: char *new_data;
941: size_t new_size;
942:
943: new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
944: if (wp->pipe_fd != -1 && new_size > 0) {
945: new_data = EVBUFFER_DATA(wp->event->input);
946: bufferevent_write(wp->pipe_event, new_data, new_size);
947: }
948:
949: input_parse(wp);
1.37 nicm 950:
1.46 nicm 951: wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.60 nicm 952:
953: /*
954: * If we get here, we're not outputting anymore, so set the silence
955: * flag on the window.
956: */
957: wp->window->flags |= WINDOW_SILENCE;
958: if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
959: fatal("gettimeofday failed.");
1.37 nicm 960: }
961:
962: void
963: window_pane_error_callback(
964: unused struct bufferevent *bufev, unused short what, void *data)
965: {
966: struct window_pane *wp = data;
967:
1.39 nicm 968: server_destroy_pane(wp);
1.37 nicm 969: }
970:
971: void
1.1 nicm 972: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
973: {
974: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 975: return;
1.1 nicm 976: wp->sx = sx;
977: wp->sy = sy;
978:
1.89 nicm 979: screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1 nicm 980: if (wp->mode != NULL)
981: wp->mode->resize(wp, sx, sy);
1.95 nicm 982:
983: wp->flags |= PANE_RESIZE;
1.44 nicm 984: }
985:
986: /*
987: * Enter alternative screen mode. A copy of the visible screen is saved and the
988: * history is not updated
989: */
990: void
1.87 nicm 991: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
992: int cursor)
1.44 nicm 993: {
994: struct screen *s = &wp->base;
995: u_int sx, sy;
996:
997: if (wp->saved_grid != NULL)
998: return;
999: if (!options_get_number(&wp->window->options, "alternate-screen"))
1000: return;
1001: sx = screen_size_x(s);
1002: sy = screen_size_y(s);
1003:
1004: wp->saved_grid = grid_create(sx, sy, 0);
1005: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87 nicm 1006: if (cursor) {
1007: wp->saved_cx = s->cx;
1008: wp->saved_cy = s->cy;
1009: }
1.44 nicm 1010: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
1011:
1012: grid_view_clear(s->grid, 0, 0, sx, sy);
1013:
1014: wp->base.grid->flags &= ~GRID_HISTORY;
1015:
1016: wp->flags |= PANE_REDRAW;
1017: }
1018:
1019: /* Exit alternate screen mode and restore the copied grid. */
1020: void
1.87 nicm 1021: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
1022: int cursor)
1.44 nicm 1023: {
1024: struct screen *s = &wp->base;
1025: u_int sx, sy;
1026:
1027: if (wp->saved_grid == NULL)
1028: return;
1029: if (!options_get_number(&wp->window->options, "alternate-screen"))
1030: return;
1031: sx = screen_size_x(s);
1032: sy = screen_size_y(s);
1033:
1034: /*
1035: * If the current size is bigger, temporarily resize to the old size
1036: * before copying back.
1037: */
1038: if (sy > wp->saved_grid->sy)
1.89 nicm 1039: screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44 nicm 1040:
1041: /* Restore the grid, cursor position and cell. */
1042: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87 nicm 1043: if (cursor)
1044: s->cx = wp->saved_cx;
1.44 nicm 1045: if (s->cx > screen_size_x(s) - 1)
1046: s->cx = screen_size_x(s) - 1;
1.87 nicm 1047: if (cursor)
1048: s->cy = wp->saved_cy;
1.44 nicm 1049: if (s->cy > screen_size_y(s) - 1)
1050: s->cy = screen_size_y(s) - 1;
1051: memcpy(gc, &wp->saved_cell, sizeof *gc);
1052:
1053: /*
1054: * Turn history back on (so resize can use it) and then resize back to
1055: * the current size.
1056: */
1057: wp->base.grid->flags |= GRID_HISTORY;
1.89 nicm 1058: if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
1059: screen_resize(s, sx, sy, 1);
1.44 nicm 1060:
1061: grid_destroy(wp->saved_grid);
1062: wp->saved_grid = NULL;
1063:
1064: wp->flags |= PANE_REDRAW;
1.1 nicm 1065: }
1066:
1067: int
1068: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1069: {
1070: struct screen *s;
1071:
1.15 nicm 1072: if (wp->mode != NULL)
1.1 nicm 1073: return (1);
1074: wp->mode = mode;
1075:
1076: if ((s = wp->mode->init(wp)) != NULL)
1077: wp->screen = s;
1.34 nicm 1078: wp->flags |= PANE_REDRAW;
1.1 nicm 1079: return (0);
1080: }
1081:
1082: void
1083: window_pane_reset_mode(struct window_pane *wp)
1084: {
1085: if (wp->mode == NULL)
1086: return;
1087:
1088: wp->mode->free(wp);
1089: wp->mode = NULL;
1090:
1091: wp->screen = &wp->base;
1.34 nicm 1092: wp->flags |= PANE_REDRAW;
1.1 nicm 1093: }
1094:
1095: void
1.51 nicm 1096: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1 nicm 1097: {
1.27 nicm 1098: struct window_pane *wp2;
1099:
1.30 nicm 1100: if (!window_pane_visible(wp))
1.3 nicm 1101: return;
1102:
1.1 nicm 1103: if (wp->mode != NULL) {
1104: if (wp->mode->key != NULL)
1.51 nicm 1105: wp->mode->key(wp, sess, key);
1.27 nicm 1106: return;
1.30 nicm 1107: }
1.27 nicm 1108:
1.30 nicm 1109: if (wp->fd == -1)
1110: return;
1.27 nicm 1111: input_key(wp, key);
1112: if (options_get_number(&wp->window->options, "synchronize-panes")) {
1113: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1114: if (wp2 == wp || wp2->mode != NULL)
1115: continue;
1116: if (wp2->fd != -1 && window_pane_visible(wp2))
1117: input_key(wp2, key);
1118: }
1119: }
1.1 nicm 1120: }
1121:
1122: void
1123: window_pane_mouse(
1.51 nicm 1124: struct window_pane *wp, struct session *sess, struct mouse_event *m)
1.1 nicm 1125: {
1.30 nicm 1126: if (!window_pane_visible(wp))
1.3 nicm 1127: return;
1128:
1.31 nicm 1129: if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1 nicm 1130: return;
1.31 nicm 1131: if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1 nicm 1132: return;
1.31 nicm 1133: m->x -= wp->xoff;
1134: m->y -= wp->yoff;
1.1 nicm 1135:
1136: if (wp->mode != NULL) {
1.65 nicm 1137: if (wp->mode->mouse != NULL &&
1138: options_get_number(&wp->window->options, "mode-mouse"))
1.51 nicm 1139: wp->mode->mouse(wp, sess, m);
1.30 nicm 1140: } else if (wp->fd != -1)
1.86 nicm 1141: input_mouse(wp, sess, m);
1.10 nicm 1142: }
1143:
1144: int
1145: window_pane_visible(struct window_pane *wp)
1146: {
1147: struct window *w = wp->window;
1148:
1.93 nicm 1149: if (wp->layout_cell == NULL)
1150: return (0);
1.10 nicm 1151: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1152: return (0);
1153: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1154: return (0);
1155: return (1);
1.1 nicm 1156: }
1157:
1158: char *
1.5 nicm 1159: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 1160: {
1.4 nicm 1161: struct screen *s = &wp->base;
1.5 nicm 1162: char *newsearchstr, *line, *msg;
1.4 nicm 1163: u_int i;
1164:
1.5 nicm 1165: msg = NULL;
1166: xasprintf(&newsearchstr, "*%s*", searchstr);
1167:
1.4 nicm 1168: for (i = 0; i < screen_size_y(s); i++) {
1169: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 1170: if (fnmatch(newsearchstr, line, 0) == 0) {
1171: msg = line;
1172: if (lineno != NULL)
1173: *lineno = i;
1174: break;
1175: }
1.82 nicm 1176: free(line);
1.4 nicm 1177: }
1.5 nicm 1178:
1.82 nicm 1179: free(newsearchstr);
1.5 nicm 1180: return (msg);
1.45 nicm 1181: }
1182:
1183: /* Find the pane directly above another. */
1184: struct window_pane *
1185: window_pane_find_up(struct window_pane *wp)
1186: {
1187: struct window_pane *wp2;
1188: u_int left, top;
1189:
1190: if (wp == NULL || !window_pane_visible(wp))
1191: return (NULL);
1192:
1193: top = wp->yoff;
1194: if (top == 0)
1195: top = wp->window->sy + 1;
1196: left = wp->xoff;
1197:
1198: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1199: if (!window_pane_visible(wp2))
1200: continue;
1201: if (wp2->yoff + wp2->sy + 1 != top)
1202: continue;
1203: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1.100 nicm 1204: return (window_pane_active_set(wp, wp2));
1.45 nicm 1205: }
1206: return (NULL);
1207: }
1208:
1209: /* Find the pane directly below another. */
1210: struct window_pane *
1211: window_pane_find_down(struct window_pane *wp)
1212: {
1213: struct window_pane *wp2;
1214: u_int left, bottom;
1215:
1216: if (wp == NULL || !window_pane_visible(wp))
1217: return (NULL);
1218:
1219: bottom = wp->yoff + wp->sy + 1;
1220: if (bottom >= wp->window->sy)
1221: bottom = 0;
1222: left = wp->xoff;
1223:
1224: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1225: if (!window_pane_visible(wp2))
1226: continue;
1227: if (wp2->yoff != bottom)
1228: continue;
1229: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1.100 nicm 1230: return (window_pane_active_set(wp, wp2));
1.45 nicm 1231: }
1232: return (NULL);
1233: }
1234:
1235: /*
1236: * Find the pane directly to the left of another, adjacent to the left side and
1237: * containing the top edge.
1238: */
1239: struct window_pane *
1240: window_pane_find_left(struct window_pane *wp)
1241: {
1242: struct window_pane *wp2;
1243: u_int left, top;
1244:
1245: if (wp == NULL || !window_pane_visible(wp))
1246: return (NULL);
1247:
1248: left = wp->xoff;
1249: if (left == 0)
1250: left = wp->window->sx + 1;
1251: top = wp->yoff;
1252:
1253: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1254: if (!window_pane_visible(wp2))
1255: continue;
1256: if (wp2->xoff + wp2->sx + 1 != left)
1257: continue;
1258: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1.100 nicm 1259: return (window_pane_active_set(wp, wp2));
1.45 nicm 1260: }
1261: return (NULL);
1262: }
1263:
1264: /*
1265: * Find the pane directly to the right of another, that is adjacent to the
1266: * right edge and including the top edge.
1267: */
1268: struct window_pane *
1269: window_pane_find_right(struct window_pane *wp)
1270: {
1271: struct window_pane *wp2;
1272: u_int right, top;
1273:
1274: if (wp == NULL || !window_pane_visible(wp))
1275: return (NULL);
1276:
1277: right = wp->xoff + wp->sx + 1;
1278: if (right >= wp->window->sx)
1279: right = 0;
1280: top = wp->yoff;
1281:
1282: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1283: if (!window_pane_visible(wp2))
1284: continue;
1285: if (wp2->xoff != right)
1286: continue;
1287: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1.100 nicm 1288: return (window_pane_active_set(wp, wp2));
1.45 nicm 1289: }
1290: return (NULL);
1.81 nicm 1291: }
1292:
1293: /* Clear alert flags for a winlink */
1294: void
1295: winlink_clear_flags(struct winlink *wl)
1296: {
1297: struct winlink *wm;
1298: struct session *s;
1299: struct window *w;
1300: u_int i;
1301:
1302: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1303: if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1304: continue;
1305:
1306: RB_FOREACH(s, sessions, &sessions) {
1307: if ((wm = session_has(s, w)) == NULL)
1308: continue;
1309:
1310: if (wm->window != wl->window)
1311: continue;
1312: if ((wm->flags & WINLINK_ALERTFLAGS) == 0)
1313: continue;
1314:
1315: wm->flags &= ~WINLINK_ALERTFLAGS;
1.98 nicm 1316: wm->window->flags &= ~WINDOW_ALERTFLAGS;
1.81 nicm 1317: server_status_session(s);
1318: }
1319: }
1.1 nicm 1320: }