=== modified file 'driver-csi.c' --- driver-csi.c 2008-10-07 23:12:41 +0000 +++ driver-csi.c 2008-10-07 22:57:01 +0000 @@ -4,9 +4,19 @@ #include #include +struct keyinfo { + termkey_type type; + termkey_keysym sym; + int modifier_mask; + int modifier_set; +}; + typedef struct { termkey_t *tk; + // There are 32 C0 codes + struct keyinfo c0[32]; + // There are 64 codes 0x40 - 0x7F struct keyinfo csi_ss3s[64]; struct keyinfo ss3s[64]; @@ -16,10 +26,12 @@ struct keyinfo *csifuncs; } termkey_csi; +static termkey_keysym register_c0(termkey_csi *csi, termkey_keysym sym, unsigned char ctrl, const char *name); static termkey_keysym register_csi_ss3(termkey_csi *csi, termkey_type type, termkey_keysym 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 termkey_keysym register_csifunc(termkey_csi *csi, termkey_type type, termkey_keysym sym, int number, const char *name); +static termkey_keysym register_c0_full(termkey_csi *csi, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char ctrl, 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 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 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); @@ -32,6 +44,10 @@ int i; + for(i = 0; i < 32; i++) { + csi->c0[i].sym = TERMKEY_SYM_UNKNOWN; + } + for(i = 0; i < 64; i++) { csi->csi_ss3s[i].sym = TERMKEY_SYM_UNKNOWN; csi->ss3s[i].sym = TERMKEY_SYM_UNKNOWN; @@ -45,6 +61,11 @@ for(i = 0; i < csi->ncsifuncs; i++) csi->csifuncs[i].sym = TERMKEY_SYM_UNKNOWN; + register_c0(csi, TERMKEY_SYM_BACKSPACE, 0x08, "Backspace"); + register_c0(csi, TERMKEY_SYM_TAB, 0x09, "Tab"); + register_c0(csi, TERMKEY_SYM_ENTER, 0x0d, "Enter"); + register_c0(csi, TERMKEY_SYM_ESCAPE, 0x1b, "Escape"); + register_csi_ss3(csi, TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_UP, 'A', "Up"); register_csi_ss3(csi, TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_DOWN, 'B', "Down"); register_csi_ss3(csi, TERMKEY_TYPE_KEYSYM, TERMKEY_SYM_RIGHT, 'C', "Right"); @@ -122,6 +143,99 @@ #define UTF8_INVALID 0xFFFD +static int utf8_seqlen(int codepoint) +{ + if(codepoint < 0x0000080) return 1; + if(codepoint < 0x0000800) return 2; + if(codepoint < 0x0010000) return 3; + if(codepoint < 0x0200000) return 4; + if(codepoint < 0x4000000) return 5; + return 6; +} + +static void fill_utf8(termkey_key *key) +{ + int codepoint = key->code.codepoint; + int nbytes = utf8_seqlen(codepoint); + + key->utf8[nbytes] = 0; + + // This is easier done backwards + int b = nbytes; + while(b > 1) { + b--; + key->utf8[b] = 0x80 | (codepoint & 0x3f); + codepoint >>= 6; + } + + switch(nbytes) { + case 1: key->utf8[0] = (codepoint & 0x7f); break; + case 2: key->utf8[0] = 0xc0 | (codepoint & 0x1f); break; + case 3: key->utf8[0] = 0xe0 | (codepoint & 0x0f); break; + case 4: key->utf8[0] = 0xf0 | (codepoint & 0x07); break; + case 5: key->utf8[0] = 0xf8 | (codepoint & 0x03); break; + case 6: key->utf8[0] = 0xfc | (codepoint & 0x01); break; + } +} + +static inline void do_codepoint(termkey_t *tk, int codepoint, termkey_key *key) +{ + termkey_csi *csi = tk->driver_info; + + if(codepoint < 0x20) { + // C0 range + key->code.codepoint = 0; + key->modifiers = 0; + + if(!(tk->flags & TERMKEY_FLAG_NOINTERPRET) && csi->c0[codepoint].sym != TERMKEY_SYM_UNKNOWN) { + key->code.sym = csi->c0[codepoint].sym; + key->modifiers |= csi->c0[codepoint].modifier_set; + } + + if(!key->code.sym) { + key->type = TERMKEY_TYPE_UNICODE; + key->code.codepoint = codepoint + 0x40; + key->modifiers = TERMKEY_KEYMOD_CTRL; + } + else { + key->type = TERMKEY_TYPE_KEYSYM; + } + } + else if(codepoint == 0x20 && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { + // ASCII space + key->type = TERMKEY_TYPE_KEYSYM; + key->code.sym = TERMKEY_SYM_SPACE; + key->modifiers = 0; + } + else if(codepoint == 0x7f && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { + // ASCII DEL + key->type = TERMKEY_TYPE_KEYSYM; + key->code.sym = TERMKEY_SYM_DEL; + key->modifiers = 0; + } + else if(codepoint >= 0x20 && codepoint < 0x80) { + // ASCII lowbyte range + key->type = TERMKEY_TYPE_UNICODE; + key->code.codepoint = codepoint; + key->modifiers = 0; + } + else if(codepoint >= 0x80 && codepoint < 0xa0) { + // UTF-8 never starts with a C1 byte. So we can be sure of these + key->type = TERMKEY_TYPE_UNICODE; + key->code.codepoint = codepoint - 0x40; + key->modifiers = TERMKEY_KEYMOD_CTRL|TERMKEY_KEYMOD_ALT; + } + else { + // UTF-8 codepoint + key->type = TERMKEY_TYPE_UNICODE; + key->code.codepoint = codepoint; + key->modifiers = 0; + } + + if(key->type == TERMKEY_TYPE_UNICODE) + fill_utf8(key); +} + #define CHARAT(i) (tk->buffer[tk->buffstart + (i)]) static termkey_result getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key) @@ -140,7 +254,7 @@ if(tk->waittime) return TERMKEY_RES_AGAIN; - (*tk->method.emit_codepoint)(tk, '[', key); + do_codepoint(tk, '[', key); key->modifiers |= TERMKEY_KEYMOD_ALT; (*tk->method.eat_bytes)(tk, introlen); return TERMKEY_RES_KEY; @@ -196,7 +310,7 @@ if(arg[0] == 27) { int mod = key->modifiers; - (*tk->method.emit_codepoint)(tk, arg[2], key); + do_codepoint(tk, arg[2], key); key->modifiers |= mod; } else if(arg[0] >= 0 && arg[0] < csi->ncsifuncs) { @@ -233,7 +347,7 @@ if(tk->waittime) return TERMKEY_RES_AGAIN; - (*tk->method.emit_codepoint)(tk, 'O', key); + do_codepoint(tk, 'O', key); key->modifiers |= TERMKEY_KEYMOD_ALT; (*tk->method.eat_bytes)(tk, tk->buffcount); return TERMKEY_RES_KEY; @@ -288,7 +402,7 @@ if(tk->waittime) return TERMKEY_RES_AGAIN; - (*tk->method.emit_codepoint)(tk, b0, key); + do_codepoint(tk, b0, key); (*tk->method.eat_bytes)(tk, 1); return TERMKEY_RES_KEY; } @@ -302,7 +416,7 @@ return getkey_ss3(tk, 2, key); if(b1 == 0x1b) { - (*tk->method.emit_codepoint)(tk, b0, key); + do_codepoint(tk, b0, key); (*tk->method.eat_bytes)(tk, 1); return TERMKEY_RES_KEY; } @@ -334,7 +448,7 @@ } else if(b0 < 0xa0) { // Single byte C0, G0 or C1 - C1 is never UTF-8 initial byte - (*tk->method.emit_codepoint)(tk, b0, key); + do_codepoint(tk, b0, key); (*tk->method.eat_bytes)(tk, 1); return TERMKEY_RES_KEY; } @@ -348,7 +462,7 @@ if(b0 < 0xc0) { // Starts with a continuation byte - that's not right - (*tk->method.emit_codepoint)(tk, UTF8_INVALID, key); + do_codepoint(tk, UTF8_INVALID, key); (*tk->method.eat_bytes)(tk, 1); return TERMKEY_RES_KEY; } @@ -373,7 +487,7 @@ codepoint = b0 & 0x01; } else { - (*tk->method.emit_codepoint)(tk, UTF8_INVALID, key); + do_codepoint(tk, UTF8_INVALID, key); (*tk->method.eat_bytes)(tk, 1); return TERMKEY_RES_KEY; } @@ -384,7 +498,7 @@ for(int b = 1; b < nbytes; b++) { unsigned char cb = CHARAT(b); if(cb < 0x80 || cb >= 0xc0) { - (*tk->method.emit_codepoint)(tk, UTF8_INVALID, key); + do_codepoint(tk, UTF8_INVALID, key); (*tk->method.eat_bytes)(tk, b - 1); return TERMKEY_RES_KEY; } @@ -403,7 +517,7 @@ codepoint == 0xFFFF) codepoint = UTF8_INVALID; - (*tk->method.emit_codepoint)(tk, codepoint, key); + do_codepoint(tk, codepoint, key); (*tk->method.eat_bytes)(tk, nbytes); return TERMKEY_RES_KEY; } @@ -424,6 +538,28 @@ return TERMKEY_SYM_NONE; } +static termkey_keysym register_c0(termkey_csi *csi, termkey_keysym sym, unsigned char ctrl, const char *name) +{ + return register_c0_full(csi, sym, 0, 0, ctrl, name); +} + +static termkey_keysym register_c0_full(termkey_csi *csi, termkey_keysym sym, int modifier_set, int modifier_mask, unsigned char ctrl, const char *name) +{ + if(ctrl >= 0x20) { + fprintf(stderr, "Cannot register C0 key at ctrl 0x%02x - out of bounds\n", ctrl); + return -1; + } + + if(name) + sym = termkey_register_keyname(csi->tk, sym, name); + + csi->c0[ctrl].sym = sym; + csi->c0[ctrl].modifier_set = modifier_set; + csi->c0[ctrl].modifier_mask = modifier_mask; + + return sym; +} + static termkey_keysym register_csi_ss3(termkey_csi *csi, termkey_type type, termkey_keysym sym, unsigned char cmd, const char *name) { return register_csi_ss3_full(csi, type, sym, 0, 0, cmd, name); === modified file 'termkey-internal.h' --- termkey-internal.h 2008-10-07 23:12:41 +0000 +++ termkey-internal.h 2008-10-07 22:57:01 +0000 @@ -13,13 +13,6 @@ termkey_result (*getkey)(termkey_t *tk, termkey_key *key); }; -struct keyinfo { - termkey_type type; - termkey_keysym sym; - int modifier_mask; - int modifier_set; -}; - struct termkey { int fd; int flags; @@ -38,9 +31,6 @@ int nkeynames; const char **keynames; - // There are 32 C0 codes - struct keyinfo c0[32]; - struct termkey_driver driver; void *driver_info; @@ -48,21 +38,9 @@ // want exported as real symbols in the library struct { void (*eat_bytes)(termkey_t *tk, size_t count); - void (*emit_codepoint)(termkey_t *tk, int codepoint, termkey_key *key); } method; }; extern struct termkey_driver termkey_driver_csi; -// Keep this here for now since it's tiny -static inline int utf8_seqlen(int codepoint) -{ - if(codepoint < 0x0000080) return 1; - if(codepoint < 0x0000800) return 2; - if(codepoint < 0x0010000) return 3; - if(codepoint < 0x0200000) return 4; - if(codepoint < 0x4000000) return 5; - return 6; -} - #endif === modified file 'termkey.c' --- termkey.c 2008-10-07 23:12:41 +0000 +++ termkey.c 2008-10-07 22:57:01 +0000 @@ -15,10 +15,6 @@ // Forwards for the "protected" methods static void eat_bytes(termkey_t *tk, size_t count); -static void emit_codepoint(termkey_t *tk, int codepoint, termkey_key *key); - -static termkey_keysym register_c0(termkey_t *tk, termkey_keysym 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); termkey_t *termkey_new_full(int fd, int flags, size_t buffsize, int waittime) { @@ -71,16 +67,7 @@ for(i = 0; i < tk->nkeynames; i++) tk->keynames[i] = NULL; - for(i = 0; i < 32; i++) - tk->c0[i].sym = TERMKEY_SYM_NONE; - tk->method.eat_bytes = &eat_bytes; - tk->method.emit_codepoint = &emit_codepoint; - - register_c0(tk, TERMKEY_SYM_BACKSPACE, 0x08, "Backspace"); - register_c0(tk, TERMKEY_SYM_TAB, 0x09, "Tab"); - register_c0(tk, TERMKEY_SYM_ENTER, 0x0d, "Enter"); - register_c0(tk, TERMKEY_SYM_ESCAPE, 0x1b, "Escape"); for(i = 0; drivers[i]; i++) { void *driver_info = (*drivers[i]->new_driver)(tk); @@ -170,87 +157,6 @@ } } -static void fill_utf8(termkey_key *key) -{ - int codepoint = key->code.codepoint; - int nbytes = utf8_seqlen(codepoint); - - key->utf8[nbytes] = 0; - - // This is easier done backwards - int b = nbytes; - while(b > 1) { - b--; - key->utf8[b] = 0x80 | (codepoint & 0x3f); - codepoint >>= 6; - } - - switch(nbytes) { - case 1: key->utf8[0] = (codepoint & 0x7f); break; - case 2: key->utf8[0] = 0xc0 | (codepoint & 0x1f); break; - case 3: key->utf8[0] = 0xe0 | (codepoint & 0x0f); break; - case 4: key->utf8[0] = 0xf0 | (codepoint & 0x07); break; - case 5: key->utf8[0] = 0xf8 | (codepoint & 0x03); break; - case 6: key->utf8[0] = 0xfc | (codepoint & 0x01); break; - } -} - -static void emit_codepoint(termkey_t *tk, int codepoint, termkey_key *key) -{ - if(codepoint < 0x20) { - // C0 range - key->code.codepoint = 0; - key->modifiers = 0; - - if(!(tk->flags & TERMKEY_FLAG_NOINTERPRET) && tk->c0[codepoint].sym != TERMKEY_SYM_UNKNOWN) { - key->code.sym = tk->c0[codepoint].sym; - key->modifiers |= tk->c0[codepoint].modifier_set; - } - - if(!key->code.sym) { - key->type = TERMKEY_TYPE_UNICODE; - key->code.codepoint = codepoint + 0x40; - key->modifiers = TERMKEY_KEYMOD_CTRL; - } - else { - key->type = TERMKEY_TYPE_KEYSYM; - } - } - else if(codepoint == 0x20 && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { - // ASCII space - key->type = TERMKEY_TYPE_KEYSYM; - key->code.sym = TERMKEY_SYM_SPACE; - key->modifiers = 0; - } - else if(codepoint == 0x7f && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { - // ASCII DEL - key->type = TERMKEY_TYPE_KEYSYM; - key->code.sym = TERMKEY_SYM_DEL; - key->modifiers = 0; - } - else if(codepoint >= 0x20 && codepoint < 0x80) { - // ASCII lowbyte range - key->type = TERMKEY_TYPE_UNICODE; - key->code.codepoint = codepoint; - key->modifiers = 0; - } - else if(codepoint >= 0x80 && codepoint < 0xa0) { - // UTF-8 never starts with a C1 byte. So we can be sure of these - key->type = TERMKEY_TYPE_UNICODE; - key->code.codepoint = codepoint - 0x40; - key->modifiers = TERMKEY_KEYMOD_CTRL|TERMKEY_KEYMOD_ALT; - } - else { - // UTF-8 codepoint - key->type = TERMKEY_TYPE_UNICODE; - key->code.codepoint = codepoint; - key->modifiers = 0; - } - - if(key->type == TERMKEY_TYPE_UNICODE) - fill_utf8(key); -} - termkey_result termkey_getkey(termkey_t *tk, termkey_key *key) { return (*tk->driver.getkey)(tk, key); @@ -367,28 +273,6 @@ return "UNKNOWN"; } -static termkey_keysym register_c0(termkey_t *tk, termkey_keysym sym, unsigned char ctrl, const char *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) -{ - if(ctrl >= 0x20) { - fprintf(stderr, "Cannot register C0 key at ctrl 0x%02x - out of bounds\n", ctrl); - return -1; - } - - if(name) - sym = termkey_register_keyname(tk, sym, name); - - tk->c0[ctrl].sym = sym; - tk->c0[ctrl].modifier_set = modifier_set; - tk->c0[ctrl].modifier_mask = modifier_mask; - - return sym; -} - size_t termkey_snprint_key(termkey_t *tk, char *buffer, size_t len, termkey_key *key, termkey_format format) { size_t pos = 0;