]> git.gir.st - tmk_keyboard.git/blob - common/layer_switch.c
Merge branch 'overlays'
[tmk_keyboard.git] / common / layer_switch.c
1 #include <stdint.h>
2 #include "keyboard.h"
3 #include "action.h"
4 #include "debug.h"
5 #include "util.h"
6 #include "layer_switch.h"
7
8
9 /*
10 * Default Layer (0-15)
11 */
12 uint8_t default_layer = 0;
13
14 void default_layer_set(uint8_t layer)
15 {
16 debug("default_layer_set: ");
17 debug_dec(default_layer); debug(" to ");
18
19 default_layer = layer;
20
21 debug_dec(default_layer); debug("\n");
22
23 clear_keyboard_but_mods(); // To avoid stuck keys
24 }
25
26
27 /*
28 * Keymap Layer (0-15)
29 */
30 uint16_t keymap_stat = 0;
31
32 /* return highest layer whose state is on */
33 uint8_t keymap_get_layer(void)
34 {
35 return biton16(keymap_stat);
36 }
37
38 static void keymap_stat_set(uint16_t stat)
39 {
40 debug("keymap: ");
41 keymap_debug(); debug(" to ");
42
43 keymap_stat = stat;
44
45 keymap_debug(); debug("\n");
46
47 clear_keyboard_but_mods(); // To avoid stuck keys
48 }
49
50 void keymap_clear(void)
51 {
52 keymap_stat_set(0);
53 }
54
55
56 void keymap_set(uint16_t stat)
57 {
58 keymap_stat_set(stat);
59 }
60
61 void keymap_move(uint8_t layer)
62 {
63 keymap_stat_set(1<<layer);
64 }
65
66 void keymap_on(uint8_t layer)
67 {
68 keymap_stat_set(keymap_stat | (1<<layer));
69 }
70
71 void keymap_off(uint8_t layer)
72 {
73 keymap_stat_set(keymap_stat & ~(1<<layer));
74 }
75
76 void keymap_invert(uint8_t layer)
77 {
78 keymap_stat_set(keymap_stat ^ (1<<layer));
79 }
80
81 void keymap_or(uint16_t stat)
82 {
83 keymap_stat_set(keymap_stat | stat);
84 }
85 void keymap_and(uint16_t stat)
86 {
87 keymap_stat_set(keymap_stat & stat);
88 }
89 void keymap_xor(uint16_t stat)
90 {
91 keymap_stat_set(keymap_stat ^ stat);
92 }
93
94 void keymap_debug(void)
95 {
96 debug_hex16(keymap_stat); debug("("); debug_dec(keymap_get_layer()); debug(")");
97 }
98
99
100
101 /*
102 * Overlay Layer (16-31 = 0-15|0x10)
103 */
104 uint16_t overlay_stat = 0;
105
106 /* return highest layer whose state is on */
107 uint8_t overlay_get_layer(void)
108 {
109 return biton16(overlay_stat);
110 }
111
112 static void overlay_stat_set(uint16_t stat)
113 {
114 debug("overlay: ");
115 overlay_debug(); debug(" to ");
116
117 overlay_stat = stat;
118
119 overlay_debug(); debug("\n");
120
121 clear_keyboard_but_mods(); // To avoid stuck keys
122 }
123
124 void overlay_clear(void)
125 {
126 overlay_stat_set(0);
127 }
128
129
130 void overlay_set(uint16_t stat)
131 {
132 overlay_stat_set(stat);
133 }
134
135 void overlay_move(uint8_t layer)
136 {
137 overlay_stat_set(1<<layer);
138 }
139
140 void overlay_on(uint8_t layer)
141 {
142 overlay_stat_set(overlay_stat | (1<<layer));
143 }
144
145 void overlay_off(uint8_t layer)
146 {
147 overlay_stat_set(overlay_stat & ~(1<<layer));
148 }
149
150 void overlay_invert(uint8_t layer)
151 {
152 overlay_stat_set(overlay_stat ^ (1<<layer));
153 }
154
155 void overlay_or(uint16_t stat)
156 {
157 overlay_stat_set(overlay_stat | stat);
158 }
159 void overlay_and(uint16_t stat)
160 {
161 overlay_stat_set(overlay_stat & stat);
162 }
163 void overlay_xor(uint16_t stat)
164 {
165 overlay_stat_set(overlay_stat ^ stat);
166 }
167
168 void overlay_debug(void)
169 {
170 debug_hex16(overlay_stat); debug("("); debug_dec(overlay_get_layer()); debug(")");
171 }
172
173 action_t layer_switch_get_action(key_t key)
174 {
175 action_t action;
176 action.code = ACTION_TRANSPARENT;
177
178 /* overlay: top layer first */
179 for (int8_t i = 15; i >= 0; i--) {
180 if (overlay_stat & (1<<i)) {
181 action = action_for_key(i | OVERLAY_BIT, key);
182 if (action.code != ACTION_TRANSPARENT) {
183 return action;
184 }
185 }
186 }
187
188 /* keymap: top layer first */
189 for (int8_t i = 15; i >= 0; i--) {
190 if (keymap_stat & (1<<i)) {
191 action = action_for_key(i, key);
192 if (action.code != ACTION_TRANSPARENT) {
193 return action;
194 }
195 }
196 }
197
198 /* default layer */
199 action = action_for_key(default_layer, key);
200 return action;
201 }
Imprint / Impressum