From 979549096edd06c17778381fbfa2a037b183f385 Mon Sep 17 00:00:00 2001 From: Nebuleon <2391500+Nebuleon@users.noreply.github.com> Date: Fri, 22 Nov 2024 06:19:11 -0500 Subject: [PATCH 1/2] [Feature] Add keycode PDF(layer) to set the default layer in EEPROM (#21881) --- builddefs/common_features.mk | 1 + data/constants/keycodes/keycodes_0.0.4.hjson | 7 ++++ docs/feature_layers.md | 3 +- docs/keycodes.md | 3 +- keyboards/planck/ez/ez.c | 1 + keyboards/zsa/moonlander/moonlander.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 68f9a1dd08..16df57d157 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.4.hjson b/data/constants/keycodes/keycodes_0.0.4.hjson index e69de29bb2..f03ef747a4 100644 --- a/data/constants/keycodes/keycodes_0.0.4.hjson +++ b/data/constants/keycodes/keycodes_0.0.4.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 e57642071f..a065c5e6ec 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.md#programming-the-behavior-of-any-keycode).) +* `DF(layer)` - sets a non-persistent 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 switch, which will not last beyond a power loss, might be used to switch from QWERTY to Dvorak layout for your own use, but leave the keyboard in the QWERTY layout when handing it to someone else who is not familiar with the Dvorak layout. +* `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 9d722216a9..bc787ad1a0 100644 --- a/docs/keycodes.md +++ b/docs/keycodes.md @@ -381,7 +381,8 @@ See also: [Layer Switching](feature_layers.md#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.md) 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.md). Example Implementation: `LM(LAYER_1, MOD_LALT)`| diff --git a/keyboards/planck/ez/ez.c b/keyboards/planck/ez/ez.c index 522fd70033..7160476767 100644 --- a/keyboards/planck/ez/ez.c +++ b/keyboards/planck/ez/ez.c @@ -322,6 +322,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/moonlander/moonlander.c b/keyboards/zsa/moonlander/moonlander.c index 02c64f4b96..6379107770 100644 --- a/keyboards/zsa/moonlander/moonlander.c +++ b/keyboards/zsa/moonlander/moonlander.c @@ -322,6 +322,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 c92028ab43..49b87f3c9a 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, @@ -1434,6 +1436,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..1a298edf7d 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: + // PD --------------------------------------------------------------------------------------------------------- + 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 011f9d73e4..dd2db02f1b 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 @@ -393,6 +397,9 @@ bool process_record_quantum(keyrecord_t *record) { #endif #ifdef TRI_LAYER_ENABLE process_tri_layer(keycode, record) && +#endif +#if !defined(NO_ACTION_LAYER) + process_default_layer(keycode, record) && #endif true)) { return false; 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..27eba4ab34 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 << "PD(" << +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)) { From 36cd9a5afb396b8fadc971f4126dfc9f466cd788 Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Fri, 22 Nov 2024 11:53:45 +0000 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Nick Brassel --- quantum/pointing_device/pointing_device_auto_mouse.c | 2 +- tests/test_common/keycode_util.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/pointing_device/pointing_device_auto_mouse.c b/quantum/pointing_device/pointing_device_auto_mouse.c index 1a298edf7d..250351f608 100644 --- a/quantum/pointing_device/pointing_device_auto_mouse.c +++ b/quantum/pointing_device/pointing_device_auto_mouse.c @@ -357,7 +357,7 @@ bool process_auto_mouse(uint16_t keycode, keyrecord_t* record) { } // DF --------------------------------------------------------------------------------------------------------- case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: - // PD --------------------------------------------------------------------------------------------------------- + // PDF -------------------------------------------------------------------------------------------------------- case QK_PERSISTENT_DEF_LAYER ... QK_PERSISTENT_DEF_LAYER_MAX: # ifndef NO_ACTION_ONESHOT // OSL((AUTO_MOUSE_TARGET_LAYER))------------------------------------------------------------------------------ diff --git a/tests/test_common/keycode_util.cpp b/tests/test_common/keycode_util.cpp index 27eba4ab34..539cab819a 100644 --- a/tests/test_common/keycode_util.cpp +++ b/tests/test_common/keycode_util.cpp @@ -95,7 +95,7 @@ std::string generate_identifier(uint16_t 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 << "PD(" << +QK_PERSISTENT_DEF_LAYER_GET_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)) {