]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/action.h
Add layer stack
[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 "keyboard.h"
21 #include "keycode.h"
22
23
24 /* Struct to record event and tap count  */
25 typedef struct {
26     keyevent_t  event;
27     uint8_t     tap_count;
28 } keyrecord_t;
29
30 /* Action struct.
31  *
32  * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15).
33  * AVR looks like a little endian in avr-gcc.
34  *
35  * NOTE: not portable across compiler/endianness?
36  * Byte order and bit order of 0x1234:
37  * Big endian:     15 ...  8 7 ... 210
38  *                |  0x12   |  0x34   |
39  *                 0001 0010 0011 0100
40  * Little endian:  012 ... 7  8 ... 15
41  *                |  0x34   |  0x12   |
42  *                 0010 1100 0100 1000
43  */
44 typedef union {
45     uint16_t code;
46     struct action_kind {
47         uint16_t param  :12;
48         uint8_t  id     :4;
49     } kind;
50     struct action_key {
51         uint8_t  code   :8;
52         uint8_t  mods   :4;
53         uint8_t  kind   :4;
54     } key;
55     struct action_layer {
56         uint8_t  code   :8;
57         uint8_t  val    :4;
58         uint8_t  kind   :4;
59     } layer;
60     struct action_usage {
61         uint16_t code   :10;
62         uint8_t  page   :2;
63         uint8_t  kind   :4;
64     } usage;
65     struct action_command {
66         uint8_t  id     :8;
67         uint8_t  opt    :4;
68         uint8_t  kind   :4;
69     } command;
70     struct action_function {
71         uint8_t  id     :8;
72         uint8_t  opt    :4;
73         uint8_t  kind   :4;
74     } func;
75 } action_t;
76
77
78
79 /* layer used currently */
80 extern uint8_t current_layer;
81 /* layer to return or start with */
82 extern uint8_t default_layer;
83
84 /* Execute action per keyevent */
85 void action_exec(keyevent_t event);
86
87 /* action for key */
88 action_t action_for_key(uint8_t layer, key_t key);
89
90 /* user defined special function */
91 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
92
93 /*
94  * Utilities for actions.
95  */
96 void register_code(uint8_t code);
97 void unregister_code(uint8_t code);
98 void add_mods(uint8_t mods);
99 void del_mods(uint8_t mods);
100 void set_mods(uint8_t mods);
101 void clear_keyboard(void);
102 void clear_keyboard_but_mods(void);
103 bool sending_anykey(void);
104 void layer_switch(uint8_t new_layer);
105 bool is_tap_key(key_t key);
106 bool waiting_buffer_has_anykey_pressed(void);
107
108
109
110 /*
111  * Action codes
112  * ============
113  * 16bit code: action_kind(4bit) + action_parameter(12bit)
114  *
115  * Keyboard Keys
116  * -------------
117  * ACT_LMODS(0000):
118  * 0000|0000|000000|00    No action
119  * 0000|0000|000000|01    Transparent
120  * 0000|0000| keycode     Key
121  * 0000|mods|000000|00    Left mods
122  * 0000|mods| keycode     Key & Left mods
123  *
124  * ACT_RMODS(0001):
125  * 0001|0000|000000|00    No action(not used)
126  * 0001|0000|000000|01    Transparent(not used)
127  * 0001|0000| keycode     Key(no used)
128  * 0001|mods|000000|00    Right mods
129  * 0001|mods| keycode     Key & Right mods
130  *
131  * ACT_LMODS_TAP(0010):
132  * 0010|mods|000000|00    Left mods OneShot
133  * 0010|mods|000000|01    (reserved)
134  * 0010|mods|000000|10    (reserved)
135  * 0010|mods|000000|11    (reserved)
136  * 0010|mods| keycode     Left mods + tap Key
137  *
138  * ACT_RMODS_TAP(0011):
139  * 0011|mods|000000|00    Right mods OneShot
140  * 0011|mods|000000|01    (reserved)
141  * 0011|mods|000000|10    (reserved)
142  * 0011|mods|000000|11    (reserved)
143  * 0011|mods| keycode     Right mods + tap Key
144  *
145  *
146  * Other HID Usage
147  * ---------------
148  * This action handles other usages than keyboard.
149  * ACT_USAGE(0100):
150  * 0100|00| usage(10)     System control(0x80) - General Desktop page(0x01)
151  * 0100|01| usage(10)     Consumer control(0x01) - Consumer page(0x0C)
152  * 0100|10| usage(10)     (reserved)
153  * 0100|11| usage(10)     (reserved)
154  *
155  *
156  * Mouse Keys
157  * ----------
158  * TODO: can be combined with 'Other HID Usage'? to save action kind id.
159  * ACT_MOUSEKEY(0110):
160  * 0101|XXXX| keycode     Mouse key
161  *
162  *
163  * Layer Actions
164  * -------------
165  * ACT_LAYER(1000):             Set layer
166  * ACT_LAYER_BIT(1001):         Bit-op layer
167  * ACT_LAYER_STACK:             Layer stack
168  *
169  * 1000|LLLL|0000 0000   set current layer on press and return to default on release(momentary)
170  * 1000|LLLL|0000 0001   set current layer on press
171  * 1000|LLLL|0000 0010   set current layer on release
172  * 1000|LLLL|0000 0011   set current layer on both
173  * 1000|LLLL| keycode    set current layer on hold and send key on tap
174  * 1000|LLLL|1111 0000   set current layer on hold and toggle on several taps
175  * 1000|DDDD|1111 1111   set default layer on press
176  * L: 0 means default layer
177  *
178  * 1001|BBBB|0000 0000   bit-on current layer on press and bit-off on release(momentary)
179  * 1001|BBBB|0000 0001   bit-xor current layer on press
180  * 1001|BBBB|0000 0010   bit-xor current layer on release
181  * 1001|BBBB|0000 0011   bit-xor current layer on both
182  * 1001|BBBB| keycode    bit-xor current layer on hold and send key on tap
183  * 1001|BBBB|1111 0000   bit-xor current layer on hold and toggle on several taps
184  * 1001|BBBB|1111 1111   bit-xor default layer on both
185  *
186  * 1011|LLLL|0000 0000   push on press and remove on release(momentary)
187  * 1011|LLLL|0000 0001   push or remove on press
188  * 1011|LLLL|0000 0010   push or remove on release
189  * 1011|LLLL|0000 0011   push or remove on both
190  * 1011|LLLL| keycode    push or remove on hold and send key on tap
191  * 1011|LLLL|1111 0000   push or remove on hold and toggle on several taps
192  * 1011|LLLL|1111 1111   (not used)
193  *
194  *
195  * Extensions(11XX)
196  * ----------------
197  * NOTE: NOT FIXED
198  *
199  * ACT_MACRO(1100):
200  * 1100|opt | id(8)      Macro play?
201  * 1100|1111| id(8)      Macro record?
202  *
203  * ACT_COMMAND(1110):
204  * 1110|opt | id(8)      Built-in Command exec
205  *
206  * ACT_FUNCTION(1111):
207  * 1111| address(12)     Function?
208  * 1111|opt | id(8)      Function?
209  *
210  */
211 enum action_kind_id {
212     ACT_LMODS           = 0b0000,
213     ACT_RMODS           = 0b0001,
214     ACT_LMODS_TAP       = 0b0010,
215     ACT_RMODS_TAP       = 0b0011,
216
217     ACT_USAGE           = 0b0100,
218     ACT_MOUSEKEY        = 0b0101,
219
220     ACT_LAYER           = 0b1000,
221     ACT_LAYER_BIT       = 0b1001,
222     ACT_LAYER_STACK     = 0b1011,
223
224     ACT_MACRO           = 0b1100,
225     ACT_COMMAND         = 0b1110,
226     ACT_FUNCTION        = 0b1111
227 };
228
229
230 /* action utility */
231 #define ACTION_NO                       0
232 #define ACTION_TRANSPARENT              1
233 #define ACTION(kind, param)             ((kind)<<12 | (param))
234 #define MODS4(mods)                     (((mods)>>4 | (mods)) & 0x0F)
235
236 /* 
237  * Key
238  */
239 #define ACTION_KEY(key)                 ACTION(ACT_LMODS,    key)
240 /* Mods & key */
241 #define ACTION_LMODS(mods)              ACTION(ACT_LMODS,    MODS4(mods)<<8 | 0x00)
242 #define ACTION_LMODS_KEY(mods, key)     ACTION(ACT_LMODS,    MODS4(mods)<<8 | (key))
243 #define ACTION_RMODS(mods)              ACTION(ACT_RMODS,    MODS4(mods)<<8 | 0x00)
244 #define ACTION_RMODS_KEY(mods, key)     ACTION(ACT_RMODS,    MODS4(mods)<<8 | (key))
245 #define ACTION_LMOD(mod)                ACTION(ACT_LMODS,    MODS4(MOD_BIT(mod))<<8 | 0x00)
246 #define ACTION_LMOD_KEY(mod, key)       ACTION(ACT_LMODS,    MODS4(MOD_BIT(mod))<<8 | (key))
247 #define ACTION_RMOD(mod)                ACTION(ACT_RMODS,    MODS4(MOD_BIT(mod))<<8 | 0x00)
248 #define ACTION_RMOD_KEY(mod, key)       ACTION(ACT_RMODS,    MODS4(MOD_BIT(mod))<<8 | (key))
249 /* Tap key */
250 enum mods_codes {
251     MODS_ONESHOT           = 0x00,
252 };
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 | MODS_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 | MODS_ONESHOT)
257 #define ACTION_LMOD_TAP_KEY(mod, key)   ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
258 #define ACTION_LMOD_ONESHOT(mod)        ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
259 #define ACTION_RMOD_TAP_KEY(mod, key)   ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
260 #define ACTION_RMOD_ONESHOT(mod)        ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
261
262
263 /*
264  * Layer switching
265  */
266 enum layer_codes {
267     LAYER_MOMENTARY = 0,
268     LAYER_ON_PRESS = 1,
269     LAYER_ON_RELEASE = 2,
270     LAYER_ON_BOTH =3,
271     LAYER_TAP_TOGGLE = 0xF0,
272     LAYER_SET_DEFAULT_ON_PRESS = 0xFD,
273     LAYER_SET_DEFAULT_ON_RELEASE = 0xFE,
274     LAYER_SET_DEFAULT_ON_BOTH = 0xFF
275 };
276 /*
277  * Default layer
278  */
279 /* set default layer */
280 #define ACTION_LAYER_SET_DEFAULT(layer)         ACTION_LAYER_SET_DEFAULT_R(layer)
281 #define ACTION_LAYER_SET_DEFAULT_P(layer)       ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_PRESS)
282 #define ACTION_LAYER_SET_DEFAULT_R(layer)       ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_RELEASE)
283 #define ACTION_LAYER_SET_DEFAULT_B(layer)       ACTION(ACT_LAYER, (layer)<<8 | LAYER_SET_DEFAULT_ON_BOTH)
284 /* bit-xor default layer */
285 #define ACTION_LAYER_BIT_DEFAULT(bits)          ACTION_LAYER_BIT_DEFAULT_R(bits)
286 #define ACTION_LAYER_BIT_DEFAULT_P(bits)        ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_PRESS)
287 #define ACTION_LAYER_BIT_DEFAULT_R(bits)        ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_RELEASE)
288 #define ACTION_LAYER_BIT_DEFAULT_B(bits)        ACTION(ACT_LAYER, (bits)<<8 | LAYER_SET_DEFAULT_ON_BOTH)
289 /*
290  * Current layer: Return to default layer
291  */
292 #define ACTION_LAYER_DEFAULT                    ACTION_LAYER_DEFAULT_R
293 #define ACTION_LAYER_DEFAULT_P                  ACTION_LAYER_SET_P(0)
294 #define ACTION_LAYER_DEFAULT_R                  ACTION_LAYER_SET_R(0)
295 #define ACTION_LAYER_DEFAULT_B                  ACTION_LAYER_SET_B(0)
296 /*
297  * Current layer: Set
298  */
299 #define ACTION_LAYER_SET(layer)                 ACTION_LAYER_SET_P(layer)
300 #define ACTION_LAYER_SET_MOMENTARY(layer)       ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY)
301 #define ACTION_LAYER_SET_TOGGLE(layer)          ACTION_LAYER_SET_R(layer)
302 #define ACTION_LAYER_SET_P(layer)               ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_PRESS)
303 #define ACTION_LAYER_SET_R(layer)               ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_RELEASE)
304 #define ACTION_LAYER_SET_B(layer)               ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_BOTH)
305 #define ACTION_LAYER_SET_TAP_TOGGLE(layer)      ACTION(ACT_LAYER, (layer)<<8 | LAYER_TAP_TOGGLE)
306 #define ACTION_LAYER_SET_TAP_KEY(layer, key)    ACTION(ACT_LAYER, (layer)<<8 | (key))
307 /*
308  * Current layer: Bit-op
309  */
310 #define ACTION_LAYER_BIT(bits)                  ACTION_LAYER_BIT_MOMENTARY(bits)
311 #define ACTION_LAYER_BIT_MOMENTARY(bits)        ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_MOMENTARY)
312 #define ACTION_LAYER_BIT_TOGGLE(bits)           ACTION_LAYER_BIT_R(bits)
313 #define ACTION_LAYER_BIT_P(bits)                ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_PRESS)
314 #define ACTION_LAYER_BIT_R(bits)                ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_RELEASE)
315 #define ACTION_LAYER_BIT_B(bits)                ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_BOTH)
316 #define ACTION_LAYER_BIT_TAP_TOGGLE(bits)       ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE)
317 #define ACTION_LAYER_BIT_TAP_KEY(bits, key)     ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
318 /*
319  * Layer Stack
320  */
321 /* momentary */
322 #define ACTION_LAYER_STACK(layer)               ACTION_LAYER_STACK_MOMENTARY(layer)
323 #define ACTION_LAYER_STACK_MOMENTARY(layer)     ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_MOMENTARY)
324 #define ACTION_LAYER_STACK_TOGGLE(layer)        ACTION_LAYER_STACK_R(layer)
325 #define ACTION_LAYER_STACK_P(layer)             ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_ON_PRESS)
326 #define ACTION_LAYER_STACK_R(layer)             ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_ON_RELEASE)
327 #define ACTION_LAYER_STACK_B(layer)             ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_ON_BOTH)
328 #define ACTION_LAYER_STACK_TAP_TOGGLE(layer)    ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_TAP_TOGGLE)
329 #define ACTION_LAYER_STACK_TAP_KEY(layer, key)  ACTION(ACT_LAYER_STACK, (layer)<<8 | (key))
330
331
332 /*
333  * HID Usage
334  */
335 enum usage_pages {
336     PAGE_SYSTEM,
337     PAGE_CONSUMER
338 };
339 #define ACTION_USAGE_SYSTEM(id)         ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
340 #define ACTION_USAGE_CONSUMER(id)       ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
341
342 /* Mousekey */
343 #define ACTION_MOUSEKEY(key)            ACTION(ACT_MOUSEKEY, key)
344
345 /* Macro */
346 #define ACTION_MACRO(opt, id)           ACTION(ACT_FUNCTION, (opt)<<8 | (addr))
347
348 /* Command */
349 #define ACTION_COMMAND(opt, id)         ACTION(ACT_COMMAND,  (opt)<<8 | (addr))
350
351 /* Function */
352 enum function_opts {
353     FUNC_TAP        = 0x8,      /* indciates function is tappable */
354 };
355 #define ACTION_FUNCTION(id, opt)        ACTION(ACT_FUNCTION, (opt)<<8 | id)
356 #define ACTION_FUNCTION_TAP(id)         ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id)
357
358 #endif  /* ACTION_H */