From 4d1e59e769a0ffcb86611664d1e641a6fb482fb1 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 18 Nov 2021 12:27:52 +0100 Subject: [PATCH] EscapeOneShot: Make it possible to disable the plugin at run-time We want to make it possible to have the plugin in firmwares shipped by Chrysalis, but still have the functionality optional. To achieve this, we need to be able to toggle it on and off at will. We move both the existing `cancel_oneshot_key_` property, and the new toggle into a struct, which we will later make use of in the upcoming configuration plugin. Signed-off-by: Gergely Nagy --- .../kaleidoscope/plugin/Escape-OneShot.cpp | 13 ++++++-- .../src/kaleidoscope/plugin/Escape-OneShot.h | 30 ++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot.cpp b/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot.cpp index c9708cbf..b1a53fb6 100644 --- a/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot.cpp +++ b/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot.cpp @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active. - * Copyright (C) 2016-2020 Keyboard.io, Inc + * Copyright (C) 2016-2021 Keyboard.io, Inc * * 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 @@ -24,14 +24,21 @@ namespace kaleidoscope { namespace plugin { -Key EscapeOneShot::cancel_oneshot_key_{Key_Escape}; +EscapeOneShot::Settings EscapeOneShot::settings_ = { + .disabled = false, + .cancel_oneshot_key = Key_Escape +}; EventHandlerResult EscapeOneShot::onKeyEvent(KeyEvent &event) { + // If we're disabled, just pass through. + if (settings_.disabled) + return EventHandlerResult::OK; + // We only act on an escape key (or `cancel_oneshot_key_`, if that has been // set) that has just been pressed, and not generated by some other // plugin. Also, only if at least one OneShot key is active and/or // sticky. Last, only if there are no OneShot keys currently being held. - if (event.key == cancel_oneshot_key_ && + if (event.key == settings_.cancel_oneshot_key && keyToggledOn(event.state) && !keyIsInjected(event.state) && ::OneShot.isActive()) { diff --git a/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot.h b/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot.h index 90e8511b..d94111db 100644 --- a/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot.h +++ b/plugins/Kaleidoscope-Escape-OneShot/src/kaleidoscope/plugin/Escape-OneShot.h @@ -1,6 +1,6 @@ /* -*- mode: c++ -*- * Kaleidoscope-Escape-OneShot -- Turn ESC into a key that cancels OneShots, if active. - * Copyright (C) 2016-2020 Keyboard.io, Inc + * Copyright (C) 2016-2021 Keyboard.io, Inc * * 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 @@ -24,19 +24,41 @@ constexpr Key OneShotCancelKey {kaleidoscope::ranges::OS_CANCEL}; namespace kaleidoscope { namespace plugin { + class EscapeOneShot : public kaleidoscope::Plugin { public: EscapeOneShot(void) {} EventHandlerResult onKeyEvent(KeyEvent &event); - void setCancelKey(Key cancel_key) { - cancel_oneshot_key_ = cancel_key; + static void setCancelKey(Key cancel_key) { + settings_.cancel_oneshot_key = cancel_key; + } + static Key getCancelKey() { + return settings_.cancel_oneshot_key; + } + static void enable() { + settings_.disabled = false; + } + static void disable() { + settings_.disabled = true; + } + static void toggle() { + settings_.disabled = !settings_.disabled; + } + static bool isEnabled() { + return !settings_.disabled; } private: - static Key cancel_oneshot_key_; + struct Settings { + bool disabled; + Key cancel_oneshot_key; + }; + static Settings settings_; }; +}; + } }