Merge branch 'overlays'
[tmk_keyboard.git] / common / action.h
1 /*
2 Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17 #ifndef ACTION_H
18 #define ACTION_H
19
20 #include "keyboard.h"
21 #include "keycode.h"
22 #include "action_macro.h"
23
24
25 /* Struct to record event and tap count */
26 typedef union {
27 struct {
28 bool interrupted :1;
29 bool reserved2 :1;
30 bool reserved1 :1;
31 bool reserved0 :1;
32 uint8_t count :4;
33 };
34 } tap_t;
35
36 typedef struct {
37 keyevent_t event;
38 tap_t tap;
39 } keyrecord_t;
40
41 /* Action struct.
42 *
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.
45 *
46 * NOTE: not portable across compiler/endianness?
47 * Byte order and bit order of 0x1234:
48 * Big endian: 15 ... 8 7 ... 210
49 * | 0x12 | 0x34 |
50 * 0001 0010 0011 0100
51 * Little endian: 012 ... 7 8 ... 15
52 * | 0x34 | 0x12 |
53 * 0010 1100 0100 1000
54 */
55 typedef union {
56 uint16_t code;
57 struct action_kind {
58 uint16_t param :12;
59 uint8_t id :4;
60 } kind;
61 struct action_key {
62 uint8_t code :8;
63 uint8_t mods :4;
64 uint8_t kind :4;
65 } key;
66 struct action_layer {
67 uint8_t code :8;
68 uint8_t val :4;
69 uint8_t kind :4;
70 } layer;
71 struct action_usage {
72 uint16_t code :10;
73 uint8_t page :2;
74 uint8_t kind :4;
75 } usage;
76 struct action_command {
77 uint8_t id :8;
78 uint8_t opt :4;
79 uint8_t kind :4;
80 } command;
81 struct action_function {
82 uint8_t id :8;
83 uint8_t opt :4;
84 uint8_t kind :4;
85 } func;
86 } action_t;
87
88
89
90 /* Execute action per keyevent */
91 void action_exec(keyevent_t event);
92
93 /* action for key */
94 action_t action_for_key(uint8_t layer, key_t key);
95
96 /* macro */
97 const prog_macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt);
98
99 /* user defined special function */
100 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
101
102 /*
103 * Utilities for actions.
104 */
105 void register_code(uint8_t code);
106 void unregister_code(uint8_t code);
107 void add_mods(uint8_t mods);
108 void del_mods(uint8_t mods);
109 void set_mods(uint8_t mods);
110 void clear_keyboard(void);
111 void clear_keyboard_but_mods(void);
112 bool sending_anykey(void);
113 void layer_switch(uint8_t new_layer);
114 bool is_tap_key(key_t key);
115 bool waiting_buffer_has_anykey_pressed(void);
116
117
118
119 /*
120 * Action codes
121 * ============
122 * 16bit code: action_kind(4bit) + action_parameter(12bit)
123 *
124 * Keyboard Keys(00XX)
125 * -------------------
126 * ACT_LMODS(0000):
127 * 0000|0000|000000|00 No action
128 * 0000|0000|000000|01 Transparent
129 * 0000|0000| keycode Key
130 * 0000|mods|000000|00 Left mods
131 * 0000|mods| keycode Key & Left mods
132 *
133 * ACT_RMODS(0001):
134 * 0001|0000|000000|00 No action(not used)
135 * 0001|0000|000000|01 Transparent(not used)
136 * 0001|0000| keycode Key(no used)
137 * 0001|mods|000000|00 Right mods
138 * 0001|mods| keycode Key & Right mods
139 *
140 * ACT_LMODS_TAP(0010):
141 * 0010|mods|000000|00 Left mods OneShot
142 * 0010|mods|000000|01 (reserved)
143 * 0010|mods|000000|10 (reserved)
144 * 0010|mods|000000|11 (reserved)
145 * 0010|mods| keycode Left mods + tap Key
146 *
147 * ACT_RMODS_TAP(0011):
148 * 0011|mods|000000|00 Right mods OneShot
149 * 0011|mods|000000|01 (reserved)
150 * 0011|mods|000000|10 (reserved)
151 * 0011|mods|000000|11 (reserved)
152 * 0011|mods| keycode Right mods + tap Key
153 *
154 *
155 * Other keys(01XX)
156 * --------------------
157 * This action handles other usages than keyboard.
158 * ACT_USAGE(0100):
159 * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
160 * 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
161 * 0100|10| usage(10) (reserved)
162 * 0100|11| usage(10) (reserved)
163 *
164 * ACT_MOUSEKEY(0110):
165 * 0101|XXXX| keycode Mouse key
166 *
167 *
168 * Layer Actions(10XX)
169 * -------------------
170 * ACT_KEYMAP:
171 * 1000|--xx|0000 0000 Clear keyamp and overlay
172 * 1000|LLLL|0000 00xx Reset default layer and clear keymap and overlay
173 * 1000|LLLL| keycode Invert with tap key
174 * 1000|LLLL|1111 0000 Invert with tap toggle
175 * 1000|LLLL|1111 00xx Invert[^= 1<<L]
176 * 1000|LLLL|1111 0100 On/Off
177 * 1000|LLLL|1111 01xx On[|= 1<<L]
178 * 1000|LLLL|1111 1000 Off/On
179 * 1000|LLLL|1111 10xx Off[&= ~(1<<L)]
180 * 1000|LLLL|1111 1100 Set/Clear
181 * 1000|LLLL|1111 11xx Set[= 1<<L]
182 * default layer: 0-15(4bit)
183 * xx: On {00:for special use, 01:press, 10:release, 11:both}
184 *
185 * ACT_OVERLAY:
186 * 1011|0000|0000 0000 Clear overlay
187 * 1011|LLLL|0000 00ss Invert 4-bit chunk [^= L<<(4*ss)]
188 * 1011|LLLL| keycode Invert with tap key
189 * 1011|LLLL|1111 0000 Invert with tap toggle
190 * 1011|LLLL|1111 00xx Invert[^= 1<<L]
191 * 1011|LLLL|1111 0100 On/Off(momentary)
192 * 1011|LLLL|1111 01xx On[|= 1<<L]
193 * 1011|LLLL|1111 1000 Off/On
194 * 1011|LLLL|1111 10xx Off[&= ~(1<<L)]
195 * 1011|LLLL|1111 1100 Set/Clear
196 * 1011|LLLL|1111 11xx Set[= 1<<L]
197 * overlays: 16-layer on/off status(16bit)
198 * xx: On {00:for special use, 01:press, 10:release, 11:both}
199 *
200 *
201 * Extensions(11XX)
202 * ----------------
203 * ACT_MACRO(1100):
204 * 1100|opt | id(8) Macro play?
205 * 1100|1111| id(8) Macro record?
206 *
207 * ACT_COMMAND(1110):
208 * 1110|opt | id(8) Built-in Command exec
209 *
210 * ACT_FUNCTION(1111):
211 * 1111| address(12) Function?
212 * 1111|opt | id(8) Function?
213 *
214 */
215 enum action_kind_id {
216 ACT_LMODS = 0b0000,
217 ACT_RMODS = 0b0001,
218 ACT_LMODS_TAP = 0b0010,
219 ACT_RMODS_TAP = 0b0011,
220
221 ACT_USAGE = 0b0100,
222 ACT_MOUSEKEY = 0b0101,
223
224 ACT_KEYMAP = 0b1000,
225 ACT_OVERLAY = 0b1001,
226
227 ACT_MACRO = 0b1100,
228 ACT_COMMAND = 0b1110,
229 ACT_FUNCTION = 0b1111
230 };
231
232
233 /* action utility */
234 #define ACTION_NO 0
235 #define ACTION_TRANSPARENT 1
236 #define ACTION(kind, param) ((kind)<<12 | (param))
237 #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
238
239 /*
240 * Key
241 */
242 #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
243 /* Mods & key */
244 #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
245 #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
246 #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
247 #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
248 #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
249 #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
250 #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
251 #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
252 /* Tap key */
253 enum mods_codes {
254 MODS_ONESHOT = 0x00,
255 };
256 #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
257 #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
258 #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
259 #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
260 #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
261 #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
262 #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
263 #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
264
265 /* HID Usage */
266 enum usage_pages {
267 PAGE_SYSTEM,
268 PAGE_CONSUMER
269 };
270 #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
271 #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
272
273 /* Mousekey */
274 #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
275
276
277
278 /* Layer Actions:
279 * Invert layer ^= (1<<layer)
280 * On layer |= (1<<layer)
281 * Off layer &= ~(1<<layer)
282 * Set layer = (1<<layer)
283 * Clear layer = 0
284 */
285 enum layer_params {
286 ON_PRESS = 1,
287 ON_RELEASE = 2,
288 ON_BOTH = 3,
289
290 OP_RESET = 0x00,
291 OP_INV4 = 0x00,
292 OP_INV = 0xF0,
293 OP_ON = 0xF4,
294 OP_OFF = 0xF8,
295 OP_SET = 0xFC,
296 };
297
298 /*
299 * Default Layer
300 */
301 #define ACTION_DEFAULT_LAYER ACTION(ACT_KEYMAP, ON_RELEASE<<8 | OP_RESET | 0)
302 #define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_TO(layer, ON_RELEASE)
303 #define ACTION_DEFAULT_LAYER_TO(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_RESET | (on))
304 /*
305 * Keymap Layer
306 */
307 #define ACTION_KEYMAP_MOMENTARY(layer) ACTION_KEYMAP_ON_OFF(layer)
308 #define ACTION_KEYMAP_TOGGLE(layer) ACTION_KEYMAP_INV(layer, ON_RELEASE)
309 /* Keymap Invert */
310 #define ACTION_KEYMAP_INV(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | (on))
311 #define ACTION_KEYMAP_TAP_TOGGLE(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | 0)
312 /* Keymap On */
313 #define ACTION_KEYMAP_ON(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | (on))
314 #define ACTION_KEYMAP_ON_OFF(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | 0)
315 /* Keymap Off */
316 #define ACTION_KEYMAP_OFF(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | (on))
317 #define ACTION_KEYMAP_OFF_ON(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | 0)
318 /* Keymap Set */
319 #define ACTION_KEYMAP_SET(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | (on))
320 #define ACTION_KEYMAP_SET_CLEAR(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | 0)
321 /* Keymap Invert with tap key */
322 #define ACTION_KEYMAP_TAP_KEY(layer, key) ACTION(ACT_KEYMAP, (layer)<<8 | (key))
323
324 /*
325 * Overlay Layer
326 */
327 #define ACTION_OVERLAY_MOMENTARY(layer) ACTION_OVERLAY_ON_OFF(layer)
328 #define ACTION_OVERLAY_TOGGLE(layer) ACTION_OVERLAY_INV(layer, ON_RELEASE)
329 /* Overlay Clear */
330 #define ACTION_OVERLAY_CLEAR(on) ACTION(ACT_OVERLAY, 0<<8 | OP_INV4 | (on))
331 /* Overlay Invert 4-bit chunk */
332 #define ACTION_OVERLAY_INV4(bits, shift) ACTION(ACT_OVERLAY, (bits)<<8 | OP_INV4 | shift)
333 /* Overlay Invert */
334 #define ACTION_OVERLAY_INV(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | (on))
335 #define ACTION_OVERLAY_TAP_TOGGLE(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | 0)
336 /* Overlay On */
337 #define ACTION_OVERLAY_ON(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | (on))
338 #define ACTION_OVERLAY_ON_OFF(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | 0)
339 /* Overlay Off */
340 #define ACTION_OVERLAY_OFF(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | (on))
341 #define ACTION_OVERLAY_OFF_ON(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | 0)
342 /* Overlay Set */
343 #define ACTION_OVERLAY_SET(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | (on))
344 #define ACTION_OVERLAY_SET_CLEAR(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | 0)
345 /* Overlay Invert with tap key */
346 #define ACTION_OVERLAY_TAP_KEY(layer, key) ACTION(ACT_OVERLAY, (layer)<<8 | (key))
347
348
349 /*
350 * Extensions
351 */
352 /* Macro */
353 #define ACTION_MACRO(id) ACTION(ACT_MACRO, (id))
354 #define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
355 #define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id))
356
357 /* Command */
358 #define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
359
360 /* Function */
361 enum function_opts {
362 FUNC_TAP = 0x8, /* indciates function is tappable */
363 };
364 #define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id))
365 #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
366 #define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id))
367
368 #endif /* ACTION_H */
Imprint / Impressum