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 inline bool has_oneshot_mods_timed_out() {
62 return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
68 #ifndef NO_ACTION_ONESHOT
69 /* oneshot_layer_data bits
73 * S => oneshot state bits
75 static int8_t oneshot_layer_data = 0;
77 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
78 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
80 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
81 static int16_t oneshot_layer_time = 0;
82 inline bool has_oneshot_layer_timed_out() {
83 return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
84 !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
89 void set_oneshot_layer(uint8_t layer, uint8_t state)
91 oneshot_layer_data = layer << 3 | state;
93 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
94 oneshot_layer_time = timer_read();
97 void reset_oneshot_layer(void) {
98 oneshot_layer_data = 0;
99 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
100 oneshot_layer_time = 0;
103 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
105 uint8_t start_state = oneshot_layer_data;
106 oneshot_layer_data &= ~state;
107 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
108 layer_off(get_oneshot_layer());
109 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
110 oneshot_layer_time = 0;
114 bool is_oneshot_layer_active(void)
116 return get_oneshot_layer_state();
120 void send_keyboard_report(void) {
121 keyboard_report->mods = real_mods;
122 keyboard_report->mods |= weak_mods;
123 keyboard_report->mods |= macro_mods;
124 #ifndef NO_ACTION_ONESHOT
126 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
127 if (has_oneshot_mods_timed_out()) {
128 dprintf("Oneshot: timeout\n");
129 clear_oneshot_mods();
132 keyboard_report->mods |= oneshot_mods;
134 clear_oneshot_mods();
139 host_keyboard_send(keyboard_report);
143 void add_key(uint8_t key)
146 if (keyboard_protocol && keymap_config.nkro) {
154 void del_key(uint8_t key)
157 if (keyboard_protocol && keymap_config.nkro) {
165 void clear_keys(void)
168 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
169 keyboard_report->raw[i] = 0;
175 uint8_t get_mods(void) { return real_mods; }
176 void add_mods(uint8_t mods) { real_mods |= mods; }
177 void del_mods(uint8_t mods) { real_mods &= ~mods; }
178 void set_mods(uint8_t mods) { real_mods = mods; }
179 void clear_mods(void) { real_mods = 0; }
182 uint8_t get_weak_mods(void) { return weak_mods; }
183 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
184 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
185 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
186 void clear_weak_mods(void) { weak_mods = 0; }
189 uint8_t get_macro_mods(void) { return macro_mods; }
190 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
191 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
192 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
193 void clear_macro_mods(void) { macro_mods = 0; }
195 /* Oneshot modifier */
196 #ifndef NO_ACTION_ONESHOT
197 void set_oneshot_mods(uint8_t mods)
200 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
201 oneshot_time = timer_read();
204 void clear_oneshot_mods(void)
207 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
211 uint8_t get_oneshot_mods(void)
218 * inspect keyboard state
220 uint8_t has_anykey(void)
223 for (uint8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
224 if (keyboard_report->raw[i])
230 uint8_t has_anymod(void)
232 return bitpop(real_mods);
235 uint8_t get_first_key(void)
238 if (keyboard_protocol && keymap_config.nkro) {
240 for (; i < KEYBOARD_REPORT_BITS && !keyboard_report->nkro.bits[i]; i++)
242 return i<<3 | biton(keyboard_report->nkro.bits[i]);
245 #ifdef USB_6KRO_ENABLE
248 if (keyboard_report->keys[i] != 0) {
252 } while (i != cb_tail);
253 return keyboard_report->keys[i];
255 return keyboard_report->keys[0];
261 /* local functions */
262 static inline void add_key_byte(uint8_t code)
264 #ifdef USB_6KRO_ENABLE
269 if (keyboard_report->keys[i] == code) {
272 if (empty == -1 && keyboard_report->keys[i] == 0) {
276 } while (i != cb_tail);
278 if (cb_tail == cb_head) {
281 // pop head when has no empty space
282 cb_head = RO_INC(cb_head);
286 // left shift when has empty space
290 if (keyboard_report->keys[i] != 0) {
291 keyboard_report->keys[empty] = keyboard_report->keys[i];
292 keyboard_report->keys[i] = 0;
293 empty = RO_INC(empty);
299 } while (i != cb_tail);
300 cb_tail = RO_SUB(cb_tail, offset);
306 keyboard_report->keys[cb_tail] = code;
307 cb_tail = RO_INC(cb_tail);
312 for (; i < KEYBOARD_REPORT_KEYS; i++) {
313 if (keyboard_report->keys[i] == code) {
316 if (empty == -1 && keyboard_report->keys[i] == 0) {
320 if (i == KEYBOARD_REPORT_KEYS) {
322 keyboard_report->keys[empty] = code;
328 static inline void del_key_byte(uint8_t code)
330 #ifdef USB_6KRO_ENABLE
334 if (keyboard_report->keys[i] == code) {
335 keyboard_report->keys[i] = 0;
338 // reset head and tail
339 cb_tail = cb_head = 0;
341 if (i == RO_DEC(cb_tail)) {
342 // left shift when next to tail
344 cb_tail = RO_DEC(cb_tail);
345 if (keyboard_report->keys[RO_DEC(cb_tail)] != 0) {
348 } while (cb_tail != cb_head);
353 } while (i != cb_tail);
356 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
357 if (keyboard_report->keys[i] == code) {
358 keyboard_report->keys[i] = 0;
365 static inline void add_key_bit(uint8_t code)
367 if ((code>>3) < KEYBOARD_REPORT_BITS) {
368 keyboard_report->nkro.bits[code>>3] |= 1<<(code&7);
370 dprintf("add_key_bit: can't add: %02X\n", code);
374 static inline void del_key_bit(uint8_t code)
376 if ((code>>3) < KEYBOARD_REPORT_BITS) {
377 keyboard_report->nkro.bits[code>>3] &= ~(1<<(code&7));
379 dprintf("del_key_bit: can't del: %02X\n", code);