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

1.57    ! nicm        1: /* $OpenBSD: screen-redraw.c,v 1.56 2019/03/12 20:02:47 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: struct screen_redraw_ctx {
                     27:        struct client   *c;
                     28:
                     29:        u_int            lines;
                     30:        int              top;
                     31:
                     32:        int              pane_status;
1.51      nicm       33:
                     34:        u_int            sx;
                     35:        u_int            sy;
1.55      nicm       36:        u_int            ox;
                     37:        u_int            oy;
1.50      nicm       38: };
                     39:
                     40: static void    screen_redraw_draw_borders(struct screen_redraw_ctx *);
                     41: static void    screen_redraw_draw_panes(struct screen_redraw_ctx *);
                     42: static void    screen_redraw_draw_status(struct screen_redraw_ctx *);
1.55      nicm       43: static void    screen_redraw_draw_pane(struct screen_redraw_ctx *,
                     44:                    struct window_pane *);
1.50      nicm       45: static void    screen_redraw_draw_number(struct screen_redraw_ctx *,
                     46:                    struct window_pane *);
1.1       nicm       47:
1.6       nicm       48: #define CELL_INSIDE 0
1.7       nicm       49: #define CELL_LEFTRIGHT 1
                     50: #define CELL_TOPBOTTOM 2
                     51: #define CELL_TOPLEFT 3
                     52: #define CELL_TOPRIGHT 4
                     53: #define CELL_BOTTOMLEFT 5
                     54: #define CELL_BOTTOMRIGHT 6
                     55: #define CELL_TOPJOIN 7
                     56: #define CELL_BOTTOMJOIN 8
                     57: #define CELL_LEFTJOIN 9
                     58: #define CELL_RIGHTJOIN 10
                     59: #define CELL_JOIN 11
                     60: #define CELL_OUTSIDE 12
1.6       nicm       61:
1.17      nicm       62: #define CELL_BORDERS " xqlkmjwvtun~"
                     63:
1.36      nicm       64: #define CELL_STATUS_OFF 0
                     65: #define CELL_STATUS_TOP 1
                     66: #define CELL_STATUS_BOTTOM 2
                     67:
1.15      nicm       68: /* Check if cell is on the border of a particular pane. */
1.39      nicm       69: static int
1.15      nicm       70: screen_redraw_cell_border1(struct window_pane *wp, u_int px, u_int py)
                     71: {
                     72:        /* Inside pane. */
                     73:        if (px >= wp->xoff && px < wp->xoff + wp->sx &&
                     74:            py >= wp->yoff && py < wp->yoff + wp->sy)
                     75:                return (0);
                     76:
                     77:        /* Left/right borders. */
                     78:        if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= wp->yoff + wp->sy) {
                     79:                if (wp->xoff != 0 && px == wp->xoff - 1)
                     80:                        return (1);
                     81:                if (px == wp->xoff + wp->sx)
1.36      nicm       82:                        return (2);
1.15      nicm       83:        }
                     84:
                     85:        /* Top/bottom borders. */
                     86:        if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= wp->xoff + wp->sx) {
                     87:                if (wp->yoff != 0 && py == wp->yoff - 1)
1.36      nicm       88:                        return (3);
1.15      nicm       89:                if (py == wp->yoff + wp->sy)
1.36      nicm       90:                        return (4);
1.15      nicm       91:        }
                     92:
                     93:        /* Outside pane. */
                     94:        return (-1);
                     95: }
                     96:
1.7       nicm       97: /* Check if a cell is on the pane border. */
1.39      nicm       98: static int
1.7       nicm       99: screen_redraw_cell_border(struct client *c, u_int px, u_int py)
1.1       nicm      100: {
                    101:        struct window           *w = c->session->curw->window;
                    102:        struct window_pane      *wp;
1.15      nicm      103:        int                      retval;
1.1       nicm      104:
1.7       nicm      105:        /* Check all the panes. */
1.1       nicm      106:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.3       nicm      107:                if (!window_pane_visible(wp))
                    108:                        continue;
1.15      nicm      109:                if ((retval = screen_redraw_cell_border1(wp, px, py)) != -1)
1.36      nicm      110:                        return (!!retval);
1.7       nicm      111:        }
                    112:
                    113:        return (0);
                    114: }
                    115:
                    116: /* Check if cell inside a pane. */
1.39      nicm      117: static int
1.36      nicm      118: screen_redraw_check_cell(struct client *c, u_int px, u_int py, int pane_status,
1.24      nicm      119:     struct window_pane **wpp)
1.7       nicm      120: {
                    121:        struct window           *w = c->session->curw->window;
                    122:        struct window_pane      *wp;
                    123:        int                      borders;
1.36      nicm      124:        u_int                    right, line;
1.7       nicm      125:
1.39      nicm      126:        *wpp = NULL;
                    127:
1.7       nicm      128:        if (px > w->sx || py > w->sy)
                    129:                return (CELL_OUTSIDE);
                    130:
1.36      nicm      131:        if (pane_status != CELL_STATUS_OFF) {
                    132:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    133:                        if (!window_pane_visible(wp))
                    134:                                continue;
                    135:
                    136:                        if (pane_status == CELL_STATUS_TOP)
                    137:                                line = wp->yoff - 1;
                    138:                        else
                    139:                                line = wp->yoff + wp->sy;
                    140:                        right = wp->xoff + 2 + wp->status_size - 1;
                    141:
                    142:                        if (py == line && px >= wp->xoff + 2 && px <= right)
                    143:                                return (CELL_INSIDE);
                    144:                }
                    145:        }
                    146:
1.7       nicm      147:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    148:                if (!window_pane_visible(wp))
                    149:                        continue;
1.24      nicm      150:                *wpp = wp;
1.7       nicm      151:
                    152:                /* If outside the pane and its border, skip it. */
                    153:                if ((wp->xoff != 0 && px < wp->xoff - 1) ||
                    154:                    px > wp->xoff + wp->sx ||
                    155:                    (wp->yoff != 0 && py < wp->yoff - 1) ||
                    156:                    py > wp->yoff + wp->sy)
                    157:                        continue;
                    158:
                    159:                /* If definitely inside, return so. */
                    160:                if (!screen_redraw_cell_border(c, px, py))
                    161:                        return (CELL_INSIDE);
                    162:
1.14      nicm      163:                /*
1.7       nicm      164:                 * Construct a bitmask of whether the cells to the left (bit
                    165:                 * 4), right, top, and bottom (bit 1) of this cell are borders.
                    166:                 */
                    167:                borders = 0;
                    168:                if (px == 0 || screen_redraw_cell_border(c, px - 1, py))
                    169:                        borders |= 8;
                    170:                if (px <= w->sx && screen_redraw_cell_border(c, px + 1, py))
                    171:                        borders |= 4;
1.36      nicm      172:                if (pane_status == CELL_STATUS_TOP) {
                    173:                        if (py != 0 && screen_redraw_cell_border(c, px, py - 1))
                    174:                                borders |= 2;
                    175:                } else {
                    176:                        if (py == 0 || screen_redraw_cell_border(c, px, py - 1))
                    177:                                borders |= 2;
                    178:                }
1.7       nicm      179:                if (py <= w->sy && screen_redraw_cell_border(c, px, py + 1))
                    180:                        borders |= 1;
                    181:
1.14      nicm      182:                /*
1.7       nicm      183:                 * Figure out what kind of border this cell is. Only one bit
                    184:                 * set doesn't make sense (can't have a border cell with no
                    185:                 * others connected).
                    186:                 */
                    187:                switch (borders) {
                    188:                case 15:        /* 1111, left right top bottom */
                    189:                        return (CELL_JOIN);
                    190:                case 14:        /* 1110, left right top */
                    191:                        return (CELL_BOTTOMJOIN);
                    192:                case 13:        /* 1101, left right bottom */
                    193:                        return (CELL_TOPJOIN);
                    194:                case 12:        /* 1100, left right */
                    195:                        return (CELL_TOPBOTTOM);
                    196:                case 11:        /* 1011, left top bottom */
                    197:                        return (CELL_RIGHTJOIN);
                    198:                case 10:        /* 1010, left top */
                    199:                        return (CELL_BOTTOMRIGHT);
                    200:                case 9:         /* 1001, left bottom */
                    201:                        return (CELL_TOPRIGHT);
                    202:                case 7:         /* 0111, right top bottom */
                    203:                        return (CELL_LEFTJOIN);
                    204:                case 6:         /* 0110, right top */
                    205:                        return (CELL_BOTTOMLEFT);
                    206:                case 5:         /* 0101, right bottom */
                    207:                        return (CELL_TOPLEFT);
                    208:                case 3:         /* 0011, top bottom */
                    209:                        return (CELL_LEFTRIGHT);
1.1       nicm      210:                }
                    211:        }
                    212:
1.6       nicm      213:        return (CELL_OUTSIDE);
1.1       nicm      214: }
                    215:
