summaryrefslogtreecommitdiffstats
path: root/tmk_core/common/action_util.c
diff options
context:
space:
mode:
authorZsolt Parragi <zsolt.parragi@cancellar.hu>2020-05-13 23:36:55 +0200
committerGitHub <noreply@github.com>2020-05-14 07:36:55 +1000
commit805f5cb72bb04c10327b40ed0c41e3848ad9ca75 (patch)
tree249680bec133e18dd2399b80cbeb14e980f1095c /tmk_core/common/action_util.c
parenta8a8bf0ff3d6e1c9637079b925b30cf7eb8913fd (diff)
One shot support for swap hands (#8590)
This commits add the SH_OS keycode, which works similarly to one shot layers: * while pressed, the keyboard is swapped * if no keys were pressed while it was pressed, the next key press is swapped SH_OS also supports chaining with one shot layers: OSL(x) + SH_OS + key interprets the key press on the oneshot layer. The ONESHOT_TIMEOUT setting used by one shot keys and layers is also used by oneshot swap hands. In the above chaining scenario the timeout of the oneshot layer is reset when swap hands is activated. Resolves #2682
Diffstat (limited to 'tmk_core/common/action_util.c')
-rw-r--r--tmk_core/common/action_util.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/tmk_core/common/action_util.c b/tmk_core/common/action_util.c
index 335aa36e62..371acfa610 100644
--- a/tmk_core/common/action_util.c
+++ b/tmk_core/common/action_util.c
@@ -83,9 +83,63 @@ static int8_t oneshot_layer_data = 0;
inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
+# ifdef SWAP_HANDS_ENABLE
+enum {
+ SHO_OFF,
+ SHO_ACTIVE, // Swap hands button was pressed, and we didn't send any swapped keys yet
+ SHO_PRESSED, // Swap hands button is currently pressed
+ SHO_USED, // Swap hands button is still pressed, and we already sent swapped keys
+} swap_hands_oneshot = SHO_OFF;
+# endif
+
# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
static uint16_t oneshot_layer_time = 0;
inline bool has_oneshot_layer_timed_out() { return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT && !(get_oneshot_layer_state() & ONESHOT_TOGGLED); }
+# ifdef SWAP_HANDS_ENABLE
+static uint16_t oneshot_swaphands_time = 0;
+inline bool has_oneshot_swaphands_timed_out() { return TIMER_DIFF_16(timer_read(), oneshot_swaphands_time) >= ONESHOT_TIMEOUT && !(swap_hands_oneshot >= SHO_PRESSED); }
+# endif
+# endif
+
+# ifdef SWAP_HANDS_ENABLE
+
+void set_oneshot_swaphands(void) {
+ swap_hands_oneshot = SHO_PRESSED;
+ swap_hands = true;
+# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
+ oneshot_swaphands_time = timer_read();
+ if (oneshot_layer_time != 0) {
+ oneshot_layer_time = oneshot_swaphands_time;
+ }
+# endif
+}
+
+void release_oneshot_swaphands(void) {
+ if (swap_hands_oneshot == SHO_PRESSED) {
+ swap_hands_oneshot = SHO_ACTIVE;
+ }
+ if (swap_hands_oneshot == SHO_USED) {
+ clear_oneshot_swaphands();
+ }
+}
+
+void use_oneshot_swaphands(void) {
+ if (swap_hands_oneshot == SHO_PRESSED) {
+ swap_hands_oneshot = SHO_USED;
+ }
+ if (swap_hands_oneshot == SHO_ACTIVE) {
+ clear_oneshot_swaphands();
+ }
+}
+
+void clear_oneshot_swaphands(void) {
+ swap_hands_oneshot = SHO_OFF;
+ swap_hands = false;
+# if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
+ oneshot_swaphands_time = 0;
+# endif
+}
+
# endif
/** \brief Set oneshot layer