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

1.22    ! nicm        1: /* $OpenBSD: screen.c,v 1.21 2011/05/20 19:17:39 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:
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: {
1.15      beck       34:        char hn[MAXHOSTNAMELEN];
                     35:
1.1       nicm       36:        s->grid = grid_create(sx, sy, hlimit);
                     37:
1.15      beck       38:        if (gethostname(hn, MAXHOSTNAMELEN) == 0)
                     39:                s->title = xstrdup(hn);
                     40:        else
                     41:                s->title = xstrdup("");
1.1       nicm       42:
1.21      nicm       43:        s->cstyle = 0;
1.20      nicm       44:        s->ccolour = xstrdup("");
1.3       nicm       45:        s->tabs = NULL;
                     46:
1.1       nicm       47:        screen_reinit(s);
                     48: }
                     49:
                     50: /* Reinitialise screen. */
                     51: void
                     52: screen_reinit(struct screen *s)
                     53: {
                     54:        s->cx = 0;
                     55:        s->cy = 0;
                     56:
                     57:        s->rupper = 0;
                     58:        s->rlower = screen_size_y(s) - 1;
                     59:
1.16      nicm       60:        s->mode = MODE_CURSOR | MODE_WRAP;
1.12      nicm       61:
1.3       nicm       62:        screen_reset_tabs(s);
1.1       nicm       63:
1.6       nicm       64:        grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
1.1       nicm       65:
                     66:        screen_clear_selection(s);
                     67: }
                     68:
                     69: /* Destroy a screen. */
                     70: void
                     71: screen_free(struct screen *s)
                     72: {
1.10      nicm       73:        if (s->tabs != NULL)
                     74:                xfree(s->tabs);
1.1       nicm       75:        xfree(s->title);
1.20      nicm       76:        xfree(s->ccolour);
1.1       nicm       77:        grid_destroy(s->grid);
                     78: }
                     79:
1.3       nicm       80: /* Reset tabs to default, eight spaces apart. */
                     81: void
                     82: screen_reset_tabs(struct screen *s)
                     83: {
                     84:        u_int   i;
                     85:
                     86:        if (s->tabs != NULL)
                     87:                xfree(s->tabs);
                     88:
                     89:        if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
                     90:                fatal("bit_alloc failed");
                     91:        for (i = 8; i < screen_size_x(s); i += 8)
                     92:                bit_set(s->tabs, i);
1.21      nicm       93: }
                     94:
                     95: /* Set screen cursor style. */
                     96: void
                     97: screen_set_cursor_style(struct screen *s, u_int style)
                     98: {
1.22    ! nicm       99:        if (style <= 6)
1.21      nicm      100:                s->cstyle = style;
1.20      nicm      101: }
                    102:
                    103: /* Set screen cursor colour. */
                    104: void
                    105: screen_set_cursor_colour(struct screen *s, const char *colour_string)
                    106: {
                    107:        xfree(s->ccolour);
                    108:        s->ccolour = xstrdup(colour_string);
1.3       nicm      109: }
                    110:
1.1       nicm      111: /* Set screen title. */
                    112: void
                    113: screen_set_title(struct screen *s, const char *title)
                    114: {
1.2       nicm      115:        char    tmp[BUFSIZ];
                    116:
1.18      nicm      117:        strlcpy(tmp, title, sizeof tmp);
1.2       nicm      118:
1.1       nicm      119:        xfree(s->title);
1.2       nicm      120:        s->title = xstrdup(tmp);
1.1       nicm      121: }
                    122:
                    123: /* Resize screen. */
                    124: void
                    125: screen_resize(struct screen *s, u_int sx, u_int sy)
                    126: {
                    127:        if (sx < 1)
                    128:                sx = 1;
                    129:        if (sy < 1)
                    130:                sy = 1;
                    131:
1.3       nicm      132:        if (sx != screen_size_x(s)) {
1.1       nicm      133:                screen_resize_x(s, sx);
1.3       nicm      134:
                    135:                /*
                    136:                 * It is unclear what should happen to tabs on resize. xterm
                    137:                 * seems to try and maintain them, rxvt resets them. Resetting
                    138:                 * is simpler and more reliable so let's do that.
                    139:                 */
                    140:                screen_reset_tabs(s);
                    141:        }
                    142:
1.1       nicm      143:        if (sy != screen_size_y(s))
                    144:                screen_resize_y(s, sy);
                    145: }
                    146:
                    147: void
                    148: screen_resize_x(struct screen *s, u_int sx)
                    149: {
                    150:        struct grid             *gd = s->grid;
                    151:
                    152:        if (sx == 0)
                    153:                fatalx("zero size");
                    154:
1.7       nicm      155:        /*
                    156:         * Treat resizing horizontally simply: just ensure the cursor is
                    157:         * on-screen and change the size. Don't bother to truncate any lines -
                    158:         * then the data should be accessible if the size is then incrased.
                    159:         *
                    160:         * The only potential wrinkle is if UTF-8 double-width characters are
                    161:         * left in the last column, but UTF-8 terminals should deal with this
                    162:         * sanely.
                    163:         */
1.1       nicm      164:        if (s->cx >= sx)
                    165:                s->cx = sx - 1;
                    166:        gd->sx = sx;
                    167: }
                    168:
                    169: void
                    170: screen_resize_y(struct screen *s, u_int sy)
                    171: {
                    172:        struct grid     *gd = s->grid;
1.4       nicm      173:        u_int            needed, available, oldy, i;
1.1       nicm      174:
                    175:        if (sy == 0)
                    176:                fatalx("zero size");
1.4       nicm      177:        oldy = screen_size_y(s);
                    178:
1.12      nicm      179:        /*
1.4       nicm      180:         * When resizing:
                    181:         *
                    182:         * If the height is decreasing, delete lines from the bottom until
                    183:         * hitting the cursor, then push lines from the top into the history.
1.12      nicm      184:         *
1.4       nicm      185:         * When increasing, pull as many lines as possible from the history to
                    186:         * the top, then fill the remaining with blanks at the bottom.
                    187:         */
1.1       nicm      188:
                    189:        /* Size decreasing. */
1.4       nicm      190:        if (sy < oldy) {
                    191:                needed = oldy - sy;
1.1       nicm      192:
1.4       nicm      193:                /* Delete as many lines as possible from the bottom. */
                    194:                available = oldy - 1 - s->cy;
                    195:                if (available > 0) {
                    196:                        if (available > needed)
                    197:                                available = needed;
                    198:                        grid_view_delete_lines(gd, oldy - available, available);
1.1       nicm      199:                }
1.4       nicm      200:                needed -= available;
1.1       nicm      201:
1.4       nicm      202:                /*
1.8       nicm      203:                 * Now just increase the history size, if possible, to take
                    204:                 * over the lines which are left. If history is off, delete
                    205:                 * lines from the top.
                    206:                 *
                    207:                 * XXX Should apply history limit?
1.4       nicm      208:                 */
1.8       nicm      209:                available = s->cy;
                    210:                if (gd->flags & GRID_HISTORY)
                    211:                        gd->hsize += needed;
1.9       nicm      212:                else if (needed > 0 && available > 0) {
1.8       nicm      213:                        if (available > needed)
                    214:                                available = needed;
                    215:                        grid_view_delete_lines(gd, 0, available);
                    216:                }
1.4       nicm      217:                s->cy -= needed;
1.12      nicm      218:        }
1.1       nicm      219:
                    220:        /* Resize line arrays. */
1.11      nicm      221:        gd->linedata = xrealloc(
                    222:            gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
1.1       nicm      223:
                    224:        /* Size increasing. */
1.4       nicm      225:        if (sy > oldy) {
                    226:                needed = sy - oldy;
                    227:
1.8       nicm      228:                /*
                    229:                 * Try to pull as much as possible out of the history, if is
                    230:                 * is enabled.
                    231:                 */
1.4       nicm      232:                available = gd->hsize;
1.8       nicm      233:                if (gd->flags & GRID_HISTORY && available > 0) {
1.4       nicm      234:                        if (available > needed)
                    235:                                available = needed;
                    236:                        gd->hsize -= available;
                    237:                        s->cy += available;
1.8       nicm      238:                } else
                    239:                        available = 0;
1.4       nicm      240:                needed -= available;
                    241:
                    242:                /* Then fill the rest in with blanks. */
1.11      nicm      243:                for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
                    244:                        memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
1.1       nicm      245:        }
                    246:
1.4       nicm      247:        /* Set the new size, and reset the scroll region. */
1.1       nicm      248:        gd->sy = sy;
                    249:        s->rupper = 0;
                    250:        s->rlower = screen_size_y(s) - 1;
                    251: }
                    252:
                    253: /* Set selection. */
                    254: void
1.14      nicm      255: screen_set_selection(struct screen *s, u_int sx, u_int sy,
                    256:     u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
1.1       nicm      257: {
                    258:        struct screen_sel       *sel = &s->sel;
                    259:
                    260:        memcpy(&sel->cell, gc, sizeof sel->cell);
1.13      nicm      261:        sel->flag = 1;
1.14      nicm      262:        sel->rectflag = rectflag;
1.13      nicm      263:
1.14      nicm      264:        sel->sx = sx; sel->sy = sy;
                    265:        sel->ex = ex; sel->ey = ey;
1.1       nicm      266: }
                    267:
                    268: /* Clear selection. */
                    269: void
                    270: screen_clear_selection(struct screen *s)
                    271: {
                    272:        struct screen_sel       *sel = &s->sel;
                    273:
                    274:        sel->flag = 0;
                    275: }
                    276:
                    277: /* Check if cell in selection. */
                    278: int
                    279: screen_check_selection(struct screen *s, u_int px, u_int py)
                    280: {
                    281:        struct screen_sel       *sel = &s->sel;
                    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:
                    331:                        if ((py == sel->sy && px < sel->sx)
                    332:                            || (py == sel->ey && px > sel->ex))
                    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:
                    339:                        if ((py == sel->sy && px >= sel->sx)
                    340:                            || (py == sel->ey && px < sel->ex))
                    341:                                return (0);
                    342:                } else {
                    343:                        /* starting line == ending line. */
                    344:                        if (py != sel->sy)
                    345:                                return (0);
                    346:
                    347:                        if (sel->ex < sel->sx) {
                    348:                                /* cursor (ex) is on the left */
                    349:                                if (px > sel->sx || px < sel->ex)
                    350:                                        return (0);
                    351:                        } else {
                    352:                                /* selection start (sx) is on the left */
                    353:                                if (px < sel->sx || px > sel->ex)
                    354:                                        return (0);
                    355:                        }
                    356:                }
1.1       nicm      357:        }
                    358:
                    359:        return (1);
                    360: }