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

1.172   ! nicm        1: /* $OpenBSD: window.c,v 1.171 2016/10/15 09:27:52 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.156     nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
1.162     nicm       20: #include <sys/ioctl.h>
1.1       nicm       21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
1.5       nicm       24: #include <fnmatch.h>
1.1       nicm       25: #include <stdint.h>
                     26: #include <stdlib.h>
                     27: #include <string.h>
                     28: #include <termios.h>
1.163     nicm       29: #include <time.h>
1.1       nicm       30: #include <unistd.h>
                     31: #include <util.h>
                     32:
                     33: #include "tmux.h"
                     34:
                     35: /*
1.14      nicm       36:  * Each window is attached to a number of panes, each of which is a pty. This
1.1       nicm       37:  * file contains code to handle them.
                     38:  *
                     39:  * A pane has two buffers attached, these are filled and emptied by the main
                     40:  * server poll loop. Output data is received from pty's in screen format,
                     41:  * translated and returned as a series of escape sequences and strings via
                     42:  * input_parse (in input.c). Input data is received as key codes and written
                     43:  * directly via input_key.
                     44:  *
                     45:  * Each pane also has a "virtual" screen (screen.c) which contains the current
                     46:  * state and is redisplayed when the window is reattached to a client.
                     47:  *
                     48:  * Windows are stored directly on a global array and wrapped in any number of
                     49:  * winlink structs to be linked onto local session RB trees. A reference count
                     50:  * is maintained and a window removed from the global list and destroyed when
                     51:  * it reaches zero.
                     52:  */
1.124     nicm       53:
1.1       nicm       54: /* Global window list. */
                     55: struct windows windows;
                     56:
1.64      nicm       57: /* Global panes tree. */
                     58: struct window_pane_tree all_window_panes;
1.166     nicm       59: static u_int   next_window_pane_id;
                     60: static u_int   next_window_id;
                     61: static u_int   next_active_point;
1.37      nicm       62:
1.169     nicm       63: static struct window_pane *window_pane_create(struct window *, u_int, u_int,
                     64:                    u_int);
                     65: static void    window_pane_destroy(struct window_pane *);
                     66:
1.166     nicm       67: static void    window_pane_set_watermark(struct window_pane *, size_t);
                     68:
                     69: static void    window_pane_read_callback(struct bufferevent *, void *);
                     70: static void    window_pane_error_callback(struct bufferevent *, short, void *);
                     71:
1.169     nicm       72: static int     winlink_next_index(struct winlinks *, int);
                     73:
1.166     nicm       74: static struct window_pane *window_pane_choose_best(struct window_pane **,
                     75:                    u_int);
1.109     nicm       76:
1.122     nicm       77: RB_GENERATE(windows, window, entry, window_cmp);
1.169     nicm       78: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
                     79: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
1.122     nicm       80:
                     81: int
                     82: window_cmp(struct window *w1, struct window *w2)
                     83: {
                     84:        return (w1->id - w2->id);
                     85: }
                     86:
1.1       nicm       87: int
                     88: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
                     89: {
                     90:        return (wl1->idx - wl2->idx);
1.12      nicm       91: }
                     92:
1.64      nicm       93: int
                     94: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
                     95: {
                     96:        return (wp1->id - wp2->id);
                     97: }
                     98:
1.12      nicm       99: struct winlink *
                    100: winlink_find_by_window(struct winlinks *wwl, struct window *w)
                    101: {
                    102:        struct winlink  *wl;
                    103:
                    104:        RB_FOREACH(wl, winlinks, wwl) {
                    105:                if (wl->window == w)
                    106:                        return (wl);
                    107:        }
                    108:
                    109:        return (NULL);
1.1       nicm      110: }
                    111:
                    112: struct winlink *
                    113: winlink_find_by_index(struct winlinks *wwl, int idx)
                    114: {
                    115:        struct winlink  wl;
                    116:
                    117:        if (idx < 0)
                    118:                fatalx("bad index");
                    119:
                    120:        wl.idx = idx;
                    121:        return (RB_FIND(winlinks, wwl, &wl));
                    122: }
                    123:
1.71      nicm      124: struct winlink *
                    125: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
                    126: {
                    127:        struct winlink *wl;
                    128:
                    129:        RB_FOREACH(wl, winlinks, wwl) {
                    130:                if (wl->window->id == id)
                    131:                        return (wl);
                    132:        }
1.78      nicm      133:        return (NULL);
1.71      nicm      134: }
                    135:
1.169     nicm      136: static int
1.22      nicm      137: winlink_next_index(struct winlinks *wwl, int idx)
1.1       nicm      138: {
1.22      nicm      139:        int     i;
1.1       nicm      140:
1.22      nicm      141:        i = idx;
                    142:        do {
1.1       nicm      143:                if (winlink_find_by_index(wwl, i) == NULL)
                    144:                        return (i);
1.22      nicm      145:                if (i == INT_MAX)
                    146:                        i = 0;
                    147:                else
                    148:                        i++;
                    149:        } while (i != idx);
                    150:        return (-1);
1.1       nicm      151: }
                    152:
                    153: u_int
                    154: winlink_count(struct winlinks *wwl)
                    155: {
                    156:        struct winlink  *wl;
                    157:        u_int            n;
                    158:
                    159:        n = 0;
                    160:        RB_FOREACH(wl, winlinks, wwl)
                    161:                n++;
                    162:
                    163:        return (n);
                    164: }
                    165:
                    166: struct winlink *
1.63      nicm      167: winlink_add(struct winlinks *wwl, int idx)
1.1       nicm      168: {
                    169:        struct winlink  *wl;
                    170:
1.22      nicm      171:        if (idx < 0) {
                    172:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
                    173:                        return (NULL);
                    174:        } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1       nicm      175:                return (NULL);
                    176:
                    177:        wl = xcalloc(1, sizeof *wl);
                    178:        wl->idx = idx;
                    179:        RB_INSERT(winlinks, wwl, wl);
                    180:
1.63      nicm      181:        return (wl);
                    182: }
                    183:
                    184: void
                    185: winlink_set_window(struct winlink *wl, struct window *w)
                    186: {
                    187:        wl->window = w;
1.1       nicm      188:        w->references++;
                    189: }
                    190:
                    191: void
                    192: winlink_remove(struct winlinks *wwl, struct winlink *wl)
                    193: {
                    194:        struct window   *w = wl->window;
                    195:
                    196:        RB_REMOVE(winlinks, wwl, wl);
1.82      nicm      197:        free(wl->status_text);
                    198:        free(wl);
1.1       nicm      199:
1.84      nicm      200:        if (w != NULL)
                    201:                window_remove_ref(w);
1.1       nicm      202: }
                    203:
                    204: struct winlink *
1.41      nicm      205: winlink_next(struct winlink *wl)
1.1       nicm      206: {
                    207:        return (RB_NEXT(winlinks, wwl, wl));
                    208: }
                    209:
                    210: struct winlink *
