diff options
Diffstat (limited to 'tmk_core/protocol/report.c')
-rw-r--r-- | tmk_core/protocol/report.c | 280 |
1 files changed, 280 insertions, 0 deletions
diff --git a/tmk_core/protocol/report.c b/tmk_core/protocol/report.c new file mode 100644 index 000000000..854b59ae4 --- /dev/null +++ b/tmk_core/protocol/report.c | |||
@@ -0,0 +1,280 @@ | |||
1 | /* Copyright 2017 Fred Sundvik | ||
2 | * | ||
3 | * This program is free software: you can redistribute it and/or modify | ||
4 | * it under the terms of the GNU General Public License as published by | ||
5 | * the Free Software Foundation, either version 2 of the License, or | ||
6 | * (at your option) any later version. | ||
7 | * | ||
8 | * This program is distributed in the hope that it will be useful, | ||
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
11 | * GNU General Public License for more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include "report.h" | ||
18 | #include "host.h" | ||
19 | #include "keycode_config.h" | ||
20 | #include "debug.h" | ||
21 | #include "util.h" | ||
22 | #include <string.h> | ||
23 | |||
24 | #ifdef RING_BUFFERED_6KRO_REPORT_ENABLE | ||
25 | # define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS) | ||
26 | # define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS) | ||
27 | # define RO_INC(a) RO_ADD(a, 1) | ||
28 | # define RO_DEC(a) RO_SUB(a, 1) | ||
29 | static int8_t cb_head = 0; | ||
30 | static int8_t cb_tail = 0; | ||
31 | static int8_t cb_count = 0; | ||
32 | #endif | ||
33 | |||
34 | /** \brief has_anykey | ||
35 | * | ||
36 | * FIXME: Needs doc | ||
37 | */ | ||
38 | uint8_t has_anykey(report_keyboard_t* keyboard_report) { | ||
39 | uint8_t cnt = 0; | ||
40 | uint8_t* p = keyboard_report->keys; | ||
41 | uint8_t lp = sizeof(keyboard_report->keys); | ||
42 | #ifdef NKRO_ENABLE | ||
43 | if (keyboard_protocol && keymap_config.nkro) { | ||
44 | p = keyboard_report->nkro.bits; | ||
45 | lp = sizeof(keyboard_report->nkro.bits); | ||
46 | } | ||
47 | #endif | ||
48 | while (lp--) { | ||
49 | if (*p++) cnt++; | ||
50 | } | ||
51 | return cnt; | ||
52 | } | ||
53 | |||
54 | /** \brief get_first_key | ||
55 | * | ||
56 | * FIXME: Needs doc | ||
57 | */ | ||
58 | uint8_t get_first_key(report_keyboard_t* keyboard_report) { | ||
59 | #ifdef NKRO_ENABLE | ||
60 | if (keyboard_protocol && keymap_config.nkro) { | ||
61 | uint8_t i = 0; | ||
62 | for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++) | ||
63 | ; | ||
64 | return i << 3 | biton(keyboard_report->nkro.bits[i]); | ||
65 | } | ||
66 | #endif | ||
67 | #ifdef RING_BUFFERED_6KRO_REPORT_ENABLE | ||
68 | uint8_t i = cb_head; | ||
69 | do { | ||
70 | if (keyboard_report->keys[i] != 0) { | ||
71 | break; | ||
72 | } | ||
73 | i = RO_INC(i); | ||
74 | } while (i != cb_tail); | ||
75 | return keyboard_report->keys[i]; | ||
76 | #else | ||
77 | return keyboard_report->keys[0]; | ||
78 | #endif | ||
79 | } | ||
80 | |||
81 | /** \brief Checks if a key is pressed in the report | ||
82 | * | ||
83 | * Returns true if the keyboard_report reports that the key is pressed, otherwise false | ||
84 | * Note: The function doesn't support modifers currently, and it returns false for KC_NO | ||
85 | */ | ||
86 | bool is_key_pressed(report_keyboard_t* keyboard_report, uint8_t key) { | ||
87 | if (key == KC_NO) { | ||
88 | return false; | ||
89 | } | ||
90 | #ifdef NKRO_ENABLE | ||
91 | if (keyboard_protocol && keymap_config.nkro) { | ||
92 | if ((key >> 3) < KEYBOARD_REPORT_BITS) { | ||
93 | return keyboard_report->nkro.bits[key >> 3] & 1 << (key & 7); | ||
94 | } else { | ||
95 | return false; | ||
96 | } | ||
97 | } | ||
98 | #endif | ||
99 | for (int i = 0; i < KEYBOARD_REPORT_KEYS; i++) { | ||
100 | if (keyboard_report->keys[i] == key) { | ||
101 | return true; | ||
102 | } | ||
103 | } | ||
104 | return false; | ||
105 | } | ||
106 | |||
107 | /** \brief add key byte | ||
108 | * | ||
109 | * FIXME: Needs doc | ||
110 | */ | ||
111 | void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { | ||
112 | #ifdef RING_BUFFERED_6KRO_REPORT_ENABLE | ||
113 | int8_t i = cb_head; | ||
114 | int8_t empty = -1; | ||
115 | if (cb_count) { | ||
116 | do { | ||
117 | if (keyboard_report->keys[i] == code) { | ||
118 | return; | ||
119 | } | ||
120 | if (empty == -1 && keyboard_report->keys[i] == 0) { | ||
121 | empty = i; | ||
122 | } | ||
123 | i = RO_INC(i); | ||
124 | } while (i != cb_tail); | ||
125 | if (i == cb_tail) { | ||
126 | if (cb_tail == cb_head) { | ||
127 | // buffer is full | ||
128 | if (empty == -1) { | ||
129 | // pop head when has no empty space | ||
130 | cb_head = RO_INC(cb_head); | ||
131 | cb_count--; | ||
132 | } else { | ||
133 | // left shift when has empty space | ||
134 | uint8_t offset = 1; | ||
135 | i = RO_INC(empty); | ||
136 | do { | ||
137 | if (keyboard_report->keys[i] != 0) { | ||
138 | keyboard_report->keys[empty] = keyboard_report->keys[i]; | ||
139 | keyboard_report->keys[i] = 0; | ||
140 | empty = RO_INC(empty); | ||
141 | } else { | ||
142 | offset++; | ||
143 | } | ||
144 | i = RO_INC(i); | ||
145 | } while (i != cb_tail); | ||
146 | cb_tail = RO_SUB(cb_tail, offset); | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | // add to tail | ||
152 | keyboard_report->keys[cb_tail] = code; | ||
153 | cb_tail = RO_INC(cb_tail); | ||
154 | cb_count++; | ||
155 | #else | ||
156 | int8_t i = 0; | ||
157 | int8_t empty = -1; | ||
158 | for (; i < KEYBOARD_REPORT_KEYS; i++) { | ||
159 | if (keyboard_report->keys[i] == code) { | ||
160 | break; | ||
161 | } | ||
162 | if (empty == -1 && keyboard_report->keys[i] == 0) { | ||
163 | empty = i; | ||
164 | } | ||
165 | } | ||
166 | if (i == KEYBOARD_REPORT_KEYS) { | ||
167 | if (empty != -1) { | ||
168 | keyboard_report->keys[empty] = code; | ||
169 | } | ||
170 | } | ||
171 | #endif | ||
172 | } | ||
173 | |||
174 | /** \brief del key byte | ||
175 | * | ||
176 | * FIXME: Needs doc | ||
177 | */ | ||
178 | void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code) { | ||
179 | #ifdef RING_BUFFERED_6KRO_REPORT_ENABLE | ||
180 | uint8_t i = cb_head; | ||
181 | if (cb_count) { | ||
182 | do { | ||
183 | if (keyboard_report->keys[i] == code) { | ||
184 | keyboard_report->keys[i] = 0; | ||
185 | cb_count--; | ||
186 | if (cb_count == 0) { | ||
187 | // reset head and tail | ||
188 | cb_tail = cb_head = 0; | ||
189 | } | ||
190 | if (i == RO_DEC(cb_tail)) { | ||
191 | // left shift when next to tail | ||
192 | do { | ||
193 | cb_tail = RO_DEC(cb_tail); | ||
194 | if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) { | ||
195 | break; | ||
196 | } | ||
197 | } while (cb_tail != cb_head); | ||
198 | } | ||
199 | break; | ||
200 | } | ||
201 | i = RO_INC(i); | ||
202 | } while (i != cb_tail); | ||
203 | } | ||
204 | #else | ||
205 | for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) { | ||
206 | if (keyboard_report->keys[i] == code) { | ||
207 | keyboard_report->keys[i] = 0; | ||
208 | } | ||
209 | } | ||
210 | #endif | ||
211 | } | ||
212 | |||
213 | #ifdef NKRO_ENABLE | ||
214 | /** \brief add key bit | ||
215 | * | ||
216 | * FIXME: Needs doc | ||
217 | */ | ||
218 | void add_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { | ||
219 | if ((code >> 3) < KEYBOARD_REPORT_BITS) { | ||
220 | keyboard_report->nkro.bits[code >> 3] |= 1 << (code & 7); | ||
221 | } else { | ||
222 | dprintf("add_key_bit: can't add: %02X\n", code); | ||
223 | } | ||
224 | } | ||
225 | |||
226 | /** \brief del key bit | ||
227 | * | ||
228 | * FIXME: Needs doc | ||
229 | */ | ||
230 | void del_key_bit(report_keyboard_t* keyboard_report, uint8_t code) { | ||
231 | if ((code >> 3) < KEYBOARD_REPORT_BITS) { | ||
232 | keyboard_report->nkro.bits[code >> 3] &= ~(1 << (code & 7)); | ||
233 | } else { | ||
234 | dprintf("del_key_bit: can't del: %02X\n", code); | ||
235 | } | ||
236 | } | ||
237 | #endif | ||
238 | |||
239 | /** \brief add key to report | ||
240 | * | ||
241 | * FIXME: Needs doc | ||
242 | */ | ||
243 | void add_key_to_report(report_keyboard_t* keyboard_report, uint8_t key) { | ||
244 | #ifdef NKRO_ENABLE | ||
245 | if (keyboard_protocol && keymap_config.nkro) { | ||
246 | add_key_bit(keyboard_report, key); | ||
247 | return; | ||
248 | } | ||
249 | #endif | ||
250 | add_key_byte(keyboard_report, key); | ||
251 | } | ||
252 | |||
253 | /** \brief del key from report | ||
254 | * | ||
255 | * FIXME: Needs doc | ||
256 | */ | ||
257 | void del_key_from_report(report_keyboard_t* keyboard_report, uint8_t key) { | ||
258 | #ifdef NKRO_ENABLE | ||
259 | if (keyboard_protocol && keymap_config.nkro) { | ||
260 | del_key_bit(keyboard_report, key); | ||
261 | return; | ||
262 | } | ||
263 | #endif | ||
264 | del_key_byte(keyboard_report, key); | ||
265 | } | ||
266 | |||
267 | /** \brief clear key from report | ||
268 | * | ||
269 | * FIXME: Needs doc | ||
270 | */ | ||
271 | void clear_keys_from_report(report_keyboard_t* keyboard_report) { | ||
272 | // not clear mods | ||
273 | #ifdef NKRO_ENABLE | ||
274 | if (keyboard_protocol && keymap_config.nkro) { | ||
275 | memset(keyboard_report->nkro.bits, 0, sizeof(keyboard_report->nkro.bits)); | ||
276 | return; | ||
277 | } | ||
278 | #endif | ||
279 | memset(keyboard_report->keys, 0, sizeof(keyboard_report->keys)); | ||
280 | } | ||