refactor one shot II

This commit is contained in:
Kawamashi 2025-10-14 20:06:11 +02:00
commit c308579282
4 changed files with 90 additions and 75 deletions

View file

@ -1,38 +1,38 @@
#include "oneshot.h" #include "oneshot.h"
oneshot_state os_shft_state = os_up_unqueued; oneshot_state os_shft_state = os_idle;
oneshot_state os_ctrl_state = os_up_unqueued; oneshot_state os_ctrl_state = os_idle;
oneshot_state os_alt_state = os_up_unqueued; oneshot_state os_alt_state = os_idle;
oneshot_state os_win_state = os_up_unqueued; oneshot_state os_win_state = os_idle;
bool process_oneshot(uint16_t keycode, keyrecord_t *record){ bool process_oneshot(uint16_t keycode, keyrecord_t *record){
uint8_t mods = one_shot_get_mod(keycode); uint8_t mods = one_shot_get_mod(keycode);
switch (mods) { switch (mods) {
case MOD_BIT(KC_LSFT): case KC_LSFT:
return process_oneshot_keys(record, MOD_BIT(KC_LSFT), &os_shft_state); return process_oneshot_keys(record, KC_LSFT, &os_shft_state);
case MOD_BIT(KC_LCTL): case KC_LCTL:
return process_oneshot_keys(record, MOD_BIT(KC_LCTL), &os_ctrl_state); return process_oneshot_keys(record, KC_LCTL, &os_ctrl_state);
case MOD_BIT(KC_LALT): case KC_LALT:
return process_oneshot_keys(record, MOD_BIT(KC_LALT), &os_alt_state); return process_oneshot_keys(record, KC_LALT, &os_alt_state);
case MOD_BIT(KC_LWIN): case KC_LWIN:
return process_oneshot_keys(record, MOD_BIT(KC_LWIN), &os_win_state); return process_oneshot_keys(record, KC_LWIN, &os_win_state);
} }
mods = get_mods(); mods = get_mods();
if (mods & MOD_BIT(KC_LSFT)) { if (mods & MOD_BIT(KC_LSFT)) {
process_mods(keycode, record, MOD_BIT(KC_LSFT), &os_shft_state); process_mods(keycode, record, KC_LSFT, &os_shft_state);
} }
if (mods & MOD_BIT(KC_LCTL)) { if (mods & MOD_BIT(KC_LCTL)) {
process_mods(keycode, record, MOD_BIT(KC_LCTL), &os_ctrl_state); process_mods(keycode, record, KC_LCTL, &os_ctrl_state);
} }
if (mods & MOD_BIT(KC_LALT)) { if (mods & MOD_BIT(KC_LALT)) {
process_mods(keycode, record, MOD_BIT(KC_LALT), &os_alt_state); process_mods(keycode, record, KC_LALT, &os_alt_state);
} }
if (mods & MOD_BIT(KC_LWIN)) { if (mods & MOD_BIT(KC_LWIN)) {
process_mods(keycode, record, MOD_BIT(KC_LWIN), &os_win_state); process_mods(keycode, record, KC_LWIN, &os_win_state);
} }
return true; return true;
} }
@ -41,8 +41,8 @@ bool process_oneshot_keys(keyrecord_t *record, uint8_t mod, oneshot_state *state
if (record->event.pressed) { if (record->event.pressed) {
// Trigger keydown // Trigger keydown
if (*state == os_up_unqueued) { if (*state == os_idle) {
register_mods(mod); register_code(mod);
} }
*state = os_down_unused; *state = os_down_unused;
} else { } else {
@ -54,8 +54,8 @@ bool process_oneshot_keys(keyrecord_t *record, uint8_t mod, oneshot_state *state
break; break;
case os_down_used: case os_down_used:
// If we did use the mod while trigger was held, unregister it. // If we did use the mod while trigger was held, unregister it.
*state = os_up_unqueued; *state = os_idle;
unregister_mods(mod); unregister_code(mod);
break; break;
default: default:
break; break;
@ -67,18 +67,18 @@ bool process_oneshot_keys(keyrecord_t *record, uint8_t mod, oneshot_state *state
void process_mods(uint16_t keycode, keyrecord_t *record, uint8_t mod, oneshot_state *state) { void process_mods(uint16_t keycode, keyrecord_t *record, uint8_t mod, oneshot_state *state) {
if (is_oneshot_cancel_key(keycode)) { if (is_oneshot_cancel_key(keycode)) {
if (record->event.pressed && *state != os_up_unqueued) { if (record->event.pressed && *state != os_idle) {
// Cancel oneshot on designated cancel keydown. // Cancel oneshot on designated cancel keydown.
*state = os_up_unqueued; *state = os_idle;
unregister_mods(mod); unregister_code(mod);
} }
} else if (!is_oneshot_ignored_key(keycode)) { } else if (!is_oneshot_ignored_key(keycode)) {
// Regular key released / roll between two regular keys // Regular key released / roll between two regular keys
if (*state == os_up_queued_used) { if (*state == os_up_queued_used) {
*state = os_up_unqueued; *state = os_idle;
unregister_mods(mod); unregister_code(mod);
} else if (record->event.pressed) { } else if (record->event.pressed) {
// Regular key pressed // Regular key pressed
@ -95,8 +95,8 @@ void process_mods(uint16_t keycode, keyrecord_t *record, uint8_t mod, oneshot_st
break; break;
// Roll between a mod key and a regular key // Roll between a mod key and a regular key
case os_up_queued: case os_up_queued:
*state = os_up_unqueued; *state = os_idle;
unregister_mods(mod); unregister_code(mod);
break; break;
default: default:
break; break;
@ -105,20 +105,28 @@ void process_mods(uint16_t keycode, keyrecord_t *record, uint8_t mod, oneshot_st
} }
} }
void process_oneshot_old(uint16_t keycode, keyrecord_t *record){ bool process_oneshot_old(uint16_t keycode, keyrecord_t *record){
// Handling Callum's OSM on OS4A layers // Handling Callum's OSM on OS4A layers
update_oneshot(&os_shft_state, KC_LSFT, OS_SHFT, keycode, record); if (!update_oneshot(&os_shft_state, KC_LSFT, OS_SHFT, keycode, record)) { return false; }
if (!update_oneshot(&os_ctrl_state, KC_LCTL, OS_CTRL, keycode, record)) { return false; }
if (!update_oneshot(&os_alt_state, KC_LALT, OS_LALT, keycode, record)) { return false; }
if (!update_oneshot(&os_win_state, KC_LWIN, OS_WIN, keycode, record)) { return false; }
return true;
/* update_oneshot(&os_shft_state, KC_LSFT, OS_SHFT, keycode, record);
update_oneshot(&os_ctrl_state, KC_LCTL, OS_CTRL, keycode, record); update_oneshot(&os_ctrl_state, KC_LCTL, OS_CTRL, keycode, record);
update_oneshot(&os_alt_state, KC_LALT, OS_LALT, keycode, record); update_oneshot(&os_alt_state, KC_LALT, OS_LALT, keycode, record);
update_oneshot(&os_win_state, KC_LWIN, OS_WIN, keycode, record); update_oneshot(&os_win_state, KC_LWIN, OS_WIN, keycode, record); */
} }
void update_oneshot(oneshot_state *state, uint16_t mod, uint16_t trigger, uint16_t keycode, keyrecord_t *record) { bool update_oneshot(oneshot_state *state, uint16_t mod, uint16_t trigger, uint16_t keycode, keyrecord_t *record) {
//const uint8_t mods = get_mods();
if (keycode == trigger) { if (keycode == trigger) {
if (record->event.pressed) { if (record->event.pressed) {
// Trigger keydown // Trigger keydown
if (*state == os_up_unqueued) { if (*state == os_idle) {
register_code(mod); register_code(mod);
} }
*state = os_down_unused; *state = os_down_unused;
@ -131,48 +139,54 @@ void update_oneshot(oneshot_state *state, uint16_t mod, uint16_t trigger, uint16
break; break;
case os_down_used: case os_down_used:
// If we did use the mod while trigger was held, unregister it. // If we did use the mod while trigger was held, unregister it.
*state = os_up_unqueued; *state = os_idle;
unregister_code(mod); unregister_code(mod);
break; break;
default: default:
break; break;
} }
} }
} else if (is_oneshot_cancel_key(keycode)) { return false;
if (record->event.pressed && *state != os_up_unqueued) {
// Cancel oneshot on designated cancel keydown.
*state = os_up_unqueued;
unregister_code(mod);
}
} else if (!is_oneshot_ignored_key(keycode)) { } else if (*state != os_idle) {
// Regular key released / roll between two regular keys
if (*state == os_up_queued_used) {
*state = os_up_unqueued;
unregister_code(mod);
} else if (record->event.pressed) { if (is_oneshot_cancel_key(keycode)) {
// Regular key pressed if (record->event.pressed) {// && *state != os_idle) {
if (*state == os_up_queued) { // Cancel oneshot on designated cancel keydown.
*state = os_up_queued_used; *state = os_idle;
unregister_code(mod);
} }
} else { } else if (!is_oneshot_ignored_key(keycode)) {
// Regular key release
switch (*state) { // Regular key released / roll between two regular keys
// When the mod key is still pressed if (*state == os_up_queued_used) {
case os_down_unused: *state = os_idle;
*state = os_down_used; unregister_code(mod);
break;
// Roll between a mod key and a regular key } else if (record->event.pressed) {
case os_up_queued: // Regular key pressed
*state = os_up_unqueued; if (*state == os_up_queued) {
unregister_code(mod); *state = os_up_queued_used;
break; }
default:
break; } else {
// Regular key release
switch (*state) {
// When the mod key is still pressed
case os_down_unused:
*state = os_down_used;
break;
// Roll between a mod key and a regular key
case os_up_queued:
*state = os_idle;
unregister_code(mod);
break;
default:
break;
}
} }
} }
} }
return true;
} }

View file

@ -5,7 +5,7 @@
// Represents the five states a oneshot key can be in // Represents the five states a oneshot key can be in
typedef enum { typedef enum {
os_up_unqueued, os_idle,
os_up_queued, os_up_queued,
os_up_queued_used, os_up_queued_used,
os_down_unused, os_down_unused,
@ -14,7 +14,7 @@ typedef enum {
uint8_t one_shot_get_mod(uint16_t keycode); uint8_t one_shot_get_mod(uint16_t keycode);
void process_oneshot_old(uint16_t keycode, keyrecord_t *record); bool process_oneshot_old(uint16_t keycode, keyrecord_t *record);
bool process_oneshot(uint16_t keycode, keyrecord_t *record); bool process_oneshot(uint16_t keycode, keyrecord_t *record);
bool process_oneshot_keys(keyrecord_t *record, uint8_t mod, oneshot_state *state); bool process_oneshot_keys(keyrecord_t *record, uint8_t mod, oneshot_state *state);
void process_mods(uint16_t keycode, keyrecord_t *record, uint8_t mod, oneshot_state *state); void process_mods(uint16_t keycode, keyrecord_t *record, uint8_t mod, oneshot_state *state);
@ -22,7 +22,7 @@ void process_mods(uint16_t keycode, keyrecord_t *record, uint8_t mod, oneshot_st
// Custom oneshot mod implementation that doesn't rely on timers. If a mod is // Custom oneshot mod implementation that doesn't rely on timers. If a mod is
// used while it is held it will be unregistered on keyup as normal, otherwise // used while it is held it will be unregistered on keyup as normal, otherwise
// it will be queued and only released after the next non-mod keyup. // it will be queued and only released after the next non-mod keyup.
void update_oneshot( bool update_oneshot(
oneshot_state *state, oneshot_state *state,
uint16_t mod, uint16_t mod,
uint16_t trigger, uint16_t trigger,

View file

@ -134,21 +134,21 @@ bool is_oneshot_cancel_key(uint16_t keycode) {
} }
} }
uint8_t one_shot_get_mod(uint16_t keycode) { /* uint8_t one_shot_get_mod(uint16_t keycode) {
switch (keycode) { switch (keycode) {
case OS_SHFT: case OS_SHFT:
return MOD_BIT(KC_LSFT); return KC_LSFT;
case OS_CTRL: case OS_CTRL:
return MOD_BIT(KC_LCTL); return KC_LCTL;
case OS_LALT: case OS_LALT:
return MOD_BIT(KC_LALT); return KC_LALT;
case OS_WIN: case OS_WIN:
return MOD_BIT(KC_LWIN); return KC_LWIN;
default: default:
return 0; return KC_NO;
} }
} } */
bool is_oneshot_ignored_key(uint16_t keycode) { bool is_oneshot_ignored_key(uint16_t keycode) {

View file

@ -92,7 +92,8 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) {
} }
// Callum Mods // Callum Mods
if (!process_oneshot(keycode, record)) { return false; } //if (!process_oneshot(keycode, record)) { return false; }
if (!process_oneshot_old(keycode, record)) { return false; };
// Multi One-Shot Mods // Multi One-Shot Mods
if (!process_os4a(keycode, record)) { return false; } if (!process_os4a(keycode, record)) { return false; }