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

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