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"
oneshot_state os_shft_state = os_up_unqueued;
oneshot_state os_ctrl_state = os_up_unqueued;
oneshot_state os_alt_state = os_up_unqueued;
oneshot_state os_win_state = os_up_unqueued;
oneshot_state os_shft_state = os_idle;
oneshot_state os_ctrl_state = os_idle;
oneshot_state os_alt_state = os_idle;
oneshot_state os_win_state = os_idle;
bool process_oneshot(uint16_t keycode, keyrecord_t *record){
uint8_t mods = one_shot_get_mod(keycode);
switch (mods) {
case MOD_BIT(KC_LSFT):
return process_oneshot_keys(record, MOD_BIT(KC_LSFT), &os_shft_state);
case MOD_BIT(KC_LCTL):
return process_oneshot_keys(record, MOD_BIT(KC_LCTL), &os_ctrl_state);
case MOD_BIT(KC_LALT):
return process_oneshot_keys(record, MOD_BIT(KC_LALT), &os_alt_state);
case MOD_BIT(KC_LWIN):
return process_oneshot_keys(record, MOD_BIT(KC_LWIN), &os_win_state);
case KC_LSFT:
return process_oneshot_keys(record, KC_LSFT, &os_shft_state);
case KC_LCTL:
return process_oneshot_keys(record, KC_LCTL, &os_ctrl_state);
case KC_LALT:
return process_oneshot_keys(record, KC_LALT, &os_alt_state);
case KC_LWIN:
return process_oneshot_keys(record, KC_LWIN, &os_win_state);
}
mods = get_mods();
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)) {
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)) {
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)) {
process_mods(keycode, record, MOD_BIT(KC_LWIN), &os_win_state);
process_mods(keycode, record, KC_LWIN, &os_win_state);
}
return true;
}
@ -41,8 +41,8 @@ bool process_oneshot_keys(keyrecord_t *record, uint8_t mod, oneshot_state *state
if (record->event.pressed) {
// Trigger keydown
if (*state == os_up_unqueued) {
register_mods(mod);
if (*state == os_idle) {
register_code(mod);
}
*state = os_down_unused;
} else {
@ -54,8 +54,8 @@ bool process_oneshot_keys(keyrecord_t *record, uint8_t mod, oneshot_state *state
break;
case os_down_used:
// If we did use the mod while trigger was held, unregister it.
*state = os_up_unqueued;
unregister_mods(mod);
*state = os_idle;
unregister_code(mod);
break;
default:
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) {
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.
*state = os_up_unqueued;
unregister_mods(mod);
*state = os_idle;
unregister_code(mod);
}
} else if (!is_oneshot_ignored_key(keycode)) {
// Regular key released / roll between two regular keys
if (*state == os_up_queued_used) {
*state = os_up_unqueued;
unregister_mods(mod);
*state = os_idle;
unregister_code(mod);
} else if (record->event.pressed) {
// Regular key pressed
@ -95,8 +95,8 @@ void process_mods(uint16_t keycode, keyrecord_t *record, uint8_t mod, oneshot_st
break;
// Roll between a mod key and a regular key
case os_up_queued:
*state = os_up_unqueued;
unregister_mods(mod);
*state = os_idle;
unregister_code(mod);
break;
default:
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
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_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 (record->event.pressed) {
// Trigger keydown
if (*state == os_up_unqueued) {
if (*state == os_idle) {
register_code(mod);
}
*state = os_down_unused;
@ -131,48 +139,54 @@ void update_oneshot(oneshot_state *state, uint16_t mod, uint16_t trigger, uint16
break;
case os_down_used:
// If we did use the mod while trigger was held, unregister it.
*state = os_up_unqueued;
*state = os_idle;
unregister_code(mod);
break;
default:
break;
}
}
} else if (is_oneshot_cancel_key(keycode)) {
if (record->event.pressed && *state != os_up_unqueued) {
// Cancel oneshot on designated cancel keydown.
*state = os_up_unqueued;
unregister_code(mod);
}
return false;
} else if (!is_oneshot_ignored_key(keycode)) {
// Regular key released / roll between two regular keys
if (*state == os_up_queued_used) {
*state = os_up_unqueued;
unregister_code(mod);
} else if (*state != os_idle) {
} else if (record->event.pressed) {
// Regular key pressed
if (*state == os_up_queued) {
*state = os_up_queued_used;
if (is_oneshot_cancel_key(keycode)) {
if (record->event.pressed) {// && *state != os_idle) {
// Cancel oneshot on designated cancel keydown.
*state = os_idle;
unregister_code(mod);
}
} 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_up_unqueued;
unregister_code(mod);
break;
default:
break;
} else if (!is_oneshot_ignored_key(keycode)) {
// Regular key released / roll between two regular keys
if (*state == os_up_queued_used) {
*state = os_idle;
unregister_code(mod);
} else if (record->event.pressed) {
// Regular key pressed
if (*state == os_up_queued) {
*state = os_up_queued_used;
}
} 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
typedef enum {
os_up_unqueued,
os_idle,
os_up_queued,
os_up_queued_used,
os_down_unused,
@ -14,7 +14,7 @@ typedef enum {
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_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);
@ -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
// 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.
void update_oneshot(
bool update_oneshot(
oneshot_state *state,
uint16_t mod,
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) {
case OS_SHFT:
return MOD_BIT(KC_LSFT);
return KC_LSFT;
case OS_CTRL:
return MOD_BIT(KC_LCTL);
return KC_LCTL;
case OS_LALT:
return MOD_BIT(KC_LALT);
return KC_LALT;
case OS_WIN:
return MOD_BIT(KC_LWIN);
return KC_LWIN;
default:
return 0;
return KC_NO;
}
}
} */
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
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
if (!process_os4a(keycode, record)) { return false; }