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

1.36    ! nicm        1: /* $OpenBSD: window.c,v 1.35 2009/11/04 20:50:11 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      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>
                     20: #include <sys/ioctl.h>
                     21:
                     22: #include <errno.h>
                     23: #include <fcntl.h>
1.5       nicm       24: #include <fnmatch.h>
1.1       nicm       25: #include <paths.h>
1.8       nicm       26: #include <pwd.h>
1.1       nicm       27: #include <signal.h>
                     28: #include <stdint.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
                     31: #include <termios.h>
                     32: #include <unistd.h>
                     33: #include <util.h>
                     34:
                     35: #include "tmux.h"
                     36:
                     37: /*
1.14      nicm       38:  * Each window is attached to a number of panes, each of which is a pty. This
1.1       nicm       39:  * file contains code to handle them.
                     40:  *
                     41:  * A pane has two buffers attached, these are filled and emptied by the main
                     42:  * server poll loop. Output data is received from pty's in screen format,
                     43:  * translated and returned as a series of escape sequences and strings via
                     44:  * input_parse (in input.c). Input data is received as key codes and written
                     45:  * directly via input_key.
                     46:  *
                     47:  * Each pane also has a "virtual" screen (screen.c) which contains the current
                     48:  * state and is redisplayed when the window is reattached to a client.
                     49:  *
                     50:  * Windows are stored directly on a global array and wrapped in any number of
                     51:  * winlink structs to be linked onto local session RB trees. A reference count
                     52:  * is maintained and a window removed from the global list and destroyed when
                     53:  * it reaches zero.
                     54:  */
                     55:
                     56: /* Global window list. */
                     57: struct windows windows;
                     58:
                     59: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
                     60:
                     61: int
                     62: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
                     63: {
                     64:        return (wl1->idx - wl2->idx);
1.12      nicm       65: }
                     66:
                     67: struct winlink *
                     68: winlink_find_by_window(struct winlinks *wwl, struct window *w)
                     69: {
                     70:        struct winlink  *wl;
                     71:
                     72:        RB_FOREACH(wl, winlinks, wwl) {
                     73:                if (wl->window == w)
                     74:                        return (wl);
                     75:        }
                     76:
                     77:        return (NULL);
1.1       nicm       78: }
                     79:
                     80: struct winlink *
                     81: winlink_find_by_index(struct winlinks *wwl, int idx)
                     82: {
                     83:        struct winlink  wl;
                     84:
                     85:        if (idx < 0)
                     86:                fatalx("bad index");
                     87:
                     88:        wl.idx = idx;
                     89:        return (RB_FIND(winlinks, wwl, &wl));
                     90: }
                     91:
                     92: int
