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

Annotation of src/usr.bin/tmux/tty-keys.c, Revision 1.8

1.8     ! nicm        1: /* $OpenBSD: tty-keys.c,v 1.7 2009/10/26 13:13:33 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: #include <sys/time.h>
                     21:
                     22: #include <string.h>
1.3       nicm       23: #include <termios.h>
                     24: #include <unistd.h>
1.1       nicm       25:
                     26: #include "tmux.h"
                     27:
                     28: void   tty_keys_add(struct tty *, const char *, int, int);
1.6       nicm       29: int    tty_keys_mouse(char *, size_t, size_t *, struct mouse_event *);
1.1       nicm       30:
                     31: struct tty_key_ent {
                     32:        enum tty_code_code      code;
                     33:        const char             *string;
                     34:
                     35:        int                     key;
                     36:        int                     flags;
                     37: };
                     38:
                     39: struct tty_key_ent tty_keys[] = {
                     40:        /* Function keys. */
1.8     ! nicm       41:        { TTYC_KF1,     NULL,           KEYC_F1,                TTYKEY_CTRL },
        !            42:        { TTYC_KF1,     NULL,           KEYC_F1,                TTYKEY_CTRL },
        !            43:        { TTYC_KF2,     NULL,           KEYC_F2,                TTYKEY_CTRL },
        !            44:        { TTYC_KF3,     NULL,           KEYC_F3,                TTYKEY_CTRL },
        !            45:        { TTYC_KF4,     NULL,           KEYC_F4,                TTYKEY_CTRL },
        !            46:        { TTYC_KF5,     NULL,           KEYC_F5,                TTYKEY_CTRL },
        !            47:        { TTYC_KF6,     NULL,           KEYC_F6,                TTYKEY_CTRL },
        !            48:        { TTYC_KF7,     NULL,           KEYC_F7,                TTYKEY_CTRL },
        !            49:        { TTYC_KF8,     NULL,           KEYC_F8,                TTYKEY_CTRL },
        !            50:        { TTYC_KF9,     NULL,           KEYC_F9,                TTYKEY_CTRL },
        !            51:        { TTYC_KF10,    NULL,           KEYC_F10,               TTYKEY_CTRL },
        !            52:        { TTYC_KF11,    NULL,           KEYC_F11,               TTYKEY_CTRL },
        !            53:        { TTYC_KF12,    NULL,           KEYC_F12,               TTYKEY_CTRL },
        !            54:        { TTYC_KF13,    NULL,           KEYC_F13,               TTYKEY_CTRL },
        !            55:        { TTYC_KF14,    NULL,           KEYC_F14,               TTYKEY_CTRL },
        !            56:        { TTYC_KF15,    NULL,           KEYC_F15,               TTYKEY_CTRL },
        !            57:        { TTYC_KF16,    NULL,           KEYC_F16,               TTYKEY_CTRL },
        !            58:        { TTYC_KF17,    NULL,           KEYC_F17,               TTYKEY_CTRL },
        !            59:        { TTYC_KF18,    NULL,           KEYC_F18,               TTYKEY_CTRL },
        !            60:        { TTYC_KF19,    NULL,           KEYC_F19,               TTYKEY_CTRL },
        !            61:        { TTYC_KF20,    NULL,           KEYC_F20,               TTYKEY_CTRL },
        !            62:        { TTYC_KICH1,   NULL,           KEYC_IC,                TTYKEY_CTRL },
        !            63:        { TTYC_KDCH1,   NULL,           KEYC_DC,                TTYKEY_CTRL },
        !            64:        { TTYC_KHOME,   NULL,           KEYC_HOME,              TTYKEY_CTRL },
        !            65:        { TTYC_KEND,    NULL,           KEYC_END,               TTYKEY_CTRL },
        !            66:        { TTYC_KNP,     NULL,           KEYC_NPAGE,             TTYKEY_CTRL },
        !            67:        { TTYC_KPP,     NULL,           KEYC_PPAGE,             TTYKEY_CTRL },
        !            68:        { TTYC_KCBT,    NULL,           KEYC_BTAB,              TTYKEY_CTRL },
1.1       nicm       69:
                     70:        /* Arrow keys. */
