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

1.117   ! nicm        1: /* $OpenBSD: window.c,v 1.116 2015/02/09 12:47:18 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.1       nicm      390: window_set_active_pane(struct window *w, struct window_pane *wp)
                    391: {
1.59      nicm      392:        if (wp == w->active)
1.114     nicm      393:                return (0);
1.58      nicm      394:        w->last = w->active;
1.1       nicm      395:        w->active = wp;
1.10      nicm      396:        while (!window_pane_visible(w->active)) {
1.1       nicm      397:                w->active = TAILQ_PREV(w->active, window_panes, entry);
1.10      nicm      398:                if (w->active == NULL)
                    399:                        w->active = TAILQ_LAST(&w->panes, window_panes);
                    400:                if (w->active == wp)
1.114     nicm      401:                        return (1);
1.29      nicm      402:        }
1.109     nicm      403:        w->active->active_point = next_active_point++;
1.114     nicm      404:        return (1);
1.29      nicm      405: }
                    406:
1.66      nicm      407: struct window_pane *
                    408: window_get_active_at(struct window *w, u_int x, u_int y)
1.29      nicm      409: {
                    410:        struct window_pane      *wp;
                    411:
                    412:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.66      nicm      413:                if (!window_pane_visible(wp))
1.29      nicm      414:                        continue;
1.66      nicm      415:                if (x < wp->xoff || x > wp->xoff + wp->sx)
1.29      nicm      416:                        continue;
1.66      nicm      417:                if (y < wp->yoff || y > wp->yoff + wp->sy)
1.29      nicm      418:                        continue;
1.66      nicm      419:                return (wp);
                    420:        }
                    421:        return (NULL);
                    422: }
                    423:
                    424: void
                    425: window_set_active_at(struct window *w, u_int x, u_int y)
                    426: {
                    427:        struct window_pane      *wp;
                    428:
                    429:        wp = window_get_active_at(w, x, y);
                    430:        if (wp != NULL && wp != w->active)
1.29      nicm      431:                window_set_active_pane(w, wp);
1.66      nicm      432: }
                    433:
                    434: struct window_pane *
                    435: window_find_string(struct window *w, const char *s)
                    436: {
                    437:        u_int   x, y;
                    438:
                    439:        x = w->sx / 2;
                    440:        y = w->sy / 2;
                    441:
                    442:        if (strcasecmp(s, "top") == 0)
                    443:                y = 0;
                    444:        else if (strcasecmp(s, "bottom") == 0)
                    445:                y = w->sy - 1;
                    446:        else if (strcasecmp(s, "left") == 0)
                    447:                x = 0;
                    448:        else if (strcasecmp(s, "right") == 0)
                    449:                x = w->sx - 1;
                    450:        else if (strcasecmp(s, "top-left") == 0) {
                    451:                x = 0;
                    452:                y = 0;
                    453:        } else if (strcasecmp(s, "top-right") == 0) {
                    454:                x = w->sx - 1;
                    455:                y = 0;
                    456:        } else if (strcasecmp(s, "bottom-left") == 0) {
                    457:                x = 0;
                    458:                y = w->sy - 1;
                    459:        } else if (strcasecmp(s, "bottom-right") == 0) {
                    460:                x = w->sx - 1;
                    461:                y = w->sy - 1;
                    462:        } else
                    463:                return (NULL);
                    464:
                    465:        return (window_get_active_at(w, x, y));
1.1       nicm      466: }
                    467:
1.93      nicm      468: int
                    469: window_zoom(struct window_pane *wp)
                    470: {
                    471:        struct window           *w = wp->window;
                    472:        struct window_pane      *wp1;
                    473:
                    474:        if (w->flags & WINDOW_ZOOMED)
                    475:                return (-1);
                    476:
                    477:        if (!window_pane_visible(wp))
                    478:                return (-1);
1.94      nicm      479:
                    480:        if (window_count_panes(w) == 1)
                    481:                return (-1);
                    482:
1.93      nicm      483:        if (w->active != wp)
                    484:                window_set_active_pane(w, wp);
                    485:
                    486:        TAILQ_FOREACH(wp1, &w->panes, entry) {
                    487:                wp1->saved_layout_cell = wp1->layout_cell;
                    488:                wp1->layout_cell = NULL;
                    489:        }
                    490:
                    491:        w->saved_layout_root = w->layout_root;
                    492:        layout_init(w, wp);
                    493:        w->flags |= WINDOW_ZOOMED;
1.115     nicm      494:        notify_window_layout_changed(w);
1.93      nicm      495:
                    496:        return (0);
                    497: }
                    498:
                    499: int
                    500: window_unzoom(struct window *w)
                    501: {
1.97      nicm      502:        struct window_pane      *wp;
1.93      nicm      503:
                    504:        if (!(w->flags & WINDOW_ZOOMED))
                    505:                return (-1);
                    506:
                    507:        w->flags &= ~WINDOW_ZOOMED;
                    508:        layout_free(w);
                    509:        w->layout_root = w->saved_layout_root;
                    510:
1.97      nicm      511:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    512:                wp->layout_cell = wp->saved_layout_cell;
                    513:                wp->saved_layout_cell = NULL;
1.93      nicm      514:        }
                    515:        layout_fix_panes(w, w->sx, w->sy);
1.115     nicm      516:        notify_window_layout_changed(w);
1.93      nicm      517:
                    518:        return (0);
                    519: }
                    520:
1.1       nicm      521: struct window_pane *
1.16      nicm      522: window_add_pane(struct window *w, u_int hlimit)
1.1       nicm      523: {
                    524:        struct window_pane      *wp;
                    525:
1.14      nicm      526:        wp = window_pane_create(w, w->sx, w->sy, hlimit);
1.1       nicm      527:        if (TAILQ_EMPTY(&w->panes))
                    528:                TAILQ_INSERT_HEAD(&w->panes, wp, entry);
                    529:        else
                    530:                TAILQ_INSERT_AFTER(&w->panes, w->active, wp, entry);
                    531:        return (wp);
                    532: }
                    533:
                    534: void
1.105     nicm      535: window_lost_pane(struct window *w, struct window_pane *wp)
1.1       nicm      536: {
1.57      nicm      537:        if (wp == w->active) {
1.58      nicm      538:                w->active = w->last;
                    539:                w->last = NULL;
                    540:                if (w->active == NULL) {
                    541:                        w->active = TAILQ_PREV(wp, window_panes, entry);
                    542:                        if (w->active == NULL)
                    543:                                w->active = TAILQ_NEXT(wp, entry);
                    544:                }
                    545:        } else if (wp == w->last)
                    546:                w->last = NULL;
1.105     nicm      547: }
                    548:
                    549: void
                    550: window_remove_pane(struct window *w, struct window_pane *wp)
                    551: {
                    552:        window_lost_pane(w, wp);
1.1       nicm      553:
                    554:        TAILQ_REMOVE(&w->panes, wp, entry);
                    555:        window_pane_destroy(wp);
                    556: }
                    557:
                    558: struct window_pane *
                    559: window_pane_at_index(struct window *w, u_int idx)
                    560: {
                    561:        struct window_pane      *wp;
                    562:        u_int                    n;
                    563:
1.67      nicm      564:        n = options_get_number(&w->options, "pane-base-index");
1.1       nicm      565:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    566:                if (n == idx)
                    567:                        return (wp);
                    568:                n++;
                    569:        }
                    570:        return (NULL);
1.53      nicm      571: }
                    572:
                    573: struct window_pane *
                    574: window_pane_next_by_number(struct window *w, struct window_pane *wp, u_int n)
                    575: {
                    576:        for (; n > 0; n--) {
                    577:                if ((wp = TAILQ_NEXT(wp, entry)) == NULL)
                    578:                        wp = TAILQ_FIRST(&w->panes);
                    579:        }
                    580:
                    581:        return (wp);
                    582: }
                    583:
                    584: struct window_pane *
                    585: window_pane_previous_by_number(struct window *w, struct window_pane *wp,
                    586:     u_int n)
                    587: {
                    588:        for (; n > 0; n--) {
                    589:                if ((wp = TAILQ_PREV(wp, window_panes, entry)) == NULL)
                    590:                        wp = TAILQ_LAST(&w->panes, window_panes);
                    591:        }
                    592:
                    593:        return (wp);
1.13      nicm      594: }
                    595:
1.69      nicm      596: int
                    597: window_pane_index(struct window_pane *wp, u_int *i)
1.13      nicm      598: {
                    599:        struct window_pane      *wq;
1.69      nicm      600:        struct window           *w = wp->window;
1.13      nicm      601:
1.69      nicm      602:        *i = options_get_number(&w->options, "pane-base-index");
1.13      nicm      603:        TAILQ_FOREACH(wq, &w->panes, entry) {
1.69      nicm      604:                if (wp == wq) {
                    605:                        return (0);
                    606:                }
                    607:                (*i)++;
1.13      nicm      608:        }
1.69      nicm      609:
                    610:        return (-1);
1.1       nicm      611: }
                    612:
                    613: u_int
                    614: window_count_panes(struct window *w)
                    615: {
                    616:        struct window_pane      *wp;
                    617:        u_int                    n;
                    618:
                    619:        n = 0;
                    620:        TAILQ_FOREACH(wp, &w->panes, entry)
                    621:                n++;
                    622:        return (n);
                    623: }
                    624:
                    625: void
                    626: window_destroy_panes(struct window *w)
                    627: {
                    628:        struct window_pane      *wp;
                    629:
                    630:        while (!TAILQ_EMPTY(&w->panes)) {
                    631:                wp = TAILQ_FIRST(&w->panes);
                    632:                TAILQ_REMOVE(&w->panes, wp, entry);
                    633:                window_pane_destroy(wp);
                    634:        }
1.61      nicm      635: }
                    636:
                    637: /* Return list of printable window flag symbols. No flags is just a space. */
                    638: char *
                    639: window_printable_flags(struct session *s, struct winlink *wl)
                    640: {
                    641:        char    flags[BUFSIZ];
                    642:        int     pos;
                    643:
                    644:        pos = 0;
                    645:        if (wl->flags & WINLINK_ACTIVITY)
                    646:                flags[pos++] = '#';
                    647:        if (wl->flags & WINLINK_BELL)
                    648:                flags[pos++] = '!';
                    649:        if (wl->flags & WINLINK_SILENCE)
                    650:                flags[pos++] = '~';
                    651:        if (wl == s->curw)
                    652:                flags[pos++] = '*';
                    653:        if (wl == TAILQ_FIRST(&s->lastw))
                    654:                flags[pos++] = '-';
1.93      nicm      655:        if (wl->window->flags & WINDOW_ZOOMED)
                    656:                flags[pos++] = 'Z';
1.61      nicm      657:        if (pos == 0)
                    658:                flags[pos++] = ' ';
                    659:        flags[pos] = '\0';
                    660:        return (xstrdup(flags));
1.1       nicm      661: }
                    662:
1.64      nicm      663: /* Find pane in global tree by id. */
                    664: struct window_pane *
                    665: window_pane_find_by_id(u_int id)
                    666: {
                    667:        struct window_pane      wp;
                    668:
                    669:        wp.id = id;
                    670:        return (RB_FIND(window_pane_tree, &all_window_panes, &wp));
                    671: }
                    672:
1.1       nicm      673: struct window_pane *
                    674: window_pane_create(struct window *w, u_int sx, u_int sy, u_int hlimit)
                    675: {
                    676:        struct window_pane      *wp;
                    677:
                    678:        wp = xcalloc(1, sizeof *wp);
                    679:        wp->window = w;
                    680:
1.71      nicm      681:        wp->id = next_window_pane_id++;
1.64      nicm      682:        RB_INSERT(window_pane_tree, &all_window_panes, wp);
                    683:
1.110     nicm      684:        wp->argc = 0;
                    685:        wp->argv = NULL;
1.23      nicm      686:        wp->shell = NULL;
1.99      nicm      687:        wp->cwd = -1;
1.1       nicm      688:
                    689:        wp->fd = -1;
1.37      nicm      690:        wp->event = NULL;
1.1       nicm      691:
                    692:        wp->mode = NULL;
1.14      nicm      693:
                    694:        wp->layout_cell = NULL;
1.1       nicm      695:
                    696:        wp->xoff = 0;
1.42      nicm      697:        wp->yoff = 0;
1.1       nicm      698:
                    699:        wp->sx = sx;
                    700:        wp->sy = sy;
                    701:
1.32      nicm      702:        wp->pipe_fd = -1;
                    703:        wp->pipe_off = 0;
1.36      nicm      704:        wp->pipe_event = NULL;
1.32      nicm      705:
1.9       nicm      706:        wp->saved_grid = NULL;
1.117   ! nicm      707:
        !           708:        memcpy(&wp->colgc, &grid_default_cell, sizeof wp->colgc);
1.9       nicm      709:
1.1       nicm      710:        screen_init(&wp->base, sx, sy, hlimit);
                    711:        wp->screen = &wp->base;
                    712:
                    713:        input_init(wp);
                    714:
                    715:        return (wp);
                    716: }
                    717:
                    718: void
                    719: window_pane_destroy(struct window_pane *wp)
                    720: {
1.55      nicm      721:        window_pane_reset_mode(wp);
                    722:
1.76      nicm      723:        if (event_initialized(&wp->changes_timer))
                    724:                evtimer_del(&wp->changes_timer);
1.75      nicm      725:
1.37      nicm      726:        if (wp->fd != -1) {
1.70      nicm      727:                bufferevent_free(wp->event);
1.1       nicm      728:                close(wp->fd);
1.37      nicm      729:        }
1.1       nicm      730:
                    731:        input_free(wp);
                    732:
                    733:        screen_free(&wp->base);
1.9       nicm      734:        if (wp->saved_grid != NULL)
                    735:                grid_destroy(wp->saved_grid);
1.1       nicm      736:
1.32      nicm      737:        if (wp->pipe_fd != -1) {
1.70      nicm      738:                bufferevent_free(wp->pipe_event);
1.32      nicm      739:                close(wp->pipe_fd);
                    740:        }
                    741:
1.64      nicm      742:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    743:
1.99      nicm      744:        close(wp->cwd);
1.82      nicm      745:        free(wp->shell);
1.110     nicm      746:        cmd_free_argv(wp->argc, wp->argv);
1.82      nicm      747:        free(wp);
1.1       nicm      748: }
                    749:
                    750: int
