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

1.72    ! nicm        1: /* $OpenBSD: window.c,v 1.71 2012/01/30 09:39:34 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.64      nicm       59: /* Global panes tree. */
                     60: struct window_pane_tree all_window_panes;
1.71      nicm       61: u_int  next_window_pane_id;
                     62: u_int  next_window_id;
1.64      nicm       63:
1.37      nicm       64: void   window_pane_read_callback(struct bufferevent *, void *);
                     65: void   window_pane_error_callback(struct bufferevent *, short, void *);
                     66:
1.1       nicm       67: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
                     68:
                     69: int
                     70: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
                     71: {
                     72:        return (wl1->idx - wl2->idx);
1.12      nicm       73: }
                     74:
1.64      nicm       75: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
                     76:
                     77: int
                     78: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
                     79: {
                     80:        return (wp1->id - wp2->id);
                     81: }
                     82:
1.12      nicm       83: struct winlink *
                     84: winlink_find_by_window(struct winlinks *wwl, struct window *w)
                     85: {
                     86:        struct winlink  *wl;
                     87:
                     88:        RB_FOREACH(wl, winlinks, wwl) {
                     89:                if (wl->window == w)
                     90:                        return (wl);
                     91:        }
                     92:
                     93:        return (NULL);
1.1       nicm       94: }
                     95:
                     96: struct winlink *
                     97: winlink_find_by_index(struct winlinks *wwl, int idx)
                     98: {
                     99:        struct winlink  wl;
                    100:
                    101:        if (idx < 0)
                    102:                fatalx("bad index");
                    103:
                    104:        wl.idx = idx;
                    105:        return (RB_FIND(winlinks, wwl, &wl));
                    106: }
                    107:
1.71      nicm      108: struct winlink *
                    109: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
                    110: {
                    111:        struct winlink *wl;
                    112:
                    113:        RB_FOREACH(wl, winlinks, wwl) {
                    114:                if (wl->window->id == id)
                    115:                        return (wl);
                    116:        }
                    117:        return NULL;
                    118: }
                    119:
1.1       nicm      120: int
1.22      nicm      121: winlink_next_index(struct winlinks *wwl, int idx)
1.1       nicm      122: {
1.22      nicm      123:        int     i;
1.1       nicm      124:
1.22      nicm      125:        i = idx;
                    126:        do {
1.1       nicm      127:                if (winlink_find_by_index(wwl, i) == NULL)
                    128:                        return (i);
1.22      nicm      129:                if (i == INT_MAX)
                    130:                        i = 0;
                    131:                else
                    132:                        i++;
                    133:        } while (i != idx);
                    134:        return (-1);
1.1       nicm      135: }
                    136:
                    137: u_int
                    138: winlink_count(struct winlinks *wwl)
                    139: {
                    140:        struct winlink  *wl;
                    141:        u_int            n;
                    142:
                    143:        n = 0;
                    144:        RB_FOREACH(wl, winlinks, wwl)
                    145:                n++;
                    146:
                    147:        return (n);
                    148: }
                    149:
                    150: struct winlink *
1.63      nicm      151: winlink_add(struct winlinks *wwl, int idx)
1.1       nicm      152: {
                    153:        struct winlink  *wl;
                    154:
1.22      nicm      155:        if (idx < 0) {
                    156:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
                    157:                        return (NULL);
                    158:        } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1       nicm      159:                return (NULL);
                    160:
                    161:        wl = xcalloc(1, sizeof *wl);
                    162:        wl->idx = idx;
                    163:        RB_INSERT(winlinks, wwl, wl);
                    164:
1.63      nicm      165:        return (wl);
                    166: }
                    167:
                    168: void
                    169: winlink_set_window(struct winlink *wl, struct window *w)
                    170: {
                    171:        wl->window = w;
1.1       nicm      172:        w->references++;
                    173: }
                    174:
                    175: void
                    176: winlink_remove(struct winlinks *wwl, struct winlink *wl)
                    177: {
                    178:        struct window   *w = wl->window;
                    179:
                    180:        RB_REMOVE(winlinks, wwl, wl);
1.40      nicm      181:        if (wl->status_text != NULL)
                    182:                xfree(wl->status_text);
1.1       nicm      183:        xfree(wl);
                    184:
1.63      nicm      185:        if (w != NULL) {
                    186:                if (w->references == 0)
                    187:                        fatal("bad reference count");
                    188:                w->references--;
                    189:                if (w->references == 0)
                    190:                        window_destroy(w);
                    191:        }
1.1       nicm      192: }
                    193:
                    194: struct winlink *
1.41      nicm      195: winlink_next(struct winlink *wl)
1.1       nicm      196: {
                    197:        return (RB_NEXT(winlinks, wwl, wl));
                    198: }
                    199:
                    200: struct winlink *
