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