]> git.donarmstrong.com Git - qmk_firmware.git/blob - tmk_core/common/action_util.c
Generate API docs from source code comments (#2491)
[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 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;
60 }
61 #else
62 bool has_oneshot_mods_timed_out(void) {
63     return false;
64 }
65 #endif
66 #endif
67
68 /* oneshot layer */
69 #ifndef NO_ACTION_ONESHOT
70 /** \brief oneshot_layer_data bits
71  * LLLL LSSS
72  * where:
73  *   L => are layer bits
74  *   S => oneshot state bits
75  */
76 static int8_t oneshot_layer_data = 0;
77
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; }
80
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);
86 }
87 #endif
88
89 /** \brief Set oneshot layer 
90  *
91  * FIXME: needs doc
92  */
93 void set_oneshot_layer(uint8_t layer, uint8_t state)
94 {
95     oneshot_layer_data = layer << 3 | state;
96     layer_on(layer);
97 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
98     oneshot_layer_time = timer_read();
99 #endif
100 }
101 /** \brief Reset oneshot layer 
102  *
103  * FIXME: needs doc
104  */
105 void reset_oneshot_layer(void) {
106     oneshot_layer_data = 0;
107 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
108     oneshot_layer_time = 0;
109 #endif
110 }
111 /** \brief Clear oneshot layer 
112  *
113  * FIXME: needs doc
114  */
115 void clear_oneshot_layer_state(oneshot_fullfillment_t state)
116 {
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;
123 #endif
124     }
125 }
126 /** \brief Is oneshot layer active
127  *
128  * FIXME: needs doc
129  */
130 bool is_oneshot_layer_active(void)
131 {
132     return get_oneshot_layer_state();
133 }
134 #endif
135
136 /** \brief Send keyboard report
137  *
138  * FIXME: needs doc
139  */
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
145     if (oneshot_mods) {
146 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
147         if (has_oneshot_mods_timed_out()) {
148             dprintf("Oneshot: timeout\n");
149             clear_oneshot_mods();
150         }
151 #endif
152         keyboard_report->mods |= oneshot_mods;
153         if (has_anykey(keyboard_report)) {
154             clear_oneshot_mods();
155         }
156     }
157
158 #endif
159     host_keyboard_send(keyboard_report);
160 }
161
162 /** \brief Get mods
163  *
164  * FIXME: needs doc
165  */
166 uint8_t get_mods(void) { return real_mods; }
167 /** \brief add mods
168  *
169  * FIXME: needs doc
170  */
171 void add_mods(uint8_t mods) { real_mods |= mods; }
172 /** \brief del mods
173  *
174  * FIXME: needs doc
175  */
176 void del_mods(uint8_t mods) { real_mods &= ~mods; }
177 /** \brief set mods
178  *
179  * FIXME: needs doc
180  */
181 void set_mods(uint8_t mods) { real_mods = mods; }
182 /** \brief clear mods
183  *
184  * FIXME: needs doc
185  */
186 void clear_mods(void) { real_mods = 0; }
187
188 /** \brief get weak mods
189  *
190  * FIXME: needs doc
191  */
192 uint8_t get_weak_mods(void) { return weak_mods; }
193 /** \brief add weak mods
194  *
195  * FIXME: needs doc
196  */
197 void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
198 /** \brief del weak mods
199  *
200  * FIXME: needs doc
201  */
202 void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
203 /** \brief set weak mods
204  *
205  * FIXME: needs doc
206  */
207 void set_weak_mods(uint8_t mods) { weak_mods = mods; }
208 /** \brief clear weak mods
209  *
210  * FIXME: needs doc
211  */
212 void clear_weak_mods(void) { weak_mods = 0; }
213
214 /* macro modifier */
215 /** \brief get macro mods
216  *
217  * FIXME: needs doc
218  */
219 uint8_t get_macro_mods(void) { return macro_mods; }
220 /** \brief add macro mods
221  *
222  * FIXME: needs doc
223  */
224 void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
225 /** \brief del macro mods
226  *
227  * FIXME: needs doc
228  */
229 void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
230 /** \brief set macro mods
231  *
232  * FIXME: needs doc
233  */
234 void set_macro_mods(uint8_t mods) { macro_mods = mods; }
235 /** \brief clear macro mods
236  *
237  * FIXME: needs doc
238  */
239 void clear_macro_mods(void) { macro_mods = 0; }
240
241 #ifndef NO_ACTION_ONESHOT
242 /** \brief set oneshot mods
243  *
244  * FIXME: needs doc
245  */
246 void set_oneshot_mods(uint8_t mods)
247 {
248     oneshot_mods = mods;
249 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
250     oneshot_time = timer_read();
251 #endif
252 }
253 /** \brief clear oneshot mods
254  *
255  * FIXME: needs doc
256  */
257 void clear_oneshot_mods(void)
258 {
259     oneshot_mods = 0;
260 #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
261     oneshot_time = 0;
262 #endif
263 }
264 /** \brief get oneshot mods
265  *
266  * FIXME: needs doc
267  */
268 uint8_t get_oneshot_mods(void)
269 {
270     return oneshot_mods;
271 }
272 #endif
273
274 /** \brief inspect keyboard state
275  *
276  * FIXME: needs doc
277  */
278 uint8_t has_anymod(void)
279 {
280     return bitpop(real_mods);
281 }