Renamed all type names to CamelCaseNames for visual distinctness, separate from variables/functions
This commit is contained in:
parent
af50967d12
commit
be998cef1b
2
Makefile
2
Makefile
|
@ -4,7 +4,7 @@ CFLAGS?=
|
||||||
CFLAGS_DEBUG=
|
CFLAGS_DEBUG=
|
||||||
|
|
||||||
VERSION_MAJOR=0
|
VERSION_MAJOR=0
|
||||||
VERSION_MINOR=2
|
VERSION_MINOR=3
|
||||||
|
|
||||||
SONAME=libtermkey.so.$(VERSION_MAJOR)
|
SONAME=libtermkey.so.$(VERSION_MAJOR)
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
#include "termkey.h"
|
#include "termkey.h"
|
||||||
|
|
||||||
static void on_key(termkey_t *tk, termkey_key *key)
|
static void on_key(TermKey *tk, TermKeyKey *key)
|
||||||
{
|
{
|
||||||
char buffer[50];
|
char buffer[50];
|
||||||
termkey_snprint_key(tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM);
|
termkey_snprint_key(tk, buffer, sizeof buffer, key, TERMKEY_FORMAT_VIM);
|
||||||
|
@ -14,7 +14,7 @@ int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
TERMKEY_CHECK_VERSION;
|
TERMKEY_CHECK_VERSION;
|
||||||
|
|
||||||
termkey_t *tk = termkey_new(0, 0);
|
TermKey *tk = termkey_new(0, 0);
|
||||||
|
|
||||||
if(!tk) {
|
if(!tk) {
|
||||||
fprintf(stderr, "Cannot allocate termkey instance\n");
|
fprintf(stderr, "Cannot allocate termkey instance\n");
|
||||||
|
@ -26,8 +26,8 @@ int main(int argc, char *argv[])
|
||||||
fd.fd = 0; /* the file descriptor we passed to termkey_new() */
|
fd.fd = 0; /* the file descriptor we passed to termkey_new() */
|
||||||
fd.events = POLLIN;
|
fd.events = POLLIN;
|
||||||
|
|
||||||
termkey_result ret;
|
TermKeyResult ret;
|
||||||
termkey_key key;
|
TermKeyKey key;
|
||||||
|
|
||||||
int running = 1;
|
int running = 1;
|
||||||
int nextwait = -1;
|
int nextwait = -1;
|
||||||
|
|
6
demo.c
6
demo.c
|
@ -7,15 +7,15 @@ int main(int argc, char *argv[])
|
||||||
TERMKEY_CHECK_VERSION;
|
TERMKEY_CHECK_VERSION;
|
||||||
|
|
||||||
char buffer[50];
|
char buffer[50];
|
||||||
termkey_t *tk = termkey_new(0, 0);
|
TermKey *tk = termkey_new(0, 0);
|
||||||
|
|
||||||
if(!tk) {
|
if(!tk) {
|
||||||
fprintf(stderr, "Cannot allocate termkey instance\n");
|
fprintf(stderr, "Cannot allocate termkey instance\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
termkey_result ret;
|
TermKeyResult ret;
|
||||||
termkey_key key;
|
TermKeyKey key;
|
||||||
|
|
||||||
while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
|
while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
|
||||||
termkey_snprint_key(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_VIM);
|
termkey_snprint_key(tk, buffer, sizeof buffer, &key, TERMKEY_FORMAT_VIM);
|
||||||
|
|
44
driver-csi.c
44
driver-csi.c
|
@ -5,7 +5,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
termkey_t *tk;
|
TermKey *tk;
|
||||||
|
|
||||||
// There are 64 codes 0x40 - 0x7F
|
// There are 64 codes 0x40 - 0x7F
|
||||||
struct keyinfo csi_ss3s[64];
|
struct keyinfo csi_ss3s[64];
|
||||||
|
@ -14,17 +14,17 @@ typedef struct {
|
||||||
|
|
||||||
int ncsifuncs;
|
int ncsifuncs;
|
||||||
struct keyinfo *csifuncs;
|
struct keyinfo *csifuncs;
|
||||||
} termkey_csi;
|
} TermKeyCsi;
|
||||||
|
|
||||||
static termkey_keysym register_csi_ss3(termkey_csi *csi, termkey_type type, termkey_keysym sym, unsigned char cmd, const char *name);
|
static TermKeySym register_csi_ss3(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, unsigned char cmd, const char *name);
|
||||||
static termkey_keysym register_ss3kpalt(termkey_csi *csi, termkey_type type, termkey_keysym sym, unsigned char cmd, const char *name, char kpalt);
|
static TermKeySym register_ss3kpalt(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, unsigned char cmd, const char *name, char kpalt);
|
||||||
static termkey_keysym register_csifunc(termkey_csi *csi, termkey_type type, termkey_keysym sym, int number, const char *name);
|
static TermKeySym register_csifunc(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, int number, const char *name);
|
||||||
|
|
||||||
static termkey_keysym register_csi_ss3_full(termkey_csi *csi, termkey_type type, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name);
|
static TermKeySym register_csi_ss3_full(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name);
|
||||||
static termkey_keysym register_ss3kpalt_full(termkey_csi *csi, termkey_type type, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name, char kpalt);
|
static TermKeySym register_ss3kpalt_full(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name, char kpalt);
|
||||||
static termkey_keysym register_csifunc_full(termkey_csi *csi, termkey_type type, termkey_keysym sym, int modifier_set, int modifier_mask, int number, const char *name);
|
static TermKeySym register_csifunc_full(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, int modifier_set, int modifier_mask, int number, const char *name);
|
||||||
|
|
||||||
static void *new_driver(termkey_t *tk, const char *term)
|
static void *new_driver(TermKey *tk, const char *term)
|
||||||
{
|
{
|
||||||
if(strncmp(term, "xterm", 5) == 0) {
|
if(strncmp(term, "xterm", 5) == 0) {
|
||||||
// We want "xterm" or "xtermc" or "xterm-..."
|
// We want "xterm" or "xtermc" or "xterm-..."
|
||||||
|
@ -43,7 +43,7 @@ static void *new_driver(termkey_t *tk, const char *term)
|
||||||
|
|
||||||
// Excellent - we'll continue
|
// Excellent - we'll continue
|
||||||
|
|
||||||
termkey_csi *csi = malloc(sizeof *csi);
|
TermKeyCsi *csi = malloc(sizeof *csi);
|
||||||
if(!csi)
|
if(!csi)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -139,7 +139,7 @@ abort_free_csi:
|
||||||
|
|
||||||
static void free_driver(void *info)
|
static void free_driver(void *info)
|
||||||
{
|
{
|
||||||
termkey_csi *csi = info;
|
TermKeyCsi *csi = info;
|
||||||
|
|
||||||
free(csi->csifuncs); csi->csifuncs = NULL;
|
free(csi->csifuncs); csi->csifuncs = NULL;
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ static void free_driver(void *info)
|
||||||
|
|
||||||
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
||||||
|
|
||||||
static termkey_result peekkey_csi(termkey_t *tk, termkey_csi *csi, size_t introlen, termkey_key *key, int force, size_t *nbytep)
|
static TermKeyResult peekkey_csi(TermKey *tk, TermKeyCsi *csi, size_t introlen, TermKeyKey *key, int force, size_t *nbytep)
|
||||||
{
|
{
|
||||||
size_t csi_end = introlen;
|
size_t csi_end = introlen;
|
||||||
|
|
||||||
|
@ -268,7 +268,7 @@ static termkey_result peekkey_csi(termkey_t *tk, termkey_csi *csi, size_t introl
|
||||||
return TERMKEY_RES_KEY;
|
return TERMKEY_RES_KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_result peekkey_ss3(termkey_t *tk, termkey_csi *csi, size_t introlen, termkey_key *key, int force, size_t *nbytep)
|
static TermKeyResult peekkey_ss3(TermKey *tk, TermKeyCsi *csi, size_t introlen, TermKeyKey *key, int force, size_t *nbytep)
|
||||||
{
|
{
|
||||||
if(tk->buffcount < introlen + 1) {
|
if(tk->buffcount < introlen + 1) {
|
||||||
if(!force)
|
if(!force)
|
||||||
|
@ -317,12 +317,12 @@ static termkey_result peekkey_ss3(termkey_t *tk, termkey_csi *csi, size_t introl
|
||||||
return TERMKEY_RES_KEY;
|
return TERMKEY_RES_KEY;
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_result peekkey(termkey_t *tk, void *info, termkey_key *key, int force, size_t *nbytep)
|
static TermKeyResult peekkey(TermKey *tk, void *info, TermKeyKey *key, int force, size_t *nbytep)
|
||||||
{
|
{
|
||||||
if(tk->buffcount == 0)
|
if(tk->buffcount == 0)
|
||||||
return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
|
return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
|
||||||
|
|
||||||
termkey_csi *csi = info;
|
TermKeyCsi *csi = info;
|
||||||
|
|
||||||
// Now we're sure at least 1 byte is valid
|
// Now we're sure at least 1 byte is valid
|
||||||
unsigned char b0 = CHARAT(0);
|
unsigned char b0 = CHARAT(0);
|
||||||
|
@ -343,12 +343,12 @@ static termkey_result peekkey(termkey_t *tk, void *info, termkey_key *key, int f
|
||||||
return TERMKEY_RES_NONE;
|
return TERMKEY_RES_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_keysym register_csi_ss3(termkey_csi *csi, termkey_type type, termkey_keysym sym, unsigned char cmd, const char *name)
|
static TermKeySym register_csi_ss3(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, unsigned char cmd, const char *name)
|
||||||
{
|
{
|
||||||
return register_csi_ss3_full(csi, type, sym, 0, 0, cmd, name);
|
return register_csi_ss3_full(csi, type, sym, 0, 0, cmd, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_keysym register_csi_ss3_full(termkey_csi *csi, termkey_type type, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name)
|
static TermKeySym register_csi_ss3_full(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name)
|
||||||
{
|
{
|
||||||
if(cmd < 0x40 || cmd >= 0x80) {
|
if(cmd < 0x40 || cmd >= 0x80) {
|
||||||
fprintf(stderr, "Cannot register CSI/SS3 key at cmd 0x%02x - out of bounds\n", cmd);
|
fprintf(stderr, "Cannot register CSI/SS3 key at cmd 0x%02x - out of bounds\n", cmd);
|
||||||
|
@ -366,12 +366,12 @@ static termkey_keysym register_csi_ss3_full(termkey_csi *csi, termkey_type type,
|
||||||
return sym;
|
return sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_keysym register_ss3kpalt(termkey_csi *csi, termkey_type type, termkey_keysym sym, unsigned char cmd, const char *name, char kpalt)
|
static TermKeySym register_ss3kpalt(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, unsigned char cmd, const char *name, char kpalt)
|
||||||
{
|
{
|
||||||
return register_ss3kpalt_full(csi, type, sym, 0, 0, cmd, name, kpalt);
|
return register_ss3kpalt_full(csi, type, sym, 0, 0, cmd, name, kpalt);
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_keysym register_ss3kpalt_full(termkey_csi *csi, termkey_type type, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name, char kpalt)
|
static TermKeySym register_ss3kpalt_full(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, int modifier_set, int modifier_mask, unsigned char cmd, const char *name, char kpalt)
|
||||||
{
|
{
|
||||||
if(cmd < 0x40 || cmd >= 0x80) {
|
if(cmd < 0x40 || cmd >= 0x80) {
|
||||||
fprintf(stderr, "Cannot register SS3 key at cmd 0x%02x - out of bounds\n", cmd);
|
fprintf(stderr, "Cannot register SS3 key at cmd 0x%02x - out of bounds\n", cmd);
|
||||||
|
@ -390,12 +390,12 @@ static termkey_keysym register_ss3kpalt_full(termkey_csi *csi, termkey_type type
|
||||||
return sym;
|
return sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_keysym register_csifunc(termkey_csi *csi, termkey_type type, termkey_keysym sym, int number, const char *name)
|
static TermKeySym register_csifunc(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, int number, const char *name)
|
||||||
{
|
{
|
||||||
return register_csifunc_full(csi, type, sym, 0, 0, number, name);
|
return register_csifunc_full(csi, type, sym, 0, 0, number, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_keysym register_csifunc_full(termkey_csi *csi, termkey_type type, termkey_keysym sym, int modifier_set, int modifier_mask, int number, const char *name)
|
static TermKeySym register_csifunc_full(TermKeyCsi *csi, TermKeyType type, TermKeySym sym, int modifier_set, int modifier_mask, int number, const char *name)
|
||||||
{
|
{
|
||||||
if(name)
|
if(name)
|
||||||
sym = termkey_register_keyname(csi->tk, sym, name);
|
sym = termkey_register_keyname(csi->tk, sym, name);
|
||||||
|
@ -420,7 +420,7 @@ static termkey_keysym register_csifunc_full(termkey_csi *csi, termkey_type type,
|
||||||
return sym;
|
return sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct termkey_driver termkey_driver_csi = {
|
struct TermKeyDriver termkey_driver_csi = {
|
||||||
.name = "CSI",
|
.name = "CSI",
|
||||||
|
|
||||||
.new_driver = new_driver,
|
.new_driver = new_driver,
|
||||||
|
|
42
driver-ti.c
42
driver-ti.c
|
@ -39,18 +39,18 @@ struct trie_node_arr {
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
termkey_t *tk;
|
TermKey *tk;
|
||||||
|
|
||||||
struct trie_node *root;
|
struct trie_node *root;
|
||||||
|
|
||||||
char *start_string;
|
char *start_string;
|
||||||
char *stop_string;
|
char *stop_string;
|
||||||
} termkey_ti;
|
} TermKeyTI;
|
||||||
|
|
||||||
static int funcname2keysym(const char *funcname, termkey_type *typep, termkey_keysym *symp, int *modmask, int *modsetp);
|
static int funcname2keysym(const char *funcname, TermKeyType *typep, TermKeySym *symp, int *modmask, int *modsetp);
|
||||||
static int register_seq(termkey_ti *ti, const char *seq, termkey_type type, termkey_keysym sym, int modmask, int modset);
|
static int register_seq(TermKeyTI *ti, const char *seq, TermKeyType type, TermKeySym sym, int modmask, int modset);
|
||||||
|
|
||||||
static struct trie_node *new_node_key(termkey_type type, termkey_keysym sym, int modmask, int modset)
|
static struct trie_node *new_node_key(TermKeyType type, TermKeySym sym, int modmask, int modset)
|
||||||
{
|
{
|
||||||
struct trie_node_key *n = malloc(sizeof(*n));
|
struct trie_node_key *n = malloc(sizeof(*n));
|
||||||
if(!n)
|
if(!n)
|
||||||
|
@ -150,7 +150,7 @@ static struct trie_node *compress_trie(struct trie_node *n)
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *new_driver(termkey_t *tk, const char *term)
|
static void *new_driver(TermKey *tk, const char *term)
|
||||||
{
|
{
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
|
@ -159,7 +159,7 @@ static void *new_driver(termkey_t *tk, const char *term)
|
||||||
if(setupterm((char*)term, 1, &err) != OK)
|
if(setupterm((char*)term, 1, &err) != OK)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
termkey_ti *ti = malloc(sizeof *ti);
|
TermKeyTI *ti = malloc(sizeof *ti);
|
||||||
if(!ti)
|
if(!ti)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -180,8 +180,8 @@ static void *new_driver(termkey_t *tk, const char *term)
|
||||||
if(!value || value == (char*)-1)
|
if(!value || value == (char*)-1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
termkey_type type;
|
TermKeyType type;
|
||||||
termkey_keysym sym;
|
TermKeySym sym;
|
||||||
int mask = 0;
|
int mask = 0;
|
||||||
int set = 0;
|
int set = 0;
|
||||||
|
|
||||||
|
@ -220,9 +220,9 @@ abort_free_ti:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void start_driver(termkey_t *tk, void *info)
|
static void start_driver(TermKey *tk, void *info)
|
||||||
{
|
{
|
||||||
termkey_ti *ti = info;
|
TermKeyTI *ti = info;
|
||||||
|
|
||||||
/* The terminfo database will contain keys in application cursor key mode.
|
/* The terminfo database will contain keys in application cursor key mode.
|
||||||
* We may need to enable that mode
|
* We may need to enable that mode
|
||||||
|
@ -233,9 +233,9 @@ static void start_driver(termkey_t *tk, void *info)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void stop_driver(termkey_t *tk, void *info)
|
static void stop_driver(TermKey *tk, void *info)
|
||||||
{
|
{
|
||||||
termkey_ti *ti = info;
|
TermKeyTI *ti = info;
|
||||||
|
|
||||||
if(ti->stop_string) {
|
if(ti->stop_string) {
|
||||||
// Can't call putp or tputs because they suck and don't give us fd control
|
// Can't call putp or tputs because they suck and don't give us fd control
|
||||||
|
@ -245,7 +245,7 @@ static void stop_driver(termkey_t *tk, void *info)
|
||||||
|
|
||||||
static void free_driver(void *info)
|
static void free_driver(void *info)
|
||||||
{
|
{
|
||||||
termkey_ti *ti = info;
|
TermKeyTI *ti = info;
|
||||||
|
|
||||||
free_trie(ti->root);
|
free_trie(ti->root);
|
||||||
|
|
||||||
|
@ -260,9 +260,9 @@ static void free_driver(void *info)
|
||||||
|
|
||||||
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
||||||
|
|
||||||
static termkey_result peekkey(termkey_t *tk, void *info, termkey_key *key, int force, size_t *nbytep)
|
static TermKeyResult peekkey(TermKey *tk, void *info, TermKeyKey *key, int force, size_t *nbytep)
|
||||||
{
|
{
|
||||||
termkey_ti *ti = info;
|
TermKeyTI *ti = info;
|
||||||
|
|
||||||
if(tk->buffcount == 0)
|
if(tk->buffcount == 0)
|
||||||
return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
|
return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
|
||||||
|
@ -297,8 +297,8 @@ static termkey_result peekkey(termkey_t *tk, void *info, termkey_key *key, int f
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
const char *funcname;
|
const char *funcname;
|
||||||
termkey_type type;
|
TermKeyType type;
|
||||||
termkey_keysym sym;
|
TermKeySym sym;
|
||||||
int mods;
|
int mods;
|
||||||
} funcs[] =
|
} funcs[] =
|
||||||
{
|
{
|
||||||
|
@ -348,7 +348,7 @@ static struct {
|
||||||
{ NULL },
|
{ NULL },
|
||||||
};
|
};
|
||||||
|
|
||||||
static int funcname2keysym(const char *funcname, termkey_type *typep, termkey_keysym *symp, int *modmaskp, int *modsetp)
|
static int funcname2keysym(const char *funcname, TermKeyType *typep, TermKeySym *symp, int *modmaskp, int *modsetp)
|
||||||
{
|
{
|
||||||
// Binary search
|
// Binary search
|
||||||
|
|
||||||
|
@ -395,7 +395,7 @@ static int funcname2keysym(const char *funcname, termkey_type *typep, termkey_ke
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int register_seq(termkey_ti *ti, const char *seq, termkey_type type, termkey_keysym sym, int modmask, int modset)
|
static int register_seq(TermKeyTI *ti, const char *seq, TermKeyType type, TermKeySym sym, int modmask, int modset)
|
||||||
{
|
{
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
struct trie_node *p = ti->root;
|
struct trie_node *p = ti->root;
|
||||||
|
@ -447,7 +447,7 @@ static int register_seq(termkey_ti *ti, const char *seq, termkey_type type, term
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct termkey_driver termkey_driver_ti = {
|
struct TermKeyDriver termkey_driver_ti = {
|
||||||
.name = "terminfo",
|
.name = "terminfo",
|
||||||
|
|
||||||
.new_driver = new_driver,
|
.new_driver = new_driver,
|
||||||
|
|
|
@ -6,31 +6,31 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
|
||||||
struct termkey_driver
|
struct TermKeyDriver
|
||||||
{
|
{
|
||||||
const char *name;
|
const char *name;
|
||||||
void *(*new_driver)(termkey_t *tk, const char *term);
|
void *(*new_driver)(TermKey *tk, const char *term);
|
||||||
void (*free_driver)(void *info);
|
void (*free_driver)(void *info);
|
||||||
void (*start_driver)(termkey_t *tk, void *info);
|
void (*start_driver)(TermKey *tk, void *info);
|
||||||
void (*stop_driver)(termkey_t *tk, void *info);
|
void (*stop_driver)(TermKey *tk, void *info);
|
||||||
termkey_result (*peekkey)(termkey_t *tk, void *info, termkey_key *key, int force, size_t *nbytes);
|
TermKeyResult (*peekkey)(TermKey *tk, void *info, TermKeyKey *key, int force, size_t *nbytes);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct keyinfo {
|
struct keyinfo {
|
||||||
termkey_type type;
|
TermKeyType type;
|
||||||
termkey_keysym sym;
|
TermKeySym sym;
|
||||||
int modifier_mask;
|
int modifier_mask;
|
||||||
int modifier_set;
|
int modifier_set;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct termkey_drivernode;
|
struct TermKeyDriverNode;
|
||||||
struct termkey_drivernode {
|
struct TermKeyDriverNode {
|
||||||
struct termkey_driver *driver;
|
struct TermKeyDriver *driver;
|
||||||
void *info;
|
void *info;
|
||||||
struct termkey_drivernode *next;
|
struct TermKeyDriverNode *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct termkey {
|
struct _TermKey {
|
||||||
int fd;
|
int fd;
|
||||||
int flags;
|
int flags;
|
||||||
unsigned char *buffer;
|
unsigned char *buffer;
|
||||||
|
@ -51,17 +51,17 @@ struct termkey {
|
||||||
// There are 32 C0 codes
|
// There are 32 C0 codes
|
||||||
struct keyinfo c0[32];
|
struct keyinfo c0[32];
|
||||||
|
|
||||||
struct termkey_drivernode *drivers;
|
struct TermKeyDriverNode *drivers;
|
||||||
|
|
||||||
// Now some "protected" methods for the driver to call but which we don't
|
// Now some "protected" methods for the driver to call but which we don't
|
||||||
// want exported as real symbols in the library
|
// want exported as real symbols in the library
|
||||||
struct {
|
struct {
|
||||||
void (*emit_codepoint)(termkey_t *tk, long codepoint, termkey_key *key);
|
void (*emit_codepoint)(TermKey *tk, long codepoint, TermKeyKey *key);
|
||||||
termkey_result (*peekkey_simple)(termkey_t *tk, termkey_key *key, int force, size_t *nbytes);
|
TermKeyResult (*peekkey_simple)(TermKey *tk, TermKeyKey *key, int force, size_t *nbytes);
|
||||||
} method;
|
} method;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct termkey_driver termkey_driver_csi;
|
extern struct TermKeyDriver termkey_driver_csi;
|
||||||
extern struct termkey_driver termkey_driver_ti;
|
extern struct TermKeyDriver termkey_driver_ti;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
100
termkey.c
100
termkey.c
|
@ -25,22 +25,22 @@ void termkey_check_version(int major, int minor)
|
||||||
// Happy
|
// Happy
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct termkey_driver *drivers[] = {
|
static struct TermKeyDriver *drivers[] = {
|
||||||
&termkey_driver_ti,
|
&termkey_driver_ti,
|
||||||
&termkey_driver_csi,
|
&termkey_driver_csi,
|
||||||
NULL,
|
NULL,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Forwards for the "protected" methods
|
// Forwards for the "protected" methods
|
||||||
// static void eat_bytes(termkey_t *tk, size_t count);
|
// static void eat_bytes(TermKey *tk, size_t count);
|
||||||
static void emit_codepoint(termkey_t *tk, long codepoint, termkey_key *key);
|
static void emit_codepoint(TermKey *tk, long codepoint, TermKeyKey *key);
|
||||||
static termkey_result peekkey_simple(termkey_t *tk, termkey_key *key, int force, size_t *nbytes);
|
static TermKeyResult peekkey_simple(TermKey *tk, TermKeyKey *key, int force, size_t *nbytes);
|
||||||
|
|
||||||
static termkey_keysym register_c0(termkey_t *tk, termkey_keysym sym, unsigned char ctrl, const char *name);
|
static TermKeySym register_c0(TermKey *tk, TermKeySym sym, unsigned char ctrl, const char *name);
|
||||||
static termkey_keysym register_c0_full(termkey_t *tk, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char ctrl, const char *name);
|
static TermKeySym register_c0_full(TermKey *tk, TermKeySym sym, int modifier_set, int modifier_mask, unsigned char ctrl, const char *name);
|
||||||
|
|
||||||
static struct {
|
static struct {
|
||||||
termkey_keysym sym;
|
TermKeySym sym;
|
||||||
const char *name;
|
const char *name;
|
||||||
} keynames[] = {
|
} keynames[] = {
|
||||||
{ TERMKEY_SYM_NONE, "NONE" },
|
{ TERMKEY_SYM_NONE, "NONE" },
|
||||||
|
@ -109,9 +109,9 @@ static struct {
|
||||||
/* We might expose this as public API one day, when the ideas are finalised.
|
/* We might expose this as public API one day, when the ideas are finalised.
|
||||||
* As yet it isn't public, so keep it static
|
* As yet it isn't public, so keep it static
|
||||||
*/
|
*/
|
||||||
static termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
|
static TermKey *termkey_new_full(int fd, int flags, size_t buffsize, int waittime)
|
||||||
{
|
{
|
||||||
termkey_t *tk = malloc(sizeof(*tk));
|
TermKey *tk = malloc(sizeof(*tk));
|
||||||
if(!tk)
|
if(!tk)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -176,7 +176,7 @@ static termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waitt
|
||||||
|
|
||||||
const char *term = getenv("TERM");
|
const char *term = getenv("TERM");
|
||||||
|
|
||||||
struct termkey_drivernode *tail = NULL;
|
struct TermKeyDriverNode *tail = NULL;
|
||||||
|
|
||||||
for(i = 0; drivers[i]; i++) {
|
for(i = 0; drivers[i]; i++) {
|
||||||
void *info = (*drivers[i]->new_driver)(tk, term);
|
void *info = (*drivers[i]->new_driver)(tk, term);
|
||||||
|
@ -187,7 +187,7 @@ static termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waitt
|
||||||
fprintf(stderr, "Loading the %s driver\n", drivers[i]->name);
|
fprintf(stderr, "Loading the %s driver\n", drivers[i]->name);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct termkey_drivernode *thisdrv = malloc(sizeof(*thisdrv));
|
struct TermKeyDriverNode *thisdrv = malloc(sizeof(*thisdrv));
|
||||||
if(!thisdrv)
|
if(!thisdrv)
|
||||||
goto abort_free_drivers;
|
goto abort_free_drivers;
|
||||||
|
|
||||||
|
@ -221,7 +221,7 @@ static termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waitt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct termkey_drivernode *p;
|
struct TermKeyDriverNode *p;
|
||||||
for(p = tk->drivers; p; p = p->next)
|
for(p = tk->drivers; p; p = p->next)
|
||||||
if(p->driver->start_driver)
|
if(p->driver->start_driver)
|
||||||
(*p->driver->start_driver)(tk, p->info);
|
(*p->driver->start_driver)(tk, p->info);
|
||||||
|
@ -231,7 +231,7 @@ static termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waitt
|
||||||
abort_free_drivers:
|
abort_free_drivers:
|
||||||
for(p = tk->drivers; p; ) {
|
for(p = tk->drivers; p; ) {
|
||||||
(*p->driver->free_driver)(p->info);
|
(*p->driver->free_driver)(p->info);
|
||||||
struct termkey_drivernode *next = p->next;
|
struct TermKeyDriverNode *next = p->next;
|
||||||
free(p);
|
free(p);
|
||||||
p = next;
|
p = next;
|
||||||
}
|
}
|
||||||
|
@ -248,20 +248,20 @@ abort_free_tk:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
termkey_t *termkey_new(int fd, int flags)
|
TermKey *termkey_new(int fd, int flags)
|
||||||
{
|
{
|
||||||
return termkey_new_full(fd, flags, 256, 50);
|
return termkey_new_full(fd, flags, 256, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
void termkey_free(termkey_t *tk)
|
void termkey_free(TermKey *tk)
|
||||||
{
|
{
|
||||||
free(tk->buffer); tk->buffer = NULL;
|
free(tk->buffer); tk->buffer = NULL;
|
||||||
free(tk->keynames); tk->keynames = NULL;
|
free(tk->keynames); tk->keynames = NULL;
|
||||||
|
|
||||||
struct termkey_drivernode *p;
|
struct TermKeyDriverNode *p;
|
||||||
for(p = tk->drivers; p; ) {
|
for(p = tk->drivers; p; ) {
|
||||||
(*p->driver->free_driver)(p->info);
|
(*p->driver->free_driver)(p->info);
|
||||||
struct termkey_drivernode *next = p->next;
|
struct TermKeyDriverNode *next = p->next;
|
||||||
free(p);
|
free(p);
|
||||||
p = next;
|
p = next;
|
||||||
}
|
}
|
||||||
|
@ -269,9 +269,9 @@ void termkey_free(termkey_t *tk)
|
||||||
free(tk);
|
free(tk);
|
||||||
}
|
}
|
||||||
|
|
||||||
void termkey_destroy(termkey_t *tk)
|
void termkey_destroy(TermKey *tk)
|
||||||
{
|
{
|
||||||
struct termkey_drivernode *p;
|
struct TermKeyDriverNode *p;
|
||||||
for(p = tk->drivers; p; p = p->next)
|
for(p = tk->drivers; p; p = p->next)
|
||||||
if(p->driver->stop_driver)
|
if(p->driver->stop_driver)
|
||||||
(*p->driver->stop_driver)(tk, p->info);
|
(*p->driver->stop_driver)(tk, p->info);
|
||||||
|
@ -282,32 +282,32 @@ void termkey_destroy(termkey_t *tk)
|
||||||
termkey_free(tk);
|
termkey_free(tk);
|
||||||
}
|
}
|
||||||
|
|
||||||
int termkey_get_fd(termkey_t *tk)
|
int termkey_get_fd(TermKey *tk)
|
||||||
{
|
{
|
||||||
return tk->fd;
|
return tk->fd;
|
||||||
}
|
}
|
||||||
|
|
||||||
int termkey_get_flags(termkey_t *tk)
|
int termkey_get_flags(TermKey *tk)
|
||||||
{
|
{
|
||||||
return tk->flags;
|
return tk->flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
void termkey_set_flags(termkey_t *tk, int newflags)
|
void termkey_set_flags(TermKey *tk, int newflags)
|
||||||
{
|
{
|
||||||
tk->flags = newflags;
|
tk->flags = newflags;
|
||||||
}
|
}
|
||||||
|
|
||||||
void termkey_set_waittime(termkey_t *tk, int msec)
|
void termkey_set_waittime(TermKey *tk, int msec)
|
||||||
{
|
{
|
||||||
tk->waittime = msec;
|
tk->waittime = msec;
|
||||||
}
|
}
|
||||||
|
|
||||||
int termkey_get_waittime(termkey_t *tk)
|
int termkey_get_waittime(TermKey *tk)
|
||||||
{
|
{
|
||||||
return tk->waittime;
|
return tk->waittime;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void eat_bytes(termkey_t *tk, size_t count)
|
static void eat_bytes(TermKey *tk, size_t count)
|
||||||
{
|
{
|
||||||
if(count >= tk->buffcount) {
|
if(count >= tk->buffcount) {
|
||||||
tk->buffstart = 0;
|
tk->buffstart = 0;
|
||||||
|
@ -329,7 +329,7 @@ static inline unsigned int utf8_seqlen(long codepoint)
|
||||||
return 6;
|
return 6;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void fill_utf8(termkey_key *key)
|
static void fill_utf8(TermKeyKey *key)
|
||||||
{
|
{
|
||||||
long codepoint = key->code.codepoint;
|
long codepoint = key->code.codepoint;
|
||||||
int nbytes = utf8_seqlen(codepoint);
|
int nbytes = utf8_seqlen(codepoint);
|
||||||
|
@ -354,7 +354,7 @@ static void fill_utf8(termkey_key *key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void emit_codepoint(termkey_t *tk, long codepoint, termkey_key *key)
|
static void emit_codepoint(TermKey *tk, long codepoint, TermKeyKey *key)
|
||||||
{
|
{
|
||||||
if(codepoint < 0x20) {
|
if(codepoint < 0x20) {
|
||||||
// C0 range
|
// C0 range
|
||||||
|
@ -421,7 +421,7 @@ static void emit_codepoint(termkey_t *tk, long codepoint, termkey_key *key)
|
||||||
|
|
||||||
#define UTF8_INVALID 0xFFFD
|
#define UTF8_INVALID 0xFFFD
|
||||||
|
|
||||||
static termkey_result peekkey(termkey_t *tk, termkey_key *key, int force, size_t *nbytep)
|
static TermKeyResult peekkey(TermKey *tk, TermKeyKey *key, int force, size_t *nbytep)
|
||||||
{
|
{
|
||||||
int again = 0;
|
int again = 0;
|
||||||
|
|
||||||
|
@ -431,8 +431,8 @@ static termkey_result peekkey(termkey_t *tk, termkey_key *key, int force, size_t
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
termkey_result ret;
|
TermKeyResult ret;
|
||||||
struct termkey_drivernode *p;
|
struct TermKeyDriverNode *p;
|
||||||
for(p = tk->drivers; p; p = p->next) {
|
for(p = tk->drivers; p; p = p->next) {
|
||||||
ret = (p->driver->peekkey)(tk, p->info, key, force, nbytep);
|
ret = (p->driver->peekkey)(tk, p->info, key, force, nbytep);
|
||||||
|
|
||||||
|
@ -486,7 +486,7 @@ static termkey_result peekkey(termkey_t *tk, termkey_key *key, int force, size_t
|
||||||
|
|
||||||
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
|
||||||
|
|
||||||
static termkey_result peekkey_simple(termkey_t *tk, termkey_key *key, int force, size_t *nbytep)
|
static TermKeyResult peekkey_simple(TermKey *tk, TermKeyKey *key, int force, size_t *nbytep)
|
||||||
{
|
{
|
||||||
if(tk->buffcount == 0)
|
if(tk->buffcount == 0)
|
||||||
return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
|
return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
|
||||||
|
@ -511,7 +511,7 @@ static termkey_result peekkey_simple(termkey_t *tk, termkey_key *key, int force,
|
||||||
tk->buffcount--;
|
tk->buffcount--;
|
||||||
|
|
||||||
// Run the full driver
|
// Run the full driver
|
||||||
termkey_result metakey_result = peekkey(tk, key, force, nbytep);
|
TermKeyResult metakey_result = peekkey(tk, key, force, nbytep);
|
||||||
|
|
||||||
tk->buffstart--;
|
tk->buffstart--;
|
||||||
tk->buffcount++;
|
tk->buffcount++;
|
||||||
|
@ -632,7 +632,7 @@ static termkey_result peekkey_simple(termkey_t *tk, termkey_key *key, int force,
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
static void print_buffer(termkey_t *tk)
|
static void print_buffer(TermKey *tk)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < tk->buffcount && i < 20; i++)
|
for(i = 0; i < tk->buffcount && i < 20; i++)
|
||||||
|
@ -641,7 +641,7 @@ static void print_buffer(termkey_t *tk)
|
||||||
fprintf(stderr, "...");
|
fprintf(stderr, "...");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_key(termkey_t *tk, termkey_key *key)
|
static void print_key(TermKey *tk, TermKeyKey *key)
|
||||||
{
|
{
|
||||||
switch(key->type) {
|
switch(key->type) {
|
||||||
case TERMKEY_TYPE_UNICODE:
|
case TERMKEY_TYPE_UNICODE:
|
||||||
|
@ -663,7 +663,7 @@ static void print_key(termkey_t *tk, termkey_key *key)
|
||||||
m & ~(TERMKEY_KEYMOD_CTRL|TERMKEY_KEYMOD_ALT|TERMKEY_KEYMOD_SHIFT));
|
m & ~(TERMKEY_KEYMOD_CTRL|TERMKEY_KEYMOD_ALT|TERMKEY_KEYMOD_SHIFT));
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *res2str(termkey_result res)
|
static const char *res2str(TermKeyResult res)
|
||||||
{
|
{
|
||||||
switch(res) {
|
switch(res) {
|
||||||
case TERMKEY_RES_KEY:
|
case TERMKEY_RES_KEY:
|
||||||
|
@ -680,10 +680,10 @@ static const char *res2str(termkey_result res)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
termkey_result termkey_getkey(termkey_t *tk, termkey_key *key)
|
TermKeyResult termkey_getkey(TermKey *tk, TermKeyKey *key)
|
||||||
{
|
{
|
||||||
size_t nbytes = 0;
|
size_t nbytes = 0;
|
||||||
termkey_result ret = peekkey(tk, key, 0, &nbytes);
|
TermKeyResult ret = peekkey(tk, key, 0, &nbytes);
|
||||||
|
|
||||||
if(ret == TERMKEY_RES_KEY)
|
if(ret == TERMKEY_RES_KEY)
|
||||||
eat_bytes(tk, nbytes);
|
eat_bytes(tk, nbytes);
|
||||||
|
@ -696,10 +696,10 @@ termkey_result termkey_getkey(termkey_t *tk, termkey_key *key)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
termkey_result termkey_getkey_force(termkey_t *tk, termkey_key *key)
|
TermKeyResult termkey_getkey_force(TermKey *tk, TermKeyKey *key)
|
||||||
{
|
{
|
||||||
size_t nbytes = 0;
|
size_t nbytes = 0;
|
||||||
termkey_result ret = peekkey(tk, key, 1, &nbytes);
|
TermKeyResult ret = peekkey(tk, key, 1, &nbytes);
|
||||||
|
|
||||||
if(ret == TERMKEY_RES_KEY)
|
if(ret == TERMKEY_RES_KEY)
|
||||||
eat_bytes(tk, nbytes);
|
eat_bytes(tk, nbytes);
|
||||||
|
@ -707,10 +707,10 @@ termkey_result termkey_getkey_force(termkey_t *tk, termkey_key *key)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key)
|
TermKeyResult termkey_waitkey(TermKey *tk, TermKeyKey *key)
|
||||||
{
|
{
|
||||||
while(1) {
|
while(1) {
|
||||||
termkey_result ret = termkey_getkey(tk, key);
|
TermKeyResult ret = termkey_getkey(tk, key);
|
||||||
|
|
||||||
switch(ret) {
|
switch(ret) {
|
||||||
case TERMKEY_RES_KEY:
|
case TERMKEY_RES_KEY:
|
||||||
|
@ -750,7 +750,7 @@ termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key)
|
||||||
/* UNREACHABLE */
|
/* UNREACHABLE */
|
||||||
}
|
}
|
||||||
|
|
||||||
void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen)
|
void termkey_pushinput(TermKey *tk, unsigned char *input, size_t inputlen)
|
||||||
{
|
{
|
||||||
if(tk->buffstart + tk->buffcount + inputlen > tk->buffsize) {
|
if(tk->buffstart + tk->buffcount + inputlen > tk->buffsize) {
|
||||||
while(tk->buffstart + tk->buffcount + inputlen > tk->buffsize)
|
while(tk->buffstart + tk->buffcount + inputlen > tk->buffsize)
|
||||||
|
@ -766,7 +766,7 @@ void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen)
|
||||||
tk->buffcount += inputlen;
|
tk->buffcount += inputlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
termkey_result termkey_advisereadable(termkey_t *tk)
|
TermKeyResult termkey_advisereadable(TermKey *tk)
|
||||||
{
|
{
|
||||||
unsigned char buffer[64]; // Smaller than the default size
|
unsigned char buffer[64]; // Smaller than the default size
|
||||||
ssize_t len = read(tk->fd, buffer, sizeof buffer);
|
ssize_t len = read(tk->fd, buffer, sizeof buffer);
|
||||||
|
@ -783,7 +783,7 @@ termkey_result termkey_advisereadable(termkey_t *tk)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
termkey_keysym termkey_register_keyname(termkey_t *tk, termkey_keysym sym, const char *name)
|
TermKeySym termkey_register_keyname(TermKey *tk, TermKeySym sym, const char *name)
|
||||||
{
|
{
|
||||||
if(!sym)
|
if(!sym)
|
||||||
sym = tk->nkeynames;
|
sym = tk->nkeynames;
|
||||||
|
@ -805,7 +805,7 @@ termkey_keysym termkey_register_keyname(termkey_t *tk, termkey_keysym sym, const
|
||||||
return sym;
|
return sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *termkey_get_keyname(termkey_t *tk, termkey_keysym sym)
|
const char *termkey_get_keyname(TermKey *tk, TermKeySym sym)
|
||||||
{
|
{
|
||||||
if(sym == TERMKEY_SYM_UNKNOWN)
|
if(sym == TERMKEY_SYM_UNKNOWN)
|
||||||
return "UNKNOWN";
|
return "UNKNOWN";
|
||||||
|
@ -816,12 +816,12 @@ const char *termkey_get_keyname(termkey_t *tk, termkey_keysym sym)
|
||||||
return "UNKNOWN";
|
return "UNKNOWN";
|
||||||
}
|
}
|
||||||
|
|
||||||
termkey_keysym termkey_keyname2sym(termkey_t *tk, const char *keyname)
|
TermKeySym termkey_keyname2sym(TermKey *tk, const char *keyname)
|
||||||
{
|
{
|
||||||
/* We store an array, so we can't do better than a linear search. Doesn't
|
/* 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 */
|
* matter because user won't be calling this too often */
|
||||||
|
|
||||||
termkey_keysym sym;
|
TermKeySym sym;
|
||||||
|
|
||||||
for(sym = 0; sym < tk->nkeynames; sym++)
|
for(sym = 0; sym < tk->nkeynames; sym++)
|
||||||
if(tk->keynames[sym] && strcmp(keyname, tk->keynames[sym]) == 0)
|
if(tk->keynames[sym] && strcmp(keyname, tk->keynames[sym]) == 0)
|
||||||
|
@ -830,12 +830,12 @@ termkey_keysym termkey_keyname2sym(termkey_t *tk, const char *keyname)
|
||||||
return TERMKEY_SYM_UNKNOWN;
|
return TERMKEY_SYM_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_keysym register_c0(termkey_t *tk, termkey_keysym sym, unsigned char ctrl, const char *name)
|
static TermKeySym register_c0(TermKey *tk, TermKeySym sym, unsigned char ctrl, const char *name)
|
||||||
{
|
{
|
||||||
return register_c0_full(tk, sym, 0, 0, ctrl, name);
|
return register_c0_full(tk, sym, 0, 0, ctrl, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
static termkey_keysym register_c0_full(termkey_t *tk, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char ctrl, const char *name)
|
static TermKeySym register_c0_full(TermKey *tk, TermKeySym sym, int modifier_set, int modifier_mask, unsigned char ctrl, const char *name)
|
||||||
{
|
{
|
||||||
if(ctrl >= 0x20) {
|
if(ctrl >= 0x20) {
|
||||||
fprintf(stderr, "Cannot register C0 key at ctrl 0x%02x - out of bounds\n", ctrl);
|
fprintf(stderr, "Cannot register C0 key at ctrl 0x%02x - out of bounds\n", ctrl);
|
||||||
|
@ -852,7 +852,7 @@ static termkey_keysym register_c0_full(termkey_t *tk, termkey_keysym sym, int mo
|
||||||
return sym;
|
return sym;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t termkey_snprint_key(termkey_t *tk, char *buffer, size_t len, termkey_key *key, termkey_format format)
|
size_t termkey_snprint_key(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, TermKeyFormat format)
|
||||||
{
|
{
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
size_t l = 0;
|
size_t l = 0;
|
||||||
|
|
61
termkey.h.in
61
termkey.h.in
|
@ -84,20 +84,20 @@ typedef enum {
|
||||||
TERMKEY_SYM_KPEQUALS,
|
TERMKEY_SYM_KPEQUALS,
|
||||||
|
|
||||||
// et cetera ad nauseum
|
// et cetera ad nauseum
|
||||||
} termkey_sym;
|
} TermKeySym;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TERMKEY_TYPE_UNICODE,
|
TERMKEY_TYPE_UNICODE,
|
||||||
TERMKEY_TYPE_FUNCTION,
|
TERMKEY_TYPE_FUNCTION,
|
||||||
TERMKEY_TYPE_KEYSYM
|
TERMKEY_TYPE_KEYSYM
|
||||||
} termkey_type;
|
} TermKeyType;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TERMKEY_RES_NONE,
|
TERMKEY_RES_NONE,
|
||||||
TERMKEY_RES_KEY,
|
TERMKEY_RES_KEY,
|
||||||
TERMKEY_RES_EOF,
|
TERMKEY_RES_EOF,
|
||||||
TERMKEY_RES_AGAIN,
|
TERMKEY_RES_AGAIN,
|
||||||
} termkey_result;
|
} TermKeyResult;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
TERMKEY_KEYMOD_SHIFT = 1 << 0,
|
TERMKEY_KEYMOD_SHIFT = 1 << 0,
|
||||||
|
@ -105,23 +105,21 @@ enum {
|
||||||
TERMKEY_KEYMOD_CTRL = 1 << 2,
|
TERMKEY_KEYMOD_CTRL = 1 << 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef int termkey_keysym;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
termkey_type type;
|
TermKeyType type;
|
||||||
union {
|
union {
|
||||||
long codepoint; // TERMKEY_TYPE_UNICODE
|
long codepoint; // TERMKEY_TYPE_UNICODE
|
||||||
int number; // TERMKEY_TYPE_FUNCTION
|
int number; // TERMKEY_TYPE_FUNCTION
|
||||||
termkey_keysym sym; // TERMKEY_TYPE_KEYSYM
|
TermKeySym sym; // TERMKEY_TYPE_KEYSYM
|
||||||
} code;
|
} code;
|
||||||
int modifiers;
|
int modifiers;
|
||||||
|
|
||||||
/* Any Unicode character can be UTF-8 encoded in no more than 6 bytes, plus
|
/* Any Unicode character can be UTF-8 encoded in no more than 6 bytes, plus
|
||||||
* terminating NUL */
|
* terminating NUL */
|
||||||
char utf8[7];
|
char utf8[7];
|
||||||
} termkey_key;
|
} TermKeyKey;
|
||||||
|
|
||||||
typedef struct termkey termkey_t;
|
typedef struct _TermKey TermKey;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
TERMKEY_FLAG_NOINTERPRET = 1 << 0, // Do not interpret C0//G1 codes if possible
|
TERMKEY_FLAG_NOINTERPRET = 1 << 0, // Do not interpret C0//G1 codes if possible
|
||||||
|
@ -133,42 +131,51 @@ enum {
|
||||||
|
|
||||||
void termkey_check_version(int major, int minor);
|
void termkey_check_version(int major, int minor);
|
||||||
|
|
||||||
termkey_t *termkey_new(int fd, int flags);
|
TermKey *termkey_new(int fd, int flags);
|
||||||
void termkey_free(termkey_t *tk);
|
void termkey_free(TermKey *tk);
|
||||||
void termkey_destroy(termkey_t *tk);
|
void termkey_destroy(TermKey *tk);
|
||||||
|
|
||||||
int termkey_get_fd(termkey_t *tk);
|
int termkey_get_fd(TermKey *tk);
|
||||||
|
|
||||||
int termkey_get_flags(termkey_t *tk);
|
int termkey_get_flags(TermKey *tk);
|
||||||
void termkey_set_flags(termkey_t *tk, int newflags);
|
void termkey_set_flags(TermKey *tk, int newflags);
|
||||||
|
|
||||||
void termkey_set_waittime(termkey_t *tk, int msec);
|
void termkey_set_waittime(TermKey *tk, int msec);
|
||||||
int termkey_get_waittime(termkey_t *tk);
|
int termkey_get_waittime(TermKey *tk);
|
||||||
|
|
||||||
termkey_result termkey_getkey(termkey_t *tk, termkey_key *key);
|
TermKeyResult termkey_getkey(TermKey *tk, TermKeyKey *key);
|
||||||
termkey_result termkey_getkey_force(termkey_t *tk, termkey_key *key);
|
TermKeyResult termkey_getkey_force(TermKey *tk, TermKeyKey *key);
|
||||||
termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key);
|
TermKeyResult termkey_waitkey(TermKey *tk, TermKeyKey *key);
|
||||||
|
|
||||||
void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen);
|
void termkey_pushinput(TermKey *tk, unsigned char *input, size_t inputlen);
|
||||||
|
|
||||||
termkey_result termkey_advisereadable(termkey_t *tk);
|
TermKeyResult termkey_advisereadable(TermKey *tk);
|
||||||
|
|
||||||
termkey_keysym termkey_register_keyname(termkey_t *tk, termkey_keysym sym, const char *name);
|
TermKeySym termkey_register_keyname(TermKey *tk, TermKeySym sym, const char *name);
|
||||||
const char *termkey_get_keyname(termkey_t *tk, termkey_keysym sym);
|
const char *termkey_get_keyname(TermKey *tk, TermKeySym sym);
|
||||||
|
|
||||||
termkey_keysym termkey_keyname2sym(termkey_t *tk, const char *keyname);
|
TermKeySym termkey_keyname2sym(TermKey *tk, const char *keyname);
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
TERMKEY_FORMAT_LONGMOD = 1 << 0, // Shift-... instead of S-...
|
TERMKEY_FORMAT_LONGMOD = 1 << 0, // Shift-... instead of S-...
|
||||||
TERMKEY_FORMAT_CARETCTRL = 1 << 1, // ^X instead of C-X
|
TERMKEY_FORMAT_CARETCTRL = 1 << 1, // ^X instead of C-X
|
||||||
TERMKEY_FORMAT_ALTISMETA = 1 << 2, // Meta- or M- instead of Alt- or A-
|
TERMKEY_FORMAT_ALTISMETA = 1 << 2, // Meta- or M- instead of Alt- or A-
|
||||||
TERMKEY_FORMAT_WRAPBRACKET = 1 << 3, // Wrap special keys in brackets like <Escape>
|
TERMKEY_FORMAT_WRAPBRACKET = 1 << 3, // Wrap special keys in brackets like <Escape>
|
||||||
} termkey_format;
|
} TermKeyFormat;
|
||||||
|
|
||||||
// Some useful combinations
|
// Some useful combinations
|
||||||
|
|
||||||
#define TERMKEY_FORMAT_VIM (TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_WRAPBRACKET)
|
#define TERMKEY_FORMAT_VIM (TERMKEY_FORMAT_ALTISMETA|TERMKEY_FORMAT_WRAPBRACKET)
|
||||||
|
|
||||||
size_t termkey_snprint_key(termkey_t *tk, char *buffer, size_t len, termkey_key *key, termkey_format format);
|
size_t termkey_snprint_key(TermKey *tk, char *buffer, size_t len, TermKeyKey *key, TermKeyFormat format);
|
||||||
|
|
||||||
|
// Legacy name typedefs
|
||||||
|
|
||||||
|
typedef TermKeySym termkey_keysym;
|
||||||
|
typedef TermKeyType termkey_type;
|
||||||
|
typedef TermKeyResult termkey_result;
|
||||||
|
typedef TermKeyKey termkey_key;
|
||||||
|
|
||||||
|
typedef TermKey termkey_t;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -5,7 +5,7 @@ termkey_advisereadable \- read more bytes from the underlying terminal
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "termkey_result termkey_advisereadable(termkey_t *" tk );
|
.BI "TermKeyResult termkey_advisereadable(TermKey *" tk );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
|
|
|
@ -5,7 +5,7 @@ termkey_get_fd \- obtain the file descriptor for the terminal
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "int termkey_get_fd(termkey_t *" tk );
|
.BI "int termkey_get_fd(TermKey *" tk );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
|
|
|
@ -5,7 +5,7 @@ termkey_get_keyname \- return a string name for a symbolic key
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "const char *termkey_get_keyname(termkey_t *" tk ", termkey_keysym " sym );
|
.BI "const char *termkey_get_keyname(TermKey *" tk ", TermKeySym " sym );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
|
|
|
@ -7,9 +7,9 @@ termkey_getkey, termkey_getkey_force \- retrieve the next key event
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "termkey_result termkey_getkey(termkey_t *" tk ", termkey_key *" key );
|
.BI "TermKeyResult termkey_getkey(TermKey *" tk ", TermKeyKey *" key );
|
||||||
.br
|
.br
|
||||||
.BI "termkey_result termkey_getkey_force(termkey_t *" tk ", termkey_key *" key );
|
.BI "TermKeyResult termkey_getkey_force(TermKey *" tk ", TermKeyKey *" key );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
|
@ -33,20 +33,20 @@ no bytes are waiting in the buffer.
|
||||||
.PP
|
.PP
|
||||||
Neither of these functions will block or perform any IO operations on the underlying filehandle. To use the instance in an asynchronous program, see \fBtermkey_advisereadable\fP(3). For a blocking call suitable for use in a synchronous program, use \fBtermkey_waitkey\fP(3) instead of \fBtermkey_getkey\fP().
|
Neither of these functions will block or perform any IO operations on the underlying filehandle. To use the instance in an asynchronous program, see \fBtermkey_advisereadable\fP(3). For a blocking call suitable for use in a synchronous program, use \fBtermkey_waitkey\fP(3) instead of \fBtermkey_getkey\fP().
|
||||||
.PP
|
.PP
|
||||||
The \fItermkey_key\fP structure is defined as follows:
|
The \fITermKeyKey\fP structure is defined as follows:
|
||||||
.PP
|
.PP
|
||||||
.in +4n
|
.in +4n
|
||||||
.nf
|
.nf
|
||||||
typedef struct {
|
typedef struct {
|
||||||
termkey_type type;
|
TermKeyType type;
|
||||||
union {
|
union {
|
||||||
long codepoint; /* TERMKEY_TYPE_UNICODE */
|
long codepoint; /* TERMKEY_TYPE_UNICODE */
|
||||||
int number; /* TERMKEY_TYPE_FUNCTION */
|
int number; /* TERMKEY_TYPE_FUNCTION */
|
||||||
termkey_keysym sym; /* TERMKEY_TYPE_KEYSYM */
|
TermKeySym sym; /* TERMKEY_TYPE_KEYSYM */
|
||||||
} code;
|
} code;
|
||||||
int modifiers;
|
int modifiers;
|
||||||
char utf8[7];
|
char utf8[7];
|
||||||
} termkey_key;
|
} TermKeyKey;
|
||||||
.fi
|
.fi
|
||||||
.in
|
.in
|
||||||
.PP
|
.PP
|
||||||
|
|
|
@ -5,7 +5,7 @@ termkey_keyname2sym \- look up a symbolic key value for a string name
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "termkey_keysym termkey_keyname2sym(termkey_t *" tk ", const char *" keyname );
|
.BI "TermKeySym termkey_keyname2sym(TermKey *" tk ", const char *" keyname );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
|
|
|
@ -7,14 +7,14 @@ termkey_new, termkey_destroy \- create or destroy new termkey instance
|
||||||
.sp
|
.sp
|
||||||
.BI "TERMKEY_CHECK_VERSION;"
|
.BI "TERMKEY_CHECK_VERSION;"
|
||||||
.br
|
.br
|
||||||
.BI "termkey_tk *termkey_new(int " fd ", int " flags );
|
.BI "TermKey *termkey_new(int " fd ", int " flags );
|
||||||
.br
|
.br
|
||||||
.BI "void termkey_destroy(termkey_t *" tk );
|
.BI "void termkey_destroy(TermKey *" tk );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI\-ltermkey\fP.
|
Link with \fI\-ltermkey\fP.
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
\fBtermkey_new\fP() creates a new termkey instance connected to the file handle opened by \fIfd\fP using the \fIflags\fP. The \fBtermkey_tk\fP structure should be considered opaque; its contents are not intended for use outside of the library.
|
\fBtermkey_new\fP() creates a new termkey instance connected to the file handle opened by \fIfd\fP using the \fIflags\fP. The \fBTermKey\fP structure should be considered opaque; its contents are not intended for use outside of the library.
|
||||||
.PP
|
.PP
|
||||||
\fBtermkey_destroy\fP() destroys the given instance and releases any resources controlled by it. It will not close the underlying filehandle given as the \fIfd\fP argument to \fBtermkey_new\fP().
|
\fBtermkey_destroy\fP() destroys the given instance and releases any resources controlled by it. It will not close the underlying filehandle given as the \fIfd\fP argument to \fBtermkey_new\fP().
|
||||||
.PP
|
.PP
|
||||||
|
|
|
@ -5,9 +5,9 @@ termkey_set_flags, termkey_get_flags \- control the operational flags
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "void termkey_set_flags(termkey_t *" tk ", int " newflags );
|
.BI "void termkey_set_flags(TermKey *" tk ", int " newflags );
|
||||||
.br
|
.br
|
||||||
.BI "int termkey_get_flags(termkey_t *" tk );
|
.BI "int termkey_get_flags(TermKey *" tk );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
|
|
|
@ -5,9 +5,9 @@ termkey_set_waittime, termkey_get_waittime \- control the wait time for multibyt
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "void termkey_set_waittime(termkey_t *" tk ", int " msec );
|
.BI "void termkey_set_waittime(TermKey *" tk ", int " msec );
|
||||||
.br
|
.br
|
||||||
.BI "int termkey_get_waittime(termkey_t *" tk );
|
.BI "int termkey_get_waittime(TermKey *" tk );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
|
|
|
@ -5,8 +5,8 @@ termkey_snprint_key \- format a string representing a key event
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "size_t termkey_snprint_key(termkey_t *" tk ", char *" buffer ", size_t " len ",
|
.BI "size_t termkey_snprint_key(TermKey *" tk ", char *" buffer ", size_t " len ",
|
||||||
.BI " termkey_key " key ", termkey_format " format );
|
.BI " TermKeyKey " key ", TermKeyFormat " format );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
|
|
|
@ -7,14 +7,14 @@ termkey_waitkey \- wait for and retrieve the next key event
|
||||||
.nf
|
.nf
|
||||||
.B #include <termkey.h>
|
.B #include <termkey.h>
|
||||||
.sp
|
.sp
|
||||||
.BI "termkey_result termkey_waitkey(termkey_t *" tk ", termkey_key *" key );
|
.BI "TermKeyResult termkey_waitkey(TermKey *" tk ", TermKeyKey *" key );
|
||||||
.fi
|
.fi
|
||||||
.sp
|
.sp
|
||||||
Link with \fI-ltermkey\fP.
|
Link with \fI-ltermkey\fP.
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
\fBtermkey_waitkey\fP attempts to retrieve a single keypress event from the buffer, and put it in the structure referred to by \fIkey\fP. If successful it will return \fBTERMKEY_RES_KEY\fP to indicate that the structure now contains a new keypress event. If nothing is in the buffer it will block until one is available. If no events are ready and the input stream is now closed, will return \fBTERMKEY_RES_EOF\fP.
|
\fBtermkey_waitkey\fP attempts to retrieve a single keypress event from the buffer, and put it in the structure referred to by \fIkey\fP. If successful it will return \fBTERMKEY_RES_KEY\fP to indicate that the structure now contains a new keypress event. If nothing is in the buffer it will block until one is available. If no events are ready and the input stream is now closed, will return \fBTERMKEY_RES_EOF\fP.
|
||||||
.PP
|
.PP
|
||||||
For details of the \fBtermkey_key\fP structure, see \fBtermkey_getkey\fP(3).
|
For details of the \fBTermKeyKey\fP structure, see \fBtermkey_getkey\fP(3).
|
||||||
.PP
|
.PP
|
||||||
Some keypresses generate multiple bytes from the terminal. Because there may be network or other delays between the terminal and an application using termkey, \fBtermkey_waitkey\fP() will attempt to wait for the remaining bytes to arrive if it detects the start of a multibyte sequence. If no more bytes arrive within a certain time, then the bytes will be reported as they stand, even if this results in interpreting a partially-complete Escape sequence as a literal Escape key followed by some normal letters or other symbols. The amount of time to wait can be set by \fBtermkey_set_waittime\fP(3).
|
Some keypresses generate multiple bytes from the terminal. Because there may be network or other delays between the terminal and an application using termkey, \fBtermkey_waitkey\fP() will attempt to wait for the remaining bytes to arrive if it detects the start of a multibyte sequence. If no more bytes arrive within a certain time, then the bytes will be reported as they stand, even if this results in interpreting a partially-complete Escape sequence as a literal Escape key followed by some normal letters or other symbols. The amount of time to wait can be set by \fBtermkey_set_waittime\fP(3).
|
||||||
.SH "RETURN VALUE"
|
.SH "RETURN VALUE"
|
||||||
|
|
Loading…
Reference in New Issue