Denote general status with return codes, not special key symbols

This commit is contained in:
Paul LeoNerd Evans 2008-02-07 01:17:59 +00:00
parent a503403226
commit 6294cca6f9
3 changed files with 54 additions and 52 deletions

3
demo.c
View File

@ -20,9 +20,10 @@ int main(int argc, char *argv[]) {
termkey_t *tk = termkey_new(0, TERMKEY_FLAG_CONVERTKP); termkey_t *tk = termkey_new(0, TERMKEY_FLAG_CONVERTKP);
termkey_result ret;
termkey_key key; termkey_key key;
while(termkey_waitkey(tk, &key) && key.code != TERMKEY_SYM_EOF) { while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) {
if(key.flags & TERMKEY_KEYFLAG_SPECIAL) if(key.flags & TERMKEY_KEYFLAG_SPECIAL)
printf("Key %s%s%s%s (code %d)\n", printf("Key %s%s%s%s (code %d)\n",
key.modifiers & TERMKEY_KEYMOD_SHIFT ? "S-" : "", key.modifiers & TERMKEY_KEYMOD_SHIFT ? "S-" : "",

View File

@ -44,7 +44,7 @@ static inline void eatbytes(termkey_t *tk, size_t count)
tk->buffvalid -= count; tk->buffvalid -= count;
} }
static int getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key) static termkey_result getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key)
{ {
size_t csi_end = introlen; size_t csi_end = introlen;
@ -54,10 +54,8 @@ static int getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key)
csi_end++; csi_end++;
} }
if(csi_end >= tk->buffvalid) { if(csi_end >= tk->buffvalid)
key->code = TERMKEY_SYM_NONE; return TERMKEY_RES_NONE;
return key->code;
}
unsigned char cmd = tk->buffer[csi_end]; unsigned char cmd = tk->buffer[csi_end];
int arg1 = -1; int arg1 = -1;
@ -85,7 +83,7 @@ static int getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key)
} }
} }
key->code = TERMKEY_SYM_UNKNOWN; eatbytes(tk, csi_end + 1);
switch(cmd) { switch(cmd) {
case 'A': key->code = TERMKEY_SYM_UP; break; case 'A': key->code = TERMKEY_SYM_UP; break;
@ -118,31 +116,29 @@ static int getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key)
default: default:
fprintf(stderr, "CSI function key %d\n", arg1); fprintf(stderr, "CSI function key %d\n", arg1);
key->code = TERMKEY_SYM_UNKNOWN;
} }
break; break;
default: default:
fprintf(stderr, "CSI arg1=%d arg2=%d cmd=%c\n", arg1, arg2, cmd); fprintf(stderr, "CSI arg1=%d arg2=%d cmd=%c\n", arg1, arg2, cmd);
key->code = TERMKEY_SYM_UNKNOWN;
} }
key->modifiers = arg2 == -1 ? 0 : arg2 - 1; key->modifiers = arg2 == -1 ? 0 : arg2 - 1;
key->flags = TERMKEY_KEYFLAG_SPECIAL; key->flags = TERMKEY_KEYFLAG_SPECIAL;
eatbytes(tk, csi_end + 1); return TERMKEY_RES_KEY;
return key->code;
} }
static int getkey_ss3(termkey_t *tk, size_t introlen, termkey_key *key) static termkey_result getkey_ss3(termkey_t *tk, size_t introlen, termkey_key *key)
{ {
if(introlen + 1 < tk->buffvalid) { if(introlen + 1 < tk->buffvalid)
key->code = TERMKEY_SYM_NONE; return TERMKEY_RES_NONE;
return key->code;
}
unsigned char cmd = tk->buffer[introlen]; unsigned char cmd = tk->buffer[introlen];
key->code = TERMKEY_SYM_UNKNOWN; eatbytes(tk, introlen + 1);
switch(cmd) { switch(cmd) {
case 'A': key->code = TERMKEY_SYM_UP; break; case 'A': key->code = TERMKEY_SYM_UP; break;
@ -176,7 +172,7 @@ static int getkey_ss3(termkey_t *tk, size_t introlen, termkey_key *key)
default: default:
fprintf(stderr, "SS3 %c (0x%02x)\n", cmd, cmd); fprintf(stderr, "SS3 %c (0x%02x)\n", cmd, cmd);
break; key->code = TERMKEY_SYM_UNKNOWN;
} }
key->modifiers = 0; key->modifiers = 0;
@ -216,17 +212,13 @@ static int getkey_ss3(termkey_t *tk, size_t introlen, termkey_key *key)
} }
} }
eatbytes(tk, introlen + 1); return TERMKEY_RES_KEY;
return key->code;
} }
int termkey_getkey(termkey_t *tk, termkey_key *key) termkey_result termkey_getkey(termkey_t *tk, termkey_key *key)
{ {
if(tk->buffvalid == 0) { if(tk->buffvalid == 0)
key->code = tk->is_closed ? TERMKEY_SYM_EOF : TERMKEY_SYM_NONE; return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE;
return key->code;
}
// Now we're sure at least 1 byte is valid // Now we're sure at least 1 byte is valid
unsigned char b0 = tk->buffer[0]; unsigned char b0 = tk->buffer[0];
@ -239,7 +231,7 @@ int termkey_getkey(termkey_t *tk, termkey_key *key)
eatbytes(tk, 1); eatbytes(tk, 1);
return key->code; return TERMKEY_RES_KEY;
} }
unsigned char b1 = tk->buffer[1]; unsigned char b1 = tk->buffer[1];
@ -251,19 +243,26 @@ int termkey_getkey(termkey_t *tk, termkey_key *key)
tk->buffer++; tk->buffer++;
int metakey = termkey_getkey(tk, key); termkey_result metakey_result = termkey_getkey(tk, key);
if(metakey != TERMKEY_SYM_NONE) {
switch(metakey_result) {
case TERMKEY_RES_KEY:
key->modifiers |= TERMKEY_KEYMOD_ALT; key->modifiers |= TERMKEY_KEYMOD_ALT;
tk->buffer--; tk->buffer--;
eatbytes(tk, 1); eatbytes(tk, 1);
break;
case TERMKEY_RES_NONE:
case TERMKEY_RES_EOF:
break;
} }
return key->code; return metakey_result;
} }
else if(b0 < 0x20) { else if(b0 < 0x20) {
// Control key // Control key
key->code = TERMKEY_SYM_UNKNOWN; key->code = 0;
if(!(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { if(!(tk->flags & TERMKEY_FLAG_NOINTERPRET)) {
// Try to interpret C0 codes that have nice names // Try to interpret C0 codes that have nice names
@ -274,7 +273,7 @@ int termkey_getkey(termkey_t *tk, termkey_key *key)
} }
} }
if(key->code == TERMKEY_SYM_UNKNOWN) { if(!key->code) {
key->code = b0 + 0x40; key->code = b0 + 0x40;
key->modifiers = TERMKEY_KEYMOD_CTRL; key->modifiers = TERMKEY_KEYMOD_CTRL;
key->flags = 0; key->flags = 0;
@ -289,7 +288,7 @@ int termkey_getkey(termkey_t *tk, termkey_key *key)
eatbytes(tk, 1); eatbytes(tk, 1);
return key->code; return TERMKEY_RES_KEY;
} }
else if(b0 == 0x20 && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { else if(b0 == 0x20 && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) {
key->code = TERMKEY_SYM_SPACE; key->code = TERMKEY_SYM_SPACE;
@ -298,7 +297,7 @@ int termkey_getkey(termkey_t *tk, termkey_key *key)
eatbytes(tk, 1); eatbytes(tk, 1);
return key->code; return TERMKEY_RES_KEY;
} }
else if(b0 == 0x7f && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { else if(b0 == 0x7f && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) {
key->code = TERMKEY_SYM_DEL; key->code = TERMKEY_SYM_DEL;
@ -307,7 +306,7 @@ int termkey_getkey(termkey_t *tk, termkey_key *key)
eatbytes(tk, 1); eatbytes(tk, 1);
return key->code; return TERMKEY_RES_KEY;
} }
else if(b0 >= 0x20 && b0 < 0x80) { else if(b0 >= 0x20 && b0 < 0x80) {
// ASCII lowbyte range // ASCII lowbyte range
@ -320,17 +319,17 @@ int termkey_getkey(termkey_t *tk, termkey_key *key)
eatbytes(tk, 1); eatbytes(tk, 1);
return key->code; return TERMKEY_RES_KEY;
} }
fprintf(stderr, "TODO - tk->buffer[0] == 0x%02x\n", tk->buffer[0]); fprintf(stderr, "TODO - tk->buffer[0] == 0x%02x\n", tk->buffer[0]);
return TERMKEY_SYM_NONE; return TERMKEY_SYM_NONE;
} }
int termkey_waitkey(termkey_t *tk, termkey_key *key) termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key)
{ {
int ret; termkey_result ret;
while((ret = termkey_getkey(tk, key)) == TERMKEY_SYM_NONE) { while((ret = termkey_getkey(tk, key)) == TERMKEY_RES_NONE) {
termkey_advisereadable(tk); termkey_advisereadable(tk);
} }
@ -362,7 +361,7 @@ void termkey_advisereadable(termkey_t *tk)
// Must be kept synchronised with enum termkey_sym_e in termkey.h // Must be kept synchronised with enum termkey_sym_e in termkey.h
const char *keynames[] = { const char *keynames[] = {
"None", "NONE",
// Special names in C0 // Special names in C0
"Backspace", "Backspace",
@ -425,10 +424,7 @@ const char *keynames[] = {
const char *termkey_describe_sym(int code) const char *termkey_describe_sym(int code)
{ {
if(code == -1) if(code == TERMKEY_SYM_UNKNOWN)
return "EOF";
if(code == -2)
return "UNKNOWN"; return "UNKNOWN";
if(code < sizeof(keynames)/sizeof(keynames[0])) if(code < sizeof(keynames)/sizeof(keynames[0]))

View File

@ -5,9 +5,8 @@
#include <stdlib.h> #include <stdlib.h>
typedef enum { typedef enum {
TERMKEY_SYM_EOF = -1, // Stream closed TERMKEY_SYM_UNKNOWN = -1,
TERMKEY_SYM_UNKNOWN = -2, TERMKEY_SYM_NONE = 0,
TERMKEY_SYM_NONE = 0, // Did not find a key
// Special names in C0 // Special names in C0
TERMKEY_SYM_BACKSPACE, TERMKEY_SYM_BACKSPACE,
@ -68,7 +67,13 @@ typedef enum {
TERMKEY_SYM_KPEQUALS, TERMKEY_SYM_KPEQUALS,
// et cetera ad nauseum // et cetera ad nauseum
} termkey_sym_e; } termkey_sym;
typedef enum {
TERMKEY_RES_NONE,
TERMKEY_RES_KEY,
TERMKEY_RES_EOF,
} termkey_result;
enum { enum {
TERMKEY_KEYFLAG_SPECIAL = 0x01, // 'code' is a special keycode, not a unicode codepoint TERMKEY_KEYFLAG_SPECIAL = 0x01, // 'code' is a special keycode, not a unicode codepoint
@ -100,8 +105,8 @@ enum {
termkey_t *termkey_new(int fd, int flags); termkey_t *termkey_new(int fd, int flags);
void termkey_free(termkey_t *tk); void termkey_free(termkey_t *tk);
int termkey_getkey(termkey_t *tk, termkey_key *key); termkey_result termkey_getkey(termkey_t *tk, termkey_key *key);
int termkey_waitkey(termkey_t *tk, termkey_key *key); termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key);
void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen); void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen);