diff --git a/src/arch/Lights/LightsDriver_HidBlueDot.cpp b/src/arch/Lights/LightsDriver_HidBlueDot.cpp index 9224c65878..8a050245b2 100644 --- a/src/arch/Lights/LightsDriver_HidBlueDot.cpp +++ b/src/arch/Lights/LightsDriver_HidBlueDot.cpp @@ -5,7 +5,7 @@ REGISTER_LIGHTS_DRIVER_CLASS(HidBlueDot); LightsDriver_HidBlueDot::LightsDriver_HidBlueDot() : - dev{ VID, pids }, + dev{ VID, PID }, m_iCabData{ 0 }, m_iPadData{ 0 } { diff --git a/src/arch/Lights/LightsDriver_HidBlueDot.h b/src/arch/Lights/LightsDriver_HidBlueDot.h index b928c5db14..68a38c6065 100644 --- a/src/arch/Lights/LightsDriver_HidBlueDot.h +++ b/src/arch/Lights/LightsDriver_HidBlueDot.h @@ -7,8 +7,6 @@ #define VID 0x04BD #define PID 0xBD -static constexpr int pids[] = { PID }; - class LightsDriver_HidBlueDot : public LightsDriver { private: diff --git a/src/arch/Lights/LightsDriver_PacDrive.cpp b/src/arch/Lights/LightsDriver_PacDrive.cpp index 78d8b95a65..30986e5100 100644 --- a/src/arch/Lights/LightsDriver_PacDrive.cpp +++ b/src/arch/Lights/LightsDriver_PacDrive.cpp @@ -9,7 +9,7 @@ REGISTER_LIGHTS_DRIVER_CLASS(PacDrive); static Preference g_sPacDriveLightOrdering("PacDriveLightOrdering", "openitg"); int iPacDriveLightOrder = 0; -LightsDriver_PacDrive::LightsDriver_PacDrive() : dev{PACDRIVE_VID, GetPids(), PACDRIVE_INTERFACE} +LightsDriver_PacDrive::LightsDriver_PacDrive() : dev{PACDRIVE_VID, make_pids(PACDRIVE_PID, PACDRIVE_PID_MAX), PACDRIVE_INTERFACE} { prev_led_state.raw = 0; memset(state.raw_state, 0x00, sizeof(state.raw_state)); diff --git a/src/arch/Lights/LightsDriver_PacDrive.h b/src/arch/Lights/LightsDriver_PacDrive.h index 793f591533..ad84de68f3 100644 --- a/src/arch/Lights/LightsDriver_PacDrive.h +++ b/src/arch/Lights/LightsDriver_PacDrive.h @@ -28,16 +28,6 @@ #define PACDRIVE_PID 0x1500 #define PACDRIVE_PID_MAX 8 -static constexpr int* GetPids() -{ - int vec[PACDRIVE_PID]{0}; - - for (int i = 0; i < PACDRIVE_PID_MAX; i++) - vec[i] = PACDRIVE_PID + i; - - return vec; -} - #define PACDRIVE_INTERFACE 0 // the first byte of the buffer is a static report id. diff --git a/src/arch/Lights/LightsDriver_snek.cpp b/src/arch/Lights/LightsDriver_snek.cpp index 28995cbde5..9dfd393b1e 100644 --- a/src/arch/Lights/LightsDriver_snek.cpp +++ b/src/arch/Lights/LightsDriver_snek.cpp @@ -8,7 +8,7 @@ REGISTER_LIGHTS_DRIVER_CLASS(snek); LightsDriver_snek::LightsDriver_snek() : - dev{ SNEK_VID , pids, SNEK_LIGHTING_INTERFACENUM }, + dev{ SNEK_VID , SNEK_PID, SNEK_LIGHTING_INTERFACENUM }, stateChanged{ false }, outputBuffer{0} { diff --git a/src/arch/Lights/LightsDriver_snek.h b/src/arch/Lights/LightsDriver_snek.h index 8e895b4e95..5b19174051 100644 --- a/src/arch/Lights/LightsDriver_snek.h +++ b/src/arch/Lights/LightsDriver_snek.h @@ -95,9 +95,6 @@ enum SnekLightIndex #define SNEK_INDEX_DANCE_P2_LEFT SNEK_LIGHTINDEX_CN11_FL3 #define SNEK_INDEX_DANCE_P2_RIGHT SNEK_LIGHTINDEX_CN11_FL4 -static constexpr int pids[] = { SNEK_PID }; - - class LightsDriver_snek : public LightsDriver { private: diff --git a/src/arch/Lights/LightsDriver_stac.cpp b/src/arch/Lights/LightsDriver_stac.cpp index c27f9661ba..b461b8e02e 100644 --- a/src/arch/Lights/LightsDriver_stac.cpp +++ b/src/arch/Lights/LightsDriver_stac.cpp @@ -7,7 +7,7 @@ REGISTER_LIGHTS_DRIVER_CLASS(stac); LightsDriver_stac::LightsDriver_stac() : - devs{ {STAC_VID , pids1, STAC_LIGHTING_INTERFACE}, {STAC_VID , pids2, STAC_LIGHTING_INTERFACE } }, + devs{ {STAC_VID , STAC_PID_P1, STAC_LIGHTING_INTERFACE}, {STAC_VID , STAC_PID_P2, STAC_LIGHTING_INTERFACE } }, stateChanged{ false }, outputBuffer{ {0} } { diff --git a/src/arch/Lights/LightsDriver_stac.h b/src/arch/Lights/LightsDriver_stac.h index 914dd6ff75..ee2f84be57 100644 --- a/src/arch/Lights/LightsDriver_stac.h +++ b/src/arch/Lights/LightsDriver_stac.h @@ -47,10 +47,6 @@ enum StacLightIndex STAC_LIGHTINDEX_MAX }; -static constexpr int pids1[] = { STAC_PID_P1 }; -static constexpr int pids2[] = { STAC_PID_P2 }; - - class LightsDriver_stac : public LightsDriver { private: diff --git a/src/archutils/Common/HidDevice.cpp b/src/archutils/Common/HidDevice.cpp index 9c20ab6443..7ebd9f97ca 100644 --- a/src/archutils/Common/HidDevice.cpp +++ b/src/archutils/Common/HidDevice.cpp @@ -2,7 +2,7 @@ #include "HidDevice.h" #include "RageLog.h" -HidDevice::HidDevice(int vid, const int pids[], int interfaceNum, bool autoReconnection, bool nonBlockingWrite) : +HidDevice::HidDevice(int vid, const std::vector pids, int interfaceNum, bool autoReconnection, bool nonBlockingWrite) : vid{ vid }, pids{ pids }, interfaceNum{ interfaceNum }, @@ -13,13 +13,18 @@ HidDevice::HidDevice(int vid, const int pids[], int interfaceNum, bool autoRecon if (!result) { - LOG->Warn("HID device with VID/PID %04x/%s not found.", vid, GetPidsString(pids).c_str()); + LOG->Warn("HidDevice %04x:%s: %d not found", vid, GetPidsString(pids).c_str(), interfaceNum); return; } else foundOnce = true; } +HidDevice::HidDevice(int vid, int pid, int interfaceNum, bool autoReconnection, bool nonBlockingWrite) : + HidDevice(vid, make_pids(pid, 1), interfaceNum, autoReconnection, nonBlockingWrite) +{ +} + HidDevice::~HidDevice() { Close(); @@ -40,7 +45,7 @@ bool HidDevice::Open(const char* path) hid_set_nonblocking(handle, 1); if(handle) - LOG->Info("HidDevice opened %04x:%04x:%d by path %s", vid, GetPidsString(pids).c_str(), interfaceNum, path); + LOG->Info("HidDevice %04x:%s: %d opened by path %s", vid, GetPidsString(pids).c_str(), interfaceNum, path); return handle != nullptr; } @@ -80,11 +85,11 @@ bool HidDevice::FoundOnce() return foundOnce; } -const RString HidDevice::GetPidsString(const int pids[]) +const RString HidDevice::GetPidsString(const std::vector pids) { RString pidsString; char pid[5] = { 0 }; - size_t size = sizeof(pids) / sizeof(pids[0]); + size_t size = pids.size(); for (size_t i = 0; i < size; ++i) { @@ -98,10 +103,10 @@ const RString HidDevice::GetPidsString(const int pids[]) return pidsString; } -char* HidDevice::GetPath(int vid, const int pids[], int interfaceNumber) +char* HidDevice::GetPath(int vid, const std::vector pids, int interfaceNumber) { struct hid_device_info* devs, * cur_dev; - size_t size = sizeof(pids) / sizeof(pids[0]); + size_t size = pids.size(); devs = hid_enumerate(vid, 0); cur_dev = devs; @@ -138,28 +143,30 @@ char* HidDevice::GetPath(int vid, const int pids[], int interfaceNumber) int HidDevice::Read(unsigned char* data, size_t length) { if (!CheckConnection()) - return -1; + return NOT_CONNECTED; int result = hid_read(handle, data, length); - if (result == -1) + if (result == FAIL) { - LOG->Warn("HID device with VID/PID %04x/%s read failed. Fail reason %ls", vid, GetPidsString(pids).c_str(), GetError()); + LOG->Warn("HidDevice %04x:%s: %d read failed. Fail reason %ls", vid, GetPidsString(pids).c_str(), interfaceNum, GetError()); } return result; } -void HidDevice::Write(const unsigned char* data, size_t length) +int HidDevice::Write(const unsigned char* data, size_t length) { if (!CheckConnection()) - return; + return NOT_CONNECTED; int result = hid_write(handle, data, length); if (result != length) { - LOG->Warn("HID device with VID/PID %04x/%s write failed. Fail reason %ls", vid, GetPidsString(pids).c_str(), GetError()); + LOG->Warn("HidDevice %04x:%s: %d write failed. Fail reason %ls", vid, GetPidsString(pids).c_str(), interfaceNum, GetError()); Close(); } + + return result; } diff --git a/src/archutils/Common/HidDevice.h b/src/archutils/Common/HidDevice.h index bea8097a99..c213cff853 100644 --- a/src/archutils/Common/HidDevice.h +++ b/src/archutils/Common/HidDevice.h @@ -2,16 +2,30 @@ #define HidDevice_H #include "hidapi.h" +#include "vector" + +#define FAIL -1 +#define NOT_CONNECTED -2 + +static std::vector make_pids(int base_pid, int size) +{ + std::vector vec(size); + + for (int i = 0; i < size; i++) + vec[i] = base_pid + i; + + return vec; +} class HidDevice { private: hid_device* handle{nullptr}; - static const RString GetPidsString(const int pids[]); + static const RString GetPidsString(const std::vector pids); int vid; - const int* pids; + const std::vector pids; int interfaceNum = -1; bool autoReconnection = true; bool nonBlockingWrite = false; @@ -23,9 +37,10 @@ private: bool CheckConnection(); const wchar_t* GetError(); public: - static char* GetPath(int vid, const int pids[], int interfaceNum = -1); + static char* GetPath(int vid, const std::vector pids, int interfaceNum = -1); - HidDevice(int vid, const int pids[], int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingWrite = false); + HidDevice(int vid, const std::vector pids, int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingWrite = false); + HidDevice(int vid, int pid, int interfaceNum = -1, bool autoReconnection = true, bool nonBlockingWrite = false); virtual ~HidDevice(); @@ -33,7 +48,7 @@ public: bool FoundOnce(); int Read(unsigned char* data, size_t length); - void Write(const unsigned char* data, size_t length); + int Write(const unsigned char* data, size_t length); }; #endif