[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.186

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