aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/action.c378
-rw-r--r--common/action.h319
-rw-r--r--common/action_code.h289
-rw-r--r--common/action_layer.c135
-rw-r--r--common/action_layer.h77
-rw-r--r--common/action_tapping.h3
-rw-r--r--common/command.c6
-rw-r--r--common/keymap.c2
-rw-r--r--common/layer_switch.c209
-rw-r--r--common/layer_switch.h110
-rw-r--r--common/util.c19
-rw-r--r--common/util.h3
12 files changed, 608 insertions, 942 deletions
diff --git a/common/action.c b/common/action.c
index 07a3a64d6..065188744 100644
--- a/common/action.c
+++ b/common/action.c
@@ -21,7 +21,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
21#include "command.h" 21#include "command.h"
22#include "debug.h" 22#include "debug.h"
23#include "led.h" 23#include "led.h"
24#include "layer_switch.h" 24#include "action_layer.h"
25#include "action_tapping.h" 25#include "action_tapping.h"
26#include "action_oneshot.h" 26#include "action_oneshot.h"
27#include "action_macro.h" 27#include "action_macro.h"
@@ -50,15 +50,19 @@ void action_exec(keyevent_t event)
50void process_action(keyrecord_t *record) 50void process_action(keyrecord_t *record)
51{ 51{
52 keyevent_t event = record->event; 52 keyevent_t event = record->event;
53#ifndef NO_ACTION_TAPPING
53 uint8_t tap_count = record->tap.count; 54 uint8_t tap_count = record->tap.count;
55#endif
54 56
55 if (IS_NOEVENT(event)) { return; } 57 if (IS_NOEVENT(event)) { return; }
56 58
57 action_t action = layer_switch_get_action(event.key); 59 action_t action = layer_switch_get_action(event.key);
58 debug("ACTION: "); debug_action(action); 60 debug("ACTION: "); debug_action(action);
59 debug(" overlays: "); overlay_debug(); 61#ifndef NO_ACTION_LAYER
60 debug(" keymaps: "); keymap_debug(); 62 debug(" layer_state: "); layer_debug();
61 debug(" default_layer: "); debug_dec(default_layer); debug("\n"); 63 debug(" default_layer_state: "); default_layer_debug();
64#endif
65 debug("\n");
62 66
63 switch (action.kind.id) { 67 switch (action.kind.id) {
64 /* Key and Mods */ 68 /* Key and Mods */
@@ -68,22 +72,17 @@ void process_action(keyrecord_t *record)
68 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods : 72 uint8_t mods = (action.kind.id == ACT_LMODS) ? action.key.mods :
69 action.key.mods<<4; 73 action.key.mods<<4;
70 if (event.pressed) { 74 if (event.pressed) {
71 uint8_t tmp_mods = host_get_mods();
72 if (mods) { 75 if (mods) {
73 host_add_mods(mods); 76 host_add_mods(mods);
74 host_send_keyboard_report(); 77 host_send_keyboard_report();
75 } 78 }
76 register_code(action.key.code); 79 register_code(action.key.code);
77 if (mods && action.key.code) {
78 host_set_mods(tmp_mods);
79 host_send_keyboard_report();
80 }
81 } else { 80 } else {
82 if (mods && !action.key.code) { 81 unregister_code(action.key.code);
82 if (mods) {
83 host_del_mods(mods); 83 host_del_mods(mods);
84 host_send_keyboard_report(); 84 host_send_keyboard_report();
85 } 85 }
86 unregister_code(action.key.code);
87 } 86 }
88 } 87 }
89 break; 88 break;
@@ -93,7 +92,7 @@ void process_action(keyrecord_t *record)
93 { 92 {
94 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : 93 uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods :
95 action.key.mods<<4; 94 action.key.mods<<4;
96 switch (action.layer.code) { 95 switch (action.layer_tap.code) {
97 #ifndef NO_ACTION_ONESHOT 96 #ifndef NO_ACTION_ONESHOT
98 case 0x00: 97 case 0x00:
99 // Oneshot modifier 98 // Oneshot modifier
@@ -199,323 +198,88 @@ void process_action(keyrecord_t *record)
199 } 198 }
200 break; 199 break;
201#endif 200#endif
202#ifndef NO_ACTION_KEYMAP 201#ifndef NO_ACTION_LAYER
203 case ACT_KEYMAP: 202 case ACT_LAYER:
204 switch (action.layer.code) { 203 if (action.layer_bitop.on == 0) {
205 /* Keymap clear */ 204 /* Default Layer Bitwise Operation */
206 case OP_RESET: 205 if (!event.pressed) {
207 switch (action.layer.val & 0x03) { 206 uint8_t shift = action.layer_bitop.part*4;
208 case 0: 207 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
209 // NOTE: reserved 208 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
210 overlay_clear(); 209 switch (action.layer_bitop.op) {
211 keymap_clear(); 210 case OP_BIT_AND: default_layer_and(bits | mask); break;
212 break; 211 case OP_BIT_OR: default_layer_or(bits | mask); break;
213 case ON_PRESS: 212 case OP_BIT_XOR: default_layer_xor(bits | mask); break;
214 if (event.pressed) { 213 case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
215 overlay_clear();
216 keymap_clear();
217 }
218 break;
219 case ON_RELEASE:
220 if (!event.pressed) {
221 overlay_clear();
222 keymap_clear();
223 }
224 break;
225 case ON_BOTH:
226 overlay_clear();
227 keymap_clear();
228 break;
229 /* NOTE: 4-7 rserved */
230 } 214 }
231 break; 215 }
232 /* Keymap Reset default layer */ 216 } else {
233 case (OP_RESET | ON_PRESS): 217 /* Layer Bitwise Operation */
234 if (event.pressed) { 218 if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
235 default_layer_set(action.layer.val); 219 (action.layer_bitop.on & ON_RELEASE)) {
236 } 220 uint8_t shift = action.layer_bitop.part*4;
237 break; 221 uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
238 case (OP_RESET | ON_RELEASE): 222 uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
239 if (!event.pressed) { 223 switch (action.layer_bitop.op) {
240 default_layer_set(action.layer.val); 224 case OP_BIT_AND: layer_and(bits | mask); break;
225 case OP_BIT_OR: layer_or(bits | mask); break;
226 case OP_BIT_XOR: layer_xor(bits | mask); break;
227 case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
241 } 228 }
242 break; 229 }
243 case (OP_RESET | ON_BOTH): 230 }
244 default_layer_set(action.layer.val); 231 break;
245 break; 232 #ifndef NO_ACTION_TAPPING
246 233 case ACT_LAYER_TAP:
247 /* Keymap Bit invert */ 234 case ACT_LAYER_TAP1:
248 case OP_INV: 235 switch (action.layer_tap.code) {
249 /* with tap toggle */ 236 case OP_TAP_TOGGLE:
237 /* tap toggle */
250 if (event.pressed) { 238 if (event.pressed) {
251 if (tap_count < TAPPING_TOGGLE) { 239 if (tap_count < TAPPING_TOGGLE) {
252 debug("KEYMAP_INV: tap toggle(press).\n"); 240 layer_invert(action.layer_tap.val);
253 keymap_invert(action.layer.val);
254 } 241 }
255 } else { 242 } else {
256 if (tap_count <= TAPPING_TOGGLE) { 243 if (tap_count <= TAPPING_TOGGLE) {
257 debug("KEYMAP_INV: tap toggle(release).\n"); 244 layer_invert(action.layer_tap.val);
258 keymap_invert(action.layer.val);
259 } 245 }
260 } 246 }
261 break; 247 break;
262 case (OP_INV | ON_PRESS): 248 case OP_ON_OFF:
263 if (event.pressed) { 249 event.pressed ? layer_on(action.layer_tap.val) :
264 keymap_invert(action.layer.val); 250 layer_off(action.layer_tap.val);
265 }
266 break;
267 case (OP_INV | ON_RELEASE):
268 if (!event.pressed) {
269 keymap_invert(action.layer.val);
270 }
271 break; 251 break;
272 case (OP_INV | ON_BOTH): 252 case OP_OFF_ON:
273 keymap_invert(action.layer.val); 253 event.pressed ? layer_off(action.layer_tap.val) :
254 layer_on(action.layer_tap.val);
274 break; 255 break;
275 256 case OP_SET_CLEAR:
276 /* Keymap Bit on */ 257 event.pressed ? layer_move(action.layer_tap.val) :
277 case OP_ON: 258 layer_clear();
278 if (event.pressed) {
279 keymap_on(action.layer.val);
280 } else {
281 keymap_off(action.layer.val);
282 }
283 break;
284 case (OP_ON | ON_PRESS):
285 if (event.pressed) {
286 keymap_on(action.layer.val);
287 }
288 break;
289 case (OP_ON | ON_RELEASE):
290 if (!event.pressed) {
291 keymap_on(action.layer.val);
292 }
293 break; 259 break;
294 case (OP_ON | ON_BOTH):
295 keymap_on(action.layer.val);
296 break;
297
298 /* Keymap Bit off */
299 case OP_OFF:
300 if (event.pressed) {
301 keymap_off(action.layer.val);
302 } else {
303 keymap_on(action.layer.val);
304 }
305 break;
306 case (OP_OFF | ON_PRESS):
307 if (event.pressed) {
308 keymap_off(action.layer.val);
309 }
310 break;
311 case (OP_OFF | ON_RELEASE):
312 if (!event.pressed) {
313 keymap_off(action.layer.val);
314 }
315 break;
316 case (OP_OFF | ON_BOTH):
317 keymap_off(action.layer.val);
318 break;
319
320 /* Keymap Bit set */
321 case OP_SET:
322 if (event.pressed) {
323 keymap_set(action.layer.val);
324 } else {
325 keymap_clear();
326 }
327 break;
328 case (OP_SET | ON_PRESS):
329 if (event.pressed) {
330 keymap_set(action.layer.val);
331 }
332 break;
333 case (OP_SET | ON_RELEASE):
334 if (!event.pressed) {
335 keymap_set(action.layer.val);
336 }
337 break;
338 case (OP_SET | ON_BOTH):
339 keymap_set(action.layer.val);
340 break;
341
342 /* Keymap Bit invert with tap key */
343 default: 260 default:
261 /* tap key */
344 if (event.pressed) { 262 if (event.pressed) {
345 if (tap_count > 0) { 263 if (tap_count > 0) {
346 debug("KEYMAP_TAP_KEY: Tap: register_code\n"); 264 debug("KEYMAP_TAP_KEY: Tap: register_code\n");
347 register_code(action.layer.code); 265 register_code(action.layer_tap.code);
348 } else { 266 } else {
349 debug("KEYMAP_TAP_KEY: No tap: On on press\n"); 267 debug("KEYMAP_TAP_KEY: No tap: On on press\n");
350 keymap_on(action.layer.val); 268 layer_on(action.layer_tap.val);
351 } 269 }
352 } else { 270 } else {
353 if (tap_count > 0) { 271 if (tap_count > 0) {
354 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n"); 272 debug("KEYMAP_TAP_KEY: Tap: unregister_code\n");
355 unregister_code(action.layer.code); 273 unregister_code(action.layer_tap.code);
356 } else { 274 } else {
357 debug("KEYMAP_TAP_KEY: No tap: Off on release\n"); 275 debug("KEYMAP_TAP_KEY: No tap: Off on release\n");
358 keymap_off(action.layer.val); 276 layer_off(action.layer_tap.val);
359 }
360 }
361 break;
362 }
363 break;
364#endif
365#ifndef NO_ACTION_OVERLAY
366 case ACT_OVERLAY:
367 switch (action.layer.code) {
368 // Overlay Invert bit4
369 case OP_INV4 | 0:
370 if (action.layer.val == 0) {
371 // NOTE: reserved for future use
372 overlay_clear();
373 } else {
374 overlay_set(overlay_stat ^ action.layer.val);
375 }
376 break;
377 case OP_INV4 | 1:
378 if (action.layer.val == 0) {
379 // on pressed
380 if (event.pressed) overlay_clear();
381 } else {
382 overlay_set(overlay_stat ^ action.layer.val<<4);
383 }
384 break;
385 case OP_INV4 | 2:
386 if (action.layer.val == 0) {
387 // on released
388 if (!event.pressed) overlay_clear();
389 } else {
390 overlay_set(overlay_stat ^ action.layer.val<<8);
391 }
392 break;
393 case OP_INV4 | 3:
394 if (action.layer.val == 0) {
395 // on both
396 overlay_clear();
397 } else {
398 overlay_set(overlay_stat ^ action.layer.val<<12);
399 }
400 break;
401
402 /* Overlay Bit invert */
403 case OP_INV:
404 /* with tap toggle */
405 if (event.pressed) {
406 if (tap_count < TAPPING_TOGGLE) {
407 debug("OVERLAY_INV: tap toggle(press).\n");
408 overlay_invert(action.layer.val);
409 }
410 } else {
411 if (tap_count <= TAPPING_TOGGLE) {
412 debug("OVERLAY_INV: tap toggle(release).\n");
413 overlay_invert(action.layer.val);
414 }
415 }
416 break;
417 case (OP_INV | ON_PRESS):
418 if (event.pressed) {
419 overlay_invert(action.layer.val);
420 }
421 break;
422 case (OP_INV | ON_RELEASE):
423 if (!event.pressed) {
424 overlay_invert(action.layer.val);
425 }
426 break;
427 case (OP_INV | ON_BOTH):
428 overlay_invert(action.layer.val);
429 break;
430
431 /* Overlay Bit on */
432 case OP_ON:
433 if (event.pressed) {
434 overlay_on(action.layer.val);
435 } else {
436 overlay_off(action.layer.val);
437 }
438 break;
439 case (OP_ON | ON_PRESS):
440 if (event.pressed) {
441 overlay_on(action.layer.val);
442 }
443 break;
444 case (OP_ON | ON_RELEASE):
445 if (!event.pressed) {
446 overlay_on(action.layer.val);
447 }
448 break;
449 case (OP_ON | ON_BOTH):
450 overlay_on(action.layer.val);
451 break;
452
453 /* Overlay Bit off */
454 case OP_OFF:
455 if (event.pressed) {
456 overlay_off(action.layer.val);
457 } else {
458 overlay_on(action.layer.val);
459 }
460 break;
461 case (OP_OFF | ON_PRESS):
462 if (event.pressed) {
463 overlay_off(action.layer.val);
464 }
465 break;
466 case (OP_OFF | ON_RELEASE):
467 if (!event.pressed) {
468 overlay_off(action.layer.val);
469 }
470 break;
471 case (OP_OFF | ON_BOTH):
472 overlay_off(action.layer.val);
473 break;
474
475 /* Overlay Bit set */
476 case OP_SET:
477 if (event.pressed) {
478 overlay_move(action.layer.val);
479 } else {
480 overlay_clear();
481 }
482 break;
483 case (OP_SET | ON_PRESS):
484 if (event.pressed) {
485 overlay_move(action.layer.val);
486 }
487 break;
488 case (OP_SET | ON_RELEASE):
489 if (!event.pressed) {
490 overlay_move(action.layer.val);
491 }
492 break;
493 case (OP_SET | ON_BOTH):
494 overlay_move(action.layer.val);
495 break;
496
497 /* Overlay Bit invert with tap key */
498 default:
499 if (event.pressed) {
500 if (tap_count > 0) {
501 debug("OVERLAY_TAP_KEY: Tap: register_code\n");
502 register_code(action.layer.code);
503 } else {
504 debug("OVERLAY_TAP_KEY: No tap: On on press\n");
505 overlay_on(action.layer.val);
506 }
507 } else {
508 if (tap_count > 0) {
509 debug("OVERLAY_TAP_KEY: Tap: unregister_code\n");
510 unregister_code(action.layer.code);
511 } else {
512 debug("OVERLAY_TAP_KEY: No tap: Off on release\n");
513 overlay_off(action.layer.val);
514 } 277 }
515 } 278 }
516 break; 279 break;
517 } 280 }
518 break; 281 break;
282 #endif
519#endif 283#endif
520 /* Extentions */ 284 /* Extentions */
521#ifndef NO_ACTION_MACRO 285#ifndef NO_ACTION_MACRO
@@ -667,16 +431,9 @@ bool is_tap_key(key_t key)
667 switch (action.kind.id) { 431 switch (action.kind.id) {
668 case ACT_LMODS_TAP: 432 case ACT_LMODS_TAP:
669 case ACT_RMODS_TAP: 433 case ACT_RMODS_TAP:
434 case ACT_LAYER_TAP:
435 case ACT_LAYER_TAP1:
670 return true; 436 return true;
671 case ACT_KEYMAP:
672 case ACT_OVERLAY:
673 switch (action.layer.code) {
674 case 0x04 ... 0xEF: /* tap key */
675 case OP_INV:
676 return true;
677 default:
678 return false;
679 }
680 case ACT_MACRO: 437 case ACT_MACRO:
681 case ACT_FUNCTION: 438 case ACT_FUNCTION:
682 if (action.func.opt & FUNC_TAP) { return true; } 439 if (action.func.opt & FUNC_TAP) { return true; }
@@ -714,8 +471,9 @@ void debug_action(action_t action)
714 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break; 471 case ACT_RMODS_TAP: debug("ACT_RMODS_TAP"); break;
715 case ACT_USAGE: debug("ACT_USAGE"); break; 472 case ACT_USAGE: debug("ACT_USAGE"); break;
716 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; 473 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
717 case ACT_KEYMAP: debug("ACT_KEYMAP"); break; 474 case ACT_LAYER: debug("ACT_LAYER"); break;
718 case ACT_OVERLAY: debug("ACT_OVERLAY"); break; 475 case ACT_LAYER_TAP: debug("ACT_LAYER_TAP"); break;
476 case ACT_LAYER_TAP1: debug("ACT_LAYER_TAP1"); break;
719 case ACT_MACRO: debug("ACT_MACRO"); break; 477 case ACT_MACRO: debug("ACT_MACRO"); break;
720 case ACT_COMMAND: debug("ACT_COMMAND"); break; 478 case ACT_COMMAND: debug("ACT_COMMAND"); break;
721 case ACT_FUNCTION: debug("ACT_FUNCTION"); break; 479 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
diff --git a/common/action.h b/common/action.h
index a6cb45384..98c4ef81a 100644
--- a/common/action.h
+++ b/common/action.h
@@ -21,71 +21,27 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
21#include <stdbool.h> 21#include <stdbool.h>
22#include "keyboard.h" 22#include "keyboard.h"
23#include "keycode.h" 23#include "keycode.h"
24#include "action_code.h"
24#include "action_macro.h" 25#include "action_macro.h"
25 26
26 27
28/* tapping count and state */
29typedef struct {
30 bool interrupted :1;
31 bool reserved2 :1;
32 bool reserved1 :1;
33 bool reserved0 :1;
34 uint8_t count :4;
35} tap_t;
36
37/* Key event container for recording */
27typedef struct { 38typedef struct {
28 keyevent_t event; 39 keyevent_t event;
29#ifndef NO_ACTION_TAPPING 40#ifndef NO_ACTION_TAPPING
30 /* tapping count and state */ 41 tap_t tap;
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 42#endif
39} keyrecord_t; 43} keyrecord_t;
40 44
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 */
55typedef 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 45
90/* Execute action per keyevent */ 46/* Execute action per keyevent */
91void action_exec(keyevent_t event); 47void action_exec(keyevent_t event);
@@ -117,255 +73,4 @@ void debug_event(keyevent_t event);
117void debug_record(keyrecord_t record); 73void debug_record(keyrecord_t record);
118void debug_action(action_t action); 74void debug_action(action_t action);
119 75
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_KEYMAP:
174 * 1000|--xx|0000 0000 Clear keyamp and overlay
175 * 1000|LLLL|0000 00xx Reset default layer and clear keymap and overlay
176 * 1000|LLLL| keycode Invert with tap key
177 * 1000|LLLL|1111 0000 Invert with tap toggle
178 * 1000|LLLL|1111 00xx Invert[^= 1<<L]
179 * 1000|LLLL|1111 0100 On/Off
180 * 1000|LLLL|1111 01xx On[|= 1<<L]
181 * 1000|LLLL|1111 1000 Off/On
182 * 1000|LLLL|1111 10xx Off[&= ~(1<<L)]
183 * 1000|LLLL|1111 1100 Set/Clear
184 * 1000|LLLL|1111 11xx Set[= 1<<L]
185 * default layer: 0-15(4bit)
186 * xx: On {00:for special use, 01:press, 10:release, 11:both}
187 *
188 * ACT_OVERLAY:
189 * 1011|0000|0000 0000 Clear overlay
190 * 1011|LLLL|0000 00ss Invert 4-bit chunk [^= L<<(4*ss)]
191 * 1011|LLLL| keycode Invert with tap key
192 * 1011|LLLL|1111 0000 Invert with tap toggle
193 * 1011|LLLL|1111 00xx Invert[^= 1<<L]
194 * 1011|LLLL|1111 0100 On/Off(momentary)
195 * 1011|LLLL|1111 01xx On[|= 1<<L]
196 * 1011|LLLL|1111 1000 Off/On
197 * 1011|LLLL|1111 10xx Off[&= ~(1<<L)]
198 * 1011|LLLL|1111 1100 Set/Clear
199 * 1011|LLLL|1111 11xx Set[= 1<<L]
200 * overlays: 16-layer on/off status(16bit)
201 * xx: On {00:for special use, 01:press, 10:release, 11:both}
202 *
203 *
204 * Extensions(11XX)
205 * ----------------
206 * ACT_MACRO(1100):
207 * 1100|opt | id(8) Macro play?
208 * 1100|1111| id(8) Macro record?
209 *
210 * ACT_COMMAND(1110):
211 * 1110|opt | id(8) Built-in Command exec
212 *
213 * ACT_FUNCTION(1111):
214 * 1111| address(12) Function?
215 * 1111|opt | id(8) Function?
216 *
217 */
218enum action_kind_id {
219 ACT_LMODS = 0b0000,
220 ACT_RMODS = 0b0001,
221 ACT_LMODS_TAP = 0b0010,
222 ACT_RMODS_TAP = 0b0011,
223
224 ACT_USAGE = 0b0100,
225 ACT_MOUSEKEY = 0b0101,
226
227 ACT_KEYMAP = 0b1000,
228 ACT_OVERLAY = 0b1001,
229
230 ACT_MACRO = 0b1100,
231 ACT_COMMAND = 0b1110,
232 ACT_FUNCTION = 0b1111
233};
234
235
236/* action utility */
237#define ACTION_NO 0
238#define ACTION_TRANSPARENT 1
239#define ACTION(kind, param) ((kind)<<12 | (param))
240#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
241
242/*
243 * Key
244 */
245#define ACTION_KEY(key) ACTION(ACT_LMODS, key)
246/* Mods & key */
247#define ACTION_LMODS(mods) ACTION(ACT_LMODS, MODS4(mods)<<8 | 0x00)
248#define ACTION_LMODS_KEY(mods, key) ACTION(ACT_LMODS, MODS4(mods)<<8 | (key))
249#define ACTION_RMODS(mods) ACTION(ACT_RMODS, MODS4(mods)<<8 | 0x00)
250#define ACTION_RMODS_KEY(mods, key) ACTION(ACT_RMODS, MODS4(mods)<<8 | (key))
251#define ACTION_LMOD(mod) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
252#define ACTION_LMOD_KEY(mod, key) ACTION(ACT_LMODS, MODS4(MOD_BIT(mod))<<8 | (key))
253#define ACTION_RMOD(mod) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | 0x00)
254#define ACTION_RMOD_KEY(mod, key) ACTION(ACT_RMODS, MODS4(MOD_BIT(mod))<<8 | (key))
255/* Tap key */
256enum mods_codes {
257 MODS_ONESHOT = 0x00,
258};
259#define ACTION_LMODS_TAP_KEY(mods, key) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | (key))
260#define ACTION_LMODS_ONESHOT(mods) ACTION(ACT_LMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
261#define ACTION_RMODS_TAP_KEY(mods, key) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | (key))
262#define ACTION_RMODS_ONESHOT(mods) ACTION(ACT_RMODS_TAP, MODS4(mods)<<8 | MODS_ONESHOT)
263#define ACTION_LMOD_TAP_KEY(mod, key) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
264#define ACTION_LMOD_ONESHOT(mod) ACTION(ACT_LMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
265#define ACTION_RMOD_TAP_KEY(mod, key) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | (key))
266#define ACTION_RMOD_ONESHOT(mod) ACTION(ACT_RMODS_TAP, MODS4(MOD_BIT(mod))<<8 | MODS_ONESHOT)
267
268/* HID Usage */
269enum usage_pages {
270 PAGE_SYSTEM,
271 PAGE_CONSUMER
272};
273#define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
274#define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
275
276/* Mousekey */
277#define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
278
279
280
281/* Layer Actions:
282 * Invert layer ^= (1<<layer)
283 * On layer |= (1<<layer)
284 * Off layer &= ~(1<<layer)
285 * Set layer = (1<<layer)
286 * Clear layer = 0
287 */
288enum layer_params {
289 ON_PRESS = 1,
290 ON_RELEASE = 2,
291 ON_BOTH = 3,
292
293 OP_RESET = 0x00,
294 OP_INV4 = 0x00,
295 OP_INV = 0xF0,
296 OP_ON = 0xF4,
297 OP_OFF = 0xF8,
298 OP_SET = 0xFC,
299};
300
301/*
302 * Default Layer
303 */
304#define ACTION_DEFAULT_LAYER ACTION(ACT_KEYMAP, ON_RELEASE<<8 | OP_RESET | 0)
305#define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_TO(layer, ON_RELEASE)
306#define ACTION_DEFAULT_LAYER_TO(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_RESET | (on))
307/*
308 * Keymap Layer
309 */
310#define ACTION_KEYMAP_MOMENTARY(layer) ACTION_KEYMAP_ON_OFF(layer)
311#define ACTION_KEYMAP_TOGGLE(layer) ACTION_KEYMAP_INV(layer, ON_RELEASE)
312/* Keymap Invert */
313#define ACTION_KEYMAP_INV(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | (on))
314#define ACTION_KEYMAP_TAP_TOGGLE(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_INV | 0)
315/* Keymap On */
316#define ACTION_KEYMAP_ON(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | (on))
317#define ACTION_KEYMAP_ON_OFF(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_ON | 0)
318/* Keymap Off */
319#define ACTION_KEYMAP_OFF(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | (on))
320#define ACTION_KEYMAP_OFF_ON(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_OFF | 0)
321/* Keymap Set */
322#define ACTION_KEYMAP_SET(layer, on) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | (on))
323#define ACTION_KEYMAP_SET_CLEAR(layer) ACTION(ACT_KEYMAP, (layer)<<8 | OP_SET | 0)
324/* Keymap Invert with tap key */
325#define ACTION_KEYMAP_TAP_KEY(layer, key) ACTION(ACT_KEYMAP, (layer)<<8 | (key))
326
327/*
328 * Overlay Layer
329 */
330#define ACTION_OVERLAY_MOMENTARY(layer) ACTION_OVERLAY_ON_OFF(layer)
331#define ACTION_OVERLAY_TOGGLE(layer) ACTION_OVERLAY_INV(layer, ON_RELEASE)
332/* Overlay Clear */
333#define ACTION_OVERLAY_CLEAR(on) ACTION(ACT_OVERLAY, 0<<8 | OP_INV4 | (on))
334/* Overlay Invert 4-bit chunk */
335#define ACTION_OVERLAY_INV4(bits, shift) ACTION(ACT_OVERLAY, (bits)<<8 | OP_INV4 | shift)
336/* Overlay Invert */
337#define ACTION_OVERLAY_INV(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | (on))
338#define ACTION_OVERLAY_TAP_TOGGLE(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_INV | 0)
339/* Overlay On */
340#define ACTION_OVERLAY_ON(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | (on))
341#define ACTION_OVERLAY_ON_OFF(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_ON | 0)
342/* Overlay Off */
343#define ACTION_OVERLAY_OFF(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | (on))
344#define ACTION_OVERLAY_OFF_ON(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_OFF | 0)
345/* Overlay Set */
346#define ACTION_OVERLAY_SET(layer, on) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | (on))
347#define ACTION_OVERLAY_SET_CLEAR(layer) ACTION(ACT_OVERLAY, (layer)<<8 | OP_SET | 0)
348/* Overlay Invert with tap key */
349#define ACTION_OVERLAY_TAP_KEY(layer, key) ACTION(ACT_OVERLAY, (layer)<<8 | (key))
350
351
352/*
353 * Extensions
354 */
355/* Macro */
356#define ACTION_MACRO(id) ACTION(ACT_MACRO, (id))
357#define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
358#define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id))
359
360/* Command */
361#define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
362
363/* Function */
364enum function_opts {
365 FUNC_TAP = 0x8, /* indciates function is tappable */
366};
367#define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id))
368#define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
369#define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id))
370
371#endif /* ACTION_H */ 76#endif /* ACTION_H */
diff --git a/common/action_code.h b/common/action_code.h
new file mode 100644
index 000000000..0933dce13
--- /dev/null
+++ b/common/action_code.h
@@ -0,0 +1,289 @@
1/*
2Copyright 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_CODE_H
18#define ACTION_CODE_H
19
20/* Action codes
21 * ============
22 * 16bit code: action_kind(4bit) + action_parameter(12bit)
23 *
24 *
25 * Key Actions(00xx)
26 * -----------------
27 * ACT_MODS(000r):
28 * 000r|0000|0000 0000 No action code
29 * 000r|0000|0000 0001 Transparent code
30 * 000r|0000| keycode Key
31 * 000r|mods|0000 0000 Modifiers
32 * 000r|mods| keycode Key and Modifiers
33 * r: Left/Right flag(Left:0, Right:1)
34 *
35 * ACT_MODS_TAP(001r):
36 * 0010|mods|0000 0000 Modifiers with OneShot
37 * 0010|mods|0000 00xx (reserved)
38 * 0010|mods| keycode Modifiers with Tap Key
39 *
40 *
41 * Other Keys(01xx)
42 * ----------------
43 * ACT_USAGE(0100): TODO: Not needed?
44 * 0100|00| usage(10) System control(0x80) - General Desktop page(0x01)
45 * 0100|01| usage(10) Consumer control(0x01) - Consumer page(0x0C)
46 * 0100|10| usage(10) (reserved)
47 * 0100|11| usage(10) (reserved)
48 *
49 * ACT_MOUSEKEY(0110): TODO: Not needed?
50 * 0101|xxxx| keycode Mouse key
51 *
52 * 011x|xxxx xxxx xxxx (reseved)
53 *
54 *
55 * Layer Actions(10xx)
56 * -------------------
57 * ACT_LAYER(1000):
58 * 1000|oo00|pppE BBBB Default Layer Bitwise operation
59 * oo: operation(00:AND, 01:OR, 10:XOR, 11:SET)
60 * ppp: 4-bit chunk part(0-7)
61 * EBBBB: bits and extra bit
62 * 1000|ooee|pppE BBBB Layer Bitwise Operation
63 * oo: operation(00:AND, 01:OR, 10:XOR, 11:SET)
64 * ppp: 4-bit chunk part(0-7)
65 * eBBBB: bits and extra bit
66 * ee: on event(00:default layer, 01:press, 10:release, 11:both)
67 *
68 * 1001|xxxx|xxxx xxxx (reserved)
69 * 1001|oopp|BBBB BBBB 8-bit Bitwise Operation???
70 *
71 * ACT_LAYER_TAP(101x):
72 * 101E|LLLL| keycode Invert with tap key
73 * 101E|LLLL|1110 xxxx Reserved(0xE0-EF)
74 * 101E|LLLL|1111 0000 Invert with tap toggle(0xF0)
75 * 101E|LLLL|1111 0001 On/Off
76 * 101E|LLLL|1111 0010 Off/On
77 * 101E|LLLL|1111 0011 Set/Clear
78 * 101E|LLLL|1111 xxxx Reserved(0xF4-FF)
79 * ELLLL: layer(0-31)
80 *
81 *
82 * Extensions(11xx)
83 * ----------------
84 * ACT_MACRO(1100):
85 * 1100|opt | id(8) Macro play?
86 * 1100|1111| id(8) Macro record?
87 *
88 * ACT_COMMAND(1110):
89 * 1110|opt | id(8) Built-in Command exec
90 *
91 * ACT_FUNCTION(1111):
92 * 1111| address(12) Function?
93 * 1111|opt | id(8) Function?
94 */
95enum action_kind_id {
96 /* Key Actions */
97 ACT_MODS = 0b0000,
98 ACT_LMODS = 0b0000,
99 ACT_RMODS = 0b0001,
100 ACT_MODS_TAP = 0b0010,
101 ACT_LMODS_TAP = 0b0010,
102 ACT_RMODS_TAP = 0b0011,
103 /* Other Keys */
104 ACT_USAGE = 0b0100,
105 ACT_MOUSEKEY = 0b0101,
106 /* Layer Actions */
107 ACT_LAYER = 0b1000,
108 ACT_LAYER_TAP = 0b1010,
109 ACT_LAYER_TAP1 = 0b1011,
110 /* Extensions */
111 ACT_MACRO = 0b1100,
112 ACT_COMMAND = 0b1110,
113 ACT_FUNCTION = 0b1111
114};
115
116
117/* Action Code Struct
118 *
119 * NOTE:
120 * In avr-gcc bit field seems to be assigned from LSB(bit0) to MSB(bit15).
121 * AVR looks like a little endian in avr-gcc.
122 * Not portable across compiler/endianness?
123 *
124 * Byte order and bit order of 0x1234:
125 * Big endian: Little endian:
126 * -------------------- --------------------
127 * FEDC BA98 7654 3210 0123 4567 89AB CDEF
128 * 0001 0010 0011 0100 0010 1100 0100 1000
129 * 0x12 0x34 0x34 0x12
130 */
131typedef union {
132 uint16_t code;
133 struct action_kind {
134 uint16_t param :12;
135 uint8_t id :4;
136 } kind;
137 struct action_key {
138 uint8_t code :8;
139 uint8_t mods :4;
140 uint8_t kind :4;
141 } key;
142 struct action_layer_bitop {
143 uint8_t bits :4;
144 uint8_t xbit :1;
145 uint8_t part :3;
146 uint8_t on :2;
147 uint8_t op :2;
148 uint8_t kind :4;
149 } layer_bitop;
150 struct action_layer_tap {
151 uint8_t code :8;
152 uint8_t val :5;
153 uint8_t kind :3;
154 } layer_tap;
155 struct action_usage {
156 uint16_t code :10;
157 uint8_t page :2;
158 uint8_t kind :4;
159 } usage;
160 struct action_command {
161 uint8_t id :8;
162 uint8_t opt :4;
163 uint8_t kind :4;
164 } command;
165 struct action_function {
166 uint8_t id :8;
167 uint8_t opt :4;
168 uint8_t kind :4;
169 } func;
170} action_t;
171
172
173/* action utility */
174#define ACTION_NO 0
175#define ACTION_TRANSPARENT 1
176#define ACTION(kind, param) ((kind)<<12 | (param))
177
178
179/*
180 * Key Actions
181 */
182/* Mod bits: 43210
183 * bit 0 ||||+- Control
184 * bit 1 |||+-- Shift
185 * bit 2 ||+--- Alt
186 * bit 3 |+---- Gui
187 * bit 4 +----- LR flag(Left:0, Right:1)
188 */
189enum mods_bit {
190 MOD_LCTL = 0x01,
191 MOD_LSFT = 0x02,
192 MOD_LALT = 0x04,
193 MOD_LGUI = 0x08,
194 MOD_RCTL = 0x11,
195 MOD_RSFT = 0x12,
196 MOD_RALT = 0x14,
197 MOD_RGUI = 0x18,
198};
199enum mods_codes {
200 MODS_ONESHOT = 0x00,
201};
202#define ACTION_KEY(key) ACTION(ACT_MODS, (key))
203#define ACTION_MODS(mods) ACTION(ACT_MODS, (mods)<<8 | 0)
204#define ACTION_MODS_KEY(mods, key) ACTION(ACT_MODS, (mods)<<8 | (key))
205#define ACTION_MODS_TAP_KEY(mods, key) ACTION(ACT_MODS_TAP, (mods)<<8 | (key))
206#define ACTION_MODS_ONESHOT(mods) ACTION(ACT_MODS_TAP, (mods)<<8 | MODS_ONESHOT)
207
208
209/*
210 * Other Keys
211 */
212enum usage_pages {
213 PAGE_SYSTEM,
214 PAGE_CONSUMER
215};
216#define ACTION_USAGE_SYSTEM(id) ACTION(ACT_USAGE, PAGE_SYSTEM<<10 | (id))
217#define ACTION_USAGE_CONSUMER(id) ACTION(ACT_USAGE, PAGE_CONSUMER<<10 | (id))
218#define ACTION_MOUSEKEY(key) ACTION(ACT_MOUSEKEY, key)
219
220
221
222/*
223 * Layer Actions
224 */
225enum layer_param_on {
226 ON_PRESS = 1,
227 ON_RELEASE = 2,
228 ON_BOTH = 3,
229};
230enum layer_param_bit_op {
231 OP_BIT_AND = 0,
232 OP_BIT_OR = 1,
233 OP_BIT_XOR = 2,
234 OP_BIT_SET = 3,
235};
236enum layer_pram_tap_op {
237 OP_TAP_TOGGLE = 0xF0,
238 OP_ON_OFF,
239 OP_OFF_ON,
240 OP_SET_CLEAR,
241};
242#define ACTION_LAYER_BITOP(op, part, bits, on) (ACT_LAYER<<12 | (op)<<10 | (on)<<8 | (part)<<5 | ((bits)&0x1f))
243#define ACTION_LAYER_TAP(layer, key) (ACT_LAYER_TAP<<12 | (layer)<<8 | (key))
244/* Default Layer */
245#define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_BIT_SET((layer)/4, 1<<((layer)%4))
246/* Layer Operation */
247#define ACTION_LAYER_CLEAR(on) ACTION_LAYER_BIT_AND(0, 0, (on))
248#define ACTION_LAYER_MOMENTARY(layer) ACTION_LAYER_ON_OFF(layer)
249#define ACTION_LAYER_TOGGLE(layer) ACTION_LAYER_INVERT(layer, ON_RELEASE)
250#define ACTION_LAYER_INVERT(layer, on) ACTION_LAYER_BIT_XOR((layer)/4, 1<<((layer)%4), (on))
251#define ACTION_LAYER_ON(layer, on) ACTION_LAYER_BIT_OR( (layer)/4, 1<<((layer)%4), (on))
252#define ACTION_LAYER_OFF(layer, on) ACTION_LAYER_BIT_AND((layer)/4, ~(1<<((layer)%4)), (on))
253#define ACTION_LAYER_SET(layer, on) ACTION_LAYER_BIT_SET((layer)/4, 1<<((layer)%4), (on))
254#define ACTION_LAYER_ON_OFF(layer) ACTION_LAYER_TAP((layer), OP_ON_OFF)
255#define ACTION_LAYER_OFF_ON(layer) ACTION_LAYER_TAP((layer), OP_OFF_ON)
256#define ACTION_LAYER_SET_CLEAR(layer) ACTION_LAYER_TAP((layer), OP_SET_CLEAR)
257/* With Tapping */
258#define ACTION_LAYER_TAP_KEY(layer, key) ACTION_LAYER_TAP((layer), (key))
259#define ACTION_LAYER_TAP_TOGGLE(layer) ACTION_LAYER_TAP((layer), OP_TAP_TOGGLE)
260/* Bitwise Operation */
261#define ACTION_LAYER_BIT_AND(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_AND, (part), (bits), (on))
262#define ACTION_LAYER_BIT_OR( part, bits, on) ACTION_LAYER_BITOP(OP_BIT_OR, (part), (bits), (on))
263#define ACTION_LAYER_BIT_XOR(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_XOR, (part), (bits), (on))
264#define ACTION_LAYER_BIT_SET(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_SET, (part), (bits), (on))
265/* Default Layer Bitwise Operation */
266#define ACTION_DEFAULT_LAYER_BIT_AND(part, bits) ACTION_LAYER_BITOP(OP_BIT_AND, (part), (bits), 0)
267#define ACTION_DEFAULT_LAYER_BIT_OR( part, bits) ACTION_LAYER_BITOP(OP_BIT_OR, (part), (bits), 0)
268#define ACTION_DEFAULT_LAYER_BIT_XOR(part, bits) ACTION_LAYER_BITOP(OP_BIT_XOR, (part), (bits), 0)
269#define ACTION_DEFAULT_LAYER_BIT_SET(part, bits) ACTION_LAYER_BITOP(OP_BIT_SET, (part), (bits), 0)
270
271
272/*
273 * Extensions
274 */
275/* Macro */
276#define ACTION_MACRO(id) ACTION(ACT_MACRO, (id))
277#define ACTION_MACRO_TAP(id) ACTION(ACT_MACRO, FUNC_TAP<<8 | (id))
278#define ACTION_MACRO_OPT(id, opt) ACTION(ACT_MACRO, (opt)<<8 | (id))
279/* Command */
280#define ACTION_COMMAND(id, opt) ACTION(ACT_COMMAND, (opt)<<8 | (addr))
281/* Function */
282enum function_opts {
283 FUNC_TAP = 0x8, /* indciates function is tappable */
284};
285#define ACTION_FUNCTION(id) ACTION(ACT_FUNCTION, (id))
286#define ACTION_FUNCTION_TAP(id) ACTION(ACT_FUNCTION, FUNC_TAP<<8 | (id))
287#define ACTION_FUNCTION_OPT(id, opt) ACTION(ACT_FUNCTION, (opt)<<8 | (id))
288
289#endif /* ACTION_CODE_H */
diff --git a/common/action_layer.c b/common/action_layer.c
new file mode 100644
index 000000000..3413c53e6
--- /dev/null
+++ b/common/action_layer.c
@@ -0,0 +1,135 @@
1#include <stdint.h>
2#include "keyboard.h"
3#include "action.h"
4#include "debug.h"
5#include "util.h"
6#include "action_layer.h"
7
8
9/*
10 * Default Layer State
11 */
12uint32_t default_layer_state = 0;
13
14static void default_layer_state_set(uint32_t state)
15{
16 debug("default_layer_state: ");
17 default_layer_debug(); debug(" to ");
18 default_layer_state = state;
19 default_layer_debug(); debug("\n");
20 clear_keyboard_but_mods(); // To avoid stuck keys
21}
22
23void default_layer_debug(void)
24{
25 debug_hex32(default_layer_state);
26 debug("("); debug_dec(biton32(default_layer_state)); debug(")");
27}
28
29void default_layer_set(uint8_t layer)
30{
31 default_layer_state_set(1UL<<layer);
32}
33
34#ifndef NO_ACTION_LAYER
35void default_layer_or(uint32_t state)
36{
37 default_layer_state_set(default_layer_state | state);
38}
39void default_layer_and(uint32_t state)
40{
41 default_layer_state_set(default_layer_state & state);
42}
43void default_layer_xor(uint32_t state)
44{
45 default_layer_state_set(default_layer_state ^ state);
46}
47#endif
48
49
50#ifndef NO_ACTION_LAYER
51/*
52 * Keymap Layer State
53 */
54uint32_t layer_state = 0;
55
56static void layer_state_set(uint32_t state)
57{
58 debug("layer_state: ");
59 layer_debug(); debug(" to ");
60 layer_state = state;
61 layer_debug(); debug("\n");
62 clear_keyboard_but_mods(); // To avoid stuck keys
63}
64
65void layer_clear(void)
66{
67 layer_state_set(0);
68}
69
70void layer_move(uint8_t layer)
71{
72 layer_state_set(1UL<<layer);
73}
74
75void layer_on(uint8_t layer)
76{
77 layer_state_set(layer_state | (1UL<<layer));
78}
79
80void layer_off(uint8_t layer)
81{
82 layer_state_set(layer_state & ~(1UL<<layer));
83}
84
85void layer_invert(uint8_t layer)
86{
87 layer_state_set(layer_state ^ (1UL<<layer));
88}
89
90void layer_or(uint32_t state)
91{
92 layer_state_set(layer_state | state);
93}
94void layer_and(uint32_t state)
95{
96 layer_state_set(layer_state & state);
97}
98void layer_xor(uint32_t state)
99{
100 layer_state_set(layer_state ^ state);
101}
102
103void layer_debug(void)
104{
105 debug_hex32(layer_state);
106 debug("("); debug_dec(biton32(layer_state)); debug(")");
107}
108#endif
109
110
111
112action_t layer_switch_get_action(key_t key)
113{
114 action_t action;
115 action.code = ACTION_TRANSPARENT;
116
117#ifndef NO_ACTION_LAYER
118 uint32_t layers = layer_state | default_layer_state;
119 /* check top layer first */
120 for (int8_t i = 31; i >= 0; i--) {
121 if (layers & (1UL<<i)) {
122 action = action_for_key(i, key);
123 if (action.code != ACTION_TRANSPARENT) {
124 return action;
125 }
126 }
127 }
128 /* fall back to layer 0 */
129 action = action_for_key(0, key);
130 return action;
131#else
132 action = action_for_key(biton32(default_layer_state), key);
133 return action;
134#endif
135}
diff --git a/common/action_layer.h b/common/action_layer.h
new file mode 100644
index 000000000..23f8a00bb
--- /dev/null
+++ b/common/action_layer.h
@@ -0,0 +1,77 @@
1/*
2Copyright 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_LAYER_H
18#define ACTION_LAYER_H
19
20#include <stdint.h>
21#include "keyboard.h"
22#include "action.h"
23
24
25/*
26 * Default Layer
27 */
28extern uint32_t default_layer_state;
29void default_layer_debug(void);
30void default_layer_set(uint8_t layer);
31
32#ifndef NO_ACTION_LAYER
33/* bitwise operation */
34void default_layer_or(uint32_t state);
35void default_layer_and(uint32_t state);
36void default_layer_xor(uint32_t state);
37#else
38#define default_layer_or(state)
39#define default_layer_and(state)
40#define default_layer_xor(state)
41#endif
42
43
44/*
45 * Keymap Layer
46 */
47#ifndef NO_ACTION_LAYER
48extern uint32_t layer_state;
49void layer_debug(void);
50void layer_clear(void);
51void layer_move(uint8_t layer);
52void layer_on(uint8_t layer);
53void layer_off(uint8_t layer);
54void layer_invert(uint8_t layer);
55/* bitwise operation */
56void layer_or(uint32_t state);
57void layer_and(uint32_t state);
58void layer_xor(uint32_t state);
59#else
60#define layer_state 0
61#define layer_clear()
62#define layer_move(layer)
63#define layer_on(layer)
64#define layer_off(layer)
65#define layer_invert(layer)
66
67#define layer_or(state)
68#define layer_and(state)
69#define layer_xor(state)
70#define layer_debug()
71#endif
72
73
74/* return action depending on current layer status */
75action_t layer_switch_get_action(key_t key);
76
77#endif
diff --git a/common/action_tapping.h b/common/action_tapping.h
index c9f09f576..9b42d50dc 100644
--- a/common/action_tapping.h
+++ b/common/action_tapping.h
@@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18#define ACTION_TAPPING_H 18#define ACTION_TAPPING_H
19 19
20 20
21#ifndef NO_ACTION_TAPPING
22 21
23/* period of tapping(ms) */ 22/* period of tapping(ms) */
24#ifndef TAPPING_TERM 23#ifndef TAPPING_TERM
@@ -33,8 +32,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
33#define WAITING_BUFFER_SIZE 8 32#define WAITING_BUFFER_SIZE 8
34 33
35 34
35#ifndef NO_ACTION_TAPPING
36void action_tapping_process(keyrecord_t record); 36void action_tapping_process(keyrecord_t record);
37
38#endif 37#endif
39 38
40#endif 39#endif
diff --git a/common/command.c b/common/command.c
index b29333883..3a1fcb186 100644
--- a/common/command.c
+++ b/common/command.c
@@ -26,7 +26,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
26#include "timer.h" 26#include "timer.h"
27#include "keyboard.h" 27#include "keyboard.h"
28#include "bootloader.h" 28#include "bootloader.h"
29#include "layer_switch.h" 29#include "action_layer.h"
30#include "eeconfig.h" 30#include "eeconfig.h"
31#include "sleep_led.h" 31#include "sleep_led.h"
32#include "led.h" 32#include "led.h"
@@ -573,8 +573,8 @@ static uint8_t numkey2num(uint8_t code)
573 573
574static void switch_default_layer(uint8_t layer) 574static void switch_default_layer(uint8_t layer)
575{ 575{
576 print("switch_default_layer: "); print_dec(default_layer); print(" to "); print_dec(layer); print("\n"); 576 print("switch_default_layer: "); print_dec(biton32(default_layer_state));
577 print(" to "); print_dec(layer); print("\n");
577 default_layer_set(layer); 578 default_layer_set(layer);
578 overlay_clear();
579 clear_keyboard(); 579 clear_keyboard();
580} 580}
diff --git a/common/keymap.c b/common/keymap.c
index ace3f49b6..c98ce09b6 100644
--- a/common/keymap.c
+++ b/common/keymap.c
@@ -18,7 +18,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
18#include "keymap.h" 18#include "keymap.h"
19#include "report.h" 19#include "report.h"
20#include "keycode.h" 20#include "keycode.h"
21#include "layer_switch.h" 21#include "action_layer.h"
22#include "action.h" 22#include "action.h"
23#include "action_macro.h" 23#include "action_macro.h"
24#include "debug.h" 24#include "debug.h"
diff --git a/common/layer_switch.c b/common/layer_switch.c
deleted file mode 100644
index a5d426a89..000000000
--- a/common/layer_switch.c
+++ /dev/null
@@ -1,209 +0,0 @@
1#include <stdint.h>
2#include "keyboard.h"
3#include "action.h"
4#include "debug.h"
5#include "util.h"
6#include "layer_switch.h"
7
8
9/*
10 * Default Layer (0-15)
11 */
12uint8_t default_layer = 0;
13
14void default_layer_set(uint8_t layer)
15{
16 debug("default_layer_set: ");
17 debug_dec(default_layer); debug(" to ");
18
19 default_layer = layer;
20
21 debug_dec(default_layer); debug("\n");
22
23 clear_keyboard_but_mods(); // To avoid stuck keys
24}
25
26
27#ifndef NO_ACTION_KEYMAP
28/*
29 * Keymap Layer (0-15)
30 */
31uint16_t keymap_stat = 0;
32
33/* return highest layer whose state is on */
34uint8_t keymap_get_layer(void)
35{
36 return biton16(keymap_stat);
37}
38
39static void keymap_stat_set(uint16_t stat)
40{
41 debug("keymap: ");
42 keymap_debug(); debug(" to ");
43
44 keymap_stat = stat;
45
46 keymap_debug(); debug("\n");
47
48 clear_keyboard_but_mods(); // To avoid stuck keys
49}
50
51void keymap_clear(void)
52{
53 keymap_stat_set(0);
54}
55
56
57void keymap_set(uint16_t stat)
58{
59 keymap_stat_set(stat);
60}
61
62void keymap_move(uint8_t layer)
63{
64 keymap_stat_set(1<<layer);
65}
66
67void keymap_on(uint8_t layer)
68{
69 keymap_stat_set(keymap_stat | (1<<layer));
70}
71
72void keymap_off(uint8_t layer)
73{
74 keymap_stat_set(keymap_stat & ~(1<<layer));
75}
76
77void keymap_invert(uint8_t layer)
78{
79 keymap_stat_set(keymap_stat ^ (1<<layer));
80}
81
82void keymap_or(uint16_t stat)
83{
84 keymap_stat_set(keymap_stat | stat);
85}
86void keymap_and(uint16_t stat)
87{
88 keymap_stat_set(keymap_stat & stat);
89}
90void keymap_xor(uint16_t stat)
91{
92 keymap_stat_set(keymap_stat ^ stat);
93}
94
95void keymap_debug(void)
96{
97 debug_hex16(keymap_stat); debug("("); debug_dec(keymap_get_layer()); debug(")");
98}
99#endif
100
101
102
103#ifndef NO_ACTION_OVERLAY
104/*
105 * Overlay Layer (16-31 = 0-15|0x10)
106 */
107uint16_t overlay_stat = 0;
108
109/* return highest layer whose state is on */
110uint8_t overlay_get_layer(void)
111{
112 return biton16(overlay_stat);
113}
114
115static void overlay_stat_set(uint16_t stat)
116{
117 debug("overlay: ");
118 overlay_debug(); debug(" to ");
119
120 overlay_stat = stat;
121
122 overlay_debug(); debug("\n");
123
124 clear_keyboard_but_mods(); // To avoid stuck keys
125}
126
127void overlay_clear(void)
128{
129 overlay_stat_set(0);
130}
131
132
133void overlay_set(uint16_t stat)
134{
135 overlay_stat_set(stat);
136}
137
138void overlay_move(uint8_t layer)
139{
140 overlay_stat_set(1<<layer);
141}
142
143void overlay_on(uint8_t layer)
144{
145 overlay_stat_set(overlay_stat | (1<<layer));
146}
147
148void overlay_off(uint8_t layer)
149{
150 overlay_stat_set(overlay_stat & ~(1<<layer));
151}
152
153void overlay_invert(uint8_t layer)
154{
155 overlay_stat_set(overlay_stat ^ (1<<layer));
156}
157
158void overlay_or(uint16_t stat)
159{
160 overlay_stat_set(overlay_stat | stat);
161}
162void overlay_and(uint16_t stat)
163{
164 overlay_stat_set(overlay_stat & stat);
165}
166void overlay_xor(uint16_t stat)
167{
168 overlay_stat_set(overlay_stat ^ stat);
169}
170
171void overlay_debug(void)
172{
173 debug_hex16(overlay_stat); debug("("); debug_dec(overlay_get_layer()); debug(")");
174}
175#endif
176
177action_t layer_switch_get_action(key_t key)
178{
179 action_t action;
180 action.code = ACTION_TRANSPARENT;
181
182#ifndef NO_ACTION_OVERLAY
183 /* overlay: top layer first */
184 for (int8_t i = 15; i >= 0; i--) {
185 if (overlay_stat & (1<<i)) {
186 action = action_for_key(i | OVERLAY_BIT, key);
187 if (action.code != ACTION_TRANSPARENT) {
188 return action;
189 }
190 }
191 }
192#endif
193
194#ifndef NO_ACTION_KEYMAP
195 /* keymap: top layer first */
196 for (int8_t i = 15; i >= 0; i--) {
197 if (keymap_stat & (1<<i)) {
198 action = action_for_key(i, key);
199 if (action.code != ACTION_TRANSPARENT) {
200 return action;
201 }
202 }
203 }
204#endif
205
206 /* default layer */
207 action = action_for_key(default_layer, key);
208 return action;
209}
diff --git a/common/layer_switch.h b/common/layer_switch.h
deleted file mode 100644
index eb4cf61ba..000000000
--- a/common/layer_switch.h
+++ /dev/null
@@ -1,110 +0,0 @@
1/*
2Copyright 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 LAYER_SWITCH_H
18#define LAYER_SWITCH_H
19
20#include <stdint.h>
21#include "keyboard.h"
22#include "action.h"
23
24
25/* overlays are asigned at layer 16-31 */
26#define OVERLAY_BIT 0x10
27#define OVERLAY_MASK 0x0F
28
29
30/*
31 * Default Layer
32 */
33/* base layer to fall back */
34extern uint8_t default_layer;
35void default_layer_set(uint8_t layer);
36
37
38/*
39 * Keymap Layer
40 */
41#ifndef NO_ACTION_KEYMAP
42extern uint16_t keymap_stat;
43/* return current active layer */
44uint8_t keymap_get_layer(void);
45void keymap_clear(void);
46void keymap_set(uint16_t stat);
47void keymap_move(uint8_t layer);
48void keymap_on(uint8_t layer);
49void keymap_off(uint8_t layer);
50void keymap_invert(uint8_t layer);
51/* bitwise operation */
52void keymap_or(uint16_t stat);
53void keymap_and(uint16_t stat);
54void keymap_xor(uint16_t stat);
55void keymap_debug(void);
56#else
57#define keymap_stat 0
58#define keymap_get_layer()
59#define keymap_clear()
60#define keymap_set(stat)
61#define keymap_move(layer)
62#define keymap_on(layer)
63#define keymap_off(layer)
64#define keymap_invert(layer)
65#define keymap_or(stat)
66#define keymap_and(stat)
67#define keymap_xor(stat)
68#define keymap_debug()
69#endif
70
71
72/*
73 * Overlay Layer
74 */
75#ifndef NO_ACTION_OVERLAY
76extern uint16_t overlay_stat;
77/* return current active layer */
78uint8_t overlay_get_layer(void);
79void overlay_clear(void);
80void overlay_set(uint16_t stat);
81void overlay_move(uint8_t layer);
82void overlay_on(uint8_t layer);
83void overlay_off(uint8_t layer);
84void overlay_invert(uint8_t layer);
85/* bitwise operation */
86void overlay_or(uint16_t stat);
87void overlay_and(uint16_t stat);
88void overlay_xor(uint16_t stat);
89void overlay_debug(void);
90#else
91#define overlay_stat 0
92#define overlay_get_layer()
93#define overlay_clear()
94#define overlay_set(stat)
95#define overlay_move(layer)
96#define overlay_on(layer)
97#define overlay_off(layer)
98#define overlay_invert(layer)
99#define overlay_or(stat)
100#define overlay_and(stat)
101#define overlay_xor(stat)
102#define overlay_debug()
103#endif
104
105
106
107/* return action depending on current layer status */
108action_t layer_switch_get_action(key_t key);
109
110#endif
diff --git a/common/util.c b/common/util.c
index ff1926d7d..6d4d6bfda 100644
--- a/common/util.c
+++ b/common/util.c
@@ -38,6 +38,14 @@ uint8_t bitpop16(uint16_t bits)
38 return c; 38 return c;
39} 39}
40 40
41uint8_t bitpop32(uint32_t bits)
42{
43 uint8_t c;
44 for (c = 0; bits; c++)
45 bits &= bits - 1;
46 return c;
47}
48
41// most significant on-bit - return highest location of on-bit 49// most significant on-bit - return highest location of on-bit
42// NOTE: return 0 when bit0 is on or all bits are off 50// NOTE: return 0 when bit0 is on or all bits are off
43uint8_t biton(uint8_t bits) 51uint8_t biton(uint8_t bits)
@@ -58,3 +66,14 @@ uint8_t biton16(uint16_t bits)
58 if (bits >> 1) { bits >>= 1; n += 1;} 66 if (bits >> 1) { bits >>= 1; n += 1;}
59 return n; 67 return n;
60} 68}
69
70uint8_t biton32(uint32_t bits)
71{
72 uint8_t n = 0;
73 if (bits >>16) { bits >>=16; n +=16;}
74 if (bits >> 8) { bits >>= 8; n += 8;}
75 if (bits >> 4) { bits >>= 4; n += 4;}
76 if (bits >> 2) { bits >>= 2; n += 2;}
77 if (bits >> 1) { bits >>= 1; n += 1;}
78 return n;
79}
diff --git a/common/util.h b/common/util.h
index 58b7fdf14..4b8b5ca3a 100644
--- a/common/util.h
+++ b/common/util.h
@@ -30,7 +30,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
30 30
31uint8_t bitpop(uint8_t bits); 31uint8_t bitpop(uint8_t bits);
32uint8_t bitpop16(uint16_t bits); 32uint8_t bitpop16(uint16_t bits);
33uint8_t bitpop32(uint32_t bits);
34
33uint8_t biton(uint8_t bits); 35uint8_t biton(uint8_t bits);
34uint8_t biton16(uint16_t bits); 36uint8_t biton16(uint16_t bits);
37uint8_t biton32(uint32_t bits);
35 38
36#endif 39#endif