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

1.290   ! nicm        1: /* $OpenBSD: window.c,v 1.289 2024/03/21 11:30:42 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:
1.236     nicm       22: #include <ctype.h>
1.1       nicm       23: #include <errno.h>
                     24: #include <fcntl.h>
1.5       nicm       25: #include <fnmatch.h>
1.232     nicm       26: #include <regex.h>
1.203     nicm       27: #include <signal.h>
1.1       nicm       28: #include <stdint.h>
                     29: #include <stdlib.h>
                     30: #include <string.h>
1.163     nicm       31: #include <time.h>
1.1       nicm       32: #include <unistd.h>
                     33: #include <util.h>
1.198     nicm       34: #include <vis.h>
1.1       nicm       35:
                     36: #include "tmux.h"
                     37:
                     38: /*
1.14      nicm       39:  * Each window is attached to a number of panes, each of which is a pty. This
1.1       nicm       40:  * file contains code to handle them.
                     41:  *
                     42:  * A pane has two buffers attached, these are filled and emptied by the main
                     43:  * server poll loop. Output data is received from pty's in screen format,
                     44:  * translated and returned as a series of escape sequences and strings via
                     45:  * input_parse (in input.c). Input data is received as key codes and written
                     46:  * directly via input_key.
                     47:  *
                     48:  * Each pane also has a "virtual" screen (screen.c) which contains the current
                     49:  * state and is redisplayed when the window is reattached to a client.
                     50:  *
                     51:  * Windows are stored directly on a global array and wrapped in any number of
                     52:  * winlink structs to be linked onto local session RB trees. A reference count
                     53:  * is maintained and a window removed from the global list and destroyed when
                     54:  * it reaches zero.
                     55:  */
1.124     nicm       56:
1.1       nicm       57: /* Global window list. */
                     58: struct windows windows;
                     59:
1.64      nicm       60: /* Global panes tree. */
                     61: struct window_pane_tree all_window_panes;
1.166     nicm       62: static u_int   next_window_pane_id;
                     63: static u_int   next_window_id;
                     64: static u_int   next_active_point;
1.37      nicm       65:
1.228     nicm       66: struct window_pane_input_data {
                     67:        struct cmdq_item        *item;
                     68:        u_int                    wp;
1.283     nicm       69:        struct client_file      *file;
1.228     nicm       70: };
                     71:
1.169     nicm       72: static struct window_pane *window_pane_create(struct window *, u_int, u_int,
                     73:                    u_int);
                     74: static void    window_pane_destroy(struct window_pane *);
                     75:
1.122     nicm       76: RB_GENERATE(windows, window, entry, window_cmp);
1.169     nicm       77: RB_GENERATE(winlinks, winlink, entry, winlink_cmp);
                     78: RB_GENERATE(window_pane_tree, window_pane, tree_entry, window_pane_cmp);
1.122     nicm       79:
                     80: int
                     81: window_cmp(struct window *w1, struct window *w2)
                     82: {
                     83:        return (w1->id - w2->id);
                     84: }
                     85:
1.1       nicm       86: int
                     87: winlink_cmp(struct winlink *wl1, struct winlink *wl2)
                     88: {
                     89:        return (wl1->idx - wl2->idx);
1.12      nicm       90: }
                     91:
1.64      nicm       92: int
                     93: window_pane_cmp(struct window_pane *wp1, struct window_pane *wp2)
                     94: {
                     95:        return (wp1->id - wp2->id);
                     96: }
                     97:
1.12      nicm       98: struct winlink *
                     99: winlink_find_by_window(struct winlinks *wwl, struct window *w)
                    100: {
                    101:        struct winlink  *wl;
                    102:
                    103:        RB_FOREACH(wl, winlinks, wwl) {
                    104:                if (wl->window == w)
                    105:                        return (wl);
                    106:        }
                    107:
                    108:        return (NULL);
1.1       nicm      109: }
                    110:
                    111: struct winlink *
                    112: winlink_find_by_index(struct winlinks *wwl, int idx)
                    113: {
                    114:        struct winlink  wl;
                    115:
                    116:        if (idx < 0)
                    117:                fatalx("bad index");
                    118:
                    119:        wl.idx = idx;
                    120:        return (RB_FIND(winlinks, wwl, &wl));
                    121: }
                    122:
1.71      nicm      123: struct winlink *
                    124: winlink_find_by_window_id(struct winlinks *wwl, u_int id)
                    125: {
                    126:        struct winlink *wl;
                    127:
                    128:        RB_FOREACH(wl, winlinks, wwl) {
                    129:                if (wl->window->id == id)
                    130:                        return (wl);
                    131:        }
1.78      nicm      132:        return (NULL);
1.71      nicm      133: }
                    134:
1.169     nicm      135: static int
1.22      nicm      136: winlink_next_index(struct winlinks *wwl, int idx)
1.1       nicm      137: {
1.22      nicm      138:        int     i;
1.1       nicm      139:
1.22      nicm      140:        i = idx;
                    141:        do {
1.1       nicm      142:                if (winlink_find_by_index(wwl, i) == NULL)
                    143:                        return (i);
1.22      nicm      144:                if (i == INT_MAX)
                    145:                        i = 0;
                    146:                else
                    147:                        i++;
                    148:        } while (i != idx);
                    149:        return (-1);
1.1       nicm      150: }
                    151:
                    152: u_int
                    153: winlink_count(struct winlinks *wwl)
                    154: {
                    155:        struct winlink  *wl;
                    156:        u_int            n;
                    157:
                    158:        n = 0;
                    159:        RB_FOREACH(wl, winlinks, wwl)
                    160:                n++;
                    161:
                    162:        return (n);
                    163: }
                    164:
                    165: struct winlink *
1.63      nicm      166: winlink_add(struct winlinks *wwl, int idx)
1.1       nicm      167: {
                    168:        struct winlink  *wl;
                    169:
1.22      nicm      170:        if (idx < 0) {
                    171:                if ((idx = winlink_next_index(wwl, -idx - 1)) == -1)
                    172:                        return (NULL);
                    173:        } else if (winlink_find_by_index(wwl, idx) != NULL)
1.1       nicm      174:                return (NULL);
                    175:
                    176:        wl = xcalloc(1, sizeof *wl);
                    177:        wl->idx = idx;
                    178:        RB_INSERT(winlinks, wwl, wl);
                    179:
1.63      nicm      180:        return (wl);
                    181: }
                    182:
                    183: void
                    184: winlink_set_window(struct winlink *wl, struct window *w)
                    185: {
1.174     nicm      186:        if (wl->window != NULL) {
                    187:                TAILQ_REMOVE(&wl->window->winlinks, wl, wentry);
1.192     nicm      188:                window_remove_ref(wl->window, __func__);
1.174     nicm      189:        }
                    190:        TAILQ_INSERT_TAIL(&w->winlinks, wl, wentry);
1.63      nicm      191:        wl->window = w;
1.192     nicm      192:        window_add_ref(w, __func__);
1.1       nicm      193: }
                    194:
                    195: void
                    196: winlink_remove(struct winlinks *wwl, struct winlink *wl)
                    197: {
                    198:        struct window   *w = wl->window;
                    199:
1.174     nicm      200:        if (w != NULL) {
                    201:                TAILQ_REMOVE(&w->winlinks, wl, wentry);
1.192     nicm      202:                window_remove_ref(w, __func__);
1.174     nicm      203:        }
                    204:
1.1       nicm      205:        RB_REMOVE(winlinks, wwl, wl);
1.82      nicm      206:        free(wl);
1.1       nicm      207: }
                    208:
                    209: struct winlink *
1.41      nicm      210: winlink_next(struct winlink *wl)
1.1       nicm      211: {
                    212:        return (RB_NEXT(winlinks, wwl, wl));
                    213: }
                    214:
                    215: struct winlink *
1.41      nicm      216: winlink_previous(struct winlink *wl)
1.1       nicm      217: {
                    218:        return (RB_PREV(winlinks, wwl, wl));
1.52      nicm      219: }
                    220:
                    221: struct winlink *
1.53      nicm      222: winlink_next_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      223: {
                    224:        for (; n > 0; n--) {
                    225:                if ((wl = RB_NEXT(winlinks, wwl, wl)) == NULL)
1.53      nicm      226:                        wl = RB_MIN(winlinks, &s->windows);
1.52      nicm      227:        }
                    228:
                    229:        return (wl);
                    230: }
                    231:
                    232: struct winlink *
1.53      nicm      233: winlink_previous_by_number(struct winlink *wl, struct session *s, int n)
1.52      nicm      234: {
                    235:        for (; n > 0; n--) {
                    236:                if ((wl = RB_PREV(winlinks, wwl, wl)) == NULL)
1.53      nicm      237:                        wl = RB_MAX(winlinks, &s->windows);
1.52      nicm      238:        }
                    239:
                    240:        return (wl);
1.1       nicm      241: }
                    242:
                    243: void
                    244: winlink_stack_push(struct winlink_stack *stack, struct winlink *wl)
                    245: {
                    246:        if (wl == NULL)
                    247:                return;
                    248:
                    249:        winlink_stack_remove(stack, wl);
1.28      nicm      250:        TAILQ_INSERT_HEAD(stack, wl, sentry);
1.286     nicm      251:        wl->flags |= WINLINK_VISITED;
1.1       nicm      252: }
                    253:
                    254: void
                    255: winlink_stack_remove(struct winlink_stack *stack, struct winlink *wl)
                    256: {
1.286     nicm      257:        if (wl != NULL && (wl->flags & WINLINK_VISITED)) {
                    258:                TAILQ_REMOVE(stack, wl, sentry);
                    259:                wl->flags &= ~WINLINK_VISITED;
1.1       nicm      260:        }
                    261: }
                    262:
                    263: struct window *
1.125     nicm      264: window_find_by_id_str(const char *s)
1.123     nicm      265: {
                    266:        const char      *errstr;
                    267:        u_int            id;
                    268:
                    269:        if (*s != '@')
                    270:                return (NULL);
                    271:
                    272:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    273:        if (errstr != NULL)
                    274:                return (NULL);
                    275:        return (window_find_by_id(id));
                    276: }
                    277:
                    278: struct window *
1.71      nicm      279: window_find_by_id(u_int id)
                    280: {
1.122     nicm      281:        struct window   w;
1.71      nicm      282:
1.122     nicm      283:        w.id = id;
                    284:        return (RB_FIND(windows, &windows, &w));
1.71      nicm      285: }
                    286:
1.143     nicm      287: void
                    288: window_update_activity(struct window *w)
                    289: {
                    290:        gettimeofday(&w->activity_time, NULL);
                    291:        alerts_queue(w, WINDOW_ACTIVITY);
                    292: }
                    293:
1.71      nicm      294: struct window *
1.245     nicm      295: window_create(u_int sx, u_int sy, u_int xpixel, u_int ypixel)
1.1       nicm      296: {
                    297:        struct window   *w;
                    298:
1.245     nicm      299:        if (xpixel == 0)
                    300:                xpixel = DEFAULT_XPIXEL;
                    301:        if (ypixel == 0)
                    302:                ypixel = DEFAULT_YPIXEL;
                    303:
1.38      nicm      304:        w = xcalloc(1, sizeof *w);
1.244     nicm      305:        w->name = xstrdup("");
1.234     nicm      306:        w->flags = 0;
1.1       nicm      307:
                    308:        TAILQ_INIT(&w->panes);
1.286     nicm      309:        TAILQ_INIT(&w->last_panes);
1.1       nicm      310:        w->active = NULL;
1.14      nicm      311:
1.17      nicm      312:        w->lastlayout = -1;
1.14      nicm      313:        w->layout_root = NULL;
1.42      nicm      314:
1.1       nicm      315:        w->sx = sx;
                    316:        w->sy = sy;
1.276     nicm      317:        w->manual_sx = sx;
                    318:        w->manual_sy = sy;
1.245     nicm      319:        w->xpixel = xpixel;
                    320:        w->ypixel = ypixel;
1.133     nicm      321:
1.146     nicm      322:        w->options = options_create(global_w_options);
1.1       nicm      323:
                    324:        w->references = 0;
1.174     nicm      325:        TAILQ_INIT(&w->winlinks);
1.1       nicm      326:
1.122     nicm      327:        w->id = next_window_id++;
1.129     nicm      328:        RB_INSERT(windows, &windows, w);
1.122     nicm      329:
1.279     nicm      330:        window_set_fill_character(w);
1.143     nicm      331:        window_update_activity(w);
                    332:
1.271     nicm      333:        log_debug("%s: @%u create %ux%u (%ux%u)", __func__, w->id, sx, sy,
                    334:            w->xpixel, w->ypixel);
1.1       nicm      335:        return (w);
                    336: }
                    337:
1.240     nicm      338: static void
1.1       nicm      339: window_destroy(struct window *w)
                    340: {
1.192     nicm      341:        log_debug("window @%u destroyed (%d references)", w->id, w->references);
1.174     nicm      342:
1.289     nicm      343:        window_unzoom(w, 0);
1.122     nicm      344:        RB_REMOVE(windows, &windows, w);
1.1       nicm      345:
1.14      nicm      346:        if (w->layout_root != NULL)
1.135     nicm      347:                layout_free_cell(w->layout_root);
                    348:        if (w->saved_layout_root != NULL)
                    349:                layout_free_cell(w->saved_layout_root);
1.127     nicm      350:        free(w->old_layout);
1.139     nicm      351:
1.227     nicm      352:        window_destroy_panes(w);
                    353:
1.141     nicm      354:        if (event_initialized(&w->name_event))
                    355:                evtimer_del(&w->name_event);
1.38      nicm      356:
1.143     nicm      357:        if (event_initialized(&w->alerts_timer))
                    358:                evtimer_del(&w->alerts_timer);
1.213     nicm      359:        if (event_initialized(&w->offset_timer))
                    360:                event_del(&w->offset_timer);
1.143     nicm      361:
1.146     nicm      362:        options_free(w->options);
1.279     nicm      363:        free(w->fill_character);
1.1       nicm      364:
1.82      nicm      365:        free(w->name);
                    366:        free(w);
1.84      nicm      367: }
                    368:
1.200     nicm      369: int
                    370: window_pane_destroy_ready(struct window_pane *wp)
                    371: {
1.201     nicm      372:        int     n;
                    373:
                    374:        if (wp->pipe_fd != -1) {
                    375:                if (EVBUFFER_LENGTH(wp->pipe_event->output) != 0)
                    376:                        return (0);
                    377:                if (ioctl(wp->fd, FIONREAD, &n) != -1 && n > 0)
                    378:                        return (0);
                    379:        }
1.200     nicm      380:
                    381:        if (~wp->flags & PANE_EXITED)
                    382:                return (0);
                    383:        return (1);
                    384: }
                    385:
1.84      nicm      386: void
1.192     nicm      387: window_add_ref(struct window *w, const char *from)
                    388: {
                    389:        w->references++;
                    390:        log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
                    391: }
                    392:
                    393: void
                    394: window_remove_ref(struct window *w, const char *from)
1.84      nicm      395: {
                    396:        w->references--;
1.192     nicm      397:        log_debug("%s: @%u %s, now %d", __func__, w->id, from, w->references);
                    398:
1.84      nicm      399:        if (w->references == 0)
                    400:                window_destroy(w);
1.72      nicm      401: }
                    402:
                    403: void
                    404: window_set_name(struct window *w, const char *new_name)
                    405: {
1.82      nicm      406:        free(w->name);
1.198     nicm      407:        utf8_stravis(&w->name, new_name, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.172     nicm      408:        notify_window("window-renamed", w);
1.1       nicm      409: }
                    410:
1.15      nicm      411: void
1.245     nicm      412: window_resize(struct window *w, u_int sx, u_int sy, int xpixel, int ypixel)
1.1       nicm      413: {
1.245     nicm      414:        if (xpixel == 0)
                    415:                xpixel = DEFAULT_XPIXEL;
                    416:        if (ypixel == 0)
                    417:                ypixel = DEFAULT_YPIXEL;
                    418:
                    419:        log_debug("%s: @%u resize %ux%u (%ux%u)", __func__, w->id, sx, sy,
1.249     nicm      420:            xpixel == -1 ? w->xpixel : (u_int)xpixel,
                    421:            ypixel == -1 ? w->ypixel : (u_int)ypixel);
1.1       nicm      422:        w->sx = sx;
                    423:        w->sy = sy;
1.245     nicm      424:        if (xpixel != -1)
                    425:                w->xpixel = xpixel;
                    426:        if (ypixel != -1)
                    427:                w->ypixel = ypixel;
                    428: }
                    429:
                    430: void
1.272     nicm      431: window_pane_send_resize(struct window_pane *wp, u_int sx, u_int sy)
1.245     nicm      432: {
                    433:        struct window   *w = wp->window;
                    434:        struct winsize   ws;
                    435:
                    436:        if (wp->fd == -1)
                    437:                return;
1.265     nicm      438:
1.272     nicm      439:        log_debug("%s: %%%u resize to %u,%u", __func__, wp->id, sx, sy);
1.245     nicm      440:
                    441:        memset(&ws, 0, sizeof ws);
1.272     nicm      442:        ws.ws_col = sx;
1.262     nicm      443:        ws.ws_row = sy;
1.245     nicm      444:        ws.ws_xpixel = w->xpixel * ws.ws_col;
                    445:        ws.ws_ypixel = w->ypixel * ws.ws_row;
                    446:        if (ioctl(wp->fd, TIOCSWINSZ, &ws) == -1)
                    447:                fatal("ioctl failed");
1.1       nicm      448: }
                    449:
1.114     nicm      450: int
1.118     nicm      451: window_has_pane(struct window *w, struct window_pane *wp)
                    452: {
                    453:        struct window_pane      *wp1;
                    454:
                    455:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    456:                if (wp1 == wp)
                    457:                        return (1);
                    458:        }
                    459:        return (0);
                    460: }
                    461:
