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

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

@ -59,10 +59,10 @@ const macro_t *macroAction(uint8_t macro_index, uint8_t key_state) {
return MACRO_NONE; return MACRO_NONE;
} }
void setup() { // Use Qukeys
// Use Qukeys KALEIDOSCOPE_INIT_PLUGINS(Qukeys, Macros);
Kaleidoscope.use(&Qukeys);
void setup() {
QUKEYS( QUKEYS(
kaleidoscope::Qukey(0, 2, 1, Key_LeftGui), // A/cmd kaleidoscope::Qukey(0, 2, 1, Key_LeftGui), // A/cmd
kaleidoscope::Qukey(0, 2, 2, Key_LeftAlt), // S/alt kaleidoscope::Qukey(0, 2, 2, Key_LeftAlt), // S/alt
@ -73,9 +73,6 @@ void setup() {
Qukeys.setTimeout(200); Qukeys.setTimeout(200);
Qukeys.setReleaseDelay(20); Qukeys.setReleaseDelay(20);
// To toggle Qukeys off and on, we use a macro
Kaleidoscope.use(&Macros);
Kaleidoscope.setup(); Kaleidoscope.setup();
} }

@ -238,15 +238,17 @@ bool Qukeys::isQukey(uint8_t addr) {
return (isDualUse(addr) || lookupQukey(addr) != QUKEY_NOT_FOUND); return (isDualUse(addr) || lookupQukey(addr) != QUKEY_NOT_FOUND);
} }
Key Qukeys::keyScanHook(Key mapped_key, byte row, byte col, uint8_t key_state) { EventHandlerResult Qukeys::onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state) {
// If key_addr is not a physical key, ignore it; some other plugin injected it // If key_addr is not a physical key, ignore it; some other plugin injected it
if (row >= ROWS || col >= COLS || (key_state & INJECTED) != 0) if (row >= ROWS || col >= COLS || (key_state & INJECTED) != 0)
return mapped_key; return EventHandlerResult::OK;
// If Qukeys is turned off, continue to next plugin // If Qukeys is turned off, continue to next plugin
if (!active_) if (!active_) {
return getDualUsePrimaryKey(mapped_key); mapped_key = getDualUsePrimaryKey(mapped_key);
return EventHandlerResult::OK;
}
// get key addr & qukey (if any) // get key addr & qukey (if any)
uint8_t key_addr = addr::addr(row, col); uint8_t key_addr = addr::addr(row, col);
@ -257,18 +259,20 @@ Key Qukeys::keyScanHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
// If it's a DualUse key, we still need to update its keycode // If it's a DualUse key, we still need to update its keycode
if (isDualUse(mapped_key)) { if (isDualUse(mapped_key)) {
if (getQukeyState(key_addr) == QUKEY_STATE_ALTERNATE) { if (getQukeyState(key_addr) == QUKEY_STATE_ALTERNATE) {
return getDualUseAlternateKey(mapped_key); mapped_key = getDualUseAlternateKey(mapped_key);
} else { } else {
return getDualUsePrimaryKey(mapped_key); mapped_key = getDualUsePrimaryKey(mapped_key);
} }
} }
// ...otherwise, just continue to the next plugin // ...otherwise, just continue to the next plugin
return mapped_key; return EventHandlerResult::OK;
} }
// If the key isn't active, and didn't just toggle off, continue to next plugin // If the key isn't active, and didn't just toggle off, continue to next plugin
if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) if (!keyIsPressed(key_state) && !keyWasPressed(key_state)) {
return getDualUsePrimaryKey(mapped_key); mapped_key = getDualUsePrimaryKey(mapped_key);
return EventHandlerResult::OK;
}
// If the key was just pressed: // If the key was just pressed:
if (keyToggledOn(key_state)) { if (keyToggledOn(key_state)) {
@ -276,13 +280,13 @@ Key Qukeys::keyScanHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
if (key_queue_length_ == 0 && if (key_queue_length_ == 0 &&
! isDualUse(mapped_key) && ! isDualUse(mapped_key) &&
qukey_index == QUKEY_NOT_FOUND) { qukey_index == QUKEY_NOT_FOUND) {
return mapped_key; return EventHandlerResult::OK;
} }
// Otherwise, queue the key and stop processing: // Otherwise, queue the key and stop processing:
enqueue(key_addr); enqueue(key_addr);
// flushQueue() has already handled this key release // flushQueue() has already handled this key release
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
// In all other cases, we need to know if the key is queued already // In all other cases, we need to know if the key is queued already
@ -294,19 +298,21 @@ Key Qukeys::keyScanHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
if (queue_index == QUKEY_NOT_FOUND) { if (queue_index == QUKEY_NOT_FOUND) {
// If a qukey was released while in its alternate state, change its keycode // If a qukey was released while in its alternate state, change its keycode
if (isDualUse(mapped_key)) { if (isDualUse(mapped_key)) {
if (getQukeyState(key_addr) == QUKEY_STATE_ALTERNATE) if (getQukeyState(key_addr) == QUKEY_STATE_ALTERNATE) {
return getDualUseAlternateKey(mapped_key); mapped_key = getDualUseAlternateKey(mapped_key);
return getDualUsePrimaryKey(mapped_key); } else {
mapped_key = getDualUsePrimaryKey(mapped_key);
}
} else if (qukey_index != QUKEY_NOT_FOUND) { } else if (qukey_index != QUKEY_NOT_FOUND) {
if (getQukeyState(key_addr) == QUKEY_STATE_ALTERNATE) if (getQukeyState(key_addr) == QUKEY_STATE_ALTERNATE) {
return qukeys[qukey_index].alt_keycode; mapped_key = qukeys[qukey_index].alt_keycode;
return mapped_key; }
} }
return mapped_key; return EventHandlerResult::OK;
} }
flushQueue(queue_index); flushQueue(queue_index);
flushQueue(); flushQueue();
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
// Otherwise, the key is still pressed // Otherwise, the key is still pressed
@ -316,29 +322,32 @@ Key Qukeys::keyScanHook(Key mapped_key, byte row, byte col, uint8_t key_state) {
! isDualUse(mapped_key)) { ! isDualUse(mapped_key)) {
// If the key was pressed before the keys in the queue, proceed: // If the key was pressed before the keys in the queue, proceed:
if (queue_index == QUKEY_NOT_FOUND) { if (queue_index == QUKEY_NOT_FOUND) {
return mapped_key; return EventHandlerResult::OK;
} else { } else {
// suppress this keypress; it's still in the queue // suppress this keypress; it's still in the queue
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
} }
// If the qukey is not in the queue, check its state // If the qukey is not in the queue, check its state
if (queue_index == QUKEY_NOT_FOUND) { if (queue_index == QUKEY_NOT_FOUND) {
if (getQukeyState(key_addr) == QUKEY_STATE_ALTERNATE) { if (getQukeyState(key_addr) == QUKEY_STATE_ALTERNATE) {
if (isDualUse(mapped_key)) if (isDualUse(mapped_key)) {
return getDualUseAlternateKey(mapped_key); mapped_key = getDualUseAlternateKey(mapped_key);
return qukeys[qukey_index].alt_keycode; } else {
mapped_key = qukeys[qukey_index].alt_keycode;
}
} else { // qukey_state == QUKEY_STATE_PRIMARY } else { // qukey_state == QUKEY_STATE_PRIMARY
return getDualUsePrimaryKey(mapped_key); mapped_key = getDualUsePrimaryKey(mapped_key);
} }
return EventHandlerResult::OK;
} }
// else state is undetermined; block. I could check timeouts here, // else state is undetermined; block. I could check timeouts here,
// but I'd rather do that in the pre-report hook // but I'd rather do that in the pre-report hook
return Key_NoKey; return EventHandlerResult::EVENT_CONSUMED;
} }
void Qukeys::preReportHook(void) { EventHandlerResult Qukeys::beforeReportingState() {
uint16_t current_time = millis(); uint16_t current_time = millis();
@ -351,7 +360,7 @@ void Qukeys::preReportHook(void) {
flushKey(QUKEY_STATE_PRIMARY, WAS_PRESSED); flushKey(QUKEY_STATE_PRIMARY, WAS_PRESSED);
flushQueue(); flushQueue();
} }
return; return EventHandlerResult::OK;
} }
} }
@ -365,14 +374,11 @@ void Qukeys::preReportHook(void) {
break; break;
} }
} }
}
void Qukeys::loopHook(bool post_clear) { return EventHandlerResult::OK;
if (!post_clear)
return preReportHook();
} }
void Qukeys::begin() { EventHandlerResult Qukeys::onSetup() {
// initializing the key_queue seems unnecessary, actually // initializing the key_queue seems unnecessary, actually
for (int8_t i = 0; i < QUKEYS_QUEUE_MAX; i++) { for (int8_t i = 0; i < QUKEYS_QUEUE_MAX; i++) {
key_queue_[i].addr = QUKEY_UNKNOWN_ADDR; key_queue_[i].addr = QUKEY_UNKNOWN_ADDR;
@ -380,10 +386,31 @@ void Qukeys::begin() {
} }
key_queue_length_ = 0; key_queue_length_ = 0;
Kaleidoscope.useEventHandlerHook(keyScanHook); return EventHandlerResult::OK;
Kaleidoscope.useLoopHook(loopHook);
} }
// Legacy V1 API
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
void Qukeys::begin() {
onSetup();
Kaleidoscope.useEventHandlerHook(legacyEventHandler);
Kaleidoscope.useLoopHook(legacyLoopHook);
}
Key Qukeys::legacyEventHandler(Key mapped_key, byte row, byte col, uint8_t key_state) {
EventHandlerResult r = ::Qukeys.onKeyswitchEvent(mapped_key, row, col, key_state);
if (r == EventHandlerResult::OK)
return mapped_key;
return Key_NoKey;
}
void Qukeys::legacyLoopHook(bool is_post_clear) {
if (is_post_clear)
return;
::Qukeys.beforeReportingState();
}
#endif
} // namespace kaleidoscope { } // namespace kaleidoscope {
kaleidoscope::Qukeys Qukeys; kaleidoscope::Qukeys Qukeys;

