upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/main/keyboard.c
blob: 741bd087ceed52c7f28f954de69e78916c1d8081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "keyboard.h"
#include <string.h>

static const char *s_alpha_lower[] = {
    "qwertyuiop",
    "asdfghjkl",
    "\001zxcvbnm\b",
    "\002\003 \004"
};

static const char *s_alpha_upper[] = {
    "QWERTYUIOP",
    "ASDFGHJKL",
    "\001ZXCVBNM\b",
    "\002\003 \004"
};

static const char *s_numsym[] = {
    "1234567890",
    "-/:;()$&@\"",
    "\001.,?!'\\b",
    "\002\003 \004"
};

#define CTRL_SHIFT  '\001'
#define CTRL_LAYER  '\002'
#define CTRL_SPACE  '\003'
#define CTRL_DONE   '\004'
#define CTRL_BS     '\b'

void kb_state_init(kb_state_t *st) {
    if (!st) return;
    memset(st, 0, sizeof(*st));
    st->layer = KB_ALPHA_LOWER;
    st->reveal = false;
}

static const char **get_layer(kb_layer_t layer) {
    switch (layer) {
        case KB_ALPHA_UPPER: return s_alpha_upper;
        case KB_NUMSYM:      return s_numsym;
        default:             return s_alpha_lower;
    }
}

static int key_is_ctrl(char c) {
    return c == CTRL_SHIFT || c == CTRL_LAYER || c == CTRL_SPACE || c == CTRL_DONE || c == CTRL_BS;
}

int kb_get_row_keys(int row, kb_layer_t layer, const char **keys_out) {
    if (row < 0 || row >= KB_ROW_COUNT) {
        *keys_out = NULL;
        return 0;
    }
    const char **layer_rows = get_layer(layer);
    const char *row_str = layer_rows[row];
    *keys_out = row_str;
    return (int)strlen(row_str);
}

static int row_x_offset(int row) {
    switch (row) {
        case 0: return 5;
        case 1: return 14;
        case 2: return 23;
        case 3: return 5;
        default: return 0;
    }
}

static int key_width_at(int row, int col, int total_keys) {
    (void)col;
    if (row == 3) {
        if (col == 0) return 42;
        if (col == total_keys - 1) return 50;
        if (total_keys == 3 && col == 1) return 168;
        if (total_keys == 4 && col == 1) return 168;
        if (total_keys == 4 && col == 2) return 42;
    }
    return KB_KEY_W;
}

kb_result_t kb_hit_test(int tx, int ty, kb_layer_t layer) {
    kb_result_t result = {KB_ACTION_NONE, 0};

    if (ty < KB_START_Y || ty >= KB_START_Y + KB_ROW_COUNT * (KB_KEY_H + KB_KEY_GAP)) {
        return result;
    }

    int row = (ty - KB_START_Y) / (KB_KEY_H + KB_KEY_GAP);
    if (row < 0 || row >= KB_ROW_COUNT) return result;

    const char *row_str;
    int total_keys = kb_get_row_keys(row, layer, &row_str);
    if (total_keys == 0) return result;

    int x_off = row_x_offset(row);
    int cx = x_off;

    for (int col = 0; col < total_keys; col++) {
        int kw = key_width_at(row, col, total_keys);
        if (tx >= cx && tx < cx + kw) {
            char c = row_str[col];
            if (c == CTRL_SHIFT) {
                result.action = KB_ACTION_SHIFT;
            } else if (c == CTRL_LAYER) {
                result.action = KB_ACTION_LAYER;
            } else if (c == CTRL_SPACE) {
                result.action = KB_ACTION_SPACE;
                result.ch = ' ';
            } else if (c == CTRL_DONE) {
                result.action = KB_ACTION_DONE;
            } else if (c == CTRL_BS) {
                result.action = KB_ACTION_BACKSPACE;
            } else {
                result.action = KB_ACTION_CHAR;
                result.ch = c;
            }
            return result;
        }
        cx += kw + KB_KEY_GAP;
    }

    return result;
}

void kb_apply(kb_state_t *st, kb_result_t result) {
    if (!st || result.action == KB_ACTION_NONE) return;

    switch (result.action) {
        case KB_ACTION_CHAR:
            if (st->cursor < KB_INPUT_MAX) {
                st->input[st->cursor++] = result.ch;
                st->input[st->cursor] = '\0';
            }
            break;
        case KB_ACTION_BACKSPACE:
            if (st->cursor > 0) {
                st->cursor--;
                st->input[st->cursor] = '\0';
            }
            break;
        case KB_ACTION_SHIFT:
            if (st->layer == KB_ALPHA_LOWER) st->layer = KB_ALPHA_UPPER;
            else if (st->layer == KB_ALPHA_UPPER) st->layer = KB_ALPHA_LOWER;
            break;
        case KB_ACTION_LAYER:
            if (st->layer == KB_NUMSYM) st->layer = KB_ALPHA_LOWER;
            else st->layer = KB_NUMSYM;
            break;
        case KB_ACTION_SPACE:
            if (st->cursor < KB_INPUT_MAX) {
                st->input[st->cursor++] = ' ';
                st->input[st->cursor] = '\0';
            }
            break;
        case KB_ACTION_DONE:
            break;
        default:
            break;
    }
}