diff options
author | June McEnroe <june@causal.agency> | 2018-08-14 15:34:10 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2018-08-14 15:34:10 -0400 |
commit | 9167a9d92cb15b5000adcbecba95555c33bd0079 (patch) | |
tree | a0f786e9aa2a8c258284fed0604ae82d530b5221 | |
parent | Keep hashing '\0' until color is not black (diff) | |
download | catgirl-9167a9d92cb15b5000adcbecba95555c33bd0079.tar.gz catgirl-9167a9d92cb15b5000adcbecba95555c33bd0079.zip |
Clean up termEvent state machine
Diffstat (limited to '')
-rw-r--r-- | term.c | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/term.c b/term.c index 1266328..4f62d20 100644 --- a/term.c +++ b/term.c @@ -21,7 +21,6 @@ #include "chat.h" -#define PAIR(a, b) (((short)(a) << 8) | ((short)(b) & 0xFF)) static bool xterm; @@ -48,20 +47,22 @@ void termMode(enum TermMode mode, bool set) { } } +#define ESC '\33' +#define T(s, i) ((s) << 8 | (i)) + enum TermEvent termEvent(char ch) { - static char state = '\0'; - switch (PAIR(state, ch)) { - break; case PAIR('\0', '\33'): state = '\33'; - break; case PAIR('\33', '['): state = '['; - break; case PAIR('[', 'I'): state = '\0'; return TERM_FOCUS_IN; - break; case PAIR('[', 'O'): state = '\0'; return TERM_FOCUS_OUT; - break; case PAIR('[', '2'): state = '2'; - break; case PAIR('2', '0'): state = '0'; - break; case PAIR('0', '0'): state = '0'; - break; case PAIR('0', '~'): state = '\0'; return TERM_PASTE_START; - break; case PAIR('0', '1'): state = '1'; - break; case PAIR('1', '~'): state = '\0'; return TERM_PASTE_END; - break; default: state = '\0'; + static int state = 0; + switch (T(state, ch)) { + case T(0, ESC): state = 1; return 0; + case T(1, '['): state = 2; return 0; + case T(2, 'I'): state = 0; return TERM_FOCUS_IN; + case T(2, 'O'): state = 0; return TERM_FOCUS_OUT; + case T(2, '2'): state = 3; return 0; + case T(3, '0'): state = 4; return 0; + case T(4, '0'): state = 5; return 0; + case T(5, '~'): state = 0; return TERM_PASTE_START; + case T(4, '1'): state = 6; return 0; + case T(6, '~'): state = 0; return TERM_PASTE_END; + default: state = 0; return 0; } - return TERM_NONE; } |