Merge pull request #25 from algernon/f/ALL_YOUR_EVENTS_ARE_BELONG_TO_US

Rework the custom event handler architecture
pull/26/head
Jesse Vincent 8 years ago committed by GitHub
commit 5ea87b738f

@ -48,19 +48,10 @@ void handle_synthetic_key_event(Key mappedKey, uint8_t currentState, uint8_t pre
} }
__attribute__((weak)) __attribute__((weak))
bool handle_user_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) { custom_handler_t eventHandlers[] = {
return false; handle_key_event_default,
} (custom_handler_t) NULL
};
static custom_handler_t customHandler = handle_user_key_event;
void set_custom_handler(custom_handler_t f) {
customHandler = f;
}
custom_handler_t get_custom_handler() {
return customHandler;
}
Key lookup_key(byte keymap, byte row, byte col) { Key lookup_key(byte keymap, byte row, byte col) {
Key mappedKey; Key mappedKey;
@ -71,13 +62,17 @@ Key lookup_key(byte keymap, byte row, byte col) {
} }
void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) { void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState) {
for (byte i = 0; eventHandlers[i] != NULL; i++) {
custom_handler_t handler = eventHandlers[i];
if ((*handler)(row, col, currentState, previousState))
return;
}
}
bool handle_key_event_default(byte row, byte col, uint8_t currentState, uint8_t previousState) {
//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
if ((*customHandler)(row, col, currentState, previousState)) {
return;
}
Key mappedKey = lookup_key(temporary_keymap, row, col); Key mappedKey = lookup_key(temporary_keymap, row, col);
Key baseKey = lookup_key(primary_keymap, row, col); Key baseKey = lookup_key(primary_keymap, row, col);
@ -88,6 +83,7 @@ void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previous
} else if (key_is_pressed(currentState, previousState)) { } else if (key_is_pressed(currentState, previousState)) {
press_key(mappedKey); press_key(mappedKey);
} }
return true;
} }
void press_key(Key mappedKey) { void press_key(Key mappedKey) {

@ -14,9 +14,7 @@ extern uint8_t primary_keymap;
extern uint8_t temporary_keymap; extern uint8_t temporary_keymap;
typedef bool (*custom_handler_t)(byte row, byte col, uint8_t currentState, uint8_t previousState); typedef bool (*custom_handler_t)(byte row, byte col, uint8_t currentState, uint8_t previousState);
extern custom_handler_t eventHandlers[];
void set_custom_handler(custom_handler_t f);
custom_handler_t get_custom_handler();
// sending events to the computer // sending events to the computer
void handle_synthetic_key_event( Key mappedKey, uint8_t currentState, uint8_t previousState); void handle_synthetic_key_event( Key mappedKey, uint8_t currentState, uint8_t previousState);
@ -24,6 +22,6 @@ void handle_key_event(byte row, byte col, uint8_t currentState, uint8_t previous
void press_key(Key mappedKey); void press_key(Key mappedKey);
void handle_keymap_key_event(Key keymapEntry, uint8_t currentState, uint8_t previousState); void handle_keymap_key_event(Key keymapEntry, uint8_t currentState, uint8_t previousState);
void handle_mouse_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState); void handle_mouse_key_event(Key mappedKey, uint8_t currentState, uint8_t previousState);
bool handle_user_key_event(byte row, byte col, uint8_t currentState, uint8_t previousState); bool handle_key_event_default(byte row, byte col, uint8_t currentState, uint8_t previousState);
Key lookup_key(byte keymap, byte row, byte col); Key lookup_key(byte keymap, byte row, byte col);

Loading…
Cancel
Save