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