1.8     ! nicm       71:        { 0,            "\033OA",       KEYC_UP,                TTYKEY_RAW },
        !            72:        { 0,            "\033OB",       KEYC_DOWN,              TTYKEY_RAW },
        !            73:        { 0,            "\033OC",       KEYC_RIGHT,             TTYKEY_RAW },
        !            74:        { 0,            "\033OD",       KEYC_LEFT,              TTYKEY_RAW },
        !            75:
        !            76:        { 0,            "\033[A",       KEYC_UP,                TTYKEY_RAW },
        !            77:        { 0,            "\033[B",       KEYC_DOWN,              TTYKEY_RAW },
        !            78:        { 0,            "\033[C",       KEYC_RIGHT,             TTYKEY_RAW },
        !            79:        { 0,            "\033[D",       KEYC_LEFT,              TTYKEY_RAW },
        !            80:
        !            81:        { 0,            "\033Oa",       KEYC_UP|KEYC_CTRL,      TTYKEY_RAW },
        !            82:        { 0,            "\033Ob",       KEYC_DOWN|KEYC_CTRL,    TTYKEY_RAW },
        !            83:        { 0,            "\033Oc",       KEYC_RIGHT|KEYC_CTRL,   TTYKEY_RAW },
        !            84:        { 0,            "\033Od",       KEYC_LEFT|KEYC_CTRL,    TTYKEY_RAW },
        !            85:        { 0,            "\033[a",       KEYC_UP|KEYC_SHIFT,     TTYKEY_RAW },
        !            86:        { 0,            "\033[b",       KEYC_DOWN|KEYC_SHIFT,   TTYKEY_RAW },
        !            87:        { 0,            "\033[c",       KEYC_RIGHT|KEYC_SHIFT,  TTYKEY_RAW },
        !            88:        { 0,            "\033[d",       KEYC_LEFT|KEYC_SHIFT,   TTYKEY_RAW },
        !            89:
        !            90:        { TTYC_KCUU1,   NULL,           KEYC_UP,                TTYKEY_CTRL },
        !            91:        { TTYC_KCUD1,   NULL,           KEYC_DOWN,              TTYKEY_CTRL },
        !            92:        { TTYC_KCUB1,   NULL,           KEYC_LEFT,              TTYKEY_CTRL },
        !            93:        { TTYC_KCUF1,   NULL,           KEYC_RIGHT,             TTYKEY_CTRL },
1.1       nicm       94:
                     95:        /*
1.8     ! nicm       96:         * Numeric keypad. Just use the vt100 escape sequences here and always
        !            97:         * put the terminal into keypad_xmit mode. Translation of numbers
        !            98:         * mode/applications mode is done in input-keys.c.
1.1       nicm       99:         */
1.8     ! nicm      100:        { 0,            "\033Oo",       KEYC_KP_SLASH,          TTYKEY_RAW },
        !           101:        { 0,            "\033Oj",       KEYC_KP_STAR,           TTYKEY_RAW },
        !           102:        { 0,            "\033Om",       KEYC_KP_MINUS,          TTYKEY_RAW },
        !           103:        { 0,            "\033Ow",       KEYC_KP_SEVEN,          TTYKEY_RAW },
        !           104:        { 0,            "\033Ox",       KEYC_KP_EIGHT,          TTYKEY_RAW },
        !           105:        { 0,            "\033Oy",       KEYC_KP_NINE,           TTYKEY_RAW },
        !           106:        { 0,            "\033Ok",       KEYC_KP_PLUS,           TTYKEY_RAW },
        !           107:        { 0,            "\033Ot",       KEYC_KP_FOUR,           TTYKEY_RAW },
        !           108:        { 0,            "\033Ou",       KEYC_KP_FIVE,           TTYKEY_RAW },
        !           109:        { 0,            "\033Ov",       KEYC_KP_SIX,            TTYKEY_RAW },
        !           110:        { 0,            "\033Oq",       KEYC_KP_ONE,            TTYKEY_RAW },
        !           111:        { 0,            "\033Or",       KEYC_KP_TWO,            TTYKEY_RAW },
        !           112:        { 0,            "\033Os",       KEYC_KP_THREE,          TTYKEY_RAW },
        !           113:        { 0,            "\033OM",       KEYC_KP_ENTER,          TTYKEY_RAW },
        !           114:        { 0,            "\033Op",       KEYC_KP_ZERO,           TTYKEY_RAW },
        !           115:        { 0,            "\033On",       KEYC_KP_PERIOD,         TTYKEY_RAW },
