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 uint8_t oneshot_mods = 0;
52 static uint8_t oneshot_locked_mods = 0;
53 uint8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
54 void set_oneshot_locked_mods(uint8_t mods) {
55 if (mods != oneshot_locked_mods) {
56 oneshot_locked_mods = mods;
57 oneshot_locked_mods_changed_kb(oneshot_locked_mods);
60 void clear_oneshot_locked_mods(void) {
61 if (oneshot_locked_mods) {
62 oneshot_locked_mods = 0;
63 oneshot_locked_mods_changed_kb(oneshot_locked_mods);
66 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
67 static uint16_t oneshot_time = 0;
68 bool has_oneshot_mods_timed_out(void) {
69 return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
72 bool has_oneshot_mods_timed_out(void) {
79 #ifndef NO_ACTION_ONESHOT
80 /** \brief oneshot_layer_data bits
84 * S => oneshot state bits
86 static int8_t oneshot_layer_data = 0;
88 inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
89 inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
91 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
92 static uint16_t oneshot_layer_time = 0;
93 inline bool has_oneshot_layer_timed_out() {
94 return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
95 !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
99 /** \brief Set oneshot layer
103 void set_oneshot_layer(uint8_t layer, uint8_t state)
105 oneshot_layer_data = layer << 3 | state;
107 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
108 oneshot_layer_time = timer_read();
110 oneshot_layer_changed_kb(get_oneshot_layer());
112 /** \brief Reset oneshot layer
116 void reset_oneshot_layer(void) {
117 oneshot_layer_data = 0;
118 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
119 oneshot_layer_time = 0;
121 oneshot_layer_changed_kb(get_oneshot_layer());
123 /** \brief Clear oneshot layer
127 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
129 uint8_t start_state = oneshot_layer_data;
130 oneshot_layer_data &= ~state;
131 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
132 layer_off(get_oneshot_layer());
133 reset_oneshot_layer();
136 /** \brief Is oneshot layer active
140 bool is_oneshot_layer_active(void)
142 return get_oneshot_layer_state();
146 /** \brief Send keyboard report
150 void send_keyboard_report(void) {
151 keyboard_report->mods = real_mods;
152 keyboard_report->mods |= weak_mods;
153 keyboard_report->mods |= macro_mods;
154 #ifndef NO_ACTION_ONESHOT
156 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
157 if (has_oneshot_mods_timed_out()) {
158 dprintf("Oneshot: timeout\n");
159 clear_oneshot_mods();
162 keyboard_report->mods |= oneshot_mods;
163 if (has_anykey(keyboard_report)) {
164 clear_oneshot_mods();
169 host_keyboard_send(keyboard_report);
176 uint8_t get_mods(void) { return real_mods; }
181 void add_mods(uint8_t mods) { real_mods |= mods; }
186 void del_mods(uint8_t mods) { real_mods &= ~mods; }
191 void set_mods(uint8_t mods) { real_mods = mods; }
192 /** \brief clear mods
196 void clear_mods(void) { real_mods = 0; }
198 /** \brief get weak mods
202 uint8_t get_weak_mods(void) { return weak_mods; }
203 /** \brief add weak mods
207 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
208 /** \brief del weak mods
212 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
213 /** \brief set weak mods
217 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
218 /** \brief clear weak mods
222 void clear_weak_mods(void) { weak_mods = 0; }
225 /** \brief get macro mods
229 uint8_t get_macro_mods(void) { return macro_mods; }
230 /** \brief add macro mods
234 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
235 /** \brief del macro mods
239 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
240 /** \brief set macro mods
244 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
245 /** \brief clear macro mods
249 void clear_macro_mods(void) { macro_mods = 0; }
251 #ifndef NO_ACTION_ONESHOT
252 /** \brief set oneshot mods
256 void set_oneshot_mods(uint8_t mods) {
257 if (oneshot_mods != mods) {
258 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
259 oneshot_time = timer_read();
262 oneshot_mods_changed_kb(mods);
265 /** \brief clear oneshot mods
269 void clear_oneshot_mods(void) {
272 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
275 oneshot_mods_changed_kb(oneshot_mods);
278 /** \brief get oneshot mods
282 uint8_t get_oneshot_mods(void)
288 /** \brief Called when the one shot modifiers have been changed.
290 * \param mods Contains the active modifiers active after the change.
292 __attribute__((weak))
293 void oneshot_locked_mods_changed_user(uint8_t mods) { }
295 /** \brief Called when the locked one shot modifiers have been changed.
297 * \param mods Contains the active modifiers active after the change.
299 __attribute__((weak))
300 void oneshot_locked_mods_changed_kb(uint8_t mods) {
301 oneshot_locked_mods_changed_user(mods);
304 /** \brief Called when the one shot modifiers have been changed.
306 * \param mods Contains the active modifiers active after the change.
308 __attribute__((weak))
309 void oneshot_mods_changed_user(uint8_t mods) { }
311 /** \brief Called when the one shot modifiers have been changed.
313 * \param mods Contains the active modifiers active after the change.
315 __attribute__((weak))
316 void oneshot_mods_changed_kb(uint8_t mods) {
317 oneshot_mods_changed_user(mods);
320 /** \brief Called when the one shot layers have been changed.
322 * \param layer Contains the layer that is toggled on, or zero when toggled off.
324 __attribute__((weak))
325 void oneshot_layer_changed_user(uint8_t layer) { }
327 /** \brief Called when the one shot layers have been changed.
329 * \param layer Contains the layer that is toggled on, or zero when toggled off.
331 __attribute__((weak))
332 void oneshot_layer_changed_kb(uint8_t layer) {
333 oneshot_layer_changed_user(layer);
336 /** \brief inspect keyboard state
340 uint8_t has_anymod(void)
342 return bitpop(real_mods);