Rename session idx to session id throughout and add $ prefix to targets
[tmux-openbsd.git] / window.c
blob38af88f9d979465a2aa670ce35e6d0d5cb2df9b3
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;
317 const char *prefix;
318 char *cmd1;
320 w = window_create1(sx, sy);
321 wp = window_add_pane(w, hlimit);
322 layout_init(w, wp);
324 if (*cmd != '\0') {
325 prefix = options_get_string(&w->options, "command-prefix");
326 xasprintf(&cmd1, "%s%s", prefix, cmd);
327 } else
328 cmd1 = xstrdup("");
329 if (window_pane_spawn(wp, cmd1, shell, cwd, env, tio, cause) != 0) {
330 window_destroy(w);
331 free(cmd1);
332 return (NULL);
334 free(cmd1);
336 w->active = TAILQ_FIRST(&w->panes);
337 if (name != NULL) {
338 w->name = xstrdup(name);
339 options_set_number(&w->options, "automatic-rename", 0);
340 } else
341 w->name = default_window_name(w);
343 return (w);
346 void
347 window_destroy(struct window *w)
349 u_int i;
351 window_unzoom(w);
353 if (window_index(w, &i) != 0)
354 fatalx("index not found");
355 ARRAY_SET(&windows, i, NULL);
356 while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
357 ARRAY_TRUNC(&windows, 1);
359 if (w->layout_root != NULL)
360 layout_free(w);
362 if (event_initialized(&w->name_timer))
363 evtimer_del(&w->name_timer);
365 options_free(&w->options);
367 window_destroy_panes(w);
369 free(w->name);
370 free(w);
373 void
374 window_remove_ref(struct window *w)
376 if (w->references == 0)
377 fatal("bad reference count");
378 w->references--;
379 if (w->references == 0)
380 window_destroy(w);
383 void
384 window_set_name(struct window *w, const char *new_name)
386 free(w->name);
387 w->name = xstrdup(new_name);
388 notify_window_renamed(w);
391 void
392 window_resize(struct window *w, u_int sx, u_int sy)
394 w->sx = sx;
395 w->sy = sy;
398 void
399 window_set_active_pane(struct window *w, struct window_pane *wp)
401 if (wp == w->active)
402 return;
403 w->last = w->active;
404 w->active = wp;
405 while (!window_pane_visible(w->active)) {
406 w->active = TAILQ_PREV(w->active, window_panes, entry);
407 if (w->active == NULL)
408 w->active = TAILQ_LAST(&w->panes, window_panes);
409 if (w->active == wp)
410 return;
414 struct window_pane *
415 window_get_active_at(struct window *w, u_int x, u_int y)
417 struct window_pane *wp;
419 TAILQ_FOREACH(wp, &w->panes, entry) {
420 if (!window_pane_visible(wp))
421 continue;
422 if (x < wp->xoff || x > wp->xoff + wp->sx)
423 continue;
424 if (y < wp->yoff || y > wp->yoff + wp->sy)
425 continue;
426 return (wp);
428 return (NULL);
431 void
432 window_set_active_at(struct window *w, u_int x, u_int y)
434 struct window_pane *wp;
436 wp = window_get_active_at(w, x, y);
437 if (wp != NULL && wp != w->active)
438 window_set_active_pane(w, wp);
441 struct window_pane *
442 window_find_string(struct window *w, const char *s)
444 u_int x, y;
446 x = w->sx / 2;
447 y = w->sy / 2;
449 if (strcasecmp(s, "top") == 0)
450 y = 0;
451 else if (strcasecmp(s, "bottom") == 0)
452 y = w->sy - 1;
453 else if (strcasecmp(s, "left") == 0)
454 x = 0;
455 else if (strcasecmp(s, "right") == 0)
456 x = w->sx - 1;
457 else if (strcasecmp(s, "top-left") == 0) {
458 x = 0;
459 y = 0;
460 } else if (strcasecmp(s, "top-right") == 0) {
461 x = w->sx - 1;
462 y = 0;
463 } else if (strcasecmp(s, "bottom-left") == 0) {
464 x = 0;
465 y = w->sy - 1;
466 } else if (strcasecmp(s, "bottom-right") == 0) {
467 x = w->sx - 1;
468 y = w->sy - 1;
469 } else
470 return (NULL);
472 return (window_get_active_at(w, x, y));
476 window_zoom(struct window_pane *wp)
478 struct window *w = wp->window;
479 struct window_pane *wp1;
481 if (w->flags & WINDOW_ZOOMED)
482 return (-1);
484 if (!window_pane_visible(wp))
485 return (-1);
486 if (w->active != wp)
487 window_set_active_pane(w, wp);
489 TAILQ_FOREACH(wp1, &w->panes, entry) {
490 wp1->saved_layout_cell = wp1->layout_cell;
491 wp1->layout_cell = NULL;
494 w->saved_layout_root = w->layout_root;
495 layout_init(w, wp);
496 w->flags |= WINDOW_ZOOMED;
498 return (0);
502 window_unzoom(struct window *w)
504 struct window_pane *wp, *wp1;
506 if (!(w->flags & WINDOW_ZOOMED))
507 return (-1);
508 wp = w->active;
510 w->flags &= ~WINDOW_ZOOMED;
511 layout_free(w);
512 w->layout_root = w->saved_layout_root;
514 TAILQ_FOREACH(wp1, &w->panes, entry) {
515 wp1->layout_cell = wp1->saved_layout_cell;
516 wp1->saved_layout_cell = NULL;
518 layout_fix_panes(w, w->sx, w->sy);
520 return (0);
523 struct window_pane *
524 window_add_pane(struct window *w, u_int hlimit)
526 struct window_pane *wp;
528 wp = window_pane_create(w, w->sx, w->sy, hlimit);
529 if (TAILQ_EMPTY(&w->panes))
530 TAILQ_INSERT_HEAD(&w->panes, wp, entry);
531 else
532 TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
533 return (wp);
536 void
537 window_remove_pane(struct window *w, struct window_pane *wp)
539 if (wp == w->active) {
540 w->active = w->last;
541 w->last = NULL;
542 if (w->active == NULL) {
543 w->active = TAILQ_PREV(wp, window_panes, entry);
544 if (w->active == NULL)
545 w->active = TAILQ_NEXT(wp, entry);
547 } else if (wp == w->last)
548 w->last = NULL;
550 TAILQ_REMOVE(&w->panes, wp, entry);
551 window_pane_destroy(wp);
554 struct window_pane *
555 window_pane_at_index(struct window *w, u_int idx)
557 struct window_pane *wp;
558 u_int n;
560 n = options_get_number(&w->options, "pane-base-index");
561 TAILQ_FOREACH(wp, &w->panes, entry) {
562 if (n == idx)
563 return (wp);
564 n++;
566 return (NULL);
569 struct window_pane *
570 window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
572 for (; n > 0; n--) {
573 if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
574 wp = TAILQ_FIRST(&w->panes);
577 return (wp);
580 struct window_pane *
581 window_pane_previous_by_number(struct window *w, struct window_pane *wp,
582 u_int n)
584 for (; n > 0; n--) {
585 if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
586 wp = TAILQ_LAST(&w->panes, window_panes);
589 return (wp);
593 window_pane_index(struct window_pane *wp, u_int *i)
595 struct window_pane *wq;
596 struct window *w = wp->window;
598 *i = options_get_number(&w->options, "pane-base-index");
599 TAILQ_FOREACH(wq, &w->panes, entry) {
600 if (wp == wq) {
601 return (0);
603 (*i)++;
606 return (-1);
609 u_int
610 window_count_panes(struct window *w)
612 struct window_pane *wp;
613 u_int n;
615 n = 0;
616 TAILQ_FOREACH(wp, &w->panes, entry)
617 n++;
618 return (n);
621 void
622 window_destroy_panes(struct window *w)
624 struct window_pane *wp;
626 while (!TAILQ_EMPTY(&w->panes)) {
627 wp = TAILQ_FIRST(&w->panes);
628 TAILQ_REMOVE(&w->panes, wp, entry);
629 window_pane_destroy(wp);
633 /* Return list of printable window flag symbols. No flags is just a space. */
634 char *
635 window_printable_flags(struct session *s, struct winlink *wl)
637 char flags[BUFSIZ];
638 int pos;
640 pos = 0;
641 if (wl->flags & WINLINK_ACTIVITY)
642 flags[pos++] = '#';
643 if (wl->flags & WINLINK_BELL)
644 flags[pos++] = '!';
645 if (wl->flags & WINLINK_CONTENT)
646 flags[pos++] = '+';
647 if (wl->flags & WINLINK_SILENCE)
648 flags[pos++] = '~';
649 if (wl == s->curw)
650 flags[pos++] = '*';
651 if (wl == TAILQ_FIRST(&s->lastw))
652 flags[pos++] = '-';
653 if (wl->window->flags & WINDOW_ZOOMED)
654 flags[pos++] = 'Z';
655 if (pos == 0)
656 flags[pos++] = ' ';
657 flags[pos] = '\0';
658 return (xstrdup(flags));
661 /* Find pane in global tree by id. */
662 struct window_pane *
663 window_pane_find_by_id(u_int id)
665 struct window_pane wp;
667 wp.id = id;
668 return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
671 struct window_pane *
672 window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
674 struct window_pane *wp;
676 wp = xcalloc(1, sizeof *wp);
677 wp->window = w;
679 wp->id = next_window_pane_id++;
680 RB_INSERT(window_pane_tree, &all_window_panes, wp);
682 wp->cmd = NULL;
683 wp->shell = NULL;
684 wp->cwd = NULL;
686 wp->fd = -1;
687 wp->event = NULL;
689 wp->mode = NULL;
691 wp->layout_cell = NULL;
693 wp->xoff = 0;
694 wp->yoff = 0;
696 wp->sx = sx;
697 wp->sy = sy;
699 wp->pipe_fd = -1;
700 wp->pipe_off = 0;
701 wp->pipe_event = NULL;
703 wp->saved_grid = NULL;
705 screen_init(&wp->base, sx, sy, hlimit);
706 wp->screen = &wp->base;
708 input_init(wp);
710 return (wp);
713 void
714 window_pane_destroy(struct window_pane *wp)
716 window_pane_reset_mode(wp);
718 if (event_initialized(&wp->changes_timer))
719 evtimer_del(&wp->changes_timer);
721 if (wp->fd != -1) {
722 bufferevent_free(wp->event);
723 close(wp->fd);
726 input_free(wp);
728 screen_free(&wp->base);
729 if (wp->saved_grid != NULL)
730 grid_destroy(wp->saved_grid);
732 if (wp->pipe_fd != -1) {
733 bufferevent_free(wp->pipe_event);
734 close(wp->pipe_fd);
737 RB_REMOVE(window_pane_tree, &all_window_panes, wp);
739 free(wp->cwd);
740 free(wp->shell);
741 free(wp->cmd);
742 free(wp);
746 window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
747 const char *cwd, struct environ *env, struct termios *tio, char **cause)
749 struct winsize ws;
750 char *argv0, paneid[16];
751 const char *ptr;
752 struct termios tio2;
754 if (wp->fd != -1) {
755 bufferevent_free(wp->event);
756 close(wp->fd);
758 if (cmd != NULL) {
759 free(wp->cmd);
760 wp->cmd = xstrdup(cmd);
762 if (shell != NULL) {
763 free(wp->shell);
764 wp->shell = xstrdup(shell);
766 if (cwd != NULL) {
767 free(wp->cwd);
768 wp->cwd = xstrdup(cwd);
771 log_debug("spawn: %s -- %s", wp->shell, wp->cmd);
773 memset(&ws, 0, sizeof ws);
774 ws.ws_col = screen_size_x(&wp->base);
775 ws.ws_row = screen_size_y(&wp->base);
777 switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
778 case -1:
779 wp->fd = -1;
780 xasprintf(cause, "%s: %s", cmd, strerror(errno));
781 return (-1);
782 case 0:
783 if (chdir(wp->cwd) != 0)
784 chdir("/");
786 if (tcgetattr(STDIN_FILENO, &tio2) != 0)
787 fatal("tcgetattr failed");
788 if (tio != NULL)
789 memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
790 tio2.c_cc[VERASE] = '\177';
791 if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
792 fatal("tcgetattr failed");
794 closefrom(STDERR_FILENO + 1);
796 xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
797 environ_set(env, "TMUX_PANE", paneid);
798 environ_push(env);
800 clear_signals(1);
801 log_close();
803 setenv("SHELL", wp->shell, 1);
804 ptr = strrchr(wp->shell, '/');
806 if (*wp->cmd != '\0') {
807 /* Use the command. */
808 if (ptr != NULL && *(ptr + 1) != '\0')
809 xasprintf(&argv0, "%s", ptr + 1);
810 else
811 xasprintf(&argv0, "%s", wp->shell);
812 execl(wp->shell, argv0, "-c", wp->cmd, (char *) NULL);
813 fatal("execl failed");
816 /* No command; fork a login shell. */
817 if (ptr != NULL && *(ptr + 1) != '\0')
818 xasprintf(&argv0, "-%s", ptr + 1);
819 else
820 xasprintf(&argv0, "-%s", wp->shell);
821 execl(wp->shell, argv0, (char *) NULL);
822 fatal("execl failed");
825 setblocking(wp->fd, 0);
827 wp->event = bufferevent_new(wp->fd,
828 window_pane_read_callback, NULL, window_pane_error_callback, wp);
829 bufferevent_enable(wp->event, EV_READ|EV_WRITE);
831 return (0);
834 void
835 window_pane_timer_start(struct window_pane *wp)
837 struct timeval tv;
839 tv.tv_sec = 0;
840 tv.tv_usec = 1000;
842 evtimer_del(&wp->changes_timer);
843 evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp);
844 evtimer_add(&wp->changes_timer, &tv);
847 void
848 window_pane_timer_callback(unused int fd, unused short events, void *data)
850 struct window_pane *wp = data;
851 struct window *w = wp->window;
852 u_int interval, trigger;
854 interval = options_get_number(&w->options, "c0-change-interval");
855 trigger = options_get_number(&w->options, "c0-change-trigger");
857 if (wp->changes_redraw++ == interval) {
858 wp->flags |= PANE_REDRAW;
859 wp->changes_redraw = 0;
863 if (trigger == 0 || wp->changes < trigger) {
864 wp->flags |= PANE_REDRAW;
865 wp->flags &= ~PANE_DROP;
866 } else
867 window_pane_timer_start(wp);
868 wp->changes = 0;
871 void
872 window_pane_read_callback(unused struct bufferevent *bufev, void *data)
874 struct window_pane *wp = data;
875 char *new_data;
876 size_t new_size;
878 new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
879 if (wp->pipe_fd != -1 && new_size > 0) {
880 new_data = EVBUFFER_DATA(wp->event->input);
881 bufferevent_write(wp->pipe_event, new_data, new_size);
884 input_parse(wp);
886 wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
889 * If we get here, we're not outputting anymore, so set the silence
890 * flag on the window.
892 wp->window->flags |= WINDOW_SILENCE;
893 if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
894 fatal("gettimeofday failed.");
897 void
898 window_pane_error_callback(
899 unused struct bufferevent *bufev, unused short what, void *data)
901 struct window_pane *wp = data;
903 server_destroy_pane(wp);
906 void
907 window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
909 if (sx == wp->sx && sy == wp->sy)
910 return;
911 wp->sx = sx;
912 wp->sy = sy;
914 screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
915 if (wp->mode != NULL)
916 wp->mode->resize(wp, sx, sy);
920 * Enter alternative screen mode. A copy of the visible screen is saved and the
921 * history is not updated
923 void
924 window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
925 int cursor)
927 struct screen *s = &wp->base;
928 u_int sx, sy;
930 if (wp->saved_grid != NULL)
931 return;
932 if (!options_get_number(&wp->window->options, "alternate-screen"))
933 return;
934 sx = screen_size_x(s);
935 sy = screen_size_y(s);
937 wp->saved_grid = grid_create(sx, sy, 0);
938 grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
939 if (cursor) {
940 wp->saved_cx = s->cx;
941 wp->saved_cy = s->cy;
943 memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
945 grid_view_clear(s->grid, 0, 0, sx, sy);
947 wp->base.grid->flags &= ~GRID_HISTORY;
949 wp->flags |= PANE_REDRAW;
952 /* Exit alternate screen mode and restore the copied grid. */
953 void
954 window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
955 int cursor)
957 struct screen *s = &wp->base;
958 u_int sx, sy;
960 if (wp->saved_grid == NULL)
961 return;
962 if (!options_get_number(&wp->window->options, "alternate-screen"))
963 return;
964 sx = screen_size_x(s);
965 sy = screen_size_y(s);
968 * If the current size is bigger, temporarily resize to the old size
969 * before copying back.
971 if (sy > wp->saved_grid->sy)
972 screen_resize(s, sx, wp->saved_grid->sy, 1);
974 /* Restore the grid, cursor position and cell. */
975 grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
976 if (cursor)
977 s->cx = wp->saved_cx;
978 if (s->cx > screen_size_x(s) - 1)
979 s->cx = screen_size_x(s) - 1;
980 if (cursor)
981 s->cy = wp->saved_cy;
982 if (s->cy > screen_size_y(s) - 1)
983 s->cy = screen_size_y(s) - 1;
984 memcpy(gc, &wp->saved_cell, sizeof *gc);
987 * Turn history back on (so resize can use it) and then resize back to
988 * the current size.
990 wp->base.grid->flags |= GRID_HISTORY;
991 if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
992 screen_resize(s, sx, sy, 1);
994 grid_destroy(wp->saved_grid);
995 wp->saved_grid = NULL;
997 wp->flags |= PANE_REDRAW;
1001 window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
1003 struct screen *s;
1005 if (wp->mode != NULL)
1006 return (1);
1007 wp->mode = mode;
1009 if ((s = wp->mode->init(wp)) != NULL)
1010 wp->screen = s;
1011 wp->flags |= PANE_REDRAW;
1012 return (0);
1015 void
1016 window_pane_reset_mode(struct window_pane *wp)
1018 if (wp->mode == NULL)
1019 return;
1021 wp->mode->free(wp);
1022 wp->mode = NULL;
1024 wp->screen = &wp->base;
1025 wp->flags |= PANE_REDRAW;
1028 void
1029 window_pane_key(struct window_pane *wp, struct session *sess, int key)
1031 struct window_pane *wp2;
1033 if (!window_pane_visible(wp))
1034 return;
1036 if (wp->mode != NULL) {
1037 if (wp->mode->key != NULL)
1038 wp->mode->key(wp, sess, key);
1039 return;
1042 if (wp->fd == -1)
1043 return;
1044 input_key(wp, key);
1045 if (options_get_number(&wp->window->options, "synchronize-panes")) {
1046 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1047 if (wp2 == wp || wp2->mode != NULL)
1048 continue;
1049 if (wp2->fd != -1 && window_pane_visible(wp2))
1050 input_key(wp2, key);
1055 void
1056 window_pane_mouse(
1057 struct window_pane *wp, struct session *sess, struct mouse_event *m)
1059 if (!window_pane_visible(wp))
1060 return;
1062 if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1063 return;
1064 if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1065 return;
1066 m->x -= wp->xoff;
1067 m->y -= wp->yoff;
1069 if (wp->mode != NULL) {
1070 if (wp->mode->mouse != NULL &&
1071 options_get_number(&wp->window->options, "mode-mouse"))
1072 wp->mode->mouse(wp, sess, m);
1073 } else if (wp->fd != -1)
1074 input_mouse(wp, sess, m);
1078 window_pane_visible(struct window_pane *wp)
1080 struct window *w = wp->window;
1082 if (wp->layout_cell == NULL)
1083 return (0);
1084 if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1085 return (0);
1086 if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1087 return (0);
1088 return (1);
1091 char *
1092 window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1094 struct screen *s = &wp->base;
1095 char *newsearchstr, *line, *msg;
1096 u_int i;
1098 msg = NULL;
1099 xasprintf(&newsearchstr, "*%s*", searchstr);
1101 for (i = 0; i < screen_size_y(s); i++) {
1102 line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1103 if (fnmatch(newsearchstr, line, 0) == 0) {
1104 msg = line;
1105 if (lineno != NULL)
1106 *lineno = i;
1107 break;
1109 free(line);
1112 free(newsearchstr);
1113 return (msg);
1116 /* Find the pane directly above another. */
1117 struct window_pane *
1118 window_pane_find_up(struct window_pane *wp)
1120 struct window_pane *wp2;
1121 u_int left, top;
1123 if (wp == NULL || !window_pane_visible(wp))
1124 return (NULL);
1126 top = wp->yoff;
1127 if (top == 0)
1128 top = wp->window->sy + 1;
1129 left = wp->xoff;
1131 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1132 if (!window_pane_visible(wp2))
1133 continue;
1134 if (wp2->yoff + wp2->sy + 1 != top)
1135 continue;
1136 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1137 return (wp2);
1139 return (NULL);
1142 /* Find the pane directly below another. */
1143 struct window_pane *
1144 window_pane_find_down(struct window_pane *wp)
1146 struct window_pane *wp2;
1147 u_int left, bottom;
1149 if (wp == NULL || !window_pane_visible(wp))
1150 return (NULL);
1152 bottom = wp->yoff + wp->sy + 1;
1153 if (bottom >= wp->window->sy)
1154 bottom = 0;
1155 left = wp->xoff;
1157 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1158 if (!window_pane_visible(wp2))
1159 continue;
1160 if (wp2->yoff != bottom)
1161 continue;
1162 if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
1163 return (wp2);
1165 return (NULL);
1169 * Find the pane directly to the left of another, adjacent to the left side and
1170 * containing the top edge.
1172 struct window_pane *
1173 window_pane_find_left(struct window_pane *wp)
1175 struct window_pane *wp2;
1176 u_int left, top;
1178 if (wp == NULL || !window_pane_visible(wp))
1179 return (NULL);
1181 left = wp->xoff;
1182 if (left == 0)
1183 left = wp->window->sx + 1;
1184 top = wp->yoff;
1186 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1187 if (!window_pane_visible(wp2))
1188 continue;
1189 if (wp2->xoff + wp2->sx + 1 != left)
1190 continue;
1191 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1192 return (wp2);
1194 return (NULL);
1198 * Find the pane directly to the right of another, that is adjacent to the
1199 * right edge and including the top edge.
1201 struct window_pane *
1202 window_pane_find_right(struct window_pane *wp)
1204 struct window_pane *wp2;
1205 u_int right, top;
1207 if (wp == NULL || !window_pane_visible(wp))
1208 return (NULL);
1210 right = wp->xoff + wp->sx + 1;
1211 if (right >= wp->window->sx)
1212 right = 0;
1213 top = wp->yoff;
1215 TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1216 if (!window_pane_visible(wp2))
1217 continue;
1218 if (wp2->xoff != right)
1219 continue;
1220 if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
1221 return (wp2);
1223 return (NULL);
1226 /* Clear alert flags for a winlink */
1227 void
1228 winlink_clear_flags(struct winlink *wl)
1230 struct winlink *wm;
1231 struct session *s;
1232 struct window *w;
1233 u_int i;
1235 for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
1236 if ((w = ARRAY_ITEM(&windows, i)) == NULL)
1237 continue;
1239 RB_FOREACH(s, sessions, &sessions) {
1240 if ((wm = session_has(s, w)) == NULL)
1241 continue;
1243 if (wm->window != wl->window)
1244 continue;
1245 if ((wm->flags & WINLINK_ALERTFLAGS) == 0)
1246 continue;
1248 wm->flags &= ~WINLINK_ALERTFLAGS;
1249 server_status_session(s);
1254 /* Set the grid_cell with fg/bg/attr information when window is in a mode. */
1255 void
1256 window_mode_attrs(struct grid_cell *gc, struct options *oo)
1258 memcpy(gc, &grid_default_cell, sizeof *gc);
1259 colour_set_fg(gc, options_get_number(oo, "mode-fg"));
1260 colour_set_bg(gc, options_get_number(oo, "mode-bg"));
1261 gc->attr |= options_get_number(oo, "mode-attr");