From 191ecdb0000c4f334c80652cbd2c9edd0cbd9218 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 12 May 2018 10:44:12 +0200 Subject: [PATCH] Updated to use the new plugin APIs Signed-off-by: Gergely Nagy --- README.md | 4 ++-- examples/Cycle/Cycle.ino | 6 +++--- src/Kaleidoscope/Cycle.cpp | 42 +++++++++++++++++++++++--------------- src/Kaleidoscope/Cycle.h | 18 +++++++++------- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 2f5d166b..bf362830 100644 --- a/README.md +++ b/README.md @@ -39,9 +39,9 @@ void cycleAction(Key previous_key, uint8_t cycle_count) { } } -void setup(void) { - Kaleidoscope.use(&Cycle); +KALEIDOSCOPE_INIT_PLUGINS(Cycle); +void setup(void) { Kaleidoscope.setup(); } ``` diff --git a/examples/Cycle/Cycle.ino b/examples/Cycle/Cycle.ino index 8d1d7f8f..cbca3a3c 100644 --- a/examples/Cycle/Cycle.ino +++ b/examples/Cycle/Cycle.ino @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Cycle -- Key sequence cycling dead key for Kaleidoscope. - * 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 @@ -51,9 +51,9 @@ void cycleAction(Key previous_key, uint8_t cycle_count) { } } -void setup() { - Kaleidoscope.use(&Cycle); +KALEIDOSCOPE_INIT_PLUGINS(Cycle); +void setup() { Kaleidoscope.setup(); } diff --git a/src/Kaleidoscope/Cycle.cpp b/src/Kaleidoscope/Cycle.cpp index 9bdc13a5..fa920d2c 100644 --- a/src/Kaleidoscope/Cycle.cpp +++ b/src/Kaleidoscope/Cycle.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Cycle -- Key sequence cycling dead key for Kaleidoscope. - * 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 @@ -31,13 +31,6 @@ uint8_t Cycle::cycle_count_; // --- api --- -Cycle::Cycle(void) { -} - -void Cycle::begin(void) { - Kaleidoscope.useEventHandlerHook(eventHandlerHook); -} - void Cycle::replace(Key key) { handleKeyswitchEvent(Key_Backspace, UNKNOWN_KEYSWITCH_LOCATION, IS_PRESSED | INJECTED); hid::sendKeyboardReport(); @@ -60,14 +53,15 @@ void Cycle::replace(uint8_t cycle_size, const Key cycle_steps[]) { // --- hooks --- -Key Cycle::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { +EventHandlerResult Cycle::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) { if (key_state & INJECTED) - return mapped_key; + return EventHandlerResult::OK; if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) { - if (isCycle(mapped_key)) - return Key_NoKey; - return mapped_key; + if (isCycle(mapped_key)) { + return EventHandlerResult::EVENT_CONSUMED; + } + return EventHandlerResult::OK; } if (!isCycle(mapped_key)) { @@ -75,16 +69,32 @@ Key Cycle::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_stat last_non_cycle_key_.raw = mapped_key.raw; cycle_count_ = 0; } - return mapped_key; + return EventHandlerResult::OK; } - if (!keyToggledOff(key_state)) - return Key_NoKey; + if (!keyToggledOff(key_state)) { + return EventHandlerResult::EVENT_CONSUMED; + } ++cycle_count_; cycleAction(last_non_cycle_key_, cycle_count_); + return EventHandlerResult::EVENT_CONSUMED; +} + +#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API +// Deprecated v1 API +void Cycle::begin() { + Kaleidoscope.useEventHandlerHook(legacyEventHandler); +} + +Key Cycle::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) { + EventHandlerResult r = ::Cycle.onKeyswitchEvent(mapped_key, row, col, key_state); + if (r == EventHandlerResult::OK) + return mapped_key; return Key_NoKey; } +#endif + }; __attribute__((weak)) diff --git a/src/Kaleidoscope/Cycle.h b/src/Kaleidoscope/Cycle.h index aca7aaa1..c0f306ff 100644 --- a/src/Kaleidoscope/Cycle.h +++ b/src/Kaleidoscope/Cycle.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Cycle -- Key sequence cycling dead key for Kaleidoscope. - * 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 @@ -29,20 +29,24 @@ }) namespace kaleidoscope { -class Cycle : public KaleidoscopePlugin { +class Cycle : public kaleidoscope::Plugin { public: - Cycle(void); - - void begin(void) final; + Cycle(void) {} static void replace(Key key); static void replace(uint8_t cycle_size, const Key cycle_steps[]); + 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 last_non_cycle_key_; static uint8_t cycle_count_; - - static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state); }; };