This commit is contained in:
k1-801
2020-12-08 01:52:39 +04:00
committed by Adam Honse
parent 5a5cc52a80
commit e050ca8350

View File

@@ -2,7 +2,6 @@
#include "SinowealthController.h"
#include "RGBController.h"
#include "RGBController_Sinowealth.h"
#include <vector>
#include <hidapi/hidapi.h>
#define SINOWEALTH_VID 0x258A
@@ -19,13 +18,6 @@ typedef struct
#define SINOWEALTH_NUM_DEVICES (sizeof(device_list) / sizeof(device_list[ 0 ]))
static const sinowealth_device device_list[] =
{
{ SINOWEALTH_VID, Glorious_Model_O_PID, "Glorious Model O / O- " },
{ SINOWEALTH_VID, Glorious_Model_D_PID, "Glorious Model D / D- " },
{ SINOWEALTH_VID, Everest_GT100_PID , "Everest GT-100 RGB" },
};
/******************************************************************************************\
* *
* DetectSinowealthControllers *
@@ -34,114 +26,75 @@ static const sinowealth_device device_list[] =
* *
\******************************************************************************************/
void DetectSinowealthControllers(std::vector<RGBController*>& rgb_controllers)
void DetectSinowealthControllers(hid_device_info* info, const std::string& name)
{
hid_device_info* info;
int pass = 0;
#ifdef _WIN32
hid_init();
for(std::size_t device_idx = 0; device_idx < SINOWEALTH_NUM_DEVICES; device_idx++)
/*-------------------------------------------------------------------------------------------------*\
| Sinowealth devices use 3 different Report IDs on the same Usage Page. |
| The 4 on 0xFF00 is for RGB, 7 is Unknown and 5 is for Commands. |
| HOWEVER, we can NOT get the report ids reliably, we only have USAGES and they are the SAME for |
| all three (1), so we try to rely on their order being the same. If it's not, we're screwed. |
\*-------------------------------------------------------------------------------------------------*/
hid_device* usages[3];
int usage_count = 0;
hid_device_info* info_temp = info;
while(info_temp)
{
if(info_temp->vendor_id == info->vendor_id // constant SINOWEALTH_VID
&& info_temp->product_id == info->product_id // NON-constant
&& info_temp->usage_page == info->usage_page) // constant 0xFF00
{
if(usage_count > 3)
{
// Error! We only know what to do with those with 3 entries
break;
}
usages[usage_count] = hid_open_path(info_temp->path);
if(usages[usage_count])
{
++usage_count;
}
// An error otherwise?
}
info_temp = info_temp->next;
}
if(usage_count == 3)
{
SinowealthController* controller = new SinowealthController(usages[0], usages[2], info->path);
RGBController_Sinowealth* rgb_controller = new RGBController_Sinowealth(controller);
rgb_controller->name = name;
ResourceManager::get()->RegisterRGBController(rgb_controller);
}
else
{
for(int i = 0; i < usage_count; ++i)
{
if(usages[i])
{
hid_close(usages[i]);
}
}
}
#else
hid_device* dev = hid_open_path(info->path);
if(dev)
{
info = hid_enumerate(device_list[device_idx].usb_vid, device_list[device_idx].usb_pid);
/*-------------------------------------------------------------------------------------------------*\
| Sinowealth devices use 3 different Report IDs on the same Usage Page. |
| The 4 on 0xFF00 is for RGB, 7 is Unknown and 5 is for Commands. |
\*-------------------------------------------------------------------------------------------------*/
//Look for Sinowealth Peripheral
while(info)
{
if((info->vendor_id == device_list[device_idx].usb_vid)
#ifdef USE_HID_USAGE
&&(info->product_id == device_list[device_idx].usb_pid)
&&(info->usage_page == 0xFF00))
#else
&&(info->product_id == device_list[device_idx].usb_pid)
&&(info->interface_number == 1))
#endif
{
//Open first
pass = 1;
hid_device* dev_report_id_4 = hid_open_path(info->path);
if( dev_report_id_4 )
{
#ifdef USE_HID_USAGE
hid_device_info* tmp_info_id_7 = info;
while(tmp_info_id_7)
{
if((tmp_info_id_7->vendor_id == device_list[device_idx].usb_vid)
&&(tmp_info_id_7->product_id == device_list[device_idx].usb_pid)
&&(tmp_info_id_7->usage_page == 0xFF00))
{
//skip second
tmp_info_id_7 = tmp_info_id_7->next;
hid_device_info* tmp_info_id_5 = tmp_info_id_7;
while(tmp_info_id_5)
{
if((tmp_info_id_5->vendor_id == device_list[device_idx].usb_vid)
&&(tmp_info_id_5->product_id == device_list[device_idx].usb_pid)
&&(tmp_info_id_5->usage_page == 0xFF00))
{
//Open third
hid_device* dev_report_id_5 = hid_open_path(tmp_info_id_5->path);
if(dev_report_id_5)
{
SinowealthController* controller = new SinowealthController(dev_report_id_4, dev_report_id_5, info->path);
RGBController_Sinowealth* rgb_controller = new RGBController_Sinowealth(controller);
rgb_controller->name = device_list[device_idx].name;
rgb_controllers.push_back(rgb_controller);
int pass = 3;
}
}
if (pass == 2 && tmp_info_id_5)
{
tmp_info_id_5 = tmp_info_id_5->next;
}
else break;
}
}
if (pass == 1 && tmp_info_id_7)
{
tmp_info_id_7 = tmp_info_id_7->next;
}
else break;
}
#else
SinowealthController* controller = new SinowealthController(dev_report_id_4, dev_report_id_4, info->path);
RGBController_Sinowealth* rgb_controller = new RGBController_Sinowealth(controller);
rgb_controller->name = device_list[device_idx].name;
rgb_controllers.push_back(rgb_controller);
#endif
}
}
if (pass == 0)
{
info = info->next;
}
else break;
}
SinowealthController* controller = new SinowealthController(dev, dev, info->path);
RGBController_Sinowealth* rgb_controller = new RGBController_Sinowealth(controller);
rgb_controller->name = name;
ResourceManager::get()->RegisterRGBController(rgb_controller);
}
#endif
}
REGISTER_DETECTOR("Sinowealth Device", DetectSinowealthControllers);
#ifdef USE_HID_USAGE
REGISTER_HID_DETECTOR_P("Glorious Model O / O-", DetectSinowealthControllers, SINOWEALTH_VID, Glorious_Model_O_PID, 0xFF00);
REGISTER_HID_DETECTOR_P("Glorious Model D / D-", DetectSinowealthControllers, SINOWEALTH_VID, Glorious_Model_D_PID, 0xFF00);
REGISTER_HID_DETECTOR_P("Everest GT-100 RGB", DetectSinowealthControllers, SINOWEALTH_VID, Everest_GT100_PID, 0xFF00);
#else
REGISTER_HID_DETECTOR_I("Glorious Model O / O-", DetectSinowealthControllers, SINOWEALTH_VID, Glorious_Model_O_PID, 1);
REGISTER_HID_DETECTOR_I("Glorious Model D / D-", DetectSinowealthControllers, SINOWEALTH_VID, Glorious_Model_D_PID, 1);
REGISTER_HID_DETECTOR_I("Everest GT-100 RGB", DetectSinowealthControllers, SINOWEALTH_VID, Everest_GT100_PID, 1);
#endif