[BACK]Return to screen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/screen.c, Revision 1.42

1.42    ! nicm        1: /* $OpenBSD: screen.c,v 1.41 2016/10/10 21:29:23 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.37      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.3       nicm       21: #include <stdlib.h>
1.1       nicm       22: #include <string.h>
1.16      nicm       23: #include <unistd.h>
1.1       nicm       24:
                     25: #include "tmux.h"
                     26:
1.41      nicm       27: static void    screen_resize_x(struct screen *, u_int);
                     28: static void    screen_resize_y(struct screen *, u_int);
1.1       nicm       29:
1.42    ! nicm       30: static void    screen_reflow(struct screen *, u_int);
        !            31:
1.1       nicm       32: /* Create a new screen. */
                     33: void
                     34: screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
                     35: {
                     36:        s->grid = grid_create(sx, sy, hlimit);
1.34      nicm       37:        s->title = xstrdup("");
1.1       nicm       38:
1.21      nicm       39:        s->cstyle = 0;
1.20      nicm       40:        s->ccolour = xstrdup("");
1.3       nicm       41:        s->tabs = NULL;
                     42:
1.39      nicm       43:        s->dirty = NULL;
                     44:        s->dirtysize = 0;
                     45:
1.1       nicm       46:        screen_reinit(s);
                     47: }
                     48:
                     49: /* Reinitialise screen. */
                     50: void
                     51: screen_reinit(struct screen *s)
                     52: {
                     53:        s->cx = 0;
                     54:        s->cy = 0;
                     55:
                     56:        s->rupper = 0;
                     57:        s->rlower = screen_size_y(s) - 1;
                     58:
1.16      nicm       59:        s->mode = MODE_CURSOR | MODE_WRAP;
1.12      nicm       60:
1.3       nicm       61:        screen_reset_tabs(s);
1.1       nicm       62:
1.6       nicm       63:        grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
1.1       nicm       64:
                     65:        screen_clear_selection(s);
                     66: }
                     67:
                     68: /* Destroy a screen. */
                     69: void
                     70: screen_free(struct screen *s)
                     71: {
1.39      nicm       72:        free(s->dirty);
1.23      nicm       73:        free(s->tabs);
                     74:        free(s->title);
                     75:        free(s->ccolour);
1.1       nicm       76:        grid_destroy(s->grid);
                     77: }
                     78:
1.3       nicm       79: /* Reset tabs to default, eight spaces apart. */
                     80: void
                     81: screen_reset_tabs(struct screen *s)
                     82: {
                     83:        u_int   i;
                     84:
1.23      nicm       85:        free(s->tabs);
1.3       nicm       86:
                     87:        if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
                     88:                fatal("bit_alloc failed");
                     89:        for (i = 8; i < screen_size_x(s); i += 8)
                     90:                bit_set(s->tabs, i);
1.21      nicm       91: }
                     92:
                     93: /* Set screen cursor style. */
                     94: void
                     95: screen_set_cursor_style(struct screen *s, u_int style)
                     96: {
1.22      nicm       97:        if (style <= 6)
1.21      nicm       98:                s->cstyle = style;
1.20      nicm       99: }
                    100:
                    101: /* Set screen cursor colour. */
                    102: void
1.36      nicm      103: screen_set_cursor_colour(struct screen *s, const char *colour)
1.20      nicm      104: {
1.23      nicm      105:        free(s->ccolour);
1.36      nicm      106:        s->ccolour = xstrdup(colour);
1.3       nicm      107: }
                    108:
1.1       nicm      109: /* Set screen title. */
                    110: void
                    111: screen_set_title(struct screen *s, const char *title)
                    112: {
1.23      nicm      113:        free(s->title);
1.27      nicm      114:        s->title = xstrdup(title);
1.1       nicm      115: }
                    116:
                    117: /* Resize screen. */
                    118: void
1.24      nicm      119: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
1.1       nicm      120: {
                    121:        if (sx < 1)
                    122:                sx = 1;
                    123:        if (sy < 1)
                    124:                sy = 1;
                    125:
1.3       nicm      126:        if (sx != screen_size_x(s)) {
1.1       nicm      127:                screen_resize_x(s, sx);
1.3       nicm      128:
                    129:                /*
                    130:                 * It is unclear what should happen to tabs on resize. xterm
                    131:                 * seems to try and maintain them, rxvt resets them. Resetting
                    132:                 * is simpler and more reliable so let's do that.
                    133:                 */
                    134:                screen_reset_tabs(s);
                    135:        }
                    136:
1.1       nicm      137:        if (sy != screen_size_y(s))
                    138:                screen_resize_y(s, sy);
1.24      nicm      139:
                    140:        if (reflow)
                    141:                screen_reflow(s, sx);
1.1       nicm      142: }
                    143:
1.41      nicm      144: static void
1.1       nicm      145: screen_resize_x(struct screen *s, u_int sx)
                    146: {
                    147:        struct grid             *gd = s->grid;
                    148:
                    149:        if (sx == 0)
                    150:                fatalx("zero size");
                    151:
1.7       nicm      152:        /*
                    153:         * Treat resizing horizontally simply: just ensure the cursor is
                    154:         * on-screen and change the size. Don't bother to truncate any lines -
1.38      nicm      155:         * then the data should be accessible if the size is then increased.
1.7       nicm      156:         *
                    157:         * The only potential wrinkle is if UTF-8 double-width characters are
                    158:         * left in the last column, but UTF-8 terminals should deal with this
                    159:         * sanely.
                    160:         */
1.1       nicm      161:        if (s->cx >= sx)
                    162:                s->cx = sx - 1;
                    163:        gd->sx = sx;
                    164: }
                    165:
1.41      nicm      166: static void
1.1       nicm      167: screen_resize_y(struct screen *s, u_int sy)
                    168: {
                    169:        struct grid     *gd = s->grid;
1.4       nicm      170:        u_int            needed, available, oldy, i;
1.1       nicm      171:
                    172:        if (sy == 0)
                    173:                fatalx("zero size");
1.4       nicm      174:        oldy = screen_size_y(s);
                    175:
1.12      nicm      176:        /*
1.4       nicm      177:         * When resizing:
                    178:         *
                    179:         * If the height is decreasing, delete lines from the bottom until
                    180:         * hitting the cursor, then push lines from the top into the history.
1.12      nicm      181:         *
1.40      nicm      182:         * When increasing, pull as many lines as possible from scrolled
                    183:         * history (not explicitly cleared from view) to the top, then fill the
                    184:         * remaining with blanks at the bottom.
1.4       nicm      185:         */
1.1       nicm      186:
                    187:        /* Size decreasing. */
1.4       nicm      188:        if (sy < oldy) {
                    189:                needed = oldy - sy;
1.1       nicm      190:
1.4       nicm      191:                /* Delete as many lines as possible from the bottom. */
                    192:                available = oldy - 1 - s->cy;
                    193:                if (available > 0) {
                    194:                        if (available > needed)
                    195:                                available = needed;
                    196:                        grid_view_delete_lines(gd, oldy - available, available);
1.1       nicm      197:                }
1.4       nicm      198:                needed -= available;
1.1       nicm      199:
1.4       nicm      200:                /*
1.8       nicm      201:                 * Now just increase the history size, if possible, to take
                    202:                 * over the lines which are left. If history is off, delete
                    203:                 * lines from the top.
1.4       nicm      204:                 */
1.8       nicm      205:                available = s->cy;
1.40      nicm      206:                if (gd->flags & GRID_HISTORY) {
                    207:                        gd->hscrolled += needed;
1.8       nicm      208:                        gd->hsize += needed;
1.40      nicm      209:                } else if (needed > 0 && available > 0) {
1.8       nicm      210:                        if (available > needed)
                    211:                                available = needed;
                    212:                        grid_view_delete_lines(gd, 0, available);
                    213:                }
1.4       nicm      214:                s->cy -= needed;
1.12      nicm      215:        }
1.1       nicm      216:
                    217:        /* Resize line arrays. */
1.30      nicm      218:        gd->linedata = xreallocarray(gd->linedata, gd->hsize + sy,
                    219:            sizeof *gd->linedata);
1.1       nicm      220:
                    221:        /* Size increasing. */
1.4       nicm      222:        if (sy > oldy) {
                    223:                needed = sy - oldy;
                    224:
1.8       nicm      225:                /*
1.41      nicm      226:                 * Try to pull as much as possible out of scrolled history, if
                    227:                 * is is enabled.
1.8       nicm      228:                 */
1.40      nicm      229:                available = gd->hscrolled;
1.8       nicm      230:                if (gd->flags & GRID_HISTORY && available > 0) {
1.4       nicm      231:                        if (available > needed)
                    232:                                available = needed;
1.40      nicm      233:                        gd->hscrolled -= available;
1.4       nicm      234:                        gd->hsize -= available;
                    235:                        s->cy += available;
1.8       nicm      236:                } else
                    237:                        available = 0;
1.4       nicm      238:                needed -= available;
                    239:
                    240:                /* Then fill the rest in with blanks. */
1.11      nicm      241:                for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
                    242:                        memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
1.1       nicm      243:        }
                    244:
1.4       nicm      245:        /* Set the new size, and reset the scroll region. */
1.1       nicm      246:        gd->sy = sy;
                    247:        s->rupper = 0;
                    248:        s->rlower = screen_size_y(s) - 1;
                    249: }
                    250:
                    251: /* Set selection. */
                    252: void
1.14      nicm      253: screen_set_selection(struct screen *s, u_int sx, u_int sy,
                    254:     u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
1.1       nicm      255: {
                    256:        struct screen_sel       *sel = &s->sel;
                    257:
                    258:        memcpy(&sel->cell, gc, sizeof sel->cell);
1.13      nicm      259:        sel->flag = 1;
1.14      nicm      260:        sel->rectflag = rectflag;
1.13      nicm      261:
1.14      nicm      262:        sel->sx = sx; sel->sy = sy;
                    263:        sel->ex = ex; sel->ey = ey;
1.1       nicm      264: }
                    265:
                    266: /* Clear selection. */
                    267: void
                    268: screen_clear_selection(struct screen *s)
                    269: {
                    270:        struct screen_sel       *sel = &s->sel;
                    271:
                    272:        sel->flag = 0;
1.32      nicm      273:        sel->lineflag = LINE_SEL_NONE;
1.1       nicm      274: }
                    275:
                    276: /* Check if cell in selection. */
                    277: int
                    278: screen_check_selection(struct screen *s, u_int px, u_int py)
                    279: {
                    280:        struct screen_sel       *sel = &s->sel;
1.28      nicm      281:        u_int                    xx;
1.1       nicm      282:
1.14      nicm      283:        if (!sel->flag)
1.1       nicm      284:                return (0);
                    285:
1.14      nicm      286:        if (sel->rectflag) {
                    287:                if (sel->sy < sel->ey) {
                    288:                        /* start line < end line -- downward selection. */
                    289:                        if (py < sel->sy || py > sel->ey)
                    290:                                return (0);
                    291:                } else if (sel->sy > sel->ey) {
                    292:                        /* start line > end line -- upward selection. */
                    293:                        if (py > sel->sy || py < sel->ey)
                    294:                                return (0);
                    295:                } else {
                    296:                        /* starting line == ending line. */
                    297:                        if (py != sel->sy)
                    298:                                return (0);
                    299:                }
                    300:
                    301:                /*
                    302:                 * Need to include the selection start row, but not the cursor
                    303:                 * row, which means the selection changes depending on which
                    304:                 * one is on the left.
                    305:                 */
                    306:                if (sel->ex < sel->sx) {
                    307:                        /* Cursor (ex) is on the left. */
1.17      nicm      308:                        if (px < sel->ex)
1.14      nicm      309:                                return (0);
                    310:
                    311:                        if (px > sel->sx)
                    312:                                return (0);
                    313:                } else {
                    314:                        /* Selection start (sx) is on the left. */
                    315:                        if (px < sel->sx)
                    316:                                return (0);
                    317:
1.17      nicm      318:                        if (px > sel->ex)
1.14      nicm      319:                                return (0);
                    320:                }
                    321:        } else {
                    322:                /*
                    323:                 * Like emacs, keep the top-left-most character, and drop the
                    324:                 * bottom-right-most, regardless of copy direction.
                    325:                 */
                    326:                if (sel->sy < sel->ey) {
                    327:                        /* starting line < ending line -- downward selection. */
                    328:                        if (py < sel->sy || py > sel->ey)
                    329:                                return (0);
                    330:
1.28      nicm      331:                        if (py == sel->sy && px < sel->sx)
1.29      nicm      332:                                return (0);
1.28      nicm      333:
                    334:                        if (py == sel->ey && px > sel->ex)
1.14      nicm      335:                                return (0);
                    336:                } else if (sel->sy > sel->ey) {
                    337:                        /* starting line > ending line -- upward selection. */
                    338:                        if (py > sel->sy || py < sel->ey)
                    339:                                return (0);
                    340:
1.28      nicm      341:                        if (py == sel->ey && px < sel->ex)
                    342:                                return (0);
                    343:
                    344:                        if (sel->modekeys == MODEKEY_EMACS)
                    345:                                xx = sel->sx - 1;
                    346:                        else
                    347:                                xx = sel->sx;
                    348:                        if (py == sel->sy && px > xx)
1.14      nicm      349:                                return (0);
                    350:                } else {
                    351:                        /* starting line == ending line. */
                    352:                        if (py != sel->sy)
                    353:                                return (0);
                    354:
                    355:                        if (sel->ex < sel->sx) {
                    356:                                /* cursor (ex) is on the left */
1.28      nicm      357:                                if (sel->modekeys == MODEKEY_EMACS)
                    358:                                        xx = sel->sx - 1;
                    359:                                else
                    360:                                        xx = sel->sx;
                    361:                                if (px > xx || px < sel->ex)
1.14      nicm      362:                                        return (0);
                    363:                        } else {
                    364:                                /* selection start (sx) is on the left */
                    365:                                if (px < sel->sx || px > sel->ex)
                    366:                                        return (0);
                    367:                        }
                    368:                }
1.1       nicm      369:        }
                    370:
                    371:        return (1);
1.24      nicm      372: }
                    373:
                    374: /* Reflow wrapped lines. */
1.42    ! nicm      375: static void
1.25      nicm      376: screen_reflow(struct screen *s, u_int new_x)
1.24      nicm      377: {
1.25      nicm      378:        struct grid     *old = s->grid;
1.26      nicm      379:        u_int            change;
1.24      nicm      380:
1.25      nicm      381:        s->grid = grid_create(old->sx, old->sy, old->hlimit);
1.26      nicm      382:
                    383:        change = grid_reflow(s->grid, old, new_x);
                    384:        if (change < s->cy)
                    385:                s->cy -= change;
                    386:        else
                    387:                s->cy = 0;
1.1       nicm      388: }