[BACK]Return to window.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/window.c, Revision 1.247

1.247   ! nicm        1: /* $OpenBSD: window.c,v 1.246 2019/12/12 11:39:56 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.156     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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>
1.162     nicm       20: #include <sys/ioctl.h>
1.1       nicm       21:
1.236     nicm       22: #include <ctype.h>
1.1       nicm       23: #include <errno.h>
                     24: #include <fcntl.h>
1.5       nicm       25: #include <fnmatch.h>
1.232     nicm       26: #include <regex.h>
1.203     nicm       27: #include <signal.h>
1.1       nicm       28: #include <stdint.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
1.163     nicm       31: #include <time.h>
1.1       nicm       32: #include <unistd.h>
                     33: #include <util.h>
1.198     nicm       34: #include <vis.h>
1.1       nicm       35:
                     36: #include "tmux.h"
                     37:
                     38: /*
1.14      nicm       39:  * Each window is attached to a number of panes, each of which is a pty. This
1.1       nicm       40:  * file contains code to handle them.
                     41:  *
                     42:  * A pane has two buffers attached, these are filled and emptied by the main
                     43:  * server poll loop. Output data is received from pty's in screen format,
                     44:  * translated and returned as a series of escape sequences and strings via
                     45:  * input_parse (in input.c). Input data is received as key codes and written
                     46:  * directly via input_key.
                     47:  *
                     48:  * Each pane also has a "virtual" screen (screen.c) which contains the current
                     49:  * state and is redisplayed when the window is reattached to a client.
                     50:  *
                     51:  * Windows are stored directly on a global array and wrapped in any number of
                     52:  * winlink structs to be linked onto local session RB trees. A reference count
                     53:  * is maintained and a window removed from the global list and destroyed when
                     54:  * it reaches zero.
                     55:  */
1.124     nicm       56:
1.1       nicm       57: /* Global window list. */
                     58: struct windows windows;
                     59:
1.64      nicm       60: /* Global panes tree. */
                     61: struct window_pane_tree all_window_panes;
1.166     nicm       62: static u_int   next_window_pane_id;
                     63: static u_int   next_window_id;
                     64: static u_int   next_active_point;
1.222     nicm       65:
                     66: /* List of window modes. */
                     67: const struct window_mode *all_window_modes[] = {
                     68:        &window_buffer_mode,
                     69:        &window_client_mode,
                     70:        &window_clock_mode,
                     71:        &window_copy_mode,
                     72:        &window_tree_mode,
                     73:        &window_view_mode,
                     74:        NULL
                     75: };
1.37      nicm       76:
1.228     nicm       77: struct window_pane_input_data {
                     78:        struct cmdq_item        *item;
                     79:        u_int                    wp;
                     80: };
                     81:
1.169     nicm       82: static struct window_pane *window_pane_create(struct window *, u_int, u_int,
                     83:                    u_int);
                     84: static void    window_pane_destroy(struct window_pane *);
                     85:
1.122     nicm       86: RB_GENERATE(windows, window, entry, window_cmp);
1.169     nicm       87: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
                     88: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
1.122     nicm       89:
                     90: int
                     91: window_cmp(struct window *w1, struct window *w2)
                     92: {
                     93:        return (w1->id - w2->id);
                     94: }
                     95:
1.1       nicm       96: int
                     97: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
                     98: {
                     99:        return (wl1->idx - wl2->idx);
1.12      nicm      100: }
                    101:
1.64      nicm      102: int
                    103: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
                    104: {
                    105:        return (wp1->id - wp2->id);
                    106: }
                    107:
1.12      nicm      108: struct winlink *
                    109: winlink_find_by_window(struct winlinks *wwl, struct window *w)
                    110: {
                    111:        struct winlink  *wl;
                    112:
                    113:        RB_FOREACH(wl, winlinks, wwl) {
                    114:                if (wl->window == w)
                    115:                        return (wl);
                    116:        }
                    117:
                    118:        return (NULL);
1.1       nicm      119: }
                    120:
                    121: struct winlink *
                    122: winlink_find_by_index(struct winlinks *wwl, int idx)
                    123: {
                    124:        struct winlink  wl;
                    125:
                    126:        if (idx < 0)
                    127:                fatalx("bad index");
                    128:
                    129:        wl.idx = idx;
                    130:        return (RB_FIND(winlinks, wwl, &wl));
                    131: }
                    132:
1.71      nicm      133: struct winlink *
                    134: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
                    135: {
                    136:        struct winlink *wl;
                    137:
                    138:        RB_FOREACH(wl, winlinks, wwl) {
                    139:                if (wl->window->id == id)
                    140:                        return (wl);
                    141:        }
1.78      nicm      142:        return (NULL);
1.71      nicm      143: }
                    144:
1.169     nicm      145: static int
1.22      nicm      146: winlink_next_index(struct winlinks *wwl, int idx)
1.1       nicm      147: {
1.22      nicm      148:        int     i;
1.1       nicm      149:
1.22      nicm      150:        i = idx;
                    151:        do {
1.1       nicm      152:                if (winlink_find_by_index(wwl, i) == NULL)
                    153:                        return (i);
1.22      nicm      154:                if (i == INT_MAX)
                    155:                        i = 0;
                    156:                else
                    157:                        i++;
                    158:        } while (i != idx);
                    159:        return (-1);
1.1       nicm      160: }
                    161:
                    162: u_int
                    163: winlink_count(struct winlinks *wwl)
                    164: {
                    165:        struct winlink  *wl;
                    166:        u_int            n;
                    167:
                    168:        n = 0;
                    169:        RB_FOREACH(wl, winlinks, wwl)
                    170:                n++;
                    171:
                    172:        return (n);
                    173: }
                    174:
                    175: struct winlink *
1.63      nicm      176: winlink_add(struct winlinks *wwl, int idx)
1.1       nicm      177: {
                    178:        struct winlink  *wl;
                    179:
1.22      nicm      180:        if (idx < 0) {
                    181:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
                    182:                        return (NULL);
                    183:        } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1       nicm      184:                return (NULL);
                    185:
                    186:        wl = xcalloc(1, sizeof *wl);
                    187:        wl->idx = idx;
                    188:        RB_INSERT(winlinks, wwl, wl);
                    189:
1.63      nicm      190:        return (wl);
                    191: }
                    192:
                    193: void
                    194: winlink_set_window(struct winlink *wl, struct window *w)
                    195: {
1.174     nicm      196:        if (wl->window != NULL) {
                    197:                TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
1.192     nicm      198:                window_remove_ref(wl->window, __func__);
1.174     nicm      199:        }
                    200:        TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
1.63      nicm      201:        wl->window = w;
1.192     nicm      202:        window_add_ref(w, __func__);
1.1       nicm      203: }
                    204:
                    205: void
                    206: winlink_remove(struct winlinks *wwl, struct winlink *wl)
                    207: {
                    208:        struct window   *w = wl->window;
                    209:
1.174     nicm      210:        if (w != NULL) {
                    211:                TAILQ_REMOVE(&w->winlinks, wl, wentry);
1.192     nicm      212:                window_remove_ref(w, __func__);
1.174     nicm      213:        }
                    214:
1.1       nicm      215:        RB_REMOVE(winlinks, wwl, wl);
1.82      nicm      216:        free(wl);
1.1       nicm      217: }
                    218:
                    219: struct winlink *
1.41      nicm      220: winlink_next(struct winlink *wl)
1.1       nicm      221: {
                    222:        return (RB_NEXT(winlinks, wwl, wl));
                    223: }
                    224:
                    225: struct winlink *
1.41      nicm      226: winlink_previous(struct winlink *wl)
1.1       nicm      227: {
                    228:        return (RB_PREV(winlinks, wwl, wl));
1.52      nicm      229: }
                    230:
                    231: struct winlink *
1.53      nicm      232: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      233: {
                    234:        for (; n > 0; n--) {
                    235:                if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53      nicm      236:                        wl = RB_MIN(winlinks, &s->windows);
1.52      nicm      237:        }
                    238:
                    239:        return (wl);
                    240: }
                    241:
                    242: struct winlink *
1.53      nicm      243: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      244: {
                    245:        for (; n > 0; n--) {
                    246:                if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53      nicm      247:                        wl = RB_MAX(winlinks, &s->windows);
1.52      nicm      248:        }
                    249:
                    250:        return (wl);
1.1       nicm      251: }
                    252:
                    253: void
                    254: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    255: {
                    256:        if (wl == NULL)
                    257:                return;
                    258:
                    259:        winlink_stack_remove(stack, wl);
1.28      nicm      260:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1       nicm      261: }
                    262:
                    263: void
                    264: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    265: {
                    266:        struct winlink  *wl2;
                    267:
                    268:        if (wl == NULL)
                    269:                return;
1.42      nicm      270:
1.28      nicm      271:        TAILQ_FOREACH(wl2, stack, sentry) {
1.1       nicm      272:                if (wl2 == wl) {
1.28      nicm      273:                        TAILQ_REMOVE(stack, wl, sentry);
1.1       nicm      274:                        return;
                    275:                }
                    276:        }
                    277: }
                    278:
                    279: struct window *
1.125     nicm      280: window_find_by_id_str(const char *s)
1.123     nicm      281: {
                    282:        const char      *errstr;
                    283:        u_int            id;
                    284:
                    285:        if (*s != '@')
                    286:                return (NULL);
                    287:
                    288:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    289:        if (errstr != NULL)
                    290:                return (NULL);
                    291:        return (window_find_by_id(id));
                    292: }
                    293:
                    294: struct window *
1.71      nicm      295: window_find_by_id(u_int id)
                    296: {
1.122     nicm      297:        struct window   w;
1.71      nicm      298:
1.122     nicm      299:        w.id = id;
                    300:        return (RB_FIND(windows, &windows, &w));
1.71      nicm      301: }
                    302:
1.143     nicm      303: void
                    304: window_update_activity(struct window *w)
                    305: {
                    306:        gettimeofday(&w->activity_time, NULL);
                    307:        alerts_queue(w, WINDOW_ACTIVITY);
                    308: }
                    309:
1.71      nicm      310: struct window *
1.245     nicm      311: window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel)
1.1       nicm      312: {
                    313:        struct window   *w;
                    314:
1.245     nicm      315:        if (xpixel == 0)
                    316:                xpixel = DEFAULT_XPIXEL;
                    317:        if (ypixel == 0)
                    318:                ypixel = DEFAULT_YPIXEL;
                    319:
1.38      nicm      320:        w = xcalloc(1, sizeof *w);
1.244     nicm      321:        w->name = xstrdup("");
1.234     nicm      322:        w->flags = 0;
1.1       nicm      323:
                    324:        TAILQ_INIT(&w->panes);
                    325:        w->active = NULL;
1.14      nicm      326:
1.17      nicm      327:        w->lastlayout = -1;
1.14      nicm      328:        w->layout_root = NULL;
1.42      nicm      329:
1.1       nicm      330:        w->sx = sx;
                    331:        w->sy = sy;
1.245     nicm      332:        w->xpixel = xpixel;
                    333:        w->ypixel = ypixel;
1.133     nicm      334:
1.146     nicm      335:        w->options = options_create(global_w_options);
1.1       nicm      336:
                    337:        w->references = 0;
1.174     nicm      338:        TAILQ_INIT(&w->winlinks);
1.1       nicm      339:
1.122     nicm      340:        w->id = next_window_id++;
1.129     nicm      341:        RB_INSERT(windows, &windows, w);
1.122     nicm      342:
1.143     nicm      343:        window_update_activity(w);
                    344:
1.1       nicm      345:        return (w);
                    346: }
                    347:
1.240     nicm      348: static void
1.1       nicm      349: window_destroy(struct window *w)
                    350: {
1.192     nicm      351:        log_debug("window @%u destroyed (%d references)", w->id, w->references);
1.174     nicm      352:
1.122     nicm      353:        RB_REMOVE(windows, &windows, w);
1.1       nicm      354:
1.14      nicm      355:        if (w->layout_root != NULL)
1.135     nicm      356:                layout_free_cell(w->layout_root);
                    357:        if (w->saved_layout_root != NULL)
                    358:                layout_free_cell(w->saved_layout_root);
1.127     nicm      359:        free(w->old_layout);
1.139     nicm      360:
1.227     nicm      361:        window_destroy_panes(w);
                    362:
1.141     nicm      363:        if (event_initialized(&w->name_event))
                    364:                evtimer_del(&w->name_event);
1.38      nicm      365:
1.143     nicm      366:        if (event_initialized(&w->alerts_timer))
                    367:                evtimer_del(&w->alerts_timer);
1.213     nicm      368:        if (event_initialized(&w->offset_timer))
                    369:                event_del(&w->offset_timer);
1.143     nicm      370:
1.146     nicm      371:        options_free(w->options);
1.1       nicm      372:
1.82      nicm      373:        free(w->name);
                    374:        free(w);
1.84      nicm      375: }
                    376:
1.200     nicm      377: int
                    378: window_pane_destroy_ready(struct window_pane *wp)
                    379: {
1.201     nicm      380:        int     n;
                    381:
                    382:        if (wp->pipe_fd != -1) {
                    383:                if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
                    384:                        return (0);
                    385:                if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0)
                    386:                        return (0);
                    387:        }
1.200     nicm      388:
                    389:        if (~wp->flags & PANE_EXITED)
                    390:                return (0);
                    391:        return (1);
                    392: }
                    393:
1.84      nicm      394: void
1.192     nicm      395: window_add_ref(struct window *w, const char *from)
                    396: {
                    397:        w->references++;
                    398:        log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
                    399: }
                    400:
                    401: void
                    402: window_remove_ref(struct window *w, const char *from)
1.84      nicm      403: {
                    404:        w->references--;
1.192     nicm      405:        log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
                    406:
1.84      nicm      407:        if (w->references == 0)
                    408:                window_destroy(w);
1.72      nicm      409: }
                    410:
                    411: void
                    412: window_set_name(struct window *w, const char *new_name)
                    413: {
1.82      nicm      414:        free(w->name);
1.198     nicm      415:        utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.172     nicm      416:        notify_window("window-renamed", w);
1.1       nicm      417: }
                    418:
1.15      nicm      419: void
1.245     nicm      420: window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
1.1       nicm      421: {
1.245     nicm      422:        if (xpixel == 0)
                    423:                xpixel = DEFAULT_XPIXEL;
                    424:        if (ypixel == 0)
                    425:                ypixel = DEFAULT_YPIXEL;
                    426:
                    427:        log_debug("%s: @%u resize %ux%u (%ux%u)", __func__, w->id, sx, sy,
                    428:            xpixel == -1 ? w->xpixel : xpixel,
                    429:            ypixel == -1 ? w->ypixel : ypixel);
1.1       nicm      430:        w->sx = sx;
                    431:        w->sy = sy;
1.245     nicm      432:        if (xpixel != -1)
                    433:                w->xpixel = xpixel;
                    434:        if (ypixel != -1)
                    435:                w->ypixel = ypixel;
                    436: }
                    437:
                    438: void
                    439: window_pane_send_resize(struct window_pane *wp, int yadjust)
                    440: {
                    441:        struct window   *w = wp->window;
                    442:        struct winsize   ws;
                    443:
                    444:        if (wp->fd == -1)
                    445:                return;
                    446:
                    447:        memset(&ws, 0, sizeof ws);
                    448:        ws.ws_col = wp->sx;
                    449:        ws.ws_row = wp->sy + yadjust;
                    450:        ws.ws_xpixel = w->xpixel * ws.ws_col;
                    451:        ws.ws_ypixel = w->ypixel * ws.ws_row;
                    452:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
                    453:                fatal("ioctl failed");
1.1       nicm      454: }
                    455:
1.114     nicm      456: int
1.118     nicm      457: window_has_pane(struct window *w, struct window_pane *wp)
                    458: {
                    459:        struct window_pane      *wp1;
                    460:
                    461:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    462:                if (wp1 == wp)
                    463:                        return (1);
                    464:        }
                    465:        return (0);
                    466: }
                    467:
                    468: int
1.226     nicm      469: window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
1.1       nicm      470: {
1.226     nicm      471:        log_debug("%s: pane %%%u", __func__, wp->id);
                    472:
1.59      nicm      473:        if (wp == w->active)
1.114     nicm      474:                return (0);
1.58      nicm      475:        w->last = w->active;
1.226     nicm      476:
1.1       nicm      477:        w->active = wp;
1.109     nicm      478:        w->active->active_point = next_active_point++;
1.136     nicm      479:        w->active->flags |= PANE_CHANGED;
1.226     nicm      480:
1.213     nicm      481:        tty_update_window_offset(w);
1.226     nicm      482:
                    483:        if (notify)
                    484:                notify_window("window-pane-changed", w);
1.114     nicm      485:        return (1);
1.145     nicm      486: }
                    487:
                    488: void
                    489: window_redraw_active_switch(struct window *w, struct window_pane *wp)
                    490: {
1.234     nicm      491:        struct style    *sy1, *sy2;
                    492:        int              c1, c2;
1.145     nicm      493:
                    494:        if (wp == w->active)
                    495:                return;
                    496:
1.234     nicm      497:        for (;;) {
                    498:                /*
                    499:                 * If the active and inactive styles or palettes are different,
                    500:                 * need to redraw the panes.
                    501:                 */
                    502:                sy1 = &wp->cached_style;
                    503:                sy2 = &wp->cached_active_style;
                    504:                if (!style_equal(sy1, sy2))
                    505:                        wp->flags |= PANE_REDRAW;
                    506:                else {
                    507:                        c1 = window_pane_get_palette(wp, sy1->gc.fg);
                    508:                        c2 = window_pane_get_palette(wp, sy2->gc.fg);
                    509:                        if (c1 != c2)
                    510:                                wp->flags |= PANE_REDRAW;
                    511:                        else {
                    512:                                c1 = window_pane_get_palette(wp, sy1->gc.bg);
                    513:                                c2 = window_pane_get_palette(wp, sy2->gc.bg);
                    514:                                if (c1 != c2)
                    515:                                        wp->flags |= PANE_REDRAW;
                    516:                        }
                    517:                }
                    518:                if (wp == w->active)
                    519:                        break;
                    520:                wp = w->active;
                    521:        }
1.29      nicm      522: }
                    523:
1.66      nicm      524: struct window_pane *
                    525: window_get_active_at(struct window *w, u_int x, u_int y)
1.29      nicm      526: {
                    527:        struct window_pane      *wp;
                    528:
                    529:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.219     nicm      530:                if (!window_pane_visible(wp))
                    531:                        continue;
1.66      nicm      532:                if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29      nicm      533:                        continue;
1.66      nicm      534:                if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29      nicm      535:                        continue;
1.66      nicm      536:                return (wp);
                    537:        }
                    538:        return (NULL);
                    539: }
                    540:
                    541: struct window_pane *
                    542: window_find_string(struct window *w, const char *s)
                    543: {
                    544:        u_int   x, y;
                    545:
                    546:        x = w->sx / 2;
                    547:        y = w->sy / 2;
                    548:
                    549:        if (strcasecmp(s, "top") == 0)
                    550:                y = 0;
                    551:        else if (strcasecmp(s, "bottom") == 0)
                    552:                y = w->sy - 1;
                    553:        else if (strcasecmp(s, "left") == 0)
                    554:                x = 0;
                    555:        else if (strcasecmp(s, "right") == 0)
                    556:                x = w->sx - 1;
                    557:        else if (strcasecmp(s, "top-left") == 0) {
                    558:                x = 0;
                    559:                y = 0;
                    560:        } else if (strcasecmp(s, "top-right") == 0) {
                    561:                x = w->sx - 1;
                    562:                y = 0;
                    563:        } else if (strcasecmp(s, "bottom-left") == 0) {
                    564:                x = 0;
                    565:                y = w->sy - 1;
                    566:        } else if (strcasecmp(s, "bottom-right") == 0) {
                    567:                x = w->sx - 1;
                    568:                y = w->sy - 1;
                    569:        } else
                    570:                return (NULL);
                    571:
                    572:        return (window_get_active_at(w, x, y));
1.1       nicm      573: }
                    574:
1.93      nicm      575: int
                    576: window_zoom(struct window_pane *wp)
                    577: {
                    578:        struct window           *w = wp->window;
                    579:        struct window_pane      *wp1;
                    580:
                    581:        if (w->flags & WINDOW_ZOOMED)
                    582:                return (-1);
                    583:
1.94      nicm      584:        if (window_count_panes(w) == 1)
                    585:                return (-1);
                    586:
1.93      nicm      587:        if (w->active != wp)
1.226     nicm      588:                window_set_active_pane(w, wp, 1);
1.93      nicm      589:
                    590:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    591:                wp1->saved_layout_cell = wp1->layout_cell;
                    592:                wp1->layout_cell = NULL;
                    593:        }
                    594:
                    595:        w->saved_layout_root = w->layout_root;
                    596:        layout_init(w, wp);
                    597:        w->flags |= WINDOW_ZOOMED;
1.172     nicm      598:        notify_window("window-layout-changed", w);
1.93      nicm      599:
                    600:        return (0);
                    601: }
                    602:
                    603: int
                    604: window_unzoom(struct window *w)
                    605: {
1.97      nicm      606:        struct window_pane      *wp;
1.93      nicm      607:
                    608:        if (!(w->flags & WINDOW_ZOOMED))
                    609:                return (-1);
                    610:
                    611:        w->flags &= ~WINDOW_ZOOMED;
                    612:        layout_free(w);
                    613:        w->layout_root = w->saved_layout_root;
1.120     nicm      614:        w->saved_layout_root = NULL;
1.93      nicm      615:
1.97      nicm      616:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    617:                wp->layout_cell = wp->saved_layout_cell;
                    618:                wp->saved_layout_cell = NULL;
1.93      nicm      619:        }
1.213     nicm      620:        layout_fix_panes(w);
1.172     nicm      621:        notify_window("window-layout-changed", w);
1.93      nicm      622:
1.241     nicm      623:        return (0);
                    624: }
                    625:
                    626: int
                    627: window_push_zoom(struct window *w, int flag)
                    628: {
                    629:        log_debug("%s: @%u %d", __func__, w->id,
                    630:            flag && (w->flags & WINDOW_ZOOMED));
                    631:        if (flag && (w->flags & WINDOW_ZOOMED))
                    632:                w->flags |= WINDOW_WASZOOMED;
                    633:        else
                    634:                w->flags &= ~WINDOW_WASZOOMED;
                    635:        return (window_unzoom(w) == 0);
                    636: }
                    637:
                    638: int
                    639: window_pop_zoom(struct window *w)
                    640: {
                    641:        log_debug("%s: @%u %d", __func__, w->id,
                    642:            !!(w->flags & WINDOW_WASZOOMED));
                    643:        if (w->flags & WINDOW_WASZOOMED)
                    644:                return (window_zoom(w->active) == 0);
1.93      nicm      645:        return (0);
                    646: }
                    647:
1.1       nicm      648: struct window_pane *
1.226     nicm      649: window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
                    650:     int flags)
1.1       nicm      651: {
                    652:        struct window_pane      *wp;
                    653:
1.185     nicm      654:        if (other == NULL)
                    655:                other = w->active;
                    656:
1.14      nicm      657:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.186     nicm      658:        if (TAILQ_EMPTY(&w->panes)) {
                    659:                log_debug("%s: @%u at start", __func__, w->id);
1.1       nicm      660:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
1.226     nicm      661:        } else if (flags & SPAWN_BEFORE) {
1.186     nicm      662:                log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
1.226     nicm      663:                if (flags & SPAWN_FULLSIZE)
1.208     nicm      664:                        TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    665:                else
                    666:                        TAILQ_INSERT_BEFORE(other, wp, entry);
1.186     nicm      667:        } else {
                    668:                log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
1.226     nicm      669:                if (flags & SPAWN_FULLSIZE)
1.208     nicm      670:                        TAILQ_INSERT_TAIL(&w->panes, wp, entry);
                    671:                else
                    672:                        TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
1.186     nicm      673:        }
1.1       nicm      674:        return (wp);
                    675: }
                    676:
                    677: void
