Having the hooks, and the hook methods in the Kaleidoscope object means we don't have to litter the definitions of the arrays around, and that the hooks are more tied to the object. We pollute the global namespace less, and having them in the object means that the hook helper functions will not be optimized out if not used within the Kaleidoscope repo. All in all, this saves us about 56 bytes of code, allows us to remove some hacks, and pulls things that are closely knit, closer together. While there, also changed the name of the `custom_handler_t` and `custom_loop_t` types to `eventHandlerHook` and `loopHook` (both under the `Kaleidoscope_` class), to better reflect what they are for. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>pull/109/head
parent
361146bf4a
commit
f7834f05fc
@ -1,49 +0,0 @@
|
|||||||
#include "hooks.h"
|
|
||||||
|
|
||||||
void
|
|
||||||
event_handler_hook_replace (custom_handler_t oldHook, custom_handler_t newHook) {
|
|
||||||
for (byte i = 0; i < HOOK_MAX; i++) {
|
|
||||||
if (eventHandlers[i] == oldHook) {
|
|
||||||
eventHandlers[i] = newHook;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
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++) {
|
|
||||||
if (loopHooks[i] == oldHook) {
|
|
||||||
loopHooks[i] = newHook;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
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);
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <Arduino.h>
|
|
||||||
#include "key_defs.h"
|
|
||||||
|
|
||||||
#define HOOK_MAX 64
|
|
||||||
|
|
||||||
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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
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_replace (custom_loop_t oldHook, custom_loop_t newHook);
|
|
Loading…
Reference in new issue