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

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