-
Notifications
You must be signed in to change notification settings - Fork 3
/
HIDManager.cpp
113 lines (88 loc) · 3.25 KB
/
HIDManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "nilHID.h"
#include "nilUtil.h"
namespace Nil {
HIDManager::HIDManager()
{
initialize();
}
const HIDRecordList& HIDManager::getRecords() const
{
return mRecords;
}
void HIDManager::onPnPPlug( const GUID& deviceClass, const wideString& devicePath )
{
if ( deviceClass != g_HIDInterfaceGUID )
return;
for ( auto record : mRecords )
if ( !_wcsicmp( record->getPath().c_str(), devicePath.c_str() ) )
return;
SafeHandle deviceHandle( CreateFileW( devicePath.c_str(), 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ) );
if ( deviceHandle.valid() )
{
auto record = new HIDRecord( devicePath, deviceHandle );
mRecords.push_back( record );
}
}
void HIDManager::onPnPUnplug( const GUID& deviceClass, const wideString& devicePath )
{
if ( deviceClass != g_HIDInterfaceGUID )
return;
for ( auto record : mRecords )
if ( !_wcsicmp( record->getPath().c_str(), devicePath.c_str() ) )
{
mRecords.remove( record );
delete record;
return;
}
}
void HIDManager::processDevice( SP_DEVICE_INTERFACE_DATA& interfaceData,
SP_DEVINFO_DATA& deviceData, const wideString& devicePath )
{
if ( interfaceData.InterfaceClassGuid != g_HIDInterfaceGUID )
return;
// Well, this string comparison is kind of nasty, but it seems
// to be what everyone does. Nothing else is quite reliable enough.
for ( auto record : mRecords )
if ( !_wcsicmp( record->getPath().c_str(), devicePath.c_str() ) )
return;
SafeHandle deviceHandle( CreateFileW( devicePath.c_str(), 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL ) );
if ( deviceHandle.valid() )
{
auto record = new HIDRecord( devicePath, deviceHandle );
mRecords.push_back( record );
}
}
void HIDManager::initialize()
{
SP_DEVICE_INTERFACE_DATA interfaceData = { sizeof( SP_DEVICE_INTERFACE_DATA ) };
auto info = SetupDiGetClassDevsW( &g_HIDInterfaceGUID,
NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE );
if ( info == INVALID_HANDLE_VALUE )
NIL_EXCEPT_WINAPI( "SetupDiGetClassDevsW failed" );
for ( unsigned long i = 0; SetupDiEnumDeviceInterfaces(
info, 0, &g_HIDInterfaceGUID, i, &interfaceData ); i++ )
{
unsigned long length = 0;
SetupDiGetDeviceInterfaceDetailW( info, &interfaceData, NULL, 0, &length, NULL );
if ( GetLastError() != ERROR_INSUFFICIENT_BUFFER )
NIL_EXCEPT_WINAPI( "SetupDiGetDeviceInterfaceDetailW failed" );
auto detailData = (PSP_DEVICE_INTERFACE_DETAIL_DATA_W)malloc( (size_t)length );
detailData->cbSize = sizeof( SP_INTERFACE_DEVICE_DETAIL_DATA_W );
SP_DEVINFO_DATA deviceData = { sizeof( SP_DEVINFO_DATA ) };
if ( SetupDiGetDeviceInterfaceDetailW( info, &interfaceData,
detailData, length, NULL, &deviceData ) )
{
processDevice( interfaceData, deviceData, detailData->DevicePath );
}
free( detailData );
}
SetupDiDestroyDeviceInfoList( info );
}
HIDManager::~HIDManager()
{
for ( auto record : mRecords )
delete record;
}
}