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

1.179   ! nicm        1: /* $OpenBSD: window.c,v 1.178 2017/01/12 00:19:32 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.42      nicm      889:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      890:        case -1:
                    891:                wp->fd = -1;
                    892:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110     nicm      893:                free(cmd);
1.1       nicm      894:                return (-1);
                    895:        case 0:
1.147     nicm      896:                if (chdir(wp->cwd) != 0) {
                    897:                        if ((home = find_home()) == NULL || chdir(home) != 0)
                    898:                                chdir("/");
                    899:                }
1.25      nicm      900:
                    901:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    902:                        fatal("tcgetattr failed");
                    903:                if (tio != NULL)
                    904:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    905:                tio2.c_cc[VERASE] = '\177';
                    906:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    907:                        fatal("tcgetattr failed");
1.18      nicm      908:
1.56      nicm      909:                closefrom(STDERR_FILENO + 1);
                    910:
1.107     nicm      911:                if (path != NULL)
1.150     nicm      912:                        environ_set(env, "PATH", "%s", path);
                    913:                environ_set(env, "TMUX_PANE", "%%%u", wp->id);
1.47      nicm      914:                environ_push(env);
1.18      nicm      915:
1.54      nicm      916:                clear_signals(1);
1.1       nicm      917:                log_close();
                    918:
1.80      nicm      919:                setenv("SHELL", wp->shell, 1);
                    920:                ptr = strrchr(wp->shell, '/');
                    921:
1.110     nicm      922:                /*
                    923:                 * If given one argument, assume it should be passed to sh -c;
                    924:                 * with more than one argument, use execvp(). If there is no
                    925:                 * arguments, create a login shell.
                    926:                 */
                    927:                if (wp->argc > 0) {
                    928:                        if (wp->argc != 1) {
                    929:                                /* Copy to ensure argv ends in NULL. */
                    930:                                argvp = cmd_copy_argv(wp->argc, wp->argv);
                    931:                                execvp(argvp[0], argvp);
                    932:                                fatal("execvp failed");
                    933:                        }
                    934:                        first = wp->argv[0];
                    935:
1.80      nicm      936:                        if (ptr != NULL && *(ptr + 1) != '\0')
                    937:                                xasprintf(&argv0, "%s", ptr + 1);
                    938:                        else
                    939:                                xasprintf(&argv0, "%s", wp->shell);
1.110     nicm      940:                        execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8       nicm      941:                        fatal("execl failed");
                    942:                }
1.23      nicm      943:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      944:                        xasprintf(&argv0, "-%s", ptr + 1);
                    945:                else
1.23      nicm      946:                        xasprintf(&argv0, "-%s", wp->shell);
1.110     nicm      947:                execl(wp->shell, argv0, (char *)NULL);
1.1       nicm      948:                fatal("execl failed");
                    949:        }
                    950:
1.62      nicm      951:        setblocking(wp->fd, 0);
                    952:
1.110     nicm      953:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
                    954:            window_pane_error_callback, wp);
1.131     nicm      955:
1.166     nicm      956:        window_pane_set_watermark(wp, READ_FAST_SIZE);
1.37      nicm      957:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      958:
1.110     nicm      959:        free(cmd);
1.1       nicm      960:        return (0);
                    961: }
                    962:
1.166     nicm      963: static void
1.149     nicm      964: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37      nicm      965: {
1.131     nicm      966:        struct window_pane      *wp = data;
                    967:        struct evbuffer         *evb = wp->event->input;
1.166     nicm      968:        size_t                   size = EVBUFFER_LENGTH(evb);
1.131     nicm      969:        char                    *new_data;
1.158     nicm      970:        size_t                   new_size;
1.131     nicm      971:
1.166     nicm      972:        if (wp->wmark_size == READ_FAST_SIZE) {
                    973:                if (size > READ_FULL_SIZE)
                    974:                        wp->wmark_hits++;
                    975:                if (wp->wmark_hits == READ_CHANGE_HITS)
                    976:                        window_pane_set_watermark(wp, READ_SLOW_SIZE);
                    977:        } else if (wp->wmark_size == READ_SLOW_SIZE) {
                    978:                if (size < READ_EMPTY_SIZE)
                    979:                        wp->wmark_hits++;
                    980:                if (wp->wmark_hits == READ_CHANGE_HITS)
                    981:                        window_pane_set_watermark(wp, READ_FAST_SIZE);
                    982:        }
                    983:        log_debug("%%%u has %zu bytes (of %u, %u hits)", wp->id, size,
                    984:            wp->wmark_size, wp->wmark_hits);
1.131     nicm      985:
1.166     nicm      986:        new_size = size - wp->pipe_off;
1.46      nicm      987:        if (wp->pipe_fd != -1 && new_size > 0) {
1.155     nicm      988:                new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
1.46      nicm      989:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    990:        }
                    991:
                    992:        input_parse(wp);
1.37      nicm      993:
1.173     nicm      994:        wp->pipe_off = EVBUFFER_LENGTH(evb);
1.37      nicm      995: }
                    996:
