Annotation of src/usr.bin/tmux/window.c, Revision 1.6
1.6 ! nicm 1: /* $OpenBSD: window.c,v 1.5 2009/06/24 22:49:56 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>
26: #include <signal.h>
27: #include <stdint.h>
28: #include <stdlib.h>
29: #include <string.h>
30: #include <termios.h>
31: #include <unistd.h>
32: #include <util.h>
33:
34: #include "tmux.h"
35:
36: /*
37: * Each window is attached to one or two panes, each of which is a pty. This
38: * file contains code to handle them.
39: *
40: * A pane has two buffers attached, these are filled and emptied by the main
41: * server poll loop. Output data is received from pty's in screen format,
42: * translated and returned as a series of escape sequences and strings via
43: * input_parse (in input.c). Input data is received as key codes and written
44: * directly via input_key.
45: *
46: * Each pane also has a "virtual" screen (screen.c) which contains the current
47: * state and is redisplayed when the window is reattached to a client.
48: *
49: * Windows are stored directly on a global array and wrapped in any number of
50: * winlink structs to be linked onto local session RB trees. A reference count
51: * is maintained and a window removed from the global list and destroyed when
52: * it reaches zero.
53: */
54:
55: /* Global window list. */
56: struct windows windows;
57:
58: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
59:
60: int
61: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
62: {
63: return (wl1->idx - wl2->idx);
64: }
65:
66: struct winlink *
67: winlink_find_by_index(struct winlinks *wwl, int idx)
68: {
69: struct winlink wl;
70:
71: if (idx < 0)
72: fatalx("bad index");
73:
74: wl.idx = idx;
75: return (RB_FIND(winlinks, wwl, &wl));
76: }
77:
78: int
79: winlink_next_index(struct winlinks *wwl)
80: {
81: u_int i;
82:
83: for (i = 0; i < INT_MAX; i++) {
84: if (winlink_find_by_index(wwl, i) == NULL)
85: return (i);
86: }
87:
88: fatalx("no free indexes");
89: }
90:
91: u_int
92: winlink_count(struct winlinks *wwl)
93: {
94: struct winlink *wl;
95: u_int n;
96:
97: n = 0;
98: RB_FOREACH(wl, winlinks, wwl)
99: n++;
100:
101: return (n);
102: }
103:
104: struct winlink *
105: winlink_add(struct winlinks *wwl, struct window *w, int idx)
106: {
107: struct winlink *wl;
108:
109: if (idx == -1)
110: idx = winlink_next_index(wwl);
111: else if (winlink_find_by_index(wwl, idx) != NULL)
112: return (NULL);
113:
114: if (idx < 0)
115: fatalx("bad index");
116:
117: wl = xcalloc(1, sizeof *wl);
118: wl->idx = idx;
119: wl->window = w;
120: RB_INSERT(winlinks, wwl, wl);
121:
122: w->references++;
123:
124: return (wl);
125: }
126:
127: void
128: winlink_remove(struct winlinks *wwl, struct winlink *wl)
129: {
130: struct window *w = wl->window;
131:
132: RB_REMOVE(winlinks, wwl, wl);
133: xfree(wl);
134:
135: if (w->references == 0)
136: fatal("bad reference count");
137: w->references--;
138: if (w->references == 0)
139: window_destroy(w);
140: }
141:
142: struct winlink *
143: winlink_next(unused struct winlinks *wwl, struct winlink *wl)
144: {
145: return (RB_NEXT(winlinks, wwl, wl));
146: }
147:
148: struct winlink *
149: winlink_previous(unused struct winlinks *wwl, struct winlink *wl)
150: {
151: return (RB_PREV(winlinks, wwl, wl));
152: }
153:
154: void
155: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
156: {
157: if (wl == NULL)
158: return;
159:
160: winlink_stack_remove(stack, wl);
161: SLIST_INSERT_HEAD(stack, wl, sentry);
162: }
163:
164: void
165: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
166: {
167: struct winlink *wl2;
168:
169: if (wl == NULL)
170: return;
171:
172: SLIST_FOREACH(wl2, stack, sentry) {
173: if (wl2 == wl) {
174: SLIST_REMOVE(stack, wl, winlink, sentry);
175: return;
176: }
177: }
178: }
179:
180: int
181: window_index(struct window *s, u_int *i)
182: {
183: for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
184: if (s == ARRAY_ITEM(&windows, *i))
185: return (0);
186: }
187: return (-1);
188: }
189:
190: struct window *
191: window_create1(u_int sx, u_int sy)
192: {
193: struct window *w;
194: u_int i;
195:
196: w = xmalloc(sizeof *w);
197: w->name = NULL;
198: w->flags = 0;
199:
200: TAILQ_INIT(&w->panes);
201: w->active = NULL;
202: w->layout = 0;
203:
204: w->sx = sx;
205: w->sy = sy;
206:
207: options_init(&w->options, &global_window_options);
208:
209: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
210: if (ARRAY_ITEM(&windows, i) == NULL) {
211: ARRAY_SET(&windows, i, w);
212: break;
213: }
214: }
215: if (i == ARRAY_LENGTH(&windows))
216: ARRAY_ADD(&windows, w);
217: w->references = 0;
218:
219: return (w);
220: }
221:
222: struct window *
223: window_create(const char *name, const char *cmd, const char *cwd,
224: const char **envp, u_int sx, u_int sy, u_int hlimit, char **cause)
225: {
226: struct window *w;
227:
228: w = window_create1(sx, sy);
229: if (window_add_pane(w, -1, cmd, cwd, envp, hlimit, cause) == NULL) {
230: window_destroy(w);
231: return (NULL);
232: }
233: w->active = TAILQ_FIRST(&w->panes);
234:
235: if (name != NULL) {
236: w->name = xstrdup(name);
237: options_set_number(&w->options, "automatic-rename", 0);
238: } else
239: w->name = default_window_name(w);
240: return (w);
241: }
242:
243: void
244: window_destroy(struct window *w)
245: {
246: u_int i;
247:
248: if (window_index(w, &i) != 0)
249: fatalx("index not found");
250: ARRAY_SET(&windows, i, NULL);
251: while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
252: ARRAY_TRUNC(&windows, 1);
253:
254: options_free(&w->options);
255:
256: window_destroy_panes(w);
257:
258: if (w->name != NULL)
259: xfree(w->name);
260: xfree(w);
261: }
262:
263: int
264: window_resize(struct window *w, u_int sx, u_int sy)
265: {
266: w->sx = sx;
267: w->sy = sy;
268:
269: return (0);
270: }
271:
272: void
273: window_set_active_pane(struct window *w, struct window_pane *wp)
274: {
275: w->active = wp;
276: while (w->active->flags & PANE_HIDDEN)
277: w->active = TAILQ_PREV(w->active, window_panes, entry);
278: }
279:
280: struct window_pane *
281: window_add_pane(struct window *w, int wanty, const char *cmd,
282: const char *cwd, const char **envp, u_int hlimit, char **cause)
283: {
284: struct window_pane *wp;
285: u_int sizey;
286:
287: if (TAILQ_EMPTY(&w->panes))
288: wanty = w->sy;
289: else {
290: sizey = w->active->sy - 1; /* for separator */
291: if (sizey < PANE_MINIMUM * 2) {
292: *cause = xstrdup("pane too small");
293: return (NULL);
294: }
295:
296: if (wanty == -1)
297: wanty = sizey / 2;
298:
299: if (wanty < PANE_MINIMUM)
300: wanty = PANE_MINIMUM;
301: if ((u_int) wanty > sizey - PANE_MINIMUM)
302: wanty = sizey - PANE_MINIMUM;
303:
304: window_pane_resize(w->active, w->sx, sizey - wanty);
305: }
306:
307: wp = window_pane_create(w, w->sx, wanty, hlimit);
308: if (TAILQ_EMPTY(&w->panes))
309: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
310: else
311: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
312: if (window_pane_spawn(wp, cmd, cwd, envp, cause) != 0) {
313: window_remove_pane(w, wp);
314: return (NULL);
315: }
316: return (wp);
317: }
318:
319: void
320: window_remove_pane(struct window *w, struct window_pane *wp)
321: {
322: w->active = TAILQ_PREV(wp, window_panes, entry);
323: if (w->active == NULL)
324: w->active = TAILQ_NEXT(wp, entry);
325:
326: TAILQ_REMOVE(&w->panes, wp, entry);
327: window_pane_destroy(wp);
328: }
329:
330: struct window_pane *
331: window_pane_at_index(struct window *w, u_int idx)
332: {
333: struct window_pane *wp;
334: u_int n;
335:
336: n = 0;
337: TAILQ_FOREACH(wp, &w->panes, entry) {
338: if (n == idx)
339: return (wp);
340: n++;
341: }
342: return (NULL);
343: }
344:
345: u_int
346: window_count_panes(struct window *w)
347: {
348: struct window_pane *wp;
349: u_int n;
350:
351: n = 0;
352: TAILQ_FOREACH(wp, &w->panes, entry)
353: n++;
354: return (n);
355: }
356:
357: void
358: window_destroy_panes(struct window *w)
359: {
360: struct window_pane *wp;
361:
362: while (!TAILQ_EMPTY(&w->panes)) {
363: wp = TAILQ_FIRST(&w->panes);
364: TAILQ_REMOVE(&w->panes, wp, entry);
365: window_pane_destroy(wp);
366: }
367: }
368:
369: struct window_pane *
370: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
371: {
372: struct window_pane *wp;
373:
374: wp = xcalloc(1, sizeof *wp);
375: wp->window = w;
376:
377: wp->cmd = NULL;
378: wp->cwd = NULL;
379:
380: wp->fd = -1;
381: wp->in = buffer_create(BUFSIZ);
382: wp->out = buffer_create(BUFSIZ);
383:
384: wp->mode = NULL;
385:
386: wp->xoff = 0;
387: wp->yoff = 0;
388:
389: wp->sx = sx;
390: wp->sy = sy;
391:
392: screen_init(&wp->base, sx, sy, hlimit);
393: wp->screen = &wp->base;
394:
395: input_init(wp);
396:
397: return (wp);
398: }
399:
400: void
401: window_pane_destroy(struct window_pane *wp)
402: {
403: if (wp->fd != -1)
404: close(wp->fd);
405:
406: input_free(wp);
407:
408: window_pane_reset_mode(wp);
409: screen_free(&wp->base);
410:
411: buffer_destroy(wp->in);
412: buffer_destroy(wp->out);
413:
414: if (wp->cwd != NULL)
415: xfree(wp->cwd);
416: if (wp->cmd != NULL)
417: xfree(wp->cmd);
418: xfree(wp);
419: }
420:
421: int
422: window_pane_spawn(struct window_pane *wp,
423: const char *cmd, const char *cwd, const char **envp, char **cause)
424: {
425: struct winsize ws;
426: int mode;
427: const char **envq;
428: struct timeval tv;
429:
430: if (wp->fd != -1)
431: close(wp->fd);
432: if (cmd != NULL) {
433: if (wp->cmd != NULL)
434: xfree(wp->cmd);
435: wp->cmd = xstrdup(cmd);
436: }
437: if (cwd != NULL) {
438: if (wp->cwd != NULL)
439: xfree(wp->cwd);
440: wp->cwd = xstrdup(cwd);
441: }
442:
443: memset(&ws, 0, sizeof ws);
444: ws.ws_col = screen_size_x(&wp->base);
445: ws.ws_row = screen_size_y(&wp->base);
446:
447: if (gettimeofday(&wp->window->name_timer, NULL) != 0)
448: fatal("gettimeofday");
449: tv.tv_sec = 0;
450: tv.tv_usec = NAME_INTERVAL * 1000L;
451: timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
452:
453: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
454: case -1:
455: wp->fd = -1;
456: xasprintf(cause, "%s: %s", cmd, strerror(errno));
457: return (-1);
458: case 0:
459: if (chdir(wp->cwd) != 0)
460: chdir("/");
461: for (envq = envp; *envq != NULL; envq++) {
1.2 nicm 462: if (putenv(xstrdup(*envq)) != 0)
1.1 nicm 463: fatal("putenv failed");
464: }
465: sigreset();
466: log_close();
467:
468: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
469: fatal("execl failed");
470: }
471:
472: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
473: fatal("fcntl failed");
474: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
475: fatal("fcntl failed");
476: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
477: fatal("fcntl failed");
478:
479: return (0);
480: }
481:
482: int
483: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
484: {
485: struct winsize ws;
486:
487: if (sx == wp->sx && sy == wp->sy)
488: return (-1);
489: wp->sx = sx;
490: wp->sy = sy;
491:
492: memset(&ws, 0, sizeof ws);
493: ws.ws_col = sx;
494: ws.ws_row = sy;
495:
496: screen_resize(&wp->base, sx, sy);
497: if (wp->mode != NULL)
498: wp->mode->resize(wp, sx, sy);
499:
500: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
501: fatal("ioctl failed");
502: return (0);
503: }
504:
505: int
506: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
507: {
508: struct screen *s;
509:
510: if (wp->mode != NULL || wp->mode == mode)
511: return (1);
512:
513: wp->mode = mode;
514:
515: if ((s = wp->mode->init(wp)) != NULL)
516: wp->screen = s;
517: server_redraw_window(wp->window);
518: return (0);
519: }
520:
521: void
522: window_pane_reset_mode(struct window_pane *wp)
523: {
524: if (wp->mode == NULL)
525: return;
526:
527: wp->mode->free(wp);
528: wp->mode = NULL;
529:
530: wp->screen = &wp->base;
531: server_redraw_window(wp->window);
532: }
533:
534: void
535: window_pane_parse(struct window_pane *wp)
536: {
537: input_parse(wp);
538: }
539:
540: void
541: window_pane_key(struct window_pane *wp, struct client *c, int key)
542: {
1.3 nicm 543: if (wp->fd == -1)
544: return;
545:
1.1 nicm 546: if (wp->mode != NULL) {
547: if (wp->mode->key != NULL)
548: wp->mode->key(wp, c, key);
549: } else
550: input_key(wp, key);
551: }
552:
553: void
554: window_pane_mouse(
555: struct window_pane *wp, struct client *c, u_char b, u_char x, u_char y)
556: {
1.3 nicm 557: if (wp->fd == -1)
558: return;
559:
1.1 nicm 560: /* XXX convert from 1-based? */
561:
562: if (x < wp->xoff || x >= wp->xoff + wp->sx)
563: return;
564: if (y < wp->yoff || y >= wp->yoff + wp->sy)
565: return;
566: x -= wp->xoff;
567: y -= wp->yoff;
568:
569: if (wp->mode != NULL) {
570: if (wp->mode->mouse != NULL)
571: wp->mode->mouse(wp, c, b, x, y);
572: } else
573: input_mouse(wp, b, x, y);
574: }
575:
576: char *
1.5 nicm 577: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 578: {
1.4 nicm 579: struct screen *s = &wp->base;
1.5 nicm 580: char *newsearchstr, *line, *msg;
1.4 nicm 581: u_int i;
582:
1.5 nicm 583: msg = NULL;
584: xasprintf(&newsearchstr, "*%s*", searchstr);
585:
1.4 nicm 586: for (i = 0; i < screen_size_y(s); i++) {
587: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 588: if (fnmatch(newsearchstr, line, 0) == 0) {
589: msg = line;
590: if (lineno != NULL)
591: *lineno = i;
592: break;
593: }
1.4 nicm 594: xfree(line);
595: }
1.5 nicm 596:
597: xfree(newsearchstr);
598: return (msg);
1.1 nicm 599: }