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 uint8_t real_mods = 0;
29 static uint8_t weak_mods = 0;
30 static uint8_t macro_mods = 0;
32 #ifdef USB_6KRO_ENABLE
33 #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
34 #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
35 #define RO_INC(a) RO_ADD(a, 1)
36 #define RO_DEC(a) RO_SUB(a, 1)
37 static int8_t cb_head = 0;
38 static int8_t cb_tail = 0;
39 static int8_t cb_count = 0;
42 // TODO: pointer variable is not needed
43 //report_keyboard_t keyboard_report = {};
44 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
46 #ifndef NO_ACTION_ONESHOT
47 static int8_t oneshot_mods = 0;
48 static int8_t oneshot_locked_mods = 0;
49 int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
50 void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
51 void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
52 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
53 static int16_t oneshot_time = 0;
54 bool has_oneshot_mods_timed_out(void) {
55 return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
58 bool has_oneshot_mods_timed_out(void) {
65 #ifndef NO_ACTION_ONESHOT
66 /* oneshot_layer_data bits
70 * S => oneshot state bits
72 static int8_t oneshot_layer_data = 0;
74 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
75 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
77 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
78 static int16_t oneshot_layer_time = 0;
79 inline bool has_oneshot_layer_timed_out() {
80 return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
81 !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
86 void set_oneshot_layer(uint8_t layer, uint8_t state)
88 oneshot_layer_data = layer << 3 | state;
90 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
91 oneshot_layer_time = timer_read();
94 void reset_oneshot_layer(void) {
95 oneshot_layer_data = 0;
96 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
97 oneshot_layer_time = 0;
100 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
102 uint8_t start_state = oneshot_layer_data;
103 oneshot_layer_data &= ~state;
104 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
105 layer_off(get_oneshot_layer());
106 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
107 oneshot_layer_time = 0;
111 bool is_oneshot_layer_active(void)
113 return get_oneshot_layer_state();
117 void send_keyboard_report(void) {
118 keyboard_report->mods = real_mods;
119 keyboard_report->mods |= weak_mods;
120 keyboard_report->mods |= macro_mods;
121 #ifndef NO_ACTION_ONESHOT
123 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
124 if (has_oneshot_mods_timed_out()) {
125 dprintf("Oneshot: timeout\n");
126 clear_oneshot_mods();
129 keyboard_report->mods |= oneshot_mods;
130 if (has_anykey(keyboard_report)) {
131 clear_oneshot_mods();
136 host_keyboard_send(keyboard_report);
140 void add_key(uint8_t key)
143 if (keyboard_protocol && keymap_config.nkro) {
144 add_key_bit(keyboard_report, key);
148 add_key_byte(keyboard_report, key);
151 void del_key(uint8_t key)
154 if (keyboard_protocol && keymap_config.nkro) {
155 del_key_bit(keyboard_report, key);
159 del_key_byte(keyboard_report, key);
162 void clear_keys(void)
165 for (int8_t i = 1; i < KEYBOARD_REPORT_SIZE; i++) {
166 keyboard_report->raw[i] = 0;
172 uint8_t get_mods(void) { return real_mods; }
173 void add_mods(uint8_t mods) { real_mods |= mods; }
174 void del_mods(uint8_t mods) { real_mods &= ~mods; }
175 void set_mods(uint8_t mods) { real_mods = mods; }
176 void clear_mods(void) { real_mods = 0; }
179 uint8_t get_weak_mods(void) { return weak_mods; }
180 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
181 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
182 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
183 void clear_weak_mods(void) { weak_mods = 0; }
186 uint8_t get_macro_mods(void) { return macro_mods; }
187 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
188 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
189 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
190 void clear_macro_mods(void) { macro_mods = 0; }
192 /* Oneshot modifier */
193 #ifndef NO_ACTION_ONESHOT
194 void set_oneshot_mods(uint8_t mods)
197 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
198 oneshot_time = timer_read();
201 void clear_oneshot_mods(void)
204 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
208 uint8_t get_oneshot_mods(void)
215 * inspect keyboard state
217 uint8_t has_anymod(void)
219 return bitpop(real_mods);