2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "action_macro.h"
29 #ifndef NO_ACTION_TAPPING
30 /* tapping count and state */
43 * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15).
44 * AVR looks like a little endian in avr-gcc.
46 * NOTE: not portable across compiler/endianness?
47 * Byte order and bit order of 0x1234:
48 * Big endian: 15 ... 8 7 ... 210
51 * Little endian: 012 ... 7 8 ... 15
76 struct action_command {
81 struct action_function {
90 /* Execute action per keyevent */
91 void action_exec(keyevent_t event);
94 action_t action_for_key(uint8_t layer, key_t key);
97 const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt);
99 /* user defined special function */
100 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
102 /* Utilities for actions. */
103 void process_action(keyrecord_t *record);
104 void register_code(uint8_t code);
105 void unregister_code(uint8_t code);
106 void add_mods(uint8_t mods);
107 void del_mods(uint8_t mods);
108 void set_mods(uint8_t mods);
109 void clear_keyboard(void);
110 void clear_keyboard_but_mods(void);
111 bool sending_anykey(void);
112 void layer_switch(uint8_t new_layer);
113 bool is_tap_key(key_t key);
116 void debug_event(keyevent_t event);
117 void debug_record(keyrecord_t record);
118 void debug_action(action_t action);
125 * 16bit code: action_kind(4bit) + action_parameter(12bit)
127 * Keyboard Keys(00XX)
128 * -------------------
130 * 0000|0000|000000|00 No action
131 * 0000|0000|000000|01 Transparent
132 * 0000|0000| keycode Key
133 * 0000|mods|000000|00 Left mods
134 * 0000|mods| keycode Key & Left mods
137 * 0001|0000|000000|00 No action(not used)
138 * 0001|0000|000000|01 Transparent(not used)
139 * 0001|0000| keycode Key(no used)
140 * 0001|mods|000000|00 Right mods
141 * 0001|mods| keycode Key & Right mods
143 * ACT_LMODS_TAP(0010):
144 * 0010|mods|000000|00 Left mods OneShot
145 * 0010|mods|000000|01 (reserved)
146 * 0010|mods|000000|10 (reserved)
147 * 0010|mods|000000|11 (reserved)
148 * 0010|mods| keycode Left mods + tap Key
150 * ACT_RMODS_TAP(0011):
151 * 0011|mods|000000|00 Right mods OneShot
152 * 0011|mods|000000|01 (reserved)
153 * 0011|mods|000000|10 (reserved)
154 * 0011|mods|000000|11 (reserved)
155 * 0011|mods| keycode Right mods + tap Key
159 * --------------------
160 * This action handles other usages than keyboard.
162 * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
163 * 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
164 * 0100|10| usage(10) (reserved)
165 * 0100|11| usage(10) (reserved)
167 * ACT_MOUSEKEY(0110):
168 * 0101|XXXX| keycode Mouse key
171 * Layer Actions(10XX)
172 * -------------------
174 * 1000|--xx|0000 0000 Clear keyamp and overlay
175 * 1000|LLLL|0000 00xx Reset default layer and clear keymap and overlay
176 * 1000|LLLL| keycode Invert with tap key
177 * 1000|LLLL|1111 0000 Invert with tap toggle
178 * 1000|LLLL|1111 00xx Invert[^= 1<<L]
179 * 1000|LLLL|1111 0100 On/Off
180 * 1000|LLLL|1111 01xx On[|= 1<<L]
181 * 1000|LLLL|1111 1000 Off/On
182 * 1000|LLLL|1111 10xx Off[&= ~(1<<L)]
183 * 1000|LLLL|1111 1100 Set/Clear
184 * 1000|LLLL|1111 11xx Set[= 1<<L]
185 * default layer: 0-15(4bit)
186 * xx: On {00:for special use, 01:press, 10:release, 11:both}
189 * 1011|0000|0000 0000 Clear overlay
190 * 1011|LLLL|0000 00ss Invert 4-bit chunk [^= L<<(4*ss)]
191 * 1011|LLLL| keycode Invert with tap key
192 * 1011|LLLL|1111 0000 Invert with tap toggle
193 * 1011|LLLL|1111 00xx Invert[^= 1<<L]
194 * 1011|LLLL|1111 0100 On/Off(momentary)
195 * 1011|LLLL|1111 01xx On[|= 1<<L]
196 * 1011|LLLL|1111 1000 Off/On
197 * 1011|LLLL|1111 10xx Off[&= ~(1<<L)]
198 * 1011|LLLL|1111 1100 Set/Clear
199 * 1011|LLLL|1111 11xx Set[= 1<<L]
200 * overlays: 16-layer on/off status(16bit)
201 * xx: On {00:for special use, 01:press, 10:release, 11:both}
207 * 1100|opt | id(8) Macro play?
208 * 1100|1111| id(8) Macro record?
211 * 1110|opt | id(8) Built-in Command exec
213 * ACT_FUNCTION(1111):
214 * 1111| address(12) Function?
215 * 1111|opt | id(8) Function?
218 enum action_kind_id {
221 ACT_LMODS_TAP = 0b0010,
222 ACT_RMODS_TAP = 0b0011,
225 ACT_MOUSEKEY = 0b0101,
228 ACT_OVERLAY = 0b1001,
231 ACT_COMMAND = 0b1110,
232 ACT_FUNCTION = 0b1111
238 #define ACTION_TRANSPARENT 1
239 #define ACTION(kind, param) ((kind)<<12 | (param))
240 #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
245 #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
247 #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
248 #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
249 #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
250 #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
251 #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
252 #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
253 #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
254 #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
259 #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
260 #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
261 #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
262 #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
263 #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
264 #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
265 #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
266 #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
273 #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
274 #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
277 #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
282 * Invert layer ^= (1<<layer)
283 * On layer |= (1<<layer)
284 * Off layer &= ~(1<<layer)
285 * Set layer = (1<<layer)
304 #define ACTION_DEFAULT_LAYER ACTION(ACT_KEYMAP, ON_RELEASE<<8 | OP_RESET | 0)
305 #define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_TO(layer, ON_RELEASE)
306 #define ACTION_DEFAULT_LAYER_TO(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_RESET | (on))
310 #define ACTION_KEYMAP_MOMENTARY(layer) ACTION_KEYMAP_ON_OFF(layer)
311 #define ACTION_KEYMAP_TOGGLE(layer) ACTION_KEYMAP_INV(layer, ON_RELEASE)
313 #define ACTION_KEYMAP_INV(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | (on))
314 #define ACTION_KEYMAP_TAP_TOGGLE(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | 0)
316 #define ACTION_KEYMAP_ON(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | (on))
317 #define ACTION_KEYMAP_ON_OFF(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | 0)
319 #define ACTION_KEYMAP_OFF(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | (on))
320 #define ACTION_KEYMAP_OFF_ON(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | 0)
322 #define ACTION_KEYMAP_SET(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | (on))
323 #define ACTION_KEYMAP_SET_CLEAR(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | 0)
324 /* Keymap Invert with tap key */
325 #define ACTION_KEYMAP_TAP_KEY(layer, key) ACTION(ACT_KEYMAP, (layer)<<8 | (key))
330 #define ACTION_OVERLAY_MOMENTARY(layer) ACTION_OVERLAY_ON_OFF(layer)
331 #define ACTION_OVERLAY_TOGGLE(layer) ACTION_OVERLAY_INV(layer, ON_RELEASE)
333 #define ACTION_OVERLAY_CLEAR(on) ACTION(ACT_OVERLAY, 0<<8 | OP_INV4 | (on))
334 /* Overlay Invert 4-bit chunk */
335 #define ACTION_OVERLAY_INV4(bits, shift) ACTION(ACT_OVERLAY, (bits)<<8 | OP_INV4 | shift)
337 #define ACTION_OVERLAY_INV(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | (on))
338 #define ACTION_OVERLAY_TAP_TOGGLE(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | 0)
340 #define ACTION_OVERLAY_ON(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | (on))
341 #define ACTION_OVERLAY_ON_OFF(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | 0)
343 #define ACTION_OVERLAY_OFF(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | (on))
344 #define ACTION_OVERLAY_OFF_ON(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | 0)
346 #define ACTION_OVERLAY_SET(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | (on))
347 #define ACTION_OVERLAY_SET_CLEAR(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | 0)
348 /* Overlay Invert with tap key */
349 #define ACTION_OVERLAY_TAP_KEY(layer, key) ACTION(ACT_OVERLAY, (layer)<<8 | (key))
356 #define ACTION_MACRO(id) ACTION(ACT_MACRO, (id))
357 #define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
358 #define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id))
361 #define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
365 FUNC_TAP = 0x8, /* indciates function is tappable */
367 #define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id))
368 #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
369 #define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id))
371 #endif /* ACTION_H */