From 4a5bae51cd1f2cbca0ab8c8ae6e15262f8dc5a68 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Sat, 23 Nov 2024 10:09:34 +0000 Subject: [PATCH] [Feature] Add keycode PDF(layer) to set the default layer in EEPROM (#24630) * [Feature] Add keycode PDF(layer) to set the default layer in EEPROM (#21881) * Apply suggestions from code review Co-authored-by: Nick Brassel --------- Co-authored-by: Nebuleon <2391500+Nebuleon@users.noreply.github.com> Co-authored-by: Nick Brassel --- builddefs/common_features.mk | 1 + data/constants/keycodes/keycodes_0.0.6.hjson | 7 ++++ docs/feature_layers.md | 3 +- docs/keycodes.md | 3 +- keyboards/zsa/moonlander/moonlander.c | 1 + keyboards/zsa/planck_ez/planck_ez.c | 1 + quantum/keycodes.h | 3 ++ .../pointing_device_auto_mouse.c | 2 ++ quantum/process_keycode/process_autocorrect.c | 1 + .../process_keycode/process_default_layer.c | 32 +++++++++++++++++++ .../process_keycode/process_default_layer.h | 27 ++++++++++++++++ quantum/quantum.c | 7 ++++ quantum/quantum_keycodes.h | 4 +++ tests/test_common/keycode_util.cpp | 2 ++ 14 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 quantum/process_keycode/process_default_layer.c create mode 100644 quantum/process_keycode/process_default_layer.h diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index 86ced30902..7bfd1436e7 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -28,6 +28,7 @@ QUANTUM_SRC += \ $(QUANTUM_DIR)/sync_timer.c \ $(QUANTUM_DIR)/logging/debug.c \ $(QUANTUM_DIR)/logging/sendchar.c \ + $(QUANTUM_DIR)/process_keycode/process_default_layer.c \ VPATH += $(QUANTUM_DIR)/logging # Fall back to lib/printf if there is no platform provided print diff --git a/data/constants/keycodes/keycodes_0.0.6.hjson b/data/constants/keycodes/keycodes_0.0.6.hjson index e69de29bb2..f03ef747a4 100644 --- a/data/constants/keycodes/keycodes_0.0.6.hjson +++ b/data/constants/keycodes/keycodes_0.0.6.hjson @@ -0,0 +1,7 @@ +{ + "ranges": { + "0x52E0/0x001F": { + "define": "QK_PERSISTENT_DEF_LAYER" + } + } +} diff --git a/docs/feature_layers.md b/docs/feature_layers.md index 45f02fe536..da6a28bd88 100644 --- a/docs/feature_layers.md +++ b/docs/feature_layers.md @@ -8,7 +8,8 @@ For a detailed explanation of how the layer stack works, checkout [Keymap Overvi These functions allow you to activate layers in various ways. Note that layers are not generally independent layouts -- multiple layers can be activated at once, and it's typical for layers to use `KC_TRNS` to allow keypresses to pass through to lower layers. When using momentary layer switching with MO(), LM(), TT(), or LT(), make sure to leave the key on the above layers transparent or it may not work as intended. -* `DF(layer)` - switches the default layer. The default layer is the always-active base layer that other layers stack on top of. See below for more about the default layer. This might be used to switch from QWERTY to Dvorak layout. (Note that this is a temporary switch that only persists until the keyboard loses power. To modify the default layer in a persistent way requires deeper customization, such as calling the `set_single_persistent_default_layer` function inside of [process_record_user](custom_quantum_functions#programming-the-behavior-of-any-keycode).) +* `DF(layer)` - switches the default layer. The default layer is the always-active base layer that other layers stack on top of. See below for more about the default layer. This might be used to switch from QWERTY to Dvorak layout. Note that this is a temporary switch that only persists until the keyboard loses power. +* `PDF(layer)` - sets a persistent default layer. This switch, which will last through a power loss, might be used to switch from QWERTY to Dvorak layout and only switch again when you want to. * `MO(layer)` - momentarily activates *layer*. As soon as you let go of the key, the layer is deactivated. * `LM(layer, mod)` - Momentarily activates *layer* (like `MO`), but with modifier(s) *mod* active. Only supports layers 0-15. The modifiers this keycode accept are prefixed with `MOD_`, not `KC_`. These modifiers can be combined using bitwise OR, e.g. `LM(_RAISE, MOD_LCTL | MOD_LALT)`. * `LT(layer, kc)` - momentarily activates *layer* when held, and sends *kc* when tapped. Only supports layers 0-15. diff --git a/docs/keycodes.md b/docs/keycodes.md index cae6418266..cf170721c8 100644 --- a/docs/keycodes.md +++ b/docs/keycodes.md @@ -401,7 +401,8 @@ See also: [Layer Switching](feature_layers#switching-and-toggling-layers) |Key |Description | |----------------|----------------------------------------------------------------------------------| -|`DF(layer)` |Set the base (default) layer | +|`DF(layer)` |Set the base (default) layer until the keyboard loses power | +|`PDF(layer)` |Set the base (default) layer in EEPROM | |`MO(layer)` |Momentarily turn on `layer` when pressed (requires `KC_TRNS` on destination layer)| |`OSL(layer)` |Momentarily activates `layer` until a key is pressed. See [One Shot Keys](one_shot_keys) for details. | |`LM(layer, mod)`|Momentarily turn on `layer` (like MO) with `mod` active as well. Where `mod` is a mods_bit. Mods can be viewed [here](mod_tap). Example Implementation: `LM(LAYER_1, MOD_LALT)`| diff --git a/keyboards/zsa/moonlander/moonlander.c b/keyboards/zsa/moonlander/moonlander.c index 41b83fd9d0..999fcbe1dd 100644 --- a/keyboards/zsa/moonlander/moonlander.c +++ b/keyboards/zsa/moonlander/moonlander.c @@ -275,6 +275,7 @@ bool music_mask_kb(uint16_t keycode) { case QK_TO ... QK_TO_MAX: case QK_MOMENTARY ... QK_MOMENTARY_MAX: case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: + case QK_PERSISTENT_DEF_LAYER ... QK_PERSISTENT_DEF_LAYER_MAX: case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX: case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: diff --git a/keyboards/zsa/planck_ez/planck_ez.c b/keyboards/zsa/planck_ez/planck_ez.c index a3f6c7362e..ff82f43c66 100644 --- a/keyboards/zsa/planck_ez/planck_ez.c +++ b/keyboards/zsa/planck_ez/planck_ez.c @@ -237,6 +237,7 @@ bool music_mask_kb(uint16_t keycode) { case QK_TO ... QK_TO_MAX: case QK_MOMENTARY ... QK_MOMENTARY_MAX: case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: + case QK_PERSISTENT_DEF_LAYER ... QK_PERSISTENT_DEF_LAYER_MAX: case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX: case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: diff --git a/quantum/keycodes.h b/quantum/keycodes.h index e9da5105ce..921dc7199f 100644 --- a/quantum/keycodes.h +++ b/quantum/keycodes.h @@ -52,6 +52,8 @@ enum qk_keycode_ranges { QK_ONE_SHOT_MOD_MAX = 0x52BF, QK_LAYER_TAP_TOGGLE = 0x52C0, QK_LAYER_TAP_TOGGLE_MAX = 0x52DF, + QK_PERSISTENT_DEF_LAYER = 0x52E0, + QK_PERSISTENT_DEF_LAYER_MAX = 0x52FF, QK_SWAP_HANDS = 0x5600, QK_SWAP_HANDS_MAX = 0x56FF, QK_TAP_DANCE = 0x5700, @@ -1462,6 +1464,7 @@ enum qk_keycode_defines { #define IS_QK_ONE_SHOT_LAYER(code) ((code) >= QK_ONE_SHOT_LAYER && (code) <= QK_ONE_SHOT_LAYER_MAX) #define IS_QK_ONE_SHOT_MOD(code) ((code) >= QK_ONE_SHOT_MOD && (code) <= QK_ONE_SHOT_MOD_MAX) #define IS_QK_LAYER_TAP_TOGGLE(code) ((code) >= QK_LAYER_TAP_TOGGLE && (code) <= QK_LAYER_TAP_TOGGLE_MAX) +#define IS_QK_PERSISTENT_DEF_LAYER(code) ((code) >= QK_PERSISTENT_DEF_LAYER && (code) <= QK_PERSISTENT_DEF_LAYER_MAX) #define IS_QK_SWAP_HANDS(code) ((code) >= QK_SWAP_HANDS && (code) <= QK_SWAP_HANDS_MAX) #define IS_QK_TAP_DANCE(code) ((code) >= QK_TAP_DANCE && (code) <= QK_TAP_DANCE_MAX) #define IS_QK_MAGIC(code) ((code) >= QK_MAGIC && (code) <= QK_MAGIC_MAX) diff --git a/quantum/pointing_device/pointing_device_auto_mouse.c b/quantum/pointing_device/pointing_device_auto_mouse.c index d9f924e258..250351f608 100644 --- a/quantum/pointing_device/pointing_device_auto_mouse.c +++ b/quantum/pointing_device/pointing_device_auto_mouse.c @@ -357,6 +357,8 @@ bool process_auto_mouse(uint16_t keycode, keyrecord_t* record) { } // DF --------------------------------------------------------------------------------------------------------- case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: + // PDF -------------------------------------------------------------------------------------------------------- + case QK_PERSISTENT_DEF_LAYER ... QK_PERSISTENT_DEF_LAYER_MAX: # ifndef NO_ACTION_ONESHOT // OSL((AUTO_MOUSE_TARGET_LAYER))------------------------------------------------------------------------------ case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: diff --git a/quantum/process_keycode/process_autocorrect.c b/quantum/process_keycode/process_autocorrect.c index edc47718f3..b7f9132acf 100644 --- a/quantum/process_keycode/process_autocorrect.c +++ b/quantum/process_keycode/process_autocorrect.c @@ -98,6 +98,7 @@ bool process_autocorrect_default_handler(uint16_t *keycode, keyrecord_t *record, case QK_TO ... QK_TO_MAX: case QK_MOMENTARY ... QK_MOMENTARY_MAX: case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: + case QK_PERSISTENT_DEF_LAYER ... QK_PERSISTENT_DEF_LAYER_MAX: case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX: case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: diff --git a/quantum/process_keycode/process_default_layer.c b/quantum/process_keycode/process_default_layer.c new file mode 100644 index 0000000000..4bca30c410 --- /dev/null +++ b/quantum/process_keycode/process_default_layer.c @@ -0,0 +1,32 @@ +/* Copyright 2023 Nebuleon + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include "process_default_layer.h" +#include "quantum.h" +#include "quantum_keycodes.h" + +#if !defined(NO_ACTION_LAYER) + +bool process_default_layer(uint16_t keycode, keyrecord_t *record) { + if (IS_QK_PERSISTENT_DEF_LAYER(keycode) && !record->event.pressed) { + uint8_t layer = QK_PERSISTENT_DEF_LAYER_GET_LAYER(keycode); + set_single_persistent_default_layer(layer); + return false; + } + + return true; +} + +#endif // !defined(NO_ACTION_LAYER) diff --git a/quantum/process_keycode/process_default_layer.h b/quantum/process_keycode/process_default_layer.h new file mode 100644 index 0000000000..246d995817 --- /dev/null +++ b/quantum/process_keycode/process_default_layer.h @@ -0,0 +1,27 @@ +/* Copyright 2023 Nebuleon + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once + +#include +#include +#include "action.h" + +#if !defined(NO_ACTION_LAYER) + +bool process_default_layer(uint16_t keycode, keyrecord_t *record); + +#endif // !defined(NO_ACTION_LAYER) diff --git a/quantum/quantum.c b/quantum/quantum.c index 4aef26a6a5..d4ebd58e7f 100644 --- a/quantum/quantum.c +++ b/quantum/quantum.c @@ -52,6 +52,10 @@ # include "process_midi.h" #endif +#if !defined(NO_ACTION_LAYER) +# include "process_default_layer.h" +#endif + #ifdef PROGRAMMABLE_BUTTON_ENABLE # include "process_programmable_button.h" #endif @@ -404,6 +408,9 @@ bool process_record_quantum(keyrecord_t *record) { #ifdef TRI_LAYER_ENABLE process_tri_layer(keycode, record) && #endif +#if !defined(NO_ACTION_LAYER) + process_default_layer(keycode, record) && +#endif #ifdef LAYER_LOCK_ENABLE process_layer_lock(keycode, record) && #endif diff --git a/quantum/quantum_keycodes.h b/quantum/quantum_keycodes.h index 882e1d07ae..bcaf94af8b 100644 --- a/quantum/quantum_keycodes.h +++ b/quantum/quantum_keycodes.h @@ -92,6 +92,10 @@ #define DF(layer) (QK_DEF_LAYER | ((layer)&0x1F)) #define QK_DEF_LAYER_GET_LAYER(kc) ((kc)&0x1F) +// Set persistent default layer - 32 layer max +#define PDF(layer) (QK_PERSISTENT_DEF_LAYER | ((layer)&0x1F)) +#define QK_PERSISTENT_DEF_LAYER_GET_LAYER(kc) ((kc)&0x1F) + // Toggle to layer - 32 layer max #define TG(layer) (QK_TOGGLE_LAYER | ((layer)&0x1F)) #define QK_TOGGLE_LAYER_GET_LAYER(kc) ((kc)&0x1F) diff --git a/tests/test_common/keycode_util.cpp b/tests/test_common/keycode_util.cpp index 9f88d40ec7..539cab819a 100644 --- a/tests/test_common/keycode_util.cpp +++ b/tests/test_common/keycode_util.cpp @@ -94,6 +94,8 @@ std::string generate_identifier(uint16_t kc) { s << "MO(" << +QK_MOMENTARY_GET_LAYER(kc) << ")"; } else if (IS_QK_DEF_LAYER(kc)) { s << "DF(" << +QK_DEF_LAYER_GET_LAYER(kc) << ")"; + } else if (IS_QK_PERSISTENT_DEF_LAYER(kc)) { + s << "PDF(" << +QK_PERSISTENT_DEF_LAYER_GET_LAYER(kc) << ")"; } else if (IS_QK_TOGGLE_LAYER(kc)) { s << "TG(" << +QK_TOGGLE_LAYER_GET_LAYER(kc) << ")"; } else if (IS_QK_LAYER_TAP_TOGGLE(kc)) {