1.274     nicm      462: void
                    463: window_update_focus(struct window *w)
                    464: {
                    465:        if (w != NULL) {
                    466:                log_debug("%s: @%u", __func__, w->id);
                    467:                window_pane_update_focus(w->active);
                    468:        }
                    469: }
                    470:
                    471: void
                    472: window_pane_update_focus(struct window_pane *wp)
                    473: {
                    474:        struct client   *c;
                    475:        int              focused = 0;
                    476:
1.290   ! nicm      477:        if (wp != NULL && (~wp->flags & PANE_EXITED)) {
1.274     nicm      478:                if (wp != wp->window->active)
                    479:                        focused = 0;
                    480:                else {
                    481:                        TAILQ_FOREACH(c, &clients, entry) {
                    482:                                if (c->session != NULL &&
                    483:                                    c->session->attached != 0 &&
                    484:                                    (c->flags & CLIENT_FOCUSED) &&
                    485:                                    c->session->curw->window == wp->window) {
                    486:                                        focused = 1;
                    487:                                        break;
                    488:                                }
                    489:                        }
                    490:                }
                    491:                if (!focused && (wp->flags & PANE_FOCUSED)) {
                    492:                        log_debug("%s: %%%u focus out", __func__, wp->id);
                    493:                        if (wp->base.mode & MODE_FOCUSON)
                    494:                                bufferevent_write(wp->event, "\033[O", 3);
                    495:                        notify_pane("pane-focus-out", wp);
                    496:                        wp->flags &= ~PANE_FOCUSED;
                    497:                } else if (focused && (~wp->flags & PANE_FOCUSED)) {
                    498:                        log_debug("%s: %%%u focus in", __func__, wp->id);
                    499:                        if (wp->base.mode & MODE_FOCUSON)
                    500:                                bufferevent_write(wp->event, "\033[I", 3);
                    501:                        notify_pane("pane-focus-in", wp);
                    502:                        wp->flags |= PANE_FOCUSED;
                    503:                } else
                    504:                        log_debug("%s: %%%u focus unchanged", __func__, wp->id);
                    505:        }
                    506: }
                    507:
1.118     nicm      508: int
1.226     nicm      509: window_set_active_pane(struct window *w, struct window_pane *wp, int notify)
1.1       nicm      510: {
1.286     nicm      511:        struct window_pane *lastwp;
                    512:
1.226     nicm      513:        log_debug("%s: pane %%%u", __func__, wp->id);
                    514:
1.59      nicm      515:        if (wp == w->active)
1.114     nicm      516:                return (0);
1.286     nicm      517:        lastwp = w->active;
                    518:
                    519:        window_pane_stack_remove(&w->last_panes, wp);
                    520:        window_pane_stack_push(&w->last_panes, lastwp);
1.226     nicm      521:
1.1       nicm      522:        w->active = wp;
1.109     nicm      523:        w->active->active_point = next_active_point++;
1.136     nicm      524:        w->active->flags |= PANE_CHANGED;
1.274     nicm      525:
                    526:        if (options_get_number(global_options, "focus-events")) {
1.286     nicm      527:                window_pane_update_focus(lastwp);
1.274     nicm      528:                window_pane_update_focus(w->active);
                    529:        }
1.226     nicm      530:
1.213     nicm      531:        tty_update_window_offset(w);
1.226     nicm      532:
                    533:        if (notify)
                    534:                notify_window("window-pane-changed", w);
1.114     nicm      535:        return (1);
1.145     nicm      536: }
                    537:
1.273     nicm      538: static int
                    539: window_pane_get_palette(struct window_pane *wp, int c)
                    540: {
                    541:        if (wp == NULL)
                    542:                return (-1);
                    543:        return (colour_palette_get(&wp->palette, c));
                    544: }
                    545:
1.145     nicm      546: void
                    547: window_redraw_active_switch(struct window *w, struct window_pane *wp)
                    548: {
1.258     nicm      549:        struct grid_cell        *gc1, *gc2;
                    550:        int                      c1, c2;
1.145     nicm      551:
                    552:        if (wp == w->active)
                    553:                return;
                    554:
1.234     nicm      555:        for (;;) {
                    556:                /*
                    557:                 * If the active and inactive styles or palettes are different,
                    558:                 * need to redraw the panes.
                    559:                 */
1.258     nicm      560:                gc1 = &wp->cached_gc;
                    561:                gc2 = &wp->cached_active_gc;
                    562:                if (!grid_cells_look_equal(gc1, gc2))
1.234     nicm      563:                        wp->flags |= PANE_REDRAW;
                    564:                else {
1.258     nicm      565:                        c1 = window_pane_get_palette(wp, gc1->fg);
                    566:                        c2 = window_pane_get_palette(wp, gc2->fg);
1.234     nicm      567:                        if (c1 != c2)
                    568:                                wp->flags |= PANE_REDRAW;
                    569:                        else {
1.258     nicm      570:                                c1 = window_pane_get_palette(wp, gc1->bg);
                    571:                                c2 = window_pane_get_palette(wp, gc2->bg);
1.234     nicm      572:                                if (c1 != c2)
                    573:                                        wp->flags |= PANE_REDRAW;
                    574:                        }
                    575:                }
                    576:                if (wp == w->active)
                    577:                        break;
                    578:                wp = w->active;
                    579:        }
1.29      nicm      580: }
                    581:
1.66      nicm      582: struct window_pane *
                    583: window_get_active_at(struct window *w, u_int x, u_int y)
1.29      nicm      584: {
                    585:        struct window_pane      *wp;
                    586:
                    587:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.219     nicm      588:                if (!window_pane_visible(wp))
                    589:                        continue;
1.66      nicm      590:                if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29      nicm      591:                        continue;
1.66      nicm      592:                if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29      nicm      593:                        continue;
1.66      nicm      594:                return (wp);
                    595:        }
                    596:        return (NULL);
                    597: }
                    598:
                    599: struct window_pane *
                    600: window_find_string(struct window *w, const char *s)
                    601: {
1.248     nicm      602:        u_int   x, y, top = 0, bottom = w->sy - 1;
                    603:        int     status;
1.66      nicm      604:
                    605:        x = w->sx / 2;
                    606:        y = w->sy / 2;
                    607:
1.248     nicm      608:        status = options_get_number(w->options, "pane-border-status");
                    609:        if (status == PANE_STATUS_TOP)
                    610:                top++;
                    611:        else if (status == PANE_STATUS_BOTTOM)
                    612:                bottom--;
                    613:
1.66      nicm      614:        if (strcasecmp(s, "top") == 0)
1.248     nicm      615:                y = top;
1.66      nicm      616:        else if (strcasecmp(s, "bottom") == 0)
1.248     nicm      617:                y = bottom;
1.66      nicm      618:        else if (strcasecmp(s, "left") == 0)
                    619:                x = 0;
                    620:        else if (strcasecmp(s, "right") == 0)
                    621:                x = w->sx - 1;
                    622:        else if (strcasecmp(s, "top-left") == 0) {
                    623:                x = 0;
1.248     nicm      624:                y = top;
1.66      nicm      625:        } else if (strcasecmp(s, "top-right") == 0) {
                    626:                x = w->sx - 1;
1.248     nicm      627:                y = top;
1.66      nicm      628:        } else if (strcasecmp(s, "bottom-left") == 0) {
                    629:                x = 0;
1.248     nicm      630:                y = bottom;
1.66      nicm      631:        } else if (strcasecmp(s, "bottom-right") == 0) {
                    632:                x = w->sx - 1;
1.248     nicm      633:                y = bottom;
1.66      nicm      634:        } else
                    635:                return (NULL);
                    636:
                    637:        return (window_get_active_at(w, x, y));
1.1       nicm      638: }
                    639:
1.93      nicm      640: int
                    641: window_zoom(struct window_pane *wp)
                    642: {
                    643:        struct window           *w = wp->window;
                    644:        struct window_pane      *wp1;
                    645:
                    646:        if (w->flags & WINDOW_ZOOMED)
                    647:                return (-1);
                    648:
1.94      nicm      649:        if (window_count_panes(w) == 1)
                    650:                return (-1);
                    651:
1.93      nicm      652:        if (w->active != wp)
1.226     nicm      653:                window_set_active_pane(w, wp, 1);
1.93      nicm      654:
                    655:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    656:                wp1->saved_layout_cell = wp1->layout_cell;
                    657:                wp1->layout_cell = NULL;
                    658:        }
                    659:
                    660:        w->saved_layout_root = w->layout_root;
                    661:        layout_init(w, wp);
                    662:        w->flags |= WINDOW_ZOOMED;
