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

1.17    ! nicm        1: /* $OpenBSD: key-string.c,v 1.16 2010/05/03 09:38:03 mcbride 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:
                     28: 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 },
                     58:        { "PPage",      KEYC_PPAGE },
                     59:        { "Tab",        '\011' },
                     60:        { "BTab",       KEYC_BTAB },
1.7       nicm       61:        { "Space",      ' ' },
1.5       nicm       62:        { "BSpace",     KEYC_BSPACE },
1.6       nicm       63:        { "Enter",      '\r' },
                     64:        { "Escape",     '\033' },
1.1       nicm       65:
                     66:        /* Arrow keys. */
                     67:        { "Up",         KEYC_UP },
                     68:        { "Down",       KEYC_DOWN },
                     69:        { "Left",       KEYC_LEFT },
                     70:        { "Right",      KEYC_RIGHT },
                     71:
                     72:        /* Numeric keypad. */
1.9       nicm       73:        { "KP/",        KEYC_KP_SLASH },
                     74:        { "KP*",        KEYC_KP_STAR },
                     75:        { "KP-",        KEYC_KP_MINUS },
                     76:        { "KP7",        KEYC_KP_SEVEN },
                     77:        { "KP8",        KEYC_KP_EIGHT },
                     78:        { "KP9",        KEYC_KP_NINE },
                     79:        { "KP+",        KEYC_KP_PLUS },
                     80:        { "KP4",        KEYC_KP_FOUR },
                     81:        { "KP5",        KEYC_KP_FIVE },
                     82:        { "KP6",        KEYC_KP_SIX },
                     83:        { "KP1",        KEYC_KP_ONE },
                     84:        { "KP2",        KEYC_KP_TWO },
                     85:        { "KP3",        KEYC_KP_THREE },
                     86:        { "KPEnter",    KEYC_KP_ENTER },
                     87:        { "KP0",        KEYC_KP_ZERO },
                     88:        { "KP.",        KEYC_KP_PERIOD },
1.1       nicm       89: };
                     90:
1.10      nicm       91: /* Find key string in table. */
1.1       nicm       92: int
                     93: key_string_search_table(const char *string)
                     94: {
                     95:        u_int   i;
                     96:
                     97:        for (i = 0; i < nitems(key_string_table); i++) {
                     98:                if (strcasecmp(string, key_string_table[i].string) == 0)
                     99:                        return (key_string_table[i].key);
                    100:        }
                    101:        return (KEYC_NONE);
                    102: }
                    103:
1.14      nicm      104: /* Find modifiers. */
                    105: int
                    106: key_string_get_modifiers(const char **string)
                    107: {
                    108:        int     modifiers;
                    109:
                    110:        modifiers = 0;
                    111:        while (((*string)[0] != '\0') && (*string)[1] == '-') {
                    112:                switch ((*string)[0]) {
                    113:                case 'C':
                    114:                case 'c':
                    115:                        modifiers |= KEYC_CTRL;
                    116:                        break;
                    117:                case 'M':
                    118:                case 'm':
                    119:                        modifiers |= KEYC_ESCAPE;
                    120:                        break;
                    121:                case 'S':
                    122:                case 's':
                    123:                        modifiers |= KEYC_SHIFT;
                    124:                        break;
                    125:                }
                    126:                *string += 2;
                    127:        }
                    128:        return (modifiers);
                    129: }
                    130:
                    131: /* Lookup a string and convert to a key value. */
1.1       nicm      132: int
                    133: key_string_lookup_string(const char *string)
                    134: {
1.14      nicm      135:        int     key, modifiers;
1.1       nicm      136:
1.14      nicm      137:        /* Check for modifiers. */
                    138:        modifiers = 0;
                    139:        if (string[0] == '^' && string[1] != '\0') {
                    140:                modifiers |= KEYC_CTRL;
                    141:                string++;
                    142:        }
                    143:        modifiers |= key_string_get_modifiers(&string);
1.1       nicm      144:        if (string[0] == '\0')
                    145:                return (KEYC_NONE);
                    146:
1.14      nicm      147:        /* Is this a standard ASCII key? */
                    148:        if (string[1] == '\0') {
                    149:                key = (u_char) string[0];
                    150:                if (key < 32 || key > 126)
1.1       nicm      151:                        return (KEYC_NONE);
1.17    ! nicm      152:        } else {
        !           153:                /* Otherwise look the key up in the table. */
        !           154:                key = key_string_search_table(string);
        !           155:                if (key == KEYC_NONE)
        !           156:                        return (KEYC_NONE);
        !           157:        }
1.12      nicm      158:
1.17    ! nicm      159:        /* Convert the standard control keys. */
        !           160:        if (key < KEYC_BASE && (modifiers & KEYC_CTRL)) {
        !           161:                if (key >= 97 && key <= 122)
        !           162:                        key -= 96;
        !           163:                else if (key >= 64 && key <= 95)
        !           164:                        key -= 64;
        !           165:                else if (key == 32)
        !           166:                        key = 0;
        !           167:                else if (key == 63)
        !           168:                        key = KEYC_BSPACE;
        !           169:                else
        !           170:                        return (KEYC_NONE);
        !           171:                modifiers &= ~KEYC_CTRL;
1.13      nicm      172:        }
                    173:
1.14      nicm      174:        return (key | modifiers);
1.1       nicm      175: }
                    176:
1.10      nicm      177: /* Convert a key code into string format, with prefix if necessary. */
1.1       nicm      178: const char *
                    179: key_string_lookup_key(int key)
                    180: {
1.14      nicm      181:        static char     out[24];
                    182:        char            tmp[8];
                    183:        u_int           i;
                    184:
                    185:        *out = '\0';
                    186:
                    187:        /* Fill in the modifiers. */
                    188:        if (key & KEYC_CTRL)
                    189:                strlcat(out, "C-", sizeof out);
                    190:        if (key & KEYC_ESCAPE)
                    191:                strlcat(out, "M-", sizeof out);
                    192:        if (key & KEYC_SHIFT)
                    193:                strlcat(out, "S-", sizeof out);
                    194:        key &= ~(KEYC_CTRL|KEYC_ESCAPE|KEYC_SHIFT);
1.1       nicm      195:
1.14      nicm      196:        /* Try the key against the string table. */
1.6       nicm      197:        for (i = 0; i < nitems(key_string_table); i++) {
                    198:                if (key == key_string_table[i].key)
1.14      nicm      199:                        break;
                    200:        }
                    201:        if (i != nitems(key_string_table)) {
                    202:                strlcat(out, key_string_table[i].string, sizeof out);
                    203:                return (out);
1.6       nicm      204:        }
                    205:
1.14      nicm      206:        /* Invalid keys are errors. */
                    207:        if (key >= 127)
                    208:                return (NULL);
1.1       nicm      209:
1.14      nicm      210:        /* Check for standard or control key. */
1.1       nicm      211:        if (key >= 0 && key <= 32) {
                    212:                if (key == 0 || key > 26)
                    213:                        xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key);
                    214:                else
                    215:                        xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key);
1.14      nicm      216:        } else if (key >= 32 && key <= 126) {
                    217:                tmp[0] = key;
                    218:                tmp[1] = '\0';
1.1       nicm      219:        }
1.14      nicm      220:        strlcat(out, tmp, sizeof out);
                    221:        return (out);
1.1       nicm      222: }