aboutsummaryrefslogtreecommitdiff
path: root/common/layer_switch.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/layer_switch.c')
-rw-r--r--common/layer_switch.c167
1 files changed, 132 insertions, 35 deletions
diff --git a/common/layer_switch.c b/common/layer_switch.c
index 22bfb34f6..19e286f88 100644
--- a/common/layer_switch.c
+++ b/common/layer_switch.c
@@ -6,84 +6,168 @@
6#include "layer_switch.h" 6#include "layer_switch.h"
7 7
8 8
9/*
10 * Default Layer (0-15)
11 */
9uint8_t default_layer = 0; 12uint8_t default_layer = 0;
10 13
11uint16_t layer_switch_stat = 0; 14void 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 */
30uint16_t keymap_stat = 0;
31
32/* return highest layer whose state is on */
33uint8_t keymap_get_layer(void)
34{
35 return biton16(keymap_stat);
36}
37
38static void keymap_stat_set(uint16_t stat)
39{
40 debug("keymap: ");
41 keymap_debug(); debug(" to ");
12 42
43 keymap_stat = stat;
44
45 keymap_debug(); debug("\n");
46
47 clear_keyboard_but_mods(); // To avoid stuck keys
48}
49
50void keymap_clear(void)
51{
52 keymap_stat_set(0);
53}
54
55
56void keymap_set(uint16_t stat)
57{
58 keymap_stat_set(stat);
59}
60
61void keymap_move(uint8_t layer)
62{
63 keymap_stat_set(1<<layer);
64}
13 65
14uint16_t layer_switch_get_stat(void) 66void keymap_on(uint8_t layer)
15{ 67{
16 return layer_switch_stat; 68 keymap_stat_set(keymap_stat | (1<<layer));
17} 69}
18 70
71void keymap_off(uint8_t layer)
72{
73 keymap_stat_set(keymap_stat & ~(1<<layer));
74}
75
76void keymap_invert(uint8_t layer)
77{
78 keymap_stat_set(keymap_stat ^ (1<<layer));
79}
80
81void keymap_or(uint16_t stat)
82{
83 keymap_stat_set(keymap_stat | stat);
84}
85void keymap_and(uint16_t stat)
86{
87 keymap_stat_set(keymap_stat & stat);
88}
89void keymap_xor(uint16_t stat)
90{
91 keymap_stat_set(keymap_stat ^ stat);
92}
93
94void 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 */
104uint16_t overlay_stat = 0;
105
19/* return highest layer whose state is on */ 106/* return highest layer whose state is on */
20uint8_t layer_switch_get_layer(void) 107uint8_t overlay_get_layer(void)
21{ 108{
22 return biton16(layer_switch_stat); 109 return biton16(overlay_stat);
23} 110}
24 111
25static inline void stat_set(uint16_t stat) 112static void overlay_stat_set(uint16_t stat)
26{ 113{
27 debug("layer_switch: "); 114 debug("overlay: ");
28 layer_switch_debug(); debug(" to "); 115 overlay_debug(); debug(" to ");
29 116
30 layer_switch_stat = stat; 117 overlay_stat = stat;
31 118
32 layer_switch_debug(); debug("\n"); 119 overlay_debug(); debug("\n");
33 120
34 clear_keyboard_but_mods(); // To avoid stuck keys 121 clear_keyboard_but_mods(); // To avoid stuck keys
35} 122}
36 123
37void layer_switch_clear(void) 124void overlay_clear(void)
38{ 125{
39 stat_set(0); 126 overlay_stat_set(0);
40} 127}
41 128
42 129
43void layer_switch_set(uint16_t stat) 130void overlay_set(uint16_t stat)
44{ 131{
45 stat_set(stat); 132 overlay_stat_set(stat);
46} 133}
47 134
48void layer_switch_move(uint8_t layer) 135void overlay_move(uint8_t layer)
49{ 136{
50 if (layer) 137 overlay_stat_set(1<<layer);
51 stat_set(1<<layer);
52 else
53 stat_set(0); // fall back to default layer
54} 138}
55 139
56void layer_switch_on(uint8_t layer) 140void overlay_on(uint8_t layer)
57{ 141{
58 stat_set(layer_switch_stat | (1<<layer)); 142 overlay_stat_set(overlay_stat | (1<<layer));
59} 143}
60 144
61void layer_switch_off(uint8_t layer) 145void overlay_off(uint8_t layer)
62{ 146{
63 stat_set(layer_switch_stat & ~(1<<layer)); 147 overlay_stat_set(overlay_stat & ~(1<<layer));
64} 148}
65 149
66void layer_switch_invert(uint8_t layer) 150void overlay_invert(uint8_t layer)
67{ 151{
68 stat_set(layer_switch_stat ^ (1<<layer)); 152 overlay_stat_set(overlay_stat ^ (1<<layer));
69} 153}
70 154
71void layer_switch_or(uint16_t stat) 155void overlay_or(uint16_t stat)
72{ 156{
73 stat_set(layer_switch_stat | stat); 157 overlay_stat_set(overlay_stat | stat);
74} 158}
75void layer_switch_and(uint16_t stat) 159void overlay_and(uint16_t stat)
76{ 160{
77 stat_set(layer_switch_stat & stat); 161 overlay_stat_set(overlay_stat & stat);
78} 162}
79void layer_switch_xor(uint16_t stat) 163void overlay_xor(uint16_t stat)
80{ 164{
81 stat_set(layer_switch_stat ^ stat); 165 overlay_stat_set(overlay_stat ^ stat);
82} 166}
83 167
84void layer_switch_debug(void) 168void overlay_debug(void)
85{ 169{
86 debug_hex16(layer_switch_stat); debug("("); debug_dec(layer_switch_get_layer()); debug(")"); 170 debug_hex16(overlay_stat); debug("("); debug_dec(overlay_get_layer()); debug(")");
87} 171}
88 172
89action_t layer_switch_get_action(key_t key) 173action_t layer_switch_get_action(key_t key)
@@ -91,14 +175,27 @@ action_t layer_switch_get_action(key_t key)
91 action_t action; 175 action_t action;
92 action.code = ACTION_TRANSPARENT; 176 action.code = ACTION_TRANSPARENT;
93 177
94 /* higher layer first */ 178 /* overlay: top layer first */
95 for (int8_t i = 15; i >= 0; i--) { 179 for (int8_t i = 15; i >= 0; i--) {
96 if (layer_switch_stat & (1<<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)) {
97 action = action_for_key(i, key); 191 action = action_for_key(i, key);
98 if (action.code != ACTION_TRANSPARENT) { 192 if (action.code != ACTION_TRANSPARENT) {
99 return action; 193 return action;
100 } 194 }
101 } 195 }
102 } 196 }
197
198 /* default layer */
199 action = action_for_key(default_layer, key);
103 return action; 200 return action;
104} 201}