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