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