a074364c |
1 | #include <stdint.h> |
2 | #include "keyboard.h" |
3 | #include "action.h" |
4 | #include "util.h" |
5 | #include "action_layer.h" |
71381457 |
6 | #include "hook.h" |
a074364c |
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; |
20b787fc |
25 | hook_default_layer_change(default_layer_state); |
a074364c |
26 | default_layer_debug(); debug("\n"); |
27 | clear_keyboard_but_mods(); // To avoid stuck keys |
28 | } |
29 | |
30 | void default_layer_debug(void) |
31 | { |
32 | dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state)); |
33 | } |
34 | |
35 | void default_layer_set(uint32_t state) |
36 | { |
37 | default_layer_state_set(state); |
38 | } |
39 | |
40 | #ifndef NO_ACTION_LAYER |
41 | void default_layer_or(uint32_t state) |
42 | { |
43 | default_layer_state_set(default_layer_state | state); |
44 | } |
45 | void default_layer_and(uint32_t state) |
46 | { |
47 | default_layer_state_set(default_layer_state & state); |
48 | } |
49 | void default_layer_xor(uint32_t state) |
50 | { |
51 | default_layer_state_set(default_layer_state ^ state); |
52 | } |
53 | #endif |
54 | |
55 | |
56 | #ifndef NO_ACTION_LAYER |
57 | /* |
58 | * Keymap Layer State |
59 | */ |
60 | uint32_t layer_state = 0; |
61 | |
62 | static void layer_state_set(uint32_t state) |
63 | { |
64 | dprint("layer_state: "); |
65 | layer_debug(); dprint(" to "); |
66 | layer_state = state; |
71381457 |
67 | hook_layer_change(layer_state); |
a074364c |
68 | layer_debug(); dprintln(); |
69 | clear_keyboard_but_mods(); // To avoid stuck keys |
70 | } |
71 | |
72 | void layer_clear(void) |
73 | { |
74 | layer_state_set(0); |
75 | } |
76 | |
77 | void layer_move(uint8_t layer) |
78 | { |
79 | layer_state_set(1UL<<layer); |
80 | } |
81 | |
82 | void layer_on(uint8_t layer) |
83 | { |
84 | layer_state_set(layer_state | (1UL<<layer)); |
85 | } |
86 | |
87 | void layer_off(uint8_t layer) |
88 | { |
89 | layer_state_set(layer_state & ~(1UL<<layer)); |
90 | } |
91 | |
92 | void layer_invert(uint8_t layer) |
93 | { |
94 | layer_state_set(layer_state ^ (1UL<<layer)); |
95 | } |
96 | |
97 | void layer_or(uint32_t state) |
98 | { |
99 | layer_state_set(layer_state | state); |
100 | } |
101 | void layer_and(uint32_t state) |
102 | { |
103 | layer_state_set(layer_state & state); |
104 | } |
105 | void layer_xor(uint32_t state) |
106 | { |
107 | layer_state_set(layer_state ^ state); |
108 | } |
109 | |
110 | void layer_debug(void) |
111 | { |
112 | dprintf("%08lX(%u)", layer_state, biton32(layer_state)); |
113 | } |
114 | #endif |
115 | |
116 | |
117 | |
118 | action_t layer_switch_get_action(keypos_t key) |
119 | { |
120 | action_t action; |
121 | action.code = ACTION_TRANSPARENT; |
122 | |
123 | #ifndef NO_ACTION_LAYER |
124 | uint32_t layers = layer_state | default_layer_state; |
125 | /* check top layer first */ |
126 | for (int8_t i = 31; i >= 0; i--) { |
127 | if (layers & (1UL<<i)) { |
128 | action = action_for_key(i, key); |
129 | if (action.code != ACTION_TRANSPARENT) { |
130 | return action; |
131 | } |
132 | } |
133 | } |
134 | /* fall back to layer 0 */ |
135 | action = action_for_key(0, key); |
136 | return action; |
137 | #else |
138 | action = action_for_key(biton32(default_layer_state), key); |
139 | return action; |
140 | #endif |
141 | } |