From eb02ca5fe6c730189a8380f365fe6d6a57360e3d Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 27 Jan 2017 10:13:18 +0100 Subject: [PATCH 1/3] hooks: Double-registration protection In most cases, one only wants a single copy of a hook. On the other hand, plugins that depend on other plugins, may want to make it easier for the end-user to use the plugin, and call the setup function of the dependent plugins too. In case the end-user calls the same setup function, we'd end up with hooks registered multiple times. To avoid this, protection against double-registration has been introduced. The new `event_handler_hook_use` and `loop_hook_use` functions will only allow one copy of the hook. The `event_handler_hook_append` and `loop_hook_append` functions will, on the other hand, just append the hooks, and not care about protection. The `event_handler_hook_add` and `loop_hook_add` functions are gone, but for the time being, they are aliases to the `_use` functions, until all plugins have been updated, and the aliases can be removed. Signed-off-by: Gergely Nagy --- src/KeyboardioFirmware.cpp | 4 ++-- src/hooks.cpp | 22 ++++++++++++++++++++-- src/hooks.h | 8 ++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/KeyboardioFirmware.cpp b/src/KeyboardioFirmware.cpp index fb942301..44fb8e6d 100644 --- a/src/KeyboardioFirmware.cpp +++ b/src/KeyboardioFirmware.cpp @@ -14,8 +14,8 @@ Keyboardio_::setup(const byte keymap_count) { KeyboardHardware.setup(); LEDControl.setup(); - event_handler_hook_add (NULL); - loop_hook_add (NULL); + event_handler_hook_use (NULL); + loop_hook_use (NULL); Layer.defaultLayer (Storage.load_primary_keymap (keymap_count)); } diff --git a/src/hooks.cpp b/src/hooks.cpp index ed638e76..f304b51a 100644 --- a/src/hooks.cpp +++ b/src/hooks.cpp @@ -11,10 +11,19 @@ event_handler_hook_replace (custom_handler_t oldHook, custom_handler_t newHook) } void -event_handler_hook_add (custom_handler_t hook) { +event_handler_hook_append (custom_handler_t hook) { event_handler_hook_replace ((custom_handler_t)NULL, hook); } +void +event_handler_hook_use (custom_handler_t hook) { + for (byte i = 0; i < HOOK_MAX; i++) { + if (eventHandlers[i] == hook) + return; + } + event_handler_hook_append (hook); +} + void loop_hook_replace (custom_loop_t oldHook, custom_loop_t newHook) { for (byte i = 0; i < HOOK_MAX; i++) { @@ -26,6 +35,15 @@ loop_hook_replace (custom_loop_t oldHook, custom_loop_t newHook) { } void -loop_hook_add (custom_loop_t hook) { +loop_hook_append (custom_loop_t hook) { loop_hook_replace ((custom_loop_t)NULL, hook); } + +void +loop_hook_use (custom_loop_t hook) { + for (byte i = 0; i < HOOK_MAX; i++) { + if (loopHooks[i] == hook) + return; + } + loop_hook_append (hook); +} diff --git a/src/hooks.h b/src/hooks.h index 615156da..99f1dd9e 100644 --- a/src/hooks.h +++ b/src/hooks.h @@ -8,11 +8,15 @@ 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); +void event_handler_hook_use (custom_handler_t hook); +void event_handler_hook_append (custom_handler_t hook); void event_handler_hook_replace (custom_handler_t oldHook, custom_handler_t newHook); +#define event_handler_hook_add event_handler_hook_use typedef void (*custom_loop_t)(bool postClear); extern custom_loop_t loopHooks[HOOK_MAX]; -void loop_hook_add (custom_loop_t hook); +void loop_hook_use (custom_loop_t hook); +void loop_hook_append (custom_loop_t hook); void loop_hook_replace (custom_loop_t oldHook, custom_loop_t newHook); +#define loop_hook_add loop_hook_use From e349f882e7b268b2feb9da59c387bc8afa323a54 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 27 Jan 2017 10:27:47 +0100 Subject: [PATCH 2/3] hooks: Add some documentation about the various hook functions Signed-off-by: Gergely Nagy --- src/hooks.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/hooks.h b/src/hooks.h index 99f1dd9e..686cd207 100644 --- a/src/hooks.h +++ b/src/hooks.h @@ -8,6 +8,24 @@ typedef Key (*custom_handler_t)(Key mappedKey, byte row, byte col, uint8_t keyState); extern custom_handler_t eventHandlers[HOOK_MAX]; +/* + * In most cases, one only wants a single copy of a hook. On the other hand, + * plugins that depend on other plugins, may want to make it easier for the + * end-user to use the plugin, and call the setup function of the dependent + * plugins too. In case the end-user calls the same setup function, we'd end up + * with hooks registered multiple times. + * + * To avoid this, protection against double-registration has been introduced. + * The `event_handler_hook_use` and `loop_hook_use` functions will only allow + * one copy of the hook. The `event_handler_hook_append` and `loop_hook_append` + * functions will, on the other hand, just append the hooks, and not care about + * protection. + * + * The `event_handler_hook_add` and `loop_hook_add` functions are deprecated, + * but for the time being, they are aliases to the `_use` functions, until all + * plugins have been updated, and the aliases can be removed. + */ + void event_handler_hook_use (custom_handler_t hook); void event_handler_hook_append (custom_handler_t hook); void event_handler_hook_replace (custom_handler_t oldHook, custom_handler_t newHook); From 452f3bfc2855975456d11f7a32cb176ce08b02a4 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 27 Jan 2017 10:35:01 +0100 Subject: [PATCH 3/3] hooks: Make the _add functions emit a deprecation warning Turn the `event_handler_hook_add` and `loop_hook_add` aliases into real functions, that emit a deprecation warning during compilation. This makes it a little bit easier to see what needs to be updated still. Signed-off-by: Gergely Nagy --- src/hooks.cpp | 10 ++++++++++ src/hooks.h | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/hooks.cpp b/src/hooks.cpp index f304b51a..c4fa9a08 100644 --- a/src/hooks.cpp +++ b/src/hooks.cpp @@ -24,6 +24,11 @@ event_handler_hook_use (custom_handler_t hook) { event_handler_hook_append (hook); } +void +event_handler_hook_add (custom_handler_t hook) { + event_handler_hook_use (hook); +} + void loop_hook_replace (custom_loop_t oldHook, custom_loop_t newHook) { for (byte i = 0; i < HOOK_MAX; i++) { @@ -47,3 +52,8 @@ loop_hook_use (custom_loop_t hook) { } loop_hook_append (hook); } + +void +loop_hook_add (custom_loop_t hook) { + loop_hook_use (hook); +} diff --git a/src/hooks.h b/src/hooks.h index 686cd207..b8084092 100644 --- a/src/hooks.h +++ b/src/hooks.h @@ -28,13 +28,13 @@ extern custom_handler_t eventHandlers[HOOK_MAX]; void event_handler_hook_use (custom_handler_t hook); void event_handler_hook_append (custom_handler_t hook); +void event_handler_hook_add (custom_handler_t hook) __attribute__((deprecated)); void event_handler_hook_replace (custom_handler_t oldHook, custom_handler_t newHook); -#define event_handler_hook_add event_handler_hook_use typedef void (*custom_loop_t)(bool postClear); extern custom_loop_t loopHooks[HOOK_MAX]; void loop_hook_use (custom_loop_t hook); void loop_hook_append (custom_loop_t hook); +void loop_hook_add (custom_loop_t hook) __attribute__((deprecated)); void loop_hook_replace (custom_loop_t oldHook, custom_loop_t newHook); -#define loop_hook_add loop_hook_use