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

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