1.110     nicm      751: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
                    752:     const char *path, const char *shell, int cwd, struct environ *env,
                    753:     struct termios *tio, char **cause)
1.1       nicm      754: {
1.47      nicm      755:        struct winsize   ws;
1.110     nicm      756:        char            *argv0, *cmd, **argvp, paneid[16];
                    757:        const char      *ptr, *first;
1.47      nicm      758:        struct termios   tio2;
1.110     nicm      759:        int              i;
1.1       nicm      760:
1.37      nicm      761:        if (wp->fd != -1) {
1.70      nicm      762:                bufferevent_free(wp->event);
1.1       nicm      763:                close(wp->fd);
1.37      nicm      764:        }
1.110     nicm      765:        if (argc > 0) {
                    766:                cmd_free_argv(wp->argc, wp->argv);
                    767:                wp->argc = argc;
                    768:                wp->argv = cmd_copy_argv(argc, argv);
1.1       nicm      769:        }
1.23      nicm      770:        if (shell != NULL) {
1.82      nicm      771:                free(wp->shell);
1.23      nicm      772:                wp->shell = xstrdup(shell);
                    773:        }
1.99      nicm      774:        if (cwd != -1) {
                    775:                close(wp->cwd);
                    776:                wp->cwd = dup(cwd);
1.1       nicm      777:        }
1.91      nicm      778:
1.110     nicm      779:        cmd = cmd_stringify_argv(wp->argc, wp->argv);
                    780:        log_debug("spawn: %s -- %s", wp->shell, cmd);
                    781:        for (i = 0; i < wp->argc; i++)
                    782:                log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
1.1       nicm      783:
                    784:        memset(&ws, 0, sizeof ws);
                    785:        ws.ws_col = screen_size_x(&wp->base);
                    786:        ws.ws_row = screen_size_y(&wp->base);
                    787:
1.42      nicm      788:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      789:        case -1:
                    790:                wp->fd = -1;
                    791:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110     nicm      792:                free(cmd);
1.1       nicm      793:                return (-1);
                    794:        case 0:
1.99      nicm      795:                if (fchdir(wp->cwd) != 0)
1.1       nicm      796:                        chdir("/");
1.25      nicm      797:
                    798:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    799:                        fatal("tcgetattr failed");
                    800:                if (tio != NULL)
                    801:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    802:                tio2.c_cc[VERASE] = '\177';
                    803:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    804:                        fatal("tcgetattr failed");
1.18      nicm      805:
1.56      nicm      806:                closefrom(STDERR_FILENO + 1);
                    807:
1.107     nicm      808:                if (path != NULL)
                    809:                        environ_set(env, "PATH", path);
1.64      nicm      810:                xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
                    811:                environ_set(env, "TMUX_PANE", paneid);
1.47      nicm      812:                environ_push(env);
1.18      nicm      813:
1.54      nicm      814:                clear_signals(1);
1.1       nicm      815:                log_close();
                    816:
1.80      nicm      817:                setenv("SHELL", wp->shell, 1);
                    818:                ptr = strrchr(wp->shell, '/');
                    819:
1.110     nicm      820:                /*
                    821:                 * If given one argument, assume it should be passed to sh -c;
                    822:                 * with more than one argument, use execvp(). If there is no
                    823:                 * arguments, create a login shell.
                    824:                 */
                    825:                if (wp->argc > 0) {
                    826:                        if (wp->argc != 1) {
                    827:                                /* Copy to ensure argv ends in NULL. */
                    828:                                argvp = cmd_copy_argv(wp->argc, wp->argv);
                    829:                                execvp(argvp[0], argvp);
                    830:                                fatal("execvp failed");
                    831:                        }
                    832:                        first = wp->argv[0];
                    833:
1.80      nicm      834:                        if (ptr != NULL && *(ptr + 1) != '\0')
                    835:                                xasprintf(&argv0, "%s", ptr + 1);
                    836:                        else
                    837:                                xasprintf(&argv0, "%s", wp->shell);
1.110     nicm      838:                        execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8       nicm      839:                        fatal("execl failed");
                    840:                }
1.23      nicm      841:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      842:                        xasprintf(&argv0, "-%s", ptr + 1);
                    843:                else
1.23      nicm      844:                        xasprintf(&argv0, "-%s", wp->shell);
1.110     nicm      845:                execl(wp->shell, argv0, (char *)NULL);
1.1       nicm      846:                fatal("execl failed");
                    847:        }
                    848:
1.62      nicm      849:        setblocking(wp->fd, 0);
                    850:
1.110     nicm      851:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
                    852:            window_pane_error_callback, wp);
1.37      nicm      853:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      854:
1.110     nicm      855:        free(cmd);
1.1       nicm      856:        return (0);
1.75      nicm      857: }
                    858:
                    859: void
                    860: window_pane_timer_start(struct window_pane *wp)
                    861: {
                    862:        struct timeval  tv;
                    863:
                    864:        tv.tv_sec = 0;
                    865:        tv.tv_usec = 1000;
                    866:
                    867:        evtimer_del(&wp->changes_timer);
                    868:        evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp);
                    869:        evtimer_add(&wp->changes_timer, &tv);
                    870: }
                    871:
                    872: void
                    873: window_pane_timer_callback(unused int fd, unused short events, void *data)
                    874: {
                    875:        struct window_pane      *wp = data;
                    876:        struct window           *w = wp->window;
                    877:        u_int                    interval, trigger;
                    878:
                    879:        interval = options_get_number(&w->options, "c0-change-interval");
                    880:        trigger = options_get_number(&w->options, "c0-change-trigger");
                    881:
                    882:        if (wp->changes_redraw++ == interval) {
                    883:                wp->flags |= PANE_REDRAW;
                    884:                wp->changes_redraw = 0;
                    885:        }
                    886:
                    887:        if (trigger == 0 || wp->changes < trigger) {
                    888:                wp->flags |= PANE_REDRAW;
                    889:                wp->flags &= ~PANE_DROP;
                    890:        } else
                    891:                window_pane_timer_start(wp);
                    892:        wp->changes = 0;
1.1       nicm      893: }
                    894:
