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

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