1.41      nicm      211: winlink_previous(struct winlink *wl)
1.1       nicm      212: {
                    213:        return (RB_PREV(winlinks, wwl, wl));
1.52      nicm      214: }
                    215:
                    216: struct winlink *
1.53      nicm      217: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      218: {
                    219:        for (; n > 0; n--) {
                    220:                if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53      nicm      221:                        wl = RB_MIN(winlinks, &s->windows);
1.52      nicm      222:        }
                    223:
                    224:        return (wl);
                    225: }
                    226:
                    227: struct winlink *
1.53      nicm      228: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      229: {
                    230:        for (; n > 0; n--) {
                    231:                if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53      nicm      232:                        wl = RB_MAX(winlinks, &s->windows);
1.52      nicm      233:        }
                    234:
                    235:        return (wl);
1.1       nicm      236: }
                    237:
                    238: void
                    239: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    240: {
                    241:        if (wl == NULL)
                    242:                return;
                    243:
                    244:        winlink_stack_remove(stack, wl);
1.28      nicm      245:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1       nicm      246: }
                    247:
                    248: void
                    249: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    250: {
                    251:        struct winlink  *wl2;
                    252:
                    253:        if (wl == NULL)
                    254:                return;
1.42      nicm      255:
1.28      nicm      256:        TAILQ_FOREACH(wl2, stack, sentry) {
1.1       nicm      257:                if (wl2 == wl) {
1.28      nicm      258:                        TAILQ_REMOVE(stack, wl, sentry);
1.1       nicm      259:                        return;
                    260:                }
                    261:        }
                    262: }
                    263:
                    264: struct window *
1.125     nicm      265: window_find_by_id_str(const char *s)
1.123     nicm      266: {
                    267:        const char      *errstr;
                    268:        u_int            id;
                    269:
                    270:        if (*s != '@')
                    271:                return (NULL);
                    272:
                    273:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    274:        if (errstr != NULL)
                    275:                return (NULL);
                    276:        return (window_find_by_id(id));
                    277: }
                    278:
                    279: struct window *
1.71      nicm      280: window_find_by_id(u_int id)
                    281: {
1.122     nicm      282:        struct window   w;
1.71      nicm      283:
1.122     nicm      284:        w.id = id;
                    285:        return (RB_FIND(windows, &windows, &w));
1.71      nicm      286: }
                    287:
1.143     nicm      288: void
                    289: window_update_activity(struct window *w)
                    290: {
                    291:        gettimeofday(&w->activity_time, NULL);
                    292:        alerts_queue(w, WINDOW_ACTIVITY);
                    293: }
                    294:
1.71      nicm      295: struct window *
1.171     nicm      296: window_create(u_int sx, u_int sy)
1.1       nicm      297: {
                    298:        struct window   *w;
                    299:
1.38      nicm      300:        w = xcalloc(1, sizeof *w);
1.1       nicm      301:        w->name = NULL;
1.160     nicm      302:        w->flags = WINDOW_STYLECHANGED;
1.1       nicm      303:
                    304:        TAILQ_INIT(&w->panes);
                    305:        w->active = NULL;
1.14      nicm      306:
1.17      nicm      307:        w->lastlayout = -1;
1.14      nicm      308:        w->layout_root = NULL;
1.42      nicm      309:
1.1       nicm      310:        w->sx = sx;
                    311:        w->sy = sy;
1.133     nicm      312:
1.146     nicm      313:        w->options = options_create(global_w_options);
1.1       nicm      314:
                    315:        w->references = 0;
                    316:
1.122     nicm      317:        w->id = next_window_id++;
1.129     nicm      318:        RB_INSERT(windows, &windows, w);
1.122     nicm      319:
1.143     nicm      320:        window_update_activity(w);
                    321:
1.1       nicm      322:        return (w);
                    323: }
                    324:
                    325: struct window *
1.171     nicm      326: window_create_spawn(const char *name, int argc, char **argv, const char *path,
1.147     nicm      327:     const char *shell, const char *cwd, struct environ *env,
                    328:     struct termios *tio, u_int sx, u_int sy, u_int hlimit, char **cause)
1.1       nicm      329: {
1.14      nicm      330:        struct window           *w;
                    331:        struct window_pane      *wp;
1.1       nicm      332:
1.171     nicm      333:        w = window_create(sx, sy);
1.161     nicm      334:        wp = window_add_pane(w, NULL, hlimit);
1.93      nicm      335:        layout_init(w, wp);
1.91      nicm      336:
1.171     nicm      337:        if (window_pane_spawn(wp, argc, argv, path, shell, cwd,
                    338:            env, tio, cause) != 0) {
1.1       nicm      339:                window_destroy(w);
                    340:                return (NULL);
                    341:        }
1.91      nicm      342:
1.1       nicm      343:        w->active = TAILQ_FIRST(&w->panes);
                    344:        if (name != NULL) {
                    345:                w->name = xstrdup(name);
1.146     nicm      346:                options_set_number(w->options, "automatic-rename", 0);
1.1       nicm      347:        } else
                    348:                w->name = default_window_name(w);
1.91      nicm      349:
1.1       nicm      350:        return (w);
                    351: }
                    352:
                    353: void
                    354: window_destroy(struct window *w)
                    355: {
1.122     nicm      356:        RB_REMOVE(windows, &windows, w);
1.1       nicm      357:
1.14      nicm      358:        if (w->layout_root != NULL)
1.135     nicm      359:                layout_free_cell(w->layout_root);
                    360:        if (w->saved_layout_root != NULL)
                    361:                layout_free_cell(w->saved_layout_root);
1.127     nicm      362:        free(w->old_layout);
1.139     nicm      363:
1.141     nicm      364:        if (event_initialized(&w->name_event))
                    365:                evtimer_del(&w->name_event);
1.38      nicm      366:
1.143     nicm      367:        if (event_initialized(&w->alerts_timer))
                    368:                evtimer_del(&w->alerts_timer);
                    369:
1.146     nicm      370:        options_free(w->options);
1.1       nicm      371:
                    372:        window_destroy_panes(w);
                    373:
1.82      nicm      374:        free(w->name);
                    375:        free(w);
1.84      nicm      376: }
                    377:
                    378: void
                    379: window_remove_ref(struct window *w)
                    380: {
                    381:        if (w->references == 0)
                    382:                fatal("bad reference count");
                    383:        w->references--;
                    384:        if (w->references == 0)
                    385:                window_destroy(w);
1.72      nicm      386: }
                    387:
                    388: void
                    389: window_set_name(struct window *w, const char *new_name)
                    390: {
1.82      nicm      391:        free(w->name);
1.72      nicm      392:        w->name = xstrdup(new_name);
1.172   ! nicm      393:        notify_window("window-renamed", w);
1.1       nicm      394: }
                    395:
1.15      nicm      396: void
1.1       nicm      397: window_resize(struct window *w, u_int sx, u_int sy)
                    398: {
                    399:        w->sx = sx;
                    400:        w->sy = sy;
                    401: }
                    402:
1.114     nicm      403: int
1.118     nicm      404: window_has_pane(struct window *w, struct window_pane *wp)
                    405: {
                    406:        struct window_pane      *wp1;
                    407:
                    408:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    409:                if (wp1 == wp)
                    410:                        return (1);
                    411:        }
                    412:        return (0);
                    413: }
                    414:
                    415: int
1.1       nicm      416: window_set_active_pane(struct window *w, struct window_pane *wp)
                    417: {
1.59      nicm      418:        if (wp == w->active)
1.114     nicm      419:                return (0);
1.58      nicm      420:        w->last = w->active;
1.1       nicm      421:        w->active = wp;
1.10      nicm      422:        while (!window_pane_visible(w->active)) {
1.1       nicm      423:                w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10      nicm      424:                if (w->active == NULL)
                    425:                        w->active = TAILQ_LAST(&w->panes, window_panes);
                    426:                if (w->active == wp)
1.114     nicm      427:                        return (1);
1.29      nicm      428:        }
1.109     nicm      429:        w->active->active_point = next_active_point++;
1.136     nicm      430:        w->active->flags |= PANE_CHANGED;
1.114     nicm      431:        return (1);
1.145     nicm      432: }
                    433:
                    434: void
                    435: window_redraw_active_switch(struct window *w, struct window_pane *wp)
                    436: {
                    437:        const struct grid_cell  *agc, *wgc;
                    438:
                    439:        if (wp == w->active)
                    440:                return;
                    441:
                    442:        /*
                    443:         * If window-style and window-active-style are the same, we don't need
                    444:         * to redraw panes when switching active panes. Otherwise, if the
                    445:         * active or inactive pane do not have a custom style, they will need
                    446:         * to be redrawn.
                    447:         */
1.146     nicm      448:        agc = options_get_style(w->options, "window-active-style");
                    449:        wgc = options_get_style(w->options, "window-style");
1.145     nicm      450:        if (style_equal(agc, wgc))
                    451:                return;
                    452:        if (style_equal(&grid_default_cell, &w->active->colgc))
                    453:                w->active->flags |= PANE_REDRAW;
                    454:        if (style_equal(&grid_default_cell, &wp->colgc))
                    455:                wp->flags |= PANE_REDRAW;
1.29      nicm      456: }
                    457:
1.66      nicm      458: struct window_pane *
                    459: window_get_active_at(struct window *w, u_int x, u_int y)
1.29      nicm      460: {
                    461:        struct window_pane      *wp;
                    462:
                    463:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.66      nicm      464:                if (!window_pane_visible(wp))
1.29      nicm      465:                        continue;
1.66      nicm      466:                if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29      nicm      467:                        continue;
1.66      nicm      468:                if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29      nicm      469:                        continue;
1.66      nicm      470:                return (wp);
                    471:        }
                    472:        return (NULL);
                    473: }
                    474:
                    475: struct window_pane *
                    476: window_find_string(struct window *w, const char *s)
                    477: {
                    478:        u_int   x, y;
                    479:
                    480:        x = w->sx / 2;
                    481:        y = w->sy / 2;
                    482:
                    483:        if (strcasecmp(s, "top") == 0)
                    484:                y = 0;
                    485:        else if (strcasecmp(s, "bottom") == 0)
                    486:                y = w->sy - 1;
                    487:        else if (strcasecmp(s, "left") == 0)
                    488:                x = 0;
                    489:        else if (strcasecmp(s, "right") == 0)
                    490:                x = w->sx - 1;
                    491:        else if (strcasecmp(s, "top-left") == 0) {
                    492:                x = 0;
                    493:                y = 0;
                    494:        } else if (strcasecmp(s, "top-right") == 0) {
                    495:                x = w->sx - 1;
                    496:                y = 0;
                    497:        } else if (strcasecmp(s, "bottom-left") == 0) {
                    498:                x = 0;
                    499:                y = w->sy - 1;
                    500:        } else if (strcasecmp(s, "bottom-right") == 0) {
                    501:                x = w->sx - 1;
                    502:                y = w->sy - 1;
                    503:        } else
                    504:                return (NULL);
                    505:
                    506:        return (window_get_active_at(w, x, y));
1.1       nicm      507: }
                    508:
1.93      nicm      509: int
                    510: window_zoom(struct window_pane *wp)
                    511: {
                    512:        struct window           *w = wp->window;
                    513:        struct window_pane      *wp1;
                    514:
                    515:        if (w->flags & WINDOW_ZOOMED)
                    516:                return (-1);
                    517:
                    518:        if (!window_pane_visible(wp))
                    519:                return (-1);
1.94      nicm      520:
                    521:        if (window_count_panes(w) == 1)
                    522:                return (-1);
                    523:
1.93      nicm      524:        if (w->active != wp)
                    525:                window_set_active_pane(w, wp);
                    526:
                    527:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    528:                wp1->saved_layout_cell = wp1->layout_cell;
                    529:                wp1->layout_cell = NULL;
                    530:        }
                    531:
                    532:        w->saved_layout_root = w->layout_root;
                    533:        layout_init(w, wp);
                    534:        w->flags |= WINDOW_ZOOMED;
1.172   ! nicm      535:        notify_window("window-layout-changed", w);
1.93      nicm      536:
                    537:        return (0);
                    538: }
                    539:
                    540: int
                    541: window_unzoom(struct window *w)
                    542: {
1.97      nicm      543:        struct window_pane      *wp;
1.93      nicm      544:
                    545:        if (!(w->flags & WINDOW_ZOOMED))
                    546:                return (-1);
                    547:
                    548:        w->flags &= ~WINDOW_ZOOMED;
                    549:        layout_free(w);
                    550:        w->layout_root = w->saved_layout_root;
1.120     nicm      551:        w->saved_layout_root = NULL;
1.93      nicm      552:
1.97      nicm      553:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    554:                wp->layout_cell = wp->saved_layout_cell;
                    555:                wp->saved_layout_cell = NULL;
1.93      nicm      556:        }
                    557:        layout_fix_panes(w, w->sx, w->sy);
1.172   ! nicm      558:        notify_window("window-layout-changed", w);
1.93      nicm      559:
                    560:        return (0);
                    561: }
                    562:
1.1       nicm      563: struct window_pane *
1.161     nicm      564: window_add_pane(struct window *w, struct window_pane *after, u_int hlimit)
1.1       nicm      565: {
                    566:        struct window_pane      *wp;
                    567:
1.14      nicm      568:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1       nicm      569:        if (TAILQ_EMPTY(&w->panes))
                    570:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
1.161     nicm      571:        else {
                    572:                if (after == NULL)
                    573:                        TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
                    574:                else
                    575:                        TAILQ_INSERT_AFTER(&w->panes, after, wp, entry);
                    576:        }
1.1       nicm      577:        return (wp);
                    578: }
                    579:
                    580: void
1.105     nicm      581: window_lost_pane(struct window *w, struct window_pane *wp)
1.1       nicm      582: {
1.152     nicm      583:        if (wp == marked_pane.wp)
1.132     nicm      584:                server_clear_marked();
                    585:
1.57      nicm      586:        if (wp == w->active) {
1.58      nicm      587:                w->active = w->last;
                    588:                w->last = NULL;
                    589:                if (w->active == NULL) {
                    590:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    591:                        if (w->active == NULL)
                    592:                                w->active = TAILQ_NEXT(wp, entry);
                    593:                }
1.151     nicm      594:                if (w->active != NULL)
                    595:                        w->active->flags |= PANE_CHANGED;
1.58      nicm      596:        } else if (wp == w->last)
                    597:                w->last = NULL;
1.105     nicm      598: }
                    599:
                    600: void
                    601: window_remove_pane(struct window *w, struct window_pane *wp)
                    602: {
                    603:        window_lost_pane(w, wp);
1.1       nicm      604:
                    605:        TAILQ_REMOVE(&w->panes, wp, entry);
                    606:        window_pane_destroy(wp);
                    607: }
                    608:
                    609: struct window_pane *
                    610: window_pane_at_index(struct window *w, u_int idx)
                    611: {
                    612:        struct window_pane      *wp;
                    613:        u_int                    n;
                    614:
1.146     nicm      615:        n = options_get_number(w->options, "pane-base-index");
1.1       nicm      616:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    617:                if (n == idx)
                    618:                        return (wp);
                    619:                n++;
                    620:        }
                    621:        return (NULL);
1.53      nicm      622: }
                    623:
                    624: struct window_pane *
                    625: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    626: {
                    627:        for (; n > 0; n--) {
                    628:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    629:                        wp = TAILQ_FIRST(&w->panes);
                    630:        }
                    631:
                    632:        return (wp);
                    633: }
                    634:
                    635: struct window_pane *
                    636: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    637:     u_int n)
                    638: {
                    639:        for (; n > 0; n--) {
                    640:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    641:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    642:        }
                    643:
                    644:        return (wp);
1.13      nicm      645: }
                    646:
1.69      nicm      647: int
                    648: window_pane_index(struct window_pane *wp, u_int *i)
1.13      nicm      649: {
                    650:        struct window_pane      *wq;
1.69      nicm      651:        struct window           *w = wp->window;
1.13      nicm      652:
1.146     nicm      653:        *i = options_get_number(w->options, "pane-base-index");
1.13      nicm      654:        TAILQ_FOREACH(wq, &w->panes, entry) {
1.69      nicm      655:                if (wp == wq) {
                    656:                        return (0);
                    657:                }
                    658:                (*i)++;
1.13      nicm      659:        }
1.69      nicm      660:
                    661:        return (-1);
1.1       nicm      662: }
                    663:
                    664: u_int
                    665: window_count_panes(struct window *w)
                    666: {
                    667:        struct window_pane      *wp;
                    668:        u_int                    n;
                    669:
                    670:        n = 0;
                    671:        TAILQ_FOREACH(wp, &w->panes, entry)
                    672:                n++;
                    673:        return (n);
                    674: }
                    675:
                    676: void
                    677: window_destroy_panes(struct window *w)
                    678: {
                    679:        struct window_pane      *wp;
                    680:
                    681:        while (!TAILQ_EMPTY(&w->panes)) {
                    682:                wp = TAILQ_FIRST(&w->panes);
                    683:                TAILQ_REMOVE(&w->panes, wp, entry);
                    684:                window_pane_destroy(wp);
                    685:        }
1.61      nicm      686: }
                    687:
1.128     nicm      688: /* Retuns the printable flags on a window, empty string if no flags set. */
1.61      nicm      689: char *
                    690: window_printable_flags(struct session *s, struct winlink *wl)
                    691: {
1.119     nicm      692:        char    flags[32];
1.61      nicm      693:        int     pos;
                    694:
                    695:        pos = 0;
                    696:        if (wl->flags & WINLINK_ACTIVITY)
                    697:                flags[pos++] = '#';
                    698:        if (wl->flags & WINLINK_BELL)
                    699:                flags[pos++] = '!';
                    700:        if (wl->flags & WINLINK_SILENCE)
                    701:                flags[pos++] = '~';
                    702:        if (wl == s->curw)
                    703:                flags[pos++] = '*';
                    704:        if (wl == TAILQ_FIRST(&s->lastw))
                    705:                flags[pos++] = '-';
1.152     nicm      706:        if (server_check_marked() && wl == marked_pane.wl)
1.132     nicm      707:                flags[pos++] = 'M';
1.93      nicm      708:        if (wl->window->flags & WINDOW_ZOOMED)
                    709:                flags[pos++] = 'Z';
1.61      nicm      710:        flags[pos] = '\0';
                    711:        return (xstrdup(flags));
1.1       nicm      712: }
                    713:
1.123     nicm      714: struct window_pane *
                    715: window_pane_find_by_id_str(const char *s)
                    716: {
                    717:        const char      *errstr;
                    718:        u_int            id;
                    719:
                    720:        if (*s != '%')
                    721:                return (NULL);
                    722:
                    723:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    724:        if (errstr != NULL)
                    725:                return (NULL);
                    726:        return (window_pane_find_by_id(id));
                    727: }
                    728:
1.64      nicm      729: struct window_pane *
                    730: window_pane_find_by_id(u_int id)
                    731: {
                    732:        struct window_pane      wp;
                    733:
                    734:        wp.id = id;
                    735:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
                    736: }
                    737:
1.169     nicm      738: static struct window_pane *
1.1       nicm      739: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    740: {
                    741:        struct window_pane      *wp;
1.140     nicm      742:        char                     host[HOST_NAME_MAX + 1];
1.1       nicm      743:
                    744:        wp = xcalloc(1, sizeof *wp);
                    745:        wp->window = w;
                    746:
1.71      nicm      747:        wp->id = next_window_pane_id++;
1.64      nicm      748:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
                    749:
1.110     nicm      750:        wp->argc = 0;
                    751:        wp->argv = NULL;
1.23      nicm      752:        wp->shell = NULL;
1.147     nicm      753:        wp->cwd = NULL;
1.1       nicm      754:
                    755:        wp->fd = -1;
1.37      nicm      756:        wp->event = NULL;
1.1       nicm      757:
                    758:        wp->mode = NULL;
1.14      nicm      759:
                    760:        wp->layout_cell = NULL;
1.1       nicm      761:
                    762:        wp->xoff = 0;
1.42      nicm      763:        wp->yoff = 0;
1.1       nicm      764:
                    765:        wp->sx = sx;
                    766:        wp->sy = sy;
                    767:
1.32      nicm      768:        wp->pipe_fd = -1;
                    769:        wp->pipe_off = 0;
1.36      nicm      770:        wp->pipe_event = NULL;
1.32      nicm      771:
1.9       nicm      772:        wp->saved_grid = NULL;
1.117     nicm      773:
                    774:        memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
1.9       nicm      775:
1.1       nicm      776:        screen_init(&wp->base, sx, sy, hlimit);
                    777:        wp->screen = &wp->base;
1.159     nicm      778:
                    779:        screen_init(&wp->status_screen, 1, 1, 0);
1.140     nicm      780:
                    781:        if (gethostname(host, sizeof host) == 0)
                    782:                screen_set_title(&wp->base, host);
1.1       nicm      783:
                    784:        input_init(wp);
                    785:
                    786:        return (wp);
                    787: }
                    788:
1.169     nicm      789: static void
1.1       nicm      790: window_pane_destroy(struct window_pane *wp)
                    791: {
1.55      nicm      792:        window_pane_reset_mode(wp);
                    793:
1.37      nicm      794:        if (wp->fd != -1) {
1.70      nicm      795:                bufferevent_free(wp->event);
1.1       nicm      796:                close(wp->fd);
1.37      nicm      797:        }
1.1       nicm      798:
                    799:        input_free(wp);
                    800:
                    801:        screen_free(&wp->base);
1.9       nicm      802:        if (wp->saved_grid != NULL)
                    803:                grid_destroy(wp->saved_grid);
1.1       nicm      804:
1.32      nicm      805:        if (wp->pipe_fd != -1) {
1.70      nicm      806:                bufferevent_free(wp->pipe_event);
1.32      nicm      807:                close(wp->pipe_fd);
                    808:        }
1.167     nicm      809:
                    810:        if (event_initialized(&wp->resize_timer))
                    811:                event_del(&wp->resize_timer);
1.32      nicm      812:
1.64      nicm      813:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    814:
1.147     nicm      815:        free((void *)wp->cwd);
1.82      nicm      816:        free(wp->shell);
1.110     nicm      817:        cmd_free_argv(wp->argc, wp->argv);
1.82      nicm      818:        free(wp);
1.1       nicm      819: }
                    820:
1.166     nicm      821: static void
                    822: window_pane_set_watermark(struct window_pane *wp, size_t size)
                    823: {
                    824:        wp->wmark_hits = 0;
                    825:        wp->wmark_size = size;
                    826:        bufferevent_setwatermark(wp->event, EV_READ, 0, size);
                    827: }
                    828:
1.1       nicm      829: int
1.110     nicm      830: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
1.147     nicm      831:     const char *path, const char *shell, const char *cwd, struct environ *env,
1.110     nicm      832:     struct termios *tio, char **cause)
1.1       nicm      833: {
1.47      nicm      834:        struct winsize   ws;
1.150     nicm      835:        char            *argv0, *cmd, **argvp;
1.147     nicm      836:        const char      *ptr, *first, *home;
1.47      nicm      837:        struct termios   tio2;
1.110     nicm      838:        int              i;
1.1       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.110     nicm      844:        if (argc > 0) {
                    845:                cmd_free_argv(wp->argc, wp->argv);
                    846:                wp->argc = argc;
                    847:                wp->argv = cmd_copy_argv(argc, argv);
1.1       nicm      848:        }
1.23      nicm      849:        if (shell != NULL) {
1.82      nicm      850:                free(wp->shell);
1.23      nicm      851:                wp->shell = xstrdup(shell);
                    852:        }
1.147     nicm      853:        if (cwd != NULL) {
                    854:                free((void *)wp->cwd);
                    855:                wp->cwd = xstrdup(cwd);
1.1       nicm      856:        }
1.91      nicm      857:
1.110     nicm      858:        cmd = cmd_stringify_argv(wp->argc, wp->argv);
                    859:        log_debug("spawn: %s -- %s", wp->shell, cmd);
                    860:        for (i = 0; i < wp->argc; i++)
                    861:                log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
1.165     nicm      862:        environ_log(env, "spawn: ");
1.1       nicm      863:
                    864:        memset(&ws, 0, sizeof ws);
                    865:        ws.ws_col = screen_size_x(&wp->base);
                    866:        ws.ws_row = screen_size_y(&wp->base);
                    867:
1.42      nicm      868:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      869:        case -1:
                    870:                wp->fd = -1;
                    871:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110     nicm      872:                free(cmd);
1.1       nicm      873:                return (-1);
                    874:        case 0:
1.147     nicm      875:                if (chdir(wp->cwd) != 0) {
                    876:                        if ((home = find_home()) == NULL || chdir(home) != 0)
                    877:                                chdir("/");
                    878:                }
1.25      nicm      879:
                    880:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    881:                        fatal("tcgetattr failed");
                    882:                if (tio != NULL)
                    883:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    884:                tio2.c_cc[VERASE] = '\177';
                    885:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    886:                        fatal("tcgetattr failed");
1.18      nicm      887:
1.56      nicm      888:                closefrom(STDERR_FILENO + 1);
                    889:
1.107     nicm      890:                if (path != NULL)
1.150     nicm      891:                        environ_set(env, "PATH", "%s", path);
                    892:                environ_set(env, "TMUX_PANE", "%%%u", wp->id);
1.47      nicm      893:                environ_push(env);
1.18      nicm      894:
1.54      nicm      895:                clear_signals(1);
1.1       nicm      896:                log_close();
                    897:
1.80      nicm      898:                setenv("SHELL", wp->shell, 1);
                    899:                ptr = strrchr(wp->shell, '/');
                    900:
1.110     nicm      901:                /*
                    902:                 * If given one argument, assume it should be passed to sh -c;
                    903:                 * with more than one argument, use execvp(). If there is no
                    904:                 * arguments, create a login shell.
                    905:                 */
                    906:                if (wp->argc > 0) {
                    907:                        if (wp->argc != 1) {
                    908:                                /* Copy to ensure argv ends in NULL. */
                    909:                                argvp = cmd_copy_argv(wp->argc, wp->argv);
                    910:                                execvp(argvp[0], argvp);
                    911:                                fatal("execvp failed");
                    912:                        }
                    913:                        first = wp->argv[0];
                    914:
1.80      nicm      915:                        if (ptr != NULL && *(ptr + 1) != '\0')
                    916:                                xasprintf(&argv0, "%s", ptr + 1);
                    917:                        else
                    918:                                xasprintf(&argv0, "%s", wp->shell);
1.110     nicm      919:                        execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8       nicm      920:                        fatal("execl failed");
                    921:                }
1.23      nicm      922:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      923:                        xasprintf(&argv0, "-%s", ptr + 1);
                    924:                else
1.23      nicm      925:                        xasprintf(&argv0, "-%s", wp->shell);
1.110     nicm      926:                execl(wp->shell, argv0, (char *)NULL);
1.1       nicm      927:                fatal("execl failed");
                    928:        }
                    929:
1.62      nicm      930:        setblocking(wp->fd, 0);
                    931:
1.110     nicm      932:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
                    933:            window_pane_error_callback, wp);
1.131     nicm      934:
1.166     nicm      935:        window_pane_set_watermark(wp, READ_FAST_SIZE);
1.37      nicm      936:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      937:
1.110     nicm      938:        free(cmd);
1.1       nicm      939:        return (0);
                    940: }
                    941:
1.166     nicm      942: static void
1.149     nicm      943: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37      nicm      944: {
1.131     nicm      945:        struct window_pane      *wp = data;
                    946:        struct evbuffer         *evb = wp->event->input;
1.166     nicm      947:        size_t                   size = EVBUFFER_LENGTH(evb);
1.131     nicm      948:        char                    *new_data;
1.158     nicm      949:        size_t                   new_size;
1.131     nicm      950:
1.166     nicm      951:        if (wp->wmark_size == READ_FAST_SIZE) {
                    952:                if (size > READ_FULL_SIZE)
                    953:                        wp->wmark_hits++;
                    954:                if (wp->wmark_hits == READ_CHANGE_HITS)
                    955:                        window_pane_set_watermark(wp, READ_SLOW_SIZE);
                    956:        } else if (wp->wmark_size == READ_SLOW_SIZE) {
                    957:                if (size < READ_EMPTY_SIZE)
                    958:                        wp->wmark_hits++;
                    959:                if (wp->wmark_hits == READ_CHANGE_HITS)
                    960:                        window_pane_set_watermark(wp, READ_FAST_SIZE);
                    961:        }
                    962:        log_debug("%%%u has %zu bytes (of %u, %u hits)", wp->id, size,
                    963:            wp->wmark_size, wp->wmark_hits);
1.131     nicm      964:
1.166     nicm      965:        new_size = size - wp->pipe_off;
1.46      nicm      966:        if (wp->pipe_fd != -1 && new_size > 0) {
1.155     nicm      967:                new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
1.46      nicm      968:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    969:        }
                    970:
                    971:        input_parse(wp);
1.37      nicm      972:
1.166     nicm      973:        wp->pipe_off = size;
1.37      nicm      974: }
                    975:
1.166     nicm      976: static void
1.149     nicm      977: window_pane_error_callback(__unused struct bufferevent *bufev,
                    978:     __unused short what, void *data)
1.37      nicm      979: {
                    980:        struct window_pane *wp = data;
                    981:
1.153     nicm      982:        server_destroy_pane(wp, 1);
1.37      nicm      983: }
                    984:
                    985: void
1.1       nicm      986: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    987: {
                    988:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      989:                return;
1.1       nicm      990:        wp->sx = sx;
                    991:        wp->sy = sy;
                    992:
1.89      nicm      993:        screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1       nicm      994:        if (wp->mode != NULL)
                    995:                wp->mode->resize(wp, sx, sy);
1.95      nicm      996:
                    997:        wp->flags |= PANE_RESIZE;
1.44      nicm      998: }
                    999:
                   1000: /*
                   1001:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                   1002:  * history is not updated
                   1003:  */
                   1004: void
1.87      nicm     1005: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
                   1006:     int cursor)
1.44      nicm     1007: {
                   1008:        struct screen   *s = &wp->base;
                   1009:        u_int            sx, sy;
                   1010:
                   1011:        if (wp->saved_grid != NULL)
                   1012:                return;
1.146     nicm     1013:        if (!options_get_number(wp->window->options, "alternate-screen"))
1.44      nicm     1014:                return;
                   1015:        sx = screen_size_x(s);
                   1016:        sy = screen_size_y(s);
                   1017:
                   1018:        wp->saved_grid = grid_create(sx, sy, 0);
                   1019:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87      nicm     1020:        if (cursor) {
                   1021:                wp->saved_cx = s->cx;
                   1022:                wp->saved_cy = s->cy;
                   1023:        }
1.44      nicm     1024:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                   1025:
1.170     nicm     1026:        grid_view_clear(s->grid, 0, 0, sx, sy, 8);
1.44      nicm     1027:
                   1028:        wp->base.grid->flags &= ~GRID_HISTORY;
                   1029:
                   1030:        wp->flags |= PANE_REDRAW;
                   1031: }
                   1032:
                   1033: /* Exit alternate screen mode and restore the copied grid. */
                   1034: void
1.87      nicm     1035: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
                   1036:     int cursor)
1.44      nicm     1037: {
                   1038:        struct screen   *s = &wp->base;
                   1039:        u_int            sx, sy;
                   1040:
                   1041:        if (wp->saved_grid == NULL)
                   1042:                return;
1.146     nicm     1043:        if (!options_get_number(wp->window->options, "alternate-screen"))
1.44      nicm     1044:                return;
                   1045:        sx = screen_size_x(s);
                   1046:        sy = screen_size_y(s);
                   1047:
                   1048:        /*
                   1049:         * If the current size is bigger, temporarily resize to the old size
                   1050:         * before copying back.
                   1051:         */
                   1052:        if (sy > wp->saved_grid->sy)
1.89      nicm     1053:                screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44      nicm     1054:
                   1055:        /* Restore the grid, cursor position and cell. */
                   1056:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87      nicm     1057:        if (cursor)
                   1058:                s->cx = wp->saved_cx;
1.44      nicm     1059:        if (s->cx > screen_size_x(s) - 1)
                   1060:                s->cx = screen_size_x(s) - 1;
1.87      nicm     1061:        if (cursor)
                   1062:                s->cy = wp->saved_cy;
1.44      nicm     1063:        if (s->cy > screen_size_y(s) - 1)
                   1064:                s->cy = screen_size_y(s) - 1;
                   1065:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                   1066:
                   1067:        /*
                   1068:         * Turn history back on (so resize can use it) and then resize back to
                   1069:         * the current size.
                   1070:         */
                   1071:        wp->base.grid->flags |= GRID_HISTORY;
1.89      nicm     1072:        if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
                   1073:                screen_resize(s, sx, sy, 1);
1.44      nicm     1074:
                   1075:        grid_destroy(wp->saved_grid);
                   1076:        wp->saved_grid = NULL;
                   1077:
                   1078:        wp->flags |= PANE_REDRAW;
1.1       nicm     1079: }
                   1080:
1.162     nicm     1081: static void
                   1082: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
                   1083: {
                   1084:        struct window_pane      *wp = arg;
                   1085:        struct timeval           tv = { .tv_sec = 10 };
                   1086:        int                      n = 0;
                   1087:
                   1088:        evtimer_del(&wp->modetimer);
                   1089:        evtimer_add(&wp->modetimer, &tv);
                   1090:
                   1091:        log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
                   1092:
                   1093:        if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
                   1094:                if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
                   1095:                        window_pane_reset_mode(wp);
                   1096:        }
                   1097: }
                   1098:
1.1       nicm     1099: int
                   1100: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                   1101: {
                   1102:        struct screen   *s;
1.162     nicm     1103:        struct timeval   tv = { .tv_sec = 10 };
1.1       nicm     1104:
1.15      nicm     1105:        if (wp->mode != NULL)
1.1       nicm     1106:                return (1);
                   1107:        wp->mode = mode;
                   1108:
1.162     nicm     1109:        wp->modelast = time(NULL);
                   1110:        evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
                   1111:        evtimer_add(&wp->modetimer, &tv);
                   1112:
1.1       nicm     1113:        if ((s = wp->mode->init(wp)) != NULL)
                   1114:                wp->screen = s;
1.142     nicm     1115:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1116:
                   1117:        server_status_window(wp->window);
1.1       nicm     1118:        return (0);
                   1119: }
                   1120:
                   1121: void
                   1122: window_pane_reset_mode(struct window_pane *wp)
                   1123: {
                   1124:        if (wp->mode == NULL)
                   1125:                return;
                   1126:
1.162     nicm     1127:        evtimer_del(&wp->modetimer);
                   1128:
1.1       nicm     1129:        wp->mode->free(wp);
                   1130:        wp->mode = NULL;
1.168     nicm     1131:        wp->modeprefix = 1;
1.1       nicm     1132:
                   1133:        wp->screen = &wp->base;
1.142     nicm     1134:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1135:
                   1136:        server_status_window(wp->window);
1.1       nicm     1137: }
                   1138:
                   1139: void
