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

1.25    ! nicm        1: /* $OpenBSD: screen-redraw.c,v 1.24 2013/03/25 11:41:49 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include <string.h>
                     22:
                     23: #include "tmux.h"
                     24:
1.15      nicm       25: int    screen_redraw_cell_border1(struct window_pane *, u_int, u_int);
1.7       nicm       26: int    screen_redraw_cell_border(struct client *, u_int, u_int);
1.24      nicm       27: int    screen_redraw_check_cell(struct client *, u_int, u_int,
                     28:            struct window_pane **);
                     29: int    screen_redraw_check_active(u_int, u_int, int, struct window *,
                     30:            struct window_pane *);
                     31:
1.10      nicm       32: void   screen_redraw_draw_number(struct client *, struct window_pane *);
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.15      nicm       50: /* Check if cell is on the border of a particular pane. */
                     51: int
                     52: screen_redraw_cell_border1(struct window_pane *wp, u_int px, u_int py)
                     53: {
                     54:        /* Inside pane. */
                     55:        if (px >= wp->xoff && px < wp->xoff + wp->sx &&
                     56:            py >= wp->yoff && py < wp->yoff + wp->sy)
                     57:                return (0);
                     58:
                     59:        /* Left/right borders. */
                     60:        if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= wp->yoff + wp->sy) {
                     61:                if (wp->xoff != 0 && px == wp->xoff - 1)
                     62:                        return (1);
                     63:                if (px == wp->xoff + wp->sx)
                     64:                        return (1);
                     65:        }
                     66:
                     67:        /* Top/bottom borders. */
                     68:        if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= wp->xoff + wp->sx) {
                     69:                if (wp->yoff != 0 && py == wp->yoff - 1)
                     70:                        return (1);
                     71:                if (py == wp->yoff + wp->sy)
                     72:                        return (1);
                     73:        }
                     74:
                     75:        /* Outside pane. */
                     76:        return (-1);
                     77: }
                     78:
1.7       nicm       79: /* Check if a cell is on the pane border. */
1.1       nicm       80: int
1.7       nicm       81: screen_redraw_cell_border(struct client *c, u_int px, u_int py)
1.1       nicm       82: {
                     83:        struct window           *w = c->session->curw->window;
                     84:        struct window_pane      *wp;
1.15      nicm       85:        int                      retval;
1.1       nicm       86:
1.7       nicm       87:        /* Check all the panes. */
1.1       nicm       88:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.3       nicm       89:                if (!window_pane_visible(wp))
                     90:                        continue;
1.15      nicm       91:                if ((retval = screen_redraw_cell_border1(wp, px, py)) != -1)
                     92:                        return (retval);
1.7       nicm       93:        }
                     94:
                     95:        return (0);
                     96: }
                     97:
                     98: /* Check if cell inside a pane. */
                     99: int
1.24      nicm      100: screen_redraw_check_cell(struct client *c, u_int px, u_int py,
                    101:     struct window_pane **wpp)
