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

1.19    ! nicm        1: /* $OpenBSD: screen.c,v 1.18 2011/05/04 17:43:11 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.3       nicm       43:        s->tabs = NULL;
                     44:
1.1       nicm       45:        screen_reinit(s);
                     46: }
                     47:
                     48: /* Reinitialise screen. */
                     49: void
                     50: screen_reinit(struct screen *s)
                     51: {
                     52:        s->cx = 0;
                     53:        s->cy = 0;
                     54:
                     55:        s->rupper = 0;
                     56:        s->rlower = screen_size_y(s) - 1;
                     57:
1.16      nicm       58:        s->mode = MODE_CURSOR | MODE_WRAP;
1.12      nicm       59:
1.3       nicm       60:        screen_reset_tabs(s);
1.1       nicm       61:
1.6       nicm       62:        grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy);
1.1       nicm       63:
                     64:        screen_clear_selection(s);
                     65: }
                     66:
                     67: /* Destroy a screen. */
                     68: void
                     69: screen_free(struct screen *s)
                     70: {
1.10      nicm       71:        if (s->tabs != NULL)
                     72:                xfree(s->tabs);
1.1       nicm       73:        xfree(s->title);
                     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:
                     83:        if (s->tabs != NULL)
                     84:                xfree(s->tabs);
                     85:
                     86:        if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
                     87:                fatal("bit_alloc failed");
                     88:        for (i = 8; i < screen_size_x(s); i += 8)
                     89:                bit_set(s->tabs, i);
                     90: }
                     91:
1.1       nicm       92: /* Set screen title. */
                     93: void
                     94: screen_set_title(struct screen *s, const char *title)
                     95: {
1.2       nicm       96:        char    tmp[BUFSIZ];
                     97:
1.18      nicm       98:        strlcpy(tmp, title, sizeof tmp);
1.2       nicm       99:
1.1       nicm      100:        xfree(s->title);
1.2       nicm      101:        s->title = xstrdup(tmp);
1.1       nicm      102: }
                    103:
                    104: /* Resize screen. */
                    105: void
                    106: screen_resize(struct screen *s, u_int sx, u_int sy)
                    107: {
                    108:        if (sx < 1)
                    109:                sx = 1;
                    110:        if (sy < 1)
                    111:                sy = 1;
                    112:
1.3       nicm      113:        if (sx != screen_size_x(s)) {
1.1       nicm      114:                screen_resize_x(s, sx);
1.3       nicm      115:
                    116:                /*
                    117:                 * It is unclear what should happen to tabs on resize. xterm
                    118:                 * seems to try and maintain them, rxvt resets them. Resetting
                    119:                 * is simpler and more reliable so let's do that.
                    120:                 */
                    121:                screen_reset_tabs(s);
                    122:        }
                    123:
1.1       nicm      124:        if (sy != screen_size_y(s))
                    125:                screen_resize_y(s, sy);
                    126: }
                    127:
                    128: void
                    129: screen_resize_x(struct screen *s, u_int sx)
                    130: {
                    131:        struct grid             *gd = s->grid;
                    132:
                    133:        if (sx == 0)
                    134:                fatalx("zero size");
                    135:
1.7       nicm      136:        /*
                    137:         * Treat resizing horizontally simply: just ensure the cursor is
                    138:         * on-screen and change the size. Don't bother to truncate any lines -
                    139:         * then the data should be accessible if the size is then incrased.
                    140:         *
                    141:         * The only potential wrinkle is if UTF-8 double-width characters are
                    142:         * left in the last column, but UTF-8 terminals should deal with this
                    143:         * sanely.
                    144:         */
1.1       nicm      145:        if (s->cx >= sx)
                    146:                s->cx = sx - 1;
                    147:        gd->sx = sx;
                    148: }
                    149:
                    150: void
                    151: screen_resize_y(struct screen *s, u_int sy)
                    152: {
                    153:        struct grid     *gd = s->grid;
1.4       nicm      154:        u_int            needed, available, oldy, i;
1.1       nicm      155:
                    156:        if (sy == 0)
                    157:                fatalx("zero size");
1.4       nicm      158:        oldy = screen_size_y(s);
                    159:
1.12      nicm      160:        /*
1.4       nicm      161:         * When resizing:
                    162:         *
                    163:         * If the height is decreasing, delete lines from the bottom until
                    164:         * hitting the cursor, then push lines from the top into the history.
1.12      nicm      165:         *
1.4       nicm      166:         * When increasing, pull as many lines as possible from the history to
                    167:         * the top, then fill the remaining with blanks at the bottom.
                    168:         */
1.1       nicm      169:
                    170:        /* Size decreasing. */
1.4       nicm      171:        if (sy < oldy) {
                    172:                needed = oldy - sy;
1.1       nicm      173:
1.4       nicm      174:                /* Delete as many lines as possible from the bottom. */
                    175:                available = oldy - 1 - s->cy;
                    176:                if (available > 0) {
                    177:                        if (available > needed)
                    178:                                available = needed;
                    179:                        grid_view_delete_lines(gd, oldy - available, available);
1.1       nicm      180:                }
1.4       nicm      181:                needed -= available;
1.1       nicm      182:
1.4       nicm      183:                /*
1.8       nicm      184:                 * Now just increase the history size, if possible, to take
                    185:                 * over the lines which are left. If history is off, delete
                    186:                 * lines from the top.
                    187:                 *
                    188:                 * XXX Should apply history limit?
1.4       nicm      189:                 */
1.8       nicm      190:                available = s->cy;
                    191:                if (gd->flags & GRID_HISTORY)
                    192:                        gd->hsize += needed;
1.9       nicm      193:                else if (needed > 0 && available > 0) {
1.8       nicm      194:                        if (available > needed)
                    195:                                available = needed;
                    196:                        grid_view_delete_lines(gd, 0, available);
                    197:                }
1.4       nicm      198:                s->cy -= needed;
1.12      nicm      199:        }
1.1       nicm      200:
                    201:        /* Resize line arrays. */
1.11      nicm      202:        gd->linedata = xrealloc(
                    203:            gd->linedata, gd->hsize + sy, sizeof *gd->linedata);
1.1       nicm      204:
                    205:        /* Size increasing. */
1.4       nicm      206:        if (sy > oldy) {
                    207:                needed = sy - oldy;
                    208:
1.8       nicm      209:                /*
                    210:                 * Try to pull as much as possible out of the history, if is
                    211:                 * is enabled.
                    212:                 */
1.4       nicm      213:                available = gd->hsize;
1.8       nicm      214:                if (gd->flags & GRID_HISTORY && available > 0) {
1.4       nicm      215:                        if (available > needed)
                    216:                                available = needed;
                    217:                        gd->hsize -= available;
                    218:                        s->cy += available;
1.8       nicm      219:                } else
                    220:                        available = 0;
1.4       nicm      221:                needed -= available;
                    222:
                    223:                /* Then fill the rest in with blanks. */
1.11      nicm      224:                for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
                    225:                        memset(&gd->linedata[i], 0, sizeof gd->linedata[i]);
1.1       nicm      226:        }
                    227:
1.4       nicm      228:        /* Set the new size, and reset the scroll region. */
1.1       nicm      229:        gd->sy = sy;
                    230:        s->rupper = 0;
                    231:        s->rlower = screen_size_y(s) - 1;
                    232: }
                    233:
                    234: /* Set selection. */
                    235: void
1.14      nicm      236: screen_set_selection(struct screen *s, u_int sx, u_int sy,
                    237:     u_int ex, u_int ey, u_int rectflag, struct grid_cell *gc)
