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

1.115   ! nicm        1: /* $OpenBSD: window.c,v 1.114 2014/10/21 22:22:04 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);
                    266:                if (w->id == id)
                    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;
                    707:
1.1       nicm      708:        screen_init(&wp->base, sx, sy, hlimit);
                    709:        wp->screen = &wp->base;
                    710:
                    711:        input_init(wp);
                    712:
                    713:        return (wp);
                    714: }
                    715:
                    716: void
                    717: window_pane_destroy(struct window_pane *wp)
                    718: {
1.55      nicm      719:        window_pane_reset_mode(wp);
                    720:
1.76      nicm      721:        if (event_initialized(&wp->changes_timer))
                    722:                evtimer_del(&wp->changes_timer);
1.75      nicm      723:
1.37      nicm      724:        if (wp->fd != -1) {
1.70      nicm      725:                bufferevent_free(wp->event);
1.1       nicm      726:                close(wp->fd);
1.37      nicm      727:        }
1.1       nicm      728:
                    729:        input_free(wp);
                    730:
                    731:        screen_free(&wp->base);
1.9       nicm      732:        if (wp->saved_grid != NULL)
                    733:                grid_destroy(wp->saved_grid);
1.1       nicm      734:
1.32      nicm      735:        if (wp->pipe_fd != -1) {
1.70      nicm      736:                bufferevent_free(wp->pipe_event);
1.32      nicm      737:                close(wp->pipe_fd);
                    738:        }
                    739:
1.64      nicm      740:        RB_REMOVE(window_pane_tree, &all_window_panes, wp);
                    741:
1.99      nicm      742:        close(wp->cwd);
1.82      nicm      743:        free(wp->shell);
1.110     nicm      744:        cmd_free_argv(wp->argc, wp->argv);
1.82      nicm      745:        free(wp);
1.1       nicm      746: }
                    747:
                    748: int
1.110     nicm      749: window_pane_spawn(struct window_pane *wp, int argc, char **argv,
                    750:     const char *path, const char *shell, int cwd, struct environ *env,
                    751:     struct termios *tio, char **cause)
1.1       nicm      752: {
1.47      nicm      753:        struct winsize   ws;
1.110     nicm      754:        char            *argv0, *cmd, **argvp, paneid[16];
                    755:        const char      *ptr, *first;
1.47      nicm      756:        struct termios   tio2;
1.110     nicm      757:        int              i;
1.1       nicm      758:
1.37      nicm      759:        if (wp->fd != -1) {
1.70      nicm      760:                bufferevent_free(wp->event);
1.1       nicm      761:                close(wp->fd);
1.37      nicm      762:        }
1.110     nicm      763:        if (argc > 0) {
                    764:                cmd_free_argv(wp->argc, wp->argv);
                    765:                wp->argc = argc;
                    766:                wp->argv = cmd_copy_argv(argc, argv);
1.1       nicm      767:        }
1.23      nicm      768:        if (shell != NULL) {
1.82      nicm      769:                free(wp->shell);
1.23      nicm      770:                wp->shell = xstrdup(shell);
                    771:        }
1.99      nicm      772:        if (cwd != -1) {
                    773:                close(wp->cwd);
                    774:                wp->cwd = dup(cwd);
1.1       nicm      775:        }
1.91      nicm      776:
1.110     nicm      777:        cmd = cmd_stringify_argv(wp->argc, wp->argv);
                    778:        log_debug("spawn: %s -- %s", wp->shell, cmd);
                    779:        for (i = 0; i < wp->argc; i++)
                    780:                log_debug("spawn: argv[%d] = %s", i, wp->argv[i]);
1.1       nicm      781:
                    782:        memset(&ws, 0, sizeof ws);
                    783:        ws.ws_col = screen_size_x(&wp->base);
                    784:        ws.ws_row = screen_size_y(&wp->base);
                    785:
1.42      nicm      786:        switch (wp->pid = forkpty(&wp->fd, wp->tty, NULL, &ws)) {
1.1       nicm      787:        case -1:
                    788:                wp->fd = -1;
                    789:                xasprintf(cause, "%s: %s", cmd, strerror(errno));
1.110     nicm      790:                free(cmd);
1.1       nicm      791:                return (-1);
                    792:        case 0:
1.99      nicm      793:                if (fchdir(wp->cwd) != 0)
1.1       nicm      794:                        chdir("/");
1.25      nicm      795:
                    796:                if (tcgetattr(STDIN_FILENO, &tio2) != 0)
                    797:                        fatal("tcgetattr failed");
                    798:                if (tio != NULL)
                    799:                        memcpy(tio2.c_cc, tio->c_cc, sizeof tio2.c_cc);
                    800:                tio2.c_cc[VERASE] = '\177';
                    801:                if (tcsetattr(STDIN_FILENO, TCSANOW, &tio2) != 0)
                    802:                        fatal("tcgetattr failed");
1.18      nicm      803:
1.56      nicm      804:                closefrom(STDERR_FILENO + 1);
                    805:
1.107     nicm      806:                if (path != NULL)
                    807:                        environ_set(env, "PATH", path);
1.64      nicm      808:                xsnprintf(paneid, sizeof paneid, "%%%u", wp->id);
                    809:                environ_set(env, "TMUX_PANE", paneid);
1.47      nicm      810:                environ_push(env);
1.18      nicm      811:
1.54      nicm      812:                clear_signals(1);
1.1       nicm      813:                log_close();
                    814:
1.80      nicm      815:                setenv("SHELL", wp->shell, 1);
                    816:                ptr = strrchr(wp->shell, '/');
                    817:
1.110     nicm      818:                /*
                    819:                 * If given one argument, assume it should be passed to sh -c;
                    820:                 * with more than one argument, use execvp(). If there is no
                    821:                 * arguments, create a login shell.
                    822:                 */
                    823:                if (wp->argc > 0) {
                    824:                        if (wp->argc != 1) {
                    825:                                /* Copy to ensure argv ends in NULL. */
                    826:                                argvp = cmd_copy_argv(wp->argc, wp->argv);
                    827:                                execvp(argvp[0], argvp);
                    828:                                fatal("execvp failed");
                    829:                        }
                    830:                        first = wp->argv[0];
                    831:
1.80      nicm      832:                        if (ptr != NULL && *(ptr + 1) != '\0')
                    833:                                xasprintf(&argv0, "%s", ptr + 1);
                    834:                        else
                    835:                                xasprintf(&argv0, "%s", wp->shell);
1.110     nicm      836:                        execl(wp->shell, argv0, "-c", first, (char *)NULL);
1.8       nicm      837:                        fatal("execl failed");
                    838:                }
1.23      nicm      839:                if (ptr != NULL && *(ptr + 1) != '\0')
1.8       nicm      840:                        xasprintf(&argv0, "-%s", ptr + 1);
                    841:                else
1.23      nicm      842:                        xasprintf(&argv0, "-%s", wp->shell);
1.110     nicm      843:                execl(wp->shell, argv0, (char *)NULL);
1.1       nicm      844:                fatal("execl failed");
                    845:        }
                    846:
1.62      nicm      847:        setblocking(wp->fd, 0);
                    848:
1.110     nicm      849:        wp->event = bufferevent_new(wp->fd, window_pane_read_callback, NULL,
                    850:            window_pane_error_callback, wp);
1.37      nicm      851:        bufferevent_enable(wp->event, EV_READ|EV_WRITE);
1.1       nicm      852:
1.110     nicm      853:        free(cmd);
1.1       nicm      854:        return (0);
1.75      nicm      855: }
                    856:
                    857: void
                    858: window_pane_timer_start(struct window_pane *wp)
                    859: {
                    860:        struct timeval  tv;
                    861:
                    862:        tv.tv_sec = 0;
                    863:        tv.tv_usec = 1000;
                    864:
                    865:        evtimer_del(&wp->changes_timer);
                    866:        evtimer_set(&wp->changes_timer, window_pane_timer_callback, wp);
                    867:        evtimer_add(&wp->changes_timer, &tv);
                    868: }
                    869:
                    870: void
                    871: window_pane_timer_callback(unused int fd, unused short events, void *data)
                    872: {
                    873:        struct window_pane      *wp = data;
                    874:        struct window           *w = wp->window;
                    875:        u_int                    interval, trigger;
                    876:
                    877:        interval = options_get_number(&w->options, "c0-change-interval");
                    878:        trigger = options_get_number(&w->options, "c0-change-trigger");
                    879:
                    880:        if (wp->changes_redraw++ == interval) {
                    881:                wp->flags |= PANE_REDRAW;
                    882:                wp->changes_redraw = 0;
                    883:        }
                    884:
                    885:        if (trigger == 0 || wp->changes < trigger) {
                    886:                wp->flags |= PANE_REDRAW;
                    887:                wp->flags &= ~PANE_DROP;
                    888:        } else
                    889:                window_pane_timer_start(wp);
                    890:        wp->changes = 0;
1.1       nicm      891: }
                    892:
1.15      nicm      893: void
1.37      nicm      894: window_pane_read_callback(unused struct bufferevent *bufev, void *data)
                    895: {
1.46      nicm      896:        struct window_pane     *wp = data;
                    897:        char                   *new_data;
                    898:        size_t                  new_size;
                    899:
                    900:        new_size = EVBUFFER_LENGTH(wp->event->input) - wp->pipe_off;
                    901:        if (wp->pipe_fd != -1 && new_size > 0) {
                    902:                new_data = EVBUFFER_DATA(wp->event->input);
                    903:                bufferevent_write(wp->pipe_event, new_data, new_size);
                    904:        }
                    905:
                    906:        input_parse(wp);
1.37      nicm      907:
1.46      nicm      908:        wp->pipe_off = EVBUFFER_LENGTH(wp->event->input);
1.60      nicm      909:
                    910:        /*
                    911:         * If we get here, we're not outputting anymore, so set the silence
                    912:         * flag on the window.
                    913:         */
                    914:        wp->window->flags |= WINDOW_SILENCE;
                    915:        if (gettimeofday(&wp->window->silence_timer, NULL) != 0)
                    916:                fatal("gettimeofday failed.");
1.37      nicm      917: }
                    918:
                    919: void
                    920: window_pane_error_callback(
                    921:     unused struct bufferevent *bufev, unused short what, void *data)
                    922: {
                    923:        struct window_pane *wp = data;
                    924:
1.39      nicm      925:        server_destroy_pane(wp);
1.37      nicm      926: }
                    927:
                    928: void
1.1       nicm      929: window_pane_resize(struct window_pane *wp, u_int sx, u_int sy)
                    930: {
                    931:        if (sx == wp->sx && sy == wp->sy)
1.15      nicm      932:                return;
1.1       nicm      933:        wp->sx = sx;
                    934:        wp->sy = sy;
                    935:
1.89      nicm      936:        screen_resize(&wp->base, sx, sy, wp->saved_grid == NULL);
1.1       nicm      937:        if (wp->mode != NULL)
                    938:                wp->mode->resize(wp, sx, sy);
1.95      nicm      939:
                    940:        wp->flags |= PANE_RESIZE;
1.44      nicm      941: }
                    942:
                    943: /*
                    944:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    945:  * history is not updated
                    946:  */
                    947: void
1.87      nicm      948: window_pane_alternate_on(struct window_pane *wp, struct grid_cell *gc,
                    949:     int cursor)
1.44      nicm      950: {
                    951:        struct screen   *s = &wp->base;
                    952:        u_int            sx, sy;
                    953:
                    954:        if (wp->saved_grid != NULL)
                    955:                return;
                    956:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    957:                return;
                    958:        sx = screen_size_x(s);
                    959:        sy = screen_size_y(s);
                    960:
                    961:        wp->saved_grid = grid_create(sx, sy, 0);
                    962:        grid_duplicate_lines(wp->saved_grid, 0, s->grid, screen_hsize(s), sy);
1.87      nicm      963:        if (cursor) {
                    964:                wp->saved_cx = s->cx;
                    965:                wp->saved_cy = s->cy;
                    966:        }
1.44      nicm      967:        memcpy(&wp->saved_cell, gc, sizeof wp->saved_cell);
                    968:
                    969:        grid_view_clear(s->grid, 0, 0, sx, sy);
                    970:
                    971:        wp->base.grid->flags &= ~GRID_HISTORY;
                    972:
                    973:        wp->flags |= PANE_REDRAW;
                    974: }
                    975:
                    976: /* Exit alternate screen mode and restore the copied grid. */
                    977: void
