oculus1
view libovr/Src/win32/OVR_Win32_HIDDevice.h @ 29:9a973ef0e2a3
fixed the performance issue under MacOSX by replacing glutSolidTeapot (which
uses glEvalMesh) with my own teapot generator.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 27 Oct 2013 06:31:18 +0200 |
parents | |
children |
line source
1 /************************************************************************************
3 Filename : OVR_Win32_HIDDevice.h
4 Content : Win32 HID device implementation.
5 Created : February 22, 2013
6 Authors : Lee Cooper
8 Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
10 Use of this software is subject to the terms of the Oculus license
11 agreement provided at the time of installation or download, or which
12 otherwise accompanies this software in either electronic or hard copy form.
14 *************************************************************************************/
16 #ifndef OVR_Win32_HIDDevice_h
17 #define OVR_Win32_HIDDevice_h
19 #include "OVR_HIDDevice.h"
20 #include "OVR_Win32_DeviceManager.h"
22 #include <windows.h>
23 #include <setupapi.h>
25 //-------------------------------------------------------------------------------------
26 // Define needed "hidsdi.h" functionality to avoid requiring DDK installation.
27 // #include "hidsdi.h"
29 #ifndef _HIDSDI_H
30 #define _HIDSDI_H
31 #include <pshpack4.h>
33 #define HIDP_STATUS_SUCCESS (0x11 << 16)
34 struct HIDP_PREPARSED_DATA;
36 struct HIDD_ATTRIBUTES
37 {
38 ULONG Size; // = sizeof (struct _HIDD_ATTRIBUTES)
39 USHORT VendorID;
40 USHORT ProductID;
41 USHORT VersionNumber;
42 };
44 struct HIDP_CAPS
45 {
46 USHORT Usage;
47 USHORT UsagePage;
48 USHORT InputReportByteLength;
49 USHORT OutputReportByteLength;
50 USHORT FeatureReportByteLength;
51 USHORT Reserved[17];
53 USHORT NumberLinkCollectionNodes;
54 USHORT NumberInputButtonCaps;
55 USHORT NumberInputValueCaps;
56 USHORT NumberInputDataIndices;
57 USHORT NumberOutputButtonCaps;
58 USHORT NumberOutputValueCaps;
59 USHORT NumberOutputDataIndices;
60 USHORT NumberFeatureButtonCaps;
61 USHORT NumberFeatureValueCaps;
62 USHORT NumberFeatureDataIndices;
63 };
65 #include <poppack.h>
66 #endif
69 namespace OVR { namespace Win32 {
71 class HIDDeviceManager;
72 class DeviceManager;
74 //-------------------------------------------------------------------------------------
75 // ***** Win32 HIDDevice
77 class HIDDevice : public OVR::HIDDevice, public DeviceManagerThread::Notifier
78 {
79 public:
81 HIDDevice(HIDDeviceManager* manager);
83 // This is a minimal constructor used during enumeration for us to pass
84 // a HIDDevice to the visit function (so that it can query feature reports).
85 HIDDevice(HIDDeviceManager* manager, HANDLE device);
87 ~HIDDevice();
89 bool HIDInitialize(const String& path);
90 void HIDShutdown();
92 // OVR::HIDDevice
93 bool SetFeatureReport(UByte* data, UInt32 length);
94 bool GetFeatureReport(UByte* data, UInt32 length);
97 // DeviceManagerThread::Notifier
98 void OnOverlappedEvent(HANDLE hevent);
99 UInt64 OnTicks(UInt64 ticksMks);
100 bool OnDeviceMessage(DeviceMessageType messageType, const String& devicePath, bool* error);
102 private:
103 bool openDevice();
104 bool initInfo();
105 bool initializeRead();
106 bool processReadResult();
107 void closeDevice();
108 void closeDeviceOnIOError();
110 bool inMinimalMode;
111 HIDDeviceManager* HIDManager;
112 HANDLE Device;
113 HIDDeviceDesc DevDesc;
115 OVERLAPPED ReadOverlapped;
116 bool ReadRequested;
118 enum { ReadBufferSize = 96 };
119 UByte ReadBuffer[ReadBufferSize];
121 UInt16 InputReportBufferLength;
122 UInt16 OutputReportBufferLength;
123 UInt16 FeatureReportBufferLength;
124 };
126 //-------------------------------------------------------------------------------------
127 // ***** Win32 HIDDeviceManager
129 class HIDDeviceManager : public OVR::HIDDeviceManager
130 {
131 friend class HIDDevice;
132 public:
134 HIDDeviceManager(DeviceManager* manager);
135 virtual ~HIDDeviceManager();
137 virtual bool Initialize();
138 virtual void Shutdown();
140 virtual bool Enumerate(HIDEnumerateVisitor* enumVisitor);
141 virtual OVR::HIDDevice* Open(const String& path);
143 // Fills HIDDeviceDesc by using the path.
144 // Returns 'true' if successful, 'false' otherwise.
145 bool GetHIDDeviceDesc(const String& path, HIDDeviceDesc* pdevDesc) const;
147 GUID GetHIDGuid() { return HidGuid; }
149 static HIDDeviceManager* CreateInternal(DeviceManager* manager);
151 private:
153 DeviceManager* Manager; // Back pointer can just be a raw pointer.
155 HMODULE hHidLib;
156 GUID HidGuid;
158 // Macros to declare and resolve needed functions from library.
159 #define OVR_DECLARE_HIDFUNC(func, rettype, args) \
160 typedef rettype (__stdcall *PFn_##func) args; \
161 PFn_##func func;
162 #define OVR_RESOLVE_HIDFUNC(func) \
163 func = (PFn_##func)::GetProcAddress(hHidLib, #func)
165 OVR_DECLARE_HIDFUNC(HidD_GetHidGuid, void, (GUID *hidGuid));
166 OVR_DECLARE_HIDFUNC(HidD_SetNumInputBuffers, BOOLEAN, (HANDLE hidDev, ULONG numberBuffers));
167 OVR_DECLARE_HIDFUNC(HidD_GetFeature, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
168 OVR_DECLARE_HIDFUNC(HidD_SetFeature, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
169 OVR_DECLARE_HIDFUNC(HidD_GetAttributes, BOOLEAN, (HANDLE hidDev, HIDD_ATTRIBUTES *attributes));
170 OVR_DECLARE_HIDFUNC(HidD_GetManufacturerString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
171 OVR_DECLARE_HIDFUNC(HidD_GetProductString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
172 OVR_DECLARE_HIDFUNC(HidD_GetSerialNumberString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
173 OVR_DECLARE_HIDFUNC(HidD_GetPreparsedData, BOOLEAN, (HANDLE hidDev, HIDP_PREPARSED_DATA **preparsedData));
174 OVR_DECLARE_HIDFUNC(HidD_FreePreparsedData, BOOLEAN, (HIDP_PREPARSED_DATA *preparsedData));
175 OVR_DECLARE_HIDFUNC(HidP_GetCaps, NTSTATUS,(HIDP_PREPARSED_DATA *preparsedData, HIDP_CAPS* caps));
177 HANDLE CreateHIDFile(const char* path, bool exclusiveAccess = true) const
178 {
179 return ::CreateFileA(path, GENERIC_WRITE|GENERIC_READ,
180 (!exclusiveAccess) ? (FILE_SHARE_READ|FILE_SHARE_WRITE) : 0x0,
181 NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
182 }
184 // Helper functions to fill in HIDDeviceDesc from open device handle.
185 bool initVendorProductVersion(HANDLE hidDev, HIDDeviceDesc* desc) const;
186 bool initUsage(HANDLE hidDev, HIDDeviceDesc* desc) const;
187 void initStrings(HANDLE hidDev, HIDDeviceDesc* desc) const;
189 bool getFullDesc(HANDLE hidDev, HIDDeviceDesc* desc) const;
190 };
192 }} // namespace OVR::Win32
194 #endif // OVR_Win32_HIDDevice_h