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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
/************************************************************************************
Filename : OVR_Win32_HIDDevice.h
Content : Win32 HID device implementation.
Created : February 22, 2013
Authors : Lee Cooper
Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.
You may obtain a copy of the License at
http://www.oculusvr.com/licenses/LICENSE-3.1
Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************************/
#ifndef OVR_Win32_HIDDevice_h
#define OVR_Win32_HIDDevice_h
#include "OVR_HIDDevice.h"
#include "OVR_Win32_DeviceManager.h"
#include <windows.h>
#include <setupapi.h>
//-------------------------------------------------------------------------------------
// Define needed "hidsdi.h" functionality to avoid requiring DDK installation.
// #include "hidsdi.h"
#ifndef _HIDSDI_H
#define _HIDSDI_H
#include <pshpack4.h>
#define HIDP_STATUS_SUCCESS (0x11 << 16)
struct HIDP_PREPARSED_DATA;
struct HIDD_ATTRIBUTES
{
ULONG Size; // = sizeof (struct _HIDD_ATTRIBUTES)
USHORT VendorID;
USHORT ProductID;
USHORT VersionNumber;
};
struct HIDP_CAPS
{
USHORT Usage;
USHORT UsagePage;
USHORT InputReportByteLength;
USHORT OutputReportByteLength;
USHORT FeatureReportByteLength;
USHORT Reserved[17];
USHORT NumberLinkCollectionNodes;
USHORT NumberInputButtonCaps;
USHORT NumberInputValueCaps;
USHORT NumberInputDataIndices;
USHORT NumberOutputButtonCaps;
USHORT NumberOutputValueCaps;
USHORT NumberOutputDataIndices;
USHORT NumberFeatureButtonCaps;
USHORT NumberFeatureValueCaps;
USHORT NumberFeatureDataIndices;
};
#include <poppack.h>
#endif
namespace OVR { namespace Win32 {
class HIDDeviceManager;
class DeviceManager;
//-------------------------------------------------------------------------------------
// ***** Win32 HIDDevice
class HIDDevice : public OVR::HIDDevice, public DeviceManagerThread::Notifier
{
public:
HIDDevice(HIDDeviceManager* manager);
// This is a minimal constructor used during enumeration for us to pass
// a HIDDevice to the visit function (so that it can query feature reports).
HIDDevice(HIDDeviceManager* manager, HANDLE device);
~HIDDevice();
bool HIDInitialize(const String& path);
void HIDShutdown();
// OVR::HIDDevice
bool SetFeatureReport(UByte* data, UInt32 length);
bool GetFeatureReport(UByte* data, UInt32 length);
// DeviceManagerThread::Notifier
void OnOverlappedEvent(HANDLE hevent);
double OnTicks(double tickSeconds);
bool OnDeviceMessage(DeviceMessageType messageType, const String& devicePath, bool* error);
private:
bool openDevice();
bool initInfo();
bool initializeRead();
bool processReadResult();
void closeDevice();
void closeDeviceOnIOError();
bool inMinimalMode;
HIDDeviceManager* HIDManager;
HANDLE Device;
HIDDeviceDesc DevDesc;
OVERLAPPED ReadOverlapped;
bool ReadRequested;
enum { ReadBufferSize = 96 };
UByte ReadBuffer[ReadBufferSize];
UInt16 InputReportBufferLength;
UInt16 OutputReportBufferLength;
UInt16 FeatureReportBufferLength;
};
//-------------------------------------------------------------------------------------
// ***** Win32 HIDDeviceManager
class HIDDeviceManager : public OVR::HIDDeviceManager
{
friend class HIDDevice;
public:
HIDDeviceManager(DeviceManager* manager);
virtual ~HIDDeviceManager();
virtual bool Initialize();
virtual void Shutdown();
virtual bool Enumerate(HIDEnumerateVisitor* enumVisitor);
virtual OVR::HIDDevice* Open(const String& path);
// Fills HIDDeviceDesc by using the path.
// Returns 'true' if successful, 'false' otherwise.
bool GetHIDDeviceDesc(const String& path, HIDDeviceDesc* pdevDesc) const;
GUID GetHIDGuid() { return HidGuid; }
static HIDDeviceManager* CreateInternal(DeviceManager* manager);
private:
DeviceManager* Manager; // Back pointer can just be a raw pointer.
HMODULE hHidLib;
GUID HidGuid;
// Macros to declare and resolve needed functions from library.
#define OVR_DECLARE_HIDFUNC(func, rettype, args) \
typedef rettype (__stdcall *PFn_##func) args; \
PFn_##func func;
#define OVR_RESOLVE_HIDFUNC(func) \
func = (PFn_##func)::GetProcAddress(hHidLib, #func)
OVR_DECLARE_HIDFUNC(HidD_GetHidGuid, void, (GUID *hidGuid));
OVR_DECLARE_HIDFUNC(HidD_SetNumInputBuffers, BOOLEAN, (HANDLE hidDev, ULONG numberBuffers));
OVR_DECLARE_HIDFUNC(HidD_GetFeature, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
OVR_DECLARE_HIDFUNC(HidD_SetFeature, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
OVR_DECLARE_HIDFUNC(HidD_GetAttributes, BOOLEAN, (HANDLE hidDev, HIDD_ATTRIBUTES *attributes));
OVR_DECLARE_HIDFUNC(HidD_GetManufacturerString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
OVR_DECLARE_HIDFUNC(HidD_GetProductString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
OVR_DECLARE_HIDFUNC(HidD_GetSerialNumberString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
OVR_DECLARE_HIDFUNC(HidD_GetPreparsedData, BOOLEAN, (HANDLE hidDev, HIDP_PREPARSED_DATA **preparsedData));
OVR_DECLARE_HIDFUNC(HidD_FreePreparsedData, BOOLEAN, (HIDP_PREPARSED_DATA *preparsedData));
OVR_DECLARE_HIDFUNC(HidP_GetCaps, NTSTATUS,(HIDP_PREPARSED_DATA *preparsedData, HIDP_CAPS* caps));
HANDLE CreateHIDFile(const char* path, bool exclusiveAccess = true) const
{
return ::CreateFileA(path, GENERIC_WRITE|GENERIC_READ,
(!exclusiveAccess) ? (FILE_SHARE_READ|FILE_SHARE_WRITE) : 0x0,
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
}
// Helper functions to fill in HIDDeviceDesc from open device handle.
bool initVendorProductVersion(HANDLE hidDev, HIDDeviceDesc* desc) const;
bool initUsage(HANDLE hidDev, HIDDeviceDesc* desc) const;
void initStrings(HANDLE hidDev, HIDDeviceDesc* desc) const;
bool getFullDesc(HANDLE hidDev, HIDDeviceDesc* desc) const;
};
}} // namespace OVR::Win32
#endif // OVR_Win32_HIDDevice_h
|