1.87      nicm      978: window_pane_alternate_off(struct window_pane *wp, struct grid_cell *gc,
                    979:     int cursor)
1.44      nicm      980: {
                    981:        struct screen   *s = &wp->base;
                    982:        u_int            sx, sy;
                    983:
                    984:        if (wp->saved_grid == NULL)
                    985:                return;
                    986:        if (!options_get_number(&wp->window->options, "alternate-screen"))
                    987:                return;
                    988:        sx = screen_size_x(s);
                    989:        sy = screen_size_y(s);
                    990:
                    991:        /*
                    992:         * If the current size is bigger, temporarily resize to the old size
                    993:         * before copying back.
                    994:         */
                    995:        if (sy > wp->saved_grid->sy)
1.89      nicm      996:                screen_resize(s, sx, wp->saved_grid->sy, 1);
1.44      nicm      997:
                    998:        /* Restore the grid, cursor position and cell. */
                    999:        grid_duplicate_lines(s->grid, screen_hsize(s), wp->saved_grid, 0, sy);
1.87      nicm     1000:        if (cursor)
                   1001:                s->cx = wp->saved_cx;
1.44      nicm     1002:        if (s->cx > screen_size_x(s) - 1)
                   1003:                s->cx = screen_size_x(s) - 1;
1.87      nicm     1004:        if (cursor)
                   1005:                s->cy = wp->saved_cy;
1.44      nicm     1006:        if (s->cy > screen_size_y(s) - 1)
                   1007:                s->cy = screen_size_y(s) - 1;
                   1008:        memcpy(gc, &wp->saved_cell, sizeof *gc);
                   1009:
                   1010:        /*
                   1011:         * Turn history back on (so resize can use it) and then resize back to
                   1012:         * the current size.
                   1013:         */
                   1014:        wp->base.grid->flags |= GRID_HISTORY;
1.89      nicm     1015:        if (sy > wp->saved_grid->sy || sx != wp->saved_grid->sx)
                   1016:                screen_resize(s, sx, sy, 1);
1.44      nicm     1017:
                   1018:        grid_destroy(wp->saved_grid);
                   1019:        wp->saved_grid = NULL;
                   1020:
                   1021:        wp->flags |= PANE_REDRAW;
1.1       nicm     1022: }
                   1023:
                   1024: int
                   1025: window_pane_set_mode(struct window_pane *wp, const struct window_mode *mode)
                   1026: {
                   1027:        struct screen   *s;
                   1028:
1.15      nicm     1029:        if (wp->mode != NULL)
1.1       nicm     1030:                return (1);
                   1031:        wp->mode = mode;
                   1032:
                   1033:        if ((s = wp->mode->init(wp)) != NULL)
                   1034:                wp->screen = s;
1.34      nicm     1035:        wp->flags |= PANE_REDRAW;
1.1       nicm     1036:        return (0);
                   1037: }
                   1038:
                   1039: void
                   1040: window_pane_reset_mode(struct window_pane *wp)
                   1041: {
                   1042:        if (wp->mode == NULL)
                   1043:                return;
                   1044:
                   1045:        wp->mode->free(wp);
                   1046:        wp->mode = NULL;
                   1047:
                   1048:        wp->screen = &wp->base;
1.34      nicm     1049:        wp->flags |= PANE_REDRAW;
1.1       nicm     1050: }
                   1051:
                   1052: void
1.51      nicm     1053: window_pane_key(struct window_pane *wp, struct session *sess, int key)
1.1       nicm     1054: {
1.27      nicm     1055:        struct window_pane      *wp2;
1.3       nicm     1056:
1.1       nicm     1057:        if (wp->mode != NULL) {
                   1058:                if (wp->mode->key != NULL)
1.51      nicm     1059:                        wp->mode->key(wp, sess, key);
1.27      nicm     1060:                return;
1.30      nicm     1061:        }
1.27      nicm     1062:
1.113     nicm     1063:        if (wp->fd == -1 || wp->flags & PANE_INPUTOFF)
1.30      nicm     1064:                return;
1.113     nicm     1065:
1.27      nicm     1066:        input_key(wp, key);
                   1067:        if (options_get_number(&wp->window->options, "synchronize-panes")) {
                   1068:                TAILQ_FOREACH(wp2, &wp->window->panes, entry) {
                   1069:                        if (wp2 == wp || wp2->mode != NULL)
                   1070:                                continue;
                   1071:                        if (wp2->fd != -1 && window_pane_visible(wp2))
                   1072:                                input_key(wp2, key);
                   1073:                }
                   1074:        }
1.1       nicm     1075: }
                   1076:
                   1077: void
