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

1.34    ! nicm        1: /* $OpenBSD: key-string.c,v 1.33 2015/12/12 18:19:00 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.19      nicm       28: 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),
                     96:        KEYC_MOUSE_STRING(WHEELUP, WheelUp),
                     97:        KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
1.1       nicm       98: };
                     99:
1.10      nicm      100: /* Find key string in table. */
1.33      nicm      101: static key_code
1.1       nicm      102: key_string_search_table(const char *string)
                    103: {
                    104:        u_int   i;
                    105:
                    106:        for (i = 0; i < nitems(key_string_table); i++) {
                    107:                if (strcasecmp(string, key_string_table[i].string) == 0)
                    108:                        return (key_string_table[i].key);
                    109:        }
1.33      nicm      110:        return (KEYC_UNKNOWN);
1.1       nicm      111: }
                    112:
1.14      nicm      113: /* Find modifiers. */
1.33      nicm      114: static key_code
1.14      nicm      115: key_string_get_modifiers(const char **string)
                    116: {
1.28      nicm      117:        key_code        modifiers;
1.14      nicm      118:
                    119:        modifiers = 0;
                    120:        while (((*string)[0] != '\0') && (*string)[1] == '-') {
                    121:                switch ((*string)[0]) {
                    122:                case 'C':
                    123:                case 'c':
                    124:                        modifiers |= KEYC_CTRL;
                    125:                        break;
                    126:                case 'M':
                    127:                case 'm':
                    128:                        modifiers |= KEYC_ESCAPE;
                    129:                        break;
                    130:                case 'S':
                    131:                case 's':
                    132:                        modifiers |= KEYC_SHIFT;
                    133:                        break;
                    134:                }
                    135:                *string += 2;
                    136:        }
                    137:        return (modifiers);
                    138: }
                    139:
                    140: /* Lookup a string and convert to a key value. */
1.28      nicm      141: key_code
1.1       nicm      142: key_string_lookup_string(const char *string)
                    143: {
1.24      nicm      144:        static const char       *other = "!#()+,-.0123456789:;<=>?'\r\t";
1.28      nicm      145:        key_code                 key;
1.24      nicm      146:        u_short                  u;
1.32      nicm      147:        int                      size;
1.28      nicm      148:        key_code                 modifiers;
1.29      nicm      149:        struct utf8_data         ud;
1.28      nicm      150:        u_int                    i;
1.32      nicm      151:        enum utf8_state          more;
1.23      nicm      152:
1.33      nicm      153:        /* Is this no key? */
                    154:        if (strcasecmp(string, "None") == 0)
                    155:                return (KEYC_NONE);
                    156:
1.23      nicm      157:        /* Is this a hexadecimal value? */
                    158:        if (string[0] == '0' && string[1] == 'x') {
                    159:                if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4)
1.33      nicm      160:                        return (KEYC_UNKNOWN);
1.23      nicm      161:                return (u);
                    162:        }
1.1       nicm      163:
1.14      nicm      164:        /* Check for modifiers. */
                    165:        modifiers = 0;
                    166:        if (string[0] == '^' && string[1] != '\0') {
                    167:                modifiers |= KEYC_CTRL;
                    168:                string++;
                    169:        }
                    170:        modifiers |= key_string_get_modifiers(&string);
1.1       nicm      171:        if (string[0] == '\0')
1.33      nicm      172:                return (KEYC_UNKNOWN);
1.1       nicm      173:
1.14      nicm      174:        /* Is this a standard ASCII key? */
1.28      nicm      175:        if (string[1] == '\0' && (u_char)string[0] <= 127) {
                    176:                key = (u_char)string[0];
                    177:                if (key < 32 || key == 127)
1.33      nicm      178:                        return (KEYC_UNKNOWN);
1.17      nicm      179:        } else {
1.28      nicm      180:                /* Try as a UTF-8 key. */
1.32      nicm      181:                if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
1.29      nicm      182:                        if (strlen(string) != ud.size)
1.33      nicm      183:                                return (KEYC_UNKNOWN);
1.29      nicm      184:                        for (i = 1; i < ud.size; i++)
1.30      nicm      185:                                more = utf8_append(&ud, (u_char)string[i]);
1.32      nicm      186:                        if (more != UTF8_DONE)
1.33      nicm      187:                                return (KEYC_UNKNOWN);
1.29      nicm      188:                        key = utf8_combine(&ud);
1.28      nicm      189:                        return (key | modifiers);
                    190:                }
                    191:
1.17      nicm      192:                /* Otherwise look the key up in the table. */
                    193:                key = key_string_search_table(string);
1.33      nicm      194:                if (key == KEYC_UNKNOWN)
                    195:                        return (KEYC_UNKNOWN);
1.17      nicm      196:        }
1.12      nicm      197:
1.17      nicm      198:        /* Convert the standard control keys. */
1.24      nicm      199:        if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
1.17      nicm      200:                if (key >= 97 && key <= 122)
                    201:                        key -= 96;
                    202:                else if (key >= 64 && key <= 95)
                    203:                        key -= 64;
                    204:                else if (key == 32)
                    205:                        key = 0;
                    206:                else if (key == 63)
                    207:                        key = KEYC_BSPACE;
                    208:                else
1.33      nicm      209:                        return (KEYC_UNKNOWN);
1.17      nicm      210:                modifiers &= ~KEYC_CTRL;
1.13      nicm      211:        }
                    212:
1.14      nicm      213:        return (key | modifiers);
1.1       nicm      214: }
                    215:
1.10      nicm      216: /* Convert a key code into string format, with prefix if necessary. */
1.1       nicm      217: const char *
1.28      nicm      218: key_string_lookup_key(key_code key)
1.1       nicm      219: {
1.28      nicm      220:        static char             out[24];
                    221:        char                    tmp[8];
                    222:        u_int                   i;
1.29      nicm      223:        struct utf8_data        ud;
1.14      nicm      224:
                    225:        *out = '\0';
1.22      nicm      226:
                    227:        /* Handle no key. */
                    228:        if (key == KEYC_NONE)
1.33      nicm      229:                return ("None");
                    230:
                    231:        /* Handle special keys. */
                    232:        if (key == KEYC_UNKNOWN)
                    233:                return ("Unknown");
1.26      nicm      234:        if (key == KEYC_MOUSE)
1.33      nicm      235:                return ("Mouse");
1.14      nicm      236:
1.18      nicm      237:        /*
                    238:         * Special case: display C-@ as C-Space. Could do this below in
                    239:         * the (key >= 0 && key <= 32), but this way we let it be found
                    240:         * in key_string_table, for the unlikely chance that we might
                    241:         * change its name.
                    242:         */
                    243:        if ((key & KEYC_MASK_KEY) == 0)
                    244:            key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
                    245:
1.14      nicm      246:        /* Fill in the modifiers. */
                    247:        if (key & KEYC_CTRL)
                    248:                strlcat(out, "C-", sizeof out);
                    249:        if (key & KEYC_ESCAPE)
                    250:                strlcat(out, "M-", sizeof out);
                    251:        if (key & KEYC_SHIFT)
                    252:                strlcat(out, "S-", sizeof out);
1.18      nicm      253:        key &= KEYC_MASK_KEY;
1.1       nicm      254:
1.14      nicm      255:        /* Try the key against the string table. */
1.6       nicm      256:        for (i = 0; i < nitems(key_string_table); i++) {
                    257:                if (key == key_string_table[i].key)
1.14      nicm      258:                        break;
                    259:        }
                    260:        if (i != nitems(key_string_table)) {
                    261:                strlcat(out, key_string_table[i].string, sizeof out);
                    262:                return (out);
1.6       nicm      263:        }
                    264:
1.28      nicm      265:        /* Is this a UTF-8 key? */
                    266:        if (key > 127 && key < KEYC_BASE) {
1.32      nicm      267:                if (utf8_split(key, &ud) == UTF8_DONE) {
1.29      nicm      268:                        memcpy(out, ud.data, ud.size);
                    269:                        out[ud.size] = '\0';
1.28      nicm      270:                        return (out);
                    271:                }
                    272:        }
                    273:
1.14      nicm      274:        /* Invalid keys are errors. */
1.27      nicm      275:        if (key == 127 || key > 255) {
1.33      nicm      276:                snprintf(out, sizeof out, "Invalid#%llx", key);
1.27      nicm      277:                return (out);
                    278:        }
1.1       nicm      279:
1.14      nicm      280:        /* Check for standard or control key. */
1.28      nicm      281:        if (key <= 32) {
1.1       nicm      282:                if (key == 0 || key > 26)
1.28      nicm      283:                        xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
1.1       nicm      284:                else
1.28      nicm      285:                        xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
1.14      nicm      286:        } else if (key >= 32 && key <= 126) {
                    287:                tmp[0] = key;
                    288:                tmp[1] = '\0';
1.20      nicm      289:        } else if (key >= 128)
1.28      nicm      290:                xsnprintf(tmp, sizeof tmp, "\\%llo", key);
1.20      nicm      291:
1.14      nicm      292:        strlcat(out, tmp, sizeof out);
                    293:        return (out);
1.1       nicm      294: }