summaryrefslogtreecommitdiffstats
path: root/common/layer_switch.c
blob: 19e286f8853fa47908d387631cfd79ee4f1ce6d1 (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
#include <stdint.h>
#include "keyboard.h"
#include "action.h"
#include "debug.h"
#include "util.h"
#include "layer_switch.h"


/* 
 * Default Layer (0-15)
 */
uint8_t default_layer = 0;

void default_layer_set(uint8_t layer)
{
    debug("default_layer_set: ");
    debug_dec(default_layer); debug(" to ");

    default_layer = layer;

    debug_dec(default_layer); debug("\n");

    clear_keyboard_but_mods(); // To avoid stuck keys
}


/* 
 * Keymap Layer (0-15)
 */
uint16_t keymap_stat = 0;

/* return highest layer whose state is on */
uint8_t keymap_get_layer(void)
{
    return biton16(keymap_stat);
}

static void keymap_stat_set(uint16_t stat)
{
    debug("keymap: ");
    keymap_debug(); debug(" to ");

    keymap_stat = stat;

    keymap_debug(); debug("\n");

    clear_keyboard_but_mods(); // To avoid stuck keys
}

void keymap_clear(void)
{
    keymap_stat_set(0);
}


void keymap_set(uint16_t stat)
{
    keymap_stat_set(stat);
}

void keymap_move(uint8_t layer)
{
    keymap_stat_set(1<<layer);
}

void keymap_on(uint8_t layer)
{
    keymap_stat_set(keymap_stat | (1<<layer));
}

void keymap_off(uint8_t layer)
{
    keymap_stat_set(keymap_stat & ~(1<<layer));
}

void keymap_invert(uint8_t layer)
{
    keymap_stat_set(keymap_stat ^ (1<<layer));
}

void keymap_or(uint16_t stat)
{
    keymap_stat_set(keymap_stat | stat);
}
void keymap_and(uint16_t stat)
{
    keymap_stat_set(keymap_stat & stat);
}
void keymap_xor(uint16_t stat)
{
    keymap_stat_set(keymap_stat ^ stat);
}

void keymap_debug(void)
{
    debug_hex16(keymap_stat); debug("("); debug_dec(keymap_get_layer()); debug(")");
}



/* 
 * Overlay Layer (16-31 = 0-15|0x10)
 */
uint16_t overlay_stat = 0;

/* return highest layer whose state is on */
uint8_t overlay_get_layer(void)
{
    return biton16(overlay_stat);
}

static void overlay_stat_set(uint16_t stat)
{
    debug("overlay: ");
    overlay_debug(); debug(" to ");

    overlay_stat = stat;

    overlay_debug(); debug("\n");

    clear_keyboard_but_mods(); // To avoid stuck keys
}

void overlay_clear(void)
{
    overlay_stat_set(0);
}


void overlay_set(uint16_t stat)
{
    overlay_stat_set(stat);
}

void overlay_move(uint8_t layer)
{
    overlay_stat_set(1<<layer);
}

void overlay_on(uint8_t layer)
{
    overlay_stat_set(overlay_stat | (1<<layer));
}

void overlay_off(uint8_t layer)
{
    overlay_stat_set(overlay_stat & ~(1<<layer));
}

void overlay_invert(uint8_t layer)
{
    overlay_stat_set(overlay_stat ^ (1<<layer));
}

void overlay_or(uint16_t stat)
{
    overlay_stat_set(overlay_stat | stat);
}
void overlay_and(uint16_t stat)
{
    overlay_stat_set(overlay_stat & stat);
}
void overlay_xor(uint16_t stat)
{
    overlay_stat_set(overlay_stat ^ stat);
}

void overlay_debug(void)
{
    debug_hex16(overlay_stat); debug("("); debug_dec(overlay_get_layer()); debug(")");
}

action_t layer_switch_get_action(key_t key)
{
    action_t action;
    action.code = ACTION_TRANSPARENT;

    /* overlay: top layer first */
    for (int8_t i = 15; i >= 0; i--) {
        if (overlay_stat & (1<<i)) {
            action = action_for_key(i | OVERLAY_BIT, key);
            if (action.code != ACTION_TRANSPARENT) {
                return action;
            }
        }
    }

    /* keymap: top layer first */
    for (int8_t i = 15; i >= 0; i--) {
        if (keymap_stat & (1<<i)) {
            action = action_for_key(i, key);
            if (action.code != ACTION_TRANSPARENT) {
                return action;
            }
        }
    }

    /* default layer */
    action = action_for_key(default_layer, key);
    return action;
}