1.22      nicm       93: winlink_next_index(struct winlinks *wwl, int idx)
1.1       nicm       94: {
1.22      nicm       95:        int     i;
1.1       nicm       96:
1.22      nicm       97:        i = idx;
                     98:        do {
1.1       nicm       99:                if (winlink_find_by_index(wwl, i) == NULL)
                    100:                        return (i);
1.22      nicm      101:                if (i == INT_MAX)
                    102:                        i = 0;
                    103:                else
                    104:                        i++;
                    105:        } while (i != idx);
                    106:        return (-1);
1.1       nicm      107: }
                    108:
                    109: u_int
                    110: winlink_count(struct winlinks *wwl)
                    111: {
                    112:        struct winlink  *wl;
                    113:        u_int            n;
                    114:
                    115:        n = 0;
                    116:        RB_FOREACH(wl, winlinks, wwl)
                    117:                n++;
                    118:
                    119:        return (n);
                    120: }
                    121:
                    122: struct winlink *
                    123: winlink_add(struct winlinks *wwl, struct window *w, int idx)
                    124: {
                    125:        struct winlink  *wl;
                    126:
1.22      nicm      127:        if (idx < 0) {
                    128:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
                    129:                        return (NULL);
                    130:        } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1       nicm      131:                return (NULL);
                    132:
                    133:        wl = xcalloc(1, sizeof *wl);
                    134:        wl->idx = idx;
                    135:        wl->window = w;
                    136:        RB_INSERT(winlinks, wwl, wl);
                    137:
                    138:        w->references++;
                    139:
                    140:        return (wl);
                    141: }
                    142:
                    143: void
                    144: winlink_remove(struct winlinks *wwl, struct winlink *wl)
                    145: {
                    146:        struct window   *w = wl->window;
                    147:
                    148:        RB_REMOVE(winlinks, wwl, wl);
                    149:        xfree(wl);
                    150:
                    151:        if (w->references == 0)
                    152:                fatal("bad reference count");
                    153:        w->references--;
                    154:        if (w->references == 0)
                    155:                window_destroy(w);
                    156: }
                    157:
                    158: struct winlink *
                    159: winlink_next(unused struct winlinks *wwl, struct winlink *wl)
                    160: {
                    161:        return (RB_NEXT(winlinks, wwl, wl));
                    162: }
                    163:
                    164: struct winlink *
                    165: winlink_previous(unused struct winlinks *wwl, struct winlink *wl)
                    166: {
                    167:        return (RB_PREV(winlinks, wwl, wl));
                    168: }
                    169:
                    170: void
                    171: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    172: {
                    173:        if (wl == NULL)
                    174:                return;
                    175:
                    176:        winlink_stack_remove(stack, wl);
1.28      nicm      177:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1       nicm      178: }
                    179:
                    180: void
                    181: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    182: {
                    183:        struct winlink  *wl2;
                    184:
                    185:        if (wl == NULL)
                    186:                return;
1.28      nicm      187:
                    188:        TAILQ_FOREACH(wl2, stack, sentry) {
1.1       nicm      189:                if (wl2 == wl) {
1.28      nicm      190:                        TAILQ_REMOVE(stack, wl, sentry);
1.1       nicm      191:                        return;
                    192:                }
                    193:        }
                    194: }
                    195:
                    196: int
                    197: window_index(struct window *s, u_int *i)
                    198: {
                    199:        for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
                    200:                if (s == ARRAY_ITEM(&windows, *i))
                    201:                        return (0);
                    202:        }
                    203:        return (-1);
                    204: }
                    205:
                    206: struct window *
                    207: window_create1(u_int sx, u_int sy)
                    208: {
                    209:        struct window   *w;
                    210:        u_int            i;
                    211:
                    212:        w = xmalloc(sizeof *w);
                    213:        w->name = NULL;
                    214:        w->flags = 0;
                    215:
                    216:        TAILQ_INIT(&w->panes);
                    217:        w->active = NULL;
1.14      nicm      218:
1.17      nicm      219:        w->lastlayout = -1;
1.14      nicm      220:        w->layout_root = NULL;
                    221:
1.1       nicm      222:        w->sx = sx;
                    223:        w->sy = sy;
                    224:
1.7       nicm      225:        options_init(&w->options, &global_w_options);
1.1       nicm      226:
                    227:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    228:                if (ARRAY_ITEM(&windows, i) == NULL) {
                    229:                        ARRAY_SET(&windows, i, w);
                    230:                        break;
                    231:                }
                    232:        }
                    233:        if (i == ARRAY_LENGTH(&windows))
                    234:                ARRAY_ADD(&windows, w);
                    235:        w->references = 0;
                    236:
                    237:        return (w);
                    238: }
                    239:
                    240: struct window *
1.23      nicm      241: window_create(const char *name, const char *cmd, const char *shell,
                    242:     const char *cwd, struct environ *env, struct termios *tio,
                    243:     u_int sx, u_int sy, u_int hlimit,char **cause)
1.1       nicm      244: {
1.14      nicm      245:        struct window           *w;
                    246:        struct window_pane      *wp;
1.1       nicm      247:
                    248:        w = window_create1(sx, sy);
1.16      nicm      249:        wp = window_add_pane(w, hlimit);
1.14      nicm      250:        layout_init(w);
1.23      nicm      251:        if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
1.1       nicm      252:                window_destroy(w);
                    253:                return (NULL);
                    254:        }
                    255:        w->active = TAILQ_FIRST(&w->panes);
                    256:        if (name != NULL) {
                    257:                w->name = xstrdup(name);
                    258:                options_set_number(&w->options, "automatic-rename", 0);
                    259:        } else
                    260:                w->name = default_window_name(w);
                    261:        return (w);
                    262: }
                    263:
                    264: void
                    265: window_destroy(struct window *w)
                    266: {
                    267:        u_int   i;
                    268:
                    269:        if (window_index(w, &i) != 0)
                    270:                fatalx("index not found");
                    271:        ARRAY_SET(&windows, i, NULL);
                    272:        while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
                    273:                ARRAY_TRUNC(&windows, 1);
                    274:
1.14      nicm      275:        if (w->layout_root != NULL)
                    276:                layout_free(w);
                    277:
1.1       nicm      278:        options_free(&w->options);
                    279:
                    280:        window_destroy_panes(w);
                    281:
                    282:        if (w->name != NULL)
                    283:                xfree(w->name);
                    284:        xfree(w);
                    285: }
                    286:
