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

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