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

1.77    ! nicm        1: /* $OpenBSD: screen.c,v 1.76 2021/10/05 12:46:02 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.47      nicm       24: #include <vis.h>
1.1       nicm       25:
                     26: #include "tmux.h"
                     27:
1.52      nicm       28: /* Selected area in screen. */
                     29: struct screen_sel {
                     30:        int              hidden;
                     31:        int              rectangle;
                     32:        int              modekeys;
                     33:
                     34:        u_int            sx;
                     35:        u_int            sy;
                     36:
                     37:        u_int            ex;
                     38:        u_int            ey;
                     39:
                     40:        struct grid_cell cell;
                     41: };
                     42:
                     43: /* Entry on title stack. */
1.48      nicm       44: struct screen_title_entry {
                     45:        char                            *text;
                     46:
                     47:        TAILQ_ENTRY(screen_title_entry)  entry;
                     48: };
                     49: TAILQ_HEAD(screen_titles, screen_title_entry);
                     50:
1.66      nicm       51: static void    screen_resize_y(struct screen *, u_int, int, u_int *);
1.67      nicm       52: static void    screen_reflow(struct screen *, u_int, u_int *, u_int *, int);
1.42      nicm       53:
1.48      nicm       54: /* Free titles stack. */
                     55: static void
                     56: screen_free_titles(struct screen *s)
                     57: {
                     58:        struct screen_title_entry       *title_entry;
                     59:
                     60:        if (s->titles == NULL)
                     61:                return;
                     62:
                     63:        while ((title_entry = TAILQ_FIRST(s->titles)) != NULL) {
                     64:                TAILQ_REMOVE(s->titles, title_entry, entry);
                     65:                free(title_entry->text);
                     66:                free(title_entry);
                     67:        }
                     68:
                     69:        free(s->titles);
                     70:        s->titles = NULL;
                     71: }
                     72:
1.1       nicm       73: /* Create a new screen. */
                     74: void
                     75: screen_init(struct screen *s, u_int sx, u_int sy, u_int hlimit)
                     76: {
                     77:        s->grid = grid_create(sx, sy, hlimit);
1.59      nicm       78:        s->saved_grid = NULL;
                     79:
1.34      nicm       80:        s->title = xstrdup("");
1.48      nicm       81:        s->titles = NULL;
1.68      nicm       82:        s->path = NULL;
1.1       nicm       83:
1.72      nicm       84:        s->cstyle = SCREEN_CURSOR_DEFAULT;
1.77    ! nicm       85:        s->ccolour = -1;
        !            86:        s->default_ccolour = -1;
1.3       nicm       87:        s->tabs = NULL;
1.52      nicm       88:        s->sel = NULL;
1.3       nicm       89:
1.64      nicm       90:        s->write_list = NULL;
                     91:
1.1       nicm       92:        screen_reinit(s);
                     93: }
                     94:
                     95: /* Reinitialise screen. */
                     96: void
                     97: screen_reinit(struct screen *s)
                     98: {
                     99:        s->cx = 0;
                    100:        s->cy = 0;
                    101:
                    102:        s->rupper = 0;
                    103:        s->rlower = screen_size_y(s) - 1;
                    104:
1.71      nicm      105:        s->mode = MODE_CURSOR|MODE_WRAP;
                    106:        if (options_get_number(global_options, "extended-keys") == 2)
                    107:                s->mode |= MODE_KEXTENDED;
1.12      nicm      108:
1.59      nicm      109:        if (s->saved_grid != NULL)
                    110:                screen_alternate_off(s, NULL, 0);
                    111:        s->saved_cx = UINT_MAX;
                    112:        s->saved_cy = UINT_MAX;
                    113:
1.3       nicm      114:        screen_reset_tabs(s);
1.1       nicm      115:
1.44      nicm      116:        grid_clear_lines(s->grid, s->grid->hsize, s->grid->sy, 8);
1.1       nicm      117:
                    118:        screen_clear_selection(s);
1.48      nicm      119:        screen_free_titles(s);
1.1       nicm      120: }
                    121:
                    122: /* Destroy a screen. */
                    123: void
                    124: screen_free(struct screen *s)
                    125: {
1.52      nicm      126:        free(s->sel);
1.23      nicm      127:        free(s->tabs);
1.68      nicm      128:        free(s->path);
1.23      nicm      129:        free(s->title);
1.48      nicm      130:
1.64      nicm      131:        if (s->write_list != NULL)
                    132:                screen_write_free_list(s);
                    133:
1.59      nicm      134:        if (s->saved_grid != NULL)
                    135:                grid_destroy(s->saved_grid);
1.1       nicm      136:        grid_destroy(s->grid);
1.48      nicm      137:
                    138:        screen_free_titles(s);
1.1       nicm      139: }
                    140:
1.3       nicm      141: /* Reset tabs to default, eight spaces apart. */
                    142: void
                    143: screen_reset_tabs(struct screen *s)
                    144: {
                    145:        u_int   i;
                    146:
1.23      nicm      147:        free(s->tabs);
1.3       nicm      148:
                    149:        if ((s->tabs = bit_alloc(screen_size_x(s))) == NULL)
                    150:                fatal("bit_alloc failed");
                    151:        for (i = 8; i < screen_size_x(s); i += 8)
                    152:                bit_set(s->tabs, i);
1.21      nicm      153: }
                    154:
                    155: /* Set screen cursor style. */
                    156: void
                    157: screen_set_cursor_style(struct screen *s, u_int style)
                    158: {
1.73      nicm      159:        log_debug("%s: new %u, was %u", __func__, style, s->cstyle);
                    160:        switch (style) {
1.72      nicm      161:        case 0:
                    162:                s->cstyle = SCREEN_CURSOR_DEFAULT;
                    163:                break;
                    164:        case 1:
                    165:                s->cstyle = SCREEN_CURSOR_BLOCK;
1.76      nicm      166:                s->mode |= MODE_CURSOR_BLINKING;
1.72      nicm      167:                break;
                    168:        case 2:
                    169:                s->cstyle = SCREEN_CURSOR_BLOCK;
1.76      nicm      170:                s->mode &= ~MODE_CURSOR_BLINKING;
1.72      nicm      171:                break;
                    172:        case 3:
                    173:                s->cstyle = SCREEN_CURSOR_UNDERLINE;
1.76      nicm      174:                s->mode |= MODE_CURSOR_BLINKING;
1.72      nicm      175:                break;
                    176:        case 4:
                    177:                s->cstyle = SCREEN_CURSOR_UNDERLINE;
1.76      nicm      178:                s->mode &= ~MODE_CURSOR_BLINKING;
1.72      nicm      179:                break;
                    180:        case 5:
                    181:                s->cstyle = SCREEN_CURSOR_BAR;
1.76      nicm      182:                s->mode |= MODE_CURSOR_BLINKING;
1.72      nicm      183:                break;
                    184:        case 6:
                    185:                s->cstyle = SCREEN_CURSOR_BAR;
1.76      nicm      186:                s->mode &= ~MODE_CURSOR_BLINKING;
1.72      nicm      187:                break;
1.70      nicm      188:        }
1.20      nicm      189: }
                    190:
                    191: /* Set screen cursor colour. */
                    192: void
1.77    ! nicm      193: screen_set_cursor_colour(struct screen *s, int colour)
1.20      nicm      194: {
1.77    ! nicm      195:        s->ccolour = colour;
1.3       nicm      196: }
                    197:
1.1       nicm      198: /* Set screen title. */
1.57      nicm      199: int
1.1       nicm      200: screen_set_title(struct screen *s, const char *title)
                    201: {
1.57      nicm      202:        if (!utf8_isvalid(title))
                    203:                return (0);
1.23      nicm      204:        free(s->title);
1.57      nicm      205:        s->title = xstrdup(title);
                    206:        return (1);
1.56      nicm      207: }
                    208:
                    209: /* Set screen path. */
                    210: void
                    211: screen_set_path(struct screen *s, const char *path)
                    212: {
                    213:        free(s->path);
                    214:        utf8_stravis(&s->path, path, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL);
1.48      nicm      215: }
                    216:
                    217: /* Push the current title onto the stack. */
                    218: void
                    219: screen_push_title(struct screen *s)
                    220: {
                    221:        struct screen_title_entry *title_entry;
                    222:
                    223:        if (s->titles == NULL) {
                    224:                s->titles = xmalloc(sizeof *s->titles);
                    225:                TAILQ_INIT(s->titles);
                    226:        }
                    227:        title_entry = xmalloc(sizeof *title_entry);
                    228:        title_entry->text = xstrdup(s->title);
                    229:        TAILQ_INSERT_HEAD(s->titles, title_entry, entry);
                    230: }
                    231:
                    232: /*
                    233:  * Pop a title from the stack and set it as the screen title. If the stack is
                    234:  * empty, do nothing.
                    235:  */
                    236: void
                    237: screen_pop_title(struct screen *s)
                    238: {
                    239:        struct screen_title_entry *title_entry;
                    240:
                    241:        if (s->titles == NULL)
                    242:                return;
                    243:
                    244:        title_entry = TAILQ_FIRST(s->titles);
                    245:        if (title_entry != NULL) {
                    246:                screen_set_title(s, title_entry->text);
                    247:
                    248:                TAILQ_REMOVE(s->titles, title_entry, entry);
                    249:                free(title_entry->text);
                    250:                free(title_entry);
                    251:        }
1.1       nicm      252: }
                    253:
1.67      nicm      254: /* Resize screen with options. */
1.1       nicm      255: void
1.62      nicm      256: screen_resize_cursor(struct screen *s, u_int sx, u_int sy, int reflow,
1.67      nicm      257:     int eat_empty, int cursor)
1.1       nicm      258: {
1.67      nicm      259:        u_int   cx = s->cx, cy = s->grid->hsize + s->cy;
1.64      nicm      260:
1.65      nicm      261:        if (s->write_list != NULL)
1.64      nicm      262:                screen_write_free_list(s);
1.62      nicm      263:
1.63      nicm      264:        log_debug("%s: new size %ux%u, now %ux%u (cursor %u,%u = %u,%u)",
                    265:            __func__, sx, sy, screen_size_x(s), screen_size_y(s), s->cx, s->cy,
1.67      nicm      266:            cx, cy);
1.62      nicm      267:
1.1       nicm      268:        if (sx < 1)
                    269:                sx = 1;
                    270:        if (sy < 1)
                    271:                sy = 1;
                    272:
1.3       nicm      273:        if (sx != screen_size_x(s)) {
1.54      nicm      274:                s->grid->sx = sx;
1.3       nicm      275:                screen_reset_tabs(s);
1.49      nicm      276:        } else
                    277:                reflow = 0;
1.3       nicm      278:
1.1       nicm      279:        if (sy != screen_size_y(s))
1.67      nicm      280:                screen_resize_y(s, sy, eat_empty, &cy);
1.24      nicm      281:
                    282:        if (reflow)
1.67      nicm      283:                screen_reflow(s, sx, &cx, &cy, cursor);
1.62      nicm      284:
1.67      nicm      285:        if (cy >= s->grid->hsize) {
                    286:                s->cx = cx;
                    287:                s->cy = cy - s->grid->hsize;
1.62      nicm      288:        } else {
                    289:                s->cx = 0;
                    290:                s->cy = 0;
                    291:        }
1.67      nicm      292:
1.63      nicm      293:        log_debug("%s: cursor finished at %u,%u = %u,%u", __func__, s->cx,
1.67      nicm      294:            s->cy, cx, cy);
1.65      nicm      295:
                    296:        if (s->write_list != NULL)
                    297:                screen_write_make_list(s);
1.62      nicm      298: }
                    299:
                    300: /* Resize screen. */
                    301: void
                    302: screen_resize(struct screen *s, u_int sx, u_int sy, int reflow)
                    303: {
1.67      nicm      304:        screen_resize_cursor(s, sx, sy, reflow, 1, 1);
1.1       nicm      305: }
                    306:
1.41      nicm      307: static void
1.66      nicm      308: screen_resize_y(struct screen *s, u_int sy, int eat_empty, u_int *cy)
1.1       nicm      309: {
                    310:        struct grid     *gd = s->grid;
1.4       nicm      311:        u_int            needed, available, oldy, i;
1.1       nicm      312:
                    313:        if (sy == 0)
                    314:                fatalx("zero size");
1.4       nicm      315:        oldy = screen_size_y(s);
                    316:
1.12      nicm      317:        /*
1.4       nicm      318:         * When resizing:
                    319:         *
                    320:         * If the height is decreasing, delete lines from the bottom until
                    321:         * hitting the cursor, then push lines from the top into the history.
1.12      nicm      322:         *
1.40      nicm      323:         * When increasing, pull as many lines as possible from scrolled
                    324:         * history (not explicitly cleared from view) to the top, then fill the
                    325:         * remaining with blanks at the bottom.
1.4       nicm      326:         */
1.1       nicm      327:
                    328:        /* Size decreasing. */
1.4       nicm      329:        if (sy < oldy) {
                    330:                needed = oldy - sy;
1.1       nicm      331:
1.4       nicm      332:                /* Delete as many lines as possible from the bottom. */
1.62      nicm      333:                if (eat_empty) {
                    334:                        available = oldy - 1 - s->cy;
                    335:                        if (available > 0) {
                    336:                                if (available > needed)
                    337:                                        available = needed;
                    338:                                grid_view_delete_lines(gd, oldy - available,
                    339:                                    available, 8);
                    340:                        }
                    341:                        needed -= available;
1.1       nicm      342:                }
                    343:
1.4       nicm      344:                /*
1.8       nicm      345:                 * Now just increase the history size, if possible, to take
                    346:                 * over the lines which are left. If history is off, delete
                    347:                 * lines from the top.
1.4       nicm      348:                 */
1.8       nicm      349:                available = s->cy;
1.40      nicm      350:                if (gd->flags & GRID_HISTORY) {
                    351:                        gd->hscrolled += needed;
1.8       nicm      352:                        gd->hsize += needed;
1.40      nicm      353:                } else if (needed > 0 && available > 0) {
1.8       nicm      354:                        if (available > needed)
                    355:                                available = needed;
1.44      nicm      356:                        grid_view_delete_lines(gd, 0, available, 8);
1.66      nicm      357:                        (*cy) -= available;
1.8       nicm      358:                }
1.12      nicm      359:        }
1.1       nicm      360:
1.51      nicm      361:        /* Resize line array. */
                    362:        grid_adjust_lines(gd, gd->hsize + sy);
1.1       nicm      363:
                    364:        /* Size increasing. */
1.4       nicm      365:        if (sy > oldy) {
                    366:                needed = sy - oldy;
                    367:
1.8       nicm      368:                /*
1.41      nicm      369:                 * Try to pull as much as possible out of scrolled history, if
                    370:                 * is is enabled.
1.8       nicm      371:                 */
1.40      nicm      372:                available = gd->hscrolled;
1.8       nicm      373:                if (gd->flags & GRID_HISTORY && available > 0) {
1.4       nicm      374:                        if (available > needed)
                    375:                                available = needed;
1.40      nicm      376:                        gd->hscrolled -= available;
1.4       nicm      377:                        gd->hsize -= available;
1.8       nicm      378:                } else
                    379:                        available = 0;
1.4       nicm      380:                needed -= available;
                    381:
                    382:                /* Then fill the rest in with blanks. */
1.11      nicm      383:                for (i = gd->hsize + sy - needed; i < gd->hsize + sy; i++)
1.61      nicm      384:                        grid_empty_line(gd, i, 8);
1.1       nicm      385:        }
                    386:
1.4       nicm      387:        /* Set the new size, and reset the scroll region. */
1.1       nicm      388:        gd->sy = sy;
                    389:        s->rupper = 0;
                    390:        s->rlower = screen_size_y(s) - 1;
                    391: }
                    392:
                    393: /* Set selection. */
                    394: void