1.166     nicm      997: static void
1.149     nicm      998: window_pane_error_callback(__unused struct bufferevent *bufev,
                    999:     __unused short what, void *data)
1.37      nicm     1000: {
                   1001:        struct window_pane *wp = data;
                   1002:
1.153     nicm     1003:        server_destroy_pane(wp, 1);
1.37      nicm     1004: }
                   1005:
                   1006: void
1.1       nicm     1007: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                   1008: {
                   1009:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm     1010:                return;
1.1       nicm     1011:        wp->sx = sx;
                   1012:        wp->sy = sy;
                   1013:
1.89      nicm     1014:        screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1       nicm     1015:        if (wp->mode != NULL)
                   1016:                wp->mode->resize(wp, sx, sy);
1.95      nicm     1017:
                   1018:        wp->flags |= PANE_RESIZE;
1.44      nicm     1019: }
                   1020:
                   1021: /*
                   1022:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                   1023:  * history is not updated
                   1024:  */
                   1025: void
1.87      nicm     1026: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
                   1027:     int cursor)
1.44      nicm     1028: {
                   1029:        struct screen   *s = &wp->base;
                   1030:        u_int            sx, sy;
                   1031:
                   1032:        if (wp->saved_grid != NULL)
                   1033:                return;
1.146     nicm     1034:        if (!options_get_number(wp->window->options, "alternate-screen"))
1.44      nicm     1035:                return;
                   1036:        sx = screen_size_x(s);
                   1037:        sy = screen_size_y(s);
                   1038:
                   1039:        wp->saved_grid = grid_create(sx, sy, 0);
                   1040:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87      nicm     1041:        if (cursor) {
                   1042:                wp->saved_cx = s->cx;
                   1043:                wp->saved_cy = s->cy;
                   1044:        }
1.44      nicm     1045:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                   1046:
1.170     nicm     1047:        grid_view_clear(s->grid, 0, 0, sx, sy, 8);
1.44      nicm     1048:
                   1049:        wp->base.grid->flags &= ~GRID_HISTORY;
                   1050:
                   1051:        wp->flags |= PANE_REDRAW;
                   1052: }
                   1053:
                   1054: /* Exit alternate screen mode and restore the copied grid. */
                   1055: void
1.87      nicm     1056: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
                   1057:     int cursor)
