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

Allow QMK to save a single default layers in the full range of 0-31 #22933

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/feature_layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ Layers stack on top of each other in numerical order. When determining what a ke

Sometimes, you might want to switch between layers in a macro or as part of a tap dance routine. `layer_on` activates a layer, and `layer_off` deactivates it. More layer-related functions can be found in [action_layer.h](https://github.com/qmk/qmk_firmware/blob/master/quantum/action_layer.h).

If you want to save multiple persistent default layers, you will want to avoid using the `set_single_persistent_default_layer` function which only supports one default persistent layer at a time. Instead, you can `#define DEFAULT_LAYER_BITMASK_ENABLE` and call the `eeconfig_update_default_layer` function directly, passing it an 8-bit wide bitfield representing the least significant byte of a layer_state_t variable. Note that this approach is limited to storing persistent default layers only between layers 0 and 7.

## Functions :id=functions

There are a number of functions (and variables) related to how you can use or manipulate the layers.
Expand Down
6 changes: 5 additions & 1 deletion keyboards/satt/comet46/keymaps/default-rgbled/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ void matrix_init_user(void) {

void matrix_scan_user(void) {
uint8_t layer = get_highest_layer(layer_state);
uint8_t default_layer = biton32(eeconfig_read_default_layer());
#ifdef DEFAULT_LAYER_BITMASK_ENABLE
uint8_t default_layer = get_highest_layer(eeconfig_read_default_layer());
#else
uint8_t default_layer = eeconfig_read_default_layer();
#endif
switch (layer) {
case _LOWER:
set_led_red;
Expand Down
4 changes: 4 additions & 0 deletions keyboards/satt/comet46/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ bool oled_task_user(void) {
char layer_str[22];
oled_write_P(PSTR("Layer: "), false);
uint8_t layer = get_highest_layer(layer_state);
#ifdef DEFAULT_LAYER_BITMASK_ENABLE
uint8_t default_layer = get_highest_layer(eeconfig_read_default_layer());
#else
uint8_t default_layer = eeconfig_read_default_layer();
#endif
switch (layer) {
case _QWERTY:
switch (default_layer) {
Expand Down
4 changes: 4 additions & 0 deletions quantum/eeconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ void eeconfig_init_quantum(void) {

eeprom_update_word(EECONFIG_MAGIC, EECONFIG_MAGIC_NUMBER);
eeprom_update_byte(EECONFIG_DEBUG, 0);
#if defined(DEFAULT_LAYER_BITMASK_ENABLE)
default_layer_state = (layer_state_t)1 << 0;
#else
default_layer_state = 0;
#endif
eeprom_update_byte(EECONFIG_DEFAULT_LAYER, default_layer_state);
Comment on lines +54 to 59
Copy link
Member

Choose a reason for hiding this comment

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

Setting the global default_layer_state to zero would be incorrect as its expected to be a bitmask of enabled layers, and should probably look more like...

Suggested change
#if defined(DEFAULT_LAYER_BITMASK_ENABLE)
default_layer_state = (layer_state_t)1 << 0;
#else
default_layer_state = 0;
#endif
eeprom_update_byte(EECONFIG_DEFAULT_LAYER, default_layer_state);
default_layer_state = (layer_state_t)1 << 0;
#if defined(DEFAULT_LAYER_BITMASK_ENABLE)
eeprom_update_byte(EECONFIG_DEFAULT_LAYER, default_layer_state);
#else
eeprom_update_byte(EECONFIG_DEFAULT_LAYER, 0);
#endif

// Enable oneshot and autocorrect by default: 0b0001 0100 0000 0000
eeprom_update_word(EECONFIG_KEYMAP, 0x1400);
Expand Down
6 changes: 6 additions & 0 deletions quantum/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,13 @@ void quantum_init(void) {
#endif

/* read here just incase bootmagic process changed its value */
#if defined(DEFAULT_LAYER_BITMASK_ENABLE)
/* stored as 8-bit-wide bitmask, so write the value directly to default_layer */
layer_state_t default_layer = (layer_state_t)eeconfig_read_default_layer();
#else
/* stored as a layer number, so left shift 1 by the stored value */
layer_state_t default_layer = (layer_state_t)(1UL << eeconfig_read_default_layer());
#endif
default_layer_set(default_layer);

/* Also initialize layer state to trigger callback functions for layer_state */
Expand Down
4 changes: 4 additions & 0 deletions quantum/quantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ void set_single_persistent_default_layer(uint8_t default_layer) {
#if defined(AUDIO_ENABLE) && defined(DEFAULT_LAYER_SONGS)
PLAY_SONG(default_layer_songs[default_layer]);
#endif
#if defined(DEFAULT_LAYER_BITMASK_ENABLE)
eeconfig_update_default_layer((layer_state_t)1 << default_layer);
#else
eeconfig_update_default_layer(default_layer);
#endif
default_layer_set((layer_state_t)1 << default_layer);
}

Expand Down
Loading