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

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