core: Fix for build option NO_ACTION_LAYER
[tmk_keyboard.git] / tmk_core / common / action_layer.c
CommitLineData
a074364c 1#include <stdint.h>
2#include "keyboard.h"
3#include "action.h"
4#include "util.h"
5#include "action_layer.h"
71381457 6#include "hook.h"
a074364c 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 */
18uint32_t default_layer_state = 0;
19
20static 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;
20b787fc 25 hook_default_layer_change(default_layer_state);
a074364c 26 default_layer_debug(); debug("\n");
ba2883fd 27#ifdef NO_TRACK_KEY_PRESS
a074364c 28 clear_keyboard_but_mods(); // To avoid stuck keys
ba2883fd 29#endif
a074364c 30}
31
32void default_layer_debug(void)
33{
34 dprintf("%08lX(%u)", default_layer_state, biton32(default_layer_state));
35}
36
37void default_layer_set(uint32_t state)
38{
39 default_layer_state_set(state);
40}
41
42#ifndef NO_ACTION_LAYER
43void default_layer_or(uint32_t state)
44{
45 default_layer_state_set(default_layer_state | state);
46}
47void default_layer_and(uint32_t state)
48{
49 default_layer_state_set(default_layer_state & state);
50}
51void 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 */
62uint32_t layer_state = 0;
63
64static void layer_state_set(uint32_t state)
65{
66 dprint("layer_state: ");
67 layer_debug(); dprint(" to ");
68 layer_state = state;
71381457 69 hook_layer_change(layer_state);
a074364c 70 layer_debug(); dprintln();
ba2883fd 71#ifdef NO_TRACK_KEY_PRESS
a074364c 72 clear_keyboard_but_mods(); // To avoid stuck keys
ba2883fd 73#endif
a074364c 74}
75
76void layer_clear(void)
77{
78 layer_state_set(0);
79}
80
81void layer_move(uint8_t layer)
82{
83 layer_state_set(1UL<<layer);
84}
85
86void layer_on(uint8_t layer)
87{
88 layer_state_set(layer_state | (1UL<<layer));
89}
90
91void layer_off(uint8_t layer)
92{
93 layer_state_set(layer_state & ~(1UL<<layer));
94}
95
96void layer_invert(uint8_t layer)
97{
98 layer_state_set(layer_state ^ (1UL<<layer));
99}
100
101void layer_or(uint32_t state)
102{
103 layer_state_set(layer_state | state);
104}
105void layer_and(uint32_t state)
106{
107 layer_state_set(layer_state & state);
108}
109void layer_xor(uint32_t state)
110{
111 layer_state_set(layer_state ^ state);
112}
113
114void layer_debug(void)
115{
116 dprintf("%08lX(%u)", layer_state, biton32(layer_state));
117}
118#endif
119
120
121
ba2883fd 122/* return layer effective for key at this time */
123static uint8_t current_layer_for_key(keypos_t key)
a074364c 124{
a074364c 125#ifndef NO_ACTION_LAYER
45f6e5cb 126 action_t action = ACTION_TRANSPARENT;
a074364c 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);
e5cb8469 132 if (action.code != (action_t)ACTION_TRANSPARENT.code) {
ba2883fd 133 return i;
a074364c 134 }
135 }
136 }
137 /* fall back to layer 0 */
ba2883fd 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 */
147static uint8_t layer_pressed[MATRIX_ROWS][MATRIX_COLS] = {};
148#endif
149action_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 }
a074364c 159#else
ba2883fd 160 layer = current_layer_for_key(event.key);
a074364c 161#endif
ba2883fd 162 return action_for_key(layer, event.key);
a074364c 163}
Imprint / Impressum