1.14      nicm      395: screen_set_selection(struct screen *s, u_int sx, u_int sy,
1.52      nicm      396:     u_int ex, u_int ey, u_int rectangle, int modekeys, struct grid_cell *gc)
1.1       nicm      397: {
1.52      nicm      398:        if (s->sel == NULL)
                    399:                s->sel = xcalloc(1, sizeof *s->sel);
1.1       nicm      400:
1.52      nicm      401:        memcpy(&s->sel->cell, gc, sizeof s->sel->cell);
                    402:        s->sel->hidden = 0;
                    403:        s->sel->rectangle = rectangle;
                    404:        s->sel->modekeys = modekeys;
                    405:
                    406:        s->sel->sx = sx;
                    407:        s->sel->sy = sy;
                    408:        s->sel->ex = ex;
                    409:        s->sel->ey = ey;
1.1       nicm      410: }
                    411:
                    412: /* Clear selection. */
                    413: void
                    414: screen_clear_selection(struct screen *s)
                    415: {
1.52      nicm      416:        free(s->sel);
                    417:        s->sel = NULL;
1.1       nicm      418: }
                    419:
1.45      nicm      420: /* Hide selection. */
                    421: void
                    422: screen_hide_selection(struct screen *s)
                    423: {
1.52      nicm      424:        if (s->sel != NULL)
                    425:                s->sel->hidden = 1;
1.45      nicm      426: }
                    427:
1.1       nicm      428: /* Check if cell in selection. */
                    429: int
                    430: screen_check_selection(struct screen *s, u_int px, u_int py)
                    431: {
1.52      nicm      432:        struct screen_sel       *sel = s->sel;
1.28      nicm      433:        u_int                    xx;
1.1       nicm      434:
1.52      nicm      435:        if (sel == NULL || sel->hidden)
1.1       nicm      436:                return (0);
                    437:
1.52      nicm      438:        if (sel->rectangle) {
1.14      nicm      439:                if (sel->sy < sel->ey) {
                    440:                        /* start line < end line -- downward selection. */
                    441:                        if (py < sel->sy || py > sel->ey)
                    442:                                return (0);
                    443:                } else if (sel->sy > sel->ey) {
                    444:                        /* start line > end line -- upward selection. */
                    445:                        if (py > sel->sy || py < sel->ey)
                    446:                                return (0);
                    447:                } else {
                    448:                        /* starting line == ending line. */
                    449:                        if (py != sel->sy)
                    450:                                return (0);
                    451:                }
                    452:
                    453:                /*
                    454:                 * Need to include the selection start row, but not the cursor
                    455:                 * row, which means the selection changes depending on which
                    456:                 * one is on the left.
                    457:                 */
                    458:                if (sel->ex < sel->sx) {
                    459:                        /* Cursor (ex) is on the left. */
1.17      nicm      460:                        if (px < sel->ex)
1.14      nicm      461:                                return (0);
                    462:
                    463:                        if (px > sel->sx)
                    464:                                return (0);
                    465:                } else {
                    466:                        /* Selection start (sx) is on the left. */
                    467:                        if (px < sel->sx)
                    468:                                return (0);
                    469:
1.17      nicm      470:                        if (px > sel->ex)
1.14      nicm      471:                                return (0);
                    472:                }
                    473:        } else {
                    474:                /*
                    475:                 * Like emacs, keep the top-left-most character, and drop the
                    476:                 * bottom-right-most, regardless of copy direction.
                    477:                 */
                    478:                if (sel->sy < sel->ey) {
                    479:                        /* starting line < ending line -- downward selection. */
                    480:                        if (py < sel->sy || py > sel->ey)
                    481:                                return (0);
                    482:
1.28      nicm      483:                        if (py == sel->sy && px < sel->sx)
1.29      nicm      484:                                return (0);
1.28      nicm      485:
1.53      nicm      486:                        if (sel->modekeys == MODEKEY_EMACS)
                    487:                                xx = (sel->ex == 0 ? 0 : sel->ex - 1);
                    488:                        else
                    489:                                xx = sel->ex;
                    490:                        if (py == sel->ey && px > xx)
1.14      nicm      491:                                return (0);
                    492:                } else if (sel->sy > sel->ey) {
                    493:                        /* starting line > ending line -- upward selection. */
                    494:                        if (py > sel->sy || py < sel->ey)
                    495:                                return (0);
                    496:
1.28      nicm      497:                        if (py == sel->ey && px < sel->ex)
                    498:                                return (0);
                    499:
                    500:                        if (sel->modekeys == MODEKEY_EMACS)
                    501:                                xx = sel->sx - 1;
                    502:                        else
                    503:                                xx = sel->sx;
1.46      nicm      504:                        if (py == sel->sy && (sel->sx == 0 || px > xx))
1.14      nicm      505:                                return (0);
                    506:                } else {
                    507:                        /* starting line == ending line. */
                    508:                        if (py != sel->sy)
                    509:                                return (0);
                    510:
                    511:                        if (sel->ex < sel->sx) {
                    512:                                /* cursor (ex) is on the left */
1.28      nicm      513:                                if (sel->modekeys == MODEKEY_EMACS)
                    514:                                        xx = sel->sx - 1;
                    515:                                else
                    516:                                        xx = sel->sx;
                    517:                                if (px > xx || px < sel->ex)
1.14      nicm      518:                                        return (0);
                    519:                        } else {
                    520:                                /* selection start (sx) is on the left */
1.53      nicm      521:                                if (sel->modekeys == MODEKEY_EMACS)
                    522:                                        xx = (sel->ex == 0 ? 0 : sel->ex - 1);
                    523:                                else
                    524:                                        xx = sel->ex;
                    525:                                if (px < sel->sx || px > xx)
1.14      nicm      526:                                        return (0);
                    527:                        }
                    528:                }
1.1       nicm      529:        }
                    530:
                    531:        return (1);
1.43      nicm      532: }
                    533:
                    534: /* Get selected grid cell. */
                    535: void
                    536: screen_select_cell(struct screen *s, struct grid_cell *dst,
                    537:     const struct grid_cell *src)
                    538: {
1.52      nicm      539:        if (s->sel == NULL || s->sel->hidden)
1.43      nicm      540:                return;
                    541:
1.52      nicm      542:        memcpy(dst, &s->sel->cell, sizeof *dst);
1.43      nicm      543:
                    544:        utf8_copy(&dst->data, &src->data);
                    545:        dst->attr = dst->attr & ~GRID_ATTR_CHARSET;
                    546:        dst->attr |= src->attr & GRID_ATTR_CHARSET;
                    547:        dst->flags = src->flags;
1.24      nicm      548: }
                    549:
                    550: /* Reflow wrapped lines. */
1.42      nicm      551: static void
1.67      nicm      552: screen_reflow(struct screen *s, u_int new_x, u_int *cx, u_int *cy, int cursor)
1.24      nicm      553: {
1.62      nicm      554:        u_int   wx, wy;
1.54      nicm      555:
1.67      nicm      556:        if (cursor) {
                    557:                grid_wrap_position(s->grid, *cx, *cy, &wx, &wy);
                    558:                log_debug("%s: cursor %u,%u is %u,%u", __func__, *cx, *cy, wx,
                    559:                    wy);
                    560:        }
1.54      nicm      561:
                    562:        grid_reflow(s->grid, new_x);
                    563:
1.67      nicm      564:        if (cursor) {
                    565:                grid_unwrap_position(s->grid, cx, cy, wx, wy);
                    566:                log_debug("%s: new cursor is %u,%u", __func__, *cx, *cy);
                    567:        }
                    568:        else {
                    569:                *cx = 0;
                    570:                *cy = s->grid->hsize;
                    571:        }
1.59      nicm      572: }
                    573:
                    574: /*
                    575:  * Enter alternative screen mode. A copy of the visible screen is saved and the
                    576:  * history is not updated.
                    577:  */
                    578: void
                    579: screen_alternate_on(struct screen *s, struct grid_cell *gc, int cursor)
                    580: {
                    581:        u_int   sx, sy;
                    582:
                    583:        if (s->saved_grid != NULL)
                    584:                return;
                    585:        sx = screen_size_x(s);
                    586:        sy = screen_size_y(s);
                    587:
                    588:        s->saved_grid = grid_create(sx, sy, 0);
                    589:        grid_duplicate_lines(s->saved_grid, 0, s->grid, screen_hsize(s), sy);
                    590:        if (cursor) {
                    591:                s->saved_cx = s->cx;
                    592:                s->saved_cy = s->cy;
                    593:        }
                    594:        memcpy(&s->saved_cell, gc, sizeof s->saved_cell);
                    595:
                    596:        grid_view_clear(s->grid, 0, 0, sx, sy, 8);
                    597:
1.60      nicm      598:        s->saved_flags = s->grid->flags;
1.59      nicm      599:        s->grid->flags &= ~GRID_HISTORY;
                    600: }
                    601:
                    602: /* Exit alternate screen mode and restore the copied grid. */
                    603: void
                    604: screen_alternate_off(struct screen *s, struct grid_cell *gc, int cursor)
                    605: {
1.69      nicm      606:        u_int   sx = screen_size_x(s), sy = screen_size_y(s);
                    607:
                    608:        /*
                    609:         * If the current size is different, temporarily resize to the old size
                    610:         * before copying back.
                    611:         */
                    612:        if (s->saved_grid != NULL)
                    613:                screen_resize(s, s->saved_grid->sx, s->saved_grid->sy, 1);
1.59      nicm      614:
                    615:        /*
                    616:         * Restore the cursor position and cell. This happens even if not
                    617:         * currently in the alternate screen.
                    618:         */
                    619:        if (cursor && s->saved_cx != UINT_MAX && s->saved_cy != UINT_MAX) {
                    620:                s->cx = s->saved_cx;
                    621:                s->cy = s->saved_cy;
                    622:                if (gc != NULL)
                    623:                        memcpy(gc, &s->saved_cell, sizeof *gc);
                    624:        }
                    625:
1.69      nicm      626:        /* If not in the alternate screen, do nothing more. */
                    627:        if (s->saved_grid == NULL) {
                    628:                if (s->cx > screen_size_x(s) - 1)
                    629:                        s->cx = screen_size_x(s) - 1;
                    630:                if (s->cy > screen_size_y(s) - 1)
                    631:                        s->cy = screen_size_y(s) - 1;
1.59      nicm      632:                return;
1.69      nicm      633:        }
1.59      nicm      634:
                    635:        /* Restore the saved grid. */
1.69      nicm      636:        grid_duplicate_lines(s->grid, screen_hsize(s), s->saved_grid, 0,
                    637:            s->saved_grid->sy);
1.59      nicm      638:
                    639:        /*
                    640:         * Turn history back on (so resize can use it) and then resize back to
                    641:         * the current size.
                    642:         */
1.60      nicm      643:        if (s->saved_flags & GRID_HISTORY)
                    644:                s->grid->flags |= GRID_HISTORY;
1.69      nicm      645:        screen_resize(s, sx, sy, 1);
1.59      nicm      646:
                    647:        grid_destroy(s->saved_grid);
                    648:        s->saved_grid = NULL;
1.69      nicm      649:
                    650:        if (s->cx > screen_size_x(s) - 1)
                    651:                s->cx = screen_size_x(s) - 1;
                    652:        if (s->cy > screen_size_y(s) - 1)
                    653:                s->cy = screen_size_y(s) - 1;
1.73      nicm      654: }
                    655:
                    656: /* Get mode as a string. */
                    657: const char *
                    658: screen_mode_to_string(int mode)
                    659: {
                    660:        static char     tmp[1024];
                    661:
                    662:        if (mode == 0)
                    663:                return "NONE";
                    664:        if (mode == ALL_MODES)
                    665:                return "ALL";
                    666:
                    667:        *tmp = '\0';
                    668:        if (mode & MODE_CURSOR)
                    669:                strlcat(tmp, "CURSOR,", sizeof tmp);
                    670:        if (mode & MODE_INSERT)
                    671:                strlcat(tmp, "INSERT,", sizeof tmp);
                    672:        if (mode & MODE_KCURSOR)
                    673:                strlcat(tmp, "KCURSOR,", sizeof tmp);
                    674:        if (mode & MODE_KKEYPAD)
                    675:                strlcat(tmp, "KKEYPAD,", sizeof tmp);
                    676:        if (mode & MODE_WRAP)
                    677:                strlcat(tmp, "WRAP,", sizeof tmp);
                    678:        if (mode & MODE_MOUSE_STANDARD)
1.75      nicm      679:                strlcat(tmp, "MOUSE_STANDARD,", sizeof tmp);
1.73      nicm      680:        if (mode & MODE_MOUSE_BUTTON)
1.75      nicm      681:                strlcat(tmp, "MOUSE_BUTTON,", sizeof tmp);
1.76      nicm      682:        if (mode & MODE_CURSOR_BLINKING)
                    683:                strlcat(tmp, "CURSOR_BLINKING,", sizeof tmp);
                    684:        if (mode & MODE_CURSOR_VERY_VISIBLE)
                    685:                strlcat(tmp, "CURSOR_VERY_VISIBLE,", sizeof tmp);
1.73      nicm      686:        if (mode & MODE_MOUSE_UTF8)
                    687:                strlcat(tmp, "UTF8,", sizeof tmp);
                    688:        if (mode & MODE_MOUSE_SGR)
                    689:                strlcat(tmp, "SGR,", sizeof tmp);
                    690:        if (mode & MODE_BRACKETPASTE)
                    691:                strlcat(tmp, "BRACKETPASTE,", sizeof tmp);
                    692:        if (mode & MODE_FOCUSON)
                    693:                strlcat(tmp, "FOCUSON,", sizeof tmp);
                    694:        if (mode & MODE_MOUSE_ALL)
1.75      nicm      695:                strlcat(tmp, "MOUSE_ALL,", sizeof tmp);
1.73      nicm      696:        if (mode & MODE_ORIGIN)
                    697:                strlcat(tmp, "ORIGIN,", sizeof tmp);
                    698:        if (mode & MODE_CRLF)
                    699:                strlcat(tmp, "CRLF,", sizeof tmp);
                    700:        if (mode & MODE_KEXTENDED)
                    701:                strlcat(tmp, "KEXTENDED,", sizeof tmp);
1.74      nicm      702:        tmp[strlen(tmp) - 1] = '\0';
1.73      nicm      703:        return (tmp);
1.1       nicm      704: }