1.1       nicm      116: };
                    117:
                    118: RB_GENERATE(tty_keys, tty_key, entry, tty_keys_cmp);
                    119:
                    120: struct tty_key *tty_keys_find(struct tty *, char *, size_t, size_t *);
                    121:
                    122: int
                    123: tty_keys_cmp(struct tty_key *k1, struct tty_key *k2)
                    124: {
                    125:        return (strcmp(k1->string, k2->string));
                    126: }
                    127:
                    128: void
                    129: tty_keys_add(struct tty *tty, const char *s, int key, int flags)
                    130: {
                    131:        struct tty_key  *tk, *tl;
                    132:
                    133:        tk = xmalloc(sizeof *tk);
                    134:        tk->string = xstrdup(s);
                    135:        tk->key = key;
                    136:        tk->flags = flags;
                    137:
                    138:        if ((tl = RB_INSERT(tty_keys, &tty->ktree, tk)) != NULL) {
                    139:                xfree(tk->string);
                    140:                xfree(tk);
                    141:                log_debug("key exists: %s (old %x, new %x)", s, tl->key, key);
                    142:                return;
                    143:        }
                    144:
                    145:        if (strlen(tk->string) > tty->ksize)
                    146:                tty->ksize = strlen(tk->string);
                    147:        log_debug("new key %x: size now %zu (%s)", key, tty->ksize, tk->string);
                    148: }
                    149:
                    150: void
                    151: tty_keys_init(struct tty *tty)
                    152: {
                    153:        struct tty_key_ent      *tke;
                    154:        u_int                    i;
                    155:        const char              *s;
                    156:        char                     tmp[64];
                    157:
                    158:        RB_INIT(&tty->ktree);
                    159:
                    160:        tty->ksize = 0;
                    161:        for (i = 0; i < nitems(tty_keys); i++) {
                    162:                tke = &tty_keys[i];
                    163:
                    164:                if (tke->flags & TTYKEY_RAW)
                    165:                        s = tke->string;
                    166:                else {
                    167:                        if (!tty_term_has(tty->term, tke->code))
                    168:                                continue;
                    169:                        s = tty_term_string(tty->term, tke->code);
                    170:                        if (s[0] != '\033' || s[1] == '\0')
                    171:                                continue;
                    172:                }
                    173:
                    174:                tty_keys_add(tty, s + 1, tke->key, tke->flags);
                    175:                if (tke->flags & TTYKEY_CTRL) {
                    176:                        if (strlcpy(tmp, s, sizeof tmp) >= sizeof tmp)
                    177:                                continue;
                    178:                        tmp[strlen(tmp) - 1] ^= 0x20;
1.2       nicm      179:                        tty_keys_add(tty, tmp + 1, tke->key | KEYC_CTRL, 0);
1.1       nicm      180:                }
                    181:        }
                    182: }
                    183:
                    184: void
                    185: tty_keys_free(struct tty *tty)
                    186: {
                    187:        struct tty_key  *tk;
                    188:
                    189:        while (!RB_EMPTY(&tty->ktree)) {
                    190:                tk = RB_ROOT(&tty->ktree);
                    191:                RB_REMOVE(tty_keys, &tty->ktree, tk);
                    192:                xfree(tk->string);
                    193:                xfree(tk);
                    194:        }
                    195: }
                    196:
                    197: struct tty_key *
                    198: tty_keys_find(struct tty *tty, char *buf, size_t len, size_t *size)
                    199: {
                    200:        struct tty_key  *tk, tl;
                    201:        char            *s;
                    202:
                    203:        if (len == 0)
                    204:                return (NULL);
                    205:
                    206:        s = xmalloc(tty->ksize + 1);
                    207:        for (*size = tty->ksize; (*size) > 0; (*size)--) {
                    208:                if ((*size) > len)
                    209:                        continue;
                    210:                memcpy(s, buf, *size);
                    211:                s[*size] = '\0';
                    212:
                    213:                log_debug2("looking for key: %s", s);
                    214:
                    215:                tl.string = s;
                    216:                tk = RB_FIND(tty_keys, &tty->ktree, &tl);
                    217:                if (tk != NULL) {
                    218:                        log_debug2("got key: 0x%x", tk->key);
                    219:                        xfree(s);
                    220:                        return (tk);
                    221:                }
                    222:        }
                    223:        xfree(s);
                    224:
                    225:        return (NULL);
                    226: }
                    227:
                    228: int
