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