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

1.64    ! nicm        1: /* $OpenBSD: window.c,v 1.63 2011/01/25 22:31:50 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:
                    359: void
                    360: window_set_active_at(struct window *w, u_int x, u_int y)
                    361: {
                    362:        struct window_pane      *wp;
                    363:
                    364:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.59      nicm      365:                if (wp == w->active || !window_pane_visible(wp))
1.29      nicm      366:                        continue;
                    367:                if (x < wp->xoff || x >= wp->xoff + wp->sx)
                    368:                        continue;
                    369:                if (y < wp->yoff || y >= wp->yoff + wp->sy)
                    370:                        continue;
                    371:                window_set_active_pane(w, wp);
                    372:                break;
1.10      nicm      373:        }
1.1       nicm      374: }
                    375:
                    376: struct window_pane *
1.16      nicm      377: window_add_pane(struct window *w, u_int hlimit)
1.1       nicm      378: {
                    379:        struct window_pane      *wp;
                    380:
1.14      nicm      381:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1       nicm      382:        if (TAILQ_EMPTY(&w->panes))
                    383:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    384:        else
                    385:                TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
                    386:        return (wp);
                    387: }
                    388:
                    389: void
                    390: window_remove_pane(struct window *w, struct window_pane *wp)
                    391: {
1.57      nicm      392:        if (wp == w->active) {
1.58      nicm      393:                w->active = w->last;
                    394:                w->last = NULL;
                    395:                if (w->active == NULL) {
                    396:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    397:                        if (w->active == NULL)
                    398:                                w->active = TAILQ_NEXT(wp, entry);
                    399:                }
                    400:        } else if (wp == w->last)
                    401:                w->last = NULL;
1.1       nicm      402:
                    403:        TAILQ_REMOVE(&w->panes, wp, entry);
                    404:        window_pane_destroy(wp);
                    405: }
                    406:
                    407: struct window_pane *
                    408: window_pane_at_index(struct window *w, u_int idx)
                    409: {
                    410:        struct window_pane      *wp;
                    411:        u_int                    n;
                    412:
                    413:        n = 0;
                    414:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    415:                if (n == idx)
                    416:                        return (wp);
                    417:                n++;
                    418:        }
                    419:        return (NULL);
1.53      nicm      420: }
                    421:
                    422: struct window_pane *
                    423: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    424: {
                    425:        for (; n > 0; n--) {
                    426:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    427:                        wp = TAILQ_FIRST(&w->panes);
                    428:        }
                    429:
                    430:        return (wp);
                    431: }
                    432:
                    433: struct window_pane *
                    434: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    435:     u_int n)
                    436: {
                    437:        for (; n > 0; n--) {
                    438:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    439:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    440:        }
                    441:
                    442:        return (wp);
1.13      nicm      443: }
                    444:
                    445: u_int
                    446: window_pane_index(struct window *w, struct window_pane *wp)
                    447: {
                    448:        struct window_pane      *wq;
                    449:        u_int                    n;
                    450:
                    451:        n = 0;
                    452:        TAILQ_FOREACH(wq, &w->panes, entry) {
                    453:                if (wp == wq)
                    454:                        break;
                    455:                n++;
                    456:        }
                    457:        return (n);
1.1       nicm      458: }
                    459:
                    460: u_int
                    461: window_count_panes(struct window *w)
                    462: {
                    463:        struct window_pane      *wp;
                    464:        u_int                    n;
                    465:
                    466:        n = 0;
                    467:        TAILQ_FOREACH(wp, &w->panes, entry)
                    468:                n++;
                    469:        return (n);
                    470: }
                    471:
                    472: void
                    473: window_destroy_panes(struct window *w)
                    474: {
                    475:        struct window_pane      *wp;
                    476:
                    477:        while (!TAILQ_EMPTY(&w->panes)) {
                    478:                wp = TAILQ_FIRST(&w->panes);
                    479:                TAILQ_REMOVE(&w->panes, wp, entry);
                    480:                window_pane_destroy(wp);
                    481:        }
1.61      nicm      482: }
                    483:
                    484: /* Return list of printable window flag symbols. No flags is just a space. */
                    485: char *
                    486: window_printable_flags(struct session *s, struct winlink *wl)
                    487: {
                    488:        char    flags[BUFSIZ];
                    489:        int     pos;
                    490:
                    491:        pos = 0;
                    492:        if (wl->flags & WINLINK_ACTIVITY)
                    493:                flags[pos++] = '#';
                    494:        if (wl->flags & WINLINK_BELL)
                    495:                flags[pos++] = '!';
                    496:        if (wl->flags & WINLINK_CONTENT)
                    497:                flags[pos++] = '+';
                    498:        if (wl->flags & WINLINK_SILENCE)
                    499:                flags[pos++] = '~';
                    500:        if (wl == s->curw)
                    501:                flags[pos++] = '*';
                    502:        if (wl == TAILQ_FIRST(&s->lastw))
                    503:                flags[pos++] = '-';
                    504:        if (pos == 0)
                    505:                flags[pos++] = ' ';
                    506:        flags[pos] = '\0';
                    507:        return (xstrdup(flags));
1.1       nicm      508: }
                    509:
1.64    ! nicm      510: /* Find pane in global tree by id. */
        !           511: struct window_pane *
        !           512: window_pane_find_by_id(u_int id)
        !           513: {
        !           514:        struct window_pane      wp;
        !           515:
        !           516:        wp.id = id;
        !           517:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
        !           518: }
        !           519:
1.1       nicm      520: struct window_pane *
                    521: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    522: {
                    523:        struct window_pane      *wp;
                    524:
                    525:        wp = xcalloc(1, sizeof *wp);
                    526:        wp->window = w;
                    527:
1.64    ! nicm      528:        wp->id = next_window_pane++;
        !           529:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
        !           530:
1.1       nicm      531:        wp->cmd = NULL;
1.23      nicm      532:        wp->shell = NULL;
1.1       nicm      533:        wp->cwd = NULL;
                    534:
                    535:        wp->fd = -1;
1.37      nicm      536:        wp->event = NULL;
1.1       nicm      537:
                    538:        wp->mode = NULL;
1.14      nicm      539:
                    540:        wp->layout_cell = NULL;
1.1       nicm      541:
                    542:        wp->xoff = 0;
1.42      nicm      543:        wp->yoff = 0;
1.1       nicm      544:
                    545:        wp->sx = sx;
                    546:        wp->sy = sy;
                    547:
1.32      nicm      548:        wp->pipe_fd = -1;
                    549:        wp->pipe_off = 0;
1.36      nicm      550:        wp->pipe_event = NULL;
1.32      nicm      551:
1.9       nicm      552:        wp->saved_grid = NULL;
                    553:
1.1       nicm      554:        screen_init(&wp->base, sx, sy, hlimit);
                    555:        wp->screen = &wp->base;
                    556:
                    557:        input_init(wp);
                    558:
                    559:        return (wp);
                    560: }
                    561:
                    562: void
                    563: window_pane_destroy(struct window_pane *wp)
                    564: {
1.55      nicm      565:        window_pane_reset_mode(wp);
                    566:
1.37      nicm      567:        if (wp->fd != -1) {
1.1       nicm      568:                close(wp->fd);
1.37      nicm      569:                bufferevent_free(wp->event);
                    570:        }
1.1       nicm      571:
                    572:        input_free(wp);
                    573:
                    574:        screen_free(&wp->base);
1.9       nicm      575:        if (wp->saved_grid != NULL)
                    576:                grid_destroy(wp->saved_grid);
1.1       nicm      577:
1.32      nicm      578:        if (wp->pipe_fd != -1) {
                    579:                close(wp->pipe_fd);
1.36      nicm      580:                bufferevent_free(wp->pipe_event);
1.32      nicm      581:        }
                    582:
1.64    ! nicm      583:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
        !           584:
1.1       nicm      585:        if (wp->cwd != NULL)
                    586:                xfree(wp->cwd);
1.23      nicm      587:        if (wp->shell != NULL)
                    588:                xfree(wp->shell);
1.1       nicm      589:        if (wp->cmd != NULL)
                    590:                xfree(wp->cmd);
                    591:        xfree(wp);
                    592: }
                    593:
                    594: int
