]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/action.h
Remove ACT_KEYMAP and ACT_OVERLAY
[tmk_firmware.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 <stdint.h>
21 #include <stdbool.h>
22 #include "keyboard.h"
23 #include "keycode.h"
24 #include "action_macro.h"
25
26
27 typedef struct {
28     keyevent_t  event;
29 #ifndef NO_ACTION_TAPPING
30     /* tapping count and state */
31     struct {
32         bool    interrupted :1;
33         bool    reserved2   :1;
34         bool    reserved1   :1;
35         bool    reserved0   :1;
36         uint8_t count       :4;
37     } tap;
38 #endif
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    :5;
69         uint8_t  kind   :3;
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 /* 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);
114
115 /* debug */
116 void debug_event(keyevent_t event);
117 void debug_record(keyrecord_t record);
118 void debug_action(action_t action);
119
120
121
122 /*
123  * Action codes
124  * ============
125  * 16bit code: action_kind(4bit) + action_parameter(12bit)
126  *
127  * Keyboard Keys(00XX)
128  * -------------------
129  * ACT_LMODS(0000):
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
135  *
136  * ACT_RMODS(0001):
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
142  *
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
149  *
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
156  *
157  *
158  * Other keys(01XX)
159  * --------------------
160  * This action handles other usages than keyboard.
161  * ACT_USAGE(0100):
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)
166  *
167  * ACT_MOUSEKEY(0110):
168  * 0101|XXXX| keycode     Mouse key
169  *
170  *
171  * Layer Actions(10XX)
172  * -------------------
173  * ACT_LAYER: 
174  * 1000|--xx|0000 0000   Clear keyamp
175  * 100X|LLLL|0000 00xx   Reset default layer and clear keymap
176  * 100X|LLLL| keycode    Invert with tap key
177  * 100X|LLLL|1111 0000   Invert with tap toggle
178  * 100X|LLLL|1111 00xx   Invert[^= 1<<L]
179  * 100X|LLLL|1111 0100   On/Off
180  * 100X|LLLL|1111 01xx   On[|= 1<<L]
181  * 100X|LLLL|1111 1000   Off/On
182  * 100X|LLLL|1111 10xx   Off[&= ~(1<<L)]
183  * 100X|LLLL|1111 1100   Set/Clear
184  * 100X|LLLL|1111 11xx   Set[= 1<<L]
185  * XLLLL: Layer 0-31
186  * xx: On {00:for special use, 01:press, 10:release, 11:both}
187  *
188  * ACT_LAYER_BITOP:
189  * 101B|Booo|xxxx xxxx   bit operation
190  * BB: operand. which part of layer state bits
191  *      00: 0-7th bit
192  *      01: 8-15th bit
193  *      10: 16-23th bit
194  *      11: 24-31th bit
195  * ooo: operation.
196  *      000: AND
197  *      001: OR
198  *      010: XOR
199  *      011: 
200  *      100: LSHIFT
201  *      101: RSHIFT
202  *      110: 
203  *      111: 
204  * bbbb bbbb: bits
205  * layer_state |= (((layer_state>>(0bBB*8)) & 0xff) BITOP 0bxxxxxxxx)<<(0bBB*8)
206  * layer_state: 32-bit layer switch state
207  *
208  *
209  *
210  *
211  * Extensions(11XX)
212  * ----------------
213  * ACT_MACRO(1100):
214  * 1100|opt | id(8)      Macro play?
215  * 1100|1111| id(8)      Macro record?
216  *
217  * ACT_COMMAND(1110):
218  * 1110|opt | id(8)      Built-in Command exec
219  *
220  * ACT_FUNCTION(1111):
221  * 1111| address(12)     Function?
222  * 1111|opt | id(8)      Function?
223  *
224  */
225 enum action_kind_id {
226     ACT_MODS            = 0b0000,
227     ACT_LMODS           = 0b0000,
228     ACT_RMODS           = 0b0001,
229     ACT_MODS_TAP        = 0b0010,
230     ACT_LMODS_TAP       = 0b0010,
231     ACT_RMODS_TAP       = 0b0011,
232
233     ACT_USAGE           = 0b0100,
234     ACT_MOUSEKEY        = 0b0101,
235
236     ACT_LAYER           = 0b1000,
237     ACT_LAYER1          = 0b1001,
238     ACT_LAYER_BITOP     = 0b1010,
239     ACT_LAYER1_BITOP    = 0b1011,
240
241     ACT_MACRO           = 0b1100,
242     ACT_COMMAND         = 0b1110,
243     ACT_FUNCTION        = 0b1111
244 };
245
246
247 /* action utility */
248 #define ACTION_NO                       0
249 #define ACTION_TRANSPARENT              1
250 #define ACTION(kind, param)             ((kind)<<12 | (param))
251 #define MODS4(mods)                     (((mods)>>4 | (mods)) & 0x0F)
252
253 /*
254  * Key
255  */
256 #define ACTION_KEY(key)                 ACTION(ACT_LMODS,    key)
257 /* Mods & key */
258 #define ACTION_LMODS(mods)              ACTION(ACT_LMODS,    MODS4(mods)<<8 | 0x00)
259 #define ACTION_LMODS_KEY(mods, key)     ACTION(ACT_LMODS,    MODS4(mods)<<8 | (key))
260 #define ACTION_RMODS(mods)              ACTION(ACT_RMODS,    MODS4(mods)<<8 | 0x00)
261 #define ACTION_RMODS_KEY(mods, key)     ACTION(ACT_RMODS,    MODS4(mods)<<8 | (key))
262 #define ACTION_LMOD(mod)                ACTION(ACT_LMODS,    MODS4(MOD_BIT(mod))<<8 | 0x00)
263 #define ACTION_LMOD_KEY(mod, key)       ACTION(ACT_LMODS,    MODS4(MOD_BIT(mod))<<8 | (key))
264 #define ACTION_RMOD(mod)                ACTION(ACT_RMODS,    MODS4(MOD_BIT(mod))<<8 | 0x00)
265 #define ACTION_RMOD_KEY(mod, key)       ACTION(ACT_RMODS,    MODS4(MOD_BIT(mod))<<8 | (key))
266 /* Tap key */
267 enum mods_codes {
268     MODS_ONESHOT           = 0x00,
269 };
270 #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
271 #define ACTION_LMODS_ONESHOT(mods)      ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
272 #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
273 #define ACTION_RMODS_ONESHOT(mods)      ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
274 #define ACTION_LMOD_TAP_KEY(mod, key)   ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
275 #define ACTION_LMOD_ONESHOT(mod)        ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
276 #define ACTION_RMOD_TAP_KEY(mod, key)   ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
277 #define ACTION_RMOD_ONESHOT(mod)        ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
278
279 /* HID Usage */
280 enum usage_pages {
281     PAGE_SYSTEM,
282     PAGE_CONSUMER
283 };
284 #define ACTION_USAGE_SYSTEM(id)         ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
285 #define ACTION_USAGE_CONSUMER(id)       ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
286
287 /* Mousekey */
288 #define ACTION_MOUSEKEY(key)            ACTION(ACT_MOUSEKEY, key)
289
290
291
292 /* Layer Actions:
293  *      Invert  layer ^= (1<<layer)
294  *      On      layer |= (1<<layer)
295  *      Off     layer &= ~(1<<layer)
296  *      Set     layer = (1<<layer)
297  *      Clear   layer = 0
298  */
299 enum layer_param_on {
300     ON_PRESS    = 1,
301     ON_RELEASE  = 2,
302     ON_BOTH     = 3,
303 };
304
305 enum layer_pram_op {
306     OP_RESET = 0x00,
307     OP_INV4  = 0x00,
308     OP_INV   = 0xF0,
309     OP_ON    = 0xF4,
310     OP_OFF   = 0xF8,
311     OP_SET   = 0xFC,
312 };
313
314 enum layer_pram_bitop {
315     BITOP_AND,
316     BITOP_OR,
317     BITOP_XOR,
318     BITOP_LSHIFT,
319     BITOP_RSHIFT,
320 };
321
322 /* 
323  * Default Layer
324  */
325 #define ACTION_DEFAULT_LAYER                     ACTION(ACT_LAYER, ON_RELEASE<<8 | OP_RESET | 0)
326 #define ACTION_DEFAULT_LAYER_SET(layer)          ACTION_DEFAULT_LAYER_TO(layer, ON_RELEASE)
327 #define ACTION_DEFAULT_LAYER_TO(layer, on)       ACTION(ACT_LAYER, (layer)<<8 | OP_RESET | (on))
328
329 /*
330  * Keymap Layer
331  */
332 #define ACTION_LAYER_MOMENTARY(layer)           ACTION_LAYER_ON_OFF(layer)
333 #define ACTION_LAYER_TOGGLE(layer)              ACTION_LAYER_INV(layer, ON_RELEASE)
334 /* Keymap Invert */
335 #define ACTION_LAYER_INV(layer, on)             ACTION(ACT_LAYER, (layer)<<8 | OP_INV | (on))
336 #define ACTION_LAYER_TAP_TOGGLE(layer)          ACTION(ACT_LAYER, (layer)<<8 | OP_INV | 0)
337 /* Keymap On */
338 #define ACTION_LAYER_ON(layer, on)              ACTION(ACT_LAYER, (layer)<<8 | OP_ON  | (on))
339 #define ACTION_LAYER_ON_OFF(layer)              ACTION(ACT_LAYER, (layer)<<8 | OP_ON  | 0)
340 /* Keymap Off */
341 #define ACTION_LAYER_OFF(layer, on)             ACTION(ACT_LAYER, (layer)<<8 | OP_OFF | (on))
342 #define ACTION_LAYER_OFF_ON(layer)              ACTION(ACT_LAYER, (layer)<<8 | OP_OFF | 0)
343 /* Keymap Set */
344 #define ACTION_LAYER_SET(layer, on)             ACTION(ACT_LAYER, (layer)<<8 | OP_SET | (on))
345 #define ACTION_LAYER_SET_CLEAR(layer)           ACTION(ACT_LAYER, (layer)<<8 | OP_SET | 0)
346 /* Keymap Invert with tap key */
347 #define ACTION_LAYER_TAP_KEY(layer, key)        ACTION(ACT_LAYER, (layer)<<8 | (key))
348
349 /* Layer BitOp: 101|BB|ooo|xxxxxxxx */
350 #define ACTION_LAYER_BITOP(op, part, bits)      (ACT_LAYER_BITOP<<12 | (part&0x3)<<11 | (op&0x7)<<8 | bits)
351 #define ACTION_LAYER_AND(part, bits)            ACTION_LAYER_BITOP(BITOP_AND, part, bits)
352 #define ACTION_LAYER_OR(part, bits)             ACTION_LAYER_BITOP(BITOP_OR, part, bits)
353 #define ACTION_LAYER_XOR(part, bits)            ACTION_LAYER_BITOP(BITOP_XOR, part, bits)
354 #define ACTION_LAYER_LSHIFT(part, bits)         ACTION_LAYER_BITOP(BITOP_LSHIFT, part, bits)
355 #define ACTION_LAYER_RSHIFT(part, bits)         ACTION_LAYER_BITOP(BITOP_RSHIFT, part, bits)
356  
357
358 /*
359  * Extensions
360  */
361 /* Macro */
362 #define ACTION_MACRO(id)                ACTION(ACT_MACRO, (id))
363 #define ACTION_MACRO_TAP(id)            ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
364 #define ACTION_MACRO_OPT(id, opt)       ACTION(ACT_MACRO, (opt)<<8 | (id))
365
366 /* Command */
367 #define ACTION_COMMAND(id, opt)         ACTION(ACT_COMMAND,  (opt)<<8 | (addr))
368
369 /* Function */
370 enum function_opts {
371     FUNC_TAP        = 0x8,      /* indciates function is tappable */
372 };
373 #define ACTION_FUNCTION(id)             ACTION(ACT_FUNCTION, (id))
374 #define ACTION_FUNCTION_TAP(id)         ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
375 #define ACTION_FUNCTION_OPT(id, opt)    ACTION(ACT_FUNCTION, (opt)<<8 | (id))
376
377 #endif  /* ACTION_H */