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

1.46    ! nicm        1: /* $OpenBSD: window.c,v 1.45 2010/03/22 19:07:52 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);
1.40      nicm      152:        if (wl->status_text != NULL)
                    153:                xfree(wl->status_text);
1.1       nicm      154:        xfree(wl);
                    155:
                    156:        if (w->references == 0)
                    157:                fatal("bad reference count");
                    158:        w->references--;
                    159:        if (w->references == 0)
                    160:                window_destroy(w);
                    161: }
                    162:
                    163: struct winlink *
1.41      nicm      164: winlink_next(struct winlink *wl)
1.1       nicm      165: {
                    166:        return (RB_NEXT(winlinks, wwl, wl));
                    167: }
                    168:
                    169: struct winlink *
1.41      nicm      170: winlink_previous(struct winlink *wl)
1.1       nicm      171: {
                    172:        return (RB_PREV(winlinks, wwl, wl));
                    173: }
                    174:
                    175: void
                    176: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    177: {
                    178:        if (wl == NULL)
                    179:                return;
                    180:
                    181:        winlink_stack_remove(stack, wl);
1.28      nicm      182:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1       nicm      183: }
                    184:
                    185: void
                    186: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    187: {
                    188:        struct winlink  *wl2;
                    189:
                    190:        if (wl == NULL)
                    191:                return;
1.42      nicm      192:
1.28      nicm      193:        TAILQ_FOREACH(wl2, stack, sentry) {
1.1       nicm      194:                if (wl2 == wl) {
1.28      nicm      195:                        TAILQ_REMOVE(stack, wl, sentry);
1.1       nicm      196:                        return;
                    197:                }
                    198:        }
                    199: }
                    200:
                    201: int
                    202: window_index(struct window *s, u_int *i)
                    203: {
                    204:        for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
                    205:                if (s == ARRAY_ITEM(&windows, *i))
                    206:                        return (0);
                    207:        }
                    208:        return (-1);
                    209: }
                    210:
                    211: struct window *
                    212: window_create1(u_int sx, u_int sy)
                    213: {
                    214:        struct window   *w;
                    215:        u_int            i;
                    216:
1.38      nicm      217:        w = xcalloc(1, sizeof *w);
1.1       nicm      218:        w->name = NULL;
                    219:        w->flags = 0;
                    220:
                    221:        TAILQ_INIT(&w->panes);
                    222:        w->active = NULL;
1.14      nicm      223:
1.17      nicm      224:        w->lastlayout = -1;
1.14      nicm      225:        w->layout_root = NULL;
1.42      nicm      226:
1.1       nicm      227:        w->sx = sx;
                    228:        w->sy = sy;
                    229:
1.38      nicm      230:        queue_window_name(w);
                    231:
1.7       nicm      232:        options_init(&w->options, &global_w_options);
1.1       nicm      233:
                    234:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    235:                if (ARRAY_ITEM(&windows, i) == NULL) {
                    236:                        ARRAY_SET(&windows, i, w);
                    237:                        break;
                    238:                }
                    239:        }
                    240:        if (i == ARRAY_LENGTH(&windows))
                    241:                ARRAY_ADD(&windows, w);
                    242:        w->references = 0;
                    243:
                    244:        return (w);
                    245: }
                    246:
                    247: struct window *
1.23      nicm      248: window_create(const char *name, const char *cmd, const char *shell,
                    249:     const char *cwd, struct environ *env, struct termios *tio,
                    250:     u_int sx, u_int sy, u_int hlimit,char **cause)
1.1       nicm      251: {
1.14      nicm      252:        struct window           *w;
                    253:        struct window_pane      *wp;
1.1       nicm      254:
                    255:        w = window_create1(sx, sy);
1.16      nicm      256:        wp = window_add_pane(w, hlimit);
1.14      nicm      257:        layout_init(w);
1.23      nicm      258:        if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
1.1       nicm      259:                window_destroy(w);
                    260:                return (NULL);
                    261:        }
                    262:        w->active = TAILQ_FIRST(&w->panes);
                    263:        if (name != NULL) {
                    264:                w->name = xstrdup(name);
                    265:                options_set_number(&w->options, "automatic-rename", 0);
                    266:        } else
                    267:                w->name = default_window_name(w);
                    268:        return (w);
                    269: }
                    270:
                    271: void
                    272: window_destroy(struct window *w)
                    273: {
                    274:        u_int   i;
                    275:
                    276:        if (window_index(w, &i) != 0)
                    277:                fatalx("index not found");
                    278:        ARRAY_SET(&windows, i, NULL);
                    279:        while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
                    280:                ARRAY_TRUNC(&windows, 1);
                    281:
1.14      nicm      282:        if (w->layout_root != NULL)
                    283:                layout_free(w);
                    284:
1.38      nicm      285:        evtimer_del(&w->name_timer);
                    286:
1.1       nicm      287:        options_free(&w->options);
                    288:
                    289:        window_destroy_panes(w);
                    290:
                    291:        if (w->name != NULL)
                    292:                xfree(w->name);
                    293:        xfree(w);
                    294: }
                    295:
1.15      nicm      296: void
1.1       nicm      297: window_resize(struct window *w, u_int sx, u_int sy)
                    298: {
                    299:        w->sx = sx;
                    300:        w->sy = sy;
                    301: }
                    302:
                    303: void
                    304: window_set_active_pane(struct window *w, struct window_pane *wp)
                    305: {
                    306:        w->active = wp;
1.10      nicm      307:        while (!window_pane_visible(w->active)) {
1.1       nicm      308:                w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10      nicm      309:                if (w->active == NULL)
                    310:                        w->active = TAILQ_LAST(&w->panes, window_panes);
                    311:                if (w->active == wp)
                    312:                        return;
1.29      nicm      313:        }
                    314: }
                    315:
                    316: void
                    317: window_set_active_at(struct window *w, u_int x, u_int y)
                    318: {
                    319:        struct window_pane      *wp;
                    320:
                    321:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    322:                if (!window_pane_visible(wp))
                    323:                        continue;
                    324:                if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    325:                        continue;
                    326:                if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    327:                        continue;
                    328:                window_set_active_pane(w, wp);
                    329:                break;
1.10      nicm      330:        }
1.1       nicm      331: }
                    332:
                    333: struct window_pane *
1.16      nicm      334: window_add_pane(struct window *w, u_int hlimit)
1.1       nicm      335: {
                    336:        struct window_pane      *wp;
                    337:
1.14      nicm      338:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1       nicm      339:        if (TAILQ_EMPTY(&w->panes))
                    340:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    341:        else
                    342:                TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
                    343:        return (wp);
                    344: }
                    345:
                    346: void
                    347: window_remove_pane(struct window *w, struct window_pane *wp)
                    348: {
                    349:        w->active = TAILQ_PREV(wp, window_panes, entry);
                    350:        if (w->active == NULL)
                    351:                w->active = TAILQ_NEXT(wp, entry);
                    352:
                    353:        TAILQ_REMOVE(&w->panes, wp, entry);
                    354:        window_pane_destroy(wp);
                    355: }
                    356:
                    357: struct window_pane *
                    358: window_pane_at_index(struct window *w, u_int idx)
                    359: {
                    360:        struct window_pane      *wp;
                    361:        u_int                    n;
                    362:
                    363:        n = 0;
                    364:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    365:                if (n == idx)
                    366:                        return (wp);
                    367:                n++;
                    368:        }
                    369:        return (NULL);
1.13      nicm      370: }
                    371:
                    372: u_int
                    373: window_pane_index(struct window *w, struct window_pane *wp)
                    374: {
                    375:        struct window_pane      *wq;
                    376:        u_int                    n;
                    377:
                    378:        n = 0;
                    379:        TAILQ_FOREACH(wq, &w->panes, entry) {
                    380:                if (wp == wq)
                    381:                        break;
                    382:                n++;
                    383:        }
                    384:        return (n);
1.1       nicm      385: }
                    386:
                    387: u_int
                    388: window_count_panes(struct window *w)
                    389: {
                    390:        struct window_pane      *wp;
                    391:        u_int                    n;
                    392:
                    393:        n = 0;
                    394:        TAILQ_FOREACH(wp, &w->panes, entry)
                    395:                n++;
                    396:        return (n);
                    397: }
                    398:
                    399: void
                    400: window_destroy_panes(struct window *w)
                    401: {
                    402:        struct window_pane      *wp;
                    403:
                    404:        while (!TAILQ_EMPTY(&w->panes)) {
                    405:                wp = TAILQ_FIRST(&w->panes);
                    406:                TAILQ_REMOVE(&w->panes, wp, entry);
                    407:                window_pane_destroy(wp);
                    408:        }
                    409: }
                    410:
                    411: struct window_pane *
                    412: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    413: {
                    414:        struct window_pane      *wp;
                    415:
                    416:        wp = xcalloc(1, sizeof *wp);
                    417:        wp->window = w;
                    418:
                    419:        wp->cmd = NULL;
1.23      nicm      420:        wp->shell = NULL;
1.1       nicm      421:        wp->cwd = NULL;
                    422:
                    423:        wp->fd = -1;
1.37      nicm      424:        wp->event = NULL;
1.1       nicm      425:
                    426:        wp->mode = NULL;
1.14      nicm      427:
                    428:        wp->layout_cell = NULL;
1.1       nicm      429:
                    430:        wp->xoff = 0;
1.42      nicm      431:        wp->yoff = 0;
1.1       nicm      432:
                    433:        wp->sx = sx;
                    434:        wp->sy = sy;
                    435:
1.32      nicm      436:        wp->pipe_fd = -1;
                    437:        wp->pipe_off = 0;
1.36      nicm      438:        wp->pipe_event = NULL;
1.32      nicm      439:
1.9       nicm      440:        wp->saved_grid = NULL;
                    441:
1.1       nicm      442:        screen_init(&wp->base, sx, sy, hlimit);
                    443:        wp->screen = &wp->base;
                    444:
                    445:        input_init(wp);
                    446:
                    447:        return (wp);
                    448: }
                    449:
                    450: void
                    451: window_pane_destroy(struct window_pane *wp)
                    452: {
1.37      nicm      453:        if (wp->fd != -1) {
1.1       nicm      454:                close(wp->fd);
1.37      nicm      455:                bufferevent_free(wp->event);
                    456:        }
1.1       nicm      457:
                    458:        input_free(wp);
                    459:
                    460:        window_pane_reset_mode(wp);
                    461:        screen_free(&wp->base);
1.9       nicm      462:        if (wp->saved_grid != NULL)
                    463:                grid_destroy(wp->saved_grid);
1.1       nicm      464:
1.32      nicm      465:        if (wp->pipe_fd != -1) {
                    466:                close(wp->pipe_fd);
1.36      nicm      467:                bufferevent_free(wp->pipe_event);
1.32      nicm      468:        }
                    469:
1.1       nicm      470:        if (wp->cwd != NULL)
                    471:                xfree(wp->cwd);
1.23      nicm      472:        if (wp->shell != NULL)
                    473:                xfree(wp->shell);
1.1       nicm      474:        if (wp->cmd != NULL)
                    475:                xfree(wp->cmd);
                    476:        xfree(wp);
                    477: }
                    478:
                    479: int
1.23      nicm      480: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21      nicm      481:     const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1       nicm      482: {
1.18      nicm      483:        struct winsize           ws;
                    484:        int                      mode;
                    485:        char                    *argv0, **varp, *var;
                    486:        ARRAY_DECL(, char *)     varlist;
                    487:        struct environ_entry    *envent;
                    488:        const char              *ptr;
1.25      nicm      489:        struct termios           tio2;
1.18      nicm      490:        u_int                    i;
1.1       nicm      491:
1.37      nicm      492:        if (wp->fd != -1) {
1.1       nicm      493:                close(wp->fd);
1.37      nicm      494:                bufferevent_free(wp->event);
                    495:        }
1.1       nicm      496:        if (cmd != NULL) {
                    497:                if (wp->cmd != NULL)
                    498:                        xfree(wp->cmd);
                    499:                wp->cmd = xstrdup(cmd);
                    500:        }
1.23      nicm      501:        if (shell != NULL) {
                    502:                if (wp->shell != NULL)
                    503:                        xfree(wp->shell);
                    504:                wp->shell = xstrdup(shell);
                    505:        }
1.1       nicm      506:        if (cwd != NULL) {
                    507:                if (wp->cwd != NULL)
                    508:                        xfree(wp->cwd);
                    509:                wp->cwd = xstrdup(cwd);
                    510:        }
                    511:
                    512:        memset(&ws, 0, sizeof ws);
                    513:        ws.ws_col = screen_size_x(&wp->base);
                    514:        ws.ws_row = screen_size_y(&wp->base);
                    515:
1.42      nicm      516:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      517:        case -1:
                    518:                wp->fd = -1;
                    519:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
                    520:                return (-1);
                    521:        case 0:
                    522:                if (chdir(wp->cwd) != 0)
                    523:                        chdir("/");
1.25      nicm      524:
                    525:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    526:                        fatal("tcgetattr failed");
                    527:                if (tio != NULL)
                    528:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    529:                tio2.c_cc[VERASE] = '\177';
                    530:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    531:                        fatal("tcgetattr failed");
1.18      nicm      532:
                    533:                ARRAY_INIT(&varlist);
                    534:                for (varp = environ; *varp != NULL; varp++) {
                    535:                        var = xstrdup(*varp);
                    536:                        var[strcspn(var, "=")] = '\0';
                    537:                        ARRAY_ADD(&varlist, var);
                    538:                }
                    539:                for (i = 0; i < ARRAY_LENGTH(&varlist); i++) {
                    540:                        var = ARRAY_ITEM(&varlist, i);
                    541:                        unsetenv(var);
1.1       nicm      542:                }
1.18      nicm      543:                RB_FOREACH(envent, environ, env) {
                    544:                        if (envent->value != NULL)
                    545:                                setenv(envent->name, envent->value, 1);
                    546:                }
                    547:
1.35      nicm      548:                server_signal_clear();
1.1       nicm      549:                log_close();
                    550:
1.8       nicm      551:                if (*wp->cmd != '\0') {
1.24      nicm      552:                        /* Set SHELL but only if it is currently not useful. */
                    553:                        shell = getenv("SHELL");
                    554:                        if (shell == NULL || *shell == '\0' || areshell(shell))
                    555:                                setenv("SHELL", wp->shell, 1);
                    556:
1.8       nicm      557:                        execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
                    558:                        fatal("execl failed");
                    559:                }
                    560:
                    561:                /* No command; fork a login shell. */
1.23      nicm      562:                ptr = strrchr(wp->shell, '/');
                    563:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      564:                        xasprintf(&argv0, "-%s", ptr + 1);
                    565:                else
1.23      nicm      566:                        xasprintf(&argv0, "-%s", wp->shell);
1.24      nicm      567:                setenv("SHELL", wp->shell, 1);
1.23      nicm      568:                execl(wp->shell, argv0, (char *) NULL);
1.1       nicm      569:                fatal("execl failed");
                    570:        }
                    571:
                    572:        if ((mode = fcntl(wp->fd, F_GETFL)) == -1)
                    573:                fatal("fcntl failed");
                    574:        if (fcntl(wp->fd, F_SETFL, mode|O_NONBLOCK) == -1)
                    575:                fatal("fcntl failed");
                    576:        if (fcntl(wp->fd, F_SETFD, FD_CLOEXEC) == -1)
                    577:                fatal("fcntl failed");
1.37      nicm      578:        wp->event = bufferevent_new(wp->fd,
                    579:            window_pane_read_callback, NULL, window_pane_error_callback, wp);
                    580:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      581:
                    582:        return (0);
                    583: }
                    584:
1.41      nicm      585: /* ARGSUSED */
1.15      nicm      586: void
1.37      nicm      587: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
                    588: {
1.46    ! nicm      589:        struct window_pane     *wp = data;
        !           590:        char                   *new_data;
        !           591:        size_t                  new_size;
        !           592:
        !           593:        new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
        !           594:        if (wp->pipe_fd != -1 && new_size > 0) {
        !           595:                new_data = EVBUFFER_DATA(wp->event->input);
        !           596:                bufferevent_write(wp->pipe_event, new_data, new_size);
        !           597:        }
        !           598:
        !           599:        input_parse(wp);
1.37      nicm      600:
1.46    ! nicm      601:        wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.37      nicm      602: }
                    603:
1.41      nicm      604: /* ARGSUSED */
1.37      nicm      605: void
                    606: window_pane_error_callback(
                    607:     unused struct bufferevent *bufev, unused short what, void *data)
                    608: {
                    609:        struct window_pane *wp = data;
                    610:
1.39      nicm      611:        server_destroy_pane(wp);
1.37      nicm      612: }
                    613:
                    614: void
1.1       nicm      615: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    616: {
                    617:        struct winsize  ws;
                    618:
                    619:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      620:                return;
1.1       nicm      621:        wp->sx = sx;
                    622:        wp->sy = sy;
                    623:
                    624:        memset(&ws, 0, sizeof ws);
                    625:        ws.ws_col = sx;
                    626:        ws.ws_row = sy;
                    627:
                    628:        screen_resize(&wp->base, sx, sy);
                    629:        if (wp->mode != NULL)
                    630:                wp->mode->resize(wp, sx, sy);
                    631:
                    632:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
                    633:                fatal("ioctl failed");
1.44      nicm      634: }
                    635:
                    636: /*
                    637:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    638:  * history is not updated
                    639:  */
                    640: void
                    641: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
                    642: {
                    643:        struct screen   *s = &wp->base;
                    644:        u_int            sx, sy;
                    645:
                    646:        if (wp->saved_grid != NULL)
                    647:                return;
                    648:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    649:                return;
                    650:        sx = screen_size_x(s);
                    651:        sy = screen_size_y(s);
                    652:
                    653:        wp->saved_grid = grid_create(sx, sy, 0);
                    654:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
                    655:        wp->saved_cx = s->cx;
                    656:        wp->saved_cy = s->cy;
                    657:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                    658:
                    659:        grid_view_clear(s->grid, 0, 0, sx, sy);
                    660:
                    661:        wp->base.grid->flags &= ~GRID_HISTORY;
                    662:
                    663:        wp->flags |= PANE_REDRAW;
                    664: }
                    665:
                    666: /* Exit alternate screen mode and restore the copied grid. */
                    667: void
                    668: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
                    669: {
                    670:        struct screen   *s = &wp->base;
                    671:        u_int            sx, sy;
                    672:
                    673:        if (wp->saved_grid == NULL)
                    674:                return;
                    675:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    676:                return;
                    677:        sx = screen_size_x(s);
                    678:        sy = screen_size_y(s);
                    679:
                    680:        /*
                    681:         * If the current size is bigger, temporarily resize to the old size
                    682:         * before copying back.
                    683:         */
                    684:        if (sy > wp->saved_grid->sy)
                    685:                screen_resize(s, sx, wp->saved_grid->sy);
                    686:
                    687:        /* Restore the grid, cursor position and cell. */
                    688:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
                    689:        s->cx = wp->saved_cx;
                    690:        if (s->cx > screen_size_x(s) - 1)
                    691:                s->cx = screen_size_x(s) - 1;
                    692:        s->cy = wp->saved_cy;
                    693:        if (s->cy > screen_size_y(s) - 1)
                    694:                s->cy = screen_size_y(s) - 1;
                    695:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                    696:
                    697:        /*
                    698:         * Turn history back on (so resize can use it) and then resize back to
                    699:         * the current size.
                    700:         */
                    701:        wp->base.grid->flags |= GRID_HISTORY;
                    702:        if (sy > wp->saved_grid->sy)
                    703:                screen_resize(s, sx, sy);
                    704:
                    705:        grid_destroy(wp->saved_grid);
                    706:        wp->saved_grid = NULL;
                    707:
                    708:        wp->flags |= PANE_REDRAW;
1.1       nicm      709: }
                    710:
                    711: int
                    712: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                    713: {
                    714:        struct screen   *s;
                    715:
1.15      nicm      716:        if (wp->mode != NULL)
1.1       nicm      717:                return (1);
                    718:        wp->mode = mode;
                    719:
                    720:        if ((s = wp->mode->init(wp)) != NULL)
                    721:                wp->screen = s;
1.34      nicm      722:        wp->flags |= PANE_REDRAW;
1.1       nicm      723:        return (0);
                    724: }
                    725:
                    726: void
                    727: window_pane_reset_mode(struct window_pane *wp)
                    728: {
                    729:        if (wp->mode == NULL)
                    730:                return;
                    731:
                    732:        wp->mode->free(wp);
                    733:        wp->mode = NULL;
                    734:
                    735:        wp->screen = &wp->base;
1.34      nicm      736:        wp->flags |= PANE_REDRAW;
1.1       nicm      737: }
                    738:
                    739: void
                    740: window_pane_key(struct window_pane *wp, struct client *c, int key)
                    741: {
1.27      nicm      742:        struct window_pane      *wp2;
                    743:
1.30      nicm      744:        if (!window_pane_visible(wp))
1.3       nicm      745:                return;
                    746:
1.1       nicm      747:        if (wp->mode != NULL) {
                    748:                if (wp->mode->key != NULL)
                    749:                        wp->mode->key(wp, c, key);
1.27      nicm      750:                return;
1.30      nicm      751:        }
1.27      nicm      752:
1.30      nicm      753:        if (wp->fd == -1)
                    754:                return;
1.27      nicm      755:        input_key(wp, key);
                    756:        if (options_get_number(&wp->window->options, "synchronize-panes")) {
                    757:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    758:                        if (wp2 == wp || wp2->mode != NULL)
                    759:                                continue;
                    760:                        if (wp2->fd != -1 && window_pane_visible(wp2))
                    761:                                input_key(wp2, key);
                    762:                }
                    763:        }
1.1       nicm      764: }
                    765:
                    766: void
                    767: window_pane_mouse(
1.31      nicm      768:     struct window_pane *wp, struct client *c, struct mouse_event *m)
1.1       nicm      769: {
1.30      nicm      770:        if (!window_pane_visible(wp))
1.3       nicm      771:                return;
                    772:
1.31      nicm      773:        if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1       nicm      774:                return;
1.31      nicm      775:        if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1       nicm      776:                return;
1.31      nicm      777:        m->x -= wp->xoff;
                    778:        m->y -= wp->yoff;
1.1       nicm      779:
                    780:        if (wp->mode != NULL) {
                    781:                if (wp->mode->mouse != NULL)
1.31      nicm      782:                        wp->mode->mouse(wp, c, m);
1.30      nicm      783:        } else if (wp->fd != -1)
1.31      nicm      784:                input_mouse(wp, m);
1.10      nicm      785: }
                    786:
                    787: int
                    788: window_pane_visible(struct window_pane *wp)
                    789: {
                    790:        struct window   *w = wp->window;
                    791:
                    792:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                    793:                return (0);
                    794:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                    795:                return (0);
                    796:        return (1);
1.1       nicm      797: }
                    798:
                    799: char *
1.5       nicm      800: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1       nicm      801: {
1.4       nicm      802:        struct screen   *s = &wp->base;
1.5       nicm      803:        char            *newsearchstr, *line, *msg;
1.4       nicm      804:        u_int            i;
                    805:
1.5       nicm      806:        msg = NULL;
                    807:        xasprintf(&newsearchstr, "*%s*", searchstr);
                    808:
1.4       nicm      809:        for (i = 0; i < screen_size_y(s); i++) {
                    810:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm      811:                if (fnmatch(newsearchstr, line, 0) == 0) {
                    812:                        msg = line;
                    813:                        if (lineno != NULL)
                    814:                                *lineno = i;
                    815:                        break;
                    816:                }
1.4       nicm      817:                xfree(line);
                    818:        }
1.5       nicm      819:
                    820:        xfree(newsearchstr);
                    821:        return (msg);
1.45      nicm      822: }
                    823:
                    824: /* Find the pane directly above another. */
                    825: struct window_pane *
                    826: window_pane_find_up(struct window_pane *wp)
                    827: {
                    828:        struct window_pane     *wp2;
                    829:        u_int                   left, top;
                    830:
                    831:        if (wp == NULL || !window_pane_visible(wp))
                    832:                return (NULL);
                    833:
                    834:        top = wp->yoff;
                    835:        if (top == 0)
                    836:                top = wp->window->sy + 1;
                    837:        left = wp->xoff;
                    838:
                    839:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    840:                if (!window_pane_visible(wp2))
                    841:                        continue;
                    842:                if (wp2->yoff + wp2->sy + 1 != top)
                    843:                        continue;
                    844:                if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
                    845:                        return (wp2);
                    846:        }
                    847:        return (NULL);
                    848: }
                    849:
                    850: /* Find the pane directly below another. */
                    851: struct window_pane *
                    852: window_pane_find_down(struct window_pane *wp)
                    853: {
                    854:        struct window_pane     *wp2;
                    855:        u_int                   left, bottom;
                    856:
                    857:        if (wp == NULL || !window_pane_visible(wp))
                    858:                return (NULL);
                    859:
                    860:        bottom = wp->yoff + wp->sy + 1;
                    861:        if (bottom >= wp->window->sy)
                    862:                bottom = 0;
                    863:        left = wp->xoff;
                    864:
                    865:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    866:                if (!window_pane_visible(wp2))
                    867:                        continue;
                    868:                if (wp2->yoff != bottom)
                    869:                        continue;
                    870:                if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
                    871:                        return (wp2);
                    872:        }
                    873:        return (NULL);
                    874: }
                    875:
                    876: /*
                    877:  * Find the pane directly to the left of another, adjacent to the left side and
                    878:  * containing the top edge.
                    879:  */
                    880: struct window_pane *
                    881: window_pane_find_left(struct window_pane *wp)
                    882: {
                    883:        struct window_pane     *wp2;
                    884:        u_int                   left, top;
                    885:
                    886:        if (wp == NULL || !window_pane_visible(wp))
                    887:                return (NULL);
                    888:
                    889:        left = wp->xoff;
                    890:        if (left == 0)
                    891:                left = wp->window->sx + 1;
                    892:        top = wp->yoff;
                    893:
                    894:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    895:                if (!window_pane_visible(wp2))
                    896:                        continue;
                    897:                if (wp2->xoff + wp2->sx + 1 != left)
                    898:                        continue;
                    899:                if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
                    900:                        return (wp2);
                    901:        }
                    902:        return (NULL);
                    903: }
                    904:
                    905: /*
                    906:  * Find the pane directly to the right of another, that is adjacent to the
                    907:  * right edge and including the top edge.
                    908:  */
                    909: struct window_pane *
                    910: window_pane_find_right(struct window_pane *wp)
                    911: {
                    912:        struct window_pane     *wp2;
                    913:        u_int                   right, top;
                    914:
                    915:        if (wp == NULL || !window_pane_visible(wp))
                    916:                return (NULL);
                    917:
                    918:        right = wp->xoff + wp->sx + 1;
                    919:        if (right >= wp->window->sx)
                    920:                right = 0;
                    921:        top = wp->yoff;
                    922:
                    923:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    924:                if (!window_pane_visible(wp2))
                    925:                        continue;
                    926:                if (wp2->xoff != right)
                    927:                        continue;
                    928:                if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
                    929:                        return (wp2);
                    930:        }
                    931:        return (NULL);
1.1       nicm      932: }