1.41      nicm      201: winlink_previous(struct winlink *wl)
1.1       nicm      202: {
                    203:        return (RB_PREV(winlinks, wwl, wl));
1.52      nicm      204: }
                    205:
                    206: struct winlink *
1.53      nicm      207: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      208: {
                    209:        for (; n > 0; n--) {
                    210:                if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53      nicm      211:                        wl = RB_MIN(winlinks, &s->windows);
1.52      nicm      212:        }
                    213:
                    214:        return (wl);
                    215: }
                    216:
                    217: struct winlink *
1.53      nicm      218: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      219: {
                    220:        for (; n > 0; n--) {
                    221:                if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53      nicm      222:                        wl = RB_MAX(winlinks, &s->windows);
1.52      nicm      223:        }
                    224:
                    225:        return (wl);
1.1       nicm      226: }
                    227:
                    228: void
                    229: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    230: {
                    231:        if (wl == NULL)
                    232:                return;
                    233:
                    234:        winlink_stack_remove(stack, wl);
1.28      nicm      235:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.1       nicm      236: }
                    237:
                    238: void
                    239: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    240: {
                    241:        struct winlink  *wl2;
                    242:
                    243:        if (wl == NULL)
                    244:                return;
1.42      nicm      245:
1.28      nicm      246:        TAILQ_FOREACH(wl2, stack, sentry) {
1.1       nicm      247:                if (wl2 == wl) {
1.28      nicm      248:                        TAILQ_REMOVE(stack, wl, sentry);
1.1       nicm      249:                        return;
                    250:                }
                    251:        }
                    252: }
                    253:
                    254: int
                    255: window_index(struct window *s, u_int *i)
                    256: {
                    257:        for (*i = 0; *i < ARRAY_LENGTH(&windows); (*i)++) {
                    258:                if (s == ARRAY_ITEM(&windows, *i))
                    259:                        return (0);
                    260:        }
                    261:        return (-1);
                    262: }
                    263:
                    264: struct window *
1.71      nicm      265: window_find_by_id(u_int id)
                    266: {
                    267:        struct window   *w;
                    268:        u_int            i;
                    269:
                    270:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    271:                w = ARRAY_ITEM(&windows, i);
                    272:                if (w->id == id)
                    273:                        return (w);
                    274:        }
                    275:        return NULL;
                    276: }
                    277:
                    278: struct window *
1.1       nicm      279: window_create1(u_int sx, u_int sy)
                    280: {
                    281:        struct window   *w;
                    282:        u_int            i;
                    283:
1.38      nicm      284:        w = xcalloc(1, sizeof *w);
1.71      nicm      285:        w->id = next_window_id++;
1.1       nicm      286:        w->name = NULL;
                    287:        w->flags = 0;
                    288:
                    289:        TAILQ_INIT(&w->panes);
                    290:        w->active = NULL;
1.14      nicm      291:
1.17      nicm      292:        w->lastlayout = -1;
1.14      nicm      293:        w->layout_root = NULL;
1.42      nicm      294:
1.1       nicm      295:        w->sx = sx;
                    296:        w->sy = sy;
                    297:
1.38      nicm      298:        queue_window_name(w);
                    299:
1.7       nicm      300:        options_init(&w->options, &global_w_options);
1.1       nicm      301:
                    302:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                    303:                if (ARRAY_ITEM(&windows, i) == NULL) {
                    304:                        ARRAY_SET(&windows, i, w);
                    305:                        break;
                    306:                }
                    307:        }
                    308:        if (i == ARRAY_LENGTH(&windows))
                    309:                ARRAY_ADD(&windows, w);
                    310:        w->references = 0;
                    311:
                    312:        return (w);
                    313: }
                    314:
                    315: struct window *
1.23      nicm      316: window_create(const char *name, const char *cmd, const char *shell,
                    317:     const char *cwd, struct environ *env, struct termios *tio,
                    318:     u_int sx, u_int sy, u_int hlimit,char **cause)