1.113     nicm     1078: window_pane_mouse(struct window_pane *wp, struct session *sess,
                   1079:     struct mouse_event *m)
1.1       nicm     1080: {
1.30      nicm     1081:        if (!window_pane_visible(wp))
1.3       nicm     1082:                return;
                   1083:
1.31      nicm     1084:        if (m->x < wp->xoff || m->x >= wp->xoff + wp->sx)
1.1       nicm     1085:                return;
1.31      nicm     1086:        if (m->y < wp->yoff || m->y >= wp->yoff + wp->sy)
1.1       nicm     1087:                return;
1.31      nicm     1088:        m->x -= wp->xoff;
                   1089:        m->y -= wp->yoff;
1.1       nicm     1090:
                   1091:        if (wp->mode != NULL) {
1.65      nicm     1092:                if (wp->mode->mouse != NULL &&
                   1093:                    options_get_number(&wp->window->options, "mode-mouse"))
1.51      nicm     1094:                        wp->mode->mouse(wp, sess, m);
1.30      nicm     1095:        } else if (wp->fd != -1)
1.86      nicm     1096:                input_mouse(wp, sess, m);
1.10      nicm     1097: }
                   1098:
                   1099: int
                   1100: window_pane_visible(struct window_pane *wp)
                   1101: {
                   1102:        struct window   *w = wp->window;
                   1103:
1.93      nicm     1104:        if (wp->layout_cell == NULL)
                   1105:                return (0);
1.10      nicm     1106:        if (wp->xoff >= w->sx || wp->yoff >= w->sy)
                   1107:                return (0);
                   1108:        if (wp->xoff + wp->sx > w->sx || wp->yoff + wp->sy > w->sy)
                   1109:                return (0);
                   1110:        return (1);
1.1       nicm     1111: }
                   1112:
                   1113: char *
1.108     nicm     1114: window_pane_search(struct window_pane *wp, const char *searchstr,
                   1115:     u_int *lineno)
1.1       nicm     1116: {
1.4       nicm     1117:        struct screen   *s = &wp->base;
1.5       nicm     1118:        char            *newsearchstr, *line, *msg;
1.4       nicm     1119:        u_int            i;
                   1120:
1.5       nicm     1121:        msg = NULL;
                   1122:        xasprintf(&newsearchstr, "*%s*", searchstr);
                   1123:
1.4       nicm     1124:        for (i = 0; i < screen_size_y(s); i++) {
                   1125:                line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
1.5       nicm     1126:                if (fnmatch(newsearchstr, line, 0) == 0) {
                   1127:                        msg = line;
                   1128:                        if (lineno != NULL)
                   1129:                                *lineno = i;
                   1130:                        break;
                   1131:                }
1.82      nicm     1132:                free(line);
1.4       nicm     1133:        }
1.5       nicm     1134:
1.82      nicm     1135:        free(newsearchstr);
1.5       nicm     1136:        return (msg);
1.45      nicm     1137: }
                   1138:
1.109     nicm     1139: /* Get MRU pane from a list. */
                   1140: struct window_pane *
                   1141: window_pane_choose_best(struct window_pane_list *list)
                   1142: {
                   1143:        struct window_pane      *next, *best;
                   1144:        u_int                    i;
                   1145:
                   1146:        if (ARRAY_LENGTH(list) == 0)
                   1147:                return (NULL);
                   1148:
                   1149:        best = ARRAY_FIRST(list);
                   1150:        for (i = 1; i < ARRAY_LENGTH(list); i++) {
                   1151:                next = ARRAY_ITEM(list, i);
                   1152:                if (next->active_point > best->active_point)
                   1153:                        best = next;
                   1154:        }
                   1155:        return (best);
                   1156: }
                   1157:
                   1158: /*
                   1159:  * Find the pane directly above another. We build a list of those adjacent to
                   1160:  * top edge and then choose the best.
                   1161:  */
