2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* 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"
|
|
|
|
|
2015-12-12 18:19:00 +00:00
|
|
|
static key_code key_string_search_table(const char *);
|
|
|
|
static key_code key_string_get_modifiers(const char **);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2016-10-10 21:29:23 +00:00
|
|
|
static const struct {
|
2010-04-21 21:17:33 +00:00
|
|
|
const char *string;
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code key;
|
2009-06-01 22:58:49 +00:00
|
|
|
} key_string_table[] = {
|
|
|
|
/* Function keys. */
|
|
|
|
{ "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 },
|
|
|
|
{ "F10", KEYC_F10 },
|
|
|
|
{ "F11", KEYC_F11 },
|
|
|
|
{ "F12", KEYC_F12 },
|
|
|
|
{ "IC", KEYC_IC },
|
|
|
|
{ "DC", KEYC_DC },
|
|
|
|
{ "Home", KEYC_HOME },
|
|
|
|
{ "End", KEYC_END },
|
|
|
|
{ "NPage", KEYC_NPAGE },
|
2011-10-23 10:19:26 +00:00
|
|
|
{ "PageDown", KEYC_NPAGE },
|
|
|
|
{ "PgDn", KEYC_NPAGE },
|
2009-06-01 22:58:49 +00:00
|
|
|
{ "PPage", KEYC_PPAGE },
|
2011-10-23 10:19:26 +00:00
|
|
|
{ "PageUp", KEYC_PPAGE },
|
|
|
|
{ "PgUp", KEYC_PPAGE },
|
2009-06-01 22:58:49 +00:00
|
|
|
{ "Tab", '\011' },
|
|
|
|
{ "BTab", KEYC_BTAB },
|
2009-10-04 08:50:05 +00:00
|
|
|
{ "Space", ' ' },
|
2009-07-26 21:42:08 +00:00
|
|
|
{ "BSpace", KEYC_BSPACE },
|
2009-07-28 09:18:01 +00:00
|
|
|
{ "Enter", '\r' },
|
|
|
|
{ "Escape", '\033' },
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
/* Arrow keys. */
|
|
|
|
{ "Up", KEYC_UP },
|
|
|
|
{ "Down", KEYC_DOWN },
|
|
|
|
{ "Left", KEYC_LEFT },
|
|
|
|
{ "Right", KEYC_RIGHT },
|
|
|
|
|
|
|
|
/* Numeric keypad. */
|
2009-10-26 13:13:33 +00: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 },
|
2015-04-19 21:34:21 +00:00
|
|
|
|
|
|
|
/* Mouse keys. */
|
|
|
|
KEYC_MOUSE_STRING(MOUSEDOWN1, MouseDown1),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEDOWN2, MouseDown2),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEDOWN3, MouseDown3),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEUP1, MouseUp1),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEUP2, MouseUp2),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEUP3, MouseUp3),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEDRAG1, MouseDrag1),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEDRAG2, MouseDrag2),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEDRAG3, MouseDrag3),
|
2016-03-18 07:28:27 +00:00
|
|
|
KEYC_MOUSE_STRING(MOUSEDRAGEND1, MouseDragEnd1),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEDRAGEND2, MouseDragEnd2),
|
|
|
|
KEYC_MOUSE_STRING(MOUSEDRAGEND3, MouseDragEnd3),
|
2015-04-19 21:34:21 +00:00
|
|
|
KEYC_MOUSE_STRING(WHEELUP, WheelUp),
|
|
|
|
KEYC_MOUSE_STRING(WHEELDOWN, WheelDown),
|
2016-10-11 09:30:36 +00:00
|
|
|
KEYC_MOUSE_STRING(DOUBLECLICK1, DoubleClick1),
|
|
|
|
KEYC_MOUSE_STRING(DOUBLECLICK2, DoubleClick2),
|
|
|
|
KEYC_MOUSE_STRING(DOUBLECLICK3, DoubleClick3),
|
|
|
|
KEYC_MOUSE_STRING(TRIPLECLICK1, TripleClick1),
|
|
|
|
KEYC_MOUSE_STRING(TRIPLECLICK2, TripleClick2),
|
|
|
|
KEYC_MOUSE_STRING(TRIPLECLICK3, TripleClick3),
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2009-11-10 17:24:43 +00:00
|
|
|
/* Find key string in table. */
|
2015-12-12 18:19:00 +00:00
|
|
|
static key_code
|
2009-06-01 22:58:49 +00:00
|
|
|
key_string_search_table(const char *string)
|
|
|
|
{
|
2017-06-23 15:36:52 +00:00
|
|
|
u_int i, user;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nitems(key_string_table); i++) {
|
|
|
|
if (strcasecmp(string, key_string_table[i].string) == 0)
|
|
|
|
return (key_string_table[i].key);
|
|
|
|
}
|
2017-06-23 15:36:52 +00:00
|
|
|
|
|
|
|
if (sscanf(string, "User%u", &user) == 1 && user < KEYC_NUSER)
|
|
|
|
return (KEYC_USER + user);
|
|
|
|
|
2015-12-12 18:19:00 +00:00
|
|
|
return (KEYC_UNKNOWN);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 21:17:33 +00:00
|
|
|
/* Find modifiers. */
|
2015-12-12 18:19:00 +00:00
|
|
|
static key_code
|
2010-04-21 21:17:33 +00:00
|
|
|
key_string_get_modifiers(const char **string)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code modifiers;
|
2010-04-21 21:17:33 +00:00
|
|
|
|
|
|
|
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;
|
2016-11-23 16:44:42 +00:00
|
|
|
default:
|
|
|
|
*string = NULL;
|
2017-01-05 09:07:15 +00:00
|
|
|
return (0);
|
2009-11-10 17:24:43 +00:00
|
|
|
}
|
2010-04-21 21:17:33 +00:00
|
|
|
*string += 2;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2010-04-21 21:17:33 +00:00
|
|
|
return (modifiers);
|
|
|
|
}
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2010-04-21 21:17:33 +00:00
|
|
|
/* Lookup a string and convert to a key value. */
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code
|
2010-04-21 21:17:33 +00:00
|
|
|
key_string_lookup_string(const char *string)
|
|
|
|
{
|
2013-03-22 10:30:04 +00:00
|
|
|
static const char *other = "!#()+,-.0123456789:;<=>?'\r\t";
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code key;
|
2016-05-26 14:49:48 +00:00
|
|
|
u_int u;
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code modifiers;
|
2015-11-12 22:04:37 +00:00
|
|
|
struct utf8_data ud;
|
2015-11-12 11:05:34 +00:00
|
|
|
u_int i;
|
2015-11-14 11:45:43 +00:00
|
|
|
enum utf8_state more;
|
2016-03-02 15:36:02 +00:00
|
|
|
wchar_t wc;
|
2012-03-04 20:40:54 +00:00
|
|
|
|
2015-12-12 18:19:00 +00:00
|
|
|
/* Is this no key? */
|
|
|
|
if (strcasecmp(string, "None") == 0)
|
|
|
|
return (KEYC_NONE);
|
|
|
|
|
2012-03-04 20:40:54 +00:00
|
|
|
/* Is this a hexadecimal value? */
|
|
|
|
if (string[0] == '0' && string[1] == 'x') {
|
2016-05-26 14:49:48 +00:00
|
|
|
if (sscanf(string + 2, "%x", &u) != 1)
|
|
|
|
return (KEYC_UNKNOWN);
|
|
|
|
if (u > 0x1fffff)
|
2015-12-12 18:19:00 +00:00
|
|
|
return (KEYC_UNKNOWN);
|
2012-03-04 20:40:54 +00:00
|
|
|
return (u);
|
|
|
|
}
|
2009-11-10 17:24:43 +00:00
|
|
|
|
2010-04-21 21:17:33 +00:00
|
|
|
/* Check for modifiers. */
|
|
|
|
modifiers = 0;
|
|
|
|
if (string[0] == '^' && string[1] != '\0') {
|
|
|
|
modifiers |= KEYC_CTRL;
|
|
|
|
string++;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
2010-04-21 21:17:33 +00:00
|
|
|
modifiers |= key_string_get_modifiers(&string);
|
2016-11-23 16:44:42 +00:00
|
|
|
if (string == NULL || string[0] == '\0')
|
2015-12-12 18:19:00 +00:00
|
|
|
return (KEYC_UNKNOWN);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2010-04-21 21:17:33 +00:00
|
|
|
/* Is this a standard ASCII key? */
|
2015-11-12 11:05:34 +00:00
|
|
|
if (string[1] == '\0' && (u_char)string[0] <= 127) {
|
|
|
|
key = (u_char)string[0];
|
|
|
|
if (key < 32 || key == 127)
|
2015-12-12 18:19:00 +00:00
|
|
|
return (KEYC_UNKNOWN);
|
2010-06-05 15:51:53 +00:00
|
|
|
} else {
|
2015-11-12 11:05:34 +00:00
|
|
|
/* Try as a UTF-8 key. */
|
2015-11-14 11:45:43 +00:00
|
|
|
if ((more = utf8_open(&ud, (u_char)*string)) == UTF8_MORE) {
|
2015-11-12 22:04:37 +00:00
|
|
|
if (strlen(string) != ud.size)
|
2015-12-12 18:19:00 +00:00
|
|
|
return (KEYC_UNKNOWN);
|
2015-11-12 22:04:37 +00:00
|
|
|
for (i = 1; i < ud.size; i++)
|
2015-11-14 10:56:31 +00:00
|
|
|
more = utf8_append(&ud, (u_char)string[i]);
|
2015-11-14 11:45:43 +00:00
|
|
|
if (more != UTF8_DONE)
|
2015-12-12 18:19:00 +00:00
|
|
|
return (KEYC_UNKNOWN);
|
2016-03-02 15:36:02 +00:00
|
|
|
if (utf8_combine(&ud, &wc) != UTF8_DONE)
|
|
|
|
return (KEYC_UNKNOWN);
|
|
|
|
return (wc | modifiers);
|
2015-11-12 11:05:34 +00:00
|
|
|
}
|
|
|
|
|
2010-06-05 15:51:53 +00:00
|
|
|
/* Otherwise look the key up in the table. */
|
|
|
|
key = key_string_search_table(string);
|
2015-12-12 18:19:00 +00:00
|
|
|
if (key == KEYC_UNKNOWN)
|
|
|
|
return (KEYC_UNKNOWN);
|
2010-06-05 15:51:53 +00:00
|
|
|
}
|
2010-04-21 21:17:33 +00:00
|
|
|
|
2010-06-05 15:51:53 +00:00
|
|
|
/* Convert the standard control keys. */
|
2013-03-22 10:30:04 +00:00
|
|
|
if (key < KEYC_BASE && (modifiers & KEYC_CTRL) && !strchr(other, key)) {
|
2010-06-05 15:51:53 +00: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
|
2015-12-12 18:19:00 +00:00
|
|
|
return (KEYC_UNKNOWN);
|
2010-06-05 15:51:53 +00:00
|
|
|
modifiers &= ~KEYC_CTRL;
|
2010-01-14 21:53:40 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 21:17:33 +00:00
|
|
|
return (key | modifiers);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2009-11-10 17:24:43 +00:00
|
|
|
/* Convert a key code into string format, with prefix if necessary. */
|
2009-06-01 22:58:49 +00:00
|
|
|
const char *
|
2015-11-12 11:05:34 +00:00
|
|
|
key_string_lookup_key(key_code key)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2017-02-22 09:01:32 +00:00
|
|
|
static char out[32];
|
2015-11-12 11:05:34 +00:00
|
|
|
char tmp[8];
|
|
|
|
u_int i;
|
2015-11-12 22:04:37 +00:00
|
|
|
struct utf8_data ud;
|
2016-04-25 17:05:53 +00:00
|
|
|
size_t off;
|
2010-04-21 21:17:33 +00:00
|
|
|
|
|
|
|
*out = '\0';
|
|
|
|
|
2012-01-21 08:40:09 +00:00
|
|
|
/* Handle no key. */
|
|
|
|
if (key == KEYC_NONE)
|
2015-12-12 18:19:00 +00:00
|
|
|
return ("None");
|
|
|
|
|
|
|
|
/* Handle special keys. */
|
|
|
|
if (key == KEYC_UNKNOWN)
|
|
|
|
return ("Unknown");
|
2017-01-11 22:36:07 +00:00
|
|
|
if (key == KEYC_FOCUS_IN)
|
|
|
|
return ("FocusIn");
|
|
|
|
if (key == KEYC_FOCUS_OUT)
|
|
|
|
return ("FocusOut");
|
2017-06-12 07:04:24 +00:00
|
|
|
if (key == KEYC_PASTE_START)
|
|
|
|
return ("PasteStart");
|
|
|
|
if (key == KEYC_PASTE_END)
|
|
|
|
return ("PasteEnd");
|
2015-04-19 21:34:21 +00:00
|
|
|
if (key == KEYC_MOUSE)
|
2015-12-12 18:19:00 +00:00
|
|
|
return ("Mouse");
|
2017-01-11 22:36:07 +00:00
|
|
|
if (key == KEYC_DRAGGING)
|
|
|
|
return ("Dragging");
|
2017-02-01 09:55:07 +00:00
|
|
|
if (key == KEYC_MOUSEMOVE_PANE)
|
|
|
|
return ("MouseMovePane");
|
|
|
|
if (key == KEYC_MOUSEMOVE_STATUS)
|
|
|
|
return ("MouseMoveStatus");
|
|
|
|
if (key == KEYC_MOUSEMOVE_BORDER)
|
|
|
|
return ("MouseMoveBorder");
|
2017-06-23 15:36:52 +00:00
|
|
|
if (key >= KEYC_USER && key < KEYC_USER + KEYC_NUSER) {
|
|
|
|
snprintf(out, sizeof out, "User%u", (u_int)(key - KEYC_USER));
|
|
|
|
return (out);
|
|
|
|
}
|
2012-01-21 08:40:09 +00:00
|
|
|
|
2010-06-06 19:00:13 +00: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-21 21:17:33 +00: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-06 19:00:13 +00:00
|
|
|
key &= KEYC_MASK_KEY;
|
2010-04-21 21:17:33 +00:00
|
|
|
|
|
|
|
/* Try the key against the string table. */
|
2009-07-28 09:18:01 +00:00
|
|
|
for (i = 0; i < nitems(key_string_table); i++) {
|
|
|
|
if (key == key_string_table[i].key)
|
2010-04-21 21:17:33 +00:00
|
|
|
break;
|
2009-07-28 09:18:01 +00:00
|
|
|
}
|
2010-04-21 21:17:33 +00:00
|
|
|
if (i != nitems(key_string_table)) {
|
|
|
|
strlcat(out, key_string_table[i].string, sizeof out);
|
|
|
|
return (out);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 11:05:34 +00:00
|
|
|
/* Is this a UTF-8 key? */
|
|
|
|
if (key > 127 && key < KEYC_BASE) {
|
2015-11-14 11:45:43 +00:00
|
|
|
if (utf8_split(key, &ud) == UTF8_DONE) {
|
2016-04-25 17:05:53 +00:00
|
|
|
off = strlen(out);
|
|
|
|
memcpy(out + off, ud.data, ud.size);
|
|
|
|
out[off + ud.size] = '\0';
|
2015-11-12 11:05:34 +00:00
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-21 21:17:33 +00:00
|
|
|
/* Invalid keys are errors. */
|
2015-10-26 22:03:04 +00:00
|
|
|
if (key == 127 || key > 255) {
|
2015-12-12 18:19:00 +00:00
|
|
|
snprintf(out, sizeof out, "Invalid#%llx", key);
|
2015-10-26 22:03:04 +00:00
|
|
|
return (out);
|
|
|
|
}
|
2010-04-21 21:17:33 +00:00
|
|
|
|
|
|
|
/* Check for standard or control key. */
|
2015-11-12 11:05:34 +00:00
|
|
|
if (key <= 32) {
|
2009-06-01 22:58:49 +00:00
|
|
|
if (key == 0 || key > 26)
|
2015-11-12 11:05:34 +00:00
|
|
|
xsnprintf(tmp, sizeof tmp, "C-%c", (int)(64 + key));
|
2009-06-01 22:58:49 +00:00
|
|
|
else
|
2015-11-12 11:05:34 +00:00
|
|
|
xsnprintf(tmp, sizeof tmp, "C-%c", (int)(96 + key));
|
2010-04-21 21:17:33 +00:00
|
|
|
} else if (key >= 32 && key <= 126) {
|
|
|
|
tmp[0] = key;
|
|
|
|
tmp[1] = '\0';
|
2011-01-23 11:04:25 +00:00
|
|
|
} else if (key >= 128)
|
2015-11-12 11:05:34 +00:00
|
|
|
xsnprintf(tmp, sizeof tmp, "\\%llo", key);
|
2011-01-23 11:04:25 +00:00
|
|
|
|
2010-04-21 21:17:33 +00:00
|
|
|
strlcat(out, tmp, sizeof out);
|
|
|
|
return (out);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|