1.7       nicm      102: {
                    103:        struct window           *w = c->session->curw->window;
                    104:        struct window_pane      *wp;
                    105:        int                      borders;
                    106:
                    107:        if (px > w->sx || py > w->sy)
                    108:                return (CELL_OUTSIDE);
                    109:
                    110:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    111:                if (!window_pane_visible(wp))
                    112:                        continue;
1.24      nicm      113:                *wpp = wp;
1.7       nicm      114:
                    115:                /* If outside the pane and its border, skip it. */
                    116:                if ((wp->xoff != 0 && px < wp->xoff - 1) ||
                    117:                    px > wp->xoff + wp->sx ||
                    118:                    (wp->yoff != 0 && py < wp->yoff - 1) ||
                    119:                    py > wp->yoff + wp->sy)
                    120:                        continue;
                    121:
                    122:                /* If definitely inside, return so. */
                    123:                if (!screen_redraw_cell_border(c, px, py))
                    124:                        return (CELL_INSIDE);
                    125:
1.14      nicm      126:                /*
1.7       nicm      127:                 * Construct a bitmask of whether the cells to the left (bit
                    128:                 * 4), right, top, and bottom (bit 1) of this cell are borders.
                    129:                 */
                    130:                borders = 0;
                    131:                if (px == 0 || screen_redraw_cell_border(c, px - 1, py))
                    132:                        borders |= 8;
                    133:                if (px <= w->sx && screen_redraw_cell_border(c, px + 1, py))
                    134:                        borders |= 4;
                    135:                if (py == 0 || screen_redraw_cell_border(c, px, py - 1))
                    136:                        borders |= 2;
                    137:                if (py <= w->sy && screen_redraw_cell_border(c, px, py + 1))
                    138:                        borders |= 1;
                    139:
1.14      nicm      140:                /*
1.7       nicm      141:                 * Figure out what kind of border this cell is. Only one bit
                    142:                 * set doesn't make sense (can't have a border cell with no
                    143:                 * others connected).
                    144:                 */
                    145:                switch (borders) {
                    146:                case 15:        /* 1111, left right top bottom */
                    147:                        return (CELL_JOIN);
                    148:                case 14:        /* 1110, left right top */
                    149:                        return (CELL_BOTTOMJOIN);
                    150:                case 13:        /* 1101, left right bottom */
                    151:                        return (CELL_TOPJOIN);
                    152:                case 12:        /* 1100, left right */
                    153:                        return (CELL_TOPBOTTOM);
                    154:                case 11:        /* 1011, left top bottom */
                    155:                        return (CELL_RIGHTJOIN);
                    156:                case 10:        /* 1010, left top */
                    157:                        return (CELL_BOTTOMRIGHT);
                    158:                case 9:         /* 1001, left bottom */
                    159:                        return (CELL_TOPRIGHT);
                    160:                case 7:         /* 0111, right top bottom */
                    161:                        return (CELL_LEFTJOIN);
                    162:                case 6:         /* 0110, right top */
                    163:                        return (CELL_BOTTOMLEFT);
                    164:                case 5:         /* 0101, right bottom */
                    165:                        return (CELL_TOPLEFT);
                    166:                case 3:         /* 0011, top bottom */
                    167:                        return (CELL_LEFTRIGHT);
1.1       nicm      168:                }
                    169:        }
                    170:
1.24      nicm      171:        *wpp = NULL;
1.6       nicm      172:        return (CELL_OUTSIDE);
1.1       nicm      173: }
                    174:
1.24      nicm      175: /* Check active pane indicator. */
                    176: int
                    177: screen_redraw_check_active(u_int px, u_int py, int type, struct window *w,
                    178:     struct window_pane *wp)
                    179: {
                    180:        /* Is this off the active pane border? */
                    181:        if (screen_redraw_cell_border1(w->active, px, py) != 1)
                    182:                return (0);
                    183:
                    184:        /* If there are more than two panes, that's enough. */
                    185:        if (window_count_panes(w) != 2)
                    186:                return (1);
                    187:
                    188:        /* Else if the cell is not a border cell, forget it. */
                    189:        if (wp == NULL || (type == CELL_OUTSIDE || type == CELL_INSIDE))
                    190:                return (1);
                    191:
                    192:        /* Check if the pane covers the whole width. */
                    193:        if (wp->xoff == 0 && wp->sx == w->sx) {
                    194:                /* This can either be the top pane or the bottom pane. */
                    195:                if (wp->yoff == 0) { /* top pane */
                    196:                        if (wp == w->active)
                    197:                                return (px <= wp->sx / 2);
                    198:                        return (px > wp->sx / 2);
                    199:                }
                    200:                return (0);
                    201:        }
                    202:
                    203:        /* Check if the pane covers the whole height. */
                    204:        if (wp->yoff == 0 && wp->sy == w->sy) {
                    205:                /* This can either be the left pane or the right pane. */
                    206:                if (wp->xoff == 0) { /* left pane */
                    207:                        if (wp == w->active)
                    208:                                return (py <= wp->sy / 2);
                    209:                        return (py > wp->sy / 2);
                    210:                }
                    211:                return (0);
                    212:        }
                    213:
                    214:        return (type);
                    215: }
                    216:
1.4       nicm      217: /* Redraw entire screen. */
1.1       nicm      218: void
1.15      nicm      219: screen_redraw_screen(struct client *c, int status_only, int borders_only)
1.1       nicm      220: {
                    221:        struct window           *w = c->session->curw->window;
1.21      nicm      222:        struct options          *oo = &c->session->options;
1.1       nicm      223:        struct tty              *tty = &c->tty;
                    224:        struct window_pane      *wp;
1.15      nicm      225:        struct grid_cell         active_gc, other_gc;
1.21      nicm      226:        u_int                    i, j, type, top;
1.25    ! nicm      227:        int                      status, spos;
1.18      nicm      228:
                    229:        /* Suspended clients should not be updated. */
                    230:        if (c->flags & CLIENT_SUSPENDED)
                    231:                return;
1.1       nicm      232:
                    233:        /* Get status line, er, status. */
1.21      nicm      234:        spos = options_get_number(oo, "status-position");
1.4       nicm      235:        if (c->message_string != NULL || c->prompt_string != NULL)
                    236:                status = 1;
                    237:        else
1.21      nicm      238:                status = options_get_number(oo, "status");
                    239:        top = 0;
                    240:        if (status && spos == 0)
                    241:                top = 1;
1.4       nicm      242:
                    243:        /* If only drawing status and it is present, don't need the rest. */
                    244:        if (status_only && status) {
1.21      nicm      245:                if (top)
                    246:                        tty_draw_line(tty, &c->status, 0, 0, 0);
                    247:                else
                    248:                        tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
1.9       nicm      249:                tty_reset(tty);
1.4       nicm      250:                return;
                    251:        }
1.1       nicm      252:
1.15      nicm      253:        /* Set up pane border attributes. */
1.25    ! nicm      254:        style_apply(&other_gc, oo, "pane-border-style");
        !           255:        style_apply(&active_gc, oo, "pane-active-border-style");
        !           256:        active_gc.attr = other_gc.attr = GRID_ATTR_CHARSET; /* nuke existing */
1.15      nicm      257:
1.6       nicm      258:        /* Draw background and borders. */
1.1       nicm      259:        for (j = 0; j < tty->sy - status; j++) {
1.21      nicm      260:                if (status_only) {
                    261:                        if (spos == 1 && j != tty->sy - 1)
                    262:                                continue;
                    263:                        else if (spos == 0 && j != 0)
                    264:                                break;
                    265:                }
1.1       nicm      266:                for (i = 0; i < tty->sx; i++) {
1.24      nicm      267:                        type = screen_redraw_check_cell(c, i, j, &wp);
1.15      nicm      268:                        if (type == CELL_INSIDE)
                    269:                                continue;
1.24      nicm      270:                        if (screen_redraw_check_active(i, j, type, w, wp))
1.15      nicm      271:                                tty_attributes(tty, &active_gc);
                    272:                        else
                    273:                                tty_attributes(tty, &other_gc);
1.21      nicm      274:                        tty_cursor(tty, i, top + j);
1.17      nicm      275:                        tty_putc(tty, CELL_BORDERS[type]);
1.1       nicm      276:                }
                    277:        }
                    278:
1.15      nicm      279:        /* If only drawing borders, that's it. */
                    280:        if (borders_only)
                    281:                return;
                    282:
                    283:        /* Draw the panes, if necessary. */
1.1       nicm      284:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.3       nicm      285:                if (!window_pane_visible(wp))
1.1       nicm      286:                        continue;
1.4       nicm      287:                for (i = 0; i < wp->sy; i++) {
1.21      nicm      288:                        if (status_only) {
                    289:                                if (spos == 1 && wp->yoff + i != tty->sy - 1)
                    290:                                        continue;
                    291:                                else if (spos == 0 && wp->yoff + i != 0)
                    292:                                        break;
                    293:                        }
                    294:                        tty_draw_line(
                    295:                            tty, wp->screen, i, wp->xoff, top + wp->yoff);
1.4       nicm      296:                }
1.10      nicm      297:                if (c->flags & CLIENT_IDENTIFY)
                    298:                        screen_redraw_draw_number(c, wp);
1.1       nicm      299:        }
                    300:
                    301:        /* Draw the status line. */
1.21      nicm      302:        if (status) {
                    303:                if (top)
                    304:                        tty_draw_line(tty, &c->status, 0, 0, 0);
                    305:                else
                    306:                        tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
                    307:        }
