Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typedef'ed layer_state_t to uint32_t. #3637

Merged
merged 12 commits into from
May 17, 2019
4 changes: 2 additions & 2 deletions quantum/quantum.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
#include "send_string_keycodes.h"
#include "suspend.h"

extern uint32_t default_layer_state;
extern layer_state_t default_layer_state;

#ifndef NO_ACTION_LAYER
extern uint32_t layer_state;
extern layer_state_t layer_state;
#endif

#ifdef MIDI_ENABLE
Expand Down
8 changes: 4 additions & 4 deletions tmk_core/common/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ void process_action(keyrecord_t *record, action_t action)
/* 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;
layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift;
layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_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;
Expand All @@ -417,8 +417,8 @@ void process_action(keyrecord_t *record, action_t action)
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;
layer_state_t bits = ((layer_state_t)action.layer_bitop.bits)<<shift;
layer_state_t mask = (action.layer_bitop.xbit) ? ~(((layer_state_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;
Expand Down
36 changes: 18 additions & 18 deletions tmk_core/common/action_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

/** \brief Default Layer State
*/
uint32_t default_layer_state = 0;
layer_state_t default_layer_state = 0;

/** \brief Default Layer State Set At user Level
*
* Run user code on default layer state change
*/
__attribute__((weak))
uint32_t default_layer_state_set_user(uint32_t state) {
layer_state_t default_layer_state_set_user(layer_state_t state) {
return state;
}

Expand All @@ -29,15 +29,15 @@ uint32_t default_layer_state_set_user(uint32_t state) {
* Run keyboard code on default layer state change
*/
__attribute__((weak))
uint32_t default_layer_state_set_kb(uint32_t state) {
layer_state_t default_layer_state_set_kb(layer_state_t state) {
return default_layer_state_set_user(state);
}

/** \brief Default Layer State Set
*
* Static function to set the default layer state, prints debug info and clears keys
*/
static void default_layer_state_set(uint32_t state) {
static void default_layer_state_set(layer_state_t state) {
state = default_layer_state_set_kb(state);
debug("default_layer_state: ");
default_layer_debug(); debug(" to ");
Expand All @@ -62,7 +62,7 @@ void default_layer_debug(void) {
*
* Sets the default layer state.
*/
void default_layer_set(uint32_t state) {
void default_layer_set(layer_state_t state) {
default_layer_state_set(state);
}

Expand All @@ -71,21 +71,21 @@ void default_layer_set(uint32_t state) {
*
* Turns on the default layer based on matching bits between specifed layer and existing layer state
*/
void default_layer_or(uint32_t state) {
void default_layer_or(layer_state_t state) {
default_layer_state_set(default_layer_state | state);
}
/** \brief Default Layer And
*
* Turns on default layer based on matching enabled bits between specifed layer and existing layer state
*/
void default_layer_and(uint32_t state) {
void default_layer_and(layer_state_t state) {
default_layer_state_set(default_layer_state & state);
}
/** \brief Default Layer Xor
*
* Turns on default layer based on non-matching bits between specifed layer and existing layer state
*/
void default_layer_xor(uint32_t state) {
void default_layer_xor(layer_state_t state) {
default_layer_state_set(default_layer_state ^ state);
}
#endif
Expand All @@ -94,14 +94,14 @@ void default_layer_xor(uint32_t state) {
#ifndef NO_ACTION_LAYER
/** \brief Keymap Layer State
*/
uint32_t layer_state = 0;
layer_state_t layer_state = 0;

/** \brief Layer state set user
*
* Runs user code on layer state change
*/
__attribute__((weak))
uint32_t layer_state_set_user(uint32_t state) {
layer_state_t layer_state_set_user(layer_state_t state) {
return state;
}

Expand All @@ -110,15 +110,15 @@ uint32_t layer_state_set_user(uint32_t state) {
* Runs keyboard code on layer state change
*/
__attribute__((weak))
uint32_t layer_state_set_kb(uint32_t state) {
layer_state_t layer_state_set_kb(layer_state_t state) {
return layer_state_set_user(state);
}

/** \brief Layer state set
*
* Sets the layer to match the specifed state (a bitmask)
*/
void layer_state_set(uint32_t state) {
void layer_state_set(layer_state_t state) {
state = layer_state_set_kb(state);
dprint("layer_state: ");
layer_debug(); dprint(" to ");
Expand Down Expand Up @@ -151,7 +151,7 @@ bool layer_state_is(uint8_t layer) {
*
* Used for comparing layers {mostly used for unit testing}
*/
bool layer_state_cmp(uint32_t cmp_layer_state, uint8_t layer) {
bool layer_state_cmp(layer_state_t cmp_layer_state, uint8_t layer) {
if (!cmp_layer_state) { return layer == 0; }
return (cmp_layer_state & (1UL<<layer)) != 0;
}
Expand Down Expand Up @@ -192,21 +192,21 @@ void layer_invert(uint8_t layer) {
*
* Turns on layers based on matching bits between specifed layer and existing layer state
*/
void layer_or(uint32_t state) {
void layer_or(layer_state_t state) {
layer_state_set(layer_state | state);
}
/** \brief Layer and
*
* Turns on layers based on matching enabled bits between specifed layer and existing layer state
*/
void layer_and(uint32_t state) {
void layer_and(layer_state_t state) {
layer_state_set(layer_state & state);
}
/** \brief Layer xor
*
* Turns on layers based on non-matching bits between specifed layer and existing layer state
*/
void layer_xor(uint32_t state) {
void layer_xor(layer_state_t state) {
layer_state_set(layer_state ^ state);
}

Expand Down Expand Up @@ -301,9 +301,9 @@ uint8_t layer_switch_get_layer(keypos_t key) {
action_t action;
action.code = ACTION_TRANSPARENT;

uint32_t layers = layer_state | default_layer_state;
layer_state_t layers = layer_state | default_layer_state;
/* check top layer first */
for (int8_t i = 31; i >= 0; i--) {
for (int8_t i = sizeof(layer_state_t)-1; i >= 0; i--) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably the issue. sizeof(layer_state_t)-1 returns 3, not 31 as this is the size in bytes of the type. This should be sizeof(layer_state_t) * 8 - 1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can confirm changing this as described fixes layer switching.

if (layers & (1UL << i)) {
action = action_for_key(i, key);
if (action.code != ACTION_TRANSPARENT) {
Expand Down
38 changes: 23 additions & 15 deletions tmk_core/common/action_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,32 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "keyboard.h"
#include "action.h"

#if defined(LAYER_STATE_8BIT) || ( defined(DYNAMIC_KEYMAP_ENABLE) && DYNAMIC_KEYMAP_LAYER_COUNT >= 8 )
typedef uint8_t layer_state_t;
#elif defined(LAYER_STATE_16BIT)
typedef uint16_t layer_state_t;
#else
typedef uint32_t layer_state_t;
alex-ong marked this conversation as resolved.
Show resolved Hide resolved
#endif


/*
* Default Layer
*/
extern uint32_t default_layer_state;
extern layer_state_t default_layer_state;
void default_layer_debug(void);
void default_layer_set(uint32_t state);
void default_layer_set(layer_state_t state);

__attribute__((weak))
uint32_t default_layer_state_set_kb(uint32_t state);
layer_state_t default_layer_state_set_kb(layer_state_t state);
__attribute__((weak))
uint32_t default_layer_state_set_user(uint32_t state);
layer_state_t default_layer_state_set_user(layer_state_t state);

#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);
void default_layer_or(layer_state_t state);
void default_layer_and(layer_state_t state);
void default_layer_xor(layer_state_t state);
#else
#define default_layer_or(state)
#define default_layer_and(state)
Expand All @@ -50,11 +58,11 @@ void default_layer_xor(uint32_t state);
* Keymap Layer
*/
#ifndef NO_ACTION_LAYER
extern uint32_t layer_state;
extern layer_state_t layer_state;

void layer_state_set(uint32_t state);
void layer_state_set(layer_state_t state);
bool layer_state_is(uint8_t layer);
bool layer_state_cmp(uint32_t layer1, uint8_t layer2);
bool layer_state_cmp(layer_state_t layer1, uint8_t layer2);

void layer_debug(void);
void layer_clear(void);
Expand All @@ -63,9 +71,9 @@ void layer_on(uint8_t layer);
void layer_off(uint8_t layer);
void layer_invert(uint8_t layer);
/* bitwise operation */
void layer_or(uint32_t state);
void layer_and(uint32_t state);
void layer_xor(uint32_t state);
void layer_or(layer_state_t state);
void layer_and(layer_state_t state);
void layer_xor(layer_state_t state);
#else
#define layer_state 0

Expand All @@ -84,8 +92,8 @@ void layer_xor(uint32_t state);
#define layer_xor(state)
#endif

uint32_t layer_state_set_user(uint32_t state);
uint32_t layer_state_set_kb(uint32_t state);
layer_state_t layer_state_set_user(layer_state_t state);
layer_state_t layer_state_set_kb(layer_state_t state);

/* pressed actions cache */
#if !defined(NO_ACTION_LAYER) && !defined(STRICT_LAYER_RELEASE)
Expand Down
4 changes: 2 additions & 2 deletions tmk_core/common/bootmagic.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ void bootmagic(void)
if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) { default_layer |= (1<<7); }
if (default_layer) {
eeconfig_update_default_layer(default_layer);
default_layer_set((uint32_t)default_layer);
default_layer_set((layer_state_t)default_layer);
} else {
default_layer = eeconfig_read_default_layer();
default_layer_set((uint32_t)default_layer);
default_layer_set((layer_state_t)default_layer);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tmk_core/common/magic.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ void magic(void)

uint8_t default_layer = 0;
default_layer = eeconfig_read_default_layer();
default_layer_set((uint32_t)default_layer);
default_layer_set((layer_state_t)default_layer);

}