@ -72,7 +72,7 @@ struct QueueItem {
}; };
// The plugin itself // The plugin itself
class Qukeys : public KaleidoscopePlugin { class Qukeys : public kaleidoscope::Plugin {
// I could use a bitfield to get the state values, but then we'd // I could use a bitfield to get the state values, but then we'd
// have to check the key_queue (there are three states). Or use a // have to check the key_queue (there are three states). Or use a
// second bitfield for the indeterminite state. Using a bitfield // second bitfield for the indeterminite state. Using a bitfield
@ -81,7 +81,6 @@ class Qukeys : public KaleidoscopePlugin {
public: public:
Qukeys(void); Qukeys(void);
void begin(void) final;
static void activate(void) { static void activate(void) {
active_ = true; active_ = true;
} }
@ -101,6 +100,16 @@ class Qukeys : public KaleidoscopePlugin {
static Qukey * qukeys; static Qukey * qukeys;
static uint8_t qukeys_count; static uint8_t qukeys_count;
EventHandlerResult onSetup();
EventHandlerResult onKeyswitchEvent(Key &mapped_key, byte row, byte col, uint8_t key_state);
EventHandlerResult beforeReportingState();
#if KALEIDOSCOPE_ENABLE_V1_PLUGIN_API
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:
static bool active_; static bool active_;
static uint16_t time_limit_; static uint16_t time_limit_;
@ -125,11 +134,6 @@ class Qukeys : public KaleidoscopePlugin {
static void flushQueue(int8_t index); static void flushQueue(int8_t index);
static void flushQueue(void); static void flushQueue(void);
static bool isQukey(uint8_t addr); static bool isQukey(uint8_t addr);
static Key keyScanHook(Key mapped_key, byte row, byte col, uint8_t key_state);
static void preReportHook(void);
static void postReportHook(void) {}
static void loopHook(bool post_clear);
}; };
} // namespace kaleidoscope { } // namespace kaleidoscope {

Loading…
Cancel
Save