[BACK]Return to screen-redraw.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/screen-redraw.c, Revision 1.94

1.94    ! nicm        1: /* $OpenBSD: screen-redraw.c,v 1.93 2022/02/01 14:46:41 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.35      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.48      nicm       21: #include <stdlib.h>
1.1       nicm       22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
1.50      nicm       26: static void    screen_redraw_draw_borders(struct screen_redraw_ctx *);
                     27: static void    screen_redraw_draw_panes(struct screen_redraw_ctx *);
                     28: static void    screen_redraw_draw_status(struct screen_redraw_ctx *);
1.55      nicm       29: static void    screen_redraw_draw_pane(struct screen_redraw_ctx *,
                     30:                    struct window_pane *);
1.79      nicm       31: static void    screen_redraw_set_context(struct client *,
                     32:                    struct screen_redraw_ctx *);
1.1       nicm       33:
1.83      nicm       34: #define START_ISOLATE "\342\201\246"
                     35: #define END_ISOLATE   "\342\201\251"
                     36:
1.93      nicm       37: /* Border in relation to a pane. */
1.75      nicm       38: enum screen_redraw_border_type {
                     39:        SCREEN_REDRAW_OUTSIDE,
                     40:        SCREEN_REDRAW_INSIDE,
1.93      nicm       41:        SCREEN_REDRAW_BORDER_LEFT,
                     42:        SCREEN_REDRAW_BORDER_RIGHT,
                     43:        SCREEN_REDRAW_BORDER_TOP,
                     44:        SCREEN_REDRAW_BORDER_BOTTOM
1.75      nicm       45: };
1.93      nicm       46: #define BORDER_MARKERS "  +,.-"
1.75      nicm       47:
1.78      nicm       48: /* Get cell border character. */
                     49: static void
1.90      nicm       50: screen_redraw_border_set(struct window_pane *wp, enum pane_lines pane_lines,
                     51:     int cell_type, struct grid_cell *gc)
1.78      nicm       52: {
                     53:        u_int   idx;
                     54:
                     55:        switch (pane_lines) {
                     56:        case PANE_LINES_NUMBER:
                     57:                if (cell_type == CELL_OUTSIDE) {
                     58:                        gc->attr |= GRID_ATTR_CHARSET;
                     59:                        utf8_set(&gc->data, CELL_BORDERS[CELL_OUTSIDE]);
                     60:                        break;
                     61:                }
                     62:                gc->attr &= ~GRID_ATTR_CHARSET;
                     63:                if (wp != NULL && window_pane_index(wp, &idx) == 0)
                     64:                        utf8_set(&gc->data, '0' + (idx % 10));
                     65:                else
                     66:                        utf8_set(&gc->data, '*');
                     67:                break;
                     68:        case PANE_LINES_DOUBLE:
                     69:                gc->attr &= ~GRID_ATTR_CHARSET;
1.90      nicm       70:                utf8_copy(&gc->data, tty_acs_double_borders(cell_type));
1.78      nicm       71:                break;
                     72:        case PANE_LINES_HEAVY:
                     73:                gc->attr &= ~GRID_ATTR_CHARSET;
1.90      nicm       74:                utf8_copy(&gc->data, tty_acs_heavy_borders(cell_type));
1.78      nicm       75:                break;
                     76:        case PANE_LINES_SIMPLE:
                     77:                gc->attr &= ~GRID_ATTR_CHARSET;
1.90      nicm       78:                utf8_set(&gc->data, SIMPLE_BORDERS[cell_type]);
1.78      nicm       79:                break;
                     80:        default:
                     81:                gc->attr |= GRID_ATTR_CHARSET;
                     82:                utf8_set(&gc->data, CELL_BORDERS[cell_type]);
                     83:                break;
                     84:        }
                     85: }
                     86:
1.75      nicm       87: /* Return if window has only two panes. */
1.39      nicm       88: static int
1.75      nicm       89: screen_redraw_two_panes(struct window *w, int direction)
1.15      nicm       90: {
1.75      nicm       91:        struct window_pane      *wp;
                     92:
                     93:        wp = TAILQ_NEXT(TAILQ_FIRST(&w->panes), entry);
                     94:        if (wp == NULL)
                     95:                return (0); /* one pane */
                     96:        if (TAILQ_NEXT(wp, entry) != NULL)
                     97:                return (0); /* more than two panes */
                     98:        if (direction == 0 && wp->xoff == 0)
                     99:                return (0);
                    100:        if (direction == 1 && wp->yoff == 0)
                    101:                return (0);
                    102:        return (1);
                    103: }
                    104:
                    105: /* Check if cell is on the border of a pane. */
                    106: static enum screen_redraw_border_type
                    107: screen_redraw_pane_border(struct window_pane *wp, u_int px, u_int py,
                    108:     int pane_status)
                    109: {
1.93      nicm      110:        struct options  *oo = wp->window->options;
                    111:        int              split = 0;
                    112:        u_int            ex = wp->xoff + wp->sx, ey = wp->yoff + wp->sy;
1.75      nicm      113:
1.15      nicm      114:        /* Inside pane. */
1.75      nicm      115:        if (px >= wp->xoff && px < ex && py >= wp->yoff && py < ey)
                    116:                return (SCREEN_REDRAW_INSIDE);
1.15      nicm      117:
1.93      nicm      118:        /* Get pane indicator. */
                    119:        switch (options_get_number(oo, "pane-border-indicators")) {
                    120:        case PANE_BORDER_COLOUR:
                    121:        case PANE_BORDER_BOTH:
                    122:                split = 1;
                    123:                break;
                    124:        }
                    125:
1.15      nicm      126:        /* Left/right borders. */
1.75      nicm      127:        if (pane_status == PANE_STATUS_OFF) {
1.93      nicm      128:                if (screen_redraw_two_panes(wp->window, 0) && split) {
1.75      nicm      129:                        if (wp->xoff == 0 && px == wp->sx && py <= wp->sy / 2)
1.93      nicm      130:                                return (SCREEN_REDRAW_BORDER_RIGHT);
1.75      nicm      131:                        if (wp->xoff != 0 &&
                    132:                            px == wp->xoff - 1 &&
                    133:                            py > wp->sy / 2)
1.93      nicm      134:                                return (SCREEN_REDRAW_BORDER_LEFT);
1.75      nicm      135:                } else {
                    136:                        if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) {
                    137:                                if (wp->xoff != 0 && px == wp->xoff - 1)
1.93      nicm      138:                                        return (SCREEN_REDRAW_BORDER_LEFT);
1.75      nicm      139:                                if (px == ex)
1.93      nicm      140:                                        return (SCREEN_REDRAW_BORDER_RIGHT);
1.75      nicm      141:                        }
                    142:                }
                    143:        } else {
                    144:                if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= ey) {
                    145:                        if (wp->xoff != 0 && px == wp->xoff - 1)
1.93      nicm      146:                                return (SCREEN_REDRAW_BORDER_LEFT);
1.75      nicm      147:                        if (px == ex)
1.93      nicm      148:                                return (SCREEN_REDRAW_BORDER_RIGHT);
1.75      nicm      149:                }
1.15      nicm      150:        }
                    151:
                    152:        /* Top/bottom borders. */
1.75      nicm      153:        if (pane_status == PANE_STATUS_OFF) {
1.93      nicm      154:                if (screen_redraw_two_panes(wp->window, 1) && split) {
1.75      nicm      155:                        if (wp->yoff == 0 && py == wp->sy && px <= wp->sx / 2)
1.93      nicm      156:                                return (SCREEN_REDRAW_BORDER_BOTTOM);
1.75      nicm      157:                        if (wp->yoff != 0 &&
                    158:                            py == wp->yoff - 1 &&
                    159:                            px > wp->sx / 2)
1.93      nicm      160:                                return (SCREEN_REDRAW_BORDER_TOP);
1.75      nicm      161:                } else {
                    162:                        if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
                    163:                                if (wp->yoff != 0 && py == wp->yoff - 1)
1.93      nicm      164:                                        return (SCREEN_REDRAW_BORDER_TOP);
1.75      nicm      165:                                if (py == ey)
1.93      nicm      166:                                        return (SCREEN_REDRAW_BORDER_BOTTOM);
1.75      nicm      167:                        }
                    168:                }
                    169:        } else if (pane_status == PANE_STATUS_TOP) {
                    170:                if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
                    171:                        if (wp->yoff != 0 && py == wp->yoff - 1)
1.93      nicm      172:                                return (SCREEN_REDRAW_BORDER_TOP);
1.75      nicm      173:                }
                    174:        } else {
                    175:                if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= ex) {
                    176:                        if (py == ey)
1.93      nicm      177:                                return (SCREEN_REDRAW_BORDER_BOTTOM);
1.75      nicm      178:                }
1.15      nicm      179:        }
                    180:
                    181:        /* Outside pane. */
1.75      nicm      182:        return (SCREEN_REDRAW_OUTSIDE);
1.15      nicm      183: }
                    184:
1.75      nicm      185: /* Check if a cell is on a border. */
1.39      nicm      186: static int
1.75      nicm      187: screen_redraw_cell_border(struct client *c, u_int px, u_int py, int pane_status)
1.1       nicm      188: {
                    189:        struct window           *w = c->session->curw->window;
                    190:        struct window_pane      *wp;
1.75      nicm      191:
                    192:        /* Outside the window? */
                    193:        if (px > w->sx || py > w->sy)
                    194:                return (0);
                    195:
                    196:        /* On the window border? */
                    197:        if (px == w->sx || py == w->sy)
                    198:                return (1);
1.1       nicm      199:
1.7       nicm      200:        /* Check all the panes. */
1.1       nicm      201:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.3       nicm      202:                if (!window_pane_visible(wp))
                    203:                        continue;
1.75      nicm      204:                switch (screen_redraw_pane_border(wp, px, py, pane_status)) {
                    205:                case SCREEN_REDRAW_INSIDE:
                    206:                        return (0);
                    207:                case SCREEN_REDRAW_OUTSIDE:
                    208:                        break;
1.93      nicm      209:                default:
                    210:                        return (1);
1.75      nicm      211:                }
1.7       nicm      212:        }
                    213:
                    214:        return (0);
                    215: }
                    216:
1.75      nicm      217: /* Work out type of border cell from surrounding cells. */
                    218: static int
                    219: screen_redraw_type_of_cell(struct client *c, u_int px, u_int py,
                    220:     int pane_status)
                    221: {
                    222:        struct window   *w = c->session->curw->window;
                    223:        u_int            sx = w->sx, sy = w->sy;
                    224:        int              borders = 0;
                    225:
1.79      nicm      226:        /* Is this outside the window? */
1.81      nicm      227:        if (px > sx || py > sy)
1.79      nicm      228:                return (CELL_OUTSIDE);
                    229:
1.75      nicm      230:        /*
                    231:         * Construct a bitmask of whether the cells to the left (bit 4), right,
                    232:         * top, and bottom (bit 1) of this cell are borders.
                    233:         */
                    234:        if (px == 0 || screen_redraw_cell_border(c, px - 1, py, pane_status))
                    235:                borders |= 8;
                    236:        if (px <= sx && screen_redraw_cell_border(c, px + 1, py, pane_status))
                    237:                borders |= 4;
                    238:        if (pane_status == PANE_STATUS_TOP) {
                    239:                if (py != 0 &&
                    240:                    screen_redraw_cell_border(c, px, py - 1, pane_status))
                    241:                        borders |= 2;
1.79      nicm      242:                if (screen_redraw_cell_border(c, px, py + 1, pane_status))
                    243:                        borders |= 1;
1.80      nicm      244:        } else if (pane_status == PANE_STATUS_BOTTOM) {
1.75      nicm      245:                if (py == 0 ||
                    246:                    screen_redraw_cell_border(c, px, py - 1, pane_status))
1.79      nicm      247:                        borders |= 2;
                    248:                if (py != sy - 1 &&
                    249:                    screen_redraw_cell_border(c, px, py + 1, pane_status))
1.80      nicm      250:                        borders |= 1;
                    251:        } else {
                    252:                if (py == 0 ||
                    253:                    screen_redraw_cell_border(c, px, py - 1, pane_status))
                    254:                        borders |= 2;
                    255:                if (screen_redraw_cell_border(c, px, py + 1, pane_status))
1.79      nicm      256:                        borders |= 1;
1.75      nicm      257:        }
                    258:
                    259:        /*
                    260:         * Figure out what kind of border this cell is. Only one bit set
                    261:         * doesn't make sense (can't have a border cell with no others
                    262:         * connected).
                    263:         */
                    264:        switch (borders) {
                    265:        case 15:        /* 1111, left right top bottom */
                    266:                return (CELL_JOIN);
                    267:        case 14:        /* 1110, left right top */
                    268:                return (CELL_BOTTOMJOIN);
                    269:        case 13:        /* 1101, left right bottom */
                    270:                return (CELL_TOPJOIN);
                    271:        case 12:        /* 1100, left right */
1.83      nicm      272:                return (CELL_LEFTRIGHT);
1.75      nicm      273:        case 11:        /* 1011, left top bottom */
                    274:                return (CELL_RIGHTJOIN);
                    275:        case 10:        /* 1010, left top */
                    276:                return (CELL_BOTTOMRIGHT);
                    277:        case 9:         /* 1001, left bottom */
                    278:                return (CELL_TOPRIGHT);
                    279:        case 7:         /* 0111, right top bottom */
                    280:                return (CELL_LEFTJOIN);
                    281:        case 6:         /* 0110, right top */
                    282:                return (CELL_BOTTOMLEFT);
                    283:        case 5:         /* 0101, right bottom */
                    284:                return (CELL_TOPLEFT);
                    285:        case 3:         /* 0011, top bottom */
1.83      nicm      286:                return (CELL_TOPBOTTOM);
1.75      nicm      287:        }
                    288:        return (CELL_OUTSIDE);
                    289: }
                    290:
1.7       nicm      291: /* Check if cell inside a pane. */
1.39      nicm      292: static int
1.36      nicm      293: screen_redraw_check_cell(struct client *c, u_int px, u_int py, int pane_status,
1.24      nicm      294:     struct window_pane **wpp)
1.7       nicm      295: {
                    296:        struct window           *w = c->session->curw->window;
1.77      nicm      297:        struct window_pane      *wp, *active;
1.75      nicm      298:        int                      border;
1.36      nicm      299:        u_int                    right, line;
1.7       nicm      300:
1.39      nicm      301:        *wpp = NULL;
                    302:
1.81      nicm      303:        if (px > w->sx || py > w->sy)
1.7       nicm      304:                return (CELL_OUTSIDE);
1.75      nicm      305:        if (px == w->sx || py == w->sy) /* window border */
                    306:                return (screen_redraw_type_of_cell(c, px, py, pane_status));
1.7       nicm      307:
1.64      nicm      308:        if (pane_status != PANE_STATUS_OFF) {
1.77      nicm      309:                active = wp = server_client_get_pane(c);
1.75      nicm      310:                do {
1.36      nicm      311:                        if (!window_pane_visible(wp))
1.75      nicm      312:                                goto next1;
1.36      nicm      313:
1.64      nicm      314:                        if (pane_status == PANE_STATUS_TOP)
1.36      nicm      315:                                line = wp->yoff - 1;
                    316:                        else
                    317:                                line = wp->yoff + wp->sy;
                    318:                        right = wp->xoff + 2 + wp->status_size - 1;
                    319:
                    320:                        if (py == line && px >= wp->xoff + 2 && px <= right)
                    321:                                return (CELL_INSIDE);
1.75      nicm      322:
                    323:                next1:
                    324:                        wp = TAILQ_NEXT(wp, entry);
                    325:                        if (wp == NULL)
                    326:                                wp = TAILQ_FIRST(&w->panes);
1.77      nicm      327:                } while (wp != active);
1.36      nicm      328:        }
                    329:
1.77      nicm      330:        active = wp = server_client_get_pane(c);
1.75      nicm      331:        do {
1.7       nicm      332:                if (!window_pane_visible(wp))
1.75      nicm      333:                        goto next2;
1.24      nicm      334:                *wpp = wp;
1.7       nicm      335:
1.14      nicm      336:                /*
1.75      nicm      337:                 * If definitely inside, return. If not on border, skip.
                    338:                 * Otherwise work out the cell.
1.7       nicm      339:                 */
1.75      nicm      340:                border = screen_redraw_pane_border(wp, px, py, pane_status);
                    341:                if (border == SCREEN_REDRAW_INSIDE)
                    342:                        return (CELL_INSIDE);
                    343:                if (border == SCREEN_REDRAW_OUTSIDE)
                    344:                        goto next2;
                    345:                return (screen_redraw_type_of_cell(c, px, py, pane_status));
                    346:
                    347:        next2:
                    348:                wp = TAILQ_NEXT(wp, entry);
                    349:                if (wp == NULL)
                    350:                        wp = TAILQ_FIRST(&w->panes);
1.77      nicm      351:        } while (wp != active);
1.1       nicm      352:
1.6       nicm      353:        return (CELL_OUTSIDE);
1.1       nicm      354: }
                    355:
1.32      nicm      356: /* Check if the border of a particular pane. */
1.39      nicm      357: static int
1.75      nicm      358: screen_redraw_check_is(u_int px, u_int py, int pane_status,
                    359:     struct window_pane *wp)
1.24      nicm      360: {
1.75      nicm      361:        enum screen_redraw_border_type  border;
1.36      nicm      362:
1.75      nicm      363:        border = screen_redraw_pane_border(wp, px, py, pane_status);
1.93      nicm      364:        if (border != SCREEN_REDRAW_INSIDE && border != SCREEN_REDRAW_OUTSIDE)
1.24      nicm      365:                return (1);
1.75      nicm      366:        return (0);
1.36      nicm      367: }
                    368:
                    369: /* Update pane status. */
1.39      nicm      370: static int
1.79      nicm      371: screen_redraw_make_pane_status(struct client *c, struct window_pane *wp,
1.90      nicm      372:     struct screen_redraw_ctx *rctx, enum pane_lines pane_lines)
1.36      nicm      373: {
1.79      nicm      374:        struct window           *w = wp->window;
1.36      nicm      375:        struct grid_cell         gc;
                    376:        const char              *fmt;
                    377:        struct format_tree      *ft;
1.59      nicm      378:        char                    *expanded;
1.79      nicm      379:        int                      pane_status = rctx->pane_status;
1.82      nicm      380:        u_int                    width, i, cell_type, px, py;
1.36      nicm      381:        struct screen_write_ctx  ctx;
1.40      nicm      382:        struct screen            old;
1.36      nicm      383:
1.75      nicm      384:        ft = format_create(c, NULL, FORMAT_PANE|wp->id, FORMAT_STATUS);
                    385:        format_defaults(ft, c, c->session, c->session->curw, wp);
                    386:
1.77      nicm      387:        if (wp == server_client_get_pane(c))
1.75      nicm      388:                style_apply(&gc, w->options, "pane-active-border-style", ft);
1.36      nicm      389:        else
1.75      nicm      390:                style_apply(&gc, w->options, "pane-border-style", ft);
1.92      nicm      391:        fmt = options_get_string(wp->options, "pane-border-format");
1.36      nicm      392:
1.59      nicm      393:        expanded = format_expand_time(ft, fmt);
1.60      nicm      394:        if (wp->sx < 4)
                    395:                wp->status_size = width = 0;
                    396:        else
                    397:                wp->status_size = width = wp->sx - 4;
1.59      nicm      398:
1.40      nicm      399:        memcpy(&old, &wp->status_screen, sizeof old);
1.59      nicm      400:        screen_init(&wp->status_screen, width, 1, 0);
1.36      nicm      401:        wp->status_screen.mode = 0;
                    402:
1.76      nicm      403:        screen_write_start(&ctx, &wp->status_screen);
1.59      nicm      404:
1.79      nicm      405:        for (i = 0; i < width; i++) {
                    406:                px = wp->xoff + 2 + i;
                    407:                if (rctx->pane_status == PANE_STATUS_TOP)
1.82      nicm      408:                        py = wp->yoff - 1;
1.79      nicm      409:                else
1.82      nicm      410:                        py = wp->yoff + wp->sy;
1.79      nicm      411:                cell_type = screen_redraw_type_of_cell(c, px, py, pane_status);
                    412:                screen_redraw_border_set(wp, pane_lines, cell_type, &gc);
1.78      nicm      413:                screen_write_cell(&ctx, &gc);
1.79      nicm      414:        }
1.59      nicm      415:        gc.attr &= ~GRID_ATTR_CHARSET;
1.36      nicm      416:
1.56      nicm      417:        screen_write_cursormove(&ctx, 0, 0, 0);
1.91      nicm      418:        format_draw(&ctx, &gc, width, expanded, NULL, 0);
1.36      nicm      419:        screen_write_stop(&ctx);
                    420:
1.59      nicm      421:        free(expanded);
1.36      nicm      422:        format_free(ft);
1.40      nicm      423:
                    424:        if (grid_compare(wp->status_screen.grid, old.grid) == 0) {
                    425:                screen_free(&old);
                    426:                return (0);
                    427:        }
                    428:        screen_free(&old);
                    429:        return (1);
1.36      nicm      430: }
                    431:
                    432: /* Draw pane status. */
