Annotation of src/usr.bin/tmux/window.c, Revision 1.30
1.30 ! nicm 1: /* $OpenBSD: window.c,v 1.29 2009/10/10 14:51:16 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:
61: int
62: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
63: {
64: return (wl1->idx - wl2->idx);
1.12 nicm 65: }
66:
67: struct winlink *
68: winlink_find_by_window(struct winlinks *wwl, struct window *w)
69: {
70: struct winlink *wl;
71:
72: RB_FOREACH(wl, winlinks, wwl) {
73: if (wl->window == w)
74: return (wl);
75: }
76:
77: return (NULL);
1.1 nicm 78: }
79:
80: struct winlink *
81: winlink_find_by_index(struct winlinks *wwl, int idx)
82: {
83: struct winlink wl;
84:
85: if (idx < 0)
86: fatalx("bad index");
87:
88: wl.idx = idx;
89: return (RB_FIND(winlinks, wwl, &wl));
90: }
91:
92: int
1.22 nicm 93: winlink_next_index(struct winlinks *wwl, int idx)
1.1 nicm 94: {
1.22 nicm 95: int i;
1.1 nicm 96:
1.22 nicm 97: i = idx;
98: do {
1.1 nicm 99: if (winlink_find_by_index(wwl, i) == NULL)
100: return (i);
1.22 nicm 101: if (i == INT_MAX)
102: i = 0;
103: else
104: i++;
105: } while (i != idx);
106: return (-1);
1.1 nicm 107: }
108:
109: u_int
110: winlink_count(struct winlinks *wwl)
111: {
112: struct winlink *wl;
113: u_int n;
114:
115: n = 0;
116: RB_FOREACH(wl, winlinks, wwl)
117: n++;
118:
119: return (n);
120: }
121:
122: struct winlink *
123: winlink_add(struct winlinks *wwl, struct window *w, int idx)
124: {
125: struct winlink *wl;
126:
1.22 nicm 127: if (idx < 0) {
128: if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
129: return (NULL);
130: } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1 nicm 131: return (NULL);
132:
133: wl = xcalloc(1, sizeof *wl);
134: wl->idx = idx;
135: wl->window = w;
136: RB_INSERT(winlinks, wwl, wl);
137:
138: w->references++;
139:
140: return (wl);
141: }
142:
143: void
144: winlink_remove(struct winlinks *wwl, struct winlink *wl)
145: {
146: struct window *w = wl->window;
147:
148: RB_REMOVE(winlinks, wwl, wl);
149: xfree(wl);
150:
151: if (w->references == 0)
152: fatal("bad reference count");
153: w->references--;
154: if (w->references == 0)
155: window_destroy(w);
156: }
157:
158: struct winlink *
159: winlink_next(unused struct winlinks *wwl, struct winlink *wl)
160: {
161: return (RB_NEXT(winlinks, wwl, wl));
162: }
163:
164: struct winlink *
165: winlink_previous(unused struct winlinks *wwl, struct winlink *wl)
166: {
167: return (RB_PREV(winlinks, wwl, wl));
168: }
169:
170: void
171: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
172: {
173: if (wl == NULL)
174: return;
175:
176: winlink_stack_remove(stack, wl);
1.28 nicm 177: TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1 nicm 178: }
179:
180: void
181: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
182: {
183: struct winlink *wl2;
184:
185: if (wl == NULL)
186: return;
1.28 nicm 187:
188: TAILQ_FOREACH(wl2, stack, sentry) {
1.1 nicm 189: if (wl2 == wl) {
1.28 nicm 190: TAILQ_REMOVE(stack, wl, sentry);
1.1 nicm 191: return;
192: }
193: }
194: }
195:
196: int
197: window_index(struct window *s, u_int *i)
198: {
199: for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
200: if (s == ARRAY_ITEM(&windows, *i))
201: return (0);
202: }
203: return (-1);
204: }
205:
206: struct window *
207: window_create1(u_int sx, u_int sy)
208: {
209: struct window *w;
210: u_int i;
211:
212: w = xmalloc(sizeof *w);
213: w->name = NULL;
214: w->flags = 0;
215:
216: TAILQ_INIT(&w->panes);
217: w->active = NULL;
1.14 nicm 218:
1.17 nicm 219: w->lastlayout = -1;
1.14 nicm 220: w->layout_root = NULL;
221:
1.1 nicm 222: w->sx = sx;
223: w->sy = sy;
224:
1.7 nicm 225: options_init(&w->options, &global_w_options);
1.1 nicm 226:
227: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
228: if (ARRAY_ITEM(&windows, i) == NULL) {
229: ARRAY_SET(&windows, i, w);
230: break;
231: }
232: }
233: if (i == ARRAY_LENGTH(&windows))
234: ARRAY_ADD(&windows, w);
235: w->references = 0;
236:
237: return (w);
238: }
239:
240: struct window *
1.23 nicm 241: window_create(const char *name, const char *cmd, const char *shell,
242: const char *cwd, struct environ *env, struct termios *tio,
243: u_int sx, u_int sy, u_int hlimit,char **cause)
1.1 nicm 244: {
1.14 nicm 245: struct window *w;
246: struct window_pane *wp;
1.1 nicm 247:
248: w = window_create1(sx, sy);
1.16 nicm 249: wp = window_add_pane(w, hlimit);
1.14 nicm 250: layout_init(w);
1.23 nicm 251: if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
1.1 nicm 252: window_destroy(w);
253: return (NULL);
254: }
255: w->active = TAILQ_FIRST(&w->panes);
256: if (name != NULL) {
257: w->name = xstrdup(name);
258: options_set_number(&w->options, "automatic-rename", 0);
259: } else
260: w->name = default_window_name(w);
261: return (w);
262: }
263:
264: void
265: window_destroy(struct window *w)
266: {
267: u_int i;
268:
269: if (window_index(w, &i) != 0)
270: fatalx("index not found");
271: ARRAY_SET(&windows, i, NULL);
272: while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
273: ARRAY_TRUNC(&windows, 1);
274:
1.14 nicm 275: if (w->layout_root != NULL)
276: layout_free(w);
277:
1.1 nicm 278: options_free(&w->options);
279:
280: window_destroy_panes(w);
281:
282: if (w->name != NULL)
283: xfree(w->name);
284: xfree(w);
285: }
286:
1.15 nicm 287: void
1.1 nicm 288: window_resize(struct window *w, u_int sx, u_int sy)
289: {
290: w->sx = sx;
291: w->sy = sy;
292: }
293:
294: void
295: window_set_active_pane(struct window *w, struct window_pane *wp)
296: {
297: w->active = wp;
1.10 nicm 298: while (!window_pane_visible(w->active)) {
1.1 nicm 299: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 300: if (w->active == NULL)
301: w->active = TAILQ_LAST(&w->panes, window_panes);
302: if (w->active == wp)
303: return;
1.29 nicm 304: }
305: }
306:
307: void
308: window_set_active_at(struct window *w, u_int x, u_int y)
309: {
310: struct window_pane *wp;
311:
312: TAILQ_FOREACH(wp, &w->panes, entry) {
313: if (!window_pane_visible(wp))
314: continue;
315: if (x < wp->xoff || x >= wp->xoff + wp->sx)
316: continue;
317: if (y < wp->yoff || y >= wp->yoff + wp->sy)
318: continue;
319: window_set_active_pane(w, wp);
320: break;
1.10 nicm 321: }
1.1 nicm 322: }
323:
324: struct window_pane *
1.16 nicm 325: window_add_pane(struct window *w, u_int hlimit)
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;
1.23 nicm 411: wp->shell = NULL;
1.1 nicm 412: wp->cwd = NULL;
413:
414: wp->fd = -1;
415: wp->in = buffer_create(BUFSIZ);
416: wp->out = buffer_create(BUFSIZ);
417:
418: wp->mode = NULL;
1.14 nicm 419:
420: wp->layout_cell = NULL;
1.1 nicm 421:
422: wp->xoff = 0;
423: wp->yoff = 0;
424:
425: wp->sx = sx;
426: wp->sy = sy;
427:
1.9 nicm 428: wp->saved_grid = NULL;
429:
1.1 nicm 430: screen_init(&wp->base, sx, sy, hlimit);
431: wp->screen = &wp->base;
432:
433: input_init(wp);
434:
435: return (wp);
436: }
437:
438: void
439: window_pane_destroy(struct window_pane *wp)
440: {
441: if (wp->fd != -1)
442: close(wp->fd);
443:
444: input_free(wp);
445:
446: window_pane_reset_mode(wp);
447: screen_free(&wp->base);
1.9 nicm 448: if (wp->saved_grid != NULL)
449: grid_destroy(wp->saved_grid);
1.1 nicm 450:
451: buffer_destroy(wp->in);
452: buffer_destroy(wp->out);
453:
454: if (wp->cwd != NULL)
455: xfree(wp->cwd);
1.23 nicm 456: if (wp->shell != NULL)
457: xfree(wp->shell);
1.1 nicm 458: if (wp->cmd != NULL)
459: xfree(wp->cmd);
460: xfree(wp);
461: }
462:
463: int
1.23 nicm 464: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21 nicm 465: const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1 nicm 466: {
1.18 nicm 467: struct winsize ws;
468: int mode;
469: char *argv0, **varp, *var;
470: ARRAY_DECL(, char *) varlist;
471: struct environ_entry *envent;
472: const char *ptr;
473: struct timeval tv;
1.25 nicm 474: struct termios tio2;
1.18 nicm 475: u_int i;
1.1 nicm 476:
477: if (wp->fd != -1)
478: close(wp->fd);
479: if (cmd != NULL) {
480: if (wp->cmd != NULL)
481: xfree(wp->cmd);
482: wp->cmd = xstrdup(cmd);
483: }
1.23 nicm 484: if (shell != NULL) {
485: if (wp->shell != NULL)
486: xfree(wp->shell);
487: wp->shell = xstrdup(shell);
488: }
1.1 nicm 489: if (cwd != NULL) {
490: if (wp->cwd != NULL)
491: xfree(wp->cwd);
492: wp->cwd = xstrdup(cwd);
493: }
494:
495: memset(&ws, 0, sizeof ws);
496: ws.ws_col = screen_size_x(&wp->base);
497: ws.ws_row = screen_size_y(&wp->base);
498:
499: if (gettimeofday(&wp->window->name_timer, NULL) != 0)
1.26 nicm 500: fatal("gettimeofday failed");
1.1 nicm 501: tv.tv_sec = 0;
502: tv.tv_usec = NAME_INTERVAL * 1000L;
503: timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
504:
1.25 nicm 505: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 506: case -1:
507: wp->fd = -1;
508: xasprintf(cause, "%s: %s", cmd, strerror(errno));
509: return (-1);
510: case 0:
511: if (chdir(wp->cwd) != 0)
512: chdir("/");
1.25 nicm 513:
514: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
515: fatal("tcgetattr failed");
516: if (tio != NULL)
517: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
518: tio2.c_cc[VERASE] = '\177';
519: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
520: fatal("tcgetattr failed");
1.18 nicm 521:
522: ARRAY_INIT(&varlist);
523: for (varp = environ; *varp != NULL; varp++) {
524: var = xstrdup(*varp);
525: var[strcspn(var, "=")] = '\0';
526: ARRAY_ADD(&varlist, var);
527: }
528: for (i = 0; i < ARRAY_LENGTH(&varlist); i++) {
529: var = ARRAY_ITEM(&varlist, i);
530: unsetenv(var);
1.1 nicm 531: }
1.18 nicm 532: RB_FOREACH(envent, environ, env) {
533: if (envent->value != NULL)
534: setenv(envent->name, envent->value, 1);
535: }
536:
1.1 nicm 537: sigreset();
538: log_close();
539:
1.8 nicm 540: if (*wp->cmd != '\0') {
1.24 nicm 541: /* Set SHELL but only if it is currently not useful. */
542: shell = getenv("SHELL");
543: if (shell == NULL || *shell == '\0' || areshell(shell))
544: setenv("SHELL", wp->shell, 1);
545:
1.8 nicm 546: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
547: fatal("execl failed");
548: }
549:
550: /* No command; fork a login shell. */
1.23 nicm 551: ptr = strrchr(wp->shell, '/');
552: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 553: xasprintf(&argv0, "-%s", ptr + 1);
554: else
1.23 nicm 555: xasprintf(&argv0, "-%s", wp->shell);
1.24 nicm 556: setenv("SHELL", wp->shell, 1);
1.23 nicm 557: execl(wp->shell, argv0, (char *) NULL);
1.1 nicm 558: fatal("execl failed");
559: }
560:
561: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
562: fatal("fcntl failed");
563: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
564: fatal("fcntl failed");
565: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
566: fatal("fcntl failed");
567:
568: return (0);
569: }
570:
1.15 nicm 571: void
1.1 nicm 572: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
573: {
574: struct winsize ws;
575:
576: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 577: return;
1.1 nicm 578: wp->sx = sx;
579: wp->sy = sy;
580:
581: memset(&ws, 0, sizeof ws);
582: ws.ws_col = sx;
583: ws.ws_row = sy;
584:
585: screen_resize(&wp->base, sx, sy);
586: if (wp->mode != NULL)
587: wp->mode->resize(wp, sx, sy);
588:
589: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
590: fatal("ioctl failed");
591: }
592:
593: int
594: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
595: {
596: struct screen *s;
597:
1.15 nicm 598: if (wp->mode != NULL)
1.1 nicm 599: return (1);
600: wp->mode = mode;
601:
602: if ((s = wp->mode->init(wp)) != NULL)
603: wp->screen = s;
604: server_redraw_window(wp->window);
605: return (0);
606: }
607:
608: void
609: window_pane_reset_mode(struct window_pane *wp)
610: {
611: if (wp->mode == NULL)
612: return;
613:
614: wp->mode->free(wp);
615: wp->mode = NULL;
616:
617: wp->screen = &wp->base;
618: server_redraw_window(wp->window);
619: }
620:
621: void
622: window_pane_parse(struct window_pane *wp)
623: {
624: input_parse(wp);
625: }
626:
627: void
628: window_pane_key(struct window_pane *wp, struct client *c, int key)
629: {
1.27 nicm 630: struct window_pane *wp2;
631:
1.30 ! nicm 632: if (!window_pane_visible(wp))
1.3 nicm 633: return;
634:
1.1 nicm 635: if (wp->mode != NULL) {
636: if (wp->mode->key != NULL)
637: wp->mode->key(wp, c, key);
1.27 nicm 638: return;
1.30 ! nicm 639: }
1.27 nicm 640:
1.30 ! nicm 641: if (wp->fd == -1)
! 642: return;
1.27 nicm 643: input_key(wp, key);
644: if (options_get_number(&wp->window->options, "synchronize-panes")) {
645: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
646: if (wp2 == wp || wp2->mode != NULL)
647: continue;
648: if (wp2->fd != -1 && window_pane_visible(wp2))
649: input_key(wp2, key);
650: }
651: }
1.1 nicm 652: }
653:
654: void
655: window_pane_mouse(
656: struct window_pane *wp, struct client *c, u_char b, u_char x, u_char y)
657: {
1.30 ! nicm 658: if (!window_pane_visible(wp))
1.3 nicm 659: return;
660:
1.1 nicm 661: /* XXX convert from 1-based? */
662:
663: if (x < wp->xoff || x >= wp->xoff + wp->sx)
664: return;
665: if (y < wp->yoff || y >= wp->yoff + wp->sy)
666: return;
667: x -= wp->xoff;
668: y -= wp->yoff;
669:
670: if (wp->mode != NULL) {
671: if (wp->mode->mouse != NULL)
672: wp->mode->mouse(wp, c, b, x, y);
1.30 ! nicm 673: } else if (wp->fd != -1)
1.1 nicm 674: input_mouse(wp, b, x, y);
1.10 nicm 675: }
676:
677: int
678: window_pane_visible(struct window_pane *wp)
679: {
680: struct window *w = wp->window;
681:
682: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
683: return (0);
684: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
685: return (0);
686: return (1);
1.1 nicm 687: }
688:
689: char *
1.5 nicm 690: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 691: {
1.4 nicm 692: struct screen *s = &wp->base;
1.5 nicm 693: char *newsearchstr, *line, *msg;
1.4 nicm 694: u_int i;
695:
1.5 nicm 696: msg = NULL;
697: xasprintf(&newsearchstr, "*%s*", searchstr);
698:
1.4 nicm 699: for (i = 0; i < screen_size_y(s); i++) {
700: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 701: if (fnmatch(newsearchstr, line, 0) == 0) {
702: msg = line;
703: if (lineno != NULL)
704: *lineno = i;
705: break;
706: }
1.4 nicm 707: xfree(line);
708: }
1.5 nicm 709:
710: xfree(newsearchstr);
711: return (msg);
1.1 nicm 712: }