From 8b187366c750de802c5bb9f41ad948b2b3144c21 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Mon, 24 Jul 2017 21:19:25 -0700 Subject: [PATCH] Create an abstraction on top of the KeyboardioHID interface so that user code doesn't need to know anything about it. (This also paves the way for @wez' EventDispatcher) --- src/Kaleidoscope.cpp | 4 +- src/key_events.cpp | 151 +++++++++++++++++++++++++++++++++---------- src/key_events.h | 26 +++++++- 3 files changed, 144 insertions(+), 37 deletions(-) diff --git a/src/Kaleidoscope.cpp b/src/Kaleidoscope.cpp index e1cf0824..4b538700 100644 --- a/src/Kaleidoscope.cpp +++ b/src/Kaleidoscope.cpp @@ -25,8 +25,8 @@ Kaleidoscope_::loop(void) { (*hook)(false); } - Keyboard.sendReport(); - Keyboard.releaseAll(); + sendKeyboardReport(); + releaseAllKeys(); for (byte i = 0; loopHooks[i] != NULL && i < HOOK_MAX; i++) { loopHook hook = loopHooks[i]; diff --git a/src/key_events.cpp b/src/key_events.cpp index a89333cd..8e740337 100644 --- a/src/key_events.cpp +++ b/src/key_events.cpp @@ -1,5 +1,6 @@ #include "Kaleidoscope.h" + static bool handleSyntheticKeyswitchEvent(Key mappedKey, uint8_t keyState) { if (mappedKey.flags & RESERVED) return false; @@ -10,15 +11,17 @@ static bool handleSyntheticKeyswitchEvent(Key mappedKey, uint8_t keyState) { if (mappedKey.flags & IS_INTERNAL) { return false; } else if (mappedKey.flags & IS_CONSUMER) { - if (keyIsPressed(keyState)) - ConsumerControl.press(mappedKey.keyCode); - else if (keyWasPressed(keyState)) - ConsumerControl.release(mappedKey.keyCode); + if (keyIsPressed(keyState)) { + pressConsumer(mappedKey); + } else if (keyWasPressed(keyState)) { + releaseConsumer(mappedKey); + } } else if (mappedKey.flags & IS_SYSCTL) { - if (keyIsPressed(keyState)) - SystemControl.press(mappedKey.keyCode); - else if (keyWasPressed(keyState)) - SystemControl.release(); + if (keyIsPressed(keyState)) { + pressSystem(mappedKey); + } else if (keyWasPressed(keyState)) { + releaseSystem(mappedKey); + } } else if (mappedKey.flags & SWITCH_TO_KEYMAP) { // Should not happen, handled elsewhere. } @@ -40,57 +43,137 @@ static bool handleKeyswitchEventDefault(Key mappedKey, byte row, byte col, uint8 return true; } +void handleKeyswitchEvent(Key mappedKey, byte row, byte col, uint8_t keyState) { + if (!(keyState & INJECTED)) { + mappedKey = Layer.lookup(row, col); + } + for (byte i = 0; Kaleidoscope.eventHandlers[i] != NULL && i < HOOK_MAX; i++) { + Kaleidoscope_::eventHandlerHook handler = Kaleidoscope.eventHandlers[i]; + mappedKey = (*handler)(mappedKey, row, col, keyState); + if (mappedKey.raw == Key_NoKey.raw) + return; + } + mappedKey = Layer.eventHandler(mappedKey, row, col, keyState); + if (mappedKey.raw == Key_NoKey.raw) + return; + handleKeyswitchEventDefault(mappedKey, row, col, keyState); +} + +void pressKeyRaw(Key mappedKey) { + Keyboard.press(mappedKey.keyCode); + +} + void pressKey(Key mappedKey) { if (mappedKey.flags & SHIFT_HELD) { - Keyboard.press(Key_LeftShift.keyCode); + pressKeyRaw(Key_LeftShift); } if (mappedKey.flags & CTRL_HELD) { - Keyboard.press(Key_LeftControl.keyCode); + pressKeyRaw(Key_LeftControl); } if (mappedKey.flags & LALT_HELD) { - Keyboard.press(Key_LeftAlt.keyCode); + pressKeyRaw(Key_LeftAlt); } if (mappedKey.flags & RALT_HELD) { - Keyboard.press(Key_RightAlt.keyCode); + pressKeyRaw(Key_RightAlt); } if (mappedKey.flags & GUI_HELD) { - Keyboard.press(Key_LeftGui.keyCode); + pressKeyRaw(Key_LeftGui); } - Keyboard.press(mappedKey.keyCode); + + pressKeyRaw(mappedKey); +} + +void releaseKeyRaw(Key mappedKey) { + Keyboard.release(mappedKey.keyCode); + } +void releaseAllKeys() { + Keyboard.releaseAll(); +} void releaseKey(Key mappedKey) { if (mappedKey.flags & SHIFT_HELD) { - Keyboard.release(Key_LeftShift.keyCode); + releaseKeyRaw(Key_LeftShift); } if (mappedKey.flags & CTRL_HELD) { - Keyboard.release(Key_LeftControl.keyCode); + releaseKeyRaw(Key_LeftControl); } if (mappedKey.flags & LALT_HELD) { - Keyboard.release(Key_LeftAlt.keyCode); + releaseKeyRaw(Key_LeftAlt); } if (mappedKey.flags & RALT_HELD) { - Keyboard.release(Key_RightAlt.keyCode); + releaseKeyRaw(Key_RightAlt); } if (mappedKey.flags & GUI_HELD) { - Keyboard.release(Key_LeftGui.keyCode); + releaseKeyRaw(Key_LeftGui); } - Keyboard.release(mappedKey.keyCode); + releaseKeyRaw(mappedKey); } -void handleKeyswitchEvent(Key mappedKey, byte row, byte col, uint8_t keyState) { - if (!(keyState & INJECTED)) { - mappedKey = Layer.lookup(row, col); - } - for (byte i = 0; Kaleidoscope.eventHandlers[i] != NULL && i < HOOK_MAX; i++) { - Kaleidoscope_::eventHandlerHook handler = Kaleidoscope.eventHandlers[i]; - mappedKey = (*handler)(mappedKey, row, col, keyState); - if (mappedKey.raw == Key_NoKey.raw) - return; - } - mappedKey = Layer.eventHandler(mappedKey, row, col, keyState); - if (mappedKey.raw == Key_NoKey.raw) - return; - handleKeyswitchEventDefault(mappedKey, row, col, keyState); + + + +void sendKeyboardReport() { + Keyboard.sendReport(); +} + +void pressConsumer(Key mappedKey) { + ConsumerControl.press(mappedKey.keyCode); +} + +void releaseConsumer(Key mappedKey) { + ConsumerControl.release(mappedKey.keyCode); +} + +void pressSystem(Key mappedKey) { + SystemControl.press(mappedKey.keyCode); +} + +void releaseSystem(Key mappedKey) { + SystemControl.release(); +} + + +/** Mouse events + * See above for commentary on connectionMask. */ + + +void moveMouse(signed char x, signed char y, signed char wheel) { + Mouse.move(x, y, wheel); +} + +void clickMouseButtons(uint8_t buttons) { + Mouse.click(buttons); +} + +void pressMouseButtons(uint8_t buttons) { + Mouse.press(buttons); +} + +void releaseMouseButtons(uint8_t buttons) { + Mouse.release(buttons); +} + +/** Absolute mouse (grapahics tablet) events + * See above for commentary on connectionMask. */ + +void moveAbsoluteMouse(signed char x, signed char y, signed char wheel) { + AbsoluteMouse.move(x, y, wheel); +} +void moveAbsoluteMouseTo(uint16_t x, uint16_t y, signed char wheel) { + AbsoluteMouse.moveTo(x, y, wheel); +} + +void clickAbsoluteMouseButtons(uint8_t buttons) { + AbsoluteMouse.click(buttons); +} + +void pressAbsoluteMouseButtons(uint8_t buttons) { + AbsoluteMouse.press(buttons); +} + +void releaseAbsoluteMouseButtons(uint8_t buttons) { + AbsoluteMouse.release(buttons); } diff --git a/src/key_events.h b/src/key_events.h index 96929f81..7b153495 100644 --- a/src/key_events.h +++ b/src/key_events.h @@ -43,6 +43,30 @@ extern const Key keymaps[][ROWS][COLS]; */ void handleKeyswitchEvent(Key mappedKey, byte row, byte col, uint8_t keyState); -// Internal use +/** Flushes any pending regular key switch events and sends them out */ +void sendKeyboardReport(); + +// A facade on top of our HID implementation void pressKey(Key mappedKey); void releaseKey(Key mappedKey); +void releaseAllKeys(); +void pressKeyRaw(Key mappedKey); +void releaseKeyRaw(Key mappedKey); + +void pressConsumer(Key mappedKey); +void releaseConsumer(Key mappedKey); + +void pressSystem(Key mappedKey); +void releaseSystem(Key mappedKey); + +void moveMouse(signed char x, signed char y, signed char wheel); +void clickMouseButtons(uint8_t buttons); +void pressMouseButtons(uint8_t buttons); +void releaseMouseButtons(uint8_t buttons); +void moveAbsoluteMouse(signed char x, signed char y, signed char wheel); +void moveAbsoluteMouseTo(uint16_t x, uint16_t y, signed char wheel); +void clickAbsoluteMouseButtons(uint8_t buttons); +void pressAbsoluteMouseButtons(uint8_t buttons); +void releaseAbsoluteMouseButtons(uint8_t buttons); + +