Annotation of src/usr.bin/tmux/window.c, Revision 1.56
1.56 ! nicm 1: /* $OpenBSD: window.c,v 1.55 2010/08/25 19:19:43 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 *
1.53 nicm 176: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52 nicm 177: {
178: for (; n > 0; n--) {
179: if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53 nicm 180: wl = RB_MIN(winlinks, &s->windows);
1.52 nicm 181: }
182:
183: return (wl);
184: }
185:
186: struct winlink *
1.53 nicm 187: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52 nicm 188: {
189: for (; n > 0; n--) {
190: if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53 nicm 191: wl = RB_MAX(winlinks, &s->windows);
1.52 nicm 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.53 nicm 392: }
393:
394: struct window_pane *
395: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
396: {
397: for (; n > 0; n--) {
398: if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
399: wp = TAILQ_FIRST(&w->panes);
400: }
401:
402: return (wp);
403: }
404:
405: struct window_pane *
406: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
407: u_int n)
408: {
409: for (; n > 0; n--) {
410: if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
411: wp = TAILQ_LAST(&w->panes, window_panes);
412: }
413:
414: return (wp);
1.13 nicm 415: }
416:
417: u_int
418: window_pane_index(struct window *w, struct window_pane *wp)
419: {
420: struct window_pane *wq;
421: u_int n;
422:
423: n = 0;
424: TAILQ_FOREACH(wq, &w->panes, entry) {
425: if (wp == wq)
426: break;
427: n++;
428: }
429: return (n);
1.1 nicm 430: }
431:
432: u_int
433: window_count_panes(struct window *w)
434: {
435: struct window_pane *wp;
436: u_int n;
437:
438: n = 0;
439: TAILQ_FOREACH(wp, &w->panes, entry)
440: n++;
441: return (n);
442: }
443:
444: void
445: window_destroy_panes(struct window *w)
446: {
447: struct window_pane *wp;
448:
449: while (!TAILQ_EMPTY(&w->panes)) {
450: wp = TAILQ_FIRST(&w->panes);
451: TAILQ_REMOVE(&w->panes, wp, entry);
452: window_pane_destroy(wp);
453: }
454: }
455:
456: struct window_pane *
457: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
458: {
459: struct window_pane *wp;
460:
461: wp = xcalloc(1, sizeof *wp);
462: wp->window = w;
463:
464: wp->cmd = NULL;
1.23 nicm 465: wp->shell = NULL;
1.1 nicm 466: wp->cwd = NULL;
467:
468: wp->fd = -1;
1.37 nicm 469: wp->event = NULL;
1.1 nicm 470:
471: wp->mode = NULL;
1.14 nicm 472:
473: wp->layout_cell = NULL;
1.1 nicm 474:
475: wp->xoff = 0;
1.42 nicm 476: wp->yoff = 0;
1.1 nicm 477:
478: wp->sx = sx;
479: wp->sy = sy;
480:
1.32 nicm 481: wp->pipe_fd = -1;
482: wp->pipe_off = 0;
1.36 nicm 483: wp->pipe_event = NULL;
1.32 nicm 484:
1.9 nicm 485: wp->saved_grid = NULL;
486:
1.1 nicm 487: screen_init(&wp->base, sx, sy, hlimit);
488: wp->screen = &wp->base;
489:
490: input_init(wp);
491:
492: return (wp);
493: }
494:
495: void
496: window_pane_destroy(struct window_pane *wp)
497: {
1.55 nicm 498: window_pane_reset_mode(wp);
499:
1.37 nicm 500: if (wp->fd != -1) {
1.1 nicm 501: close(wp->fd);
1.37 nicm 502: bufferevent_free(wp->event);
503: }
1.1 nicm 504:
505: input_free(wp);
506:
507: screen_free(&wp->base);
1.9 nicm 508: if (wp->saved_grid != NULL)
509: grid_destroy(wp->saved_grid);
1.1 nicm 510:
1.32 nicm 511: if (wp->pipe_fd != -1) {
512: close(wp->pipe_fd);
1.36 nicm 513: bufferevent_free(wp->pipe_event);
1.32 nicm 514: }
515:
1.1 nicm 516: if (wp->cwd != NULL)
517: xfree(wp->cwd);
1.23 nicm 518: if (wp->shell != NULL)
519: xfree(wp->shell);
1.1 nicm 520: if (wp->cmd != NULL)
521: xfree(wp->cmd);
522: xfree(wp);
523: }
524:
525: int
1.23 nicm 526: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21 nicm 527: const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1 nicm 528: {
1.47 nicm 529: struct winsize ws;
530: int mode;
531: char *argv0;
532: const char *ptr;
533: struct termios tio2;
1.1 nicm 534:
1.37 nicm 535: if (wp->fd != -1) {
1.1 nicm 536: close(wp->fd);
1.37 nicm 537: bufferevent_free(wp->event);
538: }
1.1 nicm 539: if (cmd != NULL) {
540: if (wp->cmd != NULL)
541: xfree(wp->cmd);
542: wp->cmd = xstrdup(cmd);
543: }
1.23 nicm 544: if (shell != NULL) {
545: if (wp->shell != NULL)
546: xfree(wp->shell);
547: wp->shell = xstrdup(shell);
548: }
1.1 nicm 549: if (cwd != NULL) {
550: if (wp->cwd != NULL)
551: xfree(wp->cwd);
552: wp->cwd = xstrdup(cwd);
553: }
554:
555: memset(&ws, 0, sizeof ws);
556: ws.ws_col = screen_size_x(&wp->base);
557: ws.ws_row = screen_size_y(&wp->base);
558:
1.42 nicm 559: switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1 nicm 560: case -1:
561: wp->fd = -1;
562: xasprintf(cause, "%s: %s", cmd, strerror(errno));
563: return (-1);
564: case 0:
565: if (chdir(wp->cwd) != 0)
566: chdir("/");
1.25 nicm 567:
568: if (tcgetattr(STDIN_FILENO, &tio2) != 0)
569: fatal("tcgetattr failed");
570: if (tio != NULL)
571: memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
572: tio2.c_cc[VERASE] = '\177';
573: if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
574: fatal("tcgetattr failed");
1.18 nicm 575:
1.56 ! nicm 576: closefrom(STDERR_FILENO + 1);
! 577:
1.47 nicm 578: environ_push(env);
1.18 nicm 579:
1.54 nicm 580: clear_signals(1);
1.1 nicm 581: log_close();
582:
1.8 nicm 583: if (*wp->cmd != '\0') {
1.24 nicm 584: /* Set SHELL but only if it is currently not useful. */
585: shell = getenv("SHELL");
586: if (shell == NULL || *shell == '\0' || areshell(shell))
587: setenv("SHELL", wp->shell, 1);
588:
1.8 nicm 589: execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
590: fatal("execl failed");
591: }
592:
593: /* No command; fork a login shell. */
1.23 nicm 594: ptr = strrchr(wp->shell, '/');
595: if (ptr != NULL && *(ptr + 1) != '\0')
1.8 nicm 596: xasprintf(&argv0, "-%s", ptr + 1);
597: else
1.23 nicm 598: xasprintf(&argv0, "-%s", wp->shell);
1.24 nicm 599: setenv("SHELL", wp->shell, 1);
1.23 nicm 600: execl(wp->shell, argv0, (char *) NULL);
1.1 nicm 601: fatal("execl failed");
602: }
603:
604: if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
605: fatal("fcntl failed");
606: if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
607: fatal("fcntl failed");
1.37 nicm 608: wp->event = bufferevent_new(wp->fd,
609: window_pane_read_callback, NULL, window_pane_error_callback, wp);
610: bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1 nicm 611:
612: return (0);
613: }
614:
1.41 nicm 615: /* ARGSUSED */
1.15 nicm 616: void
1.37 nicm 617: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
618: {
1.46 nicm 619: struct window_pane *wp = data;
620: char *new_data;
621: size_t new_size;
622:
623: new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
624: if (wp->pipe_fd != -1 && new_size > 0) {
625: new_data = EVBUFFER_DATA(wp->event->input);
626: bufferevent_write(wp->pipe_event, new_data, new_size);
627: }
628:
629: input_parse(wp);
1.37 nicm 630:
1.46 nicm 631: wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.37 nicm 632: }
633:
1.41 nicm 634: /* ARGSUSED */
1.37 nicm 635: void
636: window_pane_error_callback(
637: unused struct bufferevent *bufev, unused short what, void *data)
638: {
639: struct window_pane *wp = data;
640:
1.39 nicm 641: server_destroy_pane(wp);
1.37 nicm 642: }
643:
644: void
1.1 nicm 645: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
646: {
647: struct winsize ws;
648:
649: if (sx == wp->sx && sy == wp->sy)
1.15 nicm 650: return;
1.1 nicm 651: wp->sx = sx;
652: wp->sy = sy;
653:
654: memset(&ws, 0, sizeof ws);
655: ws.ws_col = sx;
656: ws.ws_row = sy;
657:
658: screen_resize(&wp->base, sx, sy);
659: if (wp->mode != NULL)
660: wp->mode->resize(wp, sx, sy);
661:
662: if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
663: fatal("ioctl failed");
1.44 nicm 664: }
665:
666: /*
667: * Enter alternative screen mode. A copy of the visible screen is saved and the
668: * history is not updated
669: */
670: void
671: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
672: {
673: struct screen *s = &wp->base;
674: u_int sx, sy;
675:
676: if (wp->saved_grid != NULL)
677: return;
678: if (!options_get_number(&wp->window->options, "alternate-screen"))
679: return;
680: sx = screen_size_x(s);
681: sy = screen_size_y(s);
682:
683: wp->saved_grid = grid_create(sx, sy, 0);
684: grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
685: wp->saved_cx = s->cx;
686: wp->saved_cy = s->cy;
687: memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
688:
689: grid_view_clear(s->grid, 0, 0, sx, sy);
690:
691: wp->base.grid->flags &= ~GRID_HISTORY;
692:
693: wp->flags |= PANE_REDRAW;
694: }
695:
696: /* Exit alternate screen mode and restore the copied grid. */
697: void
698: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
699: {
700: struct screen *s = &wp->base;
701: u_int sx, sy;
702:
703: if (wp->saved_grid == NULL)
704: return;
705: if (!options_get_number(&wp->window->options, "alternate-screen"))
706: return;
707: sx = screen_size_x(s);
708: sy = screen_size_y(s);
709:
710: /*
711: * If the current size is bigger, temporarily resize to the old size
712: * before copying back.
713: */
714: if (sy > wp->saved_grid->sy)
715: screen_resize(s, sx, wp->saved_grid->sy);
716:
717: /* Restore the grid, cursor position and cell. */
718: grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
719: s->cx = wp->saved_cx;
720: if (s->cx > screen_size_x(s) - 1)
721: s->cx = screen_size_x(s) - 1;
722: s->cy = wp->saved_cy;
723: if (s->cy > screen_size_y(s) - 1)
724: s->cy = screen_size_y(s) - 1;
725: memcpy(gc, &wp->saved_cell, sizeof *gc);
726:
727: /*
728: * Turn history back on (so resize can use it) and then resize back to
729: * the current size.
730: */
731: wp->base.grid->flags |= GRID_HISTORY;
732: if (sy > wp->saved_grid->sy)
733: screen_resize(s, sx, sy);
734:
735: grid_destroy(wp->saved_grid);
736: wp->saved_grid = NULL;
737:
738: wp->flags |= PANE_REDRAW;
1.1 nicm 739: }
740:
741: int
742: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
743: {
744: struct screen *s;
745:
1.15 nicm 746: if (wp->mode != NULL)
1.1 nicm 747: return (1);
748: wp->mode = mode;
749:
750: if ((s = wp->mode->init(wp)) != NULL)
751: wp->screen = s;
1.34 nicm 752: wp->flags |= PANE_REDRAW;
1.1 nicm 753: return (0);
754: }
755:
756: void
757: window_pane_reset_mode(struct window_pane *wp)
758: {
759: if (wp->mode == NULL)
760: return;
761:
762: wp->mode->free(wp);
763: wp->mode = NULL;
764:
765: wp->screen = &wp->base;
1.34 nicm 766: wp->flags |= PANE_REDRAW;
1.1 nicm 767: }
768:
769: void
1.51 nicm 770: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1 nicm 771: {
1.27 nicm 772: struct window_pane *wp2;
773:
1.30 nicm 774: if (!window_pane_visible(wp))
1.3 nicm 775: return;
776:
1.1 nicm 777: if (wp->mode != NULL) {
778: if (wp->mode->key != NULL)
1.51 nicm 779: wp->mode->key(wp, sess, key);
1.27 nicm 780: return;
1.30 nicm 781: }
1.27 nicm 782:
1.30 nicm 783: if (wp->fd == -1)
784: return;
1.27 nicm 785: input_key(wp, key);
786: if (options_get_number(&wp->window->options, "synchronize-panes")) {
787: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
788: if (wp2 == wp || wp2->mode != NULL)
789: continue;
790: if (wp2->fd != -1 && window_pane_visible(wp2))
791: input_key(wp2, key);
792: }
793: }
1.1 nicm 794: }
795:
796: void
797: window_pane_mouse(
1.51 nicm 798: struct window_pane *wp, struct session *sess, struct mouse_event *m)
1.1 nicm 799: {
1.30 nicm 800: if (!window_pane_visible(wp))
1.3 nicm 801: return;
802:
1.31 nicm 803: if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1 nicm 804: return;
1.31 nicm 805: if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1 nicm 806: return;
1.31 nicm 807: m->x -= wp->xoff;
808: m->y -= wp->yoff;
1.1 nicm 809:
810: if (wp->mode != NULL) {
811: if (wp->mode->mouse != NULL)
1.51 nicm 812: wp->mode->mouse(wp, sess, m);
1.30 nicm 813: } else if (wp->fd != -1)
1.31 nicm 814: input_mouse(wp, m);
1.10 nicm 815: }
816:
817: int
818: window_pane_visible(struct window_pane *wp)
819: {
820: struct window *w = wp->window;
821:
822: if (wp->xoff >= w->sx || wp->yoff >= w->sy)
823: return (0);
824: if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
825: return (0);
826: return (1);
1.1 nicm 827: }
828:
829: char *
1.5 nicm 830: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1 nicm 831: {
1.4 nicm 832: struct screen *s = &wp->base;
1.5 nicm 833: char *newsearchstr, *line, *msg;
1.4 nicm 834: u_int i;
835:
1.5 nicm 836: msg = NULL;
837: xasprintf(&newsearchstr, "*%s*", searchstr);
838:
1.4 nicm 839: for (i = 0; i < screen_size_y(s); i++) {
840: line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5 nicm 841: if (fnmatch(newsearchstr, line, 0) == 0) {
842: msg = line;
843: if (lineno != NULL)
844: *lineno = i;
845: break;
846: }
1.4 nicm 847: xfree(line);
848: }
1.5 nicm 849:
850: xfree(newsearchstr);
851: return (msg);
1.45 nicm 852: }
853:
854: /* Find the pane directly above another. */
855: struct window_pane *
856: window_pane_find_up(struct window_pane *wp)
857: {
858: struct window_pane *wp2;
859: u_int left, top;
860:
861: if (wp == NULL || !window_pane_visible(wp))
862: return (NULL);
863:
864: top = wp->yoff;
865: if (top == 0)
866: top = wp->window->sy + 1;
867: left = wp->xoff;
868:
869: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
870: if (!window_pane_visible(wp2))
871: continue;
872: if (wp2->yoff + wp2->sy + 1 != top)
873: continue;
874: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
875: return (wp2);
876: }
877: return (NULL);
878: }
879:
880: /* Find the pane directly below another. */
881: struct window_pane *
882: window_pane_find_down(struct window_pane *wp)
883: {
884: struct window_pane *wp2;
885: u_int left, bottom;
886:
887: if (wp == NULL || !window_pane_visible(wp))
888: return (NULL);
889:
890: bottom = wp->yoff + wp->sy + 1;
891: if (bottom >= wp->window->sy)
892: bottom = 0;
893: left = wp->xoff;
894:
895: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
896: if (!window_pane_visible(wp2))
897: continue;
898: if (wp2->yoff != bottom)
899: continue;
900: if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
901: return (wp2);
902: }
903: return (NULL);
904: }
905:
906: /*
907: * Find the pane directly to the left of another, adjacent to the left side and
908: * containing the top edge.
909: */
910: struct window_pane *
911: window_pane_find_left(struct window_pane *wp)
912: {
913: struct window_pane *wp2;
914: u_int left, top;
915:
916: if (wp == NULL || !window_pane_visible(wp))
917: return (NULL);
918:
919: left = wp->xoff;
920: if (left == 0)
921: left = wp->window->sx + 1;
922: top = wp->yoff;
923:
924: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
925: if (!window_pane_visible(wp2))
926: continue;
927: if (wp2->xoff + wp2->sx + 1 != left)
928: continue;
929: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
930: return (wp2);
931: }
932: return (NULL);
933: }
934:
935: /*
936: * Find the pane directly to the right of another, that is adjacent to the
937: * right edge and including the top edge.
938: */
939: struct window_pane *
940: window_pane_find_right(struct window_pane *wp)
941: {
942: struct window_pane *wp2;
943: u_int right, top;
944:
945: if (wp == NULL || !window_pane_visible(wp))
946: return (NULL);
947:
948: right = wp->xoff + wp->sx + 1;
949: if (right >= wp->window->sx)
950: right = 0;
951: top = wp->yoff;
952:
953: TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
954: if (!window_pane_visible(wp2))
955: continue;
956: if (wp2->xoff != right)
957: continue;
958: if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
959: return (wp2);
960: }
961: return (NULL);
1.1 nicm 962: }