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"
23 #include "keycode_config.h"
25 extern keymap_config_t keymap_config;
28 static inline void add_key_byte(uint8_t code);
29 static inline void del_key_byte(uint8_t code);
31 static inline void add_key_bit(uint8_t code);
32 static inline void del_key_bit(uint8_t code);
35 static uint8_t real_mods = 0;
36 static uint8_t weak_mods = 0;
37 static uint8_t macro_mods = 0;
39 #ifdef USB_6KRO_ENABLE
40 #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
41 #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
42 #define RO_INC(a) RO_ADD(a, 1)
43 #define RO_DEC(a) RO_SUB(a, 1)
44 static int8_t cb_head = 0;
45 static int8_t cb_tail = 0;
46 static int8_t cb_count = 0;
49 // TODO: pointer variable is not needed
50 //report_keyboard_t keyboard_report = {};
51 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
53 #ifndef NO_ACTION_ONESHOT
54 static int8_t oneshot_mods = 0;
55 static int8_t oneshot_locked_mods = 0;
56 int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
57 void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
58 void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
59 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
60 static int16_t oneshot_time = 0;
61 bool has_oneshot_mods_timed_out(void) {
62 return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
65 bool has_oneshot_mods_timed_out(void) {
72 #ifndef NO_ACTION_ONESHOT
73 /* oneshot_layer_data bits
77 * S => oneshot state bits
79 static int8_t oneshot_layer_data = 0;
81 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
82 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
84 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
85 static int16_t oneshot_layer_time = 0;
86 inline bool has_oneshot_layer_timed_out() {
87 return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
88 !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
93 void set_oneshot_layer(uint8_t layer, uint8_t state)
95 oneshot_layer_data = layer << 3 | state;
97 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
98 oneshot_layer_time = timer_read();
101 void reset_oneshot_layer(void) {
102 oneshot_layer_data = 0;
103 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
104 oneshot_layer_time = 0;
107 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
109 uint8_t start_state = oneshot_layer_data;
110 oneshot_layer_data &= ~state;
111 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
112 layer_off(get_oneshot_layer());
113 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
114 oneshot_layer_time = 0;
118 bool is_oneshot_layer_active(void)
120 return get_oneshot_layer_state();
124 void send_keyboard_report(void) {
125 keyboard_report->mods = real_mods;
126 keyboard_report->mods |= weak_mods;
127 keyboard_report->mods |= macro_mods;
128 #ifndef NO_ACTION_ONESHOT
130 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
131 if (has_oneshot_mods_timed_out()) {
132 dprintf("Oneshot: timeout\n");
133 clear_oneshot_mods();
136 keyboard_report->mods |= oneshot_mods;
138 clear_oneshot_mods();
143 host_keyboard_send(keyboard_report);
147 void add_key(uint8_t key)
150 if (keyboard_protocol && keymap_config.nkro) {
158 void del_key(uint8_t key)
161 if (keyboard_protocol && keymap_config.nkro) {
169 void clear_keys(void)
172 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
173 keyboard_report->raw[i] = 0;
179 uint8_t get_mods(void) { return real_mods; }
180 void add_mods(uint8_t mods) { real_mods |= mods; }
181 void del_mods(uint8_t mods) { real_mods &= ~mods; }
182 void set_mods(uint8_t mods) { real_mods = mods; }
183 void clear_mods(void) { real_mods = 0; }
186 uint8_t get_weak_mods(void) { return weak_mods; }
187 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
188 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
189 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
190 void clear_weak_mods(void) { weak_mods = 0; }
193 uint8_t get_macro_mods(void) { return macro_mods; }
194 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
195 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
196 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
197 void clear_macro_mods(void) { macro_mods = 0; }
199 /* Oneshot modifier */
200 #ifndef NO_ACTION_ONESHOT
201 void set_oneshot_mods(uint8_t mods)
204 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
205 oneshot_time = timer_read();
208 void clear_oneshot_mods(void)
211 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
215 uint8_t get_oneshot_mods(void)
222 * inspect keyboard state
224 uint8_t has_anykey(void)
227 for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
228 if (keyboard_report->raw[i])
234 uint8_t has_anymod(void)
236 return bitpop(real_mods);
239 uint8_t get_first_key(void)
242 if (keyboard_protocol && keymap_config.nkro) {
244 for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
246 return i<<3 | biton(keyboard_report->nkro.bits[i]);
249 #ifdef USB_6KRO_ENABLE
252 if (keyboard_report->keys[i] != 0) {
256 } while (i != cb_tail);
257 return keyboard_report->keys[i];
259 return keyboard_report->keys[0];
265 /* local functions */
266 static inline void add_key_byte(uint8_t code)
268 #ifdef USB_6KRO_ENABLE
273 if (keyboard_report->keys[i] == code) {
276 if (empty == -1 && keyboard_report->keys[i] == 0) {
280 } while (i != cb_tail);
282 if (cb_tail == cb_head) {
285 // pop head when has no empty space
286 cb_head = RO_INC(cb_head);
290 // left shift when has empty space
294 if (keyboard_report->keys[i] != 0) {
295 keyboard_report->keys[empty] = keyboard_report->keys[i];
296 keyboard_report->keys[i] = 0;
297 empty = RO_INC(empty);
303 } while (i != cb_tail);
304 cb_tail = RO_SUB(cb_tail, offset);
310 keyboard_report->keys[cb_tail] = code;
311 cb_tail = RO_INC(cb_tail);
316 for (; i < KEYBOARD_REPORT_KEYS; i++) {
317 if (keyboard_report->keys[i] == code) {
320 if (empty == -1 && keyboard_report->keys[i] == 0) {
324 if (i == KEYBOARD_REPORT_KEYS) {
326 keyboard_report->keys[empty] = code;
332 static inline void del_key_byte(uint8_t code)
334 #ifdef USB_6KRO_ENABLE
338 if (keyboard_report->keys[i] == code) {
339 keyboard_report->keys[i] = 0;
342 // reset head and tail
343 cb_tail = cb_head = 0;
345 if (i == RO_DEC(cb_tail)) {
346 // left shift when next to tail
348 cb_tail = RO_DEC(cb_tail);
349 if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
352 } while (cb_tail != cb_head);
357 } while (i != cb_tail);
360 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
361 if (keyboard_report->keys[i] == code) {
362 keyboard_report->keys[i] = 0;
369 static inline void add_key_bit(uint8_t code)
371 if ((code>>3) < KEYBOARD_REPORT_BITS) {
372 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
374 dprintf("add_key_bit: can't add: %02X\n", code);
378 static inline void del_key_bit(uint8_t code)
380 if ((code>>3) < KEYBOARD_REPORT_BITS) {
381 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
383 dprintf("del_key_bit: can't del: %02X\n", code);