Revert the command-prefix change which breaks sequences of commands.
[tmux-openbsd.git] / window.c
blob0f445edc6e1865cc68ed10e571f1a141716f090d
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_timer_callback(int, short, void *);
65 void window_pane_read_callback(struct bufferevent *, void *);
66 void window_pane_error_callback(struct bufferevent *, short, void *);
68 RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
70 int
71 winlink_cmp(struct winlink *wl1, struct winlink *wl2)
73 return (wl1->idx - wl2->idx);
76 RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
78 int
79 window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
81 return (wp1->id - wp2->id);
84 struct winlink *
85 winlink_find_by_window(struct winlinks *wwl, struct window *w)
87 struct winlink *wl;
89 RB_FOREACH(wl, winlinks, wwl) {
90 if (wl->window == w)
91 return (wl);
94 return (NULL);
97 struct winlink *
98 winlink_find_by_index(struct winlinks *wwl, int idx)
100 struct winlink wl;
102 if (idx < 0)
103 fatalx("bad index");
105 wl.idx = idx;
106 return (RB_FIND(winlinks, wwl, &wl));
109 struct winlink *
110 winlink_find_by_window_id(struct winlinks *wwl, u_int id)
112 struct winlink *wl;
114 RB_FOREACH(wl, winlinks, wwl) {
115 if (wl->window->id == id)
116 return (wl);
118 return (NULL);
122 winlink_next_index(struct winlinks *wwl, int idx)
124 int i;
126 i = idx;
127 do {
128 if (winlink_find_by_index(wwl, i) == NULL)
129 return (i);
130 if (i == INT_MAX)
131 i = 0;
132 else
133 i++;
134 } while (i != idx);
135 return (-1);
138 u_int
139 winlink_count(struct winlinks *wwl)
141 struct winlink *wl;
142 u_int n;
144 n = 0;
145 RB_FOREACH(wl, winlinks, wwl)
146 n++;
148 return (n);
151 struct winlink *
152 winlink_add(struct winlinks *wwl, int idx)
154 struct winlink *wl;
156 if (idx < 0) {
157 if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
158 return (NULL);
159 } else if (winlink_find_by_index(wwl, idx) != NULL)
160 return (NULL);
162 wl = xcalloc(1, sizeof *wl);
163 wl->idx = idx;
164 RB_INSERT(winlinks, wwl, wl);
166 return (wl);
169 void
170 winlink_set_window(struct winlink *wl, struct window *w)
172 wl->window = w;
173 w->references++;
176 void
177 winlink_remove(struct winlinks *wwl, struct winlink *wl)
179 struct window *w = wl->window;
181 RB_REMOVE(winlinks, wwl, wl);
182 free(wl->status_text);
183 free(wl);
185 if (w != NULL)
186 window_remove_ref(w);
189 struct winlink *
190 winlink_next(struct winlink *wl)
192 return (RB_NEXT(winlinks, wwl, wl));
195 struct winlink *
196 winlink_previous(struct winlink *wl)
198 return (RB_PREV(winlinks, wwl, wl));
201 struct winlink *
202 winlink_next_by_number(struct winlink *wl, struct session *s, int n)
204 for (; n > 0; n--) {
205 if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
206 wl = RB_MIN(winlinks, &s->windows);
209 return (wl);
212 struct winlink *
213 winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
215 for (; n > 0; n--) {
216 if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
217 wl = RB_MAX(winlinks, &s->windows);
220 return (wl);
223 void
224 winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
226 if (wl == NULL)
227 return;
229 winlink_stack_remove(stack, wl);
230 TAILQ_INSERT_HEAD(stack, wl, sentry);
233 void
234 winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
236 struct winlink *wl2;
238 if (wl == NULL)
239 return;
241 TAILQ_FOREACH(wl2, stack, sentry) {
242 if (wl2 == wl) {
243 TAILQ_REMOVE(stack, wl, sentry);
244 return;
250 window_index(struct window *s, u_int *i)
252 for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
253 if (s == ARRAY_ITEM(&windows, *i))
254 return (0);
256 return (-1);
259 struct window *
260 window_find_by_id(u_int id)
262 struct window *w;
263 u_int i;
265 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
266 w = ARRAY_ITEM(&windows, i);
267 if (w->id == id)
268 return (w);
270 return (NULL);
273 struct window *
274 window_create1(u_int sx, u_int sy)
276 struct window *w;
277 u_int i;
279 w = xcalloc(1, sizeof *w);
280 w->id = next_window_id++;
281 w->name = NULL;
282 w->flags = 0;
284 TAILQ_INIT(&w->panes);
285 w->active = NULL;
287 w->lastlayout = -1;
288 w->layout_root = NULL;
290 w->sx = sx;
291 w->sy = sy;
293 options_init(&w->options, &global_w_options);
294 if (options_get_number(&w->options, "automatic-rename"))
295 queue_window_name(w);
297 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
298 if (ARRAY_ITEM(&windows, i) == NULL) {
299 ARRAY_SET(&windows, i, w);
300 break;
303 if (i == ARRAY_LENGTH(&windows))
304 ARRAY_ADD(&windows, w);
305 w->references = 0;
307 return (w);
310 struct window *
311 window_create(const char *name, const char *cmd, const char *shell,
312 const char *cwd, struct environ *env, struct termios *tio,
313 u_int sx, u_int sy, u_int hlimit, char **cause)
315 struct window *w;
316 struct window_pane *wp;
318 w = window_create1(sx, sy);
319 wp = window_add_pane(w, hlimit);
320 layout_init(w, wp);
322 if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
323 window_destroy(w);
324 return (NULL);
327 w->active = TAILQ_FIRST(&w->panes);
328 if (name != NULL) {
329 w->name = xstrdup(name);
330 options_set_number(&w->options, "automatic-rename", 0);
331 } else
332 w->name = default_window_name(w);
334 return (w);
337 void
338 window_destroy(struct window *w)
340 u_int i;
342 window_unzoom(w);
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 if (event_initialized(&w->name_timer))
354 evtimer_del(&w->name_timer);
356 options_free(&w->options);
358 window_destroy_panes(w);
360 free(w->name);
361 free(w);
364 void
365 window_remove_ref(struct window *w)
367 if (w->references == 0)
368 fatal("bad reference count");
369 w->references--;
370 if (w->references == 0)
371 window_destroy(w);
374 void
375 window_set_name(struct window *w, const char *new_name)
377 free(w->name);
378 w->name = xstrdup(new_name);
379 notify_window_renamed(w);
382 void
383 window_resize(struct window *w, u_int sx, u_int sy)
385 w->sx = sx;
386 w->sy = sy;
389 void
390 window_set_active_pane(struct window *w, struct window_pane *wp)
392 if (wp == w->active)
393 return;
394 w->last = w->active;
395 w->active = wp;
396 while (!window_pane_visible(w->active)) {
397 w->active = TAILQ_PREV(w->active, window_panes, entry);
398 if (w->active == NULL)
399 w->active = TAILQ_LAST(&w->panes, window_panes);
400 if (w->active == wp)
401 return;
405 struct window_pane *
406 window_get_active_at(struct window *w, u_int x, u_int y)
408 struct window_pane *wp;
410 TAILQ_FOREACH(wp, &w->panes, entry) {
411 if (!window_pane_visible(wp))
412 continue;
413 if (x < wp->xoff || x > wp->xoff + wp->sx)
414 continue;
415 if (y < wp->yoff || y > wp->yoff + wp->sy)
416 continue;
417 return (wp);
419 return (NULL);
422 void
423 window_set_active_at(struct window *w, u_int x, u_int y)
425 struct window_pane *wp;
427 wp = window_get_active_at(w, x, y);
428 if (wp != NULL && wp != w->active)
429 window_set_active_pane(w, wp);
432 struct window_pane *
433 window_find_string(struct window *w, const char *s)
435 u_int x, y;
437 x = w->sx / 2;
438 y = w->sy / 2;
440 if (strcasecmp(s, "top") == 0)
441 y = 0;
442 else if (strcasecmp(s, "bottom") == 0)
443 y = w->sy - 1;
444 else if (strcasecmp(s, "left") == 0)
445 x = 0;
446 else if (strcasecmp(s, "right") == 0)
447 x = w->sx - 1;
448 else if (strcasecmp(s, "top-left") == 0) {
449 x = 0;
450 y = 0;
451 } else if (strcasecmp(s, "top-right") == 0) {
452 x = w->sx - 1;
453 y = 0;
454 } else if (strcasecmp(s, "bottom-left") == 0) {
455 x = 0;
456 y = w->sy - 1;
457 } else if (strcasecmp(s, "bottom-right") == 0) {
458 x = w->sx - 1;
459 y = w->sy - 1;
460 } else
461 return (NULL);
463 return (window_get_active_at(w, x, y));
467 window_zoom(struct window_pane *wp)
469 struct window *w = wp->window;
470 struct window_pane *wp1;
472 if (w->flags & WINDOW_ZOOMED)
473 return (-1);
475 if (!window_pane_visible(wp))
476 return (-1);
478 if (window_count_panes(w) == 1)
479 return (-1);
481 if (w->active != wp)
482 window_set_active_pane(w, wp);
484 TAILQ_FOREACH(wp1, &w->panes, entry) {
485 wp1->saved_layout_cell = wp1->layout_cell;
486 wp1->layout_cell = NULL;
489 w->saved_layout_root = w->layout_root;
490 layout_init(w, wp);
491 w->flags |= WINDOW_ZOOMED;
493 return (0);
497 window_unzoom(struct window *w)
499 struct window_pane *wp, *wp1;
501 if (!(w->flags & WINDOW_ZOOMED))
502 return (-1);
503 wp = w->active;
505 w->flags &= ~WINDOW_ZOOMED;
506 layout_free(w);
507 w->layout_root = w->saved_layout_root;
509 TAILQ_FOREACH(wp1, &w->panes, entry) {
510 wp1->layout_cell = wp1->saved_layout_cell;
511 wp1->saved_layout_cell = NULL;
513 layout_fix_panes(w, w->sx, w->sy);
515 return (0);
518 struct window_pane *
519 window_add_pane(struct window *w, u_int hlimit)
521 struct window_pane *wp;
523 wp = window_pane_create(w, w->sx, w->sy, hlimit);
524 if (TAILQ_EMPTY(&w->panes))
525 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
526 else
527 TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
528 return (wp);
531 void
532 window_remove_pane(struct window *w, struct window_pane *wp)
534 if (wp == w->active) {
535 w->active = w->last;
536 w->last = NULL;
537 if (w->active == NULL) {
538 w->active = TAILQ_PREV(wp, window_panes, entry);
539 if (w->active == NULL)
540 w->active = TAILQ_NEXT(wp, entry);
542 } else if (wp == w->last)
543 w->last = NULL;
545 TAILQ_REMOVE(&w->panes, wp, entry);
546 window_pane_destroy(wp);
549 struct window_pane *
550 window_pane_at_index(struct window *w, u_int idx)
552 struct window_pane *wp;
553 u_int n;
555 n = options_get_number(&w->options, "pane-base-index");
556 TAILQ_FOREACH(wp, &w->panes, entry) {
557 if (n == idx)
558 return (wp);
559 n++;
561 return (NULL);
564 struct window_pane *
565 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
567 for (; n > 0; n--) {
568 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
569 wp = TAILQ_FIRST(&w->panes);
572 return (wp);
575 struct window_pane *
576 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
577 u_int n)
579 for (; n > 0; n--) {
580 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
581 wp = TAILQ_LAST(&w->panes, window_panes);
584 return (wp);
588 window_pane_index(struct window_pane *wp, u_int *i)
590 struct window_pane *wq;
591 struct window *w = wp->window;
593 *i = options_get_number(&w->options, "pane-base-index");
594 TAILQ_FOREACH(wq, &w->panes, entry) {
595 if (wp == wq) {
596 return (0);
598 (*i)++;
601 return (-1);
604 u_int
605 window_count_panes(struct window *w)
607 struct window_pane *wp;
608 u_int n;
610 n = 0;
611 TAILQ_FOREACH(wp, &w->panes, entry)
612 n++;
613 return (n);
616 void
617 window_destroy_panes(struct window *w)
619 struct window_pane *wp;
621 while (!TAILQ_EMPTY(&w->panes)) {
622 wp = TAILQ_FIRST(&w->panes);
623 TAILQ_REMOVE(&w->panes, wp, entry);
624 window_pane_destroy(wp);
628 /* Return list of printable window flag symbols. No flags is just a space. */
629 char *
630 window_printable_flags(struct session *s, struct winlink *wl)
632 char flags[BUFSIZ];
633 int pos;
635 pos = 0;
636 if (wl->flags & WINLINK_ACTIVITY)
637 flags[pos++] = '#';
638 if (wl->flags & WINLINK_BELL)
639 flags[pos++] = '!';
640 if (wl->flags & WINLINK_CONTENT)
641 flags[pos++] = '+';
642 if (wl->flags & WINLINK_SILENCE)
643 flags[pos++] = '~';
644 if (wl == s->curw)
645 flags[pos++] = '*';
646 if (wl == TAILQ_FIRST(&s->lastw))
647 flags[pos++] = '-';
648 if (wl->window->flags & WINDOW_ZOOMED)
649 flags[pos++] = 'Z';
650 if (pos == 0)
651 flags[pos++] = ' ';
652 flags[pos] = '\0';
653 return (xstrdup(flags));
656 /* Find pane in global tree by id. */
657 struct window_pane *
658 window_pane_find_by_id(u_int id)
660 struct window_pane wp;
662 wp.id = id;
663 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
666 struct window_pane *
667 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
669 struct window_pane *wp;
671 wp = xcalloc(1, sizeof *wp);
672 wp->window = w;
674 wp->id = next_window_pane_id++;
675 RB_INSERT(window_pane_tree, &all_window_panes, wp);
677 wp->cmd = NULL;
678 wp->shell = NULL;
679 wp->cwd = NULL;
681 wp->fd = -1;
682 wp->event = NULL;
684 wp->mode = NULL;
686 wp->layout_cell = NULL;
688 wp->xoff = 0;
689 wp->yoff = 0;
691 wp->sx = sx;
692 wp->sy = sy;
694 wp->pipe_fd = -1;
695 wp->pipe_off = 0;
696 wp->pipe_event = NULL;
698 wp->saved_grid = NULL;
700 screen_init(&wp->base, sx, sy, hlimit);
701 wp->screen = &wp->base;
703 input_init(wp);
705 return (wp);
708 void
709 window_pane_destroy(struct window_pane *wp)
711 window_pane_reset_mode(wp);
713 if (event_initialized(&wp->changes_timer))
714 evtimer_del(&wp->changes_timer);
716 if (wp->fd != -1) {
717 bufferevent_free(wp->event);
718 close(wp->fd);
721 input_free(wp);
723 screen_free(&wp->base);
724 if (wp->saved_grid != NULL)
725 grid_destroy(wp->saved_grid);
727 if (wp->pipe_fd != -1) {
728 bufferevent_free(wp->pipe_event);
729 close(wp->pipe_fd);
732 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
734 free(wp->cwd);
735 free(wp->shell);
736 free(wp->cmd);
737 free(wp);
741 window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
742 const char *cwd, struct environ *env, struct termios *tio, char **cause)
744 struct winsize ws;
745 char *argv0, paneid[16];
746 const char *ptr;
747 struct termios tio2;
749 if (wp->fd != -1) {
750 bufferevent_free(wp->event);
751 close(wp->fd);
753 if (cmd != NULL) {
754 free(wp->cmd);
755 wp->cmd = xstrdup(cmd);
757 if (shell != NULL) {
758 free(wp->shell);
759 wp->shell = xstrdup(shell);
761 if (cwd != NULL) {
762 free(wp->cwd);
763 wp->cwd = xstrdup(cwd);
766 log_debug("spawn: %s -- %s", wp->shell, wp->cmd);
768 memset(&ws, 0, sizeof ws);
769 ws.ws_col = screen_size_x(&wp->base);
770 ws.ws_row = screen_size_y(&wp->base);
772 switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
773 case -1:
774 wp->fd = -1;
775 xasprintf(cause, "%s: %s", cmd, strerror(errno));
776 return (-1);
777 case 0:
778 if (chdir(wp->cwd) != 0)
779 chdir("/");
781 if (tcgetattr(STDIN_FILENO, &tio2) != 0)
782 fatal("tcgetattr failed");
783 if (tio != NULL)
784 memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
785 tio2.c_cc[VERASE] = '\177';
786 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
787 fatal("tcgetattr failed");
789 closefrom(STDERR_FILENO + 1);
791 xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
792 environ_set(env, "TMUX_PANE", paneid);
793 environ_push(env);
795 clear_signals(1);
796 log_close();
798 setenv("SHELL", wp->shell, 1);
799 ptr = strrchr(wp->shell, '/');
801 if (*wp->cmd != '\0') {
802 /* Use the command. */
803 if (ptr != NULL && *(ptr + 1) != '\0')
804 xasprintf(&argv0, "%s", ptr + 1);
805 else
806 xasprintf(&argv0, "%s", wp->shell);
807 execl(wp->shell, argv0, "-c", wp->cmd, (char *) NULL);
808 fatal("execl failed");
811 /* No command; fork a login shell. */
812 if (ptr != NULL && *(ptr + 1) != '\0')
813 xasprintf(&argv0, "-%s", ptr + 1);
814 else
815 xasprintf(&argv0, "-%s", wp->shell);
816 execl(wp->shell, argv0, (char *) NULL);
817 fatal("execl failed");
820 setblocking(wp->fd, 0);
822 wp->event = bufferevent_new(wp->fd,
823 window_pane_read_callback, NULL, window_pane_error_callback, wp);
824 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
826 return (0);
829 void
830 window_pane_timer_start(struct window_pane *wp)
832 struct timeval tv;
834 tv.tv_sec = 0;
835 tv.tv_usec = 1000;
837 evtimer_del(&wp->changes_timer);
838 evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp);
839 evtimer_add(&wp->changes_timer, &tv);
842 void
843 window_pane_timer_callback(unused int fd, unused short events, void *data)
845 struct window_pane *wp = data;
846 struct window *w = wp->window;
847 u_int interval, trigger;
849 interval = options_get_number(&w->options, "c0-change-interval");
850 trigger = options_get_number(&w->options, "c0-change-trigger");
852 if (wp->changes_redraw++ == interval) {
853 wp->flags |= PANE_REDRAW;
854 wp->changes_redraw = 0;
858 if (trigger == 0 || wp->changes < trigger) {
859 wp->flags |= PANE_REDRAW;
860 wp->flags &= ~PANE_DROP;
861 } else
862 window_pane_timer_start(wp);
863 wp->changes = 0;
866 void
867 window_pane_read_callback(unused struct bufferevent *bufev, void *data)
869 struct window_pane *wp = data;
870 char *new_data;
871 size_t new_size;
873 new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
874 if (wp->pipe_fd != -1 && new_size > 0) {
875 new_data = EVBUFFER_DATA(wp->event->input);
876 bufferevent_write(wp->pipe_event, new_data, new_size);
879 input_parse(wp);
881 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
884 * If we get here, we're not outputting anymore, so set the silence
885 * flag on the window.
887 wp->window->flags |= WINDOW_SILENCE;
888 if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
889 fatal("gettimeofday failed.");
892 void
893 window_pane_error_callback(
894 unused struct bufferevent *bufev, unused short what, void *data)
896 struct window_pane *wp = data;
898 server_destroy_pane(wp);
901 void
902 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
904 if (sx == wp->sx && sy == wp->sy)
905 return;
906 wp->sx = sx;
907 wp->sy = sy;
909 screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
910 if (wp->mode != NULL)
911 wp->mode->resize(wp, sx, sy);
913 wp->flags |= PANE_RESIZE;
917 * Enter alternative screen mode. A copy of the visible screen is saved and the
918 * history is not updated
920 void
921 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
922 int cursor)
924 struct screen *s = &wp->base;
925 u_int sx, sy;
927 if (wp->saved_grid != NULL)
928 return;
929 if (!options_get_number(&wp->window->options, "alternate-screen"))
930 return;
931 sx = screen_size_x(s);
932 sy = screen_size_y(s);
934 wp->saved_grid = grid_create(sx, sy, 0);
935 grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
936 if (cursor) {
937 wp->saved_cx = s->cx;
938 wp->saved_cy = s->cy;
940 memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
942 grid_view_clear(s->grid, 0, 0, sx, sy);
944 wp->base.grid->flags &= ~GRID_HISTORY;
946 wp->flags |= PANE_REDRAW;
949 /* Exit alternate screen mode and restore the copied grid. */
950 void
951 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
952 int cursor)
954 struct screen *s = &wp->base;
955 u_int sx, sy;
957 if (wp->saved_grid == NULL)
958 return;
959 if (!options_get_number(&wp->window->options, "alternate-screen"))
960 return;
961 sx = screen_size_x(s);
962 sy = screen_size_y(s);
965 * If the current size is bigger, temporarily resize to the old size
966 * before copying back.
968 if (sy > wp->saved_grid->sy)
969 screen_resize(s, sx, wp->saved_grid->sy, 1);
971 /* Restore the grid, cursor position and cell. */
972 grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
973 if (cursor)
974 s->cx = wp->saved_cx;
975 if (s->cx > screen_size_x(s) - 1)
976 s->cx = screen_size_x(s) - 1;
977 if (cursor)
978 s->cy = wp->saved_cy;
979 if (s->cy > screen_size_y(s) - 1)
980 s->cy = screen_size_y(s) - 1;
981 memcpy(gc, &wp->saved_cell, sizeof *gc);
984 * Turn history back on (so resize can use it) and then resize back to
985 * the current size.
987 wp->base.grid->flags |= GRID_HISTORY;
988 if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
989 screen_resize(s, sx, sy, 1);
991 grid_destroy(wp->saved_grid);
992 wp->saved_grid = NULL;
994 wp->flags |= PANE_REDRAW;
998 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1000 struct screen *s;
1002 if (wp->mode != NULL)
1003 return (1);
1004 wp->mode = mode;
1006 if ((s = wp->mode->init(wp)) != NULL)
1007 wp->screen = s;
1008 wp->flags |= PANE_REDRAW;
1009 return (0);
1012 void
1013 window_pane_reset_mode(struct window_pane *wp)
1015 if (wp->mode == NULL)
1016 return;
1018 wp->mode->free(wp);
1019 wp->mode = NULL;
1021 wp->screen = &wp->base;
1022 wp->flags |= PANE_REDRAW;
1025 void
1026 window_pane_key(struct window_pane *wp, struct session *sess, int key)
1028 struct window_pane *wp2;
1030 if (!window_pane_visible(wp))
1031 return;
1033 if (wp->mode != NULL) {
1034 if (wp->mode->key != NULL)
1035 wp->mode->key(wp, sess, key);
1036 return;
1039 if (wp->fd == -1)
1040 return;
1041 input_key(wp, key);
1042 if (options_get_number(&wp->window->options, "synchronize-panes")) {
1043 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1044 if (wp2 == wp || wp2->mode != NULL)
1045 continue;
1046 if (wp2->fd != -1 && window_pane_visible(wp2))
1047 input_key(wp2, key);
1052 void
1053 window_pane_mouse(
1054 struct window_pane *wp, struct session *sess, struct mouse_event *m)
1056 if (!window_pane_visible(wp))
1057 return;
1059 if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1060 return;
1061 if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1062 return;
1063 m->x -= wp->xoff;
1064 m->y -= wp->yoff;
1066 if (wp->mode != NULL) {
1067 if (wp->mode->mouse != NULL &&
1068 options_get_number(&wp->window->options, "mode-mouse"))
1069 wp->mode->mouse(wp, sess, m);
1070 } else if (wp->fd != -1)
1071 input_mouse(wp, sess, m);
1075 window_pane_visible(struct window_pane *wp)
1077 struct window *w = wp->window;
1079 if (wp->layout_cell == NULL)
1080 return (0);
1081 if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1082 return (0);
1083 if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1084 return (0);
1085 return (1);
1088 char *
1089 window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1091 struct screen *s = &wp->base;
1092 char *newsearchstr, *line, *msg;
1093 u_int i;
1095 msg = NULL;
1096 xasprintf(&newsearchstr, "*%s*", searchstr);
1098 for (i = 0; i < screen_size_y(s); i++) {
1099 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1100 if (fnmatch(newsearchstr, line, 0) == 0) {
1101 msg = line;
1102 if (lineno != NULL)
1103 *lineno = i;
1104 break;
1106 free(line);
1109 free(newsearchstr);
1110 return (msg);
1113 /* Find the pane directly above another. */
1114 struct window_pane *
1115 window_pane_find_up(struct window_pane *wp)
1117 struct window_pane *wp2;
1118 u_int left, top;
1120 if (wp == NULL || !window_pane_visible(wp))
1121 return (NULL);
1123 top = wp->yoff;
1124 if (top == 0)
1125 top = wp->window->sy + 1;
1126 left = wp->xoff;
1128 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1129 if (!window_pane_visible(wp2))
1130 continue;
1131 if (wp2->yoff + wp2->sy + 1 != top)
1132 continue;
1133 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1134 return (wp2);
1136 return (NULL);
1139 /* Find the pane directly below another. */
1140 struct window_pane *
1141 window_pane_find_down(struct window_pane *wp)
1143 struct window_pane *wp2;
1144 u_int left, bottom;
1146 if (wp == NULL || !window_pane_visible(wp))
1147 return (NULL);
1149 bottom = wp->yoff + wp->sy + 1;
1150 if (bottom >= wp->window->sy)
1151 bottom = 0;
1152 left = wp->xoff;
1154 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1155 if (!window_pane_visible(wp2))
1156 continue;
1157 if (wp2->yoff != bottom)
1158 continue;
1159 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1160 return (wp2);
1162 return (NULL);
1166 * Find the pane directly to the left of another, adjacent to the left side and
1167 * containing the top edge.
1169 struct window_pane *
1170 window_pane_find_left(struct window_pane *wp)
1172 struct window_pane *wp2;
1173 u_int left, top;
1175 if (wp == NULL || !window_pane_visible(wp))
1176 return (NULL);
1178 left = wp->xoff;
1179 if (left == 0)
1180 left = wp->window->sx + 1;
1181 top = wp->yoff;
1183 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1184 if (!window_pane_visible(wp2))
1185 continue;
1186 if (wp2->xoff + wp2->sx + 1 != left)
1187 continue;
1188 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1189 return (wp2);
1191 return (NULL);
1195 * Find the pane directly to the right of another, that is adjacent to the
1196 * right edge and including the top edge.
1198 struct window_pane *
1199 window_pane_find_right(struct window_pane *wp)
1201 struct window_pane *wp2;
1202 u_int right, top;
1204 if (wp == NULL || !window_pane_visible(wp))
1205 return (NULL);
1207 right = wp->xoff + wp->sx + 1;
1208 if (right >= wp->window->sx)
1209 right = 0;
1210 top = wp->yoff;
1212 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1213 if (!window_pane_visible(wp2))
1214 continue;
1215 if (wp2->xoff != right)
1216 continue;
1217 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1218 return (wp2);
1220 return (NULL);
1223 /* Clear alert flags for a winlink */
1224 void
1225 winlink_clear_flags(struct winlink *wl)
1227 struct winlink *wm;
1228 struct session *s;
1229 struct window *w;
1230 u_int i;
1232 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1233 if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1234 continue;
1236 RB_FOREACH(s, sessions, &sessions) {
1237 if ((wm = session_has(s, w)) == NULL)
1238 continue;
1240 if (wm->window != wl->window)
1241 continue;
1242 if ((wm->flags & WINLINK_ALERTFLAGS) == 0)
1243 continue;
1245 wm->flags &= ~WINLINK_ALERTFLAGS;
1246 server_status_session(s);
1251 /* Set the grid_cell with fg/bg/attr information when window is in a mode. */
1252 void
1253 window_mode_attrs(struct grid_cell *gc, struct options *oo)
1255 memcpy(gc, &grid_default_cell, sizeof *gc);
1256 colour_set_fg(gc, options_get_number(oo, "mode-fg"));
1257 colour_set_bg(gc, options_get_number(oo, "mode-bg"));
1258 gc->attr |= options_get_number(oo, "mode-attr");