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

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