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