1.1       nicm      319: {
1.14      nicm      320:        struct window           *w;
                    321:        struct window_pane      *wp;
1.1       nicm      322:
                    323:        w = window_create1(sx, sy);
1.16      nicm      324:        wp = window_add_pane(w, hlimit);
1.14      nicm      325:        layout_init(w);
1.23      nicm      326:        if (window_pane_spawn(wp, cmd, shell, cwd, env, tio, cause) != 0) {
1.1       nicm      327:                window_destroy(w);
                    328:                return (NULL);
                    329:        }
                    330:        w->active = TAILQ_FIRST(&w->panes);
                    331:        if (name != NULL) {
                    332:                w->name = xstrdup(name);
                    333:                options_set_number(&w->options, "automatic-rename", 0);
                    334:        } else
                    335:                w->name = default_window_name(w);
                    336:        return (w);
                    337: }
                    338:
                    339: void
                    340: window_destroy(struct window *w)
                    341: {
                    342:        u_int   i;
                    343:
                    344:        if (window_index(w, &i) != 0)
                    345:                fatalx("index not found");
                    346:        ARRAY_SET(&windows, i, NULL);
                    347:        while (!ARRAY_EMPTY(&windows) && ARRAY_LAST(&windows) == NULL)
                    348:                ARRAY_TRUNC(&windows, 1);
                    349:
1.14      nicm      350:        if (w->layout_root != NULL)
                    351:                layout_free(w);
                    352:
1.38      nicm      353:        evtimer_del(&w->name_timer);
                    354:
1.1       nicm      355:        options_free(&w->options);
                    356:
                    357:        window_destroy_panes(w);
                    358:
                    359:        if (w->name != NULL)
                    360:                xfree(w->name);
                    361:        xfree(w);
1.72    ! nicm      362: }
        !           363:
        !           364: void
        !           365: window_set_name(struct window *w, const char *new_name)
        !           366: {
        !           367:        if (w->name != NULL)
        !           368:                xfree(w->name);
        !           369:        w->name = xstrdup(new_name);
1.1       nicm      370: }
                    371:
1.15      nicm      372: void
1.1       nicm      373: window_resize(struct window *w, u_int sx, u_int sy)
                    374: {
                    375:        w->sx = sx;
                    376:        w->sy = sy;
                    377: }
                    378:
                    379: void
                    380: window_set_active_pane(struct window *w, struct window_pane *wp)
                    381: {
1.59      nicm      382:        if (wp == w->active)
                    383:                return;
1.58      nicm      384:        w->last = w->active;
1.1       nicm      385:        w->active = wp;
1.10      nicm      386:        while (!window_pane_visible(w->active)) {
1.1       nicm      387:                w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10      nicm      388:                if (w->active == NULL)
                    389:                        w->active = TAILQ_LAST(&w->panes, window_panes);
                    390:                if (w->active == wp)
                    391:                        return;
1.29      nicm      392:        }
                    393: }
                    394:
1.66      nicm      395: struct window_pane *
                    396: window_get_active_at(struct window *w, u_int x, u_int y)
1.29      nicm      397: {
                    398:        struct window_pane      *wp;
                    399:
                    400:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.66      nicm      401:                if (!window_pane_visible(wp))
1.29      nicm      402:                        continue;
1.66      nicm      403:                if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29      nicm      404:                        continue;
1.66      nicm      405:                if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29      nicm      406:                        continue;
1.66      nicm      407:                return (wp);
                    408:        }
                    409:        return (NULL);
                    410: }
                    411:
                    412: void
                    413: window_set_active_at(struct window *w, u_int x, u_int y)
                    414: {
                    415:        struct window_pane      *wp;
                    416:
                    417:        wp = window_get_active_at(w, x, y);
                    418:        if (wp != NULL && wp != w->active)
1.29      nicm      419:                window_set_active_pane(w, wp);
1.66      nicm      420: }
                    421:
                    422: struct window_pane *
                    423: window_find_string(struct window *w, const char *s)
                    424: {
                    425:        u_int   x, y;
                    426:
                    427:        x = w->sx / 2;
                    428:        y = w->sy / 2;
                    429:
                    430:        if (strcasecmp(s, "top") == 0)
                    431:                y = 0;
                    432:        else if (strcasecmp(s, "bottom") == 0)
                    433:                y = w->sy - 1;
                    434:        else if (strcasecmp(s, "left") == 0)
                    435:                x = 0;
                    436:        else if (strcasecmp(s, "right") == 0)
                    437:                x = w->sx - 1;
                    438:        else if (strcasecmp(s, "top-left") == 0) {
                    439:                x = 0;
                    440:                y = 0;
                    441:        } else if (strcasecmp(s, "top-right") == 0) {
                    442:                x = w->sx - 1;
                    443:                y = 0;
                    444:        } else if (strcasecmp(s, "bottom-left") == 0) {
                    445:                x = 0;
                    446:                y = w->sy - 1;
                    447:        } else if (strcasecmp(s, "bottom-right") == 0) {
                    448:                x = w->sx - 1;
                    449:                y = w->sy - 1;
                    450:        } else
                    451:                return (NULL);
                    452:
                    453:        return (window_get_active_at(w, x, y));
1.1       nicm      454: }
                    455:
                    456: struct window_pane *