1.172     nicm      663:        notify_window("window-layout-changed", w);
1.93      nicm      664:
                    665:        return (0);
                    666: }
                    667:
                    668: int
1.289     nicm      669: window_unzoom(struct window *w, int notify)
1.93      nicm      670: {
1.97      nicm      671:        struct window_pane      *wp;
1.93      nicm      672:
                    673:        if (!(w->flags & WINDOW_ZOOMED))
                    674:                return (-1);
                    675:
                    676:        w->flags &= ~WINDOW_ZOOMED;
                    677:        layout_free(w);
                    678:        w->layout_root = w->saved_layout_root;
1.120     nicm      679:        w->saved_layout_root = NULL;
1.93      nicm      680:
1.97      nicm      681:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    682:                wp->layout_cell = wp->saved_layout_cell;
                    683:                wp->saved_layout_cell = NULL;
1.93      nicm      684:        }
1.270     nicm      685:        layout_fix_panes(w, NULL);
1.289     nicm      686:
                    687:        if (notify)
                    688:                notify_window("window-layout-changed", w);
1.93      nicm      689:
1.241     nicm      690:        return (0);
                    691: }
                    692:
                    693: int
1.270     nicm      694: window_push_zoom(struct window *w, int always, int flag)
1.241     nicm      695: {
                    696:        log_debug("%s: @%u %d", __func__, w->id,
                    697:            flag && (w->flags & WINDOW_ZOOMED));
1.270     nicm      698:        if (flag && (always || (w->flags & WINDOW_ZOOMED)))
1.241     nicm      699:                w->flags |= WINDOW_WASZOOMED;
                    700:        else
                    701:                w->flags &= ~WINDOW_WASZOOMED;
1.289     nicm      702:        return (window_unzoom(w, 1) == 0);
1.241     nicm      703: }
                    704:
                    705: int
                    706: window_pop_zoom(struct window *w)
                    707: {
                    708:        log_debug("%s: @%u %d", __func__, w->id,
                    709:            !!(w->flags & WINDOW_WASZOOMED));
                    710:        if (w->flags & WINDOW_WASZOOMED)
                    711:                return (window_zoom(w->active) == 0);
1.93      nicm      712:        return (0);
                    713: }
                    714:
1.1       nicm      715: struct window_pane *
1.226     nicm      716: window_add_pane(struct window *w, struct window_pane *other, u_int hlimit,
                    717:     int flags)
1.1       nicm      718: {
                    719:        struct window_pane      *wp;
                    720:
1.185     nicm      721:        if (other == NULL)
                    722:                other = w->active;
                    723:
1.14      nicm      724:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.186     nicm      725:        if (TAILQ_EMPTY(&w->panes)) {
                    726:                log_debug("%s: @%u at start", __func__, w->id);
1.1       nicm      727:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
1.226     nicm      728:        } else if (flags & SPAWN_BEFORE) {
1.186     nicm      729:                log_debug("%s: @%u before %%%u", __func__, w->id, wp->id);
1.226     nicm      730:                if (flags & SPAWN_FULLSIZE)
1.208     nicm      731:                        TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    732:                else
                    733:                        TAILQ_INSERT_BEFORE(other, wp, entry);
1.186     nicm      734:        } else {
                    735:                log_debug("%s: @%u after %%%u", __func__, w->id, wp->id);
1.226     nicm      736:                if (flags & SPAWN_FULLSIZE)
1.208     nicm      737:                        TAILQ_INSERT_TAIL(&w->panes, wp, entry);
                    738:                else
                    739:                        TAILQ_INSERT_AFTER(&w->panes, other, wp, entry);
1.186     nicm      740:        }
1.1       nicm      741:        return (wp);
                    742: }
                    743:
                    744: void
1.105     nicm      745: window_lost_pane(struct window *w, struct window_pane *wp)
1.1       nicm      746: {
1.205     nicm      747:        log_debug("%s: @%u pane %%%u", __func__, w->id, wp->id);
                    748:
1.152     nicm      749:        if (wp == marked_pane.wp)
1.132     nicm      750:                server_clear_marked();
                    751:
1.286     nicm      752:        window_pane_stack_remove(&w->last_panes, wp);
1.57      nicm      753:        if (wp == w->active) {
1.286     nicm      754:                w->active = TAILQ_FIRST(&w->last_panes);
1.58      nicm      755:                if (w->active == NULL) {
                    756:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    757:                        if (w->active == NULL)
                    758:                                w->active = TAILQ_NEXT(wp, entry);
                    759:                }
1.193     nicm      760:                if (w->active != NULL) {
1.286     nicm      761:                        window_pane_stack_remove(&w->last_panes, w->active);
1.151     nicm      762:                        w->active->flags |= PANE_CHANGED;
1.193     nicm      763:                        notify_window("window-pane-changed", w);
1.278     nicm      764:                        window_update_focus(w);
1.193     nicm      765:                }
1.286     nicm      766:        }
1.105     nicm      767: }
                    768:
                    769: void
                    770: window_remove_pane(struct window *w, struct window_pane *wp)
                    771: {
                    772:        window_lost_pane(w, wp);
1.1       nicm      773:
                    774:        TAILQ_REMOVE(&w->panes, wp, entry);
                    775:        window_pane_destroy(wp);
                    776: }
                    777:
                    778: struct window_pane *
                    779: window_pane_at_index(struct window *w, u_int idx)
                    780: {
                    781:        struct window_pane      *wp;
                    782:        u_int                    n;
                    783:
1.146     nicm      784:        n = options_get_number(w->options, "pane-base-index");
1.1       nicm      785:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    786:                if (n == idx)
                    787:                        return (wp);
                    788:                n++;
                    789:        }
                    790:        return (NULL);
1.53      nicm      791: }
                    792:
                    793: struct window_pane *
                    794: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    795: {
                    796:        for (; n > 0; n--) {
                    797:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    798:                        wp = TAILQ_FIRST(&w->panes);
                    799:        }
                    800:
                    801:        return (wp);
                    802: }
                    803:
                    804: struct window_pane *
                    805: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    806:     u_int n)
                    807: {
                    808:        for (; n > 0; n--) {
                    809:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    810:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    811:        }
                    812:
                    813:        return (wp);
1.13      nicm      814: }
                    815:
1.69      nicm      816: int
                    817: window_pane_index(struct window_pane *wp, u_int *i)
1.13      nicm      818: {
                    819:        struct window_pane      *wq;
1.69      nicm      820:        struct window           *w = wp->window;
1.13      nicm      821:
1.146     nicm      822:        *i = options_get_number(w->options, "pane-base-index");
1.13      nicm      823:        TAILQ_FOREACH(wq, &w->panes, entry) {
1.69      nicm      824:                if (wp == wq) {
                    825:                        return (0);
                    826:                }
                    827:                (*i)++;
1.13      nicm      828:        }
1.69      nicm      829:
                    830:        return (-1);
1.1       nicm      831: }
                    832:
                    833: u_int
                    834: window_count_panes(struct window *w)
                    835: {
                    836:        struct window_pane      *wp;
                    837:        u_int                    n;
                    838:
                    839:        n = 0;
                    840:        TAILQ_FOREACH(wp, &w->panes, entry)
                    841:                n++;
                    842:        return (n);
                    843: }
                    844:
                    845: void
                    846: window_destroy_panes(struct window *w)
                    847: {
                    848:        struct window_pane      *wp;
                    849:
1.286     nicm      850:        while (!TAILQ_EMPTY(&w->last_panes)) {
                    851:                wp = TAILQ_FIRST(&w->last_panes);
                    852:                window_pane_stack_remove(&w->last_panes, wp);
                    853:        }
                    854:
1.1       nicm      855:        while (!TAILQ_EMPTY(&w->panes)) {
                    856:                wp = TAILQ_FIRST(&w->panes);
                    857:                TAILQ_REMOVE(&w->panes, wp, entry);
                    858:                window_pane_destroy(wp);
                    859:        }
1.61      nicm      860: }
                    861:
1.188     nicm      862: const char *
1.268     nicm      863: window_printable_flags(struct winlink *wl, int escape)
1.61      nicm      864: {
1.189     nicm      865:        struct session  *s = wl->session;
                    866:        static char      flags[32];
                    867:        int              pos;
1.61      nicm      868:
                    869:        pos = 0;
1.268     nicm      870:        if (wl->flags & WINLINK_ACTIVITY) {
1.61      nicm      871:                flags[pos++] = '#';
1.268     nicm      872:                if (escape)
                    873:                        flags[pos++] = '#';
                    874:        }
1.61      nicm      875:        if (wl->flags & WINLINK_BELL)
                    876:                flags[pos++] = '!';
                    877:        if (wl->flags & WINLINK_SILENCE)
                    878:                flags[pos++] = '~';
                    879:        if (wl == s->curw)
                    880:                flags[pos++] = '*';
                    881:        if (wl == TAILQ_FIRST(&s->lastw))
                    882:                flags[pos++] = '-';
1.152     nicm      883:        if (server_check_marked() && wl == marked_pane.wl)
1.132     nicm      884:                flags[pos++] = 'M';
1.93      nicm      885:        if (wl->window->flags & WINDOW_ZOOMED)
                    886:                flags[pos++] = 'Z';
1.61      nicm      887:        flags[pos] = '\0';
1.188     nicm      888:        return (flags);
1.1       nicm      889: }
                    890:
1.123     nicm      891: struct window_pane *
                    892: window_pane_find_by_id_str(const char *s)
                    893: {
                    894:        const char      *errstr;
                    895:        u_int            id;
                    896:
                    897:        if (*s != '%')
                    898:                return (NULL);
                    899:
                    900:        id = strtonum(s + 1, 0, UINT_MAX, &errstr);
                    901:        if (errstr != NULL)
                    902:                return (NULL);
                    903:        return (window_pane_find_by_id(id));
                    904: }
                    905:
1.64      nicm      906: struct window_pane *
                    907: window_pane_find_by_id(u_int id)
                    908: {
                    909:        struct window_pane      wp;
                    910:
                    911:        wp.id = id;
                    912:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
                    913: }
                    914:
1.169     nicm      915: static struct window_pane *
1.1       nicm      916: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    917: {
                    918:        struct window_pane      *wp;
1.140     nicm      919:        char                     host[HOST_NAME_MAX + 1];
1.1       nicm      920:
                    921:        wp = xcalloc(1, sizeof *wp);
                    922:        wp->window = w;
1.234     nicm      923:        wp->options = options_create(w->options);
                    924:        wp->flags = PANE_STYLECHANGED;
1.1       nicm      925:
1.71      nicm      926:        wp->id = next_window_pane_id++;
1.64      nicm      927:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
                    928:
1.1       nicm      929:        wp->fd = -1;
                    930:
1.218     nicm      931:        TAILQ_INIT(&wp->modes);
1.14      nicm      932:
1.272     nicm      933:        TAILQ_INIT (&wp->resize_queue);
1.1       nicm      934:
1.265     nicm      935:        wp->sx = sx;
                    936:        wp->sy = sy;
1.1       nicm      937:
1.32      nicm      938:        wp->pipe_fd = -1;
                    939:
1.273     nicm      940:        colour_palette_init(&wp->palette);
1.275     nicm      941:        colour_palette_from_option(&wp->palette, wp->options);
                    942:
1.1       nicm      943:        screen_init(&wp->base, sx, sy, hlimit);
                    944:        wp->screen = &wp->base;
1.281     nicm      945:        window_pane_default_cursor(wp);
1.159     nicm      946:
                    947:        screen_init(&wp->status_screen, 1, 1, 0);
1.140     nicm      948:
                    949:        if (gethostname(host, sizeof host) == 0)
                    950:                screen_set_title(&wp->base, host);
1.1       nicm      951:
                    952:        return (wp);
                    953: }
                    954:
1.169     nicm      955: static void
1.1       nicm      956: window_pane_destroy(struct window_pane *wp)
                    957: {
1.272     nicm      958:        struct window_pane_resize       *r;
                    959:        struct window_pane_resize       *r1;
                    960:
1.218     nicm      961:        window_pane_reset_mode_all(wp);
1.194     nicm      962:        free(wp->searchstr);
1.55      nicm      963:
1.37      nicm      964:        if (wp->fd != -1) {
1.70      nicm      965:                bufferevent_free(wp->event);
1.1       nicm      966:                close(wp->fd);
1.37      nicm      967:        }
1.251     nicm      968:        if (wp->ictx != NULL)
                    969:                input_free(wp->ictx);
1.225     nicm      970:
                    971:        screen_free(&wp->status_screen);
1.1       nicm      972:
                    973:        screen_free(&wp->base);
                    974:
1.32      nicm      975:        if (wp->pipe_fd != -1) {
1.70      nicm      976:                bufferevent_free(wp->pipe_event);
1.32      nicm      977:                close(wp->pipe_fd);
                    978:        }
1.167     nicm      979:
                    980:        if (event_initialized(&wp->resize_timer))
                    981:                event_del(&wp->resize_timer);
1.272     nicm      982:        TAILQ_FOREACH_SAFE(r, &wp->resize_queue, entry, r1) {
                    983:                TAILQ_REMOVE(&wp->resize_queue, r, entry);
                    984:                free(r);
                    985:        }
1.32      nicm      986:
1.64      nicm      987:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    988:
1.234     nicm      989:        options_free(wp->options);
1.147     nicm      990:        free((void *)wp->cwd);
1.82      nicm      991:        free(wp->shell);
1.110     nicm      992:        cmd_free_argv(wp->argc, wp->argv);
1.273     nicm      993:        colour_palette_free(&wp->palette);
1.82      nicm      994:        free(wp);
1.1       nicm      995: }
                    996:
1.166     nicm      997: static void
1.149     nicm      998: window_pane_read_callback(__unused struct bufferevent *bufev, void *data)
1.37      nicm      999: {
1.263     nicm     1000:        struct window_pane              *wp = data;
                   1001:        struct evbuffer                 *evb = wp->event->input;
                   1002:        struct window_pane_offset       *wpo = &wp->pipe_offset;
                   1003:        size_t                           size = EVBUFFER_LENGTH(evb);
                   1004:        char                            *new_data;
                   1005:        size_t                           new_size;
                   1006:        struct client                   *c;
                   1007:
                   1008:        if (wp->pipe_fd != -1) {
                   1009:                new_data = window_pane_get_new_data(wp, wpo, &new_size);
                   1010:                if (new_size > 0) {
                   1011:                        bufferevent_write(wp->pipe_event, new_data, new_size);
1.264     nicm     1012:                        window_pane_update_used_data(wp, wpo, new_size);
1.263     nicm     1013:                }
1.46      nicm     1014:        }
                   1015:
1.183     nicm     1016:        log_debug("%%%u has %zu bytes", wp->id, size);
1.263     nicm     1017:        TAILQ_FOREACH(c, &clients, entry) {
1.264     nicm     1018:                if (c->session != NULL && (c->flags & CLIENT_CONTROL))
1.263     nicm     1019:                        control_write_output(c, wp);
                   1020:        }
1.250     nicm     1021:        input_parse_pane(wp);
1.264     nicm     1022:        bufferevent_disable(wp->event, EV_READ);
1.37      nicm     1023: }
                   1024:
1.166     nicm     1025: static void
1.149     nicm     1026: window_pane_error_callback(__unused struct bufferevent *bufev,
                   1027:     __unused short what, void *data)
1.37      nicm     1028: {
                   1029:        struct window_pane *wp = data;
                   1030:
1.200     nicm     1031:        log_debug("%%%u error", wp->id);
1.201     nicm     1032:        wp->flags |= PANE_EXITED;
1.200     nicm     1033:
                   1034:        if (window_pane_destroy_ready(wp))
                   1035:                server_destroy_pane(wp, 1);
1.37      nicm     1036: }
                   1037:
                   1038: void
1.226     nicm     1039: window_pane_set_event(struct window_pane *wp)
                   1040: {
                   1041:        setblocking(wp->fd, 0);
                   1042:
                   1043:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback,
                   1044:            NULL, window_pane_error_callback, wp);
1.282     nicm     1045:        if (wp->event == NULL)
                   1046:                fatalx("out of memory");
1.273     nicm     1047:        wp->ictx = input_init(wp, wp->event, &wp->palette);
1.226     nicm     1048:
                   1049:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
                   1050: }
                   1051:
                   1052: void
1.1       nicm     1053: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                   1054: {
1.218     nicm     1055:        struct window_mode_entry        *wme;
1.272     nicm     1056:        struct window_pane_resize       *r;
1.218     nicm     1057:
1.1       nicm     1058:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm     1059:                return;
1.272     nicm     1060:
1.280     nicm     1061:        r = xmalloc(sizeof *r);
1.272     nicm     1062:        r->sx = sx;
                   1063:        r->sy = sy;
                   1064:        r->osx = wp->sx;
                   1065:        r->osy = wp->sy;
                   1066:        TAILQ_INSERT_TAIL (&wp->resize_queue, r, entry);
                   1067:
1.1       nicm     1068:        wp->sx = sx;
                   1069:        wp->sy = sy;
                   1070:
1.238     nicm     1071:        log_debug("%s: %%%u resize %ux%u", __func__, wp->id, sx, sy);
1.252     nicm     1072:        screen_resize(&wp->base, sx, sy, wp->base.saved_grid == NULL);
1.218     nicm     1073:
                   1074:        wme = TAILQ_FIRST(&wp->modes);
                   1075:        if (wme != NULL && wme->mode->resize != NULL)
                   1076:                wme->mode->resize(wme, sx, sy);
1.180     nicm     1077: }
                   1078:
1.1       nicm     1079: int
1.255     nicm     1080: window_pane_set_mode(struct window_pane *wp, struct window_pane *swp,
                   1081:     const struct window_mode *mode, struct cmd_find_state *fs,
                   1082:     struct args *args)
1.1       nicm     1083: {
1.218     nicm     1084:        struct window_mode_entry        *wme;
1.1       nicm     1085:
1.218     nicm     1086:        if (!TAILQ_EMPTY(&wp->modes) && TAILQ_FIRST(&wp->modes)->mode == mode)
1.1       nicm     1087:                return (1);
1.217     nicm     1088:
1.218     nicm     1089:        TAILQ_FOREACH(wme, &wp->modes, entry) {
                   1090:                if (wme->mode == mode)
                   1091:                        break;
                   1092:        }
1.223     nicm     1093:        if (wme != NULL) {
1.218     nicm     1094:                TAILQ_REMOVE(&wp->modes, wme, entry);
1.223     nicm     1095:                TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
                   1096:        } else {
1.218     nicm     1097:                wme = xcalloc(1, sizeof *wme);
                   1098:                wme->wp = wp;
1.255     nicm     1099:                wme->swp = swp;
1.218     nicm     1100:                wme->mode = mode;
                   1101:                wme->prefix = 1;
1.223     nicm     1102:                TAILQ_INSERT_HEAD(&wp->modes, wme, entry);
1.218     nicm     1103:                wme->screen = wme->mode->init(wme, fs, args);
                   1104:        }
1.162     nicm     1105:
1.218     nicm     1106:        wp->screen = wme->screen;
1.142     nicm     1107:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1108:
1.259     nicm     1109:        server_redraw_window_borders(wp->window);
1.157     nicm     1110:        server_status_window(wp->window);
1.193     nicm     1111:        notify_pane("pane-mode-changed", wp);
1.218     nicm     1112:
1.1       nicm     1113:        return (0);
                   1114: }
                   1115:
                   1116: void
                   1117: window_pane_reset_mode(struct window_pane *wp)
                   1118: {
1.218     nicm     1119:        struct window_mode_entry        *wme, *next;
                   1120:
                   1121:        if (TAILQ_EMPTY(&wp->modes))
1.1       nicm     1122:                return;
                   1123:
1.218     nicm     1124:        wme = TAILQ_FIRST(&wp->modes);
                   1125:        TAILQ_REMOVE(&wp->modes, wme, entry);
                   1126:        wme->mode->free(wme);
                   1127:        free(wme);
                   1128:
                   1129:        next = TAILQ_FIRST(&wp->modes);
                   1130:        if (next == NULL) {
1.285     nicm     1131:                wp->flags &= ~PANE_UNSEENCHANGES;
1.218     nicm     1132:                log_debug("%s: no next mode", __func__);
                   1133:                wp->screen = &wp->base;
                   1134:        } else {
                   1135:                log_debug("%s: next mode is %s", __func__, next->mode->name);
                   1136:                wp->screen = next->screen;
1.230     nicm     1137:                if (next->mode->resize != NULL)
1.218     nicm     1138:                        next->mode->resize(next, wp->sx, wp->sy);
                   1139:        }
1.142     nicm     1140:        wp->flags |= (PANE_REDRAW|PANE_CHANGED);
1.157     nicm     1141:
1.259     nicm     1142:        server_redraw_window_borders(wp->window);
1.157     nicm     1143:        server_status_window(wp->window);
1.193     nicm     1144:        notify_pane("pane-mode-changed", wp);
1.1       nicm     1145: }
                   1146:
                   1147: void
1.218     nicm     1148: window_pane_reset_mode_all(struct window_pane *wp)
                   1149: {
                   1150:        while (!TAILQ_EMPTY(&wp->modes))
                   1151:                window_pane_reset_mode(wp);
                   1152: }
                   1153:
1.267     nicm     1154: static void
                   1155: window_pane_copy_key(struct window_pane *wp, key_code key)
                   1156: {
                   1157:        struct window_pane      *loop;
                   1158:
                   1159:        TAILQ_FOREACH(loop, &wp->window->panes, entry) {
                   1160:                if (loop != wp &&
                   1161:                    TAILQ_EMPTY(&loop->modes) &&
                   1162:                    loop->fd != -1 &&
                   1163:                    (~loop->flags & PANE_INPUTOFF) &&
                   1164:                    window_pane_visible(loop) &&
                   1165:                    options_get_number(loop->options, "synchronize-panes"))
                   1166:                        input_key_pane(loop, key, NULL);
                   1167:        }
                   1168: }
                   1169:
1.247     nicm     1170: int
1.118     nicm     1171: window_pane_key(struct window_pane *wp, struct client *c, struct session *s,
1.216     nicm     1172:     struct winlink *wl, key_code key, struct mouse_event *m)
1.1       nicm     1173: {
1.218     nicm     1174:        struct window_mode_entry        *wme;
1.3       nicm     1175:
1.118     nicm     1176:        if (KEYC_IS_MOUSE(key) && m == NULL)
1.247     nicm     1177:                return (-1);
1.118     nicm     1178:
1.218     nicm     1179:        wme = TAILQ_FIRST(&wp->modes);
1.217     nicm     1180:        if (wme != NULL) {
1.261     nicm     1181:                if (wme->mode->key != NULL && c != NULL) {
                   1182:                        key &= ~KEYC_MASK_FLAGS;
                   1183:                        wme->mode->key(wme, c, s, wl, key, m);
                   1184:                }
1.247     nicm     1185:                return (0);
1.30      nicm     1186:        }
1.27      nicm     1187:
1.113     nicm     1188:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.247     nicm     1189:                return (0);
1.113     nicm     1190:
1.250     nicm     1191:        if (input_key_pane(wp, key, m) != 0)
1.247     nicm     1192:                return (-1);
1.118     nicm     1193:
                   1194:        if (KEYC_IS_MOUSE(key))
1.247     nicm     1195:                return (0);
1.267     nicm     1196:        if (options_get_number(wp->options, "synchronize-panes"))
                   1197:                window_pane_copy_key(wp, key);
1.247     nicm     1198:        return (0);
1.10      nicm     1199: }
                   1200:
                   1201: int
1.205     nicm     1202: window_pane_visible(struct window_pane *wp)
1.10      nicm     1203: {
1.213     nicm     1204:        if (~wp->window->flags & WINDOW_ZOOMED)
                   1205:                return (1);
                   1206:        return (wp == wp->window->active);
1.288     nicm     1207: }
                   1208:
                   1209: int
                   1210: window_pane_exited(struct window_pane *wp)
                   1211: {
                   1212:        return (wp->fd == -1 || (wp->flags & PANE_EXITED));
1.1       nicm     1213: }
                   1214:
1.195     nicm     1215: u_int
1.232     nicm     1216: window_pane_search(struct window_pane *wp, const char *term, int regex,
                   1217:     int ignore)
1.195     nicm     1218: {
                   1219:        struct screen   *s = &wp->base;
1.232     nicm     1220:        regex_t          r;
                   1221:        char            *new = NULL, *line;
1.195     nicm     1222:        u_int            i;
1.232     nicm     1223:        int              flags = 0, found;
1.236     nicm     1224:        size_t           n;
1.195     nicm     1225:
1.232     nicm     1226:        if (!regex) {
                   1227:                if (ignore)
                   1228:                        flags |= FNM_CASEFOLD;
                   1229:                xasprintf(&new, "*%s*", term);
                   1230:        } else {
                   1231:                if (ignore)
                   1232:                        flags |= REG_ICASE;
                   1233:                if (regcomp(&r, term, flags|REG_EXTENDED) != 0)
                   1234:                        return (0);
                   1235:        }
1.195     nicm     1236:
                   1237:        for (i = 0; i < screen_size_y(s); i++) {
                   1238:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.236     nicm     1239:                for (n = strlen(line); n > 0; n--) {
                   1240:                        if (!isspace((u_char)line[n - 1]))
                   1241:                                break;
                   1242:                        line[n - 1] = '\0';
                   1243:                }
                   1244:                log_debug("%s: %s", __func__, line);
1.232     nicm     1245:                if (!regex)
1.254     nicm     1246:                        found = (fnmatch(new, line, flags) == 0);
1.232     nicm     1247:                else
                   1248:                        found = (regexec(&r, line, 0, NULL, 0) == 0);
                   1249:                free(line);
                   1250:                if (found)
1.195     nicm     1251:                        break;
                   1252:        }
1.232     nicm     1253:        if (!regex)
                   1254:                free(new);
                   1255:        else
                   1256:                regfree(&r);
1.195     nicm     1257:
                   1258:        if (i == screen_size_y(s))
                   1259:                return (0);
                   1260:        return (i + 1);
1.45      nicm     1261: }
                   1262:
1.109     nicm     1263: /* Get MRU pane from a list. */
1.166     nicm     1264: static struct window_pane *
1.126     nicm     1265: window_pane_choose_best(struct window_pane **list, u_int size)
1.109     nicm     1266: {
                   1267:        struct window_pane      *next, *best;
                   1268:        u_int                    i;
                   1269:
1.126     nicm     1270:        if (size == 0)
1.109     nicm     1271:                return (NULL);
                   1272:
1.126     nicm     1273:        best = list[0];
                   1274:        for (i = 1; i < size; i++) {
                   1275:                next = list[i];
1.109     nicm     1276:                if (next->active_point > best->active_point)
                   1277:                        best = next;
                   1278:        }
                   1279:        return (best);
                   1280: }
                   1281:
                   1282: /*
                   1283:  * Find the pane directly above another. We build a list of those adjacent to
                   1284:  * top edge and then choose the best.
                   1285:  */
1.45      nicm     1286: struct window_pane *
                   1287: window_pane_find_up(struct window_pane *wp)
                   1288: {
1.237     nicm     1289:        struct window           *w;
1.126     nicm     1290:        struct window_pane      *next, *best, **list;
                   1291:        u_int                    edge, left, right, end, size;
1.187     nicm     1292:        int                      status, found;
1.45      nicm     1293:
1.213     nicm     1294:        if (wp == NULL)
1.45      nicm     1295:                return (NULL);
1.237     nicm     1296:        w = wp->window;
                   1297:        status = options_get_number(w->options, "pane-border-status");
1.126     nicm     1298:
                   1299:        list = NULL;
                   1300:        size = 0;
1.109     nicm     1301:
                   1302:        edge = wp->yoff;
1.237     nicm     1303:        if (status == PANE_STATUS_TOP) {
                   1304:                if (edge == 1)
                   1305:                        edge = w->sy + 1;
                   1306:        } else if (status == PANE_STATUS_BOTTOM) {
                   1307:                if (edge == 0)
                   1308:                        edge = w->sy;
                   1309:        } else {
                   1310:                if (edge == 0)
                   1311:                        edge = w->sy + 1;
                   1312:        }
1.45      nicm     1313:
                   1314:        left = wp->xoff;
1.109     nicm     1315:        right = wp->xoff + wp->sx;
1.45      nicm     1316:
1.237     nicm     1317:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1318:                if (next == wp)
1.45      nicm     1319:                        continue;
1.109     nicm     1320:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1321:                        continue;
1.109     nicm     1322:                end = next->xoff + next->sx - 1;
                   1323:
                   1324:                found = 0;
                   1325:                if (next->xoff < left && end > right)
                   1326:                        found = 1;
                   1327:                else if (next->xoff >= left && next->xoff <= right)
                   1328:                        found = 1;
                   1329:                else if (end >= left && end <= right)
                   1330:                        found = 1;
1.126     nicm     1331:                if (!found)
                   1332:                        continue;
                   1333:                list = xreallocarray(list, size + 1, sizeof *list);
                   1334:                list[size++] = next;
1.109     nicm     1335:        }
                   1336:
1.126     nicm     1337:        best = window_pane_choose_best(list, size);
                   1338:        free(list);
1.109     nicm     1339:        return (best);
1.45      nicm     1340: }
                   1341:
                   1342: /* Find the pane directly below another. */
                   1343: struct window_pane *
                   1344: window_pane_find_down(struct window_pane *wp)
                   1345: {
1.237     nicm     1346:        struct window           *w;
1.126     nicm     1347:        struct window_pane      *next, *best, **list;
                   1348:        u_int                    edge, left, right, end, size;
1.187     nicm     1349:        int                      status, found;
1.45      nicm     1350:
1.213     nicm     1351:        if (wp == NULL)
1.45      nicm     1352:                return (NULL);
1.237     nicm     1353:        w = wp->window;
                   1354:        status = options_get_number(w->options, "pane-border-status");
1.126     nicm     1355:
                   1356:        list = NULL;
                   1357:        size = 0;
1.109     nicm     1358:
                   1359:        edge = wp->yoff + wp->sy + 1;
1.237     nicm     1360:        if (status == PANE_STATUS_TOP) {
                   1361:                if (edge >= w->sy)
                   1362:                        edge = 1;
                   1363:        } else if (status == PANE_STATUS_BOTTOM) {
                   1364:                if (edge >= w->sy - 1)
                   1365:                        edge = 0;
                   1366:        } else {
1.239     nicm     1367:                if (edge >= w->sy)
1.237     nicm     1368:                        edge = 0;
                   1369:        }
1.45      nicm     1370:
                   1371:        left = wp->xoff;
1.109     nicm     1372:        right = wp->xoff + wp->sx;
1.45      nicm     1373:
1.237     nicm     1374:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1375:                if (next == wp)
1.45      nicm     1376:                        continue;
1.109     nicm     1377:                if (next->yoff != edge)
1.45      nicm     1378:                        continue;
1.109     nicm     1379:                end = next->xoff + next->sx - 1;
                   1380:
                   1381:                found = 0;
                   1382:                if (next->xoff < left && end > right)
                   1383:                        found = 1;
                   1384:                else if (next->xoff >= left && next->xoff <= right)
                   1385:                        found = 1;
                   1386:                else if (end >= left && end <= right)
                   1387:                        found = 1;
1.126     nicm     1388:                if (!found)
                   1389:                        continue;
                   1390:                list = xreallocarray(list, size + 1, sizeof *list);
                   1391:                list[size++] = next;
1.45      nicm     1392:        }
1.109     nicm     1393:
1.126     nicm     1394:        best = window_pane_choose_best(list, size);
                   1395:        free(list);
1.109     nicm     1396:        return (best);
1.45      nicm     1397: }
                   1398:
1.109     nicm     1399: /* Find the pane directly to the left of another. */
1.45      nicm     1400: struct window_pane *
                   1401: window_pane_find_left(struct window_pane *wp)
                   1402: {
1.237     nicm     1403:        struct window           *w;
1.126     nicm     1404:        struct window_pane      *next, *best, **list;
                   1405:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1406:        int                      found;
1.45      nicm     1407:
1.213     nicm     1408:        if (wp == NULL)
1.45      nicm     1409:                return (NULL);
1.237     nicm     1410:        w = wp->window;
1.126     nicm     1411:
                   1412:        list = NULL;
                   1413:        size = 0;
1.109     nicm     1414:
                   1415:        edge = wp->xoff;
                   1416:        if (edge == 0)
1.237     nicm     1417:                edge = w->sx + 1;
1.45      nicm     1418:
                   1419:        top = wp->yoff;
1.109     nicm     1420:        bottom = wp->yoff + wp->sy;
1.45      nicm     1421:
1.237     nicm     1422:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1423:                if (next == wp)
1.45      nicm     1424:                        continue;
1.109     nicm     1425:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1426:                        continue;
1.109     nicm     1427:                end = next->yoff + next->sy - 1;
                   1428:
                   1429:                found = 0;
                   1430:                if (next->yoff < top && end > bottom)
                   1431:                        found = 1;
                   1432:                else if (next->yoff >= top && next->yoff <= bottom)
                   1433:                        found = 1;
                   1434:                else if (end >= top && end <= bottom)
                   1435:                        found = 1;
1.126     nicm     1436:                if (!found)
                   1437:                        continue;
                   1438:                list = xreallocarray(list, size + 1, sizeof *list);
                   1439:                list[size++] = next;
1.45      nicm     1440:        }
1.109     nicm     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:
1.109     nicm     1447: /* Find the pane directly to the right of another. */
1.45      nicm     1448: struct window_pane *
                   1449: window_pane_find_right(struct window_pane *wp)
                   1450: {
1.237     nicm     1451:        struct window           *w;
1.126     nicm     1452:        struct window_pane      *next, *best, **list;
                   1453:        u_int                    edge, top, bottom, end, size;
1.109     nicm     1454:        int                      found;
1.45      nicm     1455:
1.213     nicm     1456:        if (wp == NULL)
1.45      nicm     1457:                return (NULL);
1.237     nicm     1458:        w = wp->window;
1.126     nicm     1459:
                   1460:        list = NULL;
                   1461:        size = 0;
1.109     nicm     1462:
                   1463:        edge = wp->xoff + wp->sx + 1;
1.237     nicm     1464:        if (edge >= w->sx)
1.109     nicm     1465:                edge = 0;
1.45      nicm     1466:
                   1467:        top = wp->yoff;
1.109     nicm     1468:        bottom = wp->yoff + wp->sy;
1.45      nicm     1469:
1.237     nicm     1470:        TAILQ_FOREACH(next, &w->panes, entry) {
1.213     nicm     1471:                if (next == wp)
1.45      nicm     1472:                        continue;
1.109     nicm     1473:                if (next->xoff != edge)
1.45      nicm     1474:                        continue;
1.109     nicm     1475:                end = next->yoff + next->sy - 1;
                   1476:
                   1477:                found = 0;
                   1478:                if (next->yoff < top && end > bottom)
                   1479:                        found = 1;
                   1480:                else if (next->yoff >= top && next->yoff <= bottom)
                   1481:                        found = 1;
                   1482:                else if (end >= top && end <= bottom)
                   1483:                        found = 1;
1.126     nicm     1484:                if (!found)
                   1485:                        continue;
                   1486:                list = xreallocarray(list, size + 1, sizeof *list);
                   1487:                list[size++] = next;
1.109     nicm     1488:        }
                   1489:
1.126     nicm     1490:        best = window_pane_choose_best(list, size);
                   1491:        free(list);
1.109     nicm     1492:        return (best);
1.286     nicm     1493: }
                   1494:
                   1495: void
                   1496: window_pane_stack_push(struct window_panes *stack, struct window_pane *wp)
                   1497: {
                   1498:        if (wp != NULL) {
                   1499:                window_pane_stack_remove(stack, wp);
                   1500:                TAILQ_INSERT_HEAD(stack, wp, sentry);
                   1501:                wp->flags |= PANE_VISITED;
                   1502:        }
                   1503: }
                   1504:
                   1505: void
                   1506: window_pane_stack_remove(struct window_panes *stack, struct window_pane *wp)
                   1507: {
                   1508:        if (wp != NULL && (wp->flags & PANE_VISITED)) {
                   1509:                TAILQ_REMOVE(stack, wp, sentry);
                   1510:                wp->flags &= ~PANE_VISITED;
                   1511:        }
1.81      nicm     1512: }
                   1513:
                   1514: /* Clear alert flags for a winlink */
                   1515: void
                   1516: winlink_clear_flags(struct winlink *wl)
                   1517: {
1.174     nicm     1518:        struct winlink  *loop;
1.81      nicm     1519:
1.174     nicm     1520:        wl->window->flags &= ~WINDOW_ALERTFLAGS;
                   1521:        TAILQ_FOREACH(loop, &wl->window->winlinks, wentry) {
                   1522:                if ((loop->flags & WINLINK_ALERTFLAGS) != 0) {
                   1523:                        loop->flags &= ~WINLINK_ALERTFLAGS;
                   1524:                        server_status_session(loop->session);
1.81      nicm     1525:                }
                   1526:        }
1.134     nicm     1527: }
                   1528:
1.184     nicm     1529: /* Shuffle window indexes up. */
1.134     nicm     1530: int
1.266     nicm     1531: winlink_shuffle_up(struct session *s, struct winlink *wl, int before)
1.134     nicm     1532: {
                   1533:        int      idx, last;
                   1534:
1.226     nicm     1535:        if (wl == NULL)
                   1536:                return (-1);
1.266     nicm     1537:        if (before)
                   1538:                idx = wl->idx;
                   1539:        else
                   1540:                idx = wl->idx + 1;
1.134     nicm     1541:
                   1542:        /* Find the next free index. */
                   1543:        for (last = idx; last < INT_MAX; last++) {
                   1544:                if (winlink_find_by_index(&s->windows, last) == NULL)
                   1545:                        break;
                   1546:        }
                   1547:        if (last == INT_MAX)
                   1548:                return (-1);
                   1549:
                   1550:        /* Move everything from last - 1 to idx up a bit. */
                   1551:        for (; last > idx; last--) {
                   1552:                wl = winlink_find_by_index(&s->windows, last - 1);
1.266     nicm     1553:                RB_REMOVE(winlinks, &s->windows, wl);
                   1554:                wl->idx++;
                   1555:                RB_INSERT(winlinks, &s->windows, wl);
1.134     nicm     1556:        }
                   1557:
                   1558:        return (idx);
1.228     nicm     1559: }
                   1560:
                   1561: static void
1.246     nicm     1562: window_pane_input_callback(struct client *c, __unused const char *path,
                   1563:     int error, int closed, struct evbuffer *buffer, void *data)
1.228     nicm     1564: {
                   1565:        struct window_pane_input_data   *cdata = data;
                   1566:        struct window_pane              *wp;
1.246     nicm     1567:        u_char                          *buf = EVBUFFER_DATA(buffer);
                   1568:        size_t                           len = EVBUFFER_LENGTH(buffer);
1.228     nicm     1569:
                   1570:        wp = window_pane_find_by_id(cdata->wp);
1.283     nicm     1571:        if (cdata->file != NULL && (wp == NULL || c->flags & CLIENT_DEAD)) {
1.284     nicm     1572:                if (wp == NULL) {
                   1573:                        c->retval = 1;
1.243     nicm     1574:                        c->flags |= CLIENT_EXIT;
1.284     nicm     1575:                }
1.283     nicm     1576:                file_cancel(cdata->file);
                   1577:        } else if (cdata->file == NULL || closed || error != 0) {
1.246     nicm     1578:                cmdq_continue(cdata->item);
1.228     nicm     1579:                server_client_unref(c);
                   1580:                free(cdata);
1.283     nicm     1581:        } else
                   1582:                input_parse_buffer(wp, buf, len);
1.246     nicm     1583:        evbuffer_drain(buffer, len);
1.228     nicm     1584: }
                   1585:
                   1586: int
                   1587: window_pane_start_input(struct window_pane *wp, struct cmdq_item *item,
                   1588:     char **cause)
                   1589: {
1.256     nicm     1590:        struct client                   *c = cmdq_get_client(item);
1.228     nicm     1591:        struct window_pane_input_data   *cdata;
                   1592:
                   1593:        if (~wp->flags & PANE_EMPTY) {
                   1594:                *cause = xstrdup("pane is not empty");
                   1595:                return (-1);
                   1596:        }
1.277     nicm     1597:        if (c->flags & (CLIENT_DEAD|CLIENT_EXITED))
                   1598:                return (1);
                   1599:        if (c->session != NULL)
                   1600:                return (1);
1.228     nicm     1601:
                   1602:        cdata = xmalloc(sizeof *cdata);
                   1603:        cdata->item = item;
                   1604:        cdata->wp = wp->id;
1.283     nicm     1605:        cdata->file = file_read(c, "-", window_pane_input_callback, cdata);
1.246     nicm     1606:        c->references++;
                   1607:
                   1608:        return (0);
1.263     nicm     1609: }
                   1610:
                   1611: void *
                   1612: window_pane_get_new_data(struct window_pane *wp,
                   1613:     struct window_pane_offset *wpo, size_t *size)
                   1614: {
                   1615:        size_t  used = wpo->used - wp->base_offset;
                   1616:
                   1617:        *size = EVBUFFER_LENGTH(wp->event->input) - used;
                   1618:        return (EVBUFFER_DATA(wp->event->input) + used);
                   1619: }
                   1620:
                   1621: void
                   1622: window_pane_update_used_data(struct window_pane *wp,
1.264     nicm     1623:     struct window_pane_offset *wpo, size_t size)
1.263     nicm     1624: {
                   1625:        size_t  used = wpo->used - wp->base_offset;
                   1626:
                   1627:        if (size > EVBUFFER_LENGTH(wp->event->input) - used)
                   1628:                size = EVBUFFER_LENGTH(wp->event->input) - used;
                   1629:        wpo->used += size;
1.279     nicm     1630: }
                   1631:
                   1632: void
                   1633: window_set_fill_character(struct window *w)
                   1634: {
                   1635:        const char              *value;
                   1636:        struct utf8_data        *ud;
                   1637:
                   1638:        free(w->fill_character);
                   1639:        w->fill_character = NULL;
                   1640:
                   1641:        value = options_get_string(w->options, "fill-character");
                   1642:        if (*value != '\0' && utf8_isvalid(value)) {
                   1643:                ud = utf8_fromcstr(value);
                   1644:                if (ud != NULL && ud[0].width == 1)
                   1645:                        w->fill_character = ud;
                   1646:        }
1.281     nicm     1647: }
                   1648:
                   1649: void
                   1650: window_pane_default_cursor(struct window_pane *wp)
                   1651: {
                   1652:        struct screen   *s = wp->screen;
                   1653:        int              c;
                   1654:
                   1655:        c = options_get_number(wp->options, "cursor-colour");
                   1656:        s->default_ccolour = c;
                   1657:
                   1658:        c = options_get_number(wp->options, "cursor-style");
                   1659:        s->default_mode = 0;
                   1660:        screen_set_cursor_style(c, &s->default_cstyle, &s->default_mode);
1.1       nicm     1661: }