1.23      nicm      595: window_pane_spawn(struct window_pane *wp, const char *cmd, const char *shell,
1.21      nicm      596:     const char *cwd, struct environ *env, struct termios *tio, char **cause)
1.1       nicm      597: {
1.47      nicm      598:        struct winsize   ws;
1.64    ! nicm      599:        char            *argv0, paneid[16];
1.47      nicm      600:        const char      *ptr;
                    601:        struct termios   tio2;
1.1       nicm      602:
1.37      nicm      603:        if (wp->fd != -1) {
1.1       nicm      604:                close(wp->fd);
1.37      nicm      605:                bufferevent_free(wp->event);
                    606:        }
1.1       nicm      607:        if (cmd != NULL) {
                    608:                if (wp->cmd != NULL)
                    609:                        xfree(wp->cmd);
                    610:                wp->cmd = xstrdup(cmd);
                    611:        }
1.23      nicm      612:        if (shell != NULL) {
                    613:                if (wp->shell != NULL)
                    614:                        xfree(wp->shell);
                    615:                wp->shell = xstrdup(shell);
                    616:        }
1.1       nicm      617:        if (cwd != NULL) {
                    618:                if (wp->cwd != NULL)
                    619:                        xfree(wp->cwd);
                    620:                wp->cwd = xstrdup(cwd);
                    621:        }
                    622:
                    623:        memset(&ws, 0, sizeof ws);
                    624:        ws.ws_col = screen_size_x(&wp->base);
                    625:        ws.ws_row = screen_size_y(&wp->base);
                    626:
1.42      nicm      627:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      628:        case -1:
                    629:                wp->fd = -1;
                    630:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
                    631:                return (-1);
                    632:        case 0:
                    633:                if (chdir(wp->cwd) != 0)
                    634:                        chdir("/");
1.25      nicm      635:
                    636:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    637:                        fatal("tcgetattr failed");
                    638:                if (tio != NULL)
                    639:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    640:                tio2.c_cc[VERASE] = '\177';
                    641:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    642:                        fatal("tcgetattr failed");
1.18      nicm      643:
1.56      nicm      644:                closefrom(STDERR_FILENO + 1);
                    645:
1.64    ! nicm      646:                xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
        !           647:                environ_set(env, "TMUX_PANE", paneid);
1.47      nicm      648:                environ_push(env);
1.18      nicm      649:
1.54      nicm      650:                clear_signals(1);
1.1       nicm      651:                log_close();
                    652:
1.8       nicm      653:                if (*wp->cmd != '\0') {
1.24      nicm      654:                        /* Set SHELL but only if it is currently not useful. */
                    655:                        shell = getenv("SHELL");
                    656:                        if (shell == NULL || *shell == '\0' || areshell(shell))
                    657:                                setenv("SHELL", wp->shell, 1);
                    658:
1.8       nicm      659:                        execl(_PATH_BSHELL, "sh", "-c", wp->cmd, (char *) NULL);
                    660:                        fatal("execl failed");
                    661:                }
                    662:
                    663:                /* No command; fork a login shell. */
1.23      nicm      664:                ptr = strrchr(wp->shell, '/');
                    665:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      666:                        xasprintf(&argv0, "-%s", ptr + 1);
                    667:                else
1.23      nicm      668:                        xasprintf(&argv0, "-%s", wp->shell);
1.24      nicm      669:                setenv("SHELL", wp->shell, 1);
1.23      nicm      670:                execl(wp->shell, argv0, (char *) NULL);
1.1       nicm      671:                fatal("execl failed");
                    672:        }
                    673:
1.62      nicm      674:        setblocking(wp->fd, 0);
                    675:
1.37      nicm      676:        wp->event = bufferevent_new(wp->fd,
                    677:            window_pane_read_callback, NULL, window_pane_error_callback, wp);
                    678:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      679:
                    680:        return (0);
                    681: }
                    682:
1.41      nicm      683: /* ARGSUSED */
1.15      nicm      684: void
1.37      nicm      685: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
                    686: {
1.46      nicm      687:        struct window_pane     *wp = data;
                    688:        char                   *new_data;
                    689:        size_t                  new_size;
                    690:
                    691:        new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
                    692:        if (wp->pipe_fd != -1 && new_size > 0) {
                    693:                new_data = EVBUFFER_DATA(wp->event->input);
                    694:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    695:        }
                    696:
                    697:        input_parse(wp);
1.37      nicm      698:
1.46      nicm      699:        wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.60      nicm      700:
                    701:        /*
                    702:         * If we get here, we're not outputting anymore, so set the silence
                    703:         * flag on the window.
                    704:         */
                    705:        wp->window->flags |= WINDOW_SILENCE;
                    706:        if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
                    707:                fatal("gettimeofday failed.");
1.37      nicm      708: }
                    709:
1.41      nicm      710: /* ARGSUSED */
1.37      nicm      711: void
                    712: window_pane_error_callback(
                    713:     unused struct bufferevent *bufev, unused short what, void *data)
                    714: {
                    715:        struct window_pane *wp = data;
                    716:
1.39      nicm      717:        server_destroy_pane(wp);
1.37      nicm      718: }
                    719:
                    720: void
1.1       nicm      721: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    722: {
                    723:        struct winsize  ws;
                    724:
                    725:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      726:                return;
1.1       nicm      727:        wp->sx = sx;
                    728:        wp->sy = sy;
                    729:
                    730:        memset(&ws, 0, sizeof ws);
                    731:        ws.ws_col = sx;
                    732:        ws.ws_row = sy;
                    733:
                    734:        screen_resize(&wp->base, sx, sy);
                    735:        if (wp->mode != NULL)
                    736:                wp->mode->resize(wp, sx, sy);
                    737:
                    738:        if (wp->fd != -1 && ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
                    739:                fatal("ioctl failed");
1.44      nicm      740: }
                    741:
                    742: /*
                    743:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    744:  * history is not updated
                    745:  */
                    746: void
                    747: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc)
                    748: {
                    749:        struct screen   *s = &wp->base;
                    750:        u_int            sx, sy;
                    751:
                    752:        if (wp->saved_grid != NULL)
                    753:                return;
                    754:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    755:                return;
                    756:        sx = screen_size_x(s);
                    757:        sy = screen_size_y(s);
                    758:
                    759:        wp->saved_grid = grid_create(sx, sy, 0);
                    760:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
                    761:        wp->saved_cx = s->cx;
                    762:        wp->saved_cy = s->cy;
                    763:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                    764:
                    765:        grid_view_clear(s->grid, 0, 0, sx, sy);
                    766:
                    767:        wp->base.grid->flags &= ~GRID_HISTORY;
                    768:
                    769:        wp->flags |= PANE_REDRAW;
                    770: }
                    771:
                    772: /* Exit alternate screen mode and restore the copied grid. */
                    773: void
                    774: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc)
                    775: {
                    776:        struct screen   *s = &wp->base;
                    777:        u_int            sx, sy;
                    778:
                    779:        if (wp->saved_grid == NULL)
                    780:                return;
                    781:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    782:                return;
                    783:        sx = screen_size_x(s);
                    784:        sy = screen_size_y(s);
                    785:
                    786:        /*
                    787:         * If the current size is bigger, temporarily resize to the old size
                    788:         * before copying back.
                    789:         */
                    790:        if (sy > wp->saved_grid->sy)
                    791:                screen_resize(s, sx, wp->saved_grid->sy);
                    792:
                    793:        /* Restore the grid, cursor position and cell. */
                    794:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
                    795:        s->cx = wp->saved_cx;
                    796:        if (s->cx > screen_size_x(s) - 1)
                    797:                s->cx = screen_size_x(s) - 1;
                    798:        s->cy = wp->saved_cy;
                    799:        if (s->cy > screen_size_y(s) - 1)
                    800:                s->cy = screen_size_y(s) - 1;
                    801:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                    802:
                    803:        /*
                    804:         * Turn history back on (so resize can use it) and then resize back to
                    805:         * the current size.
                    806:         */
                    807:        wp->base.grid->flags |= GRID_HISTORY;
                    808:        if (sy > wp->saved_grid->sy)
                    809:                screen_resize(s, sx, sy);
                    810:
                    811:        grid_destroy(wp->saved_grid);
                    812:        wp->saved_grid = NULL;
                    813:
                    814:        wp->flags |= PANE_REDRAW;
1.1       nicm      815: }
                    816:
                    817: int
                    818: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                    819: {
                    820:        struct screen   *s;
                    821:
1.15      nicm      822:        if (wp->mode != NULL)
1.1       nicm      823:                return (1);
                    824:        wp->mode = mode;
                    825:
                    826:        if ((s = wp->mode->init(wp)) != NULL)
                    827:                wp->screen = s;
1.34      nicm      828:        wp->flags |= PANE_REDRAW;
1.1       nicm      829:        return (0);
                    830: }
                    831:
                    832: void
                    833: window_pane_reset_mode(struct window_pane *wp)
                    834: {
                    835:        if (wp->mode == NULL)
                    836:                return;
                    837:
                    838:        wp->mode->free(wp);
                    839:        wp->mode = NULL;
                    840:
                    841:        wp->screen = &wp->base;
1.34      nicm      842:        wp->flags |= PANE_REDRAW;
1.1       nicm      843: }
                    844:
                    845: void
