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

1.23    ! nicm        1: /* $OpenBSD: key-string.c,v 1.22 2012/01/21 08:40:09 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:
                     21: #include <string.h>
                     22:
                     23: #include "tmux.h"
                     24:
                     25: int    key_string_search_table(const char *);
1.14      nicm       26: int    key_string_get_modifiers(const char **);
1.1       nicm       27:
1.19      nicm       28: const struct {
1.14      nicm       29:        const char     *string;
                     30:        int             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:        { "F13",        KEYC_F13 },
                     46:        { "F14",        KEYC_F14 },
                     47:        { "F15",        KEYC_F15 },
                     48:        { "F16",        KEYC_F16 },
                     49:        { "F17",        KEYC_F17 },
                     50:        { "F18",        KEYC_F18 },
                     51:        { "F19",        KEYC_F19 },
                     52:        { "F20",        KEYC_F20 },
                     53:        { "IC",         KEYC_IC },
                     54:        { "DC",         KEYC_DC },
                     55:        { "Home",       KEYC_HOME },
                     56:        { "End",        KEYC_END },
                     57:        { "NPage",      KEYC_NPAGE },
1.21      nicm       58:        { "PageDown",   KEYC_NPAGE },
                     59:        { "PgDn",       KEYC_NPAGE },
1.1       nicm       60:        { "PPage",      KEYC_PPAGE },
1.21      nicm       61:        { "PageUp",     KEYC_PPAGE },
                     62:        { "PgUp",       KEYC_PPAGE },
1.1       nicm       63:        { "Tab",        '\011' },
                     64:        { "BTab",       KEYC_BTAB },
1.7       nicm       65:        { "Space",      ' ' },
1.5       nicm       66:        { "BSpace",     KEYC_BSPACE },
1.6       nicm       67:        { "Enter",      '\r' },
                     68:        { "Escape",     '\033' },
1.1       nicm       69:
                     70:        /* Arrow keys. */
                     71:        { "Up",         KEYC_UP },
                     72:        { "Down",       KEYC_DOWN },
                     73:        { "Left",       KEYC_LEFT },
                     74:        { "Right",      KEYC_RIGHT },
                     75:
                     76:        /* Numeric keypad. */
1.9       nicm       77:        { "KP/",        KEYC_KP_SLASH },
                     78:        { "KP*",        KEYC_KP_STAR },
                     79:        { "KP-",        KEYC_KP_MINUS },
                     80:        { "KP7",        KEYC_KP_SEVEN },
                     81:        { "KP8",        KEYC_KP_EIGHT },
                     82:        { "KP9",        KEYC_KP_NINE },
                     83:        { "KP+",        KEYC_KP_PLUS },
                     84:        { "KP4",        KEYC_KP_FOUR },
                     85:        { "KP5",        KEYC_KP_FIVE },
                     86:        { "KP6",        KEYC_KP_SIX },
                     87:        { "KP1",        KEYC_KP_ONE },
                     88:        { "KP2",        KEYC_KP_TWO },
                     89:        { "KP3",        KEYC_KP_THREE },
                     90:        { "KPEnter",    KEYC_KP_ENTER },
                     91:        { "KP0",        KEYC_KP_ZERO },
                     92:        { "KP.",        KEYC_KP_PERIOD },
1.1       nicm       93: };
                     94:
1.10      nicm       95: /* Find key string in table. */
1.1       nicm       96: int
                     97: key_string_search_table(const char *string)
                     98: {
                     99:        u_int   i;
                    100:
                    101:        for (i = 0; i < nitems(key_string_table); i++) {
                    102:                if (strcasecmp(string, key_string_table[i].string) == 0)
                    103:                        return (key_string_table[i].key);
                    104:        }
                    105:        return (KEYC_NONE);
                    106: }
                    107:
1.14      nicm      108: /* Find modifiers. */
                    109: int
                    110: key_string_get_modifiers(const char **string)
                    111: {
                    112:        int     modifiers;
                    113:
                    114:        modifiers = 0;
                    115:        while (((*string)[0] != '\0') && (*string)[1] == '-') {
                    116:                switch ((*string)[0]) {
                    117:                case 'C':
                    118:                case 'c':
                    119:                        modifiers |= KEYC_CTRL;
                    120:                        break;
                    121:                case 'M':
                    122:                case 'm':
                    123:                        modifiers |= KEYC_ESCAPE;
                    124:                        break;
                    125:                case 'S':
                    126:                case 's':
                    127:                        modifiers |= KEYC_SHIFT;
                    128:                        break;
                    129:                }
                    130:                *string += 2;
                    131:        }
                    132:        return (modifiers);
                    133: }
                    134:
                    135: /* Lookup a string and convert to a key value. */
1.1       nicm      136: int
                    137: key_string_lookup_string(const char *string)
                    138: {
1.14      nicm      139:        int     key, modifiers;
1.23    ! nicm      140:        u_short u;
        !           141:        int     size;
        !           142:
        !           143:        /* Is this a hexadecimal value? */
        !           144:        if (string[0] == '0' && string[1] == 'x') {
        !           145:                if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4)
        !           146:                        return (KEYC_NONE);
        !           147:                return (u);
        !           148:        }
1.1       nicm      149:
1.14      nicm      150:        /* Check for modifiers. */
                    151:        modifiers = 0;
                    152:        if (string[0] == '^' && string[1] != '\0') {
                    153:                modifiers |= KEYC_CTRL;
                    154:                string++;
                    155:        }
                    156:        modifiers |= key_string_get_modifiers(&string);
1.1       nicm      157:        if (string[0] == '\0')
                    158:                return (KEYC_NONE);
                    159:
1.14      nicm      160:        /* Is this a standard ASCII key? */
                    161:        if (string[1] == '\0') {
                    162:                key = (u_char) string[0];
1.20      nicm      163:                if (key < 32 || key == 127 || key > 255)
1.1       nicm      164:                        return (KEYC_NONE);
1.17      nicm      165:        } else {
                    166:                /* Otherwise look the key up in the table. */
                    167:                key = key_string_search_table(string);
                    168:                if (key == KEYC_NONE)
                    169:                        return (KEYC_NONE);
                    170:        }
1.12      nicm      171:
1.17      nicm      172:        /* Convert the standard control keys. */
                    173:        if (key < KEYC_BASE && (modifiers & KEYC_CTRL)) {
                    174:                if (key >= 97 && key <= 122)
                    175:                        key -= 96;
                    176:                else if (key >= 64 && key <= 95)
                    177:                        key -= 64;
                    178:                else if (key == 32)
                    179:                        key = 0;
                    180:                else if (key == 63)
                    181:                        key = KEYC_BSPACE;
                    182:                else
                    183:                        return (KEYC_NONE);
                    184:                modifiers &= ~KEYC_CTRL;
1.13      nicm      185:        }
                    186:
1.14      nicm      187:        return (key | modifiers);
1.1       nicm      188: }
                    189:
1.10      nicm      190: /* Convert a key code into string format, with prefix if necessary. */
1.1       nicm      191: const char *
                    192: key_string_lookup_key(int key)
                    193: {
1.14      nicm      194:        static char     out[24];
                    195:        char            tmp[8];
                    196:        u_int           i;
                    197:
                    198:        *out = '\0';
1.22      nicm      199:
                    200:        /* Handle no key. */
                    201:        if (key == KEYC_NONE)
                    202:                return ("none");
1.14      nicm      203:
1.18      nicm      204:        /*
                    205:         * Special case: display C-@ as C-Space. Could do this below in
                    206:         * the (key >= 0 && key <= 32), but this way we let it be found
                    207:         * in key_string_table, for the unlikely chance that we might
                    208:         * change its name.
                    209:         */
                    210:        if ((key & KEYC_MASK_KEY) == 0)
                    211:            key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
                    212:
1.14      nicm      213:        /* Fill in the modifiers. */
                    214:        if (key & KEYC_CTRL)
                    215:                strlcat(out, "C-", sizeof out);
                    216:        if (key & KEYC_ESCAPE)
                    217:                strlcat(out, "M-", sizeof out);
                    218:        if (key & KEYC_SHIFT)
                    219:                strlcat(out, "S-", sizeof out);
1.18      nicm      220:        key &= KEYC_MASK_KEY;
1.1       nicm      221:
1.14      nicm      222:        /* Try the key against the string table. */
1.6       nicm      223:        for (i = 0; i < nitems(key_string_table); i++) {
                    224:                if (key == key_string_table[i].key)
1.14      nicm      225:                        break;
                    226:        }
                    227:        if (i != nitems(key_string_table)) {
                    228:                strlcat(out, key_string_table[i].string, sizeof out);
                    229:                return (out);
1.6       nicm      230:        }
                    231:
1.14      nicm      232:        /* Invalid keys are errors. */
1.20      nicm      233:        if (key == 127 || key > 255)
1.14      nicm      234:                return (NULL);
1.1       nicm      235:
1.14      nicm      236:        /* Check for standard or control key. */
1.1       nicm      237:        if (key >= 0 && key <= 32) {
                    238:                if (key == 0 || key > 26)
                    239:                        xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key);
                    240:                else
                    241:                        xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key);
1.14      nicm      242:        } else if (key >= 32 && key <= 126) {
                    243:                tmp[0] = key;
                    244:                tmp[1] = '\0';
1.20      nicm      245:        } else if (key >= 128)
                    246:                xsnprintf(tmp, sizeof tmp, "\\%o", key);
                    247:
1.14      nicm      248:        strlcat(out, tmp, sizeof out);
                    249:        return (out);
1.1       nicm      250: }