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 /* Execute action per keyevent */
25 void action_exec(keyevent_t event);
28 /* Struct to record event and tap count */
36 * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15).
37 * AVR looks like a little endian in avr-gcc.
39 * NOTE: not portable across compiler/endianness?
40 * Byte order and bit order of 0x1234:
41 * Big endian: 15 ... 8 7 ... 210
44 * Little endian: 012 ... 7 8 ... 15
69 struct action_command {
74 struct action_function {
83 * Utilities for actions.
85 void register_code(uint8_t code);
86 void unregister_code(uint8_t code);
87 void add_mods(uint8_t mods);
88 void del_mods(uint8_t mods);
89 void set_mods(uint8_t mods);
90 void clear_keyboard(void);
91 void clear_keyboard_but_mods(void);
92 bool sending_anykey(void);
93 void layer_switch(uint8_t new_layer);
94 bool is_tap_key(key_t key);
95 bool waiting_buffer_has_anykey_pressed(void);
103 * 16bit code: action_kind(4bit) + action_parameter(12bit)
108 0000|0000|000000|00 No action
109 0000|0000|000000|01 Transparent
110 0000|0000| keycode Key
111 0000|mods|000000|00 Left mods
112 0000|mods| keycode Key & Left mods
115 0001|0000|000000|00 No action(not used)
116 0001|0000|000000|01 Transparent(not used)
117 0001|0000| keycode Key(no used)
118 0001|mods|000000|00 Right mods
119 0001|mods| keycode Key & Right mods
122 0010|mods|000000|00 Left mods OneShot
123 0010|mods|000000|01 (reserved)
124 0010|mods|000000|10 (reserved)
125 0010|mods|000000|11 (reserved)
126 0010|mods| keycode Left mods + tap Key
129 0011|mods|000000|00 Right mods OneShot
130 0011|mods|000000|01 (reserved)
131 0011|mods|000000|10 (reserved)
132 0011|mods|000000|11 (reserved)
133 0011|mods| keycode Right mods + tap Key
138 This action handles other usages than keyboard.
140 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
141 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
142 0100|10| usage(10) (reserved)
143 0100|11| usage(10) (reserved)
148 TODO: can be combined with 'Other HID Usage'? to save action kind id.
150 0101|XXXX| keycode Mouse key
155 ACT_LAYER(1000): Set layer
156 ACT_LAYER_BIT(1001): Bit-op layer
158 1000|LLLL|0000 0000 set L to layer on press and set default on release(momentary)
159 1000|LLLL|0000 0001 set L to layer on press
160 1000|LLLL|0000 0010 set L to layer on release
161 1000|----|0000 0011 set default to layer on both(return to default layer)
162 1000|LLLL| keycode set L to layer while hold and send key on tap
163 1000|LLLL|1111 0000 set L to layer while hold and toggle on several taps
164 1000|LLLL|1111 1111 set L to default and layer(on press)
166 1001|BBBB|0000 0000 (not used)
167 1001|BBBB|0000 0001 bit-xor layer with B on press
168 1001|BBBB|0000 0010 bit-xor layer with B on release
169 1001|BBBB|0000 0011 bit-xor layer with B on both(momentary)
170 1001|BBBB| keycode bit-xor layer with B while hold and send key on tap
171 1001|BBBB|1111 0000 bit-xor layer with B while hold and toggle on several taps
172 1001|BBBB|1111 1111 bit-xor default with B and set layer(on press)
181 1100|opt | id(8) Macro play?
182 1100|1111| id(8) Macro record?
185 1110|opt | id(8) Built-in Command exec
188 1111| address(12) Function?
189 1111|opt | id(8) Function?
192 enum action_kind_id {
195 ACT_LMODS_TAP = 0b0010,
196 ACT_RMODS_TAP = 0b0011,
199 ACT_MOUSEKEY = 0b0101,
202 ACT_LAYER_BIT = 0b1001,
205 ACT_COMMAND = 0b1110,
206 ACT_FUNCTION = 0b1111
212 #define ACTION_TRANSPARENT 1
213 #define ACTION(kind, param) ((kind)<<12 | (param))
214 #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
217 #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
219 #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
220 #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
221 #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
222 #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
224 #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
225 #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
226 #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
227 #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
233 #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
234 #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
235 #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
236 #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
238 #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
239 #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
240 #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
241 #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
250 LAYER_ON_RELEASE = 2,
252 LAYER_TAP_TOGGLE = 0xF0,
253 LAYER_CHANGE_DEFAULT = 0xFF
255 enum layer_vals_default {
256 DEFAULT_ON_PRESS = 1,
257 DEFAULT_ON_RELEASE = 2,
262 * return to default layer
264 #define ACTION_LAYER_DEFAULT ACTION_LAYER_DEFAULT_R
265 /* set default layer on press */
266 #define ACTION_LAYER_DEFAULT_P ACTION(ACT_LAYER, DEFAULT_ON_PRESS<<8 | LAYER_DEFAULT)
267 /* set default layer on release */
268 #define ACTION_LAYER_DEFAULT_R ACTION(ACT_LAYER, DEFAULT_ON_RELEASE<<8 | LAYER_DEFAULT)
269 /* change default layer and set layer */
274 /* set layer on press and none on release */
275 #define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_P(layer)
276 /* set layer on press and set default on release (This is needed by legacy keymap support.) */
277 #define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY)
278 /* set layer on press and none on release */
279 #define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer)
280 /* set layer while hold and send key on tap */
281 #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key))
282 /* set layer on press */
283 #define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_PRESS)
284 /* set layer on release */
285 #define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_RELEASE)
286 /* set layer on hold and toggle on several taps */
287 #define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_TAP_TOGGLE)
288 /* set default layer on both press and release */
289 #define ACTION_LAYER_SET_DEFAULT(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_CHANGE_DEFAULT)
294 /* bit-xor on both press and release */
295 #define ACTION_LAYER_BIT(bits) ACTION_LAYER_BIT_MOMENTARY(bits)
296 #define ACTION_LAYER_BIT_MOMENTARY(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_MOMENTARY)
297 /* bit-xor on press */
298 #define ACTION_LAYER_BIT_TOGGLE(bits) ACTION_LAYER_BIT_R(bits)
299 /* bit-xor while hold and send key on tap */
300 #define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
301 /* bit-xor on press */
302 #define ACTION_LAYER_BIT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_PRESS)
303 /* bit-xor on release */
304 #define ACTION_LAYER_BIT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_RELEASE)
305 /* bit-xor while hold and toggle on several taps */
306 #define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE)
307 /* bit-xor default layer and set layer */
308 #define ACTION_LAYER_BIT_DEFAULT(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_CHANGE_DEFAULT)
316 #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
317 #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
320 #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
323 #define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr))
326 #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
330 FUNC_TAP = 0x8, /* indciates function is tappable */
332 #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id)
333 #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id)
335 #endif /* ACTION_H */