qmk_firmware/common/layer_switch.c

105 lines
1.9 KiB
C
Raw Normal View History

2013-02-15 09:48:36 +00:00
#include <stdint.h>
#include "keyboard.h"
#include "action.h"
#include "debug.h"
2013-02-15 19:05:58 +00:00
#include "util.h"
2013-02-15 09:48:36 +00:00
#include "layer_switch.h"
2013-02-15 19:05:58 +00:00
uint8_t default_layer = 0;
2013-02-15 09:48:36 +00:00
uint16_t layer_switch_stat = 0;
2013-02-15 19:05:58 +00:00
uint16_t layer_switch_get_stat(void)
2013-02-15 09:48:36 +00:00
{
return layer_switch_stat;
}
2013-02-15 19:05:58 +00:00
/* return highest layer whose state is on */
uint8_t layer_switch_get_layer(void)
{
return biton16(layer_switch_stat);
}
static inline void stat_set(uint16_t stat)
2013-02-15 09:48:36 +00:00
{
2013-02-15 19:05:58 +00:00
debug("layer_switch: ");
layer_switch_debug(); debug(" to ");
2013-02-15 09:48:36 +00:00
layer_switch_stat = stat;
2013-02-15 19:05:58 +00:00
layer_switch_debug(); debug("\n");
clear_keyboard_but_mods(); // To avoid stuck keys
2013-02-15 09:48:36 +00:00
}
void layer_switch_clear(void)
{
2013-02-15 19:05:58 +00:00
stat_set(0);
}
void layer_switch_set(uint16_t stat)
{
stat_set(stat);
}
void layer_switch_move(uint8_t layer)
{
if (layer)
stat_set(1<<layer);
else
stat_set(0); // fall back to default layer
2013-02-15 09:48:36 +00:00
}
void layer_switch_on(uint8_t layer)
{
2013-02-15 19:05:58 +00:00
stat_set(layer_switch_stat | (1<<layer));
2013-02-15 09:48:36 +00:00
}
void layer_switch_off(uint8_t layer)
{
2013-02-15 19:05:58 +00:00
stat_set(layer_switch_stat & ~(1<<layer));
}
void layer_switch_invert(uint8_t layer)
{
stat_set(layer_switch_stat ^ (1<<layer));
2013-02-15 09:48:36 +00:00
}
2013-02-15 19:05:58 +00:00
void layer_switch_or(uint16_t stat)
{
stat_set(layer_switch_stat | stat);
}
void layer_switch_and(uint16_t stat)
{
stat_set(layer_switch_stat & stat);
}
void layer_switch_xor(uint16_t stat)
2013-02-15 09:48:36 +00:00
{
2013-02-15 19:05:58 +00:00
stat_set(layer_switch_stat ^ stat);
2013-02-15 09:48:36 +00:00
}
void layer_switch_debug(void)
{
2013-02-15 19:05:58 +00:00
debug_hex16(layer_switch_stat); debug("("); debug_dec(layer_switch_get_layer()); debug(")");
2013-02-15 09:48:36 +00:00
}
action_t layer_switch_get_action(key_t key)
{
action_t action;
action.code = ACTION_TRANSPARENT;
/* higher layer first */
for (int8_t i = 15; i >= 0; i--) {
if (layer_switch_stat & (1<<i)) {
action = action_for_key(i, key);
if (action.code != ACTION_TRANSPARENT) {
return action;
}
}
}
return action;
}