From dc72193fe8f0a85e9255f233029eb9d07e8898db Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sun, 25 Oct 2015 15:02:41 +0100 Subject: [PATCH] Moved DefaultKeyboardAPI into separate file --- src/HID-APIs/DefaultKeyboardAPI.h | 77 +++++++++++++++ src/HID-APIs/DefaultKeyboardAPI.hpp | 144 ++++++++++++++++++++++++++++ src/HID-APIs/KeyboardAPI.h | 47 --------- src/HID-APIs/KeyboardAPI.hpp | 118 ----------------------- src/MultiReport/ImprovedKeyboard.h | 2 +- src/SingleReport/BootKeyboard.h | 2 +- 6 files changed, 223 insertions(+), 167 deletions(-) create mode 100644 src/HID-APIs/DefaultKeyboardAPI.h create mode 100644 src/HID-APIs/DefaultKeyboardAPI.hpp diff --git a/src/HID-APIs/DefaultKeyboardAPI.h b/src/HID-APIs/DefaultKeyboardAPI.h new file mode 100644 index 0000000..b586b09 --- /dev/null +++ b/src/HID-APIs/DefaultKeyboardAPI.h @@ -0,0 +1,77 @@ +/* +Copyright (c) 2014-2015 NicoHood +See the readme for credit to other people. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +// Include guard +#pragma once + +#include "KeyboardAPI.h" +#include "ConsumerAPI.h" + +typedef union{ + // Low level key report: up to 6 keys and shift, ctrl etc at once + uint8_t whole8[]; + uint16_t whole16[]; + uint32_t whole32[]; + struct{ + uint8_t modifiers; + uint8_t reserved; + KeyboardKeycode keycodes[6]; + }; + uint8_t keys[8]; +} HID_KeyboardReport_Data_t; + + +class DefaultKeyboardAPI : public KeyboardAPI +{ +public: + // Add special consumer key API for the reserved byte + inline size_t write(ConsumerKeycode k); + inline size_t press(ConsumerKeycode k); + inline size_t release(ConsumerKeycode k); + inline size_t add(ConsumerKeycode k); + inline size_t remove(ConsumerKeycode k); + + // Also use the base class functions + // http://en.cppreference.com/w/cpp/language/using_declaration + using KeyboardAPI::write; + using KeyboardAPI::press; + using KeyboardAPI::release; + using KeyboardAPI::add; + using KeyboardAPI::remove; + + // Implement adding/removing key functions + inline virtual size_t removeAll(void) override; + + // Needs to be implemented in a lower level + virtual int send(void) = 0; + +protected: + HID_KeyboardReport_Data_t _keyReport; + +private: + inline virtual size_t set(KeyboardKeycode k, bool s) override; +}; + +// Implementation is inline +#include "DefaultKeyboardAPI.hpp" + diff --git a/src/HID-APIs/DefaultKeyboardAPI.hpp b/src/HID-APIs/DefaultKeyboardAPI.hpp new file mode 100644 index 0000000..7ca2071 --- /dev/null +++ b/src/HID-APIs/DefaultKeyboardAPI.hpp @@ -0,0 +1,144 @@ +/* +Copyright (c) 2014-2015 NicoHood +See the readme for credit to other people. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +// Include guard +#pragma once + + +size_t DefaultKeyboardAPI::set(KeyboardKeycode k, bool s) +{ + // It's a modifier key + if(k >= KEY_LEFT_CTRL && k <= KEY_RIGHT_GUI) + { + // Convert key into bitfield (0 - 7) + k = KeyboardKeycode(uint8_t(k) - uint8_t(KEY_LEFT_CTRL)); + if(s){ + _keyReport.modifiers = (1 << k); + } + else{ + _keyReport.modifiers &= ~(1 << k); + } + return 1; + } + // Its a normal key + else{ + // Add k to the key report only if it's not already present + // and if there is an empty slot. Remove the first available key. + for (uint8_t i = 0; i < sizeof(_keyReport.keycodes); i++) + { + auto key = _keyReport.keycodes[i]; + + // Is key already in the list or did we found an empty slot? + if (s && (key == uint8_t(k) || key == KEY_RESERVED)) { + _keyReport.keycodes[i] = k; + return 1; + } + + // Test the key report to see if k is present. Clear it if it exists. + if (!s && (key == k)) { + _keyReport.keycodes[i] = KEY_RESERVED; + return 1; + } + } + } + + // No empty/pressed key was found + return 0; +} + +size_t DefaultKeyboardAPI::removeAll(void) +{ + // Release all keys + size_t ret = 0; + for (uint8_t i = 0; i < sizeof(_keyReport.keys); i++) + { + // Is a key in the list or did we found an empty slot? + if(_keyReport.keys[i]){ + ret++; + } + _keyReport.keys[i] = 0x00; + } + return ret; +} + + +size_t DefaultKeyboardAPI::write(ConsumerKeycode k) +{ + // Press and release key (if press was successfull) + auto ret = press(k); + if(ret){ + release(k); + } + return ret; +} + + +size_t DefaultKeyboardAPI::press(ConsumerKeycode k) +{ + // Press key and send report to host + auto ret = add(k); + if(ret){ + send(); + } + return ret; +} + + +size_t DefaultKeyboardAPI::release(ConsumerKeycode k) +{ + // Release key and send report to host + auto ret = remove(k); + if(ret){ + send(); + } + return ret; +} + + +size_t DefaultKeyboardAPI::add(ConsumerKeycode k) +{ + // No 2 byte keys are supported + if(k > 0xFF){ + setWriteError(); + return 0; + } + + // Place the key inside the reserved keyreport position. + // This does not work on Windows. + _keyReport.reserved = k; + return 1; +} + + +size_t DefaultKeyboardAPI::remove(ConsumerKeycode k) +{ + // No 2 byte keys are supported + if(k > 0xFF){ + return 0; + } + + // Always release the key, to make it simpler releasing a consumer key + // without releasing all other normal keyboard keys. + _keyReport.reserved = HID_CONSUMER_UNASSIGNED; + return 1; +} diff --git a/src/HID-APIs/KeyboardAPI.h b/src/HID-APIs/KeyboardAPI.h index 7689a86..81a589f 100644 --- a/src/HID-APIs/KeyboardAPI.h +++ b/src/HID-APIs/KeyboardAPI.h @@ -27,20 +27,6 @@ THE SOFTWARE. #include #include "HID-Settings.h" #include "ImprovedKeylayouts.h" -#include "ConsumerAPI.h" - -typedef union{ - // Low level key report: up to 6 keys and shift, ctrl etc at once - uint8_t whole8[]; - uint16_t whole16[]; - uint32_t whole32[]; - struct{ - uint8_t modifiers; - uint8_t reserved; - KeyboardKeycode keycodes[6]; - }; - uint8_t keys[8]; -} HID_KeyboardReport_Data_t; class KeyboardAPI : public Print @@ -74,39 +60,6 @@ private: inline size_t set(uint8_t k, bool s); }; -class DefaultKeyboardAPI : public KeyboardAPI -{ -public: - // Implement adding/removing key functions - inline virtual size_t removeAll(void) override; - - // Add special consumer key API for the reserved byte - inline size_t write(ConsumerKeycode k); - inline size_t press(ConsumerKeycode k); - inline size_t remove(ConsumerKeycode k); - inline size_t add(ConsumerKeycode k); - inline size_t release(ConsumerKeycode k); - - // Also use the base class functions - // http://en.cppreference.com/w/cpp/language/using_declaration - using KeyboardAPI::write; - using KeyboardAPI::press; - using KeyboardAPI::remove; - using KeyboardAPI::add; - using KeyboardAPI::release; - - // Needs to be implemented in a lower level - virtual int send(void) = 0; - -protected: - HID_KeyboardReport_Data_t _keyReport; - -private: - inline virtual size_t set(KeyboardKeycode k, bool s) override; -}; - -//TODO NKRO API - // Implementation is inline #include "KeyboardAPI.hpp" diff --git a/src/HID-APIs/KeyboardAPI.hpp b/src/HID-APIs/KeyboardAPI.hpp index c363c46..2cbe6fc 100644 --- a/src/HID-APIs/KeyboardAPI.hpp +++ b/src/HID-APIs/KeyboardAPI.hpp @@ -165,121 +165,3 @@ size_t KeyboardAPI::set(uint8_t k, bool s){ return ret; } - -size_t DefaultKeyboardAPI::set(KeyboardKeycode k, bool s) -{ - // It's a modifier key - if(k >= KEY_LEFT_CTRL && k <= KEY_RIGHT_GUI) - { - // Convert key into bitfield (0 - 7) - k = KeyboardKeycode(uint8_t(k) - uint8_t(KEY_LEFT_CTRL)); - if(s){ - _keyReport.modifiers = (1 << k); - } - else{ - _keyReport.modifiers &= ~(1 << k); - } - return 1; - } - // Its a normal key - else{ - // Add k to the key report only if it's not already present - // and if there is an empty slot. Remove the first available key. - for (uint8_t i = 0; i < sizeof(_keyReport.keycodes); i++) - { - auto key = _keyReport.keycodes[i]; - - // Is key already in the list or did we found an empty slot? - if (s && (key == uint8_t(k) || key == KEY_RESERVED)) { - _keyReport.keycodes[i] = k; - return 1; - } - - // Test the key report to see if k is present. Clear it if it exists. - if (!s && (key == k)) { - _keyReport.keycodes[i] = KEY_RESERVED; - return 1; - } - } - } - - // No empty/pressed key was found - return 0; -} - -size_t DefaultKeyboardAPI::removeAll(void) -{ - // Release all keys - uint8_t ret = 0; - for (uint8_t i = 0; i < sizeof(_keyReport.keys); i++) - { - // Is a key in the list or did we found an empty slot? - if(_keyReport.keys[i]){ - ret++; - } - _keyReport.keys[i] = 0x00; - } - return ret; -} - - -size_t DefaultKeyboardAPI::write(ConsumerKeycode k) -{ - // Press and release key (if press was successfull) - auto ret = press(k); - if(ret){ - release(k); - } - return ret; -} - - -size_t DefaultKeyboardAPI::press(ConsumerKeycode k) -{ - // Press key and send report to host - auto ret = add(k); - if(ret){ - send(); - } - return ret; -} - - -size_t DefaultKeyboardAPI::release(ConsumerKeycode k) -{ - // Release key and send report to host - auto ret = remove(k); - if(ret){ - send(); - } - return ret; -} - - -size_t DefaultKeyboardAPI::add(ConsumerKeycode k) -{ - // No 2 byte keys are supported - if(k > 0xFF){ - setWriteError(); - return 0; - } - - // Place the key inside the reserved keyreport position. - // This does not work on Windows. - _keyReport.reserved = k; - return 1; -} - - -size_t DefaultKeyboardAPI::remove(ConsumerKeycode k) -{ - // No 2 byte keys are supported - if(k > 0xFF){ - return 0; - } - - // Always release the key, to make it simpler releasing a consumer key - // without releasing all other normal keyboard keys. - _keyReport.reserved = HID_CONSUMER_UNASSIGNED; - return 1; -} diff --git a/src/MultiReport/ImprovedKeyboard.h b/src/MultiReport/ImprovedKeyboard.h index 3dc8f47..d9185cf 100644 --- a/src/MultiReport/ImprovedKeyboard.h +++ b/src/MultiReport/ImprovedKeyboard.h @@ -28,7 +28,7 @@ THE SOFTWARE. #include "PluggableUSB.h" #include "HID.h" #include "HID-Settings.h" -#include "../HID-APIs/KeyboardAPI.h" +#include "../HID-APIs/DefaultKeyboardAPI.h" class Keyboard_ : public DefaultKeyboardAPI diff --git a/src/SingleReport/BootKeyboard.h b/src/SingleReport/BootKeyboard.h index 3c62273..4d83e72 100644 --- a/src/SingleReport/BootKeyboard.h +++ b/src/SingleReport/BootKeyboard.h @@ -28,7 +28,7 @@ THE SOFTWARE. #include "PluggableUSB.h" #include "HID.h" #include "HID-Settings.h" -#include "../HID-APIs/KeyboardAPI.h" +#include "../HID-APIs/DefaultKeyboardAPI.h" class BootKeyboard_ : public PluggableUSBModule, public DefaultKeyboardAPI