Skip to content

Commit

Permalink
Start to move 'onNameQuery' into a mixin. This unifies a bunch of logic,
Browse files Browse the repository at this point in the history
saves stored bytes, and will form the basis for moving other duplicated
code into a couple of mixins
  • Loading branch information
obra committed Mar 6, 2024
1 parent d2a79b0 commit 5c73ff0
Show file tree
Hide file tree
Showing 35 changed files with 121 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ void ColormapEffect::max_layers(uint8_t max_) {
map_base_ = ::LEDPaletteTheme.reserveThemes(max_layers_);
}

EventHandlerResult ColormapEffect::onNameQuery() {
return ::Focus.sendName(F("ColormapEffect"));
}

bool ColormapEffect::isUninitialized() {
return ::LEDPaletteTheme.isThemeUninitialized(map_base_, max_layers_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,24 @@
#include "kaleidoscope/plugin/AccessTransientLEDMode.h" // for AccessTransientLEDMode
#include "kaleidoscope/plugin/LEDMode.h" // for LEDMode
#include "kaleidoscope/plugin/LEDModeInterface.h" // for LEDModeInterface
#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin

namespace kaleidoscope {
namespace plugin {
class ColormapEffect : public Plugin,
public FocusPlugin,
public LEDModeInterface,
public AccessTransientLEDMode {
public:
void max_layers(uint8_t max_);

EventHandlerResult onLayerChange();
EventHandlerResult onNameQuery();
EventHandlerResult onFocusEvent(const char *input);

static bool isUninitialized();
static void updateColorIndexAtPosition(uint8_t layer, uint16_t position, uint8_t palette_index);


// This class' instance has dynamic lifetime
//
class TransientLEDMode : public LEDMode {
Expand All @@ -62,6 +64,11 @@ class ColormapEffect : public Plugin,
const ColormapEffect *parent_;
};

protected:
const __FlashStringHelper *getPluginName() const override {
return F("ColormapEffect");
}

private:
static uint8_t top_layer_;
static uint8_t max_layers_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ EventHandlerResult DefaultLEDModeConfig::onFocusEvent(const char *input) {
return EventHandlerResult::EVENT_CONSUMED;
}

EventHandlerResult DefaultLEDModeConfig::onNameQuery() {
return ::Focus.sendName(F("DefaultLEDModeConfig"));
}

void DefaultLEDModeConfig::activateLEDModeIfUnconfigured(LEDModeInterface *plugin) {
if (!Runtime.storage().isSliceUninitialized(settings_base_, sizeof(settings_)))
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/plugin.h" // for Plugin
#include "kaleidoscope/plugin/LEDModeInterface.h" // for LEDModeInterface

#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin
namespace kaleidoscope {
namespace plugin {

class DefaultLEDModeConfig : public kaleidoscope::Plugin {
class DefaultLEDModeConfig : public kaleidoscope::Plugin, FocusPlugin {
public:
EventHandlerResult onSetup();
EventHandlerResult onNameQuery();
EventHandlerResult onFocusEvent(const char *input);

void activateLEDModeIfUnconfigured(LEDModeInterface *plugin);

protected:
const __FlashStringHelper *getPluginName() const override { F("DefaultLEDModeConfig"); }

private:
static uint16_t settings_base_;
static struct settings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ EventHandlerResult DynamicMacros::onKeyEvent(KeyEvent &event) {
return EventHandlerResult::EVENT_CONSUMED;
}

EventHandlerResult DynamicMacros::onNameQuery() {
return ::Focus.sendName(F("DynamicMacros"));
}

EventHandlerResult DynamicMacros::onFocusEvent(const char *input) {
const char *cmd_map = PSTR("macros.map");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/key_defs.h" // for Key
#include "kaleidoscope/plugin.h" // for Plugin

#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin
#define DM(n) ::kaleidoscope::plugin::DynamicMacrosKey(n)

namespace kaleidoscope {
Expand All @@ -34,9 +34,8 @@ constexpr Key DynamicMacrosKey(uint8_t n) {
return Key(kaleidoscope::ranges::DYNAMIC_MACRO_FIRST + n);
}

class DynamicMacros : public kaleidoscope::Plugin {
class DynamicMacros : public kaleidoscope::Plugin, public FocusPlugin {
public:
EventHandlerResult onNameQuery();
EventHandlerResult onKeyEvent(KeyEvent &event);
EventHandlerResult onFocusEvent(const char *input);
EventHandlerResult beforeReportingState(const KeyEvent &event) {
Expand All @@ -47,6 +46,12 @@ class DynamicMacros : public kaleidoscope::Plugin {

void play(uint8_t seq_id);

protected:
const __FlashStringHelper *getPluginName() const override {
return F("LEDBrightnessConfig");
}


private:
static const uint8_t MAX_MACRO_COUNT_ = 32;
uint16_t storage_base_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ EventHandlerResult EEPROMKeymap::onSetup() {
return EventHandlerResult::OK;
}

EventHandlerResult EEPROMKeymap::onNameQuery() {
return ::Focus.sendName(F("EEPROMKeymap"));
}

void EEPROMKeymap::setup(uint8_t max) {
layer_count = max;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/key_defs.h" // for Key
#include "kaleidoscope/plugin.h" // for Plugin
#include "Kaleidoscope-FocusSerial.h" // for getting focus in our path
#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin

namespace kaleidoscope {
namespace plugin {
class EEPROMKeymap : public kaleidoscope::Plugin {
class EEPROMKeymap : public kaleidoscope::Plugin, public FocusPlugin {
public:
enum class Mode {
CUSTOM,
EXTEND
};

EventHandlerResult onSetup();
EventHandlerResult onNameQuery();
EventHandlerResult onFocusEvent(const char *input);

static void setup(uint8_t max);
Expand All @@ -48,6 +49,9 @@ class EEPROMKeymap : public kaleidoscope::Plugin {

static void updateKey(uint16_t base_pos, Key key);

protected:
const __FlashStringHelper *getPluginName() const override { F("EEPROMKeymap"); }

private:
static uint16_t keymap_base_;
static uint8_t max_layers_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ EventHandlerResult EscapeOneShotConfig::onSetup() {
return EventHandlerResult::OK;
}

EventHandlerResult EscapeOneShotConfig::onNameQuery() {
return ::Focus.sendName(F("EscapeOneShot"));
}

EventHandlerResult EscapeOneShotConfig::onFocusEvent(const char *input) {
const char *cmd_cancel_key = PSTR("escape_oneshot.cancel_key");
if (::Focus.inputMatchesHelp(input))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/key_defs.h" // for Key, Key_Escape
#include "kaleidoscope/plugin.h" // for Plugin

#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin
// DEPRECATED: `OneShotCancelKey` doesn't match our normal naming, and should
// eventually be removed.
constexpr Key OneShotCancelKey{kaleidoscope::ranges::OS_CANCEL};
Expand Down Expand Up @@ -53,11 +53,13 @@ class EscapeOneShot : public kaleidoscope::Plugin {
Settings settings_ = {.cancel_oneshot_key = Key_Escape};
};

class EscapeOneShotConfig : public Plugin {
class EscapeOneShotConfig : public Plugin, FocusPlugin {
public:
EventHandlerResult onSetup();
EventHandlerResult onFocusEvent(const char *input);
EventHandlerResult onNameQuery();

protected:
const __FlashStringHelper *getPluginName() const override { F("EscapeOneShot"); }

private:
uint16_t settings_base_;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// FocusPlugin.h
#pragma once
#include <Kaleidoscope-FocusSerial.h> // for Focus, FocusSerial

#include "kaleidoscope/plugin.h" // for Plugin

namespace kaleidoscope {
namespace plugin {

class FocusPlugin {
public:
EventHandlerResult onNameQuery() {
return ::Focus.sendName(getPluginName());
}

protected:
// New virtual method to be overridden by derived classes to return the plugin name
virtual const __FlashStringHelper *getPluginName() const = 0;
};

} // namespace plugin
} // namespace kaleidoscope
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
namespace kaleidoscope {
namespace plugin {

EventHandlerResult LayerFocus::onNameQuery() {
return ::Focus.sendName(F("LayerFocus"));
}

EventHandlerResult LayerFocus::onFocusEvent(const char *input) {
const char *cmd_activate = PSTR("layer.activate");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@

#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/plugin.h" // for Plugin

#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin
namespace kaleidoscope {
namespace plugin {

class LayerFocus : public kaleidoscope::Plugin {
class LayerFocus : public kaleidoscope::Plugin, public FocusPlugin {
public:
EventHandlerResult onNameQuery();
EventHandlerResult onFocusEvent(const char *input);

protected:
const __FlashStringHelper *getPluginName() const override { F("LayerFocus"); }
};

} // namespace plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,6 @@ EventHandlerResult Macros::onKeyEvent(KeyEvent &event) {
return EventHandlerResult::OK;
}

EventHandlerResult Macros::onNameQuery() {
return ::Focus.sendName(F("Macros"));
}

} // namespace plugin
} // namespace kaleidoscope
Expand Down
8 changes: 5 additions & 3 deletions plugins/Kaleidoscope-Macros/src/kaleidoscope/plugin/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
#include "kaleidoscope/key_defs.h" // for Key
#include "kaleidoscope/plugin.h" // for Plugin
#include "kaleidoscope/plugin/Macros/MacroSteps.h" // for macro_t, MACRO_NONE

#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin
// =============================================================================
// Define this function in a Kaleidoscope sketch in order to trigger Macros.
const macro_t *macroAction(uint8_t macro_id, KeyEvent &event);

namespace kaleidoscope {
namespace plugin {

class Macros : public kaleidoscope::Plugin {
class Macros : public kaleidoscope::Plugin, public FocusPlugin {
public:
/// Send a key press event from a Macro
///
Expand Down Expand Up @@ -88,12 +88,14 @@ class Macros : public kaleidoscope::Plugin {

// ---------------------------------------------------------------------------
// Event handlers
EventHandlerResult onNameQuery();
EventHandlerResult onKeyEvent(KeyEvent &event);
EventHandlerResult beforeReportingState(const KeyEvent &event) {
return ::MacroSupport.beforeReportingState(event);
}

protected:
const __FlashStringHelper *getPluginName() const override { F("Macros"); }

private:
// Translate and ASCII character value to a corresponding `Key`
Key lookupAsciiCode(uint8_t ascii_code) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ bool MouseKeys::isMouseWheelKey(const Key &key) const {
// =============================================================================
// Event Handlers

// -----------------------------------------------------------------------------
EventHandlerResult MouseKeys::onNameQuery() {
return ::Focus.sendName(F("MouseKeys"));
}


// -----------------------------------------------------------------------------
EventHandlerResult MouseKeys::onSetup() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@

#include <stdint.h> // for uint8_t, uint16_t

#include "kaleidoscope/KeyEvent.h" // for KeyEvent
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/key_defs.h" // for Key
#include "kaleidoscope/plugin.h" // for Plugin

#include "kaleidoscope/KeyEvent.h" // for KeyEvent
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/key_defs.h" // for Key
#include "kaleidoscope/plugin.h" // for Plugin
#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin
#include "kaleidoscope/plugin/mousekeys/MouseWarpModes.h" // for warp modes
// =============================================================================
// Deprecated MousKeys code
Expand Down Expand Up @@ -65,7 +65,7 @@

namespace kaleidoscope {
namespace plugin {
class MouseKeys : public kaleidoscope::Plugin {
class MouseKeys : public kaleidoscope::Plugin, public FocusPlugin {
public:
#ifndef NDEPRECATED
DEPRECATED(MOUSEKEYS_SPEED)
Expand Down Expand Up @@ -136,7 +136,6 @@ class MouseKeys : public kaleidoscope::Plugin {
}

EventHandlerResult onSetup();
EventHandlerResult onNameQuery();
EventHandlerResult afterEachCycle();
EventHandlerResult onKeyEvent(KeyEvent &event);
EventHandlerResult onAddToReport(Key key);
Expand All @@ -157,6 +156,9 @@ class MouseKeys : public kaleidoscope::Plugin {
// directly. Mainly useful for calls to `Runtime.storage.get()`/`.put()`.
friend class MouseKeysConfig;

protected:
const __FlashStringHelper *getPluginName() const override { F("MouseKeys"); }

private:
static constexpr uint8_t cursor_update_interval_ = 4;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
namespace kaleidoscope {
namespace plugin {

EventHandlerResult Qukeys::onNameQuery() {
return ::Focus.sendName(F("Qukeys"));
}

// This is the event handler. It ignores certain events, but mostly just adds
// them to the Qukeys event queue.
Expand Down
7 changes: 5 additions & 2 deletions plugins/Kaleidoscope-Qukeys/src/kaleidoscope/plugin/Qukeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "kaleidoscope/event_handler_result.h" // for EventHandlerResult
#include "kaleidoscope/key_defs.h" // for Key, Key_Transparent
#include "kaleidoscope/plugin.h" // for Plugin
#include "kaleidoscope/plugin/FocusPlugin.h" // for FocusPlugin

// IWYU pragma: no_include "HIDAliases.h"

Expand Down Expand Up @@ -75,7 +76,7 @@ struct Qukey {
};


class Qukeys : public kaleidoscope::Plugin {
class Qukeys : public kaleidoscope::Plugin, public FocusPlugin {

public:
// Methods for turning the plugin on and off.
Expand Down Expand Up @@ -153,10 +154,12 @@ class Qukeys : public kaleidoscope::Plugin {
static constexpr int8_t layer_wildcard{-1};

// Kaleidoscope hook functions.
EventHandlerResult onNameQuery();
EventHandlerResult onKeyswitchEvent(KeyEvent &event);
EventHandlerResult afterEachCycle();

protected:
const __FlashStringHelper *getPluginName() const override { F("Qukeys"); }

private:
// An array of Qukey objects in PROGMEM.
Qukey const *qukeys_{nullptr};
Expand Down
Loading

0 comments on commit 5c73ff0

Please sign in to comment.