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]))
|
#define array_length(x) (sizeof(x) / sizeof((x)[0]))
|
||||||
|
|
||||||
typedef uint8_t u8;
|
typedef int8_t i8;
|
||||||
typedef uint16_t u16;
|
typedef uint8_t u8;
|
||||||
|
typedef uint16_t u16;
|
||||||
|
|
||||||
typedef u8 keycode;
|
typedef u8 keycode;
|
||||||
typedef u16 media_keycode;
|
typedef u16 media_keycode;
|
||||||
|
@ -64,8 +65,9 @@ static layer current_layer;
|
||||||
static keycode current_keycode;
|
static keycode current_keycode;
|
||||||
static bool current_is_pressed;
|
static bool current_is_pressed;
|
||||||
|
|
||||||
static bool layers_active[KB_LAYERS];
|
static i8 layers_active[KB_LAYERS];
|
||||||
static layer layers_top = 0;
|
static layer layers_top = 0;
|
||||||
|
static bool layer_released = false;
|
||||||
|
|
||||||
static bool layer_sticky_on;
|
static bool layer_sticky_on;
|
||||||
static bool layer_sticky[KB_LAYERS];
|
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)
|
// send the USB report (even if nothing's changed)
|
||||||
usb_keyboard_send();
|
usb_keyboard_send();
|
||||||
usb_extra_consumer_send();
|
usb_extra_consumer_send();
|
||||||
|
@ -181,9 +189,9 @@ void init_sticky() {
|
||||||
|
|
||||||
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] = 0;
|
||||||
}
|
}
|
||||||
layers_active[0] = true;
|
layers_active[0] = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
@ -191,11 +199,9 @@ void init_layers() {
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// find highest active layer
|
// find highest active layer
|
||||||
layer highest_active_layer(layer offset) {
|
layer highest_active_layer() {
|
||||||
if (offset < layers_top) {
|
for (layer l = KB_LAYERS; l > 0; l--) {
|
||||||
for (layer l = layers_top - offset; l > 0 && l < KB_LAYERS; l--) {
|
if (layers_active[l] > 0) { return l; }
|
||||||
if (layers_active[l]) { return l; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// the base layer is always active
|
// the base layer is always active
|
||||||
|
@ -204,9 +210,9 @@ layer highest_active_layer(layer offset) {
|
||||||
|
|
||||||
// enable a layer
|
// enable a layer
|
||||||
void layer_enable(layer l) {
|
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) {
|
if (l > layers_top) {
|
||||||
layers_top = l;
|
layers_top = l;
|
||||||
|
@ -218,10 +224,13 @@ void layer_disable(layer l) {
|
||||||
// base layer stays always on
|
// base layer stays always on
|
||||||
if (l >= KB_LAYERS || l == 0) { return; }
|
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) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void layer_enable_upto(layer max_layer) {
|
void re_press_pressed_layers() {
|
||||||
// FIXME clean this up
|
for (u8 row=0; row<KB_ROWS; row++) {
|
||||||
|
for (u8 col=0; col<KB_COLUMNS; col++) {
|
||||||
// pressing a key implicitly activates all lower layers as well
|
bool is_pressed = (*kb_is_pressed)[row][col];
|
||||||
for (layer l=0; l <= KB_LAYERS; l++) {
|
if (is_pressed) {
|
||||||
void (*key_function)(void) = kb_keyfunc_press(l, current_row, current_col);
|
layer layer = layers_pressed[row][col];
|
||||||
|
keycode key = kb_keycode(layer, row, col);
|
||||||
if (is_layer_keyfunc(key_function)) {
|
keyfunc func = kb_keyfunc_press(layer, row, col);
|
||||||
layer enable_layer = (layer) kb_keycode(l, current_row, current_col);
|
debug_printf("? %d l: %d on: %d at: %d x %d\n",
|
||||||
if (enable_layer <= max_layer) {
|
is_layer_keyfunc(func),
|
||||||
layer_enable(enable_layer);
|
key, layer,
|
||||||
|
row, col);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -362,19 +372,11 @@ void kbfun_modifier_press_release() {
|
||||||
void kbfun_layer_press_release() {
|
void kbfun_layer_press_release() {
|
||||||
layer_sticky_done = true; // don't disable sticky mods!
|
layer_sticky_done = true; // don't disable sticky mods!
|
||||||
|
|
||||||
|
layer l = (layer) current_keycode;
|
||||||
if (current_is_pressed) {
|
if (current_is_pressed) {
|
||||||
layer l = (layer) current_keycode;
|
layer_enable(l);
|
||||||
layer_enable_upto(l);
|
|
||||||
} else {
|
} else {
|
||||||
// letting go off a key releases *all* layers on that key
|
layer_disable(l);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,11 +19,11 @@ void _kbfun_mediakey_press_release(bool press,keycode key);
|
||||||
void _kbfun_normal_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_release(layer l,u8 row,u8 col);
|
||||||
keyfunc kb_keyfunc_press(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);
|
bool is_layer_keyfunc(keyfunc f);
|
||||||
void layer_enable(layer l);
|
void layer_enable(layer l);
|
||||||
layer highest_active_layer(layer offset);
|
layer highest_active_layer();
|
||||||
void layer_disable(layer l);
|
void layer_disable(layer l);
|
||||||
|
void re_press_pressed_layers();
|
||||||
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();
|
||||||
|
|
Loading…
Reference in New Issue