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

1.20    ! nicm        1: /* $OpenBSD: tty-keys.c,v 1.19 2009/11/09 14:40:06 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: /*
1.16      nicm       29:  * Handle keys input from the outside terminal. tty_keys[] is a base table of
                     30:  * supported keys which are looked up in terminfo(5) and translated into a
                     31:  * ternary tree (a binary tree of binary trees).
1.9       nicm       32:  */
                     33:
1.16      nicm       34: void           tty_keys_add1(struct tty_key **, const char *, int);
                     35: void           tty_keys_add(struct tty *, const char *, int);
                     36: void           tty_keys_free1(struct tty_key *);
                     37: struct tty_key *tty_keys_find1(
                     38:                    struct tty_key *, const char *, size_t, size_t *);
                     39: struct tty_key *tty_keys_find(struct tty *, const char *, size_t, size_t *);
                     40: void           tty_keys_callback(int, short, void *);
1.18      nicm       41: int            tty_keys_mouse(
                     42:                    const char *, size_t, size_t *, struct mouse_event *);
1.1       nicm       43:
                     44: struct tty_key_ent {
                     45:        enum tty_code_code      code;
                     46:        const char             *string;
                     47:
                     48:        int                     key;
                     49:        int                     flags;
1.20    ! nicm       50: #define TTYKEY_RAW 0x1
1.1       nicm       51: };
                     52:
1.16      nicm       53: /*
                     54:  * Default key tables. Those flagged with TTYKEY_RAW are inserted directly,
1.20    ! nicm       55:  * otherwise they are looked up in terminfo(5).
1.16      nicm       56:  */
1.1       nicm       57: struct tty_key_ent tty_keys[] = {
                     58:        /* Function keys. */
1.20    ! nicm       59:        { TTYC_KF1,     NULL,           KEYC_F1,                0 },
        !            60:        { TTYC_KF1,     NULL,           KEYC_F1,                0 },
        !            61:        { TTYC_KF2,     NULL,           KEYC_F2,                0 },
        !            62:        { TTYC_KF3,     NULL,           KEYC_F3,                0 },
        !            63:        { TTYC_KF4,     NULL,           KEYC_F4,                0 },
        !            64:        { TTYC_KF5,     NULL,           KEYC_F5,                0 },
        !            65:        { TTYC_KF6,     NULL,           KEYC_F6,                0 },
        !            66:        { TTYC_KF7,     NULL,           KEYC_F7,                0 },
        !            67:        { TTYC_KF8,     NULL,           KEYC_F8,                0 },
        !            68:        { TTYC_KF9,     NULL,           KEYC_F9,                0 },
        !            69:        { TTYC_KF10,    NULL,           KEYC_F10,               0 },
        !            70:        { TTYC_KF11,    NULL,           KEYC_F11,               0 },
        !            71:        { TTYC_KF12,    NULL,           KEYC_F12,               0 },
        !            72:        { TTYC_KF13,    NULL,           KEYC_F13,               0 },
        !            73:        { TTYC_KF14,    NULL,           KEYC_F14,               0 },
        !            74:        { TTYC_KF15,    NULL,           KEYC_F15,               0 },
        !            75:        { TTYC_KF16,    NULL,           KEYC_F16,               0 },
        !            76:        { TTYC_KF17,    NULL,           KEYC_F17,               0 },
        !            77:        { TTYC_KF18,    NULL,           KEYC_F18,               0 },
        !            78:        { TTYC_KF19,    NULL,           KEYC_F19,               0 },
        !            79:        { TTYC_KF20,    NULL,           KEYC_F20,               0 },
        !            80:        { TTYC_KICH1,   NULL,           KEYC_IC,                0 },
        !            81:        { TTYC_KDCH1,   NULL,           KEYC_DC,                0 },
        !            82:        { TTYC_KHOME,   NULL,           KEYC_HOME,              0 },
        !            83:        { TTYC_KEND,    NULL,           KEYC_END,               0 },
        !            84:        { TTYC_KNP,     NULL,           KEYC_NPAGE,             0 },
        !            85:        { TTYC_KPP,     NULL,           KEYC_PPAGE,             0 },
        !            86:        { TTYC_KCBT,    NULL,           KEYC_BTAB,              0 },
1.1       nicm       87:
1.12      nicm       88:        /* Arrow keys. */
1.8       nicm       89:        { 0,            "\033OA",       KEYC_UP,                TTYKEY_RAW },
                     90:        { 0,            "\033OB",       KEYC_DOWN,              TTYKEY_RAW },
                     91:        { 0,            "\033OC",       KEYC_RIGHT,             TTYKEY_RAW },
                     92:        { 0,            "\033OD",       KEYC_LEFT,              TTYKEY_RAW },
                     93:
1.12      nicm       94:        { 0,            "\033[A",       KEYC_UP,                TTYKEY_RAW },
                     95:        { 0,            "\033[B",       KEYC_DOWN,              TTYKEY_RAW },
                     96:        { 0,            "\033[C",       KEYC_RIGHT,             TTYKEY_RAW },
                     97:        { 0,            "\033[D",       KEYC_LEFT,              TTYKEY_RAW },
1.8       nicm       98:
1.20    ! nicm       99:        { TTYC_KCUU1,   NULL,           KEYC_UP,                0 },
        !           100:        { TTYC_KCUD1,   NULL,           KEYC_DOWN,              0 },
        !           101:        { TTYC_KCUB1,   NULL,           KEYC_LEFT,              0 },
        !           102:        { TTYC_KCUF1,   NULL,           KEYC_RIGHT,             0 },
1.12      nicm      103:
                    104:        /* Special-case arrow keys for rxvt until terminfo has kRIT5 etc. */
1.8       nicm      105:        { 0,            "\033Oa",       KEYC_UP|KEYC_CTRL,      TTYKEY_RAW },
                    106:        { 0,            "\033Ob",       KEYC_DOWN|KEYC_CTRL,    TTYKEY_RAW },
                    107:        { 0,            "\033Oc",       KEYC_RIGHT|KEYC_CTRL,   TTYKEY_RAW },
                    108:        { 0,            "\033Od",       KEYC_LEFT|KEYC_CTRL,    TTYKEY_RAW },
1.9       nicm      109:
1.8       nicm      110:        { 0,            "\033[a",       KEYC_UP|KEYC_SHIFT,     TTYKEY_RAW },
                    111:        { 0,            "\033[b",       KEYC_DOWN|KEYC_SHIFT,   TTYKEY_RAW },
                    112:        { 0,            "\033[c",       KEYC_RIGHT|KEYC_SHIFT,  TTYKEY_RAW },
                    113:        { 0,            "\033[d",       KEYC_LEFT|KEYC_SHIFT,   TTYKEY_RAW },
1.1       nicm      114:
                    115:        /*
1.8       nicm      116:         * Numeric keypad. Just use the vt100 escape sequences here and always
                    117:         * put the terminal into keypad_xmit mode. Translation of numbers
                    118:         * mode/applications mode is done in input-keys.c.
1.1       nicm      119:         */
1.8       nicm      120:        { 0,            "\033Oo",       KEYC_KP_SLASH,          TTYKEY_RAW },
                    121:        { 0,            "\033Oj",       KEYC_KP_STAR,           TTYKEY_RAW },
                    122:        { 0,            "\033Om",       KEYC_KP_MINUS,          TTYKEY_RAW },
                    123:        { 0,            "\033Ow",       KEYC_KP_SEVEN,          TTYKEY_RAW },
                    124:        { 0,            "\033Ox",       KEYC_KP_EIGHT,          TTYKEY_RAW },
                    125:        { 0,            "\033Oy",       KEYC_KP_NINE,           TTYKEY_RAW },
                    126:        { 0,            "\033Ok",       KEYC_KP_PLUS,           TTYKEY_RAW },
                    127:        { 0,            "\033Ot",       KEYC_KP_FOUR,           TTYKEY_RAW },
                    128:        { 0,            "\033Ou",       KEYC_KP_FIVE,           TTYKEY_RAW },
                    129:        { 0,            "\033Ov",       KEYC_KP_SIX,            TTYKEY_RAW },
                    130:        { 0,            "\033Oq",       KEYC_KP_ONE,            TTYKEY_RAW },
                    131:        { 0,            "\033Or",       KEYC_KP_TWO,            TTYKEY_RAW },
                    132:        { 0,            "\033Os",       KEYC_KP_THREE,          TTYKEY_RAW },
                    133:        { 0,            "\033OM",       KEYC_KP_ENTER,          TTYKEY_RAW },
                    134:        { 0,            "\033Op",       KEYC_KP_ZERO,           TTYKEY_RAW },
                    135:        { 0,            "\033On",       KEYC_KP_PERIOD,         TTYKEY_RAW },
1.10      nicm      136:
                    137:        /* Key and modifier capabilities. */
                    138:        { TTYC_KDC2,    NULL,           KEYC_DC|KEYC_SHIFT,     0 },
                    139:        { TTYC_KDC3,    NULL,           KEYC_DC|KEYC_ESCAPE,    0 },
                    140:        { TTYC_KDC4,    NULL,           KEYC_DC|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    141:        { TTYC_KDC5,    NULL,           KEYC_DC|KEYC_CTRL,      0 },
                    142:        { TTYC_KDC6,    NULL,           KEYC_DC|KEYC_SHIFT|KEYC_CTRL, 0 },
                    143:        { TTYC_KDC7,    NULL,           KEYC_DC|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    144:        { TTYC_KDN2,    NULL,           KEYC_DOWN|KEYC_SHIFT,   0 },
                    145:        { TTYC_KDN3,    NULL,           KEYC_DOWN|KEYC_ESCAPE,  0 },
                    146:        { TTYC_KDN4,    NULL,           KEYC_DOWN|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    147:        { TTYC_KDN5,    NULL,           KEYC_DOWN|KEYC_CTRL,    0 },
                    148:        { TTYC_KDN6,    NULL,           KEYC_DOWN|KEYC_SHIFT|KEYC_CTRL, 0 },
                    149:        { TTYC_KDN7,    NULL,           KEYC_DOWN|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    150:        { TTYC_KEND2,   NULL,           KEYC_END|KEYC_SHIFT,    0 },
                    151:        { TTYC_KEND3,   NULL,           KEYC_END|KEYC_ESCAPE,   0 },
                    152:        { TTYC_KEND4,   NULL,           KEYC_END|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    153:        { TTYC_KEND5,   NULL,           KEYC_END|KEYC_CTRL,     0 },
                    154:        { TTYC_KEND6,   NULL,           KEYC_END|KEYC_SHIFT|KEYC_CTRL, 0 },
                    155:        { TTYC_KEND7,   NULL,           KEYC_END|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    156:        { TTYC_KHOM2,   NULL,           KEYC_HOME|KEYC_SHIFT,   0 },
                    157:        { TTYC_KHOM3,   NULL,           KEYC_HOME|KEYC_ESCAPE,  0 },
                    158:        { TTYC_KHOM4,   NULL,           KEYC_HOME|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    159:        { TTYC_KHOM5,   NULL,           KEYC_HOME|KEYC_CTRL,    0 },
                    160:        { TTYC_KHOM6,   NULL,           KEYC_HOME|KEYC_SHIFT|KEYC_CTRL, 0 },
                    161:        { TTYC_KHOM7,   NULL,           KEYC_HOME|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    162:        { TTYC_KIC2,    NULL,           KEYC_IC|KEYC_SHIFT,     0 },
                    163:        { TTYC_KIC3,    NULL,           KEYC_IC|KEYC_ESCAPE,    0 },
                    164:        { TTYC_KIC4,    NULL,           KEYC_IC|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    165:        { TTYC_KIC5,    NULL,           KEYC_IC|KEYC_CTRL,      0 },
                    166:        { TTYC_KIC6,    NULL,           KEYC_IC|KEYC_SHIFT|KEYC_CTRL, 0 },
                    167:        { TTYC_KIC7,    NULL,           KEYC_IC|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    168:        { TTYC_KLFT2,   NULL,           KEYC_LEFT|KEYC_SHIFT,   0 },
                    169:        { TTYC_KLFT3,   NULL,           KEYC_LEFT|KEYC_ESCAPE,  0 },
                    170:        { TTYC_KLFT4,   NULL,           KEYC_LEFT|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    171:        { TTYC_KLFT5,   NULL,           KEYC_LEFT|KEYC_CTRL,    0 },
                    172:        { TTYC_KLFT6,   NULL,           KEYC_LEFT|KEYC_SHIFT|KEYC_CTRL, 0 },
                    173:        { TTYC_KLFT7,   NULL,           KEYC_LEFT|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    174:        { TTYC_KNXT2,   NULL,           KEYC_NPAGE|KEYC_SHIFT,  0 },
                    175:        { TTYC_KNXT3,   NULL,           KEYC_NPAGE|KEYC_ESCAPE, 0 },
                    176:        { TTYC_KNXT4,   NULL,           KEYC_NPAGE|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    177:        { TTYC_KNXT5,   NULL,           KEYC_NPAGE|KEYC_CTRL,   0 },
                    178:        { TTYC_KNXT6,   NULL,           KEYC_NPAGE|KEYC_SHIFT|KEYC_CTRL, 0 },
                    179:        { TTYC_KNXT7,   NULL,           KEYC_NPAGE|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    180:        { TTYC_KPRV2,   NULL,           KEYC_PPAGE|KEYC_SHIFT,  0 },
                    181:        { TTYC_KPRV3,   NULL,           KEYC_PPAGE|KEYC_ESCAPE, 0 },
                    182:        { TTYC_KPRV4,   NULL,           KEYC_PPAGE|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    183:        { TTYC_KPRV5,   NULL,           KEYC_PPAGE|KEYC_CTRL,   0 },
                    184:        { TTYC_KPRV6,   NULL,           KEYC_PPAGE|KEYC_SHIFT|KEYC_CTRL, 0 },
                    185:        { TTYC_KPRV7,   NULL,           KEYC_PPAGE|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    186:        { TTYC_KRIT2,   NULL,           KEYC_RIGHT|KEYC_SHIFT,  0 },
                    187:        { TTYC_KRIT3,   NULL,           KEYC_RIGHT|KEYC_ESCAPE, 0 },
                    188:        { TTYC_KRIT4,   NULL,           KEYC_RIGHT|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    189:        { TTYC_KRIT5,   NULL,           KEYC_RIGHT|KEYC_CTRL,   0 },
                    190:        { TTYC_KRIT6,   NULL,           KEYC_RIGHT|KEYC_SHIFT|KEYC_CTRL, 0 },
                    191:        { TTYC_KRIT7,   NULL,           KEYC_RIGHT|KEYC_ESCAPE|KEYC_CTRL, 0 },
                    192:        { TTYC_KUP2,    NULL,           KEYC_UP|KEYC_SHIFT,     0 },
                    193:        { TTYC_KUP3,    NULL,           KEYC_UP|KEYC_ESCAPE,    0 },
                    194:        { TTYC_KUP4,    NULL,           KEYC_UP|KEYC_SHIFT|KEYC_ESCAPE, 0 },
                    195:        { TTYC_KUP5,    NULL,           KEYC_UP|KEYC_CTRL,      0 },
                    196:        { TTYC_KUP6,    NULL,           KEYC_UP|KEYC_SHIFT|KEYC_CTRL, 0 },
                    197:        { TTYC_KUP7,    NULL,           KEYC_UP|KEYC_ESCAPE|KEYC_CTRL, 0 },
1.1       nicm      198: };
                    199:
1.16      nicm      200: void
                    201: tty_keys_add(struct tty *tty, const char *s, int key)
                    202: {
                    203:        size_t  size;
1.1       nicm      204:
1.16      nicm      205:        if (tty_keys_find(tty, s, strlen(s), &size) == NULL) {
                    206:                log_debug("new key 0x%x: %s", key, s);
                    207:                tty_keys_add1(&tty->key_tree, s, key);
                    208:        }
1.1       nicm      209: }
                    210:
1.16      nicm      211: /* Add next node to the tree. */
1.1       nicm      212: void
1.16      nicm      213: tty_keys_add1(struct tty_key **tkp, const char *s, int key)
1.1       nicm      214: {
1.16      nicm      215:        struct tty_key  *tk;
1.1       nicm      216:
1.16      nicm      217:        /* Allocate a tree entry if there isn't one already. */
                    218:        tk = *tkp;
                    219:        if (tk == NULL) {
                    220:                tk = *tkp = xcalloc(1, sizeof *tk);
                    221:                tk->ch = *s;
                    222:                tk->key = KEYC_NONE;
                    223:        }
                    224:
                    225:        /* Find the next entry. */
                    226:        if (*s == tk->ch) {
                    227:                /* Move forward in string. */
                    228:                s++;
                    229:
                    230:                /* If this is the end of the string, no more is necessary. */
                    231:                if (*s == '\0') {
                    232:                        tk->key = key;
                    233:                        return;
                    234:                }
                    235:
                    236:                /* Use the child tree for the next character. */
                    237:                tkp = &tk->next;
                    238:        } else {
                    239:                if (*s < tk->ch)
                    240:                        tkp = &tk->left;
                    241:                else if (*s > tk->ch)
                    242:                        tkp = &tk->right;
                    243:        }
                    244:
                    245:        /* And recurse to add it. */
                    246:        tty_keys_add1(tkp, s, key);
1.1       nicm      247: }
                    248:
1.16      nicm      249: /* Initialise a key tree from the table. */
1.1       nicm      250: void
                    251: tty_keys_init(struct tty *tty)
                    252: {
                    253:        struct tty_key_ent      *tke;
                    254:        u_int                    i;
                    255:        const char              *s;
                    256:
1.16      nicm      257:        tty->key_tree = NULL;
1.1       nicm      258:        for (i = 0; i < nitems(tty_keys); i++) {
                    259:                tke = &tty_keys[i];
                    260:
                    261:                if (tke->flags & TTYKEY_RAW)
                    262:                        s = tke->string;
                    263:                else {
                    264:                        if (!tty_term_has(tty->term, tke->code))
                    265:                                continue;
                    266:                        s = tty_term_string(tty->term, tke->code);
                    267:                }
1.16      nicm      268:                if (s[0] != '\033' || s[1] == '\0')
                    269:                        continue;
1.1       nicm      270:
1.15      nicm      271:                tty_keys_add(tty, s + 1, tke->key);
1.1       nicm      272:        }
                    273: }
                    274:
1.16      nicm      275: /* Free the entire key tree. */
1.1       nicm      276: void
                    277: tty_keys_free(struct tty *tty)
                    278: {
1.16      nicm      279:        tty_keys_free1(tty->key_tree);
                    280: }
1.1       nicm      281:
1.16      nicm      282: /* Free a single key. */
                    283: void
                    284: tty_keys_free1(struct tty_key *tk)
                    285: {
                    286:        if (tk->next != NULL)
                    287:                tty_keys_free1(tk->next);
                    288:        if (tk->left != NULL)
                    289:                tty_keys_free1(tk->left);
                    290:        if (tk->right != NULL)
                    291:                tty_keys_free1(tk->right);
                    292:        xfree(tk);
                    293:
1.1       nicm      294: }
                    295:
1.16      nicm      296: /* Lookup a key in the tree. */
1.1       nicm      297: struct tty_key *
1.16      nicm      298: tty_keys_find(struct tty *tty, const char *buf, size_t len, size_t *size)
1.1       nicm      299: {
1.16      nicm      300:        *size = 0;
                    301:        return (tty_keys_find1(tty->key_tree, buf, len, size));
                    302: }
1.1       nicm      303:
1.16      nicm      304: /* Find the next node. */
                    305: struct tty_key *
                    306: tty_keys_find1(struct tty_key *tk, const char *buf, size_t len, size_t *size)
                    307: {
                    308:        /* If the node is NULL, this is the end of the tree. No match. */
                    309:        if (tk == NULL)
1.1       nicm      310:                return (NULL);
                    311:
1.16      nicm      312:        /* Pick the next in the sequence. */
                    313:        if (tk->ch == *buf) {
                    314:                /* Move forward in the string. */
                    315:                buf++; len--;
                    316:                (*size)++;
1.1       nicm      317:
1.16      nicm      318:                /* At the end of the string, return the current node. */
                    319:                if (len == 0)
                    320:                        return (tk);
1.1       nicm      321:
1.16      nicm      322:                /* Move into the next tree for the following character. */
                    323:                tk = tk->next;
                    324:        } else {
                    325:                if (*buf < tk->ch)
                    326:                        tk = tk->left;
                    327:                else if (*buf > tk->ch)
                    328:                        tk = tk->right;
1.1       nicm      329:        }
                    330:
1.16      nicm      331:        /* Move to the next in the tree. */
                    332:        return (tty_keys_find1(tk, buf, len, size));
1.1       nicm      333: }
                    334:
1.16      nicm      335: /*
                    336:  * Process at least one key in the buffer and invoke tty->key_callback. Return
                    337:  * 1 if there are no further keys, or 0 if there is more in the buffer.
                    338:  */
1.1       nicm      339: int
1.14      nicm      340: tty_keys_next(struct tty *tty)
1.1       nicm      341: {
1.14      nicm      342:        struct tty_key          *tk;
                    343:        struct timeval           tv;
                    344:        struct mouse_event       mouse;
1.18      nicm      345:        const char              *buf;
1.14      nicm      346:        size_t                   len, size;
                    347:        cc_t                     bspace;
                    348:        int                      key;
1.1       nicm      349:
1.13      nicm      350:        buf = EVBUFFER_DATA(tty->event->input);
                    351:        len = EVBUFFER_LENGTH(tty->event->input);
1.1       nicm      352:        if (len == 0)
1.14      nicm      353:                return (0);
1.1       nicm      354:        log_debug("keys are %zu (%.*s)", len, (int) len, buf);
                    355:
                    356:        /* If a normal key, return it. */
                    357:        if (*buf != '\033') {
1.19      nicm      358:                key = (u_char) *buf;
1.16      nicm      359:                evbuffer_drain(tty->event->input, 1);
1.3       nicm      360:
                    361:                /*
                    362:                 * Check for backspace key using termios VERASE - the terminfo
                    363:                 * kbs entry is extremely unreliable, so cannot be safely
                    364:                 * used. termios should have a better idea.
                    365:                 */
                    366:                bspace = tty->tio.c_cc[VERASE];
1.14      nicm      367:                if (bspace != _POSIX_VDISABLE && key == bspace)
                    368:                        key = KEYC_BSPACE;
1.16      nicm      369:                goto handle_key;
1.1       nicm      370:        }
                    371:
                    372:        /* Look for matching key string and return if found. */
                    373:        tk = tty_keys_find(tty, buf + 1, len - 1, &size);
                    374:        if (tk != NULL) {
1.14      nicm      375:                key = tk->key;
1.16      nicm      376:                goto found_key;
1.1       nicm      377:        }
                    378:
                    379:        /* Not found. Is this a mouse key press? */
1.14      nicm      380:        key = tty_keys_mouse(buf, len, &size, &mouse);
                    381:        if (key != KEYC_NONE) {
1.13      nicm      382:                evbuffer_drain(tty->event->input, size);
1.16      nicm      383:                goto handle_key;
1.11      nicm      384:        }
                    385:
                    386:        /* Not found. Try to parse a key with an xterm-style modifier. */
1.14      nicm      387:        key = xterm_keys_find(buf, len, &size);
                    388:        if (key != KEYC_NONE) {
1.13      nicm      389:                evbuffer_drain(tty->event->input, size);
1.16      nicm      390:                goto handle_key;
1.1       nicm      391:        }
                    392:
                    393:        /* Skip the escape. */
                    394:        buf++;
                    395:        len--;
                    396:
                    397:        /* Is there a normal key following? */
                    398:        if (len != 0 && *buf != '\033') {
1.16      nicm      399:                key = *buf | KEYC_ESCAPE;
                    400:                evbuffer_drain(tty->event->input, 2);
                    401:                goto handle_key;
1.1       nicm      402:        }
                    403:
                    404:        /* Or a key string? */
                    405:        if (len > 1) {
                    406:                tk = tty_keys_find(tty, buf + 1, len - 1, &size);
                    407:                if (tk != NULL) {
1.14      nicm      408:                        key = tk->key | KEYC_ESCAPE;
1.16      nicm      409:                        size++; /* include escape */
                    410:                        goto found_key;
1.1       nicm      411:                }
                    412:        }
                    413:
1.16      nicm      414:        /* Escape and then nothing useful - fall through. */
                    415:
                    416: partial_key:
1.14      nicm      417:        /*
1.16      nicm      418:         * Escape but no key string. If have already seen an escape, then the
1.14      nicm      419:         * timer must have expired, so give up waiting and send the escape.
                    420:         */
                    421:        if (tty->flags & TTY_ESCAPE) {
                    422:                evbuffer_drain(tty->event->input, 1);
                    423:                key = '\033';
1.16      nicm      424:                goto handle_key;
1.14      nicm      425:        }
                    426:
1.16      nicm      427:        /* Fall through to start the timer. */
                    428:
                    429: start_timer:
1.14      nicm      430:        /* Start the timer and wait for expiry or more data. */
                    431:        tv.tv_sec = 0;
                    432:        tv.tv_usec = ESCAPE_PERIOD * 1000L;
                    433:
                    434:        evtimer_del(&tty->key_timer);
                    435:        evtimer_set(&tty->key_timer, tty_keys_callback, tty);
                    436:        evtimer_add(&tty->key_timer, &tv);
                    437:
                    438:        tty->flags |= TTY_ESCAPE;
                    439:        return (0);
1.1       nicm      440:
1.16      nicm      441: found_key:
                    442:        if (tk->next != NULL) {
                    443:                /* Partial key. Start the timer if not already expired. */
                    444:                if (!(tty->flags & TTY_ESCAPE))
                    445:                        goto start_timer;
                    446:
                    447:                /* Otherwise, if no key, send the escape alone. */
                    448:                if (tk->key == KEYC_NONE)
                    449:                        goto partial_key;
                    450:
                    451:                /* Or fall through to send the partial key found. */
                    452:        }
                    453:        evbuffer_drain(tty->event->input, size + 1);
                    454:
                    455:        goto handle_key;
                    456:
                    457: handle_key:
                    458:        evtimer_del(&tty->key_timer);
                    459:
1.14      nicm      460:        tty->key_callback(key, &mouse, tty->key_data);
1.16      nicm      461:
1.1       nicm      462:        tty->flags &= ~TTY_ESCAPE;
1.14      nicm      463:        return (1);
                    464: }
                    465:
1.16      nicm      466: /* Key timer callback. */
1.14      nicm      467: void
                    468: tty_keys_callback(unused int fd, unused short events, void *data)
                    469: {
                    470:        struct tty      *tty = data;
                    471:
1.16      nicm      472:        if (!(tty->flags & TTY_ESCAPE))
                    473:                return;
                    474:
                    475:        while (tty_keys_next(tty))
                    476:                ;
1.1       nicm      477: }
                    478:
1.16      nicm      479: /* Handle mouse key input. */
1.1       nicm      480: int
1.18      nicm      481: tty_keys_mouse(const char *buf, size_t len, size_t *size, struct mouse_event *m)
1.1       nicm      482: {
                    483:        /*
                    484:         * Mouse sequences are \033[M followed by three characters indicating
                    485:         * buttons, X and Y, all based at 32 with 1,1 top-left.
                    486:         */
                    487:
                    488:        if (len != 6 || memcmp(buf, "\033[M", 3) != 0)
                    489:                return (KEYC_NONE);
                    490:        *size = 6;
1.16      nicm      491:
                    492:        log_debug("mouse input is: %.*s", (int) len, buf);
1.1       nicm      493:
1.5       nicm      494:        m->b = buf[3];
                    495:        m->x = buf[4];
                    496:        m->y = buf[5];
                    497:        if (m->b < 32 || m->x < 33 || m->y < 33)
1.1       nicm      498:                return (KEYC_NONE);
1.5       nicm      499:        m->b -= 32;
                    500:        m->x -= 33;
                    501:        m->y -= 33;
1.1       nicm      502:        return (KEYC_MOUSE);
                    503: }