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

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