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

1.52    ! nicm        1: /* $OpenBSD: key-string.c,v 1.51 2019/07/09 14:03:12 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 },
1.50      nicm       46:        { "Insert",     KEYC_IC },
1.1       nicm       47:        { "DC",         KEYC_DC },
1.50      nicm       48:        { "Delete",     KEYC_DC },
1.1       nicm       49:        { "Home",       KEYC_HOME },
                     50:        { "End",        KEYC_END },
                     51:        { "NPage",      KEYC_NPAGE },
1.21      nicm       52:        { "PageDown",   KEYC_NPAGE },
                     53:        { "PgDn",       KEYC_NPAGE },
1.1       nicm       54:        { "PPage",      KEYC_PPAGE },
1.21      nicm       55:        { "PageUp",     KEYC_PPAGE },
                     56:        { "PgUp",       KEYC_PPAGE },
1.1       nicm       57:        { "Tab",        '\011' },
                     58:        { "BTab",       KEYC_BTAB },
1.7       nicm       59:        { "Space",      ' ' },
1.5       nicm       60:        { "BSpace",     KEYC_BSPACE },
1.6       nicm       61:        { "Enter",      '\r' },
                     62:        { "Escape",     '\033' },
1.1       nicm       63:
                     64:        /* Arrow keys. */
                     65:        { "Up",         KEYC_UP },
                     66:        { "Down",       KEYC_DOWN },
                     67:        { "Left",       KEYC_LEFT },
                     68:        { "Right",      KEYC_RIGHT },
                     69:
                     70:        /* Numeric keypad. */
1.9       nicm       71:        { "KP/",        KEYC_KP_SLASH },
                     72:        { "KP*",        KEYC_KP_STAR },
                     73:        { "KP-",        KEYC_KP_MINUS },
                     74:        { "KP7",        KEYC_KP_SEVEN },
                     75:        { "KP8",        KEYC_KP_EIGHT },
                     76:        { "KP9",        KEYC_KP_NINE },
                     77:        { "KP+",        KEYC_KP_PLUS },
                     78:        { "KP4",        KEYC_KP_FOUR },
                     79:        { "KP5",        KEYC_KP_FIVE },
                     80:        { "KP6",        KEYC_KP_SIX },
                     81:        { "KP1",        KEYC_KP_ONE },
                     82:        { "KP2",        KEYC_KP_TWO },
                     83:        { "KP3",        KEYC_KP_THREE },
                     84:        { "KPEnter",    KEYC_KP_ENTER },
                     85:        { "KP0",        KEYC_KP_ZERO },
                     86:        { "KP.",        KEYC_KP_PERIOD },
1.26      nicm       87:
                     88:        /* Mouse keys. */
                     89:        KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1),
                     90:        KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2),
                     91:        KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3),
                     92:        KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1),
                     93:        KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2),
                     94:        KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3),
                     95:        KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1),
                     96:        KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2),
                     97:        KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3),
1.36      nicm       98:        KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1),
                     99:        KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
                    100:        KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
1.26      nicm      101:        KEYC_MOUSE_STRING(WHEELUP, WheelUp),
                    102:        KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
1.40      nicm      103:        KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
                    104:        KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
                    105:        KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
                    106:        KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
                    107:        KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
                    108:        KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
1.1       nicm      109: };
                    110:
1.10      nicm      111: /* Find key string in table. */
1.33      nicm      112: static key_code
1.1       nicm      113: key_string_search_table(const char *string)
                    114: {
1.47      nicm      115:        u_int   i, user;
1.1       nicm      116:
                    117:        for (i = 0; i < nitems(key_string_table); i++) {
                    118:                if (strcasecmp(string, key_string_table[i].string) == 0)
                    119:                        return (key_string_table[i].key);
                    120:        }
1.47      nicm      121:
                    122:        if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
                    123:                return (KEYC_USER + user);
                    124:
1.33      nicm      125:        return (KEYC_UNKNOWN);
1.1       nicm      126: }
                    127:
1.14      nicm      128: /* Find modifiers. */
1.33      nicm      129: static key_code
1.14      nicm      130: key_string_get_modifiers(const char **string)
                    131: {
1.28      nicm      132:        key_code        modifiers;
1.14      nicm      133:
                    134:        modifiers = 0;
                    135:        while (((*string)[0] != '\0') && (*string)[1] == '-') {
                    136:                switch ((*string)[0]) {
                    137:                case 'C':
                    138:                case 'c':
                    139:                        modifiers |= KEYC_CTRL;
                    140:                        break;
                    141:                case 'M':
                    142:                case 'm':
                    143:                        modifiers |= KEYC_ESCAPE;
                    144:                        break;
                    145:                case 'S':
                    146:                case 's':
                    147:                        modifiers |= KEYC_SHIFT;
                    148:                        break;
1.41      nicm      149:                default:
                    150:                        *string = NULL;
1.42      nicm      151:                        return (0);
1.14      nicm      152:                }
                    153:                *string += 2;
                    154:        }
                    155:        return (modifiers);
                    156: }
                    157:
                    158: /* Lookup a string and convert to a key value. */
1.28      nicm      159: key_code
1.1       nicm      160: key_string_lookup_string(const char *string)
                    161: {
1.52    ! nicm      162:        static const char       *other = "!#()+,-.0123456789:;<=>'\r\t";
1.28      nicm      163:        key_code                 key;
1.38      nicm      164:        u_int                    u;
1.28      nicm      165:        key_code                 modifiers;
1.29      nicm      166:        struct utf8_data         ud;
1.28      nicm      167:        u_int                    i;
1.32      nicm      168:        enum utf8_state          more;
1.35      nicm      169:        wchar_t                  wc;
1.23      nicm      170:
1.48      nicm      171:        /* Is this no key or any key? */
1.33      nicm      172:        if (strcasecmp(string, "None") == 0)
                    173:                return (KEYC_NONE);
1.48      nicm      174:        if (strcasecmp(string, "Any") == 0)
                    175:                return (KEYC_ANY);
1.33      nicm      176:
1.23      nicm      177:        /* Is this a hexadecimal value? */
                    178:        if (string[0] == '0' && string[1] == 'x') {
1.38      nicm      179:                if (sscanf(string + 2, "%x", &u) != 1)
                    180:                        return (KEYC_UNKNOWN);
                    181:                if (u > 0x1fffff)
1.33      nicm      182:                        return (KEYC_UNKNOWN);
1.23      nicm      183:                return (u);
                    184:        }
1.1       nicm      185:
1.14      nicm      186:        /* Check for modifiers. */
                    187:        modifiers = 0;
                    188:        if (string[0] == '^' && string[1] != '\0') {
                    189:                modifiers |= KEYC_CTRL;
                    190:                string++;
                    191:        }
                    192:        modifiers |= key_string_get_modifiers(&string);
1.41      nicm      193:        if (string == NULL || string[0] == '\0')
1.33      nicm      194:                return (KEYC_UNKNOWN);
1.1       nicm      195:
1.14      nicm      196:        /* Is this a standard ASCII key? */
1.28      nicm      197:        if (string[1] == '\0' && (u_char)string[0] <= 127) {
                    198:                key = (u_char)string[0];
1.52    ! nicm      199:                if (key < 32)
1.33      nicm      200:                        return (KEYC_UNKNOWN);
1.17      nicm      201:        } else {
1.28      nicm      202:                /* Try as a UTF-8 key. */
1.32      nicm      203:                if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
1.29      nicm      204:                        if (strlen(string) != ud.size)
1.33      nicm      205:                                return (KEYC_UNKNOWN);
1.29      nicm      206:                        for (i = 1; i < ud.size; i++)
1.30      nicm      207:                                more = utf8_append(&ud, (u_char)string[i]);
1.32      nicm      208:                        if (more != UTF8_DONE)
1.33      nicm      209:                                return (KEYC_UNKNOWN);
1.35      nicm      210:                        if (utf8_combine(&ud, &wc) != UTF8_DONE)
                    211:                                return (KEYC_UNKNOWN);
                    212:                        return (wc | modifiers);
1.28      nicm      213:                }
                    214:
1.17      nicm      215:                /* Otherwise look the key up in the table. */
                    216:                key = key_string_search_table(string);
1.33      nicm      217:                if (key == KEYC_UNKNOWN)
                    218:                        return (KEYC_UNKNOWN);
1.17      nicm      219:        }
1.12      nicm      220:
1.17      nicm      221:        /* Convert the standard control keys. */
1.24      nicm      222:        if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
1.17      nicm      223:                if (key >= 97 && key <= 122)
                    224:                        key -= 96;
                    225:                else if (key >= 64 && key <= 95)
                    226:                        key -= 64;
                    227:                else if (key == 32)
                    228:                        key = 0;
1.52    ! nicm      229:                else if (key == '?')
        !           230:                        key = 127;
1.17      nicm      231:                else if (key == 63)
                    232:                        key = KEYC_BSPACE;
                    233:                else
1.33      nicm      234:                        return (KEYC_UNKNOWN);
1.17      nicm      235:                modifiers &= ~KEYC_CTRL;
1.13      nicm      236:        }
                    237:
1.14      nicm      238:        return (key | modifiers);
1.1       nicm      239: }
                    240:
1.10      nicm      241: /* Convert a key code into string format, with prefix if necessary. */
1.1       nicm      242: const char *
1.28      nicm      243: key_string_lookup_key(key_code key)
1.1       nicm      244: {
1.45      nicm      245:        static char             out[32];
1.28      nicm      246:        char                    tmp[8];
                    247:        u_int                   i;
1.29      nicm      248:        struct utf8_data        ud;
1.37      nicm      249:        size_t                  off;
1.14      nicm      250:
                    251:        *out = '\0';
1.22      nicm      252:
                    253:        /* Handle no key. */
                    254:        if (key == KEYC_NONE)
1.33      nicm      255:                return ("None");
                    256:
                    257:        /* Handle special keys. */
                    258:        if (key == KEYC_UNKNOWN)
                    259:                return ("Unknown");
1.48      nicm      260:        if (key == KEYC_ANY)
                    261:                return ("Any");
1.43      nicm      262:        if (key == KEYC_FOCUS_IN)
                    263:                return ("FocusIn");
                    264:        if (key == KEYC_FOCUS_OUT)
                    265:                return ("FocusOut");
1.46      nicm      266:        if (key == KEYC_PASTE_START)
                    267:                return ("PasteStart");
                    268:        if (key == KEYC_PASTE_END)
                    269:                return ("PasteEnd");
1.26      nicm      270:        if (key == KEYC_MOUSE)
1.33      nicm      271:                return ("Mouse");
1.43      nicm      272:        if (key == KEYC_DRAGGING)
                    273:                return ("Dragging");
1.44      nicm      274:        if (key == KEYC_MOUSEMOVE_PANE)
                    275:                return ("MouseMovePane");
                    276:        if (key == KEYC_MOUSEMOVE_STATUS)
                    277:                return ("MouseMoveStatus");
1.49      nicm      278:        if (key == KEYC_MOUSEMOVE_STATUS_LEFT)
                    279:                return ("MouseMoveStatusLeft");
                    280:        if (key == KEYC_MOUSEMOVE_STATUS_RIGHT)
                    281:                return ("MouseMoveStatusRight");
1.44      nicm      282:        if (key == KEYC_MOUSEMOVE_BORDER)
                    283:                return ("MouseMoveBorder");
1.47      nicm      284:        if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
                    285:                snprintf(out, sizeof out, "User%u", (u_int)(key - KEYC_USER));
1.51      nicm      286:                return (out);
                    287:        }
                    288:
                    289:        /* Literal keys are themselves. */
                    290:        if (key & KEYC_LITERAL) {
                    291:                snprintf(out, sizeof out, "%c", (int)(key & 0xff));
1.47      nicm      292:                return (out);
                    293:        }
1.14      nicm      294:
1.18      nicm      295:        /*
                    296:         * Special case: display C-@ as C-Space. Could do this below in
                    297:         * the (key >= 0 && key <= 32), but this way we let it be found
                    298:         * in key_string_table, for the unlikely chance that we might
                    299:         * change its name.
                    300:         */
                    301:        if ((key & KEYC_MASK_KEY) == 0)
                    302:            key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
                    303:
1.14      nicm      304:        /* Fill in the modifiers. */
                    305:        if (key & KEYC_CTRL)
                    306:                strlcat(out, "C-", sizeof out);
                    307:        if (key & KEYC_ESCAPE)
                    308:                strlcat(out, "M-", sizeof out);
                    309:        if (key & KEYC_SHIFT)
                    310:                strlcat(out, "S-", sizeof out);
1.18      nicm      311:        key &= KEYC_MASK_KEY;
1.1       nicm      312:
1.14      nicm      313:        /* Try the key against the string table. */
1.6       nicm      314:        for (i = 0; i < nitems(key_string_table); i++) {
                    315:                if (key == key_string_table[i].key)
1.14      nicm      316:                        break;
                    317:        }
                    318:        if (i != nitems(key_string_table)) {
                    319:                strlcat(out, key_string_table[i].string, sizeof out);
                    320:                return (out);
1.6       nicm      321:        }
                    322:
1.28      nicm      323:        /* Is this a UTF-8 key? */
                    324:        if (key > 127 && key < KEYC_BASE) {
1.32      nicm      325:                if (utf8_split(key, &ud) == UTF8_DONE) {
1.37      nicm      326:                        off = strlen(out);
                    327:                        memcpy(out + off, ud.data, ud.size);
                    328:                        out[off + ud.size] = '\0';
1.28      nicm      329:                        return (out);
                    330:                }
                    331:        }
                    332:
1.14      nicm      333:        /* Invalid keys are errors. */
1.52    ! nicm      334:        if (key > 255) {
1.33      nicm      335:                snprintf(out, sizeof out, "Invalid#%llx", key);
1.27      nicm      336:                return (out);
                    337:        }
1.1       nicm      338:
1.14      nicm      339:        /* Check for standard or control key. */
1.28      nicm      340:        if (key <= 32) {
1.1       nicm      341:                if (key == 0 || key > 26)
1.28      nicm      342:                        xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
1.1       nicm      343:                else
1.28      nicm      344:                        xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
1.14      nicm      345:        } else if (key >= 32 && key <= 126) {
                    346:                tmp[0] = key;
                    347:                tmp[1] = '\0';
1.52    ! nicm      348:        } else if (key == 127)
        !           349:                xsnprintf(tmp, sizeof tmp, "C-?");
        !           350:        else if (key >= 128)
1.28      nicm      351:                xsnprintf(tmp, sizeof tmp, "\\%llo", key);
1.20      nicm      352:
1.14      nicm      353:        strlcat(out, tmp, sizeof out);
                    354:        return (out);
1.1       nicm      355: }