summaryrefslogtreecommitdiffstats
path: root/tmk_core/common/action_layer.c
blob: b8dcb34f3a04f65b8d0cd087e9f50ef931015726 (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <stdint.h>
#include "keyboard.h"
#include "action.h"
#include "util.h"
#include "action_layer.h"

#ifdef DEBUG_ACTION
#include "debug.h"
#else
#include "nodebug.h"
#endif


/** \brief Default Layer State
 */
uint32_t default_layer_state = 0;

/** \brief Default Layer State Set At user Level
 *
 * FIXME: Needs docs
 */
__attribute__((weak))
uint32_t default_layer_state_set_user(uint32_t state) {
    return state;
}

/** \brief Default Layer State Set At Keyboard Level
 *
 * FIXME: Needs docs
 */
__attribute__((weak))
uint32_t default_layer_state_set_kb(uint32_t state) {
    return default_layer_state_set_user(state);
}

/** \brief Default Layer State Set
 *
 * FIXME: Needs docs
 */
static void default_layer_state_set(uint32_t state)
{
    state = default_layer_state_set_kb(state);
    debug("default_layer_state: ");
    default_layer_debug(); debug(" to ");
    default_layer_state = state;
    default_layer_debug(); debug("\n");
    clear_keyboard_but_mods(); // To avoid stuck keys
}

/** \brief Default Layer Print
 *
 * FIXME: Needs docs
 */
void default_layer_debug(void)
{
    dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
}

/** \brief Default Layer Set
 *
 * FIXME: Needs docs
 */
void default_layer_set(uint32_t state)
{
    default_layer_state_set(state);
}

#ifndef NO_ACTION_LAYER
/** \brief Default Layer Or
 *
 * FIXME: Needs docs
 */
void default_layer_or(uint32_t state)
{
    default_layer_state_set(default_layer_state | state);
}
/** \brief Default Layer And
 *
 * FIXME: Needs docs
 */
void default_layer_and(uint32_t state)
{
    default_layer_state_set(default_layer_state & state);
}
/** \brief Default Layer Xor
 *
 * FIXME: Needs docs
 */
void default_layer_xor(uint32_t state)
{
    default_layer_state_set(default_layer_state ^ state);
}
#endif


#ifndef NO_ACTION_LAYER
/** \brief Keymap Layer State
 */
uint32_t layer_state = 0;

/** \brief Layer state set user
 *
 * FIXME: Needs docs
 */
__attribute__((weak))
uint32_t layer_state_set_user(uint32_t state) {
    return state;
}

/** \brief Layer state set keyboard
 *
 * FIXME: Needs docs
 */
__attribute__((weak))
uint32_t layer_state_set_kb(uint32_t state) {
    return layer_state_set_user(state);
}

/** \brief Layer state set
 *
 * FIXME: Needs docs
 */
void layer_state_set(uint32_t state)
{
    state = layer_state_set_kb(state);
    dprint("layer_state: ");
    layer_debug(); dprint(" to ");
    layer_state = state;
    layer_debug(); dprintln();
    clear_keyboard_but_mods(); // To avoid stuck keys
}

/** \brief Layer clear
 *
 * FIXME: Needs docs
 */
void layer_clear(void)
{
    layer_state_set(0);
}

/** \brief Layer state is
 *
 * FIXME: Needs docs
 */
bool layer_state_is(uint8_t layer)
{
    return layer_state_cmp(layer_state, layer);
}

/** \brief Layer state compare
 *
 * FIXME: Needs docs
 */
bool layer_state_cmp(uint32_t cmp_layer_state, uint8_t layer) {
    if (!cmp_layer_state) { return layer == 0; }
    return (cmp_layer_state & (1UL<<layer)) != 0;
}

/** \brief Layer move
 *
 * FIXME: Needs docs
 */
void layer_move(uint8_t layer)
{
    layer_state_set(1UL<<layer);
}

/** \brief Layer on
 *
 * FIXME: Needs docs
 */
void layer_on(uint8_t layer)
{
    layer_state_set(layer_state | (1UL<<layer));
}

/** \brief Layer off
 *
 * FIXME: Needs docs
 */
void layer_off(uint8_t layer)
{
    layer_state_set(layer_state & ~(1UL<<layer));
}

/** \brief Layer invert
 *
 * FIXME: Needs docs
 */
void layer_invert(uint8_t layer)
{
    layer_state_set(layer_state ^ (1UL<<layer));
}

/** \brief Layer or
 *
 * FIXME: Needs docs
 */
void layer_or(uint32_t state)
{
    layer_state_set(layer_state | state);
}
/** \brief Layer and
 *
 * FIXME: Needs docs
 */
void layer_and(uint32_t state)
{
    layer_state_set(layer_state & state);
}
/** \brief Layer xor
 *
 * FIXME: Needs docs
 */
void layer_xor(uint32_t state)
{
    layer_state_set(layer_state ^ state);
}

/** \brief Layer debug printing
 *
 * FIXME: Needs docs
 */
void layer_debug(void)
{
    dprintf("%08lX(%u)", layer_state, biton32(layer_state));
}
#endif

#if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
uint8_t source_layers_cache[(MATRIX_ROWS * MATRIX_COLS + 7) / 8][MAX_LAYER_BITS] = {{0}};

void update_source_layers_cache(keypos_t key, uint8_t layer)
{
    const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
    const uint8_t storage_row = key_number / 8;
    const uint8_t storage_bit = key_number % 8;

    for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
        source_layers_cache[storage_row][bit_number] ^=
            (-((layer & (1U << bit_number)) != 0)
             ^ source_layers_cache[storage_row][bit_number])
            & (1U << storage_bit);
    }
}

uint8_t read_source_layers_cache(keypos_t key)
{
    const uint8_t key_number = key.col + (key.row * MATRIX_COLS);
    const uint8_t storage_row = key_number / 8;
    const uint8_t storage_bit = key_number % 8;
    uint8_t layer = 0;

    for (uint8_t bit_number = 0; bit_number < MAX_LAYER_BITS; bit_number++) {
        layer |=
            ((source_layers_cache[storage_row][bit_number]
              & (1U << storage_bit)) != 0)
            << bit_number;
    }

    return layer;
}
#endif

/** \brief Store or get action (FIXME: Needs better summary)
 *
 * Make sure the action triggered when the key is released is the same
 * one as the one triggered on press. It's important for the mod keys
 * when the layer is switched after the down event but before the up
 * event as they may get stuck otherwise.
 */
action_t store_or_get_action(bool pressed, keypos_t key)
{
#if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
    if (disable_action_cache) {
        return layer_switch_get_action(key);
    }

    uint8_t layer;

    if (pressed) {
        layer = layer_switch_get_layer(key);
        update_source_layers_cache(key, layer);
    }
    else {
        layer = read_source_layers_cache(key);
    }
    return action_for_key(layer, key);
#else
    return layer_switch_get_action(key);
#endif
}


/** \brief Layer switch get layer
 *
 * FIXME: Needs docs
 */
int8_t layer_switch_get_layer(keypos_t key)
{
#ifndef NO_ACTION_LAYER
    action_t action;
    action.code = 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_TRANSPARENT) {
                return i;
            }
        }
    }
    /* fall back to layer 0 */
    return 0;
#else
    return biton32(default_layer_state);
#endif
}

/** \brief Layer switch get layer
 *
 * FIXME: Needs docs
 */
action_t layer_switch_get_action(keypos_t key)
{
    return action_for_key(layer_switch_get_layer(key), key);
}