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;
33 #ifdef USB_6KRO_ENABLE
34 #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
35 #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
36 #define RO_INC(a) RO_ADD(a, 1)
37 #define RO_DEC(a) RO_SUB(a, 1)
38 static int8_t cb_head = 0;
39 static int8_t cb_tail = 0;
40 static int8_t cb_count = 0;
43 // TODO: pointer variable is not needed
44 //report_keyboard_t keyboard_report = {};
45 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
47 #ifndef NO_ACTION_ONESHOT
48 static int8_t oneshot_mods = 0;
49 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
50 static int16_t oneshot_time = 0;
55 void send_keyboard_report(void) {
56 keyboard_report->mods = real_mods;
57 keyboard_report->mods |= weak_mods;
58 #ifndef NO_ACTION_ONESHOT
60 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
61 if (TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT) {
62 dprintf("Oneshot: timeout\n");
66 keyboard_report->mods |= oneshot_mods;
72 host_keyboard_send(keyboard_report);
76 void add_key(uint8_t key)
87 void del_key(uint8_t key)
101 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
102 keyboard_report->raw[i] = 0;
108 uint8_t get_mods(void) { return real_mods; }
109 void add_mods(uint8_t mods) { real_mods |= mods; }
110 void del_mods(uint8_t mods) { real_mods &= ~mods; }
111 void set_mods(uint8_t mods) { real_mods = mods; }
112 void clear_mods(void) { real_mods = 0; }
115 uint8_t get_weak_mods(void) { return weak_mods; }
116 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
117 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
118 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
119 void clear_weak_mods(void) { weak_mods = 0; }
121 /* Oneshot modifier */
122 #ifndef NO_ACTION_ONESHOT
123 void set_oneshot_mods(uint8_t mods)
126 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
127 oneshot_time = timer_read();
130 void clear_oneshot_mods(void)
133 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
143 * inspect keyboard state
145 uint8_t has_anykey(void)
148 for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
149 if (keyboard_report->raw[i])
155 uint8_t has_anymod(void)
157 return bitpop(real_mods);
160 uint8_t get_first_key(void)
165 for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
167 return i<<3 | biton(keyboard_report->nkro.bits[i]);
170 #ifdef USB_6KRO_ENABLE
173 if (keyboard_report->keys[i] != 0) {
177 } while (i != cb_tail);
178 return keyboard_report->keys[i];
180 return keyboard_report->keys[0];
186 /* local functions */
187 static inline void add_key_byte(uint8_t code)
189 #ifdef USB_6KRO_ENABLE
194 if (keyboard_report->keys[i] == code) {
197 if (empty == -1 && keyboard_report->keys[i] == 0) {
201 } while (i != cb_tail);
203 if (cb_tail == cb_head) {
206 // pop head when has no empty space
207 cb_head = RO_INC(cb_head);
211 // left shift when has empty space
215 if (keyboard_report->keys[i] != 0) {
216 keyboard_report->keys[empty] = keyboard_report->keys[i];
217 keyboard_report->keys[i] = 0;
218 empty = RO_INC(empty);
224 } while (i != cb_tail);
225 cb_tail = RO_SUB(cb_tail, offset);
231 keyboard_report->keys[cb_tail] = code;
232 cb_tail = RO_INC(cb_tail);
237 for (; i < KEYBOARD_REPORT_KEYS; i++) {
238 if (keyboard_report->keys[i] == code) {
241 if (empty == -1 && keyboard_report->keys[i] == 0) {
245 if (i == KEYBOARD_REPORT_KEYS) {
247 keyboard_report->keys[empty] = code;
253 static inline void del_key_byte(uint8_t code)
255 #ifdef USB_6KRO_ENABLE
259 if (keyboard_report->keys[i] == code) {
260 keyboard_report->keys[i] = 0;
263 // reset head and tail
264 cb_tail = cb_head = 0;
266 if (i == RO_DEC(cb_tail)) {
267 // left shift when next to tail
269 cb_tail = RO_DEC(cb_tail);
270 if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
273 } while (cb_tail != cb_head);
278 } while (i != cb_tail);
281 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
282 if (keyboard_report->keys[i] == code) {
283 keyboard_report->keys[i] = 0;
290 static inline void add_key_bit(uint8_t code)
292 if ((code>>3) < KEYBOARD_REPORT_BITS) {
293 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
295 dprintf("add_key_bit: can't add: %02X\n", code);
299 static inline void del_key_bit(uint8_t code)
301 if ((code>>3) < KEYBOARD_REPORT_BITS) {
302 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
304 dprintf("del_key_bit: can't del: %02X\n", code);