]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action_util.c
Remove more commented out MCUs
[qmk_firmware.git] / tmk_core / common / action_util.c
1 /*
2 Copyright 2013 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17 #include "host.h"
18 #include "report.h"
19 #include "debug.h"
20 #include "action_util.h"
21 #include "action_layer.h"
22 #include "timer.h"
23 #include "keycode_config.h"
24
25 extern keymap_config_t keymap_config;
26
27
28 static uint8_t real_mods = 0;
29 static uint8_t weak_mods = 0;
30 static uint8_t macro_mods = 0;
31
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;
40 #endif
41
42 // TODO: pointer variable is not needed
43 //report_keyboard_t keyboard_report = {};
44 report_keyboard_t *keyboard_report = &(report_keyboard_t){};
45
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);
49
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);
58     }
59 }
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);
64     }
65 }
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;
70 }
71 #else
72 bool has_oneshot_mods_timed_out(void) {
73     return false;
74 }
75 #endif
76 #endif
77
78 /* oneshot layer */
79 #ifndef NO_ACTION_ONESHOT
80 /** \brief oneshot_layer_data bits
81  * LLLL LSSS
82  * where:
83  *   L => are layer bits
84  *   S => oneshot state bits
85  */
86 static int8_t oneshot_layer_data = 0;
87
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; }
90
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);
96 }
97 #endif
98
99 /** \brief Set oneshot layer 
100  *
101  * FIXME: needs doc
102  */
103 void set_oneshot_layer(uint8_t layer, uint8_t state)
104 {
105     oneshot_layer_data = layer << 3 | state;
106     layer_on(layer);
107 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
108     oneshot_layer_time = timer_read();
109 #endif
110     oneshot_layer_changed_kb(get_oneshot_layer());
111 }
112 /** \brief Reset oneshot layer 
113  *
114  * FIXME: needs doc
115  */
116 void reset_oneshot_layer(void) {
117     oneshot_layer_data = 0;
118 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
119     oneshot_layer_time = 0;
120 #endif
121     oneshot_layer_changed_kb(get_oneshot_layer());
122 }
123 /** \brief Clear oneshot layer 
124  *
125  * FIXME: needs doc
126  */
127 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
128 {
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();
134     }
135 }
136 /** \brief Is oneshot layer active
137  *
138  * FIXME: needs doc
139  */
140 bool is_oneshot_layer_active(void)
141 {
142     return get_oneshot_layer_state();
143 }
144 #endif
145
146 /** \brief Send keyboard report
147  *
148  * FIXME: needs doc
149  */
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
155     if (oneshot_mods) {
156 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
157         if (has_oneshot_mods_timed_out()) {
158             dprintf("Oneshot: timeout\n");
159             clear_oneshot_mods();
160         }
161 #endif
162         keyboard_report->mods |= oneshot_mods;
163         if (has_anykey(keyboard_report)) {
164             clear_oneshot_mods();
165         }
166     }
167
168 #endif
169     host_keyboard_send(keyboard_report);
170 }
171
172 /** \brief Get mods
173  *
174  * FIXME: needs doc
175  */
176 uint8_t get_mods(void) { return real_mods; }
177 /** \brief add mods
178  *
179  * FIXME: needs doc
180  */
181 void add_mods(uint8_t mods) { real_mods |= mods; }
182 /** \brief del mods
183  *
184  * FIXME: needs doc
185  */
186 void del_mods(uint8_t mods) { real_mods &= ~mods; }
187 /** \brief set mods
188  *
189  * FIXME: needs doc
190  */
191 void set_mods(uint8_t mods) { real_mods = mods; }
192 /** \brief clear mods
193  *
194  * FIXME: needs doc
195  */
196 void clear_mods(void) { real_mods = 0; }
197
198 /** \brief get weak mods
199  *
200  * FIXME: needs doc
201  */
202 uint8_t get_weak_mods(void) { return weak_mods; }
203 /** \brief add weak mods
204  *
205  * FIXME: needs doc
206  */
207 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
208 /** \brief del weak mods
209  *
210  * FIXME: needs doc
211  */
212 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
213 /** \brief set weak mods
214  *
215  * FIXME: needs doc
216  */
217 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
218 /** \brief clear weak mods
219  *
220  * FIXME: needs doc
221  */
222 void clear_weak_mods(void) { weak_mods = 0; }
223
224 /* macro modifier */
225 /** \brief get macro mods
226  *
227  * FIXME: needs doc
228  */
229 uint8_t get_macro_mods(void) { return macro_mods; }
230 /** \brief add macro mods
231  *
232  * FIXME: needs doc
233  */
234 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
235 /** \brief del macro mods
236  *
237  * FIXME: needs doc
238  */
239 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
240 /** \brief set macro mods
241  *
242  * FIXME: needs doc
243  */
244 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
245 /** \brief clear macro mods
246  *
247  * FIXME: needs doc
248  */
249 void clear_macro_mods(void) { macro_mods = 0; }
250
251 #ifndef NO_ACTION_ONESHOT
252 /** \brief set oneshot mods
253  *
254  * FIXME: needs doc
255  */
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();
260 #endif
261     oneshot_mods = mods;
262     oneshot_mods_changed_kb(mods);
263   }
264 }
265 /** \brief clear oneshot mods
266  *
267  * FIXME: needs doc
268  */
269 void clear_oneshot_mods(void) {
270   if (oneshot_mods) {
271     oneshot_mods = 0;
272 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
273     oneshot_time = 0;
274 #endif
275     oneshot_mods_changed_kb(oneshot_mods);
276   }
277 }
278 /** \brief get oneshot mods
279  *
280  * FIXME: needs doc
281  */
282 uint8_t get_oneshot_mods(void)
283 {
284     return oneshot_mods;
285 }
286 #endif
287
288 /** \brief Called when the one shot modifiers have been changed.
289  * 
290  * \param mods Contains the active modifiers active after the change. 
291  */
292 __attribute__((weak))
293 void oneshot_locked_mods_changed_user(uint8_t mods) { }
294
295 /** \brief Called when the locked one shot modifiers have been changed.
296  * 
297  * \param mods Contains the active modifiers active after the change. 
298  */
299 __attribute__((weak))
300 void oneshot_locked_mods_changed_kb(uint8_t mods) {
301     oneshot_locked_mods_changed_user(mods);
302 }
303
304 /** \brief Called when the one shot modifiers have been changed.
305  * 
306  * \param mods Contains the active modifiers active after the change.
307  */
308 __attribute__((weak))
309 void oneshot_mods_changed_user(uint8_t mods) { }
310
311 /** \brief Called when the one shot modifiers have been changed.
312  * 
313  * \param mods Contains the active modifiers active after the change.
314  */
315 __attribute__((weak))
316 void oneshot_mods_changed_kb(uint8_t mods) {
317     oneshot_mods_changed_user(mods);
318 }
319
320 /** \brief Called when the one shot layers have been changed.
321  * 
322  * \param layer Contains the layer that is toggled on, or zero when toggled off. 
323  */
324 __attribute__((weak))
325 void oneshot_layer_changed_user(uint8_t layer) { }
326
327 /** \brief Called when the one shot layers have been changed.
328  * 
329  * \param layer Contains the layer that is toggled on, or zero when toggled off.
330  */
331 __attribute__((weak))
332 void oneshot_layer_changed_kb(uint8_t layer) {
333     oneshot_layer_changed_user(layer);
334 }
335
336 /** \brief inspect keyboard state
337  *
338  * FIXME: needs doc
339  */
340 uint8_t has_anymod(void)
341 {
342     return bitpop(real_mods);
343 }