From 5a677ce7f02a15c716475052dc7ec1899d026a50 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 12 Nov 2016 14:59:16 +0100 Subject: [PATCH] Small hook rework Two things are done here: loop() hooks are introduced, and both loop and event handler hooks are pre-allocated for HOOK_MAX (64 by default) elements. Two helpers are introduced too, that make it easier to append a new hook to the end of these arrays. Signed-off-by: Gergely Nagy --- KeyboardioFirmware.cpp | 7 +++++++ hooks.cpp | 27 +++++++++++++++++++++++++++ hooks.h | 15 +++++++++++++++ key_events.cpp | 5 ++--- key_events.h | 4 +--- 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 hooks.cpp create mode 100644 hooks.h diff --git a/KeyboardioFirmware.cpp b/KeyboardioFirmware.cpp index 8ad84dbb..b342446f 100644 --- a/KeyboardioFirmware.cpp +++ b/KeyboardioFirmware.cpp @@ -16,8 +16,15 @@ Keyboardio_::setup(void) { temporary_keymap = primary_keymap = Storage.load_primary_keymap(KEYMAPS); } +custom_loop_t loopHooks[HOOK_MAX] = {NULL}; + void Keyboardio_::loop(void) { + for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) { + custom_loop_t hook = loopHooks[i]; + (*hook)(); + } + KeyboardHardware.scan_matrix(); LEDControl.update(temporary_keymap); Keyboard.sendReport(); diff --git a/hooks.cpp b/hooks.cpp new file mode 100644 index 00000000..182cabde --- /dev/null +++ b/hooks.cpp @@ -0,0 +1,27 @@ +#include "hooks.h" + +void +event_handler_hook_add (custom_handler_t hook) { + byte i; + + for (i = 0; i < HOOK_MAX && eventHandlers[i] != NULL; i++) { + } + + if (i == HOOK_MAX) + return; + + eventHandlers[i] = hook; +} + +void +loop_hook_add (custom_loop_t hook) { + byte i; + + for (i = 0; i < HOOK_MAX && loopHooks[i] != NULL; i++) { + } + + if (i == HOOK_MAX) + return; + + loopHooks[i] = hook; +} diff --git a/hooks.h b/hooks.h new file mode 100644 index 00000000..93b4057d --- /dev/null +++ b/hooks.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#define HOOK_MAX 64 + +typedef bool (*custom_handler_t)(byte row, byte col, uint8_t currentState, uint8_t previousState); +extern custom_handler_t eventHandlers[HOOK_MAX]; + +void event_handler_hook_add (custom_handler_t hook); + +typedef void (*custom_loop_t)(void); +extern custom_loop_t loopHooks[HOOK_MAX]; + +void loop_hook_add (custom_loop_t hook); diff --git a/key_events.cpp b/key_events.cpp index b2cf9ca3..cbc50968 100644 --- a/key_events.cpp +++ b/key_events.cpp @@ -41,8 +41,7 @@ void handle_synthetic_key_event(Key mappedKey, uint8_t currentState, uint8_t pre } } -__attribute__((weak)) -custom_handler_t eventHandlers[] = { +custom_handler_t eventHandlers[HOOK_MAX] = { handle_key_event_default, (custom_handler_t) NULL }; @@ -56,7 +55,7 @@ Key lookup_key(byte keymap, byte row, byte col) { } void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) { - for (byte i = 0; eventHandlers[i] != NULL; i++) { + for (byte i = 0; eventHandlers[i] != NULL && i < HOOK_MAX; i++) { custom_handler_t handler = eventHandlers[i]; if ((*handler)(row, col, currentState, previousState)) return; diff --git a/key_events.h b/key_events.h index df101c9a..da687301 100644 --- a/key_events.h +++ b/key_events.h @@ -8,14 +8,12 @@ #include "Storage.h" #include "keymap_metadata.h" #include "generated/keymaps.h" +#include "hooks.h" //static const Key keymaps[KEYMAPS][ROWS][COLS]; extern uint8_t primary_keymap; extern uint8_t temporary_keymap; -typedef bool (*custom_handler_t)(byte row, byte col, uint8_t currentState, uint8_t previousState); -extern custom_handler_t eventHandlers[]; - // sending events to the computer void handle_synthetic_key_event( Key mappedKey, uint8_t currentState, uint8_t previousState); void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState);