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

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