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

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