core: Change chibios repo directory names
[tmk_keyboard.git] / tmk_core / common / action_layer.c
CommitLineData
a074364c 1#include <stdint.h>
2#include "keyboard.h"
3#include "action.h"
4#include "util.h"
5#include "action_layer.h"
71381457 6#include "hook.h"
a074364c 7
8#ifdef DEBUG_ACTION
9#include "debug.h"
10#else
11#include "nodebug.h"
12#endif
13
14
15/*
16 * Default Layer State
17 */
18uint32_t default_layer_state = 0;
19
20static void default_layer_state_set(uint32_t state)
21{
22 debug("default_layer_state: ");
23 default_layer_debug(); debug(" to ");
24 default_layer_state = state;
20b787fc 25 hook_default_layer_change(default_layer_state);
a074364c 26 default_layer_debug(); debug("\n");
27 clear_keyboard_but_mods(); // To avoid stuck keys
28}
29
30void default_layer_debug(void)
31{
32 dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
33}
34
35void default_layer_set(uint32_t state)
36{
37 default_layer_state_set(state);
38}
39
40#ifndef NO_ACTION_LAYER
41void default_layer_or(uint32_t state)
42{
43 default_layer_state_set(default_layer_state | state);
44}
45void default_layer_and(uint32_t state)
46{
47 default_layer_state_set(default_layer_state & state);
48}
49void default_layer_xor(uint32_t state)
50{
51 default_layer_state_set(default_layer_state ^ state);
52}
53#endif
54
55
56#ifndef NO_ACTION_LAYER
57/*
58 * Keymap Layer State
59 */
60uint32_t layer_state = 0;
61
62static void layer_state_set(uint32_t state)
63{
64 dprint("layer_state: ");
65 layer_debug(); dprint(" to ");
66 layer_state = state;
71381457 67 hook_layer_change(layer_state);
a074364c 68 layer_debug(); dprintln();
69 clear_keyboard_but_mods(); // To avoid stuck keys
70}
71
72void layer_clear(void)
73{
74 layer_state_set(0);
75}
76
77void layer_move(uint8_t layer)
78{
79 layer_state_set(1UL<<layer);
80}
81
82void layer_on(uint8_t layer)
83{
84 layer_state_set(layer_state | (1UL<<layer));
85}
86
87void layer_off(uint8_t layer)
88{
89 layer_state_set(layer_state & ~(1UL<<layer));
90}
91
92void layer_invert(uint8_t layer)
93{
94 layer_state_set(layer_state ^ (1UL<<layer));
95}
96
97void layer_or(uint32_t state)
98{
99 layer_state_set(layer_state | state);
100}
101void layer_and(uint32_t state)
102{
103 layer_state_set(layer_state & state);
104}
105void layer_xor(uint32_t state)
106{
107 layer_state_set(layer_state ^ state);
108}
109
110void layer_debug(void)
111{
112 dprintf("%08lX(%u)", layer_state, biton32(layer_state));
113}
114#endif
115
116
117
118action_t layer_switch_get_action(keypos_t key)
119{
5e9b21d7 120 action_t action = { .code = ACTION_TRANSPARENT };
a074364c 121
122#ifndef NO_ACTION_LAYER
123 uint32_t layers = layer_state | default_layer_state;
124 /* check top layer first */
125 for (int8_t i = 31; i >= 0; i--) {
126 if (layers & (1UL<<i)) {
127 action = action_for_key(i, key);
128 if (action.code != ACTION_TRANSPARENT) {
129 return action;
130 }
131 }
132 }
133 /* fall back to layer 0 */
134 action = action_for_key(0, key);
135 return action;
136#else
137 action = action_for_key(biton32(default_layer_state), key);
138 return action;
139#endif
140}
Imprint / Impressum