diff options
Diffstat (limited to 'io.c')
-rw-r--r-- | io.c | 188 |
1 files changed, 100 insertions, 88 deletions
@@ -1,103 +1,121 @@ #include "sux.h" +char *key = NULL; uint8_t key_idx = 0; uint8_t iscol; uint8_t idx = 3; uint8_t bcd[4]; -int get_keycode(char *str) { - size_t size = strlen(str); +static inline char *get_keyseq(int keycode) { + switch (keycode) { + case KEY_BACKSPACE: return "\b"; + case KEY_UP : return "\x1B[A"; + case KEY_DOWN : return "\x1B[B"; + case KEY_RIGHT : return "\x1B[C"; + case KEY_LEFT : return "\x1B[D"; + case KEY_F( 1) : return "\x1BOP"; + case KEY_F( 2) : return "\x1BOQ"; + case KEY_F( 3) : return "\x1BOR"; + case KEY_F( 4) : return "\x1BOS"; + case KEY_F( 5) : return "\x1B[15~"; + case KEY_F( 6) : return "\x1B[17~"; + case KEY_F( 7) : return "\x1B[18~"; + case KEY_F( 8) : return "\x1B[19~"; + case KEY_F( 9) : return "\x1B[20~"; + case KEY_F(10) : return "\x1B[21~"; + case KEY_F(11) : return "\x1B[23~"; + case KEY_F(12) : return "\x1B[24~"; + default : return NULL; + } +} + +int get_key(WINDOW *scr) { + int x, y; + int c; int keycode = 0; - if (size == 1) { - keycode = str[0]; - } else if (size > 1) { - int isesc = 0; - for (int i = 0; str[i] != '\0'; i++) { - switch (str[i]) { - case '\x1B': isesc = 1; break; - case '[': - if (isesc) { - i++; - char tmp[3]; - switch (str[i]) { - case 'A': keycode = KEY_UP ; break; - case 'B': keycode = KEY_DOWN ; break; - case 'C': keycode = KEY_LEFT ; break; - case 'D': keycode = KEY_RIGHT; break; - default : - if (isdigit(str[i])) { - memcpy(tmp, str+i, 2); - i += 2; - tmp[2] = '\0'; - int num = strtol(tmp, NULL, 10); - if (str[i] == '~') { - switch (num) { - case 15: keycode = KEY_F( 5); break; - case 17: keycode = KEY_F( 6); break; - case 18: keycode = KEY_F( 7); break; - case 19: keycode = KEY_F( 8); break; - case 20: keycode = KEY_F( 9); break; - case 21: keycode = KEY_F(10); break; - case 23: keycode = KEY_F(11); break; - case 24: keycode = KEY_F(12); break; - } - } - } + curs_set(1); + if ((key == NULL) || (key && key[key_idx] == '\0') || !kbd_rdy) { + c = wgetch(scr); + if (c == 19) { + if (kbd_rdy) { + c = wgetch(scr); + } + step = 1; + } else if (c == 0x11) { + end = 1; + } + if (kbd_rdy) { + key_idx = 0; + key = get_keyseq(c); + } + keycode = c; + } + if (kbd_rdy) { + c = (key != NULL) ? key[key_idx++] : c; + } - } - } else { - keycode = '['; - } - break; - case 'O': - if (isesc) { - i++; - switch (str[i]) { - case 'P': keycode = KEY_F(1); break; - case 'Q': keycode = KEY_F(2); break; - case 'R': keycode = KEY_F(3); break; - case 'S': keycode = KEY_F(4); break; - } - } else { - keycode = 'O'; - } + if (step) { + if (keycode != 19 && keycode != 18 && keycode != 0x11 && !isalnum(keycode)) { + /*WINDOW *w; + int maxcol = getmaxx(scr)/2; + int maxrow = getmaxy(scr)/2;*/ + switch (keycode) { + case KEY_F(1): + /*w = newwin(maxrow, maxcol, maxrow, maxcol); + emumon(w); + delwin(w);*/ + endwin(); + puts("Starting asmmon()"); + asmmon("stdin"); + puts("Reinitializing screen."); + init_scr(); + wrefresh(scr); break; } + #if debug && !bench + getyx(scr, y, x); + wmove(scr, getmaxy(scr)-1, 0); + wclrtoeol(scr); + wprintw(scr, "keycode: %i", keycode); + wmove(scr, y, x); + #endif } } - return keycode; -} - -int get_key(WINDOW *scr) { - int c = 0; - uint8_t i = 0; - uint8_t isesc = 0; - size_t size = strlen(key); - if (key[key_idx] == '\0' && size) { - memset(key, 0, size+1); - key_idx = 0; - } - if (!size || !key_idx) { - for (;;) { - c = wgetch(scr); - if (c != ERR) { - key[i++] = c; - isesc = (c == '\x1B'); - } else if (i && !isesc) { - key[i] = 0; - key_idx = 0; - size = strlen(key); + if (kbd_rdy) { + switch (c) { + case ERR: + addr[CTRL_ADDR] = 0; + break; + case '\0': break; + default: + if (kbd_rdy && c < 0x100) { + addr[CTRL_ADDR] = 1; + #if debug && !bench + wmove(scr, getmaxy(scr)-1, 0); + wclrtoeol(scr); + wprintw(scr, "c: %i ", c); + wprintw(scr, "key: "); + for (int i = 0; key && key[i] != '\0'; i++) { + wprintw(scr, "$%02X%s", key[i], (key[i+1] != '\0') ? ", " : ""); + } + wmove(scr, y, x); + #endif + } break; - } + } + } else { + if (step) { + step = !(c == 18); } } - if (key[key_idx] != '\0') { - return key[key_idx++]; - } + addr[STEP_ADDR] = step; + curs_set(0); + return c; } + void io(uint64_t address, uint8_t rw) { int x, y; uint16_t scr_col = 0; @@ -109,14 +127,8 @@ void io(uint64_t address, uint8_t rw) { break; } kbd_rdy = 1; - pthread_mutex_lock(&main_mutex); - pthread_cond_signal(&main_cond); - pthread_mutex_unlock(&main_mutex); - #if !keypoll - pthread_mutex_lock(&mutex); - pthread_cond_wait(&cond, &mutex); - pthread_mutex_unlock(&mutex); - #endif + wrefresh(scr); + addr[RX_ADDR] = get_key(scr); kbd_rdy = 0; break; case TX_ADDR: |