diff --git a/src/main.c b/src/main.c index ff5d467..86ed799 100644 --- a/src/main.c +++ b/src/main.c @@ -65,19 +65,17 @@ static bool current_is_pressed; static bool layers_active[KB_LAYERS]; static layer layers_top = 0; -static bool sticky_on; -static bool sticky_done; +static bool layer_sticky_on; static bool layer_sticky[KB_LAYERS]; -static u8 modifier_sticky; +static bool layer_sticky_done; +static u8 mod_sticky; +static bool mod_sticky_done; // ---------------------------------------------------------------------------- int main() { - kb_init(); - usb_init(); - while (!usb_configured()); - // initialize + init_hw(); init_layers(); init_sticky(); @@ -127,9 +125,22 @@ void main_key_loop() { usb_extra_consumer_send(); // unset sticky keys if necessary - if (sticky_on && sticky_done) { - sticky_disable(); + if (layer_sticky_on && layer_sticky_done) { + for (layer l=1; l < KB_LAYERS; l++) { + if (layer_sticky[l]) { + layer_disable(l); + layer_sticky[l] = false; + } + } + layer_sticky_on = false; + layer_sticky_done = false; + } + + if (mod_sticky && mod_sticky_done) { + keyboard_modifier_keys &= ~mod_sticky; usb_keyboard_send(); + mod_sticky = 0; + mod_sticky_done = false; } // debounce in ms; see keyswitch spec for necessary value @@ -138,9 +149,25 @@ void main_key_loop() { } // ---------------------------------------------------------------------------- -// layer functions +// init functions // ---------------------------------------------------------------------------- +void init_hw() { + kb_init(); + usb_init(); + while (!usb_configured()); +} + +void init_sticky() { + for (layer l=1; l < KB_LAYERS; l++) { + layer_sticky[l] = false; + } + layer_sticky_on = false; + mod_sticky = 0; + layer_sticky_done = false; + mod_sticky_done = false; +} + void init_layers() { for (layer l=0; l < KB_LAYERS; l++) { layers_active[l] = false; @@ -148,6 +175,9 @@ void init_layers() { layers_active[0] = true; } +// ---------------------------------------------------------------------------- +// layer functions +// ---------------------------------------------------------------------------- // find highest active layer layer highest_active_layer(layer offset) { @@ -224,38 +254,6 @@ void layer_enable_upto(layer max_layer) { } } -// ---------------------------------------------------------------------------- -// sticky functions -// ---------------------------------------------------------------------------- - -void init_sticky() { - for (layer l=1; l < KB_LAYERS; l++) { - layer_sticky[l] = false; - } - for (keycode mod=0; mod < MODIFIERS; mod++) { - modifier_sticky = 0; - } - sticky_on = false; - sticky_done = true; -} - -void sticky_disable() { - for (layer l=1; l < KB_LAYERS; l++) { - if (layer_sticky[l]) { - layer_disable(l); - layer_sticky[l] = false; - } - } - - if (modifier_sticky) { - keyboard_modifier_keys &= ~modifier_sticky; - modifier_sticky = 0; - } - - sticky_on = false; - sticky_done = false; -} - // ---------------------------------------------------------------------------- // layout info // ---------------------------------------------------------------------------- @@ -327,6 +325,11 @@ bool _kbfun_modifier_is_pressed(keycode key) { return (keyboard_modifier_keys & (1<