2011-07-09 11:42:33 +02:00
|
|
|
/* $Id$ */
|
2007-10-03 13:26:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
2009-01-09 17:45:58 +01:00
|
|
|
int key_string_search_table(const char *);
|
2010-04-22 23:50:30 +02:00
|
|
|
int key_string_get_modifiers(const char **);
|
2009-01-09 17:45:58 +01:00
|
|
|
|
2011-01-04 00:32:04 +01:00
|
|
|
const struct {
|
2010-04-22 23:50:30 +02:00
|
|
|
const char *string;
|
|
|
|
int key;
|
2007-10-03 13:26:34 +02:00
|
|
|
} key_string_table[] = {
|
2009-01-08 23:28:02 +01:00
|
|
|
/* Function keys. */
|
2007-10-03 13:26:34 +02:00
|
|
|
{ "F1", KEYC_F1 },
|
|
|
|
{ "F2", KEYC_F2 },
|
|
|
|
{ "F3", KEYC_F3 },
|
|
|
|
{ "F4", KEYC_F4 },
|
|
|
|
{ "F5", KEYC_F5 },
|
|
|
|
{ "F6", KEYC_F6 },
|
|
|
|
{ "F7", KEYC_F7 },
|
|
|
|
{ "F8", KEYC_F8 },
|
|
|
|
{ "F9", KEYC_F9 },
|
2009-01-08 23:28:02 +01:00
|
|
|
{ "F10", KEYC_F10 },
|
|
|
|
{ "F11", KEYC_F11 },
|
|
|
|
{ "F12", KEYC_F12 },
|
2009-03-02 19:05:40 +01:00
|
|
|
{ "F13", KEYC_F13 },
|
|
|
|
{ "F14", KEYC_F14 },
|
|
|
|
{ "F15", KEYC_F15 },
|
|
|
|
{ "F16", KEYC_F16 },
|
|
|
|
{ "F17", KEYC_F17 },
|
|
|
|
{ "F18", KEYC_F18 },
|
|
|
|
{ "F19", KEYC_F19 },
|
|
|
|
{ "F20", KEYC_F20 },
|
2007-10-03 13:26:34 +02:00
|
|
|
{ "IC", KEYC_IC },
|
2009-01-08 23:28:02 +01:00
|
|
|
{ "DC", KEYC_DC },
|
2009-01-14 23:41:40 +01:00
|
|
|
{ "Home", KEYC_HOME },
|
|
|
|
{ "End", KEYC_END },
|
|
|
|
{ "NPage", KEYC_NPAGE },
|
2011-10-23 17:11:09 +02:00
|
|
|
{ "PageDown", KEYC_NPAGE },
|
|
|
|
{ "PgDn", KEYC_NPAGE },
|
2009-01-14 23:41:40 +01:00
|
|
|
{ "PPage", KEYC_PPAGE },
|
2011-10-23 17:11:09 +02:00
|
|
|
{ "PageUp", KEYC_PPAGE },
|
|
|
|
{ "PgUp", KEYC_PPAGE },
|
2009-01-16 20:32:29 +01:00
|
|
|
{ "Tab", '\011' },
|
|
|
|
{ "BTab", KEYC_BTAB },
|
2009-10-05 20:21:58 +02:00
|
|
|
{ "Space", ' ' },
|
2009-07-29 00:37:02 +02:00
|
|
|
{ "BSpace", KEYC_BSPACE },
|
2009-07-29 01:13:00 +02:00
|
|
|
{ "Enter", '\r' },
|
|
|
|
{ "Escape", '\033' },
|
2009-01-08 23:28:02 +01:00
|
|
|
|
|
|
|
/* Arrow keys. */
|
2009-01-14 23:41:40 +01:00
|
|
|
{ "Up", KEYC_UP },
|
|
|
|
{ "Down", KEYC_DOWN },
|
|
|
|
{ "Left", KEYC_LEFT },
|
|
|
|
{ "Right", KEYC_RIGHT },
|
2008-07-24 01:44:50 +02:00
|
|
|
|
|
|
|
/* Numeric keypad. */
|
2009-10-28 23:53:03 +01:00
|
|
|
{ "KP/", KEYC_KP_SLASH },
|
|
|
|
{ "KP*", KEYC_KP_STAR },
|
|
|
|
{ "KP-", KEYC_KP_MINUS },
|
|
|
|
{ "KP7", KEYC_KP_SEVEN },
|
|
|
|
{ "KP8", KEYC_KP_EIGHT },
|
|
|
|
{ "KP9", KEYC_KP_NINE },
|
|
|
|
{ "KP+", KEYC_KP_PLUS },
|
|
|
|
{ "KP4", KEYC_KP_FOUR },
|
|
|
|
{ "KP5", KEYC_KP_FIVE },
|
|
|
|
{ "KP6", KEYC_KP_SIX },
|
|
|
|
{ "KP1", KEYC_KP_ONE },
|
|
|
|
{ "KP2", KEYC_KP_TWO },
|
|
|
|
{ "KP3", KEYC_KP_THREE },
|
|
|
|
{ "KPEnter", KEYC_KP_ENTER },
|
|
|
|
{ "KP0", KEYC_KP_ZERO },
|
|
|
|
{ "KP.", KEYC_KP_PERIOD },
|
2007-10-03 13:26:34 +02:00
|
|
|
};
|
|
|
|
|
2009-11-11 00:30:26 +01:00
|
|
|
/* Find key string in table. */
|
2009-01-09 17:45:58 +01:00
|
|
|
int
|
|
|
|
key_string_search_table(const char *string)
|
|
|
|
{
|
|
|
|
u_int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nitems(key_string_table); i++) {
|
|
|
|
if (strcasecmp(string, key_string_table[i].string) == 0)
|
|
|
|
return (key_string_table[i].key);
|
|
|
|
}
|
|
|
|
return (KEYC_NONE);
|
|
|
|
}
|
|
|
|
|
2010-04-22 23:50:30 +02:00
|
|
|
/* Find modifiers. */
|
2007-10-03 13:26:34 +02:00
|
|
|
int
|
2010-04-22 23:50:30 +02:00
|
|
|
key_string_get_modifiers(const char **string)
|
2007-10-03 13:26:34 +02:00
|
|
|
{
|
2010-04-22 23:50:30 +02:00
|
|
|
int modifiers;
|
|
|
|
|
|
|
|
modifiers = 0;
|
|
|
|
while (((*string)[0] != '\0') && (*string)[1] == '-') {
|
|
|
|
switch ((*string)[0]) {
|
|
|
|
case 'C':
|
|
|
|
case 'c':
|
|
|
|
modifiers |= KEYC_CTRL;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
case 'm':
|
|
|
|
modifiers |= KEYC_ESCAPE;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
case 's':
|
|
|
|
modifiers |= KEYC_SHIFT;
|
|
|
|
break;
|
2009-11-11 00:30:26 +01:00
|
|
|
}
|
2010-04-22 23:50:30 +02:00
|
|
|
*string += 2;
|
2008-06-25 22:33:20 +02:00
|
|
|
}
|
2010-04-22 23:50:30 +02:00
|
|
|
return (modifiers);
|
|
|
|
}
|
2009-12-04 23:14:47 +01:00
|
|
|
|
2010-04-22 23:50:30 +02:00
|
|
|
/* Lookup a string and convert to a key value. */
|
|
|
|
int
|
|
|
|
key_string_lookup_string(const char *string)
|
|
|
|
{
|
2013-02-17 23:56:12 +01:00
|
|
|
static const char *other = "!#()+,-.0123456789:;<=>?'\r\t";
|
|
|
|
int key, modifiers;
|
|
|
|
u_short u;
|
|
|
|
int size;
|
2012-03-07 14:38:26 +01:00
|
|
|
|
|
|
|
/* Is this a hexadecimal value? */
|
|
|
|
if (string[0] == '0' && string[1] == 'x') {
|
|
|
|
if (sscanf(string + 2, "%hx%n", &u, &size) != 1 || size > 4)
|
|
|
|
return (KEYC_NONE);
|
|
|
|
return (u);
|
|
|
|
}
|
2009-11-11 00:30:26 +01:00
|
|
|
|
2010-04-22 23:50:30 +02:00
|
|
|
/* Check for modifiers. */
|
|
|
|
modifiers = 0;
|
|
|
|
if (string[0] == '^' && string[1] != '\0') {
|
|
|
|
modifiers |= KEYC_CTRL;
|
|
|
|
string++;
|
2008-06-25 22:33:20 +02:00
|
|
|
}
|
2010-04-22 23:50:30 +02:00
|
|
|
modifiers |= key_string_get_modifiers(&string);
|
|
|
|
if (string[0] == '\0')
|
|
|
|
return (KEYC_NONE);
|
2008-06-25 22:33:20 +02:00
|
|
|
|
2010-04-22 23:50:30 +02:00
|
|
|
/* Is this a standard ASCII key? */
|
|
|
|
if (string[1] == '\0') {
|
|
|
|
key = (u_char) string[0];
|
2011-01-22 23:31:09 +01:00
|
|
|
if (key < 32 || key == 127 || key > 255)
|
2010-01-17 20:01:27 +01:00
|
|
|
return (KEYC_NONE);
|
2010-06-05 08:27:19 +02:00
|
|
|
} else {
|
|
|
|
/* Otherwise look the key up in the table. */
|
|
|
|
key = key_string_search_table(string);
|
|
|
|
if (key == KEYC_NONE)
|
|
|
|
return (KEYC_NONE);
|
|
|
|
}
|
2010-04-22 23:50:30 +02:00
|
|
|
|
2010-06-05 08:27:19 +02:00
|
|
|
/* Convert the standard control keys. */
|
2013-02-17 23:56:12 +01:00
|
|
|
if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
|
2010-06-05 08:27:19 +02:00
|
|
|
if (key >= 97 && key <= 122)
|
|
|
|
key -= 96;
|
|
|
|
else if (key >= 64 && key <= 95)
|
|
|
|
key -= 64;
|
|
|
|
else if (key == 32)
|
|
|
|
key = 0;
|
|
|
|
else if (key == 63)
|
|
|
|
key = KEYC_BSPACE;
|
|
|
|
else
|
|
|
|
return (KEYC_NONE);
|
|
|
|
modifiers &= ~KEYC_CTRL;
|
2010-01-17 20:01:27 +01:00
|
|
|
}
|
|
|
|
|
2010-04-22 23:50:30 +02:00
|
|
|
return (key | modifiers);
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
2007-10-04 02:18:59 +02:00
|
|
|
|
2009-11-11 00:30:26 +01:00
|
|
|
/* Convert a key code into string format, with prefix if necessary. */
|
2007-10-04 02:18:59 +02:00
|
|
|
const char *
|
|
|
|
key_string_lookup_key(int key)
|
|
|
|
{
|
2010-04-22 23:50:30 +02:00
|
|
|
static char out[24];
|
|
|
|
char tmp[8];
|
2013-02-17 23:56:12 +01:00
|
|
|
u_int i;
|
2010-04-22 23:50:30 +02:00
|
|
|
|
|
|
|
*out = '\0';
|
|
|
|
|
2012-01-21 20:36:40 +01:00
|
|
|
/* Handle no key. */
|
|
|
|
if (key == KEYC_NONE)
|
|
|
|
return ("none");
|
|
|
|
|
2010-06-05 22:29:11 +02:00
|
|
|
/*
|
|
|
|
* Special case: display C-@ as C-Space. Could do this below in
|
|
|
|
* the (key >= 0 && key <= 32), but this way we let it be found
|
|
|
|
* in key_string_table, for the unlikely chance that we might
|
|
|
|
* change its name.
|
|
|
|
*/
|
|
|
|
if ((key & KEYC_MASK_KEY) == 0)
|
|
|
|
key = ' ' | KEYC_CTRL | (key & KEYC_MASK_MOD);
|
|
|
|
|
2010-04-22 23:50:30 +02:00
|
|
|
/* Fill in the modifiers. */
|
|
|
|
if (key & KEYC_CTRL)
|
|
|
|
strlcat(out, "C-", sizeof out);
|
|
|
|
if (key & KEYC_ESCAPE)
|
|
|
|
strlcat(out, "M-", sizeof out);
|
|
|
|
if (key & KEYC_SHIFT)
|
|
|
|
strlcat(out, "S-", sizeof out);
|
2010-06-05 22:29:11 +02:00
|
|
|
key &= KEYC_MASK_KEY;
|
2010-04-22 23:50:30 +02:00
|
|
|
|
|
|
|
/* Try the key against the string table. */
|
2009-07-29 01:13:00 +02:00
|
|
|
for (i = 0; i < nitems(key_string_table); i++) {
|
|
|
|
if (key == key_string_table[i].key)
|
2010-04-22 23:50:30 +02:00
|
|
|
break;
|
2009-07-29 01:13:00 +02:00
|
|
|
}
|
2010-04-22 23:50:30 +02:00
|
|
|
if (i != nitems(key_string_table)) {
|
|
|
|
strlcat(out, key_string_table[i].string, sizeof out);
|
|
|
|
return (out);
|
2007-10-04 02:18:59 +02:00
|
|
|
}
|
|
|
|
|
2010-04-22 23:50:30 +02:00
|
|
|
/* Invalid keys are errors. */
|
2011-01-22 23:31:09 +01:00
|
|
|
if (key == 127 || key > 255)
|
2010-04-22 23:50:30 +02:00
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* Check for standard or control key. */
|
2008-06-25 22:33:20 +02:00
|
|
|
if (key >= 0 && key <= 32) {
|
|
|
|
if (key == 0 || key > 26)
|
|
|
|
xsnprintf(tmp, sizeof tmp, "C-%c", 64 + key);
|
|
|
|
else
|
|
|
|
xsnprintf(tmp, sizeof tmp, "C-%c", 96 + key);
|
2010-04-22 23:50:30 +02:00
|
|
|
} else if (key >= 32 && key <= 126) {
|
|
|
|
tmp[0] = key;
|
|
|
|
tmp[1] = '\0';
|
2011-01-22 23:31:09 +01:00
|
|
|
} else if (key >= 128)
|
|
|
|
xsnprintf(tmp, sizeof tmp, "\\%o", key);
|
|
|
|
|
2010-04-22 23:50:30 +02:00
|
|
|
strlcat(out, tmp, sizeof out);
|
|
|
|
return (out);
|
2007-10-04 02:18:59 +02:00
|
|
|
}
|