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

1.273   ! nicm        1: /* $OpenBSD: window.c,v 1.272 2021/06/10 07:33:41 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:
1.236     nicm       22: #include <ctype.h>
1.1       nicm       23: #include <errno.h>
                     24: #include <fcntl.h>
1.5       nicm       25: #include <fnmatch.h>
1.232     nicm       26: #include <regex.h>
1.203     nicm       27: #include <signal.h>
1.1       nicm       28: #include <stdint.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
1.163     nicm       31: #include <time.h>
1.1       nicm       32: #include <unistd.h>
                     33: #include <util.h>
1.198     nicm       34: #include <vis.h>
1.1       nicm       35:
                     36: #include "tmux.h"
                     37:
                     38: /*
1.14      nicm       39:  * Each window is attached to a number of panes, each of which is a pty. This
1.1       nicm       40:  * file contains code to handle them.
                     41:  *
                     42:  * A pane has two buffers attached, these are filled and emptied by the main
                     43:  * server poll loop. Output data is received from pty's in screen format,
                     44:  * translated and returned as a series of escape sequences and strings via
                     45:  * input_parse (in input.c). Input data is received as key codes and written
                     46:  * directly via input_key.
                     47:  *
                     48:  * Each pane also has a "virtual" screen (screen.c) which contains the current
                     49:  * state and is redisplayed when the window is reattached to a client.
                     50:  *
                     51:  * Windows are stored directly on a global array and wrapped in any number of
                     52:  * winlink structs to be linked onto local session RB trees. A reference count
                     53:  * is maintained and a window removed from the global list and destroyed when
                     54:  * it reaches zero.
                     55:  */
1.124     nicm       56:
1.1       nicm       57: /* Global window list. */
                     58: struct windows windows;
                     59:
1.64      nicm       60: /* Global panes tree. */
                     61: struct window_pane_tree all_window_panes;
1.166     nicm       62: static u_int   next_window_pane_id;
                     63: static u_int   next_window_id;
                     64: static u_int   next_active_point;
1.37      nicm       65:
1.228     nicm       66: struct window_pane_input_data {
                     67:        struct cmdq_item        *item;
                     68:        u_int                    wp;
                     69: };
                     70:
1.169     nicm       71: static struct window_pane *window_pane_create(struct window *, u_int, u_int,
                     72:                    u_int);
                     73: static void    window_pane_destroy(struct window_pane *);
                     74:
1.122     nicm       75: RB_GENERATE(windows, window, entry, window_cmp);
1.169     nicm       76: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
                     77: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
1.122     nicm       78:
                     79: int
                     80: window_cmp(struct window *w1, struct window *w2)
                     81: {
                     82:        return (w1->id - w2->id);
                     83: }
                     84:
1.1       nicm       85: int
                     86: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
                     87: {
                     88:        return (wl1->idx - wl2->idx);
1.12      nicm       89: }
                     90:
1.64      nicm       91: int
                     92: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
                     93: {
                     94:        return (wp1->id - wp2->id);
                     95: }
                     96:
1.12      nicm       97: struct winlink *
                     98: winlink_find_by_window(struct winlinks *wwl, struct window *w)
                     99: {
                    100:        struct winlink  *wl;
                    101:
                    102:        RB_FOREACH(wl, winlinks, wwl) {
                    103:                if (wl->window == w)
                    104:                        return (wl);
                    105:        }
                    106:
                    107:        return (NULL);
1.1       nicm      108: }
                    109:
                    110: struct winlink *
                    111: winlink_find_by_index(struct winlinks *wwl, int idx)
                    112: {
                    113:        struct winlink  wl;
                    114:
                    115:        if (idx < 0)
                    116:                fatalx("bad index");
                    117:
                    118:        wl.idx = idx;
                    119:        return (RB_FIND(winlinks, wwl, &wl));
                    120: }
                    121:
1.71      nicm      122: struct winlink *
                    123: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
                    124: {
                    125:        struct winlink *wl;
                    126:
                    127:        RB_FOREACH(wl, winlinks, wwl) {
                    128:                if (wl->window->id == id)
                    129:                        return (wl);
                    130:        }
1.78      nicm      131:        return (NULL);
1.71      nicm      132: }
                    133:
1.169     nicm      134: static int
1.22      nicm      135: winlink_next_index(struct winlinks *wwl, int idx)
1.1       nicm      136: {
1.22      nicm      137:        int     i;
1.1       nicm      138:
1.22      nicm      139:        i = idx;
                    140:        do {
1.1       nicm      141:                if (winlink_find_by_index(wwl, i) == NULL)
                    142:                        return (i);
1.22      nicm      143:                if (i == INT_MAX)
                    144:                        i = 0;
                    145:                else
                    146:                        i++;
                    147:        } while (i != idx);
                    148:        return (-1);
1.1       nicm      149: }
                    150:
                    151: u_int
                    152: winlink_count(struct winlinks *wwl)
                    153: {
                    154:        struct winlink  *wl;
                    155:        u_int            n;
                    156:
                    157:        n = 0;
                    158:        RB_FOREACH(wl, winlinks, wwl)
                    159:                n++;
                    160:
                    161:        return (n);
                    162: }
                    163:
                    164: struct winlink *
1.63      nicm      165: winlink_add(struct winlinks *wwl, int idx)
1.1       nicm      166: {
                    167:        struct winlink  *wl;
                    168:
1.22      nicm      169:        if (idx < 0) {
                    170:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
                    171:                        return (NULL);
                    172:        } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1       nicm      173:                return (NULL);
                    174:
                    175:        wl = xcalloc(1, sizeof *wl);
                    176:        wl->idx = idx;
                    177:        RB_INSERT(winlinks, wwl, wl);
                    178:
1.63      nicm      179:        return (wl);
                    180: }
                    181:
                    182: void
                    183: winlink_set_window(struct winlink *wl, struct window *w)
                    184: {
1.174     nicm      185:        if (wl->window != NULL) {
                    186:                TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
1.192     nicm      187:                window_remove_ref(wl->window, __func__);
1.174     nicm      188:        }
                    189:        TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
1.63      nicm      190:        wl->window = w;
1.192     nicm      191:        window_add_ref(w, __func__);
1.1       nicm      192: }
                    193:
                    194: void
                    195: winlink_remove(struct winlinks *wwl, struct winlink *wl)
                    196: {
                    197:        struct window   *w = wl->window;
                    198:
1.174     nicm      199:        if (w != NULL) {
                    200:                TAILQ_REMOVE(&w->winlinks, wl, wentry);
1.192     nicm      201:                window_remove_ref(w, __func__);
1.174     nicm      202:        }
                    203:
1.1       nicm      204:        RB_REMOVE(winlinks, wwl, wl);
1.82      nicm      205:        free(wl);
1.1       nicm      206: }
                    207:
                    208: struct winlink *
1.41      nicm      209: winlink_next(struct winlink *wl)
1.1       nicm      210: {
                    211:        return (RB_NEXT(winlinks, wwl, wl));
                    212: }
                    213:
                    214: struct winlink *
1.41      nicm      215: winlink_previous(struct winlink *wl)
1.1       nicm      216: {
                    217:        return (RB_PREV(winlinks, wwl, wl));
1.52      nicm      218: }
                    219:
                    220: struct winlink *
1.53      nicm      221: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      222: {
                    223:        for (; n > 0; n--) {
                    224:                if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53      nicm      225:                        wl = RB_MIN(winlinks, &s->windows);
1.52      nicm      226:        }
                    227:
                    228:        return (wl);
                    229: }
                    230:
                    231: struct winlink *
