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

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