boo/lib/inputdev/DeviceSignature.cpp

79 lines
1.9 KiB
C++
Raw Normal View History

2015-08-18 22:43:30 +00:00
#include "boo/inputdev/DeviceSignature.hpp"
#include "boo/inputdev/DeviceToken.hpp"
#include "boo/inputdev/GenericPad.hpp"
#include "IHIDDevice.hpp"
namespace boo
{
2015-08-18 19:40:26 +00:00
extern const DeviceSignature BOO_DEVICE_SIGS[];
2015-08-18 19:40:26 +00:00
bool DeviceSignature::DeviceMatchToken(const DeviceToken& token, const TDeviceSignatureSet& sigSet)
{
2015-08-18 19:40:26 +00:00
if (token.getDeviceType() == DeviceToken::DEVTYPE_GENERICHID)
return true;
2015-08-18 19:40:26 +00:00
for (const DeviceSignature* sig : sigSet)
{
if (sig->m_vid == token.getVendorId() && sig->m_pid == token.getProductId())
return true;
}
return false;
}
2015-08-18 19:40:26 +00:00
IHIDDevice* IHIDDeviceNew(DeviceToken& token, DeviceBase& devImp);
DeviceBase* DeviceSignature::DeviceNew(DeviceToken& token)
{
2015-08-18 19:40:26 +00:00
DeviceBase* retval = NULL;
/* Early-return for generic HID devices */
2015-08-18 19:40:26 +00:00
if (token.getDeviceType() == DeviceToken::DEVTYPE_GENERICHID)
{
2015-08-18 19:40:26 +00:00
retval = new GenericPad(&token);
if (!retval)
return NULL;
IHIDDevice* newDev = IHIDDeviceNew(token, *retval);
if (!newDev)
{
delete retval;
return NULL;
}
return retval;
}
/* Otherwise perform signature-matching to find the appropriate device-factory */
2015-08-18 19:40:26 +00:00
const DeviceSignature* foundSig = NULL;
const DeviceSignature* sigIter = BOO_DEVICE_SIGS;
unsigned targetVid = token.getVendorId();
unsigned targetPid = token.getProductId();
while (sigIter->m_name)
{
if (sigIter->m_vid == targetVid && sigIter->m_pid == targetPid)
{
foundSig = sigIter;
break;
}
++sigIter;
}
if (!foundSig)
return NULL;
retval = foundSig->m_factory(&token);
if (!retval)
return NULL;
IHIDDevice* newDev = IHIDDeviceNew(token, *retval);
if (!newDev)
{
delete retval;
return NULL;
}
return retval;
}
}