Merge the hooks into the Kaleidoscope object

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
Gergely Nagy 8 years ago
parent 361146bf4a
commit f7834f05fc

@ -1,9 +1,10 @@
#include "Kaleidoscope.h" #include "Kaleidoscope.h"
#include <stdarg.h> #include <stdarg.h>
Kaleidoscope_::eventHandlerHook Kaleidoscope_::eventHandlers[HOOK_MAX];
Kaleidoscope_::loopHook Kaleidoscope_::loopHooks[HOOK_MAX];
Kaleidoscope_::Kaleidoscope_(void) { Kaleidoscope_::Kaleidoscope_(void) {
memset(eventHandlers, 0, HOOK_MAX * sizeof(custom_handler_t));
memset(loopHooks, 0, HOOK_MAX * sizeof(custom_handler_t));
} }
void void
@ -13,20 +14,15 @@ Kaleidoscope_::setup(const byte keymap_count) {
Keyboard.begin(); Keyboard.begin();
KeyboardHardware.setup(); KeyboardHardware.setup();
event_handler_hook_use (NULL);
loop_hook_use (NULL);
Layer.defaultLayer (KeyboardHardware.load_primary_layer (keymap_count)); Layer.defaultLayer (KeyboardHardware.load_primary_layer (keymap_count));
} }
custom_loop_t loopHooks[HOOK_MAX];
void void
Kaleidoscope_::loop(void) { Kaleidoscope_::loop(void) {
KeyboardHardware.scan_matrix(); KeyboardHardware.scan_matrix();
for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) { for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) {
custom_loop_t hook = loopHooks[i]; loopHook hook = loopHooks[i];
(*hook)(false); (*hook)(false);
} }
@ -34,7 +30,7 @@ Kaleidoscope_::loop(void) {
Keyboard.releaseAll(); Keyboard.releaseAll();
for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) { for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) {
custom_loop_t hook = loopHooks[i]; loopHook hook = loopHooks[i];
(*hook)(true); (*hook)(true);
} }
} }
@ -52,4 +48,52 @@ Kaleidoscope_::use(KaleidoscopePlugin *plugin, ...) {
va_end(ap); va_end(ap);
} }
void
Kaleidoscope_::replaceEventHandlerHook(eventHandlerHook oldHook, eventHandlerHook newHook) {
for (byte i = 0; i < HOOK_MAX; i++) {
if (eventHandlers[i] == oldHook) {
eventHandlers[i] = newHook;
return;
}
}
}
void
Kaleidoscope_::appendEventHandlerHook (eventHandlerHook hook) {
replaceEventHandlerHook((eventHandlerHook)NULL, hook);
}
void
Kaleidoscope_::useEventHandlerHook (eventHandlerHook hook) {
for (byte i = 0; i < HOOK_MAX; i++) {
if (eventHandlers[i] == hook)
return;
}
appendEventHandlerHook(hook);
}
void
Kaleidoscope_::replaceLoopHook(loopHook oldHook, loopHook newHook) {
for (byte i = 0; i < HOOK_MAX; i++) {
if (loopHooks[i] == oldHook) {
loopHooks[i] = newHook;
return;
}
}
}
void
Kaleidoscope_::appendLoopHook(loopHook hook) {
replaceLoopHook((loopHook)NULL, hook);
}
void
Kaleidoscope_::useLoopHook(loopHook hook) {
for (byte i = 0; i < HOOK_MAX; i++) {
if (loopHooks[i] == hook)
return;
}
appendLoopHook (hook);
}
Kaleidoscope_ Kaleidoscope; Kaleidoscope_ Kaleidoscope;

