=== modified file 'demo.c' --- demo.c 2008-02-07 01:17:59 +0000 +++ demo.c 2008-02-07 00:58:41 +0000 @@ -20,10 +20,9 @@ termkey_t *tk = termkey_new(0, TERMKEY_FLAG_CONVERTKP); - termkey_result ret; termkey_key key; - while((ret = termkey_waitkey(tk, &key)) != TERMKEY_RES_EOF) { + while(termkey_waitkey(tk, &key) && key.code != TERMKEY_SYM_EOF) { if(key.flags & TERMKEY_KEYFLAG_SPECIAL) printf("Key %s%s%s%s (code %d)\n", key.modifiers & TERMKEY_KEYMOD_SHIFT ? "S-" : "", === modified file 'termkey.c' --- termkey.c 2008-02-07 01:17:59 +0000 +++ termkey.c 2008-02-07 00:58:41 +0000 @@ -44,7 +44,7 @@ tk->buffvalid -= count; } -static termkey_result getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key) +static int getkey_csi(termkey_t *tk, size_t introlen, termkey_key *key) { size_t csi_end = introlen; @@ -54,8 +54,10 @@ csi_end++; } - if(csi_end >= tk->buffvalid) - return TERMKEY_RES_NONE; + if(csi_end >= tk->buffvalid) { + key->code = TERMKEY_SYM_NONE; + return key->code; + } unsigned char cmd = tk->buffer[csi_end]; int arg1 = -1; @@ -83,7 +85,7 @@ } } - eatbytes(tk, csi_end + 1); + key->code = TERMKEY_SYM_UNKNOWN; switch(cmd) { case 'A': key->code = TERMKEY_SYM_UP; break; @@ -116,29 +118,31 @@ default: fprintf(stderr, "CSI function key %d\n", arg1); - key->code = TERMKEY_SYM_UNKNOWN; } break; default: 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->flags = TERMKEY_KEYFLAG_SPECIAL; - return TERMKEY_RES_KEY; + eatbytes(tk, csi_end + 1); + + return key->code; } -static termkey_result getkey_ss3(termkey_t *tk, size_t introlen, termkey_key *key) +static int getkey_ss3(termkey_t *tk, size_t introlen, termkey_key *key) { - if(introlen + 1 < tk->buffvalid) - return TERMKEY_RES_NONE; + if(introlen + 1 < tk->buffvalid) { + key->code = TERMKEY_SYM_NONE; + return key->code; + } unsigned char cmd = tk->buffer[introlen]; - eatbytes(tk, introlen + 1); + key->code = TERMKEY_SYM_UNKNOWN; switch(cmd) { case 'A': key->code = TERMKEY_SYM_UP; break; @@ -172,7 +176,7 @@ default: fprintf(stderr, "SS3 %c (0x%02x)\n", cmd, cmd); - key->code = TERMKEY_SYM_UNKNOWN; + break; } key->modifiers = 0; @@ -212,13 +216,17 @@ } } - return TERMKEY_RES_KEY; + eatbytes(tk, introlen + 1); + + return key->code; } -termkey_result termkey_getkey(termkey_t *tk, termkey_key *key) +int termkey_getkey(termkey_t *tk, termkey_key *key) { - if(tk->buffvalid == 0) - return tk->is_closed ? TERMKEY_RES_EOF : TERMKEY_RES_NONE; + if(tk->buffvalid == 0) { + key->code = tk->is_closed ? TERMKEY_SYM_EOF : TERMKEY_SYM_NONE; + return key->code; + } // Now we're sure at least 1 byte is valid unsigned char b0 = tk->buffer[0]; @@ -231,7 +239,7 @@ eatbytes(tk, 1); - return TERMKEY_RES_KEY; + return key->code; } unsigned char b1 = tk->buffer[1]; @@ -243,26 +251,19 @@ tk->buffer++; - termkey_result metakey_result = termkey_getkey(tk, key); - - switch(metakey_result) { - case TERMKEY_RES_KEY: - key->modifiers |= TERMKEY_KEYMOD_ALT; - tk->buffer--; - eatbytes(tk, 1); - break; - - case TERMKEY_RES_NONE: - case TERMKEY_RES_EOF: - break; + int metakey = termkey_getkey(tk, key); + if(metakey != TERMKEY_SYM_NONE) { + key->modifiers |= TERMKEY_KEYMOD_ALT; + tk->buffer--; + eatbytes(tk, 1); } - return metakey_result; + return key->code; } else if(b0 < 0x20) { // Control key - key->code = 0; + key->code = TERMKEY_SYM_UNKNOWN; if(!(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { // Try to interpret C0 codes that have nice names @@ -273,7 +274,7 @@ } } - if(!key->code) { + if(key->code == TERMKEY_SYM_UNKNOWN) { key->code = b0 + 0x40; key->modifiers = TERMKEY_KEYMOD_CTRL; key->flags = 0; @@ -288,7 +289,7 @@ eatbytes(tk, 1); - return TERMKEY_RES_KEY; + return key->code; } else if(b0 == 0x20 && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { key->code = TERMKEY_SYM_SPACE; @@ -297,7 +298,7 @@ eatbytes(tk, 1); - return TERMKEY_RES_KEY; + return key->code; } else if(b0 == 0x7f && !(tk->flags & TERMKEY_FLAG_NOINTERPRET)) { key->code = TERMKEY_SYM_DEL; @@ -306,7 +307,7 @@ eatbytes(tk, 1); - return TERMKEY_RES_KEY; + return key->code; } else if(b0 >= 0x20 && b0 < 0x80) { // ASCII lowbyte range @@ -319,17 +320,17 @@ eatbytes(tk, 1); - return TERMKEY_RES_KEY; + return key->code; } fprintf(stderr, "TODO - tk->buffer[0] == 0x%02x\n", tk->buffer[0]); return TERMKEY_SYM_NONE; } -termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key) +int termkey_waitkey(termkey_t *tk, termkey_key *key) { - termkey_result ret; - while((ret = termkey_getkey(tk, key)) == TERMKEY_RES_NONE) { + int ret; + while((ret = termkey_getkey(tk, key)) == TERMKEY_SYM_NONE) { termkey_advisereadable(tk); } @@ -361,7 +362,7 @@ // Must be kept synchronised with enum termkey_sym_e in termkey.h const char *keynames[] = { - "NONE", + "None", // Special names in C0 "Backspace", @@ -424,7 +425,10 @@ const char *termkey_describe_sym(int code) { - if(code == TERMKEY_SYM_UNKNOWN) + if(code == -1) + return "EOF"; + + if(code == -2) return "UNKNOWN"; if(code < sizeof(keynames)/sizeof(keynames[0])) === modified file 'termkey.h' --- termkey.h 2008-02-07 01:17:59 +0000 +++ termkey.h 2008-02-07 00:58:41 +0000 @@ -5,8 +5,9 @@ #include typedef enum { - TERMKEY_SYM_UNKNOWN = -1, - TERMKEY_SYM_NONE = 0, + TERMKEY_SYM_EOF = -1, // Stream closed + TERMKEY_SYM_UNKNOWN = -2, + TERMKEY_SYM_NONE = 0, // Did not find a key // Special names in C0 TERMKEY_SYM_BACKSPACE, @@ -67,13 +68,7 @@ TERMKEY_SYM_KPEQUALS, // et cetera ad nauseum -} termkey_sym; - -typedef enum { - TERMKEY_RES_NONE, - TERMKEY_RES_KEY, - TERMKEY_RES_EOF, -} termkey_result; +} termkey_sym_e; enum { TERMKEY_KEYFLAG_SPECIAL = 0x01, // 'code' is a special keycode, not a unicode codepoint @@ -105,8 +100,8 @@ termkey_t *termkey_new(int fd, int flags); void termkey_free(termkey_t *tk); -termkey_result termkey_getkey(termkey_t *tk, termkey_key *key); -termkey_result termkey_waitkey(termkey_t *tk, termkey_key *key); +int termkey_getkey(termkey_t *tk, termkey_key *key); +int termkey_waitkey(termkey_t *tk, termkey_key *key); void termkey_pushinput(termkey_t *tk, unsigned char *input, size_t inputlen);