diff options
| author | Your Name <you@example.com> | 2026-05-18 22:46:53 +0530 |
|---|---|---|
| committer | Your Name <you@example.com> | 2026-05-18 22:46:53 +0530 |
| commit | 910fe47b2ed1284d93ff4d274f5c2341cb0d2d0b (patch) | |
| tree | 2c0e48e153337b22dbe7f9cc531f5e92e91561fb /main | |
| parent | f2c4b15c72a2fa89caed5d8a11a4ea9832c48be6 (diff) | |
feat: add on-screen keyboard with layout/hit detection tests
Diffstat (limited to 'main')
| -rw-r--r-- | main/keyboard.c | 162 | ||||
| -rw-r--r-- | main/keyboard.h | 47 |
2 files changed, 209 insertions, 0 deletions
diff --git a/main/keyboard.c b/main/keyboard.c new file mode 100644 index 0000000..741bd08 --- /dev/null +++ b/main/keyboard.c | |||
| @@ -0,0 +1,162 @@ | |||
| 1 | #include "keyboard.h" | ||
| 2 | #include <string.h> | ||
| 3 | |||
| 4 | static const char *s_alpha_lower[] = { | ||
| 5 | "qwertyuiop", | ||
| 6 | "asdfghjkl", | ||
| 7 | "\001zxcvbnm\b", | ||
| 8 | "\002\003 \004" | ||
| 9 | }; | ||
| 10 | |||
| 11 | static const char *s_alpha_upper[] = { | ||
| 12 | "QWERTYUIOP", | ||
| 13 | "ASDFGHJKL", | ||
| 14 | "\001ZXCVBNM\b", | ||
| 15 | "\002\003 \004" | ||
| 16 | }; | ||
| 17 | |||
| 18 | static const char *s_numsym[] = { | ||
| 19 | "1234567890", | ||
| 20 | "-/:;()$&@\"", | ||
| 21 | "\001.,?!'\\b", | ||
| 22 | "\002\003 \004" | ||
| 23 | }; | ||
| 24 | |||
| 25 | #define CTRL_SHIFT '\001' | ||
| 26 | #define CTRL_LAYER '\002' | ||
| 27 | #define CTRL_SPACE '\003' | ||
| 28 | #define CTRL_DONE '\004' | ||
| 29 | #define CTRL_BS '\b' | ||
| 30 | |||
| 31 | void kb_state_init(kb_state_t *st) { | ||
| 32 | if (!st) return; | ||
| 33 | memset(st, 0, sizeof(*st)); | ||
| 34 | st->layer = KB_ALPHA_LOWER; | ||
| 35 | st->reveal = false; | ||
| 36 | } | ||
| 37 | |||
| 38 | static const char **get_layer(kb_layer_t layer) { | ||
| 39 | switch (layer) { | ||
| 40 | case KB_ALPHA_UPPER: return s_alpha_upper; | ||
| 41 | case KB_NUMSYM: return s_numsym; | ||
| 42 | default: return s_alpha_lower; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | static int key_is_ctrl(char c) { | ||
| 47 | return c == CTRL_SHIFT || c == CTRL_LAYER || c == CTRL_SPACE || c == CTRL_DONE || c == CTRL_BS; | ||
| 48 | } | ||
| 49 | |||
| 50 | int kb_get_row_keys(int row, kb_layer_t layer, const char **keys_out) { | ||
| 51 | if (row < 0 || row >= KB_ROW_COUNT) { | ||
| 52 | *keys_out = NULL; | ||
| 53 | return 0; | ||
| 54 | } | ||
| 55 | const char **layer_rows = get_layer(layer); | ||
| 56 | const char *row_str = layer_rows[row]; | ||
| 57 | *keys_out = row_str; | ||
| 58 | return (int)strlen(row_str); | ||
| 59 | } | ||
| 60 | |||
| 61 | static int row_x_offset(int row) { | ||
| 62 | switch (row) { | ||
| 63 | case 0: return 5; | ||
| 64 | case 1: return 14; | ||
| 65 | case 2: return 23; | ||
| 66 | case 3: return 5; | ||
| 67 | default: return 0; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | static int key_width_at(int row, int col, int total_keys) { | ||
| 72 | (void)col; | ||
| 73 | if (row == 3) { | ||
| 74 | if (col == 0) return 42; | ||
| 75 | if (col == total_keys - 1) return 50; | ||
| 76 | if (total_keys == 3 && col == 1) return 168; | ||
| 77 | if (total_keys == 4 && col == 1) return 168; | ||
| 78 | if (total_keys == 4 && col == 2) return 42; | ||
| 79 | } | ||
| 80 | return KB_KEY_W; | ||
| 81 | } | ||
| 82 | |||
| 83 | kb_result_t kb_hit_test(int tx, int ty, kb_layer_t layer) { | ||
| 84 | kb_result_t result = {KB_ACTION_NONE, 0}; | ||
| 85 | |||
| 86 | if (ty < KB_START_Y || ty >= KB_START_Y + KB_ROW_COUNT * (KB_KEY_H + KB_KEY_GAP)) { | ||
| 87 | return result; | ||
| 88 | } | ||
| 89 | |||
| 90 | int row = (ty - KB_START_Y) / (KB_KEY_H + KB_KEY_GAP); | ||
| 91 | if (row < 0 || row >= KB_ROW_COUNT) return result; | ||
| 92 | |||
| 93 | const char *row_str; | ||
| 94 | int total_keys = kb_get_row_keys(row, layer, &row_str); | ||
| 95 | if (total_keys == 0) return result; | ||
| 96 | |||
| 97 | int x_off = row_x_offset(row); | ||
| 98 | int cx = x_off; | ||
| 99 | |||
| 100 | for (int col = 0; col < total_keys; col++) { | ||
| 101 | int kw = key_width_at(row, col, total_keys); | ||
| 102 | if (tx >= cx && tx < cx + kw) { | ||
| 103 | char c = row_str[col]; | ||
| 104 | if (c == CTRL_SHIFT) { | ||
| 105 | result.action = KB_ACTION_SHIFT; | ||
| 106 | } else if (c == CTRL_LAYER) { | ||
| 107 | result.action = KB_ACTION_LAYER; | ||
| 108 | } else if (c == CTRL_SPACE) { | ||
| 109 | result.action = KB_ACTION_SPACE; | ||
| 110 | result.ch = ' '; | ||
| 111 | } else if (c == CTRL_DONE) { | ||
| 112 | result.action = KB_ACTION_DONE; | ||
| 113 | } else if (c == CTRL_BS) { | ||
| 114 | result.action = KB_ACTION_BACKSPACE; | ||
| 115 | } else { | ||
| 116 | result.action = KB_ACTION_CHAR; | ||
| 117 | result.ch = c; | ||
| 118 | } | ||
| 119 | return result; | ||
| 120 | } | ||
| 121 | cx += kw + KB_KEY_GAP; | ||
| 122 | } | ||
| 123 | |||
| 124 | return result; | ||
| 125 | } | ||
| 126 | |||
| 127 | void kb_apply(kb_state_t *st, kb_result_t result) { | ||
| 128 | if (!st || result.action == KB_ACTION_NONE) return; | ||
| 129 | |||
| 130 | switch (result.action) { | ||
| 131 | case KB_ACTION_CHAR: | ||
| 132 | if (st->cursor < KB_INPUT_MAX) { | ||
| 133 | st->input[st->cursor++] = result.ch; | ||
| 134 | st->input[st->cursor] = '\0'; | ||
| 135 | } | ||
| 136 | break; | ||
| 137 | case KB_ACTION_BACKSPACE: | ||
| 138 | if (st->cursor > 0) { | ||
| 139 | st->cursor--; | ||
| 140 | st->input[st->cursor] = '\0'; | ||
| 141 | } | ||
| 142 | break; | ||
| 143 | case KB_ACTION_SHIFT: | ||
| 144 | if (st->layer == KB_ALPHA_LOWER) st->layer = KB_ALPHA_UPPER; | ||
| 145 | else if (st->layer == KB_ALPHA_UPPER) st->layer = KB_ALPHA_LOWER; | ||
| 146 | break; | ||
| 147 | case KB_ACTION_LAYER: | ||
| 148 | if (st->layer == KB_NUMSYM) st->layer = KB_ALPHA_LOWER; | ||
| 149 | else st->layer = KB_NUMSYM; | ||
| 150 | break; | ||
| 151 | case KB_ACTION_SPACE: | ||
| 152 | if (st->cursor < KB_INPUT_MAX) { | ||
| 153 | st->input[st->cursor++] = ' '; | ||
| 154 | st->input[st->cursor] = '\0'; | ||
| 155 | } | ||
| 156 | break; | ||
| 157 | case KB_ACTION_DONE: | ||
| 158 | break; | ||
| 159 | default: | ||
| 160 | break; | ||
| 161 | } | ||
| 162 | } | ||
diff --git a/main/keyboard.h b/main/keyboard.h new file mode 100644 index 0000000..d7b3400 --- /dev/null +++ b/main/keyboard.h | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | #ifndef KEYBOARD_H | ||
| 2 | #define KEYBOARD_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdbool.h> | ||
| 6 | |||
| 7 | #define KB_INPUT_MAX 64 | ||
| 8 | #define KB_KEY_W 28 | ||
| 9 | #define KB_KEY_H 36 | ||
| 10 | #define KB_KEY_GAP 2 | ||
| 11 | #define KB_ROW_COUNT 4 | ||
| 12 | #define KB_START_Y 310 | ||
| 13 | |||
| 14 | typedef enum { | ||
| 15 | KB_ALPHA_LOWER, | ||
| 16 | KB_ALPHA_UPPER, | ||
| 17 | KB_NUMSYM | ||
| 18 | } kb_layer_t; | ||
| 19 | |||
| 20 | typedef enum { | ||
| 21 | KB_ACTION_NONE = 0, | ||
| 22 | KB_ACTION_CHAR, | ||
| 23 | KB_ACTION_SHIFT, | ||
| 24 | KB_ACTION_BACKSPACE, | ||
| 25 | KB_ACTION_DONE, | ||
| 26 | KB_ACTION_LAYER, | ||
| 27 | KB_ACTION_SPACE | ||
| 28 | } kb_action_t; | ||
| 29 | |||
| 30 | typedef struct { | ||
| 31 | kb_action_t action; | ||
| 32 | char ch; | ||
| 33 | } kb_result_t; | ||
| 34 | |||
| 35 | typedef struct { | ||
| 36 | char input[KB_INPUT_MAX + 1]; | ||
| 37 | int cursor; | ||
| 38 | bool reveal; | ||
| 39 | kb_layer_t layer; | ||
| 40 | } kb_state_t; | ||
| 41 | |||
| 42 | void kb_state_init(kb_state_t *st); | ||
| 43 | int kb_get_row_keys(int row, kb_layer_t layer, const char **keys_out); | ||
| 44 | kb_result_t kb_hit_test(int tx, int ty, kb_layer_t layer); | ||
| 45 | void kb_apply(kb_state_t *st, kb_result_t result); | ||
| 46 | |||
| 47 | #endif | ||