@ -24,6 +24,8 @@ void setup();
#include "key_events.h" #include "key_events.h"
#include "layers.h" #include "layers.h"
#define HOOK_MAX 64
extern HARDWARE_IMPLEMENTATION KeyboardHardware; extern HARDWARE_IMPLEMENTATION KeyboardHardware;
#ifndef VERSION #ifndef VERSION
@ -44,6 +46,45 @@ class Kaleidoscope_ {
void setup(const byte keymap_count); void setup(const byte keymap_count);
void loop(void); void loop(void);
void use(KaleidoscopePlugin *plugin, ...) __attribute__((sentinel)); void use(KaleidoscopePlugin *plugin, ...) __attribute__((sentinel));
// ---- hooks ----
/*
* 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.
*/
typedef Key (*eventHandlerHook)(Key mappedKey, byte row, byte col, uint8_t keyState);
static eventHandlerHook eventHandlers[HOOK_MAX];
static void replaceEventHandlerHook(eventHandlerHook oldHook, eventHandlerHook newHook);
static void appendEventHandlerHook(eventHandlerHook hook);
static void useEventHandlerHook(eventHandlerHook hook);
typedef void (*loopHook)(bool postClear);
static loopHook loopHooks[HOOK_MAX];
static void replaceLoopHook(loopHook oldHook, loopHook newHook);
static void appendLoopHook(loopHook hook);
static void useLoopHook(loopHook hook);
}; };
extern Kaleidoscope_ Kaleidoscope; extern Kaleidoscope_ Kaleidoscope;
/* -- DEPRECATED aliases; remove them when there are no more users. -- */
#define event_handler_hook_use(hook) Kaleidoscope.useEventHandlerHook(hook);
#define event_handler_hook_append(hook) Kaleidoscope.appendEventHandlerHook(hook)
#define event_handler_hook_replace(oldHook, newHook) Kaleidoscope.replaceEventHandlerHook(oldHook, newHook)
#define loop_hook_use(hook) Kaleidoscope.useLoopHook(hook)
#define loop_hook_append(hook) Kaleidoscope.appendLoopHook(hook)
#define loop_hook_replace(oldHook, newHook) Kaleidoscope.replaceLoopHook(oldHook, newHook)

@ -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);

@ -1,5 +1,4 @@
#include "key_events.h" #include "Kaleidoscope.h"
#include "layers.h"
static bool handle_synthetic_key_event(Key mappedKey, uint8_t keyState) { static bool handle_synthetic_key_event(Key mappedKey, uint8_t keyState) {
if (mappedKey.flags & RESERVED) if (mappedKey.flags & RESERVED)
@ -24,8 +23,6 @@ static bool handle_synthetic_key_event(Key mappedKey, uint8_t keyState) {
return true; return true;
} }
custom_handler_t eventHandlers[HOOK_MAX];
static bool handle_key_event_default(Key mappedKey, byte row, byte col, uint8_t keyState) { static bool handle_key_event_default(Key mappedKey, byte row, byte col, uint8_t keyState) {
//for every newly pressed button, figure out what logical key it is and send a key down event //for every newly pressed button, figure out what logical key it is and send a key down event
// for every newly released button, figure out what logical key it is and send a key up event // for every newly released button, figure out what logical key it is and send a key up event
@ -83,8 +80,8 @@ void handle_key_event(Key mappedKey, byte row, byte col, uint8_t keyState) {
if (!(keyState & INJECTED)) { if (!(keyState & INJECTED)) {
mappedKey = Layer.lookup(row, col); mappedKey = Layer.lookup(row, col);
} }
for (byte i = 0; eventHandlers[i] != NULL && i < HOOK_MAX; i++) { for (byte i = 0; Kaleidoscope.eventHandlers[i] != NULL && i < HOOK_MAX; i++) {
custom_handler_t handler = eventHandlers[i]; Kaleidoscope_::eventHandlerHook handler = Kaleidoscope.eventHandlers[i];
mappedKey = (*handler)(mappedKey, row, col, keyState); mappedKey = (*handler)(mappedKey, row, col, keyState);
if (mappedKey.raw == Key_NoKey.raw) if (mappedKey.raw == Key_NoKey.raw)
return; return;

@ -5,7 +5,6 @@
#include KALEIDOSCOPE_HARDWARE_H #include KALEIDOSCOPE_HARDWARE_H
#include "key_defs.h" #include "key_defs.h"
#include "keyswitch_state.h" #include "keyswitch_state.h"
#include "hooks.h"
extern const Key keymaps[][ROWS][COLS]; extern const Key keymaps[][ROWS][COLS];

Loading…
Cancel
Save