Event hooks reworked, again

Instead of returning a bool, to signal whether further processing should be
done, return a Key. Reason being, if we want to replace a key with another, for
subsequent handlers, it is a lot easier if we can modify what gets passed along,
than it is to inject a key, and try to avoid loops and infinite recursion.
Nevertheless, injecting keys is still possible.

This is not immediately useful for the core firmware, but makes it trivially
easy to upgrade keys from their normal behaviour to something special: for
example, a one-shot handler can auto-promote modifiers to one-shot, simply by
scheduling a promoter handler before the real one.

Signed-off-by: Gergely Nagy <algernon@madhouse-project.org>
pull/71/head
Gergely Nagy 8 years ago
parent e78b63deae
commit aed2fc8d98

@ -44,17 +44,17 @@ void Macros_::play(const macro_t *macro_p) {
}
}
static bool handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState) {
static Key handleMacroEvent(Key mappedKey, byte row, byte col, uint8_t keyState) {
if (mappedKey.flags != (SYNTHETIC | IS_MACRO))
return false;
return mappedKey;
if (!key_toggled_on(keyState))
return true;
return Key_NoKey;
const macro_t *m = macroAction(mappedKey.rawKey, keyState);
Macros.play(m);
return true;
return Key_NoKey;
}
Macros_::Macros_ (void) {

@ -22,9 +22,9 @@ static void handle_mouse_key_event(Key mappedKey, uint8_t keyState) {
}
}
static bool handleMouseKeys(Key mappedKey, byte row, byte col, uint8_t keyState) {
static Key handleMouseKeys(Key mappedKey, byte row, byte col, uint8_t keyState) {
if (mappedKey.flags != (SYNTHETIC | IS_MOUSE_KEY))
return false;
return mappedKey;
if (mappedKey.rawKey & KEY_MOUSE_BUTTON) {
uint8_t button = mappedKey.rawKey & ~KEY_MOUSE_BUTTON;
@ -46,7 +46,7 @@ static bool handleMouseKeys(Key mappedKey, byte row, byte col, uint8_t keyState)
}
}
return true;
return Key_NoKey;
}
MouseKeys_::MouseKeys_(void) {

@ -109,9 +109,9 @@ void TestMode_::loop() {
TestMatrix();
}
bool handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t keyState) {
Key handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t keyState) {
Serial.write(row);
return false;
return mappedKey;
}
TestMode_ TestMode;

@ -16,4 +16,4 @@ class TestMode_ {
extern TestMode_ TestMode;
bool handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t keyState);
Key handle_key_event_test(Key mappedKey, byte row, byte col, uint8_t keyState);

@ -5,7 +5,7 @@
#define HOOK_MAX 64
typedef bool (*custom_handler_t)(Key mappedKey, byte row, byte col, uint8_t keyState);
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);

@ -89,7 +89,8 @@ void handle_key_event(Key mappedKey, byte row, byte col, uint8_t keyState) {
}
for (byte i = 0; eventHandlers[i] != NULL && i < HOOK_MAX; i++) {
custom_handler_t handler = eventHandlers[i];
if ((*handler)(mappedKey, row, col, keyState))
mappedKey = (*handler)(mappedKey, row, col, keyState);
if (mappedKey.raw == Key_NoKey.raw)
return;
}
handle_key_event_default(mappedKey, row, col, keyState);

@ -33,13 +33,13 @@ static void handle_keymap_key_event(Key keymapEntry, uint8_t keyState) {
}
}
static bool
static Key
layerEventHandler(Key mappedKey, byte row, byte col, uint8_t keyState) {
if (mappedKey.flags != (SYNTHETIC | SWITCH_TO_KEYMAP))
return false;
return mappedKey;
handle_keymap_key_event(mappedKey, keyState);
return true;
return Key_NoKey;
}
Layer_::Layer_ (void) {

Loading…
Cancel
Save