1.51      nicm      846: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1       nicm      847: {
1.27      nicm      848:        struct window_pane      *wp2;
                    849:
1.30      nicm      850:        if (!window_pane_visible(wp))
1.3       nicm      851:                return;
                    852:
1.1       nicm      853:        if (wp->mode != NULL) {
                    854:                if (wp->mode->key != NULL)
1.51      nicm      855:                        wp->mode->key(wp, sess, key);
1.27      nicm      856:                return;
1.30      nicm      857:        }
1.27      nicm      858:
1.30      nicm      859:        if (wp->fd == -1)
                    860:                return;
1.27      nicm      861:        input_key(wp, key);
                    862:        if (options_get_number(&wp->window->options, "synchronize-panes")) {
                    863:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    864:                        if (wp2 == wp || wp2->mode != NULL)
                    865:                                continue;
                    866:                        if (wp2->fd != -1 && window_pane_visible(wp2))
                    867:                                input_key(wp2, key);
                    868:                }
                    869:        }
1.1       nicm      870: }
                    871:
                    872: void
                    873: window_pane_mouse(
1.51      nicm      874:     struct window_pane *wp, struct session *sess, struct mouse_event *m)
1.1       nicm      875: {
1.30      nicm      876:        if (!window_pane_visible(wp))
1.3       nicm      877:                return;
                    878:
1.31      nicm      879:        if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1       nicm      880:                return;
1.31      nicm      881:        if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1       nicm      882:                return;
1.31      nicm      883:        m->x -= wp->xoff;
                    884:        m->y -= wp->yoff;
1.1       nicm      885:
                    886:        if (wp->mode != NULL) {
                    887:                if (wp->mode->mouse != NULL)
1.51      nicm      888:                        wp->mode->mouse(wp, sess, m);
1.30      nicm      889:        } else if (wp->fd != -1)
1.31      nicm      890:                input_mouse(wp, m);
1.10      nicm      891: }
                    892:
                    893: int
                    894: window_pane_visible(struct window_pane *wp)
                    895: {
                    896:        struct window   *w = wp->window;
                    897:
                    898:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                    899:                return (0);
                    900:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                    901:                return (0);
                    902:        return (1);
1.1       nicm      903: }
                    904:
                    905: char *