1.1       nicm      238: {
                    239:        struct screen_sel       *sel = &s->sel;
                    240:
                    241:        memcpy(&sel->cell, gc, sizeof sel->cell);
1.13      nicm      242:        sel->flag = 1;
1.14      nicm      243:        sel->rectflag = rectflag;
1.13      nicm      244:
1.14      nicm      245:        sel->sx = sx; sel->sy = sy;
                    246:        sel->ex = ex; sel->ey = ey;
1.1       nicm      247: }
                    248:
                    249: /* Clear selection. */
                    250: void
                    251: screen_clear_selection(struct screen *s)
                    252: {
                    253:        struct screen_sel       *sel = &s->sel;
                    254:
                    255:        sel->flag = 0;
                    256: }
                    257:
                    258: /* Check if cell in selection. */
                    259: int
                    260: screen_check_selection(struct screen *s, u_int px, u_int py)
                    261: {
                    262:        struct screen_sel       *sel = &s->sel;
                    263:
1.14      nicm      264:        if (!sel->flag)
1.1       nicm      265:                return (0);
                    266:
1.14      nicm      267:        if (sel->rectflag) {
                    268:                if (sel->sy < sel->ey) {
                    269:                        /* start line < end line -- downward selection. */
                    270:                        if (py < sel->sy || py > sel->ey)
                    271:                                return (0);
                    272:                } else if (sel->sy > sel->ey) {
                    273:                        /* start line > end line -- upward selection. */
                    274:                        if (py > sel->sy || py < sel->ey)
                    275:                                return (0);
                    276:                } else {
                    277:                        /* starting line == ending line. */
                    278:                        if (py != sel->sy)
                    279:                                return (0);
                    280:                }
                    281:
                    282:                /*
                    283:                 * Need to include the selection start row, but not the cursor
                    284:                 * row, which means the selection changes depending on which
                    285:                 * one is on the left.
                    286:                 */
                    287:                if (sel->ex < sel->sx) {
                    288:                        /* Cursor (ex) is on the left. */
1.17      nicm      289:                        if (px < sel->ex)
1.14      nicm      290:                                return (0);
                    291:
                    292:                        if (px > sel->sx)
                    293:                                return (0);
                    294:                } else {
                    295:                        /* Selection start (sx) is on the left. */
                    296:                        if (px < sel->sx)
                    297:                                return (0);
                    298:
1.17      nicm      299:                        if (px > sel->ex)
1.14      nicm      300:                                return (0);
                    301:                }
                    302:        } else {
                    303:                /*
                    304:                 * Like emacs, keep the top-left-most character, and drop the
                    305:                 * bottom-right-most, regardless of copy direction.
                    306:                 */
                    307:                if (sel->sy < sel->ey) {
                    308:                        /* starting line < ending line -- downward selection. */
                    309:                        if (py < sel->sy || py > sel->ey)
                    310:                                return (0);
                    311:
                    312:                        if ((py == sel->sy && px < sel->sx)
                    313:                            || (py == sel->ey && px > sel->ex))
                    314:                                return (0);
                    315:                } else if (sel->sy > sel->ey) {
                    316:                        /* starting line > ending line -- upward selection. */
                    317:                        if (py > sel->sy || py < sel->ey)
                    318:                                return (0);
                    319:
                    320:                        if ((py == sel->sy && px >= sel->sx)
                    321:                            || (py == sel->ey && px < sel->ex))
                    322:                                return (0);
                    323:                } else {
                    324:                        /* starting line == ending line. */
                    325:                        if (py != sel->sy)
                    326:                                return (0);
                    327:
                    328:                        if (sel->ex < sel->sx) {
                    329:                                /* cursor (ex) is on the left */
                    330:                                if (px > sel->sx || px < sel->ex)
                    331:                                        return (0);
                    332:                        } else {
                    333:                                /* selection start (sx) is on the left */
                    334:                                if (px < sel->sx || px > sel->ex)
                    335:                                        return (0);
                    336:                        }
                    337:                }
1.1       nicm      338:        }
                    339:
                    340:        return (1);
                    341: }