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

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