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

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