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