Don't die if fail to get root directory, from Ben Boeckel.
[tmux-openbsd.git] / window.c
blob0489d105ae23dadc038d33751b69fc975bb2aace
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <fnmatch.h>
25 #include <paths.h>
26 #include <pwd.h>
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>
35 #include "tmux.h"
38 * Each window is attached to a number of panes, each of which is a pty. This
39 * file contains code to handle them.
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.
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.
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.
56 /* Global window list. */
57 struct windows windows;
59 /* Global panes tree. */
60 struct window_pane_tree all_window_panes;
61 u_int next_window_pane_id;
62 u_int next_window_id;
64 void window_pane_read_callback(struct bufferevent *, void *);
65 void window_pane_error_callback(struct bufferevent *, short, void *);
67 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
69 int
70 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
72 return (wl1->idx - wl2->idx);
75 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
77 int
78 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
80 return (wp1->id - wp2->id);
83 struct winlink *
84 winlink_find_by_window(struct winlinks *wwl, struct window *w)
86 struct winlink *wl;
88 RB_FOREACH(wl, winlinks, wwl) {
89 if (wl->window == w)
90 return (wl);
93 return (NULL);
96 struct winlink *
97 winlink_find_by_index(struct winlinks *wwl, int idx)
99 struct winlink wl;
101 if (idx < 0)
102 fatalx("bad index");
104 wl.idx = idx;
105 return (RB_FIND(winlinks, wwl, &wl));
108 struct winlink *
109 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
111 struct winlink *wl;
113 RB_FOREACH(wl, winlinks, wwl) {
114 if (wl->window->id == id)
115 return (wl);
117 return NULL;
121 winlink_next_index(struct winlinks *wwl, int idx)
123 int i;
125 i = idx;
126 do {
127 if (winlink_find_by_index(wwl, i) == NULL)
128 return (i);
129 if (i == INT_MAX)
130 i = 0;
131 else
132 i++;
133 } while (i != idx);
134 return (-1);
137 u_int
138 winlink_count(struct winlinks *wwl)
140 struct winlink *wl;
141 u_int n;
143 n = 0;
144 RB_FOREACH(wl, winlinks, wwl)
145 n++;
147 return (n);
150 struct winlink *
151 winlink_add(struct winlinks *wwl, int idx)
153 struct winlink *wl;
155 if (idx < 0) {
156 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
157 return (NULL);
158 } else if (winlink_find_by_index(wwl, idx) != NULL)
159 return (NULL);
161 wl = xcalloc(1, sizeof *wl);
162 wl->idx = idx;
163 RB_INSERT(winlinks, wwl, wl);
165 return (wl);
168 void
169 winlink_set_window(struct winlink *wl, struct window *w)
171 wl->window = w;
172 w->references++;
175 void
176 winlink_remove(struct winlinks *wwl, struct winlink *wl)
178 struct window *w = wl->window;
180 RB_REMOVE(winlinks, wwl, wl);
181 if (wl->status_text != NULL)
182 xfree(wl->status_text);
183 xfree(wl);
185 if (w != NULL) {
186 if (w->references == 0)
187 fatal("bad reference count");
188 w->references--;
189 if (w->references == 0)
190 window_destroy(w);
194 struct winlink *
195 winlink_next(struct winlink *wl)
197 return (RB_NEXT(winlinks, wwl, wl));
200 struct winlink *
201 winlink_previous(struct winlink *wl)
203 return (RB_PREV(winlinks, wwl, wl));
206 struct winlink *
207 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
209 for (; n > 0; n--) {
210 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
211 wl = RB_MIN(winlinks, &s->windows);
214 return (wl);
217 struct winlink *
218 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
220 for (; n > 0; n--) {
221 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
222 wl = RB_MAX(winlinks, &s->windows);
225 return (wl);
228 void
229 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
231 if (wl == NULL)
232 return;
234 winlink_stack_remove(stack, wl);
235 TAILQ_INSERT_HEAD(stack, wl, sentry);
238 void
239 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
241 struct winlink *wl2;
243 if (wl == NULL)
244 return;
246 TAILQ_FOREACH(wl2, stack, sentry) {
247 if (wl2 == wl) {
248 TAILQ_REMOVE(stack, wl, sentry);
249 return;
255 window_index(struct window *s, u_int *i)
257 for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
258 if (s == ARRAY_ITEM(&windows, *i))
259 return (0);
261 return (-1);
264 struct window *
265 window_find_by_id(u_int id)
267 struct window *w;
268 u_int i;
270 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
271 w = ARRAY_ITEM(&windows, i);
272 if (w->id == id)
273 return (w);
275 return NULL;
278 struct window *
279 window_create1(u_int sx, u_int sy)
281 struct window *w;
282 u_int i;
284 w = xcalloc(1, sizeof *w);
285 w->id = next_window_id++;
286 w->name = NULL;
287 w->flags = 0;
289 TAILQ_INIT(&w->panes);
290 w->active = NULL;
292 w->lastlayout = -1;
293 w->layout_root = NULL;
295 w->sx = sx;
296 w->sy = sy;
298 queue_window_name(w);
300 options_init(&w->options, &global_w_options);
302 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
303 if (ARRAY_ITEM(&windows, i) == NULL) {
304 ARRAY_SET(&windows, i, w);
305 break;
308 if (i == ARRAY_LENGTH(&windows))
309 ARRAY_ADD(&windows, w);
310 w->references = 0;
312 return (w);
315 struct window *
316 window_create(const char *name, const char *cmd, const char *shell,
317 const char *cwd, struct environ *env, struct termios *tio,
318 u_int sx, u_int sy, u_int hlimit,char **cause)
320 struct window *w;
321 struct window_pane *wp;
323 w = window_create1(sx, sy);
324 wp = window_add_pane(w, hlimit);
325 layout_init(w);
326 if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
327 window_destroy(w);
328 return (NULL);
330 w->active = TAILQ_FIRST(&w->panes);
331 if (name != NULL) {
332 w->name = xstrdup(name);
333 options_set_number(&w->options, "automatic-rename", 0);
334 } else
335 w->name = default_window_name(w);
336 return (w);
339 void
340 window_destroy(struct window *w)
342 u_int i;
344 if (window_index(w, &i) != 0)
345 fatalx("index not found");
346 ARRAY_SET(&windows, i, NULL);
347 while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
348 ARRAY_TRUNC(&windows, 1);
350 if (w->layout_root != NULL)
351 layout_free(w);
353 evtimer_del(&w->name_timer);
355 options_free(&w->options);
357 window_destroy_panes(w);
359 if (w->name != NULL)
360 xfree(w->name);
361 xfree(w);
364 void
365 window_set_name(struct window *w, const char *new_name)
367 if (w->name != NULL)
368 xfree(w->name);
369 w->name = xstrdup(new_name);
372 void
373 window_resize(struct window *w, u_int sx, u_int sy)
375 w->sx = sx;
376 w->sy = sy;
379 void
380 window_set_active_pane(struct window *w, struct window_pane *wp)
382 if (wp == w->active)
383 return;
384 w->last = w->active;
385 w->active = wp;
386 while (!window_pane_visible(w->active)) {
387 w->active = TAILQ_PREV(w->active, window_panes, entry);
388 if (w->active == NULL)
389 w->active = TAILQ_LAST(&w->panes, window_panes);
390 if (w->active == wp)
391 return;
395 struct window_pane *
396 window_get_active_at(struct window *w, u_int x, u_int y)
398 struct window_pane *wp;
400 TAILQ_FOREACH(wp, &w->panes, entry) {
401 if (!window_pane_visible(wp))
402 continue;
403 if (x < wp->xoff || x > wp->xoff + wp->sx)
404 continue;
405 if (y < wp->yoff || y > wp->yoff + wp->sy)
406 continue;
407 return (wp);
409 return (NULL);
412 void
413 window_set_active_at(struct window *w, u_int x, u_int y)
415 struct window_pane *wp;
417 wp = window_get_active_at(w, x, y);
418 if (wp != NULL && wp != w->active)
419 window_set_active_pane(w, wp);
422 struct window_pane *
423 window_find_string(struct window *w, const char *s)
425 u_int x, y;
427 x = w->sx / 2;
428 y = w->sy / 2;
430 if (strcasecmp(s, "top") == 0)
431 y = 0;
432 else if (strcasecmp(s, "bottom") == 0)
433 y = w->sy - 1;
434 else if (strcasecmp(s, "left") == 0)
435 x = 0;
436 else if (strcasecmp(s, "right") == 0)
437 x = w->sx - 1;
438 else if (strcasecmp(s, "top-left") == 0) {
439 x = 0;
440 y = 0;
441 } else if (strcasecmp(s, "top-right") == 0) {
442 x = w->sx - 1;
443 y = 0;
444 } else if (strcasecmp(s, "bottom-left") == 0) {
445 x = 0;
446 y = w->sy - 1;
447 } else if (strcasecmp(s, "bottom-right") == 0) {
448 x = w->sx - 1;
449 y = w->sy - 1;
450 } else
451 return (NULL);
453 return (window_get_active_at(w, x, y));
456 struct window_pane *
457 window_add_pane(struct window *w, u_int hlimit)
459 struct window_pane *wp;
461 wp = window_pane_create(w, w->sx, w->sy, hlimit);
462 if (TAILQ_EMPTY(&w->panes))
463 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
464 else
465 TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
466 return (wp);
469 void
470 window_remove_pane(struct window *w, struct window_pane *wp)
472 if (wp == w->active) {
473 w->active = w->last;
474 w->last = NULL;
475 if (w->active == NULL) {
476 w->active = TAILQ_PREV(wp, window_panes, entry);
477 if (w->active == NULL)
478 w->active = TAILQ_NEXT(wp, entry);
480 } else if (wp == w->last)
481 w->last = NULL;
483 TAILQ_REMOVE(&w->panes, wp, entry);
484 window_pane_destroy(wp);
487 struct window_pane *
488 window_pane_at_index(struct window *w, u_int idx)
490 struct window_pane *wp;
491 u_int n;
493 n = options_get_number(&w->options, "pane-base-index");
494 TAILQ_FOREACH(wp, &w->panes, entry) {
495 if (n == idx)
496 return (wp);
497 n++;
499 return (NULL);
502 struct window_pane *
503 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
505 for (; n > 0; n--) {
506 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
507 wp = TAILQ_FIRST(&w->panes);
510 return (wp);
513 struct window_pane *
514 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
515 u_int n)
517 for (; n > 0; n--) {
518 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
519 wp = TAILQ_LAST(&w->panes, window_panes);
522 return (wp);
526 window_pane_index(struct window_pane *wp, u_int *i)
528 struct window_pane *wq;
529 struct window *w = wp->window;
531 *i = options_get_number(&w->options, "pane-base-index");
532 TAILQ_FOREACH(wq, &w->panes, entry) {
533 if (wp == wq) {
534 return (0);
536 (*i)++;
539 return (-1);
542 u_int
543 window_count_panes(struct window *w)
545 struct window_pane *wp;
546 u_int n;
548 n = 0;
549 TAILQ_FOREACH(wp, &w->panes, entry)
550 n++;
551 return (n);
554 void
555 window_destroy_panes(struct window *w)
557 struct window_pane *wp;
559 while (!TAILQ_EMPTY(&w->panes)) {
560 wp = TAILQ_FIRST(&w->panes);
561 TAILQ_REMOVE(&w->panes, wp, entry);
562 window_pane_destroy(wp);
566 /* Return list of printable window flag symbols. No flags is just a space. */
567 char *
568 window_printable_flags(struct session *s, struct winlink *wl)
570 char flags[BUFSIZ];
571 int pos;
573 pos = 0;
574 if (wl->flags & WINLINK_ACTIVITY)
575 flags[pos++] = '#';
576 if (wl->flags & WINLINK_BELL)
577 flags[pos++] = '!';
578 if (wl->flags & WINLINK_CONTENT)
579 flags[pos++] = '+';
580 if (wl->flags & WINLINK_SILENCE)
581 flags[pos++] = '~';
582 if (wl == s->curw)
583 flags[pos++] = '*';
584 if (wl == TAILQ_FIRST(&s->lastw))
585 flags[pos++] = '-';
586 if (pos == 0)
587 flags[pos++] = ' ';
588 flags[pos] = '\0';
589 return (xstrdup(flags));
592 /* Find pane in global tree by id. */
593 struct window_pane *
594 window_pane_find_by_id(u_int id)
596 struct window_pane wp;
598 wp.id = id;
599 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
602 struct window_pane *
603 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
605 struct window_pane *wp;
607 wp = xcalloc(1, sizeof *wp);
608 wp->window = w;
610 wp->id = next_window_pane_id++;
611 RB_INSERT(window_pane_tree, &all_window_panes, wp);
613 wp->cmd = NULL;
614 wp->shell = NULL;
615 wp->cwd = NULL;
617 wp->fd = -1;
618 wp->event = NULL;
620 wp->mode = NULL;
622 wp->layout_cell = NULL;
624 wp->xoff = 0;
625 wp->yoff = 0;
627 wp->sx = sx;
628 wp->sy = sy;
630 wp->pipe_fd = -1;
631 wp->pipe_off = 0;
632 wp->pipe_event = NULL;
634 wp->saved_grid = NULL;
636 screen_init(&wp->base, sx, sy, hlimit);
637 wp->screen = &wp->base;
639 input_init(wp);
641 return (wp);
644 void
645 window_pane_destroy(struct window_pane *wp)
647 window_pane_reset_mode(wp);
649 if (wp->fd != -1) {
650 bufferevent_free(wp->event);
651 close(wp->fd);
654 input_free(wp);
656 screen_free(&wp->base);
657 if (wp->saved_grid != NULL)
658 grid_destroy(wp->saved_grid);
660 if (wp->pipe_fd != -1) {
661 bufferevent_free(wp->pipe_event);
662 close(wp->pipe_fd);
665 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
667 if (wp->cwd != NULL)
668 xfree(wp->cwd);
669 if (wp->shell != NULL)
670 xfree(wp->shell);
671 if (wp->cmd != NULL)
672 xfree(wp->cmd);
673 xfree(wp);
677 window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
678 const char *cwd, struct environ *env, struct termios *tio, char **cause)
680 struct winsize ws;
681 char *argv0, paneid[16];
682 const char *ptr;
683 struct termios tio2;
685 if (wp->fd != -1) {
686 bufferevent_free(wp->event);
687 close(wp->fd);
689 if (cmd != NULL) {
690 if (wp->cmd != NULL)
691 xfree(wp->cmd);
692 wp->cmd = xstrdup(cmd);
694 if (shell != NULL) {
695 if (wp->shell != NULL)
696 xfree(wp->shell);
697 wp->shell = xstrdup(shell);
699 if (cwd != NULL) {
700 if (wp->cwd != NULL)
701 xfree(wp->cwd);
702 wp->cwd = xstrdup(cwd);
705 memset(&ws, 0, sizeof ws);
706 ws.ws_col = screen_size_x(&wp->base);
707 ws.ws_row = screen_size_y(&wp->base);
709 switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
710 case -1:
711 wp->fd = -1;
712 xasprintf(cause, "%s: %s", cmd, strerror(errno));
713 return (-1);
714 case 0:
715 if (chdir(wp->cwd) != 0)
716 chdir("/");
718 if (tcgetattr(STDIN_FILENO, &tio2) != 0)
719 fatal("tcgetattr failed");
720 if (tio != NULL)
721 memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
722 tio2.c_cc[VERASE] = '\177';
723 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
724 fatal("tcgetattr failed");
726 closefrom(STDERR_FILENO + 1);
728 xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
729 environ_set(env, "TMUX_PANE", paneid);
730 environ_push(env);
732 clear_signals(1);
733 log_close();
735 if (*wp->cmd != '\0') {
736 /* Set SHELL but only if it is currently not useful. */
737 shell = getenv("SHELL");
738 if (checkshell(shell))
739 setenv("SHELL", wp->shell, 1);
741 execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
742 fatal("execl failed");
745 /* No command; fork a login shell. */
746 ptr = strrchr(wp->shell, '/');
747 if (ptr != NULL && *(ptr + 1) != '\0')
748 xasprintf(&argv0, "-%s", ptr + 1);
749 else
750 xasprintf(&argv0, "-%s", wp->shell);
751 setenv("SHELL", wp->shell, 1);
752 execl(wp->shell, argv0, (char *) NULL);
753 fatal("execl failed");
756 setblocking(wp->fd, 0);
758 wp->event = bufferevent_new(wp->fd,
759 window_pane_read_callback, NULL, window_pane_error_callback, wp);
760 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
762 return (0);
765 /* ARGSUSED */
766 void
767 window_pane_read_callback(unused struct bufferevent *bufev, void *data)
769 struct window_pane *wp = data;
770 char *new_data;
771 size_t new_size;
773 new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
774 if (wp->pipe_fd != -1 && new_size > 0) {
775 new_data = EVBUFFER_DATA(wp->event->input);
776 bufferevent_write(wp->pipe_event, new_data, new_size);
779 input_parse(wp);
781 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
784 * If we get here, we're not outputting anymore, so set the silence
785 * flag on the window.
787 wp->window->flags |= WINDOW_SILENCE;
788 if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
789 fatal("gettimeofday failed.");
792 /* ARGSUSED */
793 void
794 window_pane_error_callback(
795 unused struct bufferevent *bufev, unused short what, void *data)
797 struct window_pane *wp = data;
799 server_destroy_pane(wp);
802 void
803 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
805 struct winsize ws;
807 if (sx == wp->sx && sy == wp->sy)
808 return;
809 wp->sx = sx;
810 wp->sy = sy;
812 memset(&ws, 0, sizeof ws);
813 ws.ws_col = sx;
814 ws.ws_row = sy;
816 screen_resize(&wp->base, sx, sy);
817 if (wp->mode != NULL)
818 wp->mode->resize(wp, sx, sy);
820 if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
821 fatal("ioctl failed");
825 * Enter alternative screen mode. A copy of the visible screen is saved and the
826 * history is not updated
828 void
829 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
831 struct screen *s = &wp->base;
832 u_int sx, sy;
834 if (wp->saved_grid != NULL)
835 return;
836 if (!options_get_number(&wp->window->options, "alternate-screen"))
837 return;
838 sx = screen_size_x(s);
839 sy = screen_size_y(s);
841 wp->saved_grid = grid_create(sx, sy, 0);
842 grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
843 wp->saved_cx = s->cx;
844 wp->saved_cy = s->cy;
845 memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
847 grid_view_clear(s->grid, 0, 0, sx, sy);
849 wp->base.grid->flags &= ~GRID_HISTORY;
851 wp->flags |= PANE_REDRAW;
854 /* Exit alternate screen mode and restore the copied grid. */
855 void
856 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
858 struct screen *s = &wp->base;
859 u_int sx, sy;
861 if (wp->saved_grid == NULL)
862 return;
863 if (!options_get_number(&wp->window->options, "alternate-screen"))
864 return;
865 sx = screen_size_x(s);
866 sy = screen_size_y(s);
869 * If the current size is bigger, temporarily resize to the old size
870 * before copying back.
872 if (sy > wp->saved_grid->sy)
873 screen_resize(s, sx, wp->saved_grid->sy);
875 /* Restore the grid, cursor position and cell. */
876 grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
877 s->cx = wp->saved_cx;
878 if (s->cx > screen_size_x(s) - 1)
879 s->cx = screen_size_x(s) - 1;
880 s->cy = wp->saved_cy;
881 if (s->cy > screen_size_y(s) - 1)
882 s->cy = screen_size_y(s) - 1;
883 memcpy(gc, &wp->saved_cell, sizeof *gc);
886 * Turn history back on (so resize can use it) and then resize back to
887 * the current size.
889 wp->base.grid->flags |= GRID_HISTORY;
890 if (sy > wp->saved_grid->sy)
891 screen_resize(s, sx, sy);
893 grid_destroy(wp->saved_grid);
894 wp->saved_grid = NULL;
896 wp->flags |= PANE_REDRAW;
900 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
902 struct screen *s;
904 if (wp->mode != NULL)
905 return (1);
906 wp->mode = mode;
908 if ((s = wp->mode->init(wp)) != NULL)
909 wp->screen = s;
910 wp->flags |= PANE_REDRAW;
911 return (0);
914 void
915 window_pane_reset_mode(struct window_pane *wp)
917 if (wp->mode == NULL)
918 return;
920 wp->mode->free(wp);
921 wp->mode = NULL;
923 wp->screen = &wp->base;
924 wp->flags |= PANE_REDRAW;
927 void
928 window_pane_key(struct window_pane *wp, struct session *sess, int key)
930 struct window_pane *wp2;
932 if (!window_pane_visible(wp))
933 return;
935 if (wp->mode != NULL) {
936 if (wp->mode->key != NULL)
937 wp->mode->key(wp, sess, key);
938 return;
941 if (wp->fd == -1)
942 return;
943 input_key(wp, key);
944 if (options_get_number(&wp->window->options, "synchronize-panes")) {
945 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
946 if (wp2 == wp || wp2->mode != NULL)
947 continue;
948 if (wp2->fd != -1 && window_pane_visible(wp2))
949 input_key(wp2, key);
954 void
955 window_pane_mouse(
956 struct window_pane *wp, struct session *sess, struct mouse_event *m)
958 if (!window_pane_visible(wp))
959 return;
961 if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
962 return;
963 if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
964 return;
965 m->x -= wp->xoff;
966 m->y -= wp->yoff;
968 if (wp->mode != NULL) {
969 if (wp->mode->mouse != NULL &&
970 options_get_number(&wp->window->options, "mode-mouse"))
971 wp->mode->mouse(wp, sess, m);
972 } else if (wp->fd != -1)
973 input_mouse(wp, m);
977 window_pane_visible(struct window_pane *wp)
979 struct window *w = wp->window;
981 if (wp->xoff >= w->sx || wp->yoff >= w->sy)
982 return (0);
983 if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
984 return (0);
985 return (1);
988 char *
989 window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
991 struct screen *s = &wp->base;
992 char *newsearchstr, *line, *msg;
993 u_int i;
995 msg = NULL;
996 xasprintf(&newsearchstr, "*%s*", searchstr);
998 for (i = 0; i < screen_size_y(s); i++) {
999 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1000 if (fnmatch(newsearchstr, line, 0) == 0) {
1001 msg = line;
1002 if (lineno != NULL)
1003 *lineno = i;
1004 break;
1006 xfree(line);
1009 xfree(newsearchstr);
1010 return (msg);
1013 /* Find the pane directly above another. */
1014 struct window_pane *
1015 window_pane_find_up(struct window_pane *wp)
1017 struct window_pane *wp2;
1018 u_int left, top;
1020 if (wp == NULL || !window_pane_visible(wp))
1021 return (NULL);
1023 top = wp->yoff;
1024 if (top == 0)
1025 top = wp->window->sy + 1;
1026 left = wp->xoff;
1028 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1029 if (!window_pane_visible(wp2))
1030 continue;
1031 if (wp2->yoff + wp2->sy + 1 != top)
1032 continue;
1033 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1034 return (wp2);
1036 return (NULL);
1039 /* Find the pane directly below another. */
1040 struct window_pane *
1041 window_pane_find_down(struct window_pane *wp)
1043 struct window_pane *wp2;
1044 u_int left, bottom;
1046 if (wp == NULL || !window_pane_visible(wp))
1047 return (NULL);
1049 bottom = wp->yoff + wp->sy + 1;
1050 if (bottom >= wp->window->sy)
1051 bottom = 0;
1052 left = wp->xoff;
1054 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1055 if (!window_pane_visible(wp2))
1056 continue;
1057 if (wp2->yoff != bottom)
1058 continue;
1059 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1060 return (wp2);
1062 return (NULL);
1066 * Find the pane directly to the left of another, adjacent to the left side and
1067 * containing the top edge.
1069 struct window_pane *
1070 window_pane_find_left(struct window_pane *wp)
1072 struct window_pane *wp2;
1073 u_int left, top;
1075 if (wp == NULL || !window_pane_visible(wp))
1076 return (NULL);
1078 left = wp->xoff;
1079 if (left == 0)
1080 left = wp->window->sx + 1;
1081 top = wp->yoff;
1083 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1084 if (!window_pane_visible(wp2))
1085 continue;
1086 if (wp2->xoff + wp2->sx + 1 != left)
1087 continue;
1088 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1089 return (wp2);
1091 return (NULL);
1095 * Find the pane directly to the right of another, that is adjacent to the
1096 * right edge and including the top edge.
1098 struct window_pane *
1099 window_pane_find_right(struct window_pane *wp)
1101 struct window_pane *wp2;
1102 u_int right, top;
1104 if (wp == NULL || !window_pane_visible(wp))
1105 return (NULL);
1107 right = wp->xoff + wp->sx + 1;
1108 if (right >= wp->window->sx)
1109 right = 0;
1110 top = wp->yoff;
1112 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1113 if (!window_pane_visible(wp2))
1114 continue;
1115 if (wp2->xoff != right)
1116 continue;
1117 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1118 return (wp2);
1120 return (NULL);