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

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