Annotation of src/usr.bin/tmux/window.c, Revision 1.20
1.20 ! nicm 1: /* $OpenBSD: window.c,v 1.19 2009/08/11 21:28:11 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.18 nicm 272: struct environ *env, u_int sx, u_int sy, u_int hlimit, char **cause)
1.1 nicm 273: {
1.14 nicm 274: struct window *w;
275: struct window_pane *wp;
1.1 nicm 276:
277: w = window_create1(sx, sy);
1.16 nicm 278: wp = window_add_pane(w, hlimit);
1.14 nicm 279: layout_init(w);
1.18 nicm 280: if (window_pane_spawn(wp, cmd, cwd, env, cause) != 0) {
1.1 nicm 281: window_destroy(w);
282: return (NULL);
283: }
284: w->active = TAILQ_FIRST(&w->panes);
285: if (name != NULL) {
286: w->name = xstrdup(name);
287: options_set_number(&w->options, "automatic-rename", 0);
288: } else
289: w->name = default_window_name(w);
290: return (w);
291: }
292:
293: void
294: window_destroy(struct window *w)
295: {
296: u_int i;
297:
298: if (window_index(w, &i) != 0)
299: fatalx("index not found");
300: ARRAY_SET(&windows, i, NULL);
301: while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
302: ARRAY_TRUNC(&windows, 1);
303:
1.14 nicm 304: if (w->layout_root != NULL)
305: layout_free(w);
306:
1.1 nicm 307: options_free(&w->options);
308:
309: window_destroy_panes(w);
310:
311: if (w->name != NULL)
312: xfree(w->name);
313: xfree(w);
314: }
315:
1.15 nicm 316: void
1.1 nicm 317: window_resize(struct window *w, u_int sx, u_int sy)
318: {
319: w->sx = sx;
320: w->sy = sy;
321: }
322:
323: void
324: window_set_active_pane(struct window *w, struct window_pane *wp)
325: {
326: w->active = wp;
1.10 nicm 327: while (!window_pane_visible(w->active)) {
1.1 nicm 328: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 329: if (w->active == NULL)
330: w->active = TAILQ_LAST(&w->panes, window_panes);
331: if (w->active == wp)
332: return;
333: }
1.1 nicm 334: }
335:
336: struct window_pane *
1.16 nicm 337: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 338: {
339: struct window_pane *wp;
340:
1.14 nicm 341: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 342: if (TAILQ_EMPTY(&w->panes))
343: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
344: else
345: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
346: return (wp);
347: }
348:
349: void
350: window_remove_pane(struct window *w, struct window_pane *wp)
351: {
352: w->active = TAILQ_PREV(wp, window_panes, entry);
353: if (w->active == NULL)
354: w->active = TAILQ_NEXT(wp, entry);
355:
356: TAILQ_REMOVE(&w->panes, wp, entry);
357: window_pane_destroy(wp);
358: }
359:
360: struct window_pane *
361: window_pane_at_index(struct window *w, u_int idx)
362: {
363: struct window_pane *wp;
364: u_int n;
365:
366: n = 0;
367: TAILQ_FOREACH(wp, &w->panes, entry) {
368: if (n == idx)
369: return (wp);
370: n++;
371: }
372: return (NULL);
1.13 nicm 373: }
374:
375: u_int
376: window_pane_index(struct window *w, struct window_pane *wp)
377: {
378: struct window_pane *wq;
379: u_int n;
380:
381: n = 0;
382: TAILQ_FOREACH(wq, &w->panes, entry) {
383: if (wp == wq)
384: break;
385: n++;
386: }
387: return (n);
1.1 nicm 388: }
389:
390: u_int
391: window_count_panes(struct window *w)
392: {
393: struct window_pane *wp;
394: u_int n;
395:
396: n = 0;
397: TAILQ_FOREACH(wp, &w->panes, entry)
398: n++;
399: return (n);
400: }
401:
402: void
403: window_destroy_panes(struct window *w)
404: {
405: struct window_pane *wp;
406:
407: while (!TAILQ_EMPTY(&w->panes)) {
408: wp = TAILQ_FIRST(&w->panes);
409: TAILQ_REMOVE(&w->panes, wp, entry);
410: window_pane_destroy(wp);
411: }
412: }
413:
414: struct window_pane *
415: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
416: {
417: struct window_pane *wp;
418:
419: wp = xcalloc(1, sizeof *wp);
420: wp->window = w;
421:
422: wp->cmd = NULL;
423: wp->cwd = NULL;
424:
425: wp->fd = -1;
426: wp->in = buffer_create(BUFSIZ);
427: wp->out = buffer_create(BUFSIZ);
428:
429: wp->mode = NULL;
1.14 nicm 430:
431: wp->layout_cell = NULL;
1.1 nicm 432:
433: wp->xoff = 0;
434: wp->yoff = 0;
435:
436: wp->sx = sx;
437: wp->sy = sy;
438:
1.9 nicm 439: wp->saved_grid = NULL;
440:
1.1 nicm 441: screen_init(&wp->base, sx, sy, hlimit);
442: wp->screen = &wp->base;
443:
444: input_init(wp);
445:
446: return (wp);
447: }
448:
449: void
450: window_pane_destroy(struct window_pane *wp)
451: {
452: if (wp->fd != -1)
453: close(wp->fd);
454:
455: input_free(wp);
456:
457: window_pane_reset_mode(wp);
458: screen_free(&wp->base);
1.9 nicm 459: if (wp->saved_grid != NULL)
460: grid_destroy(wp->saved_grid);
1.1 nicm 461:
462: buffer_destroy(wp->in);
463: buffer_destroy(wp->out);
464:
465: if (wp->cwd != NULL)
466: xfree(wp->cwd);
467: if (wp->cmd != NULL)
468: xfree(wp->cmd);
469: xfree(wp);
470: }
471:
472: int
473: window_pane_spawn(struct window_pane *wp,
1.18 nicm 474: const char *cmd, const char *cwd, struct environ *env, char **cause)
1.1 nicm 475: {
1.18 nicm 476: struct winsize ws;
477: int mode;
478: char *argv0, **varp, *var;
479: ARRAY_DECL(, char *) varlist;
480: struct environ_entry *envent;
481: const char *ptr;
482: struct timeval tv;
483: u_int i;
1.1 nicm 484:
485: if (wp->fd != -1)
486: close(wp->fd);
487: if (cmd != NULL) {
488: if (wp->cmd != NULL)
489: xfree(wp->cmd);
490: wp->cmd = xstrdup(cmd);
491: }
492: if (cwd != NULL) {
493: if (wp->cwd != NULL)
494: xfree(wp->cwd);
495: wp->cwd = xstrdup(cwd);
496: }
497:
498: memset(&ws, 0, sizeof ws);
499: ws.ws_col = screen_size_x(&wp->base);
500: ws.ws_row = screen_size_y(&wp->base);
501:
502: if (gettimeofday(&wp->window->name_timer, NULL) != 0)
503: fatal("gettimeofday");
504: tv.tv_sec = 0;
505: tv.tv_usec = NAME_INTERVAL * 1000L;
506: timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
507:
508: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
509: case -1:
510: wp->fd = -1;
511: xasprintf(cause, "%s: %s", cmd, strerror(errno));
512: return (-1);
513: case 0:
514: if (chdir(wp->cwd) != 0)
515: chdir("/");
1.18 nicm 516:
517: ARRAY_INIT(&varlist);
518: for (varp = environ; *varp != NULL; varp++) {
519: var = xstrdup(*varp);
520: var[strcspn(var, "=")] = '\0';
521: ARRAY_ADD(&varlist, var);
522: }
523: for (i = 0; i < ARRAY_LENGTH(&varlist); i++) {
524: var = ARRAY_ITEM(&varlist, i);
525: unsetenv(var);
1.1 nicm 526: }
1.18 nicm 527: RB_FOREACH(envent, environ, env) {
528: if (envent->value != NULL)
529: setenv(envent->name, envent->value, 1);
530: }
531:
1.1 nicm 532: sigreset();
533: log_close();
534:
1.8 nicm 535: if (*wp->cmd != '\0') {
536: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
537: fatal("execl failed");
538: }
539:
540: /* No command; fork a login shell. */
541: cmd = window_default_command();
542: if ((ptr = strrchr(cmd, '/')) != NULL && *(ptr + 1) != '\0')
543: xasprintf(&argv0, "-%s", ptr + 1);
544: else
545: xasprintf(&argv0, "-%s", cmd);
546: execl(cmd, argv0, (char *) NULL);
1.1 nicm 547: fatal("execl failed");
548: }
549:
550: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
551: fatal("fcntl failed");
552: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
553: fatal("fcntl failed");
554: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
555: fatal("fcntl failed");
556:
557: return (0);
558: }
559:
1.15 nicm 560: void
1.1 nicm 561: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
562: {
563: struct winsize ws;
564:
565: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 566: return;
1.1 nicm 567: wp->sx = sx;
568: wp->sy = sy;
569:
570: memset(&ws, 0, sizeof ws);
571: ws.ws_col = sx;
572: ws.ws_row = sy;
573:
574: screen_resize(&wp->base, sx, sy);
575: if (wp->mode != NULL)
576: wp->mode->resize(wp, sx, sy);
577:
578: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
579: fatal("ioctl failed");
580: }
581:
582: int
583: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
584: {
585: struct screen *s;
586:
1.15 nicm 587: if (wp->mode != NULL)
1.1 nicm 588: return (1);
589: wp->mode = mode;
590:
591: if ((s = wp->mode->init(wp)) != NULL)
592: wp->screen = s;
593: server_redraw_window(wp->window);
594: return (0);
595: }
596:
597: void
598: window_pane_reset_mode(struct window_pane *wp)
599: {
600: if (wp->mode == NULL)
601: return;
602:
603: wp->mode->free(wp);
604: wp->mode = NULL;
605:
606: wp->screen = &wp->base;
607: server_redraw_window(wp->window);
608: }
609:
610: void
611: window_pane_parse(struct window_pane *wp)
612: {
613: input_parse(wp);
614: }
615:
616: void
617: window_pane_key(struct window_pane *wp, struct client *c, int key)
618: {
1.11 nicm 619: if (wp->fd == -1 || !window_pane_visible(wp))
1.3 nicm 620: return;
621:
1.1 nicm 622: if (wp->mode != NULL) {
623: if (wp->mode->key != NULL)
624: wp->mode->key(wp, c, key);
625: } else
626: input_key(wp, key);
627: }
628:
629: void
630: window_pane_mouse(
631: struct window_pane *wp, struct client *c, u_char b, u_char x, u_char y)
632: {
1.11 nicm 633: if (wp->fd == -1 || !window_pane_visible(wp))
1.3 nicm 634: return;
635:
1.1 nicm 636: /* XXX convert from 1-based? */
637:
638: if (x < wp->xoff || x >= wp->xoff + wp->sx)
639: return;
640: if (y < wp->yoff || y >= wp->yoff + wp->sy)
641: return;
642: x -= wp->xoff;
643: y -= wp->yoff;
644:
645: if (wp->mode != NULL) {
646: if (wp->mode->mouse != NULL)
647: wp->mode->mouse(wp, c, b, x, y);
648: } else
649: input_mouse(wp, b, x, y);
1.10 nicm 650: }
651:
652: int
653: window_pane_visible(struct window_pane *wp)
654: {
655: struct window *w = wp->window;
656:
657: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
658: return (0);
659: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
660: return (0);
661: return (1);
1.1 nicm 662: }
663:
664: char *
1.5 nicm 665: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 666: {
1.4 nicm 667: struct screen *s = &wp->base;
1.5 nicm 668: char *newsearchstr, *line, *msg;
1.4 nicm 669: u_int i;
670:
1.5 nicm 671: msg = NULL;
672: xasprintf(&newsearchstr, "*%s*", searchstr);
673:
1.4 nicm 674: for (i = 0; i < screen_size_y(s); i++) {
675: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 676: if (fnmatch(newsearchstr, line, 0) == 0) {
677: msg = line;
678: if (lineno != NULL)
679: *lineno = i;
680: break;
681: }
1.4 nicm 682: xfree(line);
683: }
1.5 nicm 684:
685: xfree(newsearchstr);
686: return (msg);
1.1 nicm 687: }