]> git.gir.st - tmk_keyboard.git/blob - common/layer_switch.c
Rewrite layer action with layer_switch
[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 uint8_t default_layer = 0;
10
11 uint16_t layer_switch_stat = 0;
12
13
14 uint16_t layer_switch_get_stat(void)
15 {
16 return layer_switch_stat;
17 }
18
19 /* return highest layer whose state is on */
20 uint8_t layer_switch_get_layer(void)
21 {
22 return biton16(layer_switch_stat);
23 }
24
25 static inline void stat_set(uint16_t stat)
26 {
27 debug("layer_switch: ");
28 layer_switch_debug(); debug(" to ");
29
30 layer_switch_stat = stat;
31
32 layer_switch_debug(); debug("\n");
33
34 clear_keyboard_but_mods(); // To avoid stuck keys
35 }
36
37 void layer_switch_clear(void)
38 {
39 stat_set(0);
40 }
41
42
43 void layer_switch_set(uint16_t stat)
44 {
45 stat_set(stat);
46 }
47
48 void layer_switch_move(uint8_t layer)
49 {
50 if (layer)
51 stat_set(1<<layer);
52 else
53 stat_set(0); // fall back to default layer
54 }
55
56 void layer_switch_on(uint8_t layer)
57 {
58 stat_set(layer_switch_stat | (1<<layer));
59 }
60
61 void layer_switch_off(uint8_t layer)
62 {
63 stat_set(layer_switch_stat & ~(1<<layer));
64 }
65
66 void layer_switch_invert(uint8_t layer)
67 {
68 stat_set(layer_switch_stat ^ (1<<layer));
69 }
70
71 void layer_switch_or(uint16_t stat)
72 {
73 stat_set(layer_switch_stat | stat);
74 }
75 void layer_switch_and(uint16_t stat)
76 {
77 stat_set(layer_switch_stat & stat);
78 }
79 void layer_switch_xor(uint16_t stat)
80 {
81 stat_set(layer_switch_stat ^ stat);
82 }
83
84 void layer_switch_debug(void)
85 {
86 debug_hex16(layer_switch_stat); debug("("); debug_dec(layer_switch_get_layer()); debug(")");
87 }
88
89 action_t layer_switch_get_action(key_t key)
90 {
91 action_t action;
92 action.code = ACTION_TRANSPARENT;
93
94 /* higher layer first */
95 for (int8_t i = 15; i >= 0; i--) {
96 if (layer_switch_stat & (1<<i)) {
97 action = action_for_key(i, key);
98 if (action.code != ACTION_TRANSPARENT) {
99 return action;
100 }
101 }
102 }
103 return action;
104 }
Imprint / Impressum