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

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