Annotation of src/usr.bin/tmux/window.c, Revision 1.21
1.21 ! nicm 1: /* $OpenBSD: window.c,v 1.20 2009/08/12 09:14:25 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:
59: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
60:
1.8 nicm 61: const char *
62: window_default_command(void)
63: {
1.19 nicm 64: const char *shell, *ptr;
1.20 nicm 65: char *progname;
1.8 nicm 66: struct passwd *pw;
67:
68: shell = getenv("SHELL");
69: if (shell != NULL && *shell != '\0')
1.19 nicm 70: goto found;
1.8 nicm 71:
72: pw = getpwuid(getuid());
1.19 nicm 73: if (pw != NULL && pw->pw_shell != NULL && *pw->pw_shell != '\0') {
74: shell = pw->pw_shell;
75: goto found;
76: }
1.8 nicm 77:
78: return (_PATH_BSHELL);
1.19 nicm 79:
80: found:
81: if ((ptr = strrchr(shell, '/')) != NULL)
82: ptr++;
83: else
84: ptr = shell;
1.20 nicm 85: progname = __progname;
86: if (*progname == '-')
87: progname++;
88: if (strcmp(ptr, progname) == 0)
1.19 nicm 89: return (_PATH_BSHELL);
90: return (shell);
1.8 nicm 91: }
92:
1.1 nicm 93: int
94: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
95: {
96: return (wl1->idx - wl2->idx);
1.12 nicm 97: }
98:
99: struct winlink *
100: winlink_find_by_window(struct winlinks *wwl, struct window *w)
101: {
102: struct winlink *wl;
103:
104: RB_FOREACH(wl, winlinks, wwl) {
105: if (wl->window == w)
106: return (wl);
107: }
108:
109: return (NULL);
1.1 nicm 110: }
111:
112: struct winlink *
113: winlink_find_by_index(struct winlinks *wwl, int idx)
114: {
115: struct winlink wl;
116:
117: if (idx < 0)
118: fatalx("bad index");
119:
120: wl.idx = idx;
121: return (RB_FIND(winlinks, wwl, &wl));
122: }
123:
124: int
125: winlink_next_index(struct winlinks *wwl)
126: {
127: u_int i;
128:
129: for (i = 0; i < INT_MAX; i++) {
130: if (winlink_find_by_index(wwl, i) == NULL)
131: return (i);
132: }
133:
134: fatalx("no free indexes");
135: }
136:
137: u_int
138: winlink_count(struct winlinks *wwl)
139: {
140: struct winlink *wl;
141: u_int n;
142:
143: n = 0;
144: RB_FOREACH(wl, winlinks, wwl)
145: n++;
146:
147: return (n);
148: }
149:
150: struct winlink *
151: winlink_add(struct winlinks *wwl, struct window *w, int idx)
152: {
153: struct winlink *wl;
154:
155: if (idx == -1)
156: idx = winlink_next_index(wwl);
157: else if (winlink_find_by_index(wwl, idx) != NULL)
158: return (NULL);
159:
160: if (idx < 0)
161: fatalx("bad index");
162:
163: wl = xcalloc(1, sizeof *wl);
164: wl->idx = idx;
165: wl->window = w;
166: RB_INSERT(winlinks, wwl, wl);
167:
168: w->references++;
169:
170: return (wl);
171: }
172:
173: void
174: winlink_remove(struct winlinks *wwl, struct winlink *wl)
175: {
176: struct window *w = wl->window;
177:
178: RB_REMOVE(winlinks, wwl, wl);
179: xfree(wl);
180:
181: if (w->references == 0)
182: fatal("bad reference count");
183: w->references--;
184: if (w->references == 0)
185: window_destroy(w);
186: }
187:
188: struct winlink *
189: winlink_next(unused struct winlinks *wwl, struct winlink *wl)
190: {
191: return (RB_NEXT(winlinks, wwl, wl));
192: }
193:
194: struct winlink *
195: winlink_previous(unused struct winlinks *wwl, struct winlink *wl)
196: {
197: return (RB_PREV(winlinks, wwl, wl));
198: }
199:
200: void
201: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
202: {
203: if (wl == NULL)
204: return;
205:
206: winlink_stack_remove(stack, wl);
207: SLIST_INSERT_HEAD(stack, wl, sentry);
208: }
209:
210: void
211: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
212: {
213: struct winlink *wl2;
214:
215: if (wl == NULL)
216: return;
217:
218: SLIST_FOREACH(wl2, stack, sentry) {
219: if (wl2 == wl) {
220: SLIST_REMOVE(stack, wl, winlink, sentry);
221: return;
222: }
223: }
224: }
225:
226: int
227: window_index(struct window *s, u_int *i)
228: {
229: for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
230: if (s == ARRAY_ITEM(&windows, *i))
231: return (0);
232: }
233: return (-1);
234: }
235:
236: struct window *
237: window_create1(u_int sx, u_int sy)
238: {
239: struct window *w;
240: u_int i;
241:
242: w = xmalloc(sizeof *w);
243: w->name = NULL;
244: w->flags = 0;
245:
246: TAILQ_INIT(&w->panes);
247: w->active = NULL;
1.14 nicm 248:
1.17 nicm 249: w->lastlayout = -1;
1.14 nicm 250: w->layout_root = NULL;
251:
1.1 nicm 252: w->sx = sx;
253: w->sy = sy;
254:
1.7 nicm 255: options_init(&w->options, &global_w_options);
1.1 nicm 256:
257: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
258: if (ARRAY_ITEM(&windows, i) == NULL) {
259: ARRAY_SET(&windows, i, w);
260: break;
261: }
262: }
263: if (i == ARRAY_LENGTH(&windows))
264: ARRAY_ADD(&windows, w);
265: w->references = 0;
266:
267: return (w);
268: }
269:
270: struct window *
271: window_create(const char *name, const char *cmd, const char *cwd,
1.21 ! nicm 272: struct environ *env, struct termios *tio, u_int sx, u_int sy, u_int hlimit,
! 273: char **cause)
1.1 nicm 274: {
1.14 nicm 275: struct window *w;
276: struct window_pane *wp;
1.1 nicm 277:
278: w = window_create1(sx, sy);
1.16 nicm 279: wp = window_add_pane(w, hlimit);
1.14 nicm 280: layout_init(w);
1.21 ! nicm 281: if (window_pane_spawn(wp, cmd, cwd, env, tio, cause) != 0) {
1.1 nicm 282: window_destroy(w);
283: return (NULL);
284: }
285: w->active = TAILQ_FIRST(&w->panes);
286: if (name != NULL) {
287: w->name = xstrdup(name);
288: options_set_number(&w->options, "automatic-rename", 0);
289: } else
290: w->name = default_window_name(w);
291: return (w);
292: }
293:
294: void
295: window_destroy(struct window *w)
296: {
297: u_int i;
298:
299: if (window_index(w, &i) != 0)
300: fatalx("index not found");
301: ARRAY_SET(&windows, i, NULL);
302: while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
303: ARRAY_TRUNC(&windows, 1);
304:
1.14 nicm 305: if (w->layout_root != NULL)
306: layout_free(w);
307:
1.1 nicm 308: options_free(&w->options);
309:
310: window_destroy_panes(w);
311:
312: if (w->name != NULL)
313: xfree(w->name);
314: xfree(w);
315: }
316:
1.15 nicm 317: void
1.1 nicm 318: window_resize(struct window *w, u_int sx, u_int sy)
319: {
320: w->sx = sx;
321: w->sy = sy;
322: }
323:
324: void
325: window_set_active_pane(struct window *w, struct window_pane *wp)
326: {
327: w->active = wp;
1.10 nicm 328: while (!window_pane_visible(w->active)) {
1.1 nicm 329: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 330: if (w->active == NULL)
331: w->active = TAILQ_LAST(&w->panes, window_panes);
332: if (w->active == wp)
333: return;
334: }
1.1 nicm 335: }
336:
337: struct window_pane *
1.16 nicm 338: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 339: {
340: struct window_pane *wp;
341:
1.14 nicm 342: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 343: if (TAILQ_EMPTY(&w->panes))
344: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
345: else
346: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
347: return (wp);
348: }
349:
350: void
351: window_remove_pane(struct window *w, struct window_pane *wp)
352: {
353: w->active = TAILQ_PREV(wp, window_panes, entry);
354: if (w->active == NULL)
355: w->active = TAILQ_NEXT(wp, entry);
356:
357: TAILQ_REMOVE(&w->panes, wp, entry);
358: window_pane_destroy(wp);
359: }
360:
361: struct window_pane *
362: window_pane_at_index(struct window *w, u_int idx)
363: {
364: struct window_pane *wp;
365: u_int n;
366:
367: n = 0;
368: TAILQ_FOREACH(wp, &w->panes, entry) {
369: if (n == idx)
370: return (wp);
371: n++;
372: }
373: return (NULL);
1.13 nicm 374: }
375:
376: u_int
377: window_pane_index(struct window *w, struct window_pane *wp)
378: {
379: struct window_pane *wq;
380: u_int n;
381:
382: n = 0;
383: TAILQ_FOREACH(wq, &w->panes, entry) {
384: if (wp == wq)
385: break;
386: n++;
387: }
388: return (n);
1.1 nicm 389: }
390:
391: u_int
392: window_count_panes(struct window *w)
393: {
394: struct window_pane *wp;
395: u_int n;
396:
397: n = 0;
398: TAILQ_FOREACH(wp, &w->panes, entry)
399: n++;
400: return (n);
401: }
402:
403: void
404: window_destroy_panes(struct window *w)
405: {
406: struct window_pane *wp;
407:
408: while (!TAILQ_EMPTY(&w->panes)) {
409: wp = TAILQ_FIRST(&w->panes);
410: TAILQ_REMOVE(&w->panes, wp, entry);
411: window_pane_destroy(wp);
412: }
413: }
414:
415: struct window_pane *
416: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
417: {
418: struct window_pane *wp;
419:
420: wp = xcalloc(1, sizeof *wp);
421: wp->window = w;
422:
423: wp->cmd = NULL;
424: wp->cwd = NULL;
425:
426: wp->fd = -1;
427: wp->in = buffer_create(BUFSIZ);
428: wp->out = buffer_create(BUFSIZ);
429:
430: wp->mode = NULL;
1.14 nicm 431:
432: wp->layout_cell = NULL;
1.1 nicm 433:
434: wp->xoff = 0;
435: wp->yoff = 0;
436:
437: wp->sx = sx;
438: wp->sy = sy;
439:
1.9 nicm 440: wp->saved_grid = NULL;
441:
1.1 nicm 442: screen_init(&wp->base, sx, sy, hlimit);
443: wp->screen = &wp->base;
444:
445: input_init(wp);
446:
447: return (wp);
448: }
449:
450: void
451: window_pane_destroy(struct window_pane *wp)
452: {
453: if (wp->fd != -1)
454: close(wp->fd);
455:
456: input_free(wp);
457:
458: window_pane_reset_mode(wp);
459: screen_free(&wp->base);
1.9 nicm 460: if (wp->saved_grid != NULL)
461: grid_destroy(wp->saved_grid);
1.1 nicm 462:
463: buffer_destroy(wp->in);
464: buffer_destroy(wp->out);
465:
466: if (wp->cwd != NULL)
467: xfree(wp->cwd);
468: if (wp->cmd != NULL)
469: xfree(wp->cmd);
470: xfree(wp);
471: }
472:
473: int
1.21 ! nicm 474: window_pane_spawn(struct window_pane *wp, const char *cmd,
! 475: const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1 nicm 476: {
1.18 nicm 477: struct winsize ws;
478: int mode;
479: char *argv0, **varp, *var;
480: ARRAY_DECL(, char *) varlist;
481: struct environ_entry *envent;
482: const char *ptr;
483: struct timeval tv;
484: u_int i;
1.1 nicm 485:
486: if (wp->fd != -1)
487: close(wp->fd);
488: if (cmd != NULL) {
489: if (wp->cmd != NULL)
490: xfree(wp->cmd);
491: wp->cmd = xstrdup(cmd);
492: }
493: if (cwd != NULL) {
494: if (wp->cwd != NULL)
495: xfree(wp->cwd);
496: wp->cwd = xstrdup(cwd);
497: }
498:
499: memset(&ws, 0, sizeof ws);
500: ws.ws_col = screen_size_x(&wp->base);
501: ws.ws_row = screen_size_y(&wp->base);
502:
503: if (gettimeofday(&wp->window->name_timer, NULL) != 0)
504: fatal("gettimeofday");
505: tv.tv_sec = 0;
506: tv.tv_usec = NAME_INTERVAL * 1000L;
507: timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
508:
1.21 ! nicm 509: switch (wp->pid = forkpty(&wp->fd, wp->tty, tio, &ws)) {
1.1 nicm 510: case -1:
511: wp->fd = -1;
512: xasprintf(cause, "%s: %s", cmd, strerror(errno));
513: return (-1);
514: case 0:
515: if (chdir(wp->cwd) != 0)
516: chdir("/");
1.18 nicm 517:
518: ARRAY_INIT(&varlist);
519: for (varp = environ; *varp != NULL; varp++) {
520: var = xstrdup(*varp);
521: var[strcspn(var, "=")] = '\0';
522: ARRAY_ADD(&varlist, var);
523: }
524: for (i = 0; i < ARRAY_LENGTH(&varlist); i++) {
525: var = ARRAY_ITEM(&varlist, i);
526: unsetenv(var);
1.1 nicm 527: }
1.18 nicm 528: RB_FOREACH(envent, environ, env) {
529: if (envent->value != NULL)
530: setenv(envent->name, envent->value, 1);
531: }
532:
1.1 nicm 533: sigreset();
534: log_close();
535:
1.8 nicm 536: if (*wp->cmd != '\0') {
537: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
538: fatal("execl failed");
539: }
540:
541: /* No command; fork a login shell. */
542: cmd = window_default_command();
543: if ((ptr = strrchr(cmd, '/')) != NULL && *(ptr + 1) != '\0')
544: xasprintf(&argv0, "-%s", ptr + 1);
545: else
546: xasprintf(&argv0, "-%s", cmd);
547: execl(cmd, argv0, (char *) NULL);
1.1 nicm 548: fatal("execl failed");
549: }
550:
551: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
552: fatal("fcntl failed");
553: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
554: fatal("fcntl failed");
555: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
556: fatal("fcntl failed");
557:
558: return (0);
559: }
560:
1.15 nicm 561: void
1.1 nicm 562: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
563: {
564: struct winsize ws;
565:
566: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 567: return;
1.1 nicm 568: wp->sx = sx;
569: wp->sy = sy;
570:
571: memset(&ws, 0, sizeof ws);
572: ws.ws_col = sx;
573: ws.ws_row = sy;
574:
575: screen_resize(&wp->base, sx, sy);
576: if (wp->mode != NULL)
577: wp->mode->resize(wp, sx, sy);
578:
579: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
580: fatal("ioctl failed");
581: }
582:
583: int
584: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
585: {
586: struct screen *s;
587:
1.15 nicm 588: if (wp->mode != NULL)
1.1 nicm 589: return (1);
590: wp->mode = mode;
591:
592: if ((s = wp->mode->init(wp)) != NULL)
593: wp->screen = s;
594: server_redraw_window(wp->window);
595: return (0);
596: }
597:
598: void
599: window_pane_reset_mode(struct window_pane *wp)
600: {
601: if (wp->mode == NULL)
602: return;
603:
604: wp->mode->free(wp);
605: wp->mode = NULL;
606:
607: wp->screen = &wp->base;
608: server_redraw_window(wp->window);
609: }
610:
611: void
612: window_pane_parse(struct window_pane *wp)
613: {
614: input_parse(wp);
615: }
616:
617: void
618: window_pane_key(struct window_pane *wp, struct client *c, int key)
619: {
1.11 nicm 620: if (wp->fd == -1 || !window_pane_visible(wp))
1.3 nicm 621: return;
622:
1.1 nicm 623: if (wp->mode != NULL) {
624: if (wp->mode->key != NULL)
625: wp->mode->key(wp, c, key);
626: } else
627: input_key(wp, key);
628: }
629:
630: void
631: window_pane_mouse(
632: struct window_pane *wp, struct client *c, u_char b, u_char x, u_char y)
633: {
1.11 nicm 634: if (wp->fd == -1 || !window_pane_visible(wp))
1.3 nicm 635: return;
636:
1.1 nicm 637: /* XXX convert from 1-based? */
638:
639: if (x < wp->xoff || x >= wp->xoff + wp->sx)
640: return;
641: if (y < wp->yoff || y >= wp->yoff + wp->sy)
642: return;
643: x -= wp->xoff;
644: y -= wp->yoff;
645:
646: if (wp->mode != NULL) {
647: if (wp->mode->mouse != NULL)
648: wp->mode->mouse(wp, c, b, x, y);
649: } else
650: input_mouse(wp, b, x, y);
1.10 nicm 651: }
652:
653: int
654: window_pane_visible(struct window_pane *wp)
655: {
656: struct window *w = wp->window;
657:
658: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
659: return (0);
660: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
661: return (0);
662: return (1);
1.1 nicm 663: }
664:
665: char *
1.5 nicm 666: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 667: {
1.4 nicm 668: struct screen *s = &wp->base;
1.5 nicm 669: char *newsearchstr, *line, *msg;
1.4 nicm 670: u_int i;
671:
1.5 nicm 672: msg = NULL;
673: xasprintf(&newsearchstr, "*%s*", searchstr);
674:
1.4 nicm 675: for (i = 0; i < screen_size_y(s); i++) {
676: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 677: if (fnmatch(newsearchstr, line, 0) == 0) {
678: msg = line;
679: if (lineno != NULL)
680: *lineno = i;
681: break;
682: }
1.4 nicm 683: xfree(line);
684: }
1.5 nicm 685:
686: xfree(newsearchstr);
687: return (msg);
1.1 nicm 688: }