1.44      nicm     1058: {
                   1059:        struct screen   *s = &wp->base;
                   1060:        u_int            sx, sy;
                   1061:
                   1062:        if (wp->saved_grid == NULL)
                   1063:                return;
1.146     nicm     1064:        if (!options_get_number(wp->window->options, "alternate-screen"))
1.44      nicm     1065:                return;
                   1066:        sx = screen_size_x(s);
                   1067:        sy = screen_size_y(s);
                   1068:
                   1069:        /*
                   1070:         * If the current size is bigger, temporarily resize to the old size
                   1071:         * before copying back.
                   1072:         */
                   1073:        if (sy > wp->saved_grid->sy)
1.89      nicm     1074:                screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44      nicm     1075:
                   1076:        /* Restore the grid, cursor position and cell. */
                   1077:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87      nicm     1078:        if (cursor)
                   1079:                s->cx = wp->saved_cx;
1.44      nicm     1080:        if (s->cx > screen_size_x(s) - 1)
                   1081:                s->cx = screen_size_x(s) - 1;
1.87      nicm     1082:        if (cursor)
                   1083:                s->cy = wp->saved_cy;
1.44      nicm     1084:        if (s->cy > screen_size_y(s) - 1)
                   1085:                s->cy = screen_size_y(s) - 1;
                   1086:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                   1087:
                   1088:        /*
                   1089:         * Turn history back on (so resize can use it) and then resize back to
                   1090:         * the current size.
                   1091:         */
                   1092:        wp->base.grid->flags |= GRID_HISTORY;
1.89      nicm     1093:        if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
                   1094:                screen_resize(s, sx, sy, 1);
1.44      nicm     1095:
                   1096:        grid_destroy(wp->saved_grid);
                   1097:        wp->saved_grid = NULL;
                   1098:
1.177     nicm     1099:        wp->flags |= PANE_REDRAW;
                   1100: }
                   1101:
                   1102: void
                   1103: window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
                   1104: {
                   1105:        if (n > 0xff)
                   1106:                return;
                   1107:
                   1108:        if (wp->palette == NULL)
                   1109:                wp->palette = xcalloc(0x100, sizeof *wp->palette);
                   1110:
                   1111:        wp->palette[n] = colour;
                   1112:        wp->flags |= PANE_REDRAW;
                   1113: }
                   1114:
                   1115: void
                   1116: window_pane_unset_palette(struct window_pane *wp, u_int n)
                   1117: {
                   1118:        if (n > 0xff || wp->palette == NULL)
                   1119:                return;
                   1120:
                   1121:        wp->palette[n] = 0;
                   1122:        wp->flags |= PANE_REDRAW;
                   1123: }
                   1124:
                   1125: void
                   1126: window_pane_reset_palette(struct window_pane *wp)
                   1127: {
                   1128:        if (wp->palette == NULL)
                   1129:                return;
                   1130:
                   1131:        free(wp->palette);
                   1132:        wp->palette = NULL;
1.44      nicm     1133:        wp->flags |= PANE_REDRAW;
1.1       nicm     1134: }
                   1135:
1.162     nicm     1136: static void
                   1137: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
                   1138: {
                   1139:        struct window_pane      *wp = arg;
                   1140:        struct timeval           tv = { .tv_sec = 10 };
                   1141:        int                      n = 0;
                   1142:
                   1143:        evtimer_del(&wp->modetimer);
                   1144:        evtimer_add(&wp->modetimer, &tv);
                   1145:
                   1146:        log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
                   1147:
                   1148:        if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
                   1149:                if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
                   1150:                        window_pane_reset_mode(wp);
                   1151:        }
                   1152: }
                   1153:
1.1       nicm     1154: int
                   1155: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                   1156: {
                   1157:        struct screen   *s;
1.162     nicm     1158:        struct timeval   tv = { .tv_sec = 10 };
1.1       nicm     1159:
1.15      nicm     1160:        if (wp->mode != NULL)
1.1       nicm     1161:                return (1);
                   1162:        wp->mode = mode;
                   1163:
1.162     nicm     1164:        wp->modelast = time(NULL);
                   1165:        evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
                   1166:        evtimer_add(&wp->modetimer, &tv);
                   1167:
1.1       nicm     1168:        if ((s = wp->mode->init(wp)) != NULL)
                   1169:                wp->screen = s;
1.142     nicm     1170:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1171:
                   1172:        server_status_window(wp->window);
1.1       nicm     1173:        return (0);
                   1174: }
                   1175:
                   1176: void
                   1177: window_pane_reset_mode(struct window_pane *wp)
                   1178: {
                   1179:        if (wp->mode == NULL)
                   1180:                return;
                   1181:
1.162     nicm     1182:        evtimer_del(&wp->modetimer);
                   1183:
1.1       nicm     1184:        wp->mode->free(wp);
                   1185:        wp->mode = NULL;
1.168     nicm     1186:        wp->modeprefix = 1;
1.1       nicm     1187:
                   1188:        wp->screen = &wp->base;
1.142     nicm     1189:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1190:
                   1191:        server_status_window(wp->window);
1.1       nicm     1192: }
                   1193:
                   1194: void
1.118     nicm     1195: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.148     nicm     1196:     key_code key, struct mouse_event *m)
1.1       nicm     1197: {
1.27      nicm     1198:        struct window_pane      *wp2;
1.3       nicm     1199:
1.118     nicm     1200:        if (KEYC_IS_MOUSE(key) && m == NULL)
                   1201:                return;
                   1202:
1.1       nicm     1203:        if (wp->mode != NULL) {
1.162     nicm     1204:                wp->modelast = time(NULL);
1.1       nicm     1205:                if (wp->mode->key != NULL)
1.118     nicm     1206:                        wp->mode->key(wp, c, s, key, m);
1.27      nicm     1207:                return;
1.30      nicm     1208:        }
1.27      nicm     1209:
1.113     nicm     1210:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30      nicm     1211:                return;
1.113     nicm     1212:
1.118     nicm     1213:        input_key(wp, key, m);
                   1214:
                   1215:        if (KEYC_IS_MOUSE(key))
                   1216:                return;
1.146     nicm     1217:        if (options_get_number(wp->window->options, "synchronize-panes")) {
1.27      nicm     1218:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1219:                        if (wp2 == wp || wp2->mode != NULL)
                   1220:                                continue;
1.154     nicm     1221:                        if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF)
                   1222:                                continue;
                   1223:                        if (window_pane_visible(wp2))
1.118     nicm     1224:                                input_key(wp2, key, NULL);
1.27      nicm     1225:                }
                   1226:        }
