Annotation of src/usr.bin/tmux/window.c, Revision 1.22
1.22 ! nicm 1: /* $OpenBSD: window.c,v 1.21 2009/08/13 19:04:00 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
1.22 ! nicm 125: winlink_next_index(struct winlinks *wwl, int idx)
1.1 nicm 126: {
1.22 ! nicm 127: int i;
1.1 nicm 128:
1.22 ! nicm 129: i = idx;
! 130: do {
1.1 nicm 131: if (winlink_find_by_index(wwl, i) == NULL)
132: return (i);
1.22 ! nicm 133: if (i == INT_MAX)
! 134: i = 0;
! 135: else
! 136: i++;
! 137: } while (i != idx);
! 138: return (-1);
1.1 nicm 139: }
140:
141: u_int
142: winlink_count(struct winlinks *wwl)
143: {
144: struct winlink *wl;
145: u_int n;
146:
147: n = 0;
148: RB_FOREACH(wl, winlinks, wwl)
149: n++;
150:
151: return (n);
152: }
153:
154: struct winlink *
155: winlink_add(struct winlinks *wwl, struct window *w, int idx)
156: {
157: struct winlink *wl;
158:
1.22 ! nicm 159: if (idx < 0) {
! 160: if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
! 161: return (NULL);
! 162: } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1 nicm 163: return (NULL);
164:
165: wl = xcalloc(1, sizeof *wl);
166: wl->idx = idx;
167: wl->window = w;
168: RB_INSERT(winlinks, wwl, wl);
169:
170: w->references++;
171:
172: return (wl);
173: }
174:
175: void
176: winlink_remove(struct winlinks *wwl, struct winlink *wl)
177: {
178: struct window *w = wl->window;
179:
180: RB_REMOVE(winlinks, wwl, wl);
181: xfree(wl);
182:
183: if (w->references == 0)
184: fatal("bad reference count");
185: w->references--;
186: if (w->references == 0)
187: window_destroy(w);
188: }
189:
190: struct winlink *
191: winlink_next(unused struct winlinks *wwl, struct winlink *wl)
192: {
193: return (RB_NEXT(winlinks, wwl, wl));
194: }
195:
196: struct winlink *
197: winlink_previous(unused struct winlinks *wwl, struct winlink *wl)
198: {
199: return (RB_PREV(winlinks, wwl, wl));
200: }
201:
202: void
203: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
204: {
205: if (wl == NULL)
206: return;
207:
208: winlink_stack_remove(stack, wl);
209: SLIST_INSERT_HEAD(stack, wl, sentry);
210: }
211:
212: void
213: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
214: {
215: struct winlink *wl2;
216:
217: if (wl == NULL)
218: return;
219:
220: SLIST_FOREACH(wl2, stack, sentry) {
221: if (wl2 == wl) {
222: SLIST_REMOVE(stack, wl, winlink, sentry);
223: return;
224: }
225: }
226: }
227:
228: int
229: window_index(struct window *s, u_int *i)
230: {
231: for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
232: if (s == ARRAY_ITEM(&windows, *i))
233: return (0);
234: }
235: return (-1);
236: }
237:
238: struct window *
239: window_create1(u_int sx, u_int sy)
240: {
241: struct window *w;
242: u_int i;
243:
244: w = xmalloc(sizeof *w);
245: w->name = NULL;
246: w->flags = 0;
247:
248: TAILQ_INIT(&w->panes);
249: w->active = NULL;
1.14 nicm 250:
1.17 nicm 251: w->lastlayout = -1;
1.14 nicm 252: w->layout_root = NULL;
253:
1.1 nicm 254: w->sx = sx;
255: w->sy = sy;
256:
1.7 nicm 257: options_init(&w->options, &global_w_options);
1.1 nicm 258:
259: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
260: if (ARRAY_ITEM(&windows, i) == NULL) {
261: ARRAY_SET(&windows, i, w);
262: break;
263: }
264: }
265: if (i == ARRAY_LENGTH(&windows))
266: ARRAY_ADD(&windows, w);
267: w->references = 0;
268:
269: return (w);
270: }
271:
272: struct window *
273: window_create(const char *name, const char *cmd, const char *cwd,
1.21 nicm 274: struct environ *env, struct termios *tio, u_int sx, u_int sy, u_int hlimit,
275: char **cause)
1.1 nicm 276: {
1.14 nicm 277: struct window *w;
278: struct window_pane *wp;
1.1 nicm 279:
280: w = window_create1(sx, sy);
1.16 nicm 281: wp = window_add_pane(w, hlimit);
1.14 nicm 282: layout_init(w);
1.21 nicm 283: if (window_pane_spawn(wp, cmd, cwd, env, tio, cause) != 0) {
1.1 nicm 284: window_destroy(w);
285: return (NULL);
286: }
287: w->active = TAILQ_FIRST(&w->panes);
288: if (name != NULL) {
289: w->name = xstrdup(name);
290: options_set_number(&w->options, "automatic-rename", 0);
291: } else
292: w->name = default_window_name(w);
293: return (w);
294: }
295:
296: void
297: window_destroy(struct window *w)
298: {
299: u_int i;
300:
301: if (window_index(w, &i) != 0)
302: fatalx("index not found");
303: ARRAY_SET(&windows, i, NULL);
304: while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
305: ARRAY_TRUNC(&windows, 1);
306:
1.14 nicm 307: if (w->layout_root != NULL)
308: layout_free(w);
309:
1.1 nicm 310: options_free(&w->options);
311:
312: window_destroy_panes(w);
313:
314: if (w->name != NULL)
315: xfree(w->name);
316: xfree(w);
317: }
318:
1.15 nicm 319: void
1.1 nicm 320: window_resize(struct window *w, u_int sx, u_int sy)
321: {
322: w->sx = sx;
323: w->sy = sy;
324: }
325:
326: void
327: window_set_active_pane(struct window *w, struct window_pane *wp)
328: {
329: w->active = wp;
1.10 nicm 330: while (!window_pane_visible(w->active)) {
1.1 nicm 331: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 332: if (w->active == NULL)
333: w->active = TAILQ_LAST(&w->panes, window_panes);
334: if (w->active == wp)
335: return;
336: }
1.1 nicm 337: }
338:
339: struct window_pane *
1.16 nicm 340: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 341: {
342: struct window_pane *wp;
343:
1.14 nicm 344: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 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: return (wp);
350: }
351:
352: void
353: window_remove_pane(struct window *w, struct window_pane *wp)
354: {
355: w->active = TAILQ_PREV(wp, window_panes, entry);
356: if (w->active == NULL)
357: w->active = TAILQ_NEXT(wp, entry);
358:
359: TAILQ_REMOVE(&w->panes, wp, entry);
360: window_pane_destroy(wp);
361: }
362:
363: struct window_pane *
364: window_pane_at_index(struct window *w, u_int idx)
365: {
366: struct window_pane *wp;
367: u_int n;
368:
369: n = 0;
370: TAILQ_FOREACH(wp, &w->panes, entry) {
371: if (n == idx)
372: return (wp);
373: n++;
374: }
375: return (NULL);
1.13 nicm 376: }
377:
378: u_int
379: window_pane_index(struct window *w, struct window_pane *wp)
380: {
381: struct window_pane *wq;
382: u_int n;
383:
384: n = 0;
385: TAILQ_FOREACH(wq, &w->panes, entry) {
386: if (wp == wq)
387: break;
388: n++;
389: }
390: return (n);
1.1 nicm 391: }
392:
393: u_int
394: window_count_panes(struct window *w)
395: {
396: struct window_pane *wp;
397: u_int n;
398:
399: n = 0;
400: TAILQ_FOREACH(wp, &w->panes, entry)
401: n++;
402: return (n);
403: }
404:
405: void
406: window_destroy_panes(struct window *w)
407: {
408: struct window_pane *wp;
409:
410: while (!TAILQ_EMPTY(&w->panes)) {
411: wp = TAILQ_FIRST(&w->panes);
412: TAILQ_REMOVE(&w->panes, wp, entry);
413: window_pane_destroy(wp);
414: }
415: }
416:
417: struct window_pane *
418: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
419: {
420: struct window_pane *wp;
421:
422: wp = xcalloc(1, sizeof *wp);
423: wp->window = w;
424:
425: wp->cmd = NULL;
426: wp->cwd = NULL;
427:
428: wp->fd = -1;
429: wp->in = buffer_create(BUFSIZ);
430: wp->out = buffer_create(BUFSIZ);
431:
432: wp->mode = NULL;
1.14 nicm 433:
434: wp->layout_cell = NULL;
1.1 nicm 435:
436: wp->xoff = 0;
437: wp->yoff = 0;
438:
439: wp->sx = sx;
440: wp->sy = sy;
441:
1.9 nicm 442: wp->saved_grid = NULL;
443:
1.1 nicm 444: screen_init(&wp->base, sx, sy, hlimit);
445: wp->screen = &wp->base;
446:
447: input_init(wp);
448:
449: return (wp);
450: }
451:
452: void
453: window_pane_destroy(struct window_pane *wp)
454: {
455: if (wp->fd != -1)
456: close(wp->fd);
457:
458: input_free(wp);
459:
460: window_pane_reset_mode(wp);
461: screen_free(&wp->base);
1.9 nicm 462: if (wp->saved_grid != NULL)
463: grid_destroy(wp->saved_grid);
1.1 nicm 464:
465: buffer_destroy(wp->in);
466: buffer_destroy(wp->out);
467:
468: if (wp->cwd != NULL)
469: xfree(wp->cwd);
470: if (wp->cmd != NULL)
471: xfree(wp->cmd);
472: xfree(wp);
473: }
474:
475: int
1.21 nicm 476: window_pane_spawn(struct window_pane *wp, const char *cmd,
477: const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1 nicm 478: {
1.18 nicm 479: struct winsize ws;
480: int mode;
481: char *argv0, **varp, *var;
482: ARRAY_DECL(, char *) varlist;
483: struct environ_entry *envent;
484: const char *ptr;
485: struct timeval tv;
486: u_int i;
1.1 nicm 487:
488: if (wp->fd != -1)
489: close(wp->fd);
490: if (cmd != NULL) {
491: if (wp->cmd != NULL)
492: xfree(wp->cmd);
493: wp->cmd = xstrdup(cmd);
494: }
495: if (cwd != NULL) {
496: if (wp->cwd != NULL)
497: xfree(wp->cwd);
498: wp->cwd = xstrdup(cwd);
499: }
500:
501: memset(&ws, 0, sizeof ws);
502: ws.ws_col = screen_size_x(&wp->base);
503: ws.ws_row = screen_size_y(&wp->base);
504:
505: if (gettimeofday(&wp->window->name_timer, NULL) != 0)
506: fatal("gettimeofday");
507: tv.tv_sec = 0;
508: tv.tv_usec = NAME_INTERVAL * 1000L;
509: timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
510:
1.21 nicm 511: switch (wp->pid = forkpty(&wp->fd, wp->tty, tio, &ws)) {
1.1 nicm 512: case -1:
513: wp->fd = -1;
514: xasprintf(cause, "%s: %s", cmd, strerror(errno));
515: return (-1);
516: case 0:
517: if (chdir(wp->cwd) != 0)
518: chdir("/");
1.18 nicm 519:
520: ARRAY_INIT(&varlist);
521: for (varp = environ; *varp != NULL; varp++) {
522: var = xstrdup(*varp);
523: var[strcspn(var, "=")] = '\0';
524: ARRAY_ADD(&varlist, var);
525: }
526: for (i = 0; i < ARRAY_LENGTH(&varlist); i++) {
527: var = ARRAY_ITEM(&varlist, i);
528: unsetenv(var);
1.1 nicm 529: }
1.18 nicm 530: RB_FOREACH(envent, environ, env) {
531: if (envent->value != NULL)
532: setenv(envent->name, envent->value, 1);
533: }
534:
1.1 nicm 535: sigreset();
536: log_close();
537:
1.8 nicm 538: if (*wp->cmd != '\0') {
539: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
540: fatal("execl failed");
541: }
542:
543: /* No command; fork a login shell. */
544: cmd = window_default_command();
545: if ((ptr = strrchr(cmd, '/')) != NULL && *(ptr + 1) != '\0')
546: xasprintf(&argv0, "-%s", ptr + 1);
547: else
548: xasprintf(&argv0, "-%s", cmd);
549: execl(cmd, argv0, (char *) NULL);
1.1 nicm 550: fatal("execl failed");
551: }
552:
553: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
554: fatal("fcntl failed");
555: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
556: fatal("fcntl failed");
557: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
558: fatal("fcntl failed");
559:
560: return (0);
561: }
562:
1.15 nicm 563: void
1.1 nicm 564: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
565: {
566: struct winsize ws;
567:
568: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 569: return;
1.1 nicm 570: wp->sx = sx;
571: wp->sy = sy;
572:
573: memset(&ws, 0, sizeof ws);
574: ws.ws_col = sx;
575: ws.ws_row = sy;
576:
577: screen_resize(&wp->base, sx, sy);
578: if (wp->mode != NULL)
579: wp->mode->resize(wp, sx, sy);
580:
581: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
582: fatal("ioctl failed");
583: }
584:
585: int
586: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
587: {
588: struct screen *s;
589:
1.15 nicm 590: if (wp->mode != NULL)
1.1 nicm 591: return (1);
592: wp->mode = mode;
593:
594: if ((s = wp->mode->init(wp)) != NULL)
595: wp->screen = s;
596: server_redraw_window(wp->window);
597: return (0);
598: }
599:
600: void
601: window_pane_reset_mode(struct window_pane *wp)
602: {
603: if (wp->mode == NULL)
604: return;
605:
606: wp->mode->free(wp);
607: wp->mode = NULL;
608:
609: wp->screen = &wp->base;
610: server_redraw_window(wp->window);
611: }
612:
613: void
614: window_pane_parse(struct window_pane *wp)
615: {
616: input_parse(wp);
617: }
618:
619: void
620: window_pane_key(struct window_pane *wp, struct client *c, int key)
621: {
1.11 nicm 622: if (wp->fd == -1 || !window_pane_visible(wp))
1.3 nicm 623: return;
624:
1.1 nicm 625: if (wp->mode != NULL) {
626: if (wp->mode->key != NULL)
627: wp->mode->key(wp, c, key);
628: } else
629: input_key(wp, key);
630: }
631:
632: void
633: window_pane_mouse(
634: struct window_pane *wp, struct client *c, u_char b, u_char x, u_char y)
635: {
1.11 nicm 636: if (wp->fd == -1 || !window_pane_visible(wp))
1.3 nicm 637: return;
638:
1.1 nicm 639: /* XXX convert from 1-based? */
640:
641: if (x < wp->xoff || x >= wp->xoff + wp->sx)
642: return;
643: if (y < wp->yoff || y >= wp->yoff + wp->sy)
644: return;
645: x -= wp->xoff;
646: y -= wp->yoff;
647:
648: if (wp->mode != NULL) {
649: if (wp->mode->mouse != NULL)
650: wp->mode->mouse(wp, c, b, x, y);
651: } else
652: input_mouse(wp, b, x, y);
1.10 nicm 653: }
654:
655: int
656: window_pane_visible(struct window_pane *wp)
657: {
658: struct window *w = wp->window;
659:
660: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
661: return (0);
662: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
663: return (0);
664: return (1);
1.1 nicm 665: }
666:
667: char *
1.5 nicm 668: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 669: {
1.4 nicm 670: struct screen *s = &wp->base;
1.5 nicm 671: char *newsearchstr, *line, *msg;
1.4 nicm 672: u_int i;
673:
1.5 nicm 674: msg = NULL;
675: xasprintf(&newsearchstr, "*%s*", searchstr);
676:
1.4 nicm 677: for (i = 0; i < screen_size_y(s); i++) {
678: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 679: if (fnmatch(newsearchstr, line, 0) == 0) {
680: msg = line;
681: if (lineno != NULL)
682: *lineno = i;
683: break;
684: }
1.4 nicm 685: xfree(line);
686: }
1.5 nicm 687:
688: xfree(newsearchstr);
689: return (msg);
1.1 nicm 690: }