Annotation of src/usr.bin/tmux/window.c, Revision 1.5
1.5 ! nicm 1: /* $OpenBSD: window.c,v 1.4 2009/06/24 22:04:18 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: u_int
331: window_index_of_pane(struct window *w, struct window_pane *find)
332: {
333: struct window_pane *wp;
334: u_int n;
335:
336: n = 0;
337: TAILQ_FOREACH(wp, &w->panes, entry) {
338: if (wp == find)
339: return (n);
340: n++;
341: }
342: fatalx("unknown pane");
343: }
344:
345: struct window_pane *
346: window_pane_at_index(struct window *w, u_int idx)
347: {
348: struct window_pane *wp;
349: u_int n;
350:
351: n = 0;
352: TAILQ_FOREACH(wp, &w->panes, entry) {
353: if (n == idx)
354: return (wp);
355: n++;
356: }
357: return (NULL);
358: }
359:
360: u_int
361: window_count_panes(struct window *w)
362: {
363: struct window_pane *wp;
364: u_int n;
365:
366: n = 0;
367: TAILQ_FOREACH(wp, &w->panes, entry)
368: n++;
369: return (n);
370: }
371:
372: void
373: window_destroy_panes(struct window *w)
374: {
375: struct window_pane *wp;
376:
377: while (!TAILQ_EMPTY(&w->panes)) {
378: wp = TAILQ_FIRST(&w->panes);
379: TAILQ_REMOVE(&w->panes, wp, entry);
380: window_pane_destroy(wp);
381: }
382: }
383:
384: struct window_pane *
385: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
386: {
387: struct window_pane *wp;
388:
389: wp = xcalloc(1, sizeof *wp);
390: wp->window = w;
391:
392: wp->cmd = NULL;
393: wp->cwd = NULL;
394:
395: wp->fd = -1;
396: wp->in = buffer_create(BUFSIZ);
397: wp->out = buffer_create(BUFSIZ);
398:
399: wp->mode = NULL;
400:
401: wp->xoff = 0;
402: wp->yoff = 0;
403:
404: wp->sx = sx;
405: wp->sy = sy;
406:
407: screen_init(&wp->base, sx, sy, hlimit);
408: wp->screen = &wp->base;
409:
410: input_init(wp);
411:
412: return (wp);
413: }
414:
415: void
416: window_pane_destroy(struct window_pane *wp)
417: {
418: if (wp->fd != -1)
419: close(wp->fd);
420:
421: input_free(wp);
422:
423: window_pane_reset_mode(wp);
424: screen_free(&wp->base);
425:
426: buffer_destroy(wp->in);
427: buffer_destroy(wp->out);
428:
429: if (wp->cwd != NULL)
430: xfree(wp->cwd);
431: if (wp->cmd != NULL)
432: xfree(wp->cmd);
433: xfree(wp);
434: }
435:
436: int
437: window_pane_spawn(struct window_pane *wp,
438: const char *cmd, const char *cwd, const char **envp, char **cause)
439: {
440: struct winsize ws;
441: int mode;
442: const char **envq;
443: struct timeval tv;
444:
445: if (wp->fd != -1)
446: close(wp->fd);
447: if (cmd != NULL) {
448: if (wp->cmd != NULL)
449: xfree(wp->cmd);
450: wp->cmd = xstrdup(cmd);
451: }
452: if (cwd != NULL) {
453: if (wp->cwd != NULL)
454: xfree(wp->cwd);
455: wp->cwd = xstrdup(cwd);
456: }
457:
458: memset(&ws, 0, sizeof ws);
459: ws.ws_col = screen_size_x(&wp->base);
460: ws.ws_row = screen_size_y(&wp->base);
461:
462: if (gettimeofday(&wp->window->name_timer, NULL) != 0)
463: fatal("gettimeofday");
464: tv.tv_sec = 0;
465: tv.tv_usec = NAME_INTERVAL * 1000L;
466: timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
467:
468: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
469: case -1:
470: wp->fd = -1;
471: xasprintf(cause, "%s: %s", cmd, strerror(errno));
472: return (-1);
473: case 0:
474: if (chdir(wp->cwd) != 0)
475: chdir("/");
476: for (envq = envp; *envq != NULL; envq++) {
1.2 nicm 477: if (putenv(xstrdup(*envq)) != 0)
1.1 nicm 478: fatal("putenv failed");
479: }
480: sigreset();
481: log_close();
482:
483: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
484: fatal("execl failed");
485: }
486:
487: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
488: fatal("fcntl failed");
489: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
490: fatal("fcntl failed");
491: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
492: fatal("fcntl failed");
493:
494: return (0);
495: }
496:
497: int
498: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
499: {
500: struct winsize ws;
501:
502: if (sx == wp->sx && sy == wp->sy)
503: return (-1);
504: wp->sx = sx;
505: wp->sy = sy;
506:
507: memset(&ws, 0, sizeof ws);
508: ws.ws_col = sx;
509: ws.ws_row = sy;
510:
511: screen_resize(&wp->base, sx, sy);
512: if (wp->mode != NULL)
513: wp->mode->resize(wp, sx, sy);
514:
515: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
516: fatal("ioctl failed");
517: return (0);
518: }
519:
520: int
521: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
522: {
523: struct screen *s;
524:
525: if (wp->mode != NULL || wp->mode == mode)
526: return (1);
527:
528: wp->mode = mode;
529:
530: if ((s = wp->mode->init(wp)) != NULL)
531: wp->screen = s;
532: server_redraw_window(wp->window);
533: return (0);
534: }
535:
536: void
537: window_pane_reset_mode(struct window_pane *wp)
538: {
539: if (wp->mode == NULL)
540: return;
541:
542: wp->mode->free(wp);
543: wp->mode = NULL;
544:
545: wp->screen = &wp->base;
546: server_redraw_window(wp->window);
547: }
548:
549: void
550: window_pane_parse(struct window_pane *wp)
551: {
552: input_parse(wp);
553: }
554:
555: void
556: window_pane_key(struct window_pane *wp, struct client *c, int key)
557: {
1.3 nicm 558: if (wp->fd == -1)
559: return;
560:
1.1 nicm 561: if (wp->mode != NULL) {
562: if (wp->mode->key != NULL)
563: wp->mode->key(wp, c, key);
564: } else
565: input_key(wp, key);
566: }
567:
568: void
569: window_pane_mouse(
570: struct window_pane *wp, struct client *c, u_char b, u_char x, u_char y)
571: {
1.3 nicm 572: if (wp->fd == -1)
573: return;
574:
1.1 nicm 575: /* XXX convert from 1-based? */
576:
577: if (x < wp->xoff || x >= wp->xoff + wp->sx)
578: return;
579: if (y < wp->yoff || y >= wp->yoff + wp->sy)
580: return;
581: x -= wp->xoff;
582: y -= wp->yoff;
583:
584: if (wp->mode != NULL) {
585: if (wp->mode->mouse != NULL)
586: wp->mode->mouse(wp, c, b, x, y);
587: } else
588: input_mouse(wp, b, x, y);
589: }
590:
591: char *
1.5 ! nicm 592: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 593: {
1.4 nicm 594: struct screen *s = &wp->base;
1.5 ! nicm 595: char *newsearchstr, *line, *msg;
1.4 nicm 596: u_int i;
597:
1.5 ! nicm 598: msg = NULL;
! 599: xasprintf(&newsearchstr, "*%s*", searchstr);
! 600:
1.4 nicm 601: for (i = 0; i < screen_size_y(s); i++) {
602: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 ! nicm 603: if (fnmatch(newsearchstr, line, 0) == 0) {
! 604: msg = line;
! 605: if (lineno != NULL)
! 606: *lineno = i;
! 607: break;
! 608: }
1.4 nicm 609: xfree(line);
610: }
1.5 ! nicm 611:
! 612: xfree(newsearchstr);
! 613: return (msg);
1.1 nicm 614: }