core: Fix for stuck key problem #441
[tmk_keyboard.git] / tmk_core / common / action_layer.c
1 #include <stdint.h>
2 #include "keyboard.h"
3 #include "action.h"
4 #include "util.h"
5 #include "action_layer.h"
6 #include "hook.h"
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;
25 hook_default_layer_change(default_layer_state);
26 default_layer_debug(); debug("\n");
27 #ifdef NO_TRACK_KEY_PRESS
28 clear_keyboard_but_mods(); // To avoid stuck keys
29 #endif
30 }
31
32 void default_layer_debug(void)
33 {
34 dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
35 }
36
37 void default_layer_set(uint32_t state)
38 {
39 default_layer_state_set(state);
40 }
41
42 #ifndef NO_ACTION_LAYER
43 void default_layer_or(uint32_t state)
44 {
45 default_layer_state_set(default_layer_state | state);
46 }
47 void default_layer_and(uint32_t state)
48 {
49 default_layer_state_set(default_layer_state & state);
50 }
51 void default_layer_xor(uint32_t state)
52 {
53 default_layer_state_set(default_layer_state ^ state);
54 }
55 #endif
56
57
58 #ifndef NO_ACTION_LAYER
59 /*
60 * Keymap Layer State
61 */
62 uint32_t layer_state = 0;
63
64 static void layer_state_set(uint32_t state)
65 {
66 dprint("layer_state: ");
67 layer_debug(); dprint(" to ");
68 layer_state = state;
69 hook_layer_change(layer_state);
70 layer_debug(); dprintln();
71 #ifdef NO_TRACK_KEY_PRESS
72 clear_keyboard_but_mods(); // To avoid stuck keys
73 #endif
74 }
75
76 void layer_clear(void)
77 {
78 layer_state_set(0);
79 }
80
81 void layer_move(uint8_t layer)
82 {
83 layer_state_set(1UL<<layer);
84 }
85
86 void layer_on(uint8_t layer)
87 {
88 layer_state_set(layer_state | (1UL<<layer));
89 }
90
91 void layer_off(uint8_t layer)
92 {
93 layer_state_set(layer_state & ~(1UL<<layer));
94 }
95
96 void layer_invert(uint8_t layer)
97 {
98 layer_state_set(layer_state ^ (1UL<<layer));
99 }
100
101 void layer_or(uint32_t state)
102 {
103 layer_state_set(layer_state | state);
104 }
105 void layer_and(uint32_t state)
106 {
107 layer_state_set(layer_state & state);
108 }
109 void layer_xor(uint32_t state)
110 {
111 layer_state_set(layer_state ^ state);
112 }
113
114 void layer_debug(void)
115 {
116 dprintf("%08lX(%u)", layer_state, biton32(layer_state));
117 }
118 #endif
119
120
121
122 /* return layer effective for key at this time */
123 static uint8_t current_layer_for_key(keypos_t key)
124 {
125 action_t action = ACTION_TRANSPARENT;
126
127 #ifndef NO_ACTION_LAYER
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);
133 if (action.code != (action_t)ACTION_TRANSPARENT.code) {
134 return i;
135 }
136 }
137 }
138 /* fall back to layer 0 */
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 {
152 uint8_t layer = 0;
153 #ifndef NO_TRACK_KEY_PRESS
154 if (event.pressed) {
155 layer = current_layer_for_key(event.key);
156 layer_pressed[event.key.row][event.key.col] = layer;
157 } else {
158 layer = layer_pressed[event.key.row][event.key.col];
159 }
160 #else
161 layer = current_layer_for_key(event.key);
162 #endif
163 return action_for_key(layer, event.key);
164 }
Imprint / Impressum