diff --git a/src/main.c b/src/main.c index 993f293..5218cec 100644 --- a/src/main.c +++ b/src/main.c @@ -24,8 +24,9 @@ #define array_length(x) (sizeof(x) / sizeof((x)[0])) -typedef uint8_t u8; -typedef uint16_t u16; +typedef int8_t i8; +typedef uint8_t u8; +typedef uint16_t u16; typedef u8 keycode; typedef u16 media_keycode; @@ -64,8 +65,9 @@ static layer current_layer; static keycode current_keycode; static bool current_is_pressed; -static bool layers_active[KB_LAYERS]; +static i8 layers_active[KB_LAYERS]; static layer layers_top = 0; +static bool layer_released = false; static bool layer_sticky_on; static bool layer_sticky[KB_LAYERS]; @@ -131,6 +133,12 @@ void main_key_loop() { } } + // re-press all layers so stacked layers work right + if (layer_released) { + layer_released = false; + re_press_pressed_layers(); + } + // send the USB report (even if nothing's changed) usb_keyboard_send(); usb_extra_consumer_send(); @@ -181,9 +189,9 @@ void init_sticky() { void init_layers() { for (layer l=0; l < KB_LAYERS; l++) { - layers_active[l] = false; + layers_active[l] = 0; } - layers_active[0] = true; + layers_active[0] = 1; } // ---------------------------------------------------------------------------- @@ -191,11 +199,9 @@ void init_layers() { // ---------------------------------------------------------------------------- // find highest active layer -layer highest_active_layer(layer offset) { - if (offset < layers_top) { - for (layer l = layers_top - offset; l > 0 && l < KB_LAYERS; l--) { - if (layers_active[l]) { return l; } - } +layer highest_active_layer() { + for (layer l = KB_LAYERS; l > 0; l--) { + if (layers_active[l] > 0) { return l; } } // the base layer is always active @@ -204,9 +210,9 @@ layer highest_active_layer(layer offset) { // enable a layer void layer_enable(layer l) { - if (l >= KB_LAYERS) { return; } + if (l >= KB_LAYERS || l == 0) { return; } - layers_active[l] = true; + layers_active[l] += 1; if (l > layers_top) { layers_top = l; @@ -218,10 +224,13 @@ void layer_disable(layer l) { // base layer stays always on if (l >= KB_LAYERS || l == 0) { return; } - layers_active[l] = false; + if (layers_active[l] > 0) { + layers_active[l] -= 1; + } + layer_released = true; if (l == layers_top) { - layers_top = highest_active_layer(1); + layers_top = highest_active_layer(); } } @@ -234,17 +243,18 @@ bool is_layer_keyfunc(keyfunc f) { return false; } -void layer_enable_upto(layer max_layer) { - // FIXME clean this up - - // pressing a key implicitly activates all lower layers as well - for (layer l=0; l <= KB_LAYERS; l++) { - void (*key_function)(void) = kb_keyfunc_press(l, current_row, current_col); - - if (is_layer_keyfunc(key_function)) { - layer enable_layer = (layer) kb_keycode(l, current_row, current_col); - if (enable_layer <= max_layer) { - layer_enable(enable_layer); +void re_press_pressed_layers() { + for (u8 row=0; row