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

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