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

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