]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/action_layer.c
Rename file layer_switch to action_layer
[tmk_firmware.git] / common / action_layer.c
1 #include <stdint.h>
2 #include "keyboard.h"
3 #include "action.h"
4 #include "debug.h"
5 #include "util.h"
6 #include "action_layer.h"
7
8
9 /* 
10  * Default Layer State
11  */
12 uint32_t default_layer_state = 0;
13
14 static void default_layer_state_set(uint32_t state)
15 {
16     debug("default_layer_state: ");
17     default_layer_debug(); debug(" to ");
18     default_layer_state = state;
19     default_layer_debug(); debug("\n");
20     clear_keyboard_but_mods(); // To avoid stuck keys
21 }
22
23 void default_layer_debug(void)
24 {
25     debug_hex32(default_layer_state);
26     debug("("); debug_dec(biton32(default_layer_state)); debug(")");
27 }
28
29 void default_layer_set(uint8_t layer)
30 {
31     default_layer_state_set(1UL<<layer);
32 }
33
34 #ifndef NO_ACTION_LAYER
35 void default_layer_or(uint32_t state)
36 {
37     default_layer_state_set(default_layer_state | state);
38 }
39 void default_layer_and(uint32_t state)
40 {
41     default_layer_state_set(default_layer_state & state);
42 }
43 void default_layer_xor(uint32_t state)
44 {
45     default_layer_state_set(default_layer_state ^ state);
46 }
47 #endif
48
49
50 #ifndef NO_ACTION_LAYER
51 /* 
52  * Keymap Layer State
53  */
54 uint32_t layer_state = 0;
55
56 static void layer_state_set(uint32_t state)
57 {
58     debug("layer_state: ");
59     layer_debug(); debug(" to ");
60     layer_state = state;
61     layer_debug(); debug("\n");
62     clear_keyboard_but_mods(); // To avoid stuck keys
63 }
64
65 void layer_clear(void)
66 {
67     layer_state_set(0);
68 }
69
70 void layer_move(uint8_t layer)
71 {
72     layer_state_set(1UL<<layer);
73 }
74
75 void layer_on(uint8_t layer)
76 {
77     layer_state_set(layer_state | (1UL<<layer));
78 }
79
80 void layer_off(uint8_t layer)
81 {
82     layer_state_set(layer_state & ~(1UL<<layer));
83 }
84
85 void layer_invert(uint8_t layer)
86 {
87     layer_state_set(layer_state ^ (1UL<<layer));
88 }
89
90 void layer_or(uint32_t state)
91 {
92     layer_state_set(layer_state | state);
93 }
94 void layer_and(uint32_t state)
95 {
96     layer_state_set(layer_state & state);
97 }
98 void layer_xor(uint32_t state)
99 {
100     layer_state_set(layer_state ^ state);
101 }
102
103 void layer_debug(void)
104 {
105     debug_hex32(layer_state);
106     debug("("); debug_dec(biton32(layer_state)); debug(")");
107 }
108 #endif
109
110
111
112 action_t layer_switch_get_action(key_t key)
113 {
114     action_t action;
115     action.code = ACTION_TRANSPARENT;
116
117 #ifndef NO_ACTION_LAYER
118     uint32_t layers = layer_state | default_layer_state;
119     /* check top layer first */
120     for (int8_t i = 31; i >= 0; i--) {
121         if (layers & (1UL<<i)) {
122             action = action_for_key(i, key);
123             if (action.code != ACTION_TRANSPARENT) {
124                 return action;
125             }
126         }
127     }
128     /* fall back to layer 0 */
129     action = action_for_key(0, key);
130     return action;
131 #else
132     action = action_for_key(biton32(default_layer_state), key);
133     return action;
134 #endif
135 }