1.39      nicm      433: static void
1.52      nicm      434: screen_redraw_draw_pane_status(struct screen_redraw_ctx *ctx)
1.36      nicm      435: {
1.53      nicm      436:        struct client           *c = ctx->c;
1.36      nicm      437:        struct window           *w = c->session->curw->window;
                    438:        struct tty              *tty = &c->tty;
                    439:        struct window_pane      *wp;
1.55      nicm      440:        struct screen           *s;
                    441:        u_int                    i, x, width, xoff, yoff, size;
                    442:
                    443:        log_debug("%s: %s @%u", __func__, c->name, w->id);
1.36      nicm      444:
                    445:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.40      nicm      446:                if (!window_pane_visible(wp))
                    447:                        continue;
1.55      nicm      448:                s = &wp->status_screen;
                    449:
                    450:                size = wp->status_size;
1.64      nicm      451:                if (ctx->pane_status == PANE_STATUS_TOP)
1.36      nicm      452:                        yoff = wp->yoff - 1;
                    453:                else
                    454:                        yoff = wp->yoff + wp->sy;
1.55      nicm      455:                xoff = wp->xoff + 2;
                    456:
                    457:                if (xoff + size <= ctx->ox ||
                    458:                    xoff >= ctx->ox + ctx->sx ||
                    459:                    yoff < ctx->oy ||
                    460:                    yoff >= ctx->oy + ctx->sy)
                    461:                        continue;
                    462:
                    463:                if (xoff >= ctx->ox && xoff + size <= ctx->ox + ctx->sx) {
                    464:                        /* All visible. */
                    465:                        i = 0;
                    466:                        x = xoff - ctx->ox;
                    467:                        width = size;
                    468:                } else if (xoff < ctx->ox && xoff + size > ctx->ox + ctx->sx) {
                    469:                        /* Both left and right not visible. */
                    470:                        i = ctx->ox;
                    471:                        x = 0;
                    472:                        width = ctx->sx;
                    473:                } else if (xoff < ctx->ox) {
                    474:                        /* Left not visible. */
                    475:                        i = ctx->ox - xoff;
                    476:                        x = 0;
                    477:                        width = size - i;
                    478:                } else {
                    479:                        /* Right not visible. */
                    480:                        i = 0;
                    481:                        x = xoff - ctx->ox;
                    482:                        width = size - x;
                    483:                }
1.36      nicm      484:
1.61      nicm      485:                if (ctx->statustop)
                    486:                        yoff += ctx->statuslines;
1.76      nicm      487:                tty_draw_line(tty, s, i, 0, width, x, yoff - ctx->oy,
                    488:                    &grid_default_cell, NULL);
1.36      nicm      489:        }
                    490:        tty_cursor(tty, 0, 0);
1.24      nicm      491: }
                    492:
1.38      nicm      493: /* Update status line and change flags if unchanged. */
1.54      nicm      494: static int
                    495: screen_redraw_update(struct client *c, int flags)
1.38      nicm      496: {
1.90      nicm      497:        struct window                   *w = c->session->curw->window;
                    498:        struct window_pane              *wp;
                    499:        struct options                  *wo = w->options;
                    500:        int                              redraw;
                    501:        enum pane_lines                  lines;
                    502:        struct screen_redraw_ctx         ctx;
1.38      nicm      503:
                    504:        if (c->message_string != NULL)
                    505:                redraw = status_message_redraw(c);
                    506:        else if (c->prompt_string != NULL)
                    507:                redraw = status_prompt_redraw(c);
                    508:        else
                    509:                redraw = status_redraw(c);
1.54      nicm      510:        if (!redraw && (~flags & CLIENT_REDRAWSTATUSALWAYS))
                    511:                flags &= ~CLIENT_REDRAWSTATUS;
1.38      nicm      512:
1.63      nicm      513:        if (c->overlay_draw != NULL)
                    514:                flags |= CLIENT_REDRAWOVERLAY;
                    515:
1.64      nicm      516:        if (options_get_number(wo, "pane-border-status") != PANE_STATUS_OFF) {
1.79      nicm      517:                screen_redraw_set_context(c, &ctx);
1.78      nicm      518:                lines = options_get_number(wo, "pane-border-lines");
1.38      nicm      519:                redraw = 0;
                    520:                TAILQ_FOREACH(wp, &w->panes, entry) {
1.79      nicm      521:                        if (screen_redraw_make_pane_status(c, wp, &ctx, lines))
1.38      nicm      522:                                redraw = 1;
                    523:                }
                    524:                if (redraw)
1.54      nicm      525:                        flags |= CLIENT_REDRAWBORDERS;
1.38      nicm      526:        }
1.54      nicm      527:        return (flags);
1.38      nicm      528: }
                    529:
1.52      nicm      530: /* Set up redraw context. */
                    531: static void
                    532: screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
                    533: {
1.53      nicm      534:        struct session  *s = c->session;
                    535:        struct options  *oo = s->options;
                    536:        struct window   *w = s->curw->window;
                    537:        struct options  *wo = w->options;
1.61      nicm      538:        u_int            lines;
1.52      nicm      539:
                    540:        memset(ctx, 0, sizeof *ctx);
                    541:        ctx->c = c;
                    542:
1.61      nicm      543:        lines = status_line_size(c);
1.55      nicm      544:        if (c->message_string != NULL || c->prompt_string != NULL)
1.61      nicm      545:                lines = (lines == 0) ? 1 : lines;
                    546:        if (lines != 0 && options_get_number(oo, "status-position") == 0)
                    547:                ctx->statustop = 1;
                    548:        ctx->statuslines = lines;
                    549:
1.52      nicm      550:        ctx->pane_status = options_get_number(wo, "pane-border-status");
1.78      nicm      551:        ctx->pane_lines = options_get_number(wo, "pane-border-lines");
1.52      nicm      552:
1.55      nicm      553:        tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
                    554:
                    555:        log_debug("%s: %s @%u ox=%u oy=%u sx=%u sy=%u %u/%d", __func__, c->name,
1.61      nicm      556:            w->id, ctx->ox, ctx->oy, ctx->sx, ctx->sy, ctx->statuslines,
                    557:            ctx->statustop);
1.52      nicm      558: }
                    559:
1.4       nicm      560: /* Redraw entire screen. */
1.1       nicm      561: void
1.53      nicm      562: screen_redraw_screen(struct client *c)
1.1       nicm      563: {
1.53      nicm      564:        struct screen_redraw_ctx        ctx;
1.54      nicm      565:        int                             flags;
1.18      nicm      566:
                    567:        if (c->flags & CLIENT_SUSPENDED)
                    568:                return;
1.1       nicm      569:
1.54      nicm      570:        flags = screen_redraw_update(c, c->flags);
1.74      nicm      571:        if ((flags & CLIENT_ALLREDRAWFLAGS) == 0)
                    572:                return;
                    573:
1.52      nicm      574:        screen_redraw_set_context(c, &ctx);
1.84      nicm      575:        tty_sync_start(&c->tty);
1.74      nicm      576:        tty_update_mode(&c->tty, c->tty.mode, NULL);
1.51      nicm      577:
1.54      nicm      578:        if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) {
1.70      nicm      579:                log_debug("%s: redrawing borders", c->name);
1.64      nicm      580:                if (ctx.pane_status != PANE_STATUS_OFF)
1.53      nicm      581:                        screen_redraw_draw_pane_status(&ctx);
1.50      nicm      582:                screen_redraw_draw_borders(&ctx);
1.53      nicm      583:        }
1.70      nicm      584:        if (flags & CLIENT_REDRAWWINDOW) {
                    585:                log_debug("%s: redrawing panes", c->name);
1.50      nicm      586:                screen_redraw_draw_panes(&ctx);
1.70      nicm      587:        }
1.61      nicm      588:        if (ctx.statuslines != 0 &&
1.70      nicm      589:            (flags & (CLIENT_REDRAWSTATUS|CLIENT_REDRAWSTATUSALWAYS))) {
                    590:                log_debug("%s: redrawing status", c->name);
1.50      nicm      591:                screen_redraw_draw_status(&ctx);
1.70      nicm      592:        }
                    593:        if (c->overlay_draw != NULL && (flags & CLIENT_REDRAWOVERLAY)) {
                    594:                log_debug("%s: redrawing overlay", c->name);
1.88      nicm      595:                c->overlay_draw(c, c->overlay_data, &ctx);
1.70      nicm      596:        }
1.67      nicm      597:
1.52      nicm      598:        tty_reset(&c->tty);
1.26      nicm      599: }
1.4       nicm      600:
1.55      nicm      601: /* Redraw a single pane. */
1.26      nicm      602: void
                    603: screen_redraw_pane(struct client *c, struct window_pane *wp)
                    604: {
1.55      nicm      605:        struct screen_redraw_ctx         ctx;
1.26      nicm      606:
1.85      nicm      607:        if (!window_pane_visible(wp))
1.4       nicm      608:                return;
1.1       nicm      609:
1.55      nicm      610:        screen_redraw_set_context(c, &ctx);
1.84      nicm      611:        tty_sync_start(&c->tty);
1.74      nicm      612:        tty_update_mode(&c->tty, c->tty.mode, NULL);
1.26      nicm      613:
1.55      nicm      614:        screen_redraw_draw_pane(&ctx, wp);
1.67      nicm      615:
1.26      nicm      616:        tty_reset(&c->tty);
                    617: }
                    618:
1.75      nicm      619: /* Get border cell style. */
                    620: static const struct grid_cell *
                    621: screen_redraw_draw_borders_style(struct screen_redraw_ctx *ctx, u_int x,
                    622:     u_int y, struct window_pane *wp)
                    623: {
                    624:        struct client           *c = ctx->c;
                    625:        struct session          *s = c->session;
                    626:        struct window           *w = s->curw->window;
1.77      nicm      627:        struct window_pane      *active = server_client_get_pane(c);
1.75      nicm      628:        struct options          *oo = w->options;
                    629:        struct format_tree      *ft;
                    630:
                    631:        if (wp->border_gc_set)
                    632:                return (&wp->border_gc);
                    633:        wp->border_gc_set = 1;
                    634:
                    635:        ft = format_create_defaults(NULL, c, s, s->curw, wp);
1.78      nicm      636:        if (screen_redraw_check_is(x, y, ctx->pane_status, active))
                    637:                style_apply(&wp->border_gc, oo, "pane-active-border-style", ft);
                    638:        else
                    639:                style_apply(&wp->border_gc, oo, "pane-border-style", ft);
                    640:        format_free(ft);
1.75      nicm      641:
1.78      nicm      642:        return (&wp->border_gc);
1.75      nicm      643: }
                    644:
1.50      nicm      645: /* Draw a border cell. */
                    646: static void
