2008-02-07 01:58:41 +01:00
|
|
|
#include "termkey.h"
|
2008-10-07 00:27:19 +02:00
|
|
|
#include "termkey-internal.h"
|
2008-02-07 01:58:41 +01:00
|
|
|
|
2013-08-26 02:32:35 +02:00
|
|
|
#include <ctype.h>
|
2008-02-09 20:01:49 +01:00
|
|
|
#include <errno.h>
|
2008-02-10 21:22:33 +01:00
|
|
|
#include <poll.h>
|
2008-02-07 01:58:41 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2008-11-16 13:40:14 +01:00
|
|
|
void termkey_check_version(int major, int minor)
|
|
|
|
{
|
|
|
|
if(major != TERMKEY_VERSION_MAJOR) {
|
|
|
|
fprintf(stderr, "libtermkey major version mismatch; %d (wants) != %d (library)\n",
|
|
|
|
major, TERMKEY_VERSION_MAJOR);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(minor > TERMKEY_VERSION_MINOR) {
|
|
|
|
fprintf(stderr, "libtermkey minor version mismatch; %d (wants) > %d (library)\n",
|
|
|
|
minor, TERMKEY_VERSION_MINOR);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Happy
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static struct TermKeyDriver *drivers[] = {
|
2008-10-09 21:23:45 +02:00
|
|
|
&termkey_driver_ti,
|
2008-11-09 21:04:14 +01:00
|
|
|
&termkey_driver_csi,
|
2008-10-07 04:17:06 +02:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2008-10-07 23:22:59 +02:00
|
|
|
// Forwards for the "protected" methods
|
2009-07-15 21:40:44 +02:00
|
|
|
// static void eat_bytes(TermKey *tk, size_t count);
|
|
|
|
static void emit_codepoint(TermKey *tk, long codepoint, TermKeyKey *key);
|
|
|
|
static TermKeyResult peekkey_simple(TermKey *tk, TermKeyKey *key, int force, size_t *nbytes);
|
2009-11-26 01:27:58 +01:00
|
|
|
static TermKeyResult peekkey_mouse(TermKey *tk, TermKeyKey *key, size_t *nbytes);
|
2008-10-08 01:12:41 +02:00
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static TermKeySym register_c0(TermKey *tk, TermKeySym sym, unsigned char ctrl, const char *name);
|
|
|
|
static TermKeySym register_c0_full(TermKey *tk, TermKeySym sym, int modifier_set, int modifier_mask, unsigned char ctrl, const char *name);
|
2008-10-07 23:22:59 +02:00
|
|
|
|
2008-10-08 02:52:53 +02:00
|
|
|
static struct {
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeySym sym;
|
2008-10-08 02:52:53 +02:00
|
|
|
const char *name;
|
|
|
|
} keynames[] = {
|
|
|
|
{ TERMKEY_SYM_NONE, "NONE" },
|
|
|
|
{ TERMKEY_SYM_BACKSPACE, "Backspace" },
|
|
|
|
{ TERMKEY_SYM_TAB, "Tab" },
|
|
|
|
{ TERMKEY_SYM_ENTER, "Enter" },
|
|
|
|
{ TERMKEY_SYM_ESCAPE, "Escape" },
|
|
|
|
{ TERMKEY_SYM_SPACE, "Space" },
|
|
|
|
{ TERMKEY_SYM_DEL, "DEL" },
|
2008-10-08 03:00:22 +02:00
|
|
|
{ TERMKEY_SYM_UP, "Up" },
|
|
|
|
{ TERMKEY_SYM_DOWN, "Down" },
|
|
|
|
{ TERMKEY_SYM_LEFT, "Left" },
|
|
|
|
{ TERMKEY_SYM_RIGHT, "Right" },
|
|
|
|
{ TERMKEY_SYM_BEGIN, "Begin" },
|
|
|
|
{ TERMKEY_SYM_FIND, "Find" },
|
|
|
|
{ TERMKEY_SYM_INSERT, "Insert" },
|
|
|
|
{ TERMKEY_SYM_DELETE, "Delete" },
|
|
|
|
{ TERMKEY_SYM_SELECT, "Select" },
|
|
|
|
{ TERMKEY_SYM_PAGEUP, "PageUp" },
|
|
|
|
{ TERMKEY_SYM_PAGEDOWN, "PageDown" },
|
|
|
|
{ TERMKEY_SYM_HOME, "Home" },
|
|
|
|
{ TERMKEY_SYM_END, "End" },
|
2008-10-10 02:11:24 +02:00
|
|
|
{ TERMKEY_SYM_CANCEL, "Cancel" },
|
|
|
|
{ TERMKEY_SYM_CLEAR, "Clear" },
|
|
|
|
{ TERMKEY_SYM_CLOSE, "Close" },
|
|
|
|
{ TERMKEY_SYM_COMMAND, "Command" },
|
|
|
|
{ TERMKEY_SYM_COPY, "Copy" },
|
|
|
|
{ TERMKEY_SYM_EXIT, "Exit" },
|
|
|
|
{ TERMKEY_SYM_HELP, "Help" },
|
|
|
|
{ TERMKEY_SYM_MARK, "Mark" },
|
|
|
|
{ TERMKEY_SYM_MESSAGE, "Message" },
|
|
|
|
{ TERMKEY_SYM_MOVE, "Move" },
|
|
|
|
{ TERMKEY_SYM_OPEN, "Open" },
|
|
|
|
{ TERMKEY_SYM_OPTIONS, "Options" },
|
|
|
|
{ TERMKEY_SYM_PRINT, "Print" },
|
|
|
|
{ TERMKEY_SYM_REDO, "Redo" },
|
|
|
|
{ TERMKEY_SYM_REFERENCE, "Reference" },
|
|
|
|
{ TERMKEY_SYM_REFRESH, "Refresh" },
|
|
|
|
{ TERMKEY_SYM_REPLACE, "Replace" },
|
|
|
|
{ TERMKEY_SYM_RESTART, "Restart" },
|
|
|
|
{ TERMKEY_SYM_RESUME, "Resume" },
|
|
|
|
{ TERMKEY_SYM_SAVE, "Save" },
|
|
|
|
{ TERMKEY_SYM_SUSPEND, "Suspend" },
|
|
|
|
{ TERMKEY_SYM_UNDO, "Undo" },
|
2008-10-08 03:00:22 +02:00
|
|
|
{ TERMKEY_SYM_KP0, "KP0" },
|
|
|
|
{ TERMKEY_SYM_KP1, "KP1" },
|
|
|
|
{ TERMKEY_SYM_KP2, "KP2" },
|
|
|
|
{ TERMKEY_SYM_KP3, "KP3" },
|
|
|
|
{ TERMKEY_SYM_KP4, "KP4" },
|
|
|
|
{ TERMKEY_SYM_KP5, "KP5" },
|
|
|
|
{ TERMKEY_SYM_KP6, "KP6" },
|
|
|
|
{ TERMKEY_SYM_KP7, "KP7" },
|
|
|
|
{ TERMKEY_SYM_KP8, "KP8" },
|
|
|
|
{ TERMKEY_SYM_KP9, "KP9" },
|
|
|
|
{ TERMKEY_SYM_KPENTER, "KPEnter" },
|
|
|
|
{ TERMKEY_SYM_KPPLUS, "KPPlus" },
|
|
|
|
{ TERMKEY_SYM_KPMINUS, "KPMinus" },
|
|
|
|
{ TERMKEY_SYM_KPMULT, "KPMult" },
|
|
|
|
{ TERMKEY_SYM_KPDIV, "KPDiv" },
|
|
|
|
{ TERMKEY_SYM_KPCOMMA, "KPComma" },
|
|
|
|
{ TERMKEY_SYM_KPPERIOD, "KPPeriod" },
|
|
|
|
{ TERMKEY_SYM_KPEQUALS, "KPEquals" },
|
2008-10-08 02:52:53 +02:00
|
|
|
{ 0, NULL },
|
|
|
|
};
|
|
|
|
|
2009-11-27 15:50:58 +01:00
|
|
|
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
/* Some internal deubgging functions */
|
|
|
|
|
|
|
|
static void print_buffer(TermKey *tk)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < tk->buffcount && i < 20; i++)
|
|
|
|
fprintf(stderr, "%02x ", CHARAT(i));
|
|
|
|
if(tk->buffcount > 20)
|
|
|
|
fprintf(stderr, "...");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_key(TermKey *tk, TermKeyKey *key)
|
|
|
|
{
|
|
|
|
switch(key->type) {
|
|
|
|
case TERMKEY_TYPE_UNICODE:
|
|
|
|
fprintf(stderr, "Unicode codepoint=U+%04lx utf8='%s'", key->code.codepoint, key->utf8);
|
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_FUNCTION:
|
|
|
|
fprintf(stderr, "Function F%d", key->code.number);
|
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_KEYSYM:
|
|
|
|
fprintf(stderr, "Keysym sym=%d(%s)", key->code.sym, termkey_get_keyname(tk, key->code.sym));
|
|
|
|
break;
|
2009-11-27 15:56:38 +01:00
|
|
|
case TERMKEY_TYPE_MOUSE:
|
|
|
|
{
|
|
|
|
TermKeyMouseEvent ev;
|
|
|
|
int button, line, col;
|
|
|
|
termkey_interpret_mouse(tk, key, &ev, &button, &line, &col);
|
|
|
|
fprintf(stderr, "Mouse ev=%d button=%d pos=(%d,%d)\n", ev, button, line, col);
|
|
|
|
}
|
|
|
|
break;
|
2012-12-06 03:09:03 +01:00
|
|
|
case TERMKEY_TYPE_POSITION:
|
|
|
|
{
|
|
|
|
int line, col;
|
|
|
|
termkey_interpret_position(tk, key, &line, &col);
|
|
|
|
fprintf(stderr, "Position report pos=(%d,%d)\n", line, col);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_MODEREPORT:
|
|
|
|
{
|
|
|
|
int initial, mode, value;
|
|
|
|
termkey_interpret_modereport(tk, key, &initial, &mode, &value);
|
|
|
|
fprintf(stderr, "Mode report mode=%s %d val=%d\n", initial == '?' ? "DEC" : "ANSI", mode, value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_UNKNOWN_CSI:
|
|
|
|
fprintf(stderr, "unknown CSI\n");
|
|
|
|
break;
|
2009-11-27 15:50:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int m = key->modifiers;
|
|
|
|
fprintf(stderr, " mod=%s%s%s+%02x",
|
|
|
|
(m & TERMKEY_KEYMOD_CTRL ? "C" : ""),
|
|
|
|
(m & TERMKEY_KEYMOD_ALT ? "A" : ""),
|
|
|
|
(m & TERMKEY_KEYMOD_SHIFT ? "S" : ""),
|
|
|
|
m & ~(TERMKEY_KEYMOD_CTRL|TERMKEY_KEYMOD_ALT|TERMKEY_KEYMOD_SHIFT));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *res2str(TermKeyResult res)
|
|
|
|
{
|
2011-09-05 14:58:15 +02:00
|
|
|
static char errorbuffer[256];
|
|
|
|
|
2009-11-27 15:50:58 +01:00
|
|
|
switch(res) {
|
|
|
|
case TERMKEY_RES_KEY:
|
|
|
|
return "TERMKEY_RES_KEY";
|
|
|
|
case TERMKEY_RES_EOF:
|
|
|
|
return "TERMKEY_RES_EOF";
|
|
|
|
case TERMKEY_RES_AGAIN:
|
|
|
|
return "TERMKEY_RES_AGAIN";
|
|
|
|
case TERMKEY_RES_NONE:
|
|
|
|
return "TERMKEY_RES_NONE";
|
2011-09-05 14:58:15 +02:00
|
|
|
case TERMKEY_RES_ERROR:
|
|
|
|
snprintf(errorbuffer, sizeof errorbuffer, "TERMKEY_RES_ERROR(errno=%d)\n", errno);
|
|
|
|
return (const char*)errorbuffer;
|
2009-11-27 15:50:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-08-26 03:23:19 +02:00
|
|
|
/* Similar to snprintf(str, size, "%s", src) except it turns CamelCase into
|
|
|
|
* space separated values
|
|
|
|
*/
|
|
|
|
static int snprint_cameltospaces(char *str, size_t size, const char *src)
|
|
|
|
{
|
|
|
|
int prev_lower = 0;
|
|
|
|
size_t l = 0;
|
2013-08-26 19:46:01 +02:00
|
|
|
while(*src && l < size - 1) {
|
2013-08-26 03:23:19 +02:00
|
|
|
if(isupper(*src) && prev_lower) {
|
|
|
|
if(str)
|
|
|
|
str[l++] = ' ';
|
2013-08-26 19:46:01 +02:00
|
|
|
if(l >= size - 1)
|
|
|
|
break;
|
2013-08-26 03:23:19 +02:00
|
|
|
}
|
|
|
|
prev_lower = islower(*src);
|
|
|
|
str[l++] = tolower(*src++);
|
|
|
|
}
|
|
|
|
str[l] = 0;
|
2013-08-26 19:46:01 +02:00
|
|
|
/* For consistency with snprintf, return the number of bytes that would have
|
|
|
|
* been written, excluding '\0' */
|
|
|
|
while(*src) {
|
|
|
|
if(isupper(*src) && prev_lower) {
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
prev_lower = islower(*src);
|
|
|
|
src++; l++;
|
|
|
|
}
|
2013-08-26 03:23:19 +02:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Similar to strcmp(str, strcamel, n) except that:
|
|
|
|
* it compares CamelCase in strcamel with space separated values in str;
|
|
|
|
* it takes char**s and updates them
|
2013-08-26 03:30:43 +02:00
|
|
|
* n counts bytes of strcamel, not str
|
2013-08-26 03:23:19 +02:00
|
|
|
*/
|
|
|
|
static int strpncmp_camel(const char **strp, const char **strcamelp, size_t n)
|
|
|
|
{
|
|
|
|
const char *str = *strp, *strcamel = *strcamelp;
|
|
|
|
int prev_lower = 0;
|
|
|
|
|
|
|
|
for( ; (*str || *strcamel) && n; n--) {
|
|
|
|
char b = tolower(*strcamel);
|
|
|
|
if(isupper(*strcamel) && prev_lower) {
|
|
|
|
if(*str != ' ')
|
|
|
|
break;
|
|
|
|
str++;
|
|
|
|
if(*str != b)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if(*str != b)
|
|
|
|
break;
|
|
|
|
|
|
|
|
prev_lower = islower(*strcamel);
|
|
|
|
|
|
|
|
str++;
|
|
|
|
strcamel++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*strp = str;
|
|
|
|
*strcamelp = strcamel;
|
|
|
|
return *str - *strcamel;
|
|
|
|
}
|
|
|
|
|
2012-01-26 11:06:05 +01:00
|
|
|
static TermKey *termkey_alloc(void)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2012-01-26 11:06:05 +01:00
|
|
|
TermKey *tk = malloc(sizeof(TermKey));
|
|
|
|
if(!tk)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Default all the object fields but don't allocate anything */
|
|
|
|
|
|
|
|
tk->fd = -1;
|
|
|
|
tk->flags = 0;
|
|
|
|
tk->canonflags = 0;
|
|
|
|
|
|
|
|
tk->buffer = NULL;
|
|
|
|
tk->buffstart = 0;
|
|
|
|
tk->buffcount = 0;
|
|
|
|
tk->buffsize = 256; /* bytes */
|
2012-11-30 15:35:23 +01:00
|
|
|
tk->hightide = 0;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
|
|
|
tk->restore_termios_valid = 0;
|
|
|
|
|
|
|
|
tk->waittime = 50; /* msec */
|
|
|
|
|
|
|
|
tk->is_closed = 0;
|
2012-03-08 22:15:25 +01:00
|
|
|
tk->is_started = 0;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
|
|
|
tk->nkeynames = 64;
|
|
|
|
tk->keynames = NULL;
|
|
|
|
|
|
|
|
for(int i = 0; i < 32; i++)
|
|
|
|
tk->c0[i].sym = TERMKEY_SYM_NONE;
|
|
|
|
|
|
|
|
tk->drivers = NULL;
|
|
|
|
|
|
|
|
tk->method.emit_codepoint = &emit_codepoint;
|
|
|
|
tk->method.peekkey_simple = &peekkey_simple;
|
|
|
|
tk->method.peekkey_mouse = &peekkey_mouse;
|
|
|
|
|
|
|
|
return tk;
|
|
|
|
}
|
|
|
|
|
2012-01-26 11:13:03 +01:00
|
|
|
static int termkey_init(TermKey *tk, const char *term)
|
2012-01-26 11:06:05 +01:00
|
|
|
{
|
|
|
|
tk->buffer = malloc(tk->buffsize);
|
2008-10-10 00:19:10 +02:00
|
|
|
if(!tk->buffer)
|
2012-01-26 11:13:03 +01:00
|
|
|
return 0;
|
2008-02-10 18:34:05 +01:00
|
|
|
|
|
|
|
tk->keynames = malloc(sizeof(tk->keynames[0]) * tk->nkeynames);
|
2008-10-10 00:19:10 +02:00
|
|
|
if(!tk->keynames)
|
|
|
|
goto abort_free_buffer;
|
2008-02-10 18:34:05 +01:00
|
|
|
|
2008-10-07 00:27:19 +02:00
|
|
|
int i;
|
2008-02-10 18:34:05 +01:00
|
|
|
for(i = 0; i < tk->nkeynames; i++)
|
|
|
|
tk->keynames[i] = NULL;
|
|
|
|
|
2008-10-08 02:52:53 +02:00
|
|
|
for(i = 0; keynames[i].name; i++)
|
2012-01-20 19:03:08 +01:00
|
|
|
if(termkey_register_keyname(tk, keynames[i].sym, keynames[i].name) == -1)
|
|
|
|
goto abort_free_keynames;
|
2008-10-08 02:52:53 +02:00
|
|
|
|
|
|
|
register_c0(tk, TERMKEY_SYM_BACKSPACE, 0x08, NULL);
|
|
|
|
register_c0(tk, TERMKEY_SYM_TAB, 0x09, NULL);
|
|
|
|
register_c0(tk, TERMKEY_SYM_ENTER, 0x0d, NULL);
|
|
|
|
register_c0(tk, TERMKEY_SYM_ESCAPE, 0x1b, NULL);
|
2008-10-07 23:22:59 +02:00
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
struct TermKeyDriverNode *tail = NULL;
|
2008-11-09 20:58:11 +01:00
|
|
|
|
2008-10-07 04:17:06 +02:00
|
|
|
for(i = 0; drivers[i]; i++) {
|
2008-11-09 20:58:11 +01:00
|
|
|
void *info = (*drivers[i]->new_driver)(tk, term);
|
|
|
|
if(!info)
|
2008-10-07 04:17:06 +02:00
|
|
|
continue;
|
|
|
|
|
2008-11-09 20:58:11 +01:00
|
|
|
#ifdef DEBUG
|
2011-09-05 15:14:09 +02:00
|
|
|
fprintf(stderr, "Loading the %s driver...\n", drivers[i]->name);
|
2008-11-09 20:58:11 +01:00
|
|
|
#endif
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
struct TermKeyDriverNode *thisdrv = malloc(sizeof(*thisdrv));
|
2008-11-09 20:58:11 +01:00
|
|
|
if(!thisdrv)
|
|
|
|
goto abort_free_drivers;
|
|
|
|
|
|
|
|
thisdrv->driver = drivers[i];
|
|
|
|
thisdrv->info = info;
|
|
|
|
thisdrv->next = NULL;
|
|
|
|
|
|
|
|
if(!tail)
|
|
|
|
tk->drivers = thisdrv;
|
|
|
|
else
|
|
|
|
tail->next = thisdrv;
|
|
|
|
|
|
|
|
tail = thisdrv;
|
2011-09-05 15:14:09 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Loaded %s driver\n", drivers[i]->name);
|
|
|
|
#endif
|
2008-10-07 04:17:06 +02:00
|
|
|
}
|
2008-10-07 00:36:14 +02:00
|
|
|
|
2008-11-09 20:58:11 +01:00
|
|
|
if(!tk->drivers) {
|
2012-01-29 17:13:38 +01:00
|
|
|
errno = ENOENT;
|
2008-10-10 00:19:10 +02:00
|
|
|
goto abort_free_keynames;
|
2008-10-07 04:17:06 +02:00
|
|
|
}
|
2008-02-10 18:34:05 +01:00
|
|
|
|
2012-01-26 11:13:03 +01:00
|
|
|
return 1;
|
2008-10-10 00:19:10 +02:00
|
|
|
|
2008-11-09 20:58:11 +01:00
|
|
|
abort_free_drivers:
|
2012-03-08 22:15:25 +01:00
|
|
|
for(struct TermKeyDriverNode *p = tk->drivers; p; ) {
|
2008-11-09 20:58:11 +01:00
|
|
|
(*p->driver->free_driver)(p->info);
|
2009-07-15 21:40:44 +02:00
|
|
|
struct TermKeyDriverNode *next = p->next;
|
2008-11-09 20:58:11 +01:00
|
|
|
free(p);
|
|
|
|
p = next;
|
|
|
|
}
|
|
|
|
|
2008-10-10 00:19:10 +02:00
|
|
|
abort_free_keynames:
|
|
|
|
free(tk->keynames);
|
|
|
|
|
|
|
|
abort_free_buffer:
|
|
|
|
free(tk->buffer);
|
|
|
|
|
2012-01-26 11:13:03 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
TermKey *termkey_new(int fd, int flags)
|
|
|
|
{
|
|
|
|
TermKey *tk = termkey_alloc();
|
|
|
|
if(!tk)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tk->fd = fd;
|
|
|
|
|
|
|
|
if(!(flags & (TERMKEY_FLAG_RAW|TERMKEY_FLAG_UTF8))) {
|
|
|
|
char *e;
|
|
|
|
|
2014-01-05 19:23:46 +01:00
|
|
|
if(((e = getenv("LANG")) || (e = getenv("LC_MESSAGES")) || (e = getenv("LC_ALL"))) &&
|
2014-01-24 00:57:19 +01:00
|
|
|
(strstr(e, ".UTF-8") || strstr(e, ".utf8")))
|
2012-01-26 11:13:03 +01:00
|
|
|
flags |= TERMKEY_FLAG_UTF8;
|
|
|
|
else
|
|
|
|
flags |= TERMKEY_FLAG_RAW;
|
|
|
|
}
|
|
|
|
|
|
|
|
termkey_set_flags(tk, flags);
|
|
|
|
|
|
|
|
const char *term = getenv("TERM");
|
|
|
|
|
2012-03-08 22:15:25 +01:00
|
|
|
if(!termkey_init(tk, term))
|
|
|
|
goto abort;
|
|
|
|
|
|
|
|
if(!termkey_start(tk))
|
|
|
|
goto abort;
|
2012-01-26 13:55:16 +01:00
|
|
|
|
|
|
|
return tk;
|
2012-03-08 22:15:25 +01:00
|
|
|
|
|
|
|
abort:
|
|
|
|
free(tk);
|
|
|
|
return NULL;
|
2012-01-26 13:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
TermKey *termkey_new_abstract(const char *term, int flags)
|
|
|
|
{
|
|
|
|
TermKey *tk = termkey_alloc();
|
|
|
|
if(!tk)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tk->fd = -1;
|
|
|
|
|
|
|
|
termkey_set_flags(tk, flags);
|
|
|
|
|
|
|
|
if(!termkey_init(tk, term)) {
|
|
|
|
free(tk);
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-01-26 11:13:03 +01:00
|
|
|
|
2012-03-08 22:15:25 +01:00
|
|
|
termkey_start(tk);
|
|
|
|
|
2012-01-26 11:13:03 +01:00
|
|
|
return tk;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
void termkey_free(TermKey *tk)
|
2008-02-23 21:07:48 +01:00
|
|
|
{
|
|
|
|
free(tk->buffer); tk->buffer = NULL;
|
|
|
|
free(tk->keynames); tk->keynames = NULL;
|
2008-10-07 00:27:19 +02:00
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
struct TermKeyDriverNode *p;
|
2008-11-09 20:58:11 +01:00
|
|
|
for(p = tk->drivers; p; ) {
|
|
|
|
(*p->driver->free_driver)(p->info);
|
2009-07-15 21:40:44 +02:00
|
|
|
struct TermKeyDriverNode *next = p->next;
|
2008-11-09 20:58:11 +01:00
|
|
|
free(p);
|
|
|
|
p = next;
|
|
|
|
}
|
2008-02-23 21:07:48 +01:00
|
|
|
|
|
|
|
free(tk);
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
void termkey_destroy(TermKey *tk)
|
2008-02-23 21:26:04 +01:00
|
|
|
{
|
2012-03-08 22:15:25 +01:00
|
|
|
if(tk->is_started)
|
|
|
|
termkey_stop(tk);
|
|
|
|
|
|
|
|
termkey_free(tk);
|
|
|
|
}
|
|
|
|
|
|
|
|
int termkey_start(TermKey *tk)
|
|
|
|
{
|
|
|
|
if(tk->is_started)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if(tk->fd != -1 && !(tk->flags & TERMKEY_FLAG_NOTERMIOS)) {
|
|
|
|
struct termios termios;
|
|
|
|
if(tcgetattr(tk->fd, &termios) == 0) {
|
|
|
|
tk->restore_termios = termios;
|
|
|
|
tk->restore_termios_valid = 1;
|
|
|
|
|
|
|
|
termios.c_iflag &= ~(IXON|INLCR|ICRNL);
|
|
|
|
termios.c_lflag &= ~(ICANON|ECHO);
|
|
|
|
termios.c_cc[VMIN] = 1;
|
|
|
|
termios.c_cc[VTIME] = 0;
|
|
|
|
|
|
|
|
if(tk->flags & TERMKEY_FLAG_CTRLC)
|
|
|
|
/* want no signal keys at all, so just disable ISIG */
|
|
|
|
termios.c_lflag &= ~ISIG;
|
|
|
|
else {
|
|
|
|
/* Disable Ctrl-\==VQUIT and Ctrl-D==VSUSP but leave Ctrl-C as SIGINT */
|
|
|
|
termios.c_cc[VQUIT] = _POSIX_VDISABLE;
|
|
|
|
termios.c_cc[VSUSP] = _POSIX_VDISABLE;
|
|
|
|
/* Some OSes have Ctrl-Y==VDSUSP */
|
|
|
|
#ifdef VDSUSP
|
|
|
|
termios.c_cc[VDSUSP] = _POSIX_VDISABLE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Setting termios(3) flags\n");
|
|
|
|
#endif
|
|
|
|
tcsetattr(tk->fd, TCSANOW, &termios);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TermKeyDriverNode *p;
|
|
|
|
for(p = tk->drivers; p; p = p->next)
|
|
|
|
if(p->driver->start_driver)
|
|
|
|
if(!(*p->driver->start_driver)(tk, p->info))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Drivers started; termkey instance %p is ready\n", tk);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tk->is_started = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int termkey_stop(TermKey *tk)
|
|
|
|
{
|
|
|
|
if(!tk->is_started)
|
|
|
|
return 1;
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
struct TermKeyDriverNode *p;
|
2008-11-09 20:58:11 +01:00
|
|
|
for(p = tk->drivers; p; p = p->next)
|
|
|
|
if(p->driver->stop_driver)
|
|
|
|
(*p->driver->stop_driver)(tk, p->info);
|
2008-11-02 18:54:24 +01:00
|
|
|
|
2008-02-23 21:26:04 +01:00
|
|
|
if(tk->restore_termios_valid)
|
|
|
|
tcsetattr(tk->fd, TCSANOW, &tk->restore_termios);
|
|
|
|
|
2012-03-08 22:15:25 +01:00
|
|
|
tk->is_started = 0;
|
|
|
|
|
|
|
|
return 1;
|
2008-02-23 21:26:04 +01:00
|
|
|
}
|
|
|
|
|
2012-03-08 22:24:39 +01:00
|
|
|
int termkey_is_started(TermKey *tk)
|
|
|
|
{
|
|
|
|
return tk->is_started;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
int termkey_get_fd(TermKey *tk)
|
2008-12-01 23:36:14 +01:00
|
|
|
{
|
|
|
|
return tk->fd;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
int termkey_get_flags(TermKey *tk)
|
2008-11-10 22:21:19 +01:00
|
|
|
{
|
|
|
|
return tk->flags;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
void termkey_set_flags(TermKey *tk, int newflags)
|
2008-11-10 22:21:19 +01:00
|
|
|
{
|
|
|
|
tk->flags = newflags;
|
2011-08-28 18:50:18 +02:00
|
|
|
|
|
|
|
if(tk->flags & TERMKEY_FLAG_SPACESYMBOL)
|
|
|
|
tk->canonflags |= TERMKEY_CANON_SPACESYMBOL;
|
|
|
|
else
|
|
|
|
tk->canonflags &= ~TERMKEY_CANON_SPACESYMBOL;
|
2008-11-10 22:21:19 +01:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
void termkey_set_waittime(TermKey *tk, int msec)
|
2008-02-10 21:22:33 +01:00
|
|
|
{
|
|
|
|
tk->waittime = msec;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
int termkey_get_waittime(TermKey *tk)
|
2008-02-10 21:22:33 +01:00
|
|
|
{
|
|
|
|
return tk->waittime;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2011-08-28 18:50:18 +02:00
|
|
|
int termkey_get_canonflags(TermKey *tk)
|
|
|
|
{
|
|
|
|
return tk->canonflags;
|
|
|
|
}
|
|
|
|
|
|
|
|
void termkey_set_canonflags(TermKey *tk, int flags)
|
|
|
|
{
|
|
|
|
tk->canonflags = flags;
|
|
|
|
|
|
|
|
if(tk->canonflags & TERMKEY_CANON_SPACESYMBOL)
|
|
|
|
tk->flags |= TERMKEY_FLAG_SPACESYMBOL;
|
|
|
|
else
|
|
|
|
tk->flags &= ~TERMKEY_FLAG_SPACESYMBOL;
|
|
|
|
}
|
|
|
|
|
2012-02-13 22:57:33 +01:00
|
|
|
size_t termkey_get_buffer_size(TermKey *tk)
|
|
|
|
{
|
|
|
|
return tk->buffsize;
|
|
|
|
}
|
|
|
|
|
2012-02-14 00:40:42 +01:00
|
|
|
int termkey_set_buffer_size(TermKey *tk, size_t size)
|
|
|
|
{
|
|
|
|
unsigned char *buffer = realloc(tk->buffer, size);
|
|
|
|
if(!buffer)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
tk->buffer = buffer;
|
|
|
|
tk->buffsize = size;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-01-18 11:04:15 +01:00
|
|
|
size_t termkey_get_buffer_remaining(TermKey *tk)
|
|
|
|
{
|
|
|
|
/* Return the total number of free bytes in the buffer, because that's what
|
|
|
|
* is available to the user. */
|
|
|
|
return tk->buffsize - tk->buffcount;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static void eat_bytes(TermKey *tk, size_t count)
|
2008-10-07 23:22:59 +02:00
|
|
|
{
|
|
|
|
if(count >= tk->buffcount) {
|
|
|
|
tk->buffstart = 0;
|
|
|
|
tk->buffcount = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tk->buffstart += count;
|
|
|
|
tk->buffcount -= count;
|
|
|
|
}
|
|
|
|
|
2008-11-24 22:58:55 +01:00
|
|
|
static inline unsigned int utf8_seqlen(long codepoint)
|
2008-10-08 01:36:27 +02:00
|
|
|
{
|
|
|
|
if(codepoint < 0x0000080) return 1;
|
|
|
|
if(codepoint < 0x0000800) return 2;
|
|
|
|
if(codepoint < 0x0010000) return 3;
|
|
|
|
if(codepoint < 0x0200000) return 4;
|
|
|
|
if(codepoint < 0x4000000) return 5;
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static void fill_utf8(TermKeyKey *key)
|
2008-10-08 01:12:41 +02:00
|
|
|
{
|
2008-10-10 01:50:56 +02:00
|
|
|
long codepoint = key->code.codepoint;
|
2008-10-08 01:12:41 +02:00
|
|
|
int nbytes = utf8_seqlen(codepoint);
|
|
|
|
|
|
|
|
key->utf8[nbytes] = 0;
|
|
|
|
|
|
|
|
// This is easier done backwards
|
|
|
|
int b = nbytes;
|
|
|
|
while(b > 1) {
|
|
|
|
b--;
|
|
|
|
key->utf8[b] = 0x80 | (codepoint & 0x3f);
|
|
|
|
codepoint >>= 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(nbytes) {
|
|
|
|
case 1: key->utf8[0] = (codepoint & 0x7f); break;
|
|
|
|
case 2: key->utf8[0] = 0xc0 | (codepoint & 0x1f); break;
|
|
|
|
case 3: key->utf8[0] = 0xe0 | (codepoint & 0x0f); break;
|
|
|
|
case 4: key->utf8[0] = 0xf0 | (codepoint & 0x07); break;
|
|
|
|
case 5: key->utf8[0] = 0xf8 | (codepoint & 0x03); break;
|
|
|
|
case 6: key->utf8[0] = 0xfc | (codepoint & 0x01); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-01 00:42:52 +02:00
|
|
|
#define UTF8_INVALID 0xFFFD
|
|
|
|
static TermKeyResult parse_utf8(const unsigned char *bytes, size_t len, long *cp, size_t *nbytep)
|
|
|
|
{
|
|
|
|
unsigned int nbytes;
|
|
|
|
|
|
|
|
unsigned char b0 = bytes[0];
|
|
|
|
|
2011-04-01 01:50:51 +02:00
|
|
|
if(b0 < 0x80) {
|
|
|
|
// Single byte ASCII
|
|
|
|
*cp = b0;
|
|
|
|
*nbytep = 1;
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xc0) {
|
2011-04-01 00:42:52 +02:00
|
|
|
// Starts with a continuation byte - that's not right
|
|
|
|
*cp = UTF8_INVALID;
|
|
|
|
*nbytep = 1;
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xe0) {
|
|
|
|
nbytes = 2;
|
|
|
|
*cp = b0 & 0x1f;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xf0) {
|
|
|
|
nbytes = 3;
|
|
|
|
*cp = b0 & 0x0f;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xf8) {
|
|
|
|
nbytes = 4;
|
|
|
|
*cp = b0 & 0x07;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xfc) {
|
|
|
|
nbytes = 5;
|
|
|
|
*cp = b0 & 0x03;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xfe) {
|
|
|
|
nbytes = 6;
|
|
|
|
*cp = b0 & 0x01;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*cp = UTF8_INVALID;
|
|
|
|
*nbytep = 1;
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(unsigned int b = 1; b < nbytes; b++) {
|
|
|
|
unsigned char cb;
|
|
|
|
|
|
|
|
if(b >= len)
|
|
|
|
return TERMKEY_RES_AGAIN;
|
|
|
|
|
|
|
|
cb = bytes[b];
|
|
|
|
if(cb < 0x80 || cb >= 0xc0) {
|
|
|
|
*cp = UTF8_INVALID;
|
|
|
|
*nbytep = b;
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
*cp <<= 6;
|
|
|
|
*cp |= cb & 0x3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for overlong sequences
|
|
|
|
if(nbytes > utf8_seqlen(*cp))
|
|
|
|
*cp = UTF8_INVALID;
|
|
|
|
|
|
|
|
// Check for UTF-16 surrogates or invalid *cps
|
|
|
|
if((*cp >= 0xD800 && *cp <= 0xDFFF) ||
|
|
|
|
*cp == 0xFFFE ||
|
|
|
|
*cp == 0xFFFF)
|
|
|
|
*cp = UTF8_INVALID;
|
|
|
|
|
|
|
|
*nbytep = nbytes;
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static void emit_codepoint(TermKey *tk, long codepoint, TermKeyKey *key)
|
2008-10-08 01:12:41 +02:00
|
|
|
{
|
|
|
|
if(codepoint < 0x20) {
|
|
|
|
// C0 range
|
|
|
|
key->code.codepoint = 0;
|
|
|
|
key->modifiers = 0;
|
|
|
|
|
|
|
|
if(!(tk->flags & TERMKEY_FLAG_NOINTERPRET) && tk->c0[codepoint].sym != TERMKEY_SYM_UNKNOWN) {
|
|
|
|
key->code.sym = tk->c0[codepoint].sym;
|
|
|
|
key->modifiers |= tk->c0[codepoint].modifier_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!key->code.sym) {
|
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
2008-12-10 02:34:40 +01:00
|
|
|
/* Generically modified Unicode ought not report the SHIFT state, or else
|
|
|
|
* we get into complicationg trying to report Shift-; vs : and so on...
|
2013-08-26 03:35:40 +02:00
|
|
|
* In order to be able to represent Ctrl-Shift-A as CTRL modified
|
2008-12-10 02:34:40 +01:00
|
|
|
* unicode A, we need to call Ctrl-A simply 'a', lowercase
|
|
|
|
*/
|
|
|
|
if(codepoint+0x40 >= 'A' && codepoint+0x40 <= 'Z')
|
|
|
|
// it's a letter - use lowecase instead
|
|
|
|
key->code.codepoint = codepoint + 0x60;
|
|
|
|
else
|
|
|
|
key->code.codepoint = codepoint + 0x40;
|
2008-10-08 01:12:41 +02:00
|
|
|
key->modifiers = TERMKEY_KEYMOD_CTRL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
key->type = TERMKEY_TYPE_KEYSYM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(codepoint == 0x7f && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) {
|
|
|
|
// ASCII DEL
|
|
|
|
key->type = TERMKEY_TYPE_KEYSYM;
|
|
|
|
key->code.sym = TERMKEY_SYM_DEL;
|
|
|
|
key->modifiers = 0;
|
|
|
|
}
|
|
|
|
else if(codepoint >= 0x20 && codepoint < 0x80) {
|
|
|
|
// ASCII lowbyte range
|
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
|
|
|
key->code.codepoint = codepoint;
|
|
|
|
key->modifiers = 0;
|
|
|
|
}
|
|
|
|
else if(codepoint >= 0x80 && codepoint < 0xa0) {
|
|
|
|
// UTF-8 never starts with a C1 byte. So we can be sure of these
|
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
|
|
|
key->code.codepoint = codepoint - 0x40;
|
|
|
|
key->modifiers = TERMKEY_KEYMOD_CTRL|TERMKEY_KEYMOD_ALT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// UTF-8 codepoint
|
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
|
|
|
key->code.codepoint = codepoint;
|
|
|
|
key->modifiers = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-27 20:59:02 +02:00
|
|
|
termkey_canonicalise(tk, key);
|
|
|
|
|
2008-10-08 01:12:41 +02:00
|
|
|
if(key->type == TERMKEY_TYPE_UNICODE)
|
|
|
|
fill_utf8(key);
|
|
|
|
}
|
|
|
|
|
2011-08-27 20:59:02 +02:00
|
|
|
void termkey_canonicalise(TermKey *tk, TermKeyKey *key)
|
|
|
|
{
|
2011-08-28 18:50:18 +02:00
|
|
|
int flags = tk->canonflags;
|
2011-08-27 20:59:02 +02:00
|
|
|
|
2011-08-28 18:50:18 +02:00
|
|
|
if(flags & TERMKEY_CANON_SPACESYMBOL) {
|
2011-08-27 20:59:02 +02:00
|
|
|
if(key->type == TERMKEY_TYPE_UNICODE && key->code.number == 0x20) {
|
|
|
|
key->type = TERMKEY_TYPE_KEYSYM;
|
|
|
|
key->code.sym = TERMKEY_SYM_SPACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(key->type == TERMKEY_TYPE_KEYSYM && key->code.sym == TERMKEY_SYM_SPACE) {
|
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
|
|
|
key->code.number = 0x20;
|
|
|
|
fill_utf8(key);
|
|
|
|
}
|
|
|
|
}
|
2011-08-28 18:57:57 +02:00
|
|
|
|
|
|
|
if(flags & TERMKEY_CANON_DELBS) {
|
|
|
|
if(key->type == TERMKEY_TYPE_KEYSYM && key->code.sym == TERMKEY_SYM_DEL) {
|
|
|
|
key->code.sym = TERMKEY_SYM_BACKSPACE;
|
|
|
|
}
|
|
|
|
}
|
2011-08-27 20:59:02 +02:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static TermKeyResult peekkey(TermKey *tk, TermKeyKey *key, int force, size_t *nbytep)
|
2008-12-06 00:55:44 +01:00
|
|
|
{
|
|
|
|
int again = 0;
|
|
|
|
|
2012-03-08 22:31:16 +01:00
|
|
|
if(!tk->is_started) {
|
|
|
|
errno = EINVAL;
|
|
|
|
return TERMKEY_RES_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-12-06 00:55:44 +01:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "getkey(force=%d): buffer ", force);
|
|
|
|
print_buffer(tk);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
#endif
|
|
|
|
|
2012-11-30 15:35:23 +01:00
|
|
|
if(tk->hightide) {
|
|
|
|
tk->buffstart += tk->hightide;
|
|
|
|
tk->buffcount -= tk->hightide;
|
|
|
|
tk->hightide = 0;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult ret;
|
|
|
|
struct TermKeyDriverNode *p;
|
2008-12-06 00:55:44 +01:00
|
|
|
for(p = tk->drivers; p; p = p->next) {
|
2008-12-10 01:23:47 +01:00
|
|
|
ret = (p->driver->peekkey)(tk, p->info, key, force, nbytep);
|
2008-12-06 00:55:44 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Driver %s yields %s\n", p->driver->name, res2str(ret));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
switch(ret) {
|
|
|
|
case TERMKEY_RES_KEY:
|
|
|
|
#ifdef DEBUG
|
|
|
|
print_key(tk, key); fprintf(stderr, "\n");
|
|
|
|
#endif
|
2008-12-06 01:03:48 +01:00
|
|
|
// Slide the data down to stop it running away
|
|
|
|
{
|
|
|
|
size_t halfsize = tk->buffsize / 2;
|
|
|
|
|
|
|
|
if(tk->buffstart > halfsize) {
|
|
|
|
memcpy(tk->buffer, tk->buffer + halfsize, halfsize);
|
|
|
|
tk->buffstart -= halfsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 00:55:44 +01:00
|
|
|
/* fallthrough */
|
|
|
|
case TERMKEY_RES_EOF:
|
2011-08-18 12:20:41 +02:00
|
|
|
case TERMKEY_RES_ERROR:
|
2008-12-06 00:55:44 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
case TERMKEY_RES_AGAIN:
|
|
|
|
if(!force)
|
|
|
|
again = 1;
|
|
|
|
|
|
|
|
/* fallthrough */
|
|
|
|
case TERMKEY_RES_NONE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(again)
|
|
|
|
return TERMKEY_RES_AGAIN;
|
|
|
|
|
2008-12-10 01:23:47 +01:00
|
|
|
ret = peekkey_simple(tk, key, force, nbytep);
|
2008-12-06 00:55:44 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "getkey_simple(force=%d) yields %s\n", force, res2str(ret));
|
|
|
|
if(ret == TERMKEY_RES_KEY) {
|
|
|
|
print_key(tk, key); fprintf(stderr, "\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static TermKeyResult peekkey_simple(TermKey *tk, TermKeyKey *key, int force, size_t *nbytep)
|
2008-10-08 01:36:27 +02:00
|
|
|
{
|
2008-11-09 20:29:30 +01:00
|
|
|
if(tk->buffcount == 0)
|
|
|
|
return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
|
|
|
|
|
2008-10-08 01:36:27 +02:00
|
|
|
unsigned char b0 = CHARAT(0);
|
|
|
|
|
2008-11-02 15:54:33 +01:00
|
|
|
if(b0 == 0x1b) {
|
|
|
|
// Escape-prefixed value? Might therefore be Alt+key
|
|
|
|
if(tk->buffcount == 1) {
|
|
|
|
// This might be an <Esc> press, or it may want to be part of a longer
|
|
|
|
// sequence
|
|
|
|
if(!force)
|
|
|
|
return TERMKEY_RES_AGAIN;
|
|
|
|
|
|
|
|
(*tk->method.emit_codepoint)(tk, b0, key);
|
2008-12-10 01:23:47 +01:00
|
|
|
*nbytep = 1;
|
2008-11-02 15:54:33 +01:00
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try another key there
|
|
|
|
tk->buffstart++;
|
2008-11-03 15:45:59 +01:00
|
|
|
tk->buffcount--;
|
2008-11-02 15:54:33 +01:00
|
|
|
|
|
|
|
// Run the full driver
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult metakey_result = peekkey(tk, key, force, nbytep);
|
2008-11-02 15:54:33 +01:00
|
|
|
|
2008-11-03 15:45:59 +01:00
|
|
|
tk->buffstart--;
|
|
|
|
tk->buffcount++;
|
|
|
|
|
2008-11-02 15:54:33 +01:00
|
|
|
switch(metakey_result) {
|
|
|
|
case TERMKEY_RES_KEY:
|
|
|
|
key->modifiers |= TERMKEY_KEYMOD_ALT;
|
2008-12-10 01:23:47 +01:00
|
|
|
(*nbytep)++;
|
2008-11-02 15:54:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TERMKEY_RES_NONE:
|
|
|
|
case TERMKEY_RES_EOF:
|
|
|
|
case TERMKEY_RES_AGAIN:
|
2011-08-18 12:20:41 +02:00
|
|
|
case TERMKEY_RES_ERROR:
|
2008-11-02 15:54:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return metakey_result;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xa0) {
|
2008-10-08 01:36:27 +02:00
|
|
|
// Single byte C0, G0 or C1 - C1 is never UTF-8 initial byte
|
|
|
|
(*tk->method.emit_codepoint)(tk, b0, key);
|
2008-12-10 01:23:47 +01:00
|
|
|
*nbytep = 1;
|
2008-10-08 01:36:27 +02:00
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
else if(tk->flags & TERMKEY_FLAG_UTF8) {
|
|
|
|
// Some UTF-8
|
2008-10-10 01:50:56 +02:00
|
|
|
long codepoint;
|
2011-04-01 00:42:52 +02:00
|
|
|
TermKeyResult res = parse_utf8(tk->buffer + tk->buffstart, tk->buffcount, &codepoint, nbytep);
|
2008-10-08 01:36:27 +02:00
|
|
|
|
2011-04-01 00:42:52 +02:00
|
|
|
if(res == TERMKEY_RES_AGAIN && force) {
|
2008-11-03 22:10:14 +01:00
|
|
|
/* There weren't enough bytes for a complete UTF-8 sequence but caller
|
|
|
|
* demands an answer. About the best thing we can do here is eat as many
|
|
|
|
* bytes as we have, and emit a UTF8_INVALID. If the remaining bytes
|
|
|
|
* arrive later, they'll be invalid too.
|
|
|
|
*/
|
2011-04-01 00:42:52 +02:00
|
|
|
codepoint = UTF8_INVALID;
|
2008-12-10 01:23:47 +01:00
|
|
|
*nbytep = tk->buffcount;
|
2011-04-01 00:42:52 +02:00
|
|
|
res = TERMKEY_RES_KEY;
|
2008-10-08 01:36:27 +02:00
|
|
|
}
|
|
|
|
|
2011-04-01 00:42:52 +02:00
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
|
|
|
key->modifiers = 0;
|
2008-10-08 01:36:27 +02:00
|
|
|
(*tk->method.emit_codepoint)(tk, codepoint, key);
|
2011-04-01 00:42:52 +02:00
|
|
|
return res;
|
2008-10-08 01:36:27 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Non UTF-8 case - just report the raw byte
|
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
|
|
|
key->code.codepoint = b0;
|
|
|
|
key->modifiers = 0;
|
|
|
|
|
|
|
|
key->utf8[0] = key->code.codepoint;
|
|
|
|
key->utf8[1] = 0;
|
|
|
|
|
2008-12-10 01:23:47 +01:00
|
|
|
*nbytep = 1;
|
2008-10-08 01:36:27 +02:00
|
|
|
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-26 01:27:58 +01:00
|
|
|
static TermKeyResult peekkey_mouse(TermKey *tk, TermKeyKey *key, size_t *nbytep)
|
|
|
|
{
|
|
|
|
if(tk->buffcount < 3)
|
|
|
|
return TERMKEY_RES_AGAIN;
|
|
|
|
|
|
|
|
key->type = TERMKEY_TYPE_MOUSE;
|
2009-11-27 15:36:29 +01:00
|
|
|
key->code.mouse[0] = CHARAT(0) - 0x20;
|
|
|
|
key->code.mouse[1] = CHARAT(1) - 0x20;
|
|
|
|
key->code.mouse[2] = CHARAT(2) - 0x20;
|
2012-04-12 01:19:10 +02:00
|
|
|
key->code.mouse[3] = 0;
|
2012-04-05 19:54:30 +02:00
|
|
|
|
|
|
|
key->modifiers = (key->code.mouse[0] & 0x1c) >> 2;
|
|
|
|
key->code.mouse[0] &= ~0x1c;
|
|
|
|
|
2009-11-26 01:27:58 +01:00
|
|
|
*nbytep = 3;
|
|
|
|
return TERMKEY_RES_KEY;
|
2012-04-24 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult termkey_getkey(TermKey *tk, TermKeyKey *key)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2008-12-10 01:23:47 +01:00
|
|
|
size_t nbytes = 0;
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult ret = peekkey(tk, key, 0, &nbytes);
|
2008-12-10 01:23:47 +01:00
|
|
|
|
|
|
|
if(ret == TERMKEY_RES_KEY)
|
|
|
|
eat_bytes(tk, nbytes);
|
|
|
|
|
2008-12-10 01:38:21 +01:00
|
|
|
if(ret == TERMKEY_RES_AGAIN)
|
|
|
|
/* Call peekkey() again in force mode to obtain whatever it can */
|
|
|
|
(void)peekkey(tk, key, 1, &nbytes);
|
|
|
|
/* Don't eat it yet though */
|
|
|
|
|
2008-12-10 01:23:47 +01:00
|
|
|
return ret;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult termkey_getkey_force(TermKey *tk, TermKeyKey *key)
|
2008-02-10 21:22:33 +01:00
|
|
|
{
|
2008-12-10 01:23:47 +01:00
|
|
|
size_t nbytes = 0;
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult ret = peekkey(tk, key, 1, &nbytes);
|
2008-12-10 01:23:47 +01:00
|
|
|
|
|
|
|
if(ret == TERMKEY_RES_KEY)
|
|
|
|
eat_bytes(tk, nbytes);
|
|
|
|
|
|
|
|
return ret;
|
2008-02-10 21:22:33 +01:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult termkey_waitkey(TermKey *tk, TermKeyKey *key)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2012-01-18 15:03:39 +01:00
|
|
|
if(tk->fd == -1) {
|
|
|
|
errno = EBADF;
|
|
|
|
return TERMKEY_RES_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-02-10 21:22:33 +01:00
|
|
|
while(1) {
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult ret = termkey_getkey(tk, key);
|
2008-02-10 21:22:33 +01:00
|
|
|
|
|
|
|
switch(ret) {
|
|
|
|
case TERMKEY_RES_KEY:
|
|
|
|
case TERMKEY_RES_EOF:
|
2011-08-18 12:20:41 +02:00
|
|
|
case TERMKEY_RES_ERROR:
|
2008-02-10 21:22:33 +01:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
case TERMKEY_RES_NONE:
|
2011-08-18 12:20:41 +02:00
|
|
|
ret = termkey_advisereadable(tk);
|
|
|
|
if(ret == TERMKEY_RES_ERROR)
|
|
|
|
return ret;
|
2008-02-10 21:22:33 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TERMKEY_RES_AGAIN:
|
|
|
|
{
|
2008-11-03 22:08:34 +01:00
|
|
|
if(tk->is_closed)
|
|
|
|
// We're closed now. Never going to get more bytes so just go with
|
|
|
|
// what we have
|
|
|
|
return termkey_getkey_force(tk, key);
|
|
|
|
|
2008-02-10 21:22:33 +01:00
|
|
|
struct pollfd fd;
|
|
|
|
|
2011-08-25 11:48:41 +02:00
|
|
|
retry:
|
2008-02-10 21:22:33 +01:00
|
|
|
fd.fd = tk->fd;
|
|
|
|
fd.events = POLLIN;
|
|
|
|
|
2011-08-18 12:20:41 +02:00
|
|
|
int pollret = poll(&fd, 1, tk->waittime);
|
2011-08-25 11:48:41 +02:00
|
|
|
if(pollret == -1) {
|
|
|
|
if(errno == EINTR && !(tk->flags & TERMKEY_FLAG_EINTR))
|
|
|
|
goto retry;
|
|
|
|
|
2011-08-18 12:20:41 +02:00
|
|
|
return TERMKEY_RES_ERROR;
|
2011-08-25 11:48:41 +02:00
|
|
|
}
|
2008-02-10 21:22:33 +01:00
|
|
|
|
2008-11-03 22:08:34 +01:00
|
|
|
if(fd.revents & (POLLIN|POLLHUP|POLLERR))
|
|
|
|
ret = termkey_advisereadable(tk);
|
|
|
|
else
|
|
|
|
ret = TERMKEY_RES_NONE;
|
2008-02-10 21:22:33 +01:00
|
|
|
|
2011-08-18 12:20:41 +02:00
|
|
|
if(ret == TERMKEY_RES_ERROR)
|
|
|
|
return ret;
|
2008-11-03 22:08:34 +01:00
|
|
|
if(ret == TERMKEY_RES_NONE)
|
|
|
|
return termkey_getkey_force(tk, key);
|
2008-02-10 21:22:33 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2008-02-10 21:22:33 +01:00
|
|
|
/* UNREACHABLE */
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeyResult termkey_advisereadable(TermKey *tk)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2011-08-25 11:48:41 +02:00
|
|
|
ssize_t len;
|
|
|
|
|
2012-01-18 15:03:39 +01:00
|
|
|
if(tk->fd == -1) {
|
|
|
|
errno = EBADF;
|
|
|
|
return TERMKEY_RES_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-18 11:07:36 +01:00
|
|
|
if(tk->buffstart) {
|
|
|
|
memmove(tk->buffer, tk->buffer + tk->buffstart, tk->buffcount);
|
|
|
|
tk->buffstart = 0;
|
|
|
|
}
|
|
|
|
|
2012-01-18 11:36:50 +01:00
|
|
|
/* Not expecting it ever to be greater but doesn't hurt to handle that */
|
|
|
|
if(tk->buffcount >= tk->buffsize) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return TERMKEY_RES_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-08-25 11:48:41 +02:00
|
|
|
retry:
|
2012-01-18 11:07:36 +01:00
|
|
|
len = read(tk->fd, tk->buffer + tk->buffcount, tk->buffsize - tk->buffcount);
|
2008-02-07 01:58:41 +01:00
|
|
|
|
2011-08-18 12:20:41 +02:00
|
|
|
if(len == -1) {
|
|
|
|
if(errno == EAGAIN)
|
|
|
|
return TERMKEY_RES_NONE;
|
2011-08-25 11:48:41 +02:00
|
|
|
else if(errno == EINTR && !(tk->flags & TERMKEY_FLAG_EINTR))
|
|
|
|
goto retry;
|
2011-08-18 12:20:41 +02:00
|
|
|
else
|
|
|
|
return TERMKEY_RES_ERROR;
|
|
|
|
}
|
2008-02-10 20:23:18 +01:00
|
|
|
else if(len < 1) {
|
2008-02-07 01:58:41 +01:00
|
|
|
tk->is_closed = 1;
|
2008-02-10 20:23:18 +01:00
|
|
|
return TERMKEY_RES_NONE;
|
|
|
|
}
|
|
|
|
else {
|
2012-01-18 11:07:36 +01:00
|
|
|
tk->buffcount += len;
|
2008-02-10 20:23:18 +01:00
|
|
|
return TERMKEY_RES_AGAIN;
|
|
|
|
}
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2012-01-18 14:39:50 +01:00
|
|
|
size_t termkey_push_bytes(TermKey *tk, const char *bytes, size_t len)
|
|
|
|
{
|
|
|
|
if(tk->buffstart) {
|
|
|
|
memmove(tk->buffer, tk->buffer + tk->buffstart, tk->buffcount);
|
|
|
|
tk->buffstart = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not expecting it ever to be greater but doesn't hurt to handle that */
|
|
|
|
if(tk->buffcount >= tk->buffsize) {
|
|
|
|
errno = ENOMEM;
|
|
|
|
return (size_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len > tk->buffsize - tk->buffcount)
|
|
|
|
len = tk->buffsize - tk->buffcount;
|
|
|
|
|
|
|
|
// memcpy(), not strncpy() in case of null bytes in input
|
|
|
|
memcpy(tk->buffer + tk->buffcount, bytes, len);
|
|
|
|
tk->buffcount += len;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
TermKeySym termkey_register_keyname(TermKey *tk, TermKeySym sym, const char *name)
|
2008-02-07 03:21:14 +01:00
|
|
|
{
|
2008-08-21 21:05:17 +02:00
|
|
|
if(!sym)
|
|
|
|
sym = tk->nkeynames;
|
2008-02-07 03:21:14 +01:00
|
|
|
|
2008-08-21 21:05:17 +02:00
|
|
|
if(sym >= tk->nkeynames) {
|
|
|
|
const char **new_keynames = realloc(tk->keynames, sizeof(new_keynames[0]) * (sym + 1));
|
2012-01-20 19:03:08 +01:00
|
|
|
if(!new_keynames)
|
|
|
|
return -1;
|
|
|
|
|
2008-02-10 18:34:05 +01:00
|
|
|
tk->keynames = new_keynames;
|
2008-02-07 03:21:14 +01:00
|
|
|
|
|
|
|
// Fill in the hole
|
2008-08-21 21:05:17 +02:00
|
|
|
for(int i = tk->nkeynames; i < sym; i++)
|
2008-02-07 03:21:14 +01:00
|
|
|
tk->keynames[i] = NULL;
|
|
|
|
|
2008-08-21 21:05:17 +02:00
|
|
|
tk->nkeynames = sym + 1;
|
2008-02-07 03:21:14 +01:00
|
|
|
}
|
|
|
|
|
2008-08-21 21:05:17 +02:00
|
|
|
tk->keynames[sym] = name;
|
2008-02-07 03:21:14 +01:00
|
|
|
|
2008-08-21 21:05:17 +02:00
|
|
|
return sym;
|
2008-02-07 03:21:14 +01:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
const char *termkey_get_keyname(TermKey *tk, TermKeySym sym)
|
2008-02-23 21:28:18 +01:00
|
|
|
{
|
2008-08-21 21:05:17 +02:00
|
|
|
if(sym == TERMKEY_SYM_UNKNOWN)
|
2008-02-23 21:28:18 +01:00
|
|
|
return "UNKNOWN";
|
|
|
|
|
2008-08-21 21:05:17 +02:00
|
|
|
if(sym < tk->nkeynames)
|
|
|
|
return tk->keynames[sym];
|
2008-02-23 21:28:18 +01:00
|
|
|
|
|
|
|
return "UNKNOWN";
|
|
|
|
}
|
|
|
|
|
2013-08-26 03:23:19 +02:00
|
|
|
static const char *termkey_lookup_keyname_format(TermKey *tk, const char *str, TermKeySym *sym, TermKeyFormat format)
|
2008-12-02 01:56:43 +01:00
|
|
|
{
|
|
|
|
/* We store an array, so we can't do better than a linear search. Doesn't
|
|
|
|
* matter because user won't be calling this too often */
|
|
|
|
|
2011-04-01 14:26:54 +02:00
|
|
|
for(*sym = 0; *sym < tk->nkeynames; (*sym)++) {
|
|
|
|
const char *thiskey = tk->keynames[*sym];
|
|
|
|
if(!thiskey)
|
|
|
|
continue;
|
|
|
|
size_t len = strlen(thiskey);
|
2013-08-26 03:23:19 +02:00
|
|
|
if(format & TERMKEY_FORMAT_LOWERSPACE) {
|
|
|
|
const char *thisstr = str;
|
|
|
|
if(strpncmp_camel(&thisstr, &thiskey, len) == 0)
|
|
|
|
return thisstr;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(strncmp(str, thiskey, len) == 0)
|
|
|
|
return (char *)str + len;
|
|
|
|
}
|
2011-04-01 14:26:54 +02:00
|
|
|
}
|
2008-12-02 01:56:43 +01:00
|
|
|
|
2011-04-01 14:26:54 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-02 01:56:43 +01:00
|
|
|
|
2013-08-26 03:23:19 +02:00
|
|
|
const char *termkey_lookup_keyname(TermKey *tk, const char *str, TermKeySym *sym)
|
|
|
|
{
|
|
|
|
return termkey_lookup_keyname_format(tk, str, sym, 0);
|
|
|
|
}
|
|
|
|
|
2011-04-01 14:26:54 +02:00
|
|
|
TermKeySym termkey_keyname2sym(TermKey *tk, const char *keyname)
|
|
|
|
{
|
|
|
|
TermKeySym sym;
|
2013-07-15 02:03:35 +02:00
|
|
|
const char *endp = termkey_lookup_keyname(tk, keyname, &sym);
|
2011-04-01 14:26:54 +02:00
|
|
|
if(!endp || endp[0])
|
|
|
|
return TERMKEY_SYM_UNKNOWN;
|
|
|
|
return sym;
|
2008-12-02 01:56:43 +01:00
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static TermKeySym register_c0(TermKey *tk, TermKeySym sym, unsigned char ctrl, const char *name)
|
2008-10-08 01:12:41 +02:00
|
|
|
{
|
|
|
|
return register_c0_full(tk, sym, 0, 0, ctrl, name);
|
|
|
|
}
|
|
|
|
|
2009-07-15 21:40:44 +02:00
|
|
|
static TermKeySym register_c0_full(TermKey *tk, TermKeySym sym, int modifier_set, int modifier_mask, unsigned char ctrl, const char *name)
|
2008-10-08 01:12:41 +02:00
|
|
|
{
|
|
|
|
if(ctrl >= 0x20) {
|
2012-01-29 17:13:38 +01:00
|
|
|
errno = EINVAL;
|
2008-10-08 01:12:41 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(name)
|
|
|
|
sym = termkey_register_keyname(tk, sym, name);
|
|
|
|
|
|
|
|
tk->c0[ctrl].sym = sym;
|
|
|
|
tk->c0[ctrl].modifier_set = modifier_set;
|
|
|
|
tk->c0[ctrl].modifier_mask = modifier_mask;
|
|
|
|
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
2011-08-27 20:32:39 +02:00
|
|
|
/* Previous name for this function
|
|
|
|
* No longer declared in termkey.h but it remains in the compiled library for
|
|
|
|
* backward-compatibility reasons.
|
|
|
|
*/
|
2009-07-15 21:40:44 +02:00
|
|
|
size_t termkey_snprint_key(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, TermKeyFormat format)
|
2011-03-31 12:08:43 +02:00
|
|
|
{
|
|
|
|
return termkey_strfkey(tk, buffer, len, key, format);
|
|
|
|
}
|
|
|
|
|
2012-01-22 19:56:08 +01:00
|
|
|
static struct modnames {
|
2011-04-01 01:26:02 +02:00
|
|
|
const char *shift, *alt, *ctrl;
|
|
|
|
}
|
|
|
|
modnames[] = {
|
|
|
|
{ "S", "A", "C" }, // 0
|
|
|
|
{ "Shift", "Alt", "Ctrl" }, // LONGMOD
|
|
|
|
{ "S", "M", "C" }, // ALTISMETA
|
|
|
|
{ "Shift", "Meta", "Ctrl" }, // ALTISMETA+LONGMOD
|
2013-08-26 02:14:35 +02:00
|
|
|
{ "s", "a", "c" }, // LOWERMOD
|
|
|
|
{ "shift", "alt", "ctrl" }, // LOWERMOD+LONGMOD
|
|
|
|
{ "s", "m", "c" }, // LOWERMOD+ALTISMETA
|
|
|
|
{ "shift", "meta", "ctrl" }, // LOWERMOD+ALTISMETA+LONGMOD
|
2011-04-01 01:26:02 +02:00
|
|
|
};
|
|
|
|
|
2011-03-31 12:08:43 +02:00
|
|
|
size_t termkey_strfkey(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, TermKeyFormat format)
|
2008-08-21 21:20:07 +02:00
|
|
|
{
|
|
|
|
size_t pos = 0;
|
2008-11-24 23:05:06 +01:00
|
|
|
size_t l = 0;
|
2008-08-21 21:20:07 +02:00
|
|
|
|
2011-04-01 01:26:02 +02:00
|
|
|
struct modnames *mods = &modnames[!!(format & TERMKEY_FORMAT_LONGMOD) +
|
2013-08-26 02:14:35 +02:00
|
|
|
!!(format & TERMKEY_FORMAT_ALTISMETA) * 2 +
|
|
|
|
!!(format & TERMKEY_FORMAT_LOWERMOD) * 4];
|
2008-10-06 23:02:12 +02:00
|
|
|
|
2008-10-06 23:33:49 +02:00
|
|
|
int wrapbracket = (format & TERMKEY_FORMAT_WRAPBRACKET) &&
|
|
|
|
(key->type != TERMKEY_TYPE_UNICODE || key->modifiers != 0);
|
|
|
|
|
2013-08-26 01:56:07 +02:00
|
|
|
char sep = (format & TERMKEY_FORMAT_SPACEMOD) ? ' ' : '-';
|
|
|
|
|
2013-08-26 03:35:40 +02:00
|
|
|
if(format & TERMKEY_FORMAT_CARETCTRL &&
|
2008-12-10 02:34:40 +01:00
|
|
|
key->type == TERMKEY_TYPE_UNICODE &&
|
|
|
|
key->modifiers == TERMKEY_KEYMOD_CTRL) {
|
|
|
|
long codepoint = key->code.codepoint;
|
|
|
|
|
|
|
|
// Handle some of the special casesfirst
|
|
|
|
if(codepoint >= 'a' && codepoint <= 'z') {
|
|
|
|
l = snprintf(buffer + pos, len - pos, wrapbracket ? "<^%c>" : "^%c", (char)codepoint - 0x20);
|
|
|
|
if(l <= 0) return pos;
|
2009-11-24 00:59:46 +01:00
|
|
|
pos += l;
|
2008-12-10 02:34:40 +01:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
else if((codepoint >= '@' && codepoint < 'A') ||
|
|
|
|
(codepoint > 'Z' && codepoint <= '_')) {
|
|
|
|
l = snprintf(buffer + pos, len - pos, wrapbracket ? "<^%c>" : "^%c", (char)codepoint);
|
|
|
|
if(l <= 0) return pos;
|
2009-11-24 00:59:46 +01:00
|
|
|
pos += l;
|
2008-12-10 02:34:40 +01:00
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-06 23:33:49 +02:00
|
|
|
if(wrapbracket) {
|
|
|
|
l = snprintf(buffer + pos, len - pos, "<");
|
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
2008-08-21 21:20:07 +02:00
|
|
|
if(key->modifiers & TERMKEY_KEYMOD_ALT) {
|
2013-08-26 01:56:07 +02:00
|
|
|
l = snprintf(buffer + pos, len - pos, "%s%c", mods->alt, sep);
|
2008-08-21 21:20:07 +02:00
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
2008-10-06 23:39:00 +02:00
|
|
|
if(key->modifiers & TERMKEY_KEYMOD_CTRL) {
|
2013-08-26 01:56:07 +02:00
|
|
|
l = snprintf(buffer + pos, len - pos, "%s%c", mods->ctrl, sep);
|
2008-10-06 23:39:00 +02:00
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
2008-08-21 21:20:07 +02:00
|
|
|
if(key->modifiers & TERMKEY_KEYMOD_SHIFT) {
|
2013-08-26 01:56:07 +02:00
|
|
|
l = snprintf(buffer + pos, len - pos, "%s%c", mods->shift, sep);
|
2008-08-21 21:20:07 +02:00
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(key->type) {
|
|
|
|
case TERMKEY_TYPE_UNICODE:
|
2011-03-31 15:07:55 +02:00
|
|
|
if(!key->utf8[0]) // In case of user-supplied key structures
|
|
|
|
fill_utf8(key);
|
2008-08-21 21:20:07 +02:00
|
|
|
l = snprintf(buffer + pos, len - pos, "%s", key->utf8);
|
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_KEYSYM:
|
2013-08-26 02:32:35 +02:00
|
|
|
{
|
|
|
|
const char *name = termkey_get_keyname(tk, key->code.sym);
|
|
|
|
if(format & TERMKEY_FORMAT_LOWERSPACE)
|
|
|
|
l = snprint_cameltospaces(buffer + pos, len - pos, name);
|
|
|
|
else
|
|
|
|
l = snprintf(buffer + pos, len - pos, "%s", name);
|
|
|
|
}
|
2008-08-21 21:20:07 +02:00
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_FUNCTION:
|
2013-08-26 02:32:35 +02:00
|
|
|
l = snprintf(buffer + pos, len - pos, "%c%d",
|
|
|
|
(format & TERMKEY_FORMAT_LOWERSPACE ? 'f' : 'F'), key->code.number);
|
2008-08-21 21:20:07 +02:00
|
|
|
break;
|
2009-11-24 02:22:55 +01:00
|
|
|
case TERMKEY_TYPE_MOUSE:
|
2009-11-27 15:36:29 +01:00
|
|
|
{
|
|
|
|
TermKeyMouseEvent ev;
|
|
|
|
int button;
|
2009-11-27 16:07:05 +01:00
|
|
|
int line, col;
|
|
|
|
termkey_interpret_mouse(tk, key, &ev, &button, &line, &col);
|
2009-11-27 15:36:29 +01:00
|
|
|
|
2013-07-15 02:03:35 +02:00
|
|
|
static const char *evnames[] = { "Unknown", "Press", "Drag", "Release" };
|
2009-11-27 15:36:29 +01:00
|
|
|
|
|
|
|
l = snprintf(buffer + pos, len - pos, "Mouse%s(%d)",
|
|
|
|
evnames[ev], button);
|
2009-11-27 16:07:05 +01:00
|
|
|
|
|
|
|
if(format & TERMKEY_FORMAT_MOUSE_POS) {
|
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
|
2011-06-16 18:07:47 +02:00
|
|
|
l = snprintf(buffer + pos, len - pos, " @ (%u,%u)", col, line);
|
2009-11-27 16:07:05 +01:00
|
|
|
}
|
2009-11-27 15:36:29 +01:00
|
|
|
}
|
2009-11-24 02:22:55 +01:00
|
|
|
break;
|
2012-04-24 18:27:48 +02:00
|
|
|
case TERMKEY_TYPE_POSITION:
|
|
|
|
l = snprintf(buffer + pos, len - pos, "Position");
|
|
|
|
break;
|
2012-11-30 17:34:47 +01:00
|
|
|
case TERMKEY_TYPE_MODEREPORT:
|
|
|
|
{
|
|
|
|
int initial, mode, value;
|
|
|
|
termkey_interpret_modereport(tk, key, &initial, &mode, &value);
|
|
|
|
if(initial)
|
|
|
|
l = snprintf(buffer + pos, len - pos, "Mode(%c%d=%d)", initial, mode, value);
|
|
|
|
else
|
|
|
|
l = snprintf(buffer + pos, len - pos, "Mode(%d=%d)", mode, value);
|
|
|
|
}
|
2012-11-30 15:35:23 +01:00
|
|
|
case TERMKEY_TYPE_UNKNOWN_CSI:
|
|
|
|
l = snprintf(buffer + pos, len - pos, "CSI %c", key->code.number & 0xff);
|
|
|
|
break;
|
2008-08-21 21:20:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
|
2008-10-06 23:33:49 +02:00
|
|
|
if(wrapbracket) {
|
|
|
|
l = snprintf(buffer + pos, len - pos, ">");
|
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
2008-08-21 21:20:07 +02:00
|
|
|
return pos;
|
|
|
|
}
|
2011-04-01 01:50:51 +02:00
|
|
|
|
2013-07-15 02:03:35 +02:00
|
|
|
const char *termkey_strpkey(TermKey *tk, const char *str, TermKeyKey *key, TermKeyFormat format)
|
2011-04-01 01:50:51 +02:00
|
|
|
{
|
|
|
|
struct modnames *mods = &modnames[!!(format & TERMKEY_FORMAT_LONGMOD) +
|
2013-08-26 02:14:35 +02:00
|
|
|
!!(format & TERMKEY_FORMAT_ALTISMETA) * 2 +
|
|
|
|
!!(format & TERMKEY_FORMAT_LOWERMOD) * 4];
|
2011-04-01 01:50:51 +02:00
|
|
|
|
|
|
|
key->modifiers = 0;
|
|
|
|
|
|
|
|
if((format & TERMKEY_FORMAT_CARETCTRL) && str[0] == '^' && str[1]) {
|
2011-04-01 14:49:20 +02:00
|
|
|
str = termkey_strpkey(tk, str+1, key, format & ~TERMKEY_FORMAT_CARETCTRL);
|
2011-04-01 01:50:51 +02:00
|
|
|
|
2011-04-01 14:49:20 +02:00
|
|
|
if(!str ||
|
|
|
|
key->type != TERMKEY_TYPE_UNICODE ||
|
|
|
|
key->code.codepoint < '@' || key->code.codepoint > '_' ||
|
|
|
|
key->modifiers != 0)
|
|
|
|
return NULL;
|
2011-04-01 01:50:51 +02:00
|
|
|
|
2011-04-01 11:41:44 +02:00
|
|
|
if(key->code.codepoint >= 'A' && key->code.codepoint <= 'Z')
|
|
|
|
key->code.codepoint += 0x20;
|
2011-04-01 01:50:51 +02:00
|
|
|
key->modifiers = TERMKEY_KEYMOD_CTRL;
|
|
|
|
fill_utf8(key);
|
2011-04-01 14:49:20 +02:00
|
|
|
return (char *)str;
|
2011-04-01 01:50:51 +02:00
|
|
|
}
|
|
|
|
|
2013-08-26 01:56:07 +02:00
|
|
|
const char *sep_at;
|
2011-04-01 01:50:51 +02:00
|
|
|
|
2013-08-26 01:56:07 +02:00
|
|
|
while((sep_at = strchr(str, (format & TERMKEY_FORMAT_SPACEMOD) ? ' ' : '-'))) {
|
|
|
|
size_t n = sep_at - str;
|
2011-04-01 01:50:51 +02:00
|
|
|
|
|
|
|
if(n == strlen(mods->alt) && strncmp(mods->alt, str, n) == 0)
|
|
|
|
key->modifiers |= TERMKEY_KEYMOD_ALT;
|
|
|
|
else if(n == strlen(mods->ctrl) && strncmp(mods->ctrl, str, n) == 0)
|
|
|
|
key->modifiers |= TERMKEY_KEYMOD_CTRL;
|
|
|
|
else if(n == strlen(mods->shift) && strncmp(mods->shift, str, n) == 0)
|
|
|
|
key->modifiers |= TERMKEY_KEYMOD_SHIFT;
|
|
|
|
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
2013-08-26 01:56:07 +02:00
|
|
|
str = sep_at + 1;
|
2011-04-01 01:50:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t nbytes;
|
2012-01-20 18:01:38 +01:00
|
|
|
ssize_t snbytes;
|
2013-07-15 02:03:35 +02:00
|
|
|
const char *endstr;
|
2011-04-01 01:50:51 +02:00
|
|
|
|
2013-08-26 03:23:19 +02:00
|
|
|
if((endstr = termkey_lookup_keyname_format(tk, str, &key->code.sym, format))) {
|
2011-04-01 01:50:51 +02:00
|
|
|
key->type = TERMKEY_TYPE_KEYSYM;
|
2011-04-01 14:49:20 +02:00
|
|
|
str = endstr;
|
2011-04-01 01:50:51 +02:00
|
|
|
}
|
2012-01-20 18:01:38 +01:00
|
|
|
else if(sscanf(str, "F%d%zn", &key->code.number, &snbytes) == 1) {
|
2011-04-01 11:48:15 +02:00
|
|
|
key->type = TERMKEY_TYPE_FUNCTION;
|
2012-01-20 18:01:38 +01:00
|
|
|
str += snbytes;
|
2011-04-01 11:48:15 +02:00
|
|
|
}
|
2011-04-07 21:19:34 +02:00
|
|
|
// Unicode must be last
|
2013-07-15 02:03:35 +02:00
|
|
|
else if(parse_utf8((unsigned const char *)str, strlen(str), &key->code.codepoint, &nbytes) == TERMKEY_RES_KEY) {
|
2011-04-07 21:19:34 +02:00
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
|
|
|
fill_utf8(key);
|
|
|
|
str += nbytes;
|
|
|
|
}
|
2011-04-01 11:48:15 +02:00
|
|
|
// TODO: Consider mouse events?
|
2011-04-01 14:49:20 +02:00
|
|
|
else
|
|
|
|
return NULL;
|
2011-04-01 01:50:51 +02:00
|
|
|
|
2011-08-27 20:59:02 +02:00
|
|
|
termkey_canonicalise(tk, key);
|
|
|
|
|
2011-04-01 14:49:20 +02:00
|
|
|
return (char *)str;
|
2011-04-01 01:50:51 +02:00
|
|
|
}
|
2011-04-02 16:07:30 +02:00
|
|
|
|
2011-09-06 23:49:05 +02:00
|
|
|
int termkey_keycmp(TermKey *tk, const TermKeyKey *key1p, const TermKeyKey *key2p)
|
2011-04-02 16:07:30 +02:00
|
|
|
{
|
2011-09-06 23:49:05 +02:00
|
|
|
/* Copy the key structs since we'll be modifying them */
|
|
|
|
TermKeyKey key1 = *key1p, key2 = *key2p;
|
2011-04-02 16:07:30 +02:00
|
|
|
|
2011-09-06 23:49:05 +02:00
|
|
|
termkey_canonicalise(tk, &key1);
|
|
|
|
termkey_canonicalise(tk, &key2);
|
|
|
|
|
|
|
|
if(key1.type != key2.type)
|
|
|
|
return key1.type - key2.type;
|
|
|
|
|
|
|
|
switch(key1.type) {
|
2011-04-02 16:07:30 +02:00
|
|
|
case TERMKEY_TYPE_UNICODE:
|
2011-09-06 23:49:05 +02:00
|
|
|
if(key1.code.codepoint != key2.code.codepoint)
|
|
|
|
return key1.code.codepoint - key2.code.codepoint;
|
2012-04-24 18:27:48 +02:00
|
|
|
break;
|
2011-04-02 16:07:30 +02:00
|
|
|
case TERMKEY_TYPE_KEYSYM:
|
2011-09-06 23:49:05 +02:00
|
|
|
if(key1.code.sym != key2.code.sym)
|
|
|
|
return key1.code.sym - key2.code.sym;
|
2012-04-24 18:27:48 +02:00
|
|
|
break;
|
2011-04-02 16:07:30 +02:00
|
|
|
case TERMKEY_TYPE_FUNCTION:
|
2012-11-30 15:35:23 +01:00
|
|
|
case TERMKEY_TYPE_UNKNOWN_CSI:
|
2011-09-06 23:49:05 +02:00
|
|
|
if(key1.code.number != key2.code.number)
|
|
|
|
return key1.code.number - key2.code.number;
|
2012-04-24 18:27:48 +02:00
|
|
|
break;
|
2011-04-02 16:07:30 +02:00
|
|
|
case TERMKEY_TYPE_MOUSE:
|
|
|
|
{
|
2011-09-06 23:49:05 +02:00
|
|
|
int cmp = strncmp(key1.code.mouse, key2.code.mouse, 4);
|
2011-04-02 16:07:30 +02:00
|
|
|
if(cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
}
|
2012-04-24 18:27:48 +02:00
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_POSITION:
|
|
|
|
{
|
|
|
|
int line1, col1, line2, col2;
|
|
|
|
termkey_interpret_position(tk, &key1, &line1, &col1);
|
|
|
|
termkey_interpret_position(tk, &key2, &line2, &col2);
|
|
|
|
if(line1 != line2)
|
|
|
|
return line1 - line2;
|
|
|
|
return col1 - col2;
|
|
|
|
}
|
|
|
|
break;
|
2012-11-30 17:34:47 +01:00
|
|
|
case TERMKEY_TYPE_MODEREPORT:
|
|
|
|
{
|
|
|
|
int initial1, initial2, mode1, mode2, value1, value2;
|
|
|
|
termkey_interpret_modereport(tk, &key1, &initial1, &mode1, &value1);
|
|
|
|
termkey_interpret_modereport(tk, &key2, &initial2, &mode2, &value2);
|
|
|
|
if(initial1 != initial2)
|
|
|
|
return initial1 - initial2;
|
|
|
|
if(mode1 != mode2)
|
|
|
|
return mode1 - mode2;
|
|
|
|
return value1 - value2;
|
|
|
|
}
|
2011-04-02 16:07:30 +02:00
|
|
|
}
|
|
|
|
|
2011-09-06 23:49:05 +02:00
|
|
|
return key1.modifiers - key2.modifiers;
|
2011-04-02 16:07:30 +02:00
|
|
|
}
|