1.15      nicm      895: void
1.37      nicm      896: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
                    897: {
1.46      nicm      898:        struct window_pane     *wp = data;
                    899:        char                   *new_data;
                    900:        size_t                  new_size;
                    901:
                    902:        new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
                    903:        if (wp->pipe_fd != -1 && new_size > 0) {
                    904:                new_data = EVBUFFER_DATA(wp->event->input);
                    905:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    906:        }
                    907:
                    908:        input_parse(wp);
1.37      nicm      909:
1.46      nicm      910:        wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.60      nicm      911:
                    912:        /*
                    913:         * If we get here, we're not outputting anymore, so set the silence
                    914:         * flag on the window.
                    915:         */
                    916:        wp->window->flags |= WINDOW_SILENCE;
                    917:        if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
                    918:                fatal("gettimeofday failed.");
1.37      nicm      919: }
                    920:
                    921: void
                    922: window_pane_error_callback(
                    923:     unused struct bufferevent *bufev, unused short what, void *data)
                    924: {
                    925:        struct window_pane *wp = data;
                    926:
1.39      nicm      927:        server_destroy_pane(wp);
1.37      nicm      928: }
                    929:
                    930: void
1.1       nicm      931: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    932: {
                    933:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      934:                return;
1.1       nicm      935:        wp->sx = sx;
                    936:        wp->sy = sy;
                    937:
1.89      nicm      938:        screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1       nicm      939:        if (wp->mode != NULL)
                    940:                wp->mode->resize(wp, sx, sy);
1.95      nicm      941:
                    942:        wp->flags |= PANE_RESIZE;
1.44      nicm      943: }
                    944:
                    945: /*
                    946:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    947:  * history is not updated
                    948:  */
                    949: void
1.87      nicm      950: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
                    951:     int cursor)
1.44      nicm      952: {
                    953:        struct screen   *s = &wp->base;
                    954:        u_int            sx, sy;
                    955:
                    956:        if (wp->saved_grid != NULL)
                    957:                return;
                    958:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    959:                return;
                    960:        sx = screen_size_x(s);
                    961:        sy = screen_size_y(s);
                    962:
                    963:        wp->saved_grid = grid_create(sx, sy, 0);
                    964:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87      nicm      965:        if (cursor) {
                    966:                wp->saved_cx = s->cx;
                    967:                wp->saved_cy = s->cy;
                    968:        }
1.44      nicm      969:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                    970:
                    971:        grid_view_clear(s->grid, 0, 0, sx, sy);
                    972:
                    973:        wp->base.grid->flags &= ~GRID_HISTORY;
                    974:
                    975:        wp->flags |= PANE_REDRAW;
                    976: }
                    977:
                    978: /* Exit alternate screen mode and restore the copied grid. */
                    979: void
1.87      nicm      980: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
                    981:     int cursor)
