From 79c6c4f2665949466e6e8b2b856eccbf4ad261f5 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 13 May 2018 22:05:40 +0200 Subject: [PATCH] Updated to use the new plugin APIs Signed-off-by: Gergely Nagy --- README.md | 10 +++---- examples/SpaceCadet/SpaceCadet.ino | 19 +++++++------- src/Kaleidoscope/SpaceCadet.cpp | 42 ++++++++++++++++++------------ src/Kaleidoscope/SpaceCadet.h | 15 +++++++---- 4 files changed, 50 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 874d88df..83eb5385 100644 --- a/README.md +++ b/README.md @@ -45,9 +45,9 @@ enabling the plugin: #include #include -void setup() { - Kaleidoscope.use(&SpaceCadet); +KALEIDOSCOPE_INIT_PLUGINS(SpaceCadet); +void setup() { Kaleidoscope.setup(); } ``` @@ -65,8 +65,10 @@ passing a new keymap into the SpaceCadet object, as shown below: #include #include +KALEIDOSCOPE_INIT_PLUGINS(SpaceCadet); + void setup() { - Kaleidoscope.use(&SpaceCadet); + Kaleidoscope.setup(); //Set the keymap with a 250ms timeout per-key //Setting is {KeyThatWasPressed, AlternativeKeyToSend, TimeoutInMS} @@ -83,8 +85,6 @@ void setup() { }; //Set the map. SpaceCadet.map = spacecadetmap; - - Kaleidoscope.setup(); } ``` diff --git a/examples/SpaceCadet/SpaceCadet.ino b/examples/SpaceCadet/SpaceCadet.ino index 4cff2d47..b130f240 100644 --- a/examples/SpaceCadet/SpaceCadet.ino +++ b/examples/SpaceCadet/SpaceCadet.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-SpaceCadet -- Space Cadet Shift - * Copyright (C) 2016, 2017 Gergely Nagy + * Copyright (C) 2016, 2017, 2018 Gergely Nagy * * 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 @@ -19,6 +19,7 @@ #include #include +// *INDENT-OFF* const Key keymaps[][ROWS][COLS] PROGMEM = { [0] = KEYMAP_STACKED ( @@ -30,18 +31,20 @@ const Key keymaps[][ROWS][COLS] PROGMEM = { Key_LeftControl, Key_Backspace, Key_LeftGui, Key_LeftShift, Key_skip, - Key_SpaceCadetDisable, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, - Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, - Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, - Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, + Key_SpaceCadetDisable, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, + Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, + Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, + Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, Key_RightShift, Key_RightAlt, Key_Spacebar, Key_RightControl, Key_skip), }; +// *INDENT-ON* + +KALEIDOSCOPE_INIT_PLUGINS(SpaceCadet); void setup() { - //Tell Kaleidoscope to use SpaceCadet - Kaleidoscope.use(&SpaceCadet); + Kaleidoscope.setup(); //Set the SpaceCadet map //Setting is {KeyThatWasPressed, AlternativeKeyToSend, TimeoutInMS} @@ -58,8 +61,6 @@ void setup() { }; //Set the map. SpaceCadet.map = spacecadetmap; - - Kaleidoscope.setup(); } void loop() { diff --git a/src/Kaleidoscope/SpaceCadet.cpp b/src/Kaleidoscope/SpaceCadet.cpp index 1a7f7a88..5aee55bd 100644 --- a/src/Kaleidoscope/SpaceCadet.cpp +++ b/src/Kaleidoscope/SpaceCadet.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-SpaceCadet -- Space Cadet Shift Extended - * Copyright (C) 2016, 2017 Gergely Nagy, Ben Gemperline + * Copyright (C) 2016, 2017, 2018 Gergely Nagy, Ben Gemperline * * 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 @@ -39,7 +39,6 @@ SpaceCadet::KeyBinding * SpaceCadet::map; uint16_t SpaceCadet::time_out = 1000; bool SpaceCadet::disabled = false; -//Empty Constructor SpaceCadet::SpaceCadet() { static SpaceCadet::KeyBinding initialmap[] = { //By default, respect the default timeout @@ -71,11 +70,7 @@ bool SpaceCadet::active() { return !disabled; } -void SpaceCadet::begin() { - Kaleidoscope.useEventHandlerHook(eventHandlerHook); -} - -Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { +EventHandlerResult SpaceCadet::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) { //Handle our synthetic keys for enabling and disabling functionality if (mapped_key.raw >= kaleidoscope::ranges::SC_FIRST && mapped_key.raw <= kaleidoscope::ranges::SC_LAST) { @@ -88,8 +83,7 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key } } - //in any case, return NoKey (these don't do anything else) - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } //if SpaceCadet is disabled, this was an injected key, it was NoKey, @@ -101,7 +95,7 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key || mapped_key == Key_NoKey || (!keyIsPressed(key_state) && !keyWasPressed(key_state)) ) { - return mapped_key; + return EventHandlerResult::OK; } // If a key has been just toggled on... @@ -152,13 +146,13 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key //anything until we know that we're either sending the alternate key or we //know for sure that we want to send the originally pressed key. if (valid_key) { - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } //this is all we need to do on keypress, let the next handler do its thing too. //This case assumes we weren't a valid key that we were watching, so we don't //need to do anything else. - return mapped_key; + return EventHandlerResult::OK; } // if the state is empty, that means that either an activating key wasn't pressed, @@ -194,7 +188,7 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key //If no valid mapped keys were pressed, simply return the key that //was originally passed in. if (!valid_key) { - return mapped_key; + return EventHandlerResult::OK; } //use the map index to find the local timeout for this key @@ -212,14 +206,14 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key map[index].start_time = 0; //Just return this key itself (we won't run alternative keys check) - return mapped_key; + return EventHandlerResult::OK; } // If the key that was pressed isn't one of our mapped keys, just // return. This can happen when another key is released, and that should not // interrupt us. if (!pressed_key_was_valid) { - return mapped_key; + return EventHandlerResult::OK; } // if a key toggled off (and that must be one of the mapped keys at this point), @@ -243,12 +237,26 @@ Key SpaceCadet::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key //This prevents us from accidentally triggering a keypress that we didn't //mean to handle. if (valid_key) { - return Key_NoKey; + return EventHandlerResult::EVENT_CONSUMED; } //Finally, as a final sanity check, simply return the passed-in key as-is. - return mapped_key; + return EventHandlerResult::OK; +} + +// Legacy V1 API +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +void SpaceCadet::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); +} + +Key SpaceCadet::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::SpaceCadet.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; + return Key_NoKey; } +#endif } diff --git a/src/Kaleidoscope/SpaceCadet.h b/src/Kaleidoscope/SpaceCadet.h index bf901afa..34043b89 100644 --- a/src/Kaleidoscope/SpaceCadet.h +++ b/src/Kaleidoscope/SpaceCadet.h @@ -29,8 +29,8 @@ #define Key_SpaceCadetDisable (Key) { .raw = kaleidoscope::ranges::SC_LAST } namespace kaleidoscope { -//Declaration for the method (implementing KaleidoscopePlugin) -class SpaceCadet : public KaleidoscopePlugin { + +class SpaceCadet : public kaleidoscope::Plugin { public: //Internal Class //Declarations for the modifier key mapping @@ -54,11 +54,9 @@ class SpaceCadet : public KaleidoscopePlugin { uint32_t start_time = 0; }; - //Empty constructor SpaceCadet(void); //Methods - void begin(void) final; static void enable(void); static void disable(void); static bool active(void); @@ -67,8 +65,15 @@ class SpaceCadet : public KaleidoscopePlugin { static uint16_t time_out; // The global timeout in milliseconds static SpaceCadet::KeyBinding * map; // The map of key bindings + EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state); + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API + protected: + void begin(); + static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state); +#endif + private: - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); static bool disabled; }; };