1.32      nicm      216: /* Check if the border of a particular pane. */
1.39      nicm      217: static int
1.36      nicm      218: screen_redraw_check_is(u_int px, u_int py, int type, int pane_status,
                    219:     struct window *w, struct window_pane *wantwp, struct window_pane *wp)
1.24      nicm      220: {
1.36      nicm      221:        int     border;
                    222:
1.24      nicm      223:        /* Is this off the active pane border? */
1.36      nicm      224:        border = screen_redraw_cell_border1(wantwp, px, py);
                    225:        if (border == 0 || border == -1)
                    226:                return (0);
                    227:        if (pane_status == CELL_STATUS_TOP && border == 4)
                    228:                return (0);
                    229:        if (pane_status == CELL_STATUS_BOTTOM && border == 3)
1.24      nicm      230:                return (0);
                    231:
                    232:        /* If there are more than two panes, that's enough. */
                    233:        if (window_count_panes(w) != 2)
                    234:                return (1);
                    235:
                    236:        /* Else if the cell is not a border cell, forget it. */
                    237:        if (wp == NULL || (type == CELL_OUTSIDE || type == CELL_INSIDE))
                    238:                return (1);
                    239:
1.36      nicm      240:        /* With status lines mark the entire line. */
                    241:        if (pane_status != CELL_STATUS_OFF)
                    242:                return (1);
                    243:
1.24      nicm      244:        /* Check if the pane covers the whole width. */
                    245:        if (wp->xoff == 0 && wp->sx == w->sx) {
                    246:                /* This can either be the top pane or the bottom pane. */
                    247:                if (wp->yoff == 0) { /* top pane */
1.32      nicm      248:                        if (wp == wantwp)
1.24      nicm      249:                                return (px <= wp->sx / 2);
                    250:                        return (px > wp->sx / 2);
                    251:                }
                    252:                return (0);
                    253:        }
                    254:
                    255:        /* Check if the pane covers the whole height. */
                    256:        if (wp->yoff == 0 && wp->sy == w->sy) {
                    257:                /* This can either be the left pane or the right pane. */
                    258:                if (wp->xoff == 0) { /* left pane */
1.32      nicm      259:                        if (wp == wantwp)
1.24      nicm      260:                                return (py <= wp->sy / 2);
                    261:                        return (py > wp->sy / 2);
                    262:                }
                    263:                return (0);
                    264:        }
                    265:
1.36      nicm      266:        return (1);
                    267: }
                    268:
                    269: /* Update pane status. */
1.39      nicm      270: static int
1.36      nicm      271: screen_redraw_make_pane_status(struct client *c, struct window *w,
                    272:     struct window_pane *wp)
                    273: {
                    274:        struct grid_cell         gc;
                    275:        const char              *fmt;
                    276:        struct format_tree      *ft;
                    277:        char                    *out;
1.40      nicm      278:        size_t                   outlen;
1.36      nicm      279:        struct screen_write_ctx  ctx;
1.40      nicm      280:        struct screen            old;
1.36      nicm      281:
                    282:        if (wp == w->active)
                    283:                style_apply(&gc, w->options, "pane-active-border-style");
                    284:        else
                    285:                style_apply(&gc, w->options, "pane-border-style");
                    286:
                    287:        fmt = options_get_string(w->options, "pane-border-format");
                    288:
1.46      nicm      289:        ft = format_create(c, NULL, FORMAT_PANE|wp->id, 0);
1.36      nicm      290:        format_defaults(ft, c, NULL, NULL, wp);
                    291:
1.40      nicm      292:        memcpy(&old, &wp->status_screen, sizeof old);
1.36      nicm      293:        screen_init(&wp->status_screen, wp->sx, 1, 0);
                    294:        wp->status_screen.mode = 0;
                    295:
                    296:        out = format_expand(ft, fmt);
                    297:        outlen = screen_write_cstrlen("%s", out);
                    298:        if (outlen > wp->sx - 4)
                    299:                outlen = wp->sx - 4;
                    300:        screen_resize(&wp->status_screen, outlen, 1, 0);
                    301:
                    302:        screen_write_start(&ctx, NULL, &wp->status_screen);
1.56      nicm      303:        screen_write_cursormove(&ctx, 0, 0, 0);
1.41      nicm      304:        screen_write_clearline(&ctx, 8);
1.36      nicm      305:        screen_write_cnputs(&ctx, outlen, &gc, "%s", out);
                    306:        screen_write_stop(&ctx);
                    307:
1.48      nicm      308:        free(out);
1.36      nicm      309:        format_free(ft);
                    310:
                    311:        wp->status_size = outlen;
1.40      nicm      312:
                    313:        if (grid_compare(wp->status_screen.grid, old.grid) == 0) {
                    314:                screen_free(&old);
                    315:                return (0);
                    316:        }
                    317:        screen_free(&old);
                    318:        return (1);
1.36      nicm      319: }
                    320:
                    321: /* Draw pane status. */
1.39      nicm      322: static void
1.52      nicm      323: screen_redraw_draw_pane_status(struct screen_redraw_ctx *ctx)
1.36      nicm      324: {
1.53      nicm      325:        struct client           *c = ctx->c;
1.36      nicm      326:        struct window           *w = c->session->curw->window;
                    327:        struct tty              *tty = &c->tty;
                    328:        struct window_pane      *wp;
1.55      nicm      329:        struct screen           *s;
                    330:        u_int                    i, x, width, xoff, yoff, size;
                    331:
                    332:        log_debug("%s: %s @%u", __func__, c->name, w->id);
1.36      nicm      333:
                    334:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.40      nicm      335:                if (!window_pane_visible(wp))
                    336:                        continue;
1.55      nicm      337:                s = &wp->status_screen;
                    338:
                    339:                size = wp->status_size;
1.52      nicm      340:                if (ctx->pane_status == CELL_STATUS_TOP)
1.36      nicm      341:                        yoff = wp->yoff - 1;
                    342:                else
                    343:                        yoff = wp->yoff + wp->sy;
1.55      nicm      344:                xoff = wp->xoff + 2;
                    345:
                    346:                if (xoff + size <= ctx->ox ||
                    347:                    xoff >= ctx->ox + ctx->sx ||
                    348:                    yoff < ctx->oy ||
                    349:                    yoff >= ctx->oy + ctx->sy)
                    350:                        continue;
                    351:
                    352:                if (xoff >= ctx->ox && xoff + size <= ctx->ox + ctx->sx) {
                    353:                        /* All visible. */
                    354:                        i = 0;
                    355:                        x = xoff - ctx->ox;
                    356:                        width = size;
                    357:                } else if (xoff < ctx->ox && xoff + size > ctx->ox + ctx->sx) {
                    358:                        /* Both left and right not visible. */
                    359:                        i = ctx->ox;
                    360:                        x = 0;
                    361:                        width = ctx->sx;
                    362:                } else if (xoff < ctx->ox) {
                    363:                        /* Left not visible. */
                    364:                        i = ctx->ox - xoff;
                    365:                        x = 0;
                    366:                        width = size - i;
                    367:                } else {
                    368:                        /* Right not visible. */
                    369:                        i = 0;
                    370:                        x = xoff - ctx->ox;
                    371:                        width = size - x;
                    372:                }
1.36      nicm      373:
1.55      nicm      374:                if (ctx->top)
                    375:                        yoff += ctx->lines;
                    376:                tty_draw_line(tty, NULL, s, i, 0, width, x, yoff - ctx->oy);
1.36      nicm      377:        }
                    378:        tty_cursor(tty, 0, 0);
1.24      nicm      379: }
                    380:
1.38      nicm      381: /* Update status line and change flags if unchanged. */
1.54      nicm      382: static int
                    383: screen_redraw_update(struct client *c, int flags)
1.38      nicm      384: {
                    385:        struct window           *w = c->session->curw->window;
                    386:        struct window_pane      *wp;
                    387:        struct options          *wo = w->options;
                    388:        int                      redraw;
                    389:
                    390:        if (c->message_string != NULL)
                    391:                redraw = status_message_redraw(c);
                    392:        else if (c->prompt_string != NULL)
                    393:                redraw = status_prompt_redraw(c);
                    394:        else
                    395:                redraw = status_redraw(c);
1.54      nicm      396:        if (!redraw && (~flags & CLIENT_REDRAWSTATUSALWAYS))
                    397:                flags &= ~CLIENT_REDRAWSTATUS;
1.38      nicm      398:
                    399:        if (options_get_number(wo, "pane-border-status") != CELL_STATUS_OFF) {
                    400:                redraw = 0;
                    401:                TAILQ_FOREACH(wp, &w->panes, entry) {
                    402:                        if (screen_redraw_make_pane_status(c, w, wp))
                    403:                                redraw = 1;
                    404:                }
                    405:                if (redraw)
1.54      nicm      406:                        flags |= CLIENT_REDRAWBORDERS;
1.38      nicm      407:        }
1.54      nicm      408:        return (flags);
1.38      nicm      409: }
                    410:
1.52      nicm      411: /* Set up redraw context. */
                    412: static void
                    413: screen_redraw_set_context(struct client *c, struct screen_redraw_ctx *ctx)
                    414: {
1.53      nicm      415:        struct session  *s = c->session;
                    416:        struct options  *oo = s->options;
                    417:        struct window   *w = s->curw->window;
                    418:        struct options  *wo = w->options;
1.52      nicm      419:
                    420:        memset(ctx, 0, sizeof *ctx);
                    421:        ctx->c = c;
                    422:
1.55      nicm      423:        ctx->lines = status_line_size(c);
                    424:        if (c->message_string != NULL || c->prompt_string != NULL)
                    425:                ctx->lines = (ctx->lines == 0) ? 1 : ctx->lines;
1.52      nicm      426:        if (ctx->lines != 0 && options_get_number(oo, "status-position") == 0)
                    427:                ctx->top = 1;
                    428:        ctx->pane_status = options_get_number(wo, "pane-border-status");
                    429:
1.55      nicm      430:        tty_window_offset(&c->tty, &ctx->ox, &ctx->oy, &ctx->sx, &ctx->sy);
                    431:
                    432:        log_debug("%s: %s @%u ox=%u oy=%u sx=%u sy=%u %u/%d", __func__, c->name,
                    433:            w->id, ctx->ox, ctx->oy, ctx->sx, ctx->sy, ctx->lines, ctx->top);
1.52      nicm      434: }
                    435:
1.4       nicm      436: /* Redraw entire screen. */
1.1       nicm      437: void
1.53      nicm      438: screen_redraw_screen(struct client *c)
1.1       nicm      439: {
1.53      nicm      440:        struct screen_redraw_ctx        ctx;
1.54      nicm      441:        int                             flags;
1.18      nicm      442:
                    443:        if (c->flags & CLIENT_SUSPENDED)
                    444:                return;
1.1       nicm      445:
1.54      nicm      446:        flags = screen_redraw_update(c, c->flags);
1.52      nicm      447:        screen_redraw_set_context(c, &ctx);
1.51      nicm      448:
1.54      nicm      449:        if (flags & (CLIENT_REDRAWWINDOW|CLIENT_REDRAWBORDERS)) {
1.53      nicm      450:                if (ctx.pane_status != CELL_STATUS_OFF)
                    451:                        screen_redraw_draw_pane_status(&ctx);
1.50      nicm      452:                screen_redraw_draw_borders(&ctx);
1.53      nicm      453:        }
1.54      nicm      454:        if (flags & CLIENT_REDRAWWINDOW)
1.50      nicm      455:                screen_redraw_draw_panes(&ctx);
1.54      nicm      456:        if (ctx.lines != 0 &&
                    457:            (flags & (CLIENT_REDRAWSTATUS|CLIENT_REDRAWSTATUSALWAYS)))
1.50      nicm      458:                screen_redraw_draw_status(&ctx);
1.52      nicm      459:        tty_reset(&c->tty);
1.26      nicm      460: }
1.4       nicm      461:
1.55      nicm      462: /* Redraw a single pane. */
1.26      nicm      463: void
                    464: screen_redraw_pane(struct client *c, struct window_pane *wp)
                    465: {
1.55      nicm      466:        struct screen_redraw_ctx         ctx;
1.26      nicm      467:
                    468:        if (!window_pane_visible(wp))
1.4       nicm      469:                return;
1.1       nicm      470:
1.55      nicm      471:        screen_redraw_set_context(c, &ctx);
1.26      nicm      472:
1.55      nicm      473:        screen_redraw_draw_pane(&ctx, wp);
1.26      nicm      474:        tty_reset(&c->tty);
                    475: }
                    476:
1.50      nicm      477: /* Draw a border cell. */
                    478: static void
1.55      nicm      479: screen_redraw_draw_borders_cell(struct screen_redraw_ctx *ctx, u_int i, u_int j,
                    480:     struct grid_cell *m_active_gc, struct grid_cell *active_gc,
                    481:     struct grid_cell *m_other_gc, struct grid_cell *other_gc)
