From 1acc23063e4cfd0e8cdc6e7ab532c061ff153f33 Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 24 Dec 2016 10:45:24 +0100 Subject: [PATCH] Rearrange the SYNTHETIC bits, and their handling Introduces the RESERVED bit, a bit reserved for plugins. If it is set, the core handlers will not handle the event. Also rearranges the SYNTHETIC bits, to make slightly more sense. In practice, this means that LED_TOGGLE was promoted to a flag bit, under IS_INTERNAL. The handler that deals with synthetic events was updated to look at the flag bits in an order that does not cause confusion, and preparations were also made to turn it into an independent handler on its own (but that step has not been taken yet). This is just groundwork to clean things up, and make the event flow easier to follow. Signed-off-by: Gergely Nagy --- .../Keyboardio-Macros/src/MacroKeyDefs.h | 2 +- .../Keyboardio-MouseKeys/src/MouseKeyDefs.h | 2 +- src/key_defs.h | 18 ++--- src/key_events.cpp | 70 +++++++++++-------- src/key_events.h | 2 - 5 files changed, 52 insertions(+), 42 deletions(-) diff --git a/libraries/Keyboardio-Macros/src/MacroKeyDefs.h b/libraries/Keyboardio-Macros/src/MacroKeyDefs.h index 9efa9481..c96f5a4c 100644 --- a/libraries/Keyboardio-Macros/src/MacroKeyDefs.h +++ b/libraries/Keyboardio-Macros/src/MacroKeyDefs.h @@ -1,6 +1,6 @@ #pragma once -#define IS_MACRO B00000001 +#define IS_MACRO B00000100 #define M(n) (Key){ KEY_FLAGS|SYNTHETIC|IS_MACRO, n} #define Key_macroKey1 M(1) diff --git a/libraries/Keyboardio-MouseKeys/src/MouseKeyDefs.h b/libraries/Keyboardio-MouseKeys/src/MouseKeyDefs.h index b8479b27..3e0951d7 100644 --- a/libraries/Keyboardio-MouseKeys/src/MouseKeyDefs.h +++ b/libraries/Keyboardio-MouseKeys/src/MouseKeyDefs.h @@ -1,6 +1,6 @@ #pragma once -#define IS_MOUSE_KEY B00010000 +#define IS_MOUSE_KEY B00000010 // Synthetic, not internal #define KEY_MOUSE_BTN_L 0x01 // Synthetic key diff --git a/src/key_defs.h b/src/key_defs.h index 3d8ee3e4..f76a716f 100644 --- a/src/key_defs.h +++ b/src/key_defs.h @@ -19,7 +19,8 @@ typedef union { #define RALT_HELD B00000100 #define SHIFT_HELD B00001000 #define GUI_HELD B00010000 -#define SYNTHETIC B10000000 +#define SYNTHETIC B01000000 +#define RESERVED B10000000 #define LCTRL(k) ((Key) { k.flags | CTRL_HELD, k.rawKey }) #define LALT(k) ((Key) { k.flags | LALT_HELD, k.rawKey }) @@ -28,16 +29,15 @@ typedef union { #define LGUI(k) ((Key) { k.flags | GUI_HELD, k.rawKey }) // we assert that synthetic keys can never have keys held, so we reuse the _HELD bits -#define IS_SYSCTL B00000010 -#define IS_CONSUMER B00000100 -#define IS_INTERNAL B00001000 -#define SWITCH_TO_KEYMAP B00100000 -#define SWITCH_TO_KEYMAP_MOMENTARY B01000000 - +#define IS_SYSCTL B00000001 +#define IS_CONSUMER B00000010 +#define SWITCH_TO_KEYMAP B00000100 +#define SWITCH_TO_KEYMAP_MOMENTARY B00001000 +#define IS_INTERNAL B00010000 // IS_INTERNAL key table: -#define LED_TOGGLE 0x01 // Synthetic, internal +#define LED_TOGGLE B00000001 // Synthetic, internal #define KEYMAP_0 0 @@ -253,4 +253,4 @@ typedef union { -#define Key_LEDEffectNext (Key) { KEY_FLAGS | SYNTHETIC | IS_INTERNAL, LED_TOGGLE } +#define Key_LEDEffectNext (Key) { KEY_FLAGS | SYNTHETIC | IS_INTERNAL | LED_TOGGLE, 0 } diff --git a/src/key_events.cpp b/src/key_events.cpp index e2692f3d..dd0e3272 100644 --- a/src/key_events.cpp +++ b/src/key_events.cpp @@ -1,18 +1,53 @@ #include "key_events.h" -void handle_synthetic_key_event(Key mappedKey, uint8_t keyState) { +static void handle_keymap_key_event(Key keymapEntry, uint8_t keyState) { + if (keymapEntry.flags & SWITCH_TO_KEYMAP_MOMENTARY ) { + if (key_toggled_on(keyState)) { + if ( keymapEntry.rawKey == KEYMAP_NEXT) { + temporary_keymap++; + } else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) { + temporary_keymap--; + } else { + temporary_keymap = keymapEntry.rawKey; + } + } + if (key_toggled_off(keyState)) { + temporary_keymap = primary_keymap; + } + + // switch keymap and stay there + } else if (key_toggled_on(keyState)) { + temporary_keymap = primary_keymap = keymapEntry.rawKey; + Storage.save_primary_keymap(primary_keymap); + } +} + +static bool handle_synthetic_key_event(Key mappedKey, uint8_t keyState) { + if (mappedKey.flags & RESERVED) + return false; + + if (!(mappedKey.flags & SYNTHETIC)) + return false; + if (!key_toggled_on(keyState)) - return; + return true; - if (mappedKey.flags & IS_CONSUMER) { - ConsumerControl.press(mappedKey.rawKey); - } else if (mappedKey.flags & IS_INTERNAL) { - if (mappedKey.rawKey == LED_TOGGLE) { + if (mappedKey.flags & IS_INTERNAL) { + if (mappedKey.flags & LED_TOGGLE) { LEDControl.next_mode(); + } else { + return false; } + } else if (mappedKey.flags & IS_CONSUMER) { + ConsumerControl.press(mappedKey.rawKey); } else if (mappedKey.flags & IS_SYSCTL) { SystemControl.press(mappedKey.rawKey); + } else if (mappedKey.flags & SWITCH_TO_KEYMAP || + mappedKey.flags & SWITCH_TO_KEYMAP_MOMENTARY) { + // Should not happen, handled elsewhere. } + + return true; } custom_handler_t eventHandlers[HOOK_MAX]; @@ -96,26 +131,3 @@ void release_key(Key mappedKey) { } Keyboard.release(mappedKey.rawKey); } - - -void handle_keymap_key_event(Key keymapEntry, uint8_t keyState) { - if (keymapEntry.flags & SWITCH_TO_KEYMAP_MOMENTARY ) { - if (key_toggled_on(keyState)) { - if ( keymapEntry.rawKey == KEYMAP_NEXT) { - temporary_keymap++; - } else if ( keymapEntry.rawKey == KEYMAP_PREVIOUS) { - temporary_keymap--; - } else { - temporary_keymap = keymapEntry.rawKey; - } - } - if (key_toggled_off(keyState)) { - temporary_keymap = primary_keymap; - } - - // switch keymap and stay there - } else if (key_toggled_on(keyState)) { - temporary_keymap = primary_keymap = keymapEntry.rawKey; - Storage.save_primary_keymap(primary_keymap); - } -} diff --git a/src/key_events.h b/src/key_events.h index 2bc6d799..cfa1faab 100644 --- a/src/key_events.h +++ b/src/key_events.h @@ -46,10 +46,8 @@ extern const Key keymaps[][ROWS][COLS]; void handle_key_event(Key mappedKey, byte row, byte col, uint8_t keyState); // Internal use -void handle_synthetic_key_event( Key mappedKey, uint8_t keyState); void press_key(Key mappedKey); void release_key(Key mappedKey); -void handle_keymap_key_event(Key keymapEntry, uint8_t keyState); bool handle_key_event_default(Key mappedKey, byte row, byte col, uint8_t keyState); Key lookup_key(byte keymap, byte row, byte col);