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

1.192   ! nicm        1: /* $OpenBSD: window.c,v 1.191 2017/04/28 19:12:15 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);
1.192   ! nicm      189:                window_remove_ref(wl->window, __func__);
1.174     nicm      190:        }
                    191:        TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
1.63      nicm      192:        wl->window = w;
1.192   ! nicm      193:        window_add_ref(w, __func__);
1.1       nicm      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);
1.192   ! nicm      203:                window_remove_ref(w, __func__);
1.174     nicm      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.192   ! nicm      364:        log_debug("window @%u destroyed (%d references)", w->id, w->references);
1.174     nicm      365:
1.122     nicm      366:        RB_REMOVE(windows, &windows, w);
1.1       nicm      367:
1.14      nicm      368:        if (w->layout_root != NULL)
1.135     nicm      369:                layout_free_cell(w->layout_root);
                    370:        if (w->saved_layout_root != NULL)
                    371:                layout_free_cell(w->saved_layout_root);
1.127     nicm      372:        free(w->old_layout);
1.139     nicm      373:
1.141     nicm      374:        if (event_initialized(&w->name_event))
                    375:                evtimer_del(&w->name_event);
1.38      nicm      376:
1.143     nicm      377:        if (event_initialized(&w->alerts_timer))
                    378:                evtimer_del(&w->alerts_timer);
                    379:
1.146     nicm      380:        options_free(w->options);
1.1       nicm      381:
                    382:        window_destroy_panes(w);
                    383:
1.82      nicm      384:        free(w->name);
                    385:        free(w);
1.84      nicm      386: }
                    387:
                    388: void
1.192   ! nicm      389: window_add_ref(struct window *w, const char *from)
        !           390: {
        !           391:        w->references++;
        !           392:        log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
        !           393: }
        !           394:
        !           395: void
        !           396: window_remove_ref(struct window *w, const char *from)
1.84      nicm      397: {
                    398:        w->references--;
1.192   ! nicm      399:        log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
        !           400:
1.84      nicm      401:        if (w->references == 0)
                    402:                window_destroy(w);
1.72      nicm      403: }
                    404:
                    405: void
                    406: window_set_name(struct window *w, const char *new_name)
                    407: {
1.82      nicm      408:        free(w->name);
1.72      nicm      409:        w->name = xstrdup(new_name);
1.172     nicm      410:        notify_window("window-renamed", w);
1.1       nicm      411: }
                    412:
1.15      nicm      413: void
1.1       nicm      414: window_resize(struct window *w, u_int sx, u_int sy)
                    415: {
                    416:        w->sx = sx;
                    417:        w->sy = sy;
                    418: }
                    419:
1.114     nicm      420: int
1.118     nicm      421: window_has_pane(struct window *w, struct window_pane *wp)
                    422: {
                    423:        struct window_pane      *wp1;
                    424:
                    425:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    426:                if (wp1 == wp)
                    427:                        return (1);
                    428:        }
                    429:        return (0);
                    430: }
                    431:
                    432: int
1.1       nicm      433: window_set_active_pane(struct window *w, struct window_pane *wp)
                    434: {
1.185     nicm      435:        log_debug("%s: pane %%%u (was %%%u)", __func__, wp->id, w->active->id);
1.59      nicm      436:        if (wp == w->active)
1.114     nicm      437:                return (0);
1.58      nicm      438:        w->last = w->active;
1.1       nicm      439:        w->active = wp;
1.10      nicm      440:        while (!window_pane_visible(w->active)) {
1.1       nicm      441:                w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10      nicm      442:                if (w->active == NULL)
                    443:                        w->active = TAILQ_LAST(&w->panes, window_panes);
                    444:                if (w->active == wp)
1.114     nicm      445:                        return (1);
1.29      nicm      446:        }
1.109     nicm      447:        w->active->active_point = next_active_point++;
1.136     nicm      448:        w->active->flags |= PANE_CHANGED;
1.114     nicm      449:        return (1);
1.145     nicm      450: }
                    451:
                    452: void
                    453: window_redraw_active_switch(struct window *w, struct window_pane *wp)
                    454: {
1.177     nicm      455:        const struct grid_cell  *gc;
1.145     nicm      456:
                    457:        if (wp == w->active)
                    458:                return;
                    459:
                    460:        /*
                    461:         * If window-style and window-active-style are the same, we don't need
1.177     nicm      462:         * to redraw panes when switching active panes.
1.145     nicm      463:         */
1.177     nicm      464:        gc = options_get_style(w->options, "window-active-style");
                    465:        if (style_equal(gc, options_get_style(w->options, "window-style")))
1.145     nicm      466:                return;
1.177     nicm      467:
                    468:        /*
                    469:         * If the now active or inactive pane do not have a custom style or if
                    470:         * the palette is different, they need to be redrawn.
                    471:         */
1.178     nicm      472:        if (window_pane_get_palette(w->active, w->active->colgc.fg) != -1 ||
                    473:            window_pane_get_palette(w->active, w->active->colgc.bg) != -1 ||
1.177     nicm      474:            style_equal(&grid_default_cell, &w->active->colgc))
1.145     nicm      475:                w->active->flags |= PANE_REDRAW;
1.178     nicm      476:        if (window_pane_get_palette(wp, wp->colgc.fg) != -1 ||
                    477:            window_pane_get_palette(wp, wp->colgc.bg) != -1 ||
1.177     nicm      478:            style_equal(&grid_default_cell, &wp->colgc))
1.145     nicm      479:                wp->flags |= PANE_REDRAW;
1.29      nicm      480: }
                    481:
1.66      nicm      482: struct window_pane *
                    483: window_get_active_at(struct window *w, u_int x, u_int y)
1.29      nicm      484: {
                    485:        struct window_pane      *wp;
                    486:
                    487:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.66      nicm      488:                if (!window_pane_visible(wp))
1.29      nicm      489:                        continue;
1.66      nicm      490:                if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29      nicm      491:                        continue;
1.66      nicm      492:                if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29      nicm      493:                        continue;
1.66      nicm      494:                return (wp);
                    495:        }
                    496:        return (NULL);
                    497: }
                    498:
                    499: struct window_pane *
                    500: window_find_string(struct window *w, const char *s)
                    501: {
                    502:        u_int   x, y;
                    503:
                    504:        x = w->sx / 2;
                    505:        y = w->sy / 2;
                    506:
                    507:        if (strcasecmp(s, "top") == 0)
                    508:                y = 0;
                    509:        else if (strcasecmp(s, "bottom") == 0)
                    510:                y = w->sy - 1;
                    511:        else if (strcasecmp(s, "left") == 0)
                    512:                x = 0;
                    513:        else if (strcasecmp(s, "right") == 0)
                    514:                x = w->sx - 1;
                    515:        else if (strcasecmp(s, "top-left") == 0) {
                    516:                x = 0;
                    517:                y = 0;
                    518:        } else if (strcasecmp(s, "top-right") == 0) {
                    519:                x = w->sx - 1;
                    520:                y = 0;
                    521:        } else if (strcasecmp(s, "bottom-left") == 0) {
                    522:                x = 0;
                    523:                y = w->sy - 1;
                    524:        } else if (strcasecmp(s, "bottom-right") == 0) {
                    525:                x = w->sx - 1;
                    526:                y = w->sy - 1;
                    527:        } else
                    528:                return (NULL);
                    529:
                    530:        return (window_get_active_at(w, x, y));
1.1       nicm      531: }
                    532:
1.93      nicm      533: int
                    534: window_zoom(struct window_pane *wp)
                    535: {
                    536:        struct window           *w = wp->window;
                    537:        struct window_pane      *wp1;
                    538:
                    539:        if (w->flags & WINDOW_ZOOMED)
                    540:                return (-1);
                    541:
                    542:        if (!window_pane_visible(wp))
                    543:                return (-1);
1.94      nicm      544:
                    545:        if (window_count_panes(w) == 1)
                    546:                return (-1);
                    547:
1.93      nicm      548:        if (w->active != wp)
                    549:                window_set_active_pane(w, wp);
                    550:
                    551:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    552:                wp1->saved_layout_cell = wp1->layout_cell;
                    553:                wp1->layout_cell = NULL;
                    554:        }
                    555:
                    556:        w->saved_layout_root = w->layout_root;
                    557:        layout_init(w, wp);
                    558:        w->flags |= WINDOW_ZOOMED;
1.172     nicm      559:        notify_window("window-layout-changed", w);
1.93      nicm      560:
                    561:        return (0);
                    562: }
                    563:
                    564: int
                    565: window_unzoom(struct window *w)
                    566: {
1.97      nicm      567:        struct window_pane      *wp;
1.93      nicm      568:
                    569:        if (!(w->flags & WINDOW_ZOOMED))
                    570:                return (-1);
                    571:
                    572:        w->flags &= ~WINDOW_ZOOMED;
                    573:        layout_free(w);
                    574:        w->layout_root = w->saved_layout_root;
1.120     nicm      575:        w->saved_layout_root = NULL;
1.93      nicm      576:
1.97      nicm      577:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    578:                wp->layout_cell = wp->saved_layout_cell;
                    579:                wp->saved_layout_cell = NULL;
1.93      nicm      580:        }
                    581:        layout_fix_panes(w, w->sx, w->sy);
1.172     nicm      582:        notify_window("window-layout-changed", w);
1.93      nicm      583:
                    584:        return (0);
                    585: }
                    586:
1.1       nicm      587: struct window_pane *
1.185     nicm      588: window_add_pane(struct window *w, struct window_pane *other, int before,
                    589:     u_int hlimit)
1.1       nicm      590: {
                    591:        struct window_pane      *wp;
                    592:
1.185     nicm      593:        if (other == NULL)
                    594:                other = w->active;
                    595:
1.14      nicm      596:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.186     nicm      597:        if (TAILQ_EMPTY(&w->panes)) {
                    598:                log_debug("%s: @%u at start", __func__, w->id);
1.1       nicm      599:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
1.186     nicm      600:        } else if (before) {
                    601:                log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
1.185     nicm      602:                TAILQ_INSERT_BEFORE(other, wp, entry);
1.186     nicm      603:        } else {
                    604:                log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
1.185     nicm      605:                TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
1.186     nicm      606:        }
1.1       nicm      607:        return (wp);
                    608: }
                    609:
                    610: void
1.105     nicm      611: window_lost_pane(struct window *w, struct window_pane *wp)
1.1       nicm      612: {
1.152     nicm      613:        if (wp == marked_pane.wp)
1.132     nicm      614:                server_clear_marked();
                    615:
1.57      nicm      616:        if (wp == w->active) {
1.58      nicm      617:                w->active = w->last;
                    618:                w->last = NULL;
                    619:                if (w->active == NULL) {
                    620:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    621:                        if (w->active == NULL)
                    622:                                w->active = TAILQ_NEXT(wp, entry);
                    623:                }
1.151     nicm      624:                if (w->active != NULL)
                    625:                        w->active->flags |= PANE_CHANGED;
1.58      nicm      626:        } else if (wp == w->last)
                    627:                w->last = NULL;
1.105     nicm      628: }
                    629:
                    630: void
                    631: window_remove_pane(struct window *w, struct window_pane *wp)
                    632: {
                    633:        window_lost_pane(w, wp);
1.1       nicm      634:
                    635:        TAILQ_REMOVE(&w->panes, wp, entry);
                    636:        window_pane_destroy(wp);
                    637: }
                    638:
                    639: struct window_pane *
                    640: window_pane_at_index(struct window *w, u_int idx)
                    641: {
                    642:        struct window_pane      *wp;
                    643:        u_int                    n;
                    644:
1.146     nicm      645:        n = options_get_number(w->options, "pane-base-index");
1.1       nicm      646:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    647:                if (n == idx)
                    648:                        return (wp);
                    649:                n++;
                    650:        }
                    651:        return (NULL);
