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