Merge pull request #363 from keyboardio/f/onLayerChange

Introduce a new event: onLayerChange
pull/474/head
Jesse Vincent 6 years ago committed by GitHub
commit d4b2332e0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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. 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 ## New hardware support
Kaleidoscope has been ported to the following devices: Kaleidoscope has been ported to the following devices:

@ -71,6 +71,12 @@
(const char *command), __NL__ \ (const char *command), __NL__ \
(command), ##__VA_ARGS__) __NL__ \ (command), ##__VA_ARGS__) __NL__ \
__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__ \ /* 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__ \ /* last point in a cycle where a plugin can alter what gets */ __NL__ \
/* reported to the host. */ __NL__ \ /* reported to the host. */ __NL__ \

@ -32,6 +32,9 @@ extern void handleKeyswitchEvent(kaleidoscope::Key mappedKey, byte row, byte col
namespace kaleidoscope { namespace kaleidoscope {
// Forward declaration to enable friend declarations.
class Layer_;
// The reason why the hook routing entry point functions live within // The reason why the hook routing entry point functions live within
// class Hooks and not directly within a namespace is, that we want // class Hooks and not directly within a namespace is, that we want
// to restrict who is allowed to trigger hooks, mainly to prevent // to restrict who is allowed to trigger hooks, mainly to prevent
@ -49,6 +52,7 @@ class Hooks {
// Kaleidoscope_ calls Hooks::onSetup, Hooks::beforeReportingState // Kaleidoscope_ calls Hooks::onSetup, Hooks::beforeReportingState
// and Hooks::afterEachCycle. // and Hooks::afterEachCycle.
friend class Kaleidoscope_; friend class Kaleidoscope_;
friend class ::kaleidoscope::Layer_;
// ::handleKeyswitchEvent(...) calls Hooks::onKeyswitchEvent. // ::handleKeyswitchEvent(...) calls Hooks::onKeyswitchEvent.
friend void ::handleKeyswitchEvent(kaleidoscope::Key mappedKey, friend void ::handleKeyswitchEvent(kaleidoscope::Key mappedKey,

@ -168,6 +168,8 @@ void Layer_::on(uint8_t layer) {
// Update the keymap cache (but not liveCompositeKeymap; that gets // Update the keymap cache (but not liveCompositeKeymap; that gets
// updated separately, when keys toggle on or off. See layers.h) // updated separately, when keys toggle on or off. See layers.h)
updateActiveLayers(); updateActiveLayers();
kaleidoscope::Hooks::onLayerChange();
} }
// Deactivate a given layer // Deactivate a given layer
@ -187,6 +189,8 @@ void Layer_::off(uint8_t layer) {
// Update the keymap cache (but not liveCompositeKeymap; that gets // Update the keymap cache (but not liveCompositeKeymap; that gets
// updated separately, when keys toggle on or off. See layers.h) // updated separately, when keys toggle on or off. See layers.h)
updateActiveLayers(); updateActiveLayers();
kaleidoscope::Hooks::onLayerChange();
} }
boolean Layer_::isOn(uint8_t layer) { boolean Layer_::isOn(uint8_t layer) {

@ -28,7 +28,7 @@ namespace plugin {
uint16_t ColormapEffect::map_base_; uint16_t ColormapEffect::map_base_;
uint8_t ColormapEffect::max_layers_; uint8_t ColormapEffect::max_layers_;
uint8_t ColormapEffect::last_highest_layer_; uint8_t ColormapEffect::top_layer_;
void ColormapEffect::max_layers(uint8_t max_) { void ColormapEffect::max_layers(uint8_t max_) {
if (map_base_ != 0) if (map_base_ != 0)
@ -42,21 +42,20 @@ void ColormapEffect::onActivate(void) {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return; return;
last_highest_layer_ = Layer.top(); top_layer_ = Layer.top();
if (last_highest_layer_ <= max_layers_) if (top_layer_ <= max_layers_)
::LEDPaletteTheme.updateHandler(map_base_, last_highest_layer_); ::LEDPaletteTheme.updateHandler(map_base_, top_layer_);
} }
void ColormapEffect::update(void) { void ColormapEffect::refreshAt(byte row, byte col) {
if (!Kaleidoscope.has_leds || Layer.top() == last_highest_layer_) if (top_layer_ <= max_layers_)
return; ::LEDPaletteTheme.refreshAt(map_base_, top_layer_, row, col);
onActivate();
} }
void ColormapEffect::refreshAt(byte row, byte col) { EventHandlerResult ColormapEffect::onLayerChange() {
if (last_highest_layer_ <= max_layers_) if (::LEDControl.get_mode() == this)
::LEDPaletteTheme.refreshAt(map_base_, last_highest_layer_, row, col); onActivate();
return EventHandlerResult::OK;
} }
EventHandlerResult ColormapEffect::onFocusEvent(const char *command) { EventHandlerResult ColormapEffect::onFocusEvent(const char *command) {

@ -28,15 +28,15 @@ class ColormapEffect : public LEDMode {
void max_layers(uint8_t max_); void max_layers(uint8_t max_);
EventHandlerResult onLayerChange();
EventHandlerResult onFocusEvent(const char *command); EventHandlerResult onFocusEvent(const char *command);
protected: protected:
void onActivate(void) final; void onActivate(void) final;
void update(void) final;
void refreshAt(byte row, byte col) final; void refreshAt(byte row, byte col) final;
private: private:
static uint8_t last_highest_layer_; static uint8_t top_layer_;
static uint8_t max_layers_; static uint8_t max_layers_;
static uint16_t map_base_; static uint16_t map_base_;
}; };

@ -22,41 +22,30 @@
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
uint8_t ActiveModColorEffect::mod_keys_[MAX_MODS_PER_LAYER];
uint8_t ActiveModColorEffect::mod_key_count_;
cRGB ActiveModColorEffect::highlight_color = (cRGB) { cRGB ActiveModColorEffect::highlight_color = (cRGB) {
0xff, 0xff, 0xff 0xff, 0xff, 0xff
}; };
cRGB ActiveModColorEffect::sticky_color = CRGB(0xff, 0x00, 0x00); cRGB ActiveModColorEffect::sticky_color = CRGB(0xff, 0x00, 0x00);
EventHandlerResult ActiveModColorEffect::beforeReportingState() { EventHandlerResult ActiveModColorEffect::onLayerChange() {
if (!Kaleidoscope.has_leds) if (!Kaleidoscope.has_leds)
return EventHandlerResult::OK; return EventHandlerResult::OK;
mod_key_count_ = 0;
for (byte r = 0; r < ROWS; r++) { for (byte r = 0; r < ROWS; r++) {
for (byte c = 0; c < COLS; c++) { for (byte c = 0; c < COLS; c++) {
Key k = Layer.lookupOnActiveLayer(r, c); Key k = Layer.lookup(r, c);
if (::OneShot.isOneShotKey(k)) { if (::OneShot.isOneShotKey(k) ||
if (::OneShot.isSticky(k)) (k.raw >= Key_LeftControl.raw && k.raw <= Key_RightGui.raw) ||
::LEDControl.setCrgbAt(r, c, sticky_color); (k.flags == (SYNTHETIC | SWITCH_TO_KEYMAP))) {
else if (::OneShot.isActive(k)) uint8_t coords = r * COLS + c;
::LEDControl.setCrgbAt(r, c, highlight_color); mod_keys_[mod_key_count_++] = coords;
else
::LEDControl.refreshAt(r, c);
} else if (k.raw >= Key_LeftControl.raw && k.raw <= Key_RightGui.raw) {
if (hid::isModifierKeyActive(k))
::LEDControl.setCrgbAt(r, c, highlight_color);
else
::LEDControl.refreshAt(r, c);
} else if (k.flags == (SYNTHETIC | SWITCH_TO_KEYMAP)) {
uint8_t layer = k.keyCode;
if (layer >= LAYER_SHIFT_OFFSET)
layer -= LAYER_SHIFT_OFFSET;
if (Layer.isOn(layer))
::LEDControl.setCrgbAt(r, c, highlight_color);
else
::LEDControl.refreshAt(r, c);
} }
} }
} }
@ -64,6 +53,41 @@ EventHandlerResult ActiveModColorEffect::beforeReportingState() {
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
EventHandlerResult ActiveModColorEffect::beforeReportingState() {
for (uint8_t i = 0; i < mod_key_count_; i++) {
uint8_t coords = mod_keys_[i];
byte c = coords % COLS;
byte r = (coords - c) / COLS;
Key k = Layer.lookup(r, c);
if (::OneShot.isOneShotKey(k)) {
if (::OneShot.isSticky(k))
::LEDControl.setCrgbAt(r, c, sticky_color);
else if (::OneShot.isActive(k))
::LEDControl.setCrgbAt(r, c, highlight_color);
else
::LEDControl.refreshAt(r, c);
} else if (k.raw >= Key_LeftControl.raw && k.raw <= Key_RightGui.raw) {
if (hid::isModifierKeyActive(k))
::LEDControl.setCrgbAt(r, c, highlight_color);
else
::LEDControl.refreshAt(r, c);
} else if (k.flags == (SYNTHETIC | SWITCH_TO_KEYMAP)) {
uint8_t layer = k.keyCode;
if (layer >= LAYER_SHIFT_OFFSET)
layer -= LAYER_SHIFT_OFFSET;
if (Layer.isOn(layer))
::LEDControl.setCrgbAt(r, c, highlight_color);
else
::LEDControl.refreshAt(r, c);
}
}
return EventHandlerResult::OK;
}
} }
} }

@ -20,6 +20,8 @@
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-LEDControl.h> #include <Kaleidoscope-LEDControl.h>
#define MAX_MODS_PER_LAYER 16
namespace kaleidoscope { namespace kaleidoscope {
namespace plugin { namespace plugin {
class ActiveModColorEffect : public kaleidoscope::Plugin { class ActiveModColorEffect : public kaleidoscope::Plugin {
@ -30,6 +32,11 @@ class ActiveModColorEffect : public kaleidoscope::Plugin {
static cRGB sticky_color; static cRGB sticky_color;
EventHandlerResult beforeReportingState(); EventHandlerResult beforeReportingState();
EventHandlerResult onLayerChange();
private:
static uint8_t mod_keys_[MAX_MODS_PER_LAYER];
static uint8_t mod_key_count_;
}; };
} }
} }

Loading…
Cancel
Save