]> git.gir.st - tmk_keyboard.git/blob - common/action_layer.c
Squashed 'tmk_core/' changes from ee8c5ba..d5c5ac6
[tmk_keyboard.git] / common / action_layer.c
1 #include <stdint.h>
2 #include "keyboard.h"
3 #include "action.h"
4 #include "util.h"
5 #include "action_layer.h"
6 #include "hook.h"
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 */
18 uint32_t default_layer_state = 0;
19
20 static 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;
25 default_layer_debug(); debug("\n");
26 clear_keyboard_but_mods(); // To avoid stuck keys
27 }
28
29 void default_layer_debug(void)
30 {
31 dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
32 }
33
34 void default_layer_set(uint32_t state)
35 {
36 default_layer_state_set(state);
37 }
38
39 #ifndef NO_ACTION_LAYER
40 void default_layer_or(uint32_t state)
41 {
42 default_layer_state_set(default_layer_state | state);
43 }
44 void default_layer_and(uint32_t state)
45 {
46 default_layer_state_set(default_layer_state & state);
47 }
48 void default_layer_xor(uint32_t state)
49 {
50 default_layer_state_set(default_layer_state ^ state);
51 }
52 #endif
53
54
55 #ifndef NO_ACTION_LAYER
56 /*
57 * Keymap Layer State
58 */
59 uint32_t layer_state = 0;
60
61 static void layer_state_set(uint32_t state)
62 {
63 dprint("layer_state: ");
64 layer_debug(); dprint(" to ");
65 layer_state = state;
66 hook_layer_change(layer_state);
67 layer_debug(); dprintln();
68 clear_keyboard_but_mods(); // To avoid stuck keys
69 }
70
71 void layer_clear(void)
72 {
73 layer_state_set(0);
74 }
75
76 void layer_move(uint8_t layer)
77 {
78 layer_state_set(1UL<<layer);
79 }
80
81 void layer_on(uint8_t layer)
82 {
83 layer_state_set(layer_state | (1UL<<layer));
84 }
85
86 void layer_off(uint8_t layer)
87 {
88 layer_state_set(layer_state & ~(1UL<<layer));
89 }
90
91 void layer_invert(uint8_t layer)
92 {
93 layer_state_set(layer_state ^ (1UL<<layer));
94 }
95
96 void layer_or(uint32_t state)
97 {
98 layer_state_set(layer_state | state);
99 }
100 void layer_and(uint32_t state)
101 {
102 layer_state_set(layer_state & state);
103 }
104 void layer_xor(uint32_t state)
105 {
106 layer_state_set(layer_state ^ state);
107 }
108
109 void layer_debug(void)
110 {
111 dprintf("%08lX(%u)", layer_state, biton32(layer_state));
112 }
113 #endif
114
115
116
117 action_t layer_switch_get_action(keypos_t key)
118 {
119 action_t action;
120 action.code = ACTION_TRANSPARENT;
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