1.50      nicm      482: {
1.53      nicm      483:        struct client           *c = ctx->c;
                    484:        struct session          *s = c->session;
                    485:        struct window           *w = s->curw->window;
                    486:        struct tty              *tty = &c->tty;
                    487:        struct window_pane      *wp;
                    488:        struct window_pane      *active = w->active;
                    489:        struct window_pane      *marked = marked_pane.wp;
1.55      nicm      490:        u_int                    type, x = ctx->ox + i, y = ctx->oy + j;
1.53      nicm      491:        int                      flag, pane_status = ctx->pane_status;
1.50      nicm      492:
                    493:        type = screen_redraw_check_cell(c, x, y, pane_status, &wp);
                    494:        if (type == CELL_INSIDE)
                    495:                return;
                    496:        flag = screen_redraw_check_is(x, y, type, pane_status, w, active, wp);
                    497:
                    498:        if (server_is_marked(s, s->curw, marked_pane.wp) &&
                    499:            screen_redraw_check_is(x, y, type, pane_status, w, marked, wp)) {
                    500:                if (flag)
                    501:                        tty_attributes(tty, m_active_gc, NULL);
                    502:                else
                    503:                        tty_attributes(tty, m_other_gc, NULL);
                    504:        } else if (flag)
                    505:                tty_attributes(tty, active_gc, NULL);
                    506:        else
                    507:                tty_attributes(tty, other_gc, NULL);
                    508:        if (ctx->top)
1.55      nicm      509:                tty_cursor(tty, i, ctx->lines + j);
1.50      nicm      510:        else
1.55      nicm      511:                tty_cursor(tty, i, j);
1.50      nicm      512:        tty_putc(tty, CELL_BORDERS[type]);
                    513: }
                    514:
1.26      nicm      515: /* Draw the borders. */
1.39      nicm      516: static void
1.50      nicm      517: screen_redraw_draw_borders(struct screen_redraw_ctx *ctx)
1.26      nicm      518: {
1.50      nicm      519:        struct client           *c = ctx->c;
1.32      nicm      520:        struct session          *s = c->session;
                    521:        struct window           *w = s->curw->window;
1.55      nicm      522:        struct tty              *tty = &c->tty;
1.33      nicm      523:        struct options          *oo = w->options;
1.32      nicm      524:        struct grid_cell         m_active_gc, active_gc, m_other_gc, other_gc;
1.55      nicm      525:        u_int                    i, j;
                    526:
                    527:        log_debug("%s: %s @%u", __func__, c->name, w->id);
1.26      nicm      528:
1.25      nicm      529:        style_apply(&other_gc, oo, "pane-border-style");
                    530:        style_apply(&active_gc, oo, "pane-active-border-style");
1.26      nicm      531:        active_gc.attr = other_gc.attr = GRID_ATTR_CHARSET;
1.15      nicm      532:
1.32      nicm      533:        memcpy(&m_other_gc, &other_gc, sizeof m_other_gc);
                    534:        m_other_gc.attr ^= GRID_ATTR_REVERSE;
                    535:        memcpy(&m_active_gc, &active_gc, sizeof m_active_gc);
                    536:        m_active_gc.attr ^= GRID_ATTR_REVERSE;
                    537:
1.55      nicm      538:        for (j = 0; j < tty->sy - ctx->lines; j++) {
                    539:                for (i = 0; i < tty->sx; i++) {
                    540:                        screen_redraw_draw_borders_cell(ctx, i, j,
                    541:                            &m_active_gc, &active_gc, &m_other_gc, &other_gc);
1.1       nicm      542:                }
1.28      nicm      543:        }
1.26      nicm      544: }
1.1       nicm      545:
1.26      nicm      546: /* Draw the panes. */
1.39      nicm      547: static void
1.50      nicm      548: screen_redraw_draw_panes(struct screen_redraw_ctx *ctx)
1.26      nicm      549: {
1.50      nicm      550:        struct client           *c = ctx->c;
1.26      nicm      551:        struct window           *w = c->session->curw->window;
                    552:        struct window_pane      *wp;
1.47      nicm      553:
1.55      nicm      554:        log_debug("%s: %s @%u", __func__, c->name, w->id);
                    555:
1.1       nicm      556:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.3       nicm      557:                if (!window_pane_visible(wp))
1.1       nicm      558:                        continue;
1.55      nicm      559:                screen_redraw_draw_pane(ctx, wp);
1.10      nicm      560:                if (c->flags & CLIENT_IDENTIFY)
1.50      nicm      561:                        screen_redraw_draw_number(ctx, wp);
1.1       nicm      562:        }
                    563: }
                    564:
1.26      nicm      565: /* Draw the status line. */
1.39      nicm      566: static void
1.50      nicm      567: screen_redraw_draw_status(struct screen_redraw_ctx *ctx)
1.1       nicm      568: {
1.50      nicm      569:        struct client   *c = ctx->c;
1.55      nicm      570:        struct window   *w = c->session->curw->window;
1.26      nicm      571:        struct tty      *tty = &c->tty;
1.57    ! nicm      572:        struct screen   *s = &c->status.screen;
1.47      nicm      573:        u_int            i, y;
1.23      nicm      574:
1.55      nicm      575:        log_debug("%s: %s @%u", __func__, c->name, w->id);
                    576:
1.50      nicm      577:        if (ctx->top)
1.47      nicm      578:                y = 0;
1.26      nicm      579:        else
1.55      nicm      580:                y = c->tty.sy - ctx->lines;
1.50      nicm      581:        for (i = 0; i < ctx->lines; i++)
1.55      nicm      582:                tty_draw_line(tty, NULL, s, 0, i, UINT_MAX, 0, y + i);
                    583: }
                    584:
                    585: /* Draw one pane. */
                    586: static void
                    587: screen_redraw_draw_pane(struct screen_redraw_ctx *ctx, struct window_pane *wp)
                    588: {
                    589:        struct client   *c = ctx->c;
                    590:        struct window   *w = c->session->curw->window;
                    591:        struct tty      *tty = &c->tty;
                    592:        struct screen   *s;
                    593:        u_int            i, j, top, x, y, width;
                    594:
                    595:        log_debug("%s: %s @%u %%%u", __func__, c->name, w->id, wp->id);
                    596:
                    597:        if (wp->xoff + wp->sx <= ctx->ox || wp->xoff >= ctx->ox + ctx->sx)
                    598:                return;
                    599:        if (ctx->top)
                    600:                top = ctx->lines;
                    601:        else
                    602:                top = 0;
                    603:
                    604:        s = wp->screen;
                    605:        for (j = 0; j < wp->sy; j++) {
                    606:                if (wp->yoff + j < ctx->oy || wp->yoff + j >= ctx->oy + ctx->sy)
                    607:                        continue;
                    608:                y = top + wp->yoff + j - ctx->oy;
                    609:
                    610:                if (wp->xoff >= ctx->ox &&
                    611:                    wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
                    612:                        /* All visible. */
                    613:                        i = 0;
                    614:                        x = wp->xoff - ctx->ox;
                    615:                        width = wp->sx;
                    616:                } else if (wp->xoff < ctx->ox &&
                    617:                    wp->xoff + wp->sx > ctx->ox + ctx->sx) {
                    618:                        /* Both left and right not visible. */
                    619:                        i = ctx->ox;
                    620:                        x = 0;
                    621:                        width = ctx->sx;
                    622:                } else if (wp->xoff < ctx->ox) {
                    623:                        /* Left not visible. */
                    624:                        i = ctx->ox - wp->xoff;
                    625:                        x = 0;
                    626:                        width = wp->sx - i;
                    627:                } else {
                    628:                        /* Right not visible. */
                    629:                        i = 0;
                    630:                        x = wp->xoff - ctx->ox;
                    631:                        width = ctx->sx - x;
                    632:                }
                    633:                log_debug("%s: %s %%%u line %u,%u at %u,%u, width %u",
                    634:                    __func__, c->name, wp->id, i, j, x, y, width);
                    635:
                    636:                tty_draw_line(tty, wp, s, i, j, width, x, y);
                    637:        }
1.10      nicm      638: }
                    639:
                    640: /* Draw number on a pane. */
