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

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