Clean up termEvent state machine

master
Curtis McEnroe 2018-08-14 15:34:10 -04:00
parent 398f752322
commit 9167a9d92c
No known key found for this signature in database
GPG Key ID: CEA2F97ADCFCD77C
1 changed files with 16 additions and 15 deletions

31
term.c
View File

@ -21,7 +21,6 @@
#include "chat.h" #include "chat.h"
#define PAIR(a, b) (((short)(a) << 8) | ((short)(b) & 0xFF))
static bool xterm; 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) { enum TermEvent termEvent(char ch) {
static char state = '\0'; static int state = 0;
switch (PAIR(state, ch)) { switch (T(state, ch)) {
break; case PAIR('\0', '\33'): state = '\33'; case T(0, ESC): state = 1; return 0;
break; case PAIR('\33', '['): state = '['; case T(1, '['): state = 2; return 0;
break; case PAIR('[', 'I'): state = '\0'; return TERM_FOCUS_IN; case T(2, 'I'): state = 0; return TERM_FOCUS_IN;
break; case PAIR('[', 'O'): state = '\0'; return TERM_FOCUS_OUT; case T(2, 'O'): state = 0; return TERM_FOCUS_OUT;
break; case PAIR('[', '2'): state = '2'; case T(2, '2'): state = 3; return 0;
break; case PAIR('2', '0'): state = '0'; case T(3, '0'): state = 4; return 0;
break; case PAIR('0', '0'): state = '0'; case T(4, '0'): state = 5; return 0;
break; case PAIR('0', '~'): state = '\0'; return TERM_PASTE_START; case T(5, '~'): state = 0; return TERM_PASTE_START;
break; case PAIR('0', '1'): state = '1'; case T(4, '1'): state = 6; return 0;
break; case PAIR('1', '~'): state = '\0'; return TERM_PASTE_END; case T(6, '~'): state = 0; return TERM_PASTE_END;
break; default: state = '\0'; default: state = 0; return 0;
} }
return TERM_NONE;
} }