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 extern inline void add_key(uint8_t key);
47 extern inline void del_key(uint8_t key);
48 extern inline void clear_keys(void);
50 #ifndef NO_ACTION_ONESHOT
51 static int8_t oneshot_mods = 0;
52 static int8_t oneshot_locked_mods = 0;
53 int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
54 void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
55 void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
56 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
57 static uint16_t oneshot_time = 0;
58 bool has_oneshot_mods_timed_out(void) {
59 return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
62 bool has_oneshot_mods_timed_out(void) {
69 #ifndef NO_ACTION_ONESHOT
70 /** \brief oneshot_layer_data bits
74 * S => oneshot state bits
76 static int8_t oneshot_layer_data = 0;
78 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
79 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
81 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
82 static uint16_t oneshot_layer_time = 0;
83 inline bool has_oneshot_layer_timed_out() {
84 return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
85 !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
89 /** \brief Set oneshot layer
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 /** \brief Reset oneshot layer
105 void reset_oneshot_layer(void) {
106 oneshot_layer_data = 0;
107 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
108 oneshot_layer_time = 0;
111 /** \brief Clear oneshot layer
115 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
117 uint8_t start_state = oneshot_layer_data;
118 oneshot_layer_data &= ~state;
119 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
120 layer_off(get_oneshot_layer());
121 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
122 oneshot_layer_time = 0;
126 /** \brief Is oneshot layer active
130 bool is_oneshot_layer_active(void)
132 return get_oneshot_layer_state();
136 /** \brief Send keyboard report
140 void send_keyboard_report(void) {
141 keyboard_report->mods = real_mods;
142 keyboard_report->mods |= weak_mods;
143 keyboard_report->mods |= macro_mods;
144 #ifndef NO_ACTION_ONESHOT
146 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
147 if (has_oneshot_mods_timed_out()) {
148 dprintf("Oneshot: timeout\n");
149 clear_oneshot_mods();
152 keyboard_report->mods |= oneshot_mods;
153 if (has_anykey(keyboard_report)) {
154 clear_oneshot_mods();
159 host_keyboard_send(keyboard_report);
166 uint8_t get_mods(void) { return real_mods; }
171 void add_mods(uint8_t mods) { real_mods |= mods; }
176 void del_mods(uint8_t mods) { real_mods &= ~mods; }
181 void set_mods(uint8_t mods) { real_mods = mods; }
182 /** \brief clear mods
186 void clear_mods(void) { real_mods = 0; }
188 /** \brief get weak mods
192 uint8_t get_weak_mods(void) { return weak_mods; }
193 /** \brief add weak mods
197 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
198 /** \brief del weak mods
202 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
203 /** \brief set weak mods
207 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
208 /** \brief clear weak mods
212 void clear_weak_mods(void) { weak_mods = 0; }
215 /** \brief get macro mods
219 uint8_t get_macro_mods(void) { return macro_mods; }
220 /** \brief add macro mods
224 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
225 /** \brief del macro mods
229 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
230 /** \brief set macro mods
234 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
235 /** \brief clear macro mods
239 void clear_macro_mods(void) { macro_mods = 0; }
241 #ifndef NO_ACTION_ONESHOT
242 /** \brief set oneshot mods
246 void set_oneshot_mods(uint8_t mods)
249 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
250 oneshot_time = timer_read();
253 /** \brief clear oneshot mods
257 void clear_oneshot_mods(void)
260 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
264 /** \brief get oneshot mods
268 uint8_t get_oneshot_mods(void)
274 /** \brief inspect keyboard state
278 uint8_t has_anymod(void)
280 return bitpop(real_mods);