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

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