aboutsummaryrefslogtreecommitdiff
path: root/common/action.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/action.h')
-rw-r--r--common/action.h331
1 files changed, 331 insertions, 0 deletions
diff --git a/common/action.h b/common/action.h
new file mode 100644
index 000000000..9b559cb18
--- /dev/null
+++ b/common/action.h
@@ -0,0 +1,331 @@
1/*
2Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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/* Execute action per keyevent */
25void action_exec(keyevent_t event);
26
27
28/* Struct to record event and tap count */
29typedef struct {
30 keyevent_t event;
31 uint8_t tap_count;
32} keyrecord_t;
33
34/* Action struct.
35 *
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.
38 *
39 * NOTE: not portable across compiler/endianness?
40 * Byte order and bit order of 0x1234:
41 * Big endian: 15 ... 8 7 ... 210
42 * | 0x12 | 0x34 |
43 * 0001 0010 0011 0100
44 * Little endian: 012 ... 7 8 ... 15
45 * | 0x34 | 0x12 |
46 * 0010 1100 0100 1000
47 */
48typedef union {
49 uint16_t code;
50 struct action_kind {
51 uint16_t param :12;
52 uint8_t id :4;
53 } kind;
54 struct action_key {
55 uint8_t code :8;
56 uint8_t mods :4;
57 uint8_t kind :4;
58 } key;
59 struct action_layer {
60 uint8_t code :8;
61 uint8_t val :4;
62 uint8_t kind :4;
63 } layer;
64 struct action_usage {
65 uint16_t code :10;
66 uint8_t page :2;
67 uint8_t kind :4;
68 } usage;
69 struct action_command {
70 uint8_t id :8;
71 uint8_t opt :4;
72 uint8_t kind :4;
73 } command;
74 struct action_function {
75 uint8_t id :8;
76 uint8_t opt :4;
77 uint8_t kind :4;
78 } func;
79} action_t;
80
81
82/*
83 * Utilities for actions.
84 */
85void register_code(uint8_t code);
86void unregister_code(uint8_t code);
87void add_mods(uint8_t mods);
88void del_mods(uint8_t mods);
89void set_mods(uint8_t mods);
90void clear_keyboard(void);
91void clear_keyboard_but_mods(void);
92bool sending_anykey(void);
93void layer_switch(uint8_t new_layer);
94bool is_tap_key(key_t key);
95bool waiting_buffer_has_anykey_pressed(void);
96
97
98
99
100/*
101 * Action codes
102 * ============
103 * 16bit code: action_kind(4bit) + action_parameter(12bit)
104 *
105Keyboard Keys
106-------------
107ACT_LMODS(0000):
1080000|0000|000000|00 No action
1090000|0000| keycode Key
1100000|mods|000000|00 Left mods
1110000|mods| keycode Key & Left mods
112
113ACT_RMODS(0001):
1140001|0000|000000|00 No action
1150001|0000| keycode Key(no used)
1160001|mods|000000|00 Right mods
1170001|mods| keycode Key & Right mods
118
119ACT_LMODS_TAP(0010):
1200010|mods|000000|00 Left mods OneShot
1210010|mods|000000|01 (reserved)
1220010|mods|000000|10 (reserved)
1230010|mods|000000|11 (reserved)
1240010|mods| keycode Left mods + tap Key
125
126ACT_RMODS_TAP(0011):
1270011|mods|000000|00 Right mods OneShot
1280011|mods|000000|01 (reserved)
1290011|mods|000000|10 (reserved)
1300011|mods|000000|11 (reserved)
1310011|mods| keycode Right mods + tap Key
132
133
134Other HID Usage
135---------------
136This action handles other usages than keyboard.
137ACT_USAGE(0100):
1380100|00| usage(10) System control(0x80) - General Desktop page(0x01)
1390100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
1400100|10| usage(10) (reserved)
1410100|11| usage(10) (reserved)
142
143
144Mouse Keys
145----------
146TODO: can be combined with 'Other HID Usage'? to save action kind id.
147ACT_MOUSEKEY(0110):
1480101|XXXX| keycode Mouse key
149
150
151Layer Actions
152-------------
153ACT_LAYER(1000): Set layer
154ACT_LAYER_BIT(1001): Bit-op layer
155
1561000|LLLL|0000 0000 set L to layer on press and set default on release(momentary)
1571000|LLLL|0000 0001 set L to layer on press
1581000|LLLL|0000 0010 set L to layer on release
1591000|----|0000 0011 set default to layer on both(return to default layer)
1601000|LLLL|xxxx xxxx set L to layer while hold and send key on tap
1611000|LLLL|1111 0000 set L to layer while hold and toggle on several taps
1621000|LLLL|1111 1111 set L to default and layer(on press)
163
1641001|BBBB|0000 0000 (not used)
1651001|BBBB|0000 0001 bit-xor layer with B on press
1661001|BBBB|0000 0010 bit-xor layer with B on release
1671001|BBBB|0000 0011 bit-xor layer with B on both(momentary)
1681001|BBBB|xxxx xxxx bit-xor layer with B while hold and send key on tap
1691001|BBBB|1111 0000 bit-xor layer with B while hold and toggle on several taps
1701001|BBBB|1111 1111 bit-xor default with B and set layer(on press)
171
172
173
174Extensions(11XX)
175----------------
176NOTE: NOT FIXED
177
178ACT_MACRO(1100):
1791100|opt | id(8) Macro play?
1801100|1111| id(8) Macro record?
181
182ACT_COMMAND(1110):
1831110|opt | id(8) Built-in Command exec
184
185ACT_FUNCTION(1111):
1861111| address(12) Function?
1871111|opt | id(8) Function?
188
189 */
190enum action_kind_id {
191 ACT_LMODS = 0b0000,
192 ACT_RMODS = 0b0001,
193 ACT_LMODS_TAP = 0b0010,
194 ACT_RMODS_TAP = 0b0011,
195
196 ACT_USAGE = 0b0100,
197 ACT_MOUSEKEY = 0b0101,
198
199 ACT_LAYER = 0b1000,
200 ACT_LAYER_BIT = 0b1001,
201
202 ACT_MACRO = 0b1100,
203 ACT_COMMAND = 0b1110,
204 ACT_FUNCTION = 0b1111
205};
206
207
208/* action utility */
209#define ACTION_NO 0
210#define ACTION(kind, param) ((kind)<<12 | (param))
211#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
212
213/* Key */
214#define ACTION_KEY(key) ACTION(ACT_LMODS, key)
215/* Mods & key */
216#define ACTION_LMODS(mods) ACTION(ACT_LMODS, (mods)<<8 | 0x00)
217#define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, (mods)<<8 | (key))
218#define ACTION_RMODS(mods) ACTION(ACT_RMODS, (mods)<<8 | 0x00)
219#define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, (mods)<<8 | (key))
220/* Mod & key */
221#define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
222#define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
223#define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
224#define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
225
226/* Mods + Tap key */
227enum mods_codes {
228 MODS_ONESHOT = 0x00,
229};
230#define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
231#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
232#define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
233#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
234/* Mod + Tap key */
235#define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
236#define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
237#define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
238#define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
239
240
241/*
242 * Switch layer
243 */
244enum layer_codes {
245 LAYER_MOMENTARY = 0,
246 LAYER_ON_PRESS = 1,
247 LAYER_ON_RELEASE = 2,
248 LAYER_DEFAULT =3,
249 LAYER_TAP_TOGGLE = 0xF0,
250 LAYER_CHANGE_DEFAULT = 0xFF
251};
252enum layer_vals_default {
253 DEFAULT_ON_PRESS = 1,
254 DEFAULT_ON_RELEASE = 2,
255 DEFAULT_ON_BOTH = 3,
256};
257
258/*
259 * return to default layer
260 */
261#define ACTION_LAYER_DEFAULT ACTION_LAYER_DEFAULT_R
262/* set default layer on press */
263#define ACTION_LAYER_DEFAULT_P ACTION(ACT_LAYER, DEFAULT_ON_PRESS<<8 | LAYER_DEFAULT)
264/* set default layer on release */
265#define ACTION_LAYER_DEFAULT_R ACTION(ACT_LAYER, DEFAULT_ON_RELEASE<<8 | LAYER_DEFAULT)
266/* change default layer and set layer */
267
268/*
269 * Set layer
270 */
271/* set layer on press and set default on release */
272#define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_MOMENTARY(layer)
273#define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_MOMENTARY)
274/* set layer on press and none on release */
275#define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer)
276/* set layer while hold and send key on tap */
277#define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key))
278/* set layer on press */
279#define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_PRESS)
280/* set layer on release */
281#define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_ON_RELEASE)
282/* set layer on hold and toggle on several taps */
283#define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_TAP_TOGGLE)
284/* set default layer on both press and release */
285#define ACTION_LAYER_SET_DEFAULT(layer) ACTION(ACT_LAYER, (layer)<<8 | LAYER_CHANGE_DEFAULT)
286
287/*
288 * Bit-op layer
289 */
290/* bit-xor on both press and release */
291#define ACTION_LAYER_BIT(bits) ACTION_LAYER_BIT_MOMENTARY(bits)
292#define ACTION_LAYER_BIT_MOMENTARY(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_MOMENTARY)
293/* bit-xor on press */
294#define ACTION_LAYER_BIT_TOGGLE(bits) ACTION_LAYER_BIT_R(bits)
295/* bit-xor while hold and send key on tap */
296#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
297/* bit-xor on press */
298#define ACTION_LAYER_BIT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_PRESS)
299/* bit-xor on release */
300#define ACTION_LAYER_BIT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_RELEASE)
301/* bit-xor while hold and toggle on several taps */
302#define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE)
303/* bit-xor default layer and set layer */
304#define ACTION_LAYER_BIT_DEFAULT(bits) ACTION(ACT_LAYER, (bits)<<8 | LAYER_CHANGE_DEFAULT)
305
306
307/* HID Usage */
308enum usage_pages {
309 PAGE_SYSTEM,
310 PAGE_CONSUMER
311};
312#define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
313#define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
314
315/* Mousekey */
316#define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
317
318/* Macro */
319#define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr))
320
321/* Command */
322#define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
323
324/* Function */
325enum function_opts {
326 FUNC_TAP = 0x8, /* indciates function is tappable */
327};
328#define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id)
329#define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id)
330
331#endif /* ACTION_H */