split {mod,layer}_sticky_on

master
Stefan Dorn 2016-06-14 12:38:17 +01:00
parent e8c5f2a69d
commit 90bb49c27d
2 changed files with 61 additions and 60 deletions

View File

@ -65,19 +65,17 @@ static bool current_is_pressed;
static bool layers_active[KB_LAYERS]; static bool layers_active[KB_LAYERS];
static layer layers_top = 0; static layer layers_top = 0;
static bool sticky_on; static bool layer_sticky_on;
static bool sticky_done;
static bool layer_sticky[KB_LAYERS]; 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() { int main() {
kb_init();
usb_init();
while (!usb_configured());
// initialize // initialize
init_hw();
init_layers(); init_layers();
init_sticky(); init_sticky();
@ -127,9 +125,22 @@ void main_key_loop() {
usb_extra_consumer_send(); usb_extra_consumer_send();
// unset sticky keys if necessary // unset sticky keys if necessary
if (sticky_on && sticky_done) { if (layer_sticky_on && layer_sticky_done) {
sticky_disable(); 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(); usb_keyboard_send();
mod_sticky = 0;
mod_sticky_done = false;
} }
// debounce in ms; see keyswitch spec for necessary value // 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() { void init_layers() {
for (layer l=0; l < KB_LAYERS; l++) { for (layer l=0; l < KB_LAYERS; l++) {
layers_active[l] = false; layers_active[l] = false;
@ -148,6 +175,9 @@ void init_layers() {
layers_active[0] = true; layers_active[0] = true;
} }
// ----------------------------------------------------------------------------
// layer functions
// ----------------------------------------------------------------------------
// find highest active layer // find highest active layer
layer highest_active_layer(layer offset) { 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 // layout info
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -327,6 +325,11 @@ bool _kbfun_modifier_is_pressed(keycode key) {
return (keyboard_modifier_keys & (1<<key)); return (keyboard_modifier_keys & (1<<key));
} }
void _kbfun_normal_sticky_done() {
layer_sticky_done = true;
mod_sticky_done = true;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// basic keyfuncs // basic keyfuncs
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -343,13 +346,13 @@ void exec_key() {
// normal key // normal key
void kbfun_normal_press_release() { void kbfun_normal_press_release() {
sticky_done = true; _kbfun_normal_sticky_done();
_kbfun_normal_press_release(current_is_pressed, current_keycode); _kbfun_normal_press_release(current_is_pressed, current_keycode);
} }
// media key // media key
void kbfun_mediakey_press_release() { void kbfun_mediakey_press_release() {
sticky_done = true; _kbfun_normal_sticky_done();
_kbfun_mediakey_press_release(current_is_pressed, current_keycode); _kbfun_mediakey_press_release(current_is_pressed, current_keycode);
} }
@ -360,7 +363,7 @@ void kbfun_modifier_press_release() {
// enable layer // enable layer
void kbfun_layer_enable() { void kbfun_layer_enable() {
sticky_done = true; layer_sticky_done = true; // don't disable sticky mods!
layer l = (layer) current_keycode; layer l = (layer) current_keycode;
layer_enable_upto(l); layer_enable_upto(l);
@ -368,11 +371,10 @@ void kbfun_layer_enable() {
// disable layer // disable layer
void kbfun_layer_disable() { void kbfun_layer_disable() {
sticky_done = true; layer_sticky_done = true; // don't disable sticky mods!
// letting go off a key releases *all* layers on that key // letting go off a key releases *all* layers on that key
for (layer l=0; l <= KB_LAYERS; l++) { for (layer l=0; l <= KB_LAYERS; l++) {
// FIXME this is broken with sticky
void (*key_function)(void) = kb_keyfunc_release(l, current_row, current_col); void (*key_function)(void) = kb_keyfunc_release(l, current_row, current_col);
if (is_layer_disable(key_function)) { if (is_layer_disable(key_function)) {
@ -388,14 +390,14 @@ void kbfun_layer_sticky() {
if (current_is_pressed) { if (current_is_pressed) {
layer_enable(l); layer_enable(l);
sticky_done = false; layer_sticky_done = false;
} else { } else {
if (sticky_done) { if (layer_sticky_done) {
layer_disable(l); layer_disable(l);
} else { } else {
layer_sticky[l] = true; layer_sticky[l] = true;
sticky_on = true; layer_sticky_on = true;
sticky_done = false; layer_sticky_done = false;
} }
} }
} }
@ -403,19 +405,17 @@ void kbfun_layer_sticky() {
// sticky modifier key // sticky modifier key
void kbfun_modifier_sticky() { void kbfun_modifier_sticky() {
// TODO handle: sticky, then same modifier // TODO handle: sticky, then same modifier
// TODO split sticky_on/done layer vs modifier so we can fine-tune it more?
keycode mod = current_keycode; keycode mod = current_keycode;
if (current_is_pressed) { if (current_is_pressed) {
kbfun_modifier_press_release(); kbfun_modifier_press_release();
sticky_done = false; mod_sticky_done = false;
} else { } else {
if (sticky_done) { if (mod_sticky_done) {
kbfun_modifier_press_release(); kbfun_modifier_press_release();
} else { } else {
modifier_sticky |= 1<<mod; mod_sticky |= 1<<mod;
sticky_on = true; mod_sticky_done = false;
sticky_done = false;
} }
} }
} }

View File

@ -9,6 +9,7 @@ void kbfun_modifier_sticky();
void kbfun_modifier_press_release(); void kbfun_modifier_press_release();
void kbfun_mediakey_press_release(); void kbfun_mediakey_press_release();
void kbfun_normal_press_release(); void kbfun_normal_press_release();
void _kbfun_normal_sticky_done();
bool _kbfun_modifier_is_pressed(keycode key); bool _kbfun_modifier_is_pressed(keycode key);
bool _kbfun_mediakey_is_pressed(keycode key); bool _kbfun_mediakey_is_pressed(keycode key);
bool _kbfun_normal_is_pressed(keycode key); bool _kbfun_normal_is_pressed(keycode key);
@ -25,13 +26,13 @@ void kbfun_layer_enable();
bool is_layer_enable(keyfunc f); bool is_layer_enable(keyfunc f);
layer layer_peek(layer offset); layer layer_peek(layer offset);
void layer_disable_top(); void layer_disable_top();
void layer_disable(layer l);
void layer_enable(layer l); void layer_enable(layer l);
layer highest_active_layer(layer offset); layer highest_active_layer(layer offset);
void sticky_disable(); void layer_disable(layer l);
void exec_key(); void exec_key();
keycode kb_keycode(layer l,u8 row,u8 col); keycode kb_keycode(layer l,u8 row,u8 col);
void main_key_loop(); void main_key_loop();
void init_sticky(); void init_sticky();
void init_layers(); void init_layers();
void init_hw();
int main(); int main();