rewrite layer code
parent
70a8382000
commit
5e9593a4a5
74
src/main.c
74
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<KB_ROWS; row++) {
|
||||
for (u8 col=0; col<KB_COLUMNS; col++) {
|
||||
bool is_pressed = (*kb_is_pressed)[row][col];
|
||||
if (is_pressed) {
|
||||
layer layer = layers_pressed[row][col];
|
||||
keycode key = kb_keycode(layer, row, col);
|
||||
keyfunc func = kb_keyfunc_press(layer, row, col);
|
||||
debug_printf("? %d l: %d on: %d at: %d x %d\n",
|
||||
is_layer_keyfunc(func),
|
||||
key, layer,
|
||||
row, col);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -362,19 +372,11 @@ void kbfun_modifier_press_release() {
|
|||
void kbfun_layer_press_release() {
|
||||
layer_sticky_done = true; // don't disable sticky mods!
|
||||
|
||||
layer l = (layer) current_keycode;
|
||||
if (current_is_pressed) {
|
||||
layer l = (layer) current_keycode;
|
||||
layer_enable_upto(l);
|
||||
layer_enable(l);
|
||||
} else {
|
||||
// letting go off a key releases *all* layers on that key
|
||||
for (layer l=0; l <= KB_LAYERS; l++) {
|
||||
void (*key_function)(void) = kb_keyfunc_release(l, current_row, current_col);
|
||||
|
||||
if (is_layer_keyfunc(key_function)) {
|
||||
layer disable_layer = (layer) kb_keycode(l, current_row, current_col);
|
||||
layer_disable(disable_layer);
|
||||
}
|
||||
}
|
||||
layer_disable(l);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,11 +19,11 @@ void _kbfun_mediakey_press_release(bool press,keycode key);
|
|||
void _kbfun_normal_press_release(bool press,keycode key);
|
||||
keyfunc kb_keyfunc_release(layer l,u8 row,u8 col);
|
||||
keyfunc kb_keyfunc_press(layer l,u8 row,u8 col);
|
||||
void layer_enable_upto(layer max_layer);
|
||||
bool is_layer_keyfunc(keyfunc f);
|
||||
void layer_enable(layer l);
|
||||
layer highest_active_layer(layer offset);
|
||||
layer highest_active_layer();
|
||||
void layer_disable(layer l);
|
||||
void re_press_pressed_layers();
|
||||
void exec_key();
|
||||
keycode kb_keycode(layer l,u8 row,u8 col);
|
||||
void main_key_loop();
|
||||
|
|
Loading…
Reference in New Issue