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

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