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

1.1     ! nicm        1: /* $OpenBSD$ */
        !             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>
        !            23:
        !            24: #include "tmux.h"
        !            25:
        !            26: void   tty_keys_add(struct tty *, const char *, int, int);
        !            27: int    tty_keys_parse_xterm(struct tty *, char *, size_t, size_t *);
        !            28: int    tty_keys_parse_mouse(struct tty *, char *, size_t, size_t *, u_char *);
        !            29:
        !            30: struct tty_key_ent {
        !            31:        enum tty_code_code      code;
        !            32:        const char             *string;
        !            33:
        !            34:        int                     key;
        !            35:        int                     flags;
        !            36: };
        !            37:
        !            38: struct tty_key_ent tty_keys[] = {
        !            39:        /* Function keys. */
        !            40:        { TTYC_KF1,   NULL,     KEYC_F1,    TTYKEY_CTRL },
        !            41:        { TTYC_KF2,   NULL,     KEYC_F2,    TTYKEY_CTRL },
        !            42:        { TTYC_KF3,   NULL,     KEYC_F3,    TTYKEY_CTRL },
        !            43:        { TTYC_KF4,   NULL,     KEYC_F4,    TTYKEY_CTRL },
        !            44:        { TTYC_KF5,   NULL,     KEYC_F5,    TTYKEY_CTRL },
        !            45:        { TTYC_KF6,   NULL,     KEYC_F6,    TTYKEY_CTRL },
        !            46:        { TTYC_KF7,   NULL,     KEYC_F7,    TTYKEY_CTRL },
        !            47:        { TTYC_KF8,   NULL,     KEYC_F8,    TTYKEY_CTRL },
        !            48:        { TTYC_KF9,   NULL,     KEYC_F9,    TTYKEY_CTRL },
        !            49:        { TTYC_KF10,  NULL,     KEYC_F10,   TTYKEY_CTRL },
        !            50:        { TTYC_KF11,  NULL,     KEYC_F11,   TTYKEY_CTRL },
        !            51:        { TTYC_KF12,  NULL,     KEYC_F12,   TTYKEY_CTRL },
        !            52:        { TTYC_KF13,  NULL,     KEYC_F13,   TTYKEY_CTRL },
        !            53:        { TTYC_KF14,  NULL,     KEYC_F14,   TTYKEY_CTRL },
        !            54:        { TTYC_KF15,  NULL,     KEYC_F15,   TTYKEY_CTRL },
        !            55:        { TTYC_KF16,  NULL,     KEYC_F16,   TTYKEY_CTRL },
        !            56:        { TTYC_KF17,  NULL,     KEYC_F17,   TTYKEY_CTRL },
        !            57:        { TTYC_KF18,  NULL,     KEYC_F18,   TTYKEY_CTRL },
        !            58:        { TTYC_KF19,  NULL,     KEYC_F19,   TTYKEY_CTRL },
        !            59:        { TTYC_KF20,  NULL,     KEYC_F20,   TTYKEY_CTRL },
        !            60:        { TTYC_KICH1, NULL,     KEYC_IC,    TTYKEY_CTRL },
        !            61:        { TTYC_KDCH1, NULL,     KEYC_DC,    TTYKEY_CTRL },
        !            62:        { TTYC_KHOME, NULL,     KEYC_HOME,  TTYKEY_CTRL },
        !            63:        { TTYC_KEND,  NULL,     KEYC_END,   TTYKEY_CTRL },
        !            64:        { TTYC_KNP,   NULL,     KEYC_NPAGE, TTYKEY_CTRL },
        !            65:        { TTYC_KPP,   NULL,     KEYC_PPAGE, TTYKEY_CTRL },
        !            66:        { TTYC_KCBT,  NULL,     KEYC_BTAB,  TTYKEY_CTRL },
        !            67:
        !            68:        /* Arrow keys. */
        !            69:        { 0,          "\033OA", KEYC_UP,    TTYKEY_RAW },
        !            70:        { 0,          "\033OB", KEYC_DOWN,  TTYKEY_RAW },
        !            71:        { 0,          "\033OC", KEYC_RIGHT, TTYKEY_RAW },
        !            72:        { 0,          "\033OD", KEYC_LEFT,  TTYKEY_RAW },
        !            73:
        !            74:        { 0,          "\033[A", KEYC_UP,    TTYKEY_RAW },
        !            75:        { 0,          "\033[B", KEYC_DOWN,  TTYKEY_RAW },
        !            76:        { 0,          "\033[C", KEYC_RIGHT, TTYKEY_RAW },
        !            77:        { 0,          "\033[D", KEYC_LEFT,  TTYKEY_RAW },
        !            78:
        !            79:        { 0,          "\033Oa", KEYC_ADDCTL(KEYC_UP),    TTYKEY_RAW },
        !            80:        { 0,          "\033Ob", KEYC_ADDCTL(KEYC_DOWN),  TTYKEY_RAW },
        !            81:        { 0,          "\033Oc", KEYC_ADDCTL(KEYC_RIGHT), TTYKEY_RAW },
        !            82:        { 0,          "\033Od", KEYC_ADDCTL(KEYC_LEFT),  TTYKEY_RAW },
        !            83:        { 0,          "\033[a", KEYC_ADDSFT(KEYC_UP),    TTYKEY_RAW },
        !            84:        { 0,          "\033[b", KEYC_ADDSFT(KEYC_DOWN),  TTYKEY_RAW },
        !            85:        { 0,          "\033[c", KEYC_ADDSFT(KEYC_RIGHT), TTYKEY_RAW },
        !            86:        { 0,          "\033[d", KEYC_ADDSFT(KEYC_LEFT),  TTYKEY_RAW },
        !            87:
        !            88:        { TTYC_KCUU1, NULL,     KEYC_UP,    TTYKEY_CTRL },
        !            89:        { TTYC_KCUD1, NULL,     KEYC_DOWN,  TTYKEY_CTRL },
        !            90:        { TTYC_KCUB1, NULL,     KEYC_LEFT,  TTYKEY_CTRL },
        !            91:        { TTYC_KCUF1, NULL,     KEYC_RIGHT, TTYKEY_CTRL },
        !            92:
        !            93:        /*
        !            94:         * Numeric keypad. termcap and terminfo are totally confusing for this.
        !            95:         * There are definitions for some keypad keys and for function keys,
        !            96:         * but these seem to now be used for the real function keys rather than
        !            97:         * for the keypad keys in application mode (which is different from
        !            98:         * what it says in the termcap file). So, we just hardcode the vt100
        !            99:         * escape sequences here and always put the terminal into keypad_xmit
        !           100:         * mode. Translation of numbers mode/applications mode is done in
        !           101:         * input-keys.c.
        !           102:         */
        !           103:        { 0,          "\033Oo", KEYC_KP0_1, TTYKEY_RAW },
        !           104:        { 0,          "\033Oj", KEYC_KP0_2, TTYKEY_RAW },
        !           105:        { 0,          "\033Om", KEYC_KP0_3, TTYKEY_RAW },
        !           106:        { 0,          "\033Ow", KEYC_KP1_0, TTYKEY_RAW },
        !           107:        { 0,          "\033Ox", KEYC_KP1_1, TTYKEY_RAW },
        !           108:        { 0,          "\033Oy", KEYC_KP1_2, TTYKEY_RAW },
        !           109:        { 0,          "\033Ok", KEYC_KP1_3, TTYKEY_RAW },
        !           110:        { 0,          "\033Ot", KEYC_KP2_0, TTYKEY_RAW },
        !           111:        { 0,          "\033Ou", KEYC_KP2_1, TTYKEY_RAW },
        !           112:        { 0,          "\033Ov", KEYC_KP2_2, TTYKEY_RAW },
        !           113:        { 0,          "\033Oq", KEYC_KP3_0, TTYKEY_RAW },
        !           114:        { 0,          "\033Or", KEYC_KP3_1, TTYKEY_RAW },
        !           115:        { 0,          "\033Os", KEYC_KP3_2, TTYKEY_RAW },
        !           116:        { 0,          "\033OM", KEYC_KP3_3, TTYKEY_RAW },
        !           117:        { 0,          "\033Op", KEYC_KP4_0, TTYKEY_RAW },
        !           118:        { 0,          "\033On", KEYC_KP4_2, TTYKEY_RAW },
        !           119: };
        !           120:
        !           121: RB_GENERATE(tty_keys, tty_key, entry, tty_keys_cmp);
        !           122:
        !           123: struct tty_key *tty_keys_find(struct tty *, char *, size_t, size_t *);
        !           124:
        !           125: int
        !           126: tty_keys_cmp(struct tty_key *k1, struct tty_key *k2)
        !           127: {
        !           128:        return (strcmp(k1->string, k2->string));
        !           129: }
        !           130:
        !           131: void
        !           132: tty_keys_add(struct tty *tty, const char *s, int key, int flags)
        !           133: {
        !           134:        struct tty_key  *tk, *tl;
        !           135:
        !           136:        tk = xmalloc(sizeof *tk);
        !           137:        tk->string = xstrdup(s);
        !           138:        tk->key = key;
        !           139:        tk->flags = flags;
        !           140:
        !           141:        if ((tl = RB_INSERT(tty_keys, &tty->ktree, tk)) != NULL) {
        !           142:                xfree(tk->string);
        !           143:                xfree(tk);
        !           144:                log_debug("key exists: %s (old %x, new %x)", s, tl->key, key);
        !           145:                return;
        !           146:        }
        !           147:
        !           148:        if (strlen(tk->string) > tty->ksize)
        !           149:                tty->ksize = strlen(tk->string);
        !           150:        log_debug("new key %x: size now %zu (%s)", key, tty->ksize, tk->string);
        !           151: }
        !           152:
        !           153: void
        !           154: tty_keys_init(struct tty *tty)
        !           155: {
        !           156:        struct tty_key_ent      *tke;
        !           157:        u_int                    i;
        !           158:        const char              *s;
        !           159:        char                     tmp[64];
        !           160:
        !           161:        RB_INIT(&tty->ktree);
        !           162:
        !           163:        tty->ksize = 0;
        !           164:        for (i = 0; i < nitems(tty_keys); i++) {
        !           165:                tke = &tty_keys[i];
        !           166:
        !           167:                if (tke->flags & TTYKEY_RAW)
        !           168:                        s = tke->string;
        !           169:                else {
        !           170:                        if (!tty_term_has(tty->term, tke->code))
        !           171:                                continue;
        !           172:                        s = tty_term_string(tty->term, tke->code);
        !           173:                        if (s[0] != '\033' || s[1] == '\0')
        !           174:                                continue;
        !           175:                }
        !           176:
        !           177:                tty_keys_add(tty, s + 1, tke->key, tke->flags);
        !           178:                if (tke->flags & TTYKEY_CTRL) {
        !           179:                        if (strlcpy(tmp, s, sizeof tmp) >= sizeof tmp)
        !           180:                                continue;
        !           181:                        tmp[strlen(tmp) - 1] ^= 0x20;
        !           182:                        tty_keys_add(tty, tmp + 1, KEYC_ADDCTL(tke->key), 0);
        !           183:                }
        !           184:        }
        !           185: }
        !           186:
        !           187: void
        !           188: tty_keys_free(struct tty *tty)
        !           189: {
        !           190:        struct tty_key  *tk;
        !           191:
        !           192:        while (!RB_EMPTY(&tty->ktree)) {
        !           193:                tk = RB_ROOT(&tty->ktree);
        !           194:                RB_REMOVE(tty_keys, &tty->ktree, tk);
        !           195:                xfree(tk->string);
        !           196:                xfree(tk);
        !           197:        }
        !           198: }
        !           199:
        !           200: struct tty_key *
        !           201: tty_keys_find(struct tty *tty, char *buf, size_t len, size_t *size)
        !           202: {
        !           203:        struct tty_key  *tk, tl;
        !           204:        char            *s;
        !           205:
        !           206:        if (len == 0)
        !           207:                return (NULL);
        !           208:
        !           209:        s = xmalloc(tty->ksize + 1);
        !           210:        for (*size = tty->ksize; (*size) > 0; (*size)--) {
        !           211:                if ((*size) > len)
        !           212:                        continue;
        !           213:                memcpy(s, buf, *size);
        !           214:                s[*size] = '\0';
        !           215:
        !           216:                log_debug2("looking for key: %s", s);
        !           217:
        !           218:                tl.string = s;
        !           219:                tk = RB_FIND(tty_keys, &tty->ktree, &tl);
        !           220:                if (tk != NULL) {
        !           221:                        log_debug2("got key: 0x%x", tk->key);
        !           222:                        xfree(s);
        !           223:                        return (tk);
        !           224:                }
        !           225:        }
        !           226:        xfree(s);
        !           227:
        !           228:        return (NULL);
        !           229: }
        !           230:
        !           231: int
        !           232: tty_keys_next(struct tty *tty, int *key, u_char *mouse)
        !           233: {
        !           234:        struct tty_key  *tk;
        !           235:        struct timeval   tv;
        !           236:        char            *buf;
        !           237:        size_t           len, size;
        !           238:
        !           239:        buf = BUFFER_OUT(tty->in);
        !           240:        len = BUFFER_USED(tty->in);
        !           241:        if (len == 0)
        !           242:                return (1);
        !           243:        log_debug("keys are %zu (%.*s)", len, (int) len, buf);
        !           244:
        !           245:        /* If a normal key, return it. */
        !           246:        if (*buf != '\033') {
        !           247:                *key = buffer_read8(tty->in);
        !           248:                goto found;
        !           249:        }
        !           250:
        !           251:        /* Look for matching key string and return if found. */
        !           252:        tk = tty_keys_find(tty, buf + 1, len - 1, &size);
        !           253:        if (tk != NULL) {
        !           254:                buffer_remove(tty->in, size + 1);
        !           255:                *key = tk->key;
        !           256:                goto found;
        !           257:        }
        !           258:
        !           259:        /* Not found. Is this a mouse key press? */
        !           260:        *key = tty_keys_parse_mouse(tty, buf, len, &size, mouse);
        !           261:        if (*key != KEYC_NONE) {
        !           262:                buffer_remove(tty->in, size);
        !           263:                goto found;
        !           264:        }
        !           265:
        !           266:        /* Not found. Try to parse xterm-type arguments. */
        !           267:        *key = tty_keys_parse_xterm(tty, buf, len, &size);
        !           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)
        !           278:                        fatal("gettimeofday");
        !           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);
        !           292:                *key = KEYC_ADDESC(buffer_read8(tty->in));
        !           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);
        !           301:                        *key = KEYC_ADDESC(tk->key);
        !           302:                        goto found;
        !           303:                }
        !           304:        }
        !           305:
        !           306:        /* If the timer hasn't expired, keep waiting. */
        !           307:        if (gettimeofday(&tv, NULL) != 0)
        !           308:                fatal("gettimeofday");
        !           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
        !           322: tty_keys_parse_mouse(
        !           323:     unused struct tty *tty, char *buf, size_t len, size_t *size, u_char *mouse)
        !           324: {
        !           325:        /*
        !           326:         * Mouse sequences are \033[M followed by three characters indicating
        !           327:         * buttons, X and Y, all based at 32 with 1,1 top-left.
        !           328:         */
        !           329:
        !           330:        log_debug("mouse input is: %.*s", (int) len, buf);
        !           331:        if (len != 6 || memcmp(buf, "\033[M", 3) != 0)
        !           332:                return (KEYC_NONE);
        !           333:        *size = 6;
        !           334:
        !           335:        if (buf[3] < 32 || buf[4] < 33 || buf[5] < 33)
        !           336:                return (KEYC_NONE);
        !           337:
        !           338:        mouse[0] = buf[3] - 32;
        !           339:        mouse[1] = buf[4] - 33;
        !           340:        mouse[2] = buf[5] - 33;
        !           341:        return (KEYC_MOUSE);
        !           342: }
        !           343:
        !           344: int
        !           345: tty_keys_parse_xterm(struct tty *tty, char *buf, size_t len, size_t *size)
        !           346: {
        !           347:        struct tty_key  *tk;
        !           348:        char             tmp[5];
        !           349:        size_t           tmplen;
        !           350:        int              key;
        !           351:
        !           352:        /*
        !           353:         * xterm sequences with modifier keys are of the form:
        !           354:         *
        !           355:         * ^[[1;xD becomes ^[[D
        !           356:         * ^[[5;x~ becomes ^[[5~
        !           357:         *
        !           358:         * This function is a bit of a hack. Need to figure out what exact
        !           359:         * format and meaning xterm outputs and fix it. XXX
        !           360:         */
        !           361:
        !           362:        log_debug("xterm input is: %.*s", (int) len, buf);
        !           363:        if (len != 6 || memcmp(buf, "\033[1;", 4) != 0)
        !           364:                return (KEYC_NONE);
        !           365:        *size = 6;
        !           366:
        !           367:        tmplen = 0;
        !           368:        tmp[tmplen++] = '[';
        !           369:        if (buf[5] == '~') {
        !           370:                tmp[tmplen++] = buf[2];
        !           371:                tmp[tmplen++] = '~';
        !           372:        } else
        !           373:                tmp[tmplen++] = buf[5];
        !           374:        log_debug("xterm output is: %.*s", (int) tmplen, tmp);
        !           375:
        !           376:        tk = tty_keys_find(tty, tmp, tmplen, size);
        !           377:        if (tk == NULL)
        !           378:                return (KEYC_NONE);
        !           379:        key = tk->key;
        !           380:
        !           381:        switch (buf[4]) {
        !           382:        case '8':
        !           383:                key = KEYC_ADDSFT(key);
        !           384:                key = KEYC_ADDESC(key);
        !           385:                key = KEYC_ADDCTL(key);
        !           386:                break;
        !           387:        case '7':
        !           388:                key = KEYC_ADDESC(key);
        !           389:                key = KEYC_ADDCTL(key);
        !           390:                break;
        !           391:        case '6':
        !           392:                key = KEYC_ADDSFT(key);
        !           393:                key = KEYC_ADDCTL(key);
        !           394:                break;
        !           395:        case '5':
        !           396:                key = KEYC_ADDCTL(key);
        !           397:                break;
        !           398:        case '4':
        !           399:                key = KEYC_ADDSFT(key);
        !           400:                key = KEYC_ADDESC(key);
        !           401:                break;
        !           402:        case '3':
        !           403:                key = KEYC_ADDESC(key);
        !           404:                break;
        !           405:        case '2':
        !           406:                key = KEYC_ADDSFT(key);
        !           407:                break;
        !           408:        }
        !           409:
        !           410:        *size = 6;
        !           411:        return (key);
        !           412: }