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

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