2 Copyright 2013 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "action_util.h"
23 static inline void add_key_byte(uint8_t code);
24 static inline void del_key_byte(uint8_t code);
26 static inline void add_key_bit(uint8_t code);
27 static inline void del_key_bit(uint8_t code);
30 static uint8_t real_mods = 0;
31 static uint8_t weak_mods = 0;
32 static uint8_t macro_mods = 0;
34 #ifdef USB_6KRO_ENABLE
35 #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
36 #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
37 #define RO_INC(a) RO_ADD(a, 1)
38 #define RO_DEC(a) RO_SUB(a, 1)
39 static int8_t cb_head = 0;
40 static int8_t cb_tail = 0;
41 static int8_t cb_count = 0;
44 // TODO: pointer variable is not needed
45 //report_keyboard_t keyboard_report = {};
46 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
48 #ifndef NO_ACTION_ONESHOT
49 static int8_t oneshot_mods = 0;
50 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
51 static int16_t oneshot_time = 0;
56 void send_keyboard_report(void) {
57 keyboard_report->mods = real_mods;
58 keyboard_report->mods |= weak_mods;
59 keyboard_report->mods |= macro_mods;
60 #ifndef NO_ACTION_ONESHOT
62 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
63 if (TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT) {
64 dprintf("Oneshot: timeout\n");
68 keyboard_report->mods |= oneshot_mods;
74 host_keyboard_send(keyboard_report);
78 void add_key(uint8_t key)
81 if (keyboard_protocol && keyboard_nkro) {
89 void del_key(uint8_t key)
92 if (keyboard_protocol && keyboard_nkro) {
100 void clear_keys(void)
103 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
104 keyboard_report->raw[i] = 0;
110 uint8_t get_mods(void) { return real_mods; }
111 void add_mods(uint8_t mods) { real_mods |= mods; }
112 void del_mods(uint8_t mods) { real_mods &= ~mods; }
113 void set_mods(uint8_t mods) { real_mods = mods; }
114 void clear_mods(void) { real_mods = 0; }
117 uint8_t get_weak_mods(void) { return weak_mods; }
118 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
119 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
120 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
121 void clear_weak_mods(void) { weak_mods = 0; }
124 uint8_t get_macro_mods(void) { return macro_mods; }
125 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
126 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
127 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
128 void clear_macro_mods(void) { macro_mods = 0; }
130 /* Oneshot modifier */
131 #ifndef NO_ACTION_ONESHOT
132 void set_oneshot_mods(uint8_t mods)
135 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
136 oneshot_time = timer_read();
139 void clear_oneshot_mods(void)
142 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
152 * inspect keyboard state
154 uint8_t has_anykey(void)
157 for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
158 if (keyboard_report->raw[i])
164 uint8_t has_anymod(void)
166 return bitpop(real_mods);
169 uint8_t get_first_key(void)
172 if (keyboard_protocol && keyboard_nkro) {
174 for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
176 return i<<3 | biton(keyboard_report->nkro.bits[i]);
179 #ifdef USB_6KRO_ENABLE
182 if (keyboard_report->keys[i] != 0) {
186 } while (i != cb_tail);
187 return keyboard_report->keys[i];
189 return keyboard_report->keys[0];
195 /* local functions */
196 static inline void add_key_byte(uint8_t code)
198 #ifdef USB_6KRO_ENABLE
203 if (keyboard_report->keys[i] == code) {
206 if (empty == -1 && keyboard_report->keys[i] == 0) {
210 } while (i != cb_tail);
212 if (cb_tail == cb_head) {
215 // pop head when has no empty space
216 cb_head = RO_INC(cb_head);
220 // left shift when has empty space
224 if (keyboard_report->keys[i] != 0) {
225 keyboard_report->keys[empty] = keyboard_report->keys[i];
226 keyboard_report->keys[i] = 0;
227 empty = RO_INC(empty);
233 } while (i != cb_tail);
234 cb_tail = RO_SUB(cb_tail, offset);
240 keyboard_report->keys[cb_tail] = code;
241 cb_tail = RO_INC(cb_tail);
246 for (; i < KEYBOARD_REPORT_KEYS; i++) {
247 if (keyboard_report->keys[i] == code) {
250 if (empty == -1 && keyboard_report->keys[i] == 0) {
254 if (i == KEYBOARD_REPORT_KEYS) {
256 keyboard_report->keys[empty] = code;
262 static inline void del_key_byte(uint8_t code)
264 #ifdef USB_6KRO_ENABLE
268 if (keyboard_report->keys[i] == code) {
269 keyboard_report->keys[i] = 0;
272 // reset head and tail
273 cb_tail = cb_head = 0;
275 if (i == RO_DEC(cb_tail)) {
276 // left shift when next to tail
278 cb_tail = RO_DEC(cb_tail);
279 if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
282 } while (cb_tail != cb_head);
287 } while (i != cb_tail);
290 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
291 if (keyboard_report->keys[i] == code) {
292 keyboard_report->keys[i] = 0;
299 static inline void add_key_bit(uint8_t code)
301 if ((code>>3) < KEYBOARD_REPORT_BITS) {
302 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
304 dprintf("add_key_bit: can't add: %02X\n", code);
308 static inline void del_key_bit(uint8_t code)
310 if ((code>>3) < KEYBOARD_REPORT_BITS) {
311 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
313 dprintf("del_key_bit: can't del: %02X\n", code);