1.15      nicm      287: void
1.1       nicm      288: window_resize(struct window *w, u_int sx, u_int sy)
                    289: {
                    290:        w->sx = sx;
                    291:        w->sy = sy;
                    292: }
                    293:
                    294: void
                    295: window_set_active_pane(struct window *w, struct window_pane *wp)
                    296: {
                    297:        w->active = wp;
1.10      nicm      298:        while (!window_pane_visible(w->active)) {
1.1       nicm      299:                w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10      nicm      300:                if (w->active == NULL)
                    301:                        w->active = TAILQ_LAST(&w->panes, window_panes);
                    302:                if (w->active == wp)
                    303:                        return;
1.29      nicm      304:        }
                    305: }
                    306:
                    307: void
                    308: window_set_active_at(struct window *w, u_int x, u_int y)
                    309: {
                    310:        struct window_pane      *wp;
                    311:
                    312:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    313:                if (!window_pane_visible(wp))
                    314:                        continue;
                    315:                if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    316:                        continue;
                    317:                if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    318:                        continue;
                    319:                window_set_active_pane(w, wp);
                    320:                break;
1.10      nicm      321:        }
1.1       nicm      322: }
                    323:
                    324: struct window_pane *
1.16      nicm      325: window_add_pane(struct window *w, u_int hlimit)
1.1       nicm      326: {
                    327:        struct window_pane      *wp;
                    328:
1.14      nicm      329:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1       nicm      330:        if (TAILQ_EMPTY(&w->panes))
                    331:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    332:        else
                    333:                TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
                    334:        return (wp);
                    335: }
                    336:
                    337: void
                    338: window_remove_pane(struct window *w, struct window_pane *wp)
                    339: {
                    340:        w->active = TAILQ_PREV(wp, window_panes, entry);
                    341:        if (w->active == NULL)
                    342:                w->active = TAILQ_NEXT(wp, entry);
                    343:
                    344:        TAILQ_REMOVE(&w->panes, wp, entry);
                    345:        window_pane_destroy(wp);
                    346: }
                    347:
                    348: struct window_pane *
                    349: window_pane_at_index(struct window *w, u_int idx)
                    350: {
                    351:        struct window_pane      *wp;
                    352:        u_int                    n;
                    353:
                    354:        n = 0;
                    355:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    356:                if (n == idx)
                    357:                        return (wp);
                    358:                n++;
                    359:        }
                    360:        return (NULL);