1.45      nicm     1162: struct window_pane *
                   1163: window_pane_find_up(struct window_pane *wp)
                   1164: {
1.109     nicm     1165:        struct window_pane      *next, *best;
                   1166:        u_int                    edge, left, right, end;
                   1167:        struct window_pane_list  list;
                   1168:        int                      found;
1.45      nicm     1169:
                   1170:        if (wp == NULL || !window_pane_visible(wp))
                   1171:                return (NULL);
1.109     nicm     1172:        ARRAY_INIT(&list);
                   1173:
                   1174:        edge = wp->yoff;
                   1175:        if (edge == 0)
                   1176:                edge = wp->window->sy + 1;
1.45      nicm     1177:
                   1178:        left = wp->xoff;
1.109     nicm     1179:        right = wp->xoff + wp->sx;
1.45      nicm     1180:
1.109     nicm     1181:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1182:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1183:                        continue;
1.109     nicm     1184:                if (next->yoff + next->sy + 1 != edge)
1.45      nicm     1185:                        continue;
1.109     nicm     1186:                end = next->xoff + next->sx - 1;
                   1187:
                   1188:                found = 0;
                   1189:                if (next->xoff < left && end > right)
                   1190:                        found = 1;
                   1191:                else if (next->xoff >= left && next->xoff <= right)
                   1192:                        found = 1;
                   1193:                else if (end >= left && end <= right)
                   1194:                        found = 1;
                   1195:                if (found)
                   1196:                        ARRAY_ADD(&list, next);
                   1197:        }
                   1198:
                   1199:        best = window_pane_choose_best(&list);
                   1200:        ARRAY_FREE(&list);
                   1201:        return (best);
1.45      nicm     1202: }
                   1203:
                   1204: /* Find the pane directly below another. */
                   1205: struct window_pane *
                   1206: window_pane_find_down(struct window_pane *wp)
                   1207: {
1.109     nicm     1208:        struct window_pane      *next, *best;
                   1209:        u_int                    edge, left, right, end;
                   1210:        struct window_pane_list  list;
                   1211:        int                      found;
1.45      nicm     1212:
                   1213:        if (wp == NULL || !window_pane_visible(wp))
                   1214:                return (NULL);
1.109     nicm     1215:        ARRAY_INIT(&list);
                   1216:
                   1217:        edge = wp->yoff + wp->sy + 1;
                   1218:        if (edge >= wp->window->sy)
                   1219:                edge = 0;
1.45      nicm     1220:
                   1221:        left = wp->xoff;
1.109     nicm     1222:        right = wp->xoff + wp->sx;
1.45      nicm     1223:
1.109     nicm     1224:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1225:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1226:                        continue;
1.109     nicm     1227:                if (next->yoff != edge)
1.45      nicm     1228:                        continue;
1.109     nicm     1229:                end = next->xoff + next->sx - 1;
                   1230:
                   1231:                found = 0;
                   1232:                if (next->xoff < left && end > right)
                   1233:                        found = 1;
                   1234:                else if (next->xoff >= left && next->xoff <= right)
                   1235:                        found = 1;
                   1236:                else if (end >= left && end <= right)
                   1237:                        found = 1;
                   1238:                if (found)
                   1239:                        ARRAY_ADD(&list, next);
1.45      nicm     1240:        }
1.109     nicm     1241:
                   1242:        best = window_pane_choose_best(&list);
                   1243:        ARRAY_FREE(&list);
                   1244:        return (best);
1.45      nicm     1245: }
                   1246:
1.109     nicm     1247: /* Find the pane directly to the left of another. */
1.45      nicm     1248: struct window_pane *
                   1249: window_pane_find_left(struct window_pane *wp)
                   1250: {
1.109     nicm     1251:        struct window_pane      *next, *best;
                   1252:        u_int                    edge, top, bottom, end;
                   1253:        struct window_pane_list  list;
                   1254:        int                      found;
1.45      nicm     1255:
                   1256:        if (wp == NULL || !window_pane_visible(wp))
                   1257:                return (NULL);
1.109     nicm     1258:        ARRAY_INIT(&list);
                   1259:
                   1260:        edge = wp->xoff;
                   1261:        if (edge == 0)
                   1262:                edge = wp->window->sx + 1;
1.45      nicm     1263:
                   1264:        top = wp->yoff;
1.109     nicm     1265:        bottom = wp->yoff + wp->sy;
1.45      nicm     1266:
1.109     nicm     1267:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1268:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1269:                        continue;
1.109     nicm     1270:                if (next->xoff + next->sx + 1 != edge)
1.45      nicm     1271:                        continue;
1.109     nicm     1272:                end = next->yoff + next->sy - 1;
                   1273:
                   1274:                found = 0;
                   1275:                if (next->yoff < top && end > bottom)
                   1276:                        found = 1;
                   1277:                else if (next->yoff >= top && next->yoff <= bottom)
                   1278:                        found = 1;
                   1279:                else if (end >= top && end <= bottom)
                   1280:                        found = 1;
                   1281:                if (found)
                   1282:                        ARRAY_ADD(&list, next);
1.45      nicm     1283:        }
1.109     nicm     1284:
                   1285:        best = window_pane_choose_best(&list);
                   1286:        ARRAY_FREE(&list);
                   1287:        return (best);
1.45      nicm     1288: }
                   1289:
1.109     nicm     1290: /* Find the pane directly to the right of another. */
1.45      nicm     1291: struct window_pane *
                   1292: window_pane_find_right(struct window_pane *wp)
                   1293: {
1.109     nicm     1294:        struct window_pane      *next, *best;
                   1295:        u_int                    edge, top, bottom, end;
                   1296:        struct window_pane_list  list;
                   1297:        int                      found;
1.45      nicm     1298:
                   1299:        if (wp == NULL || !window_pane_visible(wp))
                   1300:                return (NULL);
1.109     nicm     1301:        ARRAY_INIT(&list);
                   1302:
                   1303:        edge = wp->xoff + wp->sx + 1;
                   1304:        if (edge >= wp->window->sx)
                   1305:                edge = 0;
1.45      nicm     1306:
                   1307:        top = wp->yoff;
1.109     nicm     1308:        bottom = wp->yoff + wp->sy;
1.45      nicm     1309:
1.109     nicm     1310:        TAILQ_FOREACH(next, &wp->window->panes, entry) {
                   1311:                if (next == wp || !window_pane_visible(next))
1.45      nicm     1312:                        continue;
1.109     nicm     1313:                if (next->xoff != edge)
1.45      nicm     1314:                        continue;
1.109     nicm     1315:                end = next->yoff + next->sy - 1;
                   1316:
                   1317:                found = 0;
                   1318:                if (next->yoff < top && end > bottom)
                   1319:                        found = 1;
                   1320:                else if (next->yoff >= top && next->yoff <= bottom)
                   1321:                        found = 1;
                   1322:                else if (end >= top && end <= bottom)
                   1323:                        found = 1;
                   1324:                if (found)
                   1325:                        ARRAY_ADD(&list, next);
                   1326:        }
                   1327:
                   1328:        best = window_pane_choose_best(&list);
                   1329:        ARRAY_FREE(&list);
                   1330:        return (best);
1.81      nicm     1331: }
                   1332:
                   1333: /* Clear alert flags for a winlink */
                   1334: void
                   1335: winlink_clear_flags(struct winlink *wl)
                   1336: {
                   1337:        struct winlink  *wm;
                   1338:        struct session  *s;
                   1339:        struct window   *w;
                   1340:        u_int            i;
                   1341:
                   1342:        for (i = 0; i < ARRAY_LENGTH(&windows); i++) {
                   1343:                if ((w = ARRAY_ITEM(&windows, i)) == NULL)
                   1344:                        continue;
                   1345:
                   1346:                RB_FOREACH(s, sessions, &sessions) {
                   1347:                        if ((wm = session_has(s, w)) == NULL)
                   1348:                                continue;
                   1349:
                   1350:                        if (wm->window != wl->window)
                   1351:                                continue;
                   1352:                        if ((wm->flags & WINLINK_ALERTFLAGS) == 0)
                   1353:                                continue;
                   1354:
                   1355:                        wm->flags &= ~WINLINK_ALERTFLAGS;
1.98      nicm     1356:                        wm->window->flags &= ~WINDOW_ALERTFLAGS;
1.81      nicm     1357:                        server_status_session(s);
                   1358:                }
                   1359:        }
1.1       nicm     1360: }