diff --git a/NEWS.md b/NEWS.md index abe27afd..7f2c9748 100644 --- a/NEWS.md +++ b/NEWS.md @@ -79,6 +79,10 @@ The [Cycle](doc/plugin/Cycle.md) plugin has much better support for cycling thro There are situations where one would like to disable sending a report after each and every step of a macro, and rather have direct control over when reports are sent. The new `WITH_EXPLICIT_REPORT`, `WITH_IMPLICIT_REPORT` and `SEND_REPORT` steps help with that. Please see the [Macros](doc/plugin/Macros.md) documentation for more information. +### Events now trigger on layer changes + +Changing layers now triggers the `onLayerChange` event - but only if there was real change (thus, calling `Layer.on(SOME_LAYER)` multiple times in a row will only trigger one event). This event was introduced to help plugins that depend on layer state schedule their work better. + ## New hardware support Kaleidoscope has been ported to the following devices: diff --git a/src/kaleidoscope/event_handlers.h b/src/kaleidoscope/event_handlers.h index 45c9feac..39f12a81 100644 --- a/src/kaleidoscope/event_handlers.h +++ b/src/kaleidoscope/event_handlers.h @@ -71,6 +71,12 @@ (const char *command), __NL__ \ (command), ##__VA_ARGS__) __NL__ \ __NL__ \ + /* Called when the layer state changes. Which layes changed are */ __NL__ \ + /* not passed as arguments. If one needs that info, they should */ __NL__ \ + /* track Layer.getState() themselves. */ __NL__ \ + OPERATION(onLayerChange, __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__ \ diff --git a/src/kaleidoscope/hooks.h b/src/kaleidoscope/hooks.h index d83bb293..9fd57ead 100644 --- a/src/kaleidoscope/hooks.h +++ b/src/kaleidoscope/hooks.h @@ -32,6 +32,9 @@ extern void handleKeyswitchEvent(kaleidoscope::Key mappedKey, byte row, byte col namespace kaleidoscope { +// Forward declaration to enable friend declarations. +class Layer_; + // The reason why the hook routing entry point functions live within // class Hooks and not directly within a namespace is, that we want // to restrict who is allowed to trigger hooks, mainly to prevent @@ -49,6 +52,7 @@ class Hooks { // Kaleidoscope_ calls Hooks::onSetup, Hooks::beforeReportingState // and Hooks::afterEachCycle. friend class Kaleidoscope_; + friend class ::kaleidoscope::Layer_; // ::handleKeyswitchEvent(...) calls Hooks::onKeyswitchEvent. friend void ::handleKeyswitchEvent(kaleidoscope::Key mappedKey, diff --git a/src/kaleidoscope/layers.cpp b/src/kaleidoscope/layers.cpp index 72e4a7e3..2c6fd0b5 100644 --- a/src/kaleidoscope/layers.cpp +++ b/src/kaleidoscope/layers.cpp @@ -168,6 +168,8 @@ void Layer_::on(uint8_t layer) { // Update the keymap cache (but not liveCompositeKeymap; that gets // updated separately, when keys toggle on or off. See layers.h) updateActiveLayers(); + + kaleidoscope::Hooks::onLayerChange(); } // Deactivate a given layer @@ -187,6 +189,8 @@ void Layer_::off(uint8_t layer) { // Update the keymap cache (but not liveCompositeKeymap; that gets // updated separately, when keys toggle on or off. See layers.h) updateActiveLayers(); + + kaleidoscope::Hooks::onLayerChange(); } boolean Layer_::isOn(uint8_t layer) {