From ec834f2fe5e1e4f1637d7a664a07ada36da3921d Mon Sep 17 00:00:00 2001 From: Michael Richters Date: Mon, 21 Mar 2022 20:14:05 -0500 Subject: [PATCH] Define constexpr constructors for plugin-specific Key objects In some cases, we still need preprocessor macros to preserve the same keymap markup as before, because they convert `X` to `Key_X` (for example). Signed-off-by: Michael Richters --- .../src/kaleidoscope/plugin/Cycle.h | 2 +- .../src/kaleidoscope/plugin/DynamicMacros.h | 6 +++++- .../src/kaleidoscope/plugin/Leader.h | 6 +++++- .../src/kaleidoscope/plugin/OneShot.h | 13 ++++++++++-- .../src/kaleidoscope/plugin/Qukeys.h | 20 +++++++++++++------ .../src/kaleidoscope/plugin/Redial.h | 2 +- .../src/kaleidoscope/plugin/SpaceCadet.h | 4 ++-- .../src/kaleidoscope/plugin/Syster.h | 3 ++- .../src/kaleidoscope/plugin/TapDance.h | 7 ++++++- .../src/kaleidoscope/plugin/TopsyTurvy.h | 6 +++++- .../src/kaleidoscope/plugin/Turbo.h | 2 +- 11 files changed, 53 insertions(+), 18 deletions(-) diff --git a/plugins/Kaleidoscope-Cycle/src/kaleidoscope/plugin/Cycle.h b/plugins/Kaleidoscope-Cycle/src/kaleidoscope/plugin/Cycle.h index ee7d013c..30d9c5d0 100644 --- a/plugins/Kaleidoscope-Cycle/src/kaleidoscope/plugin/Cycle.h +++ b/plugins/Kaleidoscope-Cycle/src/kaleidoscope/plugin/Cycle.h @@ -20,7 +20,7 @@ #include "kaleidoscope/Runtime.h" #include -#define Key_Cycle Key(kaleidoscope::ranges::CYCLE) +constexpr Key Key_Cycle = Key(kaleidoscope::ranges::CYCLE); #define cycleThrough(...) ({ \ static const Key __k[] PROGMEM = { __VA_ARGS__ }; \ diff --git a/plugins/Kaleidoscope-DynamicMacros/src/kaleidoscope/plugin/DynamicMacros.h b/plugins/Kaleidoscope-DynamicMacros/src/kaleidoscope/plugin/DynamicMacros.h index 5499dab0..9c2c84c9 100644 --- a/plugins/Kaleidoscope-DynamicMacros/src/kaleidoscope/plugin/DynamicMacros.h +++ b/plugins/Kaleidoscope-DynamicMacros/src/kaleidoscope/plugin/DynamicMacros.h @@ -22,13 +22,17 @@ #include "kaleidoscope/plugin/Macros/MacroSteps.h" -#define DM(n) Key(kaleidoscope::ranges::DYNAMIC_MACRO_FIRST + n) +#define DM(n) ::kaleidoscope::plugin::DynamicMacrosKey(n) #define MAX_CONCURRENT_DYNAMIC_MACRO_KEYS 8 namespace kaleidoscope { namespace plugin { +constexpr Key DynamicMacrosKey(uint8_t n) { + return Key(kaleidoscope::ranges::DYNAMIC_MACRO_FIRST + n); +} + class DynamicMacros : public kaleidoscope::Plugin { public: EventHandlerResult onNameQuery(); diff --git a/plugins/Kaleidoscope-Leader/src/kaleidoscope/plugin/Leader.h b/plugins/Kaleidoscope-Leader/src/kaleidoscope/plugin/Leader.h index 0b730cbf..d4918883 100644 --- a/plugins/Kaleidoscope-Leader/src/kaleidoscope/plugin/Leader.h +++ b/plugins/Kaleidoscope-Leader/src/kaleidoscope/plugin/Leader.h @@ -36,7 +36,7 @@ #define LEADER_MAX_SEQUENCE_LENGTH 4 -#define LEAD(n) Key(kaleidoscope::ranges::LEAD_FIRST + n) +#define LEAD(n) kaleidoscope::plugin::LeaderKey(n) #define LEADER_SEQ(...) { __VA_ARGS__, Key_NoKey } #define LEADER_DICT(...) { __VA_ARGS__, {{Key_NoKey}, NULL} } @@ -44,6 +44,10 @@ namespace kaleidoscope { namespace plugin { +constexpr Key LeaderKey(uint8_t n) { + return Key(kaleidoscope::ranges::LEAD_FIRST + n); +} + class Leader : public kaleidoscope::Plugin { public: typedef void (*action_t)(uint8_t seq_index); diff --git a/plugins/Kaleidoscope-OneShot/src/kaleidoscope/plugin/OneShot.h b/plugins/Kaleidoscope-OneShot/src/kaleidoscope/plugin/OneShot.h index 9a1e7e8c..6afcfa3e 100644 --- a/plugins/Kaleidoscope-OneShot/src/kaleidoscope/plugin/OneShot.h +++ b/plugins/Kaleidoscope-OneShot/src/kaleidoscope/plugin/OneShot.h @@ -24,12 +24,21 @@ // ---------------------------------------------------------------------------- // Keymap macros -#define OSM(kc) Key(kaleidoscope::ranges::OSM_FIRST + (Key_ ## kc).getKeyCode() - Key_LeftControl.getKeyCode()) -#define OSL(n) Key(kaleidoscope::ranges::OSL_FIRST + n) +#define OSM(k) ::kaleidoscope::plugin::OneShotModifierKey(Key_ ## k) +#define OSL(n) ::kaleidoscope::plugin::OneShotLayerKey(n) namespace kaleidoscope { namespace plugin { +constexpr Key OneShotModifierKey(Key mod_key) { + return Key(kaleidoscope::ranges::OSM_FIRST + + mod_key.getKeyCode() - HID_KEYBOARD_FIRST_MODIFIER); +} + +constexpr Key OneShotLayerKey(uint8_t layer) { + return Key(kaleidoscope::ranges::OSL_FIRST + layer); +} + class OneShot : public kaleidoscope::Plugin { public: // Constructor diff --git a/plugins/Kaleidoscope-Qukeys/src/kaleidoscope/plugin/Qukeys.h b/plugins/Kaleidoscope-Qukeys/src/kaleidoscope/plugin/Qukeys.h index d40aeeaf..dd86015e 100644 --- a/plugins/Kaleidoscope-Qukeys/src/kaleidoscope/plugin/Qukeys.h +++ b/plugins/Kaleidoscope-Qukeys/src/kaleidoscope/plugin/Qukeys.h @@ -24,21 +24,29 @@ #include "kaleidoscope/KeyEventTracker.h" // DualUse Key definitions for Qukeys in the keymap -#define MT(mod, key) Key( \ - kaleidoscope::ranges::DUM_FIRST + \ - (((Key_ ## mod).getKeyCode() - Key_LeftControl.getKeyCode()) << 8) + \ - (Key_ ## key).getKeyCode() \ -) +#define MT(mod, key) kaleidoscope::plugin::ModTapKey(Key_ ## mod, Key_ ## key) + #define SFT_T(key) MT(LeftShift, key) #define CTL_T(key) MT(LeftControl, key) #define ALT_T(key) MT(LeftAlt, key) #define GUI_T(key) MT(LeftGui, key) -#define LT(layer, key) Key(kaleidoscope::ranges::DUL_FIRST + (layer << 8) + (Key_ ## key).getKeyCode()) +#define LT(layer, key) kaleidoscope::plugin::LayerTapKey(layer, Key_ ## key) namespace kaleidoscope { namespace plugin { +constexpr Key ModTapKey(Key mod_key, Key tap_key) { + uint8_t mod = mod_key.getKeyCode() - HID_KEYBOARD_FIRST_MODIFIER; + return Key(kaleidoscope::ranges::DUM_FIRST + + (mod << 8) + tap_key.getKeyCode()); +} + +constexpr Key LayerTapKey(uint8_t layer, Key tap_key) { + return Key(kaleidoscope::ranges::DUL_FIRST + + (layer << 8) + tap_key.getKeyCode()); +} + // Data structure for an individual qukey struct Qukey { // The layer this qukey is mapped on. diff --git a/plugins/Kaleidoscope-Redial/src/kaleidoscope/plugin/Redial.h b/plugins/Kaleidoscope-Redial/src/kaleidoscope/plugin/Redial.h index e2514eda..dd7ac14e 100644 --- a/plugins/Kaleidoscope-Redial/src/kaleidoscope/plugin/Redial.h +++ b/plugins/Kaleidoscope-Redial/src/kaleidoscope/plugin/Redial.h @@ -20,7 +20,7 @@ #include "kaleidoscope/Runtime.h" #include -#define Key_Redial Key(kaleidoscope::ranges::REDIAL) +constexpr Key Key_Redial = Key(kaleidoscope::ranges::REDIAL); namespace kaleidoscope { namespace plugin { diff --git a/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.h b/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.h index 3365d755..0c4d372a 100644 --- a/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.h +++ b/plugins/Kaleidoscope-SpaceCadet/src/kaleidoscope/plugin/SpaceCadet.h @@ -27,8 +27,8 @@ #define SPACECADET_MAP_END (kaleidoscope::plugin::SpaceCadet::KeyBinding) { Key_NoKey, Key_NoKey, 0 } #endif -#define Key_SpaceCadetEnable Key(kaleidoscope::ranges::SC_FIRST) -#define Key_SpaceCadetDisable Key(kaleidoscope::ranges::SC_LAST) +constexpr Key Key_SpaceCadetEnable = Key(kaleidoscope::ranges::SC_FIRST); +constexpr Key Key_SpaceCadetDisable = Key(kaleidoscope::ranges::SC_LAST); namespace kaleidoscope { namespace plugin { diff --git a/plugins/Kaleidoscope-Syster/src/kaleidoscope/plugin/Syster.h b/plugins/Kaleidoscope-Syster/src/kaleidoscope/plugin/Syster.h index ab47c0ea..f8c36898 100644 --- a/plugins/Kaleidoscope-Syster/src/kaleidoscope/plugin/Syster.h +++ b/plugins/Kaleidoscope-Syster/src/kaleidoscope/plugin/Syster.h @@ -22,7 +22,8 @@ #define SYSTER_MAX_SYMBOL_LENGTH 32 -#define SYSTER Key(kaleidoscope::ranges::SYSTER) +constexpr Key Key_Syster = Key(kaleidoscope::ranges::SYSTER); +constexpr Key SYSTER = Key_Syster; namespace kaleidoscope { namespace plugin { diff --git a/plugins/Kaleidoscope-TapDance/src/kaleidoscope/plugin/TapDance.h b/plugins/Kaleidoscope-TapDance/src/kaleidoscope/plugin/TapDance.h index 2c46f98c..fb1fd640 100644 --- a/plugins/Kaleidoscope-TapDance/src/kaleidoscope/plugin/TapDance.h +++ b/plugins/Kaleidoscope-TapDance/src/kaleidoscope/plugin/TapDance.h @@ -24,7 +24,7 @@ #include "kaleidoscope/KeyAddrEventQueue.h" #include "kaleidoscope/KeyEventTracker.h" -#define TD(n) Key(kaleidoscope::ranges::TD_FIRST + n) +#define TD(n) kaleidoscope::plugin::TapDanceKey(n) #define tapDanceActionKeys(tap_count, tap_dance_action, ...) ({ \ static const Key __k[] PROGMEM = { __VA_ARGS__ }; \ @@ -34,6 +34,11 @@ namespace kaleidoscope { namespace plugin { + +constexpr Key TapDanceKey(uint8_t n) { + return Key(kaleidoscope::ranges::TD_FIRST + n); +} + class TapDance : public kaleidoscope::Plugin { public: enum ActionType { diff --git a/plugins/Kaleidoscope-TopsyTurvy/src/kaleidoscope/plugin/TopsyTurvy.h b/plugins/Kaleidoscope-TopsyTurvy/src/kaleidoscope/plugin/TopsyTurvy.h index 9a3ab9ca..2f397dd4 100644 --- a/plugins/Kaleidoscope-TopsyTurvy/src/kaleidoscope/plugin/TopsyTurvy.h +++ b/plugins/Kaleidoscope-TopsyTurvy/src/kaleidoscope/plugin/TopsyTurvy.h @@ -20,11 +20,15 @@ #include "kaleidoscope/Runtime.h" #include -#define TOPSY(k) Key(kaleidoscope::ranges::TT_FIRST + (Key_ ## k).getKeyCode()) +#define TOPSY(k) ::kaleidoscope::plugin::TopsyTurvyKey(Key_ ## k) namespace kaleidoscope { namespace plugin { +constexpr Key TopsyTurvyKey(Key key) { + return Key(kaleidoscope::ranges::TT_FIRST + key.getKeyCode()); +} + class TopsyTurvy: public kaleidoscope::Plugin { public: TopsyTurvy(void) {} diff --git a/plugins/Kaleidoscope-Turbo/src/kaleidoscope/plugin/Turbo.h b/plugins/Kaleidoscope-Turbo/src/kaleidoscope/plugin/Turbo.h index 463b4434..a2b3fab2 100644 --- a/plugins/Kaleidoscope-Turbo/src/kaleidoscope/plugin/Turbo.h +++ b/plugins/Kaleidoscope-Turbo/src/kaleidoscope/plugin/Turbo.h @@ -21,7 +21,7 @@ #pragma once -#define Key_Turbo Key{kaleidoscope::ranges::TURBO} +constexpr Key Key_Turbo = Key(kaleidoscope::ranges::TURBO); namespace kaleidoscope { namespace plugin {