2009-10-26 17:46:33 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-10-26 17:46:33 +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"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xterm-style function keys append one of the following values before the last
|
|
|
|
* character:
|
|
|
|
*
|
|
|
|
* 2 Shift
|
2009-12-03 22:50:09 +00:00
|
|
|
* 3 Alt
|
|
|
|
* 4 Shift + Alt
|
|
|
|
* 5 Ctrl
|
|
|
|
* 6 Shift + Ctrl
|
|
|
|
* 7 Alt + Ctrl
|
2009-10-26 17:46:33 +00:00
|
|
|
* 8 Shift + Alt + Ctrl
|
|
|
|
*
|
|
|
|
* Rather than parsing them, just match against a table.
|
|
|
|
*
|
2011-06-23 10:02:49 +00:00
|
|
|
* There are three forms for F1-F4 (\\033O_P and \\033O1;_P and \\033[1;_P).
|
|
|
|
* We accept any but always output the latter (it comes first in the table).
|
2009-10-26 17:46:33 +00:00
|
|
|
*/
|
|
|
|
|
2016-10-03 22:52:11 +00:00
|
|
|
static int xterm_keys_match(const char *, const char *, size_t, size_t *,
|
|
|
|
key_code *);
|
|
|
|
static int xterm_keys_modifiers(const char *, size_t, size_t *,
|
|
|
|
key_code *);
|
2009-10-26 17:46:33 +00:00
|
|
|
|
|
|
|
struct xterm_keys_entry {
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code key;
|
2009-10-26 17:46:33 +00:00
|
|
|
const char *template;
|
|
|
|
};
|
|
|
|
|
2017-01-25 14:36:08 +00:00
|
|
|
static const struct xterm_keys_entry xterm_keys_table[] = {
|
2009-10-26 17:46:33 +00:00
|
|
|
{ KEYC_F1, "\033[1;_P" },
|
2011-06-23 10:02:49 +00:00
|
|
|
{ KEYC_F1, "\033O1;_P" },
|
2009-11-05 19:35:16 +00:00
|
|
|
{ KEYC_F1, "\033O_P" },
|
2009-10-26 17:46:33 +00:00
|
|
|
{ KEYC_F2, "\033[1;_Q" },
|
2011-06-23 10:02:49 +00:00
|
|
|
{ KEYC_F2, "\033O1;_Q" },
|
2009-11-05 19:35:16 +00:00
|
|
|
{ KEYC_F2, "\033O_Q" },
|
2009-10-26 17:46:33 +00:00
|
|
|
{ KEYC_F3, "\033[1;_R" },
|
2011-06-23 10:02:49 +00:00
|
|
|
{ KEYC_F3, "\033O1;_R" },
|
2009-11-05 19:35:16 +00:00
|
|
|
{ KEYC_F3, "\033O_R" },
|
2009-10-26 17:46:33 +00:00
|
|
|
{ KEYC_F4, "\033[1;_S" },
|
2011-06-23 10:02:49 +00:00
|
|
|
{ KEYC_F4, "\033O1;_S" },
|
2009-11-05 19:35:16 +00:00
|
|
|
{ KEYC_F4, "\033O_S" },
|
2009-10-26 17:46:33 +00:00
|
|
|
{ KEYC_F5, "\033[15;_~" },
|
|
|
|
{ KEYC_F6, "\033[17;_~" },
|
|
|
|
{ KEYC_F7, "\033[18;_~" },
|
|
|
|
{ KEYC_F8, "\033[19;_~" },
|
|
|
|
{ KEYC_F9, "\033[20;_~" },
|
|
|
|
{ KEYC_F10, "\033[21;_~" },
|
|
|
|
{ KEYC_F11, "\033[23;_~" },
|
|
|
|
{ KEYC_F12, "\033[24;_~" },
|
|
|
|
{ KEYC_UP, "\033[1;_A" },
|
|
|
|
{ KEYC_DOWN, "\033[1;_B" },
|
|
|
|
{ KEYC_RIGHT, "\033[1;_C" },
|
|
|
|
{ KEYC_LEFT, "\033[1;_D" },
|
|
|
|
{ KEYC_HOME, "\033[1;_H" },
|
|
|
|
{ KEYC_END, "\033[1;_F" },
|
|
|
|
{ KEYC_PPAGE, "\033[5;_~" },
|
|
|
|
{ KEYC_NPAGE, "\033[6;_~" },
|
|
|
|
{ KEYC_IC, "\033[2;_~" },
|
|
|
|
{ KEYC_DC, "\033[3;_~" },
|
2013-03-22 10:30:04 +00:00
|
|
|
|
|
|
|
{ '!', "\033[27;_;33~" },
|
|
|
|
{ '#', "\033[27;_;35~" },
|
|
|
|
{ '(', "\033[27;_;40~" },
|
|
|
|
{ ')', "\033[27;_;41~" },
|
|
|
|
{ '+', "\033[27;_;43~" },
|
|
|
|
{ ',', "\033[27;_;44~" },
|
|
|
|
{ '-', "\033[27;_;45~" },
|
|
|
|
{ '.', "\033[27;_;46~" },
|
|
|
|
{ '0', "\033[27;_;48~" },
|
|
|
|
{ '1', "\033[27;_;49~" },
|
|
|
|
{ '2', "\033[27;_;50~" },
|
|
|
|
{ '3', "\033[27;_;51~" },
|
|
|
|
{ '4', "\033[27;_;52~" },
|
|
|
|
{ '5', "\033[27;_;53~" },
|
|
|
|
{ '6', "\033[27;_;54~" },
|
|
|
|
{ '7', "\033[27;_;55~" },
|
|
|
|
{ '8', "\033[27;_;56~" },
|
|
|
|
{ '9', "\033[27;_;57~" },
|
|
|
|
{ ':', "\033[27;_;58~" },
|
|
|
|
{ ';', "\033[27;_;59~" },
|
|
|
|
{ '<', "\033[27;_;60~" },
|
|
|
|
{ '=', "\033[27;_;61~" },
|
|
|
|
{ '>', "\033[27;_;62~" },
|
|
|
|
{ '?', "\033[27;_;63~" },
|
|
|
|
{ '\'', "\033[27;_;39~" },
|
|
|
|
{ '\r', "\033[27;_;13~" },
|
|
|
|
{ '\t', "\033[27;_;9~" },
|
2009-10-26 17:46:33 +00:00
|
|
|
};
|
|
|
|
|
2009-12-03 22:50:09 +00:00
|
|
|
/*
|
2009-11-30 16:44:03 +00:00
|
|
|
* Match key against buffer, treating _ as a wildcard. Return -1 for no match,
|
|
|
|
* 0 for match, 1 if the end of the buffer is reached (need more data).
|
|
|
|
*/
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2014-05-27 13:04:42 +00:00
|
|
|
xterm_keys_match(const char *template, const char *buf, size_t len,
|
2015-11-12 11:05:34 +00:00
|
|
|
size_t *size, key_code *modifiers)
|
2009-10-26 17:46:33 +00:00
|
|
|
{
|
|
|
|
size_t pos;
|
2014-05-27 13:04:42 +00:00
|
|
|
int retval;
|
2009-10-26 17:46:33 +00:00
|
|
|
|
2015-08-30 15:43:40 +00:00
|
|
|
*modifiers = 0;
|
|
|
|
|
2009-11-30 16:44:03 +00:00
|
|
|
if (len == 0)
|
2009-10-26 17:46:33 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
do {
|
2014-05-27 13:04:42 +00:00
|
|
|
if (*template == '_') {
|
|
|
|
retval = xterm_keys_modifiers(buf, len, &pos,
|
|
|
|
modifiers);
|
|
|
|
if (retval != 0)
|
|
|
|
return (retval);
|
2014-02-10 11:20:41 +00:00
|
|
|
continue;
|
2014-05-27 13:04:42 +00:00
|
|
|
}
|
2014-02-10 11:20:41 +00:00
|
|
|
if (buf[pos] != *template)
|
2009-11-30 16:44:03 +00:00
|
|
|
return (-1);
|
2014-05-27 13:04:42 +00:00
|
|
|
pos++;
|
|
|
|
} while (*++template != '\0' && pos != len);
|
2009-10-26 17:46:33 +00:00
|
|
|
|
2009-11-30 16:44:03 +00:00
|
|
|
if (*template != '\0') /* partial */
|
|
|
|
return (1);
|
|
|
|
|
2014-05-27 13:04:42 +00:00
|
|
|
*size = pos;
|
2009-11-30 16:44:03 +00:00
|
|
|
return (0);
|
2009-10-26 17:46:33 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 13:04:42 +00:00
|
|
|
/* Find modifiers from buffer. */
|
2016-10-03 22:52:11 +00:00
|
|
|
static int
|
2015-11-12 11:05:34 +00:00
|
|
|
xterm_keys_modifiers(const char *buf, size_t len, size_t *pos,
|
|
|
|
key_code *modifiers)
|
2009-10-26 17:46:33 +00:00
|
|
|
{
|
2014-05-27 13:04:42 +00:00
|
|
|
u_int flags;
|
2009-10-26 17:46:33 +00:00
|
|
|
|
2014-05-27 13:04:42 +00:00
|
|
|
if (len - *pos < 2)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
if (buf[*pos] < '0' || buf[*pos] > '9')
|
|
|
|
return (-1);
|
|
|
|
flags = buf[(*pos)++] - '0';
|
|
|
|
if (buf[*pos] >= '0' && buf[*pos] <= '9')
|
|
|
|
flags = (flags * 10) + (buf[(*pos)++] - '0');
|
|
|
|
flags -= 1;
|
|
|
|
|
|
|
|
*modifiers = 0;
|
|
|
|
if (flags & 1)
|
|
|
|
*modifiers |= KEYC_SHIFT;
|
|
|
|
if (flags & 2)
|
|
|
|
*modifiers |= KEYC_ESCAPE;
|
|
|
|
if (flags & 4)
|
|
|
|
*modifiers |= KEYC_CTRL;
|
|
|
|
if (flags & 8)
|
|
|
|
*modifiers |= KEYC_ESCAPE;
|
|
|
|
return (0);
|
2009-10-26 17:46:33 +00:00
|
|
|
}
|
|
|
|
|
2009-11-30 16:44:03 +00:00
|
|
|
/*
|
|
|
|
* Lookup key from a buffer against the table. Returns 0 for found (and the
|
|
|
|
* key), -1 for not found, 1 for partial match.
|
|
|
|
*/
|
2009-10-26 17:46:33 +00:00
|
|
|
int
|
2015-11-12 11:05:34 +00:00
|
|
|
xterm_keys_find(const char *buf, size_t len, size_t *size, key_code *key)
|
2009-10-26 17:46:33 +00:00
|
|
|
{
|
2011-01-01 03:43:20 +00:00
|
|
|
const struct xterm_keys_entry *entry;
|
2015-11-12 11:05:34 +00:00
|
|
|
u_int i;
|
2014-05-27 13:04:42 +00:00
|
|
|
int matched;
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code modifiers;
|
2009-10-26 17:46:33 +00:00
|
|
|
|
2017-01-25 14:36:08 +00:00
|
|
|
for (i = 0; i < nitems(xterm_keys_table); i++) {
|
|
|
|
entry = &xterm_keys_table[i];
|
2014-05-27 13:04:42 +00:00
|
|
|
|
|
|
|
matched = xterm_keys_match(entry->template, buf, len, size,
|
|
|
|
&modifiers);
|
|
|
|
if (matched == -1)
|
|
|
|
continue;
|
|
|
|
if (matched == 0)
|
2017-05-07 21:25:59 +00:00
|
|
|
*key = (entry->key|modifiers|KEYC_XTERM);
|
2014-05-27 13:04:42 +00:00
|
|
|
return (matched);
|
2009-10-26 17:46:33 +00:00
|
|
|
}
|
2009-11-30 16:44:03 +00:00
|
|
|
return (-1);
|
2009-10-26 17:46:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup a key number from the table. */
|
|
|
|
char *
|
2017-01-25 14:36:08 +00:00
|
|
|
xterm_keys_lookup(key_code key)
|
2009-10-26 17:46:33 +00:00
|
|
|
{
|
2017-01-25 14:36:08 +00:00
|
|
|
const struct xterm_keys_entry *entry;
|
|
|
|
u_int i;
|
2015-11-12 11:05:34 +00:00
|
|
|
key_code modifiers;
|
2011-01-01 03:43:20 +00:00
|
|
|
char *out;
|
2009-10-26 17:46:33 +00:00
|
|
|
|
2010-09-01 21:11:14 +00:00
|
|
|
modifiers = 1;
|
|
|
|
if (key & KEYC_SHIFT)
|
|
|
|
modifiers += 1;
|
|
|
|
if (key & KEYC_ESCAPE)
|
|
|
|
modifiers += 2;
|
|
|
|
if (key & KEYC_CTRL)
|
|
|
|
modifiers += 4;
|
2009-10-26 17:46:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the key has no modifiers, return NULL and let it fall through to
|
|
|
|
* the normal lookup.
|
|
|
|
*/
|
2017-01-25 14:36:08 +00:00
|
|
|
if (modifiers == 1)
|
|
|
|
return (NULL);
|
2009-10-26 17:46:33 +00:00
|
|
|
|
2017-05-07 21:25:59 +00:00
|
|
|
/*
|
|
|
|
* If this has the escape modifier, but was not originally an xterm
|
|
|
|
* key, it may be a genuine escape + key. So don't pass it through as
|
|
|
|
* an xterm key or programs like vi may be confused.
|
|
|
|
*/
|
|
|
|
if ((key & (KEYC_ESCAPE|KEYC_XTERM)) == KEYC_ESCAPE)
|
|
|
|
return (NULL);
|
|
|
|
|
2009-10-26 17:46:33 +00:00
|
|
|
/* Otherwise, find the key in the table. */
|
2017-05-07 21:25:59 +00:00
|
|
|
key &= KEYC_MASK_KEY;
|
2017-01-25 14:36:08 +00:00
|
|
|
for (i = 0; i < nitems(xterm_keys_table); i++) {
|
|
|
|
entry = &xterm_keys_table[i];
|
2009-10-26 17:46:33 +00:00
|
|
|
if (key == entry->key)
|
|
|
|
break;
|
|
|
|
}
|
2017-01-25 14:36:08 +00:00
|
|
|
if (i == nitems(xterm_keys_table))
|
2009-10-26 17:46:33 +00:00
|
|
|
return (NULL);
|
2009-12-03 22:50:09 +00:00
|
|
|
|
2009-10-26 17:46:33 +00:00
|
|
|
/* Copy the template and replace the modifier. */
|
|
|
|
out = xstrdup(entry->template);
|
2017-01-25 14:36:08 +00:00
|
|
|
out[strcspn(out, "_")] = '0' + modifiers;
|
2009-10-26 17:46:33 +00:00
|
|
|
return (out);
|
|
|
|
}
|