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

1.61    ! nicm        1: /* $OpenBSD: key-string.c,v 1.60 2020/05/20 07:11:45 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. */
1.59      nicm       65:        { "Up",         KEYC_UP|KEYC_CURSOR },
                     66:        { "Down",       KEYC_DOWN|KEYC_CURSOR },
                     67:        { "Left",       KEYC_LEFT|KEYC_CURSOR },
                     68:        { "Right",      KEYC_RIGHT|KEYC_CURSOR },
1.1       nicm       69:
                     70:        /* Numeric keypad. */
1.59      nicm       71:        { "KP/",        KEYC_KP_SLASH|KEYC_KEYPAD },
                     72:        { "KP*",        KEYC_KP_STAR|KEYC_KEYPAD },
                     73:        { "KP-",        KEYC_KP_MINUS|KEYC_KEYPAD },
                     74:        { "KP7",        KEYC_KP_SEVEN|KEYC_KEYPAD },
                     75:        { "KP8",        KEYC_KP_EIGHT|KEYC_KEYPAD },
                     76:        { "KP9",        KEYC_KP_NINE|KEYC_KEYPAD },
                     77:        { "KP+",        KEYC_KP_PLUS|KEYC_KEYPAD },
                     78:        { "KP4",        KEYC_KP_FOUR|KEYC_KEYPAD },
                     79:        { "KP5",        KEYC_KP_FIVE|KEYC_KEYPAD },
                     80:        { "KP6",        KEYC_KP_SIX|KEYC_KEYPAD },
                     81:        { "KP1",        KEYC_KP_ONE|KEYC_KEYPAD },
                     82:        { "KP2",        KEYC_KP_TWO|KEYC_KEYPAD },
                     83:        { "KP3",        KEYC_KP_THREE|KEYC_KEYPAD },
                     84:        { "KPEnter",    KEYC_KP_ENTER|KEYC_KEYPAD },
                     85:        { "KP0",        KEYC_KP_ZERO|KEYC_KEYPAD },
                     86:        { "KP.",        KEYC_KP_PERIOD|KEYC_KEYPAD },
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.55      nicm      103:        KEYC_MOUSE_STRING(SECONDCLICK1, SecondClick1),
                    104:        KEYC_MOUSE_STRING(SECONDCLICK2, SecondClick2),
                    105:        KEYC_MOUSE_STRING(SECONDCLICK3, SecondClick3),
1.40      nicm      106:        KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
                    107:        KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
                    108:        KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
                    109:        KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
                    110:        KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
                    111:        KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
1.1       nicm      112: };
                    113:
1.10      nicm      114: /* Find key string in table. */
1.33      nicm      115: static key_code
1.1       nicm      116: key_string_search_table(const char *string)
                    117: {
1.47      nicm      118:        u_int   i, user;
1.1       nicm      119:
                    120:        for (i = 0; i < nitems(key_string_table); i++) {
                    121:                if (strcasecmp(string, key_string_table[i].string) == 0)
                    122:                        return (key_string_table[i].key);
                    123:        }
1.47      nicm      124:
                    125:        if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
                    126:                return (KEYC_USER + user);
                    127:
1.33      nicm      128:        return (KEYC_UNKNOWN);
1.1       nicm      129: }
                    130:
1.14      nicm      131: /* Find modifiers. */
1.33      nicm      132: static key_code
1.14      nicm      133: key_string_get_modifiers(const char **string)
                    134: {
1.28      nicm      135:        key_code        modifiers;
1.14      nicm      136:
                    137:        modifiers = 0;
                    138:        while (((*string)[0] != '\0') && (*string)[1] == '-') {
                    139:                switch ((*string)[0]) {
                    140:                case 'C':
                    141:                case 'c':
                    142:                        modifiers |= KEYC_CTRL;
                    143:                        break;
                    144:                case 'M':
                    145:                case 'm':
1.57      nicm      146:                        modifiers |= KEYC_META;
1.14      nicm      147:                        break;
                    148:                case 'S':
                    149:                case 's':
                    150:                        modifiers |= KEYC_SHIFT;
                    151:                        break;
1.41      nicm      152:                default:
                    153:                        *string = NULL;
1.42      nicm      154:                        return (0);
1.14      nicm      155:                }
                    156:                *string += 2;
                    157:        }
                    158:        return (modifiers);
                    159: }
                    160:
                    161: /* Lookup a string and convert to a key value. */