1.53      nicm      652: }
                    653:
                    654: struct window_pane *
                    655: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    656: {
                    657:        for (; n > 0; n--) {
                    658:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    659:                        wp = TAILQ_FIRST(&w->panes);
                    660:        }
                    661:
                    662:        return (wp);
                    663: }
                    664:
                    665: struct window_pane *
                    666: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    667:     u_int n)
                    668: {
                    669:        for (; n > 0; n--) {
                    670:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    671:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    672:        }
                    673:
                    674:        return (wp);
1.13      nicm      675: }
                    676:
1.69      nicm      677: int
                    678: window_pane_index(struct window_pane *wp, u_int *i)
1.13      nicm      679: {
                    680:        struct window_pane      *wq;
1.69      nicm      681:        struct window           *w = wp->window;
1.13      nicm      682:
1.146     nicm      683:        *i = options_get_number(w->options, "pane-base-index");
1.13      nicm      684:        TAILQ_FOREACH(wq, &w->panes, entry) {
1.69      nicm      685:                if (wp == wq) {
                    686:                        return (0);
                    687:                }
                    688:                (*i)++;
1.13      nicm      689:        }
1.69      nicm      690:
                    691:        return (-1);
1.1       nicm      692: }
                    693:
                    694: u_int
                    695: window_count_panes(struct window *w)
                    696: {
                    697:        struct window_pane      *wp;
                    698:        u_int                    n;
                    699:
                    700:        n = 0;
                    701:        TAILQ_FOREACH(wp, &w->panes, entry)
                    702:                n++;
                    703:        return (n);
                    704: }
                    705:
                    706: void
                    707: window_destroy_panes(struct window *w)
                    708: {
                    709:        struct window_pane      *wp;
                    710:
                    711:        while (!TAILQ_EMPTY(&w->panes)) {
                    712:                wp = TAILQ_FIRST(&w->panes);
                    713:                TAILQ_REMOVE(&w->panes, wp, entry);
                    714:                window_pane_destroy(wp);
                    715:        }
1.61      nicm      716: }
                    717:
1.188     nicm      718: const char *
1.189     nicm      719: window_printable_flags(struct winlink *wl)
1.61      nicm      720: {
1.189     nicm      721:        struct session  *s = wl->session;
                    722:        static char      flags[32];
                    723:        int              pos;
1.61      nicm      724:
                    725:        pos = 0;
                    726:        if (wl->flags & WINLINK_ACTIVITY)
                    727:                flags[pos++] = '#';
                    728:        if (wl->flags & WINLINK_BELL)
                    729:                flags[pos++] = '!';
                    730:        if (wl->flags & WINLINK_SILENCE)
                    731:                flags[pos++] = '~';
                    732:        if (wl == s->curw)
                    733:                flags[pos++] = '*';
                    734:        if (wl == TAILQ_FIRST(&s->lastw))
                    735:                flags[pos++] = '-';
1.152     nicm      736:        if (server_check_marked() && wl == marked_pane.wl)
1.132     nicm      737:                flags[pos++] = 'M';
1.93      nicm      738:        if (wl->window->flags & WINDOW_ZOOMED)
                    739:                flags[pos++] = 'Z';
1.61      nicm      740:        flags[pos] = '\0';
1.188     nicm      741:        return (flags);
1.1       nicm      742: }
                    743:
1.123     nicm      744: struct window_pane *
                    745: window_pane_find_by_id_str(const char *s)
                    746: {
                    747:        const char      *errstr;
                    748:        u_int            id;
                    749:
                    750:        if (*s != '%')
                    751:                return (NULL);
                    752:
                    753:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    754:        if (errstr != NULL)
                    755:                return (NULL);
                    756:        return (window_pane_find_by_id(id));
                    757: }
                    758:
1.64      nicm      759: struct window_pane *
                    760: window_pane_find_by_id(u_int id)
                    761: {
                    762:        struct window_pane      wp;
                    763:
                    764:        wp.id = id;
                    765:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
                    766: }
                    767:
1.169     nicm      768: static struct window_pane *
1.1       nicm      769: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    770: {
                    771:        struct window_pane      *wp;
1.140     nicm      772:        char                     host[HOST_NAME_MAX + 1];
1.1       nicm      773:
                    774:        wp = xcalloc(1, sizeof *wp);
                    775:        wp->window = w;
                    776:
1.71      nicm      777:        wp->id = next_window_pane_id++;
1.64      nicm      778:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
                    779:
1.110     nicm      780:        wp->argc = 0;
                    781:        wp->argv = NULL;
1.23      nicm      782:        wp->shell = NULL;
1.147     nicm      783:        wp->cwd = NULL;
1.1       nicm      784:
                    785:        wp->fd = -1;
1.37      nicm      786:        wp->event = NULL;
1.1       nicm      787:
                    788:        wp->mode = NULL;
1.176     nicm      789:        wp->modeprefix = 1;
1.14      nicm      790:
                    791:        wp->layout_cell = NULL;
1.1       nicm      792:
                    793:        wp->xoff = 0;
1.42      nicm      794:        wp->yoff = 0;
1.1       nicm      795:
                    796:        wp->sx = sx;
                    797:        wp->sy = sy;
                    798:
1.32      nicm      799:        wp->pipe_fd = -1;
                    800:        wp->pipe_off = 0;
1.36      nicm      801:        wp->pipe_event = NULL;
1.32      nicm      802:
1.9       nicm      803:        wp->saved_grid = NULL;
1.117     nicm      804:
                    805:        memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
1.9       nicm      806:
1.1       nicm      807:        screen_init(&wp->base, sx, sy, hlimit);
                    808:        wp->screen = &wp->base;
1.159     nicm      809:
                    810:        screen_init(&wp->status_screen, 1, 1, 0);
1.140     nicm      811:
                    812:        if (gethostname(host, sizeof host) == 0)
                    813:                screen_set_title(&wp->base, host);
1.1       nicm      814:
                    815:        input_init(wp);
                    816:
                    817:        return (wp);
                    818: }
                    819:
1.169     nicm      820: static void
1.1       nicm      821: window_pane_destroy(struct window_pane *wp)
                    822: {
1.55      nicm      823:        window_pane_reset_mode(wp);
                    824:
1.37      nicm      825:        if (wp->fd != -1) {
1.70      nicm      826:                bufferevent_free(wp->event);
1.1       nicm      827:                close(wp->fd);
1.37      nicm      828:        }
1.1       nicm      829:
                    830:        input_free(wp);
                    831:
                    832:        screen_free(&wp->base);
1.9       nicm      833:        if (wp->saved_grid != NULL)
                    834:                grid_destroy(wp->saved_grid);
1.1       nicm      835:
1.32      nicm      836:        if (wp->pipe_fd != -1) {
1.70      nicm      837:                bufferevent_free(wp->pipe_event);
1.32      nicm      838:                close(wp->pipe_fd);
                    839:        }
1.167     nicm      840:
                    841:        if (event_initialized(&wp->resize_timer))
                    842:                event_del(&wp->resize_timer);
1.32      nicm      843:
1.64      nicm      844:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    845:
1.147     nicm      846:        free((void *)wp->cwd);
1.82      nicm      847:        free(wp->shell);
1.110     nicm      848:        cmd_free_argv(wp->argc, wp->argv);
1.177     nicm      849:        free(wp->palette);
1.82      nicm      850:        free(wp);
1.1       nicm      851: }
                    852:
                    853: int
1.110     nicm      854: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
1.147     nicm      855:     const char *path, const char *shell, const char *cwd, struct environ *env,
1.110     nicm      856:     struct termios *tio, char **cause)
1.1       nicm      857: {
1.47      nicm      858:        struct winsize   ws;
1.150     nicm      859:        char            *argv0, *cmd, **argvp;
1.147     nicm      860:        const char      *ptr, *first, *home;
1.47      nicm      861:        struct termios   tio2;
1.110     nicm      862:        int              i;
1.1       nicm      863:
1.37      nicm      864:        if (wp->fd != -1) {
1.70      nicm      865:                bufferevent_free(wp->event);
1.1       nicm      866:                close(wp->fd);
1.37      nicm      867:        }
1.110     nicm      868:        if (argc > 0) {
                    869:                cmd_free_argv(wp->argc, wp->argv);
                    870:                wp->argc = argc;
                    871:                wp->argv = cmd_copy_argv(argc, argv);
1.1       nicm      872:        }
1.23      nicm      873:        if (shell != NULL) {
1.82      nicm      874:                free(wp->shell);
1.23      nicm      875:                wp->shell = xstrdup(shell);
                    876:        }
1.147     nicm      877:        if (cwd != NULL) {
                    878:                free((void *)wp->cwd);
                    879:                wp->cwd = xstrdup(cwd);
1.1       nicm      880:        }
1.91      nicm      881:
1.110     nicm      882:        cmd = cmd_stringify_argv(wp->argc, wp->argv);
                    883:        log_debug("spawn: %s -- %s", wp->shell, cmd);
                    884:        for (i = 0; i < wp->argc; i++)
                    885:                log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
1.165     nicm      886:        environ_log(env, "spawn: ");
1.1       nicm      887:
                    888:        memset(&ws, 0, sizeof ws);
                    889:        ws.ws_col = screen_size_x(&wp->base);
                    890:        ws.ws_row = screen_size_y(&wp->base);
                    891:
1.190     nicm      892:        wp->pid = fdforkpty(ptm_fd, &wp->fd, wp->tty, NULL, &ws);
1.182     nicm      893:        switch (wp->pid) {
1.1       nicm      894:        case -1:
                    895:                wp->fd = -1;
                    896:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110     nicm      897:                free(cmd);
1.1       nicm      898:                return (-1);
                    899:        case 0:
1.147     nicm      900:                if (chdir(wp->cwd) != 0) {
                    901:                        if ((home = find_home()) == NULL || chdir(home) != 0)
                    902:                                chdir("/");
                    903:                }
1.25      nicm      904:
                    905:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    906:                        fatal("tcgetattr failed");
                    907:                if (tio != NULL)
                    908:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    909:                tio2.c_cc[VERASE] = '\177';
                    910:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    911:                        fatal("tcgetattr failed");
1.18      nicm      912:
1.56      nicm      913:                closefrom(STDERR_FILENO + 1);
                    914:
1.107     nicm      915:                if (path != NULL)
1.150     nicm      916:                        environ_set(env, "PATH", "%s", path);
                    917:                environ_set(env, "TMUX_PANE", "%%%u", wp->id);
1.47      nicm      918:                environ_push(env);
1.18      nicm      919:
1.54      nicm      920:                clear_signals(1);
1.1       nicm      921:                log_close();
                    922:
1.80      nicm      923:                setenv("SHELL", wp->shell, 1);
                    924:                ptr = strrchr(wp->shell, '/');
                    925:
1.110     nicm      926:                /*
                    927:                 * If given one argument, assume it should be passed to sh -c;
                    928:                 * with more than one argument, use execvp(). If there is no
                    929:                 * arguments, create a login shell.
                    930:                 */
                    931:                if (wp->argc > 0) {
                    932:                        if (wp->argc != 1) {
                    933:                                /* Copy to ensure argv ends in NULL. */
                    934:                                argvp = cmd_copy_argv(wp->argc, wp->argv);
                    935:                                execvp(argvp[0], argvp);
                    936:                                fatal("execvp failed");
                    937:                        }
                    938:                        first = wp->argv[0];
                    939:
1.80      nicm      940:                        if (ptr != NULL && *(ptr + 1) != '\0')
                    941:                                xasprintf(&argv0, "%s", ptr + 1);
                    942:                        else
                    943:                                xasprintf(&argv0, "%s", wp->shell);
1.110     nicm      944:                        execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8       nicm      945:                        fatal("execl failed");
                    946:                }
1.23      nicm      947:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      948:                        xasprintf(&argv0, "-%s", ptr + 1);
                    949:                else
1.23      nicm      950:                        xasprintf(&argv0, "-%s", wp->shell);
1.110     nicm      951:                execl(wp->shell, argv0, (char *)NULL);
1.1       nicm      952:                fatal("execl failed");
                    953:        }
                    954:
1.62      nicm      955:        setblocking(wp->fd, 0);
                    956:
1.110     nicm      957:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
                    958:            window_pane_error_callback, wp);