1.105     nicm      678: window_lost_pane(struct window *w, struct window_pane *wp)
1.1       nicm      679: {
1.205     nicm      680:        log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
                    681:
1.152     nicm      682:        if (wp == marked_pane.wp)
1.132     nicm      683:                server_clear_marked();
                    684:
1.57      nicm      685:        if (wp == w->active) {
1.58      nicm      686:                w->active = w->last;
                    687:                w->last = NULL;
                    688:                if (w->active == NULL) {
                    689:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    690:                        if (w->active == NULL)
                    691:                                w->active = TAILQ_NEXT(wp, entry);
                    692:                }
1.193     nicm      693:                if (w->active != NULL) {
1.151     nicm      694:                        w->active->flags |= PANE_CHANGED;
1.193     nicm      695:                        notify_window("window-pane-changed", w);
                    696:                }
1.58      nicm      697:        } else if (wp == w->last)
                    698:                w->last = NULL;
1.105     nicm      699: }
                    700:
                    701: void
                    702: window_remove_pane(struct window *w, struct window_pane *wp)
                    703: {
                    704:        window_lost_pane(w, wp);
1.1       nicm      705:
                    706:        TAILQ_REMOVE(&w->panes, wp, entry);
                    707:        window_pane_destroy(wp);
                    708: }
                    709:
                    710: struct window_pane *
                    711: window_pane_at_index(struct window *w, u_int idx)
                    712: {
                    713:        struct window_pane      *wp;
                    714:        u_int                    n;
                    715:
1.146     nicm      716:        n = options_get_number(w->options, "pane-base-index");
1.1       nicm      717:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    718:                if (n == idx)
                    719:                        return (wp);
                    720:                n++;
                    721:        }
                    722:        return (NULL);
1.53      nicm      723: }
                    724:
                    725: struct window_pane *
                    726: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    727: {
                    728:        for (; n > 0; n--) {
                    729:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    730:                        wp = TAILQ_FIRST(&w->panes);
                    731:        }
                    732:
                    733:        return (wp);
                    734: }
                    735:
                    736: struct window_pane *
                    737: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    738:     u_int n)
                    739: {
                    740:        for (; n > 0; n--) {
                    741:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    742:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    743:        }
                    744:
                    745:        return (wp);
1.13      nicm      746: }
                    747:
1.69      nicm      748: int
                    749: window_pane_index(struct window_pane *wp, u_int *i)
1.13      nicm      750: {
                    751:        struct window_pane      *wq;
1.69      nicm      752:        struct window           *w = wp->window;
1.13      nicm      753:
1.146     nicm      754:        *i = options_get_number(w->options, "pane-base-index");
1.13      nicm      755:        TAILQ_FOREACH(wq, &w->panes, entry) {
1.69      nicm      756:                if (wp == wq) {
                    757:                        return (0);
                    758:                }
                    759:                (*i)++;
1.13      nicm      760:        }
1.69      nicm      761:
                    762:        return (-1);
1.1       nicm      763: }
                    764:
                    765: u_int
                    766: window_count_panes(struct window *w)
                    767: {
                    768:        struct window_pane      *wp;
                    769:        u_int                    n;
                    770:
                    771:        n = 0;
                    772:        TAILQ_FOREACH(wp, &w->panes, entry)
                    773:                n++;
                    774:        return (n);
                    775: }
                    776:
                    777: void
                    778: window_destroy_panes(struct window *w)
                    779: {
                    780:        struct window_pane      *wp;
                    781:
                    782:        while (!TAILQ_EMPTY(&w->panes)) {
                    783:                wp = TAILQ_FIRST(&w->panes);
                    784:                TAILQ_REMOVE(&w->panes, wp, entry);
                    785:                window_pane_destroy(wp);
                    786:        }
1.61      nicm      787: }
                    788:
1.188     nicm      789: const char *
1.189     nicm      790: window_printable_flags(struct winlink *wl)
1.61      nicm      791: {
1.189     nicm      792:        struct session  *s = wl->session;
                    793:        static char      flags[32];
                    794:        int              pos;
1.61      nicm      795:
                    796:        pos = 0;
                    797:        if (wl->flags & WINLINK_ACTIVITY)
                    798:                flags[pos++] = '#';
                    799:        if (wl->flags & WINLINK_BELL)
                    800:                flags[pos++] = '!';
                    801:        if (wl->flags & WINLINK_SILENCE)
                    802:                flags[pos++] = '~';
                    803:        if (wl == s->curw)
                    804:                flags[pos++] = '*';
                    805:        if (wl == TAILQ_FIRST(&s->lastw))
                    806:                flags[pos++] = '-';
1.152     nicm      807:        if (server_check_marked() && wl == marked_pane.wl)
1.132     nicm      808:                flags[pos++] = 'M';
1.93      nicm      809:        if (wl->window->flags & WINDOW_ZOOMED)
                    810:                flags[pos++] = 'Z';
1.61      nicm      811:        flags[pos] = '\0';
1.188     nicm      812:        return (flags);
1.1       nicm      813: }
                    814:
1.123     nicm      815: struct window_pane *
                    816: window_pane_find_by_id_str(const char *s)
                    817: {
                    818:        const char      *errstr;
                    819:        u_int            id;
                    820:
                    821:        if (*s != '%')
                    822:                return (NULL);
                    823:
                    824:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    825:        if (errstr != NULL)
                    826:                return (NULL);
                    827:        return (window_pane_find_by_id(id));
                    828: }
                    829:
1.64      nicm      830: struct window_pane *
                    831: window_pane_find_by_id(u_int id)
                    832: {
                    833:        struct window_pane      wp;
                    834:
                    835:        wp.id = id;
                    836:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
                    837: }
                    838:
1.169     nicm      839: static struct window_pane *
1.1       nicm      840: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    841: {
                    842:        struct window_pane      *wp;
1.140     nicm      843:        char                     host[HOST_NAME_MAX + 1];
1.1       nicm      844:
                    845:        wp = xcalloc(1, sizeof *wp);
                    846:        wp->window = w;
1.234     nicm      847:        wp->options = options_create(w->options);
                    848:        wp->flags = PANE_STYLECHANGED;
1.1       nicm      849:
1.71      nicm      850:        wp->id = next_window_pane_id++;
1.64      nicm      851:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
                    852:
1.110     nicm      853:        wp->argc = 0;
                    854:        wp->argv = NULL;
1.23      nicm      855:        wp->shell = NULL;
1.147     nicm      856:        wp->cwd = NULL;
1.1       nicm      857:
                    858:        wp->fd = -1;
1.37      nicm      859:        wp->event = NULL;
1.1       nicm      860:
1.218     nicm      861:        TAILQ_INIT(&wp->modes);
1.14      nicm      862:
                    863:        wp->layout_cell = NULL;
1.1       nicm      864:
                    865:        wp->xoff = 0;
1.42      nicm      866:        wp->yoff = 0;
1.1       nicm      867:
1.197     nicm      868:        wp->sx = wp->osx = sx;
                    869:        wp->sy = wp->osx = sy;
1.1       nicm      870:
1.32      nicm      871:        wp->pipe_fd = -1;
                    872:        wp->pipe_off = 0;
1.36      nicm      873:        wp->pipe_event = NULL;
1.32      nicm      874:
1.9       nicm      875:        wp->saved_grid = NULL;
1.231     nicm      876:        wp->saved_cx = UINT_MAX;
                    877:        wp->saved_cy = UINT_MAX;
1.117     nicm      878:
1.1       nicm      879:        screen_init(&wp->base, sx, sy, hlimit);
                    880:        wp->screen = &wp->base;
1.159     nicm      881:
                    882:        screen_init(&wp->status_screen, 1, 1, 0);
1.140     nicm      883:
                    884:        if (gethostname(host, sizeof host) == 0)
                    885:                screen_set_title(&wp->base, host);
1.1       nicm      886:
                    887:        input_init(wp);
                    888:
                    889:        return (wp);
                    890: }
                    891:
1.169     nicm      892: static void
1.1       nicm      893: window_pane_destroy(struct window_pane *wp)
                    894: {
1.218     nicm      895:        window_pane_reset_mode_all(wp);
1.194     nicm      896:        free(wp->searchstr);
1.55      nicm      897:
1.37      nicm      898:        if (wp->fd != -1) {
1.70      nicm      899:                bufferevent_free(wp->event);
1.1       nicm      900:                close(wp->fd);
1.37      nicm      901:        }
1.1       nicm      902:
                    903:        input_free(wp);
1.225     nicm      904:
                    905:        screen_free(&wp->status_screen);
1.1       nicm      906:
                    907:        screen_free(&wp->base);
1.9       nicm      908:        if (wp->saved_grid != NULL)
                    909:                grid_destroy(wp->saved_grid);
1.1       nicm      910:
1.32      nicm      911:        if (wp->pipe_fd != -1) {
1.70      nicm      912:                bufferevent_free(wp->pipe_event);
1.32      nicm      913:                close(wp->pipe_fd);
                    914:        }
1.167     nicm      915:
                    916:        if (event_initialized(&wp->resize_timer))
                    917:                event_del(&wp->resize_timer);
1.32      nicm      918:
1.64      nicm      919:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    920:
1.234     nicm      921:        options_free(wp->options);
1.147     nicm      922:        free((void *)wp->cwd);
1.82      nicm      923:        free(wp->shell);
1.110     nicm      924:        cmd_free_argv(wp->argc, wp->argv);
1.177     nicm      925:        free(wp->palette);
1.82      nicm      926:        free(wp);
1.1       nicm      927: }
                    928:
1.166     nicm      929: static void
1.149     nicm      930: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37      nicm      931: {
1.131     nicm      932:        struct window_pane      *wp = data;
                    933:        struct evbuffer         *evb = wp->event->input;
1.166     nicm      934:        size_t                   size = EVBUFFER_LENGTH(evb);
1.131     nicm      935:        char                    *new_data;
1.158     nicm      936:        size_t                   new_size;
1.131     nicm      937:
1.166     nicm      938:        new_size = size - wp->pipe_off;
1.46      nicm      939:        if (wp->pipe_fd != -1 && new_size > 0) {
1.155     nicm      940:                new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
1.46      nicm      941:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    942:        }
                    943:
1.183     nicm      944:        log_debug("%%%u has %zu bytes", wp->id, size);
1.46      nicm      945:        input_parse(wp);
1.37      nicm      946:
1.173     nicm      947:        wp->pipe_off = EVBUFFER_LENGTH(evb);
1.37      nicm      948: }
                    949:
1.166     nicm      950: static void
1.149     nicm      951: window_pane_error_callback(__unused struct bufferevent *bufev,
                    952:     __unused short what, void *data)
1.37      nicm      953: {
                    954:        struct window_pane *wp = data;
                    955:
1.200     nicm      956:        log_debug("%%%u error", wp->id);
1.201     nicm      957:        wp->flags |= PANE_EXITED;
1.200     nicm      958:
                    959:        if (window_pane_destroy_ready(wp))
                    960:                server_destroy_pane(wp, 1);
1.37      nicm      961: }
                    962:
                    963: void
1.226     nicm      964: window_pane_set_event(struct window_pane *wp)
                    965: {
                    966:        setblocking(wp->fd, 0);
                    967:
                    968:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
                    969:            NULL, window_pane_error_callback, wp);
                    970:
                    971:        bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
                    972:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
                    973: }
                    974:
                    975: void
1.1       nicm      976: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    977: {
1.218     nicm      978:        struct window_mode_entry        *wme;
                    979:
1.1       nicm      980:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      981:                return;
1.1       nicm      982:        wp->sx = sx;
                    983:        wp->sy = sy;
                    984:
1.238     nicm      985:        log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1.89      nicm      986:        screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.218     nicm      987:
                    988:        wme = TAILQ_FIRST(&wp->modes);
                    989:        if (wme != NULL && wme->mode->resize != NULL)
                    990:                wme->mode->resize(wme, sx, sy);
1.95      nicm      991:
1.242     nicm      992:        wp->flags |= (PANE_RESIZE|PANE_RESIZED);
1.44      nicm      993: }
                    994:
                    995: /*
                    996:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    997:  * history is not updated
                    998:  */
                    999: void
1.87      nicm     1000: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
                   1001:     int cursor)
1.44      nicm     1002: {
                   1003:        struct screen   *s = &wp->base;
                   1004:        u_int            sx, sy;
                   1005:
                   1006:        if (wp->saved_grid != NULL)
                   1007:                return;
1.235     nicm     1008:        if (!options_get_number(wp->options, "alternate-screen"))
1.44      nicm     1009:                return;
                   1010:        sx = screen_size_x(s);
                   1011:        sy = screen_size_y(s);
                   1012:
                   1013:        wp->saved_grid = grid_create(sx, sy, 0);
                   1014:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87      nicm     1015:        if (cursor) {
                   1016:                wp->saved_cx = s->cx;
                   1017:                wp->saved_cy = s->cy;
                   1018:        }
1.44      nicm     1019:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                   1020:
1.170     nicm     1021:        grid_view_clear(s->grid, 0, 0, sx, sy, 8);
1.44      nicm     1022:
                   1023:        wp->base.grid->flags &= ~GRID_HISTORY;
                   1024:
                   1025:        wp->flags |= PANE_REDRAW;
                   1026: }
                   1027:
                   1028: /* Exit alternate screen mode and restore the copied grid. */
                   1029: void
1.87      nicm     1030: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
                   1031:     int cursor)
1.44      nicm     1032: {
                   1033:        struct screen   *s = &wp->base;
                   1034:        u_int            sx, sy;
                   1035:
1.235     nicm     1036:        if (!options_get_number(wp->options, "alternate-screen"))
1.231     nicm     1037:                return;
                   1038:
                   1039:        /*
                   1040:         * Restore the cursor position and cell. This happens even if not
                   1041:         * currently in the alternate screen.
                   1042:         */
                   1043:        if (cursor && wp->saved_cx != UINT_MAX && wp->saved_cy != UINT_MAX) {
                   1044:                s->cx = wp->saved_cx;
                   1045:                if (s->cx > screen_size_x(s) - 1)
                   1046:                        s->cx = screen_size_x(s) - 1;
                   1047:                s->cy = wp->saved_cy;
                   1048:                if (s->cy > screen_size_y(s) - 1)
                   1049:                        s->cy = screen_size_y(s) - 1;
                   1050:                memcpy(gc, &wp->saved_cell, sizeof *gc);
                   1051:        }
                   1052:
1.44      nicm     1053:        if (wp->saved_grid == NULL)
                   1054:                return;
                   1055:        sx = screen_size_x(s);
                   1056:        sy = screen_size_y(s);
                   1057:
                   1058:        /*
                   1059:         * If the current size is bigger, temporarily resize to the old size
                   1060:         * before copying back.
                   1061:         */
                   1062:        if (sy > wp->saved_grid->sy)
1.89      nicm     1063:                screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44      nicm     1064:
1.231     nicm     1065:        /* Restore the saved grid. */
1.44      nicm     1066:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
                   1067:
                   1068:        /*
                   1069:         * Turn history back on (so resize can use it) and then resize back to
                   1070:         * the current size.
                   1071:         */
                   1072:        wp->base.grid->flags |= GRID_HISTORY;
1.89      nicm     1073:        if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
                   1074:                screen_resize(s, sx, sy, 1);
1.44      nicm     1075:
                   1076:        grid_destroy(wp->saved_grid);
                   1077:        wp->saved_grid = NULL;
                   1078:
1.177     nicm     1079:        wp->flags |= PANE_REDRAW;
                   1080: }
                   1081:
                   1082: void
                   1083: window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
                   1084: {
                   1085:        if (n > 0xff)
                   1086:                return;
                   1087:
                   1088:        if (wp->palette == NULL)
                   1089:                wp->palette = xcalloc(0x100, sizeof *wp->palette);
                   1090:
                   1091:        wp->palette[n] = colour;
                   1092:        wp->flags |= PANE_REDRAW;
                   1093: }
                   1094:
                   1095: void
                   1096: window_pane_unset_palette(struct window_pane *wp, u_int n)
                   1097: {
                   1098:        if (n > 0xff || wp->palette == NULL)
                   1099:                return;
                   1100:
                   1101:        wp->palette[n] = 0;
                   1102:        wp->flags |= PANE_REDRAW;
                   1103: }
                   1104:
                   1105: void
                   1106: window_pane_reset_palette(struct window_pane *wp)
                   1107: {
                   1108:        if (wp->palette == NULL)
                   1109:                return;
                   1110:
                   1111:        free(wp->palette);
                   1112:        wp->palette = NULL;
1.44      nicm     1113:        wp->flags |= PANE_REDRAW;
1.1       nicm     1114: }
                   1115:
1.180     nicm     1116: int
1.220     nicm     1117: window_pane_get_palette(struct window_pane *wp, int c)
1.180     nicm     1118: {
                   1119:        int     new;
                   1120:
                   1121:        if (wp == NULL || wp->palette == NULL)
                   1122:                return (-1);
                   1123:
                   1124:        new = -1;
                   1125:        if (c < 8)
                   1126:                new = wp->palette[c];
                   1127:        else if (c >= 90 && c <= 97)
                   1128:                new = wp->palette[8 + c - 90];
                   1129:        else if (c & COLOUR_FLAG_256)
                   1130:                new = wp->palette[c & ~COLOUR_FLAG_256];
                   1131:        if (new == 0)
                   1132:                return (-1);
                   1133:        return (new);
                   1134: }
                   1135:
1.162     nicm     1136: static void
                   1137: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
                   1138: {
                   1139:        struct window_pane      *wp = arg;
                   1140:        struct timeval           tv = { .tv_sec = 10 };
                   1141:        int                      n = 0;
                   1142:
                   1143:        evtimer_del(&wp->modetimer);
                   1144:        evtimer_add(&wp->modetimer, &tv);
                   1145:
                   1146:        log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
                   1147:
                   1148:        if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
                   1149:                if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
1.218     nicm     1150:                        window_pane_reset_mode_all(wp);
1.162     nicm     1151:        }
                   1152: }
                   1153:
1.1       nicm     1154: int
1.196     nicm     1155: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode,
                   1156:     struct cmd_find_state *fs, struct args *args)
1.1       nicm     1157: {
1.218     nicm     1158:        struct timeval                   tv = { .tv_sec = 10 };
                   1159:        struct window_mode_entry        *wme;
1.1       nicm     1160:
1.218     nicm     1161:        if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1.1       nicm     1162:                return (1);
1.217     nicm     1163:
1.162     nicm     1164:        wp->modelast = time(NULL);
1.218     nicm     1165:        if (TAILQ_EMPTY(&wp->modes)) {
                   1166:                evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
                   1167:                evtimer_add(&wp->modetimer, &tv);
                   1168:        }
                   1169:
                   1170:        TAILQ_FOREACH(wme, &wp->modes, entry) {
                   1171:                if (wme->mode == mode)
                   1172:                        break;
                   1173:        }
1.223     nicm     1174:        if (wme != NULL) {
1.218     nicm     1175:                TAILQ_REMOVE(&wp->modes, wme, entry);
1.223     nicm     1176:                TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
                   1177:        } else {
1.218     nicm     1178:                wme = xcalloc(1, sizeof *wme);
                   1179:                wme->wp = wp;
                   1180:                wme->mode = mode;
                   1181:                wme->prefix = 1;
1.223     nicm     1182:                TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1.218     nicm     1183:                wme->screen = wme->mode->init(wme, fs, args);
                   1184:        }
1.162     nicm     1185:
1.218     nicm     1186:        wp->screen = wme->screen;
1.142     nicm     1187:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1188:
                   1189:        server_status_window(wp->window);
1.193     nicm     1190:        notify_pane("pane-mode-changed", wp);
1.218     nicm     1191:
1.1       nicm     1192:        return (0);
                   1193: }
                   1194:
                   1195: void
                   1196: window_pane_reset_mode(struct window_pane *wp)
                   1197: {
1.218     nicm     1198:        struct window_mode_entry        *wme, *next;
                   1199:
                   1200:        if (TAILQ_EMPTY(&wp->modes))
1.1       nicm     1201:                return;
                   1202:
1.218     nicm     1203:        wme = TAILQ_FIRST(&wp->modes);
                   1204:        TAILQ_REMOVE(&wp->modes, wme, entry);
                   1205:        wme->mode->free(wme);
                   1206:        free(wme);
                   1207:
                   1208:        next = TAILQ_FIRST(&wp->modes);
                   1209:        if (next == NULL) {
                   1210:                log_debug("%s: no next mode", __func__);
                   1211:                evtimer_del(&wp->modetimer);
                   1212:                wp->screen = &wp->base;
                   1213:        } else {
                   1214:                log_debug("%s: next mode is %s", __func__, next->mode->name);
                   1215:                wp->screen = next->screen;
1.230     nicm     1216:                if (next->mode->resize != NULL)
1.218     nicm     1217:                        next->mode->resize(next, wp->sx, wp->sy);
                   1218:        }
1.142     nicm     1219:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1220:
                   1221:        server_status_window(wp->window);
1.193     nicm     1222:        notify_pane("pane-mode-changed", wp);
1.1       nicm     1223: }
                   1224:
                   1225: void
1.218     nicm     1226: window_pane_reset_mode_all(struct window_pane *wp)
                   1227: {
                   1228:        while (!TAILQ_EMPTY(&wp->modes))
                   1229:                window_pane_reset_mode(wp);
                   1230: }
                   1231:
1.247   ! nicm     1232: int
1.118     nicm     1233: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.216     nicm     1234:     struct winlink *wl, key_code key, struct mouse_event *m)
1.1       nicm     1235: {
1.218     nicm     1236:        struct window_mode_entry        *wme;
1.217     nicm     1237:        struct window_pane              *wp2;
1.3       nicm     1238:
1.118     nicm     1239:        if (KEYC_IS_MOUSE(key) && m == NULL)
1.247   ! nicm     1240:                return (-1);
1.118     nicm     1241:
1.218     nicm     1242:        wme = TAILQ_FIRST(&wp->modes);
1.217     nicm     1243:        if (wme != NULL) {
1.162     nicm     1244:                wp->modelast = time(NULL);
1.217     nicm     1245:                if (wme->mode->key != NULL)
                   1246:                        wme->mode->key(wme, c, s, wl, (key & ~KEYC_XTERM), m);
1.247   ! nicm     1247:                return (0);
1.30      nicm     1248:        }
1.27      nicm     1249:
1.113     nicm     1250:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.247   ! nicm     1251:                return (0);
1.113     nicm     1252:
1.247   ! nicm     1253:        if (input_key(wp, key, m) != 0)
        !          1254:                return (-1);
1.118     nicm     1255:
                   1256:        if (KEYC_IS_MOUSE(key))
1.247   ! nicm     1257:                return (0);
1.146     nicm     1258:        if (options_get_number(wp->window->options, "synchronize-panes")) {
1.27      nicm     1259:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
1.213     nicm     1260:                        if (wp2 != wp &&
1.218     nicm     1261:                            TAILQ_EMPTY(&wp2->modes) &&
1.213     nicm     1262:                            wp2->fd != -1 &&
                   1263:                            (~wp2->flags & PANE_INPUTOFF) &&
                   1264:                            window_pane_visible(wp2))
1.118     nicm     1265:                                input_key(wp2, key, NULL);
1.27      nicm     1266:                }
                   1267:        }