1.5       nicm      229: tty_keys_next(struct tty *tty, int *key, struct mouse_event *mouse)
1.1       nicm      230: {
                    231:        struct tty_key  *tk;
                    232:        struct timeval   tv;
                    233:        char            *buf;
                    234:        size_t           len, size;
1.3       nicm      235:        cc_t             bspace;
1.1       nicm      236:
                    237:        buf = BUFFER_OUT(tty->in);
                    238:        len = BUFFER_USED(tty->in);
                    239:        if (len == 0)
                    240:                return (1);
                    241:        log_debug("keys are %zu (%.*s)", len, (int) len, buf);
                    242:
                    243:        /* If a normal key, return it. */
                    244:        if (*buf != '\033') {
                    245:                *key = buffer_read8(tty->in);
1.3       nicm      246:
                    247:                /*
                    248:                 * Check for backspace key using termios VERASE - the terminfo
                    249:                 * kbs entry is extremely unreliable, so cannot be safely
                    250:                 * used. termios should have a better idea.
                    251:                 */
                    252:                bspace = tty->tio.c_cc[VERASE];
                    253:                if (bspace != _POSIX_VDISABLE && *key == bspace)
                    254:                        *key = KEYC_BSPACE;
1.1       nicm      255:                goto found;
                    256:        }
                    257:
                    258:        /* Look for matching key string and return if found. */
                    259:        tk = tty_keys_find(tty, buf + 1, len - 1, &size);
                    260:        if (tk != NULL) {
                    261:                buffer_remove(tty->in, size + 1);
                    262:                *key = tk->key;
                    263:                goto found;
                    264:        }
                    265:
                    266:        /* Not found. Is this a mouse key press? */
1.6       nicm      267:        *key = tty_keys_mouse(buf, len, &size, mouse);
1.1       nicm      268:        if (*key != KEYC_NONE) {
                    269:                buffer_remove(tty->in, size);
                    270:                goto found;
                    271:        }
                    272:
                    273:        /* Escape but no key string. If the timer isn't started, start it. */
                    274:        if (!(tty->flags & TTY_ESCAPE)) {
                    275:                tv.tv_sec = 0;
                    276:                tv.tv_usec = ESCAPE_PERIOD * 1000L;
                    277:                if (gettimeofday(&tty->key_timer, NULL) != 0)
1.4       nicm      278:                        fatal("gettimeofday failed");
1.1       nicm      279:                timeradd(&tty->key_timer, &tv, &tty->key_timer);
                    280:
                    281:                tty->flags |= TTY_ESCAPE;
                    282:                return (1);
                    283:        }
                    284:
                    285:        /* Skip the escape. */
                    286:        buf++;
                    287:        len--;
                    288:
                    289:        /* Is there a normal key following? */
                    290:        if (len != 0 && *buf != '\033') {
                    291:                buffer_remove(tty->in, 1);
1.2       nicm      292:                *key = buffer_read8(tty->in) | KEYC_ESCAPE;
1.1       nicm      293:                goto found;
                    294:        }
                    295:
                    296:        /* Or a key string? */
                    297:        if (len > 1) {
                    298:                tk = tty_keys_find(tty, buf + 1, len - 1, &size);
                    299:                if (tk != NULL) {
                    300:                        buffer_remove(tty->in, size + 2);
1.2       nicm      301:                        *key = tk->key | KEYC_ESCAPE;
1.1       nicm      302:                        goto found;
                    303:                }
                    304:        }
                    305:
                    306:        /* If the timer hasn't expired, keep waiting. */
                    307:        if (gettimeofday(&tv, NULL) != 0)
1.4       nicm      308:                fatal("gettimeofday failed");
1.1       nicm      309:        if (timercmp(&tty->key_timer, &tv, >))
                    310:                return (1);
                    311:
                    312:        /* Give up and return the escape. */
                    313:        buffer_remove(tty->in, 1);
                    314:        *key = '\033';
                    315:
                    316: found:
                    317:        tty->flags &= ~TTY_ESCAPE;
                    318:        return (0);
                    319: }
                    320:
                    321: int
1.6       nicm      322: tty_keys_mouse(char *buf, size_t len, size_t *size, struct mouse_event *m)
1.1       nicm      323: {
                    324:        /*
                    325:         * Mouse sequences are \033[M followed by three characters indicating
                    326:         * buttons, X and Y, all based at 32 with 1,1 top-left.
                    327:         */
                    328:
                    329:        log_debug("mouse input is: %.*s", (int) len, buf);
                    330:        if (len != 6 || memcmp(buf, "\033[M", 3) != 0)
                    331:                return (KEYC_NONE);
                    332:        *size = 6;
                    333:
1.5       nicm      334:        m->b = buf[3];
                    335:        m->x = buf[4];
                    336:        m->y = buf[5];
                    337:        if (m->b < 32 || m->x < 33 || m->y < 33)
1.1       nicm      338:                return (KEYC_NONE);
1.5       nicm      339:        m->b -= 32;
                    340:        m->x -= 33;
                    341:        m->y -= 33;
1.1       nicm      342:        return (KEYC_MOUSE);
                    343: }