1.131     nicm      959:
1.183     nicm      960:        bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
1.37      nicm      961:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      962:
1.110     nicm      963:        free(cmd);
1.1       nicm      964:        return (0);
                    965: }
                    966:
1.166     nicm      967: static void
1.149     nicm      968: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37      nicm      969: {
1.131     nicm      970:        struct window_pane      *wp = data;
                    971:        struct evbuffer         *evb = wp->event->input;
1.166     nicm      972:        size_t                   size = EVBUFFER_LENGTH(evb);
1.131     nicm      973:        char                    *new_data;
1.158     nicm      974:        size_t                   new_size;
1.131     nicm      975:
1.166     nicm      976:        new_size = size - wp->pipe_off;
1.46      nicm      977:        if (wp->pipe_fd != -1 && new_size > 0) {
1.155     nicm      978:                new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
1.46      nicm      979:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    980:        }
                    981:
1.183     nicm      982:        log_debug("%%%u has %zu bytes", wp->id, size);
1.46      nicm      983:        input_parse(wp);
1.37      nicm      984:
1.173     nicm      985:        wp->pipe_off = EVBUFFER_LENGTH(evb);
1.37      nicm      986: }
                    987:
1.166     nicm      988: static void
1.149     nicm      989: window_pane_error_callback(__unused struct bufferevent *bufev,
                    990:     __unused short what, void *data)
1.37      nicm      991: {
                    992:        struct window_pane *wp = data;
                    993:
1.153     nicm      994:        server_destroy_pane(wp, 1);
1.37      nicm      995: }
                    996:
                    997: void
1.1       nicm      998: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    999: {
                   1000:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm     1001:                return;
1.1       nicm     1002:        wp->sx = sx;
                   1003:        wp->sy = sy;
                   1004:
1.89      nicm     1005:        screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1       nicm     1006:        if (wp->mode != NULL)
                   1007:                wp->mode->resize(wp, sx, sy);
1.95      nicm     1008:
                   1009:        wp->flags |= PANE_RESIZE;
1.44      nicm     1010: }
                   1011:
                   1012: /*
                   1013:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                   1014:  * history is not updated
                   1015:  */
                   1016: void
1.87      nicm     1017: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
                   1018:     int cursor)
1.44      nicm     1019: {
                   1020:        struct screen   *s = &wp->base;
                   1021:        u_int            sx, sy;
                   1022:
                   1023:        if (wp->saved_grid != NULL)
                   1024:                return;
1.146     nicm     1025:        if (!options_get_number(wp->window->options, "alternate-screen"))
1.44      nicm     1026:                return;
                   1027:        sx = screen_size_x(s);
                   1028:        sy = screen_size_y(s);
                   1029:
                   1030:        wp->saved_grid = grid_create(sx, sy, 0);
                   1031:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87      nicm     1032:        if (cursor) {
                   1033:                wp->saved_cx = s->cx;
                   1034:                wp->saved_cy = s->cy;
                   1035:        }
1.44      nicm     1036:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                   1037:
1.170     nicm     1038:        grid_view_clear(s->grid, 0, 0, sx, sy, 8);
1.44      nicm     1039:
                   1040:        wp->base.grid->flags &= ~GRID_HISTORY;
                   1041:
                   1042:        wp->flags |= PANE_REDRAW;
                   1043: }
                   1044:
                   1045: /* Exit alternate screen mode and restore the copied grid. */
                   1046: void
1.87      nicm     1047: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
                   1048:     int cursor)
1.44      nicm     1049: {
                   1050:        struct screen   *s = &wp->base;
                   1051:        u_int            sx, sy;
                   1052:
                   1053:        if (wp->saved_grid == NULL)
                   1054:                return;
1.146     nicm     1055:        if (!options_get_number(wp->window->options, "alternate-screen"))
1.44      nicm     1056:                return;
                   1057:        sx = screen_size_x(s);
                   1058:        sy = screen_size_y(s);
                   1059:
                   1060:        /*
                   1061:         * If the current size is bigger, temporarily resize to the old size
                   1062:         * before copying back.
                   1063:         */
                   1064:        if (sy > wp->saved_grid->sy)
1.89      nicm     1065:                screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44      nicm     1066:
                   1067:        /* Restore the grid, cursor position and cell. */
                   1068:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87      nicm     1069:        if (cursor)
                   1070:                s->cx = wp->saved_cx;
1.44      nicm     1071:        if (s->cx > screen_size_x(s) - 1)
                   1072:                s->cx = screen_size_x(s) - 1;
1.87      nicm     1073:        if (cursor)
                   1074:                s->cy = wp->saved_cy;
1.44      nicm     1075:        if (s->cy > screen_size_y(s) - 1)
                   1076:                s->cy = screen_size_y(s) - 1;
                   1077:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                   1078:
                   1079:        /*
                   1080:         * Turn history back on (so resize can use it) and then resize back to
                   1081:         * the current size.
                   1082:         */
                   1083:        wp->base.grid->flags |= GRID_HISTORY;
1.89      nicm     1084:        if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
                   1085:                screen_resize(s, sx, sy, 1);
1.44      nicm     1086:
                   1087:        grid_destroy(wp->saved_grid);
                   1088:        wp->saved_grid = NULL;
                   1089:
1.177     nicm     1090:        wp->flags |= PANE_REDRAW;
                   1091: }
                   1092:
                   1093: void
                   1094: window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
                   1095: {
                   1096:        if (n > 0xff)
                   1097:                return;
                   1098:
                   1099:        if (wp->palette == NULL)
                   1100:                wp->palette = xcalloc(0x100, sizeof *wp->palette);
                   1101:
                   1102:        wp->palette[n] = colour;
                   1103:        wp->flags |= PANE_REDRAW;
                   1104: }
                   1105:
                   1106: void
                   1107: window_pane_unset_palette(struct window_pane *wp, u_int n)
                   1108: {
                   1109:        if (n > 0xff || wp->palette == NULL)
                   1110:                return;
                   1111:
                   1112:        wp->palette[n] = 0;
                   1113:        wp->flags |= PANE_REDRAW;
                   1114: }
                   1115:
                   1116: void
                   1117: window_pane_reset_palette(struct window_pane *wp)
                   1118: {
                   1119:        if (wp->palette == NULL)
                   1120:                return;
                   1121:
                   1122:        free(wp->palette);
                   1123:        wp->palette = NULL;
1.44      nicm     1124:        wp->flags |= PANE_REDRAW;
1.1       nicm     1125: }
                   1126:
1.180     nicm     1127: int
                   1128: window_pane_get_palette(const struct window_pane *wp, int c)
                   1129: {
                   1130:        int     new;
                   1131:
                   1132:        if (wp == NULL || wp->palette == NULL)
                   1133:                return (-1);
                   1134:
                   1135:        new = -1;
                   1136:        if (c < 8)
                   1137:                new = wp->palette[c];
                   1138:        else if (c >= 90 && c <= 97)
                   1139:                new = wp->palette[8 + c - 90];
                   1140:        else if (c & COLOUR_FLAG_256)
                   1141:                new = wp->palette[c & ~COLOUR_FLAG_256];
                   1142:        if (new == 0)
                   1143:                return (-1);
                   1144:        return (new);
                   1145: }
                   1146:
1.162     nicm     1147: static void
                   1148: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
                   1149: {
                   1150:        struct window_pane      *wp = arg;
                   1151:        struct timeval           tv = { .tv_sec = 10 };
                   1152:        int                      n = 0;
                   1153:
                   1154:        evtimer_del(&wp->modetimer);
                   1155:        evtimer_add(&wp->modetimer, &tv);
                   1156:
                   1157:        log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
                   1158:
                   1159:        if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
                   1160:                if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
                   1161:                        window_pane_reset_mode(wp);
                   1162:        }
                   1163: }
                   1164:
1.1       nicm     1165: int
                   1166: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                   1167: {
                   1168:        struct screen   *s;
1.162     nicm     1169:        struct timeval   tv = { .tv_sec = 10 };
1.1       nicm     1170:
1.15      nicm     1171:        if (wp->mode != NULL)
1.1       nicm     1172:                return (1);
                   1173:        wp->mode = mode;
                   1174:
1.162     nicm     1175:        wp->modelast = time(NULL);
                   1176:        evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
                   1177:        evtimer_add(&wp->modetimer, &tv);
                   1178:
1.1       nicm     1179:        if ((s = wp->mode->init(wp)) != NULL)
                   1180:                wp->screen = s;
1.142     nicm     1181:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1182:
                   1183:        server_status_window(wp->window);
1.1       nicm     1184:        return (0);
                   1185: }
                   1186:
                   1187: void
                   1188: window_pane_reset_mode(struct window_pane *wp)
                   1189: {
                   1190:        if (wp->mode == NULL)
                   1191:                return;
                   1192:
1.162     nicm     1193:        evtimer_del(&wp->modetimer);
                   1194:
1.1       nicm     1195:        wp->mode->free(wp);
                   1196:        wp->mode = NULL;
1.168     nicm     1197:        wp->modeprefix = 1;
1.1       nicm     1198:
                   1199:        wp->screen = &wp->base;
1.142     nicm     1200:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1201:
                   1202:        server_status_window(wp->window);
1.1       nicm     1203: }
                   1204:
                   1205: void
1.118     nicm     1206: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.148     nicm     1207:     key_code key, struct mouse_event *m)
1.1       nicm     1208: {
1.27      nicm     1209:        struct window_pane      *wp2;
1.3       nicm     1210:
1.118     nicm     1211:        if (KEYC_IS_MOUSE(key) && m == NULL)
                   1212:                return;
                   1213:
1.1       nicm     1214:        if (wp->mode != NULL) {
1.162     nicm     1215:                wp->modelast = time(NULL);
1.1       nicm     1216:                if (wp->mode->key != NULL)
1.118     nicm     1217:                        wp->mode->key(wp, c, s, key, m);
1.27      nicm     1218:                return;
1.30      nicm     1219:        }
1.27      nicm     1220:
1.113     nicm     1221:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30      nicm     1222:                return;
1.113     nicm     1223:
1.118     nicm     1224:        input_key(wp, key, m);
                   1225:
                   1226:        if (KEYC_IS_MOUSE(key))
                   1227:                return;
1.146     nicm     1228:        if (options_get_number(wp->window->options, "synchronize-panes")) {
1.27      nicm     1229:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1230:                        if (wp2 == wp || wp2->mode != NULL)
                   1231:                                continue;
1.154     nicm     1232:                        if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF)
                   1233:                                continue;
                   1234:                        if (window_pane_visible(wp2))
1.118     nicm     1235:                                input_key(wp2, key, NULL);
1.27      nicm     1236:                }
                   1237:        }
1.10      nicm     1238: }
                   1239:
                   1240: int