1.39      nicm      641: static void
1.50      nicm      642: screen_redraw_draw_number(struct screen_redraw_ctx *ctx, struct window_pane *wp)
1.10      nicm      643: {
1.50      nicm      644:        struct client           *c = ctx->c;
1.10      nicm      645:        struct tty              *tty = &c->tty;
                    646:        struct session          *s = c->session;
1.33      nicm      647:        struct options          *oo = s->options;
1.16      nicm      648:        struct window           *w = wp->window;
1.10      nicm      649:        struct grid_cell         gc;
1.55      nicm      650:        u_int                    idx, px, py, i, j, xoff, yoff, sx, sy;
1.16      nicm      651:        int                      colour, active_colour;
1.10      nicm      652:        char                     buf[16], *ptr;
                    653:        size_t                   len;
                    654:
1.55      nicm      655:        if (wp->xoff + wp->sx <= ctx->ox ||
                    656:            wp->xoff >= ctx->ox + ctx->sx ||
                    657:            wp->yoff + wp->sy <= ctx->oy ||
                    658:            wp->yoff >= ctx->oy + ctx->sy)
                    659:                return;
                    660:
                    661:        if (wp->xoff >= ctx->ox && wp->xoff + wp->sx <= ctx->ox + ctx->sx) {
                    662:                /* All visible. */
                    663:                xoff = wp->xoff - ctx->ox;
                    664:                sx = wp->sx;
                    665:        } else if (wp->xoff < ctx->ox &&
                    666:            wp->xoff + wp->sx > ctx->ox + ctx->sx) {
                    667:                /* Both left and right not visible. */
                    668:                xoff = 0;
                    669:                sx = ctx->sx;
                    670:        } else if (wp->xoff < ctx->ox) {
                    671:                /* Left not visible. */
                    672:                xoff = 0;
                    673:                sx = wp->sx - (ctx->ox - wp->xoff);
                    674:        } else {
                    675:                /* Right not visible. */
                    676:                xoff = wp->xoff - ctx->ox;
                    677:                sx = wp->sx - xoff;
                    678:        }
                    679:        if (wp->yoff >= ctx->oy && wp->yoff + wp->sy <= ctx->oy + ctx->sy) {
                    680:                /* All visible. */
                    681:                yoff = wp->yoff - ctx->oy;
                    682:                sy = wp->sy;
                    683:        } else if (wp->yoff < ctx->oy &&
                    684:            wp->yoff + wp->sy > ctx->oy + ctx->sy) {
                    685:                /* Both top and bottom not visible. */
                    686:                yoff = 0;
                    687:                sy = ctx->sy;
                    688:        } else if (wp->yoff < ctx->oy) {
                    689:                /* Top not visible. */
                    690:                yoff = 0;
                    691:                sy = wp->sy - (ctx->oy - wp->yoff);
                    692:        } else {
                    693:                /* Bottom not visible. */
                    694:                yoff = wp->yoff - ctx->oy;
                    695:                sy = wp->sy - yoff;
                    696:        }
                    697:
                    698:        if (ctx->top)
                    699:                yoff += ctx->lines;
                    700:        px = sx / 2;
                    701:        py = sy / 2;
                    702:
1.19      nicm      703:        if (window_pane_index(wp, &idx) != 0)
                    704:                fatalx("index not found");
1.10      nicm      705:        len = xsnprintf(buf, sizeof buf, "%u", idx);
                    706:
1.55      nicm      707:        if (sx < len)
1.10      nicm      708:                return;
1.16      nicm      709:        colour = options_get_number(oo, "display-panes-colour");
                    710:        active_colour = options_get_number(oo, "display-panes-active-colour");
1.10      nicm      711:
1.55      nicm      712:        if (sx < len * 6 || sy < 5) {
1.12      nicm      713:                tty_cursor(tty, xoff + px - len / 2, yoff + py);
1.20      nicm      714:                goto draw_text;
1.10      nicm      715:        }
1.14      nicm      716:
1.10      nicm      717:        px -= len * 3;
                    718:        py -= 2;
                    719:
1.25      nicm      720:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.16      nicm      721:        if (w->active == wp)
1.37      nicm      722:                gc.bg = active_colour;
1.16      nicm      723:        else
1.37      nicm      724:                gc.bg = colour;
1.42      nicm      725:        gc.flags |= GRID_FLAG_NOPALETTE;
                    726:
1.30      nicm      727:        tty_attributes(tty, &gc, wp);
1.10      nicm      728:        for (ptr = buf; *ptr != '\0'; ptr++) {
                    729:                if (*ptr < '0' || *ptr > '9')
                    730:                        continue;
                    731:                idx = *ptr - '0';
1.14      nicm      732:
1.10      nicm      733:                for (j = 0; j < 5; j++) {
                    734:                        for (i = px; i < px + 5; i++) {
1.12      nicm      735:                                tty_cursor(tty, xoff + i, yoff + py + j);
1.27      nicm      736:                                if (window_clock_table[idx][j][i - px])
1.11      nicm      737:                                        tty_putc(tty, ' ');
1.10      nicm      738:                        }
                    739:                }
                    740:                px += 6;
                    741:        }
1.20      nicm      742:
                    743:        len = xsnprintf(buf, sizeof buf, "%ux%u", wp->sx, wp->sy);
1.55      nicm      744:        if (sx < len || sy < 6)
1.20      nicm      745:                return;
1.55      nicm      746:        tty_cursor(tty, xoff + sx - len, yoff);
1.20      nicm      747:
                    748: draw_text:
1.25      nicm      749:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.20      nicm      750:        if (w->active == wp)
1.37      nicm      751:                gc.fg = active_colour;
1.20      nicm      752:        else
1.37      nicm      753:                gc.fg = colour;
1.42      nicm      754:        gc.flags |= GRID_FLAG_NOPALETTE;
                    755:
1.30      nicm      756:        tty_attributes(tty, &gc, wp);
1.20      nicm      757:        tty_puts(tty, buf);
                    758:
                    759:        tty_cursor(tty, 0, 0);
1.1       nicm      760: }