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

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