2009-10-06 16:15:45 +02:00
|
|
|
/* $Id: key-string.c,v 1.24 2009-10-06 14:15:45 tcunha Exp $ */
|
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 *);
|
|
|
|
|
2007-10-03 13:26:34 +02:00
|
|
|
struct {
|
|
|
|
const char *string;
|
|
|
|
int key;
|
|
|
|
} 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 },
|
|
|
|
{ "PPage", 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. */
|
|
|
|
{ "KP/", KEYC_KP0_1 },
|
|
|
|
{ "KP*", KEYC_KP0_2 },
|
|
|
|
{ "KP-", KEYC_KP0_3 },
|
|
|
|
{ "KP7", KEYC_KP1_0 },
|
|
|
|
{ "KP8", KEYC_KP1_1 },
|
|
|
|
{ "KP9", KEYC_KP1_2 },
|
|
|
|
{ "KP+", KEYC_KP1_3 },
|
|
|
|
{ "KP4", KEYC_KP2_0 },
|
|
|
|
{ "KP5", KEYC_KP2_1 },
|
|
|
|
{ "KP6", KEYC_KP2_2 },
|
|
|
|
{ "KP1", KEYC_KP3_0 },
|
|
|
|
{ "KP2", KEYC_KP3_1 },
|
|
|
|
{ "KP3", KEYC_KP3_2 },
|
2009-01-14 23:41:40 +01:00
|
|
|
{ "KPEnter", KEYC_KP3_3 },
|
2008-07-24 01:44:50 +02:00
|
|
|
{ "KP0", KEYC_KP4_0 },
|
|
|
|
{ "KP.", KEYC_KP4_2 },
|
2007-10-03 13:26:34 +02:00
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2007-10-03 13:26:34 +02:00
|
|
|
int
|
2007-10-04 02:18:59 +02:00
|
|
|
key_string_lookup_string(const char *string)
|
2007-10-03 13:26:34 +02:00
|
|
|
{
|
2009-01-12 21:13:20 +01:00
|
|
|
int key;
|
2009-01-12 21:24:12 +01:00
|
|
|
const u_char *ptr;
|
2007-10-03 13:26:34 +02:00
|
|
|
|
|
|
|
if (string[0] == '\0')
|
|
|
|
return (KEYC_NONE);
|
|
|
|
if (string[1] == '\0')
|
|
|
|
return (string[0]);
|
|
|
|
|
2009-01-12 21:13:20 +01:00
|
|
|
ptr = NULL;
|
2009-07-25 10:53:48 +02:00
|
|
|
if ((string[0] == 'C' || string[0] == 'c') && string[1] == '-')
|
2009-01-12 21:13:20 +01:00
|
|
|
ptr = string + 2;
|
|
|
|
else if (string[0] == '^')
|
|
|
|
ptr = string + 1;
|
|
|
|
if (ptr != NULL) {
|
|
|
|
if (ptr[0] == '\0')
|
2008-06-25 22:33:20 +02:00
|
|
|
return (KEYC_NONE);
|
2009-01-12 21:13:20 +01:00
|
|
|
if (ptr[1] == '\0') {
|
|
|
|
if (ptr[0] == 32)
|
|
|
|
return (0);
|
2009-10-06 16:15:45 +02:00
|
|
|
if (ptr[0] == 63)
|
|
|
|
return (KEYC_BSPACE);
|
2009-01-12 21:13:20 +01:00
|
|
|
if (ptr[0] >= 64 && ptr[0] <= 95)
|
|
|
|
return (ptr[0] - 64);
|
|
|
|
if (ptr[0] >= 97 && ptr[0] <= 122)
|
|
|
|
return (ptr[0] - 96);
|
2008-06-25 22:33:20 +02:00
|
|
|
return (KEYC_NONE);
|
2009-01-12 21:13:20 +01:00
|
|
|
}
|
|
|
|
key = key_string_search_table(ptr);
|
2009-01-09 17:45:58 +01:00
|
|
|
if (key != KEYC_NONE)
|
2009-07-22 18:24:59 +02:00
|
|
|
return (key | KEYC_CTRL);
|
2008-06-25 22:33:20 +02:00
|
|
|
return (KEYC_NONE);
|
|
|
|
}
|
2009-07-25 10:53:48 +02:00
|
|
|
|
|
|
|
if ((string[0] == 'M' || string[0] == 'm') && string[1] == '-') {
|
2009-01-12 21:24:12 +01:00
|
|
|
ptr = string + 2;
|
|
|
|
if (ptr[0] == '\0')
|
2008-06-25 22:33:20 +02:00
|
|
|
return (KEYC_NONE);
|
2009-01-12 21:24:12 +01:00
|
|
|
if (ptr[1] == '\0') {
|
|
|
|
if (ptr[0] < 32 || ptr[0] > 127)
|
|
|
|
return (KEYC_NONE);
|
2009-07-22 18:24:59 +02:00
|
|
|
return (ptr[0] | KEYC_ESCAPE);
|
2009-01-12 21:24:12 +01:00
|
|
|
}
|
|
|
|
key = key_string_lookup_string(ptr);
|
|
|
|
if (key != KEYC_NONE)
|
2009-07-22 18:24:59 +02:00
|
|
|
return (key | KEYC_ESCAPE);
|
2009-01-12 21:24:12 +01:00
|
|
|
return (KEYC_NONE);
|
2008-06-25 22:33:20 +02:00
|
|
|
}
|
|
|
|
|
2009-01-09 17:45:58 +01:00
|
|
|
return (key_string_search_table(string));
|
2007-10-03 13:26:34 +02:00
|
|
|
}
|
2007-10-04 02:18:59 +02:00
|
|
|
|
|
|
|
const char *
|
|
|
|
key_string_lookup_key(int key)
|
|
|
|
{
|
2008-06-25 22:33:20 +02:00
|
|
|
static char tmp[24], tmp2[24];
|
|
|
|
const char *s;
|
2007-10-04 02:18:59 +02:00
|
|
|
u_int i;
|
|
|
|
|
2008-06-25 22:33:20 +02:00
|
|
|
if (key == 127)
|
|
|
|
return (NULL);
|
|
|
|
|
2009-07-22 18:24:59 +02:00
|
|
|
if (key & KEYC_ESCAPE) {
|
|
|
|
if ((s = key_string_lookup_key(key & ~KEYC_ESCAPE)) == NULL)
|
2008-06-25 22:33:20 +02:00
|
|
|
return (NULL);
|
|
|
|
xsnprintf(tmp2, sizeof tmp2, "M-%s", s);
|
|
|
|
return (tmp2);
|
|
|
|
}
|
2009-07-22 18:24:59 +02:00
|
|
|
if (key & KEYC_CTRL) {
|
|
|
|
if ((s = key_string_lookup_key(key & ~KEYC_CTRL)) == NULL)
|
2009-01-09 17:45:58 +01:00
|
|
|
return (NULL);
|
|
|
|
xsnprintf(tmp2, sizeof tmp2, "C-%s", s);
|
|
|
|
return (tmp2);
|
|
|
|
}
|
2009-07-22 18:24:59 +02:00
|
|
|
if (key & KEYC_SHIFT) {
|
|
|
|
if ((s = key_string_lookup_key(key & ~KEYC_SHIFT)) == NULL)
|
2009-01-12 21:13:20 +01:00
|
|
|
return (NULL);
|
|
|
|
xsnprintf(tmp2, sizeof tmp2, "S-%s", s);
|
|
|
|
return (tmp2);
|
|
|
|
}
|
2008-06-25 22:33:20 +02:00
|
|
|
|
2009-07-29 01:13:00 +02:00
|
|
|
for (i = 0; i < nitems(key_string_table); i++) {
|
|
|
|
if (key == key_string_table[i].key)
|
|
|
|
return (key_string_table[i].string);
|
|
|
|
}
|
|
|
|
|
2008-06-25 22:33:20 +02:00
|
|
|
if (key >= 32 && key <= 255) {
|
2007-10-04 02:18:59 +02:00
|
|
|
tmp[0] = key;
|
|
|
|
tmp[1] = '\0';
|
|
|
|
return (tmp);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
return (tmp);
|
|
|
|
}
|
|
|
|
|
2007-10-04 02:18:59 +02:00
|
|
|
return (NULL);
|
|
|
|
}
|