keychron_qmk_firmware/common/action_layer.c

139 lines
2.6 KiB
C
Raw Normal View History

2013-02-15 15:48:36 +06:00
#include <stdint.h>
#include "keyboard.h"
#include "action.h"
2013-02-16 01:05:58 +06:00
#include "util.h"
#include "action_layer.h"
2013-02-15 15:48:36 +06:00
2013-05-14 13:18:22 +06:00
#ifdef DEBUG_ACTION
#include "debug.h"
#else
#include "nodebug.h"
#endif
2013-02-15 15:48:36 +06:00
2013-02-20 08:16:13 +06:00
/*
* Default Layer State
2013-02-20 08:16:13 +06:00
*/
uint32_t default_layer_state = 0;
2013-02-16 01:05:58 +06:00
static void default_layer_state_set(uint32_t state)
2013-02-20 08:16:13 +06:00
{
debug("default_layer_state: ");
default_layer_debug(); debug(" to ");
default_layer_state = state;
default_layer_debug(); debug("\n");
2013-02-20 08:16:13 +06:00
clear_keyboard_but_mods(); // To avoid stuck keys
}
void default_layer_debug(void)
2013-02-20 08:16:13 +06:00
{
2013-05-14 13:18:22 +06:00
dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
2013-02-20 08:16:13 +06:00
}
void default_layer_set(uint8_t layer)
2013-02-20 08:16:13 +06:00
{
default_layer_state_set(1UL<<layer);
}
2013-02-15 15:48:36 +06:00
#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
2013-02-20 08:16:13 +06:00
#ifndef NO_ACTION_LAYER
/*
* Keymap Layer State
*/
uint32_t layer_state = 0;
2013-02-20 08:16:13 +06:00
static void layer_state_set(uint32_t state)
2013-02-20 08:16:13 +06:00
{
2013-05-14 13:18:22 +06:00
dprint("layer_state: ");
layer_debug(); dprint(" to ");
layer_state = state;
2013-05-14 13:18:22 +06:00
layer_debug(); dprintln();
clear_keyboard_but_mods(); // To avoid stuck keys
2013-02-20 08:16:13 +06:00
}
void layer_clear(void)
2013-02-20 08:16:13 +06:00
{
layer_state_set(0);
2013-02-20 08:16:13 +06:00
}
void layer_move(uint8_t layer)
2013-02-20 08:16:13 +06:00
{
layer_state_set(1UL<<layer);
2013-02-20 08:16:13 +06:00
}
2013-02-15 15:48:36 +06:00
void layer_on(uint8_t layer)
2013-02-15 15:48:36 +06:00
{
layer_state_set(layer_state | (1UL<<layer));
2013-02-15 15:48:36 +06:00
}
void layer_off(uint8_t layer)
2013-02-20 08:16:13 +06:00
{
layer_state_set(layer_state & ~(1UL<<layer));
2013-02-20 08:16:13 +06:00
}
void layer_invert(uint8_t layer)
2013-02-20 08:16:13 +06:00
{
layer_state_set(layer_state ^ (1UL<<layer));
2013-02-20 08:16:13 +06:00
}
void layer_or(uint32_t state)
2013-02-20 08:16:13 +06:00
{
layer_state_set(layer_state | state);
2013-02-20 08:16:13 +06:00
}
void layer_and(uint32_t state)
2013-02-20 08:16:13 +06:00
{
layer_state_set(layer_state & state);
2013-02-20 08:16:13 +06:00
}
void layer_xor(uint32_t state)
2013-02-20 08:16:13 +06:00
{
layer_state_set(layer_state ^ state);
2013-02-20 08:16:13 +06:00
}
void layer_debug(void)
2013-02-20 08:16:13 +06:00
{
2013-05-14 13:18:22 +06:00
dprintf("%08lX(%u)", layer_state, biton32(layer_state));
2013-02-20 08:16:13 +06:00
}
2013-03-19 21:56:54 +06:00
#endif
2013-02-20 08:16:13 +06:00
2013-02-15 15:48:36 +06:00
action_t layer_switch_get_action(key_t key)
{
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)) {
2013-02-15 15:48:36 +06:00
action = action_for_key(i, key);
if (action.code != ACTION_TRANSPARENT) {
return action;
}
}
}
/* fall back to layer 0 */
action = action_for_key(0, key);
return action;
#else
action = action_for_key(biton32(default_layer_state), key);
2013-02-15 15:48:36 +06:00
return action;
#endif
2013-02-15 15:48:36 +06:00
}