1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
#include "termkey.h"
#include "termkey-internal.h"
#include <term.h>
#include <curses.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
struct ti_keyinfo {
const char *seq;
size_t seqlen; // cached strlen of seq since we'll use it lots
struct keyinfo key;
};
typedef struct {
termkey_t *tk;
int nseqs;
int alloced_seqs;
struct ti_keyinfo *seqs;
} termkey_ti;
static int funcname2keysym(const char *funcname, termkey_type *typep, termkey_keysym *symp, int *modmask, int *modsetp);
static void register_seq(termkey_ti *ti, const char *seq, termkey_type type, termkey_keysym sym, int modmask, int modset);
static void *new_driver(termkey_t *tk, const char *term)
{
int err;
if(setupterm(term, 1, &err) != OK)
return NULL;
termkey_ti *ti = malloc(sizeof *ti);
ti->tk = tk;
ti->alloced_seqs = 32; // We'll allocate more space if we need
ti->nseqs = 0;
ti->seqs = malloc(ti->alloced_seqs * sizeof(ti->seqs[0]));
int i;
for(i = 0; strfnames[i]; i++) {
// Only care about the key_* constants
const char *name = strfnames[i];
if(strncmp(name, "key_", 4) != 0)
continue;
const char *value = tigetstr(strnames[i]);
if(!value || value == (char*)-1)
continue;
termkey_type type;
termkey_keysym sym;
int mask = 0;
int set = 0;
if(!funcname2keysym(strfnames[i] + 4, &type, &sym, &mask, &set))
continue;
register_seq(ti, value, type, sym, mask, set);
}
return ti;
}
static void free_driver(void *private)
{
}
#define CHARAT(i) (tk->buffer[tk->buffstart + (i)])
static termkey_result getkey(termkey_t *tk, termkey_key *key, int force)
{
termkey_ti *ti = tk->driver_info;
if(tk->buffcount == 0)
return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
// Now we're sure at least 1 byte is valid
unsigned char b0 = CHARAT(0);
int i;
for(i = 0; i < ti->nseqs; i++) {
struct ti_keyinfo *s = &(ti->seqs[i]);
if(s->seq[0] != b0)
continue;
if(tk->buffcount >= s->seqlen) {
if(strncmp(s->seq, (const char*)tk->buffer + tk->buffstart, s->seqlen) == 0) {
key->type = s->key.type;
key->code.sym = s->key.sym;
key->modifiers = s->key.modifier_set;
(*tk->method.eat_bytes)(tk, s->seqlen);
return TERMKEY_RES_KEY;
}
}
else if(!force) {
// Maybe we'd get a partial match
if(strncmp(s->seq, (const char*)tk->buffer + tk->buffstart, tk->buffcount) == 0)
return TERMKEY_RES_AGAIN;
}
}
// No special seq. Must be a simple key then
return (*tk->method.getkey_simple)(tk, key);
}
static struct {
const char *funcname;
termkey_type type;
termkey_keysym sym;
int mods;
} funcs[] =
{
{ "backspace", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_BACKSPACE, 0 },
{ "begin", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_BEGIN, 0 },
{ "btab", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_TAB, TERMKEY_KEYMOD_SHIFT },
{ "dc", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_DELETE, 0 },
{ "down", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_DOWN, 0 },
{ "end", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_END, 0 },
{ "find", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_FIND, 0 },
{ "home", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_HOME, 0 },
{ "ic", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_INSERT, 0 },
{ "left", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_LEFT, 0 },
{ "next", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_PAGEDOWN, 0 }, // Not quite, but it's the best we can do
{ "npage", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_PAGEDOWN, 0 },
{ "ppage", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_PAGEUP, 0 },
{ "previous", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_PAGEUP, 0 }, // Not quite, but it's the best we can do
{ "right", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_RIGHT, 0 },
{ "select", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_SELECT, 0 },
{ "up", TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_UP, 0 },
{ NULL },
};
static int funcname2keysym(const char *funcname, termkey_type *typep, termkey_keysym *symp, int *modmaskp, int *modsetp)
{
int i;
for(i = 0; funcs[i].funcname; i++) {
if(strcmp(funcname, funcs[i].funcname) == 0) {
*typep = funcs[i].type;
*symp = funcs[i].sym;
*modmaskp = funcs[i].mods;
*modsetp = funcs[i].mods;
return 1;
}
}
if(funcname[0] == 'f' && isdigit(funcname[1])) {
*typep = TERMKEY_TYPE_FUNCTION;
*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)) {
*modmaskp |= TERMKEY_KEYMOD_SHIFT;
*modsetp |= TERMKEY_KEYMOD_SHIFT;
return 1;
}
printf("TODO: Need to convert funcname %s to a type/sym\n", funcname);
return 0;
}
static void register_seq(termkey_ti *ti, const char *seq, termkey_type type, termkey_keysym sym, int modmask, int modset)
{
if(ti->nseqs == ti->alloced_seqs) {
ti->alloced_seqs *= 2;
void *newseqs = realloc(ti->seqs, ti->alloced_seqs * sizeof(ti->seqs[9]));
// TODO: Error handle
ti->seqs = newseqs;
}
int i = ti->nseqs++;
ti->seqs[i].seq = seq;
ti->seqs[i].seqlen = strlen(seq);
ti->seqs[i].key.type = type;
ti->seqs[i].key.sym = sym;
ti->seqs[i].key.modifier_mask = modmask;
ti->seqs[i].key.modifier_set = modset;
}
struct termkey_driver termkey_driver_ti = {
.new_driver = new_driver,
.free_driver = free_driver,
.getkey = getkey,
};
|