]> git.gir.st - tmk_keyboard.git/blob - common/action.h
Rewrite layer action with layer_switch
[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
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 /* Execute action per keyevent */
80 void action_exec(keyevent_t event);
81
82 /* action for key */
83 action_t action_for_key(uint8_t layer, key_t key);
84
85 /* user defined special function */
86 void action_function(keyrecord_t *record, uint8_t id, uint8_t opt);
87
88 /*
89 * Utilities for actions.
90 */
91 void register_code(uint8_t code);
92 void unregister_code(uint8_t code);
93 void add_mods(uint8_t mods);
94 void del_mods(uint8_t mods);
95 void set_mods(uint8_t mods);
96 void clear_keyboard(void);
97 void clear_keyboard_but_mods(void);
98 bool sending_anykey(void);
99 void layer_switch(uint8_t new_layer);
100 bool is_tap_key(key_t key);
101 bool waiting_buffer_has_anykey_pressed(void);
102
103
104
105 /*
106 * Action codes
107 * ============
108 * 16bit code: action_kind(4bit) + action_parameter(12bit)
109 *
110 * Keyboard Keys
111 * -------------
112 * ACT_LMODS(0000):
113 * 0000|0000|000000|00 No action
114 * 0000|0000|000000|01 Transparent
115 * 0000|0000| keycode Key
116 * 0000|mods|000000|00 Left mods
117 * 0000|mods| keycode Key & Left mods
118 *
119 * ACT_RMODS(0001):
120 * 0001|0000|000000|00 No action(not used)
121 * 0001|0000|000000|01 Transparent(not used)
122 * 0001|0000| keycode Key(no used)
123 * 0001|mods|000000|00 Right mods
124 * 0001|mods| keycode Key & Right mods
125 *
126 * ACT_LMODS_TAP(0010):
127 * 0010|mods|000000|00 Left mods OneShot
128 * 0010|mods|000000|01 (reserved)
129 * 0010|mods|000000|10 (reserved)
130 * 0010|mods|000000|11 (reserved)
131 * 0010|mods| keycode Left mods + tap Key
132 *
133 * ACT_RMODS_TAP(0011):
134 * 0011|mods|000000|00 Right mods OneShot
135 * 0011|mods|000000|01 (reserved)
136 * 0011|mods|000000|10 (reserved)
137 * 0011|mods|000000|11 (reserved)
138 * 0011|mods| keycode Right mods + tap Key
139 *
140 *
141 * Other HID Usage
142 * ---------------
143 * This action handles other usages than keyboard.
144 * ACT_USAGE(0100):
145 * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
146 * 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
147 * 0100|10| usage(10) (reserved)
148 * 0100|11| usage(10) (reserved)
149 *
150 *
151 * Mouse Keys
152 * ----------
153 * NOTE: can be combined with 'Other HID Usage'? to save action kind id.
154 * ACT_MOUSEKEY(0110):
155 * 0101|XXXX| keycode Mouse key
156 *
157 *
158 * Layer Actions
159 * -------------
160 * ACT_LAYER_SET(1000): Set layer
161 * 1000|LLLL|0000 0000 set current layer on press and return to default on release(momentary)
162 * 1000|LLLL|0000 0001 set current layer on press
163 * 1000|LLLL|0000 0010 set current layer on release
164 * 1000|LLLL|0000 0011 set current layer on both
165 * 1000|LLLL| keycode set current layer on hold and send key on tap
166 * 1000|LLLL|1111 0000 set current layer on hold and toggle on several taps
167 * 1000|DDDD|1111 1111 set default layer on press
168 * L: 0 means default layer
169 *
170 * ACT_LAYER_BIT(1001): Bit-op layer
171 * 1001|BBBB|0000 0000 bit-on current layer on press and bit-off on release(momentary)
172 * 1001|BBBB|0000 0001 bit-xor current layer on press
173 * 1001|BBBB|0000 0010 bit-xor current layer on release
174 * 1001|BBBB|0000 0011 bit-xor current layer on both
175 * 1001|BBBB| keycode bit-xor current layer on hold and send key on tap
176 * 1001|BBBB|1111 0000 bit-xor current layer on hold and toggle on several taps
177 * 1001|BBBB|1111 1111 bit-xor default layer on both
178 *
179 * ACT_LAYER_SWITCH: Switch
180 * 1011|LLLL|0000 0000 On on press and Off on release(momentary)
181 * 1011|LLLL|0000 0001 Invert on press
182 * 1011|LLLL|0000 0010 Invert on release
183 * 1011|LLLL|0000 0011 Invert on both
184 * 1011|LLLL| keycode Invert on hold and send key on tap
185 * 1011|LLLL|1111 0000 Invert on hold and toggle on several taps
186 * 1011|LLLL|1111 1111 (not used)
187 *
188 *
189 * Extensions(11XX)
190 * ----------------
191 * NOTE: NOT FIXED
192 *
193 * ACT_MACRO(1100):
194 * 1100|opt | id(8) Macro play?
195 * 1100|1111| id(8) Macro record?
196 *
197 * ACT_COMMAND(1110):
198 * 1110|opt | id(8) Built-in Command exec
199 *
200 * ACT_FUNCTION(1111):
201 * 1111| address(12) Function?
202 * 1111|opt | id(8) Function?
203 *
204 */
205 enum action_kind_id {
206 ACT_LMODS = 0b0000,
207 ACT_RMODS = 0b0001,
208 ACT_LMODS_TAP = 0b0010,
209 ACT_RMODS_TAP = 0b0011,
210
211 ACT_USAGE = 0b0100,
212 ACT_MOUSEKEY = 0b0101,
213
214 ACT_LAYER_SET = 0b1000,
215 ACT_LAYER_BIT = 0b1001,
216 ACT_LAYER_SWITCH = 0b1011,
217
218 ACT_MACRO = 0b1100,
219 ACT_COMMAND = 0b1110,
220 ACT_FUNCTION = 0b1111
221 };
222
223
224 /* action utility */
225 #define ACTION_NO 0
226 #define ACTION_TRANSPARENT 1
227 #define ACTION(kind, param) ((kind)<<12 | (param))
228 #define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
229
230 /*
231 * Key
232 */
233 #define ACTION_KEY(key) ACTION(ACT_LMODS, key)
234 /* Mods & key */
235 #define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
236 #define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
237 #define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
238 #define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
239 #define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
240 #define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
241 #define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
242 #define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
243 /* Tap key */
244 enum mods_codes {
245 MODS_ONESHOT = 0x00,
246 };
247 #define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
248 #define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
249 #define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
250 #define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
251 #define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
252 #define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
253 #define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
254 #define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
255
256
257 /*
258 * Layer switching
259 */
260 enum layer_codes {
261 LAYER_MOMENTARY = 0,
262 LAYER_ON_PRESS = 1,
263 LAYER_ON_RELEASE = 2,
264 LAYER_ON_BOTH =3,
265 LAYER_TAP_TOGGLE = 0xF0,
266 LAYER_SET_DEFAULT_ON_PRESS = 0xFD,
267 LAYER_SET_DEFAULT_ON_RELEASE = 0xFE,
268 LAYER_SET_DEFAULT_ON_BOTH = 0xFF
269 };
270 /*
271 * Default layer
272 */
273 /* set default layer */
274 #define ACTION_LAYER_SET_DEFAULT(layer) ACTION_LAYER_SET_DEFAULT_R(layer)
275 #define ACTION_LAYER_SET_DEFAULT_P(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_SET_DEFAULT_ON_PRESS)
276 #define ACTION_LAYER_SET_DEFAULT_R(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_SET_DEFAULT_ON_RELEASE)
277 #define ACTION_LAYER_SET_DEFAULT_B(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_SET_DEFAULT_ON_BOTH)
278 /* bit-xor default layer */
279 #define ACTION_LAYER_BIT_DEFAULT(bits) ACTION_LAYER_BIT_DEFAULT_R(bits)
280 #define ACTION_LAYER_BIT_DEFAULT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_SET_DEFAULT_ON_PRESS)
281 #define ACTION_LAYER_BIT_DEFAULT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_SET_DEFAULT_ON_RELEASE)
282 #define ACTION_LAYER_BIT_DEFAULT_B(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_SET_DEFAULT_ON_BOTH)
283 /*
284 * Current layer: Return to default layer
285 */
286 #define ACTION_LAYER_DEFAULT ACTION_LAYER_DEFAULT_R
287 #define ACTION_LAYER_DEFAULT_P ACTION_LAYER_SET_P(0)
288 #define ACTION_LAYER_DEFAULT_R ACTION_LAYER_SET_R(0)
289 #define ACTION_LAYER_DEFAULT_B ACTION_LAYER_SET_B(0)
290 /*
291 * Current layer: Set
292 */
293 #define ACTION_LAYER_SET(layer) ACTION_LAYER_SET_P(layer)
294 #define ACTION_LAYER_SET_MOMENTARY(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_MOMENTARY)
295 #define ACTION_LAYER_SET_TOGGLE(layer) ACTION_LAYER_SET_R(layer)
296 #define ACTION_LAYER_SET_P(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_ON_PRESS)
297 #define ACTION_LAYER_SET_R(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_ON_RELEASE)
298 #define ACTION_LAYER_SET_B(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_ON_BOTH)
299 #define ACTION_LAYER_SET_TAP_TOGGLE(layer) ACTION(ACT_LAYER_SET, (layer)<<8 | LAYER_TAP_TOGGLE)
300 #define ACTION_LAYER_SET_TAP_KEY(layer, key) ACTION(ACT_LAYER_SET, (layer)<<8 | (key))
301 /*
302 * Current layer: Bit-op
303 */
304 #define ACTION_LAYER_BIT(bits) ACTION_LAYER_BIT_MOMENTARY(bits)
305 #define ACTION_LAYER_BIT_MOMENTARY(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_MOMENTARY)
306 #define ACTION_LAYER_BIT_TOGGLE(bits) ACTION_LAYER_BIT_R(bits)
307 #define ACTION_LAYER_BIT_P(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_PRESS)
308 #define ACTION_LAYER_BIT_R(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_RELEASE)
309 #define ACTION_LAYER_BIT_B(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_ON_BOTH)
310 #define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE)
311 #define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
312 /*
313 * Layer SWITCH
314 */
315 /* momentary */
316 #define ACTION_LAYER_SWITCH(layer) ACTION_LAYER_SWITCH_MOMENTARY(layer)
317 #define ACTION_LAYER_SWITCH_MOMENTARY(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_MOMENTARY)
318 #define ACTION_LAYER_SWITCH_TOGGLE(layer) ACTION_LAYER_SWITCH_R(layer)
319 #define ACTION_LAYER_SWITCH_P(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_PRESS)
320 #define ACTION_LAYER_SWITCH_R(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_RELEASE)
321 #define ACTION_LAYER_SWITCH_B(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_BOTH)
322 #define ACTION_LAYER_SWITCH_TAP_TOGGLE(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_TAP_TOGGLE)
323 #define ACTION_LAYER_SWITCH_TAP_KEY(layer, key) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | (key))
324
325
326 /*
327 * HID Usage
328 */
329 enum usage_pages {
330 PAGE_SYSTEM,
331 PAGE_CONSUMER
332 };
333 #define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
334 #define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
335
336 /* Mousekey */
337 #define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
338
339 /* Macro */
340 #define ACTION_MACRO(opt, id) ACTION(ACT_FUNCTION, (opt)<<8 | (addr))
341
342 /* Command */
343 #define ACTION_COMMAND(opt, id) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
344
345 /* Function */
346 enum function_opts {
347 FUNC_TAP = 0x8, /* indciates function is tappable */
348 };
349 #define ACTION_FUNCTION(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | id)
350 #define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | id)
351
352 #endif /* ACTION_H */
Imprint / Impressum