Skip to content

Commit

Permalink
Fix flaky unshifting
Browse files Browse the repository at this point in the history
  • Loading branch information
p00ya committed Jul 28, 2020
1 parent 7bae20a commit 682fb71
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions quantum/process_keycode/process_auto_shift.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,30 @@ void autoshift_timer_report(void) {
send_string((const char *)display);
}

/** \brief Resets the autoshift state and releases the shift key */
/** \brief Resets the autoshift state */
static void autoshift_flush(uint16_t now) {
if (autoshift_flags.in_progress && !autoshift_flags.registered) {
// Register the key.
register_code(autoshift_lastkey);
# if TAP_CODE_DELAY > 0
wait_ms(TAP_CODE_DELAY);
# endif
} else if (autoshift_flags.holding_shift) {
// Release the shift key if it was simulated.
unregister_code(KC_LSFT);
}
// Roll the autoshift_time forward for detecting tap-and-hold.
autoshift_time = now;
autoshift_flags.in_progress = false;
autoshift_flags.holding_shift = false;
autoshift_flags.registered = false;
}

/** \brief Releases the shift key if it was held by auto-shift */
static void autoshift_flush_shift(void) {
if (autoshift_flags.holding_shift) {
// Release the shift key if it was simulated.
unregister_code(KC_LSFT);
}
autoshift_flags.holding_shift = false;
}

/** \brief Record the press of an autoshiftable key
*
* \return Whether the record should be further processed.
Expand All @@ -74,16 +79,14 @@ static bool autoshift_press(uint16_t keycode, keyrecord_t *record) {
return true;
}

const uint16_t elapsed = TIMER_DIFF_16(record->event.time, autoshift_time);
autoshift_flush(record->event.time);

# ifndef AUTO_SHIFT_MODIFIERS
if (get_mods()) {
if (get_mods() ^ (autoshift_flags.holding_shift ? MOD_BIT(KC_LSFT) : 0)) {
return true;
}
# endif

const uint16_t elapsed = TIMER_DIFF_16(record->event.time, autoshift_time);

# ifndef TAPPING_FORCE_HOLD
if (elapsed < TAPPING_TERM && keycode == autoshift_lastkey && !autoshift_flags.lastshifted) {
// Allow a tap-then-hold to hold the unshifted key.
Expand All @@ -93,7 +96,6 @@ static bool autoshift_press(uint16_t keycode, keyrecord_t *record) {
# endif

// Record the keycode so we can simulate it later.
autoshift_time = record->event.time;
autoshift_lastkey = keycode;
autoshift_flags.in_progress = true;

Expand Down Expand Up @@ -146,15 +148,16 @@ static void autoshift_check_record(uint16_t keycode, keyrecord_t *record) {

// Process the release of the autoshiftable key.
if (keycode == autoshift_lastkey && !record->event.pressed) {
autoshift_flush_shift();
// Time since the initial press was recorded.
const uint16_t elapsed = TIMER_DIFF_16(record->event.time, autoshift_time);
if (!autoshift_flags.registered && elapsed < autoshift_timeout) {
autoshift_flags.lastshifted = false;
// Auto-shiftable key is being released before the shift timeout;
// simulate the original press then let the usual processing take
// care of the release.
register_code(keycode);
autoshift_flags.registered = true;
autoshift_flags.lastshifted = false;
autoshift_flags.registered = true;
# if TAP_CODE_DELAY > 0
wait_ms(TAP_CODE_DELAY);
# endif
Expand All @@ -167,9 +170,13 @@ static void autoshift_check_record(uint16_t keycode, keyrecord_t *record) {
autoshift_flags.holding_shift = false;
} else if (!autoshift_flags.registered) {
// If the key isn't registered yet, it means the timeout hasn't
// elapsed, so register the unshifted key.
// elapsed, so register the key without additional shifting.
autoshift_flush(0);
autoshift_lastkey = KC_NO;
} else {
// The key is registered; flush any shift state for the
// non-autoshiftable key.
autoshift_flush_shift();
}
}
}
Expand All @@ -190,17 +197,20 @@ void autoshift_matrix_scan(void) {

void autoshift_enable(void) {
autoshift_flags.enabled = true;
autoshift_flush_shift();
autoshift_flush(0);
}

void autoshift_disable(void) {
autoshift_flags.enabled = false;
autoshift_flush_shift();
autoshift_flush(0);
}

void autoshift_toggle(void) {
if (autoshift_flags.enabled) {
autoshift_flags.enabled = false;
autoshift_flush_shift();
autoshift_flush(0);
} else {
autoshift_flags.enabled = true;
Expand Down

0 comments on commit 682fb71

Please sign in to comment.