1.10      nicm     1227: }
                   1228:
                   1229: int
1.175     nicm     1230: window_pane_outside(struct window_pane *wp)
1.10      nicm     1231: {
                   1232:        struct window   *w = wp->window;
                   1233:
                   1234:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
1.175     nicm     1235:                return (1);
1.10      nicm     1236:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
1.175     nicm     1237:                return (1);
                   1238:        return (0);
                   1239: }
                   1240:
                   1241: int
                   1242: window_pane_visible(struct window_pane *wp)
                   1243: {
                   1244:        if (wp->layout_cell == NULL)
1.10      nicm     1245:                return (0);
1.175     nicm     1246:        return (!window_pane_outside(wp));
1.1       nicm     1247: }
                   1248:
                   1249: char *
1.108     nicm     1250: window_pane_search(struct window_pane *wp, const char *searchstr,
                   1251:     u_int *lineno)
1.1       nicm     1252: {
1.4       nicm     1253:        struct screen   *s = &wp->base;
1.5       nicm     1254:        char            *newsearchstr, *line, *msg;
1.4       nicm     1255:        u_int            i;
                   1256:
1.5       nicm     1257:        msg = NULL;
                   1258:        xasprintf(&newsearchstr, "*%s*", searchstr);
                   1259:
1.4       nicm     1260:        for (i = 0; i < screen_size_y(s); i++) {
                   1261:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm     1262:                if (fnmatch(newsearchstr, line, 0) == 0) {
                   1263:                        msg = line;
                   1264:                        if (lineno != NULL)
                   1265:                                *lineno = i;
                   1266:                        break;
                   1267:                }
1.82      nicm     1268:                free(line);
1.4       nicm     1269:        }
1.5       nicm     1270:
1.82      nicm     1271:        free(newsearchstr);
1.5       nicm     1272:        return (msg);
1.45      nicm     1273: }
                   1274:
1.109     nicm     1275: /* Get MRU pane from a list. */
1.166     nicm     1276: static struct window_pane *
1.126     nicm     1277: window_pane_choose_best(struct window_pane **list, u_int size)
1.109     nicm     1278: {
                   1279:        struct window_pane      *next, *best;
                   1280:        u_int                    i;
                   1281:
1.126     nicm     1282:        if (size == 0)
1.109     nicm     1283:                return (NULL);
                   1284:
1.126     nicm     1285:        best = list[0];
                   1286:        for (i = 1; i < size; i++) {
                   1287:                next = list[i];
1.109     nicm     1288:                if (next->active_point > best->active_point)
                   1289:                        best = next;
                   1290:        }
                   1291:        return (best);
                   1292: }
                   1293:
                   1294: /*
                   1295:  * Find the pane directly above another. We build a list of those adjacent to
                   1296:  * top edge and then choose the best.
                   1297:  */
1.45      nicm     1298: struct window_pane *
                   1299: window_pane_find_up(struct window_pane *wp)
                   1300: {
1.126     nicm     1301:        struct window_pane      *next, *best, **list;
                   1302:        u_int                    edge, left, right, end, size;
1.109     nicm     1303:        int                      found;
1.45      nicm     1304:
                   1305:        if (wp == NULL || !window_pane_visible(wp))
                   1306:                return (NULL);
1.126     nicm     1307:
                   1308:        list = NULL;
                   1309:        size = 0;
1.109     nicm     1310:
                   1311:        edge = wp->yoff;
                   1312:        if (edge == 0)
                   1313:                edge = wp->window->sy + 1;
1.45      nicm     1314:
                   1315:        left = wp->xoff;
1.109     nicm     1316:        right = wp->xoff + wp->sx;
1.45      nicm     1317:
1.109     nicm     1318:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1319:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1320:                        continue;
1.109     nicm     1321:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1322:                        continue;
1.109     nicm     1323:                end = next->xoff + next->sx - 1;
                   1324:
                   1325:                found = 0;
                   1326:                if (next->xoff < left && end > right)
                   1327:                        found = 1;
                   1328:                else if (next->xoff >= left && next->xoff <= right)
                   1329:                        found = 1;
                   1330:                else if (end >= left && end <= right)
                   1331:                        found = 1;
1.126     nicm     1332:                if (!found)
                   1333:                        continue;
                   1334:                list = xreallocarray(list, size + 1, sizeof *list);
                   1335:                list[size++] = next;
1.109     nicm     1336:        }
                   1337:
1.126     nicm     1338:        best = window_pane_choose_best(list, size);
                   1339:        free(list);
1.109     nicm     1340:        return (best);
1.45      nicm     1341: }
                   1342:
                   1343: /* Find the pane directly below another. */
                   1344: struct window_pane *
                   1345: window_pane_find_down(struct window_pane *wp)
                   1346: {
1.126     nicm     1347:        struct window_pane      *next, *best, **list;
                   1348:        u_int                    edge, left, right, end, size;
1.109     nicm     1349:        int                      found;
1.45      nicm     1350:
                   1351:        if (wp == NULL || !window_pane_visible(wp))
                   1352:                return (NULL);
1.126     nicm     1353:
                   1354:        list = NULL;
                   1355:        size = 0;
1.109     nicm     1356:
                   1357:        edge = wp->yoff + wp->sy + 1;
                   1358:        if (edge >= wp->window->sy)
                   1359:                edge = 0;
1.45      nicm     1360:
                   1361:        left = wp->xoff;
1.109     nicm     1362:        right = wp->xoff + wp->sx;
1.45      nicm     1363:
1.109     nicm     1364:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1365:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1366:                        continue;
1.109     nicm     1367:                if (next->yoff != edge)
1.45      nicm     1368:                        continue;
1.109     nicm     1369:                end = next->xoff + next->sx - 1;
                   1370:
                   1371:                found = 0;
                   1372:                if (next->xoff < left && end > right)
                   1373:                        found = 1;
                   1374:                else if (next->xoff >= left && next->xoff <= right)
                   1375:                        found = 1;
                   1376:                else if (end >= left && end <= right)
                   1377:                        found = 1;
1.126     nicm     1378:                if (!found)
                   1379:                        continue;
                   1380:                list = xreallocarray(list, size + 1, sizeof *list);
                   1381:                list[size++] = next;
1.45      nicm     1382:        }
1.109     nicm     1383:
1.126     nicm     1384:        best = window_pane_choose_best(list, size);
                   1385:        free(list);
1.109     nicm     1386:        return (best);
1.45      nicm     1387: }
                   1388:
1.109     nicm     1389: /* Find the pane directly to the left of another. */
1.45      nicm     1390: struct window_pane *
                   1391: window_pane_find_left(struct window_pane *wp)
                   1392: {
1.126     nicm     1393:        struct window_pane      *next, *best, **list;
                   1394:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1395:        int                      found;
1.45      nicm     1396:
                   1397:        if (wp == NULL || !window_pane_visible(wp))
                   1398:                return (NULL);
1.126     nicm     1399:
                   1400:        list = NULL;
                   1401:        size = 0;
1.109     nicm     1402:
                   1403:        edge = wp->xoff;
                   1404:        if (edge == 0)
                   1405:                edge = wp->window->sx + 1;
1.45      nicm     1406:
                   1407:        top = wp->yoff;
1.109     nicm     1408:        bottom = wp->yoff + wp->sy;
1.45      nicm     1409:
1.109     nicm     1410:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1411:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1412:                        continue;
1.109     nicm     1413:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1414:                        continue;
1.109     nicm     1415:                end = next->yoff + next->sy - 1;
                   1416:
                   1417:                found = 0;
                   1418:                if (next->yoff < top && end > bottom)
                   1419:                        found = 1;
                   1420:                else if (next->yoff >= top && next->yoff <= bottom)
                   1421:                        found = 1;
                   1422:                else if (end >= top && end <= bottom)
                   1423:                        found = 1;
1.126     nicm     1424:                if (!found)
                   1425:                        continue;
                   1426:                list = xreallocarray(list, size + 1, sizeof *list);
                   1427:                list[size++] = next;
1.45      nicm     1428:        }
1.109     nicm     1429:
1.126     nicm     1430:        best = window_pane_choose_best(list, size);
                   1431:        free(list);
