Annotation of embedaddon/tmux/window.c, revision 1.1

1.1     ! misho       1: /* $OpenBSD$ */
        !             2: 
        !             3: /*
        !             4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
        !             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>
        !            24: #include <fnmatch.h>
        !            25: #include <signal.h>
        !            26: #include <stdint.h>
        !            27: #include <stdlib.h>
        !            28: #include <string.h>
        !            29: #include <termios.h>
        !            30: #include <time.h>
        !            31: #include <unistd.h>
        !            32: 
        !            33: #include "tmux.h"
        !            34: 
        !            35: /*
        !            36:  * Each window is attached to a number of panes, each of which is a pty. This
        !            37:  * file contains code to handle them.
        !            38:  *
        !            39:  * A pane has two buffers attached, these are filled and emptied by the main
        !            40:  * server poll loop. Output data is received from pty's in screen format,
        !            41:  * translated and returned as a series of escape sequences and strings via
        !            42:  * input_parse (in input.c). Input data is received as key codes and written
        !            43:  * directly via input_key.
        !            44:  *
        !            45:  * Each pane also has a "virtual" screen (screen.c) which contains the current
        !            46:  * state and is redisplayed when the window is reattached to a client.
        !            47:  *
        !            48:  * Windows are stored directly on a global array and wrapped in any number of
        !            49:  * winlink structs to be linked onto local session RB trees. A reference count
        !            50:  * is maintained and a window removed from the global list and destroyed when
        !            51:  * it reaches zero.
        !            52:  */
        !            53: 
        !            54: /* Global window list. */
        !            55: struct windows windows;
        !            56: 
        !            57: /* Global panes tree. */
        !            58: struct window_pane_tree all_window_panes;
        !            59: static u_int   next_window_pane_id;
        !            60: static u_int   next_window_id;
        !            61: static u_int   next_active_point;
        !            62: 
        !            63: static void    window_destroy(struct window *);
        !            64: 
        !            65: static struct window_pane *window_pane_create(struct window *, u_int, u_int,
        !            66:                    u_int);
        !            67: static void    window_pane_destroy(struct window_pane *);
        !            68: 
        !            69: static void    window_pane_read_callback(struct bufferevent *, void *);
        !            70: static void    window_pane_error_callback(struct bufferevent *, short, void *);
        !            71: 
        !            72: static int     winlink_next_index(struct winlinks *, int);
        !            73: 
        !            74: static struct window_pane *window_pane_choose_best(struct window_pane **,
        !            75:                    u_int);
        !            76: 
        !            77: RB_GENERATE(windows, window, entry, window_cmp);
        !            78: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
        !            79: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
        !            80: 
        !            81: int
        !            82: window_cmp(struct window *w1, struct window *w2)
        !            83: {
        !            84:        return (w1->id - w2->id);
        !            85: }
        !            86: 
        !            87: int
        !            88: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
        !            89: {
        !            90:        return (wl1->idx - wl2->idx);
        !            91: }
        !            92: 
        !            93: int
        !            94: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
        !            95: {
        !            96:        return (wp1->id - wp2->id);
        !            97: }
        !            98: 
        !            99: struct winlink *
        !           100: winlink_find_by_window(struct winlinks *wwl, struct window *w)
        !           101: {
        !           102:        struct winlink  *wl;
        !           103: 
        !           104:        RB_FOREACH(wl, winlinks, wwl) {
        !           105:                if (wl->window == w)
        !           106:                        return (wl);
        !           107:        }
        !           108: 
        !           109:        return (NULL);
        !           110: }
        !           111: 
        !           112: struct winlink *
        !           113: winlink_find_by_index(struct winlinks *wwl, int idx)
        !           114: {
        !           115:        struct winlink  wl;
        !           116: 
        !           117:        if (idx < 0)
        !           118:                fatalx("bad index");
        !           119: 
        !           120:        wl.idx = idx;
        !           121:        return (RB_FIND(winlinks, wwl, &wl));
        !           122: }
        !           123: 
        !           124: struct winlink *
        !           125: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
        !           126: {
        !           127:        struct winlink *wl;
        !           128: 
        !           129:        RB_FOREACH(wl, winlinks, wwl) {
        !           130:                if (wl->window->id == id)
        !           131:                        return (wl);
        !           132:        }
        !           133:        return (NULL);
        !           134: }
        !           135: 
        !           136: static int
        !           137: winlink_next_index(struct winlinks *wwl, int idx)
        !           138: {
        !           139:        int     i;
        !           140: 
        !           141:        i = idx;
        !           142:        do {
        !           143:                if (winlink_find_by_index(wwl, i) == NULL)
        !           144:                        return (i);
        !           145:                if (i == INT_MAX)
        !           146:                        i = 0;
        !           147:                else
        !           148:                        i++;
        !           149:        } while (i != idx);
        !           150:        return (-1);
        !           151: }
        !           152: 
        !           153: u_int
        !           154: winlink_count(struct winlinks *wwl)
        !           155: {
        !           156:        struct winlink  *wl;
        !           157:        u_int            n;
        !           158: 
        !           159:        n = 0;
        !           160:        RB_FOREACH(wl, winlinks, wwl)
        !           161:                n++;
        !           162: 
        !           163:        return (n);
        !           164: }
        !           165: 
        !           166: struct winlink *
        !           167: winlink_add(struct winlinks *wwl, int idx)
        !           168: {
        !           169:        struct winlink  *wl;
        !           170: 
        !           171:        if (idx < 0) {
        !           172:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
        !           173:                        return (NULL);
        !           174:        } else if (winlink_find_by_index(wwl, idx) != NULL)
        !           175:                return (NULL);
        !           176: 
        !           177:        wl = xcalloc(1, sizeof *wl);
        !           178:        wl->idx = idx;
        !           179:        RB_INSERT(winlinks, wwl, wl);
        !           180: 
        !           181:        return (wl);
        !           182: }
        !           183: 
        !           184: void
        !           185: winlink_set_window(struct winlink *wl, struct window *w)
        !           186: {
        !           187:        if (wl->window != NULL) {
        !           188:                TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
        !           189:                window_remove_ref(w);
        !           190:        }
        !           191:        TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
        !           192:        wl->window = w;
        !           193:        w->references++;
        !           194: }
        !           195: 
        !           196: void
        !           197: winlink_remove(struct winlinks *wwl, struct winlink *wl)
        !           198: {
        !           199:        struct window   *w = wl->window;
        !           200: 
        !           201:        if (w != NULL) {
        !           202:                TAILQ_REMOVE(&w->winlinks, wl, wentry);
        !           203:                window_remove_ref(w);
        !           204:        }
        !           205: 
        !           206:        RB_REMOVE(winlinks, wwl, wl);
        !           207:        free(wl->status_text);
        !           208:        free(wl);
        !           209: }
        !           210: 
        !           211: struct winlink *
        !           212: winlink_next(struct winlink *wl)
        !           213: {
        !           214:        return (RB_NEXT(winlinks, wwl, wl));
        !           215: }
        !           216: 
        !           217: struct winlink *
        !           218: winlink_previous(struct winlink *wl)
        !           219: {
        !           220:        return (RB_PREV(winlinks, wwl, wl));
        !           221: }
        !           222: 
        !           223: struct winlink *
        !           224: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
        !           225: {
        !           226:        for (; n > 0; n--) {
        !           227:                if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
        !           228:                        wl = RB_MIN(winlinks, &s->windows);
        !           229:        }
        !           230: 
        !           231:        return (wl);
        !           232: }
        !           233: 
        !           234: struct winlink *
        !           235: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
        !           236: {
        !           237:        for (; n > 0; n--) {
        !           238:                if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
        !           239:                        wl = RB_MAX(winlinks, &s->windows);
        !           240:        }
        !           241: 
        !           242:        return (wl);
        !           243: }
        !           244: 
        !           245: void
        !           246: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
        !           247: {
        !           248:        if (wl == NULL)
        !           249:                return;
        !           250: 
        !           251:        winlink_stack_remove(stack, wl);
        !           252:        TAILQ_INSERT_HEAD(stack, wl, sentry);
        !           253: }
        !           254: 
        !           255: void
        !           256: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
        !           257: {
        !           258:        struct winlink  *wl2;
        !           259: 
        !           260:        if (wl == NULL)
        !           261:                return;
        !           262: 
        !           263:        TAILQ_FOREACH(wl2, stack, sentry) {
        !           264:                if (wl2 == wl) {
        !           265:                        TAILQ_REMOVE(stack, wl, sentry);
        !           266:                        return;
        !           267:                }
        !           268:        }
        !           269: }
        !           270: 
        !           271: struct window *
        !           272: window_find_by_id_str(const char *s)
        !           273: {
        !           274:        const char      *errstr;
        !           275:        u_int            id;
        !           276: 
        !           277:        if (*s != '@')
        !           278:                return (NULL);
        !           279: 
        !           280:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
        !           281:        if (errstr != NULL)
        !           282:                return (NULL);
        !           283:        return (window_find_by_id(id));
        !           284: }
        !           285: 
        !           286: struct window *
        !           287: window_find_by_id(u_int id)
        !           288: {
        !           289:        struct window   w;
        !           290: 
        !           291:        w.id = id;
        !           292:        return (RB_FIND(windows, &windows, &w));
        !           293: }
        !           294: 
        !           295: void
        !           296: window_update_activity(struct window *w)
        !           297: {
        !           298:        gettimeofday(&w->activity_time, NULL);
        !           299:        alerts_queue(w, WINDOW_ACTIVITY);
        !           300: }
        !           301: 
        !           302: struct window *
        !           303: window_create(u_int sx, u_int sy)
        !           304: {
        !           305:        struct window   *w;
        !           306: 
        !           307:        w = xcalloc(1, sizeof *w);
        !           308:        w->name = NULL;
        !           309:        w->flags = WINDOW_STYLECHANGED;
        !           310: 
        !           311:        TAILQ_INIT(&w->panes);
        !           312:        w->active = NULL;
        !           313: 
        !           314:        w->lastlayout = -1;
        !           315:        w->layout_root = NULL;
        !           316: 
        !           317:        w->sx = sx;
        !           318:        w->sy = sy;
        !           319: 
        !           320:        w->options = options_create(global_w_options);
        !           321: 
        !           322:        w->references = 0;
        !           323:        TAILQ_INIT(&w->winlinks);
        !           324: 
        !           325:        w->id = next_window_id++;
        !           326:        RB_INSERT(windows, &windows, w);
        !           327: 
        !           328:        window_update_activity(w);
        !           329: 
        !           330:        return (w);
        !           331: }
        !           332: 
        !           333: struct window *
        !           334: window_create_spawn(const char *name, int argc, char **argv, const char *path,
        !           335:     const char *shell, const char *cwd, struct environ *env,
        !           336:     struct termios *tio, u_int sx, u_int sy, u_int hlimit, char **cause)
        !           337: {
        !           338:        struct window           *w;
        !           339:        struct window_pane      *wp;
        !           340: 
        !           341:        w = window_create(sx, sy);
        !           342:        wp = window_add_pane(w, NULL, 0, hlimit);
        !           343:        layout_init(w, wp);
        !           344: 
        !           345:        if (window_pane_spawn(wp, argc, argv, path, shell, cwd,
        !           346:            env, tio, cause) != 0) {
        !           347:                window_destroy(w);
        !           348:                return (NULL);
        !           349:        }
        !           350: 
        !           351:        w->active = TAILQ_FIRST(&w->panes);
        !           352:        if (name != NULL) {
        !           353:                w->name = xstrdup(name);
        !           354:                options_set_number(w->options, "automatic-rename", 0);
        !           355:        } else
        !           356:                w->name = default_window_name(w);
        !           357: 
        !           358:        return (w);
        !           359: }
        !           360: 
        !           361: static void
        !           362: window_destroy(struct window *w)
        !           363: {
        !           364:        if (!TAILQ_EMPTY(&w->winlinks))
        !           365:                fatalx("window destroyed with winlinks");
        !           366: 
        !           367:        RB_REMOVE(windows, &windows, w);
        !           368: 
        !           369:        if (w->layout_root != NULL)
        !           370:                layout_free_cell(w->layout_root);
        !           371:        if (w->saved_layout_root != NULL)
        !           372:                layout_free_cell(w->saved_layout_root);
        !           373:        free(w->old_layout);
        !           374: 
        !           375:        if (event_initialized(&w->name_event))
        !           376:                evtimer_del(&w->name_event);
        !           377: 
        !           378:        if (event_initialized(&w->alerts_timer))
        !           379:                evtimer_del(&w->alerts_timer);
        !           380: 
        !           381:        options_free(w->options);
        !           382: 
        !           383:        window_destroy_panes(w);
        !           384: 
        !           385:        free(w->name);
        !           386:        free(w);
        !           387: }
        !           388: 
        !           389: void
        !           390: window_remove_ref(struct window *w)
        !           391: {
        !           392:        if (w->references == 0)
        !           393:                fatal("bad reference count");
        !           394:        w->references--;
        !           395:        if (w->references == 0)
        !           396:                window_destroy(w);
        !           397: }
        !           398: 
        !           399: void
        !           400: window_set_name(struct window *w, const char *new_name)
        !           401: {
        !           402:        free(w->name);
        !           403:        w->name = xstrdup(new_name);
        !           404:        notify_window("window-renamed", w);
        !           405: }
        !           406: 
        !           407: void
        !           408: window_resize(struct window *w, u_int sx, u_int sy)
        !           409: {
        !           410:        w->sx = sx;
        !           411:        w->sy = sy;
        !           412: }
        !           413: 
        !           414: int
        !           415: window_has_pane(struct window *w, struct window_pane *wp)
        !           416: {
        !           417:        struct window_pane      *wp1;
        !           418: 
        !           419:        TAILQ_FOREACH(wp1, &w->panes, entry) {
        !           420:                if (wp1 == wp)
        !           421:                        return (1);
        !           422:        }
        !           423:        return (0);
        !           424: }
        !           425: 
        !           426: int
        !           427: window_set_active_pane(struct window *w, struct window_pane *wp)
        !           428: {
        !           429:        log_debug("%s: pane %%%u (was %%%u)", __func__, wp->id, w->active->id);
        !           430:        if (wp == w->active)
        !           431:                return (0);
        !           432:        w->last = w->active;
        !           433:        w->active = wp;
        !           434:        while (!window_pane_visible(w->active)) {
        !           435:                w->active = TAILQ_PREV(w->active, window_panes, entry);
        !           436:                if (w->active == NULL)
        !           437:                        w->active = TAILQ_LAST(&w->panes, window_panes);
        !           438:                if (w->active == wp)
        !           439:                        return (1);
        !           440:        }
        !           441:        w->active->active_point = next_active_point++;
        !           442:        w->active->flags |= PANE_CHANGED;
        !           443:        return (1);
        !           444: }
        !           445: 
        !           446: void
        !           447: window_redraw_active_switch(struct window *w, struct window_pane *wp)
        !           448: {
        !           449:        const struct grid_cell  *gc;
        !           450: 
        !           451:        if (wp == w->active)
        !           452:                return;
        !           453: 
        !           454:        /*
        !           455:         * If window-style and window-active-style are the same, we don't need
        !           456:         * to redraw panes when switching active panes.
        !           457:         */
        !           458:        gc = options_get_style(w->options, "window-active-style");
        !           459:        if (style_equal(gc, options_get_style(w->options, "window-style")))
        !           460:                return;
        !           461: 
        !           462:        /*
        !           463:         * If the now active or inactive pane do not have a custom style or if
        !           464:         * the palette is different, they need to be redrawn.
        !           465:         */
        !           466:        if (window_pane_get_palette(w->active, w->active->colgc.fg) != -1 ||
        !           467:            window_pane_get_palette(w->active, w->active->colgc.bg) != -1 ||
        !           468:            style_equal(&grid_default_cell, &w->active->colgc))
        !           469:                w->active->flags |= PANE_REDRAW;
        !           470:        if (window_pane_get_palette(wp, wp->colgc.fg) != -1 ||
        !           471:            window_pane_get_palette(wp, wp->colgc.bg) != -1 ||
        !           472:            style_equal(&grid_default_cell, &wp->colgc))
        !           473:                wp->flags |= PANE_REDRAW;
        !           474: }
        !           475: 
        !           476: struct window_pane *
        !           477: window_get_active_at(struct window *w, u_int x, u_int y)
        !           478: {
        !           479:        struct window_pane      *wp;
        !           480: 
        !           481:        TAILQ_FOREACH(wp, &w->panes, entry) {
        !           482:                if (!window_pane_visible(wp))
        !           483:                        continue;
        !           484:                if (x < wp->xoff || x > wp->xoff + wp->sx)
        !           485:                        continue;
        !           486:                if (y < wp->yoff || y > wp->yoff + wp->sy)
        !           487:                        continue;
        !           488:                return (wp);
        !           489:        }
        !           490:        return (NULL);
        !           491: }
        !           492: 
        !           493: struct window_pane *
        !           494: window_find_string(struct window *w, const char *s)
        !           495: {
        !           496:        u_int   x, y;
        !           497: 
        !           498:        x = w->sx / 2;
        !           499:        y = w->sy / 2;
        !           500: 
        !           501:        if (strcasecmp(s, "top") == 0)
        !           502:                y = 0;
        !           503:        else if (strcasecmp(s, "bottom") == 0)
        !           504:                y = w->sy - 1;
        !           505:        else if (strcasecmp(s, "left") == 0)
        !           506:                x = 0;
        !           507:        else if (strcasecmp(s, "right") == 0)
        !           508:                x = w->sx - 1;
        !           509:        else if (strcasecmp(s, "top-left") == 0) {
        !           510:                x = 0;
        !           511:                y = 0;
        !           512:        } else if (strcasecmp(s, "top-right") == 0) {
        !           513:                x = w->sx - 1;
        !           514:                y = 0;
        !           515:        } else if (strcasecmp(s, "bottom-left") == 0) {
        !           516:                x = 0;
        !           517:                y = w->sy - 1;
        !           518:        } else if (strcasecmp(s, "bottom-right") == 0) {
        !           519:                x = w->sx - 1;
        !           520:                y = w->sy - 1;
        !           521:        } else
        !           522:                return (NULL);
        !           523: 
        !           524:        return (window_get_active_at(w, x, y));
        !           525: }
        !           526: 
        !           527: int
        !           528: window_zoom(struct window_pane *wp)
        !           529: {
        !           530:        struct window           *w = wp->window;
        !           531:        struct window_pane      *wp1;
        !           532: 
        !           533:        if (w->flags & WINDOW_ZOOMED)
        !           534:                return (-1);
        !           535: 
        !           536:        if (!window_pane_visible(wp))
        !           537:                return (-1);
        !           538: 
        !           539:        if (window_count_panes(w) == 1)
        !           540:                return (-1);
        !           541: 
        !           542:        if (w->active != wp)
        !           543:                window_set_active_pane(w, wp);
        !           544: 
        !           545:        TAILQ_FOREACH(wp1, &w->panes, entry) {
        !           546:                wp1->saved_layout_cell = wp1->layout_cell;
        !           547:                wp1->layout_cell = NULL;
        !           548:        }
        !           549: 
        !           550:        w->saved_layout_root = w->layout_root;
        !           551:        layout_init(w, wp);
        !           552:        w->flags |= WINDOW_ZOOMED;
        !           553:        notify_window("window-layout-changed", w);
        !           554: 
        !           555:        return (0);
        !           556: }
        !           557: 
        !           558: int
        !           559: window_unzoom(struct window *w)
        !           560: {
        !           561:        struct window_pane      *wp;
        !           562: 
        !           563:        if (!(w->flags & WINDOW_ZOOMED))
        !           564:                return (-1);
        !           565: 
        !           566:        w->flags &= ~WINDOW_ZOOMED;
        !           567:        layout_free(w);
        !           568:        w->layout_root = w->saved_layout_root;
        !           569:        w->saved_layout_root = NULL;
        !           570: 
        !           571:        TAILQ_FOREACH(wp, &w->panes, entry) {
        !           572:                wp->layout_cell = wp->saved_layout_cell;
        !           573:                wp->saved_layout_cell = NULL;
        !           574:        }
        !           575:        layout_fix_panes(w, w->sx, w->sy);
        !           576:        notify_window("window-layout-changed", w);
        !           577: 
        !           578:        return (0);
        !           579: }
        !           580: 
        !           581: struct window_pane *
        !           582: window_add_pane(struct window *w, struct window_pane *other, int before,
        !           583:     u_int hlimit)
        !           584: {
        !           585:        struct window_pane      *wp;
        !           586: 
        !           587:        if (other == NULL)
        !           588:                other = w->active;
        !           589: 
        !           590:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
        !           591:        if (TAILQ_EMPTY(&w->panes)) {
        !           592:                log_debug("%s: @%u at start", __func__, w->id);
        !           593:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
        !           594:        } else if (before) {
        !           595:                log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
        !           596:                TAILQ_INSERT_BEFORE(other, wp, entry);
        !           597:        } else {
        !           598:                log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
        !           599:                TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
        !           600:        }
        !           601:        return (wp);
        !           602: }
        !           603: 
        !           604: void
        !           605: window_lost_pane(struct window *w, struct window_pane *wp)
        !           606: {
        !           607:        if (wp == marked_pane.wp)
        !           608:                server_clear_marked();
        !           609: 
        !           610:        if (wp == w->active) {
        !           611:                w->active = w->last;
        !           612:                w->last = NULL;
        !           613:                if (w->active == NULL) {
        !           614:                        w->active = TAILQ_PREV(wp, window_panes, entry);
        !           615:                        if (w->active == NULL)
        !           616:                                w->active = TAILQ_NEXT(wp, entry);
        !           617:                }
        !           618:                if (w->active != NULL)
        !           619:                        w->active->flags |= PANE_CHANGED;
        !           620:        } else if (wp == w->last)
        !           621:                w->last = NULL;
        !           622: }
        !           623: 
        !           624: void
        !           625: window_remove_pane(struct window *w, struct window_pane *wp)
        !           626: {
        !           627:        window_lost_pane(w, wp);
        !           628: 
        !           629:        TAILQ_REMOVE(&w->panes, wp, entry);
        !           630:        window_pane_destroy(wp);
        !           631: }
        !           632: 
        !           633: struct window_pane *
        !           634: window_pane_at_index(struct window *w, u_int idx)
        !           635: {
        !           636:        struct window_pane      *wp;
        !           637:        u_int                    n;
        !           638: 
        !           639:        n = options_get_number(w->options, "pane-base-index");
        !           640:        TAILQ_FOREACH(wp, &w->panes, entry) {
        !           641:                if (n == idx)
        !           642:                        return (wp);
        !           643:                n++;
        !           644:        }
        !           645:        return (NULL);
        !           646: }
        !           647: 
        !           648: struct window_pane *
        !           649: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
        !           650: {
        !           651:        for (; n > 0; n--) {
        !           652:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
        !           653:                        wp = TAILQ_FIRST(&w->panes);
        !           654:        }
        !           655: 
        !           656:        return (wp);
        !           657: }
        !           658: 
        !           659: struct window_pane *
        !           660: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
        !           661:     u_int n)
        !           662: {
        !           663:        for (; n > 0; n--) {
        !           664:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
        !           665:                        wp = TAILQ_LAST(&w->panes, window_panes);
        !           666:        }
        !           667: 
        !           668:        return (wp);
        !           669: }
        !           670: 
        !           671: int
        !           672: window_pane_index(struct window_pane *wp, u_int *i)
        !           673: {
        !           674:        struct window_pane      *wq;
        !           675:        struct window           *w = wp->window;
        !           676: 
        !           677:        *i = options_get_number(w->options, "pane-base-index");
        !           678:        TAILQ_FOREACH(wq, &w->panes, entry) {
        !           679:                if (wp == wq) {
        !           680:                        return (0);
        !           681:                }
        !           682:                (*i)++;
        !           683:        }
        !           684: 
        !           685:        return (-1);
        !           686: }
        !           687: 
        !           688: u_int
        !           689: window_count_panes(struct window *w)
        !           690: {
        !           691:        struct window_pane      *wp;
        !           692:        u_int                    n;
        !           693: 
        !           694:        n = 0;
        !           695:        TAILQ_FOREACH(wp, &w->panes, entry)
        !           696:                n++;
        !           697:        return (n);
        !           698: }
        !           699: 
        !           700: void
        !           701: window_destroy_panes(struct window *w)
        !           702: {
        !           703:        struct window_pane      *wp;
        !           704: 
        !           705:        while (!TAILQ_EMPTY(&w->panes)) {
        !           706:                wp = TAILQ_FIRST(&w->panes);
        !           707:                TAILQ_REMOVE(&w->panes, wp, entry);
        !           708:                window_pane_destroy(wp);
        !           709:        }
        !           710: }
        !           711: 
        !           712: /* Retuns the printable flags on a window, empty string if no flags set. */
        !           713: char *
        !           714: window_printable_flags(struct session *s, struct winlink *wl)
        !           715: {
        !           716:        char    flags[32];
        !           717:        int     pos;
        !           718: 
        !           719:        pos = 0;
        !           720:        if (wl->flags & WINLINK_ACTIVITY)
        !           721:                flags[pos++] = '#';
        !           722:        if (wl->flags & WINLINK_BELL)
        !           723:                flags[pos++] = '!';
        !           724:        if (wl->flags & WINLINK_SILENCE)
        !           725:                flags[pos++] = '~';
        !           726:        if (wl == s->curw)
        !           727:                flags[pos++] = '*';
        !           728:        if (wl == TAILQ_FIRST(&s->lastw))
        !           729:                flags[pos++] = '-';
        !           730:        if (server_check_marked() && wl == marked_pane.wl)
        !           731:                flags[pos++] = 'M';
        !           732:        if (wl->window->flags & WINDOW_ZOOMED)
        !           733:                flags[pos++] = 'Z';
        !           734:        flags[pos] = '\0';
        !           735:        return (xstrdup(flags));
        !           736: }
        !           737: 
        !           738: struct window_pane *
        !           739: window_pane_find_by_id_str(const char *s)
        !           740: {
        !           741:        const char      *errstr;
        !           742:        u_int            id;
        !           743: 
        !           744:        if (*s != '%')
        !           745:                return (NULL);
        !           746: 
        !           747:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
        !           748:        if (errstr != NULL)
        !           749:                return (NULL);
        !           750:        return (window_pane_find_by_id(id));
        !           751: }
        !           752: 
        !           753: struct window_pane *
        !           754: window_pane_find_by_id(u_int id)
        !           755: {
        !           756:        struct window_pane      wp;
        !           757: 
        !           758:        wp.id = id;
        !           759:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
        !           760: }
        !           761: 
        !           762: static struct window_pane *
        !           763: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
        !           764: {
        !           765:        struct window_pane      *wp;
        !           766:        char                     host[HOST_NAME_MAX + 1];
        !           767: 
        !           768:        wp = xcalloc(1, sizeof *wp);
        !           769:        wp->window = w;
        !           770: 
        !           771:        wp->id = next_window_pane_id++;
        !           772:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
        !           773: 
        !           774:        wp->argc = 0;
        !           775:        wp->argv = NULL;
        !           776:        wp->shell = NULL;
        !           777:        wp->cwd = NULL;
        !           778: 
        !           779:        wp->fd = -1;
        !           780:        wp->event = NULL;
        !           781: 
        !           782:        wp->mode = NULL;
        !           783:        wp->modeprefix = 1;
        !           784: 
        !           785:        wp->layout_cell = NULL;
        !           786: 
        !           787:        wp->xoff = 0;
        !           788:        wp->yoff = 0;
        !           789: 
        !           790:        wp->sx = sx;
        !           791:        wp->sy = sy;
        !           792: 
        !           793:        wp->pipe_fd = -1;
        !           794:        wp->pipe_off = 0;
        !           795:        wp->pipe_event = NULL;
        !           796: 
        !           797:        wp->saved_grid = NULL;
        !           798: 
        !           799:        memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
        !           800: 
        !           801:        screen_init(&wp->base, sx, sy, hlimit);
        !           802:        wp->screen = &wp->base;
        !           803: 
        !           804:        screen_init(&wp->status_screen, 1, 1, 0);
        !           805: 
        !           806:        if (gethostname(host, sizeof host) == 0)
        !           807:                screen_set_title(&wp->base, host);
        !           808: 
        !           809:        input_init(wp);
        !           810: 
        !           811:        return (wp);
        !           812: }
        !           813: 
        !           814: static void
        !           815: window_pane_destroy(struct window_pane *wp)
        !           816: {
        !           817:        window_pane_reset_mode(wp);
        !           818: 
        !           819:        if (wp->fd != -1) {
        !           820: #ifdef HAVE_UTEMPTER
        !           821:                utempter_remove_record(wp->fd);
        !           822: #endif
        !           823:                bufferevent_free(wp->event);
        !           824:                close(wp->fd);
        !           825:        }
        !           826: 
        !           827:        input_free(wp);
        !           828: 
        !           829:        screen_free(&wp->base);
        !           830:        if (wp->saved_grid != NULL)
        !           831:                grid_destroy(wp->saved_grid);
        !           832: 
        !           833:        if (wp->pipe_fd != -1) {
        !           834:                bufferevent_free(wp->pipe_event);
        !           835:                close(wp->pipe_fd);
        !           836:        }
        !           837: 
        !           838:        if (event_initialized(&wp->resize_timer))
        !           839:                event_del(&wp->resize_timer);
        !           840: 
        !           841:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
        !           842: 
        !           843:        free((void *)wp->cwd);
        !           844:        free(wp->shell);
        !           845:        cmd_free_argv(wp->argc, wp->argv);
        !           846:        free(wp->palette);
        !           847:        free(wp);
        !           848: }
        !           849: 
        !           850: int
        !           851: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
        !           852:     const char *path, const char *shell, const char *cwd, struct environ *env,
        !           853:     struct termios *tio, char **cause)
        !           854: {
        !           855:        struct winsize   ws;
        !           856:        char            *argv0, *cmd, **argvp;
        !           857:        const char      *ptr, *first, *home;
        !           858:        struct termios   tio2;
        !           859: #ifdef HAVE_UTEMPTER
        !           860:        char             s[32];
        !           861: #endif
        !           862:        int              i;
        !           863: 
        !           864:        if (wp->fd != -1) {
        !           865:                bufferevent_free(wp->event);
        !           866:                close(wp->fd);
        !           867:        }
        !           868:        if (argc > 0) {
        !           869:                cmd_free_argv(wp->argc, wp->argv);
        !           870:                wp->argc = argc;
        !           871:                wp->argv = cmd_copy_argv(argc, argv);
        !           872:        }
        !           873:        if (shell != NULL) {
        !           874:                free(wp->shell);
        !           875:                wp->shell = xstrdup(shell);
        !           876:        }
        !           877:        if (cwd != NULL) {
        !           878:                free((void *)wp->cwd);
        !           879:                wp->cwd = xstrdup(cwd);
        !           880:        }
        !           881: 
        !           882:        cmd = cmd_stringify_argv(wp->argc, wp->argv);
        !           883:        log_debug("spawn: %s -- %s", wp->shell, cmd);
        !           884:        for (i = 0; i < wp->argc; i++)
        !           885:                log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
        !           886:        environ_log(env, "spawn: ");
        !           887: 
        !           888:        memset(&ws, 0, sizeof ws);
        !           889:        ws.ws_col = screen_size_x(&wp->base);
        !           890:        ws.ws_row = screen_size_y(&wp->base);
        !           891: 
        !           892:        wp->pid = pty_fork(ptm_fd, &wp->fd, wp->tty, sizeof wp->tty, &ws);
        !           893:        switch (wp->pid) {
        !           894:        case -1:
        !           895:                wp->fd = -1;
        !           896:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
        !           897:                free(cmd);
        !           898:                return (-1);
        !           899:        case 0:
        !           900:                if (chdir(wp->cwd) != 0) {
        !           901:                        if ((home = find_home()) == NULL || chdir(home) != 0)
        !           902:                                chdir("/");
        !           903:                }
        !           904: 
        !           905:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
        !           906:                        fatal("tcgetattr failed");
        !           907:                if (tio != NULL)
        !           908:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
        !           909:                tio2.c_cc[VERASE] = '\177';
        !           910: #ifdef IUTF8
        !           911:                tio2.c_iflag |= IUTF8;
        !           912: #endif
        !           913:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
        !           914:                        fatal("tcgetattr failed");
        !           915: 
        !           916:                closefrom(STDERR_FILENO + 1);
        !           917: 
        !           918:                if (path != NULL)
        !           919:                        environ_set(env, "PATH", "%s", path);
        !           920:                environ_set(env, "TMUX_PANE", "%%%u", wp->id);
        !           921:                environ_push(env);
        !           922: 
        !           923:                clear_signals(1);
        !           924:                log_close();
        !           925: 
        !           926:                setenv("SHELL", wp->shell, 1);
        !           927:                ptr = strrchr(wp->shell, '/');
        !           928: 
        !           929:                /*
        !           930:                 * If given one argument, assume it should be passed to sh -c;
        !           931:                 * with more than one argument, use execvp(). If there is no
        !           932:                 * arguments, create a login shell.
        !           933:                 */
        !           934:                if (wp->argc > 0) {
        !           935:                        if (wp->argc != 1) {
        !           936:                                /* Copy to ensure argv ends in NULL. */
        !           937:                                argvp = cmd_copy_argv(wp->argc, wp->argv);
        !           938:                                execvp(argvp[0], argvp);
        !           939:                                fatal("execvp failed");
        !           940:                        }
        !           941:                        first = wp->argv[0];
        !           942: 
        !           943:                        if (ptr != NULL && *(ptr + 1) != '\0')
        !           944:                                xasprintf(&argv0, "%s", ptr + 1);
        !           945:                        else
        !           946:                                xasprintf(&argv0, "%s", wp->shell);
        !           947:                        execl(wp->shell, argv0, "-c", first, (char *)NULL);
        !           948:                        fatal("execl failed");
        !           949:                }
        !           950:                if (ptr != NULL && *(ptr + 1) != '\0')
        !           951:                        xasprintf(&argv0, "-%s", ptr + 1);
        !           952:                else
        !           953:                        xasprintf(&argv0, "-%s", wp->shell);
        !           954:                execl(wp->shell, argv0, (char *)NULL);
        !           955:                fatal("execl failed");
        !           956:        }
        !           957: 
        !           958: #ifdef HAVE_UTEMPTER
        !           959:        xsnprintf(s, sizeof s, "tmux(%lu).%%%u", (long) getpid(), wp->id);
        !           960:        utempter_add_record(wp->fd, s);
        !           961:        kill(getpid(), SIGCHLD);
        !           962: #endif
        !           963: 
        !           964:        setblocking(wp->fd, 0);
        !           965: 
        !           966:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
        !           967:            window_pane_error_callback, wp);
        !           968: 
        !           969:        bufferevent_setwatermark(wp->event, EV_READ, 0, READ_SIZE);
        !           970:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
        !           971: 
        !           972:        free(cmd);
        !           973:        return (0);
        !           974: }
        !           975: 
        !           976: static void
        !           977: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
        !           978: {
        !           979:        struct window_pane      *wp = data;
        !           980:        struct evbuffer         *evb = wp->event->input;
        !           981:        size_t                   size = EVBUFFER_LENGTH(evb);
        !           982:        char                    *new_data;
        !           983:        size_t                   new_size;
        !           984: 
        !           985:        new_size = size - wp->pipe_off;
        !           986:        if (wp->pipe_fd != -1 && new_size > 0) {
        !           987:                new_data = EVBUFFER_DATA(evb) + wp->pipe_off;
        !           988:                bufferevent_write(wp->pipe_event, new_data, new_size);
        !           989:        }
        !           990: 
        !           991:        log_debug("%%%u has %zu bytes", wp->id, size);
        !           992:        input_parse(wp);
        !           993: 
        !           994:        wp->pipe_off = EVBUFFER_LENGTH(evb);
        !           995: }
        !           996: 
        !           997: static void
        !           998: window_pane_error_callback(__unused struct bufferevent *bufev,
        !           999:     __unused short what, void *data)
        !          1000: {
        !          1001:        struct window_pane *wp = data;
        !          1002: 
        !          1003:        server_destroy_pane(wp, 1);
        !          1004: }
        !          1005: 
        !          1006: void
        !          1007: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
        !          1008: {
        !          1009:        if (sx == wp->sx && sy == wp->sy)
        !          1010:                return;
        !          1011:        wp->sx = sx;
        !          1012:        wp->sy = sy;
        !          1013: 
        !          1014:        screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
        !          1015:        if (wp->mode != NULL)
        !          1016:                wp->mode->resize(wp, sx, sy);
        !          1017: 
        !          1018:        wp->flags |= PANE_RESIZE;
        !          1019: }
        !          1020: 
        !          1021: /*
        !          1022:  * Enter alternative screen mode. A copy of the visible screen is saved and the
        !          1023:  * history is not updated
        !          1024:  */
        !          1025: void
        !          1026: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
        !          1027:     int cursor)
        !          1028: {
        !          1029:        struct screen   *s = &wp->base;
        !          1030:        u_int            sx, sy;
        !          1031: 
        !          1032:        if (wp->saved_grid != NULL)
        !          1033:                return;
        !          1034:        if (!options_get_number(wp->window->options, "alternate-screen"))
        !          1035:                return;
        !          1036:        sx = screen_size_x(s);
        !          1037:        sy = screen_size_y(s);
        !          1038: 
        !          1039:        wp->saved_grid = grid_create(sx, sy, 0);
        !          1040:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
        !          1041:        if (cursor) {
        !          1042:                wp->saved_cx = s->cx;
        !          1043:                wp->saved_cy = s->cy;
        !          1044:        }
        !          1045:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
        !          1046: 
        !          1047:        grid_view_clear(s->grid, 0, 0, sx, sy, 8);
        !          1048: 
        !          1049:        wp->base.grid->flags &= ~GRID_HISTORY;
        !          1050: 
        !          1051:        wp->flags |= PANE_REDRAW;
        !          1052: }
        !          1053: 
        !          1054: /* Exit alternate screen mode and restore the copied grid. */
        !          1055: void
        !          1056: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
        !          1057:     int cursor)
        !          1058: {
        !          1059:        struct screen   *s = &wp->base;
        !          1060:        u_int            sx, sy;
        !          1061: 
        !          1062:        if (wp->saved_grid == NULL)
        !          1063:                return;
        !          1064:        if (!options_get_number(wp->window->options, "alternate-screen"))
        !          1065:                return;
        !          1066:        sx = screen_size_x(s);
        !          1067:        sy = screen_size_y(s);
        !          1068: 
        !          1069:        /*
        !          1070:         * If the current size is bigger, temporarily resize to the old size
        !          1071:         * before copying back.
        !          1072:         */
        !          1073:        if (sy > wp->saved_grid->sy)
        !          1074:                screen_resize(s, sx, wp->saved_grid->sy, 1);
        !          1075: 
        !          1076:        /* Restore the grid, cursor position and cell. */
        !          1077:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
        !          1078:        if (cursor)
        !          1079:                s->cx = wp->saved_cx;
        !          1080:        if (s->cx > screen_size_x(s) - 1)
        !          1081:                s->cx = screen_size_x(s) - 1;
        !          1082:        if (cursor)
        !          1083:                s->cy = wp->saved_cy;
        !          1084:        if (s->cy > screen_size_y(s) - 1)
        !          1085:                s->cy = screen_size_y(s) - 1;
        !          1086:        memcpy(gc, &wp->saved_cell, sizeof *gc);
        !          1087: 
        !          1088:        /*
        !          1089:         * Turn history back on (so resize can use it) and then resize back to
        !          1090:         * the current size.
        !          1091:         */
        !          1092:        wp->base.grid->flags |= GRID_HISTORY;
        !          1093:        if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
        !          1094:                screen_resize(s, sx, sy, 1);
        !          1095: 
        !          1096:        grid_destroy(wp->saved_grid);
        !          1097:        wp->saved_grid = NULL;
        !          1098: 
        !          1099:        wp->flags |= PANE_REDRAW;
        !          1100: }
        !          1101: 
        !          1102: void
        !          1103: window_pane_set_palette(struct window_pane *wp, u_int n, int colour)
        !          1104: {
        !          1105:        if (n > 0xff)
        !          1106:                return;
        !          1107: 
        !          1108:        if (wp->palette == NULL)
        !          1109:                wp->palette = xcalloc(0x100, sizeof *wp->palette);
        !          1110: 
        !          1111:        wp->palette[n] = colour;
        !          1112:        wp->flags |= PANE_REDRAW;
        !          1113: }
        !          1114: 
        !          1115: void
        !          1116: window_pane_unset_palette(struct window_pane *wp, u_int n)
        !          1117: {
        !          1118:        if (n > 0xff || wp->palette == NULL)
        !          1119:                return;
        !          1120: 
        !          1121:        wp->palette[n] = 0;
        !          1122:        wp->flags |= PANE_REDRAW;
        !          1123: }
        !          1124: 
        !          1125: void
        !          1126: window_pane_reset_palette(struct window_pane *wp)
        !          1127: {
        !          1128:        if (wp->palette == NULL)
        !          1129:                return;
        !          1130: 
        !          1131:        free(wp->palette);
        !          1132:        wp->palette = NULL;
        !          1133:        wp->flags |= PANE_REDRAW;
        !          1134: }
        !          1135: 
        !          1136: int
        !          1137: window_pane_get_palette(const struct window_pane *wp, int c)
        !          1138: {
        !          1139:        int     new;
        !          1140: 
        !          1141:        if (wp == NULL || wp->palette == NULL)
        !          1142:                return (-1);
        !          1143: 
        !          1144:        new = -1;
        !          1145:        if (c < 8)
        !          1146:                new = wp->palette[c];
        !          1147:        else if (c >= 90 && c <= 97)
        !          1148:                new = wp->palette[8 + c - 90];
        !          1149:        else if (c & COLOUR_FLAG_256)
        !          1150:                new = wp->palette[c & ~COLOUR_FLAG_256];
        !          1151:        if (new == 0)
        !          1152:                return (-1);
        !          1153:        return (new);
        !          1154: }
        !          1155: 
        !          1156: static void
        !          1157: window_pane_mode_timer(__unused int fd, __unused short events, void *arg)
        !          1158: {
        !          1159:        struct window_pane      *wp = arg;
        !          1160:        struct timeval           tv = { .tv_sec = 10 };
        !          1161:        int                      n = 0;
        !          1162: 
        !          1163:        evtimer_del(&wp->modetimer);
        !          1164:        evtimer_add(&wp->modetimer, &tv);
        !          1165: 
        !          1166:        log_debug("%%%u in mode: last=%ld", wp->id, (long)wp->modelast);
        !          1167: 
        !          1168:        if (wp->modelast < time(NULL) - WINDOW_MODE_TIMEOUT) {
        !          1169:                if (ioctl(wp->fd, FIONREAD, &n) == -1 || n > 0)
        !          1170:                        window_pane_reset_mode(wp);
        !          1171:        }
        !          1172: }
        !          1173: 
        !          1174: int
        !          1175: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
        !          1176: {
        !          1177:        struct screen   *s;
        !          1178:        struct timeval   tv = { .tv_sec = 10 };
        !          1179: 
        !          1180:        if (wp->mode != NULL)
        !          1181:                return (1);
        !          1182:        wp->mode = mode;
        !          1183: 
        !          1184:        wp->modelast = time(NULL);
        !          1185:        evtimer_set(&wp->modetimer, window_pane_mode_timer, wp);
        !          1186:        evtimer_add(&wp->modetimer, &tv);
        !          1187: 
        !          1188:        if ((s = wp->mode->init(wp)) != NULL)
        !          1189:                wp->screen = s;
        !          1190:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
        !          1191: 
        !          1192:        server_status_window(wp->window);
        !          1193:        return (0);
        !          1194: }
        !          1195: 
        !          1196: void
        !          1197: window_pane_reset_mode(struct window_pane *wp)
        !          1198: {
        !          1199:        if (wp->mode == NULL)
        !          1200:                return;
        !          1201: 
        !          1202:        evtimer_del(&wp->modetimer);
        !          1203: 
        !          1204:        wp->mode->free(wp);
        !          1205:        wp->mode = NULL;
        !          1206:        wp->modeprefix = 1;
        !          1207: 
        !          1208:        wp->screen = &wp->base;
        !          1209:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
        !          1210: 
        !          1211:        server_status_window(wp->window);
        !          1212: }
        !          1213: 
        !          1214: void
        !          1215: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
        !          1216:     key_code key, struct mouse_event *m)
        !          1217: {
        !          1218:        struct window_pane      *wp2;
        !          1219: 
        !          1220:        if (KEYC_IS_MOUSE(key) && m == NULL)
        !          1221:                return;
        !          1222: 
        !          1223:        if (wp->mode != NULL) {
        !          1224:                wp->modelast = time(NULL);
        !          1225:                if (wp->mode->key != NULL)
        !          1226:                        wp->mode->key(wp, c, s, key, m);
        !          1227:                return;
        !          1228:        }
        !          1229: 
        !          1230:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
        !          1231:                return;
        !          1232: 
        !          1233:        input_key(wp, key, m);
        !          1234: 
        !          1235:        if (KEYC_IS_MOUSE(key))
        !          1236:                return;
        !          1237:        if (options_get_number(wp->window->options, "synchronize-panes")) {
        !          1238:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
        !          1239:                        if (wp2 == wp || wp2->mode != NULL)
        !          1240:                                continue;
        !          1241:                        if (wp2->fd == -1 || wp2->flags & PANE_INPUTOFF)
        !          1242:                                continue;
        !          1243:                        if (window_pane_visible(wp2))
        !          1244:                                input_key(wp2, key, NULL);
        !          1245:                }
        !          1246:        }
        !          1247: }
        !          1248: 
        !          1249: int
        !          1250: window_pane_outside(struct window_pane *wp)
        !          1251: {
        !          1252:        struct window   *w = wp->window;
        !          1253: 
        !          1254:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
        !          1255:                return (1);
        !          1256:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
        !          1257:                return (1);
        !          1258:        return (0);
        !          1259: }
        !          1260: 
        !          1261: int
        !          1262: window_pane_visible(struct window_pane *wp)
        !          1263: {
        !          1264:        if (wp->layout_cell == NULL)
        !          1265:                return (0);
        !          1266:        return (!window_pane_outside(wp));
        !          1267: }
        !          1268: 
        !          1269: char *
        !          1270: window_pane_search(struct window_pane *wp, const char *searchstr,
        !          1271:     u_int *lineno)
        !          1272: {
        !          1273:        struct screen   *s = &wp->base;
        !          1274:        char            *newsearchstr, *line, *msg;
        !          1275:        u_int            i;
        !          1276: 
        !          1277:        msg = NULL;
        !          1278:        xasprintf(&newsearchstr, "*%s*", searchstr);
        !          1279: 
        !          1280:        for (i = 0; i < screen_size_y(s); i++) {
        !          1281:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
        !          1282:                if (fnmatch(newsearchstr, line, 0) == 0) {
        !          1283:                        msg = line;
        !          1284:                        if (lineno != NULL)
        !          1285:                                *lineno = i;
        !          1286:                        break;
        !          1287:                }
        !          1288:                free(line);
        !          1289:        }
        !          1290: 
        !          1291:        free(newsearchstr);
        !          1292:        return (msg);
        !          1293: }
        !          1294: 
        !          1295: /* Get MRU pane from a list. */
        !          1296: static struct window_pane *
        !          1297: window_pane_choose_best(struct window_pane **list, u_int size)
        !          1298: {
        !          1299:        struct window_pane      *next, *best;
        !          1300:        u_int                    i;
        !          1301: 
        !          1302:        if (size == 0)
        !          1303:                return (NULL);
        !          1304: 
        !          1305:        best = list[0];
        !          1306:        for (i = 1; i < size; i++) {
        !          1307:                next = list[i];
        !          1308:                if (next->active_point > best->active_point)
        !          1309:                        best = next;
        !          1310:        }
        !          1311:        return (best);
        !          1312: }
        !          1313: 
        !          1314: /*
        !          1315:  * Find the pane directly above another. We build a list of those adjacent to
        !          1316:  * top edge and then choose the best.
        !          1317:  */
        !          1318: struct window_pane *
        !          1319: window_pane_find_up(struct window_pane *wp)
        !          1320: {
        !          1321:        struct window_pane      *next, *best, **list;
        !          1322:        u_int                    edge, left, right, end, size;
        !          1323:        int                      status, found;
        !          1324: 
        !          1325:        if (wp == NULL || !window_pane_visible(wp))
        !          1326:                return (NULL);
        !          1327:        status = options_get_number(wp->window->options, "pane-border-status");
        !          1328: 
        !          1329:        list = NULL;
        !          1330:        size = 0;
        !          1331: 
        !          1332:        edge = wp->yoff;
        !          1333:        if (edge == (status == 1 ? 1 : 0))
        !          1334:                edge = wp->window->sy + 1 - (status == 2 ? 1 : 0);
        !          1335: 
        !          1336:        left = wp->xoff;
        !          1337:        right = wp->xoff + wp->sx;
        !          1338: 
        !          1339:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
        !          1340:                if (next == wp || !window_pane_visible(next))
        !          1341:                        continue;
        !          1342:                if (next->yoff + next->sy + 1 != edge)
        !          1343:                        continue;
        !          1344:                end = next->xoff + next->sx - 1;
        !          1345: 
        !          1346:                found = 0;
        !          1347:                if (next->xoff < left && end > right)
        !          1348:                        found = 1;
        !          1349:                else if (next->xoff >= left && next->xoff <= right)
        !          1350:                        found = 1;
        !          1351:                else if (end >= left && end <= right)
        !          1352:                        found = 1;
        !          1353:                if (!found)
        !          1354:                        continue;
        !          1355:                list = xreallocarray(list, size + 1, sizeof *list);
        !          1356:                list[size++] = next;
        !          1357:        }
        !          1358: 
        !          1359:        best = window_pane_choose_best(list, size);
        !          1360:        free(list);
        !          1361:        return (best);
        !          1362: }
        !          1363: 
        !          1364: /* Find the pane directly below another. */
        !          1365: struct window_pane *
        !          1366: window_pane_find_down(struct window_pane *wp)
        !          1367: {
        !          1368:        struct window_pane      *next, *best, **list;
        !          1369:        u_int                    edge, left, right, end, size;
        !          1370:        int                      status, found;
        !          1371: 
        !          1372:        if (wp == NULL || !window_pane_visible(wp))
        !          1373:                return (NULL);
        !          1374:        status = options_get_number(wp->window->options, "pane-border-status");
        !          1375: 
        !          1376:        list = NULL;
        !          1377:        size = 0;
        !          1378: 
        !          1379:        edge = wp->yoff + wp->sy + 1;
        !          1380:        if (edge >= wp->window->sy - (status == 2 ? 1 : 0))
        !          1381:                edge = (status == 1 ? 1 : 0);
        !          1382: 
        !          1383:        left = wp->xoff;
        !          1384:        right = wp->xoff + wp->sx;
        !          1385: 
        !          1386:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
        !          1387:                if (next == wp || !window_pane_visible(next))
        !          1388:                        continue;
        !          1389:                if (next->yoff != edge)
        !          1390:                        continue;
        !          1391:                end = next->xoff + next->sx - 1;
        !          1392: 
        !          1393:                found = 0;
        !          1394:                if (next->xoff < left && end > right)
        !          1395:                        found = 1;
        !          1396:                else if (next->xoff >= left && next->xoff <= right)
        !          1397:                        found = 1;
        !          1398:                else if (end >= left && end <= right)
        !          1399:                        found = 1;
        !          1400:                if (!found)
        !          1401:                        continue;
        !          1402:                list = xreallocarray(list, size + 1, sizeof *list);
        !          1403:                list[size++] = next;
        !          1404:        }
        !          1405: 
        !          1406:        best = window_pane_choose_best(list, size);
        !          1407:        free(list);
        !          1408:        return (best);
        !          1409: }
        !          1410: 
        !          1411: /* Find the pane directly to the left of another. */
        !          1412: struct window_pane *
        !          1413: window_pane_find_left(struct window_pane *wp)
        !          1414: {
        !          1415:        struct window_pane      *next, *best, **list;
        !          1416:        u_int                    edge, top, bottom, end, size;
        !          1417:        int                      found;
        !          1418: 
        !          1419:        if (wp == NULL || !window_pane_visible(wp))
        !          1420:                return (NULL);
        !          1421: 
        !          1422:        list = NULL;
        !          1423:        size = 0;
        !          1424: 
        !          1425:        edge = wp->xoff;
        !          1426:        if (edge == 0)
        !          1427:                edge = wp->window->sx + 1;
        !          1428: 
        !          1429:        top = wp->yoff;
        !          1430:        bottom = wp->yoff + wp->sy;
        !          1431: 
        !          1432:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
        !          1433:                if (next == wp || !window_pane_visible(next))
        !          1434:                        continue;
        !          1435:                if (next->xoff + next->sx + 1 != edge)
        !          1436:                        continue;
        !          1437:                end = next->yoff + next->sy - 1;
        !          1438: 
        !          1439:                found = 0;
        !          1440:                if (next->yoff < top && end > bottom)
        !          1441:                        found = 1;
        !          1442:                else if (next->yoff >= top && next->yoff <= bottom)
        !          1443:                        found = 1;
        !          1444:                else if (end >= top && end <= bottom)
        !          1445:                        found = 1;
        !          1446:                if (!found)
        !          1447:                        continue;
        !          1448:                list = xreallocarray(list, size + 1, sizeof *list);
        !          1449:                list[size++] = next;
        !          1450:        }
        !          1451: 
        !          1452:        best = window_pane_choose_best(list, size);
        !          1453:        free(list);
        !          1454:        return (best);
        !          1455: }
        !          1456: 
        !          1457: /* Find the pane directly to the right of another. */
        !          1458: struct window_pane *
        !          1459: window_pane_find_right(struct window_pane *wp)
        !          1460: {
        !          1461:        struct window_pane      *next, *best, **list;
        !          1462:        u_int                    edge, top, bottom, end, size;
        !          1463:        int                      found;
        !          1464: 
        !          1465:        if (wp == NULL || !window_pane_visible(wp))
        !          1466:                return (NULL);
        !          1467: 
        !          1468:        list = NULL;
        !          1469:        size = 0;
        !          1470: 
        !          1471:        edge = wp->xoff + wp->sx + 1;
        !          1472:        if (edge >= wp->window->sx)
        !          1473:                edge = 0;
        !          1474: 
        !          1475:        top = wp->yoff;
        !          1476:        bottom = wp->yoff + wp->sy;
        !          1477: 
        !          1478:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
        !          1479:                if (next == wp || !window_pane_visible(next))
        !          1480:                        continue;
        !          1481:                if (next->xoff != edge)
        !          1482:                        continue;
        !          1483:                end = next->yoff + next->sy - 1;
        !          1484: 
        !          1485:                found = 0;
        !          1486:                if (next->yoff < top && end > bottom)
        !          1487:                        found = 1;
        !          1488:                else if (next->yoff >= top && next->yoff <= bottom)
        !          1489:                        found = 1;
        !          1490:                else if (end >= top && end <= bottom)
        !          1491:                        found = 1;
        !          1492:                if (!found)
        !          1493:                        continue;
        !          1494:                list = xreallocarray(list, size + 1, sizeof *list);
        !          1495:                list[size++] = next;
        !          1496:        }
        !          1497: 
        !          1498:        best = window_pane_choose_best(list, size);
        !          1499:        free(list);
        !          1500:        return (best);
        !          1501: }
        !          1502: 
        !          1503: /* Clear alert flags for a winlink */
        !          1504: void
        !          1505: winlink_clear_flags(struct winlink *wl)
        !          1506: {
        !          1507:        struct winlink  *loop;
        !          1508: 
        !          1509:        wl->window->flags &= ~WINDOW_ALERTFLAGS;
        !          1510:        TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
        !          1511:                if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
        !          1512:                        loop->flags &= ~WINLINK_ALERTFLAGS;
        !          1513:                        server_status_session(loop->session);
        !          1514:                }
        !          1515:        }
        !          1516: }
        !          1517: 
        !          1518: /* Shuffle window indexes up. */
        !          1519: int
        !          1520: winlink_shuffle_up(struct session *s, struct winlink *wl)
        !          1521: {
        !          1522:        int      idx, last;
        !          1523: 
        !          1524:        idx = wl->idx + 1;
        !          1525: 
        !          1526:        /* Find the next free index. */
        !          1527:        for (last = idx; last < INT_MAX; last++) {
        !          1528:                if (winlink_find_by_index(&s->windows, last) == NULL)
        !          1529:                        break;
        !          1530:        }
        !          1531:        if (last == INT_MAX)
        !          1532:                return (-1);
        !          1533: 
        !          1534:        /* Move everything from last - 1 to idx up a bit. */
        !          1535:        for (; last > idx; last--) {
        !          1536:                wl = winlink_find_by_index(&s->windows, last - 1);
        !          1537:                server_link_window(s, wl, s, last, 0, 0, NULL);
        !          1538:                server_unlink_window(s, wl);
        !          1539:        }
        !          1540: 
        !          1541:        return (idx);
        !          1542: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>