To make replacing work sanely, we first NULL out both the eventHandlers and loopHooks arrays in the Keyboardio_ constructor. This allows the replace functions to just run through the whole array, and either see hook pointers there, or NULL. So they don't need to be afraid of garbage being there. This makes the replacing very easy: run through the array, and replace the first occurrence of the old hook with the new. This further simplifies addition: the old hook we pass in, will be NULL. If we run out of space, it silently fails, like before. Replacing hooks is important for cases where one wants to build features that can be toggled off, or their behaviour otherwise changed at run-time. In this case, the most efficent way is to replace the hook, which is what these new helpers allow us to do. This closes #36. Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>pull/40/head
parent
9fff7d8519
commit
2734edb2b0
@ -1,27 +1,31 @@
|
||||
#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)
|
||||
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;
|
||||
|
||||
eventHandlers[i] = hook;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
loop_hook_add (custom_loop_t hook) {
|
||||
byte i;
|
||||
|
||||
for (i = 0; i < HOOK_MAX && loopHooks[i] != NULL; i++) {
|
||||
}
|
||||
event_handler_hook_add (custom_handler_t hook) {
|
||||
event_handler_hook_replace ((custom_handler_t)NULL, hook);
|
||||
}
|
||||
|
||||
if (i == HOOK_MAX)
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
loopHooks[i] = hook;
|
||||
void
|
||||
loop_hook_add (custom_loop_t hook) {
|
||||
loop_hook_replace ((custom_loop_t)NULL, hook);
|
||||
}
|
||||
|
Loading…
Reference in new issue