From fddbd75ff89f33a806188340710ca23381458e58 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 25 Nov 2019 15:56:45 +0100 Subject: [PATCH] New event handler: onLEDModeChange We'd like to be able to run custom code whenever the led mode changes, reliably, without having to resort to checking the mode every cycle. For this purpose, we introduce the `onLEDModeChange()` handler plugins can hook into. It will be called every time `LEDControl.set_mode()` is called, even if that just sets the mode to the currently active one. Signed-off-by: Gergely Nagy --- src/kaleidoscope/event_handlers.h | 12 ++++++++++++ src/kaleidoscope/hooks.h | 5 +++++ src/kaleidoscope/plugin/LEDControl.cpp | 2 ++ 3 files changed, 19 insertions(+) diff --git a/src/kaleidoscope/event_handlers.h b/src/kaleidoscope/event_handlers.h index 86c21db9..4bd8b38c 100644 --- a/src/kaleidoscope/event_handlers.h +++ b/src/kaleidoscope/event_handlers.h @@ -161,6 +161,14 @@ _CURRENT_IMPLEMENTATION, __NL__ \ _NOT_ABORTABLE, __NL__ \ (), (), ##__VA_ARGS__) __NL__ \ + /* Called when the LED mode changes. If one needs to know what */ __NL__ \ + /* from and what to the mode changed, they should track that */ __NL__ \ + /* themselves. */ __NL__ \ + OPERATION(onLEDModeChange, __NL__ \ + 1, __NL__ \ + _CURRENT_IMPLEMENTATION, __NL__ \ + _NOT_ABORTABLE, __NL__ \ + (), (), ##__VA_ARGS__) __NL__ \ /* Called before reporting our state to the host. This is the */ __NL__ \ /* last point in a cycle where a plugin can alter what gets */ __NL__ \ /* reported to the host. */ __NL__ \ @@ -219,6 +227,10 @@ OP(onLayerChange, 1) __NL__ \ END(onLayerChange, 1) __NL__ \ __NL__ \ + START(onLEDModeChange, 1) __NL__ \ + OP(onLEDModeChange, 1) __NL__ \ + END(onLEDModeChange, 1) __NL__ \ + __NL__ \ START(beforeReportingState, 1) __NL__ \ OP(beforeReportingState, 1) __NL__ \ END(beforeReportingState, 1) __NL__ \ diff --git a/src/kaleidoscope/hooks.h b/src/kaleidoscope/hooks.h index 4e154f49..abc6d5f1 100644 --- a/src/kaleidoscope/hooks.h +++ b/src/kaleidoscope/hooks.h @@ -33,6 +33,10 @@ extern void handleKeyswitchEvent(kaleidoscope::Key mappedKey, KeyAddr key_addr, DEPRECATED(ROW_COL_FUNC) extern void handleKeyswitchEvent(kaleidoscope::Key mappedKey, byte row, byte col, uint8_t keyState); namespace kaleidoscope { +namespace plugin { +// Forward declaration to enable friend declarations. +class LEDControl; +} // Forward declaration to enable friend declarations. class Layer_; @@ -55,6 +59,7 @@ class Hooks { // and Hooks::afterEachCycle. friend class Kaleidoscope_; friend class ::kaleidoscope::Layer_; + friend class ::kaleidoscope::plugin::LEDControl; // ::handleKeyswitchEvent(...) calls Hooks::onKeyswitchEvent. friend void ::handleKeyswitchEvent(kaleidoscope::Key mappedKey, diff --git a/src/kaleidoscope/plugin/LEDControl.cpp b/src/kaleidoscope/plugin/LEDControl.cpp index 9dc3b931..a5390072 100644 --- a/src/kaleidoscope/plugin/LEDControl.cpp +++ b/src/kaleidoscope/plugin/LEDControl.cpp @@ -68,6 +68,8 @@ LEDControl::set_mode(uint8_t mode_) { cur_led_mode_ = LEDModeManager::getLEDMode(mode_id); refreshAll(); + + kaleidoscope::Hooks::onLEDModeChange(); } void LEDControl::activate(LEDModeInterface *plugin) {