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

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