2008-12-03 21:19:15 +01:00
|
|
|
// we want strdup()
|
2011-08-25 23:49:17 +02:00
|
|
|
#define _XOPEN_SOURCE 600
|
2008-12-03 21:19:15 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
#include "termo.h"
|
|
|
|
#include "termo-internal.h"
|
2008-10-08 03:04:47 +02:00
|
|
|
|
2011-09-24 01:04:01 +02:00
|
|
|
#ifdef HAVE_UNIBILIUM
|
|
|
|
# include <unibilium.h>
|
|
|
|
#else
|
|
|
|
# include <curses.h>
|
|
|
|
# include <term.h>
|
|
|
|
#endif
|
2009-11-24 02:31:35 +01:00
|
|
|
|
2008-10-08 03:04:47 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2008-11-02 18:59:30 +01:00
|
|
|
#include <unistd.h>
|
2008-10-08 03:04:47 +02:00
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// To be efficient at lookups, we store the byte sequence => keyinfo mapping
|
|
|
|
// in a trie. This avoids a slow linear search through a flat list of
|
|
|
|
// sequences. Because it is likely most nodes will be very sparse, we optimise
|
|
|
|
// vector to store an extent map after the database is loaded.
|
2008-11-12 17:32:17 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TYPE_KEY,
|
|
|
|
TYPE_ARRAY,
|
|
|
|
TYPE_MOUSE,
|
|
|
|
}
|
|
|
|
trie_nodetype_t;
|
2008-10-08 03:04:47 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
typedef struct trie_node
|
|
|
|
{
|
|
|
|
trie_nodetype_t type;
|
|
|
|
}
|
|
|
|
trie_node_t;
|
2008-12-03 21:19:15 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
typedef struct trie_node_key
|
|
|
|
{
|
|
|
|
trie_nodetype_t type;
|
|
|
|
keyinfo_t key;
|
|
|
|
}
|
|
|
|
trie_node_key_t;
|
2008-10-08 03:04:47 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
typedef struct trie_node_array
|
|
|
|
{
|
|
|
|
trie_nodetype_t type;
|
2014-10-14 21:19:11 +02:00
|
|
|
unsigned char min, max; // INCLUSIVE endpoints of the extent range
|
|
|
|
trie_node_t *arr[]; // dynamic size at allocation time
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
trie_node_array_t;
|
2008-10-08 03:04:47 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
typedef struct
|
2008-11-12 17:32:17 +01:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_t *tk;
|
2014-09-23 01:38:08 +02:00
|
|
|
trie_node_t *root;
|
2008-11-12 17:32:17 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
char *start_string;
|
|
|
|
char *stop_string;
|
2014-10-13 01:03:13 +02:00
|
|
|
|
|
|
|
bool have_mouse;
|
|
|
|
char *set_mouse_string;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_ti_t;
|
2008-11-12 17:32:17 +01:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static int funcname2keysym (const char *funcname, termo_type_t *typep,
|
|
|
|
termo_sym_t *symp, int *modmask, int *modsetp);
|
|
|
|
static int insert_seq (termo_ti_t *ti, const char *seq, trie_node_t *node);
|
2008-11-12 17:32:17 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static trie_node_t *
|
2014-10-14 00:08:15 +02:00
|
|
|
new_node_key (termo_type_t type, termo_sym_t sym, int modmask, int modset)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
trie_node_key_t *n = malloc (sizeof *n);
|
|
|
|
if (!n)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
n->type = TYPE_KEY;
|
|
|
|
n->key.type = type;
|
|
|
|
n->key.sym = sym;
|
|
|
|
n->key.modifier_mask = modmask;
|
|
|
|
n->key.modifier_set = modset;
|
|
|
|
return (trie_node_t *) n;
|
2008-11-12 17:32:17 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static trie_node_t *
|
|
|
|
new_node_arr (unsigned char min, unsigned char max)
|
2008-11-12 17:32:17 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
trie_node_array_t *n = malloc (sizeof *n
|
|
|
|
+ ((int) max - min + 1) * sizeof n->arr[0]);
|
|
|
|
if (!n)
|
|
|
|
return NULL;
|
2008-11-12 17:32:17 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
n->type = TYPE_ARRAY;
|
|
|
|
n->min = min;
|
|
|
|
n->max = max;
|
2008-11-12 17:32:17 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
int i;
|
|
|
|
for (i = min; i <= max; i++)
|
|
|
|
n->arr[i - min] = NULL;
|
2008-11-12 17:32:17 +01:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
return (trie_node_t *) n;
|
2008-11-12 17:32:17 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static trie_node_t *
|
|
|
|
lookup_next (trie_node_t *n, unsigned char b)
|
2008-11-12 17:32:17 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
switch (n->type)
|
|
|
|
{
|
|
|
|
case TYPE_KEY:
|
|
|
|
case TYPE_MOUSE:
|
2014-09-28 03:51:45 +02:00
|
|
|
fprintf (stderr, "fatal: lookup_next within a TYPE_KEY node\n");
|
2014-09-23 01:38:08 +02:00
|
|
|
abort ();
|
|
|
|
case TYPE_ARRAY:
|
|
|
|
{
|
|
|
|
trie_node_array_t *nar = (trie_node_array_t *) n;
|
|
|
|
if (b < nar->min || b > nar->max)
|
|
|
|
return NULL;
|
|
|
|
return nar->arr[b - nar->min];
|
|
|
|
}
|
|
|
|
}
|
2014-09-28 03:51:45 +02:00
|
|
|
return NULL; // Never reached but keeps compiler happy
|
2008-11-12 17:32:17 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
|
|
|
free_trie (trie_node_t *n)
|
2008-11-12 17:32:17 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
switch (n->type)
|
|
|
|
{
|
|
|
|
case TYPE_KEY:
|
|
|
|
case TYPE_MOUSE:
|
|
|
|
break;
|
|
|
|
case TYPE_ARRAY:
|
|
|
|
{
|
|
|
|
trie_node_array_t *nar = (trie_node_array_t *) n;
|
|
|
|
int i;
|
|
|
|
for (i = nar->min; i <= nar->max; i++)
|
|
|
|
if (nar->arr[i - nar->min])
|
|
|
|
free_trie (nar->arr[i - nar->min]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free (n);
|
2008-11-12 17:32:17 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static trie_node_t *
|
|
|
|
compress_trie (struct trie_node *n)
|
2008-11-13 00:58:20 +01:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!n)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
switch (n->type)
|
|
|
|
{
|
|
|
|
case TYPE_KEY:
|
|
|
|
case TYPE_MOUSE:
|
|
|
|
return n;
|
|
|
|
case TYPE_ARRAY:
|
|
|
|
{
|
|
|
|
trie_node_array_t *nar = (trie_node_array_t *) n;
|
|
|
|
// Find the real bounds
|
|
|
|
unsigned char min, max;
|
|
|
|
for (min = 0; !nar->arr[min]; min++)
|
|
|
|
;
|
|
|
|
for (max = 0xff; !nar->arr[max]; max--)
|
|
|
|
;
|
|
|
|
|
|
|
|
trie_node_array_t *new = (trie_node_array_t *) new_node_arr (min, max);
|
|
|
|
int i;
|
|
|
|
for (i = min; i <= max; i++)
|
|
|
|
new->arr[i - min] = compress_trie (nar->arr[i]);
|
|
|
|
|
|
|
|
free (nar);
|
|
|
|
return (trie_node_t *) new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n;
|
2008-11-13 00:58:20 +01:00
|
|
|
}
|
|
|
|
|
2014-10-13 01:03:13 +02:00
|
|
|
static bool
|
2014-10-14 00:08:15 +02:00
|
|
|
load_terminfo (termo_ti_t *ti, const char *term)
|
2008-10-08 03:04:47 +02:00
|
|
|
{
|
2014-10-13 01:03:13 +02:00
|
|
|
const char *mouse_report_string = NULL;
|
2011-09-24 01:04:01 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_UNIBILIUM
|
2014-09-23 01:38:08 +02:00
|
|
|
unibi_term *unibi = unibi_from_term (term);
|
|
|
|
if (!unibi)
|
2014-10-13 01:03:13 +02:00
|
|
|
return false;
|
2008-10-09 21:23:45 +02:00
|
|
|
|
2014-10-13 01:03:13 +02:00
|
|
|
for (int i = unibi_string_begin_ + 1; i < unibi_string_end_; i++)
|
|
|
|
{
|
|
|
|
const char *name = unibi_name_str (i);
|
|
|
|
const char *value = unibi_get_str (unibi, i);
|
|
|
|
#else
|
2014-10-14 21:19:11 +02:00
|
|
|
// Have to cast away the const. But it's OK - we know terminfo won't
|
|
|
|
// really modify term
|
2014-10-13 01:03:13 +02:00
|
|
|
int err;
|
2014-09-23 01:38:08 +02:00
|
|
|
if (setupterm ((char *) term, 1, &err) != OK)
|
2014-10-13 01:03:13 +02:00
|
|
|
return false;
|
2011-09-24 01:04:01 +02:00
|
|
|
|
2014-10-13 01:03:13 +02:00
|
|
|
for (int i = 0; strfnames[i]; i++)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
const char *name = strfnames[i];
|
2014-10-13 01:03:13 +02:00
|
|
|
const char *value = tigetstr (strnames[i]);
|
2011-09-24 01:04:01 +02:00
|
|
|
#endif
|
2014-10-13 01:03:13 +02:00
|
|
|
// Only care about the key_* constants
|
2014-09-23 01:38:08 +02:00
|
|
|
if (strncmp (name, "key_", 4) != 0)
|
|
|
|
continue;
|
|
|
|
if (!value || value == (char*) -1)
|
|
|
|
continue;
|
|
|
|
|
2014-10-13 01:03:13 +02:00
|
|
|
trie_node_t *node = NULL;
|
|
|
|
if (!strcmp (name + 4, "mouse"))
|
|
|
|
mouse_report_string = value;
|
2014-09-23 01:38:08 +02:00
|
|
|
else
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_type_t type;
|
|
|
|
termo_sym_t sym;
|
2014-09-23 01:38:08 +02:00
|
|
|
int mask = 0;
|
|
|
|
int set = 0;
|
|
|
|
|
|
|
|
if (!funcname2keysym (name + 4, &type, &sym, &mask, &set))
|
|
|
|
continue;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (sym == TERMO_SYM_NONE)
|
2014-09-23 01:38:08 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
node = new_node_key (type, sym, mask, set);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node && !insert_seq (ti, value, node))
|
|
|
|
{
|
2014-10-09 23:47:24 +02:00
|
|
|
free (node);
|
2014-10-13 01:03:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone the behaviour of ncurses for xterm mouse support
|
|
|
|
#ifdef HAVE_UNIBILIUM
|
|
|
|
const char *set_mouse_string = unibi_get_str (unibi, "XM");
|
|
|
|
#else
|
|
|
|
const char *set_mouse_string = tigetstr ("XM");
|
|
|
|
#endif
|
|
|
|
if (!set_mouse_string || set_mouse_string == (char *) -1)
|
|
|
|
ti->set_mouse_string = strdup ("\E[?1000%?%p1%{1}%=%th%el%;");
|
|
|
|
else
|
|
|
|
ti->set_mouse_string = strdup (set_mouse_string);
|
|
|
|
|
|
|
|
if (!mouse_report_string && strstr (term, "xterm"))
|
|
|
|
mouse_report_string = "\x1b[M";
|
|
|
|
if (mouse_report_string)
|
|
|
|
{
|
|
|
|
ti->have_mouse = true;
|
|
|
|
|
|
|
|
trie_node_t *node = malloc (sizeof *node);
|
|
|
|
if (!node)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
node->type = TYPE_MOUSE;
|
|
|
|
if (!insert_seq (ti, mouse_report_string, node))
|
|
|
|
{
|
|
|
|
free (node);
|
|
|
|
return false;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-14 21:19:11 +02:00
|
|
|
// Take copies of these terminfo strings, in case we build multiple termo
|
|
|
|
// instances for multiple different termtypes, and it's different by the
|
|
|
|
// time we want to use it
|
2011-09-24 01:04:01 +02:00
|
|
|
#ifdef HAVE_UNIBILIUM
|
2014-09-23 01:38:08 +02:00
|
|
|
const char *keypad_xmit = unibi_get_str (unibi, unibi_pkey_xmit);
|
2011-09-24 01:04:01 +02:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (keypad_xmit)
|
|
|
|
ti->start_string = strdup (keypad_xmit);
|
|
|
|
else
|
|
|
|
ti->start_string = NULL;
|
2008-12-03 21:19:15 +01:00
|
|
|
|
2011-09-24 01:04:01 +02:00
|
|
|
#ifdef HAVE_UNIBILIUM
|
2014-09-23 01:38:08 +02:00
|
|
|
const char *keypad_local = unibi_get_str (unibi, unibi_pkey_local);
|
2011-09-24 01:04:01 +02:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (keypad_local)
|
|
|
|
ti->stop_string = strdup (keypad_local);
|
|
|
|
else
|
|
|
|
ti->stop_string = NULL;
|
2008-12-03 21:19:15 +01:00
|
|
|
|
2011-09-24 01:04:01 +02:00
|
|
|
#ifdef HAVE_UNIBILIUM
|
2014-09-23 01:38:08 +02:00
|
|
|
unibi_destroy (unibi);
|
2011-09-24 01:04:01 +02:00
|
|
|
#endif
|
|
|
|
|
2014-10-13 01:03:13 +02:00
|
|
|
return true;
|
2011-09-24 00:40:54 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void *
|
2014-10-14 00:08:15 +02:00
|
|
|
new_driver (termo_t *tk, const char *term)
|
2011-09-24 00:40:54 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_ti_t *ti = calloc (1, sizeof *ti);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!ti)
|
|
|
|
return NULL;
|
2011-09-24 00:40:54 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
ti->tk = tk;
|
|
|
|
ti->root = new_node_arr (0, 0xff);
|
|
|
|
if (!ti->root)
|
|
|
|
goto abort_free_ti;
|
2011-09-24 00:40:54 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
if (!load_terminfo (ti, term))
|
|
|
|
goto abort_free_trie;
|
2011-09-24 00:40:54 +02:00
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
ti->root = compress_trie (ti->root);
|
|
|
|
return ti;
|
2008-10-10 00:19:10 +02:00
|
|
|
|
2008-11-12 17:32:17 +01:00
|
|
|
abort_free_trie:
|
2014-09-28 03:51:45 +02:00
|
|
|
free_trie (ti->root);
|
2008-11-12 17:10:58 +01:00
|
|
|
|
2008-10-10 00:19:10 +02:00
|
|
|
abort_free_ti:
|
2014-09-28 03:51:45 +02:00
|
|
|
free (ti);
|
2014-09-23 01:38:08 +02:00
|
|
|
return NULL;
|
2008-10-08 03:04:47 +02:00
|
|
|
}
|
|
|
|
|
2014-10-13 01:03:13 +02:00
|
|
|
static bool
|
2014-10-14 00:08:15 +02:00
|
|
|
write_string (termo_t *tk, char *string)
|
2008-11-02 18:59:30 +01:00
|
|
|
{
|
2014-09-23 02:51:08 +02:00
|
|
|
if (tk->fd == -1 || !isatty (tk->fd) || !string)
|
2014-10-13 01:03:13 +02:00
|
|
|
return true;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-13 01:03:13 +02:00
|
|
|
// The terminfo database will contain keys in application cursor key mode.
|
|
|
|
// We may need to enable that mode
|
2014-09-23 01:38:08 +02:00
|
|
|
|
|
|
|
// Can't call putp or tputs because they suck and don't give us fd control
|
2014-09-23 02:51:08 +02:00
|
|
|
size_t len = strlen (string);
|
2014-09-23 01:38:08 +02:00
|
|
|
while (len)
|
|
|
|
{
|
2014-09-23 02:51:08 +02:00
|
|
|
ssize_t written = write (tk->fd, string, len);
|
2014-09-23 01:38:08 +02:00
|
|
|
if (written == -1)
|
2014-10-13 01:03:13 +02:00
|
|
|
return false;
|
2014-09-23 02:51:08 +02:00
|
|
|
string += written;
|
2014-09-23 01:38:08 +02:00
|
|
|
len -= written;
|
|
|
|
}
|
2014-10-13 01:03:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2014-10-14 00:08:15 +02:00
|
|
|
set_mouse (termo_ti_t *ti, bool enable)
|
2014-10-13 01:03:13 +02:00
|
|
|
{
|
|
|
|
#ifdef HAVE_UNIBILIUM
|
|
|
|
unibi_var_t params[9] = { enable, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
char start_string[unibi_run (ti->set_mouse_string, params, NULL, 0)];
|
|
|
|
unibi_run (ti->set_mouse_string, params,
|
|
|
|
start_string, sizeof start_string);
|
|
|
|
#else
|
|
|
|
char *start_string = tparm (ti->set_mouse_string,
|
|
|
|
enable, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
|
#endif
|
|
|
|
return write_string (ti->tk, start_string);
|
2008-11-02 18:59:30 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static int
|
2014-10-14 00:08:15 +02:00
|
|
|
start_driver (termo_t *tk, void *info)
|
2008-11-02 18:59:30 +01:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_ti_t *ti = info;
|
2014-10-13 01:03:13 +02:00
|
|
|
// TODO: Don't start the mouse automatically, find a nice place to put
|
|
|
|
// a public function to be called by users.
|
|
|
|
// TODO: Try to autodetect rxvt and use its protocol instead of mode 1000
|
|
|
|
// TODO: Also give the user a choice to use 1005, 1006 or 1015
|
|
|
|
if (ti->have_mouse && !set_mouse (ti, true))
|
|
|
|
return false;
|
2014-09-23 02:51:08 +02:00
|
|
|
return write_string (tk, ti->start_string);
|
|
|
|
}
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-09-23 02:51:08 +02:00
|
|
|
static int
|
2014-10-14 00:08:15 +02:00
|
|
|
stop_driver (termo_t *tk, void *info)
|
2014-09-23 02:51:08 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_ti_t *ti = info;
|
2014-10-13 01:03:13 +02:00
|
|
|
if (ti->have_mouse && !set_mouse (ti, false))
|
|
|
|
return false;
|
2014-09-23 02:51:08 +02:00
|
|
|
return write_string (tk, ti->stop_string);
|
2008-11-02 18:59:30 +01:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static void
|
|
|
|
free_driver (void *info)
|
2008-10-08 03:04:47 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_ti_t *ti = info;
|
2014-09-23 01:38:08 +02:00
|
|
|
free_trie (ti->root);
|
2014-10-13 01:03:13 +02:00
|
|
|
free (ti->set_mouse_string);
|
2014-09-23 01:38:08 +02:00
|
|
|
free (ti->start_string);
|
|
|
|
free (ti->stop_string);
|
|
|
|
free (ti);
|
2008-10-08 03:04:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
static termo_result_t
|
|
|
|
peekkey (termo_t *tk, void *info,
|
|
|
|
termo_key_t *key, int force, size_t *nbytep)
|
2008-10-08 03:04:47 +02:00
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_ti_t *ti = info;
|
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
|
|
|
|
|
|
|
trie_node_t *p = ti->root;
|
|
|
|
unsigned int pos = 0;
|
|
|
|
while (pos < tk->buffcount)
|
|
|
|
{
|
|
|
|
p = lookup_next (p, CHARAT (pos));
|
|
|
|
if (!p)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
if (p->type == TYPE_KEY)
|
|
|
|
{
|
|
|
|
trie_node_key_t *nk = (trie_node_key_t *) p;
|
|
|
|
key->type = nk->key.type;
|
|
|
|
key->code.sym = nk->key.sym;
|
|
|
|
key->modifiers = nk->key.modifier_set;
|
|
|
|
*nbytep = pos;
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_KEY;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
else if (p->type == TYPE_MOUSE)
|
|
|
|
{
|
|
|
|
tk->buffstart += pos;
|
|
|
|
tk->buffcount -= pos;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_result_t mouse_result =
|
2014-09-23 01:38:08 +02:00
|
|
|
(*tk->method.peekkey_mouse) (tk, key, nbytep);
|
|
|
|
|
|
|
|
tk->buffstart -= pos;
|
|
|
|
tk->buffcount += pos;
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
if (mouse_result == TERMO_RES_KEY)
|
2014-09-23 01:38:08 +02:00
|
|
|
*nbytep += pos;
|
|
|
|
|
|
|
|
return mouse_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If p is not NULL then we hadn't walked off the end yet, so we have a
|
|
|
|
// partial match
|
|
|
|
if (p && !force)
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_AGAIN;
|
2014-09-23 01:38:08 +02:00
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
return TERMO_RES_NONE;
|
2008-10-08 03:04:47 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 02:41:40 +02:00
|
|
|
static struct func
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
const char *funcname;
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_type_t type;
|
|
|
|
termo_sym_t sym;
|
2014-09-23 01:38:08 +02:00
|
|
|
int mods;
|
|
|
|
}
|
|
|
|
funcs[] =
|
2008-10-08 03:04:47 +02:00
|
|
|
{
|
2014-10-14 21:19:11 +02:00
|
|
|
// THIS LIST MUST REMAIN SORTED!
|
2014-10-14 00:08:15 +02:00
|
|
|
{ "backspace", TERMO_TYPE_KEYSYM, TERMO_SYM_BACKSPACE, 0 },
|
|
|
|
{ "begin", TERMO_TYPE_KEYSYM, TERMO_SYM_BEGIN, 0 },
|
|
|
|
{ "beg", TERMO_TYPE_KEYSYM, TERMO_SYM_BEGIN, 0 },
|
|
|
|
{ "btab", TERMO_TYPE_KEYSYM, TERMO_SYM_TAB, TERMO_KEYMOD_SHIFT },
|
|
|
|
{ "cancel", TERMO_TYPE_KEYSYM, TERMO_SYM_CANCEL, 0 },
|
|
|
|
{ "clear", TERMO_TYPE_KEYSYM, TERMO_SYM_CLEAR, 0 },
|
|
|
|
{ "close", TERMO_TYPE_KEYSYM, TERMO_SYM_CLOSE, 0 },
|
|
|
|
{ "command", TERMO_TYPE_KEYSYM, TERMO_SYM_COMMAND, 0 },
|
|
|
|
{ "copy", TERMO_TYPE_KEYSYM, TERMO_SYM_COPY, 0 },
|
|
|
|
{ "dc", TERMO_TYPE_KEYSYM, TERMO_SYM_DELETE, 0 },
|
|
|
|
{ "down", TERMO_TYPE_KEYSYM, TERMO_SYM_DOWN, 0 },
|
|
|
|
{ "end", TERMO_TYPE_KEYSYM, TERMO_SYM_END, 0 },
|
|
|
|
{ "enter", TERMO_TYPE_KEYSYM, TERMO_SYM_ENTER, 0 },
|
|
|
|
{ "exit", TERMO_TYPE_KEYSYM, TERMO_SYM_EXIT, 0 },
|
|
|
|
{ "find", TERMO_TYPE_KEYSYM, TERMO_SYM_FIND, 0 },
|
|
|
|
{ "help", TERMO_TYPE_KEYSYM, TERMO_SYM_HELP, 0 },
|
|
|
|
{ "home", TERMO_TYPE_KEYSYM, TERMO_SYM_HOME, 0 },
|
|
|
|
{ "ic", TERMO_TYPE_KEYSYM, TERMO_SYM_INSERT, 0 },
|
|
|
|
{ "left", TERMO_TYPE_KEYSYM, TERMO_SYM_LEFT, 0 },
|
|
|
|
{ "mark", TERMO_TYPE_KEYSYM, TERMO_SYM_MARK, 0 },
|
|
|
|
{ "message", TERMO_TYPE_KEYSYM, TERMO_SYM_MESSAGE, 0 },
|
|
|
|
{ "mouse", TERMO_TYPE_KEYSYM, TERMO_SYM_NONE, 0 },
|
|
|
|
{ "move", TERMO_TYPE_KEYSYM, TERMO_SYM_MOVE, 0 },
|
2014-09-23 01:38:08 +02:00
|
|
|
// Not quite, but it's the best we can do
|
2014-10-14 00:08:15 +02:00
|
|
|
{ "next", TERMO_TYPE_KEYSYM, TERMO_SYM_PAGEDOWN, 0 },
|
|
|
|
{ "npage", TERMO_TYPE_KEYSYM, TERMO_SYM_PAGEDOWN, 0 },
|
|
|
|
{ "open", TERMO_TYPE_KEYSYM, TERMO_SYM_OPEN, 0 },
|
|
|
|
{ "options", TERMO_TYPE_KEYSYM, TERMO_SYM_OPTIONS, 0 },
|
|
|
|
{ "ppage", TERMO_TYPE_KEYSYM, TERMO_SYM_PAGEUP, 0 },
|
2014-09-23 01:38:08 +02:00
|
|
|
// Not quite, but it's the best we can do
|
2014-10-14 00:08:15 +02:00
|
|
|
{ "previous", TERMO_TYPE_KEYSYM, TERMO_SYM_PAGEUP, 0 },
|
|
|
|
{ "print", TERMO_TYPE_KEYSYM, TERMO_SYM_PRINT, 0 },
|
|
|
|
{ "redo", TERMO_TYPE_KEYSYM, TERMO_SYM_REDO, 0 },
|
|
|
|
{ "reference", TERMO_TYPE_KEYSYM, TERMO_SYM_REFERENCE, 0 },
|
|
|
|
{ "refresh", TERMO_TYPE_KEYSYM, TERMO_SYM_REFRESH, 0 },
|
|
|
|
{ "replace", TERMO_TYPE_KEYSYM, TERMO_SYM_REPLACE, 0 },
|
|
|
|
{ "restart", TERMO_TYPE_KEYSYM, TERMO_SYM_RESTART, 0 },
|
|
|
|
{ "resume", TERMO_TYPE_KEYSYM, TERMO_SYM_RESUME, 0 },
|
|
|
|
{ "right", TERMO_TYPE_KEYSYM, TERMO_SYM_RIGHT, 0 },
|
|
|
|
{ "save", TERMO_TYPE_KEYSYM, TERMO_SYM_SAVE, 0 },
|
|
|
|
{ "select", TERMO_TYPE_KEYSYM, TERMO_SYM_SELECT, 0 },
|
|
|
|
{ "suspend", TERMO_TYPE_KEYSYM, TERMO_SYM_SUSPEND, 0 },
|
|
|
|
{ "undo", TERMO_TYPE_KEYSYM, TERMO_SYM_UNDO, 0 },
|
|
|
|
{ "up", TERMO_TYPE_KEYSYM, TERMO_SYM_UP, 0 },
|
2014-09-23 01:38:08 +02:00
|
|
|
{ NULL },
|
2008-10-08 03:04:47 +02:00
|
|
|
};
|
|
|
|
|
2014-09-23 02:41:40 +02:00
|
|
|
static int
|
|
|
|
func_compare (const void *key, const void *element)
|
|
|
|
{
|
|
|
|
return strcmp (key, ((struct func *) element)->funcname);
|
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static int
|
|
|
|
funcname2keysym (const char *funcname,
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_type_t *typep, termo_sym_t *symp, int *modmaskp, int *modsetp)
|
2008-10-08 03:04:47 +02:00
|
|
|
{
|
2014-09-23 02:41:40 +02:00
|
|
|
struct func *func = bsearch (funcname, funcs,
|
|
|
|
sizeof funcs / sizeof funcs[0], sizeof funcs[0], func_compare);
|
|
|
|
if (func)
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
2014-09-23 02:41:40 +02:00
|
|
|
*typep = func->type;
|
|
|
|
*symp = func->sym;
|
|
|
|
*modmaskp = func->mods;
|
|
|
|
*modsetp = func->mods;
|
|
|
|
return 1;
|
2014-09-23 01:38:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (funcname[0] == 'f' && isdigit (funcname[1]))
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
*typep = TERMO_TYPE_FUNCTION;
|
2014-09-23 01:38:08 +02:00
|
|
|
*symp = atoi (funcname + 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last-ditch attempt; maybe it's a shift key?
|
|
|
|
if (funcname[0] == 's' && funcname2keysym
|
|
|
|
(funcname + 1, typep, symp, modmaskp, modsetp))
|
|
|
|
{
|
2014-10-14 00:08:15 +02:00
|
|
|
*modmaskp |= TERMO_KEYMOD_SHIFT;
|
|
|
|
*modsetp |= TERMO_KEYMOD_SHIFT;
|
2014-09-23 01:38:08 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2008-10-08 03:04:47 +02:00
|
|
|
|
2008-11-18 16:55:29 +01:00
|
|
|
#ifdef DEBUG
|
2014-09-28 03:51:45 +02:00
|
|
|
fprintf (stderr, "TODO: Need to convert funcname"
|
|
|
|
" %s to a type/sym\n", funcname);
|
2008-11-18 16:55:29 +01:00
|
|
|
#endif
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
return 0;
|
2008-10-08 03:04:47 +02:00
|
|
|
}
|
|
|
|
|
2014-09-23 01:38:08 +02:00
|
|
|
static int
|
2014-10-14 00:08:15 +02:00
|
|
|
insert_seq (termo_ti_t *ti, const char *seq, trie_node_t *node)
|
2008-10-08 03:04:47 +02:00
|
|
|
{
|
2014-09-23 01:38:08 +02:00
|
|
|
int pos = 0;
|
|
|
|
trie_node_t *p = ti->root;
|
|
|
|
|
|
|
|
// Unsigned because we'll be using it as an array subscript
|
|
|
|
unsigned char b;
|
|
|
|
|
|
|
|
while ((b = seq[pos]))
|
|
|
|
{
|
|
|
|
trie_node_t *next = lookup_next (p, b);
|
|
|
|
if (!next)
|
|
|
|
break;
|
|
|
|
p = next;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((b = seq[pos]))
|
|
|
|
{
|
|
|
|
trie_node_t *next;
|
|
|
|
if (seq[pos+1])
|
|
|
|
// Intermediate node
|
|
|
|
next = new_node_arr (0, 0xff);
|
|
|
|
else
|
|
|
|
// Final key node
|
|
|
|
next = node;
|
|
|
|
|
|
|
|
if (!next)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (p->type)
|
|
|
|
{
|
|
|
|
case TYPE_ARRAY:
|
|
|
|
{
|
|
|
|
trie_node_array_t *nar = (trie_node_array_t *) p;
|
|
|
|
if (b < nar->min || b > nar->max)
|
|
|
|
{
|
2014-09-28 03:51:45 +02:00
|
|
|
fprintf (stderr, "fatal: trie insert at 0x%02x is outside of"
|
|
|
|
" extent bounds (0x%02x..0x%02x)\n",
|
2014-09-23 01:38:08 +02:00
|
|
|
b, nar->min, nar->max);
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
nar->arr[b - nar->min] = next;
|
|
|
|
p = next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TYPE_KEY:
|
|
|
|
case TYPE_MOUSE:
|
2014-09-28 03:51:45 +02:00
|
|
|
fprintf (stderr, "fatal: tried to insert child node in TYPE_KEY\n");
|
2014-09-23 01:38:08 +02:00
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
return 1;
|
2008-10-08 03:04:47 +02:00
|
|
|
}
|
|
|
|
|
2014-10-14 00:08:15 +02:00
|
|
|
termo_driver_t termo_driver_ti =
|
2014-09-23 01:38:08 +02:00
|
|
|
{
|
|
|
|
.name = "terminfo",
|
|
|
|
.new_driver = new_driver,
|
|
|
|
.free_driver = free_driver,
|
|
|
|
.start_driver = start_driver,
|
|
|
|
.stop_driver = stop_driver,
|
|
|
|
.peekkey = peekkey,
|
2008-10-08 03:04:47 +02:00
|
|
|
};
|