1.28      nicm      162: key_code
1.1       nicm      163: key_string_lookup_string(const char *string)
                    164: {
1.52      nicm      165:        static const char       *other = "!#()+,-.0123456789:;<=>'\r\t";
1.28      nicm      166:        key_code                 key;
1.38      nicm      167:        u_int                    u;
1.28      nicm      168:        key_code                 modifiers;
1.29      nicm      169:        struct utf8_data         ud;
1.28      nicm      170:        u_int                    i;
1.32      nicm      171:        enum utf8_state          more;
1.61    ! nicm      172:        utf8_char                uc;
1.23      nicm      173:
1.48      nicm      174:        /* Is this no key or any key? */
1.33      nicm      175:        if (strcasecmp(string, "None") == 0)
                    176:                return (KEYC_NONE);
1.48      nicm      177:        if (strcasecmp(string, "Any") == 0)
                    178:                return (KEYC_ANY);
1.33      nicm      179:
1.23      nicm      180:        /* Is this a hexadecimal value? */
                    181:        if (string[0] == '0' && string[1] == 'x') {
1.38      nicm      182:                if (sscanf(string + 2, "%x", &u) != 1)
                    183:                        return (KEYC_UNKNOWN);
                    184:                if (u > 0x1fffff)
1.33      nicm      185:                        return (KEYC_UNKNOWN);
1.23      nicm      186:                return (u);
                    187:        }
1.1       nicm      188:
1.14      nicm      189:        /* Check for modifiers. */
                    190:        modifiers = 0;
                    191:        if (string[0] == '^' && string[1] != '\0') {
                    192:                modifiers |= KEYC_CTRL;
                    193:                string++;
                    194:        }
                    195:        modifiers |= key_string_get_modifiers(&string);
1.41      nicm      196:        if (string == NULL || string[0] == '\0')
1.33      nicm      197:                return (KEYC_UNKNOWN);
1.1       nicm      198:
1.14      nicm      199:        /* Is this a standard ASCII key? */
1.28      nicm      200:        if (string[1] == '\0' && (u_char)string[0] <= 127) {
                    201:                key = (u_char)string[0];
1.52      nicm      202:                if (key < 32)
1.33      nicm      203:                        return (KEYC_UNKNOWN);
1.17      nicm      204:        } else {
1.28      nicm      205:                /* Try as a UTF-8 key. */
1.32      nicm      206:                if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
1.29      nicm      207:                        if (strlen(string) != ud.size)
1.33      nicm      208:                                return (KEYC_UNKNOWN);
1.29      nicm      209:                        for (i = 1; i < ud.size; i++)
1.30      nicm      210:                                more = utf8_append(&ud, (u_char)string[i]);
1.32      nicm      211:                        if (more != UTF8_DONE)
1.33      nicm      212:                                return (KEYC_UNKNOWN);
1.61    ! nicm      213:                        if (utf8_from_data(&ud, &uc) != UTF8_DONE)
1.35      nicm      214:                                return (KEYC_UNKNOWN);
1.61    ! nicm      215:                        return (uc|modifiers);
1.28      nicm      216:                }
                    217:
1.17      nicm      218:                /* Otherwise look the key up in the table. */
                    219:                key = key_string_search_table(string);
1.33      nicm      220:                if (key == KEYC_UNKNOWN)
                    221:                        return (KEYC_UNKNOWN);
1.17      nicm      222:        }
1.12      nicm      223:
1.17      nicm      224:        /* Convert the standard control keys. */
1.24      nicm      225:        if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
1.17      nicm      226:                if (key >= 97 && key <= 122)
                    227:                        key -= 96;
                    228:                else if (key >= 64 && key <= 95)
                    229:                        key -= 64;
                    230:                else if (key == 32)
                    231:                        key = 0;
1.56      nicm      232:                else if (key == 63)
1.52      nicm      233:                        key = 127;
1.17      nicm      234:                else
1.33      nicm      235:                        return (KEYC_UNKNOWN);
1.17      nicm      236:                modifiers &= ~KEYC_CTRL;
1.13      nicm      237:        }
                    238:
1.58      nicm      239:        return (key|modifiers);
1.1       nicm      240: }
                    241:
1.10      nicm      242: /* Convert a key code into string format, with prefix if necessary. */
1.1       nicm      243: const char *
1.58      nicm      244: key_string_lookup_key(key_code key, int with_flags)
1.1       nicm      245: {
1.58      nicm      246:        key_code                 saved = key;
                    247:        static char              out[64];
1.53      nicm      248:        char                     tmp[8];
                    249:        const char              *s;
                    250:        u_int                    i;
                    251:        struct utf8_data         ud;
                    252:        size_t                   off;
1.14      nicm      253:
                    254:        *out = '\0';
1.22      nicm      255:
1.53      nicm      256:        /* Literal keys are themselves. */
                    257:        if (key & KEYC_LITERAL) {
                    258:                snprintf(out, sizeof out, "%c", (int)(key & 0xff));
1.58      nicm      259:                goto out;
1.53      nicm      260:        }
                    261:
1.54      nicm      262:        /* Display C-@ as C-Space. */
1.58      nicm      263:        if ((key & (KEYC_MASK_KEY|KEYC_MASK_MODIFIERS)) == 0)
                    264:                key = ' '|KEYC_CTRL;
1.54      nicm      265:
1.53      nicm      266:        /* Fill in the modifiers. */
                    267:        if (key & KEYC_CTRL)
                    268:                strlcat(out, "C-", sizeof out);
1.57      nicm      269:        if (key & KEYC_META)
1.53      nicm      270:                strlcat(out, "M-", sizeof out);
                    271:        if (key & KEYC_SHIFT)
                    272:                strlcat(out, "S-", sizeof out);
                    273:        key &= KEYC_MASK_KEY;
                    274:
1.22      nicm      275:        /* Handle no key. */
1.58      nicm      276:        if (key == KEYC_NONE) {
                    277:                s = "None";
                    278:                goto append;
                    279:        }
1.33      nicm      280:
                    281:        /* Handle special keys. */
1.53      nicm      282:        if (key == KEYC_UNKNOWN) {
                    283:                s = "Unknown";
                    284:                goto append;
                    285:        }
                    286:        if (key == KEYC_ANY) {
                    287:                s = "Any";
                    288:                goto append;
                    289:        }
                    290:        if (key == KEYC_FOCUS_IN) {
                    291:                s = "FocusIn";
                    292:                goto append;
                    293:        }
                    294:        if (key == KEYC_FOCUS_OUT) {
                    295:                s = "FocusOut";
                    296:                goto append;
                    297:        }
                    298:        if (key == KEYC_PASTE_START) {
                    299:                s = "PasteStart";
                    300:                goto append;
                    301:        }
                    302:        if (key == KEYC_PASTE_END) {
                    303:                s = "PasteEnd";
                    304:                goto append;
                    305:        }
                    306:        if (key == KEYC_MOUSE) {
                    307:                s = "Mouse";
                    308:                goto append;
                    309:        }
                    310:        if (key == KEYC_DRAGGING) {
                    311:                s = "Dragging";
                    312:                goto append;
                    313:        }
                    314:        if (key == KEYC_MOUSEMOVE_PANE) {
                    315:                s = "MouseMovePane";
                    316:                goto append;
                    317:        }
                    318:        if (key == KEYC_MOUSEMOVE_STATUS) {
                    319:                s = "MouseMoveStatus";
                    320:                goto append;
                    321:        }
                    322:        if (key == KEYC_MOUSEMOVE_STATUS_LEFT) {
                    323:                s = "MouseMoveStatusLeft";
                    324:                goto append;
                    325:        }
                    326:        if (key == KEYC_MOUSEMOVE_STATUS_RIGHT) {
                    327:                s = "MouseMoveStatusRight";
                    328:                goto append;
                    329:        }
                    330:        if (key == KEYC_MOUSEMOVE_BORDER) {
                    331:                s = "MouseMoveBorder";
                    332:                goto append;
                    333:        }
1.47      nicm      334:        if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
1.53      nicm      335:                snprintf(tmp, sizeof tmp, "User%u", (u_int)(key - KEYC_USER));
                    336:                strlcat(out, tmp, sizeof out);
1.58      nicm      337:                goto out;
1.47      nicm      338:        }
1.18      nicm      339:
1.14      nicm      340:        /* Try the key against the string table. */
1.6       nicm      341:        for (i = 0; i < nitems(key_string_table); i++) {
1.60      nicm      342:                if (key == (key_string_table[i].key & KEYC_MASK_KEY))
1.14      nicm      343:                        break;
                    344:        }
                    345:        if (i != nitems(key_string_table)) {
                    346:                strlcat(out, key_string_table[i].string, sizeof out);
1.58      nicm      347:                goto out;
1.6       nicm      348:        }
                    349:
1.28      nicm      350:        /* Is this a UTF-8 key? */
                    351:        if (key > 127 && key < KEYC_BASE) {
1.61    ! nicm      352:                utf8_to_data(key, &ud);
        !           353:                off = strlen(out);
        !           354:                memcpy(out + off, ud.data, ud.size);
        !           355:                out[off + ud.size] = '\0';
        !           356:                goto out;
1.28      nicm      357:        }
                    358:
1.14      nicm      359:        /* Invalid keys are errors. */
1.52      nicm      360:        if (key > 255) {
1.60      nicm      361:                snprintf(out, sizeof out, "Invalid#%llx", saved);
1.58      nicm      362:                goto out;
1.27      nicm      363:        }
1.1       nicm      364:
1.14      nicm      365:        /* Check for standard or control key. */
1.28      nicm      366:        if (key <= 32) {
1.1       nicm      367:                if (key == 0 || key > 26)
1.28      nicm      368:                        xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
1.1       nicm      369:                else
1.28      nicm      370:                        xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
1.14      nicm      371:        } else if (key >= 32 && key <= 126) {
                    372:                tmp[0] = key;
                    373:                tmp[1] = '\0';
1.52      nicm      374:        } else if (key == 127)
                    375:                xsnprintf(tmp, sizeof tmp, "C-?");
                    376:        else if (key >= 128)
1.28      nicm      377:                xsnprintf(tmp, sizeof tmp, "\\%llo", key);
1.20      nicm      378:
1.14      nicm      379:        strlcat(out, tmp, sizeof out);
1.58      nicm      380:        goto out;
1.53      nicm      381:
                    382: append:
                    383:        strlcat(out, s, sizeof out);
1.58      nicm      384:
                    385: out:
                    386:        if (with_flags && (saved & KEYC_MASK_FLAGS) != 0) {
                    387:                strlcat(out, "[", sizeof out);
                    388:                if (saved & KEYC_LITERAL)
                    389:                        strlcat(out, "L", sizeof out);
                    390:                if (saved & KEYC_KEYPAD)
                    391:                        strlcat(out, "K", sizeof out);
                    392:                if (saved & KEYC_CURSOR)
                    393:                        strlcat(out, "C", sizeof out);
                    394:                if (saved & KEYC_IMPLIED_META)
                    395:                        strlcat(out, "I", sizeof out);
                    396:                if (saved & KEYC_BUILD_MODIFIERS)
                    397:                        strlcat(out, "B", sizeof out);
                    398:                strlcat(out, "]", sizeof out);
                    399:        }
1.14      nicm      400:        return (out);
1.1       nicm      401: }