Annotation of src/usr.bin/tmux/window.c, Revision 1.52
1.52 ! nicm 1: /* $OpenBSD: window.c,v 1.51 2010/05/23 19:42:19 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:
1.37 nicm 59: void window_pane_read_callback(struct bufferevent *, void *);
60: void window_pane_error_callback(struct bufferevent *, short, void *);
61:
1.1 nicm 62: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
63:
64: int
65: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
66: {
67: return (wl1->idx - wl2->idx);
1.12 nicm 68: }
69:
70: struct winlink *
71: winlink_find_by_window(struct winlinks *wwl, struct window *w)
72: {
73: struct winlink *wl;
74:
75: RB_FOREACH(wl, winlinks, wwl) {
76: if (wl->window == w)
77: return (wl);
78: }
79:
80: return (NULL);
1.1 nicm 81: }
82:
83: struct winlink *
84: winlink_find_by_index(struct winlinks *wwl, int idx)
85: {
86: struct winlink wl;
87:
88: if (idx < 0)
89: fatalx("bad index");
90:
91: wl.idx = idx;
92: return (RB_FIND(winlinks, wwl, &wl));
93: }
94:
95: int
1.22 nicm 96: winlink_next_index(struct winlinks *wwl, int idx)
1.1 nicm 97: {
1.22 nicm 98: int i;
1.1 nicm 99:
1.22 nicm 100: i = idx;
101: do {
1.1 nicm 102: if (winlink_find_by_index(wwl, i) == NULL)
103: return (i);
1.22 nicm 104: if (i == INT_MAX)
105: i = 0;
106: else
107: i++;
108: } while (i != idx);
109: return (-1);
1.1 nicm 110: }
111:
112: u_int
113: winlink_count(struct winlinks *wwl)
114: {
115: struct winlink *wl;
116: u_int n;
117:
118: n = 0;
119: RB_FOREACH(wl, winlinks, wwl)
120: n++;
121:
122: return (n);
123: }
124:
125: struct winlink *
126: winlink_add(struct winlinks *wwl, struct window *w, int idx)
127: {
128: struct winlink *wl;
129:
1.22 nicm 130: if (idx < 0) {
131: if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
132: return (NULL);
133: } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1 nicm 134: return (NULL);
135:
136: wl = xcalloc(1, sizeof *wl);
137: wl->idx = idx;
138: wl->window = w;
139: RB_INSERT(winlinks, wwl, wl);
140:
141: w->references++;
142:
143: return (wl);
144: }
145:
146: void
147: winlink_remove(struct winlinks *wwl, struct winlink *wl)
148: {
149: struct window *w = wl->window;
150:
151: RB_REMOVE(winlinks, wwl, wl);
1.40 nicm 152: if (wl->status_text != NULL)
153: xfree(wl->status_text);
1.1 nicm 154: xfree(wl);
155:
156: if (w->references == 0)
157: fatal("bad reference count");
158: w->references--;
159: if (w->references == 0)
160: window_destroy(w);
161: }
162:
163: struct winlink *
1.41 nicm 164: winlink_next(struct winlink *wl)
1.1 nicm 165: {
166: return (RB_NEXT(winlinks, wwl, wl));
167: }
168:
169: struct winlink *
1.41 nicm 170: winlink_previous(struct winlink *wl)
1.1 nicm 171: {
172: return (RB_PREV(winlinks, wwl, wl));
1.52 ! nicm 173: }
! 174:
! 175: struct winlink *
! 176: winlink_next_by_number(struct winlink *wl, int n)
! 177: {
! 178: for (; n > 0; n--) {
! 179: if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
! 180: break;
! 181: }
! 182:
! 183: return (wl);
! 184: }
! 185:
! 186: struct winlink *
! 187: winlink_previous_by_number(struct winlink *wl, int n)
! 188: {
! 189: for (; n > 0; n--) {
! 190: if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
! 191: break;
! 192: }
! 193:
! 194: return (wl);
1.1 nicm 195: }
196:
197: void
198: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
199: {
200: if (wl == NULL)
201: return;
202:
203: winlink_stack_remove(stack, wl);
1.28 nicm 204: TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1 nicm 205: }
206:
207: void
208: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
209: {
210: struct winlink *wl2;
211:
212: if (wl == NULL)
213: return;
1.42 nicm 214:
1.28 nicm 215: TAILQ_FOREACH(wl2, stack, sentry) {
1.1 nicm 216: if (wl2 == wl) {
1.28 nicm 217: TAILQ_REMOVE(stack, wl, sentry);
1.1 nicm 218: return;
219: }
220: }
221: }
222:
223: int
224: window_index(struct window *s, u_int *i)
225: {
226: for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
227: if (s == ARRAY_ITEM(&windows, *i))
228: return (0);
229: }
230: return (-1);
231: }
232:
233: struct window *
234: window_create1(u_int sx, u_int sy)
235: {
236: struct window *w;
237: u_int i;
238:
1.38 nicm 239: w = xcalloc(1, sizeof *w);
1.1 nicm 240: w->name = NULL;
241: w->flags = 0;
242:
243: TAILQ_INIT(&w->panes);
244: w->active = NULL;
1.14 nicm 245:
1.17 nicm 246: w->lastlayout = -1;
1.14 nicm 247: w->layout_root = NULL;
1.42 nicm 248:
1.1 nicm 249: w->sx = sx;
250: w->sy = sy;
251:
1.38 nicm 252: queue_window_name(w);
253:
1.7 nicm 254: options_init(&w->options, &global_w_options);
1.1 nicm 255:
256: for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
257: if (ARRAY_ITEM(&windows, i) == NULL) {
258: ARRAY_SET(&windows, i, w);
259: break;
260: }
261: }
262: if (i == ARRAY_LENGTH(&windows))
263: ARRAY_ADD(&windows, w);
264: w->references = 0;
265:
266: return (w);
267: }
268:
269: struct window *
1.23 nicm 270: window_create(const char *name, const char *cmd, const char *shell,
271: const char *cwd, struct environ *env, struct termios *tio,
272: u_int sx, u_int sy, u_int hlimit,char **cause)
1.1 nicm 273: {
1.14 nicm 274: struct window *w;
275: struct window_pane *wp;
1.1 nicm 276:
277: w = window_create1(sx, sy);
1.16 nicm 278: wp = window_add_pane(w, hlimit);
1.14 nicm 279: layout_init(w);
1.23 nicm 280: if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
1.1 nicm 281: window_destroy(w);
282: return (NULL);
283: }
284: w->active = TAILQ_FIRST(&w->panes);
285: if (name != NULL) {
286: w->name = xstrdup(name);
287: options_set_number(&w->options, "automatic-rename", 0);
288: } else
289: w->name = default_window_name(w);
290: return (w);
291: }
292:
293: void
294: window_destroy(struct window *w)
295: {
296: u_int i;
297:
298: if (window_index(w, &i) != 0)
299: fatalx("index not found");
300: ARRAY_SET(&windows, i, NULL);
301: while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
302: ARRAY_TRUNC(&windows, 1);
303:
1.14 nicm 304: if (w->layout_root != NULL)
305: layout_free(w);
306:
1.38 nicm 307: evtimer_del(&w->name_timer);
308:
1.1 nicm 309: options_free(&w->options);
310:
311: window_destroy_panes(w);
312:
313: if (w->name != NULL)
314: xfree(w->name);
315: xfree(w);
316: }
317:
1.15 nicm 318: void
1.1 nicm 319: window_resize(struct window *w, u_int sx, u_int sy)
320: {
321: w->sx = sx;
322: w->sy = sy;
323: }
324:
325: void
326: window_set_active_pane(struct window *w, struct window_pane *wp)
327: {
328: w->active = wp;
1.10 nicm 329: while (!window_pane_visible(w->active)) {
1.1 nicm 330: w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10 nicm 331: if (w->active == NULL)
332: w->active = TAILQ_LAST(&w->panes, window_panes);
333: if (w->active == wp)
334: return;
1.29 nicm 335: }
336: }
337:
338: void
339: window_set_active_at(struct window *w, u_int x, u_int y)
340: {
341: struct window_pane *wp;
342:
343: TAILQ_FOREACH(wp, &w->panes, entry) {
344: if (!window_pane_visible(wp))
345: continue;
346: if (x < wp->xoff || x >= wp->xoff + wp->sx)
347: continue;
348: if (y < wp->yoff || y >= wp->yoff + wp->sy)
349: continue;
350: window_set_active_pane(w, wp);
351: break;
1.10 nicm 352: }
1.1 nicm 353: }
354:
355: struct window_pane *
1.16 nicm 356: window_add_pane(struct window *w, u_int hlimit)
1.1 nicm 357: {
358: struct window_pane *wp;
359:
1.14 nicm 360: wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1 nicm 361: if (TAILQ_EMPTY(&w->panes))
362: TAILQ_INSERT_HEAD(&w->panes, wp, entry);
363: else
364: TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
365: return (wp);
366: }
367:
368: void
369: window_remove_pane(struct window *w, struct window_pane *wp)
370: {
371: w->active = TAILQ_PREV(wp, window_panes, entry);
372: if (w->active == NULL)
373: w->active = TAILQ_NEXT(wp, entry);
374:
375: TAILQ_REMOVE(&w->panes, wp, entry);
376: window_pane_destroy(wp);
377: }
378:
379: struct window_pane *
380: window_pane_at_index(struct window *w, u_int idx)
381: {
382: struct window_pane *wp;
383: u_int n;
384:
385: n = 0;
386: TAILQ_FOREACH(wp, &w->panes, entry) {
387: if (n == idx)
388: return (wp);
389: n++;
390: }
391: return (NULL);
1.13 nicm 392: }
393:
394: u_int
395: window_pane_index(struct window *w, struct window_pane *wp)
396: {
397: struct window_pane *wq;
398: u_int n;
399:
400: n = 0;
401: TAILQ_FOREACH(wq, &w->panes, entry) {
402: if (wp == wq)
403: break;
404: n++;
405: }
406: return (n);
1.1 nicm 407: }
408:
409: u_int
410: window_count_panes(struct window *w)
411: {
412: struct window_pane *wp;
413: u_int n;
414:
415: n = 0;
416: TAILQ_FOREACH(wp, &w->panes, entry)
417: n++;
418: return (n);
419: }
420:
421: void
422: window_destroy_panes(struct window *w)
423: {
424: struct window_pane *wp;
425:
426: while (!TAILQ_EMPTY(&w->panes)) {
427: wp = TAILQ_FIRST(&w->panes);
428: TAILQ_REMOVE(&w->panes, wp, entry);
429: window_pane_destroy(wp);
430: }
431: }
432:
433: struct window_pane *
434: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
435: {
436: struct window_pane *wp;
437:
438: wp = xcalloc(1, sizeof *wp);
439: wp->window = w;
440:
441: wp->cmd = NULL;
1.23 nicm 442: wp->shell = NULL;
1.1 nicm 443: wp->cwd = NULL;
444:
445: wp->fd = -1;
1.37 nicm 446: wp->event = NULL;
1.1 nicm 447:
448: wp->mode = NULL;
1.14 nicm 449:
450: wp->layout_cell = NULL;
1.1 nicm 451:
452: wp->xoff = 0;
1.42 nicm 453: wp->yoff = 0;
1.1 nicm 454:
455: wp->sx = sx;
456: wp->sy = sy;
457:
1.32 nicm 458: wp->pipe_fd = -1;
459: wp->pipe_off = 0;
1.36 nicm 460: wp->pipe_event = NULL;
1.32 nicm 461:
1.9 nicm 462: wp->saved_grid = NULL;
463:
1.1 nicm 464: screen_init(&wp->base, sx, sy, hlimit);
465: wp->screen = &wp->base;
466:
467: input_init(wp);
468:
469: return (wp);
470: }
471:
472: void
473: window_pane_destroy(struct window_pane *wp)
474: {
1.37 nicm 475: if (wp->fd != -1) {
1.1 nicm 476: close(wp->fd);
1.37 nicm 477: bufferevent_free(wp->event);
478: }
1.1 nicm 479:
480: input_free(wp);
481:
482: window_pane_reset_mode(wp);
483: screen_free(&wp->base);
1.9 nicm 484: if (wp->saved_grid != NULL)
485: grid_destroy(wp->saved_grid);
1.1 nicm 486:
1.32 nicm 487: if (wp->pipe_fd != -1) {
488: close(wp->pipe_fd);
1.36 nicm 489: bufferevent_free(wp->pipe_event);
1.32 nicm 490: }
491:
1.1 nicm 492: if (wp->cwd != NULL)
493: xfree(wp->cwd);
1.23 nicm 494: if (wp->shell != NULL)
495: xfree(wp->shell);
1.1 nicm 496: if (wp->cmd != NULL)
497: xfree(wp->cmd);
498: xfree(wp);
499: }
500:
501: int
1.23 nicm 502: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21 nicm 503: const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1 nicm 504: {
1.47 nicm 505: struct winsize ws;
506: int mode;
507: char *argv0;
508: const char *ptr;
509: struct termios tio2;
1.1 nicm 510:
1.37 nicm 511: if (wp->fd != -1) {
1.1 nicm 512: close(wp->fd);
1.37 nicm 513: bufferevent_free(wp->event);
514: }
1.1 nicm 515: if (cmd != NULL) {
516: if (wp->cmd != NULL)
517: xfree(wp->cmd);
518: wp->cmd = xstrdup(cmd);
519: }
1.23 nicm 520: if (shell != NULL) {
521: if (wp->shell != NULL)
522: xfree(wp->shell);
523: wp->shell = xstrdup(shell);
524: }
1.1 nicm 525: if (cwd != NULL) {
526: if (wp->cwd != NULL)
527: xfree(wp->cwd);
528: wp->cwd = xstrdup(cwd);
529: }
530:
531: memset(&ws, 0, sizeof ws);
532: ws.ws_col = screen_size_x(&wp->base);
533: ws.ws_row = screen_size_y(&wp->base);
534:
1.42 nicm 535: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 536: case -1:
537: wp->fd = -1;
538: xasprintf(cause, "%s: %s", cmd, strerror(errno));
539: return (-1);
540: case 0:
541: if (chdir(wp->cwd) != 0)
542: chdir("/");
1.25 nicm 543:
544: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
545: fatal("tcgetattr failed");
546: if (tio != NULL)
547: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
548: tio2.c_cc[VERASE] = '\177';
549: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
550: fatal("tcgetattr failed");
1.18 nicm 551:
1.47 nicm 552: environ_push(env);
1.18 nicm 553:
1.50 nicm 554: clear_signals();
1.1 nicm 555: log_close();
556:
1.8 nicm 557: if (*wp->cmd != '\0') {
1.24 nicm 558: /* Set SHELL but only if it is currently not useful. */
559: shell = getenv("SHELL");
560: if (shell == NULL || *shell == '\0' || areshell(shell))
561: setenv("SHELL", wp->shell, 1);
562:
1.8 nicm 563: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
564: fatal("execl failed");
565: }
566:
567: /* No command; fork a login shell. */
1.23 nicm 568: ptr = strrchr(wp->shell, '/');
569: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 570: xasprintf(&argv0, "-%s", ptr + 1);
571: else
1.23 nicm 572: xasprintf(&argv0, "-%s", wp->shell);
1.24 nicm 573: setenv("SHELL", wp->shell, 1);
1.23 nicm 574: execl(wp->shell, argv0, (char *) NULL);
1.1 nicm 575: fatal("execl failed");
576: }
577:
578: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
579: fatal("fcntl failed");
580: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
581: fatal("fcntl failed");
582: if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
583: fatal("fcntl failed");
1.37 nicm 584: wp->event = bufferevent_new(wp->fd,
585: window_pane_read_callback, NULL, window_pane_error_callback, wp);
586: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 587:
588: return (0);
589: }
590:
1.41 nicm 591: /* ARGSUSED */
1.15 nicm 592: void
1.37 nicm 593: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
594: {
1.46 nicm 595: struct window_pane *wp = data;
596: char *new_data;
597: size_t new_size;
598:
599: new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
600: if (wp->pipe_fd != -1 && new_size > 0) {
601: new_data = EVBUFFER_DATA(wp->event->input);
602: bufferevent_write(wp->pipe_event, new_data, new_size);
603: }
604:
605: input_parse(wp);
1.37 nicm 606:
1.46 nicm 607: wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.37 nicm 608: }
609:
1.41 nicm 610: /* ARGSUSED */
1.37 nicm 611: void
612: window_pane_error_callback(
613: unused struct bufferevent *bufev, unused short what, void *data)
614: {
615: struct window_pane *wp = data;
616:
1.39 nicm 617: server_destroy_pane(wp);
1.37 nicm 618: }
619:
620: void
1.1 nicm 621: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
622: {
623: struct winsize ws;
624:
625: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 626: return;
1.1 nicm 627: wp->sx = sx;
628: wp->sy = sy;
629:
630: memset(&ws, 0, sizeof ws);
631: ws.ws_col = sx;
632: ws.ws_row = sy;
633:
634: screen_resize(&wp->base, sx, sy);
635: if (wp->mode != NULL)
636: wp->mode->resize(wp, sx, sy);
637:
638: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
639: fatal("ioctl failed");
1.44 nicm 640: }
641:
642: /*
643: * Enter alternative screen mode. A copy of the visible screen is saved and the
644: * history is not updated
645: */
646: void
647: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
648: {
649: struct screen *s = &wp->base;
650: u_int sx, sy;
651:
652: if (wp->saved_grid != NULL)
653: return;
654: if (!options_get_number(&wp->window->options, "alternate-screen"))
655: return;
656: sx = screen_size_x(s);
657: sy = screen_size_y(s);
658:
659: wp->saved_grid = grid_create(sx, sy, 0);
660: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
661: wp->saved_cx = s->cx;
662: wp->saved_cy = s->cy;
663: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
664:
665: grid_view_clear(s->grid, 0, 0, sx, sy);
666:
667: wp->base.grid->flags &= ~GRID_HISTORY;
668:
669: wp->flags |= PANE_REDRAW;
670: }
671:
672: /* Exit alternate screen mode and restore the copied grid. */
673: void
674: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
675: {
676: struct screen *s = &wp->base;
677: u_int sx, sy;
678:
679: if (wp->saved_grid == NULL)
680: return;
681: if (!options_get_number(&wp->window->options, "alternate-screen"))
682: return;
683: sx = screen_size_x(s);
684: sy = screen_size_y(s);
685:
686: /*
687: * If the current size is bigger, temporarily resize to the old size
688: * before copying back.
689: */
690: if (sy > wp->saved_grid->sy)
691: screen_resize(s, sx, wp->saved_grid->sy);
692:
693: /* Restore the grid, cursor position and cell. */
694: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
695: s->cx = wp->saved_cx;
696: if (s->cx > screen_size_x(s) - 1)
697: s->cx = screen_size_x(s) - 1;
698: s->cy = wp->saved_cy;
699: if (s->cy > screen_size_y(s) - 1)
700: s->cy = screen_size_y(s) - 1;
701: memcpy(gc, &wp->saved_cell, sizeof *gc);
702:
703: /*
704: * Turn history back on (so resize can use it) and then resize back to
705: * the current size.
706: */
707: wp->base.grid->flags |= GRID_HISTORY;
708: if (sy > wp->saved_grid->sy)
709: screen_resize(s, sx, sy);
710:
711: grid_destroy(wp->saved_grid);
712: wp->saved_grid = NULL;
713:
714: wp->flags |= PANE_REDRAW;
1.1 nicm 715: }
716:
717: int
718: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
719: {
720: struct screen *s;
721:
1.15 nicm 722: if (wp->mode != NULL)
1.1 nicm 723: return (1);
724: wp->mode = mode;
725:
726: if ((s = wp->mode->init(wp)) != NULL)
727: wp->screen = s;
1.34 nicm 728: wp->flags |= PANE_REDRAW;
1.1 nicm 729: return (0);
730: }
731:
732: void
733: window_pane_reset_mode(struct window_pane *wp)
734: {
735: if (wp->mode == NULL)
736: return;
737:
738: wp->mode->free(wp);
739: wp->mode = NULL;
740:
741: wp->screen = &wp->base;
1.34 nicm 742: wp->flags |= PANE_REDRAW;
1.1 nicm 743: }
744:
745: void
1.51 nicm 746: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1 nicm 747: {
1.27 nicm 748: struct window_pane *wp2;
749:
1.30 nicm 750: if (!window_pane_visible(wp))
1.3 nicm 751: return;
752:
1.1 nicm 753: if (wp->mode != NULL) {
754: if (wp->mode->key != NULL)
1.51 nicm 755: wp->mode->key(wp, sess, key);
1.27 nicm 756: return;
1.30 nicm 757: }
1.27 nicm 758:
1.30 nicm 759: if (wp->fd == -1)
760: return;
1.27 nicm 761: input_key(wp, key);
762: if (options_get_number(&wp->window->options, "synchronize-panes")) {
763: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
764: if (wp2 == wp || wp2->mode != NULL)
765: continue;
766: if (wp2->fd != -1 && window_pane_visible(wp2))
767: input_key(wp2, key);
768: }
769: }
1.1 nicm 770: }
771:
772: void
773: window_pane_mouse(
1.51 nicm 774: struct window_pane *wp, struct session *sess, struct mouse_event *m)
1.1 nicm 775: {
1.30 nicm 776: if (!window_pane_visible(wp))
1.3 nicm 777: return;
778:
1.31 nicm 779: if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1 nicm 780: return;
1.31 nicm 781: if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1 nicm 782: return;
1.31 nicm 783: m->x -= wp->xoff;
784: m->y -= wp->yoff;
1.1 nicm 785:
786: if (wp->mode != NULL) {
787: if (wp->mode->mouse != NULL)
1.51 nicm 788: wp->mode->mouse(wp, sess, m);
1.30 nicm 789: } else if (wp->fd != -1)
1.31 nicm 790: input_mouse(wp, m);
1.10 nicm 791: }
792:
793: int
794: window_pane_visible(struct window_pane *wp)
795: {
796: struct window *w = wp->window;
797:
798: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
799: return (0);
800: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
801: return (0);
802: return (1);
1.1 nicm 803: }
804:
805: char *
1.5 nicm 806: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 807: {
1.4 nicm 808: struct screen *s = &wp->base;
1.5 nicm 809: char *newsearchstr, *line, *msg;
1.4 nicm 810: u_int i;
811:
1.5 nicm 812: msg = NULL;
813: xasprintf(&newsearchstr, "*%s*", searchstr);
814:
1.4 nicm 815: for (i = 0; i < screen_size_y(s); i++) {
816: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 817: if (fnmatch(newsearchstr, line, 0) == 0) {
818: msg = line;
819: if (lineno != NULL)
820: *lineno = i;
821: break;
822: }
1.4 nicm 823: xfree(line);
824: }
1.5 nicm 825:
826: xfree(newsearchstr);
827: return (msg);
1.45 nicm 828: }
829:
830: /* Find the pane directly above another. */
831: struct window_pane *
832: window_pane_find_up(struct window_pane *wp)
833: {
834: struct window_pane *wp2;
835: u_int left, top;
836:
837: if (wp == NULL || !window_pane_visible(wp))
838: return (NULL);
839:
840: top = wp->yoff;
841: if (top == 0)
842: top = wp->window->sy + 1;
843: left = wp->xoff;
844:
845: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
846: if (!window_pane_visible(wp2))
847: continue;
848: if (wp2->yoff + wp2->sy + 1 != top)
849: continue;
850: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
851: return (wp2);
852: }
853: return (NULL);
854: }
855:
856: /* Find the pane directly below another. */
857: struct window_pane *
858: window_pane_find_down(struct window_pane *wp)
859: {
860: struct window_pane *wp2;
861: u_int left, bottom;
862:
863: if (wp == NULL || !window_pane_visible(wp))
864: return (NULL);
865:
866: bottom = wp->yoff + wp->sy + 1;
867: if (bottom >= wp->window->sy)
868: bottom = 0;
869: left = wp->xoff;
870:
871: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
872: if (!window_pane_visible(wp2))
873: continue;
874: if (wp2->yoff != bottom)
875: continue;
876: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
877: return (wp2);
878: }
879: return (NULL);
880: }
881:
882: /*
883: * Find the pane directly to the left of another, adjacent to the left side and
884: * containing the top edge.
885: */
886: struct window_pane *
887: window_pane_find_left(struct window_pane *wp)
888: {
889: struct window_pane *wp2;
890: u_int left, top;
891:
892: if (wp == NULL || !window_pane_visible(wp))
893: return (NULL);
894:
895: left = wp->xoff;
896: if (left == 0)
897: left = wp->window->sx + 1;
898: top = wp->yoff;
899:
900: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
901: if (!window_pane_visible(wp2))
902: continue;
903: if (wp2->xoff + wp2->sx + 1 != left)
904: continue;
905: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
906: return (wp2);
907: }
908: return (NULL);
909: }
910:
911: /*
912: * Find the pane directly to the right of another, that is adjacent to the
913: * right edge and including the top edge.
914: */
915: struct window_pane *
916: window_pane_find_right(struct window_pane *wp)
917: {
918: struct window_pane *wp2;
919: u_int right, top;
920:
921: if (wp == NULL || !window_pane_visible(wp))
922: return (NULL);
923:
924: right = wp->xoff + wp->sx + 1;
925: if (right >= wp->window->sx)
926: right = 0;
927: top = wp->yoff;
928:
929: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
930: if (!window_pane_visible(wp2))
931: continue;
932: if (wp2->xoff != right)
933: continue;
934: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
935: return (wp2);
936: }
937: return (NULL);
1.1 nicm 938: }