1.75      nicm      647: screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j)
1.50      nicm      648: {
1.53      nicm      649:        struct client           *c = ctx->c;
                    650:        struct session          *s = c->session;
1.86      nicm      651:        struct window           *w = s->curw->window;
                    652:        struct options          *oo = w->options;
1.53      nicm      653:        struct tty              *tty = &c->tty;
1.86      nicm      654:        struct format_tree      *ft;
1.93      nicm      655:        struct window_pane      *wp, *active = server_client_get_pane(c);
1.89      nicm      656:        struct grid_cell         gc;
                    657:        const struct grid_cell  *tmp;
                    658:        struct overlay_ranges    r;
1.78      nicm      659:        u_int                    cell_type, x = ctx->ox + i, y = ctx->oy + j;
1.93      nicm      660:        int                      arrows = 0, border;
1.83      nicm      661:        int                      pane_status = ctx->pane_status, isolates;
1.50      nicm      662:
1.89      nicm      663:        if (c->overlay_check != NULL) {
                    664:                c->overlay_check(c, c->overlay_data, x, y, 1, &r);
                    665:                if (r.nx[0] + r.nx[1] == 0)
                    666:                        return;
                    667:        }
1.75      nicm      668:
1.78      nicm      669:        cell_type = screen_redraw_check_cell(c, x, y, pane_status, &wp);
                    670:        if (cell_type == CELL_INSIDE)
1.50      nicm      671:                return;
                    672:
1.86      nicm      673:        if (wp == NULL) {
                    674:                if (!ctx->no_pane_gc_set) {
                    675:                        ft = format_create_defaults(NULL, c, s, s->curw, NULL);
                    676:                        memcpy(&ctx->no_pane_gc, &grid_default_cell, sizeof gc);
                    677:                        style_add(&ctx->no_pane_gc, oo, "pane-border-style",
                    678:                            ft);
                    679:                        format_free(ft);
                    680:                        ctx->no_pane_gc_set = 1;
                    681:                }
                    682:                memcpy(&gc, &ctx->no_pane_gc, sizeof gc);
                    683:        } else {
1.78      nicm      684:                tmp = screen_redraw_draw_borders_style(ctx, x, y, wp);
                    685:                if (tmp == NULL)
1.75      nicm      686:                        return;
1.78      nicm      687:                memcpy(&gc, tmp, sizeof gc);
1.75      nicm      688:
                    689:                if (server_is_marked(s, s->curw, marked_pane.wp) &&
1.78      nicm      690:                    screen_redraw_check_is(x, y, pane_status, marked_pane.wp))
                    691:                        gc.attr ^= GRID_ATTR_REVERSE;
1.75      nicm      692:        }
1.78      nicm      693:        screen_redraw_border_set(wp, ctx->pane_lines, cell_type, &gc);
1.75      nicm      694:
1.83      nicm      695:        if (cell_type == CELL_TOPBOTTOM &&
                    696:            (c->flags & CLIENT_UTF8) &&
                    697:            tty_term_has(tty->term, TTYC_BIDI))
                    698:                isolates = 1;
                    699:        else
                    700:                isolates = 0;
                    701:
1.61      nicm      702:        if (ctx->statustop)
                    703:                tty_cursor(tty, i, ctx->statuslines + j);
1.50      nicm      704:        else
1.55      nicm      705:                tty_cursor(tty, i, j);
1.83      nicm      706:        if (isolates)
                    707:                tty_puts(tty, END_ISOLATE);
1.93      nicm      708:
                    709:        switch (options_get_number(oo, "pane-border-indicators")) {
                    710:        case PANE_BORDER_ARROWS:
                    711:        case PANE_BORDER_BOTH:
                    712:                arrows = 1;
                    713:                break;
                    714:        }
                    715:
                    716:        if (wp != NULL && arrows) {
                    717:                border = screen_redraw_pane_border(active, x, y, pane_status);
                    718:                if (((i == wp->xoff + 1 &&
                    719:                    (cell_type == CELL_LEFTRIGHT ||
                    720:                    (cell_type == CELL_TOPJOIN &&
                    721:                    border == SCREEN_REDRAW_BORDER_BOTTOM) ||
                    722:                    (cell_type == CELL_BOTTOMJOIN &&
                    723:                    border == SCREEN_REDRAW_BORDER_TOP))) ||
                    724:                    (j == wp->yoff + 1 &&
                    725:                    (cell_type == CELL_TOPBOTTOM ||
                    726:                    (cell_type == CELL_LEFTJOIN &&
                    727:                    border == SCREEN_REDRAW_BORDER_RIGHT) ||
                    728:                    (cell_type == CELL_RIGHTJOIN &&
                    729:                    border == SCREEN_REDRAW_BORDER_LEFT)))) &&
1.94    ! nicm      730:                    screen_redraw_check_is(x, y, pane_status, active)) {
        !           731:                        gc.attr |= GRID_ATTR_CHARSET;
1.93      nicm      732:                        utf8_set(&gc.data, BORDER_MARKERS[border]);
1.94    ! nicm      733:                }
1.93      nicm      734:        }
                    735:
1.78      nicm      736:        tty_cell(tty, &gc, &grid_default_cell, NULL);
1.83      nicm      737:        if (isolates)
                    738:                tty_puts(tty, START_ISOLATE);
1.50      nicm      739: }
                    740:
1.26      nicm      741: /* Draw the borders. */
1.39      nicm      742: static void
1.50      nicm      743: screen_redraw_draw_borders(struct screen_redraw_ctx *ctx)
1.26      nicm      744: {
1.50      nicm      745:        struct client           *c = ctx->c;
1.32      nicm      746:        struct session          *s = c->session;
                    747:        struct window           *w = s->curw->window;
1.75      nicm      748:        struct window_pane      *wp;
1.55      nicm      749:        u_int                    i, j;
                    750:
                    751:        log_debug("%s: %s @%u", __func__, c->name, w->id);
1.26      nicm      752:
1.75      nicm      753:        TAILQ_FOREACH(wp, &w->panes, entry)
                    754:                wp->border_gc_set = 0;
                    755:
                    756:        for (j = 0; j < c->tty.sy - ctx->statuslines; j++) {
                    757:                for (i = 0; i < c->tty.sx; i++)
                    758:                        screen_redraw_draw_borders_cell(ctx, i, j);
1.28      nicm      759:        }
1.26      nicm      760: }
1.1       nicm      761:
1.26      nicm      762: /* Draw the panes. */
1.39      nicm      763: static void
1.50      nicm      764: screen_redraw_draw_panes(struct screen_redraw_ctx *ctx)
1.26      nicm      765: {
1.50      nicm      766:        struct client           *c = ctx->c;
1.26      nicm      767:        struct window           *w = c->session->curw->window;
                    768:        struct window_pane      *wp;
1.47      nicm      769:
1.55      nicm      770:        log_debug("%s: %s @%u", __func__, c->name, w->id);
                    771:
1.1       nicm      772:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.61      nicm      773:                if (window_pane_visible(wp))
                    774:                        screen_redraw_draw_pane(ctx, wp);
1.1       nicm      775:        }
                    776: }
                    777:
1.26      nicm      778: /* Draw the status line. */
1.39      nicm      779: static void
1.50      nicm      780: screen_redraw_draw_status(struct screen_redraw_ctx *ctx)
1.1       nicm      781: {
1.50      nicm      782:        struct client   *c = ctx->c;
1.55      nicm      783:        struct window   *w = c->session->curw->window;
1.26      nicm      784:        struct tty      *tty = &c->tty;
1.58      nicm      785:        struct screen   *s = c->status.active;
1.47      nicm      786:        u_int            i, y;
1.23      nicm      787:
1.55      nicm      788:        log_debug("%s: %s @%u", __func__, c->name, w->id);
                    789:
1.61      nicm      790:        if (ctx->statustop)
1.47      nicm      791:                y = 0;
1.26      nicm      792:        else
1.61      nicm      793:                y = c->tty.sy - ctx->statuslines;
1.76      nicm      794:        for (i = 0; i < ctx->statuslines; i++) {
                    795:                tty_draw_line(tty, s, 0, i, UINT_MAX, 0, y + i,
                    796:                    &grid_default_cell, NULL);
                    797:        }
1.55      nicm      798: }
                    799:
                    800: /* Draw one pane. */
                    801: static void
                    802: screen_redraw_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
                    803: {
1.87      nicm      804:        struct client           *c = ctx->c;
                    805:        struct window           *w = c->session->curw->window;
                    806:        struct tty              *tty = &c->tty;
                    807:        struct screen           *s = wp->screen;
                    808:        struct colour_palette   *palette = &wp->palette;
                    809:        struct grid_cell         defaults;
                    810:        u_int                    i, j, top, x, y, width;
1.55      nicm      811:
                    812:        log_debug("%s: %s @%u %%%u", __func__, c->name, w->id, wp->id);
                    813:
                    814:        if (wp->xoff + wp->sx <= ctx->ox || wp->xoff >= ctx->ox + ctx->sx)
                    815:                return;
1.61      nicm      816:        if (ctx->statustop)
                    817:                top = ctx->statuslines;
1.55      nicm      818:        else
                    819:                top = 0;
                    820:        for (j = 0; j < wp->sy; j++) {
                    821:                if (wp->yoff + j < ctx->oy || wp->yoff + j >= ctx->oy + ctx->sy)
                    822:                        continue;
                    823:                y = top + wp->yoff + j - ctx->oy;
                    824:
                    825:                if (wp->xoff >= ctx->ox &&
                    826:                    wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
                    827:                        /* All visible. */
                    828:                        i = 0;
                    829:                        x = wp->xoff - ctx->ox;
                    830:                        width = wp->sx;
                    831:                } else if (wp->xoff < ctx->ox &&
                    832:                    wp->xoff + wp->sx > ctx->ox + ctx->sx) {
                    833:                        /* Both left and right not visible. */
                    834:                        i = ctx->ox;
                    835:                        x = 0;
                    836:                        width = ctx->sx;
                    837:                } else if (wp->xoff < ctx->ox) {
                    838:                        /* Left not visible. */
                    839:                        i = ctx->ox - wp->xoff;
                    840:                        x = 0;
                    841:                        width = wp->sx - i;
                    842:                } else {
                    843:                        /* Right not visible. */
                    844:                        i = 0;
                    845:                        x = wp->xoff - ctx->ox;
                    846:                        width = ctx->sx - x;
                    847:                }
                    848:                log_debug("%s: %s %%%u line %u,%u at %u,%u, width %u",
                    849:                    __func__, c->name, wp->id, i, j, x, y, width);
                    850:
1.76      nicm      851:                tty_default_colours(&defaults, wp);
1.87      nicm      852:                tty_draw_line(tty, s, i, j, width, x, y, &defaults, palette);
1.55      nicm      853:        }
1.1       nicm      854: }