1.16      nicm      457: window_add_pane(struct window *w, u_int hlimit)
1.1       nicm      458: {
                    459:        struct window_pane      *wp;
                    460:
1.14      nicm      461:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1       nicm      462:        if (TAILQ_EMPTY(&w->panes))
                    463:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    464:        else
                    465:                TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
                    466:        return (wp);
                    467: }
                    468:
                    469: void
                    470: window_remove_pane(struct window *w, struct window_pane *wp)
                    471: {
1.57      nicm      472:        if (wp == w->active) {
1.58      nicm      473:                w->active = w->last;
                    474:                w->last = NULL;
                    475:                if (w->active == NULL) {
                    476:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    477:                        if (w->active == NULL)
                    478:                                w->active = TAILQ_NEXT(wp, entry);
                    479:                }
                    480:        } else if (wp == w->last)
                    481:                w->last = NULL;
1.1       nicm      482:
                    483:        TAILQ_REMOVE(&w->panes, wp, entry);
                    484:        window_pane_destroy(wp);
                    485: }
                    486:
                    487: struct window_pane *
                    488: window_pane_at_index(struct window *w, u_int idx)
                    489: {
                    490:        struct window_pane      *wp;
                    491:        u_int                    n;
                    492:
1.67      nicm      493:        n = options_get_number(&w->options, "pane-base-index");
1.1       nicm      494:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    495:                if (n == idx)
                    496:                        return (wp);
                    497:                n++;
                    498:        }
                    499:        return (NULL);
1.53      nicm      500: }
                    501:
                    502: struct window_pane *
                    503: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    504: {
                    505:        for (; n > 0; n--) {
                    506:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    507:                        wp = TAILQ_FIRST(&w->panes);
                    508:        }
                    509:
                    510:        return (wp);
                    511: }
                    512:
                    513: struct window_pane *
                    514: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    515:     u_int n)
                    516: {
                    517:        for (; n > 0; n--) {
                    518:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    519:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    520:        }
                    521:
                    522:        return (wp);
1.13      nicm      523: }
                    524:
1.69      nicm      525: int
                    526: window_pane_index(struct window_pane *wp, u_int *i)
1.13      nicm      527: {
                    528:        struct window_pane      *wq;
1.69      nicm      529:        struct window           *w = wp->window;
1.13      nicm      530:
1.69      nicm      531:        *i = options_get_number(&w->options, "pane-base-index");
1.13      nicm      532:        TAILQ_FOREACH(wq, &w->panes, entry) {
1.69      nicm      533:                if (wp == wq) {
                    534:                        return (0);
                    535:                }
                    536:                (*i)++;
1.13      nicm      537:        }
1.69      nicm      538:
                    539:        return (-1);
1.1       nicm      540: }
                    541:
                    542: u_int
                    543: window_count_panes(struct window *w)
                    544: {
                    545:        struct window_pane      *wp;
                    546:        u_int                    n;
                    547:
                    548:        n = 0;
                    549:        TAILQ_FOREACH(wp, &w->panes, entry)
                    550:                n++;
                    551:        return (n);
                    552: }
                    553:
                    554: void
                    555: window_destroy_panes(struct window *w)
                    556: {
                    557:        struct window_pane      *wp;
                    558:
                    559:        while (!TAILQ_EMPTY(&w->panes)) {
                    560:                wp = TAILQ_FIRST(&w->panes);
                    561:                TAILQ_REMOVE(&w->panes, wp, entry);
                    562:                window_pane_destroy(wp);
                    563:        }
1.61      nicm      564: }
                    565:
                    566: /* Return list of printable window flag symbols. No flags is just a space. */
                    567: char *
                    568: window_printable_flags(struct session *s, struct winlink *wl)
                    569: {
                    570:        char    flags[BUFSIZ];
                    571:        int     pos;
                    572:
                    573:        pos = 0;
                    574:        if (wl->flags & WINLINK_ACTIVITY)
                    575:                flags[pos++] = '#';
                    576:        if (wl->flags & WINLINK_BELL)
                    577:                flags[pos++] = '!';
                    578:        if (wl->flags & WINLINK_CONTENT)
                    579:                flags[pos++] = '+';
                    580:        if (wl->flags & WINLINK_SILENCE)
                    581:                flags[pos++] = '~';
                    582:        if (wl == s->curw)
                    583:                flags[pos++] = '*';
                    584:        if (wl == TAILQ_FIRST(&s->lastw))
                    585:                flags[pos++] = '-';
                    586:        if (pos == 0)
                    587:                flags[pos++] = ' ';
                    588:        flags[pos] = '\0';
                    589:        return (xstrdup(flags));
1.1       nicm      590: }
                    591:
1.64      nicm      592: /* Find pane in global tree by id. */
                    593: struct window_pane *
                    594: window_pane_find_by_id(u_int id)
                    595: {
                    596:        struct window_pane      wp;
                    597:
                    598:        wp.id = id;
                    599:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
                    600: }
                    601:
1.1       nicm      602: struct window_pane *
                    603: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    604: {
                    605:        struct window_pane      *wp;
                    606:
                    607:        wp = xcalloc(1, sizeof *wp);
                    608:        wp->window = w;
                    609:
1.71      nicm      610:        wp->id = next_window_pane_id++;
1.64      nicm      611:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
                    612:
1.1       nicm      613:        wp->cmd = NULL;
1.23      nicm      614:        wp->shell = NULL;
1.1       nicm      615:        wp->cwd = NULL;
                    616:
                    617:        wp->fd = -1;
1.37      nicm      618:        wp->event = NULL;
1.1       nicm      619:
                    620:        wp->mode = NULL;
1.14      nicm      621:
                    622:        wp->layout_cell = NULL;
1.1       nicm      623:
                    624:        wp->xoff = 0;
1.42      nicm      625:        wp->yoff = 0;
1.1       nicm      626:
                    627:        wp->sx = sx;
                    628:        wp->sy = sy;
                    629:
1.32      nicm      630:        wp->pipe_fd = -1;
                    631:        wp->pipe_off = 0;
1.36      nicm      632:        wp->pipe_event = NULL;
1.32      nicm      633:
1.9       nicm      634:        wp->saved_grid = NULL;
                    635:
1.1       nicm      636:        screen_init(&wp->base, sx, sy, hlimit);
                    637:        wp->screen = &wp->base;
                    638:
                    639:        input_init(wp);
                    640:
                    641:        return (wp);
                    642: }
                    643:
                    644: void
                    645: window_pane_destroy(struct window_pane *wp)
                    646: {
1.55      nicm      647:        window_pane_reset_mode(wp);
                    648:
1.37      nicm      649:        if (wp->fd != -1) {
1.70      nicm      650:                bufferevent_free(wp->event);
1.1       nicm      651:                close(wp->fd);
1.37      nicm      652:        }
1.1       nicm      653:
                    654:        input_free(wp);
                    655:
                    656:        screen_free(&wp->base);
1.9       nicm      657:        if (wp->saved_grid != NULL)
                    658:                grid_destroy(wp->saved_grid);
1.1       nicm      659:
1.32      nicm      660:        if (wp->pipe_fd != -1) {
1.70      nicm      661:                bufferevent_free(wp->pipe_event);
1.32      nicm      662:                close(wp->pipe_fd);
                    663:        }
                    664:
1.64      nicm      665:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    666:
1.1       nicm      667:        if (wp->cwd != NULL)
                    668:                xfree(wp->cwd);
1.23      nicm      669:        if (wp->shell != NULL)
                    670:                xfree(wp->shell);
1.1       nicm      671:        if (wp->cmd != NULL)
                    672:                xfree(wp->cmd);
                    673:        xfree(wp);
                    674: }
                    675:
                    676: int
1.23      nicm      677: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21      nicm      678:     const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1       nicm      679: {
1.47      nicm      680:        struct winsize   ws;
1.64      nicm      681:        char            *argv0, paneid[16];
1.47      nicm      682:        const char      *ptr;
                    683:        struct termios   tio2;
1.1       nicm      684:
1.37      nicm      685:        if (wp->fd != -1) {
1.70      nicm      686:                bufferevent_free(wp->event);
1.1       nicm      687:                close(wp->fd);
1.37      nicm      688:        }
1.1       nicm      689:        if (cmd != NULL) {
                    690:                if (wp->cmd != NULL)
                    691:                        xfree(wp->cmd);
                    692:                wp->cmd = xstrdup(cmd);
                    693:        }
1.23      nicm      694:        if (shell != NULL) {
                    695:                if (wp->shell != NULL)
                    696:                        xfree(wp->shell);
                    697:                wp->shell = xstrdup(shell);
                    698:        }
1.1       nicm      699:        if (cwd != NULL) {
                    700:                if (wp->cwd != NULL)
                    701:                        xfree(wp->cwd);
                    702:                wp->cwd = xstrdup(cwd);
                    703:        }
                    704:
                    705:        memset(&ws, 0, sizeof ws);
                    706:        ws.ws_col = screen_size_x(&wp->base);
                    707:        ws.ws_row = screen_size_y(&wp->base);
                    708:
1.42      nicm      709:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      710:        case -1:
                    711:                wp->fd = -1;
                    712:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
                    713:                return (-1);
                    714:        case 0:
                    715:                if (chdir(wp->cwd) != 0)
                    716:                        chdir("/");
1.25      nicm      717:
                    718:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    719:                        fatal("tcgetattr failed");
                    720:                if (tio != NULL)
                    721:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    722:                tio2.c_cc[VERASE] = '\177';
                    723:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    724:                        fatal("tcgetattr failed");
1.18      nicm      725:
1.56      nicm      726:                closefrom(STDERR_FILENO + 1);
                    727:
1.64      nicm      728:                xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
                    729:                environ_set(env, "TMUX_PANE", paneid);
1.47      nicm      730:                environ_push(env);
1.18      nicm      731:
1.54      nicm      732:                clear_signals(1);
1.1       nicm      733:                log_close();
                    734:
1.8       nicm      735:                if (*wp->cmd != '\0') {
1.24      nicm      736:                        /* Set SHELL but only if it is currently not useful. */
                    737:                        shell = getenv("SHELL");
1.68      nicm      738:                        if (checkshell(shell))
1.24      nicm      739:                                setenv("SHELL", wp->shell, 1);
                    740:
1.8       nicm      741:                        execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
                    742:                        fatal("execl failed");
                    743:                }
                    744:
                    745:                /* No command; fork a login shell. */
1.23      nicm      746:                ptr = strrchr(wp->shell, '/');
                    747:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      748:                        xasprintf(&argv0, "-%s", ptr + 1);
                    749:                else
1.23      nicm      750:                        xasprintf(&argv0, "-%s", wp->shell);
1.24      nicm      751:                setenv("SHELL", wp->shell, 1);
1.23      nicm      752:                execl(wp->shell, argv0, (char *) NULL);
1.1       nicm      753:                fatal("execl failed");
                    754:        }
                    755:
1.62      nicm      756:        setblocking(wp->fd, 0);
                    757:
1.37      nicm      758:        wp->event = bufferevent_new(wp->fd,
                    759:            window_pane_read_callback, NULL, window_pane_error_callback, wp);
                    760:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      761:
                    762:        return (0);
                    763: }
                    764:
1.41      nicm      765: /* ARGSUSED */
1.15      nicm      766: void
1.37      nicm      767: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
                    768: {
1.46      nicm      769:        struct window_pane     *wp = data;
                    770:        char                   *new_data;
                    771:        size_t                  new_size;
                    772:
                    773:        new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
                    774:        if (wp->pipe_fd != -1 && new_size > 0) {
                    775:                new_data = EVBUFFER_DATA(wp->event->input);
                    776:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    777:        }
                    778:
                    779:        input_parse(wp);
1.37      nicm      780:
1.46      nicm      781:        wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.60      nicm      782:
                    783:        /*
                    784:         * If we get here, we're not outputting anymore, so set the silence
                    785:         * flag on the window.
                    786:         */
                    787:        wp->window->flags |= WINDOW_SILENCE;
                    788:        if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
                    789:                fatal("gettimeofday failed.");
1.37      nicm      790: }
                    791:
1.41      nicm      792: /* ARGSUSED */
1.37      nicm      793: void
                    794: window_pane_error_callback(
                    795:     unused struct bufferevent *bufev, unused short what, void *data)
                    796: {
                    797:        struct window_pane *wp = data;
                    798:
1.39      nicm      799:        server_destroy_pane(wp);
1.37      nicm      800: }
                    801:
                    802: void
