From 14369a98533f550b12b4dc1be331da1d0d7af7c9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 24 Aug 2019 20:36:48 -0400 Subject: [PATCH 1/3] HIDParser: Use std::array where applicable Same thing, but strongly enforces the type of the array. This also allows removing the include, since we can just query the size of the array. --- lib/inputdev/HIDParser.cpp | 516 +++++++++++++++++++------------------ 1 file changed, 266 insertions(+), 250 deletions(-) diff --git a/lib/inputdev/HIDParser.cpp b/lib/inputdev/HIDParser.cpp index 2442d34..2ec28ed 100644 --- a/lib/inputdev/HIDParser.cpp +++ b/lib/inputdev/HIDParser.cpp @@ -1,8 +1,8 @@ #include "boo/inputdev/HIDParser.hpp" #include +#include #include -#include #undef min #undef max @@ -13,251 +13,257 @@ namespace boo { * http://www.usb.org/developers/hidpage/HID1_11.pdf */ -static const char* UsagePageNames[] = {"Undefined", "Generic Desktop", "Simulation", "VR", "Sport", - "Game Controls", "Generic Device", "Keyboard", "LEDs", "Button", - "Ordinal", "Telephony", "Consumer", "Digitizer"}; +constexpr std::array UsagePageNames{ + "Undefined", "Generic Desktop", "Simulation", "VR", "Sport", + "Game Controls", "Generic Device", "Keyboard", "LEDs", "Button", + "Ordinal", "Telephony", "Consumer", "Digitizer", +}; -static const char* GenericDesktopUsages[] = {"Undefined", - "Pointer", - "Mouse", - "Reserved", - "Joystick", - "Game Pad", - "Keyboard", - "Keypad", - "Multi-axis Controller", - "Tablet PC System Controls", - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - "X", - "Y", - "Z", - "Rx", - "Ry", - "Rz", - "Slider", - "Dial", - "Wheel", - "Hat Switch", - "Counted Buffer", - "Byte Count", - "Motion Wakeup", - "Start", - "Select", - "Reserved", - "Vx", - "Vy", - "Vz", - "Vbrx", - "Vbry", - "Vbrz", - "Vno", - "Feature Notification", - "Resolution Multiplier", - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - "System Control", - "System Power Down", - "System Sleep", - "System Wake Up", - "System Context Menu", - "System Main Menu", - "System App Menu", - "System Menu Help", - "System Menu Exit", - "System Menu Select", - "System Menu Right", - "System Menu Left", - "System Menu Up", - "System Menu Down", - "System Cold Restart", - "System Warm Restart", - "D-pad Up", - "D-pad Down", - "D-pad Right", - "D-pad Left", - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - "System Dock", - "System Undock", - "System Setup", - "System Break", - "System Debugger Break", - "Application Break", - "Application Debugger Break", - "System Speaker Mute", - "System Hibernate", - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - "System Display Invert", - "System Display Internal", - "System Display External", - "System Display Both", - "System Display Dual", - "System Display Toggle Int/Ext"}; +constexpr std::array GenericDesktopUsages{ + "Undefined", + "Pointer", + "Mouse", + "Reserved", + "Joystick", + "Game Pad", + "Keyboard", + "Keypad", + "Multi-axis Controller", + "Tablet PC System Controls", + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + "X", + "Y", + "Z", + "Rx", + "Ry", + "Rz", + "Slider", + "Dial", + "Wheel", + "Hat Switch", + "Counted Buffer", + "Byte Count", + "Motion Wakeup", + "Start", + "Select", + "Reserved", + "Vx", + "Vy", + "Vz", + "Vbrx", + "Vbry", + "Vbrz", + "Vno", + "Feature Notification", + "Resolution Multiplier", + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + "System Control", + "System Power Down", + "System Sleep", + "System Wake Up", + "System Context Menu", + "System Main Menu", + "System App Menu", + "System Menu Help", + "System Menu Exit", + "System Menu Select", + "System Menu Right", + "System Menu Left", + "System Menu Up", + "System Menu Down", + "System Cold Restart", + "System Warm Restart", + "D-pad Up", + "D-pad Down", + "D-pad Right", + "D-pad Left", + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + "System Dock", + "System Undock", + "System Setup", + "System Break", + "System Debugger Break", + "Application Break", + "Application Debugger Break", + "System Speaker Mute", + "System Hibernate", + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + "System Display Invert", + "System Display Internal", + "System Display External", + "System Display Both", + "System Display Dual", + "System Display Toggle Int/Ext", +}; -static const char* GameUsages[] = {"Undefined", - "3D Game Controller", - "Pinball Device", - "Gun Device", - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - nullptr, - "Point of View", - "Turn Right/Left", - "Pitch Forward/Backward", - "Roll Right/Left", - "Move Right/Left", - "Move Forward/Backward", - "Move Up/Down", - "Lean Right/Left", - "Lean Forward/Backward", - "Height of POV", - "Flipper", - "Secondary Flipper", - "Bump", - "New Game", - "Shoot Ball", - "Player", - "Gun Bolt", - "Gun Clip", - "Gun Selector", - "Gun Single Shot", - "Gun Burst", - "Gun Automatic", - "Gun Safety", - "Gemepad Fire/Jump", - nullptr, - "Gamepad Trigger"}; +constexpr std::array GameUsages{ + "Undefined", + "3D Game Controller", + "Pinball Device", + "Gun Device", + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + nullptr, + "Point of View", + "Turn Right/Left", + "Pitch Forward/Backward", + "Roll Right/Left", + "Move Right/Left", + "Move Forward/Backward", + "Move Up/Down", + "Lean Right/Left", + "Lean Forward/Backward", + "Height of POV", + "Flipper", + "Secondary Flipper", + "Bump", + "New Game", + "Shoot Ball", + "Player", + "Gun Bolt", + "Gun Clip", + "Gun Selector", + "Gun Single Shot", + "Gun Burst", + "Gun Automatic", + "Gun Safety", + "Gemepad Fire/Jump", + nullptr, + "Gamepad Trigger", +}; enum class HIDCollectionType : uint8_t { Physical, @@ -382,21 +388,31 @@ HIDMainItem::HIDMainItem(uint32_t flags, HIDUsagePage usagePage, HIDUsage usage, , m_reportSize(reportSize) {} const char* HIDMainItem::GetUsagePageName() const { - if (int(m_usagePage) >= std::extent::value) + const auto index = size_t(m_usagePage); + + if (index >= UsagePageNames.size()) { return nullptr; - return UsagePageNames[int(m_usagePage)]; + } + + return UsagePageNames[index]; } const char* HIDMainItem::GetUsageName() const { + const auto index = size_t(m_usage); + switch (m_usagePage) { case HIDUsagePage::GenericDesktop: - if (int(m_usage) >= std::extent::value) + if (index >= GenericDesktopUsages.size()) { return nullptr; - return GenericDesktopUsages[int(m_usage)]; + } + return GenericDesktopUsages[index]; + case HIDUsagePage::Game: - if (int(m_usage) >= std::extent::value) + if (index >= GameUsages.size()) { return nullptr; - return GameUsages[int(m_usage)]; + } + return GameUsages[index]; + default: return nullptr; } From c8bebc9948074dc285d6a96d5bb201850eae5810 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sat, 24 Aug 2019 20:55:50 -0400 Subject: [PATCH 2/3] HIDParser: Eliminate variable shadowing We can rename some locals to avoid shadowing other variables. --- lib/inputdev/HIDParser.cpp | 46 +++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/lib/inputdev/HIDParser.cpp b/lib/inputdev/HIDParser.cpp index 2ec28ed..a007ad5 100644 --- a/lib/inputdev/HIDParser.cpp +++ b/lib/inputdev/HIDParser.cpp @@ -461,17 +461,17 @@ HIDParser::ParserStatus HIDParser::Parse(const PHIDP_PREPARSED_DATA descriptorDa USHORT length = caps.NumberInputButtonCaps; std::vector bCaps(caps.NumberInputButtonCaps, HIDP_BUTTON_CAPS()); HidP_GetButtonCaps(HidP_Input, bCaps.data(), &length, descriptorData); - for (const HIDP_BUTTON_CAPS& caps : bCaps) { - if (caps.IsRange) { - int usage = caps.Range.UsageMin; - for (int i = caps.Range.DataIndexMin; i <= caps.Range.DataIndexMax; ++i, ++usage) { - inputItems.insert(std::make_pair( - i, HIDMainItem(caps.BitField, HIDUsagePage(caps.UsagePage), HIDUsage(usage), std::make_pair(0, 1), 1))); + for (const HIDP_BUTTON_CAPS& buttonCaps : bCaps) { + if (buttonCaps.IsRange) { + int usage = buttonCaps.Range.UsageMin; + for (int i = buttonCaps.Range.DataIndexMin; i <= buttonCaps.Range.DataIndexMax; ++i, ++usage) { + inputItems.insert(std::make_pair(i, HIDMainItem(buttonCaps.BitField, HIDUsagePage(buttonCaps.UsagePage), + HIDUsage(usage), std::make_pair(0, 1), 1))); } } else { - inputItems.insert(std::make_pair(caps.NotRange.DataIndex, - HIDMainItem(caps.BitField, HIDUsagePage(caps.UsagePage), - HIDUsage(caps.NotRange.Usage), std::make_pair(0, 1), 1))); + inputItems.insert(std::make_pair(buttonCaps.NotRange.DataIndex, + HIDMainItem(buttonCaps.BitField, HIDUsagePage(buttonCaps.UsagePage), + HIDUsage(buttonCaps.NotRange.Usage), std::make_pair(0, 1), 1))); } } } @@ -481,19 +481,19 @@ HIDParser::ParserStatus HIDParser::Parse(const PHIDP_PREPARSED_DATA descriptorDa USHORT length = caps.NumberInputValueCaps; std::vector vCaps(caps.NumberInputValueCaps, HIDP_VALUE_CAPS()); HidP_GetValueCaps(HidP_Input, vCaps.data(), &length, descriptorData); - for (const HIDP_VALUE_CAPS& caps : vCaps) { - if (caps.IsRange) { - int usage = caps.Range.UsageMin; - for (int i = caps.Range.DataIndexMin; i <= caps.Range.DataIndexMax; ++i, ++usage) { - inputItems.insert( - std::make_pair(i, HIDMainItem(caps.BitField, HIDUsagePage(caps.UsagePage), HIDUsage(usage), - std::make_pair(caps.LogicalMin, caps.LogicalMax), caps.BitSize))); + for (const HIDP_VALUE_CAPS& valueCaps : vCaps) { + if (valueCaps.IsRange) { + int usage = valueCaps.Range.UsageMin; + for (int i = valueCaps.Range.DataIndexMin; i <= valueCaps.Range.DataIndexMax; ++i, ++usage) { + inputItems.insert(std::make_pair( + i, HIDMainItem(valueCaps.BitField, HIDUsagePage(valueCaps.UsagePage), HIDUsage(usage), + std::make_pair(valueCaps.LogicalMin, valueCaps.LogicalMax), valueCaps.BitSize))); } } else { - inputItems.insert( - std::make_pair(caps.NotRange.DataIndex, - HIDMainItem(caps.BitField, HIDUsagePage(caps.UsagePage), HIDUsage(caps.NotRange.Usage), - HIDRange(caps.LogicalMin, caps.LogicalMax), caps.BitSize))); + inputItems.insert(std::make_pair( + valueCaps.NotRange.DataIndex, + HIDMainItem(valueCaps.BitField, HIDUsagePage(valueCaps.UsagePage), HIDUsage(valueCaps.NotRange.Usage), + HIDRange(valueCaps.LogicalMin, valueCaps.LogicalMax), valueCaps.BitSize))); } } } @@ -822,9 +822,9 @@ void HIDParser::ScanValues(const std::function Date: Sat, 24 Aug 2019 20:58:54 -0400 Subject: [PATCH 3/3] HIDParser: Use emplace() instead of insert() Same thing, but shorter. --- lib/inputdev/HIDParser.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/inputdev/HIDParser.cpp b/lib/inputdev/HIDParser.cpp index a007ad5..bce6544 100644 --- a/lib/inputdev/HIDParser.cpp +++ b/lib/inputdev/HIDParser.cpp @@ -465,13 +465,13 @@ HIDParser::ParserStatus HIDParser::Parse(const PHIDP_PREPARSED_DATA descriptorDa if (buttonCaps.IsRange) { int usage = buttonCaps.Range.UsageMin; for (int i = buttonCaps.Range.DataIndexMin; i <= buttonCaps.Range.DataIndexMax; ++i, ++usage) { - inputItems.insert(std::make_pair(i, HIDMainItem(buttonCaps.BitField, HIDUsagePage(buttonCaps.UsagePage), - HIDUsage(usage), std::make_pair(0, 1), 1))); + inputItems.emplace(i, HIDMainItem(buttonCaps.BitField, HIDUsagePage(buttonCaps.UsagePage), HIDUsage(usage), + std::make_pair(0, 1), 1)); } } else { - inputItems.insert(std::make_pair(buttonCaps.NotRange.DataIndex, - HIDMainItem(buttonCaps.BitField, HIDUsagePage(buttonCaps.UsagePage), - HIDUsage(buttonCaps.NotRange.Usage), std::make_pair(0, 1), 1))); + inputItems.emplace(buttonCaps.NotRange.DataIndex, + HIDMainItem(buttonCaps.BitField, HIDUsagePage(buttonCaps.UsagePage), + HIDUsage(buttonCaps.NotRange.Usage), std::make_pair(0, 1), 1)); } } } @@ -485,15 +485,15 @@ HIDParser::ParserStatus HIDParser::Parse(const PHIDP_PREPARSED_DATA descriptorDa if (valueCaps.IsRange) { int usage = valueCaps.Range.UsageMin; for (int i = valueCaps.Range.DataIndexMin; i <= valueCaps.Range.DataIndexMax; ++i, ++usage) { - inputItems.insert(std::make_pair( - i, HIDMainItem(valueCaps.BitField, HIDUsagePage(valueCaps.UsagePage), HIDUsage(usage), - std::make_pair(valueCaps.LogicalMin, valueCaps.LogicalMax), valueCaps.BitSize))); + inputItems.emplace(i, HIDMainItem(valueCaps.BitField, HIDUsagePage(valueCaps.UsagePage), HIDUsage(usage), + std::make_pair(valueCaps.LogicalMin, valueCaps.LogicalMax), + valueCaps.BitSize)); } } else { - inputItems.insert(std::make_pair( - valueCaps.NotRange.DataIndex, - HIDMainItem(valueCaps.BitField, HIDUsagePage(valueCaps.UsagePage), HIDUsage(valueCaps.NotRange.Usage), - HIDRange(valueCaps.LogicalMin, valueCaps.LogicalMax), valueCaps.BitSize))); + inputItems.emplace(valueCaps.NotRange.DataIndex, + HIDMainItem(valueCaps.BitField, HIDUsagePage(valueCaps.UsagePage), + HIDUsage(valueCaps.NotRange.Usage), + HIDRange(valueCaps.LogicalMin, valueCaps.LogicalMax), valueCaps.BitSize)); } } }