1.109     nicm     1432:        return (best);
1.45      nicm     1433: }
                   1434:
1.109     nicm     1435: /* Find the pane directly to the right of another. */
1.45      nicm     1436: struct window_pane *
                   1437: window_pane_find_right(struct window_pane *wp)
                   1438: {
1.126     nicm     1439:        struct window_pane      *next, *best, **list;
                   1440:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1441:        int                      found;
1.45      nicm     1442:
                   1443:        if (wp == NULL || !window_pane_visible(wp))
                   1444:                return (NULL);
1.126     nicm     1445:
                   1446:        list = NULL;
                   1447:        size = 0;
1.109     nicm     1448:
                   1449:        edge = wp->xoff + wp->sx + 1;
                   1450:        if (edge >= wp->window->sx)
                   1451:                edge = 0;
1.45      nicm     1452:
                   1453:        top = wp->yoff;
1.109     nicm     1454:        bottom = wp->yoff + wp->sy;
1.45      nicm     1455:
1.109     nicm     1456:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1457:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1458:                        continue;
1.109     nicm     1459:                if (next->xoff != edge)
1.45      nicm     1460:                        continue;
1.109     nicm     1461:                end = next->yoff + next->sy - 1;
                   1462:
                   1463:                found = 0;
                   1464:                if (next->yoff < top && end > bottom)
                   1465:                        found = 1;
                   1466:                else if (next->yoff >= top && next->yoff <= bottom)
                   1467:                        found = 1;
                   1468:                else if (end >= top && end <= bottom)
                   1469:                        found = 1;
1.126     nicm     1470:                if (!found)
                   1471:                        continue;
                   1472:                list = xreallocarray(list, size + 1, sizeof *list);
                   1473:                list[size++] = next;
1.109     nicm     1474:        }
                   1475:
1.126     nicm     1476:        best = window_pane_choose_best(list, size);
                   1477:        free(list);
1.109     nicm     1478:        return (best);
1.81      nicm     1479: }
                   1480:
                   1481: /* Clear alert flags for a winlink */
                   1482: void
                   1483: winlink_clear_flags(struct winlink *wl)
                   1484: {
1.174     nicm     1485:        struct winlink  *loop;
1.81      nicm     1486:
1.174     nicm     1487:        wl->window->flags &= ~WINDOW_ALERTFLAGS;
                   1488:        TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
                   1489:                if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
                   1490:                        loop->flags &= ~WINLINK_ALERTFLAGS;
                   1491:                        server_status_session(loop->session);
1.81      nicm     1492:                }
                   1493:        }
1.134     nicm     1494: }
                   1495:
                   1496: int
                   1497: winlink_shuffle_up(struct session *s, struct winlink *wl)
                   1498: {
                   1499:        int      idx, last;
                   1500:
                   1501:        idx = wl->idx + 1;
                   1502:
                   1503:        /* Find the next free index. */
                   1504:        for (last = idx; last < INT_MAX; last++) {
                   1505:                if (winlink_find_by_index(&s->windows, last) == NULL)
                   1506:                        break;
                   1507:        }
                   1508:        if (last == INT_MAX)
                   1509:                return (-1);
                   1510:
                   1511:        /* Move everything from last - 1 to idx up a bit. */
                   1512:        for (; last > idx; last--) {
                   1513:                wl = winlink_find_by_index(&s->windows, last - 1);
                   1514:                server_link_window(s, wl, s, last, 0, 0, NULL);
                   1515:                server_unlink_window(s, wl);
                   1516:        }
                   1517:
                   1518:        return (idx);
1.178     nicm     1519: }
                   1520:
                   1521: int
                   1522: window_pane_get_palette(const struct window_pane *wp, int c)
                   1523: {
                   1524:        int     new;
                   1525:
                   1526:        if (wp == NULL || wp->palette == NULL)
                   1527:                return (-1);
                   1528:
                   1529:        new = -1;
                   1530:        if (c < 8)
                   1531:                new = wp->palette[c];
                   1532:        else if (c >= 90 && c <= 97)
1.179   ! nicm     1533:                new = wp->palette[8 + c - 90];
1.178     nicm     1534:        else if (c & COLOUR_FLAG_256)
                   1535:                new = wp->palette[c & ~COLOUR_FLAG_256];
                   1536:        if (new == 0)
                   1537:                return (-1);
                   1538:        return (new);
1.1       nicm     1539: }