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

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