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

Annotation of src/usr.bin/tmux/key-string.c, Revision 1.48

1.48    ! nicm        1: /* $OpenBSD: key-string.c,v 1.47 2017/06/23 15:36:52 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.34      nicm        4:  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include <string.h>
                     22:
                     23: #include "tmux.h"
                     24:
1.33      nicm       25: static key_code        key_string_search_table(const char *);
                     26: static key_code        key_string_get_modifiers(const char **);
1.1       nicm       27:
1.39      nicm       28: static const struct {
1.14      nicm       29:        const char     *string;
1.28      nicm       30:        key_code        key;
1.1       nicm       31: } key_string_table[] = {
                     32:        /* Function keys. */
                     33:        { "F1",         KEYC_F1 },
                     34:        { "F2",         KEYC_F2 },
                     35:        { "F3",         KEYC_F3 },
                     36:        { "F4",         KEYC_F4 },
                     37:        { "F5",         KEYC_F5 },
                     38:        { "F6",         KEYC_F6 },
                     39:        { "F7",         KEYC_F7 },
                     40:        { "F8",         KEYC_F8 },
                     41:        { "F9",         KEYC_F9 },
                     42:        { "F10",        KEYC_F10 },
                     43:        { "F11",        KEYC_F11 },
                     44:        { "F12",        KEYC_F12 },
                     45:        { "IC",         KEYC_IC },
                     46:        { "DC",         KEYC_DC },
                     47:        { "Home",       KEYC_HOME },
                     48:        { "End",        KEYC_END },
                     49:        { "NPage",      KEYC_NPAGE },
1.21      nicm       50:        { "PageDown",   KEYC_NPAGE },
                     51:        { "PgDn",       KEYC_NPAGE },
1.1       nicm       52:        { "PPage",      KEYC_PPAGE },
1.21      nicm       53:        { "PageUp",     KEYC_PPAGE },
                     54:        { "PgUp",       KEYC_PPAGE },
1.1       nicm       55:        { "Tab",        '\011' },
                     56:        { "BTab",       KEYC_BTAB },
1.7       nicm       57:        { "Space",      ' ' },
1.5       nicm       58:        { "BSpace",     KEYC_BSPACE },
1.6       nicm       59:        { "Enter",      '\r' },
                     60:        { "Escape",     '\033' },
1.1       nicm       61:
                     62:        /* Arrow keys. */
                     63:        { "Up",         KEYC_UP },
                     64:        { "Down",       KEYC_DOWN },
                     65:        { "Left",       KEYC_LEFT },
                     66:        { "Right",      KEYC_RIGHT },
                     67:
                     68:        /* Numeric keypad. */
1.9       nicm       69:        { "KP/",        KEYC_KP_SLASH },
                     70:        { "KP*",        KEYC_KP_STAR },
                     71:        { "KP-",        KEYC_KP_MINUS },
                     72:        { "KP7",        KEYC_KP_SEVEN },
                     73:        { "KP8",        KEYC_KP_EIGHT },
                     74:        { "KP9",        KEYC_KP_NINE },
                     75:        { "KP+",        KEYC_KP_PLUS },
                     76:        { "KP4",        KEYC_KP_FOUR },
                     77:        { "KP5",        KEYC_KP_FIVE },
                     78:        { "KP6",        KEYC_KP_SIX },
                     79:        { "KP1",        KEYC_KP_ONE },
                     80:        { "KP2",        KEYC_KP_TWO },
                     81:        { "KP3",        KEYC_KP_THREE },
                     82:        { "KPEnter",    KEYC_KP_ENTER },
                     83:        { "KP0",        KEYC_KP_ZERO },
                     84:        { "KP.",        KEYC_KP_PERIOD },
1.26      nicm       85:
                     86:        /* Mouse keys. */
                     87:        KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1),
                     88:        KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2),
                     89:        KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3),
                     90:        KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1),
                     91:        KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2),
                     92:        KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3),
                     93:        KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1),
                     94:        KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2),
                     95:        KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3),
1.36      nicm       96:        KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1),
                     97:        KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
                     98:        KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
1.26      nicm       99:        KEYC_MOUSE_STRING(WHEELUP, WheelUp),
                    100:        KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
1.40      nicm      101:        KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
                    102:        KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
                    103:        KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
                    104:        KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
                    105:        KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
                    106:        KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
1.1       nicm      107: };
                    108:
1.10      nicm      109: /* Find key string in table. */
1.33      nicm      110: static key_code
1.1       nicm      111: key_string_search_table(const char *string)
                    112: {
1.47      nicm      113:        u_int   i, user;
1.1       nicm      114:
                    115:        for (i = 0; i < nitems(key_string_table); i++) {
                    116:                if (strcasecmp(string, key_string_table[i].string) == 0)
                    117:                        return (key_string_table[i].key);
                    118:        }
1.47      nicm      119:
                    120:        if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
                    121:                return (KEYC_USER + user);
                    122:
1.33      nicm      123:        return (KEYC_UNKNOWN);
1.1       nicm      124: }
                    125:
1.14      nicm      126: /* Find modifiers. */
1.33      nicm      127: static key_code
1.14      nicm      128: key_string_get_modifiers(const char **string)
                    129: {
1.28      nicm      130:        key_code        modifiers;
1.14      nicm      131:
                    132:        modifiers = 0;
                    133:        while (((*string)[0] != '\0') && (*string)[1] == '-') {
                    134:                switch ((*string)[0]) {
                    135:                case 'C':
                    136:                case 'c':
                    137:                        modifiers |= KEYC_CTRL;
                    138:                        break;
                    139:                case 'M':
                    140:                case 'm':
                    141:                        modifiers |= KEYC_ESCAPE;
                    142:                        break;
                    143:                case 'S':
                    144:                case 's':
                    145:                        modifiers |= KEYC_SHIFT;
                    146:                        break;
1.41      nicm      147:                default:
                    148:                        *string = NULL;
1.42      nicm      149:                        return (0);
1.14      nicm      150:                }
                    151:                *string += 2;
                    152:        }
                    153:        return (modifiers);
                    154: }
                    155:
                    156: /* Lookup a string and convert to a key value. */
1.28      nicm      157: key_code
1.1       nicm      158: key_string_lookup_string(const char *string)
                    159: {
1.24      nicm      160:        static const char       *other = "!#()+,-.0123456789:;<=>?'\r\t";
1.28      nicm      161:        key_code                 key;
1.38      nicm      162:        u_int                    u;
1.28      nicm      163:        key_code                 modifiers;
1.29      nicm      164:        struct utf8_data         ud;
1.28      nicm      165:        u_int                    i;
1.32      nicm      166:        enum utf8_state          more;
1.35      nicm      167:        wchar_t                  wc;
1.23      nicm      168:
1.48    ! nicm      169:        /* Is this no key or any key? */
1.33      nicm      170:        if (strcasecmp(string, "None") == 0)
                    171:                return (KEYC_NONE);
1.48    ! nicm      172:        if (strcasecmp(string, "Any") == 0)
        !           173:                return (KEYC_ANY);
1.33      nicm      174:
1.23      nicm      175:        /* Is this a hexadecimal value? */
                    176:        if (string[0] == '0' && string[1] == 'x') {
1.38      nicm      177:                if (sscanf(string + 2, "%x", &u) != 1)
                    178:                        return (KEYC_UNKNOWN);
                    179:                if (u > 0x1fffff)
1.33      nicm      180:                        return (KEYC_UNKNOWN);
1.23      nicm      181:                return (u);
                    182:        }
1.1       nicm      183:
1.14      nicm      184:        /* Check for modifiers. */
                    185:        modifiers = 0;
                    186:        if (string[0] == '^' && string[1] != '\0') {
                    187:                modifiers |= KEYC_CTRL;
                    188:                string++;
                    189:        }
                    190:        modifiers |= key_string_get_modifiers(&string);
1.41      nicm      191:        if (string == NULL || string[0] == '\0')
1.33      nicm      192:                return (KEYC_UNKNOWN);
1.1       nicm      193:
1.14      nicm      194:        /* Is this a standard ASCII key? */
1.28      nicm      195:        if (string[1] == '\0' && (u_char)string[0] <= 127) {
                    196:                key = (u_char)string[0];
                    197:                if (key < 32 || key == 127)
1.33      nicm      198:                        return (KEYC_UNKNOWN);
1.17      nicm      199:        } else {
1.28      nicm      200:                /* Try as a UTF-8 key. */
1.32      nicm      201:                if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
1.29      nicm      202:                        if (strlen(string) != ud.size)
1.33      nicm      203:                                return (KEYC_UNKNOWN);
1.29      nicm      204:                        for (i = 1; i < ud.size; i++)
1.30      nicm      205:                                more = utf8_append(&ud, (u_char)string[i]);
1.32      nicm      206:                        if (more != UTF8_DONE)
1.33      nicm      207:                                return (KEYC_UNKNOWN);
1.35      nicm      208:                        if (utf8_combine(&ud, &wc) != UTF8_DONE)
                    209:                                return (KEYC_UNKNOWN);
                    210:                        return (wc | modifiers);
1.28      nicm      211:                }
                    212:
1.17      nicm      213:                /* Otherwise look the key up in the table. */
                    214:                key = key_string_search_table(string);
1.33      nicm      215:                if (key == KEYC_UNKNOWN)
                    216:                        return (KEYC_UNKNOWN);
1.17      nicm      217:        }
1.12      nicm      218:
1.17      nicm      219:        /* Convert the standard control keys. */
1.24      nicm      220:        if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
1.17      nicm      221:                if (key >= 97 && key <= 122)
                    222:                        key -= 96;
                    223:                else if (key >= 64 && key <= 95)
                    224:                        key -= 64;
                    225:                else if (key == 32)
                    226:                        key = 0;
                    227:                else if (key == 63)
                    228:                        key = KEYC_BSPACE;
                    229:                else
1.33      nicm      230:                        return (KEYC_UNKNOWN);
1.17      nicm      231:                modifiers &= ~KEYC_CTRL;
1.13      nicm      232:        }
                    233:
1.14      nicm      234:        return (key | modifiers);
1.1       nicm      235: }
                    236:
1.10      nicm      237: /* Convert a key code into string format, with prefix if necessary. */
1.1       nicm      238: const char *
1.28      nicm      239: key_string_lookup_key(key_code key)
1.1       nicm      240: {
1.45      nicm      241:        static char             out[32];
1.28      nicm      242:        char                    tmp[8];
                    243:        u_int                   i;
1.29      nicm      244:        struct utf8_data        ud;
1.37      nicm      245:        size_t                  off;
1.14      nicm      246:
                    247:        *out = '\0';
1.22      nicm      248:
                    249:        /* Handle no key. */
                    250:        if (key == KEYC_NONE)
1.33      nicm      251:                return ("None");
                    252:
                    253:        /* Handle special keys. */
                    254:        if (key == KEYC_UNKNOWN)
                    255:                return ("Unknown");
1.48    ! nicm      256:        if (key == KEYC_ANY)
        !           257:                return ("Any");
1.43      nicm      258:        if (key == KEYC_FOCUS_IN)
                    259:                return ("FocusIn");
                    260:        if (key == KEYC_FOCUS_OUT)
                    261:                return ("FocusOut");
1.46      nicm      262:        if (key == KEYC_PASTE_START)
                    263:                return ("PasteStart");
                    264:        if (key == KEYC_PASTE_END)
                    265:                return ("PasteEnd");
1.26      nicm      266:        if (key == KEYC_MOUSE)
1.33      nicm      267:                return ("Mouse");
1.43      nicm      268:        if (key == KEYC_DRAGGING)
                    269:                return ("Dragging");
1.44      nicm      270:        if (key == KEYC_MOUSEMOVE_PANE)
                    271:                return ("MouseMovePane");
                    272:        if (key == KEYC_MOUSEMOVE_STATUS)
                    273:                return ("MouseMoveStatus");
                    274:        if (key == KEYC_MOUSEMOVE_BORDER)
                    275:                return ("MouseMoveBorder");
1.47      nicm      276:        if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
                    277:                snprintf(out, sizeof out, "User%u", (u_int)(key - KEYC_USER));
                    278:                return (out);
                    279:        }
1.14      nicm      280:
1.18      nicm      281:        /*
                    282:         * Special case: display C-@ as C-Space. Could do this below in
                    283:         * the (key >= 0 && key <= 32), but this way we let it be found
                    284:         * in key_string_table, for the unlikely chance that we might
                    285:         * change its name.
                    286:         */
                    287:        if ((key & KEYC_MASK_KEY) == 0)
                    288:            key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
                    289:
1.14      nicm      290:        /* Fill in the modifiers. */
                    291:        if (key & KEYC_CTRL)
                    292:                strlcat(out, "C-", sizeof out);
                    293:        if (key & KEYC_ESCAPE)
                    294:                strlcat(out, "M-", sizeof out);
                    295:        if (key & KEYC_SHIFT)
                    296:                strlcat(out, "S-", sizeof out);
1.18      nicm      297:        key &= KEYC_MASK_KEY;
1.1       nicm      298:
1.14      nicm      299:        /* Try the key against the string table. */
1.6       nicm      300:        for (i = 0; i < nitems(key_string_table); i++) {
                    301:                if (key == key_string_table[i].key)
1.14      nicm      302:                        break;
                    303:        }
                    304:        if (i != nitems(key_string_table)) {
                    305:                strlcat(out, key_string_table[i].string, sizeof out);
                    306:                return (out);
1.6       nicm      307:        }
                    308:
1.28      nicm      309:        /* Is this a UTF-8 key? */
                    310:        if (key > 127 && key < KEYC_BASE) {
1.32      nicm      311:                if (utf8_split(key, &ud) == UTF8_DONE) {
1.37      nicm      312:                        off = strlen(out);
                    313:                        memcpy(out + off, ud.data, ud.size);
                    314:                        out[off + ud.size] = '\0';
1.28      nicm      315:                        return (out);
                    316:                }
                    317:        }
                    318:
1.14      nicm      319:        /* Invalid keys are errors. */
1.27      nicm      320:        if (key == 127 || key > 255) {
1.33      nicm      321:                snprintf(out, sizeof out, "Invalid#%llx", key);
1.27      nicm      322:                return (out);
                    323:        }
1.1       nicm      324:
1.14      nicm      325:        /* Check for standard or control key. */
1.28      nicm      326:        if (key <= 32) {
1.1       nicm      327:                if (key == 0 || key > 26)
1.28      nicm      328:                        xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
1.1       nicm      329:                else
1.28      nicm      330:                        xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
1.14      nicm      331:        } else if (key >= 32 && key <= 126) {
                    332:                tmp[0] = key;
                    333:                tmp[1] = '\0';
1.20      nicm      334:        } else if (key >= 128)
1.28      nicm      335:                xsnprintf(tmp, sizeof tmp, "\\%llo", key);
1.20      nicm      336:
1.14      nicm      337:        strlcat(out, tmp, sizeof out);
                    338:        return (out);
1.1       nicm      339: }