aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/common/action_util.c
diff options
context:
space:
mode:
authorThomas Baart <mail@thomasbaart.nl>2019-02-05 19:19:56 +0100
committerDrashna Jaelre <drashna@live.com>2019-02-05 10:19:56 -0800
commit47051f506fd4888bbb55fa7981cb7b24827f3d1b (patch)
tree88628409bc9d63bfb0999be7f5b328ff5421b365 /tmk_core/common/action_util.c
parent7df9e584fa1ee31ab5bb35b1c7fa09845cba0a80 (diff)
downloadqmk_firmware-47051f506fd4888bbb55fa7981cb7b24827f3d1b.tar.gz
qmk_firmware-47051f506fd4888bbb55fa7981cb7b24827f3d1b.zip
Add One Shot Key callbacks (#4697)
* Added callbacks to one shot modifier changes * Altered signature of callback functions * Reordered the callback methods, shortened brief documentation * Added One Shot Modifier callback documentation * First attempt at unit tests * Revert "First attempt at unit tests" This reverts commit 5ec21a782202c0d74cf21cb852bd319f0c8b4842. * Simplified oneshot function implementations * Made clear_oneshot_locked_mods to be conform action_util.h, adhering to the (void) signature * Made used datatypes for oneshot mods consistently unsigned integers * Corrected callback to call clear callback when clear_oneshot_locked_mods is invoked * Simplified oneshot equals statements * Corrected return type and signature of get and set oneshot locked mods * Consolidated one shot callbacks, added initial layer callback version * Fixed non-updating one shot layer variable after timeout or other clear, added code comments * Added better one shot key docs
Diffstat (limited to 'tmk_core/common/action_util.c')
-rw-r--r--tmk_core/common/action_util.c88
1 files changed, 75 insertions, 13 deletions
diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c
index 58401ace5..365ed6a1d 100644
--- a/tmk_core/common/action_util.c
+++ b/tmk_core/common/action_util.c
@@ -48,11 +48,21 @@ extern inline void del_key(uint8_t key);
48extern inline void clear_keys(void); 48extern inline void clear_keys(void);
49 49
50#ifndef NO_ACTION_ONESHOT 50#ifndef NO_ACTION_ONESHOT
51static int8_t oneshot_mods = 0; 51static uint8_t oneshot_mods = 0;
52static int8_t oneshot_locked_mods = 0; 52static uint8_t oneshot_locked_mods = 0;
53int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; } 53uint8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
54void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; } 54void set_oneshot_locked_mods(uint8_t mods) {
55void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; } 55 if (mods != oneshot_locked_mods) {
56 oneshot_locked_mods = mods;
57 oneshot_locked_mods_changed_kb(oneshot_locked_mods);
58 }
59}
60void 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}
56#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) 66#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
57static uint16_t oneshot_time = 0; 67static uint16_t oneshot_time = 0;
58bool has_oneshot_mods_timed_out(void) { 68bool has_oneshot_mods_timed_out(void) {
@@ -97,6 +107,7 @@ void set_oneshot_layer(uint8_t layer, uint8_t state)
97#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) 107#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
98 oneshot_layer_time = timer_read(); 108 oneshot_layer_time = timer_read();
99#endif 109#endif
110 oneshot_layer_changed_kb(get_oneshot_layer());
100} 111}
101/** \brief Reset oneshot layer 112/** \brief Reset oneshot layer
102 * 113 *
@@ -107,6 +118,7 @@ void reset_oneshot_layer(void) {
107#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) 118#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
108 oneshot_layer_time = 0; 119 oneshot_layer_time = 0;
109#endif 120#endif
121 oneshot_layer_changed_kb(get_oneshot_layer());
110} 122}
111/** \brief Clear oneshot layer 123/** \brief Clear oneshot layer
112 * 124 *
@@ -118,9 +130,7 @@ void clear_oneshot_layer_state(oneshot_fullfillment_t state)
118 oneshot_layer_data &= ~state; 130 oneshot_layer_data &= ~state;
119 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) { 131 if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
120 layer_off(get_oneshot_layer()); 132 layer_off(get_oneshot_layer());
121#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) 133 reset_oneshot_layer();
122 oneshot_layer_time = 0;
123#endif
124 } 134 }
125} 135}
126/** \brief Is oneshot layer active 136/** \brief Is oneshot layer active
@@ -243,23 +253,27 @@ void clear_macro_mods(void) { macro_mods = 0; }
243 * 253 *
244 * FIXME: needs doc 254 * FIXME: needs doc
245 */ 255 */
246void set_oneshot_mods(uint8_t mods) 256void set_oneshot_mods(uint8_t mods) {
247{ 257 if (oneshot_mods != mods) {
248 oneshot_mods = mods;
249#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) 258#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
250 oneshot_time = timer_read(); 259 oneshot_time = timer_read();
251#endif 260#endif
261 oneshot_mods = mods;
262 oneshot_mods_changed_kb(mods);
263 }
252} 264}
253/** \brief clear oneshot mods 265/** \brief clear oneshot mods
254 * 266 *
255 * FIXME: needs doc 267 * FIXME: needs doc
256 */ 268 */
257void clear_oneshot_mods(void) 269void clear_oneshot_mods(void) {
258{ 270 if (oneshot_mods) {
259 oneshot_mods = 0; 271 oneshot_mods = 0;
260#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0)) 272#if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
261 oneshot_time = 0; 273 oneshot_time = 0;
262#endif 274#endif
275 oneshot_mods_changed_kb(oneshot_mods);
276 }
263} 277}
264/** \brief get oneshot mods 278/** \brief get oneshot mods
265 * 279 *
@@ -271,6 +285,54 @@ uint8_t get_oneshot_mods(void)
271} 285}
272#endif 286#endif
273 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))
293void 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))
300void 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))
309void 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))
316void 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))
325void 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))
332void oneshot_layer_changed_kb(uint8_t layer) {
333 oneshot_layer_changed_user(layer);
334}
335
274/** \brief inspect keyboard state 336/** \brief inspect keyboard state
275 * 337 *
276 * FIXME: needs doc 338 * FIXME: needs doc