31d659b7aab2d6544dd50f535af5dfe05ce1e370
[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 #ifndef NO_ACTION_LAYER
126 action_t action = ACTION_TRANSPARENT;
127 uint32_t layers = layer_state | default_layer_state;
128 /* check top layer first */
129 for (int8_t i = 31; i >= 0; i--) {
130 if (layers & (1UL<<i)) {
131 action = action_for_key(i, key);
132 if (action.code != (action_t)ACTION_TRANSPARENT.code) {
133 return i;
134 }
135 }
136 }
137 /* fall back to layer 0 */
138 return 0;
139 #else
140 return biton32(default_layer_state);
141 #endif
142 }
143
144
145 #ifndef NO_TRACK_KEY_PRESS
146 /* record layer on where key is pressed */
147 static uint8_t layer_pressed[MATRIX_ROWS][MATRIX_COLS] = {};
148 #endif
149 action_t layer_switch_get_action(keyevent_t event)
150 {
151 uint8_t layer = 0;
152 #ifndef NO_TRACK_KEY_PRESS
153 if (event.pressed) {
154 layer = current_layer_for_key(event.key);
155 layer_pressed[event.key.row][event.key.col] = layer;
156 } else {
157 layer = layer_pressed[event.key.row][event.key.col];
158 }
159 #else
160 layer = current_layer_for_key(event.key);
161 #endif
162 return action_for_key(layer, event.key);
163 }
Imprint / Impressum