1.13      nicm      361: }
                    362:
                    363: u_int
                    364: window_pane_index(struct window *w, struct window_pane *wp)
                    365: {
                    366:        struct window_pane      *wq;
                    367:        u_int                    n;
                    368:
                    369:        n = 0;
                    370:        TAILQ_FOREACH(wq, &w->panes, entry) {
                    371:                if (wp == wq)
                    372:                        break;
                    373:                n++;
                    374:        }
                    375:        return (n);
1.1       nicm      376: }
                    377:
                    378: u_int
                    379: window_count_panes(struct window *w)
                    380: {
                    381:        struct window_pane      *wp;
                    382:        u_int                    n;
                    383:
                    384:        n = 0;
                    385:        TAILQ_FOREACH(wp, &w->panes, entry)
                    386:                n++;
                    387:        return (n);
                    388: }
                    389:
                    390: void
                    391: window_destroy_panes(struct window *w)
                    392: {
                    393:        struct window_pane      *wp;
                    394:
                    395:        while (!TAILQ_EMPTY(&w->panes)) {
                    396:                wp = TAILQ_FIRST(&w->panes);
                    397:                TAILQ_REMOVE(&w->panes, wp, entry);
                    398:                window_pane_destroy(wp);
                    399:        }
                    400: }
                    401:
                    402: struct window_pane *
                    403: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    404: {
                    405:        struct window_pane      *wp;
                    406:
                    407:        wp = xcalloc(1, sizeof *wp);
                    408:        wp->window = w;
                    409:
                    410:        wp->cmd = NULL;
1.23      nicm      411:        wp->shell = NULL;
1.1       nicm      412:        wp->cwd = NULL;
                    413:
                    414:        wp->fd = -1;
                    415:        wp->in = buffer_create(BUFSIZ);
                    416:        wp->out = buffer_create(BUFSIZ);
                    417:
                    418:        wp->mode = NULL;
1.14      nicm      419:
                    420:        wp->layout_cell = NULL;
1.1       nicm      421:
                    422:        wp->xoff = 0;
                    423:        wp->yoff = 0;
                    424:
                    425:        wp->sx = sx;
                    426:        wp->sy = sy;
                    427:
1.32      nicm      428:        wp->pipe_fd = -1;
                    429:        wp->pipe_off = 0;
1.36    ! nicm      430:        wp->pipe_event = NULL;
1.32      nicm      431:
1.9       nicm      432:        wp->saved_grid = NULL;
                    433:
1.1       nicm      434:        screen_init(&wp->base, sx, sy, hlimit);
                    435:        wp->screen = &wp->base;
                    436:
                    437:        input_init(wp);
                    438:
                    439:        return (wp);
                    440: }
                    441:
                    442: void
                    443: window_pane_destroy(struct window_pane *wp)
                    444: {
                    445:        if (wp->fd != -1)
                    446:                close(wp->fd);
                    447:
                    448:        input_free(wp);
                    449:
                    450:        window_pane_reset_mode(wp);
                    451:        screen_free(&wp->base);
1.9       nicm      452:        if (wp->saved_grid != NULL)
                    453:                grid_destroy(wp->saved_grid);
1.1       nicm      454:
1.32      nicm      455:        if (wp->pipe_fd != -1) {
                    456:                close(wp->pipe_fd);
1.36    ! nicm      457:                bufferevent_free(wp->pipe_event);
1.32      nicm      458:        }
                    459:
1.1       nicm      460:        buffer_destroy(wp->in);
                    461:        buffer_destroy(wp->out);
1.35      nicm      462:        event_del(&wp->event);
1.1       nicm      463:
                    464:        if (wp->cwd != NULL)
                    465:                xfree(wp->cwd);
1.23      nicm      466:        if (wp->shell != NULL)
                    467:                xfree(wp->shell);
1.1       nicm      468:        if (wp->cmd != NULL)
                    469:                xfree(wp->cmd);
                    470:        xfree(wp);
                    471: }
                    472:
                    473: int
