action_layer.h
5656 bytes
1/*
2Copyright 2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#pragma once
19
20#include <stdint.h>
21#include "keyboard.h"
22#include "action.h"
23#include "bitwise.h"
24
25#ifdef DYNAMIC_KEYMAP_ENABLE
26# ifndef DYNAMIC_KEYMAP_LAYER_COUNT
27# define DYNAMIC_KEYMAP_LAYER_COUNT 4
28# endif
29# define MAX_LAYER DYNAMIC_KEYMAP_LAYER_COUNT
30# if DYNAMIC_KEYMAP_LAYER_COUNT <= 8
31# ifndef LAYER_STATE_8BIT
32# define LAYER_STATE_8BIT
33# endif
34# elif DYNAMIC_KEYMAP_LAYER_COUNT <= 16
35# ifndef LAYER_STATE_16BIT
36# define LAYER_STATE_16BIT
37# endif
38# else
39# ifndef LAYER_STATE_32BIT
40# define LAYER_STATE_32BIT
41# endif
42# endif
43#endif
44
45#if !defined(LAYER_STATE_8BIT) && !defined(LAYER_STATE_16BIT) && !defined(LAYER_STATE_32BIT)
46# define LAYER_STATE_16BIT
47#endif
48
49#if defined(LAYER_STATE_8BIT)
50typedef uint8_t layer_state_t;
51# define MAX_LAYER_BITS 3
52# ifndef MAX_LAYER
53# define MAX_LAYER 8
54# endif
55# define get_highest_layer(state) biton(state)
56#elif defined(LAYER_STATE_16BIT)
57typedef uint16_t layer_state_t;
58# define MAX_LAYER_BITS 4
59# ifndef MAX_LAYER
60# define MAX_LAYER 16
61# endif
62# define get_highest_layer(state) biton16(state)
63#elif defined(LAYER_STATE_32BIT)
64typedef uint32_t layer_state_t;
65# define MAX_LAYER_BITS 5
66# ifndef MAX_LAYER
67# define MAX_LAYER 32
68# endif
69# define get_highest_layer(state) biton32(state)
70#else
71# error Layer Mask size not specified. HOW?!
72#endif
73
74/*
75 * Default Layer
76 */
77extern layer_state_t default_layer_state;
78void default_layer_debug(void);
79void default_layer_set(layer_state_t state);
80
81__attribute__((weak)) layer_state_t default_layer_state_set_kb(layer_state_t state);
82__attribute__((weak)) layer_state_t default_layer_state_set_user(layer_state_t state);
83
84#ifndef NO_ACTION_LAYER
85/* bitwise operation */
86void default_layer_or(layer_state_t state);
87void default_layer_and(layer_state_t state);
88void default_layer_xor(layer_state_t state);
89#else
90# define default_layer_or(state)
91# define default_layer_and(state)
92# define default_layer_xor(state)
93#endif
94
95/*
96 * Keymap Layer
97 */
98#ifndef NO_ACTION_LAYER
99extern layer_state_t layer_state;
100
101void layer_state_set(layer_state_t state);
102bool layer_state_is(uint8_t layer);
103bool layer_state_cmp(layer_state_t layer1, uint8_t layer2);
104
105void layer_debug(void);
106void layer_clear(void);
107void layer_move(uint8_t layer);
108void layer_on(uint8_t layer);
109void layer_off(uint8_t layer);
110void layer_invert(uint8_t layer);
111/* bitwise operation */
112void layer_or(layer_state_t state);
113void layer_and(layer_state_t state);
114void layer_xor(layer_state_t state);
115layer_state_t layer_state_set_user(layer_state_t state);
116layer_state_t layer_state_set_kb(layer_state_t state);
117
118/**
119 * @brief Applies the tri layer to global layer state. Not be used in layer_state_set_(kb|user) functions.
120 *
121 * @param layer1 First layer to check for tri layer
122 * @param layer2 Second layer to check for tri layer
123 * @param layer3 Layer to activate if both other layers are enabled
124 */
125void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3);
126/**
127 * @brief Applies the tri layer behavior to supplied layer bitmask, without using layer functions.
128 *
129 * @param state Original layer bitmask to check and modify
130 * @param layer1 First layer to check for tri layer
131 * @param layer2 Second layer to check for tri layer
132 * @param layer3 Layer to activate if both other layers are enabled
133 * @return layer_state_t returns a modified layer bitmask with tri layer modifications applied
134 */
135layer_state_t update_tri_layer_state(layer_state_t state, uint8_t layer1, uint8_t layer2, uint8_t layer3);
136#else
137# define layer_state 0
138
139# define layer_state_set(layer)
140# define layer_state_is(layer) (layer == 0)
141# define layer_state_cmp(state, layer) (state == 0 ? layer == 0 : (state & (layer_state_t)1 << layer) != 0)
142
143# define layer_debug()
144# define layer_clear()
145# define layer_move(layer) (void)layer
146# define layer_on(layer) (void)layer
147# define layer_off(layer) (void)layer
148# define layer_invert(layer) (void)layer
149# define layer_or(state) (void)state
150# define layer_and(state) (void)state
151# define layer_xor(state) (void)state
152# define layer_state_set_kb(state) (void)state
153# define layer_state_set_user(state) (void)state
154# define update_tri_layer(layer1, layer2, layer3)
155# define update_tri_layer_state(state, layer1, layer2, layer3) (void)state
156#endif
157
158/* pressed actions cache */
159#if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
160
161void update_source_layers_cache(keypos_t key, uint8_t layer);
162uint8_t read_source_layers_cache(keypos_t key);
163#endif
164action_t store_or_get_action(bool pressed, keypos_t key);
165
166/* return the topmost non-transparent layer currently associated with key */
167uint8_t layer_switch_get_layer(keypos_t key);
168
169/* return action depending on current layer status */
170action_t layer_switch_get_action(keypos_t key);