From 6ac9398a18201734b6f3b853fd547578cda44f5a Mon Sep 17 00:00:00 2001 From: NicoHood Date: Sun, 11 Oct 2015 10:22:32 +0200 Subject: [PATCH] Added Consumer API --- src/HID-APIs/AbsoluteMouseAPI.h | 2 +- src/HID-APIs/ConsumerAPI.h | 94 +++++++++++++++++++++++++++++++++ src/HID-APIs/ConsumerAPI.hpp | 71 +++++++++++++++++++++++++ src/HID-APIs/MouseAPI.h | 2 +- src/HID-Project.h | 2 +- src/MultiReport/Consumer.cpp | 57 ++++++++++++++++++++ src/MultiReport/Consumer.h | 43 +++++++++++++++ 7 files changed, 268 insertions(+), 3 deletions(-) create mode 100644 src/HID-APIs/ConsumerAPI.h create mode 100644 src/HID-APIs/ConsumerAPI.hpp create mode 100644 src/MultiReport/Consumer.cpp create mode 100644 src/MultiReport/Consumer.h diff --git a/src/HID-APIs/AbsoluteMouseAPI.h b/src/HID-APIs/AbsoluteMouseAPI.h index 44b6fae..37a6ee8 100644 --- a/src/HID-APIs/AbsoluteMouseAPI.h +++ b/src/HID-APIs/AbsoluteMouseAPI.h @@ -53,7 +53,7 @@ typedef union{ class AbsoluteMouseAPI { -private: +protected: int16_t xAxis; int16_t yAxis; uint8_t _buttons; diff --git a/src/HID-APIs/ConsumerAPI.h b/src/HID-APIs/ConsumerAPI.h new file mode 100644 index 0000000..a71b2c5 --- /dev/null +++ b/src/HID-APIs/ConsumerAPI.h @@ -0,0 +1,94 @@ +/* +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 +#include "HID-Settings.h" + +// Media key definitions, see official USB docs for more +#define MEDIA_FAST_FORWARD 0xB3 +#define MEDIA_REWIND 0xB4 +#define MEDIA_NEXT 0xB5 +#define MEDIA_PREVIOUS 0xB6 +#define MEDIA_PREV MEDIA_PREVIOUS +#define MEDIA_STOP 0xB7 +#define MEDIA_PLAY_PAUSE 0xCD + +#define MEDIA_VOLUME_MUTE 0xE2 +#define MEDIA_VOLUME_UP 0xE9 +#define MEDIA_VOLUME_DOWN 0xEA +#define MEDIA_VOL_MUTE MEDIA_VOLUME_MUTE +#define MEDIA_VOL_UP MEDIA_VOLUME_UP +#define MEDIA_VOL_DOWN MEDIA_VOLUME_DOWN + +#define CONSUMER_SCREENSAVER 0x19e + +#define CONSUMER_PROGRAMMABLE_BUTTON_CONFIGURATION 0x182 +#define CONSUMER_CONTROL_CONFIGURATION 0x183 +#define CONSUMER_EMAIL_READER 0x18A +#define CONSUMER_CALCULATOR 0x192 +#define CONSUMER_EXPLORER 0x194 + +#define CONSUMER_BROWSER_HOME 0x223 +#define CONSUMER_BROWSER_BACK 0x224 +#define CONSUMER_BROWSER_FORWARD 0x225 +#define CONSUMER_BROWSER_REFRESH 0x227 +#define CONSUMER_BROWSER_BOOKMARKS 0x22A + + +typedef union { + // Every usable Consumer key possible, up to 4 keys presses possible + uint8_t whole8[]; + uint16_t whole16[]; + uint32_t whole32[]; + struct { + uint16_t key1; + uint16_t key2; + uint16_t key3; + uint16_t key4; + }; +} HID_ConsumerControlReport_Data_t; + +class ConsumerAPI +{ +public: + inline ConsumerAPI(void); + inline void begin(void); + inline void end(void); + inline void write(uint16_t m); + inline void press(uint16_t m); + inline void release(uint16_t m); + inline void releaseAll(void); + + // Sending is public in the base class for advanced users. + virtual void SendReport(void* data, int length) = 0; + +protected: + HID_ConsumerControlReport_Data_t _report; +}; + +// Implementation is inline +#include "ConsumerAPI.hpp" + diff --git a/src/HID-APIs/ConsumerAPI.hpp b/src/HID-APIs/ConsumerAPI.hpp new file mode 100644 index 0000000..55cd864 --- /dev/null +++ b/src/HID-APIs/ConsumerAPI.hpp @@ -0,0 +1,71 @@ +/* +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 + +ConsumerAPI::ConsumerAPI(void) +{ + // Empty +} + +void ConsumerAPI::begin(void) { + // release all buttons + end(); +} + +void ConsumerAPI::end(void) { + memset(&_report, 0, sizeof(_report)); + SendReport(&_report, sizeof(_report)); +} + +void ConsumerAPI::write(uint16_t m) { + press(m); + release(m); +} + +void ConsumerAPI::press(uint16_t m) { + // search for a free spot + for (uint8_t i = 0; i < sizeof(HID_ConsumerControlReport_Data_t) / 2; i++) { + if (_report.whole16[i] == 0x00) { + _report.whole16[i] = m; + break; + } + } + SendReport(&_report, sizeof(_report)); +} + +void ConsumerAPI::release(uint16_t m) { + // search and release the keypress + for (uint8_t i = 0; i < sizeof(HID_ConsumerControlReport_Data_t) / 2; i++) { + if (_report.whole16[i] == m) { + _report.whole16[i] = 0x00; + // no break to delete multiple keys + } + } + SendReport(&_report, sizeof(_report)); +} + +void ConsumerAPI::releaseAll(void) { + end(); +} diff --git a/src/HID-APIs/MouseAPI.h b/src/HID-APIs/MouseAPI.h index e50c67f..1357d14 100644 --- a/src/HID-APIs/MouseAPI.h +++ b/src/HID-APIs/MouseAPI.h @@ -78,7 +78,7 @@ public: // Sending is public in the base class for advanced users. virtual void SendReport(void* data, int length) = 0; -private: +protected: uint8_t _buttons; void buttons(uint8_t b); }; diff --git a/src/HID-Project.h b/src/HID-Project.h index 84e897b..84cdc5b 100644 --- a/src/HID-Project.h +++ b/src/HID-Project.h @@ -41,7 +41,7 @@ THE SOFTWARE. #include "MultiReport/AbsoluteMouse.h" #include "SingleReport/BootMouse.h" #include "MultiReport/ImprovedMouse.h" -//#include "Consumer.h" +#include "MultiReport/Consumer.h" //#include "Gamepad.h" #include "MultiReport/System.h" //#include "RawHID.h" diff --git a/src/MultiReport/Consumer.cpp b/src/MultiReport/Consumer.cpp new file mode 100644 index 0000000..7e8a116 --- /dev/null +++ b/src/MultiReport/Consumer.cpp @@ -0,0 +1,57 @@ +/* +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 "Consumer.h" + + +static const uint8_t _hidMultiReportDescriptorConsumer[] PROGMEM = { + /* Consumer Control (Sound/Media keys) */ + 0x05, 0x0C, /* usage page (consumer device) */ + 0x09, 0x01, /* usage -- consumer control */ + 0xA1, 0x01, /* collection (application) */ + 0x85, HID_REPORTID_CONSUMERCONTROL, /* report id */ + /* 4 Media Keys */ + 0x15, 0x00, /* logical minimum */ + 0x26, 0xFF, 0x03, /* logical maximum (3ff) */ + 0x19, 0x00, /* usage minimum (0) */ + 0x2A, 0xFF, 0x03, /* usage maximum (3ff) */ + 0x95, 0x04, /* report count (4) */ + 0x75, 0x10, /* report size (16) */ + 0x81, 0x00, /* input */ + 0xC0 /* end collection */ +}; + +Consumer_::Consumer_(void) +{ + static HIDDescriptorListNode node(_hidMultiReportDescriptorConsumer, sizeof(_hidMultiReportDescriptorConsumer)); + HID().AppendDescriptor(&node); +} + + +void Consumer_::SendReport(void* data, int length) +{ + HID().SendReport(HID_REPORTID_CONSUMERCONTROL, data, length); +} + +Consumer_ Consumer; + diff --git a/src/MultiReport/Consumer.h b/src/MultiReport/Consumer.h new file mode 100644 index 0000000..ede2458 --- /dev/null +++ b/src/MultiReport/Consumer.h @@ -0,0 +1,43 @@ +/* +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 +#include "PluggableUSB.h" +#include "HID.h" +#include "HID-Settings.h" +#include "../HID-APIs/ConsumerAPI.h" + + +class Consumer_ : public ConsumerAPI +{ +public: + Consumer_(void); + +protected: + virtual inline void SendReport(void* data, int length) override; +}; +extern Consumer_ Consumer; +