1.53      nicm      232: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      233: {
                    234:        for (; n > 0; n--) {
                    235:                if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53      nicm      236:                        wl = RB_MAX(winlinks, &s->windows);
1.52      nicm      237:        }
                    238:
                    239:        return (wl);
1.1       nicm      240: }
                    241:
                    242: void
                    243: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    244: {
                    245:        if (wl == NULL)
                    246:                return;
                    247:
                    248:        winlink_stack_remove(stack, wl);
1.28      nicm      249:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1       nicm      250: }
                    251:
                    252: void
                    253: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    254: {
                    255:        struct winlink  *wl2;
                    256:
                    257:        if (wl == NULL)
                    258:                return;
1.42      nicm      259:
1.28      nicm      260:        TAILQ_FOREACH(wl2, stack, sentry) {
1.1       nicm      261:                if (wl2 == wl) {
1.28      nicm      262:                        TAILQ_REMOVE(stack, wl, sentry);
1.1       nicm      263:                        return;
                    264:                }
                    265:        }
                    266: }
                    267:
                    268: struct window *
1.125     nicm      269: window_find_by_id_str(const char *s)
1.123     nicm      270: {
                    271:        const char      *errstr;
                    272:        u_int            id;
                    273:
                    274:        if (*s != '@')
                    275:                return (NULL);
                    276:
                    277:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    278:        if (errstr != NULL)
                    279:                return (NULL);
                    280:        return (window_find_by_id(id));
                    281: }
                    282:
                    283: struct window *
1.71      nicm      284: window_find_by_id(u_int id)
                    285: {
1.122     nicm      286:        struct window   w;
1.71      nicm      287:
1.122     nicm      288:        w.id = id;
                    289:        return (RB_FIND(windows, &windows, &w));
1.71      nicm      290: }
                    291:
1.143     nicm      292: void
                    293: window_update_activity(struct window *w)
                    294: {
                    295:        gettimeofday(&w->activity_time, NULL);
                    296:        alerts_queue(w, WINDOW_ACTIVITY);
                    297: }
                    298:
1.71      nicm      299: struct window *
1.245     nicm      300: window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel)
1.1       nicm      301: {
                    302:        struct window   *w;
                    303:
1.245     nicm      304:        if (xpixel == 0)
                    305:                xpixel = DEFAULT_XPIXEL;
                    306:        if (ypixel == 0)
                    307:                ypixel = DEFAULT_YPIXEL;
                    308:
1.38      nicm      309:        w = xcalloc(1, sizeof *w);
1.244     nicm      310:        w->name = xstrdup("");
1.234     nicm      311:        w->flags = 0;
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.245     nicm      321:        w->xpixel = xpixel;
                    322:        w->ypixel = ypixel;
1.133     nicm      323:
1.146     nicm      324:        w->options = options_create(global_w_options);
1.1       nicm      325:
                    326:        w->references = 0;
1.174     nicm      327:        TAILQ_INIT(&w->winlinks);
1.1       nicm      328:
1.122     nicm      329:        w->id = next_window_id++;
1.129     nicm      330:        RB_INSERT(windows, &windows, w);
1.122     nicm      331:
1.143     nicm      332:        window_update_activity(w);
                    333:
1.271     nicm      334:        log_debug("%s: @%u create %ux%u (%ux%u)", __func__, w->id, sx, sy,
                    335:            w->xpixel, w->ypixel);
1.1       nicm      336:        return (w);
                    337: }
                    338:
1.240     nicm      339: static void
1.1       nicm      340: window_destroy(struct window *w)
                    341: {
1.192     nicm      342:        log_debug("window @%u destroyed (%d references)", w->id, w->references);
1.174     nicm      343:
1.122     nicm      344:        RB_REMOVE(windows, &windows, w);
1.1       nicm      345:
1.14      nicm      346:        if (w->layout_root != NULL)
1.135     nicm      347:                layout_free_cell(w->layout_root);
                    348:        if (w->saved_layout_root != NULL)
                    349:                layout_free_cell(w->saved_layout_root);
1.127     nicm      350:        free(w->old_layout);
1.139     nicm      351:
1.227     nicm      352:        window_destroy_panes(w);
                    353:
1.141     nicm      354:        if (event_initialized(&w->name_event))
                    355:                evtimer_del(&w->name_event);
1.38      nicm      356:
1.143     nicm      357:        if (event_initialized(&w->alerts_timer))
                    358:                evtimer_del(&w->alerts_timer);
1.213     nicm      359:        if (event_initialized(&w->offset_timer))
                    360:                event_del(&w->offset_timer);
1.143     nicm      361:
1.146     nicm      362:        options_free(w->options);
1.1       nicm      363:
1.82      nicm      364:        free(w->name);
                    365:        free(w);
1.84      nicm      366: }
                    367:
1.200     nicm      368: int
                    369: window_pane_destroy_ready(struct window_pane *wp)
                    370: {
1.201     nicm      371:        int     n;
                    372:
                    373:        if (wp->pipe_fd != -1) {
                    374:                if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
                    375:                        return (0);
                    376:                if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0)
                    377:                        return (0);
                    378:        }
1.200     nicm      379:
                    380:        if (~wp->flags & PANE_EXITED)
                    381:                return (0);
                    382:        return (1);
                    383: }
                    384:
1.84      nicm      385: void
1.192     nicm      386: window_add_ref(struct window *w, const char *from)
                    387: {
                    388:        w->references++;
                    389:        log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
                    390: }
                    391:
                    392: void
                    393: window_remove_ref(struct window *w, const char *from)
1.84      nicm      394: {
                    395:        w->references--;
1.192     nicm      396:        log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
                    397:
1.84      nicm      398:        if (w->references == 0)
                    399:                window_destroy(w);
1.72      nicm      400: }
                    401:
                    402: void
                    403: window_set_name(struct window *w, const char *new_name)
                    404: {
1.82      nicm      405:        free(w->name);
1.198     nicm      406:        utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.172     nicm      407:        notify_window("window-renamed", w);
1.1       nicm      408: }
                    409:
1.15      nicm      410: void
1.245     nicm      411: window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
1.1       nicm      412: {
1.245     nicm      413:        if (xpixel == 0)
                    414:                xpixel = DEFAULT_XPIXEL;
                    415:        if (ypixel == 0)
                    416:                ypixel = DEFAULT_YPIXEL;
                    417:
                    418:        log_debug("%s: @%u resize %ux%u (%ux%u)", __func__, w->id, sx, sy,
1.249     nicm      419:            xpixel == -1 ? w->xpixel : (u_int)xpixel,
                    420:            ypixel == -1 ? w->ypixel : (u_int)ypixel);
1.1       nicm      421:        w->sx = sx;
                    422:        w->sy = sy;
1.245     nicm      423:        if (xpixel != -1)
                    424:                w->xpixel = xpixel;
                    425:        if (ypixel != -1)
                    426:                w->ypixel = ypixel;
                    427: }
                    428:
                    429: void
1.272     nicm      430: window_pane_send_resize(struct window_pane *wp, u_int sx, u_int sy)
1.245     nicm      431: {
                    432:        struct window   *w = wp->window;
                    433:        struct winsize   ws;
                    434:
                    435:        if (wp->fd == -1)
                    436:                return;
1.265     nicm      437:
1.272     nicm      438:        log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, sx, sy);
1.245     nicm      439:
                    440:        memset(&ws, 0, sizeof ws);
