2014-10-14 00:08:15 +02:00
|
|
|
#include "termo.h"
|
|
|
|
#include "termo-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>
|
2014-03-05 02:23:16 +01:00
|
|
|
#include <strings.h>
|
2014-09-28 03:51:45 +02:00
|
|
|
#include <langinfo.h>
|
2008-02-07 01:58:41 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
void
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_check_version (int major, int minor)
|
2008-11-16 13:40:14 +01:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
if (major != TERMO_VERSION_MAJOR)
|
|
|
|
fprintf (stderr, "libtermo major version mismatch;"
|
2014-09-28 03:51:45 +02:00
|
|
|
" %d (wants) != %d (library)\n",
|
2014-10-14 00:08:15 +02:00
|
|
|
major, TERMO_VERSION_MAJOR);
|
|
|
|
else if (minor > TERMO_VERSION_MINOR)
|
|
|
|
fprintf (stderr, "libtermo minor version mismatch;"
|
2014-09-28 03:51:45 +02:00
|
|
|
" %d (wants) > %d (library)\n",
|
2014-10-14 00:08:15 +02:00
|
|
|
minor, TERMO_VERSION_MINOR);
|
2014-09-28 03:51:45 +02:00
|
|
|
else
|
|
|
|
return;
|
|
|
|
exit (1);
|
2008-11-16 13:40:14 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_driver_t *drivers[] =
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
&termo_driver_ti,
|
|
|
|
&termo_driver_csi,
|
2014-09-23 01:38:08 +02:00
|
|
|
NULL,
|
2008-10-07 04:17:06 +02:00
|
|
|
};
|
|
|
|
|
2008-10-07 23:22:59 +02:00
|
|
|
// Forwards for the "protected" methods
|
2014-10-14 00:08:15 +02:00
|
|
|
static void emit_codepoint (termo_t *tk, uint32_t codepoint, termo_key_t *key);
|
|
|
|
static termo_result_t peekkey_simple (termo_t *tk,
|
|
|
|
termo_key_t *key, int force, size_t *nbytes);
|
|
|
|
static termo_result_t peekkey_mouse (termo_t *tk,
|
|
|
|
termo_key_t *key, size_t *nbytes);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_sym_t register_c0 (termo_t *tk, termo_sym_t sym,
|
2014-09-23 01:38:08 +02:00
|
|
|
unsigned char ctrl, const char *name);
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_sym_t register_c0_full (termo_t *tk, termo_sym_t sym,
|
2014-09-23 01:38:08 +02:00
|
|
|
int modifier_set, int modifier_mask, unsigned char ctrl, const char *name);
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_sym_t sym;
|
2014-09-23 01:38:08 +02:00
|
|
|
const char *name;
|
|
|
|
}
|
|
|
|
keynames[] =
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
{ TERMO_SYM_NONE, "NONE" },
|
|
|
|
{ TERMO_SYM_BACKSPACE, "Backspace" },
|
|
|
|
{ TERMO_SYM_TAB, "Tab" },
|
|
|
|
{ TERMO_SYM_ENTER, "Enter" },
|
|
|
|
{ TERMO_SYM_ESCAPE, "Escape" },
|
|
|
|
{ TERMO_SYM_SPACE, "Space" },
|
|
|
|
{ TERMO_SYM_DEL, "DEL" },
|
|
|
|
{ TERMO_SYM_UP, "Up" },
|
|
|
|
{ TERMO_SYM_DOWN, "Down" },
|
|
|
|
{ TERMO_SYM_LEFT, "Left" },
|
|
|
|
{ TERMO_SYM_RIGHT, "Right" },
|
|
|
|
{ TERMO_SYM_BEGIN, "Begin" },
|
|
|
|
{ TERMO_SYM_FIND, "Find" },
|
|
|
|
{ TERMO_SYM_INSERT, "Insert" },
|
|
|
|
{ TERMO_SYM_DELETE, "Delete" },
|
|
|
|
{ TERMO_SYM_SELECT, "Select" },
|
|
|
|
{ TERMO_SYM_PAGEUP, "PageUp" },
|
|
|
|
{ TERMO_SYM_PAGEDOWN, "PageDown" },
|
|
|
|
{ TERMO_SYM_HOME, "Home" },
|
|
|
|
{ TERMO_SYM_END, "End" },
|
|
|
|
{ TERMO_SYM_CANCEL, "Cancel" },
|
|
|
|
{ TERMO_SYM_CLEAR, "Clear" },
|
|
|
|
{ TERMO_SYM_CLOSE, "Close" },
|
|
|
|
{ TERMO_SYM_COMMAND, "Command" },
|
|
|
|
{ TERMO_SYM_COPY, "Copy" },
|
|
|
|
{ TERMO_SYM_EXIT, "Exit" },
|
|
|
|
{ TERMO_SYM_HELP, "Help" },
|
|
|
|
{ TERMO_SYM_MARK, "Mark" },
|
|
|
|
{ TERMO_SYM_MESSAGE, "Message" },
|
|
|
|
{ TERMO_SYM_MOVE, "Move" },
|
|
|
|
{ TERMO_SYM_OPEN, "Open" },
|
|
|
|
{ TERMO_SYM_OPTIONS, "Options" },
|
|
|
|
{ TERMO_SYM_PRINT, "Print" },
|
|
|
|
{ TERMO_SYM_REDO, "Redo" },
|
|
|
|
{ TERMO_SYM_REFERENCE, "Reference" },
|
|
|
|
{ TERMO_SYM_REFRESH, "Refresh" },
|
|
|
|
{ TERMO_SYM_REPLACE, "Replace" },
|
|
|
|
{ TERMO_SYM_RESTART, "Restart" },
|
|
|
|
{ TERMO_SYM_RESUME, "Resume" },
|
|
|
|
{ TERMO_SYM_SAVE, "Save" },
|
|
|
|
{ TERMO_SYM_SUSPEND, "Suspend" },
|
|
|
|
{ TERMO_SYM_UNDO, "Undo" },
|
|
|
|
{ TERMO_SYM_KP0, "KP0" },
|
|
|
|
{ TERMO_SYM_KP1, "KP1" },
|
|
|
|
{ TERMO_SYM_KP2, "KP2" },
|
|
|
|
{ TERMO_SYM_KP3, "KP3" },
|
|
|
|
{ TERMO_SYM_KP4, "KP4" },
|
|
|
|
{ TERMO_SYM_KP5, "KP5" },
|
|
|
|
{ TERMO_SYM_KP6, "KP6" },
|
|
|
|
{ TERMO_SYM_KP7, "KP7" },
|
|
|
|
{ TERMO_SYM_KP8, "KP8" },
|
|
|
|
{ TERMO_SYM_KP9, "KP9" },
|
|
|
|
{ TERMO_SYM_KPENTER, "KPEnter" },
|
|
|
|
{ TERMO_SYM_KPPLUS, "KPPlus" },
|
|
|
|
{ TERMO_SYM_KPMINUS, "KPMinus" },
|
|
|
|
{ TERMO_SYM_KPMULT, "KPMult" },
|
|
|
|
{ TERMO_SYM_KPDIV, "KPDiv" },
|
|
|
|
{ TERMO_SYM_KPCOMMA, "KPComma" },
|
|
|
|
{ TERMO_SYM_KPPERIOD, "KPPeriod" },
|
|
|
|
{ TERMO_SYM_KPEQUALS, "KPEquals" },
|
2014-10-18 19:11:08 +02:00
|
|
|
{ 0, NULL },
|
2008-10-08 02:52:53 +02:00
|
|
|
};
|
|
|
|
|
2009-11-27 15:50:58 +01:00
|
|
|
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-10-14 21:19:11 +02:00
|
|
|
// Some internal deubgging functions
|
2009-11-27 15:50:58 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
2014-10-14 00:08:15 +02:00
|
|
|
print_buffer (termo_t *tk)
|
2009-11-27 15:50:58 +01:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
size_t i;
|
2014-09-23 01:38:08 +02:00
|
|
|
for (i = 0; i < tk->buffcount && i < 20; i++)
|
|
|
|
fprintf (stderr, "%02x ", CHARAT (i));
|
|
|
|
if (tk->buffcount > 20)
|
|
|
|
fprintf (stderr, "...");
|
2009-11-27 15:50:58 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
2014-10-14 00:08:15 +02:00
|
|
|
print_key (termo_t *tk, termo_key_t *key)
|
2009-11-27 15:50:58 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
switch (key->type)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_KEY:
|
2014-09-28 03:51:45 +02:00
|
|
|
fprintf (stderr, "Unicode codepoint=U+%04lx multibyte='%s'",
|
|
|
|
(long) key->code.codepoint, key->multibyte);
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_FUNCTION:
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Function F%d", key->code.number);
|
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_KEYSYM:
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Keysym sym=%d(%s)",
|
2014-10-14 00:08:15 +02:00
|
|
|
key->code.sym, termo_get_keyname (tk, key->code.sym));
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_MOUSE:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_mouse_event_t ev;
|
2014-09-23 01:38:08 +02:00
|
|
|
int button, line, col;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_mouse (tk, key, &ev, &button, &line, &col);
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Mouse ev=%d button=%d pos=(%d,%d)\n",
|
|
|
|
ev, button, line, col);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_POSITION:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
int line, col;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_position (tk, key, &line, &col);
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Position report pos=(%d,%d)\n", line, col);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_MODEREPORT:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
int initial, mode, value;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_modereport (tk, key, &initial, &mode, &value);
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Mode report mode=%s %d val=%d\n",
|
|
|
|
initial == '?' ? "DEC" : "ANSI", mode, value);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_UNKNOWN_CSI:
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "unknown CSI\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
int m = key->modifiers;
|
|
|
|
fprintf (stderr, " mod=%s%s%s+%02x",
|
2014-10-14 00:08:15 +02:00
|
|
|
(m & TERMO_KEYMOD_CTRL ? "C" : ""),
|
|
|
|
(m & TERMO_KEYMOD_ALT ? "A" : ""),
|
|
|
|
(m & TERMO_KEYMOD_SHIFT ? "S" : ""),
|
|
|
|
m & ~(TERMO_KEYMOD_CTRL | TERMO_KEYMOD_ALT | TERMO_KEYMOD_SHIFT));
|
2009-11-27 15:50:58 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static const char *
|
2014-10-14 00:08:15 +02:00
|
|
|
res2str (termo_result_t res)
|
2009-11-27 15:50:58 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
static char errorbuffer[256];
|
|
|
|
|
|
|
|
switch (res)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_KEY:
|
|
|
|
return "TERMO_RES_KEY";
|
|
|
|
case TERMO_RES_EOF:
|
|
|
|
return "TERMO_RES_EOF";
|
|
|
|
case TERMO_RES_AGAIN:
|
|
|
|
return "TERMO_RES_AGAIN";
|
|
|
|
case TERMO_RES_NONE:
|
|
|
|
return "TERMO_RES_NONE";
|
|
|
|
case TERMO_RES_ERROR:
|
2014-09-23 01:38:08 +02:00
|
|
|
snprintf (errorbuffer, sizeof errorbuffer,
|
2014-10-14 00:08:15 +02:00
|
|
|
"TERMO_RES_ERROR(errno=%d)\n", errno);
|
2014-09-23 01:38:08 +02:00
|
|
|
return (const char*) errorbuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return "unknown";
|
2009-11-27 15:50:58 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// Similar to snprintf(str, size, "%s", src) except it turns CamelCase into
|
|
|
|
// space separated values
|
2014-09-23 01:38:08 +02:00
|
|
|
static int
|
|
|
|
snprint_cameltospaces (char *str, size_t size, const char *src)
|
2013-08-26 03:23:19 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
int prev_lower = 0;
|
|
|
|
size_t l = 0;
|
|
|
|
while (*src && l < size - 1)
|
|
|
|
{
|
|
|
|
if (isupper (*src) && prev_lower)
|
|
|
|
{
|
|
|
|
if (str)
|
|
|
|
str[l++] = ' ';
|
|
|
|
if (l >= size - 1)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev_lower = islower (*src);
|
|
|
|
str[l++] = tolower (*src++);
|
|
|
|
}
|
|
|
|
str[l] = 0;
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// For consistency with snprintf, return the number of bytes that would
|
|
|
|
// have been written, excluding '\0'
|
2014-09-23 01:38:08 +02:00
|
|
|
for (; *src; src++)
|
|
|
|
{
|
|
|
|
if (isupper (*src) && prev_lower)
|
|
|
|
l++;
|
|
|
|
prev_lower = islower (*src);
|
|
|
|
l++;
|
|
|
|
}
|
|
|
|
return l;
|
2013-08-26 03:23:19 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// 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
|
|
|
|
// n counts bytes of strcamel, not str
|
2014-09-23 01:38:08 +02:00
|
|
|
static int
|
|
|
|
strpncmp_camel (const char **strp, const char **strcamelp, size_t n)
|
2013-08-26 03:23:19 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
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;
|
2013-08-26 03:23:19 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_t *
|
|
|
|
termo_alloc (void)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_t *tk = malloc (sizeof *tk);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk)
|
|
|
|
return NULL;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// Default all the object fields but don't allocate anything
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->fd = -1;
|
|
|
|
tk->flags = 0;
|
|
|
|
tk->canonflags = 0;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->buffer = NULL;
|
|
|
|
tk->buffstart = 0;
|
|
|
|
tk->buffcount = 0;
|
2014-10-14 21:19:11 +02:00
|
|
|
tk->buffsize = 256; // bytes
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->hightide = 0;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
tk->restore_termios_valid = false;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
tk->waittime = 50; // msec
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
tk->is_closed = false;
|
|
|
|
tk->is_started = false;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->nkeynames = 64;
|
|
|
|
tk->keynames = NULL;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
for (int i = 0; i < 32; i++)
|
2014-10-14 00:08:15 +02:00
|
|
|
tk->c0[i].sym = TERMO_SYM_NONE;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->drivers = NULL;
|
2012-01-26 11:06:05 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->method.emit_codepoint = &emit_codepoint;
|
|
|
|
tk->method.peekkey_simple = &peekkey_simple;
|
|
|
|
tk->method.peekkey_mouse = &peekkey_mouse;
|
2014-10-22 22:23:01 +02:00
|
|
|
|
|
|
|
tk->mouse_proto = TERMO_MOUSE_PROTO_NONE;
|
2014-11-19 02:41:36 +01:00
|
|
|
tk->mouse_tracking = TERMO_MOUSE_TRACKING_CLICK;
|
2014-10-22 22:23:01 +02:00
|
|
|
tk->guessed_mouse_proto = TERMO_MOUSE_PROTO_NONE;
|
|
|
|
|
|
|
|
tk->ti_data = NULL;
|
|
|
|
tk->ti_method.set_mouse_proto = NULL;
|
|
|
|
tk->ti_method.set_mouse_tracking_mode = NULL;
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk;
|
2012-01-26 11:06:05 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_init (termo_t *tk, const char *term, const char *encoding)
|
2012-01-26 11:06:05 +01:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
if (!encoding)
|
|
|
|
encoding = nl_langinfo (CODESET);
|
|
|
|
|
2014-11-18 22:28:31 +01:00
|
|
|
// If we don't specify the endianity, iconv() outputs the BOM first
|
|
|
|
static const uint16_t endianity = 0x0102;
|
|
|
|
const char *utf32 = (*(uint8_t *) &endianity == 0x01)
|
|
|
|
? "UTF-32BE" : "UTF-32LE";
|
|
|
|
|
|
|
|
if ((tk->to_utf32_conv = iconv_open (utf32, encoding)) == (iconv_t) -1)
|
2014-09-28 03:51:45 +02:00
|
|
|
return 0;
|
2014-11-18 22:28:31 +01:00
|
|
|
if ((tk->from_utf32_conv = iconv_open (encoding, utf32)) == (iconv_t) -1)
|
2014-09-28 03:51:45 +02:00
|
|
|
goto abort_free_to_utf32;
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->buffer = malloc (tk->buffsize);
|
|
|
|
if (!tk->buffer)
|
2014-09-28 03:51:45 +02:00
|
|
|
goto abort_free_from_utf32;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
tk->keynames = malloc (sizeof tk->keynames[0] * tk->nkeynames);
|
|
|
|
if (!tk->keynames)
|
|
|
|
goto abort_free_buffer;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < tk->nkeynames; i++)
|
|
|
|
tk->keynames[i] = NULL;
|
|
|
|
for (i = 0; keynames[i].name; i++)
|
2014-10-14 00:08:15 +02:00
|
|
|
if (termo_register_keyname (tk,
|
2014-09-23 01:38:08 +02:00
|
|
|
keynames[i].sym, keynames[i].name) == -1)
|
|
|
|
goto abort_free_keynames;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
register_c0 (tk, TERMO_SYM_BACKSPACE, 0x08, NULL);
|
|
|
|
register_c0 (tk, TERMO_SYM_TAB, 0x09, NULL);
|
|
|
|
register_c0 (tk, TERMO_SYM_ENTER, 0x0d, NULL);
|
|
|
|
register_c0 (tk, TERMO_SYM_ESCAPE, 0x1b, NULL);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_driver_node_t **tail = &tk->drivers;
|
2014-09-23 01:38:08 +02:00
|
|
|
for (i = 0; drivers[i]; i++)
|
|
|
|
{
|
|
|
|
void *info = (*drivers[i]->new_driver) (tk, term);
|
|
|
|
if (!info)
|
|
|
|
continue;
|
2008-10-07 04:17:06 +02:00
|
|
|
|
2008-11-09 20:58:11 +01:00
|
|
|
#ifdef DEBUG
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Loading the %s driver...\n", drivers[i]->name);
|
2008-11-09 20:58:11 +01:00
|
|
|
#endif
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_driver_node_t *thisdrv = malloc (sizeof *thisdrv);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!thisdrv)
|
|
|
|
goto abort_free_drivers;
|
2008-11-09 20:58:11 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
thisdrv->driver = drivers[i];
|
|
|
|
thisdrv->info = info;
|
|
|
|
thisdrv->next = NULL;
|
2008-11-09 20:58:11 +01:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
*tail = thisdrv;
|
|
|
|
tail = &thisdrv->next;
|
2011-09-05 15:14:09 +02:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Loaded %s driver\n", drivers[i]->name);
|
2011-09-05 15:14:09 +02:00
|
|
|
#endif
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2008-02-10 18:34:05 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk->drivers)
|
|
|
|
{
|
|
|
|
errno = ENOENT;
|
|
|
|
goto abort_free_keynames;
|
|
|
|
}
|
|
|
|
return 1;
|
2008-10-10 00:19:10 +02:00
|
|
|
|
2008-11-09 20:58:11 +01:00
|
|
|
abort_free_drivers:
|
2014-10-14 00:08:15 +02:00
|
|
|
for (termo_driver_node_t *p = tk->drivers; p; )
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
(*p->driver->free_driver) (p->info);
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_driver_node_t *next = p->next;
|
2014-09-23 01:38:08 +02:00
|
|
|
free (p);
|
|
|
|
p = next;
|
|
|
|
}
|
2008-11-09 20:58:11 +01:00
|
|
|
|
2008-10-10 00:19:10 +02:00
|
|
|
abort_free_keynames:
|
2014-09-23 01:38:08 +02:00
|
|
|
free (tk->keynames);
|
2008-10-10 00:19:10 +02:00
|
|
|
abort_free_buffer:
|
2014-09-23 01:38:08 +02:00
|
|
|
free (tk->buffer);
|
2014-09-28 03:51:45 +02:00
|
|
|
abort_free_from_utf32:
|
|
|
|
iconv_close (tk->from_utf32_conv);
|
|
|
|
abort_free_to_utf32:
|
|
|
|
iconv_close (tk->to_utf32_conv);
|
2014-09-23 01:38:08 +02:00
|
|
|
return 0;
|
2012-01-26 11:13:03 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_t *
|
|
|
|
termo_new (int fd, const char *encoding, int flags)
|
2012-01-26 11:13:03 +01:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_t *tk = termo_alloc ();
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk)
|
|
|
|
return NULL;
|
2012-01-26 11:13:03 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->fd = fd;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_set_flags (tk, flags);
|
2012-01-26 11:13:03 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
const char *term = getenv ("TERM");
|
2014-10-14 00:08:15 +02:00
|
|
|
if (termo_init (tk, term, encoding)
|
|
|
|
&& termo_start (tk))
|
2014-09-28 03:51:45 +02:00
|
|
|
return tk;
|
2012-01-26 11:13:03 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
free (tk);
|
|
|
|
return NULL;
|
2012-01-26 13:55:16 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_t *
|
|
|
|
termo_new_abstract (const char *term, const char *encoding, int flags)
|
2012-01-26 13:55:16 +01:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_t *tk = termo_alloc ();
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tk->fd = -1;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_set_flags (tk, flags);
|
2014-09-28 03:51:45 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (!termo_init (tk, term, encoding))
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
free (tk);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_start (tk);
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
void
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_free (termo_t *tk)
|
2008-02-23 21:07:48 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
free (tk->buffer); tk->buffer = NULL;
|
|
|
|
free (tk->keynames); tk->keynames = NULL;
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
iconv_close (tk->to_utf32_conv);
|
|
|
|
tk->to_utf32_conv = (iconv_t) -1;
|
|
|
|
iconv_close (tk->from_utf32_conv);
|
|
|
|
tk->from_utf32_conv = (iconv_t) -1;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_driver_node_t *p, *next;
|
2014-09-28 03:51:45 +02:00
|
|
|
for (p = tk->drivers; p; p = next)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
(*p->driver->free_driver) (p->info);
|
2014-09-28 03:51:45 +02:00
|
|
|
next = p->next;
|
2014-09-23 01:38:08 +02:00
|
|
|
free (p);
|
|
|
|
}
|
|
|
|
free (tk);
|
2008-02-23 21:07:48 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
void
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_destroy (termo_t *tk)
|
2008-02-23 21:26:04 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->is_started)
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_stop (tk);
|
2012-03-08 22:15:25 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_free (tk);
|
2012-03-08 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_start (termo_t *tk)
|
2012-03-08 22:15:25 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->is_started)
|
|
|
|
return 1;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (tk->fd != -1 && !(tk->flags & TERMO_FLAG_NOTERMIOS))
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
struct termios termios;
|
|
|
|
if (tcgetattr (tk->fd, &termios) == 0)
|
|
|
|
{
|
|
|
|
tk->restore_termios = termios;
|
2014-09-28 03:51:45 +02:00
|
|
|
tk->restore_termios_valid = true;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
termios.c_iflag &= ~(IXON|INLCR|ICRNL);
|
|
|
|
termios.c_lflag &= ~(ICANON|ECHO);
|
|
|
|
termios.c_cc[VMIN] = 1;
|
|
|
|
termios.c_cc[VTIME] = 0;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (tk->flags & TERMO_FLAG_CTRLC)
|
2014-10-14 21:19:11 +02:00
|
|
|
// want no signal keys at all, so just disable ISIG
|
2014-09-23 01:38:08 +02:00
|
|
|
termios.c_lflag &= ~ISIG;
|
|
|
|
else
|
|
|
|
{
|
2014-10-14 21:19:11 +02:00
|
|
|
// Disable ^\ == VQUIT and ^D == VSUSP but leave ^C as SIGINT
|
2014-09-23 01:38:08 +02:00
|
|
|
termios.c_cc[VQUIT] = _POSIX_VDISABLE;
|
|
|
|
termios.c_cc[VSUSP] = _POSIX_VDISABLE;
|
2014-10-14 21:19:11 +02:00
|
|
|
// Some OSes have ^Y == VDSUSP
|
2012-03-08 22:15:25 +01:00
|
|
|
#ifdef VDSUSP
|
2014-09-23 01:38:08 +02:00
|
|
|
termios.c_cc[VDSUSP] = _POSIX_VDISABLE;
|
2012-03-08 22:15:25 +01:00
|
|
|
#endif
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2012-03-08 22:15:25 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Setting termios(3) flags\n");
|
2012-03-08 22:15:25 +01:00
|
|
|
#endif
|
2014-09-23 01:38:08 +02:00
|
|
|
tcsetattr (tk->fd, TCSANOW, &termios);
|
|
|
|
}
|
|
|
|
}
|
2012-03-08 22:15:25 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_driver_node_t *p;
|
2014-09-23 01:38:08 +02:00
|
|
|
for (p = tk->drivers; p; p = p->next)
|
|
|
|
if (p->driver->start_driver)
|
|
|
|
if (!(*p->driver->start_driver) (tk, p->info))
|
|
|
|
return 0;
|
2012-03-08 22:15:25 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-10-14 00:08:15 +02:00
|
|
|
fprintf (stderr, "Drivers started; termo instance %p is ready\n", tk);
|
2012-03-08 22:15:25 +01:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->is_started = 1;
|
|
|
|
return 1;
|
2012-03-08 22:15:25 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_stop (termo_t *tk)
|
2012-03-08 22:15:25 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk->is_started)
|
|
|
|
return 1;
|
2012-03-08 22:15:25 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
struct termo_driver_node *p;
|
2014-09-23 01:38:08 +02: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
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->restore_termios_valid)
|
|
|
|
tcsetattr (tk->fd, TCSANOW, &tk->restore_termios);
|
2008-02-23 21:26:04 +01:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
tk->is_started = false;
|
2014-09-23 01:38:08 +02:00
|
|
|
return 1;
|
2008-02-23 21:26:04 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_is_started (termo_t *tk)
|
2012-03-08 22:24:39 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk->is_started;
|
2012-03-08 22:24:39 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_get_fd (termo_t *tk)
|
2008-12-01 23:36:14 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk->fd;
|
2008-12-01 23:36:14 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_get_flags (termo_t *tk)
|
2008-11-10 22:21:19 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk->flags;
|
2008-11-10 22:21:19 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
void
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_set_flags (termo_t *tk, int newflags)
|
2008-11-10 22:21:19 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->flags = newflags;
|
2014-10-14 00:08:15 +02:00
|
|
|
if (tk->flags & TERMO_FLAG_SPACESYMBOL)
|
|
|
|
tk->canonflags |= TERMO_CANON_SPACESYMBOL;
|
2014-09-23 01:38:08 +02:00
|
|
|
else
|
2014-10-14 00:08:15 +02:00
|
|
|
tk->canonflags &= ~TERMO_CANON_SPACESYMBOL;
|
2008-11-10 22:21:19 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
void
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_set_waittime (termo_t *tk, int msec)
|
2008-02-10 21:22:33 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->waittime = msec;
|
2008-02-10 21:22:33 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_get_waittime (termo_t *tk)
|
2008-02-10 21:22:33 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk->waittime;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_get_canonflags (termo_t *tk)
|
2011-08-28 18:50:18 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk->canonflags;
|
2011-08-28 18:50:18 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
void
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_set_canonflags (termo_t *tk, int flags)
|
2011-08-28 18:50:18 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->canonflags = flags;
|
2014-10-14 00:08:15 +02:00
|
|
|
if (tk->canonflags & TERMO_CANON_SPACESYMBOL)
|
|
|
|
tk->flags |= TERMO_FLAG_SPACESYMBOL;
|
2014-09-23 01:38:08 +02:00
|
|
|
else
|
2014-10-14 00:08:15 +02:00
|
|
|
tk->flags &= ~TERMO_FLAG_SPACESYMBOL;
|
2011-08-28 18:50:18 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
size_t
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_get_buffer_size (termo_t *tk)
|
2012-02-13 22:57:33 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk->buffsize;
|
2012-02-13 22:57:33 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_set_buffer_size (termo_t *tk, size_t size)
|
2012-02-14 00:40:42 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
unsigned char *buffer = realloc (tk->buffer, size);
|
|
|
|
if (!buffer)
|
|
|
|
return 0;
|
2012-02-14 00:40:42 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->buffer = buffer;
|
|
|
|
tk->buffsize = size;
|
|
|
|
return 1;
|
2012-02-14 00:40:42 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
size_t
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_get_buffer_remaining (termo_t *tk)
|
2012-01-18 11:04:15 +01:00
|
|
|
{
|
2014-10-14 21:19:11 +02:00
|
|
|
// Return the total number of free bytes in the buffer,
|
|
|
|
// because that's what is available to the user.
|
2014-09-23 01:38:08 +02:00
|
|
|
return tk->buffsize - tk->buffcount;
|
2012-01-18 11:04:15 +01:00
|
|
|
}
|
|
|
|
|
2014-11-19 02:41:36 +01:00
|
|
|
termo_mouse_proto_t
|
|
|
|
termo_get_mouse_proto(termo_t *tk)
|
2014-10-22 22:23:01 +02:00
|
|
|
{
|
|
|
|
return tk->mouse_proto;
|
|
|
|
}
|
|
|
|
|
2014-11-19 02:41:36 +01:00
|
|
|
termo_mouse_proto_t
|
|
|
|
termo_guess_mouse_proto(termo_t *tk)
|
2014-10-22 22:23:01 +02:00
|
|
|
{
|
|
|
|
return tk->guessed_mouse_proto;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-11-19 02:41:36 +01:00
|
|
|
termo_set_mouse_proto (termo_t *tk, termo_mouse_proto_t proto)
|
2014-10-22 22:23:01 +02:00
|
|
|
{
|
2014-11-19 02:41:36 +01:00
|
|
|
termo_mouse_proto_t old_proto = tk->mouse_proto;
|
2014-10-22 22:23:01 +02:00
|
|
|
tk->mouse_proto = proto;
|
|
|
|
|
|
|
|
// Call the TI driver to apply the change if needed
|
2014-11-19 02:41:36 +01:00
|
|
|
if (proto == old_proto
|
|
|
|
|| !tk->is_started
|
2014-10-22 22:23:01 +02:00
|
|
|
|| !tk->ti_method.set_mouse_proto)
|
|
|
|
return true;
|
2014-11-19 02:41:36 +01:00
|
|
|
|
|
|
|
// Unsetting the protocol disables tracking; this is a bit hackish
|
|
|
|
if (!tk->ti_method.set_mouse_tracking_mode (tk->ti_data,
|
|
|
|
tk->mouse_tracking, proto != TERMO_MOUSE_PROTO_NONE))
|
|
|
|
return false;
|
|
|
|
|
2014-10-22 22:23:01 +02:00
|
|
|
return tk->ti_method.set_mouse_proto (tk->ti_data, old_proto, false)
|
|
|
|
&& tk->ti_method.set_mouse_proto (tk->ti_data, proto, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
termo_mouse_tracking_t
|
|
|
|
termo_get_mouse_tracking_mode (termo_t *tk)
|
|
|
|
{
|
|
|
|
return tk->mouse_tracking;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
termo_set_mouse_tracking_mode (termo_t *tk, termo_mouse_tracking_t mode)
|
|
|
|
{
|
|
|
|
termo_mouse_tracking_t old_mode = tk->mouse_tracking;
|
|
|
|
tk->mouse_tracking = mode;
|
|
|
|
|
|
|
|
// Call the TI driver to apply the change if needed
|
2014-11-19 02:41:36 +01:00
|
|
|
if (mode == old_mode
|
|
|
|
|| !tk->is_started
|
2014-10-22 22:23:01 +02:00
|
|
|
|| !tk->ti_method.set_mouse_tracking_mode)
|
|
|
|
return true;
|
2014-11-19 02:41:36 +01:00
|
|
|
|
2014-10-22 22:23:01 +02:00
|
|
|
return tk->ti_method.set_mouse_tracking_mode (tk->ti_data, old_mode, false)
|
|
|
|
&& tk->ti_method.set_mouse_tracking_mode (tk->ti_data, mode, true);
|
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
2014-10-14 00:08:15 +02:00
|
|
|
eat_bytes (termo_t *tk, size_t count)
|
2008-10-07 23:22:59 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (count >= tk->buffcount)
|
|
|
|
{
|
|
|
|
tk->buffstart = 0;
|
|
|
|
tk->buffcount = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tk->buffstart += count;
|
|
|
|
tk->buffcount -= count;
|
2008-10-07 23:22:59 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
#define MULTIBYTE_INVALID '?'
|
2008-10-08 01:36:27 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
2014-10-14 00:08:15 +02:00
|
|
|
fill_multibyte (termo_t *tk, termo_key_t *key)
|
2008-10-08 01:12:41 +02:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
size_t codepoint_len = sizeof key->code.codepoint;
|
|
|
|
char *codepoint_ptr = (char *) &key->code.codepoint;
|
|
|
|
size_t multibyte_len = sizeof key->multibyte;
|
|
|
|
char *multibyte_ptr = (char *) key->multibyte;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
size_t result = iconv (tk->from_utf32_conv,
|
|
|
|
&codepoint_ptr, &codepoint_len, &multibyte_ptr, &multibyte_len);
|
|
|
|
size_t output = sizeof key->multibyte - multibyte_len;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
// Something broke
|
|
|
|
if (result == (size_t) -1 || output == 0)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
key->multibyte[0] = MULTIBYTE_INVALID;
|
|
|
|
key->multibyte[1] = 0;
|
|
|
|
return;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// Append a null character, as it wasn't part of the input
|
2014-09-28 03:51:45 +02:00
|
|
|
key->multibyte[output] = 0;
|
2008-10-08 01:12:41 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_result_t
|
|
|
|
parse_multibyte (termo_t *tk, const unsigned char *bytes, size_t len,
|
2014-09-28 03:51:45 +02:00
|
|
|
uint32_t *cp, size_t *nbytep)
|
2011-04-01 00:42:52 +02:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
size_t multibyte_len = len;
|
|
|
|
char *multibyte_ptr = (char *) bytes;
|
|
|
|
size_t codepoint_len = sizeof *cp;
|
|
|
|
char *codepoint_ptr = (char *) cp;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
// Fingers crossed...
|
|
|
|
errno = 0;
|
|
|
|
iconv (tk->to_utf32_conv,
|
|
|
|
&multibyte_ptr, &multibyte_len, &codepoint_ptr, &codepoint_len);
|
|
|
|
|
|
|
|
// Only one Unicode character could have been processed at maximum,
|
|
|
|
// so let's just set the number of processed bytes to the difference
|
|
|
|
*nbytep = len - multibyte_len;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
// Nothing has been converted, let's examine what happened
|
|
|
|
if (codepoint_ptr == (char *) cp)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
if (errno == 0)
|
|
|
|
// The input was probably a shift sequence
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_AGAIN;
|
2014-09-28 03:51:45 +02:00
|
|
|
if (errno == EINVAL)
|
|
|
|
// Incomplete character or shift sequence
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_AGAIN;
|
2014-09-28 03:51:45 +02:00
|
|
|
if (errno == EILSEQ)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
// Invalid multibyte sequence in the input, let's try going
|
|
|
|
// byte after byte in hope we skip it completely
|
|
|
|
*cp = MULTIBYTE_INVALID;
|
|
|
|
*nbytep = 1;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_KEY;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
// We can't really get E2BIG so what the fuck is going on here
|
|
|
|
abort ();
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_KEY;
|
2011-04-01 00:42:52 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
2014-10-14 00:08:15 +02:00
|
|
|
emit_codepoint (termo_t *tk, uint32_t codepoint, termo_key_t *key)
|
2008-10-08 01:12:41 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (codepoint < 0x20)
|
|
|
|
{
|
|
|
|
// C0 range
|
|
|
|
key->code.codepoint = 0;
|
|
|
|
key->modifiers = 0;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (!(tk->flags & TERMO_FLAG_NOINTERPRET)
|
|
|
|
&& tk->c0[codepoint].sym != TERMO_SYM_UNKNOWN)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
key->code.sym = tk->c0[codepoint].sym;
|
|
|
|
key->modifiers |= tk->c0[codepoint].modifier_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key->code.sym)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEY;
|
2014-10-14 21:19:11 +02:00
|
|
|
// Generically modified Unicode ought not report the SHIFT state,
|
|
|
|
// or else we get into complications trying to report Shift-; vs :
|
|
|
|
// and so on... In order to be able to represent Ctrl-Shift-A as
|
|
|
|
// CTRL modified unicode A, we need to call Ctrl-A simply 'a',
|
|
|
|
// lowercase
|
2014-09-23 01:38:08 +02:00
|
|
|
if (codepoint + 0x40 >= 'A' && codepoint + 0x40 <= 'Z')
|
2014-10-09 23:47:24 +02:00
|
|
|
// It's a letter - use lowercase instead
|
2014-09-23 01:38:08 +02:00
|
|
|
key->code.codepoint = codepoint + 0x60;
|
|
|
|
else
|
|
|
|
key->code.codepoint = codepoint + 0x40;
|
2014-10-14 00:08:15 +02:00
|
|
|
key->modifiers = TERMO_KEYMOD_CTRL;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
else
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEYSYM;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
else if (codepoint == 0x7f && !(tk->flags & TERMO_FLAG_NOINTERPRET))
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
// ASCII DEL
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEYSYM;
|
|
|
|
key->code.sym = TERMO_SYM_DEL;
|
2014-09-23 01:38:08 +02:00
|
|
|
key->modifiers = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEY;
|
2014-09-23 01:38:08 +02:00
|
|
|
key->code.codepoint = codepoint;
|
|
|
|
key->modifiers = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_canonicalise (tk, key);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (key->type == TERMO_TYPE_KEY)
|
2014-09-28 03:51:45 +02:00
|
|
|
fill_multibyte (tk, key);
|
2008-10-08 01:12:41 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
void
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_canonicalise (termo_t *tk, termo_key_t *key)
|
2011-08-27 20:59:02 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
int flags = tk->canonflags;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (flags & TERMO_CANON_SPACESYMBOL)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
if (key->type == TERMO_TYPE_KEY && key->code.codepoint == 0x20)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEYSYM;
|
|
|
|
key->code.sym = TERMO_SYM_SPACE;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
if (key->type == TERMO_TYPE_KEYSYM
|
|
|
|
&& key->code.sym == TERMO_SYM_SPACE)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEY;
|
2014-09-28 03:51:45 +02:00
|
|
|
key->code.codepoint = 0x20;
|
|
|
|
fill_multibyte (tk, key);
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (flags & TERMO_CANON_DELBS)
|
|
|
|
if (key->type == TERMO_TYPE_KEYSYM
|
|
|
|
&& key->code.sym == TERMO_SYM_DEL)
|
|
|
|
key->code.sym = TERMO_SYM_BACKSPACE;
|
2011-08-27 20:59:02 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_result_t
|
|
|
|
peekkey (termo_t *tk, termo_key_t *key, int force, size_t *nbytep)
|
2008-12-06 00:55:44 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
int again = 0;
|
2008-12-06 00:55:44 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!tk->is_started)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_ERROR;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2012-03-08 22:31:16 +01:00
|
|
|
|
2008-12-06 00:55:44 +01:00
|
|
|
#ifdef DEBUG
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "getkey(force=%d): buffer ", force);
|
|
|
|
print_buffer (tk);
|
|
|
|
fprintf (stderr, "\n");
|
2008-12-06 00:55:44 +01:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->hightide)
|
|
|
|
{
|
|
|
|
tk->buffstart += tk->hightide;
|
|
|
|
tk->buffcount -= tk->hightide;
|
|
|
|
tk->hightide = 0;
|
|
|
|
}
|
2012-11-30 15:35:23 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t ret;
|
|
|
|
termo_driver_node_t *p;
|
2014-09-23 01:38:08 +02:00
|
|
|
for (p = tk->drivers; p; p = p->next)
|
|
|
|
{
|
|
|
|
ret = (p->driver->peekkey) (tk, p->info, key, force, nbytep);
|
2008-12-06 00:55:44 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "Driver %s yields %s\n",
|
|
|
|
p->driver->name, res2str (ret));
|
2008-12-06 00:55:44 +01:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
switch (ret)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_KEY:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2008-12-06 00:55:44 +01:00
|
|
|
#ifdef DEBUG
|
2014-09-23 01:38:08 +02:00
|
|
|
print_key (tk, key); fprintf (stderr, "\n");
|
2008-12-06 00:55:44 +01:00
|
|
|
#endif
|
2014-09-23 01:38:08 +02: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;
|
|
|
|
}
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// Fallthrough
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_EOF:
|
|
|
|
case TERMO_RES_ERROR:
|
2014-09-23 01:38:08 +02:00
|
|
|
return ret;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_AGAIN:
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!force)
|
|
|
|
again = 1;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_NONE:
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (again)
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_AGAIN;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
ret = peekkey_simple (tk, key, force, nbytep);
|
2008-12-06 00:55:44 +01:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-09-23 01:38:08 +02:00
|
|
|
fprintf (stderr, "getkey_simple(force=%d) yields %s\n",
|
|
|
|
force, res2str (ret));
|
2014-10-14 00:08:15 +02:00
|
|
|
if (ret == TERMO_RES_KEY)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
print_key (tk, key);
|
|
|
|
fprintf (stderr, "\n");
|
|
|
|
}
|
2008-12-06 00:55:44 +01:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
return ret;
|
2008-12-06 00:55:44 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_result_t
|
|
|
|
peekkey_simple (termo_t *tk, termo_key_t *key, int force, size_t *nbytep)
|
2008-10-08 01:36:27 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->buffcount == 0)
|
2014-10-14 00:08:15 +02:00
|
|
|
return tk->is_closed ? TERMO_RES_EOF : TERMO_RES_NONE;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
unsigned char b0 = CHARAT (0);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (b0 == 0x1b)
|
|
|
|
{
|
|
|
|
// Escape-prefixed value? Might therefore be Alt+key
|
|
|
|
if (tk->buffcount == 1)
|
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
// This might be an <Esc> press, or it may want to be part
|
|
|
|
// of a longer sequence
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!force)
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_AGAIN;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
(*tk->method.emit_codepoint) (tk, b0, key);
|
|
|
|
*nbytep = 1;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_KEY;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try another key there
|
|
|
|
tk->buffstart++;
|
|
|
|
tk->buffcount--;
|
|
|
|
|
|
|
|
// Run the full driver
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t metakey_result = peekkey (tk, key, force, nbytep);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
tk->buffstart--;
|
|
|
|
tk->buffcount++;
|
|
|
|
|
|
|
|
switch (metakey_result)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_KEY:
|
|
|
|
key->modifiers |= TERMO_KEYMOD_ALT;
|
2014-09-23 01:38:08 +02:00
|
|
|
(*nbytep)++;
|
|
|
|
break;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_NONE:
|
|
|
|
case TERMO_RES_EOF:
|
|
|
|
case TERMO_RES_AGAIN:
|
|
|
|
case TERMO_RES_ERROR:
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return metakey_result;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
else if (!(tk->flags & TERMO_FLAG_RAW))
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
uint32_t codepoint;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t res = parse_multibyte
|
2014-09-28 03:51:45 +02:00
|
|
|
(tk, tk->buffer + tk->buffstart, tk->buffcount, &codepoint, nbytep);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (res == TERMO_RES_AGAIN && force)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 21:19:11 +02:00
|
|
|
// There weren't enough bytes for a complete character but
|
|
|
|
// caller demands an answer. About the best thing we can do here
|
|
|
|
// is eat as many bytes as we have, and emit a MULTIBYTE_INVALID.
|
|
|
|
// If the remaining bytes arrive later, they'll be invalid too.
|
2014-09-28 03:51:45 +02:00
|
|
|
codepoint = MULTIBYTE_INVALID;
|
2014-09-23 01:38:08 +02:00
|
|
|
*nbytep = tk->buffcount;
|
2014-10-14 00:08:15 +02:00
|
|
|
res = TERMO_RES_KEY;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEY;
|
2014-09-23 01:38:08 +02:00
|
|
|
key->modifiers = 0;
|
|
|
|
(*tk->method.emit_codepoint) (tk, codepoint, key);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
// Non multibyte case - just report the raw byte
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEY;
|
2014-09-23 01:38:08 +02:00
|
|
|
key->code.codepoint = b0;
|
|
|
|
key->modifiers = 0;
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
key->multibyte[0] = b0;
|
|
|
|
key->multibyte[1] = 0;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
*nbytep = 1;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_KEY;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2008-10-08 01:36:27 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 23:36:14 +02:00
|
|
|
// XXX: With the current infrastructure I'm not sure how to properly handle
|
|
|
|
// this. peekkey() isn't made for skipping invalid inputs.
|
|
|
|
#define INVALID_1005 0x20
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_result_t
|
2014-10-13 23:36:14 +02:00
|
|
|
parse_1005_value (const unsigned char **bytes, size_t *len, uint32_t *cp)
|
|
|
|
{
|
|
|
|
unsigned int nbytes;
|
|
|
|
unsigned char b0 = (*bytes)[0];
|
|
|
|
if (b0 < 0x80)
|
|
|
|
{
|
|
|
|
// Single byte ASCII
|
|
|
|
*cp = b0;
|
|
|
|
nbytes = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
else if (b0 < 0xc0)
|
|
|
|
{
|
|
|
|
// Starts with a continuation byte - that's not right
|
|
|
|
*cp = INVALID_1005;
|
|
|
|
nbytes = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
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 = INVALID_1005;
|
|
|
|
nbytes = 1;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int b = 1; b < nbytes; b++)
|
|
|
|
{
|
|
|
|
if (b >= *len)
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_AGAIN;
|
2014-10-13 23:36:14 +02:00
|
|
|
|
|
|
|
unsigned char cb = (*bytes)[b];
|
|
|
|
if (cb < 0x80 || cb >= 0xc0)
|
|
|
|
{
|
|
|
|
*cp = INVALID_1005;
|
|
|
|
nbytes = b;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
*cp <<= 6;
|
|
|
|
*cp |= cb & 0x3f;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
(*bytes) += nbytes;
|
|
|
|
(*len) -= nbytes;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_KEY;
|
2014-10-13 23:36:14 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_result_t
|
|
|
|
peekkey_mouse (termo_t *tk, termo_key_t *key, size_t *nbytep)
|
2009-11-26 01:27:58 +01:00
|
|
|
{
|
2014-10-13 23:36:14 +02:00
|
|
|
uint32_t b, x, y;
|
|
|
|
|
|
|
|
// TODO: Add some API to switch on 1005 mode support
|
|
|
|
if (false)
|
|
|
|
{
|
|
|
|
const unsigned char *buff = tk->buffer + tk->buffstart;
|
|
|
|
size_t len = tk->buffcount;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (parse_1005_value (&buff, &len, &b) == TERMO_RES_AGAIN
|
|
|
|
|| parse_1005_value (&buff, &len, &x) == TERMO_RES_AGAIN
|
|
|
|
|| parse_1005_value (&buff, &len, &y) == TERMO_RES_AGAIN)
|
|
|
|
return TERMO_RES_AGAIN;
|
2014-10-13 23:36:14 +02:00
|
|
|
|
|
|
|
*nbytep = tk->buffcount - len;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (tk->buffcount < 3)
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_AGAIN;
|
2014-10-13 23:36:14 +02:00
|
|
|
|
|
|
|
b = CHARAT (0);
|
|
|
|
x = CHARAT (1);
|
|
|
|
y = CHARAT (2);
|
|
|
|
|
|
|
|
*nbytep = 3;
|
|
|
|
}
|
2009-11-26 01:27:58 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_MOUSE;
|
2014-10-13 23:36:14 +02:00
|
|
|
key->code.mouse.info = b - 0x20;
|
|
|
|
key->code.mouse.x = x - 0x20 - 1;
|
|
|
|
key->code.mouse.y = y - 0x20 - 1;
|
2012-04-05 19:54:30 +02:00
|
|
|
|
2014-10-13 23:36:14 +02:00
|
|
|
key->modifiers = (key->code.mouse.info & 0x1c) >> 2;
|
|
|
|
key->code.mouse.info &= ~0x1c;
|
2012-04-05 19:54:30 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_KEY;
|
2012-04-24 18:27:48 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t
|
|
|
|
termo_getkey (termo_t *tk, termo_key_t *key)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
size_t nbytes = 0;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t ret = peekkey (tk, key, 0, &nbytes);
|
2008-12-10 01:23:47 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (ret == TERMO_RES_KEY)
|
2014-09-23 01:38:08 +02:00
|
|
|
eat_bytes (tk, nbytes);
|
2008-12-10 01:23:47 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (ret == TERMO_RES_AGAIN)
|
2014-10-14 21:19:11 +02:00
|
|
|
// Call peekkey() again in force mode to obtain whatever it can
|
2014-09-23 01:38:08 +02:00
|
|
|
(void) peekkey (tk, key, 1, &nbytes);
|
2014-10-14 21:19:11 +02:00
|
|
|
// Don't eat it yet though
|
2008-12-10 01:38:21 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
return ret;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t
|
|
|
|
termo_getkey_force (termo_t *tk, termo_key_t *key)
|
2008-02-10 21:22:33 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
size_t nbytes = 0;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t ret = peekkey (tk, key, 1, &nbytes);
|
2008-12-10 01:23:47 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (ret == TERMO_RES_KEY)
|
2014-09-23 01:38:08 +02:00
|
|
|
eat_bytes (tk, nbytes);
|
2008-12-10 01:23:47 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
return ret;
|
2008-02-10 21:22:33 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t
|
|
|
|
termo_waitkey (termo_t *tk, termo_key_t *key)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->fd == -1)
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_ERROR;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t ret = termo_getkey (tk, key);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
switch (ret)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_KEY:
|
|
|
|
case TERMO_RES_EOF:
|
|
|
|
case TERMO_RES_ERROR:
|
2014-09-23 01:38:08 +02:00
|
|
|
return ret;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_NONE:
|
|
|
|
ret = termo_advisereadable (tk);
|
|
|
|
if (ret == TERMO_RES_ERROR)
|
2014-09-23 01:38:08 +02:00
|
|
|
return ret;
|
|
|
|
break;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_RES_AGAIN:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
if (tk->is_closed)
|
|
|
|
// We're closed now. Never going to get more bytes
|
|
|
|
// so just go with what we have
|
2014-10-14 00:08:15 +02:00
|
|
|
return termo_getkey_force (tk, key);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
struct pollfd fd;
|
2011-08-25 11:48:41 +02:00
|
|
|
retry:
|
2014-09-23 01:38:08 +02:00
|
|
|
fd.fd = tk->fd;
|
|
|
|
fd.events = POLLIN;
|
|
|
|
|
|
|
|
int pollret = poll (&fd, 1, tk->waittime);
|
|
|
|
if (pollret == -1)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
if (errno == EINTR && !(tk->flags & TERMO_FLAG_EINTR))
|
2014-09-23 01:38:08 +02:00
|
|
|
goto retry;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_ERROR;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
if (fd.revents & (POLLIN | POLLHUP | POLLERR))
|
2014-10-14 00:08:15 +02:00
|
|
|
ret = termo_advisereadable (tk);
|
2014-09-23 01:38:08 +02:00
|
|
|
else
|
2014-10-14 00:08:15 +02:00
|
|
|
ret = TERMO_RES_NONE;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (ret == TERMO_RES_ERROR)
|
2014-09-23 01:38:08 +02:00
|
|
|
return ret;
|
2014-10-14 00:08:15 +02:00
|
|
|
if (ret == TERMO_RES_NONE)
|
|
|
|
return termo_getkey_force (tk, key);
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// UNREACHABLE
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t
|
|
|
|
termo_advisereadable (termo_t *tk)
|
2008-02-07 01:58:41 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->fd == -1)
|
|
|
|
{
|
|
|
|
errno = EBADF;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_ERROR;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tk->buffstart)
|
|
|
|
{
|
|
|
|
memmove (tk->buffer, tk->buffer + tk->buffstart, tk->buffcount);
|
|
|
|
tk->buffstart = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// Not expecting it ever to be greater but doesn't hurt to handle that
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->buffcount >= tk->buffsize)
|
|
|
|
{
|
|
|
|
errno = ENOMEM;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_ERROR;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t len;
|
2011-08-25 11:48:41 +02:00
|
|
|
retry:
|
2014-09-23 01:38:08 +02:00
|
|
|
len = read (tk->fd, tk->buffer + tk->buffcount,
|
|
|
|
tk->buffsize - tk->buffcount);
|
|
|
|
|
|
|
|
if (len == -1)
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_NONE;
|
|
|
|
if (errno == EINTR && !(tk->flags & TERMO_FLAG_EINTR))
|
2014-09-23 01:38:08 +02:00
|
|
|
goto retry;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_ERROR;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
if (len < 1)
|
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
tk->is_closed = true;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_NONE;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
tk->buffcount += len;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_AGAIN;
|
2008-02-07 01:58:41 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
size_t
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_push_bytes (termo_t *tk, const char *bytes, size_t len)
|
2012-01-18 14:39:50 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (tk->buffstart)
|
|
|
|
{
|
|
|
|
memmove (tk->buffer, tk->buffer + tk->buffstart, tk->buffcount);
|
|
|
|
tk->buffstart = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// Not expecting it ever to be greater but doesn't hurt to handle that
|
2014-09-23 01:38:08 +02:00
|
|
|
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;
|
2012-01-18 14:39:50 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_sym_t
|
|
|
|
termo_register_keyname (termo_t *tk, termo_sym_t sym, const char *name)
|
2008-02-07 03:21:14 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!sym)
|
|
|
|
sym = tk->nkeynames;
|
2012-01-20 19:03:08 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (sym >= tk->nkeynames)
|
|
|
|
{
|
|
|
|
const char **new_keynames =
|
|
|
|
realloc (tk->keynames, sizeof new_keynames[0] * (sym + 1));
|
|
|
|
if (!new_keynames)
|
|
|
|
return -1;
|
2008-02-07 03:21:14 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->keynames = new_keynames;
|
2008-02-07 03:21:14 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
// Fill in the hole
|
|
|
|
for (int i = tk->nkeynames; i < sym; i++)
|
|
|
|
tk->keynames[i] = NULL;
|
2008-02-07 03:21:14 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->nkeynames = sym + 1;
|
|
|
|
}
|
2008-02-07 03:21:14 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
tk->keynames[sym] = name;
|
|
|
|
return sym;
|
2008-02-07 03:21:14 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
const char *
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_get_keyname (termo_t *tk, termo_sym_t sym)
|
2008-02-23 21:28:18 +01:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
if (sym == TERMO_SYM_UNKNOWN)
|
2014-09-23 01:38:08 +02:00
|
|
|
return "UNKNOWN";
|
|
|
|
if (sym < tk->nkeynames)
|
|
|
|
return tk->keynames[sym];
|
|
|
|
return "UNKNOWN";
|
2008-02-23 21:28:18 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static const char *
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_lookup_keyname_format (termo_t *tk,
|
|
|
|
const char *str, termo_sym_t *sym, termo_format_t format)
|
2008-12-02 01:56:43 +01:00
|
|
|
{
|
2014-10-14 21:19:11 +02: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
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
for (*sym = 0; *sym < tk->nkeynames; (*sym)++)
|
|
|
|
{
|
|
|
|
const char *thiskey = tk->keynames[*sym];
|
|
|
|
if (!thiskey)
|
|
|
|
continue;
|
|
|
|
size_t len = strlen (thiskey);
|
2014-10-14 00:08:15 +02:00
|
|
|
if (format & TERMO_FORMAT_LOWERSPACE)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
const char *thisstr = str;
|
|
|
|
if (strpncmp_camel (&thisstr, &thiskey, len) == 0)
|
|
|
|
return thisstr;
|
|
|
|
}
|
|
|
|
else if (!strncmp (str, thiskey, len))
|
|
|
|
return (char *) str + len;
|
|
|
|
}
|
|
|
|
return NULL;
|
2011-04-01 14:26:54 +02:00
|
|
|
}
|
2008-12-02 01:56:43 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
const char *
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_lookup_keyname (termo_t *tk, const char *str, termo_sym_t *sym)
|
2013-08-26 03:23:19 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
return termo_lookup_keyname_format (tk, str, sym, 0);
|
2013-08-26 03:23:19 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_sym_t
|
|
|
|
termo_keyname2sym (termo_t *tk, const char *keyname)
|
2011-04-01 14:26:54 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_sym_t sym;
|
|
|
|
const char *endp = termo_lookup_keyname (tk, keyname, &sym);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!endp || endp[0])
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_SYM_UNKNOWN;
|
2014-09-23 01:38:08 +02:00
|
|
|
return sym;
|
2008-12-02 01:56:43 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_sym_t
|
|
|
|
register_c0 (termo_t *tk,
|
|
|
|
termo_sym_t sym, unsigned char ctrl, const char *name)
|
2008-10-08 01:12:41 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
return register_c0_full (tk, sym, 0, 0, ctrl, name);
|
2008-10-08 01:12:41 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_sym_t
|
|
|
|
register_c0_full (termo_t *tk, termo_sym_t sym,
|
2014-09-23 01:38:08 +02:00
|
|
|
int modifier_set, int modifier_mask, unsigned char ctrl, const char *name)
|
2008-10-08 01:12:41 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (ctrl >= 0x20)
|
|
|
|
{
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name)
|
2014-10-14 00:08:15 +02:00
|
|
|
sym = termo_register_keyname (tk, sym, name);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
tk->c0[ctrl].sym = sym;
|
|
|
|
tk->c0[ctrl].modifier_set = modifier_set;
|
|
|
|
tk->c0[ctrl].modifier_mask = modifier_mask;
|
|
|
|
return sym;
|
2008-10-08 01:12:41 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static struct modnames
|
2011-03-31 12:08:43 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
const char *shift, *alt, *ctrl;
|
2011-04-01 01:26:02 +02:00
|
|
|
}
|
2014-09-23 01:38:08 +02:00
|
|
|
modnames[] =
|
|
|
|
{
|
|
|
|
{ "S", "A", "C" }, // 0
|
|
|
|
{ "Shift", "Alt", "Ctrl" }, // LONGMOD
|
|
|
|
{ "S", "M", "C" }, // ALTISMETA
|
|
|
|
{ "Shift", "Meta", "Ctrl" }, // ALTISMETA+LONGMOD
|
|
|
|
{ "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
|
|
|
};
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
size_t
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_strfkey (termo_t *tk, char *buffer, size_t len,
|
|
|
|
termo_key_t *key, termo_format_t format)
|
2008-08-21 21:20:07 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
size_t pos = 0;
|
|
|
|
size_t l = 0;
|
|
|
|
|
|
|
|
struct modnames *mods = &modnames[
|
2014-10-14 00:08:15 +02:00
|
|
|
!!(format & TERMO_FORMAT_LONGMOD) +
|
|
|
|
!!(format & TERMO_FORMAT_ALTISMETA) * 2 +
|
|
|
|
!!(format & TERMO_FORMAT_LOWERMOD) * 4];
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
int wrapbracket = (format & TERMO_FORMAT_WRAPBRACKET) &&
|
|
|
|
(key->type != TERMO_TYPE_KEY || key->modifiers != 0);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
char sep = (format & TERMO_FORMAT_SPACEMOD) ? ' ' : '-';
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (format & TERMO_FORMAT_CARETCTRL &&
|
|
|
|
key->type == TERMO_TYPE_KEY &&
|
|
|
|
key->modifiers == TERMO_KEYMOD_CTRL)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
uint32_t codepoint = key->code.codepoint;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
pos += l;
|
|
|
|
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;
|
|
|
|
pos += l;
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wrapbracket)
|
|
|
|
{
|
|
|
|
l = snprintf (buffer + pos, len - pos, "<");
|
|
|
|
if (l <= 0)
|
|
|
|
return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (key->modifiers & TERMO_KEYMOD_ALT)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
l = snprintf (buffer + pos, len - pos, "%s%c", mods->alt, sep);
|
2014-09-28 03:51:45 +02:00
|
|
|
if (l <= 0)
|
2014-09-23 01:38:08 +02:00
|
|
|
return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
if (key->modifiers & TERMO_KEYMOD_CTRL)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
l = snprintf (buffer + pos, len - pos, "%s%c", mods->ctrl, sep);
|
|
|
|
if (l <= 0)
|
|
|
|
return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
if (key->modifiers & TERMO_KEYMOD_SHIFT)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
l = snprintf (buffer + pos, len - pos, "%s%c", mods->shift, sep);
|
|
|
|
if (l <= 0)
|
|
|
|
return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (key->type)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_KEY:
|
2014-09-28 03:51:45 +02:00
|
|
|
if (!key->multibyte[0]) // In case of user-supplied key structures
|
|
|
|
fill_multibyte (tk, key);
|
|
|
|
l = snprintf (buffer + pos, len - pos, "%s", key->multibyte);
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_KEYSYM:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
const char *name = termo_get_keyname (tk, key->code.sym);
|
|
|
|
if (format & TERMO_FORMAT_LOWERSPACE)
|
2014-09-23 01:38:08 +02:00
|
|
|
l = snprint_cameltospaces (buffer + pos, len - pos, name);
|
|
|
|
else
|
|
|
|
l = snprintf (buffer + pos, len - pos, "%s", name);
|
|
|
|
break;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_FUNCTION:
|
2014-09-23 01:38:08 +02:00
|
|
|
l = snprintf (buffer + pos, len - pos, "%c%d",
|
2014-10-14 00:08:15 +02:00
|
|
|
(format & TERMO_FORMAT_LOWERSPACE ? 'f' : 'F'), key->code.number);
|
2014-09-23 01:38:08 +02:00
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_MOUSE:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_mouse_event_t ev;
|
2014-09-23 01:38:08 +02:00
|
|
|
int button;
|
|
|
|
int line, col;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_mouse (tk, key, &ev, &button, &line, &col);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
static const char *evnames[] =
|
|
|
|
{ "Unknown", "Press", "Drag", "Release" };
|
2014-09-28 03:51:45 +02:00
|
|
|
l = snprintf (buffer + pos, len - pos,
|
2014-09-23 01:38:08 +02:00
|
|
|
"Mouse%s(%d)", evnames[ev], button);
|
2014-10-14 00:08:15 +02:00
|
|
|
if (format & TERMO_FORMAT_MOUSE_POS)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
if (l <= 0)
|
|
|
|
return pos;
|
|
|
|
pos += l;
|
|
|
|
l = snprintf (buffer + pos, len - pos, " @ (%u,%u)", col, line);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_POSITION:
|
2014-09-23 01:38:08 +02:00
|
|
|
l = snprintf (buffer + pos, len - pos, "Position");
|
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_MODEREPORT:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
int initial, mode, value;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_modereport (tk, key, &initial, &mode, &value);
|
2014-09-23 01:38:08 +02:00
|
|
|
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);
|
2014-09-28 03:51:45 +02:00
|
|
|
break;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_UNKNOWN_CSI:
|
2014-09-23 01:38:08 +02:00
|
|
|
l = snprintf (buffer + pos, len - pos,
|
|
|
|
"CSI %c", key->code.number & 0xff);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l <= 0)
|
|
|
|
return pos;
|
|
|
|
pos += l;
|
|
|
|
|
|
|
|
if (wrapbracket)
|
|
|
|
{
|
|
|
|
l = snprintf (buffer + pos, len - pos, ">");
|
|
|
|
if (l <= 0)
|
|
|
|
return pos;
|
|
|
|
pos += l;
|
|
|
|
}
|
|
|
|
return pos;
|
2008-08-21 21:20:07 +02:00
|
|
|
}
|
2011-04-01 01:50:51 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
const char *
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_strpkey (termo_t *tk,
|
|
|
|
const char *str, termo_key_t *key, termo_format_t format)
|
2011-04-01 01:50:51 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
struct modnames *mods = &modnames[
|
2014-10-14 00:08:15 +02:00
|
|
|
!!(format & TERMO_FORMAT_LONGMOD) +
|
|
|
|
!!(format & TERMO_FORMAT_ALTISMETA) * 2 +
|
|
|
|
!!(format & TERMO_FORMAT_LOWERMOD) * 4];
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
key->modifiers = 0;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if ((format & TERMO_FORMAT_CARETCTRL) && str[0] == '^' && str[1])
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
str = termo_strpkey (tk,
|
|
|
|
str + 1, key, format & ~TERMO_FORMAT_CARETCTRL);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
if (!str
|
2014-10-14 00:08:15 +02:00
|
|
|
|| key->type != TERMO_TYPE_KEY
|
2014-09-23 01:38:08 +02:00
|
|
|
|| key->code.codepoint < '@'
|
|
|
|
|| key->code.codepoint > '_'
|
|
|
|
|| key->modifiers != 0)
|
|
|
|
return NULL;
|
|
|
|
|
2014-09-28 03:51:45 +02:00
|
|
|
if (key->code.codepoint >= 'A'
|
|
|
|
&& key->code.codepoint <= 'Z')
|
2014-09-23 01:38:08 +02:00
|
|
|
key->code.codepoint += 0x20;
|
2014-10-14 00:08:15 +02:00
|
|
|
key->modifiers = TERMO_KEYMOD_CTRL;
|
2014-09-28 03:51:45 +02:00
|
|
|
fill_multibyte (tk, key);
|
2014-09-23 01:38:08 +02:00
|
|
|
return (char *) str;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *sep_at;
|
|
|
|
while ((sep_at = strchr (str,
|
2014-10-14 00:08:15 +02:00
|
|
|
(format & TERMO_FORMAT_SPACEMOD) ? ' ' : '-')))
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
size_t n = sep_at - str;
|
|
|
|
if (n == strlen (mods->alt) && !strncmp (mods->alt, str, n))
|
2014-10-14 00:08:15 +02:00
|
|
|
key->modifiers |= TERMO_KEYMOD_ALT;
|
2014-09-23 01:38:08 +02:00
|
|
|
else if (n == strlen (mods->ctrl) && !strncmp (mods->ctrl, str, n))
|
2014-10-14 00:08:15 +02:00
|
|
|
key->modifiers |= TERMO_KEYMOD_CTRL;
|
2014-09-23 01:38:08 +02:00
|
|
|
else if (n == strlen (mods->shift) && !strncmp (mods->shift, str, n))
|
2014-10-14 00:08:15 +02:00
|
|
|
key->modifiers |= TERMO_KEYMOD_SHIFT;
|
2014-09-23 01:38:08 +02:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
|
|
|
|
str = sep_at + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t nbytes;
|
|
|
|
ssize_t snbytes;
|
|
|
|
const char *endstr;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if ((endstr = termo_lookup_keyname_format
|
2014-09-23 01:38:08 +02:00
|
|
|
(tk, str, &key->code.sym, format)))
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEYSYM;
|
2014-09-23 01:38:08 +02:00
|
|
|
str = endstr;
|
|
|
|
}
|
2014-10-14 21:19:11 +02:00
|
|
|
else if (sscanf (str, "F%d%zn", &key->code.number, &snbytes) == 1)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_FUNCTION;
|
2014-09-23 01:38:08 +02:00
|
|
|
str += snbytes;
|
|
|
|
}
|
2014-09-28 03:51:45 +02:00
|
|
|
// Multibyte must be last
|
|
|
|
else if (parse_multibyte (tk, (unsigned const char *) str, strlen (str),
|
2014-10-14 00:08:15 +02:00
|
|
|
&key->code.codepoint, &nbytes) == TERMO_RES_KEY)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
key->type = TERMO_TYPE_KEY;
|
2014-09-28 03:51:45 +02:00
|
|
|
fill_multibyte (tk, key);
|
2014-09-23 01:38:08 +02:00
|
|
|
str += nbytes;
|
|
|
|
}
|
|
|
|
// TODO: Consider mouse events?
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_canonicalise (tk, key);
|
2014-09-23 01:38:08 +02:00
|
|
|
return (char *) str;
|
2011-04-01 01:50:51 +02:00
|
|
|
}
|
2011-04-02 16:07:30 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_keycmp (termo_t *tk,
|
|
|
|
const termo_key_t *key1p, const termo_key_t *key2p)
|
2011-04-02 16:07:30 +02:00
|
|
|
{
|
2014-10-14 21:19:11 +02:00
|
|
|
// Copy the key structs since we'll be modifying them
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_key_t key1 = *key1p, key2 = *key2p;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_canonicalise (tk, &key1);
|
|
|
|
termo_canonicalise (tk, &key2);
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
if (key1.type != key2.type)
|
|
|
|
return key1.type - key2.type;
|
|
|
|
|
|
|
|
switch (key1.type)
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_KEY:
|
2014-09-23 01:38:08 +02:00
|
|
|
if (key1.code.codepoint != key2.code.codepoint)
|
|
|
|
return key1.code.codepoint - key2.code.codepoint;
|
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_KEYSYM:
|
2014-09-23 01:38:08 +02:00
|
|
|
if (key1.code.sym != key2.code.sym)
|
|
|
|
return key1.code.sym - key2.code.sym;
|
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_FUNCTION:
|
|
|
|
case TERMO_TYPE_UNKNOWN_CSI:
|
2014-09-23 01:38:08 +02:00
|
|
|
if (key1.code.number != key2.code.number)
|
|
|
|
return key1.code.number - key2.code.number;
|
|
|
|
break;
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_MOUSE:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-10-13 23:36:14 +02:00
|
|
|
int cmp = memcmp (&key1.code.mouse, &key2.code.mouse,
|
|
|
|
sizeof key1.code.mouse);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (cmp != 0)
|
|
|
|
return cmp;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_POSITION:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
int line1, col1, line2, col2;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_position (tk, &key1, &line1, &col1);
|
|
|
|
termo_interpret_position (tk, &key2, &line2, &col2);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (line1 != line2)
|
|
|
|
return line1 - line2;
|
|
|
|
return col1 - col2;
|
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
case TERMO_TYPE_MODEREPORT:
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
int initial1, initial2, mode1, mode2, value1, value2;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_interpret_modereport (tk, &key1, &initial1, &mode1, &value1);
|
|
|
|
termo_interpret_modereport (tk, &key2, &initial2, &mode2, &value2);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (initial1 != initial2)
|
|
|
|
return initial1 - initial2;
|
|
|
|
if (mode1 != mode2)
|
|
|
|
return mode1 - mode2;
|
|
|
|
return value1 - value2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return key1.modifiers - key2.modifiers;
|
2011-04-02 16:07:30 +02:00
|
|
|
}
|
2014-09-23 01:38:08 +02:00
|
|
|
|