forked from Ikcelaks/qmk_sequence_transform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeybuffer.c
More file actions
195 lines (189 loc) · 6.03 KB
/
keybuffer.c
File metadata and controls
195 lines (189 loc) · 6.03 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2024 Guillaume Stordeur <guillaume.stordeur@gmail.com>
// Copyright 2024 Matt Skalecki <ikcelaks@gmail.com>
// Copyright 2024 QKekos <q.kekos.q@gmail.com>
// SPDX-License-Identifier: Apache-2.0
#include "st_defaults.h"
#include "qmk_wrapper.h"
#include "st_debug.h"
#include "triecodes.h"
#include "keybuffer.h"
#include "utils.h"
#include <ctype.h>
#define TRIECODE_AT(i) st_key_buffer_get_triecode(buf, (i))
//////////////////////////////////////////////////////////////////
// Public
//////////////////////////////////////////////////////////////////
// Most recent keypress data is at index 0.
// Positive indexes move back towards older keypresses
// Negative indexes start at the oldest keypress still in the
// buffer and move towards more recent presses
// If you type "abc" then:
// st_key_buffer_get(buf, 0) -> c
// st_key_buffer_get(buf, 1) -> b
// st_key_buffer_get(buf, 2) -> a
// st_key_buffer_get(buf, -1) -> a
// st_key_buffer_get(buf, -2) -> b
// st_key_buffer_get(buf, -3) -> c
// returns KC_NO if index is out of bounds
uint8_t st_key_buffer_get_triecode(const st_key_buffer_t *buf, int index)
{
const st_key_action_t *keyaction = st_key_buffer_get(buf, index);
return (keyaction ? keyaction->triecode : '\0');
}
/**
* @brief Gets an st_key_action_t from the `index` position in the key_buffer
* @param buf st_key_buffer* receives the st_key_action
* @param index int index starting with 0 as the most recent keypress and increasing for older keypresses
*
* @return true if the index points to a valid key_action in the buffer
* @return false if index is out-of-bounds
*/
st_key_action_t *st_key_buffer_get(const st_key_buffer_t *buf, int index)
{
if (index < 0) {
index += buf->size;
}
if (index >= buf->size || index < 0) {
return NULL;
}
int buf_index = buf->head - index;
if (buf_index < 0) {
buf_index += buf->capacity;
}
return &buf->data[buf_index];
}
//////////////////////////////////////////////////////////////////
void st_key_buffer_reset(st_key_buffer_t *buf)
{
buf->size = 0;
buf->seq_ref_size = 0;
st_key_buffer_push(buf, ' ');
}
//////////////////////////////////////////////////////////////////
void st_key_buffer_push(st_key_buffer_t *buf, uint8_t triecode)
{
// Store all alpha chars as lowercase
if (buf->size < buf->capacity) {
buf->size++;
}
if (++buf->head >= buf->capacity) { // increment cur_pos
buf->head = 0; // wrap to 0
}
buf->data[buf->head].triecode = tolower(triecode);
buf->data[buf->head].action_taken = ST_DEFAULT_KEY_ACTION;
st_key_buffer_push_seq_ref(buf, '\0');
}
//////////////////////////////////////////////////////////////////
void st_key_buffer_pop(st_key_buffer_t *buf)
{
if (buf->size <= 1) {
st_key_buffer_reset(buf);
return;
} else {
buf->size--;
}
buf->head--;
if (buf->head < 0) {
buf->head += buf->capacity;
}
while (buf->seq_ref_cache[buf->seq_ref_head]) {
buf->seq_ref_size--;
buf->seq_ref_head--;
if (buf->seq_ref_size < 1) {
return;
}
if (buf->seq_ref_head < 0) {
buf->seq_ref_head += buf->seq_ref_capacity;
}
}
if (buf->seq_ref_size > 0) {
buf->seq_ref_size--;
buf->seq_ref_head--;
}
if (buf->seq_ref_head < 0) {
buf->seq_ref_head += buf->seq_ref_capacity;
}
}
//////////////////////////////////////////////////////////////////
void st_key_buffer_print(const st_key_buffer_t *buf)
{
#ifndef NO_PRINT
uprintf("buffer: |");
for (int i = -1; i >= -buf->size; --i) {
const uint8_t code = TRIECODE_AT(i);
#ifdef ST_TESTER
const char *token = st_get_seq_token_utf8(code);
if (token) {
uprintf("%s", token);
} else {
uprintf("%c", code);
}
#else
const char c = st_triecode_to_ascii(code);
uprintf("%c", c);
#endif
}
uprintf("| (%d)\n", buf->size);
#endif
}
//////////////////////////////////////////////////////////////////
void st_key_buffer_push_seq_ref(st_key_buffer_t *buf, uint8_t triecode)
{
// Store all alpha chars as lowercase
if (buf->seq_ref_head < buf->seq_ref_capacity) {
buf->seq_ref_size++;
}
if (++buf->seq_ref_head >= buf->seq_ref_capacity) { // increment cur_pos
buf->seq_ref_head = 0; // wrap to 0
}
buf->seq_ref_cache[buf->seq_ref_head] = triecode;
}
//////////////////////////////////////////////////////////////////
uint8_t st_key_buffer_get_seq_ref(const st_key_buffer_t * const buf, int index)
{
if (index >= buf->seq_ref_size) {
return '\0';
}
int i = buf->seq_ref_head - index;
if (i < 0) {
i += buf->seq_ref_capacity;
}
return buf->seq_ref_cache[i];
}
//////////////////////////////////////////////////////////////////
bool st_key_buffer_advance_seq_ref_index(const st_key_buffer_t * const buf, int *index)
{
while (st_key_buffer_get_seq_ref(buf, *index) != '\0') {
++*index;
}
++*index;
return *index < buf->seq_ref_size;
}
//////////////////////////////////////////////////////////////////////
// These are (currently) only used by the tester,
// so let's not compile them into the firmware.
#ifdef ST_TESTER
//////////////////////////////////////////////////////////////////////
// returns true if there are any sequence tokens in the buffer
// before the most recent key
bool st_key_buffer_has_unexpanded_seq(st_key_buffer_t *buf)
{
for (int i = 1; i < buf->size; ++i) {
const uint8_t code = TRIECODE_AT(i);
if (st_is_seq_token_triecode(code)) {
return true;
}
}
return false;
}
//////////////////////////////////////////////////////////////////
// Converts keys from buffer to null terminated ascii string
void st_key_buffer_to_ascii_str(const st_key_buffer_t *buf, char *str)
{
for (int i = -1; i >= -buf->size; --i) {
const uint8_t code = TRIECODE_AT(i);
*str++ = st_triecode_to_ascii(code);
}
*str = 0;
}
#endif // ST_TESTER