1.118     nicm     1140: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.148     nicm     1141:     key_code key, struct mouse_event *m)
1.1       nicm     1142: {
1.27      nicm     1143:        struct window_pane      *wp2;
1.3       nicm     1144:
1.118     nicm     1145:        if (KEYC_IS_MOUSE(key) && m == NULL)
                   1146:                return;
                   1147:
1.1       nicm     1148:        if (wp->mode != NULL) {
1.162     nicm     1149:                wp->modelast = time(NULL);
1.1       nicm     1150:                if (wp->mode->key != NULL)
1.118     nicm     1151:                        wp->mode->key(wp, c, s, key, m);
1.27      nicm     1152:                return;
1.30      nicm     1153:        }
1.27      nicm     1154:
1.113     nicm     1155:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30      nicm     1156:                return;
1.113     nicm     1157:
1.118     nicm     1158:        input_key(wp, key, m);
                   1159:
                   1160:        if (KEYC_IS_MOUSE(key))
                   1161:                return;
1.146     nicm     1162:        if (options_get_number(wp->window->options, "synchronize-panes")) {
1.27      nicm     1163:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1164:                        if (wp2 == wp || wp2->mode != NULL)
                   1165:                                continue;
1.154     nicm     1166:                        if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF)
                   1167:                                continue;
                   1168:                        if (window_pane_visible(wp2))
1.118     nicm     1169:                                input_key(wp2, key, NULL);
1.27      nicm     1170:                }
                   1171:        }
1.10      nicm     1172: }
                   1173:
                   1174: int
                   1175: window_pane_visible(struct window_pane *wp)
                   1176: {
                   1177:        struct window   *w = wp->window;
                   1178:
1.93      nicm     1179:        if (wp->layout_cell == NULL)
                   1180:                return (0);
1.10      nicm     1181:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                   1182:                return (0);
                   1183:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                   1184:                return (0);
                   1185:        return (1);
1.1       nicm     1186: }
                   1187:
                   1188: char *
1.108     nicm     1189: window_pane_search(struct window_pane *wp, const char *searchstr,
                   1190:     u_int *lineno)
1.1       nicm     1191: {
1.4       nicm     1192:        struct screen   *s = &wp->base;
1.5       nicm     1193:        char            *newsearchstr, *line, *msg;
1.4       nicm     1194:        u_int            i;
                   1195:
1.5       nicm     1196:        msg = NULL;
                   1197:        xasprintf(&newsearchstr, "*%s*", searchstr);
                   1198:
1.4       nicm     1199:        for (i = 0; i < screen_size_y(s); i++) {
                   1200:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm     1201:                if (fnmatch(newsearchstr, line, 0) == 0) {
                   1202:                        msg = line;
                   1203:                        if (lineno != NULL)
                   1204:                                *lineno = i;
                   1205:                        break;
                   1206:                }
1.82      nicm     1207:                free(line);
1.4       nicm     1208:        }
1.5       nicm     1209:
1.82      nicm     1210:        free(newsearchstr);
1.5       nicm     1211:        return (msg);
1.45      nicm     1212: }
                   1213:
1.109     nicm     1214: /* Get MRU pane from a list. */
1.166     nicm     1215: static struct window_pane *
1.126     nicm     1216: window_pane_choose_best(struct window_pane **list, u_int size)
1.109     nicm     1217: {
                   1218:        struct window_pane      *next, *best;
                   1219:        u_int                    i;
                   1220:
1.126     nicm     1221:        if (size == 0)
1.109     nicm     1222:                return (NULL);
                   1223:
1.126     nicm     1224:        best = list[0];
                   1225:        for (i = 1; i < size; i++) {
                   1226:                next = list[i];
1.109     nicm     1227:                if (next->active_point > best->active_point)
                   1228:                        best = next;
                   1229:        }
                   1230:        return (best);
                   1231: }
                   1232:
                   1233: /*
                   1234:  * Find the pane directly above another. We build a list of those adjacent to
                   1235:  * top edge and then choose the best.
                   1236:  */
1.45      nicm     1237: struct window_pane *
                   1238: window_pane_find_up(struct window_pane *wp)
                   1239: {
1.126     nicm     1240:        struct window_pane      *next, *best, **list;
                   1241:        u_int                    edge, left, right, end, size;
1.109     nicm     1242:        int                      found;
1.45      nicm     1243:
                   1244:        if (wp == NULL || !window_pane_visible(wp))
                   1245:                return (NULL);
1.126     nicm     1246:
                   1247:        list = NULL;
                   1248:        size = 0;
1.109     nicm     1249:
                   1250:        edge = wp->yoff;
                   1251:        if (edge == 0)
                   1252:                edge = wp->window->sy + 1;
1.45      nicm     1253:
                   1254:        left = wp->xoff;
1.109     nicm     1255:        right = wp->xoff + wp->sx;
1.45      nicm     1256:
1.109     nicm     1257:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1258:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1259:                        continue;
1.109     nicm     1260:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1261:                        continue;
1.109     nicm     1262:                end = next->xoff + next->sx - 1;
                   1263:
                   1264:                found = 0;
                   1265:                if (next->xoff < left && end > right)
                   1266:                        found = 1;
                   1267:                else if (next->xoff >= left && next->xoff <= right)
                   1268:                        found = 1;
                   1269:                else if (end >= left && end <= right)
                   1270:                        found = 1;
1.126     nicm     1271:                if (!found)
                   1272:                        continue;
                   1273:                list = xreallocarray(list, size + 1, sizeof *list);
                   1274:                list[size++] = next;
1.109     nicm     1275:        }
                   1276:
1.126     nicm     1277:        best = window_pane_choose_best(list, size);
                   1278:        free(list);
