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| keycode Key
110 0000|mods|000000|00 Left mods
111 0000|mods| keycode Key & Left mods
114 0001|0000|000000|00 No action
115 0001|0000| keycode Key(no used)
116 0001|mods|000000|00 Right mods
117 0001|mods| keycode Key & Right mods
120 0010|mods|000000|00 Left mods OneShot
121 0010|mods|000000|01 (reserved)
122 0010|mods|000000|10 (reserved)
123 0010|mods|000000|11 (reserved)
124 0010|mods| keycode Left mods + tap Key
127 0011|mods|000000|00 Right mods OneShot
128 0011|mods|000000|01 (reserved)
129 0011|mods|000000|10 (reserved)
130 0011|mods|000000|11 (reserved)
131 0011|mods| keycode Right mods + tap Key
136 This action handles other usages than keyboard.
138 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
139 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
140 0100|10| usage(10) (reserved)
141 0100|11| usage(10) (reserved)
146 TODO: can be combined with 'Other HID Usage'? to save action kind id.
148 0101|XXXX| keycode Mouse key
153 TODO: reconsider layer methods.
154 1 momemtary + tap key up: L, down: default
155 1 bitwise + tap key up: xor B, down: xor B
156 3 momemtary go + tap key? up: X, down:
157 3 toggle(mementary back) + tap key? up: down: Y
158 3 no tap up: X, down: Y
160 ACT_LAYER_PRESSED(1000): Set layer on key pressed
161 ACT_LAYER_RELEASED(1001): Set layer on key released
162 ACT_LAYER_BIT(1010): On/Off layer bit
163 ACT_LAYER_EXT(1011): Extentions
165 1000|LLLL|0000 0000 set layer L when pressed
166 1001|LLLL|0000 0000 set layer L when released
167 1010|BBBB|0000 0000 on/off bit B when pressed/released
168 1011|0000|0000 0000 set default layer when pressed
169 1011|0001|0000 0000 set default layer when released
171 1000|LLLL|1111 0000 set layer L when pressed + tap toggle
172 1001|LLLL|1111 0000 set layer L when released + tap toggle
173 1010|BBBB|1111 0000 on/off bit B when pressed/released + tap toggle
174 1011|0000|1111 0000 set default layer when pressed + tap toggle
175 1011|0001|1111 0000 set default layer when released + tap toggle
177 1000|LLLL|1111 1111 set L to default layer when pressed
178 1001|LLLL|1111 1111 set L to default layer when released
179 1010|BBBB|1111 1111 on/off bit B of default layer when pressed/released
180 1011|0000|1111 1111 set current to default layer when pressed
181 1011|0001|1111 1111 set current to default layer when released
183 1000|LLLL| keycode set layer L when pressed + tap key
184 1001|LLLL| keyocde set layer L when released + tap key
185 1010|BBBB| keyocde on/off bit B when pressed/released + tap key
186 1011|0000| keyocde set default layer when pressed + tap key
187 1011|0001| keyocde set default layer when released + tap key
195 1100|opt | id(8) Macro play?
196 1100|1111| id(8) Macro record?
199 1110|opt | id(8) Built-in Command exec
202 1111| address(12) Function?
203 1111|opt | id(8) Function?
206 enum action_kind_id {
209 ACT_LMODS_TAP = 0b0010,
210 ACT_RMODS_TAP = 0b0011,
213 ACT_MOUSEKEY = 0b0101,
215 ACT_LAYER_PRESSED = 0b1000,
216 ACT_LAYER_RELEASED = 0b1001,
217 ACT_LAYER_BIT = 0b1010,
218 ACT_LAYER_EXT = 0b1011,
221 ACT_COMMAND = 0b1110,
222 ACT_FUNCTION = 0b1111
236 #define ACTION(kind, param) ((kind)<<12 | (param))
237 #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
240 #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
242 #define ACTION_LMODS(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x00)
243 #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, (mods)<<8 | (key))
244 #define ACTION_RMODS(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x00)
245 #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key))
247 #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
248 #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
249 #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
250 #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
253 #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
254 #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | P_ONESHOT)
255 #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
256 #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | P_ONESHOT)
258 #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
259 #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | P_ONESHOT)
260 #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
261 #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | P_ONESHOT)
263 // TODO: contemplate about layer action
264 /* Switch current layer */
265 #define ACTION_LAYER_SET(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00)
266 #define ACTION_LAYER_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0x00)
267 #define ACTION_LAYER_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0x00)
268 #define ACTION_LAYER_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0x00)
269 #define ACTION_LAYER_SET_DEFAULT ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00)
270 #define ACTION_LAYER_RETURN_DEFAULT ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00)
271 #define ACTION_LAYER_SET_DEFAULT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0x00)
272 #define ACTION_LAYER_SET_DEFAULT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0x00)
273 /* Switch default layer */
274 #define ACTION_LAYER_DEFAULT_SET(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF)
275 #define ACTION_LAYER_DEFAULT_SET_ON_PRESSED(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xFF)
276 #define ACTION_LAYER_DEFAULT_SET_ON_RELEASED(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xFF)
277 #define ACTION_LAYER_DEFAULT_BIT(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | 0xFF)
278 #define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_PRESSED ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xFF)
279 #define ACTION_LAYER_DEFAULT_SET_CURRENT_ON_RELEASED ACTION(ACT_LAYER_EXT, 0x1<<8 | 0xFF)
280 /* Layer switch with tap key */
281 #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | (key))
282 #define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
283 #define ACTION_LAYER_DEFAULT_SET_TAP_KEY(key) ACTION(ACT_LAYER_EXT, 0x0<<8 | (key))
284 /* Layer switch with tap toggle */
285 #define ACTION_LAYER_SET_ON_PRESSED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_PRESSED, (layer)<<8 | 0xF0)
286 #define ACTION_LAYER_SET_ON_RELEASED_TAP_TOGGLE(layer) ACTION(ACT_LAYER_RELEASED, (layer)<<8 | 0xF0)
287 #define ACTION_LAYER_BIT_TAP_TOGGLE(layer) ACTION(ACT_LAYER_BIT, (layer)<<8 | 0xF0)
288 #define ACTION_LAYER_DEFAULT_TAP_TOGGLE ACTION(ACT_LAYER_EXT, 0x0<<8 | 0xF0)
291 #define ACTION_USAGE_PAGE_SYSTEM 0
292 #define ACTION_USAGE_PAGE_CONSUMER 1
293 #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, ACTION_USAGE_PAGE_SYSTEM<<10 | (id))
294 #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, ACTION_USAGE_PAGE_CONSUMER<<10 | (id))
297 #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
300 #define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr))
303 #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
306 #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id)
307 #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, O_TAP<<8 | id)
309 #endif /* ACTION_H */