1.23      nicm      474: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21      nicm      475:     const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1       nicm      476: {
1.18      nicm      477:        struct winsize           ws;
                    478:        int                      mode;
                    479:        char                    *argv0, **varp, *var;
                    480:        ARRAY_DECL(, char *)     varlist;
                    481:        struct environ_entry    *envent;
                    482:        const char              *ptr;
                    483:        struct timeval           tv;
1.25      nicm      484:        struct termios           tio2;
1.18      nicm      485:        u_int                    i;
1.1       nicm      486:
                    487:        if (wp->fd != -1)
                    488:                close(wp->fd);
                    489:        if (cmd != NULL) {
                    490:                if (wp->cmd != NULL)
                    491:                        xfree(wp->cmd);
                    492:                wp->cmd = xstrdup(cmd);
                    493:        }
1.23      nicm      494:        if (shell != NULL) {
                    495:                if (wp->shell != NULL)
                    496:                        xfree(wp->shell);
                    497:                wp->shell = xstrdup(shell);
                    498:        }
1.1       nicm      499:        if (cwd != NULL) {
                    500:                if (wp->cwd != NULL)
                    501:                        xfree(wp->cwd);
                    502:                wp->cwd = xstrdup(cwd);
                    503:        }
                    504:
                    505:        memset(&ws, 0, sizeof ws);
                    506:        ws.ws_col = screen_size_x(&wp->base);
                    507:        ws.ws_row = screen_size_y(&wp->base);
                    508:
                    509:        if (gettimeofday(&wp->window->name_timer, NULL) != 0)
1.26      nicm      510:                fatal("gettimeofday failed");
1.1       nicm      511:        tv.tv_sec = 0;
                    512:        tv.tv_usec = NAME_INTERVAL * 1000L;
                    513:        timeradd(&wp->window->name_timer, &tv, &wp->window->name_timer);
                    514:
1.25      nicm      515:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      516:        case -1:
                    517:                wp->fd = -1;
                    518:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
                    519:                return (-1);
                    520:        case 0:
                    521:                if (chdir(wp->cwd) != 0)
                    522:                        chdir("/");
1.25      nicm      523:
                    524:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    525:                        fatal("tcgetattr failed");
                    526:                if (tio != NULL)
                    527:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    528:                tio2.c_cc[VERASE] = '\177';
                    529:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    530:                        fatal("tcgetattr failed");
1.18      nicm      531:
                    532:                ARRAY_INIT(&varlist);
                    533:                for (varp = environ; *varp != NULL; varp++) {
                    534:                        var = xstrdup(*varp);
                    535:                        var[strcspn(var, "=")] = '\0';
                    536:                        ARRAY_ADD(&varlist, var);
                    537:                }
                    538:                for (i = 0; i < ARRAY_LENGTH(&varlist); i++) {
                    539:                        var = ARRAY_ITEM(&varlist, i);
                    540:                        unsetenv(var);
1.1       nicm      541:                }
1.18      nicm      542:                RB_FOREACH(envent, environ, env) {
                    543:                        if (envent->value != NULL)
                    544:                                setenv(envent->name, envent->value, 1);
                    545:                }
                    546:
1.35      nicm      547:                server_signal_clear();
1.1       nicm      548:                log_close();
                    549:
1.8       nicm      550:                if (*wp->cmd != '\0') {
1.24      nicm      551:                        /* Set SHELL but only if it is currently not useful. */
                    552:                        shell = getenv("SHELL");
                    553:                        if (shell == NULL || *shell == '\0' || areshell(shell))
                    554:                                setenv("SHELL", wp->shell, 1);
                    555:
1.8       nicm      556:                        execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
                    557:                        fatal("execl failed");
                    558:                }
                    559:
                    560:                /* No command; fork a login shell. */
1.23      nicm      561:                ptr = strrchr(wp->shell, '/');
                    562:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      563:                        xasprintf(&argv0, "-%s", ptr + 1);
                    564:                else
1.23      nicm      565:                        xasprintf(&argv0, "-%s", wp->shell);
1.24      nicm      566:                setenv("SHELL", wp->shell, 1);
1.23      nicm      567:                execl(wp->shell, argv0, (char *) NULL);
1.1       nicm      568:                fatal("execl failed");
                    569:        }
                    570:
                    571:        if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
                    572:                fatal("fcntl failed");
                    573:        if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
                    574:                fatal("fcntl failed");
                    575:        if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
                    576:                fatal("fcntl failed");
                    577:
                    578:        return (0);
                    579: }
                    580:
1.15      nicm      581: void
1.1       nicm      582: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    583: {
                    584:        struct winsize  ws;
                    585:
                    586:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      587:                return;
1.1       nicm      588:        wp->sx = sx;
                    589:        wp->sy = sy;
                    590:
                    591:        memset(&ws, 0, sizeof ws);
                    592:        ws.ws_col = sx;
                    593:        ws.ws_row = sy;
                    594:
                    595:        screen_resize(&wp->base, sx, sy);
                    596:        if (wp->mode != NULL)
                    597:                wp->mode->resize(wp, sx, sy);
                    598:
                    599:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
                    600:                fatal("ioctl failed");
                    601: }
                    602:
                    603: int
                    604: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                    605: {
                    606:        struct screen   *s;
                    607:
1.15      nicm      608:        if (wp->mode != NULL)
1.1       nicm      609:                return (1);
                    610:        wp->mode = mode;
                    611:
                    612:        if ((s = wp->mode->init(wp)) != NULL)
                    613:                wp->screen = s;
1.34      nicm      614:        wp->flags |= PANE_REDRAW;
1.1       nicm      615:        return (0);
                    616: }
                    617:
                    618: void
                    619: window_pane_reset_mode(struct window_pane *wp)
                    620: {
                    621:        if (wp->mode == NULL)
                    622:                return;
                    623:
                    624:        wp->mode->free(wp);
                    625:        wp->mode = NULL;
                    626:
                    627:        wp->screen = &wp->base;
1.34      nicm      628:        wp->flags |= PANE_REDRAW;
1.1       nicm      629: }
                    630:
                    631: void
                    632: window_pane_parse(struct window_pane *wp)
                    633: {
1.32      nicm      634:        size_t  new_size;
1.33      nicm      635:
                    636:        if (wp->mode != NULL)
                    637:                return;
1.32      nicm      638:
                    639:        new_size = BUFFER_USED(wp->in) - wp->pipe_off;
                    640:        if (wp->pipe_fd != -1 && new_size > 0)
1.36    ! nicm      641:                bufferevent_write(wp->pipe_event, BUFFER_OUT(wp->in), new_size);
1.32      nicm      642:
1.1       nicm      643:        input_parse(wp);
1.32      nicm      644:
                    645:        wp->pipe_off = BUFFER_USED(wp->in);
1.1       nicm      646: }
                    647:
                    648: void
                    649: window_pane_key(struct window_pane *wp, struct client *c, int key)
                    650: {
1.27      nicm      651:        struct window_pane      *wp2;
                    652:
1.30      nicm      653:        if (!window_pane_visible(wp))
1.3       nicm      654:                return;
                    655:
1.1       nicm      656:        if (wp->mode != NULL) {
                    657:                if (wp->mode->key != NULL)
                    658:                        wp->mode->key(wp, c, key);
1.27      nicm      659:                return;
1.30      nicm      660:        }
1.27      nicm      661:
1.30      nicm      662:        if (wp->fd == -1)
                    663:                return;
1.27      nicm      664:        input_key(wp, key);
                    665:        if (options_get_number(&wp->window->options, "synchronize-panes")) {
                    666:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    667:                        if (wp2 == wp || wp2->mode != NULL)
                    668:                                continue;
                    669:                        if (wp2->fd != -1 && window_pane_visible(wp2))
                    670:                                input_key(wp2, key);
                    671:                }
                    672:        }
1.1       nicm      673: }
                    674:
                    675: void
                    676: window_pane_mouse(
1.31      nicm      677:     struct window_pane *wp, struct client *c, struct mouse_event *m)
1.1       nicm      678: {
1.30      nicm      679:        if (!window_pane_visible(wp))
1.3       nicm      680:                return;
                    681:
1.31      nicm      682:        if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1       nicm      683:                return;
1.31      nicm      684:        if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1       nicm      685:                return;
1.31      nicm      686:        m->x -= wp->xoff;
                    687:        m->y -= wp->yoff;
1.1       nicm      688:
                    689:        if (wp->mode != NULL) {
                    690:                if (wp->mode->mouse != NULL)
1.31      nicm      691:                        wp->mode->mouse(wp, c, m);
1.30      nicm      692:        } else if (wp->fd != -1)
1.31      nicm      693:                input_mouse(wp, m);
1.10      nicm      694: }
                    695:
                    696: int
                    697: window_pane_visible(struct window_pane *wp)
                    698: {
                    699:        struct window   *w = wp->window;
                    700:
                    701:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                    702:                return (0);
                    703:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                    704:                return (0);
                    705:        return (1);
1.1       nicm      706: }
                    707:
                    708: char *
1.5       nicm      709: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1       nicm      710: {
1.4       nicm      711:        struct screen   *s = &wp->base;
1.5       nicm      712:        char            *newsearchstr, *line, *msg;
1.4       nicm      713:        u_int            i;
                    714:
1.5       nicm      715:        msg = NULL;
                    716:        xasprintf(&newsearchstr, "*%s*", searchstr);
                    717:
1.4       nicm      718:        for (i = 0; i < screen_size_y(s); i++) {
                    719:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm      720:                if (fnmatch(newsearchstr, line, 0) == 0) {
                    721:                        msg = line;
                    722:                        if (lineno != NULL)
                    723:                                *lineno = i;
                    724:                        break;
                    725:                }
1.4       nicm      726:                xfree(line);
                    727:        }
1.5       nicm      728:
                    729:        xfree(newsearchstr);
                    730:        return (msg);
1.1       nicm      731: }