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"
21 #include "action_layer.h"
24 static inline void add_key_byte(uint8_t code);
25 static inline void del_key_byte(uint8_t code);
27 static inline void add_key_bit(uint8_t code);
28 static inline void del_key_bit(uint8_t code);
31 static uint8_t real_mods = 0;
32 static uint8_t weak_mods = 0;
33 static uint8_t macro_mods = 0;
35 #ifdef USB_6KRO_ENABLE
36 #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
37 #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
38 #define RO_INC(a) RO_ADD(a, 1)
39 #define RO_DEC(a) RO_SUB(a, 1)
40 static int8_t cb_head = 0;
41 static int8_t cb_tail = 0;
42 static int8_t cb_count = 0;
45 // TODO: pointer variable is not needed
46 //report_keyboard_t keyboard_report = {};
47 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
49 #ifndef NO_ACTION_ONESHOT
50 static int8_t oneshot_mods = 0;
51 static int8_t oneshot_locked_mods = 0;
52 int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
53 void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
54 void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
55 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
56 static int16_t oneshot_time = 0;
57 inline bool has_oneshot_mods_timed_out() {
58 return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
64 #ifndef NO_ACTION_ONESHOT
65 /* oneshot_layer_data bits
69 * S => oneshot state bits
71 static int8_t oneshot_layer_data = 0;
73 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
74 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
76 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
77 static int16_t oneshot_layer_time = 0;
78 inline bool has_oneshot_layer_timed_out() {
79 return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
80 !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
85 void set_oneshot_layer(uint8_t layer, uint8_t state)
87 oneshot_layer_data = layer << 3 | state;
89 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
90 oneshot_layer_time = timer_read();
93 void reset_oneshot_layer(void) {
94 oneshot_layer_data = 0;
95 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
96 oneshot_layer_time = 0;
99 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
101 uint8_t start_state = oneshot_layer_data;
102 oneshot_layer_data &= ~state;
103 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
104 layer_off(get_oneshot_layer());
105 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
106 oneshot_layer_time = 0;
110 bool is_oneshot_layer_active(void)
112 return get_oneshot_layer_state();
116 void send_keyboard_report(void) {
117 keyboard_report->mods = real_mods;
118 keyboard_report->mods |= weak_mods;
119 keyboard_report->mods |= macro_mods;
120 #ifndef NO_ACTION_ONESHOT
122 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
123 if (has_oneshot_mods_timed_out()) {
124 dprintf("Oneshot: timeout\n");
125 clear_oneshot_mods();
128 keyboard_report->mods |= oneshot_mods;
130 clear_oneshot_mods();
135 host_keyboard_send(keyboard_report);
139 void add_key(uint8_t key)
142 if (keyboard_protocol && keyboard_nkro) {
150 void del_key(uint8_t key)
153 if (keyboard_protocol && keyboard_nkro) {
161 void clear_keys(void)
164 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
165 keyboard_report->raw[i] = 0;
171 uint8_t get_mods(void) { return real_mods; }
172 void add_mods(uint8_t mods) { real_mods |= mods; }
173 void del_mods(uint8_t mods) { real_mods &= ~mods; }
174 void set_mods(uint8_t mods) { real_mods = mods; }
175 void clear_mods(void) { real_mods = 0; }
178 uint8_t get_weak_mods(void) { return weak_mods; }
179 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
180 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
181 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
182 void clear_weak_mods(void) { weak_mods = 0; }
185 uint8_t get_macro_mods(void) { return macro_mods; }
186 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
187 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
188 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
189 void clear_macro_mods(void) { macro_mods = 0; }
191 /* Oneshot modifier */
192 #ifndef NO_ACTION_ONESHOT
193 void set_oneshot_mods(uint8_t mods)
196 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
197 oneshot_time = timer_read();
200 void clear_oneshot_mods(void)
203 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
207 uint8_t get_oneshot_mods(void)
214 * inspect keyboard state
216 uint8_t has_anykey(void)
219 for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
220 if (keyboard_report->raw[i])
226 uint8_t has_anymod(void)
228 return bitpop(real_mods);
231 uint8_t get_first_key(void)
234 if (keyboard_protocol && keyboard_nkro) {
236 for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
238 return i<<3 | biton(keyboard_report->nkro.bits[i]);
241 #ifdef USB_6KRO_ENABLE
244 if (keyboard_report->keys[i] != 0) {
248 } while (i != cb_tail);
249 return keyboard_report->keys[i];
251 return keyboard_report->keys[0];
257 /* local functions */
258 static inline void add_key_byte(uint8_t code)
260 #ifdef USB_6KRO_ENABLE
265 if (keyboard_report->keys[i] == code) {
268 if (empty == -1 && keyboard_report->keys[i] == 0) {
272 } while (i != cb_tail);
274 if (cb_tail == cb_head) {
277 // pop head when has no empty space
278 cb_head = RO_INC(cb_head);
282 // left shift when has empty space
286 if (keyboard_report->keys[i] != 0) {
287 keyboard_report->keys[empty] = keyboard_report->keys[i];
288 keyboard_report->keys[i] = 0;
289 empty = RO_INC(empty);
295 } while (i != cb_tail);
296 cb_tail = RO_SUB(cb_tail, offset);
302 keyboard_report->keys[cb_tail] = code;
303 cb_tail = RO_INC(cb_tail);
308 for (; i < KEYBOARD_REPORT_KEYS; i++) {
309 if (keyboard_report->keys[i] == code) {
312 if (empty == -1 && keyboard_report->keys[i] == 0) {
316 if (i == KEYBOARD_REPORT_KEYS) {
318 keyboard_report->keys[empty] = code;
324 static inline void del_key_byte(uint8_t code)
326 #ifdef USB_6KRO_ENABLE
330 if (keyboard_report->keys[i] == code) {
331 keyboard_report->keys[i] = 0;
334 // reset head and tail
335 cb_tail = cb_head = 0;
337 if (i == RO_DEC(cb_tail)) {
338 // left shift when next to tail
340 cb_tail = RO_DEC(cb_tail);
341 if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
344 } while (cb_tail != cb_head);
349 } while (i != cb_tail);
352 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
353 if (keyboard_report->keys[i] == code) {
354 keyboard_report->keys[i] = 0;
361 static inline void add_key_bit(uint8_t code)
363 if ((code>>3) < KEYBOARD_REPORT_BITS) {
364 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
366 dprintf("add_key_bit: can't add: %02X\n", code);
370 static inline void del_key_bit(uint8_t code)
372 if ((code>>3) < KEYBOARD_REPORT_BITS) {
373 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
375 dprintf("del_key_bit: can't del: %02X\n", code);