HIDParser: Use std::array where applicable

Same thing, but strongly enforces the type of the array. This also
allows removing the <type_traits> include, since we can just query the
size of the array.
This commit is contained in:
Lioncash 2019-08-24 20:36:48 -04:00
parent b0c246abc7
commit 14369a9853
1 changed files with 266 additions and 250 deletions

View File

@ -1,8 +1,8 @@
#include "boo/inputdev/HIDParser.hpp"
#include <algorithm>
#include <array>
#include <map>
#include <type_traits>
#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<const char*, 14> 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<const char*, 182> 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<const char*, 58> 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<decltype(UsagePageNames)>::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<decltype(GenericDesktopUsages)>::value)
if (index >= GenericDesktopUsages.size()) {
return nullptr;
return GenericDesktopUsages[int(m_usage)];
}
return GenericDesktopUsages[index];
case HIDUsagePage::Game:
if (int(m_usage) >= std::extent<decltype(GameUsages)>::value)
if (index >= GameUsages.size()) {
return nullptr;
return GameUsages[int(m_usage)];
}
return GameUsages[index];
default:
return nullptr;
}