1.1       nicm      803: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    804: {
                    805:        struct winsize  ws;
                    806:
                    807:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      808:                return;
1.1       nicm      809:        wp->sx = sx;
                    810:        wp->sy = sy;
                    811:
                    812:        memset(&ws, 0, sizeof ws);
                    813:        ws.ws_col = sx;
                    814:        ws.ws_row = sy;
                    815:
                    816:        screen_resize(&wp->base, sx, sy);
                    817:        if (wp->mode != NULL)
                    818:                wp->mode->resize(wp, sx, sy);
                    819:
                    820:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
                    821:                fatal("ioctl failed");
1.44      nicm      822: }
                    823:
                    824: /*
                    825:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    826:  * history is not updated
                    827:  */
                    828: void
                    829: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
                    830: {
                    831:        struct screen   *s = &wp->base;
                    832:        u_int            sx, sy;
                    833:
                    834:        if (wp->saved_grid != NULL)
                    835:                return;
                    836:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    837:                return;
                    838:        sx = screen_size_x(s);
                    839:        sy = screen_size_y(s);
                    840:
                    841:        wp->saved_grid = grid_create(sx, sy, 0);
                    842:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
                    843:        wp->saved_cx = s->cx;
                    844:        wp->saved_cy = s->cy;
                    845:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                    846:
                    847:        grid_view_clear(s->grid, 0, 0, sx, sy);
                    848:
                    849:        wp->base.grid->flags &= ~GRID_HISTORY;
                    850:
                    851:        wp->flags |= PANE_REDRAW;
                    852: }
                    853:
                    854: /* Exit alternate screen mode and restore the copied grid. */
                    855: void
                    856: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
                    857: {
                    858:        struct screen   *s = &wp->base;
                    859:        u_int            sx, sy;
                    860:
                    861:        if (wp->saved_grid == NULL)
                    862:                return;
                    863:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    864:                return;
                    865:        sx = screen_size_x(s);
                    866:        sy = screen_size_y(s);
                    867:
                    868:        /*
                    869:         * If the current size is bigger, temporarily resize to the old size
                    870:         * before copying back.
                    871:         */
                    872:        if (sy > wp->saved_grid->sy)
                    873:                screen_resize(s, sx, wp->saved_grid->sy);
                    874:
                    875:        /* Restore the grid, cursor position and cell. */
                    876:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
                    877:        s->cx = wp->saved_cx;
                    878:        if (s->cx > screen_size_x(s) - 1)
                    879:                s->cx = screen_size_x(s) - 1;
                    880:        s->cy = wp->saved_cy;
                    881:        if (s->cy > screen_size_y(s) - 1)
                    882:                s->cy = screen_size_y(s) - 1;
                    883:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                    884:
                    885:        /*
                    886:         * Turn history back on (so resize can use it) and then resize back to
                    887:         * the current size.
                    888:         */
                    889:        wp->base.grid->flags |= GRID_HISTORY;
                    890:        if (sy > wp->saved_grid->sy)
                    891:                screen_resize(s, sx, sy);
                    892:
                    893:        grid_destroy(wp->saved_grid);
                    894:        wp->saved_grid = NULL;
                    895:
                    896:        wp->flags |= PANE_REDRAW;
1.1       nicm      897: }
                    898:
                    899: int
                    900: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                    901: {
                    902:        struct screen   *s;
                    903:
1.15      nicm      904:        if (wp->mode != NULL)
1.1       nicm      905:                return (1);
                    906:        wp->mode = mode;
                    907:
                    908:        if ((s = wp->mode->init(wp)) != NULL)
                    909:                wp->screen = s;
1.34      nicm      910:        wp->flags |= PANE_REDRAW;
1.1       nicm      911:        return (0);
                    912: }
                    913:
                    914: void
                    915: window_pane_reset_mode(struct window_pane *wp)
                    916: {
                    917:        if (wp->mode == NULL)
                    918:                return;
                    919:
                    920:        wp->mode->free(wp);
                    921:        wp->mode = NULL;
                    922:
                    923:        wp->screen = &wp->base;
1.34      nicm      924:        wp->flags |= PANE_REDRAW;
1.1       nicm      925: }
                    926:
                    927: void
1.51      nicm      928: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1       nicm      929: {
1.27      nicm      930:        struct window_pane      *wp2;
                    931:
1.30      nicm      932:        if (!window_pane_visible(wp))
1.3       nicm      933:                return;
                    934:
1.1       nicm      935:        if (wp->mode != NULL) {
                    936:                if (wp->mode->key != NULL)
1.51      nicm      937:                        wp->mode->key(wp, sess, key);
1.27      nicm      938:                return;
1.30      nicm      939:        }
1.27      nicm      940:
1.30      nicm      941:        if (wp->fd == -1)
                    942:                return;
1.27      nicm      943:        input_key(wp, key);
                    944:        if (options_get_number(&wp->window->options, "synchronize-panes")) {
                    945:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    946:                        if (wp2 == wp || wp2->mode != NULL)
                    947:                                continue;
                    948:                        if (wp2->fd != -1 && window_pane_visible(wp2))
                    949:                                input_key(wp2, key);
                    950:                }
                    951:        }
1.1       nicm      952: }
                    953:
                    954: void
                    955: window_pane_mouse(
1.51      nicm      956:     struct window_pane *wp, struct session *sess, struct mouse_event *m)
1.1       nicm      957: {
1.30      nicm      958:        if (!window_pane_visible(wp))
1.3       nicm      959:                return;
                    960:
1.31      nicm      961:        if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1       nicm      962:                return;
1.31      nicm      963:        if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1       nicm      964:                return;
1.31      nicm      965:        m->x -= wp->xoff;
                    966:        m->y -= wp->yoff;
1.1       nicm      967:
                    968:        if (wp->mode != NULL) {
1.65      nicm      969:                if (wp->mode->mouse != NULL &&
                    970:                    options_get_number(&wp->window->options, "mode-mouse"))
1.51      nicm      971:                        wp->mode->mouse(wp, sess, m);
1.30      nicm      972:        } else if (wp->fd != -1)
1.31      nicm      973:                input_mouse(wp, m);
1.10      nicm      974: }
                    975:
                    976: int
                    977: window_pane_visible(struct window_pane *wp)
                    978: {
                    979:        struct window   *w = wp->window;
                    980:
                    981:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                    982:                return (0);
                    983:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                    984:                return (0);
                    985:        return (1);
1.1       nicm      986: }
                    987:
                    988: char *
