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 int16_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 /* 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 int16_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);
90 void set_oneshot_layer(uint8_t layer, uint8_t state)
92 oneshot_layer_data = layer << 3 | state;
94 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
95 oneshot_layer_time = timer_read();
98 void reset_oneshot_layer(void) {
99 oneshot_layer_data = 0;
100 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
101 oneshot_layer_time = 0;
104 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
106 uint8_t start_state = oneshot_layer_data;
107 oneshot_layer_data &= ~state;
108 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
109 layer_off(get_oneshot_layer());
110 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
111 oneshot_layer_time = 0;
115 bool is_oneshot_layer_active(void)
117 return get_oneshot_layer_state();
121 void send_keyboard_report(void) {
122 keyboard_report->mods = real_mods;
123 keyboard_report->mods |= weak_mods;
124 keyboard_report->mods |= macro_mods;
125 #ifndef NO_ACTION_ONESHOT
127 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
128 if (has_oneshot_mods_timed_out()) {
129 dprintf("Oneshot: timeout\n");
130 clear_oneshot_mods();
133 keyboard_report->mods |= oneshot_mods;
134 if (has_anykey(keyboard_report)) {
135 clear_oneshot_mods();
140 host_keyboard_send(keyboard_report);
144 uint8_t get_mods(void) { return real_mods; }
145 void add_mods(uint8_t mods) { real_mods |= mods; }
146 void del_mods(uint8_t mods) { real_mods &= ~mods; }
147 void set_mods(uint8_t mods) { real_mods = mods; }
148 void clear_mods(void) { real_mods = 0; }
151 uint8_t get_weak_mods(void) { return weak_mods; }
152 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
153 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
154 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
155 void clear_weak_mods(void) { weak_mods = 0; }
158 uint8_t get_macro_mods(void) { return macro_mods; }
159 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
160 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
161 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
162 void clear_macro_mods(void) { macro_mods = 0; }
164 /* Oneshot modifier */
165 #ifndef NO_ACTION_ONESHOT
166 void set_oneshot_mods(uint8_t mods)
169 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
170 oneshot_time = timer_read();
173 void clear_oneshot_mods(void)
176 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
180 uint8_t get_oneshot_mods(void)
187 * inspect keyboard state
189 uint8_t has_anymod(void)
191 return bitpop(real_mods);