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

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