1.5       nicm      989: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1       nicm      990: {
1.4       nicm      991:        struct screen   *s = &wp->base;
1.5       nicm      992:        char            *newsearchstr, *line, *msg;
1.4       nicm      993:        u_int            i;
                    994:
1.5       nicm      995:        msg = NULL;
                    996:        xasprintf(&newsearchstr, "*%s*", searchstr);
                    997:
1.4       nicm      998:        for (i = 0; i < screen_size_y(s); i++) {
                    999:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm     1000:                if (fnmatch(newsearchstr, line, 0) == 0) {
                   1001:                        msg = line;
                   1002:                        if (lineno != NULL)
                   1003:                                *lineno = i;
                   1004:                        break;
                   1005:                }
1.4       nicm     1006:                xfree(line);
                   1007:        }
1.5       nicm     1008:
                   1009:        xfree(newsearchstr);
                   1010:        return (msg);
1.45      nicm     1011: }
                   1012:
                   1013: /* Find the pane directly above another. */
                   1014: struct window_pane *
                   1015: window_pane_find_up(struct window_pane *wp)
                   1016: {
                   1017:        struct window_pane     *wp2;
                   1018:        u_int                   left, top;
                   1019:
                   1020:        if (wp == NULL || !window_pane_visible(wp))
                   1021:                return (NULL);
                   1022:
                   1023:        top = wp->yoff;
                   1024:        if (top == 0)
                   1025:                top = wp->window->sy + 1;
                   1026:        left = wp->xoff;
                   1027:
                   1028:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1029:                if (!window_pane_visible(wp2))
                   1030:                        continue;
                   1031:                if (wp2->yoff + wp2->sy + 1 != top)
                   1032:                        continue;
                   1033:                if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
                   1034:                        return (wp2);
                   1035:        }
                   1036:        return (NULL);
                   1037: }
                   1038:
                   1039: /* Find the pane directly below another. */
                   1040: struct window_pane *
                   1041: window_pane_find_down(struct window_pane *wp)
                   1042: {
                   1043:        struct window_pane     *wp2;
                   1044:        u_int                   left, bottom;
                   1045:
                   1046:        if (wp == NULL || !window_pane_visible(wp))
                   1047:                return (NULL);
                   1048:
                   1049:        bottom = wp->yoff + wp->sy + 1;
                   1050:        if (bottom >= wp->window->sy)
                   1051:                bottom = 0;
                   1052:        left = wp->xoff;
                   1053:
                   1054:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1055:                if (!window_pane_visible(wp2))
                   1056:                        continue;
                   1057:                if (wp2->yoff != bottom)
                   1058:                        continue;
                   1059:                if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
                   1060:                        return (wp2);
                   1061:        }
                   1062:        return (NULL);
                   1063: }
                   1064:
                   1065: /*
                   1066:  * Find the pane directly to the left of another, adjacent to the left side and
                   1067:  * containing the top edge.
                   1068:  */
                   1069: struct window_pane *
                   1070: window_pane_find_left(struct window_pane *wp)
                   1071: {
                   1072:        struct window_pane     *wp2;
                   1073:        u_int                   left, top;
                   1074:
                   1075:        if (wp == NULL || !window_pane_visible(wp))
                   1076:                return (NULL);
                   1077:
                   1078:        left = wp->xoff;
                   1079:        if (left == 0)
                   1080:                left = wp->window->sx + 1;
                   1081:        top = wp->yoff;
                   1082:
                   1083:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1084:                if (!window_pane_visible(wp2))
                   1085:                        continue;
                   1086:                if (wp2->xoff + wp2->sx + 1 != left)
                   1087:                        continue;
                   1088:                if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
                   1089:                        return (wp2);
                   1090:        }
                   1091:        return (NULL);
                   1092: }
                   1093:
                   1094: /*
                   1095:  * Find the pane directly to the right of another, that is adjacent to the
                   1096:  * right edge and including the top edge.
                   1097:  */
                   1098: struct window_pane *
                   1099: window_pane_find_right(struct window_pane *wp)
                   1100: {
                   1101:        struct window_pane     *wp2;
                   1102:        u_int                   right, top;
                   1103:
                   1104:        if (wp == NULL || !window_pane_visible(wp))
                   1105:                return (NULL);
                   1106:
                   1107:        right = wp->xoff + wp->sx + 1;
                   1108:        if (right >= wp->window->sx)
                   1109:                right = 0;
                   1110:        top = wp->yoff;
                   1111:
                   1112:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1113:                if (!window_pane_visible(wp2))
                   1114:                        continue;
                   1115:                if (wp2->xoff != right)
                   1116:                        continue;
                   1117:                if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
                   1118:                        return (wp2);
                   1119:        }
                   1120:        return (NULL);
1.1       nicm     1121: }