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