]>
Commit | Line | Data |
---|---|---|
a074364c | 1 | #include <stdint.h> |
2 | #include "keyboard.h" | |
3 | #include "action.h" | |
4 | #include "util.h" | |
5 | #include "action_layer.h" | |
a209c962 | 6 | #include "led.h" |
71381457 | 7 | #include "hook.h" |
a074364c | 8 | |
9 | #ifdef DEBUG_ACTION | |
10 | #include "debug.h" | |
11 | #else | |
12 | #include "nodebug.h" | |
13 | #endif | |
14 | ||
15 | ||
a209c962 | 16 | /* |
a074364c | 17 | * Default Layer State |
18 | */ | |
19 | uint32_t default_layer_state = 0; | |
20 | ||
21 | static void default_layer_state_set(uint32_t state) | |
22 | { | |
23 | debug("default_layer_state: "); | |
24 | default_layer_debug(); debug(" to "); | |
25 | default_layer_state = state; | |
20b787fc | 26 | hook_default_layer_change(default_layer_state); |
a074364c | 27 | default_layer_debug(); debug("\n"); |
ba2883fd | 28 | #ifdef NO_TRACK_KEY_PRESS |
a074364c | 29 | clear_keyboard_but_mods(); // To avoid stuck keys |
ba2883fd | 30 | #endif |
a074364c | 31 | } |
32 | ||
33 | void default_layer_debug(void) | |
34 | { | |
35 | dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state)); | |
36 | } | |
37 | ||
38 | void default_layer_set(uint32_t state) | |
39 | { | |
40 | default_layer_state_set(state); | |
41 | } | |
42 | ||
43 | #ifndef NO_ACTION_LAYER | |
44 | void default_layer_or(uint32_t state) | |
45 | { | |
46 | default_layer_state_set(default_layer_state | state); | |
47 | } | |
48 | void default_layer_and(uint32_t state) | |
49 | { | |
50 | default_layer_state_set(default_layer_state & state); | |
51 | } | |
52 | void default_layer_xor(uint32_t state) | |
53 | { | |
54 | default_layer_state_set(default_layer_state ^ state); | |
55 | } | |
56 | #endif | |
57 | ||
58 | ||
59 | #ifndef NO_ACTION_LAYER | |
a209c962 | 60 | /* |
a074364c | 61 | * Keymap Layer State |
62 | */ | |
63 | uint32_t layer_state = 0; | |
64 | ||
65 | static void layer_state_set(uint32_t state) | |
66 | { | |
67 | dprint("layer_state: "); | |
68 | layer_debug(); dprint(" to "); | |
69 | layer_state = state; | |
71381457 | 70 | hook_layer_change(layer_state); |
a074364c | 71 | layer_debug(); dprintln(); |
ba2883fd | 72 | #ifdef NO_TRACK_KEY_PRESS |
a074364c | 73 | clear_keyboard_but_mods(); // To avoid stuck keys |
ba2883fd | 74 | #endif |
a074364c | 75 | } |
76 | ||
77 | void layer_clear(void) | |
78 | { | |
79 | layer_state_set(0); | |
80 | } | |
81 | ||
82 | void layer_move(uint8_t layer) | |
83 | { | |
84 | layer_state_set(1UL<<layer); | |
85 | } | |
86 | ||
87 | void layer_on(uint8_t layer) | |
88 | { | |
89 | layer_state_set(layer_state | (1UL<<layer)); | |
90 | } | |
91 | ||
92 | void layer_off(uint8_t layer) | |
93 | { | |
94 | layer_state_set(layer_state & ~(1UL<<layer)); | |
95 | } | |
96 | ||
97 | void layer_invert(uint8_t layer) | |
98 | { | |
99 | layer_state_set(layer_state ^ (1UL<<layer)); | |
100 | } | |
101 | ||
102 | void layer_or(uint32_t state) | |
103 | { | |
104 | layer_state_set(layer_state | state); | |
105 | } | |
106 | void layer_and(uint32_t state) | |
107 | { | |
108 | layer_state_set(layer_state & state); | |
109 | } | |
110 | void layer_xor(uint32_t state) | |
111 | { | |
112 | layer_state_set(layer_state ^ state); | |
113 | } | |
114 | ||
115 | void layer_debug(void) | |
116 | { | |
117 | dprintf("%08lX(%u)", layer_state, biton32(layer_state)); | |
118 | } | |
119 | #endif | |
120 | ||
121 | ||
122 | ||
ba2883fd | 123 | /* return layer effective for key at this time */ |
124 | static uint8_t current_layer_for_key(keypos_t key) | |
a074364c | 125 | { |
a074364c | 126 | #ifndef NO_ACTION_LAYER |
45f6e5cb | 127 | action_t action = ACTION_TRANSPARENT; |
a074364c | 128 | uint32_t layers = layer_state | default_layer_state; |
129 | /* check top layer first */ | |
130 | for (int8_t i = 31; i >= 0; i--) { | |
131 | if (layers & (1UL<<i)) { | |
132 | action = action_for_key(i, key); | |
e5cb8469 | 133 | if (action.code != (action_t)ACTION_TRANSPARENT.code) { |
ba2883fd | 134 | return i; |
a074364c | 135 | } |
136 | } | |
137 | } | |
138 | /* fall back to layer 0 */ | |
ba2883fd | 139 | return 0; |
140 | #else | |
141 | return biton32(default_layer_state); | |
142 | #endif | |
143 | } | |
144 | ||
145 | ||
146 | #ifndef NO_TRACK_KEY_PRESS | |
147 | /* record layer on where key is pressed */ | |
148 | static uint8_t layer_pressed[MATRIX_ROWS][MATRIX_COLS] = {}; | |
149 | #endif | |
150 | action_t layer_switch_get_action(keyevent_t event) | |
151 | { | |
df7ce59d | 152 | if (IS_NOEVENT(event)) return (action_t)ACTION_NO; |
153 | ||
ba2883fd | 154 | uint8_t layer = 0; |
155 | #ifndef NO_TRACK_KEY_PRESS | |
156 | if (event.pressed) { | |
157 | layer = current_layer_for_key(event.key); | |
158 | layer_pressed[event.key.row][event.key.col] = layer; | |
159 | } else { | |
160 | layer = layer_pressed[event.key.row][event.key.col]; | |
161 | } | |
a074364c | 162 | #else |
ba2883fd | 163 | layer = current_layer_for_key(event.key); |
a074364c | 164 | #endif |
ba2883fd | 165 | return action_for_key(layer, event.key); |
a074364c | 166 | } |