1.44      nicm      982: {
                    983:        struct screen   *s = &wp->base;
                    984:        u_int            sx, sy;
                    985:
                    986:        if (wp->saved_grid == NULL)
                    987:                return;
                    988:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    989:                return;
                    990:        sx = screen_size_x(s);
                    991:        sy = screen_size_y(s);
                    992:
                    993:        /*
                    994:         * If the current size is bigger, temporarily resize to the old size
                    995:         * before copying back.
                    996:         */
                    997:        if (sy > wp->saved_grid->sy)
1.89      nicm      998:                screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44      nicm      999:
                   1000:        /* Restore the grid, cursor position and cell. */
                   1001:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87      nicm     1002:        if (cursor)
                   1003:                s->cx = wp->saved_cx;
1.44      nicm     1004:        if (s->cx > screen_size_x(s) - 1)
                   1005:                s->cx = screen_size_x(s) - 1;
1.87      nicm     1006:        if (cursor)
                   1007:                s->cy = wp->saved_cy;
1.44      nicm     1008:        if (s->cy > screen_size_y(s) - 1)
                   1009:                s->cy = screen_size_y(s) - 1;
                   1010:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                   1011:
                   1012:        /*
                   1013:         * Turn history back on (so resize can use it) and then resize back to
                   1014:         * the current size.
                   1015:         */
                   1016:        wp->base.grid->flags |= GRID_HISTORY;
1.89      nicm     1017:        if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
                   1018:                screen_resize(s, sx, sy, 1);
1.44      nicm     1019:
                   1020:        grid_destroy(wp->saved_grid);
                   1021:        wp->saved_grid = NULL;
                   1022:
                   1023:        wp->flags |= PANE_REDRAW;
1.1       nicm     1024: }
                   1025:
                   1026: int
                   1027: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                   1028: {
                   1029:        struct screen   *s;
                   1030:
1.15      nicm     1031:        if (wp->mode != NULL)
1.1       nicm     1032:                return (1);
                   1033:        wp->mode = mode;
                   1034:
                   1035:        if ((s = wp->mode->init(wp)) != NULL)
                   1036:                wp->screen = s;
1.34      nicm     1037:        wp->flags |= PANE_REDRAW;
1.1       nicm     1038:        return (0);
                   1039: }
                   1040:
                   1041: void
                   1042: window_pane_reset_mode(struct window_pane *wp)
                   1043: {
                   1044:        if (wp->mode == NULL)
                   1045:                return;
                   1046:
                   1047:        wp->mode->free(wp);
                   1048:        wp->mode = NULL;
                   1049:
                   1050:        wp->screen = &wp->base;
1.34      nicm     1051:        wp->flags |= PANE_REDRAW;
1.1       nicm     1052: }
                   1053:
                   1054: void
1.51      nicm     1055: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1       nicm     1056: {
1.27      nicm     1057:        struct window_pane      *wp2;
1.3       nicm     1058:
1.1       nicm     1059:        if (wp->mode != NULL) {
                   1060:                if (wp->mode->key != NULL)
1.51      nicm     1061:                        wp->mode->key(wp, sess, key);
1.27      nicm     1062:                return;
1.30      nicm     1063:        }
1.27      nicm     1064:
1.113     nicm     1065:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30      nicm     1066:                return;
1.113     nicm     1067:
1.27      nicm     1068:        input_key(wp, key);
                   1069:        if (options_get_number(&wp->window->options, "synchronize-panes")) {
                   1070:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1071:                        if (wp2 == wp || wp2->mode != NULL)
                   1072:                                continue;
                   1073:                        if (wp2->fd != -1 && window_pane_visible(wp2))
                   1074:                                input_key(wp2, key);
                   1075:                }
                   1076:        }
1.1       nicm     1077: }
                   1078:
                   1079: void
1.113     nicm     1080: window_pane_mouse(struct window_pane *wp, struct session *sess,
                   1081:     struct mouse_event *m)
1.1       nicm     1082: {
1.30      nicm     1083:        if (!window_pane_visible(wp))
1.3       nicm     1084:                return;
                   1085:
1.31      nicm     1086:        if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1       nicm     1087:                return;
1.31      nicm     1088:        if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1       nicm     1089:                return;
1.31      nicm     1090:        m->x -= wp->xoff;
                   1091:        m->y -= wp->yoff;
1.1       nicm     1092:
                   1093:        if (wp->mode != NULL) {
1.65      nicm     1094:                if (wp->mode->mouse != NULL &&
                   1095:                    options_get_number(&wp->window->options, "mode-mouse"))
1.51      nicm     1096:                        wp->mode->mouse(wp, sess, m);
1.30      nicm     1097:        } else if (wp->fd != -1)
1.86      nicm     1098:                input_mouse(wp, sess, m);
1.10      nicm     1099: }
                   1100:
                   1101: int
                   1102: window_pane_visible(struct window_pane *wp)
                   1103: {
                   1104:        struct window   *w = wp->window;
                   1105:
1.93      nicm     1106:        if (wp->layout_cell == NULL)
                   1107:                return (0);
1.10      nicm     1108:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                   1109:                return (0);
                   1110:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                   1111:                return (0);
                   1112:        return (1);
1.1       nicm     1113: }
                   1114:
                   1115: char *
1.108     nicm     1116: window_pane_search(struct window_pane *wp, const char *searchstr,
                   1117:     u_int *lineno)