1.272     nicm      441:        ws.ws_col = sx;
1.262     nicm      442:        ws.ws_row = sy;
1.245     nicm      443:        ws.ws_xpixel = w->xpixel * ws.ws_col;
                    444:        ws.ws_ypixel = w->ypixel * ws.ws_row;
                    445:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
                    446:                fatal("ioctl failed");
1.1       nicm      447: }
                    448:
1.114     nicm      449: int
1.118     nicm      450: window_has_pane(struct window *w, struct window_pane *wp)
                    451: {
                    452:        struct window_pane      *wp1;
                    453:
                    454:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    455:                if (wp1 == wp)
                    456:                        return (1);
                    457:        }
                    458:        return (0);
                    459: }
                    460:
                    461: int
1.226     nicm      462: window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
1.1       nicm      463: {
1.226     nicm      464:        log_debug("%s: pane %%%u", __func__, wp->id);
                    465:
1.59      nicm      466:        if (wp == w->active)
1.114     nicm      467:                return (0);
1.58      nicm      468:        w->last = w->active;
1.226     nicm      469:
1.1       nicm      470:        w->active = wp;
1.109     nicm      471:        w->active->active_point = next_active_point++;
1.136     nicm      472:        w->active->flags |= PANE_CHANGED;
1.226     nicm      473:
1.213     nicm      474:        tty_update_window_offset(w);
1.226     nicm      475:
                    476:        if (notify)
                    477:                notify_window("window-pane-changed", w);
1.114     nicm      478:        return (1);
1.145     nicm      479: }
                    480:
1.273   ! nicm      481: static int
        !           482: window_pane_get_palette(struct window_pane *wp, int c)
        !           483: {
        !           484:        if (wp == NULL)
        !           485:                return (-1);
        !           486:        return (colour_palette_get(&wp->palette, c));
        !           487: }
        !           488:
1.145     nicm      489: void
                    490: window_redraw_active_switch(struct window *w, struct window_pane *wp)
                    491: {
1.258     nicm      492:        struct grid_cell        *gc1, *gc2;
                    493:        int                      c1, c2;
1.145     nicm      494:
                    495:        if (wp == w->active)
                    496:                return;
                    497:
1.234     nicm      498:        for (;;) {
                    499:                /*
                    500:                 * If the active and inactive styles or palettes are different,
                    501:                 * need to redraw the panes.
                    502:                 */
1.258     nicm      503:                gc1 = &wp->cached_gc;
                    504:                gc2 = &wp->cached_active_gc;
                    505:                if (!grid_cells_look_equal(gc1, gc2))
1.234     nicm      506:                        wp->flags |= PANE_REDRAW;
                    507:                else {
1.258     nicm      508:                        c1 = window_pane_get_palette(wp, gc1->fg);
                    509:                        c2 = window_pane_get_palette(wp, gc2->fg);
1.234     nicm      510:                        if (c1 != c2)
                    511:                                wp->flags |= PANE_REDRAW;
                    512:                        else {
1.258     nicm      513:                                c1 = window_pane_get_palette(wp, gc1->bg);
                    514:                                c2 = window_pane_get_palette(wp, gc2->bg);
1.234     nicm      515:                                if (c1 != c2)
                    516:                                        wp->flags |= PANE_REDRAW;
                    517:                        }
                    518:                }
                    519:                if (wp == w->active)
                    520:                        break;
                    521:                wp = w->active;
                    522:        }
1.29      nicm      523: }
                    524:
1.66      nicm      525: struct window_pane *
                    526: window_get_active_at(struct window *w, u_int x, u_int y)
1.29      nicm      527: {
                    528:        struct window_pane      *wp;
                    529:
                    530:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.219     nicm      531:                if (!window_pane_visible(wp))
                    532:                        continue;
1.66      nicm      533:                if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29      nicm      534:                        continue;
1.66      nicm      535:                if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29      nicm      536:                        continue;
1.66      nicm      537:                return (wp);
                    538:        }
                    539:        return (NULL);
                    540: }
                    541:
                    542: struct window_pane *
                    543: window_find_string(struct window *w, const char *s)
                    544: {
1.248     nicm      545:        u_int   x, y, top = 0, bottom = w->sy - 1;
                    546:        int     status;
1.66      nicm      547:
                    548:        x = w->sx / 2;
                    549:        y = w->sy / 2;
                    550:
1.248     nicm      551:        status = options_get_number(w->options, "pane-border-status");
                    552:        if (status == PANE_STATUS_TOP)
                    553:                top++;
                    554:        else if (status == PANE_STATUS_BOTTOM)
                    555:                bottom--;
                    556:
1.66      nicm      557:        if (strcasecmp(s, "top") == 0)
1.248     nicm      558:                y = top;
1.66      nicm      559:        else if (strcasecmp(s, "bottom") == 0)
1.248     nicm      560:                y = bottom;
1.66      nicm      561:        else if (strcasecmp(s, "left") == 0)
                    562:                x = 0;
                    563:        else if (strcasecmp(s, "right") == 0)
                    564:                x = w->sx - 1;
                    565:        else if (strcasecmp(s, "top-left") == 0) {
                    566:                x = 0;
1.248     nicm      567:                y = top;
1.66      nicm      568:        } else if (strcasecmp(s, "top-right") == 0) {
                    569:                x = w->sx - 1;
1.248     nicm      570:                y = top;
1.66      nicm      571:        } else if (strcasecmp(s, "bottom-left") == 0) {
                    572:                x = 0;
1.248     nicm      573:                y = bottom;
1.66      nicm      574:        } else if (strcasecmp(s, "bottom-right") == 0) {
                    575:                x = w->sx - 1;
1.248     nicm      576:                y = bottom;
1.66      nicm      577:        } else
                    578:                return (NULL);
                    579:
                    580:        return (window_get_active_at(w, x, y));
1.1       nicm      581: }
                    582:
1.93      nicm      583: int
                    584: window_zoom(struct window_pane *wp)
                    585: {
                    586:        struct window           *w = wp->window;
                    587:        struct window_pane      *wp1;
                    588:
                    589:        if (w->flags & WINDOW_ZOOMED)
                    590:                return (-1);
                    591:
1.94      nicm      592:        if (window_count_panes(w) == 1)
                    593:                return (-1);
                    594:
1.93      nicm      595:        if (w->active != wp)
1.226     nicm      596:                window_set_active_pane(w, wp, 1);
1.93      nicm      597:
                    598:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    599:                wp1->saved_layout_cell = wp1->layout_cell;
                    600:                wp1->layout_cell = NULL;
                    601:        }
                    602:
                    603:        w->saved_layout_root = w->layout_root;
                    604:        layout_init(w, wp);
                    605:        w->flags |= WINDOW_ZOOMED;
1.172     nicm      606:        notify_window("window-layout-changed", w);
1.93      nicm      607:
                    608:        return (0);
                    609: }
                    610:
                    611: int
                    612: window_unzoom(struct window *w)
                    613: {
1.97      nicm      614:        struct window_pane      *wp;
1.93      nicm      615:
                    616:        if (!(w->flags & WINDOW_ZOOMED))
                    617:                return (-1);
                    618:
                    619:        w->flags &= ~WINDOW_ZOOMED;
                    620:        layout_free(w);
                    621:        w->layout_root = w->saved_layout_root;
1.120     nicm      622:        w->saved_layout_root = NULL;
1.93      nicm      623:
1.97      nicm      624:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    625:                wp->layout_cell = wp->saved_layout_cell;
                    626:                wp->saved_layout_cell = NULL;
1.93      nicm      627:        }
1.270     nicm      628:        layout_fix_panes(w, NULL);
1.172     nicm      629:        notify_window("window-layout-changed", w);
1.93      nicm      630:
1.241     nicm      631:        return (0);
                    632: }
                    633:
                    634: int
1.270     nicm      635: window_push_zoom(struct window *w, int always, int flag)
1.241     nicm      636: {
                    637:        log_debug("%s: @%u %d", __func__, w->id,
                    638:            flag && (w->flags & WINDOW_ZOOMED));
1.270     nicm      639:        if (flag && (always || (w->flags & WINDOW_ZOOMED)))
1.241     nicm      640:                w->flags |= WINDOW_WASZOOMED;
                    641:        else
                    642:                w->flags &= ~WINDOW_WASZOOMED;
                    643:        return (window_unzoom(w) == 0);
                    644: }
                    645:
                    646: int
                    647: window_pop_zoom(struct window *w)
                    648: {
                    649:        log_debug("%s: @%u %d", __func__, w->id,
                    650:            !!(w->flags & WINDOW_WASZOOMED));
                    651:        if (w->flags & WINDOW_WASZOOMED)
                    652:                return (window_zoom(w->active) == 0);
1.93      nicm      653:        return (0);
                    654: }
                    655:
1.1       nicm      656: struct window_pane *
1.226     nicm      657: window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
                    658:     int flags)
1.1       nicm      659: {
                    660:        struct window_pane      *wp;
                    661:
1.185     nicm      662:        if (other == NULL)
                    663:                other = w->active;
                    664:
1.14      nicm      665:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.186     nicm      666:        if (TAILQ_EMPTY(&w->panes)) {
                    667:                log_debug("%s: @%u at start", __func__, w->id);
1.1       nicm      668:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
1.226     nicm      669:        } else if (flags & SPAWN_BEFORE) {
1.186     nicm      670:                log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
1.226     nicm      671:                if (flags & SPAWN_FULLSIZE)
1.208     nicm      672:                        TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    673:                else
                    674:                        TAILQ_INSERT_BEFORE(other, wp, entry);
1.186     nicm      675:        } else {
                    676:                log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
1.226     nicm      677:                if (flags & SPAWN_FULLSIZE)
1.208     nicm      678:                        TAILQ_INSERT_TAIL(&w->panes, wp, entry);
                    679:                else
                    680:                        TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
1.186     nicm      681:        }
1.1       nicm      682:        return (wp);
                    683: }
                    684:
                    685: void
1.105     nicm      686: window_lost_pane(struct window *w, struct window_pane *wp)
1.1       nicm      687: {
1.205     nicm      688:        log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
                    689:
1.152     nicm      690:        if (wp == marked_pane.wp)
1.132     nicm      691:                server_clear_marked();
                    692:
1.57      nicm      693:        if (wp == w->active) {
1.58      nicm      694:                w->active = w->last;
                    695:                w->last = NULL;
                    696:                if (w->active == NULL) {
                    697:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    698:                        if (w->active == NULL)
                    699:                                w->active = TAILQ_NEXT(wp, entry);
                    700:                }
1.193     nicm      701:                if (w->active != NULL) {
1.151     nicm      702:                        w->active->flags |= PANE_CHANGED;
1.193     nicm      703:                        notify_window("window-pane-changed", w);
                    704:                }
1.58      nicm      705:        } else if (wp == w->last)
                    706:                w->last = NULL;
1.105     nicm      707: }
                    708:
                    709: void
                    710: window_remove_pane(struct window *w, struct window_pane *wp)
                    711: {
                    712:        window_lost_pane(w, wp);
1.1       nicm      713:
                    714:        TAILQ_REMOVE(&w->panes, wp, entry);
                    715:        window_pane_destroy(wp);
                    716: }
                    717:
                    718: struct window_pane *
                    719: window_pane_at_index(struct window *w, u_int idx)
                    720: {
                    721:        struct window_pane      *wp;
                    722:        u_int                    n;
                    723:
1.146     nicm      724:        n = options_get_number(w->options, "pane-base-index");
1.1       nicm      725:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    726:                if (n == idx)
                    727:                        return (wp);
                    728:                n++;
                    729:        }
                    730:        return (NULL);
1.53      nicm      731: }
                    732:
                    733: struct window_pane *
                    734: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    735: {
                    736:        for (; n > 0; n--) {
                    737:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    738:                        wp = TAILQ_FIRST(&w->panes);
                    739:        }
                    740:
                    741:        return (wp);
                    742: }
                    743:
                    744: struct window_pane *
                    745: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    746:     u_int n)
                    747: {
                    748:        for (; n > 0; n--) {
                    749:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    750:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    751:        }
                    752:
                    753:        return (wp);
1.13      nicm      754: }
                    755:
1.69      nicm      756: int
                    757: window_pane_index(struct window_pane *wp, u_int *i)
1.13      nicm      758: {
                    759:        struct window_pane      *wq;
1.69      nicm      760:        struct window           *w = wp->window;
1.13      nicm      761:
1.146     nicm      762:        *i = options_get_number(w->options, "pane-base-index");
1.13      nicm      763:        TAILQ_FOREACH(wq, &w->panes, entry) {
1.69      nicm      764:                if (wp == wq) {
                    765:                        return (0);
                    766:                }
                    767:                (*i)++;
1.13      nicm      768:        }
1.69      nicm      769:
                    770:        return (-1);
1.1       nicm      771: }
                    772:
                    773: u_int
                    774: window_count_panes(struct window *w)
                    775: {
                    776:        struct window_pane      *wp;
                    777:        u_int                    n;
                    778:
                    779:        n = 0;
                    780:        TAILQ_FOREACH(wp, &w->panes, entry)
                    781:                n++;
                    782:        return (n);
                    783: }
                    784:
                    785: void
                    786: window_destroy_panes(struct window *w)
                    787: {
                    788:        struct window_pane      *wp;
                    789:
                    790:        while (!TAILQ_EMPTY(&w->panes)) {
                    791:                wp = TAILQ_FIRST(&w->panes);
                    792:                TAILQ_REMOVE(&w->panes, wp, entry);
                    793:                window_pane_destroy(wp);
                    794:        }
1.61      nicm      795: }
                    796:
1.188     nicm      797: const char *
1.268     nicm      798: window_printable_flags(struct winlink *wl, int escape)
1.61      nicm      799: {
1.189     nicm      800:        struct session  *s = wl->session;
                    801:        static char      flags[32];
                    802:        int              pos;
1.61      nicm      803:
                    804:        pos = 0;
1.268     nicm      805:        if (wl->flags & WINLINK_ACTIVITY) {
1.61      nicm      806:                flags[pos++] = '#';
1.268     nicm      807:                if (escape)
                    808:                        flags[pos++] = '#';
                    809:        }
1.61      nicm      810:        if (wl->flags & WINLINK_BELL)
                    811:                flags[pos++] = '!';
                    812:        if (wl->flags & WINLINK_SILENCE)
                    813:                flags[pos++] = '~';
                    814:        if (wl == s->curw)
                    815:                flags[pos++] = '*';
                    816:        if (wl == TAILQ_FIRST(&s->lastw))
                    817:                flags[pos++] = '-';
1.152     nicm      818:        if (server_check_marked() && wl == marked_pane.wl)
1.132     nicm      819:                flags[pos++] = 'M';
1.93      nicm      820:        if (wl->window->flags & WINDOW_ZOOMED)
                    821:                flags[pos++] = 'Z';
1.61      nicm      822:        flags[pos] = '\0';
1.188     nicm      823:        return (flags);
1.1       nicm      824: }
                    825:
1.123     nicm      826: struct window_pane *
                    827: window_pane_find_by_id_str(const char *s)
                    828: {
                    829:        const char      *errstr;
                    830:        u_int            id;
                    831:
                    832:        if (*s != '%')
                    833:                return (NULL);
                    834:
                    835:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    836:        if (errstr != NULL)
                    837:                return (NULL);
                    838:        return (window_pane_find_by_id(id));
                    839: }
                    840:
1.64      nicm      841: struct window_pane *
                    842: window_pane_find_by_id(u_int id)
                    843: {
                    844:        struct window_pane      wp;
                    845:
                    846:        wp.id = id;
                    847:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
                    848: }
                    849:
1.169     nicm      850: static struct window_pane *
1.1       nicm      851: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    852: {
                    853:        struct window_pane      *wp;
1.140     nicm      854:        char                     host[HOST_NAME_MAX + 1];
1.1       nicm      855:
                    856:        wp = xcalloc(1, sizeof *wp);
                    857:        wp->window = w;
1.234     nicm      858:        wp->options = options_create(w->options);
                    859:        wp->flags = PANE_STYLECHANGED;
1.1       nicm      860:
1.71      nicm      861:        wp->id = next_window_pane_id++;
1.64      nicm      862:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
                    863:
1.1       nicm      864:        wp->fd = -1;
                    865:
1.218     nicm      866:        TAILQ_INIT(&wp->modes);
1.14      nicm      867:
1.272     nicm      868:        TAILQ_INIT (&wp->resize_queue);
1.1       nicm      869:
1.265     nicm      870:        wp->sx = sx;
                    871:        wp->sy = sy;
1.1       nicm      872:
1.32      nicm      873:        wp->pipe_fd = -1;
                    874:
1.273   ! nicm      875:        colour_palette_init(&wp->palette);
1.1       nicm      876:        screen_init(&wp->base, sx, sy, hlimit);
                    877:        wp->screen = &wp->base;
1.159     nicm      878:
                    879:        screen_init(&wp->status_screen, 1, 1, 0);
1.140     nicm      880:
                    881:        if (gethostname(host, sizeof host) == 0)
                    882:                screen_set_title(&wp->base, host);
1.1       nicm      883:
                    884:        return (wp);
                    885: }
                    886:
1.169     nicm      887: static void
1.1       nicm      888: window_pane_destroy(struct window_pane *wp)
                    889: {
1.272     nicm      890:        struct window_pane_resize       *r;
                    891:        struct window_pane_resize       *r1;
                    892:
1.218     nicm      893:        window_pane_reset_mode_all(wp);
1.194     nicm      894:        free(wp->searchstr);
1.55      nicm      895:
1.37      nicm      896:        if (wp->fd != -1) {
1.70      nicm      897:                bufferevent_free(wp->event);
1.1       nicm      898:                close(wp->fd);
1.37      nicm      899:        }
1.251     nicm      900:        if (wp->ictx != NULL)
                    901:                input_free(wp->ictx);
1.225     nicm      902:
                    903:        screen_free(&wp->status_screen);
1.1       nicm      904:
                    905:        screen_free(&wp->base);
                    906:
1.32      nicm      907:        if (wp->pipe_fd != -1) {
1.70      nicm      908:                bufferevent_free(wp->pipe_event);
1.32      nicm      909:                close(wp->pipe_fd);
                    910:        }
1.167     nicm      911:
                    912:        if (event_initialized(&wp->resize_timer))
                    913:                event_del(&wp->resize_timer);
1.272     nicm      914:        TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
                    915:                TAILQ_REMOVE(&wp->resize_queue, r, entry);
                    916:                free(r);
                    917:        }
1.32      nicm      918:
1.64      nicm      919:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    920:
1.234     nicm      921:        options_free(wp->options);
1.147     nicm      922:        free((void *)wp->cwd);
1.82      nicm      923:        free(wp->shell);
1.110     nicm      924:        cmd_free_argv(wp->argc, wp->argv);
1.273   ! nicm      925:        colour_palette_free(&wp->palette);
1.82      nicm      926:        free(wp);
1.1       nicm      927: }
                    928:
1.166     nicm      929: static void
1.149     nicm      930: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37      nicm      931: {
1.263     nicm      932:        struct window_pane              *wp = data;
                    933:        struct evbuffer                 *evb = wp->event->input;
                    934:        struct window_pane_offset       *wpo = &wp->pipe_offset;
                    935:        size_t                           size = EVBUFFER_LENGTH(evb);
                    936:        char                            *new_data;
                    937:        size_t                           new_size;
                    938:        struct client                   *c;
                    939:
                    940:        if (wp->pipe_fd != -1) {
                    941:                new_data = window_pane_get_new_data(wp, wpo, &new_size);
                    942:                if (new_size > 0) {
                    943:                        bufferevent_write(wp->pipe_event, new_data, new_size);
1.264     nicm      944:                        window_pane_update_used_data(wp, wpo, new_size);
1.263     nicm      945:                }
1.46      nicm      946:        }
                    947:
1.183     nicm      948:        log_debug("%%%u has %zu bytes", wp->id, size);
1.263     nicm      949:        TAILQ_FOREACH(c, &clients, entry) {
1.264     nicm      950:                if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1.263     nicm      951:                        control_write_output(c, wp);
                    952:        }
1.250     nicm      953:        input_parse_pane(wp);
1.264     nicm      954:        bufferevent_disable(wp->event, EV_READ);
1.37      nicm      955: }
                    956:
1.166     nicm      957: static void
1.149     nicm      958: window_pane_error_callback(__unused struct bufferevent *bufev,
                    959:     __unused short what, void *data)
1.37      nicm      960: {
                    961:        struct window_pane *wp = data;
                    962:
1.200     nicm      963:        log_debug("%%%u error", wp->id);
1.201     nicm      964:        wp->flags |= PANE_EXITED;
1.200     nicm      965:
                    966:        if (window_pane_destroy_ready(wp))
                    967:                server_destroy_pane(wp, 1);
1.37      nicm      968: }
                    969:
                    970: void
1.226     nicm      971: window_pane_set_event(struct window_pane *wp)
                    972: {
                    973:        setblocking(wp->fd, 0);
                    974:
                    975:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
                    976:            NULL, window_pane_error_callback, wp);
1.273   ! nicm      977:        wp->ictx = input_init(wp, wp->event, &wp->palette);
1.226     nicm      978:
                    979:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
                    980: }
                    981:
                    982: void
1.1       nicm      983: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    984: {
1.218     nicm      985:        struct window_mode_entry        *wme;
1.272     nicm      986:        struct window_pane_resize       *r;
1.218     nicm      987:
1.1       nicm      988:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      989:                return;
1.272     nicm      990:
                    991:        r = xmalloc (sizeof *r);
                    992:        r->sx = sx;
                    993:        r->sy = sy;
                    994:        r->osx = wp->sx;
                    995:        r->osy = wp->sy;
                    996:        TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
                    997:
1.1       nicm      998:        wp->sx = sx;
                    999:        wp->sy = sy;
                   1000:
1.238     nicm     1001:        log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1.252     nicm     1002:        screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1.218     nicm     1003:
                   1004:        wme = TAILQ_FIRST(&wp->modes);
                   1005:        if (wme != NULL && wme->mode->resize != NULL)
                   1006:                wme->mode->resize(wme, sx, sy);
1.180     nicm     1007: }
                   1008:
1.1       nicm     1009: int
1.255     nicm     1010: window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
                   1011:     const struct window_mode *mode, struct cmd_find_state *fs,
                   1012:     struct args *args)
