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