Merge pull request #31 from keyboardio/f/plugin-v2

Updated to use the new plugin APIs
pull/389/head
Gergely Nagy 6 years ago committed by GitHub
commit ceaee8a74c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -64,9 +64,9 @@ plugin:
// somewhere in the keymap... // somewhere in the keymap...
OSM(LeftControl), OSL(_FN) OSM(LeftControl), OSL(_FN)
void setup() { KALEIDOSCOPE_INIT_PLUGINS(OneShot);
Kaleidoscope.use(&OneShot);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
} }
``` ```

@ -1,6 +1,6 @@
/* -*- mode: c++ -*- /* -*- mode: c++ -*-
* Kaleidoscope-OneShot -- One-shot modifiers and layers * Kaleidoscope-OneShot -- One-shot modifiers and layers
* Copyright (C) 2016, 2017 Gergely Nagy * Copyright (C) 2016, 2017, 2018 Gergely Nagy
* *
* This program is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -19,6 +19,7 @@
#include <Kaleidoscope.h> #include <Kaleidoscope.h>
#include <Kaleidoscope-OneShot.h> #include <Kaleidoscope-OneShot.h>
// *INDENT-OFF*
const Key keymaps[][ROWS][COLS] PROGMEM = { const Key keymaps[][ROWS][COLS] PROGMEM = {
[0] = KEYMAP_STACKED [0] = KEYMAP_STACKED
( (
@ -32,7 +33,7 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip, Key_skip, Key_6, Key_7, Key_8, Key_9, Key_0, Key_skip,
Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals, Key_Enter, Key_Y, Key_U, Key_I, Key_O, Key_P, Key_Equals,
Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote, Key_H, Key_J, Key_K, Key_L, Key_Semicolon, Key_Quote,
Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus, Key_skip, Key_N, Key_M, Key_Comma, Key_Period, Key_Slash, Key_Minus,
Key_RightShift, OSM(RightAlt), Key_Spacebar, OSM(RightControl), Key_RightShift, OSM(RightAlt), Key_Spacebar, OSM(RightControl),
@ -50,16 +51,17 @@ const Key keymaps[][ROWS][COLS] PROGMEM = {
___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___,
Key_UpArrow, Key_DownArrow, Key_LeftArrow, Key_RightArrow, ___, ___, Key_UpArrow, Key_DownArrow, Key_LeftArrow, Key_RightArrow,___, ___,
___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___, ___,
___, ___, ___, ___, ___, ___, ___, ___,
___), ___),
}; };
// *INDENT-ON*
void setup() { KALEIDOSCOPE_INIT_PLUGINS(OneShot);
Kaleidoscope.use(&OneShot);
void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
} }

@ -78,7 +78,7 @@ void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) {
key.keyCode = LAYER_SHIFT_OFFSET + idx - 8; key.keyCode = LAYER_SHIFT_OFFSET + idx - 8;
} }
positionToCoords(idx, &row, &col); positionToCoords(positions_[idx], &row, &col);
handleKeyswitchEvent(key, row, col, key_state | INJECTED); handleKeyswitchEvent(key, row, col, key_state | INJECTED);
} }
@ -91,24 +91,24 @@ void OneShot::cancelOneShot(uint8_t idx) {
injectNormalKey(idx, WAS_PRESSED); injectNormalKey(idx, WAS_PRESSED);
} }
Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state) { EventHandlerResult OneShot::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t keyState) {
uint8_t idx = mapped_key.raw - ranges::OS_FIRST; uint8_t idx = mapped_key.raw - ranges::OS_FIRST;
if (key_state & INJECTED) if (keyState & INJECTED)
return mapped_key; return EventHandlerResult::OK;
if (!state_.all) { if (!state_.all) {
if (!isOneShotKey(mapped_key)) { if (!isOneShotKey(mapped_key)) {
return mapped_key; return EventHandlerResult::OK;
} }
if (keyToggledOff(key_state)) { if (keyToggledOff(keyState)) {
clearPressed(idx); clearPressed(idx);
if (mapped_key >= ranges::OSL_FIRST && mapped_key <= ranges::OSL_LAST) { if (mapped_key >= ranges::OSL_FIRST && mapped_key <= ranges::OSL_LAST) {
should_mask_on_interrupt_ = true; should_mask_on_interrupt_ = true;
} }
} else if (keyToggledOn(key_state)) { } else if (keyToggledOn(keyState)) {
start_time_ = millis(); start_time_ = millis();
positions_[idx] = row * COLS + col; positions_[idx] = row * COLS + col;
setPressed(idx); setPressed(idx);
@ -118,21 +118,21 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st
activateOneShot(idx); activateOneShot(idx);
} }
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) if (!keyIsPressed(keyState) && !keyWasPressed(keyState))
return mapped_key; return EventHandlerResult::OK;
if (isOneShotKey(mapped_key)) { if (isOneShotKey(mapped_key)) {
if (isSticky_(idx)) { if (isSticky_(idx)) {
if (keyToggledOn(key_state)) { // maybe on _off instead? if (keyToggledOn(keyState)) { // maybe on _off instead?
saveAsPrevious(mapped_key); saveAsPrevious(mapped_key);
clearSticky(idx); clearSticky(idx);
cancelOneShot(idx); cancelOneShot(idx);
} }
} else { } else {
if (keyToggledOff(key_state)) { if (keyToggledOff(keyState)) {
clearPressed(idx); clearPressed(idx);
if ((millis() - start_time_) >= hold_time_out) { if ((millis() - start_time_) >= hold_time_out) {
cancelOneShot(idx); cancelOneShot(idx);
@ -140,7 +140,7 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st
} }
} }
if (keyToggledOn(key_state)) { if (keyToggledOn(keyState)) {
setPressed(idx); setPressed(idx);
bool set_sticky = false; bool set_sticky = false;
@ -169,12 +169,12 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st
} }
} }
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
// ordinary key here, with some event // ordinary key here, with some event
if (keyIsPressed(key_state)) { if (keyIsPressed(keyState)) {
saveAsPrevious(mapped_key); saveAsPrevious(mapped_key);
if (!isModifier(mapped_key) && (mapped_key.flags != (KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP))) { if (!isModifier(mapped_key) && (mapped_key.flags != (KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP))) {
if (should_mask_on_interrupt_) if (should_mask_on_interrupt_)
@ -183,59 +183,62 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st
} }
} }
return mapped_key; return EventHandlerResult::OK;
} }
void OneShot::loopHook(bool is_post_clear) { EventHandlerResult OneShot::beforeReportingState() {
if (!state_.all) if (!state_.all)
return; return EventHandlerResult::OK;
if (is_post_clear) { for (uint8_t i = 0; i < 8; i++) {
if (hasTimedOut()) if (isOneShot(i)) {
cancel(); activateOneShot(i);
}
bool is_cancelled = false; }
for (uint8_t i = 0; i < 32; i++) { return EventHandlerResult::OK;
if (should_cancel_) { }
if (isSticky_(i)) {
if (should_cancel_stickies_) { EventHandlerResult OneShot::afterEachCycle() {
is_cancelled = true; if (!state_.all)
clearSticky(i); return EventHandlerResult::OK;
cancelOneShot(i);
clearPressed(i); if (hasTimedOut())
} cancel();
} else if (isOneShot(i) && !isPressed(i)) {
bool is_cancelled = false;
for (uint8_t i = 0; i < 32; i++) {
if (should_cancel_) {
if (isSticky_(i)) {
if (should_cancel_stickies_) {
is_cancelled = true; is_cancelled = true;
clearSticky(i);
cancelOneShot(i); cancelOneShot(i);
clearPressed(i);
} }
} } else if (isOneShot(i) && !isPressed(i)) {
} is_cancelled = true;
cancelOneShot(i);
if (is_cancelled) {
should_cancel_ = false;
should_cancel_stickies_ = false;
should_mask_on_interrupt_ = false;
}
} else {
for (uint8_t i = 0; i < 8; i++) {
if (isOneShot(i)) {
activateOneShot(i);
} }
} }
} }
}
// --- glue code --- if (is_cancelled) {
should_cancel_ = false;
should_cancel_stickies_ = false;
should_mask_on_interrupt_ = false;
}
OneShot::OneShot(void) { return EventHandlerResult::OK;
} }
void OneShot::begin(void) { void OneShot::inject(Key mapped_key, uint8_t key_state) {
Kaleidoscope.useEventHandlerHook(eventHandlerHook); onKeyswitchEvent(mapped_key, UNKNOWN_KEYSWITCH_LOCATION, key_state);
Kaleidoscope.useLoopHook(loopHook);
} }
// --- glue code ---
bool OneShot::isActive(void) { bool OneShot::isActive(void) {
return (state_.all && !hasTimedOut()) || (pressed_state_.all) || (sticky_state_.all); return (state_.all && !hasTimedOut()) || (pressed_state_.all) || (sticky_state_.all);
} }
@ -264,9 +267,28 @@ void OneShot::cancel(bool with_stickies) {
should_cancel_stickies_ = with_stickies; should_cancel_stickies_ = with_stickies;
} }
void OneShot::inject(Key key, uint8_t key_state) { // Legacy V1 API
eventHandlerHook(key, UNKNOWN_KEYSWITCH_LOCATION, key_state); #if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void OneShot::begin() {
Kaleidoscope.useEventHandlerHook(legacyEventHandler);
Kaleidoscope.useLoopHook(legacyLoopHook);
}
Key OneShot::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) {
EventHandlerResult r = ::OneShot.onKeyswitchEvent(mapped_key, row, col, key_state);
if (r == EventHandlerResult::OK)
return mapped_key;
return Key_NoKey;
}
void OneShot::legacyLoopHook(bool is_post_clear) {
if (is_post_clear) {
::OneShot.afterEachCycle();
} else {
::OneShot.beforeReportingState();
}
} }
#endif
} }

@ -26,11 +26,9 @@
namespace kaleidoscope { namespace kaleidoscope {
class OneShot : public KaleidoscopePlugin { class OneShot : public kaleidoscope::Plugin {
public: public:
OneShot(void); OneShot(void) {}
void begin(void) final;
static bool isOneShotKey(Key key) { static bool isOneShotKey(Key key) {
return (key.raw >= kaleidoscope::ranges::OS_FIRST && key.raw <= kaleidoscope::ranges::OS_LAST); return (key.raw >= kaleidoscope::ranges::OS_FIRST && key.raw <= kaleidoscope::ranges::OS_LAST);
@ -50,7 +48,18 @@ class OneShot : public KaleidoscopePlugin {
static bool isModifierActive(Key key); static bool isModifierActive(Key key);
void inject(Key key, uint8_t key_state); EventHandlerResult beforeReportingState();
EventHandlerResult afterEachCycle();
EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t keyState);
void inject(Key mapped_key, uint8_t key_state);
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
protected:
void begin();
static Key legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state);
static void legacyLoopHook(bool is_post_clear);
#endif
private: private:
typedef union { typedef union {
@ -75,9 +84,6 @@ class OneShot : public KaleidoscopePlugin {
static void injectNormalKey(uint8_t idx, uint8_t key_state); static void injectNormalKey(uint8_t idx, uint8_t key_state);
static void activateOneShot(uint8_t idx); static void activateOneShot(uint8_t idx);
static void cancelOneShot(uint8_t idx); static void cancelOneShot(uint8_t idx);
static Key eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_state);
static void loopHook(bool is_post_clear);
}; };
} }

Loading…
Cancel
Save