1.1       nicm     1013: {
1.218     nicm     1014:        struct window_mode_entry        *wme;
1.1       nicm     1015:
1.218     nicm     1016:        if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1.1       nicm     1017:                return (1);
1.217     nicm     1018:
1.218     nicm     1019:        TAILQ_FOREACH(wme, &wp->modes, entry) {
                   1020:                if (wme->mode == mode)
                   1021:                        break;
                   1022:        }
1.223     nicm     1023:        if (wme != NULL) {
1.218     nicm     1024:                TAILQ_REMOVE(&wp->modes, wme, entry);
1.223     nicm     1025:                TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
                   1026:        } else {
1.218     nicm     1027:                wme = xcalloc(1, sizeof *wme);
                   1028:                wme->wp = wp;
1.255     nicm     1029:                wme->swp = swp;
1.218     nicm     1030:                wme->mode = mode;
                   1031:                wme->prefix = 1;
1.223     nicm     1032:                TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1.218     nicm     1033:                wme->screen = wme->mode->init(wme, fs, args);
                   1034:        }
1.162     nicm     1035:
1.218     nicm     1036:        wp->screen = wme->screen;
1.142     nicm     1037:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1038:
1.259     nicm     1039:        server_redraw_window_borders(wp->window);
1.157     nicm     1040:        server_status_window(wp->window);
1.193     nicm     1041:        notify_pane("pane-mode-changed", wp);
1.218     nicm     1042:
1.1       nicm     1043:        return (0);
                   1044: }
                   1045:
                   1046: void
                   1047: window_pane_reset_mode(struct window_pane *wp)
                   1048: {
1.218     nicm     1049:        struct window_mode_entry        *wme, *next;
                   1050:
                   1051:        if (TAILQ_EMPTY(&wp->modes))
1.1       nicm     1052:                return;
                   1053:
1.218     nicm     1054:        wme = TAILQ_FIRST(&wp->modes);
                   1055:        TAILQ_REMOVE(&wp->modes, wme, entry);
                   1056:        wme->mode->free(wme);
                   1057:        free(wme);
                   1058:
                   1059:        next = TAILQ_FIRST(&wp->modes);
                   1060:        if (next == NULL) {
                   1061:                log_debug("%s: no next mode", __func__);
                   1062:                wp->screen = &wp->base;
                   1063:        } else {
                   1064:                log_debug("%s: next mode is %s", __func__, next->mode->name);
                   1065:                wp->screen = next->screen;
1.230     nicm     1066:                if (next->mode->resize != NULL)
1.218     nicm     1067:                        next->mode->resize(next, wp->sx, wp->sy);
                   1068:        }
1.142     nicm     1069:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1070:
1.259     nicm     1071:        server_redraw_window_borders(wp->window);
1.157     nicm     1072:        server_status_window(wp->window);
1.193     nicm     1073:        notify_pane("pane-mode-changed", wp);
1.1       nicm     1074: }
                   1075:
                   1076: void
1.218     nicm     1077: window_pane_reset_mode_all(struct window_pane *wp)
                   1078: {
                   1079:        while (!TAILQ_EMPTY(&wp->modes))
                   1080:                window_pane_reset_mode(wp);
                   1081: }
                   1082:
1.267     nicm     1083: static void
                   1084: window_pane_copy_key(struct window_pane *wp, key_code key)
                   1085: {
                   1086:        struct window_pane      *loop;
                   1087:
                   1088:        TAILQ_FOREACH(loop, &wp->window->panes, entry) {
                   1089:                if (loop != wp &&
                   1090:                    TAILQ_EMPTY(&loop->modes) &&
                   1091:                    loop->fd != -1 &&
                   1092:                    (~loop->flags & PANE_INPUTOFF) &&
                   1093:                    window_pane_visible(loop) &&
                   1094:                    options_get_number(loop->options, "synchronize-panes"))
                   1095:                        input_key_pane(loop, key, NULL);
                   1096:        }
                   1097: }
                   1098:
1.247     nicm     1099: int
1.118     nicm     1100: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.216     nicm     1101:     struct winlink *wl, key_code key, struct mouse_event *m)
1.1       nicm     1102: {
1.218     nicm     1103:        struct window_mode_entry        *wme;
1.3       nicm     1104:
1.118     nicm     1105:        if (KEYC_IS_MOUSE(key) && m == NULL)
1.247     nicm     1106:                return (-1);
1.118     nicm     1107:
1.218     nicm     1108:        wme = TAILQ_FIRST(&wp->modes);
1.217     nicm     1109:        if (wme != NULL) {
1.261     nicm     1110:                if (wme->mode->key != NULL && c != NULL) {
                   1111:                        key &= ~KEYC_MASK_FLAGS;
                   1112:                        wme->mode->key(wme, c, s, wl, key, m);
                   1113:                }
1.247     nicm     1114:                return (0);
1.30      nicm     1115:        }
1.27      nicm     1116:
1.113     nicm     1117:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.247     nicm     1118:                return (0);
1.113     nicm     1119:
1.250     nicm     1120:        if (input_key_pane(wp, key, m) != 0)
1.247     nicm     1121:                return (-1);
1.118     nicm     1122:
                   1123:        if (KEYC_IS_MOUSE(key))
1.247     nicm     1124:                return (0);
1.267     nicm     1125:        if (options_get_number(wp->options, "synchronize-panes"))
                   1126:                window_pane_copy_key(wp, key);
1.247     nicm     1127:        return (0);
1.10      nicm     1128: }
                   1129:
                   1130: int
1.205     nicm     1131: window_pane_visible(struct window_pane *wp)
1.10      nicm     1132: {
1.213     nicm     1133:        if (~wp->window->flags & WINDOW_ZOOMED)
                   1134:                return (1);
                   1135:        return (wp == wp->window->active);
1.1       nicm     1136: }
                   1137:
1.195     nicm     1138: u_int
1.232     nicm     1139: window_pane_search(struct window_pane *wp, const char *term, int regex,
                   1140:     int ignore)
1.195     nicm     1141: {
                   1142:        struct screen   *s = &wp->base;
1.232     nicm     1143:        regex_t          r;
                   1144:        char            *new = NULL, *line;
1.195     nicm     1145:        u_int            i;
1.232     nicm     1146:        int              flags = 0, found;
1.236     nicm     1147:        size_t           n;
1.195     nicm     1148:
1.232     nicm     1149:        if (!regex) {
                   1150:                if (ignore)
                   1151:                        flags |= FNM_CASEFOLD;
                   1152:                xasprintf(&new, "*%s*", term);
                   1153:        } else {
                   1154:                if (ignore)
                   1155:                        flags |= REG_ICASE;
                   1156:                if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
                   1157:                        return (0);
                   1158:        }
1.195     nicm     1159:
                   1160:        for (i = 0; i < screen_size_y(s); i++) {
                   1161:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.236     nicm     1162:                for (n = strlen(line); n > 0; n--) {
                   1163:                        if (!isspace((u_char)line[n - 1]))
                   1164:                                break;
                   1165:                        line[n - 1] = '\0';
                   1166:                }
                   1167:                log_debug("%s: %s", __func__, line);
1.232     nicm     1168:                if (!regex)
1.254     nicm     1169:                        found = (fnmatch(new, line, flags) == 0);
1.232     nicm     1170:                else
                   1171:                        found = (regexec(&r, line, 0, NULL, 0) == 0);
                   1172:                free(line);
                   1173:                if (found)
1.195     nicm     1174:                        break;
                   1175:        }
1.232     nicm     1176:        if (!regex)
                   1177:                free(new);
                   1178:        else
                   1179:                regfree(&r);
1.195     nicm     1180:
                   1181:        if (i == screen_size_y(s))
                   1182:                return (0);
                   1183:        return (i + 1);
1.45      nicm     1184: }
                   1185:
1.109     nicm     1186: /* Get MRU pane from a list. */
1.166     nicm     1187: static struct window_pane *
1.126     nicm     1188: window_pane_choose_best(struct window_pane **list, u_int size)
1.109     nicm     1189: {
                   1190:        struct window_pane      *next, *best;
                   1191:        u_int                    i;
                   1192:
1.126     nicm     1193:        if (size == 0)
1.109     nicm     1194:                return (NULL);
                   1195:
1.126     nicm     1196:        best = list[0];
                   1197:        for (i = 1; i < size; i++) {
                   1198:                next = list[i];
1.109     nicm     1199:                if (next->active_point > best->active_point)
                   1200:                        best = next;
                   1201:        }
                   1202:        return (best);
                   1203: }
                   1204:
                   1205: /*
                   1206:  * Find the pane directly above another. We build a list of those adjacent to
                   1207:  * top edge and then choose the best.
                   1208:  */
1.45      nicm     1209: struct window_pane *
                   1210: window_pane_find_up(struct window_pane *wp)
                   1211: {
1.237     nicm     1212:        struct window           *w;
1.126     nicm     1213:        struct window_pane      *next, *best, **list;
                   1214:        u_int                    edge, left, right, end, size;
1.187     nicm     1215:        int                      status, found;
1.45      nicm     1216:
1.213     nicm     1217:        if (wp == NULL)
1.45      nicm     1218:                return (NULL);
1.237     nicm     1219:        w = wp->window;
                   1220:        status = options_get_number(w->options, "pane-border-status");
1.126     nicm     1221:
                   1222:        list = NULL;
                   1223:        size = 0;
1.109     nicm     1224:
                   1225:        edge = wp->yoff;
1.237     nicm     1226:        if (status == PANE_STATUS_TOP) {
                   1227:                if (edge == 1)
                   1228:                        edge = w->sy + 1;
                   1229:        } else if (status == PANE_STATUS_BOTTOM) {
                   1230:                if (edge == 0)
                   1231:                        edge = w->sy;
                   1232:        } else {
                   1233:                if (edge == 0)
                   1234:                        edge = w->sy + 1;
                   1235:        }
1.45      nicm     1236:
                   1237:        left = wp->xoff;
1.109     nicm     1238:        right = wp->xoff + wp->sx;
1.45      nicm     1239:
1.237     nicm     1240:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1241:                if (next == wp)
1.45      nicm     1242:                        continue;
1.109     nicm     1243:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1244:                        continue;
1.109     nicm     1245:                end = next->xoff + next->sx - 1;
                   1246:
                   1247:                found = 0;
                   1248:                if (next->xoff < left && end > right)
                   1249:                        found = 1;
                   1250:                else if (next->xoff >= left && next->xoff <= right)
                   1251:                        found = 1;
                   1252:                else if (end >= left && end <= right)
                   1253:                        found = 1;
1.126     nicm     1254:                if (!found)
                   1255:                        continue;
                   1256:                list = xreallocarray(list, size + 1, sizeof *list);
                   1257:                list[size++] = next;
1.109     nicm     1258:        }
                   1259:
1.126     nicm     1260:        best = window_pane_choose_best(list, size);
                   1261:        free(list);
1.109     nicm     1262:        return (best);
1.45      nicm     1263: }
                   1264:
                   1265: /* Find the pane directly below another. */
                   1266: struct window_pane *
                   1267: window_pane_find_down(struct window_pane *wp)
                   1268: {
1.237     nicm     1269:        struct window           *w;
1.126     nicm     1270:        struct window_pane      *next, *best, **list;
                   1271:        u_int                    edge, left, right, end, size;
1.187     nicm     1272:        int                      status, found;
1.45      nicm     1273:
1.213     nicm     1274:        if (wp == NULL)
1.45      nicm     1275:                return (NULL);
1.237     nicm     1276:        w = wp->window;
                   1277:        status = options_get_number(w->options, "pane-border-status");
1.126     nicm     1278:
                   1279:        list = NULL;
                   1280:        size = 0;
1.109     nicm     1281:
                   1282:        edge = wp->yoff + wp->sy + 1;
1.237     nicm     1283:        if (status == PANE_STATUS_TOP) {
                   1284:                if (edge >= w->sy)
                   1285:                        edge = 1;
                   1286:        } else if (status == PANE_STATUS_BOTTOM) {
                   1287:                if (edge >= w->sy - 1)
                   1288:                        edge = 0;
                   1289:        } else {
1.239     nicm     1290:                if (edge >= w->sy)
1.237     nicm     1291:                        edge = 0;
                   1292:        }
1.45      nicm     1293:
                   1294:        left = wp->xoff;
1.109     nicm     1295:        right = wp->xoff + wp->sx;
1.45      nicm     1296:
1.237     nicm     1297:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1298:                if (next == wp)
1.45      nicm     1299:                        continue;
1.109     nicm     1300:                if (next->yoff != edge)
1.45      nicm     1301:                        continue;
1.109     nicm     1302:                end = next->xoff + next->sx - 1;
                   1303:
                   1304:                found = 0;
                   1305:                if (next->xoff < left && end > right)
                   1306:                        found = 1;
                   1307:                else if (next->xoff >= left && next->xoff <= right)
                   1308:                        found = 1;
                   1309:                else if (end >= left && end <= right)
                   1310:                        found = 1;
1.126     nicm     1311:                if (!found)
                   1312:                        continue;
                   1313:                list = xreallocarray(list, size + 1, sizeof *list);
                   1314:                list[size++] = next;
1.45      nicm     1315:        }
1.109     nicm     1316:
1.126     nicm     1317:        best = window_pane_choose_best(list, size);
                   1318:        free(list);
1.109     nicm     1319:        return (best);
1.45      nicm     1320: }
                   1321:
1.109     nicm     1322: /* Find the pane directly to the left of another. */
1.45      nicm     1323: struct window_pane *
                   1324: window_pane_find_left(struct window_pane *wp)
                   1325: {
1.237     nicm     1326:        struct window           *w;
1.126     nicm     1327:        struct window_pane      *next, *best, **list;
                   1328:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1329:        int                      found;
1.45      nicm     1330:
1.213     nicm     1331:        if (wp == NULL)
1.45      nicm     1332:                return (NULL);
1.237     nicm     1333:        w = wp->window;
1.126     nicm     1334:
                   1335:        list = NULL;
                   1336:        size = 0;
1.109     nicm     1337:
                   1338:        edge = wp->xoff;
                   1339:        if (edge == 0)
1.237     nicm     1340:                edge = w->sx + 1;
1.45      nicm     1341:
                   1342:        top = wp->yoff;
1.109     nicm     1343:        bottom = wp->yoff + wp->sy;
1.45      nicm     1344:
1.237     nicm     1345:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1346:                if (next == wp)
1.45      nicm     1347:                        continue;
1.109     nicm     1348:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1349:                        continue;
1.109     nicm     1350:                end = next->yoff + next->sy - 1;
                   1351:
                   1352:                found = 0;
                   1353:                if (next->yoff < top && end > bottom)
                   1354:                        found = 1;
                   1355:                else if (next->yoff >= top && next->yoff <= bottom)
                   1356:                        found = 1;
                   1357:                else if (end >= top && end <= bottom)
                   1358:                        found = 1;
1.126     nicm     1359:                if (!found)
                   1360:                        continue;
                   1361:                list = xreallocarray(list, size + 1, sizeof *list);
                   1362:                list[size++] = next;
1.45      nicm     1363:        }
1.109     nicm     1364:
1.126     nicm     1365:        best = window_pane_choose_best(list, size);
                   1366:        free(list);
