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