1.247   ! nicm     1268:        return (0);
1.10      nicm     1269: }
                   1270:
                   1271: int
1.205     nicm     1272: window_pane_visible(struct window_pane *wp)
1.10      nicm     1273: {
1.213     nicm     1274:        if (~wp->window->flags & WINDOW_ZOOMED)
                   1275:                return (1);
                   1276:        return (wp == wp->window->active);
1.1       nicm     1277: }
                   1278:
1.195     nicm     1279: u_int
1.232     nicm     1280: window_pane_search(struct window_pane *wp, const char *term, int regex,
                   1281:     int ignore)
1.195     nicm     1282: {
                   1283:        struct screen   *s = &wp->base;
1.232     nicm     1284:        regex_t          r;
                   1285:        char            *new = NULL, *line;
1.195     nicm     1286:        u_int            i;
1.232     nicm     1287:        int              flags = 0, found;
1.236     nicm     1288:        size_t           n;
1.195     nicm     1289:
1.232     nicm     1290:        if (!regex) {
                   1291:                if (ignore)
                   1292:                        flags |= FNM_CASEFOLD;
                   1293:                xasprintf(&new, "*%s*", term);
                   1294:        } else {
                   1295:                if (ignore)
                   1296:                        flags |= REG_ICASE;
                   1297:                if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
                   1298:                        return (0);
                   1299:        }
1.195     nicm     1300:
                   1301:        for (i = 0; i < screen_size_y(s); i++) {
                   1302:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.236     nicm     1303:                for (n = strlen(line); n > 0; n--) {
                   1304:                        if (!isspace((u_char)line[n - 1]))
                   1305:                                break;
                   1306:                        line[n - 1] = '\0';
                   1307:                }
                   1308:                log_debug("%s: %s", __func__, line);
1.232     nicm     1309:                if (!regex)
                   1310:                        found = (fnmatch(new, line, 0) == 0);
                   1311:                else
                   1312:                        found = (regexec(&r, line, 0, NULL, 0) == 0);
                   1313:                free(line);
                   1314:                if (found)
1.195     nicm     1315:                        break;
                   1316:        }
1.232     nicm     1317:        if (!regex)
                   1318:                free(new);
                   1319:        else
                   1320:                regfree(&r);
1.195     nicm     1321:
                   1322:        if (i == screen_size_y(s))
                   1323:                return (0);
                   1324:        return (i + 1);
1.45      nicm     1325: }
                   1326:
1.109     nicm     1327: /* Get MRU pane from a list. */
1.166     nicm     1328: static struct window_pane *
1.126     nicm     1329: window_pane_choose_best(struct window_pane **list, u_int size)
1.109     nicm     1330: {
                   1331:        struct window_pane      *next, *best;
                   1332:        u_int                    i;
                   1333:
1.126     nicm     1334:        if (size == 0)
1.109     nicm     1335:                return (NULL);
                   1336:
1.126     nicm     1337:        best = list[0];
                   1338:        for (i = 1; i < size; i++) {
                   1339:                next = list[i];
1.109     nicm     1340:                if (next->active_point > best->active_point)
                   1341:                        best = next;
                   1342:        }
                   1343:        return (best);
                   1344: }
                   1345:
                   1346: /*
                   1347:  * Find the pane directly above another. We build a list of those adjacent to
                   1348:  * top edge and then choose the best.
                   1349:  */
1.45      nicm     1350: struct window_pane *
                   1351: window_pane_find_up(struct window_pane *wp)
                   1352: {
1.237     nicm     1353:        struct window           *w;
1.126     nicm     1354:        struct window_pane      *next, *best, **list;
                   1355:        u_int                    edge, left, right, end, size;
1.187     nicm     1356:        int                      status, found;
1.45      nicm     1357:
1.213     nicm     1358:        if (wp == NULL)
1.45      nicm     1359:                return (NULL);
1.237     nicm     1360:        w = wp->window;
                   1361:        status = options_get_number(w->options, "pane-border-status");
1.126     nicm     1362:
                   1363:        list = NULL;
                   1364:        size = 0;
1.109     nicm     1365:
                   1366:        edge = wp->yoff;
1.237     nicm     1367:        if (status == PANE_STATUS_TOP) {
                   1368:                if (edge == 1)
                   1369:                        edge = w->sy + 1;
                   1370:        } else if (status == PANE_STATUS_BOTTOM) {
                   1371:                if (edge == 0)
                   1372:                        edge = w->sy;
                   1373:        } else {
                   1374:                if (edge == 0)
                   1375:                        edge = w->sy + 1;
                   1376:        }
1.45      nicm     1377:
                   1378:        left = wp->xoff;
1.109     nicm     1379:        right = wp->xoff + wp->sx;
1.45      nicm     1380:
1.237     nicm     1381:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1382:                if (next == wp)
1.45      nicm     1383:                        continue;
1.109     nicm     1384:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1385:                        continue;
1.109     nicm     1386:                end = next->xoff + next->sx - 1;
                   1387:
                   1388:                found = 0;
                   1389:                if (next->xoff < left && end > right)
                   1390:                        found = 1;
                   1391:                else if (next->xoff >= left && next->xoff <= right)
                   1392:                        found = 1;
                   1393:                else if (end >= left && end <= right)
                   1394:                        found = 1;
1.126     nicm     1395:                if (!found)
                   1396:                        continue;
                   1397:                list = xreallocarray(list, size + 1, sizeof *list);
                   1398:                list[size++] = next;
1.109     nicm     1399:        }
                   1400:
1.126     nicm     1401:        best = window_pane_choose_best(list, size);
                   1402:        free(list);
1.109     nicm     1403:        return (best);
1.45      nicm     1404: }
                   1405:
                   1406: /* Find the pane directly below another. */
                   1407: struct window_pane *
                   1408: window_pane_find_down(struct window_pane *wp)
                   1409: {
1.237     nicm     1410:        struct window           *w;
1.126     nicm     1411:        struct window_pane      *next, *best, **list;
                   1412:        u_int                    edge, left, right, end, size;
1.187     nicm     1413:        int                      status, found;
1.45      nicm     1414:
1.213     nicm     1415:        if (wp == NULL)
1.45      nicm     1416:                return (NULL);
1.237     nicm     1417:        w = wp->window;
                   1418:        status = options_get_number(w->options, "pane-border-status");
1.126     nicm     1419:
                   1420:        list = NULL;
                   1421:        size = 0;
1.109     nicm     1422:
                   1423:        edge = wp->yoff + wp->sy + 1;
1.237     nicm     1424:        if (status == PANE_STATUS_TOP) {
                   1425:                if (edge >= w->sy)
                   1426:                        edge = 1;
                   1427:        } else if (status == PANE_STATUS_BOTTOM) {
                   1428:                if (edge >= w->sy - 1)
                   1429:                        edge = 0;
                   1430:        } else {
1.239     nicm     1431:                if (edge >= w->sy)
1.237     nicm     1432:                        edge = 0;
                   1433:        }
1.45      nicm     1434:
                   1435:        left = wp->xoff;
1.109     nicm     1436:        right = wp->xoff + wp->sx;
1.45      nicm     1437:
1.237     nicm     1438:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1439:                if (next == wp)
1.45      nicm     1440:                        continue;
1.109     nicm     1441:                if (next->yoff != edge)
1.45      nicm     1442:                        continue;
1.109     nicm     1443:                end = next->xoff + next->sx - 1;
                   1444:
                   1445:                found = 0;
                   1446:                if (next->xoff < left && end > right)
                   1447:                        found = 1;
                   1448:                else if (next->xoff >= left && next->xoff <= right)
                   1449:                        found = 1;
                   1450:                else if (end >= left && end <= right)
                   1451:                        found = 1;
1.126     nicm     1452:                if (!found)
                   1453:                        continue;
                   1454:                list = xreallocarray(list, size + 1, sizeof *list);
                   1455:                list[size++] = next;
1.45      nicm     1456:        }
1.109     nicm     1457:
1.126     nicm     1458:        best = window_pane_choose_best(list, size);
                   1459:        free(list);
