minor cleanup

master
Stefan Dorn 2016-06-12 04:39:06 +01:00
parent a57fdf7efd
commit 91e2e9f41f
1 changed files with 72 additions and 73 deletions

View File

@ -18,10 +18,10 @@
typedef void (*void_funptr_t)(void); typedef void (*void_funptr_t)(void);
typedef enum StickyState { typedef enum StickyState {
eStickyNone, StickyNone,
eStickyOnceDown, StickyOnceDown,
eStickyOnceUp, StickyOnceUp,
eStickyLock StickyLock
} StickyState; } StickyState;
// layer data // layer data
@ -31,27 +31,26 @@ typedef enum StickyState {
// globals // globals
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
static bool _main_kb_is_pressed[KB_ROWS][KB_COLUMNS]; static bool _kb_is_pressed[KB_ROWS][KB_COLUMNS];
static bool (*main_kb_is_pressed)[KB_ROWS][KB_COLUMNS] = &_main_kb_is_pressed; static bool (*kb_is_pressed)[KB_ROWS][KB_COLUMNS] = &_kb_is_pressed;
static bool _main_kb_was_pressed[KB_ROWS][KB_COLUMNS]; static bool _kb_was_pressed[KB_ROWS][KB_COLUMNS];
static bool (*main_kb_was_pressed)[KB_ROWS][KB_COLUMNS] = &_main_kb_was_pressed; static bool (*kb_was_pressed)[KB_ROWS][KB_COLUMNS] = &_kb_was_pressed;
static bool main_kb_was_transparent[KB_ROWS][KB_COLUMNS]; static bool kb_was_transparent[KB_ROWS][KB_COLUMNS];
static uint8_t main_layers_pressed[KB_ROWS][KB_COLUMNS]; static uint8_t layers_pressed[KB_ROWS][KB_COLUMNS];
static const uint8_t PROGMEM _kb_layout[KB_LAYERS][KB_ROWS][KB_COLUMNS]; static const uint8_t PROGMEM _kb_layout[KB_LAYERS][KB_ROWS][KB_COLUMNS];
static const void_funptr_t PROGMEM _kb_layout_press[KB_LAYERS][KB_ROWS][KB_COLUMNS]; static const void_funptr_t PROGMEM _kb_layout_press[KB_LAYERS][KB_ROWS][KB_COLUMNS];
static const void_funptr_t PROGMEM _kb_layout_release[KB_LAYERS][KB_ROWS][KB_COLUMNS]; static const void_funptr_t PROGMEM _kb_layout_release[KB_LAYERS][KB_ROWS][KB_COLUMNS];
static uint8_t main_arg_layer; static uint8_t current_layer;
static uint8_t main_arg_layer_offset; static uint8_t layer_offset;
static uint8_t main_arg_row; static uint8_t current_row;
static uint8_t main_arg_col; static uint8_t current_col;
static bool main_arg_is_pressed; static bool current_is_pressed;
static bool main_arg_was_pressed; static bool non_trans_key_pressed;
static bool main_arg_any_non_trans_key_pressed; static bool trans_key_pressed;
static bool main_arg_trans_key_pressed;
static bool layers_active[KB_LAYERS]; static bool layers_active[KB_LAYERS];
static StickyState layers_sticky[KB_LAYERS]; static StickyState layers_sticky[KB_LAYERS];
@ -84,7 +83,7 @@ int main(void) {
void main_init_layers() { void main_init_layers() {
for (uint8_t layer=0; layer < KB_LAYERS; layer++) { for (uint8_t layer=0; layer < KB_LAYERS; layer++) {
layers_active[layer] = false; layers_active[layer] = false;
layers_sticky[layer] = eStickyNone; layers_sticky[layer] = StickyNone;
} }
layers_active[0] = true; layers_active[0] = true;
} }
@ -117,7 +116,7 @@ StickyState main_layers_sticky(uint8_t layer) {
if (layer < KB_LAYERS) { if (layer < KB_LAYERS) {
return layers_sticky[layer]; return layers_sticky[layer];
} }
return eStickyNone; return StickyNone;
} }
// enable a layer // enable a layer
@ -138,7 +137,7 @@ void main_layers_disable(uint8_t layer) {
if (layer >= KB_LAYERS || layer == 0) { return; } if (layer >= KB_LAYERS || layer == 0) { return; }
layers_active[layer] = false; layers_active[layer] = false;
layers_sticky[layer] = eStickyNone; layers_sticky[layer] = StickyNone;
if (layer == layers_top) { if (layer == layers_top) {
layers_top = _highest_active_layer(1); layers_top = _highest_active_layer(1);
@ -158,9 +157,9 @@ uint8_t main_layers_peek(uint8_t offset) {
// execute the keypress or keyrelease function (if it exists) of the key at the current possition // execute the keypress or keyrelease function (if it exists) of the key at the current possition
void main_exec_key(void) { void main_exec_key(void) {
void (*key_function)(void) = void (*key_function)(void) =
( (main_arg_is_pressed) ( (current_is_pressed)
? kb_layout_press_get(main_arg_layer, main_arg_row, main_arg_col) ? kb_layout_press_get(current_layer, current_row, current_col)
: kb_layout_release_get(main_arg_layer, main_arg_row, main_arg_col) ); : kb_layout_release_get(current_layer, current_row, current_col) );
if (key_function) { if (key_function) {
(*key_function)(); (*key_function)();
@ -168,7 +167,7 @@ void main_exec_key(void) {
// If the current layer is in the sticky once up state and a key defined // If the current layer is in the sticky once up state and a key defined
// for this layer (a non-transparent key) was pressed, pop the layer // for this layer (a non-transparent key) was pressed, pop the layer
if (main_layers_top_sticky() == eStickyOnceUp && main_arg_any_non_trans_key_pressed) { if (main_layers_top_sticky() == StickyOnceUp && non_trans_key_pressed) {
main_layers_disable_top(); main_layers_disable_top();
} }
} }
@ -272,7 +271,7 @@ void _kbfun_mediakey_press_release(bool press, uint8_t keycode) {
} }
uint8_t _kbfun_get_keycode() { uint8_t _kbfun_get_keycode() {
return kb_layout_get(main_arg_layer, main_arg_row, main_arg_col); return kb_layout_get(current_layer, current_row, current_col);
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -280,8 +279,8 @@ uint8_t _kbfun_get_keycode() {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
void kbfun_press_release() { void kbfun_press_release() {
if (!main_arg_trans_key_pressed) { if (!trans_key_pressed) {
main_arg_any_non_trans_key_pressed = true; non_trans_key_pressed = true;
} }
kbfun_press_release_preserve_sticky(); kbfun_press_release_preserve_sticky();
} }
@ -299,7 +298,7 @@ void kbfun_press_release() {
*/ */
void kbfun_press_release_preserve_sticky() { void kbfun_press_release_preserve_sticky() {
uint8_t keycode = _kbfun_get_keycode(); uint8_t keycode = _kbfun_get_keycode();
_kbfun_press_release(main_arg_is_pressed, keycode); _kbfun_press_release(current_is_pressed, keycode);
} }
/* /*
@ -317,10 +316,10 @@ void kbfun_toggle(void) {
*/ */
void kbfun_transparent(void) { void kbfun_transparent(void) {
// TODO maybe re-implement this cleaner? // TODO maybe re-implement this cleaner?
main_arg_trans_key_pressed = true; trans_key_pressed = true;
main_arg_layer_offset++; layer_offset++;
main_arg_layer = main_layers_peek(main_arg_layer_offset); current_layer = main_layers_peek(layer_offset);
main_layers_pressed[main_arg_row][main_arg_col] = main_arg_layer; layers_pressed[current_row][current_col] = current_layer;
main_exec_key(); main_exec_key();
} }
@ -348,12 +347,12 @@ static void layer_enable_upto(uint8_t max_layer) {
// pressing a key implicitly activates all lower layers as well // pressing a key implicitly activates all lower layers as well
for (uint8_t layer=0; layer <= KB_LAYERS; layer++) { for (uint8_t layer=0; layer <= KB_LAYERS; layer++) {
void (*key_function)(void) = kb_layout_press_get(layer, main_arg_row, main_arg_col); void (*key_function)(void) = kb_layout_press_get(layer, current_row, current_col);
if (is_layer_enable(key_function)) { if (is_layer_enable(key_function)) {
uint8_t enable_layer = kb_layout_get(layer, main_arg_row, main_arg_col); uint8_t enable_layer = kb_layout_get(layer, current_row, current_col);
if (enable_layer <= max_layer) { if (enable_layer <= max_layer) {
main_layers_enable(enable_layer, eStickyNone); main_layers_enable(enable_layer, StickyNone);
} }
} }
} }
@ -371,7 +370,7 @@ void kbfun_layer_enable() {
// Only the topmost layer on the stack should be in sticky once state, pop // Only the topmost layer on the stack should be in sticky once state, pop
// the top layer if it is in sticky once state // the top layer if it is in sticky once state
/* uint8_t topSticky = main_layers_top_sticky(); */ /* uint8_t topSticky = main_layers_top_sticky(); */
/* if (topSticky == eStickyOnceDown || topSticky == eStickyOnceUp) { */ /* if (topSticky == StickyOnceDown || topSticky == StickyOnceUp) { */
/* main_layers_disable_top(); */ /* main_layers_disable_top(); */
/* } */ /* } */
@ -385,10 +384,10 @@ void kbfun_layer_disable() {
// letting go off a key releases *all* layers on that key // letting go off a key releases *all* layers on that key
for (uint8_t layer=0; layer <= KB_LAYERS; layer++) { for (uint8_t layer=0; layer <= KB_LAYERS; layer++) {
void (*key_function)(void) = kb_layout_release_get(layer, main_arg_row, main_arg_col); void (*key_function)(void) = kb_layout_release_get(layer, current_row, current_col);
if (is_layer_disable(key_function)) { if (is_layer_disable(key_function)) {
uint8_t disable_layer = kb_layout_get(layer, main_arg_row, main_arg_col); uint8_t disable_layer = kb_layout_get(layer, current_row, current_col);
main_layers_disable(disable_layer); main_layers_disable(disable_layer);
} }
} }
@ -414,32 +413,32 @@ void kbfun_layer_sticky() {
uint8_t topLayer = main_layers_top_layer(); uint8_t topLayer = main_layers_top_layer();
StickyState topSticky = main_layers_top_sticky(); StickyState topSticky = main_layers_top_sticky();
if (main_arg_is_pressed) { if (current_is_pressed) {
if (topLayer == layer) { if (topLayer == layer) {
// FIXME // FIXME
/* if (topSticky == eStickyOnceUp) { */ /* if (topSticky == StickyOnceUp) { */
/* main_layers_enable(layer, eStickyLock); */ /* main_layers_enable(layer, StickyLock); */
/* } */ /* } */
} else { } else {
// only the topmost layer on the stack should be in sticky once state // only the topmost layer on the stack should be in sticky once state
if (topSticky == eStickyOnceDown || topSticky == eStickyOnceUp) { if (topSticky == StickyOnceDown || topSticky == StickyOnceUp) {
main_layers_disable_top(); main_layers_disable_top();
} }
main_layers_enable(layer, eStickyOnceDown); main_layers_enable(layer, StickyOnceDown);
// this should be the only place we care about this flag being cleared // this should be the only place we care about this flag being cleared
main_arg_any_non_trans_key_pressed = false; non_trans_key_pressed = false;
} }
} else { } else {
if (main_layers_sticky(layer) == eStickyOnceDown) { if (main_layers_sticky(layer) == StickyOnceDown) {
// When releasing this sticky key, pop the layer always // When releasing this sticky key, pop the layer always
main_layers_disable(layer); main_layers_disable(layer);
if (!main_arg_any_non_trans_key_pressed) { if (!non_trans_key_pressed) {
// If no key defined for this layer (a non-transparent key) // If no key defined for this layer (a non-transparent key)
// was pressed, push the layer again, but in the // was pressed, push the layer again, but in the
// StickyOnceUp state // StickyOnceUp state
main_layers_enable(layer, eStickyOnceUp); main_layers_enable(layer, StickyOnceUp);
} }
} }
} }
@ -453,7 +452,7 @@ void kbfun_layer_sticky() {
* Generate a 'shift' press or release before the normal keypress or release * Generate a 'shift' press or release before the normal keypress or release
*/ */
void kbfun_shift_press_release(void) { void kbfun_shift_press_release(void) {
_kbfun_press_release(main_arg_is_pressed, KEY_LeftShift); _kbfun_press_release(current_is_pressed, KEY_LeftShift);
kbfun_press_release(); kbfun_press_release();
} }
@ -461,7 +460,7 @@ void kbfun_shift_press_release(void) {
* Generate a 'control' press or release before the normal keypress or release * Generate a 'control' press or release before the normal keypress or release
*/ */
void kbfun_control_press_release(void) { void kbfun_control_press_release(void) {
_kbfun_press_release(main_arg_is_pressed, KEY_LeftControl); _kbfun_press_release(current_is_pressed, KEY_LeftControl);
kbfun_press_release(); kbfun_press_release();
} }
@ -482,13 +481,13 @@ void kbfun_2_keys_capslock_press_release(void) {
uint8_t keycode = _kbfun_get_keycode(); uint8_t keycode = _kbfun_get_keycode();
if (!main_arg_is_pressed) { keys_pressed--; } if (!current_is_pressed) { keys_pressed--; }
// take care of the key that was actually pressed // take care of the key that was actually pressed
_kbfun_press_release(main_arg_is_pressed, keycode); _kbfun_press_release(current_is_pressed, keycode);
// take care of capslock (only on the press of the 2nd key) // take care of capslock (only on the press of the 2nd key)
if (keys_pressed == 1 && main_arg_is_pressed) { if (keys_pressed == 1 && current_is_pressed) {
// save the state of left and right shift // save the state of left and right shift
lshift_pressed = _kbfun_is_pressed(KEY_LeftShift); lshift_pressed = _kbfun_is_pressed(KEY_LeftShift);
rshift_pressed = _kbfun_is_pressed(KEY_RightShift); rshift_pressed = _kbfun_is_pressed(KEY_RightShift);
@ -505,7 +504,7 @@ void kbfun_2_keys_capslock_press_release(void) {
if (rshift_pressed) { _kbfun_press_release(true, KEY_RightShift); } if (rshift_pressed) { _kbfun_press_release(true, KEY_RightShift); }
} }
if (main_arg_is_pressed) { keys_pressed++; } if (current_is_pressed) { keys_pressed++; }
} }
/* /*
@ -513,19 +512,19 @@ void kbfun_2_keys_capslock_press_release(void) {
*/ */
void kbfun_mediakey_press_release(void) { void kbfun_mediakey_press_release(void) {
uint8_t keycode = _kbfun_get_keycode(); uint8_t keycode = _kbfun_get_keycode();
_kbfun_mediakey_press_release(main_arg_is_pressed, keycode); _kbfun_mediakey_press_release(current_is_pressed, keycode);
} }
// -------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------
void main_key_loop() { void main_key_loop() {
for (;;) { for (;;) {
// swap `main_kb_is_pressed` and `main_kb_was_pressed`, then update // swap `kb_is_pressed` and `kb_was_pressed`, then update
bool (*temp)[KB_ROWS][KB_COLUMNS] = main_kb_was_pressed; bool (*temp)[KB_ROWS][KB_COLUMNS] = kb_was_pressed;
main_kb_was_pressed = main_kb_is_pressed; kb_was_pressed = kb_is_pressed;
main_kb_is_pressed = temp; kb_is_pressed = temp;
kb_update_matrix(*main_kb_is_pressed); kb_update_matrix(*kb_is_pressed);
// this loop is responsible to // this loop is responsible to
// - "execute" keys when they change state // - "execute" keys when they change state
@ -539,25 +538,25 @@ void main_key_loop() {
// - see "lib/key-functions/public/*.c" for the function definitions // - see "lib/key-functions/public/*.c" for the function definitions
for (uint8_t row=0; row<KB_ROWS; row++) { for (uint8_t row=0; row<KB_ROWS; row++) {
for (uint8_t col=0; col<KB_COLUMNS; col++) { for (uint8_t col=0; col<KB_COLUMNS; col++) {
main_arg_is_pressed = (*main_kb_is_pressed)[row][col]; current_is_pressed = (*kb_is_pressed)[row][col];
main_arg_was_pressed = (*main_kb_was_pressed)[row][col]; bool was_pressed = (*kb_was_pressed)[row][col];
if (main_arg_is_pressed != main_arg_was_pressed) { if (current_is_pressed != was_pressed) {
if (main_arg_is_pressed) { if (current_is_pressed) {
main_arg_layer = main_layers_top_layer(); current_layer = main_layers_top_layer();
main_layers_pressed[row][col] = main_arg_layer; layers_pressed[row][col] = current_layer;
main_arg_trans_key_pressed = false; trans_key_pressed = false;
} else { } else {
main_arg_layer = main_layers_pressed[row][col]; current_layer = layers_pressed[row][col];
main_arg_trans_key_pressed = main_kb_was_transparent[row][col]; trans_key_pressed = kb_was_transparent[row][col];
} }
// set remaining vars, and "execute" key // set remaining vars, and "execute" key
main_arg_row = row; current_row = row;
main_arg_col = col; current_col = col;
main_arg_layer_offset = 0; layer_offset = 0;
main_exec_key(); main_exec_key();
main_kb_was_transparent[row][col] = main_arg_trans_key_pressed; kb_was_transparent[row][col] = trans_key_pressed;
} }
} }
} }