summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-04-02 16:09:43 +0900
committertmk <nobody@nowhere>2013-04-02 16:09:43 +0900
commitcc8e66754b1a5d0c11985cb0feb51ead49668744 (patch)
treeb2c7aa7bf5a2ed20650febc6b25fbbd3d773fe10 /common
parent10f33a3e484e24065ed5eaab1c56c35156bace89 (diff)
Refine ACT_LAYER and ACT_LAYER_TAP
- Remove ACT_LAYER_BITOP
Diffstat (limited to 'common')
-rw-r--r--common/action.c201
-rw-r--r--common/action.h166
-rw-r--r--common/action_tapping.h3
-rw-r--r--common/command.c3
-rw-r--r--common/layer_switch.c122
-rw-r--r--common/layer_switch.h61
-rw-r--r--common/util.c19
-rw-r--r--common/util.h3
8 files changed, 263 insertions, 315 deletions
diff --git a/common/action.c b/common/action.c
index ef04851b11..596831d4de 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)<<shift;
+ uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
+ switch (action.layer_bitop.op) {
+ case OP_BIT_AND: default_layer_and(bits | mask); break;
+ case OP_BIT_OR: default_layer_or(bits | mask); break;
+ case OP_BIT_XOR: default_layer_xor(bits | mask); break;
+ case OP_BIT_SET: default_layer_and(mask); default_layer_or(bits); break;
}
- break;
- /* Keymap Reset default layer */
- case (OP_RESET | ON_PRESS):
- if (event.pressed) {
- default_layer_set(action.layer.val);
- }
- break;
- case (OP_RESET | ON_RELEASE):
- if (!event.pressed) {
- default_layer_set(action.layer.val);
+ }
+ } else {
+ /* Layer Bitwise Operation */
+ if (event.pressed ? (action.layer_bitop.on & ON_PRESS) :
+ (action.layer_bitop.on & ON_RELEASE)) {
+ uint8_t shift = action.layer_bitop.part*4;
+ uint32_t bits = ((uint32_t)action.layer_bitop.bits)<<shift;
+ uint32_t mask = (action.layer_bitop.xbit) ? ~(((uint32_t)0xf)<<shift) : 0;
+ switch (action.layer_bitop.op) {
+ case OP_BIT_AND: layer_and(bits | mask); break;
+ case OP_BIT_OR: layer_or(bits | mask); break;
+ case OP_BIT_XOR: layer_xor(bits | mask); break;
+ case OP_BIT_SET: layer_and(mask); layer_or(bits); break;
}
- break;
- case (OP_RESET | ON_BOTH):
- default_layer_set(action.layer.val);
- break;
-
- /* Keymap Bit invert */
- case OP_INV:
- /* with tap toggle */
+ }
+ }
+ break;
+ #ifndef NO_ACTION_TAPPING
+ case ACT_LAYER_TAP:
+ case ACT_LAYER_TAP1:
+ switch (action.layer_tap.code) {
+ case OP_TAP_TOGGLE:
+ /* tap toggle */
if (event.pressed) {
if (tap_count < TAPPING_TOGGLE) {
- debug("KEYMAP_INV: tap toggle(press).\n");
- keymap_invert(action.layer.val);
+ layer_invert(action.layer_tap.val);
}
} else {
if (tap_count <= TAPPING_TOGGLE) {
- debug("KEYMAP_INV: tap toggle(release).\n");
- keymap_invert(action.layer.val);
+ layer_invert(action.layer_tap.val);
}
}
break;
- case (OP_INV | ON_PRESS):
- if (event.pressed) {
- keymap_invert(action.layer.val);
- }
+ case OP_ON_OFF:
+ event.pressed ? layer_on(action.layer_tap.val) :
+ layer_off(action.layer_tap.val);
break;
- case (OP_INV | ON_RELEASE):
- if (!event.pressed) {
- keymap_invert(action.layer.val);
- }
+ case OP_OFF_ON:
+ event.pressed ? layer_off(action.layer_tap.val) :
+ layer_on(action.layer_tap.val);
break;
- case (OP_INV | ON_BOTH):
- keymap_invert(action.layer.val);
+ case OP_SET_CLEAR:
+ event.pressed ? layer_move(action.layer_tap.val) :
+ layer_clear();
break;
-
- /* Keymap Bit on */
- case OP_ON:
- if (event.pressed) {
- keymap_on(action.layer.val);
- } else {
- keymap_off(action.layer.val);
- }
- break;
- case (OP_ON | ON_PRESS):
- if (event.pressed) {
- keymap_on(action.layer.val);
- }
- break;
- case (OP_ON | ON_RELEASE):
- if (!event.pressed) {
- keymap_on(action.layer.val);
- }
- break;
- case (OP_ON | ON_BOTH):
- keymap_on(action.layer.val);
- break;
-
- /* Keymap Bit off */
- case OP_OFF:
- if (event.pressed) {
- keymap_off(action.layer.val);
- } else {
- keymap_on(action.layer.val);
- }
- break;
- case (OP_OFF | ON_PRESS):
- if (event.pressed) {
- keymap_off(action.layer.val);
- }
- break;
- case (OP_OFF | ON_RELEASE):
- if (!event.pressed) {
- keymap_off(action.layer.val);
- }
- break;
- case (OP_OFF | ON_BOTH):
- keymap_off(action.layer.val);
- break;
-
- /* Keymap Bit set */
- case OP_SET:
- if (event.pressed) {
- keymap_set(action.layer.val);
- } else {
- keymap_clear();
- }
- break;
- case (OP_SET | ON_PRESS):
- if (event.pressed) {
- keymap_set(action.layer.val);
- }
- break;
- case (OP_SET | ON_RELEASE):
- if (!event.pressed) {
- keymap_set(action.layer.val);
- }
- break;
- case (OP_SET | ON_BOTH):
- keymap_set(action.layer.val);
- break;
-
- /* Keymap Bit invert with tap key */
default:
+ /* tap key */
if (event.pressed) {
if (tap_count > 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 2c4f306a44..4daae1d04f 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<<L]
- * 100X|LLLL|1111 0100 On/Off
- * 100X|LLLL|1111 01xx On[|= 1<<L]
- * 100X|LLLL|1111 1000 Off/On
- * 100X|LLLL|1111 10xx Off[&= ~(1<<L)]
- * 100X|LLLL|1111 1100 Set/Clear
- * 100X|LLLL|1111 11xx Set[= 1<<L]
- * XLLLL: Layer 0-31
- * xx: On {00:for special use, 01:press, 10:release, 11:both}
+ * ACT_LAYER:
+ * 1000|oo00|pppx BBBB Default Layer Bitwise operation
+ * oo: operation(00:AND, 01:OR, 10:XOR, 11:SET)
+ * ppp: 4-bit chunk part(0-7)
+ * xBBBB: bits and extra bit
+ * 1000|ooee|pppx BBBB Layer Bitwise Operation
+ * oo: operation(00:AND, 01:OR, 10:XOR, 11:SET)
+ * ppp: 4-bit chunk part(0-7)
+ * xBBBB: bits and extra bit
+ * ee: on event(00:default layer, 01:press, 10:release, 11:both)
+ *
+ * ACT_LAYER_TAP:
+ * 101x|LLLL| keycode Invert with tap key
+ * 101x|LLLL|1110 xxxx Reserved(0xE0-EF)
+ * 101x|LLLL|1111 0000 Invert with tap toggle(0xF0)
+ * 101x|LLLL|1111 0001 On Off
+ * 101x|LLLL|1111 0010 Off On
+ * 101x|LLLL|1111 0011 Set Clear
+ * 101x|LLLL|1111 xxxx Reserved(0xF4-FF)
+ * xLLLL: layer(0-31)
*
- * ACT_LAYER_BITOP:
- * 101B|Booo|xxxx xxxx bit operation
- * BB: operand. which part of layer state bits
- * 00: 0-7th bit
- * 01: 8-15th bit
- * 10: 16-23th bit
- * 11: 24-31th bit
- * ooo: operation.
- * 000: AND
- * 001: OR
- * 010: XOR
- * 011:
- * 100: LSHIFT
- * 101: RSHIFT
- * 110:
- * 111:
- * bbbb bbbb: bits
- * layer_state |= (((layer_state>>(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<<layer)
- * On layer |= (1<<layer)
- * Off layer &= ~(1<<layer)
- * Set layer = (1<<layer)
- * Clear layer = 0
- */
+/* Layer Actions */
enum layer_param_on {
ON_PRESS = 1,
ON_RELEASE = 2,
ON_BOTH = 3,
};
-enum layer_pram_op {
- OP_RESET = 0x00,
- OP_INV4 = 0x00,
- OP_INV = 0xF0,
- OP_ON = 0xF4,
- OP_OFF = 0xF8,
- OP_SET = 0xFC,
+enum layer_param_op {
+ OP_DEFAULT_LAYER = 0,
};
-enum layer_pram_bitop {
- BITOP_AND,
- BITOP_OR,
- BITOP_XOR,
- BITOP_LSHIFT,
- BITOP_RSHIFT,
+enum layer_param_bit_op {
+ OP_BIT_AND = 0,
+ OP_BIT_OR,
+ OP_BIT_XOR,
+ OP_BIT_SET,
};
-/*
- * Default Layer
- */
-#define ACTION_DEFAULT_LAYER ACTION(ACT_LAYER, ON_RELEASE<<8 | OP_RESET | 0)
-#define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER_TO(layer, ON_RELEASE)
-#define ACTION_DEFAULT_LAYER_TO(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_RESET | (on))
+enum layer_pram_tap_op {
+ OP_TAP_TOGGLE = 0xF0,
+ OP_ON_OFF,
+ OP_OFF_ON,
+ OP_SET_CLEAR,
+};
-/*
- * Keymap Layer
- */
+/* Layer Operation 1000|ee00|ooov vvvv */
+#define ACTION_LAYER(op, val, on) (ACT_LAYER<<12 | (on)<<10 | (op)<<5 | val)
+/* Layer Bitwise Operation 1000|ooee|pppx BBBB */
+#define ACTION_LAYER_BITOP(op, part, bits, on) (ACT_LAYER<<12 | (op)<<10 | (on)<<8 | (part)<<5 | (bits)&0x1f)
+/* Layer with Tapping 101x|LLLL| keycode */
+#define ACTION_LAYER_TAP(layer, key) (ACT_LAYER_TAP<<12 | (layer)<<8 | (key))
+
+/* Default Layer Operation */
+#define ACTION_DEFAULT_LAYER_SET(layer) ACTION_DEFAULT_LAYER(layer, ON_RELEASE)
+#define ACTION_DEFAULT_LAYER(layer, on) ACTION_LAYER(OP_DEFAULT_LAYER, layer, on)
+/* Layer Operation */
+#define ACTION_LAYER_CLEAR(on) ACTION_LAYER_AND(0x1f, (on))
#define ACTION_LAYER_MOMENTARY(layer) ACTION_LAYER_ON_OFF(layer)
-#define ACTION_LAYER_TOGGLE(layer) ACTION_LAYER_INV(layer, ON_RELEASE)
-/* Keymap Invert */
-#define ACTION_LAYER_INV(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_INV | (on))
-#define ACTION_LAYER_TAP_TOGGLE(layer) ACTION(ACT_LAYER, (layer)<<8 | OP_INV | 0)
-/* Keymap On */
-#define ACTION_LAYER_ON(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_ON | (on))
-#define ACTION_LAYER_ON_OFF(layer) ACTION(ACT_LAYER, (layer)<<8 | OP_ON | 0)
-/* Keymap Off */
-#define ACTION_LAYER_OFF(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_OFF | (on))
-#define ACTION_LAYER_OFF_ON(layer) ACTION(ACT_LAYER, (layer)<<8 | OP_OFF | 0)
-/* Keymap Set */
-#define ACTION_LAYER_SET(layer, on) ACTION(ACT_LAYER, (layer)<<8 | OP_SET | (on))
-#define ACTION_LAYER_SET_CLEAR(layer) ACTION(ACT_LAYER, (layer)<<8 | OP_SET | 0)
-/* Keymap Invert with tap key */
-#define ACTION_LAYER_TAP_KEY(layer, key) ACTION(ACT_LAYER, (layer)<<8 | (key))
-
-/* Layer BitOp: 101|BB|ooo|xxxxxxxx */
-#define ACTION_LAYER_BITOP(op, part, bits) (ACT_LAYER_BITOP<<12 | (part&0x3)<<11 | (op&0x7)<<8 | bits)
-#define ACTION_LAYER_AND(part, bits) ACTION_LAYER_BITOP(BITOP_AND, part, bits)
-#define ACTION_LAYER_OR(part, bits) ACTION_LAYER_BITOP(BITOP_OR, part, bits)
-#define ACTION_LAYER_XOR(part, bits) ACTION_LAYER_BITOP(BITOP_XOR, part, bits)
-#define ACTION_LAYER_LSHIFT(part, bits) ACTION_LAYER_BITOP(BITOP_LSHIFT, part, bits)
-#define ACTION_LAYER_RSHIFT(part, bits) ACTION_LAYER_BITOP(BITOP_RSHIFT, part, bits)
-
+#define ACTION_LAYER_TOGGLE(layer) ACTION_LAYER_INVERT(layer, ON_RELEASE)
+#define ACTION_LAYER_INVERT(layer, on) ACTION_LAYER_BIT_XOR((layer)/4, 1<<((layer)%4), (on))
+#define ACTION_LAYER_ON(layer, on) ACTION_LAYER_BIT_OR((layer)/4, 1<<((layer)%4), (on))
+#define ACTION_LAYER_OFF(layer, on) ACTION_LAYER_BIT_AND((layer)/4, ~(1<<((layer)%4)), (on))
+#define ACTION_LAYER_SET(layer, on) ACTION_LAYER_BIT_SET((layer)/4, 1<<((layer)%4), (on))
+#define ACTION_LAYER_ON_OFF(layer) ACTION_LAYER_TAP((layer), OP_ON_OFF)
+#define ACTION_LAYER_OFF_ON(layer) ACTION_LAYER_TAP((layer), OP_OFF_ON)
+#define ACTION_LAYER_SET_CLEAR(layer) ACTION_LAYER_TAP((layer), OP_SET_CLEAR)
+/* Bitwise Operation */
+#define ACTION_LAYER_BIT_AND(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_AND, part, bits)
+#define ACTION_LAYER_BIT_OR(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_OR, part, bits)
+#define ACTION_LAYER_BIT_XOR(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_XOR, part, bits)
+#define ACTION_LAYER_BIT_SET(part, bits, on) ACTION_LAYER_BITOP(OP_BIT_SET, part, bits)
+/* with Tapping */
+#define ACTION_LAYER_TAP_KEY(layer, key) ACTION_LAYER_TAP((layer), (key))
+#define ACTION_LAYER_TAP_TOGGLE(layer) ACTION_LAYER_TAP((layer), OP_TAP_TOGGLE)
+
/*
* Extensions
diff --git a/common/action_tapping.h b/common/action_tapping.h
index c9f09f576d..9b42d50dc3 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/>.
#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 <http://www.gnu.org/licenses/>.
#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 e197a8f80a..c954ff02f4 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 359e6b9d84..9905741f48 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;
-void default_layer_set(uint8_t layer)
+static void default_layer_state_set(uint32_t state)
{
- debug("default_layer_set: ");
- debug_dec(default_layer); debug(" to ");
-
- default_layer = layer;
-
- debug_dec(default_layer); debug("\n");
-
+ 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
}
-
-#ifndef NO_ACTION_LAYER
-/*
- * Keymap Layer (0-15)
- */
-uint16_t keymap_stat = 0;
-
-/* return highest layer whose state is on */
-uint8_t keymap_get_layer(void)
+void default_layer_debug(void)
{
- return biton16(keymap_stat);
+ debug_hex32(default_layer_state);
+ debug("("); debug_dec(biton32(default_layer_state)); debug(")");
}
-static void keymap_stat_set(uint16_t stat)
+void default_layer_set(uint8_t layer)
{
- debug("keymap: ");
- keymap_debug(); debug(" to ");
+ default_layer_state_set(1UL<<layer);
+}
- keymap_stat = stat;
+#ifndef NO_ACTION_LAYER
+void default_layer_or(uint32_t state)
+{
+ default_layer_state_set(default_layer_state | state);
+}
+void default_layer_and(uint32_t state)
+{
+ default_layer_state_set(default_layer_state & state);
+}
+void default_layer_xor(uint32_t state)
+{
+ default_layer_state_set(default_layer_state ^ state);
+}
+#endif
- keymap_debug(); debug("\n");
- clear_keyboard_but_mods(); // To avoid stuck keys
-}
+#ifndef NO_ACTION_LAYER
+/*
+ * Keymap Layer State
+ */
+uint32_t layer_state = 0;
-void keymap_clear(void)
+static void layer_state_set(uint32_t state)
{
- keymap_stat_set(0);
+ debug("layer_state: ");
+ layer_debug(); debug(" to ");
+ layer_state = state;
+ layer_debug(); debug("\n");
+ clear_keyboard_but_mods(); // To avoid stuck keys
}
-
-void keymap_set(uint16_t stat)
+void layer_clear(void)
{
- keymap_stat_set(stat);
+ layer_state_set(0);
}
-void keymap_move(uint8_t layer)
+void layer_move(uint8_t layer)
{
- keymap_stat_set(1<<layer);
+ layer_state_set(1UL<<layer);
}
-void keymap_on(uint8_t layer)
+void layer_on(uint8_t layer)
{
- keymap_stat_set(keymap_stat | (1<<layer));
+ layer_state_set(layer_state | (1UL<<layer));
}
-void keymap_off(uint8_t layer)
+void layer_off(uint8_t layer)
{
- keymap_stat_set(keymap_stat & ~(1<<layer));
+ layer_state_set(layer_state & ~(1UL<<layer));
}
-void keymap_invert(uint8_t layer)
+void layer_invert(uint8_t layer)
{
- keymap_stat_set(keymap_stat ^ (1<<layer));
+ layer_state_set(layer_state ^ (1UL<<layer));
}
-void keymap_or(uint16_t stat)
+void layer_or(uint32_t state)
{
- keymap_stat_set(keymap_stat | stat);
+ layer_state_set(layer_state | state);
}
-void keymap_and(uint16_t stat)
+void layer_and(uint32_t state)
{
- keymap_stat_set(keymap_stat & stat);
+ layer_state_set(layer_state & state);
}
-void keymap_xor(uint16_t stat)
+void layer_xor(uint32_t state)
{
- keymap_stat_set(keymap_stat ^ stat);
+ layer_state_set(layer_state ^ state);
}
-void keymap_debug(void)
+void layer_debug(void)
{
- debug_hex16(keymap_stat); debug("("); debug_dec(keymap_get_layer()); debug(")");
+ debug_hex32(layer_state);
+ debug("("); debug_dec(biton32(layer_state)); debug(")");
}
#endif
@@ -106,18 +115,21 @@ action_t layer_switch_get_action(key_t key)
action.code = ACTION_TRANSPARENT;
#ifndef NO_ACTION_LAYER
- /* keymap: top layer first */
- for (int8_t i = 15; i >= 0; i--) {
- if (keymap_stat & (1<<i)) {
+ uint32_t layers = layer_state | default_layer_state;
+ /* check top layer first */
+ for (int8_t i = 31; i >= 0; i--) {
+ if (layers & (1UL<<i)) {
action = action_for_key(i, key);
if (action.code != ACTION_TRANSPARENT) {
return action;
}
}
}
-#endif
-
- /* default layer */
- action = action_for_key(default_layer, key);
+ /* fall back to layer 0 */
+ action = action_for_key(0, key);
+ return action;
+#else
+ action = action_for_key(biton32(default_layer_state), key);
return action;
+#endif
}
diff --git a/common/layer_switch.h b/common/layer_switch.h
index 423dafb5b8..ed8dfb5025 100644
--- a/common/layer_switch.h
+++ b/common/layer_switch.h
@@ -25,42 +25,49 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
/*
* 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 ff1926d7d1..6d4d6bfda1 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 58b7fdf145..4b8b5ca3a4 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/>.
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