Annotation of src/usr.bin/tmux/window.c, Revision 1.12
1.12 ! nicm 1: /* $OpenBSD: window.c,v 1.11 2009/07/14 16:52:50 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);
380: }
381:
382: u_int
383: window_count_panes(struct window *w)
384: {
385: struct window_pane *wp;
386: u_int n;
387:
388: n = 0;
389: TAILQ_FOREACH(wp, &w->panes, entry)
390: n++;
391: return (n);
392: }
393:
394: void
395: window_destroy_panes(struct window *w)
396: {
397: struct window_pane *wp;
398:
399: while (!TAILQ_EMPTY(&w->panes)) {
400: wp = TAILQ_FIRST(&w->panes);
401: TAILQ_REMOVE(&w->panes, wp, entry);
402: window_pane_destroy(wp);
403: }
404: }
405:
406: struct window_pane *
407: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
408: {
409: struct window_pane *wp;
410:
411: wp = xcalloc(1, sizeof *wp);
412: wp->window = w;
413:
414: wp->cmd = NULL;
415: wp->cwd = NULL;
416:
417: wp->fd = -1;
418: wp->in = buffer_create(BUFSIZ);
419: wp->out = buffer_create(BUFSIZ);
420:
421: wp->mode = NULL;
422:
423: wp->xoff = 0;
424: wp->yoff = 0;
425:
426: wp->sx = sx;
427: wp->sy = sy;
428:
1.9 nicm 429: wp->saved_grid = NULL;
430:
1.1 nicm 431: screen_init(&wp->base, sx, sy, hlimit);
432: wp->screen = &wp->base;
433:
434: input_init(wp);
435:
436: return (wp);
437: }
438:
439: void
440: window_pane_destroy(struct window_pane *wp)
441: {
442: if (wp->fd != -1)
443: close(wp->fd);
444:
445: input_free(wp);
446:
447: window_pane_reset_mode(wp);
448: screen_free(&wp->base);
1.9 nicm 449: if (wp->saved_grid != NULL)
450: grid_destroy(wp->saved_grid);
1.1 nicm 451:
452: buffer_destroy(wp->in);
453: buffer_destroy(wp->out);
454:
455: if (wp->cwd != NULL)
456: xfree(wp->cwd);
457: if (wp->cmd != NULL)
458: xfree(wp->cmd);
459: xfree(wp);
460: }
461:
462: int
463: window_pane_spawn(struct window_pane *wp,
464: const char *cmd, const char *cwd, const char **envp, char **cause)
465: {
466: struct winsize ws;
467: int mode;
1.8 nicm 468: const char **envq, *ptr;
469: char *argv0;
1.1 nicm 470: struct timeval tv;
471:
472: if (wp->fd != -1)
473: close(wp->fd);
474: if (cmd != NULL) {
475: if (wp->cmd != NULL)
476: xfree(wp->cmd);
477: wp->cmd = xstrdup(cmd);
478: }
479: if (cwd != NULL) {
480: if (wp->cwd != NULL)
481: xfree(wp->cwd);
482: wp->cwd = xstrdup(cwd);
483: }
484:
485: memset(&ws, 0, sizeof ws);
486: ws.ws_col = screen_size_x(&wp->base);
487: ws.ws_row = screen_size_y(&wp->base);
488:
489: if (gettimeofday(&wp->window->name_timer, NULL) != 0)
490: fatal("gettimeofday");
491: tv.tv_sec = 0;
492: tv.tv_usec = NAME_INTERVAL * 1000L;
493: timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
494:
495: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
496: case -1:
497: wp->fd = -1;
498: xasprintf(cause, "%s: %s", cmd, strerror(errno));
499: return (-1);
500: case 0:
501: if (chdir(wp->cwd) != 0)
502: chdir("/");
503: for (envq = envp; *envq != NULL; envq++) {
1.2 nicm 504: if (putenv(xstrdup(*envq)) != 0)
1.1 nicm 505: fatal("putenv failed");
506: }
507: sigreset();
508: log_close();
509:
1.8 nicm 510: if (*wp->cmd != '\0') {
511: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
512: fatal("execl failed");
513: }
514:
515: /* No command; fork a login shell. */
516: cmd = window_default_command();
517: if ((ptr = strrchr(cmd, '/')) != NULL && *(ptr + 1) != '\0')
518: xasprintf(&argv0, "-%s", ptr + 1);
519: else
520: xasprintf(&argv0, "-%s", cmd);
521: execl(cmd, argv0, (char *) NULL);
1.1 nicm 522: fatal("execl failed");
523: }
524:
525: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
526: fatal("fcntl failed");
527: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
528: fatal("fcntl failed");
529: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
530: fatal("fcntl failed");
531:
532: return (0);
533: }
534:
535: int
536: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
537: {
538: struct winsize ws;
539:
540: if (sx == wp->sx && sy == wp->sy)
541: return (-1);
542: wp->sx = sx;
543: wp->sy = sy;
544:
545: memset(&ws, 0, sizeof ws);
546: ws.ws_col = sx;
547: ws.ws_row = sy;
548:
549: screen_resize(&wp->base, sx, sy);
550: if (wp->mode != NULL)
551: wp->mode->resize(wp, sx, sy);
552:
553: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
554: fatal("ioctl failed");
555: return (0);
556: }
557:
558: int
559: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
560: {
561: struct screen *s;
562:
563: if (wp->mode != NULL || wp->mode == mode)
564: return (1);
565:
566: wp->mode = mode;
567:
568: if ((s = wp->mode->init(wp)) != NULL)
569: wp->screen = s;
570: server_redraw_window(wp->window);
571: return (0);
572: }
573:
574: void
575: window_pane_reset_mode(struct window_pane *wp)
576: {
577: if (wp->mode == NULL)
578: return;
579:
580: wp->mode->free(wp);
581: wp->mode = NULL;
582:
583: wp->screen = &wp->base;
584: server_redraw_window(wp->window);
585: }
586:
587: void
588: window_pane_parse(struct window_pane *wp)
589: {
590: input_parse(wp);
591: }
592:
593: void
594: window_pane_key(struct window_pane *wp, struct client *c, int key)
595: {
1.11 nicm 596: if (wp->fd == -1 || !window_pane_visible(wp))
1.3 nicm 597: return;
598:
1.1 nicm 599: if (wp->mode != NULL) {
600: if (wp->mode->key != NULL)
601: wp->mode->key(wp, c, key);
602: } else
603: input_key(wp, key);
604: }
605:
606: void
607: window_pane_mouse(
608: struct window_pane *wp, struct client *c, u_char b, u_char x, u_char y)
609: {
1.11 nicm 610: if (wp->fd == -1 || !window_pane_visible(wp))
1.3 nicm 611: return;
612:
1.1 nicm 613: /* XXX convert from 1-based? */
614:
615: if (x < wp->xoff || x >= wp->xoff + wp->sx)
616: return;
617: if (y < wp->yoff || y >= wp->yoff + wp->sy)
618: return;
619: x -= wp->xoff;
620: y -= wp->yoff;
621:
622: if (wp->mode != NULL) {
623: if (wp->mode->mouse != NULL)
624: wp->mode->mouse(wp, c, b, x, y);
625: } else
626: input_mouse(wp, b, x, y);
1.10 nicm 627: }
628:
629: int
630: window_pane_visible(struct window_pane *wp)
631: {
632: struct window *w = wp->window;
633:
634: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
635: return (0);
636: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
637: return (0);
638: return (1);
1.1 nicm 639: }
640:
641: char *
1.5 nicm 642: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 643: {
1.4 nicm 644: struct screen *s = &wp->base;
1.5 nicm 645: char *newsearchstr, *line, *msg;
1.4 nicm 646: u_int i;
647:
1.5 nicm 648: msg = NULL;
649: xasprintf(&newsearchstr, "*%s*", searchstr);
650:
1.4 nicm 651: for (i = 0; i < screen_size_y(s); i++) {
652: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 653: if (fnmatch(newsearchstr, line, 0) == 0) {
654: msg = line;
655: if (lineno != NULL)
656: *lineno = i;
657: break;
658: }
1.4 nicm 659: xfree(line);
660: }
1.5 nicm 661:
662: xfree(newsearchstr);
663: return (msg);
1.1 nicm 664: }