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
|
|
|
|
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-10-07 04:17:06 +02:00
|
|
|
static struct termkey_driver *drivers[] = {
|
|
|
|
&termkey_driver_csi,
|
2008-10-09 21:23:45 +02:00
|
|
|
&termkey_driver_ti,
|
2008-10-07 04:17:06 +02:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2008-10-07 23:22:59 +02:00
|
|
|
// Forwards for the "protected" methods
|
2008-10-08 00:57:01 +02:00
|
|
|
static void eat_bytes(termkey_t *tk, size_t count);
|
2008-10-10 01:50:56 +02:00
|
|
|
static void emit_codepoint(termkey_t *tk, long codepoint, termkey_key *key);
|
2008-11-02 15:43:19 +01:00
|
|
|
static termkey_result getkey_simple(termkey_t *tk, termkey_key *key, int force);
|
2008-10-08 01:12:41 +02:00
|
|
|
|
|
|
|
static termkey_keysym register_c0(termkey_t *tk, termkey_keysym sym, unsigned char ctrl, const char *name);
|
|
|
|
static termkey_keysym register_c0_full(termkey_t *tk, termkey_keysym 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 {
|
|
|
|
termkey_keysym sym;
|
|
|
|
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 },
|
|
|
|
};
|
|
|
|
|
2008-02-10 21:22:33 +01:00
|
|
|
termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2008-02-10 18:34:05 +01:00
|
|
|
termkey_t *tk = malloc(sizeof(*tk));
|
|
|
|
if(!tk)
|
|
|
|
return NULL;
|
2008-02-07 01:58:41 +01:00
|
|
|
|
2008-02-09 20:30:37 +01:00
|
|
|
if(!(flags & (TERMKEY_FLAG_RAW|TERMKEY_FLAG_UTF8))) {
|
|
|
|
int locale_is_utf8 = 0;
|
|
|
|
char *e;
|
|
|
|
|
|
|
|
if((e = getenv("LANG")) && strstr(e, "UTF-8"))
|
|
|
|
locale_is_utf8 = 1;
|
|
|
|
|
|
|
|
if(!locale_is_utf8 && (e = getenv("LC_MESSAGES")) && strstr(e, "UTF-8"))
|
|
|
|
locale_is_utf8 = 1;
|
|
|
|
|
|
|
|
if(!locale_is_utf8 && (e = getenv("LC_ALL")) && strstr(e, "UTF-8"))
|
|
|
|
locale_is_utf8 = 1;
|
|
|
|
|
|
|
|
if(locale_is_utf8)
|
|
|
|
flags |= TERMKEY_FLAG_UTF8;
|
|
|
|
else
|
|
|
|
flags |= TERMKEY_FLAG_RAW;
|
|
|
|
}
|
|
|
|
|
2008-02-07 01:58:41 +01:00
|
|
|
tk->fd = fd;
|
|
|
|
tk->flags = flags;
|
|
|
|
|
2008-02-10 18:34:05 +01:00
|
|
|
tk->buffer = malloc(buffsize);
|
2008-10-10 00:19:10 +02:00
|
|
|
if(!tk->buffer)
|
|
|
|
goto abort_free_tk;
|
2008-02-10 18:34:05 +01:00
|
|
|
|
2008-02-10 20:15:29 +01:00
|
|
|
tk->buffstart = 0;
|
|
|
|
tk->buffcount = 0;
|
2008-02-07 01:58:41 +01:00
|
|
|
tk->buffsize = buffsize;
|
|
|
|
|
2008-02-23 21:26:04 +01:00
|
|
|
tk->restore_termios_valid = 0;
|
|
|
|
|
2008-02-10 21:22:33 +01:00
|
|
|
tk->waittime = waittime;
|
|
|
|
|
2008-02-07 01:58:41 +01:00
|
|
|
tk->is_closed = 0;
|
|
|
|
|
2008-02-10 18:34:05 +01:00
|
|
|
tk->nkeynames = 64;
|
|
|
|
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 01:12:41 +02:00
|
|
|
for(i = 0; i < 32; i++)
|
|
|
|
tk->c0[i].sym = TERMKEY_SYM_NONE;
|
|
|
|
|
2008-10-08 01:36:27 +02:00
|
|
|
tk->method.eat_bytes = &eat_bytes;
|
2008-10-08 01:12:41 +02:00
|
|
|
tk->method.emit_codepoint = &emit_codepoint;
|
2008-10-08 01:36:27 +02:00
|
|
|
tk->method.getkey_simple = &getkey_simple;
|
2008-10-08 01:12:41 +02:00
|
|
|
|
2008-10-08 02:52:53 +02:00
|
|
|
for(i = 0; keynames[i].name; i++)
|
|
|
|
termkey_register_keyname(tk, keynames[i].sym, keynames[i].name);
|
|
|
|
|
|
|
|
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
|
|
|
|
2008-10-09 21:23:45 +02:00
|
|
|
const char *term = getenv("TERM");
|
|
|
|
|
2008-10-07 04:17:06 +02:00
|
|
|
for(i = 0; drivers[i]; i++) {
|
2008-10-09 21:23:45 +02:00
|
|
|
void *driver_info = (*drivers[i]->new_driver)(tk, term);
|
2008-10-07 04:17:06 +02:00
|
|
|
if(!driver_info)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tk->driver = *(drivers[i]);
|
|
|
|
tk->driver_info = driver_info;
|
2008-10-08 01:53:59 +02:00
|
|
|
break;
|
2008-10-07 04:17:06 +02:00
|
|
|
}
|
2008-10-07 00:36:14 +02:00
|
|
|
|
2008-10-07 04:17:06 +02:00
|
|
|
if(!tk->driver_info) {
|
|
|
|
fprintf(stderr, "Unable to find a terminal driver\n");
|
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
|
|
|
|
2008-02-23 21:26:04 +01:00
|
|
|
if(!(flags & TERMKEY_FLAG_NOTERMIOS)) {
|
|
|
|
struct termios termios;
|
|
|
|
if(tcgetattr(fd, &termios) == 0) {
|
|
|
|
tk->restore_termios = termios;
|
|
|
|
tk->restore_termios_valid = 1;
|
|
|
|
|
|
|
|
termios.c_iflag &= ~(IXON|INLCR|ICRNL);
|
|
|
|
termios.c_lflag &= ~(ICANON|ECHO|ISIG);
|
|
|
|
|
|
|
|
tcsetattr(fd, TCSANOW, &termios);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-02 18:54:24 +01:00
|
|
|
if(tk->driver.start_driver)
|
|
|
|
(*tk->driver.start_driver)(tk);
|
|
|
|
|
2008-02-07 01:58:41 +01:00
|
|
|
return tk;
|
2008-10-10 00:19:10 +02:00
|
|
|
|
|
|
|
abort_free_keynames:
|
|
|
|
free(tk->keynames);
|
|
|
|
|
|
|
|
abort_free_buffer:
|
|
|
|
free(tk->buffer);
|
|
|
|
|
|
|
|
abort_free_tk:
|
|
|
|
free(tk);
|
|
|
|
|
|
|
|
return NULL;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
termkey_t *termkey_new(int fd, int flags)
|
|
|
|
{
|
2008-02-10 21:22:33 +01:00
|
|
|
return termkey_new_full(fd, flags, 256, 50);
|
|
|
|
}
|
|
|
|
|
2008-02-23 21:07:48 +01:00
|
|
|
void termkey_free(termkey_t *tk)
|
|
|
|
{
|
|
|
|
free(tk->buffer); tk->buffer = NULL;
|
|
|
|
free(tk->keynames); tk->keynames = NULL;
|
2008-10-07 00:27:19 +02:00
|
|
|
|
2008-10-07 00:36:14 +02:00
|
|
|
(*tk->driver.free_driver)(tk->driver_info);
|
2008-10-07 00:27:19 +02:00
|
|
|
tk->driver_info = NULL; /* Be nice to GC'ers, etc */
|
2008-02-23 21:07:48 +01:00
|
|
|
|
|
|
|
free(tk);
|
|
|
|
}
|
|
|
|
|
2008-02-23 21:26:04 +01:00
|
|
|
void termkey_destroy(termkey_t *tk)
|
|
|
|
{
|
2008-11-02 18:54:24 +01:00
|
|
|
if(tk->driver.stop_driver)
|
|
|
|
(*tk->driver.stop_driver)(tk);
|
|
|
|
|
2008-02-23 21:26:04 +01:00
|
|
|
if(tk->restore_termios_valid)
|
|
|
|
tcsetattr(tk->fd, TCSANOW, &tk->restore_termios);
|
|
|
|
|
|
|
|
termkey_free(tk);
|
|
|
|
}
|
|
|
|
|
2008-02-10 21:22:33 +01:00
|
|
|
void termkey_setwaittime(termkey_t *tk, int msec)
|
|
|
|
{
|
|
|
|
tk->waittime = msec;
|
|
|
|
}
|
|
|
|
|
|
|
|
int termkey_getwaittime(termkey_t *tk)
|
|
|
|
{
|
|
|
|
return tk->waittime;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 00:57:01 +02:00
|
|
|
static void eat_bytes(termkey_t *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;
|
|
|
|
|
|
|
|
size_t halfsize = tk->buffsize / 2;
|
|
|
|
|
|
|
|
if(tk->buffstart > halfsize) {
|
|
|
|
memcpy(tk->buffer, tk->buffer + halfsize, halfsize);
|
|
|
|
tk->buffstart -= halfsize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-10 01:50:56 +02:00
|
|
|
static inline 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;
|
|
|
|
}
|
|
|
|
|
2008-10-08 01:12:41 +02:00
|
|
|
static void fill_utf8(termkey_key *key)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-10 01:50:56 +02:00
|
|
|
static void emit_codepoint(termkey_t *tk, long codepoint, termkey_key *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;
|
|
|
|
key->code.codepoint = codepoint + 0x40;
|
|
|
|
key->modifiers = TERMKEY_KEYMOD_CTRL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
key->type = TERMKEY_TYPE_KEYSYM;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(codepoint == 0x20 && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) {
|
|
|
|
// ASCII space
|
|
|
|
key->type = TERMKEY_TYPE_KEYSYM;
|
|
|
|
key->code.sym = TERMKEY_SYM_SPACE;
|
|
|
|
key->modifiers = 0;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(key->type == TERMKEY_TYPE_UNICODE)
|
|
|
|
fill_utf8(key);
|
|
|
|
}
|
|
|
|
|
2008-10-08 01:36:27 +02:00
|
|
|
#define UTF8_INVALID 0xFFFD
|
|
|
|
|
|
|
|
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
|
|
|
|
2008-11-02 15:43:19 +01:00
|
|
|
static termkey_result getkey_simple(termkey_t *tk, termkey_key *key, int force)
|
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);
|
|
|
|
(*tk->method.eat_bytes)(tk, 1);
|
|
|
|
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
|
|
|
|
termkey_result metakey_result = (*tk->driver.getkey)(tk, key, force);
|
|
|
|
|
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;
|
|
|
|
(*tk->method.eat_bytes)(tk, 1);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TERMKEY_RES_NONE:
|
|
|
|
case TERMKEY_RES_EOF:
|
|
|
|
case TERMKEY_RES_AGAIN:
|
|
|
|
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);
|
|
|
|
(*tk->method.eat_bytes)(tk, 1);
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
else if(tk->flags & TERMKEY_FLAG_UTF8) {
|
|
|
|
// Some UTF-8
|
|
|
|
int nbytes;
|
2008-10-10 01:50:56 +02:00
|
|
|
long codepoint;
|
2008-10-08 01:36:27 +02:00
|
|
|
|
|
|
|
key->type = TERMKEY_TYPE_UNICODE;
|
|
|
|
key->modifiers = 0;
|
|
|
|
|
|
|
|
if(b0 < 0xc0) {
|
|
|
|
// Starts with a continuation byte - that's not right
|
|
|
|
(*tk->method.emit_codepoint)(tk, UTF8_INVALID, key);
|
|
|
|
(*tk->method.eat_bytes)(tk, 1);
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xe0) {
|
|
|
|
nbytes = 2;
|
|
|
|
codepoint = b0 & 0x1f;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xf0) {
|
|
|
|
nbytes = 3;
|
|
|
|
codepoint = b0 & 0x0f;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xf8) {
|
|
|
|
nbytes = 4;
|
|
|
|
codepoint = b0 & 0x07;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xfc) {
|
|
|
|
nbytes = 5;
|
|
|
|
codepoint = b0 & 0x03;
|
|
|
|
}
|
|
|
|
else if(b0 < 0xfe) {
|
|
|
|
nbytes = 6;
|
|
|
|
codepoint = b0 & 0x01;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
(*tk->method.emit_codepoint)(tk, UTF8_INVALID, key);
|
|
|
|
(*tk->method.eat_bytes)(tk, 1);
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
|
2008-11-03 22:10:14 +01:00
|
|
|
if(tk->buffcount < nbytes) {
|
|
|
|
if(!force)
|
|
|
|
return TERMKEY_RES_AGAIN;
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
(*tk->method.emit_codepoint)(tk, UTF8_INVALID, key);
|
|
|
|
(*tk->method.eat_bytes)(tk, tk->buffcount);
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
2008-10-08 01:36:27 +02:00
|
|
|
|
|
|
|
for(int b = 1; b < nbytes; b++) {
|
|
|
|
unsigned char cb = CHARAT(b);
|
|
|
|
if(cb < 0x80 || cb >= 0xc0) {
|
|
|
|
(*tk->method.emit_codepoint)(tk, UTF8_INVALID, key);
|
|
|
|
(*tk->method.eat_bytes)(tk, b - 1);
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
codepoint <<= 6;
|
|
|
|
codepoint |= cb & 0x3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for overlong sequences
|
|
|
|
if(nbytes > utf8_seqlen(codepoint))
|
|
|
|
codepoint = UTF8_INVALID;
|
|
|
|
|
|
|
|
// Check for UTF-16 surrogates or invalid codepoints
|
|
|
|
if((codepoint >= 0xD800 && codepoint <= 0xDFFF) ||
|
|
|
|
codepoint == 0xFFFE ||
|
|
|
|
codepoint == 0xFFFF)
|
|
|
|
codepoint = UTF8_INVALID;
|
|
|
|
|
|
|
|
(*tk->method.emit_codepoint)(tk, codepoint, key);
|
|
|
|
(*tk->method.eat_bytes)(tk, nbytes);
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
|
|
|
|
(*tk->method.eat_bytes)(tk, 1);
|
|
|
|
|
|
|
|
return TERMKEY_RES_KEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-07 02:17:59 +01:00
|
|
|
termkey_result termkey_getkey(termkey_t *tk, termkey_key *key)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2008-10-08 01:44:33 +02:00
|
|
|
return (*tk->driver.getkey)(tk, key, 0);
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2008-02-10 21:22:33 +01:00
|
|
|
termkey_result termkey_getkey_force(termkey_t *tk, termkey_key *key)
|
|
|
|
{
|
2008-10-08 01:44:33 +02:00
|
|
|
return (*tk->driver.getkey)(tk, key, 1);
|
2008-02-10 21:22:33 +01:00
|
|
|
}
|
|
|
|
|
2008-02-07 02:17:59 +01:00
|
|
|
termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2008-02-10 21:22:33 +01:00
|
|
|
while(1) {
|
|
|
|
termkey_result ret = termkey_getkey(tk, key);
|
|
|
|
|
|
|
|
switch(ret) {
|
|
|
|
case TERMKEY_RES_KEY:
|
|
|
|
case TERMKEY_RES_EOF:
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
case TERMKEY_RES_NONE:
|
|
|
|
termkey_advisereadable(tk);
|
|
|
|
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;
|
|
|
|
|
|
|
|
fd.fd = tk->fd;
|
|
|
|
fd.events = POLLIN;
|
|
|
|
|
2008-11-03 22:08:34 +01:00
|
|
|
poll(&fd, 1, tk->waittime);
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen)
|
|
|
|
{
|
2008-02-10 20:15:29 +01:00
|
|
|
if(tk->buffstart + tk->buffcount + inputlen > tk->buffsize) {
|
2008-02-10 21:29:59 +01:00
|
|
|
while(tk->buffstart + tk->buffcount + inputlen > tk->buffsize)
|
|
|
|
tk->buffsize *= 2;
|
|
|
|
|
|
|
|
unsigned char *newbuffer = realloc(tk->buffer, tk->buffsize);
|
2008-10-10 00:53:35 +02:00
|
|
|
// TODO: Handle realloc() failure
|
2008-02-10 21:29:59 +01:00
|
|
|
tk->buffer = newbuffer;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Not strcpy just in case of NUL bytes
|
2008-02-10 20:15:29 +01:00
|
|
|
memcpy(tk->buffer + tk->buffstart + tk->buffcount, input, inputlen);
|
|
|
|
tk->buffcount += inputlen;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2008-02-10 20:23:18 +01:00
|
|
|
termkey_result termkey_advisereadable(termkey_t *tk)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
|
|
|
unsigned char buffer[64]; // Smaller than the default size
|
|
|
|
size_t len = read(tk->fd, buffer, sizeof buffer);
|
|
|
|
|
2008-02-09 20:01:49 +01:00
|
|
|
if(len == -1 && errno == EAGAIN)
|
2008-02-10 20:23:18 +01:00
|
|
|
return TERMKEY_RES_NONE;
|
|
|
|
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 {
|
2008-02-07 01:58:41 +01:00
|
|
|
termkey_pushinput(tk, buffer, len);
|
2008-02-10 20:23:18 +01:00
|
|
|
return TERMKEY_RES_AGAIN;
|
|
|
|
}
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2008-08-21 21:05:17 +02:00
|
|
|
termkey_keysym termkey_register_keyname(termkey_t *tk, termkey_keysym 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));
|
2008-10-10 00:53:35 +02:00
|
|
|
// TODO: Handle realloc() failure
|
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
|
|
|
}
|
|
|
|
|
2008-08-21 21:05:17 +02:00
|
|
|
const char *termkey_get_keyname(termkey_t *tk, termkey_keysym 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";
|
|
|
|
}
|
|
|
|
|
2008-10-08 01:12:41 +02:00
|
|
|
static termkey_keysym register_c0(termkey_t *tk, termkey_keysym sym, unsigned char ctrl, const char *name)
|
|
|
|
{
|
|
|
|
return register_c0_full(tk, sym, 0, 0, ctrl, name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static termkey_keysym register_c0_full(termkey_t *tk, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char ctrl, const char *name)
|
|
|
|
{
|
|
|
|
if(ctrl >= 0x20) {
|
|
|
|
fprintf(stderr, "Cannot register C0 key at ctrl 0x%02x - out of bounds\n", ctrl);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2008-10-06 23:02:12 +02:00
|
|
|
size_t termkey_snprint_key(termkey_t *tk, char *buffer, size_t len, termkey_key *key, termkey_format format)
|
2008-08-21 21:20:07 +02:00
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t l;
|
|
|
|
|
2008-10-06 23:02:12 +02:00
|
|
|
int longmod = format & TERMKEY_FORMAT_LONGMOD;
|
|
|
|
|
2008-10-06 23:33:49 +02:00
|
|
|
int wrapbracket = (format & TERMKEY_FORMAT_WRAPBRACKET) &&
|
|
|
|
(key->type != TERMKEY_TYPE_UNICODE || key->modifiers != 0);
|
|
|
|
|
|
|
|
if(wrapbracket) {
|
|
|
|
l = snprintf(buffer + pos, len - pos, "<");
|
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
2008-10-06 23:22:09 +02:00
|
|
|
if(format & TERMKEY_FORMAT_CARETCTRL) {
|
|
|
|
if(key->type == TERMKEY_TYPE_UNICODE &&
|
|
|
|
key->modifiers == TERMKEY_KEYMOD_CTRL &&
|
|
|
|
key->code.number >= '@' &&
|
|
|
|
key->code.number <= '_') {
|
|
|
|
l = snprintf(buffer + pos, len - pos, "^");
|
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
goto do_codepoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-21 21:20:07 +02:00
|
|
|
if(key->modifiers & TERMKEY_KEYMOD_ALT) {
|
2008-10-06 23:22:09 +02:00
|
|
|
int altismeta = format & TERMKEY_FORMAT_ALTISMETA;
|
|
|
|
|
|
|
|
l = snprintf(buffer + pos, len - pos, longmod ? ( altismeta ? "Meta-" : "Alt-" )
|
|
|
|
: ( altismeta ? "M-" : "A-" ));
|
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) {
|
|
|
|
l = snprintf(buffer + pos, len - pos, longmod ? "Ctrl-" : "C-");
|
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
2008-08-21 21:20:07 +02:00
|
|
|
if(key->modifiers & TERMKEY_KEYMOD_SHIFT) {
|
|
|
|
l = snprintf(buffer + pos, len - pos, longmod ? "Shift-" : "S-");
|
|
|
|
if(l <= 0) return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
2008-10-06 23:22:09 +02:00
|
|
|
do_codepoint:
|
|
|
|
|
2008-08-21 21:20:07 +02:00
|
|
|
switch(key->type) {
|
|
|
|
case TERMKEY_TYPE_UNICODE:
|
|
|
|
l = snprintf(buffer + pos, len - pos, "%s", key->utf8);
|
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_KEYSYM:
|
|
|
|
l = snprintf(buffer + pos, len - pos, "%s", termkey_get_keyname(tk, key->code.sym));
|
|
|
|
break;
|
|
|
|
case TERMKEY_TYPE_FUNCTION:
|
|
|
|
l = snprintf(buffer + pos, len - pos, "F%d", key->code.number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|