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

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