1.9       nicm      308:        tty_reset(tty);
1.1       nicm      309: }
                    310:
                    311: /* Draw a single pane. */
                    312: void
                    313: screen_redraw_pane(struct client *c, struct window_pane *wp)
                    314: {
1.21      nicm      315:        u_int   i, yoff;
1.23      nicm      316:
                    317:        if (!window_pane_visible(wp))
                    318:                return;
1.21      nicm      319:
                    320:        yoff = wp->yoff;
                    321:        if (status_at_line(c) == 0)
                    322:                yoff++;
1.1       nicm      323:
                    324:        for (i = 0; i < wp->sy; i++)
1.21      nicm      325:                tty_draw_line(&c->tty, wp->screen, i, wp->xoff, yoff);
1.9       nicm      326:        tty_reset(&c->tty);
1.10      nicm      327: }
                    328:
                    329: /* Draw number on a pane. */
                    330: void
                    331: screen_redraw_draw_number(struct client *c, struct window_pane *wp)
                    332: {
                    333:        struct tty              *tty = &c->tty;
                    334:        struct session          *s = c->session;
1.19      nicm      335:        struct options          *oo = &s->options;
1.16      nicm      336:        struct window           *w = wp->window;
1.10      nicm      337:        struct grid_cell         gc;
1.12      nicm      338:        u_int                    idx, px, py, i, j, xoff, yoff;
1.16      nicm      339:        int                      colour, active_colour;
1.10      nicm      340:        char                     buf[16], *ptr;
                    341:        size_t                   len;
                    342:
1.19      nicm      343:        if (window_pane_index(wp, &idx) != 0)
                    344:                fatalx("index not found");
1.10      nicm      345:        len = xsnprintf(buf, sizeof buf, "%u", idx);
                    346:
                    347:        if (wp->sx < len)
                    348:                return;
1.16      nicm      349:        colour = options_get_number(oo, "display-panes-colour");
                    350:        active_colour = options_get_number(oo, "display-panes-active-colour");
1.10      nicm      351:
1.12      nicm      352:        px = wp->sx / 2; py = wp->sy / 2;
                    353:        xoff = wp->xoff; yoff = wp->yoff;
                    354:
1.10      nicm      355:        if (wp->sx < len * 6 || wp->sy < 5) {
1.12      nicm      356:                tty_cursor(tty, xoff + px - len / 2, yoff + py);
1.20      nicm      357:                goto draw_text;
1.10      nicm      358:        }
1.14      nicm      359:
1.10      nicm      360:        px -= len * 3;
                    361:        py -= 2;
                    362:
1.25    ! nicm      363:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.16      nicm      364:        if (w->active == wp)
                    365:                colour_set_bg(&gc, active_colour);
                    366:        else
                    367:                colour_set_bg(&gc, colour);
1.10      nicm      368:        tty_attributes(tty, &gc);
                    369:        for (ptr = buf; *ptr != '\0'; ptr++) {
                    370:                if (*ptr < '0' || *ptr > '9')
                    371:                        continue;
                    372:                idx = *ptr - '0';
1.14      nicm      373:
1.10      nicm      374:                for (j = 0; j < 5; j++) {
                    375:                        for (i = px; i < px + 5; i++) {
1.12      nicm      376:                                tty_cursor(tty, xoff + i, yoff + py + j);
1.11      nicm      377:                                if (clock_table[idx][j][i - px])
                    378:                                        tty_putc(tty, ' ');
1.10      nicm      379:                        }
                    380:                }
                    381:                px += 6;
                    382:        }
1.20      nicm      383:
                    384:        len = xsnprintf(buf, sizeof buf, "%ux%u", wp->sx, wp->sy);
                    385:        if (wp->sx < len || wp->sy < 6)
                    386:                return;
                    387:        tty_cursor(tty, xoff + wp->sx - len, yoff);
                    388:
                    389: draw_text:
1.25    ! nicm      390:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.20      nicm      391:        if (w->active == wp)
                    392:                colour_set_fg(&gc, active_colour);
                    393:        else
                    394:                colour_set_fg(&gc, colour);
                    395:        tty_attributes(tty, &gc);
                    396:        tty_puts(tty, buf);
                    397:
                    398:        tty_cursor(tty, 0, 0);
1.1       nicm      399: }