1.109     nicm     1279:        return (best);
1.45      nicm     1280: }
                   1281:
                   1282: /* Find the pane directly below another. */
                   1283: struct window_pane *
                   1284: window_pane_find_down(struct window_pane *wp)
                   1285: {
1.126     nicm     1286:        struct window_pane      *next, *best, **list;
                   1287:        u_int                    edge, left, right, end, size;
1.109     nicm     1288:        int                      found;
1.45      nicm     1289:
                   1290:        if (wp == NULL || !window_pane_visible(wp))
                   1291:                return (NULL);
1.126     nicm     1292:
                   1293:        list = NULL;
                   1294:        size = 0;
1.109     nicm     1295:
                   1296:        edge = wp->yoff + wp->sy + 1;
                   1297:        if (edge >= wp->window->sy)
                   1298:                edge = 0;
1.45      nicm     1299:
                   1300:        left = wp->xoff;
1.109     nicm     1301:        right = wp->xoff + wp->sx;
1.45      nicm     1302:
1.109     nicm     1303:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1304:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1305:                        continue;
1.109     nicm     1306:                if (next->yoff != edge)
1.45      nicm     1307:                        continue;
1.109     nicm     1308:                end = next->xoff + next->sx - 1;
                   1309:
                   1310:                found = 0;
                   1311:                if (next->xoff < left && end > right)
                   1312:                        found = 1;
                   1313:                else if (next->xoff >= left && next->xoff <= right)
                   1314:                        found = 1;
                   1315:                else if (end >= left && end <= right)
                   1316:                        found = 1;
1.126     nicm     1317:                if (!found)
                   1318:                        continue;
                   1319:                list = xreallocarray(list, size + 1, sizeof *list);
                   1320:                list[size++] = next;
1.45      nicm     1321:        }
1.109     nicm     1322:
1.126     nicm     1323:        best = window_pane_choose_best(list, size);
                   1324:        free(list);
1.109     nicm     1325:        return (best);
1.45      nicm     1326: }
                   1327:
1.109     nicm     1328: /* Find the pane directly to the left of another. */
1.45      nicm     1329: struct window_pane *
                   1330: window_pane_find_left(struct window_pane *wp)
                   1331: {
1.126     nicm     1332:        struct window_pane      *next, *best, **list;
                   1333:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1334:        int                      found;
1.45      nicm     1335:
                   1336:        if (wp == NULL || !window_pane_visible(wp))
                   1337:                return (NULL);
1.126     nicm     1338:
                   1339:        list = NULL;
                   1340:        size = 0;
1.109     nicm     1341:
                   1342:        edge = wp->xoff;
                   1343:        if (edge == 0)
                   1344:                edge = wp->window->sx + 1;
1.45      nicm     1345:
                   1346:        top = wp->yoff;
1.109     nicm     1347:        bottom = wp->yoff + wp->sy;
1.45      nicm     1348:
1.109     nicm     1349:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1350:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1351:                        continue;
1.109     nicm     1352:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1353:                        continue;
1.109     nicm     1354:                end = next->yoff + next->sy - 1;
                   1355:
                   1356:                found = 0;
                   1357:                if (next->yoff < top && end > bottom)
                   1358:                        found = 1;
                   1359:                else if (next->yoff >= top && next->yoff <= bottom)
                   1360:                        found = 1;
                   1361:                else if (end >= top && end <= bottom)
                   1362:                        found = 1;
1.126     nicm     1363:                if (!found)
                   1364:                        continue;
                   1365:                list = xreallocarray(list, size + 1, sizeof *list);
                   1366:                list[size++] = next;
1.45      nicm     1367:        }
1.109     nicm     1368:
1.126     nicm     1369:        best = window_pane_choose_best(list, size);
                   1370:        free(list);
1.109     nicm     1371:        return (best);
1.45      nicm     1372: }
                   1373:
1.109     nicm     1374: /* Find the pane directly to the right of another. */
1.45      nicm     1375: struct window_pane *
                   1376: window_pane_find_right(struct window_pane *wp)
                   1377: {
1.126     nicm     1378:        struct window_pane      *next, *best, **list;
                   1379:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1380:        int                      found;
1.45      nicm     1381:
                   1382:        if (wp == NULL || !window_pane_visible(wp))
                   1383:                return (NULL);
1.126     nicm     1384:
                   1385:        list = NULL;
                   1386:        size = 0;
1.109     nicm     1387:
                   1388:        edge = wp->xoff + wp->sx + 1;
                   1389:        if (edge >= wp->window->sx)
                   1390:                edge = 0;
1.45      nicm     1391:
                   1392:        top = wp->yoff;
1.109     nicm     1393:        bottom = wp->yoff + wp->sy;
1.45      nicm     1394:
1.109     nicm     1395:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1396:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1397:                        continue;
1.109     nicm     1398:                if (next->xoff != edge)
1.45      nicm     1399:                        continue;
1.109     nicm     1400:                end = next->yoff + next->sy - 1;
                   1401:
                   1402:                found = 0;
                   1403:                if (next->yoff < top && end > bottom)
                   1404:                        found = 1;
                   1405:                else if (next->yoff >= top && next->yoff <= bottom)
                   1406:                        found = 1;
                   1407:                else if (end >= top && end <= bottom)
                   1408:                        found = 1;
1.126     nicm     1409:                if (!found)
                   1410:                        continue;
                   1411:                list = xreallocarray(list, size + 1, sizeof *list);
                   1412:                list[size++] = next;
1.109     nicm     1413:        }
                   1414:
1.126     nicm     1415:        best = window_pane_choose_best(list, size);
                   1416:        free(list);
1.109     nicm     1417:        return (best);
1.81      nicm     1418: }
                   1419:
                   1420: /* Clear alert flags for a winlink */
                   1421: void
                   1422: winlink_clear_flags(struct winlink *wl)
                   1423: {
                   1424:        struct session  *s;
1.122     nicm     1425:        struct winlink  *wl_loop;
1.81      nicm     1426:
1.122     nicm     1427:        RB_FOREACH(s, sessions, &sessions) {
                   1428:                RB_FOREACH(wl_loop, winlinks, &s->windows) {
                   1429:                        if (wl_loop->window != wl->window)
1.81      nicm     1430:                                continue;
1.122     nicm     1431:                        if ((wl_loop->flags & WINLINK_ALERTFLAGS) == 0)
1.81      nicm     1432:                                continue;
                   1433:
1.122     nicm     1434:                        wl_loop->flags &= ~WINLINK_ALERTFLAGS;
                   1435:                        wl_loop->window->flags &= ~WINDOW_ALERTFLAGS;
1.81      nicm     1436:                        server_status_session(s);
                   1437:                }
                   1438:        }
1.134     nicm     1439: }
                   1440:
                   1441: int
                   1442: winlink_shuffle_up(struct session *s, struct winlink *wl)
                   1443: {
                   1444:        int      idx, last;
                   1445:
                   1446:        idx = wl->idx + 1;
                   1447:
                   1448:        /* Find the next free index. */
                   1449:        for (last = idx; last < INT_MAX; last++) {
                   1450:                if (winlink_find_by_index(&s->windows, last) == NULL)
                   1451:                        break;
                   1452:        }
                   1453:        if (last == INT_MAX)
                   1454:                return (-1);
                   1455:
                   1456:        /* Move everything from last - 1 to idx up a bit. */
                   1457:        for (; last > idx; last--) {
                   1458:                wl = winlink_find_by_index(&s->windows, last - 1);
                   1459:                server_link_window(s, wl, s, last, 0, 0, NULL);
                   1460:                server_unlink_window(s, wl);
                   1461:        }
                   1462:
                   1463:        return (idx);
1.1       nicm     1464: }