2015-04-10 01:32:04 +09:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "keyboard.h"
|
|
|
|
#include "action.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "action_layer.h"
|
|
|
|
|
2016-04-28 23:23:33 -04:00
|
|
|
#ifdef DEBUG_ACTION
|
2015-04-10 01:32:04 +09:00
|
|
|
#include "debug.h"
|
2016-04-28 23:23:33 -04:00
|
|
|
#else
|
|
|
|
#include "nodebug.h"
|
|
|
|
#endif
|
2015-04-10 01:32:04 +09:00
|
|
|
|
|
|
|
|
2016-04-17 12:54:32 -05:00
|
|
|
/*
|
2015-04-10 01:32:04 +09:00
|
|
|
* Default Layer State
|
|
|
|
*/
|
|
|
|
uint32_t default_layer_state = 0;
|
|
|
|
|
|
|
|
static void default_layer_state_set(uint32_t state)
|
|
|
|
{
|
|
|
|
debug("default_layer_state: ");
|
|
|
|
default_layer_debug(); debug(" to ");
|
|
|
|
default_layer_state = state;
|
|
|
|
default_layer_debug(); debug("\n");
|
|
|
|
clear_keyboard_but_mods(); // To avoid stuck keys
|
|
|
|
}
|
|
|
|
|
|
|
|
void default_layer_debug(void)
|
|
|
|
{
|
|
|
|
dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
|
|
|
|
}
|
|
|
|
|
|
|
|
void default_layer_set(uint32_t state)
|
|
|
|
{
|
|
|
|
default_layer_state_set(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NO_ACTION_LAYER
|
|
|
|
void default_layer_or(uint32_t state)
|
|
|
|
{
|
|
|
|
default_layer_state_set(default_layer_state | state);
|
|
|
|
}
|
|
|
|
void default_layer_and(uint32_t state)
|
|
|
|
{
|
|
|
|
default_layer_state_set(default_layer_state & state);
|
|
|
|
}
|
|
|
|
void default_layer_xor(uint32_t state)
|
|
|
|
{
|
|
|
|
default_layer_state_set(default_layer_state ^ state);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef NO_ACTION_LAYER
|
2016-04-17 12:54:32 -05:00
|
|
|
/*
|
2015-04-10 01:32:04 +09:00
|
|
|
* Keymap Layer State
|
|
|
|
*/
|
|
|
|
uint32_t layer_state = 0;
|
|
|
|
|
2017-02-15 05:02:15 +07:00
|
|
|
__attribute__((weak))
|
|
|
|
uint32_t layer_state_set_kb(uint32_t state) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2015-04-10 01:32:04 +09:00
|
|
|
static void layer_state_set(uint32_t state)
|
|
|
|
{
|
2017-02-15 05:02:15 +07:00
|
|
|
state = layer_state_set_kb(state);
|
2015-04-10 01:32:04 +09:00
|
|
|
dprint("layer_state: ");
|
|
|
|
layer_debug(); dprint(" to ");
|
|
|
|
layer_state = state;
|
|
|
|
layer_debug(); dprintln();
|
|
|
|
clear_keyboard_but_mods(); // To avoid stuck keys
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_clear(void)
|
|
|
|
{
|
|
|
|
layer_state_set(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_move(uint8_t layer)
|
|
|
|
{
|
|
|
|
layer_state_set(1UL<<layer);
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_on(uint8_t layer)
|
|
|
|
{
|
|
|
|
layer_state_set(layer_state | (1UL<<layer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_off(uint8_t layer)
|
|
|
|
{
|
|
|
|
layer_state_set(layer_state & ~(1UL<<layer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_invert(uint8_t layer)
|
|
|
|
{
|
|
|
|
layer_state_set(layer_state ^ (1UL<<layer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_or(uint32_t state)
|
|
|
|
{
|
|
|
|
layer_state_set(layer_state | state);
|
|
|
|
}
|
|
|
|
void layer_and(uint32_t state)
|
|
|
|
{
|
|
|
|
layer_state_set(layer_state & state);
|
|
|
|
}
|
|
|
|
void layer_xor(uint32_t state)
|
|
|
|
{
|
|
|
|
layer_state_set(layer_state ^ state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void layer_debug(void)
|
|
|
|
{
|
|
|
|
dprintf("%08lX(%u)", layer_state, biton32(layer_state));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-04-05 10:54:47 +02:00
|
|
|
#if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
|
2016-07-24 12:56:28 -07:00
|
|
|
uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS] = {{0}};
|
2016-04-05 10:54:47 +02:00
|
|
|
|
|
|
|
void update_source_layers_cache(keypos_t key, uint8_t layer)
|
|
|
|
{
|
|
|
|
const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
|
|
|
|
const uint8_t storage_row = key_number / 8;
|
|
|
|
const uint8_t storage_bit = key_number % 8;
|
|
|
|
|
|
|
|
for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
|
2016-04-13 22:24:42 +02:00
|
|
|
source_layers_cache[storage_row][bit_number] ^=
|
2016-04-05 10:54:47 +02:00
|
|
|
(-((layer & (1U << bit_number)) != 0)
|
2016-04-13 22:24:42 +02:00
|
|
|
^ source_layers_cache[storage_row][bit_number])
|
2016-04-05 10:54:47 +02:00
|
|
|
& (1U << storage_bit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t read_source_layers_cache(keypos_t key)
|
|
|
|
{
|
|
|
|
const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
|
|
|
|
const uint8_t storage_row = key_number / 8;
|
|
|
|
const uint8_t storage_bit = key_number % 8;
|
|
|
|
uint8_t layer = 0;
|
|
|
|
|
|
|
|
for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
|
|
|
|
layer |=
|
2016-04-13 22:24:42 +02:00
|
|
|
((source_layers_cache[storage_row][bit_number]
|
2016-04-05 10:54:47 +02:00
|
|
|
& (1U << storage_bit)) != 0)
|
|
|
|
<< bit_number;
|
|
|
|
}
|
|
|
|
|
|
|
|
return layer;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure the action triggered when the key is released is the same
|
|
|
|
* one as the one triggered on press. It's important for the mod keys
|
|
|
|
* when the layer is switched after the down event but before the up
|
|
|
|
* event as they may get stuck otherwise.
|
|
|
|
*/
|
|
|
|
action_t store_or_get_action(bool pressed, keypos_t key)
|
|
|
|
{
|
|
|
|
#if !defined(NO_ACTION_LAYER) && defined(PREVENT_STUCK_MODIFIERS)
|
|
|
|
if (disable_action_cache) {
|
|
|
|
return layer_switch_get_action(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t layer;
|
|
|
|
|
|
|
|
if (pressed) {
|
|
|
|
layer = layer_switch_get_layer(key);
|
|
|
|
update_source_layers_cache(key, layer);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
layer = read_source_layers_cache(key);
|
|
|
|
}
|
|
|
|
return action_for_key(layer, key);
|
|
|
|
#else
|
|
|
|
return layer_switch_get_action(key);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-04-10 01:32:04 +09:00
|
|
|
|
2016-03-27 23:50:07 +02:00
|
|
|
int8_t layer_switch_get_layer(keypos_t key)
|
2015-04-10 01:32:04 +09:00
|
|
|
{
|
|
|
|
action_t action;
|
|
|
|
action.code = ACTION_TRANSPARENT;
|
|
|
|
|
|
|
|
#ifndef NO_ACTION_LAYER
|
|
|
|
uint32_t layers = layer_state | default_layer_state;
|
|
|
|
/* check top layer first */
|
|
|
|
for (int8_t i = 31; i >= 0; i--) {
|
|
|
|
if (layers & (1UL<<i)) {
|
|
|
|
action = action_for_key(i, key);
|
|
|
|
if (action.code != ACTION_TRANSPARENT) {
|
2016-03-27 23:50:07 +02:00
|
|
|
return i;
|
2015-04-10 01:32:04 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* fall back to layer 0 */
|
2016-03-27 23:50:07 +02:00
|
|
|
return 0;
|
2015-04-10 01:32:04 +09:00
|
|
|
#else
|
2016-03-27 23:50:07 +02:00
|
|
|
return biton32(default_layer_state);
|
2015-04-10 01:32:04 +09:00
|
|
|
#endif
|
|
|
|
}
|
2016-03-27 23:50:07 +02:00
|
|
|
|
|
|
|
action_t layer_switch_get_action(keypos_t key)
|
|
|
|
{
|
|
|
|
return action_for_key(layer_switch_get_layer(key), key);
|
|
|
|
}
|