1.175     nicm     1241: window_pane_outside(struct window_pane *wp)
1.10      nicm     1242: {
                   1243:        struct window   *w = wp->window;
                   1244:
                   1245:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1.175     nicm     1246:                return (1);
1.10      nicm     1247:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1.175     nicm     1248:                return (1);
                   1249:        return (0);
                   1250: }
                   1251:
                   1252: int
                   1253: window_pane_visible(struct window_pane *wp)
                   1254: {
                   1255:        if (wp->layout_cell == NULL)
1.10      nicm     1256:                return (0);
1.175     nicm     1257:        return (!window_pane_outside(wp));
1.1       nicm     1258: }
                   1259:
                   1260: char *
1.108     nicm     1261: window_pane_search(struct window_pane *wp, const char *searchstr,
                   1262:     u_int *lineno)
1.1       nicm     1263: {
1.4       nicm     1264:        struct screen   *s = &wp->base;
1.5       nicm     1265:        char            *newsearchstr, *line, *msg;
1.187     nicm     1266:        u_int            i;
1.4       nicm     1267:
1.5       nicm     1268:        msg = NULL;
                   1269:        xasprintf(&newsearchstr, "*%s*", searchstr);
                   1270:
1.4       nicm     1271:        for (i = 0; i < screen_size_y(s); i++) {
                   1272:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm     1273:                if (fnmatch(newsearchstr, line, 0) == 0) {
                   1274:                        msg = line;
                   1275:                        if (lineno != NULL)
                   1276:                                *lineno = i;
                   1277:                        break;
                   1278:                }
1.82      nicm     1279:                free(line);
1.4       nicm     1280:        }
1.5       nicm     1281:
1.82      nicm     1282:        free(newsearchstr);
1.5       nicm     1283:        return (msg);
1.45      nicm     1284: }
                   1285:
1.109     nicm     1286: /* Get MRU pane from a list. */
1.166     nicm     1287: static struct window_pane *
1.126     nicm     1288: window_pane_choose_best(struct window_pane **list, u_int size)
1.109     nicm     1289: {
                   1290:        struct window_pane      *next, *best;
                   1291:        u_int                    i;
                   1292:
1.126     nicm     1293:        if (size == 0)
1.109     nicm     1294:                return (NULL);
                   1295:
1.126     nicm     1296:        best = list[0];
                   1297:        for (i = 1; i < size; i++) {
                   1298:                next = list[i];
1.109     nicm     1299:                if (next->active_point > best->active_point)
                   1300:                        best = next;
                   1301:        }
                   1302:        return (best);
                   1303: }
                   1304:
                   1305: /*
                   1306:  * Find the pane directly above another. We build a list of those adjacent to
                   1307:  * top edge and then choose the best.
                   1308:  */
1.45      nicm     1309: struct window_pane *
                   1310: window_pane_find_up(struct window_pane *wp)
                   1311: {
1.126     nicm     1312:        struct window_pane      *next, *best, **list;
                   1313:        u_int                    edge, left, right, end, size;
1.187     nicm     1314:        int                      status, found;
1.45      nicm     1315:
                   1316:        if (wp == NULL || !window_pane_visible(wp))
                   1317:                return (NULL);
1.187     nicm     1318:        status = options_get_number(wp->window->options, "pane-border-status");
1.126     nicm     1319:
                   1320:        list = NULL;
                   1321:        size = 0;
1.109     nicm     1322:
                   1323:        edge = wp->yoff;
1.187     nicm     1324:        if (edge == (status == 1 ? 1 : 0))
                   1325:                edge = wp->window->sy + 1 - (status == 2 ? 1 : 0);
1.45      nicm     1326:
                   1327:        left = wp->xoff;
1.109     nicm     1328:        right = wp->xoff + wp->sx;
1.45      nicm     1329:
1.109     nicm     1330:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1331:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1332:                        continue;
1.109     nicm     1333:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1334:                        continue;
1.109     nicm     1335:                end = next->xoff + next->sx - 1;
                   1336:
                   1337:                found = 0;
                   1338:                if (next->xoff < left && end > right)
                   1339:                        found = 1;
                   1340:                else if (next->xoff >= left && next->xoff <= right)
                   1341:                        found = 1;
                   1342:                else if (end >= left && end <= right)
                   1343:                        found = 1;
1.126     nicm     1344:                if (!found)
                   1345:                        continue;
                   1346:                list = xreallocarray(list, size + 1, sizeof *list);
                   1347:                list[size++] = next;
1.109     nicm     1348:        }
                   1349:
1.126     nicm     1350:        best = window_pane_choose_best(list, size);
                   1351:        free(list);
1.109     nicm     1352:        return (best);
1.45      nicm     1353: }
                   1354:
                   1355: /* Find the pane directly below another. */
                   1356: struct window_pane *
                   1357: window_pane_find_down(struct window_pane *wp)
                   1358: {
1.126     nicm     1359:        struct window_pane      *next, *best, **list;
                   1360:        u_int                    edge, left, right, end, size;
1.187     nicm     1361:        int                      status, found;
1.45      nicm     1362:
                   1363:        if (wp == NULL || !window_pane_visible(wp))
                   1364:                return (NULL);
1.187     nicm     1365:        status = options_get_number(wp->window->options, "pane-border-status");
1.126     nicm     1366:
                   1367:        list = NULL;
                   1368:        size = 0;
1.109     nicm     1369:
                   1370:        edge = wp->yoff + wp->sy + 1;
1.187     nicm     1371:        if (edge >= wp->window->sy - (status == 2 ? 1 : 0))
                   1372:                edge = (status == 1 ? 1 : 0);
1.45      nicm     1373:
                   1374:        left = wp->xoff;
1.109     nicm     1375:        right = wp->xoff + wp->sx;
1.45      nicm     1376:
1.109     nicm     1377:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1378:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1379:                        continue;
1.109     nicm     1380:                if (next->yoff != edge)
1.45      nicm     1381:                        continue;
1.109     nicm     1382:                end = next->xoff + next->sx - 1;
                   1383:
                   1384:                found = 0;
                   1385:                if (next->xoff < left && end > right)
                   1386:                        found = 1;
                   1387:                else if (next->xoff >= left && next->xoff <= right)
                   1388:                        found = 1;
                   1389:                else if (end >= left && end <= right)
                   1390:                        found = 1;
1.126     nicm     1391:                if (!found)
                   1392:                        continue;
                   1393:                list = xreallocarray(list, size + 1, sizeof *list);
                   1394:                list[size++] = next;
1.45      nicm     1395:        }
1.109     nicm     1396:
1.126     nicm     1397:        best = window_pane_choose_best(list, size);
                   1398:        free(list);
