From cc8e66754b1a5d0c11985cb0feb51ead49668744 Mon Sep 17 00:00:00 2001 From: tmk Date: Tue, 2 Apr 2013 16:09:43 +0900 Subject: [PATCH] Refine ACT_LAYER and ACT_LAYER_TAP - Remove ACT_LAYER_BITOP --- common/action.c | 201 +++++++++++++--------------------------- common/action.h | 164 +++++++++++++++----------------- common/action_tapping.h | 3 +- common/command.c | 3 +- common/layer_switch.c | 134 +++++++++++++++------------ common/layer_switch.h | 61 ++++++------ common/util.c | 19 ++++ common/util.h | 3 + 8 files changed, 268 insertions(+), 320 deletions(-) diff --git a/common/action.c b/common/action.c index ef04851b..596831d4 100644 --- a/common/action.c +++ b/common/action.c @@ -50,14 +50,19 @@ void action_exec(keyevent_t event) void process_action(keyrecord_t *record) { keyevent_t event = record->event; +#ifndef NO_ACTION_TAPPING uint8_t tap_count = record->tap.count; +#endif if (IS_NOEVENT(event)) { return; } action_t action = layer_switch_get_action(event.key); debug("ACTION: "); debug_action(action); - debug(" keymaps: "); keymap_debug(); - debug(" default_layer: "); debug_dec(default_layer); debug("\n"); +#ifndef NO_ACTION_LAYER + debug(" layer_state: "); layer_debug(); + debug(" default_layer_state: "); default_layer_debug(); +#endif + debug("\n"); switch (action.kind.id) { /* Key and Mods */ @@ -92,7 +97,7 @@ void process_action(keyrecord_t *record) { uint8_t mods = (action.kind.id == ACT_LMODS_TAP) ? action.key.mods : action.key.mods<<4; - switch (action.layer.code) { + switch (action.layer_tap.code) { #ifndef NO_ACTION_ONESHOT case 0x00: // Oneshot modifier @@ -200,163 +205,86 @@ void process_action(keyrecord_t *record) #endif #ifndef NO_ACTION_LAYER case ACT_LAYER: - case ACT_LAYER1: - switch (action.layer.code) { - /* Keymap clear */ - case OP_RESET: - switch (action.layer.val & 0x03) { - case 0: - // NOTE: reserved - keymap_clear(); - break; - case ON_PRESS: - if (event.pressed) { - keymap_clear(); - } - break; - case ON_RELEASE: - if (!event.pressed) { - keymap_clear(); - } - break; - case ON_BOTH: - keymap_clear(); - break; - /* NOTE: 4-7 rserved */ + if (action.layer_bitop.on == 0) { + /* Default Layer Bitwise Operation */ + if (!event.pressed) { + uint8_t shift = action.layer_bitop.part*4; + uint32_t bits = ((uint32_t)action.layer_bitop.bits)< 0) { debug("KEYMAP_TAP_KEY: Tap: register_code\n"); - register_code(action.layer.code); + register_code(action.layer_tap.code); } else { debug("KEYMAP_TAP_KEY: No tap: On on press\n"); - keymap_on(action.layer.val); + layer_on(action.layer_tap.val); } } else { if (tap_count > 0) { debug("KEYMAP_TAP_KEY: Tap: unregister_code\n"); - unregister_code(action.layer.code); + unregister_code(action.layer_tap.code); } else { debug("KEYMAP_TAP_KEY: No tap: Off on release\n"); - keymap_off(action.layer.val); + layer_off(action.layer_tap.val); } } break; } break; + #endif #endif /* Extentions */ #ifndef NO_ACTION_MACRO @@ -508,15 +436,9 @@ bool is_tap_key(key_t key) switch (action.kind.id) { case ACT_LMODS_TAP: case ACT_RMODS_TAP: + case ACT_LAYER_TAP: + case ACT_LAYER_TAP1: return true; - case ACT_LAYER: - switch (action.layer.code) { - case 0x04 ... 0xEF: /* tap key */ - case OP_INV: - return true; - default: - return false; - } case ACT_MACRO: case ACT_FUNCTION: if (action.func.opt & FUNC_TAP) { return true; } @@ -555,7 +477,8 @@ void debug_action(action_t action) case ACT_USAGE: debug("ACT_USAGE"); break; case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; case ACT_LAYER: debug("ACT_LAYER"); break; - case ACT_LAYER_BITOP: debug("ACT_LAYER_BITOP"); break; + case ACT_LAYER_TAP: debug("ACT_LAYER_TAP"); break; + case ACT_LAYER_TAP1: debug("ACT_LAYER_TAP1"); break; case ACT_MACRO: debug("ACT_MACRO"); break; case ACT_COMMAND: debug("ACT_COMMAND"); break; case ACT_FUNCTION: debug("ACT_FUNCTION"); break; diff --git a/common/action.h b/common/action.h index 2c4f306a..4daae1d0 100644 --- a/common/action.h +++ b/common/action.h @@ -63,11 +63,19 @@ typedef union { uint8_t mods :4; uint8_t kind :4; } key; - struct action_layer { + struct action_layer_bitop { + uint8_t bits :4; + uint8_t xbit :1; + uint8_t part :3; + uint8_t on :2; + uint8_t op :2; + uint8_t kind :4; + } layer_bitop; + struct action_layer_tap { uint8_t code :8; uint8_t val :5; uint8_t kind :3; - } layer; + } layer_tap; struct action_usage { uint16_t code :10; uint8_t page :2; @@ -170,40 +178,27 @@ void debug_action(action_t action); * * Layer Actions(10XX) * ------------------- - * ACT_LAYER: - * 1000|--xx|0000 0000 Clear keyamp - * 100X|LLLL|0000 00xx Reset default layer and clear keymap - * 100X|LLLL| keycode Invert with tap key - * 100X|LLLL|1111 0000 Invert with tap toggle - * 100X|LLLL|1111 00xx Invert[^= 1<>(0bBB*8)) & 0xff) BITOP 0bxxxxxxxx)<<(0bBB*8) - * layer_state: 32-bit layer switch state * * * @@ -234,9 +229,8 @@ enum action_kind_id { ACT_MOUSEKEY = 0b0101, ACT_LAYER = 0b1000, - ACT_LAYER1 = 0b1001, - ACT_LAYER_BITOP = 0b1010, - ACT_LAYER1_BITOP = 0b1011, + ACT_LAYER_TAP = 0b1010, + ACT_LAYER_TAP1 = 0b1011, ACT_MACRO = 0b1100, ACT_COMMAND = 0b1110, @@ -289,71 +283,61 @@ enum usage_pages { -/* Layer Actions: - * Invert layer ^= (1<. #define ACTION_TAPPING_H -#ifndef NO_ACTION_TAPPING /* period of tapping(ms) */ #ifndef TAPPING_TERM @@ -33,8 +32,8 @@ along with this program. If not, see . #define WAITING_BUFFER_SIZE 8 +#ifndef NO_ACTION_TAPPING void action_tapping_process(keyrecord_t record); - #endif #endif diff --git a/common/command.c b/common/command.c index e197a8f8..c954ff02 100644 --- a/common/command.c +++ b/common/command.c @@ -573,7 +573,8 @@ static uint8_t numkey2num(uint8_t code) static void switch_default_layer(uint8_t layer) { - print("switch_default_layer: "); print_dec(default_layer); print(" to "); print_dec(layer); print("\n"); + print("switch_default_layer: "); print_dec(biton32(default_layer_state)); default_layer_set(layer); + print(" to "); print_dec(biton32(default_layer_state)); print("\n"); clear_keyboard(); } diff --git a/common/layer_switch.c b/common/layer_switch.c index 359e6b9d..9905741f 100644 --- a/common/layer_switch.c +++ b/common/layer_switch.c @@ -7,94 +7,103 @@ /* - * Default Layer (0-15) + * Default Layer State */ -uint8_t default_layer = 0; +uint32_t default_layer_state = 0; + +static void default_layer_state_set(uint32_t state) +{ + debug("default_layer_state: "); + default_layer_debug(); debug(" to "); + default_layer_state = state; + default_layer_debug(); debug("\n"); + clear_keyboard_but_mods(); // To avoid stuck keys +} + +void default_layer_debug(void) +{ + debug_hex32(default_layer_state); + debug("("); debug_dec(biton32(default_layer_state)); debug(")"); +} void default_layer_set(uint8_t layer) { - debug("default_layer_set: "); - debug_dec(default_layer); debug(" to "); - - default_layer = layer; - - debug_dec(default_layer); debug("\n"); - - clear_keyboard_but_mods(); // To avoid stuck keys + default_layer_state_set(1UL<= 0; i--) { - if (keymap_stat & (1<= 0; i--) { + if (layers & (1UL<. /* * Default Layer */ -/* base layer to fall back */ -extern uint8_t default_layer; +extern uint32_t default_layer_state; +void default_layer_debug(void); void default_layer_set(uint8_t layer); +#ifndef NO_ACTION_LAYER +/* bitwise operation */ +void default_layer_or(uint32_t state); +void default_layer_and(uint32_t state); +void default_layer_xor(uint32_t state); +#else +#define default_layer_or(state) +#define default_layer_and(state) +#define default_layer_xor(state) +#endif + /* * Keymap Layer */ #ifndef NO_ACTION_LAYER -extern uint16_t keymap_stat; -/* return current active layer */ -uint8_t keymap_get_layer(void); -void keymap_clear(void); -void keymap_set(uint16_t stat); -void keymap_move(uint8_t layer); -void keymap_on(uint8_t layer); -void keymap_off(uint8_t layer); -void keymap_invert(uint8_t layer); +extern uint32_t layer_state; +void layer_debug(void); +void layer_clear(void); +void layer_move(uint8_t layer); +void layer_on(uint8_t layer); +void layer_off(uint8_t layer); +void layer_invert(uint8_t layer); /* bitwise operation */ -void keymap_or(uint16_t stat); -void keymap_and(uint16_t stat); -void keymap_xor(uint16_t stat); -void keymap_debug(void); +void layer_or(uint32_t state); +void layer_and(uint32_t state); +void layer_xor(uint32_t state); #else -#define keymap_stat 0 -#define keymap_get_layer() -#define keymap_clear() -#define keymap_set(stat) -#define keymap_move(layer) -#define keymap_on(layer) -#define keymap_off(layer) -#define keymap_invert(layer) -#define keymap_or(stat) -#define keymap_and(stat) -#define keymap_xor(stat) -#define keymap_debug() +#define layer_state 0 +#define layer_clear() +#define layer_move(layer) +#define layer_on(layer) +#define layer_off(layer) +#define layer_invert(layer) + +#define layer_or(state) +#define layer_and(state) +#define layer_xor(state) +#define layer_debug() #endif diff --git a/common/util.c b/common/util.c index ff1926d7..6d4d6bfd 100644 --- a/common/util.c +++ b/common/util.c @@ -38,6 +38,14 @@ uint8_t bitpop16(uint16_t bits) return c; } +uint8_t bitpop32(uint32_t bits) +{ + uint8_t c; + for (c = 0; bits; c++) + bits &= bits - 1; + return c; +} + // most significant on-bit - return highest location of on-bit // NOTE: return 0 when bit0 is on or all bits are off uint8_t biton(uint8_t bits) @@ -58,3 +66,14 @@ uint8_t biton16(uint16_t bits) if (bits >> 1) { bits >>= 1; n += 1;} return n; } + +uint8_t biton32(uint32_t bits) +{ + uint8_t n = 0; + if (bits >>16) { bits >>=16; n +=16;} + if (bits >> 8) { bits >>= 8; n += 8;} + if (bits >> 4) { bits >>= 4; n += 4;} + if (bits >> 2) { bits >>= 2; n += 2;} + if (bits >> 1) { bits >>= 1; n += 1;} + return n; +} diff --git a/common/util.h b/common/util.h index 58b7fdf1..4b8b5ca3 100644 --- a/common/util.h +++ b/common/util.h @@ -30,7 +30,10 @@ along with this program. If not, see . uint8_t bitpop(uint8_t bits); uint8_t bitpop16(uint16_t bits); +uint8_t bitpop32(uint32_t bits); + uint8_t biton(uint8_t bits); uint8_t biton16(uint16_t bits); +uint8_t biton32(uint32_t bits); #endif