1.5       nicm      906: window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
1.1       nicm      907: {
1.4       nicm      908:        struct screen   *s = &wp->base;
1.5       nicm      909:        char            *newsearchstr, *line, *msg;
1.4       nicm      910:        u_int            i;
                    911:
1.5       nicm      912:        msg = NULL;
                    913:        xasprintf(&newsearchstr, "*%s*", searchstr);
                    914:
1.4       nicm      915:        for (i = 0; i < screen_size_y(s); i++) {
                    916:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm      917:                if (fnmatch(newsearchstr, line, 0) == 0) {
                    918:                        msg = line;
                    919:                        if (lineno != NULL)
                    920:                                *lineno = i;
                    921:                        break;
                    922:                }
1.4       nicm      923:                xfree(line);
                    924:        }
1.5       nicm      925:
                    926:        xfree(newsearchstr);
                    927:        return (msg);
1.45      nicm      928: }
                    929:
                    930: /* Find the pane directly above another. */
                    931: struct window_pane *
                    932: window_pane_find_up(struct window_pane *wp)
                    933: {
                    934:        struct window_pane     *wp2;
                    935:        u_int                   left, top;
                    936:
                    937:        if (wp == NULL || !window_pane_visible(wp))
                    938:                return (NULL);
                    939:
                    940:        top = wp->yoff;
                    941:        if (top == 0)
                    942:                top = wp->window->sy + 1;
                    943:        left = wp->xoff;
                    944:
                    945:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    946:                if (!window_pane_visible(wp2))
                    947:                        continue;
                    948:                if (wp2->yoff + wp2->sy + 1 != top)
                    949:                        continue;
                    950:                if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
                    951:                        return (wp2);
                    952:        }
                    953:        return (NULL);
                    954: }
                    955:
                    956: /* Find the pane directly below another. */
                    957: struct window_pane *
                    958: window_pane_find_down(struct window_pane *wp)
                    959: {
                    960:        struct window_pane     *wp2;
                    961:        u_int                   left, bottom;
                    962:
                    963:        if (wp == NULL || !window_pane_visible(wp))
                    964:                return (NULL);
                    965:
                    966:        bottom = wp->yoff + wp->sy + 1;
                    967:        if (bottom >= wp->window->sy)
                    968:                bottom = 0;
                    969:        left = wp->xoff;
                    970:
                    971:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                    972:                if (!window_pane_visible(wp2))
                    973:                        continue;
                    974:                if (wp2->yoff != bottom)
                    975:                        continue;
                    976:                if (left >= wp2->xoff && left <= wp2->xoff + wp2->sx)
                    977:                        return (wp2);
                    978:        }
                    979:        return (NULL);
                    980: }
                    981:
                    982: /*
                    983:  * Find the pane directly to the left of another, adjacent to the left side and
                    984:  * containing the top edge.
                    985:  */
                    986: struct window_pane *
                    987: window_pane_find_left(struct window_pane *wp)
                    988: {
                    989:        struct window_pane     *wp2;
                    990:        u_int                   left, top;
                    991:
                    992:        if (wp == NULL || !window_pane_visible(wp))
                    993:                return (NULL);
                    994:
                    995:        left = wp->xoff;
                    996:        if (left == 0)
                    997:                left = wp->window->sx + 1;
                    998:        top = wp->yoff;
                    999:
                   1000:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1001:                if (!window_pane_visible(wp2))
                   1002:                        continue;
                   1003:                if (wp2->xoff + wp2->sx + 1 != left)
                   1004:                        continue;
                   1005:                if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
                   1006:                        return (wp2);
                   1007:        }
                   1008:        return (NULL);
                   1009: }
                   1010:
                   1011: /*
                   1012:  * Find the pane directly to the right of another, that is adjacent to the
                   1013:  * right edge and including the top edge.
                   1014:  */
                   1015: struct window_pane *
                   1016: window_pane_find_right(struct window_pane *wp)
                   1017: {
                   1018:        struct window_pane     *wp2;
                   1019:        u_int                   right, top;
                   1020:
                   1021:        if (wp == NULL || !window_pane_visible(wp))
                   1022:                return (NULL);
                   1023:
                   1024:        right = wp->xoff + wp->sx + 1;
                   1025:        if (right >= wp->window->sx)
                   1026:                right = 0;
                   1027:        top = wp->yoff;
                   1028:
                   1029:        TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1030:                if (!window_pane_visible(wp2))
                   1031:                        continue;
                   1032:                if (wp2->xoff != right)
                   1033:                        continue;
                   1034:                if (top >= wp2->yoff && top <= wp2->yoff + wp2->sy)
                   1035:                        return (wp2);
                   1036:        }
                   1037:        return (NULL);
1.1       nicm     1038: }