1.109     nicm     1399:        return (best);
1.45      nicm     1400: }
                   1401:
1.109     nicm     1402: /* Find the pane directly to the left of another. */
1.45      nicm     1403: struct window_pane *
                   1404: window_pane_find_left(struct window_pane *wp)
                   1405: {
1.126     nicm     1406:        struct window_pane      *next, *best, **list;
                   1407:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1408:        int                      found;
1.45      nicm     1409:
                   1410:        if (wp == NULL || !window_pane_visible(wp))
                   1411:                return (NULL);
1.126     nicm     1412:
                   1413:        list = NULL;
                   1414:        size = 0;
1.109     nicm     1415:
                   1416:        edge = wp->xoff;
                   1417:        if (edge == 0)
                   1418:                edge = wp->window->sx + 1;
1.45      nicm     1419:
                   1420:        top = wp->yoff;
1.109     nicm     1421:        bottom = wp->yoff + wp->sy;
1.45      nicm     1422:
1.109     nicm     1423:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1424:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1425:                        continue;
1.109     nicm     1426:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1427:                        continue;
1.109     nicm     1428:                end = next->yoff + next->sy - 1;
                   1429:
                   1430:                found = 0;
                   1431:                if (next->yoff < top && end > bottom)
                   1432:                        found = 1;
                   1433:                else if (next->yoff >= top && next->yoff <= bottom)
                   1434:                        found = 1;
                   1435:                else if (end >= top && end <= bottom)
                   1436:                        found = 1;
1.126     nicm     1437:                if (!found)
                   1438:                        continue;
                   1439:                list = xreallocarray(list, size + 1, sizeof *list);
                   1440:                list[size++] = next;
1.45      nicm     1441:        }
1.109     nicm     1442:
1.126     nicm     1443:        best = window_pane_choose_best(list, size);
                   1444:        free(list);
1.109     nicm     1445:        return (best);
1.45      nicm     1446: }
                   1447:
1.109     nicm     1448: /* Find the pane directly to the right of another. */
1.45      nicm     1449: struct window_pane *
                   1450: window_pane_find_right(struct window_pane *wp)
                   1451: {
1.126     nicm     1452:        struct window_pane      *next, *best, **list;
                   1453:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1454:        int                      found;
1.45      nicm     1455:
                   1456:        if (wp == NULL || !window_pane_visible(wp))
                   1457:                return (NULL);
1.126     nicm     1458:
                   1459:        list = NULL;
                   1460:        size = 0;
1.109     nicm     1461:
                   1462:        edge = wp->xoff + wp->sx + 1;
                   1463:        if (edge >= wp->window->sx)
                   1464:                edge = 0;
1.45      nicm     1465:
                   1466:        top = wp->yoff;
1.109     nicm     1467:        bottom = wp->yoff + wp->sy;
1.45      nicm     1468:
1.109     nicm     1469:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1470:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1471:                        continue;
1.109     nicm     1472:                if (next->xoff != edge)
1.45      nicm     1473:                        continue;
1.109     nicm     1474:                end = next->yoff + next->sy - 1;
                   1475:
                   1476:                found = 0;
                   1477:                if (next->yoff < top && end > bottom)
                   1478:                        found = 1;
                   1479:                else if (next->yoff >= top && next->yoff <= bottom)
                   1480:                        found = 1;
                   1481:                else if (end >= top && end <= bottom)
                   1482:                        found = 1;
1.126     nicm     1483:                if (!found)
                   1484:                        continue;
                   1485:                list = xreallocarray(list, size + 1, sizeof *list);
                   1486:                list[size++] = next;
1.109     nicm     1487:        }
                   1488:
1.126     nicm     1489:        best = window_pane_choose_best(list, size);
                   1490:        free(list);
1.109     nicm     1491:        return (best);
1.81      nicm     1492: }
                   1493:
                   1494: /* Clear alert flags for a winlink */
                   1495: void
                   1496: winlink_clear_flags(struct winlink *wl)
                   1497: {
1.174     nicm     1498:        struct winlink  *loop;
1.81      nicm     1499:
1.174     nicm     1500:        wl->window->flags &= ~WINDOW_ALERTFLAGS;
                   1501:        TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
                   1502:                if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
                   1503:                        loop->flags &= ~WINLINK_ALERTFLAGS;
                   1504:                        server_status_session(loop->session);
1.81      nicm     1505:                }
                   1506:        }
1.134     nicm     1507: }
                   1508:
1.184     nicm     1509: /* Shuffle window indexes up. */
1.134     nicm     1510: int
                   1511: winlink_shuffle_up(struct session *s, struct winlink *wl)
                   1512: {
                   1513:        int      idx, last;
                   1514:
                   1515:        idx = wl->idx + 1;
                   1516:
                   1517:        /* Find the next free index. */
                   1518:        for (last = idx; last < INT_MAX; last++) {
                   1519:                if (winlink_find_by_index(&s->windows, last) == NULL)
                   1520:                        break;
                   1521:        }
                   1522:        if (last == INT_MAX)
                   1523:                return (-1);
                   1524:
                   1525:        /* Move everything from last - 1 to idx up a bit. */
                   1526:        for (; last > idx; last--) {
                   1527:                wl = winlink_find_by_index(&s->windows, last - 1);
                   1528:                server_link_window(s, wl, s, last, 0, 0, NULL);
                   1529:                server_unlink_window(s, wl);
                   1530:        }
                   1531:
                   1532:        return (idx);
1.1       nicm     1533: }