From aed2fc8d98c2fa90f29f703e6d373f9fdebb5d46 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sun, 25 Dec 2016 09:20:42 +0100 Subject: [PATCH] Event hooks reworked, again Instead of returning a bool, to signal whether further processing should be done, return a Key. Reason being, if we want to replace a key with another, for subsequent handlers, it is a lot easier if we can modify what gets passed along, than it is to inject a key, and try to avoid loops and infinite recursion. Nevertheless, injecting keys is still possible. This is not immediately useful for the core firmware, but makes it trivially easy to upgrade keys from their normal behaviour to something special: for example, a one-shot handler can auto-promote modifiers to one-shot, simply by scheduling a promoter handler before the real one. Signed-off-by: Gergely Nagy --- libraries/Keyboardio-Macros/src/Keyboardio-Macros.cpp | 8 ++++---- .../Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp | 6 +++--- src/TestMode.cpp | 4 ++-- src/TestMode.h | 2 +- src/hooks.h | 2 +- src/key_events.cpp | 3 ++- src/layers.cpp | 6 +++--- 7 files changed, 16 insertions(+), 15 deletions(-) diff --git a/libraries/Keyboardio-Macros/src/Keyboardio-Macros.cpp b/libraries/Keyboardio-Macros/src/Keyboardio-Macros.cpp index 15cadd54..4d052d6a 100644 --- a/libraries/Keyboardio-Macros/src/Keyboardio-Macros.cpp +++ b/libraries/Keyboardio-Macros/src/Keyboardio-Macros.cpp @@ -44,17 +44,17 @@ void Macros_::play(const macro_t *macro_p) { } } -static bool handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState) { +static Key handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState) { if (mappedKey.flags != (SYNTHETIC | IS_MACRO)) - return false; + return mappedKey; if (!key_toggled_on(keyState)) - return true; + return Key_NoKey; const macro_t *m = macroAction(mappedKey.rawKey, keyState); Macros.play(m); - return true; + return Key_NoKey; } Macros_::Macros_ (void) { diff --git a/libraries/Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp b/libraries/Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp index cc03d5fe..a84eb173 100644 --- a/libraries/Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp +++ b/libraries/Keyboardio-MouseKeys/src/Keyboardio-MouseKeys.cpp @@ -22,9 +22,9 @@ static void handle_mouse_key_event(Key mappedKey, uint8_t keyState) { } } -static bool handleMouseKeys(Key mappedKey, byte row, byte col, uint8_t keyState) { +static Key handleMouseKeys(Key mappedKey, byte row, byte col, uint8_t keyState) { if (mappedKey.flags != (SYNTHETIC | IS_MOUSE_KEY)) - return false; + return mappedKey; if (mappedKey.rawKey & KEY_MOUSE_BUTTON) { uint8_t button = mappedKey.rawKey & ~KEY_MOUSE_BUTTON; @@ -46,7 +46,7 @@ static bool handleMouseKeys(Key mappedKey, byte row, byte col, uint8_t keyState) } } - return true; + return Key_NoKey; } MouseKeys_::MouseKeys_(void) { diff --git a/src/TestMode.cpp b/src/TestMode.cpp index e4e9d02d..46daf146 100644 --- a/src/TestMode.cpp +++ b/src/TestMode.cpp @@ -109,9 +109,9 @@ void TestMode_::loop() { TestMatrix(); } -bool handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t keyState) { +Key handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t keyState) { Serial.write(row); - return false; + return mappedKey; } TestMode_ TestMode; diff --git a/src/TestMode.h b/src/TestMode.h index 7455e485..9115876a 100644 --- a/src/TestMode.h +++ b/src/TestMode.h @@ -16,4 +16,4 @@ class TestMode_ { extern TestMode_ TestMode; -bool handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t keyState); +Key handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t keyState); diff --git a/src/hooks.h b/src/hooks.h index 464b4727..615156da 100644 --- a/src/hooks.h +++ b/src/hooks.h @@ -5,7 +5,7 @@ #define HOOK_MAX 64 -typedef bool (*custom_handler_t)(Key mappedKey, byte row, byte col, uint8_t keyState); +typedef Key (*custom_handler_t)(Key mappedKey, byte row, byte col, uint8_t keyState); extern custom_handler_t eventHandlers[HOOK_MAX]; void event_handler_hook_add (custom_handler_t hook); diff --git a/src/key_events.cpp b/src/key_events.cpp index 23030b3b..2bc85a17 100644 --- a/src/key_events.cpp +++ b/src/key_events.cpp @@ -89,7 +89,8 @@ void handle_key_event(Key mappedKey, byte row, byte col, uint8_t keyState) { } for (byte i = 0; eventHandlers[i] != NULL && i < HOOK_MAX; i++) { custom_handler_t handler = eventHandlers[i]; - if ((*handler)(mappedKey, row, col, keyState)) + mappedKey = (*handler)(mappedKey, row, col, keyState); + if (mappedKey.raw == Key_NoKey.raw) return; } handle_key_event_default(mappedKey, row, col, keyState); diff --git a/src/layers.cpp b/src/layers.cpp index 9444b2b4..b422925f 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -33,13 +33,13 @@ static void handle_keymap_key_event(Key keymapEntry, uint8_t keyState) { } } -static bool +static Key layerEventHandler(Key mappedKey, byte row, byte col, uint8_t keyState) { if (mappedKey.flags != (SYNTHETIC | SWITCH_TO_KEYMAP)) - return false; + return mappedKey; handle_keymap_key_event(mappedKey, keyState); - return true; + return Key_NoKey; } Layer_::Layer_ (void) {