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