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

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