1.1       nicm     1118: {
1.4       nicm     1119:        struct screen   *s = &wp->base;
1.5       nicm     1120:        char            *newsearchstr, *line, *msg;
1.4       nicm     1121:        u_int            i;
                   1122:
1.5       nicm     1123:        msg = NULL;
                   1124:        xasprintf(&newsearchstr, "*%s*", searchstr);
                   1125:
1.4       nicm     1126:        for (i = 0; i < screen_size_y(s); i++) {
                   1127:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm     1128:                if (fnmatch(newsearchstr, line, 0) == 0) {
                   1129:                        msg = line;
                   1130:                        if (lineno != NULL)
                   1131:                                *lineno = i;
                   1132:                        break;
                   1133:                }
1.82      nicm     1134:                free(line);
1.4       nicm     1135:        }
1.5       nicm     1136:
1.82      nicm     1137:        free(newsearchstr);
1.5       nicm     1138:        return (msg);
1.45      nicm     1139: }
                   1140:
1.109     nicm     1141: /* Get MRU pane from a list. */
                   1142: struct window_pane *
                   1143: window_pane_choose_best(struct window_pane_list *list)
                   1144: {
                   1145:        struct window_pane      *next, *best;
                   1146:        u_int                    i;
                   1147:
                   1148:        if (ARRAY_LENGTH(list) == 0)
                   1149:                return (NULL);
                   1150:
                   1151:        best = ARRAY_FIRST(list);
                   1152:        for (i = 1; i < ARRAY_LENGTH(list); i++) {
                   1153:                next = ARRAY_ITEM(list, i);
                   1154:                if (next->active_point > best->active_point)
                   1155:                        best = next;
                   1156:        }
                   1157:        return (best);
                   1158: }
                   1159:
                   1160: /*
                   1161:  * Find the pane directly above another. We build a list of those adjacent to
                   1162:  * top edge and then choose the best.
                   1163:  */
1.45      nicm     1164: struct window_pane *
                   1165: window_pane_find_up(struct window_pane *wp)
                   1166: {
1.109     nicm     1167:        struct window_pane      *next, *best;
                   1168:        u_int                    edge, left, right, end;
                   1169:        struct window_pane_list  list;
                   1170:        int                      found;
1.45      nicm     1171:
                   1172:        if (wp == NULL || !window_pane_visible(wp))
                   1173:                return (NULL);
1.109     nicm     1174:        ARRAY_INIT(&list);
                   1175:
                   1176:        edge = wp->yoff;
                   1177:        if (edge == 0)
                   1178:                edge = wp->window->sy + 1;
1.45      nicm     1179:
                   1180:        left = wp->xoff;
1.109     nicm     1181:        right = wp->xoff + wp->sx;
1.45      nicm     1182:
1.109     nicm     1183:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1184:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1185:                        continue;
1.109     nicm     1186:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1187:                        continue;
1.109     nicm     1188:                end = next->xoff + next->sx - 1;
                   1189:
                   1190:                found = 0;
                   1191:                if (next->xoff < left && end > right)
                   1192:                        found = 1;
                   1193:                else if (next->xoff >= left && next->xoff <= right)
                   1194:                        found = 1;
                   1195:                else if (end >= left && end <= right)
                   1196:                        found = 1;
                   1197:                if (found)
                   1198:                        ARRAY_ADD(&list, next);
                   1199:        }
                   1200:
                   1201:        best = window_pane_choose_best(&list);
                   1202:        ARRAY_FREE(&list);
                   1203:        return (best);
1.45      nicm     1204: }
                   1205:
                   1206: /* Find the pane directly below another. */
                   1207: struct window_pane *
                   1208: window_pane_find_down(struct window_pane *wp)
                   1209: {
1.109     nicm     1210:        struct window_pane      *next, *best;
                   1211:        u_int                    edge, left, right, end;
                   1212:        struct window_pane_list  list;
                   1213:        int                      found;
1.45      nicm     1214:
                   1215:        if (wp == NULL || !window_pane_visible(wp))
                   1216:                return (NULL);
1.109     nicm     1217:        ARRAY_INIT(&list);
                   1218:
                   1219:        edge = wp->yoff + wp->sy + 1;
                   1220:        if (edge >= wp->window->sy)
                   1221:                edge = 0;
1.45      nicm     1222:
                   1223:        left = wp->xoff;
1.109     nicm     1224:        right = wp->xoff + wp->sx;
1.45      nicm     1225:
1.109     nicm     1226:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1227:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1228:                        continue;
1.109     nicm     1229:                if (next->yoff != edge)
1.45      nicm     1230:                        continue;
1.109     nicm     1231:                end = next->xoff + next->sx - 1;
                   1232:
                   1233:                found = 0;
                   1234:                if (next->xoff < left && end > right)
                   1235:                        found = 1;
                   1236:                else if (next->xoff >= left && next->xoff <= right)
                   1237:                        found = 1;
                   1238:                else if (end >= left && end <= right)
                   1239:                        found = 1;
                   1240:                if (found)
                   1241:                        ARRAY_ADD(&list, next);
1.45      nicm     1242:        }
1.109     nicm     1243:
                   1244:        best = window_pane_choose_best(&list);
                   1245:        ARRAY_FREE(&list);
                   1246:        return (best);
