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

1.15    ! nicm        1: /* $OpenBSD: screen-redraw.c,v 1.14 2009/12/03 22:50:10 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.1       nicm       27: int    screen_redraw_check_cell(struct client *, u_int, u_int);
1.10      nicm       28: void   screen_redraw_draw_number(struct client *, struct window_pane *);
1.1       nicm       29:
1.6       nicm       30: #define CELL_INSIDE 0
1.7       nicm       31: #define CELL_LEFTRIGHT 1
                     32: #define CELL_TOPBOTTOM 2
                     33: #define CELL_TOPLEFT 3
                     34: #define CELL_TOPRIGHT 4
                     35: #define CELL_BOTTOMLEFT 5
                     36: #define CELL_BOTTOMRIGHT 6
                     37: #define CELL_TOPJOIN 7
                     38: #define CELL_BOTTOMJOIN 8
                     39: #define CELL_LEFTJOIN 9
                     40: #define CELL_RIGHTJOIN 10
                     41: #define CELL_JOIN 11
                     42: #define CELL_OUTSIDE 12
1.6       nicm       43:
1.15    ! nicm       44: /* Check if cell is on the border of a particular pane. */
        !            45: int
        !            46: screen_redraw_cell_border1(struct window_pane *wp, u_int px, u_int py)
        !            47: {
        !            48:        /* Inside pane. */
        !            49:        if (px >= wp->xoff && px < wp->xoff + wp->sx &&
        !            50:            py >= wp->yoff && py < wp->yoff + wp->sy)
        !            51:                return (0);
        !            52:
        !            53:        /* Left/right borders. */
        !            54:        if ((wp->yoff == 0 || py >= wp->yoff - 1) && py <= wp->yoff + wp->sy) {
        !            55:                if (wp->xoff != 0 && px == wp->xoff - 1)
        !            56:                        return (1);
        !            57:                if (px == wp->xoff + wp->sx)
        !            58:                        return (1);
        !            59:        }
        !            60:
        !            61:        /* Top/bottom borders. */
        !            62:        if ((wp->xoff == 0 || px >= wp->xoff - 1) && px <= wp->xoff + wp->sx) {
        !            63:                if (wp->yoff != 0 && py == wp->yoff - 1)
        !            64:                        return (1);
        !            65:                if (py == wp->yoff + wp->sy)
        !            66:                        return (1);
        !            67:        }
        !            68:
        !            69:        /* Outside pane. */
        !            70:        return (-1);
        !            71: }
        !            72:
1.7       nicm       73: /* Check if a cell is on the pane border. */
1.1       nicm       74: int
1.7       nicm       75: screen_redraw_cell_border(struct client *c, u_int px, u_int py)
1.1       nicm       76: {
                     77:        struct window           *w = c->session->curw->window;
                     78:        struct window_pane      *wp;
1.15    ! nicm       79:        int                      retval;
1.1       nicm       80:
1.7       nicm       81:        /* Check all the panes. */
1.1       nicm       82:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.3       nicm       83:                if (!window_pane_visible(wp))
                     84:                        continue;
1.15    ! nicm       85:                if ((retval = screen_redraw_cell_border1(wp, px, py)) != -1)
        !            86:                        return (retval);
1.7       nicm       87:        }
                     88:
                     89:        return (0);
                     90: }
                     91:
                     92: /* Check if cell inside a pane. */
                     93: int
                     94: screen_redraw_check_cell(struct client *c, u_int px, u_int py)
                     95: {
                     96:        struct window           *w = c->session->curw->window;
                     97:        struct window_pane      *wp;
                     98:        int                      borders;
                     99:
                    100:        if (px > w->sx || py > w->sy)
                    101:                return (CELL_OUTSIDE);
                    102:
                    103:        TAILQ_FOREACH(wp, &w->panes, entry) {
                    104:                if (!window_pane_visible(wp))
                    105:                        continue;
                    106:
                    107:                /* If outside the pane and its border, skip it. */
                    108:                if ((wp->xoff != 0 && px < wp->xoff - 1) ||
                    109:                    px > wp->xoff + wp->sx ||
                    110:                    (wp->yoff != 0 && py < wp->yoff - 1) ||
                    111:                    py > wp->yoff + wp->sy)
                    112:                        continue;
                    113:
                    114:                /* If definitely inside, return so. */
                    115:                if (!screen_redraw_cell_border(c, px, py))
                    116:                        return (CELL_INSIDE);
                    117:
1.14      nicm      118:                /*
1.7       nicm      119:                 * Construct a bitmask of whether the cells to the left (bit
                    120:                 * 4), right, top, and bottom (bit 1) of this cell are borders.
                    121:                 */
                    122:                borders = 0;
                    123:                if (px == 0 || screen_redraw_cell_border(c, px - 1, py))
                    124:                        borders |= 8;
                    125:                if (px <= w->sx && screen_redraw_cell_border(c, px + 1, py))
                    126:                        borders |= 4;
                    127:                if (py == 0 || screen_redraw_cell_border(c, px, py - 1))
                    128:                        borders |= 2;
                    129:                if (py <= w->sy && screen_redraw_cell_border(c, px, py + 1))
                    130:                        borders |= 1;
                    131:
1.14      nicm      132:                /*
1.7       nicm      133:                 * Figure out what kind of border this cell is. Only one bit
                    134:                 * set doesn't make sense (can't have a border cell with no
                    135:                 * others connected).
                    136:                 */
                    137:                switch (borders) {
                    138:                case 15:        /* 1111, left right top bottom */
                    139:                        return (CELL_JOIN);
                    140:                case 14:        /* 1110, left right top */
                    141:                        return (CELL_BOTTOMJOIN);
                    142:                case 13:        /* 1101, left right bottom */
                    143:                        return (CELL_TOPJOIN);
                    144:                case 12:        /* 1100, left right */
                    145:                        return (CELL_TOPBOTTOM);
                    146:                case 11:        /* 1011, left top bottom */
                    147:                        return (CELL_RIGHTJOIN);
                    148:                case 10:        /* 1010, left top */
                    149:                        return (CELL_BOTTOMRIGHT);
                    150:                case 9:         /* 1001, left bottom */
                    151:                        return (CELL_TOPRIGHT);
                    152:                case 7:         /* 0111, right top bottom */
                    153:                        return (CELL_LEFTJOIN);
                    154:                case 6:         /* 0110, right top */
                    155:                        return (CELL_BOTTOMLEFT);
                    156:                case 5:         /* 0101, right bottom */
                    157:                        return (CELL_TOPLEFT);
                    158:                case 3:         /* 0011, top bottom */
                    159:                        return (CELL_LEFTRIGHT);
1.1       nicm      160:                }
                    161:        }
                    162:
1.6       nicm      163:        return (CELL_OUTSIDE);
1.1       nicm      164: }
                    165:
1.4       nicm      166: /* Redraw entire screen. */
1.1       nicm      167: void
1.15    ! nicm      168: screen_redraw_screen(struct client *c, int status_only, int borders_only)
1.1       nicm      169: {
                    170:        struct window           *w = c->session->curw->window;
                    171:        struct tty              *tty = &c->tty;
                    172:        struct window_pane      *wp;
1.15    ! nicm      173:        struct grid_cell         active_gc, other_gc;
1.6       nicm      174:        u_int                    i, j, type;
1.15    ! nicm      175:        int                      status, fg, bg;
1.8       nicm      176:        const u_char            *base, *ptr;
                    177:        u_char                   ch, border[20];
1.1       nicm      178:
                    179:        /* Get status line, er, status. */
1.4       nicm      180:        if (c->message_string != NULL || c->prompt_string != NULL)
                    181:                status = 1;
                    182:        else
                    183:                status = options_get_number(&c->session->options, "status");
                    184:
                    185:        /* If only drawing status and it is present, don't need the rest. */
                    186:        if (status_only && status) {
                    187:                tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
1.9       nicm      188:                tty_reset(tty);
1.4       nicm      189:                return;
                    190:        }
1.1       nicm      191:
1.15    ! nicm      192:        /* Set up pane border attributes. */
        !           193:        memcpy(&other_gc, &grid_default_cell, sizeof other_gc);
        !           194:        memcpy(&active_gc, &grid_default_cell, sizeof active_gc);
        !           195:        active_gc.data = other_gc.data = 'x'; /* not space */
        !           196:        fg = options_get_number(&c->session->options, "pane-border-fg");
        !           197:        colour_set_fg(&other_gc, fg);
        !           198:        bg = options_get_number(&c->session->options, "pane-border-bg");
        !           199:        colour_set_bg(&other_gc, bg);
        !           200:        fg = options_get_number(&c->session->options, "pane-active-border-fg");
        !           201:        colour_set_fg(&active_gc, fg);
        !           202:        bg = options_get_number(&c->session->options, "pane-active-border-bg");
        !           203:        colour_set_bg(&active_gc, bg);
        !           204:
1.6       nicm      205:        /* Draw background and borders. */
1.8       nicm      206:        strlcpy(border, " |-....--||+.", sizeof border);
1.1       nicm      207:        if (tty_term_has(tty->term, TTYC_ACSC)) {
1.8       nicm      208:                base = " xqlkmjwvtun~";
                    209:                for (ptr = base; *ptr != '\0'; ptr++) {
                    210:                        if ((ch = tty_get_acs(tty, *ptr)) != '\0')
                    211:                                border[ptr - base] = ch;
                    212:                }
1.15    ! nicm      213:                other_gc.attr |= GRID_ATTR_CHARSET;
        !           214:                active_gc.attr |= GRID_ATTR_CHARSET;
1.8       nicm      215:        }
1.1       nicm      216:        for (j = 0; j < tty->sy - status; j++) {
1.4       nicm      217:                if (status_only && j != tty->sy - 1)
                    218:                        continue;
1.1       nicm      219:                for (i = 0; i < tty->sx; i++) {
1.6       nicm      220:                        type = screen_redraw_check_cell(c, i, j);
1.15    ! nicm      221:                        if (type == CELL_INSIDE)
        !           222:                                continue;
        !           223:                        if (screen_redraw_cell_border1(w->active, i, j) == 1)
        !           224:                                tty_attributes(tty, &active_gc);
        !           225:                        else
        !           226:                                tty_attributes(tty, &other_gc);
        !           227:                        tty_cursor(tty, i, j);
        !           228:                        tty_putc(tty, border[type]);
1.1       nicm      229:                }
                    230:        }
                    231:
1.15    ! nicm      232:        /* If only drawing borders, that's it. */
        !           233:        if (borders_only)
        !           234:                return;
        !           235:
        !           236:        /* Draw the panes, if necessary. */
1.1       nicm      237:        TAILQ_FOREACH(wp, &w->panes, entry) {
1.3       nicm      238:                if (!window_pane_visible(wp))
1.1       nicm      239:                        continue;
1.4       nicm      240:                for (i = 0; i < wp->sy; i++) {
1.5       nicm      241:                        if (status_only && wp->yoff + i != tty->sy - 1)
1.4       nicm      242:                                continue;
                    243:                        tty_draw_line(tty, wp->screen, i, wp->xoff, wp->yoff);
                    244:                }
1.10      nicm      245:                if (c->flags & CLIENT_IDENTIFY)
                    246:                        screen_redraw_draw_number(c, wp);
1.1       nicm      247:        }
                    248:
                    249:        /* Draw the status line. */
1.4       nicm      250:        if (status)
                    251:                tty_draw_line(tty, &c->status, 0, 0, tty->sy - 1);
1.9       nicm      252:        tty_reset(tty);
1.1       nicm      253: }
                    254:
                    255: /* Draw a single pane. */
                    256: void
                    257: screen_redraw_pane(struct client *c, struct window_pane *wp)
                    258: {
                    259:        u_int   i;
                    260:
                    261:        for (i = 0; i < wp->sy; i++)
                    262:                tty_draw_line(&c->tty, wp->screen, i, wp->xoff, wp->yoff);
1.9       nicm      263:        tty_reset(&c->tty);
1.10      nicm      264: }
                    265:
                    266: /* Draw number on a pane. */
                    267: void
                    268: screen_redraw_draw_number(struct client *c, struct window_pane *wp)
                    269: {
                    270:        struct tty              *tty = &c->tty;
                    271:        struct session          *s = c->session;
                    272:        struct grid_cell         gc;
1.12      nicm      273:        u_int                    idx, px, py, i, j, xoff, yoff;
1.11      nicm      274:        int                      colour;
1.10      nicm      275:        char                     buf[16], *ptr;
                    276:        size_t                   len;
                    277:
                    278:        idx = window_pane_index(wp->window, wp);
                    279:        len = xsnprintf(buf, sizeof buf, "%u", idx);
                    280:
                    281:        if (wp->sx < len)
                    282:                return;
                    283:        colour = options_get_number(&s->options, "display-panes-colour");
                    284:
1.12      nicm      285:        px = wp->sx / 2; py = wp->sy / 2;
                    286:        xoff = wp->xoff; yoff = wp->yoff;
                    287:
1.10      nicm      288:        if (wp->sx < len * 6 || wp->sy < 5) {
1.12      nicm      289:                tty_cursor(tty, xoff + px - len / 2, yoff + py);
1.10      nicm      290:                memcpy(&gc, &grid_default_cell, sizeof gc);
1.13      nicm      291:                gc.data = '_'; /* not space */
1.11      nicm      292:                colour_set_fg(&gc, colour);
1.10      nicm      293:                tty_attributes(tty, &gc);
                    294:                tty_puts(tty, buf);
                    295:                return;
                    296:        }
1.14      nicm      297:
1.10      nicm      298:        px -= len * 3;
                    299:        py -= 2;
                    300:
                    301:        memcpy(&gc, &grid_default_cell, sizeof gc);
1.13      nicm      302:        gc.data = '_'; /* not space */
1.11      nicm      303:        colour_set_bg(&gc, colour);
1.10      nicm      304:        tty_attributes(tty, &gc);
                    305:        for (ptr = buf; *ptr != '\0'; ptr++) {
                    306:                if (*ptr < '0' || *ptr > '9')
                    307:                        continue;
                    308:                idx = *ptr - '0';
1.14      nicm      309:
1.10      nicm      310:                for (j = 0; j < 5; j++) {
                    311:                        for (i = px; i < px + 5; i++) {
1.12      nicm      312:                                tty_cursor(tty, xoff + i, yoff + py + j);
1.11      nicm      313:                                if (clock_table[idx][j][i - px])
                    314:                                        tty_putc(tty, ' ');
1.10      nicm      315:                        }
                    316:                }
                    317:                px += 6;
                    318:        }
1.1       nicm      319: }