1.109     nicm     1460:        return (best);
1.45      nicm     1461: }
                   1462:
1.109     nicm     1463: /* Find the pane directly to the left of another. */
1.45      nicm     1464: struct window_pane *
                   1465: window_pane_find_left(struct window_pane *wp)
                   1466: {
1.237     nicm     1467:        struct window           *w;
1.126     nicm     1468:        struct window_pane      *next, *best, **list;
                   1469:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1470:        int                      found;
1.45      nicm     1471:
1.213     nicm     1472:        if (wp == NULL)
1.45      nicm     1473:                return (NULL);
1.237     nicm     1474:        w = wp->window;
1.126     nicm     1475:
                   1476:        list = NULL;
                   1477:        size = 0;
1.109     nicm     1478:
                   1479:        edge = wp->xoff;
                   1480:        if (edge == 0)
1.237     nicm     1481:                edge = w->sx + 1;
1.45      nicm     1482:
                   1483:        top = wp->yoff;
1.109     nicm     1484:        bottom = wp->yoff + wp->sy;
1.45      nicm     1485:
1.237     nicm     1486:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1487:                if (next == wp)
1.45      nicm     1488:                        continue;
1.109     nicm     1489:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1490:                        continue;
1.109     nicm     1491:                end = next->yoff + next->sy - 1;
                   1492:
                   1493:                found = 0;
                   1494:                if (next->yoff < top && end > bottom)
                   1495:                        found = 1;
                   1496:                else if (next->yoff >= top && next->yoff <= bottom)
                   1497:                        found = 1;
                   1498:                else if (end >= top && end <= bottom)
                   1499:                        found = 1;
1.126     nicm     1500:                if (!found)
                   1501:                        continue;
                   1502:                list = xreallocarray(list, size + 1, sizeof *list);
                   1503:                list[size++] = next;
1.45      nicm     1504:        }
1.109     nicm     1505:
1.126     nicm     1506:        best = window_pane_choose_best(list, size);
                   1507:        free(list);
1.109     nicm     1508:        return (best);
1.45      nicm     1509: }
                   1510:
1.109     nicm     1511: /* Find the pane directly to the right of another. */
1.45      nicm     1512: struct window_pane *
                   1513: window_pane_find_right(struct window_pane *wp)
                   1514: {
1.237     nicm     1515:        struct window           *w;
1.126     nicm     1516:        struct window_pane      *next, *best, **list;
                   1517:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1518:        int                      found;
1.45      nicm     1519:
1.213     nicm     1520:        if (wp == NULL)
1.45      nicm     1521:                return (NULL);
1.237     nicm     1522:        w = wp->window;
1.126     nicm     1523:
                   1524:        list = NULL;
                   1525:        size = 0;
1.109     nicm     1526:
                   1527:        edge = wp->xoff + wp->sx + 1;
1.237     nicm     1528:        if (edge >= w->sx)
1.109     nicm     1529:                edge = 0;
1.45      nicm     1530:
                   1531:        top = wp->yoff;
1.109     nicm     1532:        bottom = wp->yoff + wp->sy;
1.45      nicm     1533:
1.237     nicm     1534:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1535:                if (next == wp)
1.45      nicm     1536:                        continue;
1.109     nicm     1537:                if (next->xoff != edge)
1.45      nicm     1538:                        continue;
1.109     nicm     1539:                end = next->yoff + next->sy - 1;
                   1540:
                   1541:                found = 0;
                   1542:                if (next->yoff < top && end > bottom)
                   1543:                        found = 1;
                   1544:                else if (next->yoff >= top && next->yoff <= bottom)
                   1545:                        found = 1;
                   1546:                else if (end >= top && end <= bottom)
                   1547:                        found = 1;
1.126     nicm     1548:                if (!found)
                   1549:                        continue;
                   1550:                list = xreallocarray(list, size + 1, sizeof *list);
                   1551:                list[size++] = next;
1.109     nicm     1552:        }
                   1553:
1.126     nicm     1554:        best = window_pane_choose_best(list, size);
                   1555:        free(list);
1.109     nicm     1556:        return (best);
1.81      nicm     1557: }
                   1558:
                   1559: /* Clear alert flags for a winlink */
                   1560: void
                   1561: winlink_clear_flags(struct winlink *wl)
                   1562: {
1.174     nicm     1563:        struct winlink  *loop;
1.81      nicm     1564:
1.174     nicm     1565:        wl->window->flags &= ~WINDOW_ALERTFLAGS;
                   1566:        TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
                   1567:                if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
                   1568:                        loop->flags &= ~WINLINK_ALERTFLAGS;
                   1569:                        server_status_session(loop->session);
1.81      nicm     1570:                }
                   1571:        }
1.134     nicm     1572: }
                   1573:
1.184     nicm     1574: /* Shuffle window indexes up. */
1.134     nicm     1575: int
                   1576: winlink_shuffle_up(struct session *s, struct winlink *wl)
                   1577: {
                   1578:        int      idx, last;
                   1579:
1.226     nicm     1580:        if (wl == NULL)
                   1581:                return (-1);
1.134     nicm     1582:        idx = wl->idx + 1;
                   1583:
                   1584:        /* Find the next free index. */
                   1585:        for (last = idx; last < INT_MAX; last++) {
                   1586:                if (winlink_find_by_index(&s->windows, last) == NULL)
                   1587:                        break;
                   1588:        }
                   1589:        if (last == INT_MAX)
                   1590:                return (-1);
                   1591:
                   1592:        /* Move everything from last - 1 to idx up a bit. */
                   1593:        for (; last > idx; last--) {
                   1594:                wl = winlink_find_by_index(&s->windows, last - 1);
                   1595:                server_link_window(s, wl, s, last, 0, 0, NULL);
                   1596:                server_unlink_window(s, wl);
                   1597:        }
                   1598:
                   1599:        return (idx);
1.228     nicm     1600: }
                   1601:
                   1602: static void
1.246     nicm     1603: window_pane_input_callback(struct client *c, __unused const char *path,
                   1604:     int error, int closed, struct evbuffer *buffer, void *data)
1.228     nicm     1605: {
                   1606:        struct window_pane_input_data   *cdata = data;
                   1607:        struct window_pane              *wp;
1.246     nicm     1608:        u_char                          *buf = EVBUFFER_DATA(buffer);
                   1609:        size_t                           len = EVBUFFER_LENGTH(buffer);
1.228     nicm     1610:
                   1611:        wp = window_pane_find_by_id(cdata->wp);
1.246     nicm     1612:        if (wp == NULL || closed || error != 0 || c->flags & CLIENT_DEAD) {
1.243     nicm     1613:                if (wp == NULL)
                   1614:                        c->flags |= CLIENT_EXIT;
                   1615:
1.246     nicm     1616:                evbuffer_drain(buffer, len);
                   1617:                cmdq_continue(cdata->item);
                   1618:
1.228     nicm     1619:                server_client_unref(c);
                   1620:                free(cdata);
                   1621:                return;
                   1622:        }
1.229     nicm     1623:        input_parse_buffer(wp, buf, len);
1.246     nicm     1624:        evbuffer_drain(buffer, len);
1.228     nicm     1625: }
                   1626:
                   1627: int
                   1628: window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
                   1629:     char **cause)
                   1630: {
                   1631:        struct client                   *c = item->client;
                   1632:        struct window_pane_input_data   *cdata;
                   1633:
                   1634:        if (~wp->flags & PANE_EMPTY) {
                   1635:                *cause = xstrdup("pane is not empty");
                   1636:                return (-1);
                   1637:        }
                   1638:
                   1639:        cdata = xmalloc(sizeof *cdata);
                   1640:        cdata->item = item;
                   1641:        cdata->wp = wp->id;
                   1642:
1.246     nicm     1643:        c->references++;
                   1644:        file_read(c, "-", window_pane_input_callback, cdata);
                   1645:
                   1646:        return (0);
1.1       nicm     1647: }