=== modified file 'Makefile' --- Makefile 2008-02-10 17:34:05 +0000 +++ Makefile 2008-02-07 00:58:41 +0000 @@ -1,6 +1,9 @@ CCFLAGS=-Wall -Iinclude -std=c99 LDFLAGS= +CCFLAGS+=$(shell pkg-config --cflags glib-2.0) +LDFLAGS+=$(shell pkg-config --libs glib-2.0) + all: demo demo: termkey.o demo.c === modified file 'termkey.c' --- termkey.c 2008-02-10 17:34:05 +0000 +++ termkey.c 2008-02-10 15:28:07 +0000 @@ -4,6 +4,8 @@ #include #include +// Use GLib to implement for now because I am lazy. Rewrite sometime +#include #include struct termkey { @@ -18,10 +20,12 @@ int nkeynames; const char **keynames; - // There are 64 codes 0x40 - 0x7F - int csi_ss3s[64]; - int ss3s[64]; - char ss3_kpalts[64]; + int ncsi_ss3s; + int *csi_ss3s; + + int nss3s; + int *ss3s; + char *ss3_kpalts; int ncsifuncs; int *csifuncs; @@ -29,9 +33,7 @@ termkey_t *termkey_new_full(int fd, int flags, size_t buffsize) { - termkey_t *tk = malloc(sizeof(*tk)); - if(!tk) - return NULL; + termkey_t *tk = g_new0(struct termkey, 1); if(!(flags & (TERMKEY_FLAG_RAW|TERMKEY_FLAG_UTF8))) { int locale_is_utf8 = 0; @@ -55,37 +57,12 @@ tk->fd = fd; tk->flags = flags; - tk->buffer = malloc(buffsize); - if(!tk->buffer) { - free(tk); - return NULL; - } - + tk->buffer = g_malloc0(buffsize); tk->buffvalid = 0; tk->buffsize = buffsize; tk->is_closed = 0; - int i; - for(i = 0; i < 64; i++) { - tk->csi_ss3s[i] = TERMKEY_SYM_UNKNOWN; - tk->ss3s[i] = TERMKEY_SYM_UNKNOWN; - tk->ss3_kpalts[i] = 0; - } - - // These numbers aren't too important; buffers will be grown if insufficient - tk->nkeynames = 64; - tk->ncsifuncs = 32; - - tk->keynames = malloc(sizeof(tk->keynames[0]) * tk->nkeynames); - tk->csifuncs = malloc(sizeof(tk->csifuncs[0]) * tk->ncsifuncs); - - for(i = 0; i < tk->nkeynames; i++) - tk->keynames[i] = NULL; - - for(i = 0; i < tk->ncsifuncs; i++) - tk->csifuncs[i] = TERMKEY_SYM_NONE; - // Special built-in names termkey_register_keyname(tk, TERMKEY_SYM_NONE, "NONE"); @@ -222,8 +199,10 @@ fprintf(stderr, "CSI function key %d\n", arg[0]); } else { - // We know from the logic above that cmd must be >= 0x40 and < 0x80 - key->code = tk->csi_ss3s[cmd - 0x40]; + if(cmd < tk->ncsi_ss3s) + key->code = tk->csi_ss3s[cmd]; + else + key->code = TERMKEY_SYM_UNKNOWN; if(key->code == TERMKEY_SYM_UNKNOWN) fprintf(stderr, "CSI arg1=%d arg2=%d cmd=%c\n", arg[0], arg[1], cmd); @@ -244,14 +223,14 @@ eatbytes(tk, introlen + 1); - if(cmd < 0x40 || cmd >= 0x80) - return TERMKEY_SYM_UNKNOWN; - - key->code = tk->csi_ss3s[cmd - 0x40]; - - if(key->code == TERMKEY_SYM_UNKNOWN) { - if(tk->flags & TERMKEY_FLAG_CONVERTKP && tk->ss3_kpalts[cmd - 0x40]) { - key->code = tk->ss3_kpalts[cmd - 0x40]; + key->code = TERMKEY_SYM_UNKNOWN; + + if(cmd < tk->ncsi_ss3s) + key->code = tk->csi_ss3s[cmd]; + + if(key->code == TERMKEY_SYM_UNKNOWN && cmd < tk->nss3s) { + if(tk->flags & TERMKEY_FLAG_CONVERTKP && tk->ss3_kpalts[cmd]) { + key->code = tk->ss3_kpalts[cmd]; key->modifiers = 0; key->flags = 0; @@ -261,7 +240,7 @@ return TERMKEY_RES_KEY; } - key->code = tk->ss3s[cmd - 0x40]; + key->code = tk->ss3s[cmd]; } if(key->code == TERMKEY_SYM_UNKNOWN) @@ -598,8 +577,7 @@ code = tk->nkeynames; if(code >= tk->nkeynames) { - const char **new_keynames = realloc(tk->keynames, sizeof(new_keynames[0]) * (code + 1)); - tk->keynames = new_keynames; + tk->keynames = g_renew(const char*, tk->keynames, code + 1); // Fill in the hole for(int i = tk->nkeynames; i < code; i++) @@ -615,31 +593,44 @@ int termkey_register_csi_ss3(termkey_t *tk, int code, unsigned char cmd, const char *name) { - if(cmd < 0x40 || cmd >= 0x80) { - fprintf(stderr, "Cannot register CSI/SS3 key at cmd 0x%02x - out of bounds\n", cmd); - return -1; - } - if(name) code = termkey_register_keyname(tk, code, name); - tk->csi_ss3s[cmd - 0x40] = code; + if(cmd >= tk->ncsi_ss3s) { + tk->csi_ss3s = g_renew(int, tk->csi_ss3s, cmd + 1); + + // Fill in the hole + for(int i = tk->ncsi_ss3s; i < cmd; i++) + tk->csi_ss3s[i] = TERMKEY_SYM_UNKNOWN; + + tk->ncsi_ss3s = cmd + 1; + } + + tk->csi_ss3s[cmd] = code; return code; } int termkey_register_ss3kpalt(termkey_t *tk, int code, unsigned char cmd, const char *name, char kpalt) { - if(cmd < 0x40 || cmd >= 0x80) { - fprintf(stderr, "Cannot register SS3 key at cmd 0x%02x - out of bounds\n", cmd); - return -1; - } - if(name) code = termkey_register_keyname(tk, code, name); - tk->ss3s[cmd - 0x40] = code; - tk->ss3_kpalts[cmd - 0x40] = kpalt; + if(cmd >= tk->nss3s) { + tk->ss3s = g_renew(int, tk->ss3s, cmd + 1); + tk->ss3_kpalts = g_renew(char, tk->ss3_kpalts, cmd + 1); + + // Fill in the hole + for(int i = tk->nss3s; i < cmd; i++) { + tk->ss3s[i] = TERMKEY_SYM_UNKNOWN; + tk->ss3_kpalts[i] = 0; + } + + tk->nss3s = cmd + 1; + } + + tk->ss3s[cmd] = code; + tk->ss3_kpalts[cmd] = kpalt; return code; } @@ -650,14 +641,12 @@ code = termkey_register_keyname(tk, code, name); if(number >= tk->ncsifuncs) { - int *new_csifuncs = realloc(tk->csifuncs, sizeof(new_csifuncs[0]) * (number + 1)); - tk->csifuncs = new_csifuncs; + tk->csifuncs = g_renew(int, tk->csifuncs, number + 1); + tk->ncsifuncs = number + 1; // Fill in the hole for(int i = tk->ncsifuncs; i < number; i++) tk->csifuncs[i] = TERMKEY_SYM_UNKNOWN; - - tk->ncsifuncs = number + 1; } tk->csifuncs[number] = code;