Skip to content

Commit

Permalink
[Keyrecords] Add Pre-process and move accordion there
Browse files Browse the repository at this point in the history
  • Loading branch information
drashna committed Jan 15, 2024
1 parent 46e8fc4 commit c272b0f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
53 changes: 36 additions & 17 deletions users/drashna/keyrecords/achordion.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ static uint16_t hold_timer = 0;
// Eagerly applied mods, if any.
static uint8_t eager_mods = 0;

#ifdef ACHORDION_STREAK
// Timer for typing streak
static uint16_t streak_timer = 0;
#else
// When disabled, is_streak is never true
# define is_streak false
#endif

// Achordion's current state.
enum {
STATE_UNSETTLED, // A tap-hold key is pressed, but hasn't yet been settled as tapped or held.
Expand Down Expand Up @@ -103,6 +111,9 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
}
}

#ifdef ACHORDION_STREAK
streak_timer = (timer_read() + achordion_streak_timeout(keycode)) | 1;
#endif
return true; // Otherwise, continue with default handling.
}

Expand All @@ -125,6 +136,11 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
}

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;
#endif

// Press event occurred on a key other than the active tap-hold key.

// If the other key is *also* a tap-hold key and considered by QMK to be
Expand All @@ -137,8 +153,8 @@ 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_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();
} else {
Expand All @@ -165,6 +181,10 @@ bool process_achordion(uint16_t keycode, keyrecord_t* record) {
return false; // Block the original event.
}

#ifdef ACHORDION_STREAK
// update idle timer on regular keys event
streak_timer = (timer_read() + achordion_streak_timeout(keycode)) | 1;
#endif
return true;
}

Expand All @@ -173,6 +193,12 @@ void achordion_task(void) {
dprintln("Achordion: Timeout. Plumbing hold press.");
settle_as_hold(); // Timeout expired, settle the key as held.
}

#ifdef ACHORDION_STREAK
if (streak_timer && timer_expired(timer_read(), streak_timer)) {
streak_timer = 0; // Expired.
}
#endif
}

// Returns true if `pos` on the left hand of the keyboard, false if right.
Expand All @@ -192,21 +218,6 @@ bool achordion_opposite_hands(const keyrecord_t* tap_hold_record, const keyrecor
// "held" only when it and the other key are on opposite hands.
__attribute__((weak)) bool achordion_chord(uint16_t tap_hold_keycode, keyrecord_t* tap_hold_record,
uint16_t other_keycode, keyrecord_t* other_record) {
// Exceptionally consider the following chords as holds, even though they
// are on the same hand in Dvorak.
switch (tap_hold_keycode) {
case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
return true;
}

// Also allow same-hand holds when the other key is in the rows below the
// alphas. I need the `% (MATRIX_ROWS / 2)` because my keyboard is split.
if (other_record->event.key.row % (MATRIX_ROWS / 2) >= 4) {
return true;
}

// Otherwise, follow the opposite hands rule.
return achordion_opposite_hands(tap_hold_record, other_record);
}

Expand All @@ -219,3 +230,11 @@ __attribute__((weak)) uint16_t achordion_timeout(uint16_t tap_hold_keycode) {
__attribute__((weak)) bool achordion_eager_mod(uint8_t mod) {
return (mod & (MOD_LALT | MOD_LGUI)) == 0;
}

#ifdef ACHORDION_STREAK
__attribute__((weak)) uint16_t achordion_streak_timeout(uint16_t tap_hold_keycode) {
return 100; // Defa
ult of 100 ms.
}

#endif
22 changes: 16 additions & 6 deletions users/drashna/keyrecords/process_records.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
// SPDX-License-Identifier: GPL-3.0-or-later

#include "process_records.h"
#include "drashna.h"
#include "version.h"
#ifdef OS_DETECTION_ENABLE
Expand All @@ -17,6 +18,20 @@ uint16_t copy_paste_timer;
// Defines actions tor my global custom keycodes. Defined in drashna.h file
// Then runs the _keymap's record handier if not processed here

__attribute__((weak)) bool pre_process_record_keymap(uint16_t keycode, keyrecord_t *record) {
return true;
}

bool pre_process_record_user(uint16_t keycode, keyrecord_t *record) {
#ifdef ACHORDION_ENABLE
if (!process_achordion(keycode, record)) {
return false;
}
#endif

return pre_process_record_keymap(keycode, record);
}

/**
* @brief Keycode handler for keymaps
*
Expand Down Expand Up @@ -52,11 +67,6 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
}
}
#endif
#ifdef ACHORDION_ENABLE
if (!process_achordion(keycode, record)) {
return false;
}
#endif

// If console is enabled, it will print the matrix position and status of each key pressed
#ifdef KEYLOGGER_ENABLE
Expand Down Expand Up @@ -97,7 +107,7 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
&& process_sentence_case(keycode, record)
#endif
#ifdef ORBITAL_MOUSE_ENABLE
&& process_orbital_mouse(keycode, record)
&& process_orbital_mouse(keycode, record)
#endif
&& true)) {
return false;
Expand Down
1 change: 1 addition & 0 deletions users/drashna/keyrecords/process_records.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ enum userspace_custom_keycodes {
};

bool process_record_secrets(uint16_t keycode, keyrecord_t *record);
bool pre_process_record_keymap(uint16_t keycode, keyrecord_t *record);
bool process_record_keymap(uint16_t keycode, keyrecord_t *record);
void post_process_record_keymap(uint16_t keycode, keyrecord_t *record);
#ifdef CUSTOM_UNICODE_ENABLE
Expand Down

0 comments on commit c272b0f

Please sign in to comment.