1.45      nicm     1247: }
                   1248:
1.109     nicm     1249: /* Find the pane directly to the left of another. */
1.45      nicm     1250: struct window_pane *
                   1251: window_pane_find_left(struct window_pane *wp)
                   1252: {
1.109     nicm     1253:        struct window_pane      *next, *best;
                   1254:        u_int                    edge, top, bottom, end;
                   1255:        struct window_pane_list  list;
                   1256:        int                      found;
1.45      nicm     1257:
                   1258:        if (wp == NULL || !window_pane_visible(wp))
                   1259:                return (NULL);
1.109     nicm     1260:        ARRAY_INIT(&list);
                   1261:
                   1262:        edge = wp->xoff;
                   1263:        if (edge == 0)
                   1264:                edge = wp->window->sx + 1;
1.45      nicm     1265:
                   1266:        top = wp->yoff;
1.109     nicm     1267:        bottom = wp->yoff + wp->sy;
1.45      nicm     1268:
1.109     nicm     1269:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1270:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1271:                        continue;
1.109     nicm     1272:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1273:                        continue;
1.109     nicm     1274:                end = next->yoff + next->sy - 1;
                   1275:
                   1276:                found = 0;
                   1277:                if (next->yoff < top && end > bottom)
                   1278:                        found = 1;
                   1279:                else if (next->yoff >= top && next->yoff <= bottom)
                   1280:                        found = 1;
                   1281:                else if (end >= top && end <= bottom)
                   1282:                        found = 1;
                   1283:                if (found)
                   1284:                        ARRAY_ADD(&list, next);
1.45      nicm     1285:        }
1.109     nicm     1286:
                   1287:        best = window_pane_choose_best(&list);
                   1288:        ARRAY_FREE(&list);
                   1289:        return (best);
1.45      nicm     1290: }
                   1291:
1.109     nicm     1292: /* Find the pane directly to the right of another. */
1.45      nicm     1293: struct window_pane *
                   1294: window_pane_find_right(struct window_pane *wp)
                   1295: {
1.109     nicm     1296:        struct window_pane      *next, *best;
                   1297:        u_int                    edge, top, bottom, end;
                   1298:        struct window_pane_list  list;
                   1299:        int                      found;
1.45      nicm     1300:
                   1301:        if (wp == NULL || !window_pane_visible(wp))
                   1302:                return (NULL);
1.109     nicm     1303:        ARRAY_INIT(&list);
                   1304:
                   1305:        edge = wp->xoff + wp->sx + 1;
                   1306:        if (edge >= wp->window->sx)
                   1307:                edge = 0;
1.45      nicm     1308:
                   1309:        top = wp->yoff;
1.109     nicm     1310:        bottom = wp->yoff + wp->sy;
1.45      nicm     1311:
1.109     nicm     1312:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1313:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1314:                        continue;
1.109     nicm     1315:                if (next->xoff != edge)
1.45      nicm     1316:                        continue;
1.109     nicm     1317:                end = next->yoff + next->sy - 1;
                   1318:
                   1319:                found = 0;
                   1320:                if (next->yoff < top && end > bottom)
                   1321:                        found = 1;
                   1322:                else if (next->yoff >= top && next->yoff <= bottom)
                   1323:                        found = 1;
                   1324:                else if (end >= top && end <= bottom)
                   1325:                        found = 1;
                   1326:                if (found)
                   1327:                        ARRAY_ADD(&list, next);
                   1328:        }
                   1329:
                   1330:        best = window_pane_choose_best(&list);
                   1331:        ARRAY_FREE(&list);
                   1332:        return (best);
1.81      nicm     1333: }
                   1334:
                   1335: /* Clear alert flags for a winlink */
                   1336: void
                   1337: winlink_clear_flags(struct winlink *wl)
                   1338: {
                   1339:        struct winlink  *wm;
                   1340:        struct session  *s;
                   1341:        struct window   *w;
                   1342:        u_int            i;
                   1343:
                   1344:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                   1345:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
                   1346:                        continue;
                   1347:
                   1348:                RB_FOREACH(s, sessions, &sessions) {
                   1349:                        if ((wm = session_has(s, w)) == NULL)
                   1350:                                continue;
                   1351:
                   1352:                        if (wm->window != wl->window)
                   1353:                                continue;
                   1354:                        if ((wm->flags & WINLINK_ALERTFLAGS) == 0)
                   1355:                                continue;
                   1356:
                   1357:                        wm->flags &= ~WINLINK_ALERTFLAGS;
1.98      nicm     1358:                        wm->window->flags &= ~WINDOW_ALERTFLAGS;
1.81      nicm     1359:                        server_status_session(s);
                   1360:                }
                   1361:        }
1.1       nicm     1362: }