1.109     nicm     1367:        return (best);
1.45      nicm     1368: }
                   1369:
1.109     nicm     1370: /* Find the pane directly to the right of another. */
1.45      nicm     1371: struct window_pane *
                   1372: window_pane_find_right(struct window_pane *wp)
                   1373: {
1.237     nicm     1374:        struct window           *w;
1.126     nicm     1375:        struct window_pane      *next, *best, **list;
                   1376:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1377:        int                      found;
1.45      nicm     1378:
1.213     nicm     1379:        if (wp == NULL)
1.45      nicm     1380:                return (NULL);
1.237     nicm     1381:        w = wp->window;
1.126     nicm     1382:
                   1383:        list = NULL;
                   1384:        size = 0;
1.109     nicm     1385:
                   1386:        edge = wp->xoff + wp->sx + 1;
1.237     nicm     1387:        if (edge >= w->sx)
1.109     nicm     1388:                edge = 0;
1.45      nicm     1389:
                   1390:        top = wp->yoff;
1.109     nicm     1391:        bottom = wp->yoff + wp->sy;
1.45      nicm     1392:
1.237     nicm     1393:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1394:                if (next == wp)
1.45      nicm     1395:                        continue;
1.109     nicm     1396:                if (next->xoff != edge)
1.45      nicm     1397:                        continue;
1.109     nicm     1398:                end = next->yoff + next->sy - 1;
                   1399:
                   1400:                found = 0;
                   1401:                if (next->yoff < top && end > bottom)
                   1402:                        found = 1;
                   1403:                else if (next->yoff >= top && next->yoff <= bottom)
                   1404:                        found = 1;
                   1405:                else if (end >= top && end <= bottom)
                   1406:                        found = 1;
1.126     nicm     1407:                if (!found)
                   1408:                        continue;
                   1409:                list = xreallocarray(list, size + 1, sizeof *list);
                   1410:                list[size++] = next;
1.109     nicm     1411:        }
                   1412:
1.126     nicm     1413:        best = window_pane_choose_best(list, size);
                   1414:        free(list);
1.109     nicm     1415:        return (best);
1.81      nicm     1416: }
                   1417:
                   1418: /* Clear alert flags for a winlink */
                   1419: void
                   1420: winlink_clear_flags(struct winlink *wl)
                   1421: {
1.174     nicm     1422:        struct winlink  *loop;
1.81      nicm     1423:
1.174     nicm     1424:        wl->window->flags &= ~WINDOW_ALERTFLAGS;
                   1425:        TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
                   1426:                if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
                   1427:                        loop->flags &= ~WINLINK_ALERTFLAGS;
                   1428:                        server_status_session(loop->session);
1.81      nicm     1429:                }
                   1430:        }
1.134     nicm     1431: }
                   1432:
1.184     nicm     1433: /* Shuffle window indexes up. */
1.134     nicm     1434: int
1.266     nicm     1435: winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1.134     nicm     1436: {
                   1437:        int      idx, last;
                   1438:
1.226     nicm     1439:        if (wl == NULL)
                   1440:                return (-1);
1.266     nicm     1441:        if (before)
                   1442:                idx = wl->idx;
                   1443:        else
                   1444:                idx = wl->idx + 1;
1.134     nicm     1445:
                   1446:        /* Find the next free index. */
                   1447:        for (last = idx; last < INT_MAX; last++) {
                   1448:                if (winlink_find_by_index(&s->windows, last) == NULL)
                   1449:                        break;
                   1450:        }
                   1451:        if (last == INT_MAX)
                   1452:                return (-1);
                   1453:
                   1454:        /* Move everything from last - 1 to idx up a bit. */
                   1455:        for (; last > idx; last--) {
                   1456:                wl = winlink_find_by_index(&s->windows, last - 1);
1.266     nicm     1457:                RB_REMOVE(winlinks, &s->windows, wl);
                   1458:                wl->idx++;
                   1459:                RB_INSERT(winlinks, &s->windows, wl);
1.134     nicm     1460:        }
                   1461:
                   1462:        return (idx);
1.228     nicm     1463: }
                   1464:
                   1465: static void
1.246     nicm     1466: window_pane_input_callback(struct client *c, __unused const char *path,
                   1467:     int error, int closed, struct evbuffer *buffer, void *data)
1.228     nicm     1468: {
                   1469:        struct window_pane_input_data   *cdata = data;
                   1470:        struct window_pane              *wp;
1.246     nicm     1471:        u_char                          *buf = EVBUFFER_DATA(buffer);
                   1472:        size_t                           len = EVBUFFER_LENGTH(buffer);
1.228     nicm     1473:
                   1474:        wp = window_pane_find_by_id(cdata->wp);
1.246     nicm     1475:        if (wp == NULL || closed || error != 0 || c->flags & CLIENT_DEAD) {
1.243     nicm     1476:                if (wp == NULL)
                   1477:                        c->flags |= CLIENT_EXIT;
                   1478:
1.246     nicm     1479:                evbuffer_drain(buffer, len);
                   1480:                cmdq_continue(cdata->item);
                   1481:
1.228     nicm     1482:                server_client_unref(c);
                   1483:                free(cdata);
                   1484:                return;
                   1485:        }
1.229     nicm     1486:        input_parse_buffer(wp, buf, len);
1.246     nicm     1487:        evbuffer_drain(buffer, len);
1.228     nicm     1488: }
                   1489:
                   1490: int
                   1491: window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
                   1492:     char **cause)
                   1493: {
1.256     nicm     1494:        struct client                   *c = cmdq_get_client(item);
1.228     nicm     1495:        struct window_pane_input_data   *cdata;
                   1496:
                   1497:        if (~wp->flags & PANE_EMPTY) {
                   1498:                *cause = xstrdup("pane is not empty");
                   1499:                return (-1);
                   1500:        }
                   1501:
                   1502:        cdata = xmalloc(sizeof *cdata);
                   1503:        cdata->item = item;
                   1504:        cdata->wp = wp->id;
                   1505:
1.246     nicm     1506:        c->references++;
                   1507:        file_read(c, "-", window_pane_input_callback, cdata);
                   1508:
                   1509:        return (0);
1.263     nicm     1510: }
                   1511:
                   1512: void *
                   1513: window_pane_get_new_data(struct window_pane *wp,
                   1514:     struct window_pane_offset *wpo, size_t *size)
                   1515: {
                   1516:        size_t  used = wpo->used - wp->base_offset;
                   1517:
                   1518:        *size = EVBUFFER_LENGTH(wp->event->input) - used;
                   1519:        return (EVBUFFER_DATA(wp->event->input) + used);
                   1520: }
                   1521:
                   1522: void
                   1523: window_pane_update_used_data(struct window_pane *wp,
1.264     nicm     1524:     struct window_pane_offset *wpo, size_t size)
1.263     nicm     1525: {
                   1526:        size_t  used = wpo->used - wp->base_offset;
                   1527:
                   1528:        if (size > EVBUFFER_LENGTH(wp->event->input) - used)
                   1529:                size = EVBUFFER_LENGTH(wp->event->input) - used;
                   1530:        wpo->used += size;
1.1       nicm     1531: }