summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Cullmann <christoph@cullmann.io>2024-06-15 14:44:37 +0200
committerChristoph Cullmann <christoph@cullmann.io>2024-06-15 14:44:37 +0200
commit477feeaa9255738cba3c5f3840e355f59e9e1aca (patch)
tree7d37051edc5a3705a79635630960f8fde1446dd9
parent6cac2866d899a0349edd26fa156322353fa6aa45 (diff)
sync with https://github.com/getreuer/qmk-keymap
-rw-r--r--common/achordion.c112
-rw-r--r--common/achordion.h54
2 files changed, 133 insertions, 33 deletions
diff --git a/common/achordion.c b/common/achordion.c
index dd1c8fb..4833d1a 100644
--- a/common/achordion.c
+++ b/common/achordion.c
@@ -1,4 +1,4 @@
-// Copyright 2022-2023 Google LLC
+// Copyright 2022-2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -35,6 +35,8 @@ static uint16_t tap_hold_keycode = KC_NO;
static uint16_t hold_timer = 0;
// Eagerly applied mods, if any.
static uint8_t eager_mods = 0;
+// Flag to determine whether another key is pressed within the timeout.
+static bool pressed_another_key_before_release = false;
#ifdef ACHORDION_STREAK
// Timer for typing streak
@@ -81,12 +83,31 @@ static void settle_as_hold(void) {
recursively_process_record(&tap_hold_record, STATE_HOLDING);
}
+#ifdef ACHORDION_STREAK
+static void update_streak_timer(uint16_t keycode, keyrecord_t* record) {
+ if (achordion_streak_continue(keycode)) {
+ // We use 0 to represent an unset timer, so `| 1` to force a nonzero value.
+ streak_timer = record->event.time | 1;
+ } else {
+ streak_timer = 0;
+ }
+}
+#endif
+
bool process_achordion(uint16_t keycode, keyrecord_t* record) {
// Don't process events that Achordion generated.
if (achordion_state == STATE_RECURSING) {
return true;
}
+ // If this is a keypress and if the key is different than the tap-hold key,
+ // this information is saved to a flag to be processed later when the tap-hold
+ // key is released.
+ if (!pressed_another_key_before_release && record->event.pressed &&
+ tap_hold_keycode != KC_NO && tap_hold_keycode != keycode) {
+ pressed_another_key_before_release = true;
+ }
+
// Determine whether the current event is for a mod-tap or layer-tap key.
const bool is_mt = IS_QK_MOD_TAP(keycode);
const bool is_tap_hold = is_mt || IS_QK_LAYER_TAP(keycode);
@@ -125,7 +146,7 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
}
#ifdef ACHORDION_STREAK
- streak_timer = (timer_read() + achordion_streak_timeout(keycode)) | 1;
+ update_streak_timer(keycode, record);
#endif
return true; // Otherwise, continue with default handling.
}
@@ -137,6 +158,15 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
tap_hold_record.event.pressed = false;
// Plumb hold release event.
recursively_process_record(&tap_hold_record, STATE_RELEASED);
+ } else if (!pressed_another_key_before_release) {
+ // No other key was pressed between the press and release of the tap-hold
+ // key, simulate a hold and then a release without waiting for Achordion
+ // timeout to end.
+ dprintln("Achordion: Key released. Simulating hold and release.");
+ settle_as_hold();
+ tap_hold_record.event.pressed = false;
+ // Plumb hold release event.
+ recursively_process_record(&tap_hold_record, STATE_RELEASED);
} else {
dprintf("Achordion: Key released.%s\n",
eager_mods ? " Clearing eager mods." : "");
@@ -146,13 +176,19 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
}
achordion_state = STATE_RELEASED;
+ // The tap-hold key is released, clear the related keycode and the flag.
+ tap_hold_keycode = KC_NO;
+ pressed_another_key_before_release = false;
return false;
}
if (achordion_state == STATE_UNSETTLED && record->event.pressed) {
#ifdef ACHORDION_STREAK
- const bool is_streak = (streak_timer != 0);
- streak_timer = (timer_read() + achordion_streak_timeout(keycode)) | 1;
+ const uint16_t s_timeout =
+ achordion_streak_chord_timeout(tap_hold_keycode, keycode);
+ const bool is_streak =
+ streak_timer && s_timeout &&
+ !timer_expired(record->event.time, (streak_timer + s_timeout));
#endif
// Press event occurred on a key other than the active tap-hold key.
@@ -167,10 +203,23 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
// events back into the handling pipeline so that QMK features and other
// user code can see them. This is done by calling `process_record()`, which
// in turn calls most handlers including `process_record_user()`.
- if (!is_streak && (!is_key_event || (is_tap_hold && record->tap.count == 0) ||
- achordion_chord(tap_hold_keycode, &tap_hold_record, keycode, record))) {
+ if (!is_streak &&
+ (!is_key_event || (is_tap_hold && record->tap.count == 0) ||
+ achordion_chord(tap_hold_keycode, &tap_hold_record, keycode,
+ record))) {
dprintln("Achordion: Plumbing hold press.");
settle_as_hold();
+
+#ifdef REPEAT_KEY_ENABLE
+ // Edge case involving LT + Repeat Key: in a sequence of "LT down, other
+ // down" where "other" is on the other layer in the same position as
+ // Repeat or Alternate Repeat, the repeated keycode is set instead of the
+ // the one on the switched-to layer. Here we correct that.
+ if (get_repeat_key_count() != 0 && IS_QK_LAYER_TAP(tap_hold_keycode)) {
+ record->keycode = KC_NO; // Forget the repeated keycode.
+ clear_weak_mods();
+ }
+#endif // REPEAT_KEY_ENABLE
} else {
clear_eager_mods(); // Clear in case eager mods were set.
@@ -189,6 +238,21 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
tap_hold_record.event.pressed = false;
// Plumb tap release event.
recursively_process_record(&tap_hold_record, STATE_TAPPING);
+#ifdef ACHORDION_STREAK
+ update_streak_timer(keycode, record);
+ if (is_streak && is_key_event && is_tap_hold && record->tap.count == 0) {
+ // If we are in a streak and resolved the current tap-hold key as a tap
+ // consider the next tap-hold key as active to be resolved next.
+ update_streak_timer(tap_hold_keycode, &tap_hold_record);
+ const uint16_t timeout = achordion_timeout(keycode);
+ tap_hold_keycode = keycode;
+ tap_hold_record = *record;
+ hold_timer = record->event.time + timeout;
+ achordion_state = STATE_UNSETTLED;
+ pressed_another_key_before_release = false;
+ return false;
+ }
+#endif
}
recursively_process_record(record, achordion_state); // Re-process event.
@@ -197,7 +261,7 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
#ifdef ACHORDION_STREAK
// update idle timer on regular keys event
- streak_timer = (timer_read() + achordion_streak_timeout(keycode)) | 1;
+ update_streak_timer(keycode, record);
#endif
return true;
}
@@ -210,7 +274,9 @@ void achordion_task(void) {
}
#ifdef ACHORDION_STREAK
- if (streak_timer && timer_expired(timer_read(), streak_timer)) {
+#define MAX_STREAK_TIMEOUT 800
+ if (streak_timer &&
+ timer_expired(timer_read(), (streak_timer + MAX_STREAK_TIMEOUT))) {
streak_timer = 0; // Expired.
}
#endif
@@ -252,8 +318,34 @@ __attribute__((weak)) bool achordion_eager_mod(uint8_t mod) {
}
#ifdef ACHORDION_STREAK
-__attribute__((weak)) uint16_t achordion_streak_timeout(uint16_t tap_hold_keycode) {
- return 100; // Default of 100 ms.
+__attribute__((weak)) bool achordion_streak_continue(uint16_t keycode) {
+ // If any mods other than shift or AltGr are held, don't continue the streak
+ if (get_mods() & (MOD_MASK_CG | MOD_BIT_LALT)) return false;
+ // This function doesn't get called for holds, so convert to tap version of
+ // keycodes
+ if (IS_QK_MOD_TAP(keycode)) keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode);
+ if (IS_QK_LAYER_TAP(keycode)) keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode);
+ // Regular letters and punctuation continue the streak.
+ if (keycode >= KC_A && keycode <= KC_Z) return true;
+ switch (keycode) {
+ case KC_DOT:
+ case KC_COMMA:
+ case KC_QUOTE:
+ case KC_SPACE:
+ return true;
+ }
+ // All other keys end the streak
+ return false;
+}
+
+__attribute__((weak)) uint16_t achordion_streak_chord_timeout(
+ uint16_t tap_hold_keycode, uint16_t next_keycode) {
+ return achordion_streak_timeout(tap_hold_keycode);
+}
+
+__attribute__((weak)) uint16_t
+achordion_streak_timeout(uint16_t tap_hold_keycode) {
+ return 200;
}
#endif
diff --git a/common/achordion.h b/common/achordion.h
index dfa8dc2..f1e08ec 100644
--- a/common/achordion.h
+++ b/common/achordion.h
@@ -1,4 +1,4 @@
-// Copyright 2022-2023 Google LLC
+// Copyright 2022-2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -53,27 +53,6 @@
#include "quantum.h"
-/**
- * Suppress tap-hold mods within a *typing streak* by defining
- * ACHORDION_STREAK. This can help preventing accidental mod
- * activation when performing a fast tapping sequence.
- * This is inspired by https://sunaku.github.io/home-row-mods.html#typing-streaks
- *
- * Enable with:
- *
- * #define ACHORDION_STREAK
- *
- * Adjust the maximum time between key events before modifiers can be enabled
- * by defining the following callback in your keymap.c:
- *
- * uint16_t achordion_streak_timeout(uint16_t tap_hold_keycode) {
- * return 100; // Default of 100 ms.
- * }
- */
-#ifdef ACHORDION_STREAK
-uint16_t achordion_streak_timeout(uint16_t tap_hold_keycode);
-#endif
-
#ifdef __cplusplus
extern "C" {
#endif
@@ -163,7 +142,7 @@ uint16_t achordion_timeout(uint16_t tap_hold_keycode);
* }
*
* @note `mod` should be compared with `MOD_` prefixed codes, not `KC_` codes,
- * described at <https://docs.qmk.fm/#/mod_tap>.
+ * described at <https://docs.qmk.fm/mod_tap>.
*
* @param mod Modifier `MOD_` code.
* @return True if the modifier should be eagerly applied.
@@ -180,6 +159,35 @@ bool achordion_eager_mod(uint8_t mod);
bool achordion_opposite_hands(const keyrecord_t* tap_hold_record,
const keyrecord_t* other_record);
+/**
+ * Suppress tap-hold mods within a *typing streak* by defining
+ * ACHORDION_STREAK. This can help preventing accidental mod
+ * activation when performing a fast tapping sequence.
+ * This is inspired by
+ * https://sunaku.github.io/home-row-mods.html#typing-streaks
+ *
+ * Enable with:
+ *
+ * #define ACHORDION_STREAK
+ *
+ * Adjust the maximum time between key events before modifiers can be enabled
+ * by defining the following callback in your keymap.c:
+ *
+ * uint16_t achordion_streak_chord_timeout(
+ * uint16_t tap_hold_keycode, uint16_t next_keycode) {
+ * return 200; // Default of 200 ms.
+ * }
+ */
+#ifdef ACHORDION_STREAK
+uint16_t achordion_streak_chord_timeout(uint16_t tap_hold_keycode,
+ uint16_t next_keycode);
+
+bool achordion_streak_continue(uint16_t keycode);
+
+/** @deprecated Use `achordion_streak_chord_timeout()` instead. */
+uint16_t achordion_streak_timeout(uint16_t tap_hold_keycode);
+#endif
+
#ifdef __cplusplus
}
#endif