Updated to use the new plugin APIs

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/389/head
Gergely Nagy 7 years ago
parent 0c6054018f
commit d668dce127

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

@ -1,6 +1,6 @@
/* -*- mode: c++ -*-
* 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
* it under the terms of the GNU General Public License as published by
@ -19,6 +19,7 @@
#include <Kaleidoscope.h>
#include <Kaleidoscope-OneShot.h>
// *INDENT-OFF*
const Key keymaps[][ROWS][COLS] PROGMEM = {
[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_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_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.use(&OneShot);
KALEIDOSCOPE_INIT_PLUGINS(OneShot);
void setup() {
Kaleidoscope.setup();
}

@ -78,7 +78,7 @@ void OneShot::injectNormalKey(uint8_t idx, uint8_t key_state) {
key.keyCode = LAYER_SHIFT_OFFSET + idx - 8;
}
positionToCoords(idx, &row, &col);
positionToCoords(positions_[idx], &row, &col);
handleKeyswitchEvent(key, row, col, key_state | INJECTED);
}
@ -91,24 +91,24 @@ void OneShot::cancelOneShot(uint8_t idx) {
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;
if (key_state & INJECTED)
return mapped_key;
if (keyState & INJECTED)
return EventHandlerResult::OK;
if (!state_.all) {
if (!isOneShotKey(mapped_key)) {
return mapped_key;
return EventHandlerResult::OK;
}
if (keyToggledOff(key_state)) {
if (keyToggledOff(keyState)) {
clearPressed(idx);
if (mapped_key >= ranges::OSL_FIRST && mapped_key <= ranges::OSL_LAST) {
should_mask_on_interrupt_ = true;
}
} else if (keyToggledOn(key_state)) {
} else if (keyToggledOn(keyState)) {
start_time_ = millis();
positions_[idx] = row * COLS + col;
setPressed(idx);
@ -118,21 +118,21 @@ Key OneShot::eventHandlerHook(Key mapped_key, byte row, byte col, uint8_t key_st
activateOneShot(idx);
}
return Key_NoKey;
return EventHandlerResult::EVENT_CONSUMED;
}
if (!keyIsPressed(key_state) && !keyWasPressed(key_state))
return mapped_key;
if (!keyIsPressed(keyState) && !keyWasPressed(keyState))
return EventHandlerResult::OK;
if (isOneShotKey(mapped_key)) {
if (isSticky_(idx)) {
if (keyToggledOn(key_state)) { // maybe on _off instead?
if (keyToggledOn(keyState)) { // maybe on _off instead?
saveAsPrevious(mapped_key);
clearSticky(idx);
cancelOneShot(idx);
}
} else {
if (keyToggledOff(key_state)) {
if (keyToggledOff(keyState)) {
clearPressed(idx);
if ((millis() - start_time_) >= hold_time_out) {
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);
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
if (keyIsPressed(key_state)) {
if (keyIsPressed(keyState)) {
saveAsPrevious(mapped_key);
if (!isModifier(mapped_key) && (mapped_key.flags != (KEY_FLAGS | SYNTHETIC | SWITCH_TO_KEYMAP))) {
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)
return;
return EventHandlerResult::OK;
if (is_post_clear) {
if (hasTimedOut())
cancel();
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;
clearSticky(i);
cancelOneShot(i);
clearPressed(i);
}
} else if (isOneShot(i) && !isPressed(i)) {
for (uint8_t i = 0; i < 8; i++) {
if (isOneShot(i)) {
activateOneShot(i);
}
}
return EventHandlerResult::OK;
}
EventHandlerResult OneShot::afterEachCycle() {
if (!state_.all)
return EventHandlerResult::OK;
if (hasTimedOut())
cancel();
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;
clearSticky(i);
cancelOneShot(i);
clearPressed(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);
} else if (isOneShot(i) && !isPressed(i)) {
is_cancelled = true;
cancelOneShot(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) {
Kaleidoscope.useEventHandlerHook(eventHandlerHook);
Kaleidoscope.useLoopHook(loopHook);
void OneShot::inject(Key mapped_key, uint8_t key_state) {
onKeyswitchEvent(mapped_key, UNKNOWN_KEYSWITCH_LOCATION, key_state);
}
// --- glue code ---
bool OneShot::isActive(void) {
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;
}
void OneShot::inject(Key key, uint8_t key_state) {
eventHandlerHook(key, UNKNOWN_KEYSWITCH_LOCATION, key_state);
// Legacy V1 API
#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 {
class OneShot : public KaleidoscopePlugin {
class OneShot : public kaleidoscope::Plugin {
public:
OneShot(void);
void begin(void) final;
OneShot(void) {}
static bool isOneShotKey(Key key) {
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);
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:
typedef union {
@ -75,9 +84,6 @@ class OneShot : public KaleidoscopePlugin {
static void injectNormalKey(uint8_t idx, uint8_t key_state);
static void activateOneShot(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