oculus1

diff libovr/Src/win32/OVR_Win32_HIDDevice.h @ 1:e2f9e4603129

added LibOVR and started a simple vr wrapper.
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Sep 2013 16:14:59 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libovr/Src/win32/OVR_Win32_HIDDevice.h	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,194 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +Filename    :   OVR_Win32_HIDDevice.h
     1.7 +Content     :   Win32 HID device implementation.
     1.8 +Created     :   February 22, 2013
     1.9 +Authors     :   Lee Cooper
    1.10 +
    1.11 +Copyright   :   Copyright 2013 Oculus VR, Inc. All Rights reserved.
    1.12 +
    1.13 +Use of this software is subject to the terms of the Oculus license
    1.14 +agreement provided at the time of installation or download, or which
    1.15 +otherwise accompanies this software in either electronic or hard copy form.
    1.16 +
    1.17 +*************************************************************************************/
    1.18 +
    1.19 +#ifndef OVR_Win32_HIDDevice_h
    1.20 +#define OVR_Win32_HIDDevice_h
    1.21 +
    1.22 +#include "OVR_HIDDevice.h"
    1.23 +#include "OVR_Win32_DeviceManager.h"
    1.24 +
    1.25 +#include <windows.h>
    1.26 +#include <setupapi.h>
    1.27 +
    1.28 +//-------------------------------------------------------------------------------------
    1.29 +// Define needed "hidsdi.h" functionality to avoid requiring DDK installation.
    1.30 +// #include "hidsdi.h"
    1.31 +
    1.32 +#ifndef _HIDSDI_H
    1.33 +#define _HIDSDI_H
    1.34 +#include <pshpack4.h>
    1.35 +
    1.36 +#define HIDP_STATUS_SUCCESS (0x11 << 16)
    1.37 +struct HIDP_PREPARSED_DATA;
    1.38 +
    1.39 +struct HIDD_ATTRIBUTES
    1.40 +{
    1.41 +    ULONG   Size; // = sizeof (struct _HIDD_ATTRIBUTES)
    1.42 +    USHORT  VendorID;
    1.43 +    USHORT  ProductID;
    1.44 +    USHORT  VersionNumber;
    1.45 +};
    1.46 +
    1.47 +struct HIDP_CAPS
    1.48 +{
    1.49 +    USHORT   Usage;
    1.50 +    USHORT   UsagePage;
    1.51 +    USHORT   InputReportByteLength;
    1.52 +    USHORT   OutputReportByteLength;
    1.53 +    USHORT   FeatureReportByteLength;
    1.54 +    USHORT   Reserved[17];
    1.55 +
    1.56 +    USHORT   NumberLinkCollectionNodes;
    1.57 +    USHORT   NumberInputButtonCaps;
    1.58 +    USHORT   NumberInputValueCaps;
    1.59 +    USHORT   NumberInputDataIndices;
    1.60 +    USHORT   NumberOutputButtonCaps;
    1.61 +    USHORT   NumberOutputValueCaps;
    1.62 +    USHORT   NumberOutputDataIndices;
    1.63 +    USHORT   NumberFeatureButtonCaps;
    1.64 +    USHORT   NumberFeatureValueCaps;
    1.65 +    USHORT   NumberFeatureDataIndices;
    1.66 +};
    1.67 +
    1.68 +#include <poppack.h>
    1.69 +#endif
    1.70 +
    1.71 +
    1.72 +namespace OVR { namespace Win32 { 
    1.73 +
    1.74 +class HIDDeviceManager;
    1.75 +class DeviceManager;
    1.76 +
    1.77 +//-------------------------------------------------------------------------------------
    1.78 +// ***** Win32 HIDDevice
    1.79 +
    1.80 +class HIDDevice : public OVR::HIDDevice, public DeviceManagerThread::Notifier
    1.81 +{
    1.82 +public:
    1.83 +
    1.84 +    HIDDevice(HIDDeviceManager* manager);
    1.85 +
    1.86 +    // This is a minimal constructor used during enumeration for us to pass
    1.87 +    // a HIDDevice to the visit function (so that it can query feature reports). 
    1.88 +    HIDDevice(HIDDeviceManager* manager, HANDLE device);
    1.89 +
    1.90 +    ~HIDDevice();
    1.91 +
    1.92 +    bool HIDInitialize(const String& path);
    1.93 +    void HIDShutdown();
    1.94 +
    1.95 +    // OVR::HIDDevice
    1.96 +	bool SetFeatureReport(UByte* data, UInt32 length);
    1.97 +	bool GetFeatureReport(UByte* data, UInt32 length);
    1.98 +    
    1.99 +
   1.100 +    // DeviceManagerThread::Notifier
   1.101 +    void OnOverlappedEvent(HANDLE hevent);
   1.102 +    UInt64 OnTicks(UInt64 ticksMks);
   1.103 +    bool OnDeviceMessage(DeviceMessageType messageType, const String& devicePath, bool* error);
   1.104 +
   1.105 +private:
   1.106 +    bool openDevice();
   1.107 +    bool initInfo();
   1.108 +    bool initializeRead();
   1.109 +    bool processReadResult();
   1.110 +    void closeDevice();
   1.111 +    void closeDeviceOnIOError();
   1.112 +
   1.113 +    bool                inMinimalMode;
   1.114 +    HIDDeviceManager*   HIDManager;
   1.115 +	HANDLE              Device;
   1.116 +    HIDDeviceDesc       DevDesc; 
   1.117 +
   1.118 +    OVERLAPPED          ReadOverlapped;
   1.119 +    bool                ReadRequested;
   1.120 +
   1.121 +    enum { ReadBufferSize = 96 };
   1.122 +    UByte               ReadBuffer[ReadBufferSize];
   1.123 +
   1.124 +    UInt16              InputReportBufferLength;
   1.125 +    UInt16              OutputReportBufferLength;
   1.126 +    UInt16              FeatureReportBufferLength;
   1.127 +};
   1.128 +
   1.129 +//-------------------------------------------------------------------------------------
   1.130 +// ***** Win32 HIDDeviceManager
   1.131 +
   1.132 +class HIDDeviceManager : public OVR::HIDDeviceManager
   1.133 +{
   1.134 +	friend class HIDDevice;
   1.135 +public:
   1.136 +
   1.137 +	HIDDeviceManager(DeviceManager* manager);
   1.138 +    virtual ~HIDDeviceManager();
   1.139 +
   1.140 +    virtual bool Initialize();
   1.141 +    virtual void Shutdown();
   1.142 +
   1.143 +    virtual bool Enumerate(HIDEnumerateVisitor* enumVisitor);
   1.144 +    virtual OVR::HIDDevice* Open(const String& path);
   1.145 +
   1.146 +    // Fills HIDDeviceDesc by using the path.
   1.147 +    // Returns 'true' if successful, 'false' otherwise.
   1.148 +    bool GetHIDDeviceDesc(const String& path, HIDDeviceDesc* pdevDesc) const;
   1.149 +
   1.150 +    GUID GetHIDGuid() { return HidGuid; }
   1.151 +    
   1.152 +    static HIDDeviceManager* CreateInternal(DeviceManager* manager);
   1.153 +
   1.154 +private:
   1.155 +
   1.156 +    DeviceManager* Manager;     // Back pointer can just be a raw pointer.
   1.157 +
   1.158 +    HMODULE hHidLib;
   1.159 +    GUID    HidGuid;
   1.160 +
   1.161 +    // Macros to declare and resolve needed functions from library.
   1.162 +#define OVR_DECLARE_HIDFUNC(func, rettype, args)   \
   1.163 +typedef rettype (__stdcall *PFn_##func) args;  \
   1.164 +PFn_##func      func;
   1.165 +#define OVR_RESOLVE_HIDFUNC(func) \
   1.166 +func = (PFn_##func)::GetProcAddress(hHidLib, #func)
   1.167 +
   1.168 +    OVR_DECLARE_HIDFUNC(HidD_GetHidGuid,            void,    (GUID *hidGuid));
   1.169 +    OVR_DECLARE_HIDFUNC(HidD_SetNumInputBuffers,    BOOLEAN, (HANDLE hidDev, ULONG numberBuffers));
   1.170 +    OVR_DECLARE_HIDFUNC(HidD_GetFeature,            BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
   1.171 +    OVR_DECLARE_HIDFUNC(HidD_SetFeature,            BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
   1.172 +    OVR_DECLARE_HIDFUNC(HidD_GetAttributes,         BOOLEAN, (HANDLE hidDev, HIDD_ATTRIBUTES *attributes));
   1.173 +    OVR_DECLARE_HIDFUNC(HidD_GetManufacturerString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
   1.174 +    OVR_DECLARE_HIDFUNC(HidD_GetProductString,      BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
   1.175 +    OVR_DECLARE_HIDFUNC(HidD_GetSerialNumberString, BOOLEAN, (HANDLE hidDev, PVOID buffer, ULONG bufferLength));
   1.176 +    OVR_DECLARE_HIDFUNC(HidD_GetPreparsedData,      BOOLEAN, (HANDLE hidDev, HIDP_PREPARSED_DATA **preparsedData));
   1.177 +    OVR_DECLARE_HIDFUNC(HidD_FreePreparsedData,     BOOLEAN, (HIDP_PREPARSED_DATA *preparsedData));
   1.178 +    OVR_DECLARE_HIDFUNC(HidP_GetCaps,               NTSTATUS,(HIDP_PREPARSED_DATA *preparsedData, HIDP_CAPS* caps));
   1.179 +
   1.180 +    HANDLE CreateHIDFile(const char* path, bool exclusiveAccess = true) const
   1.181 +    {
   1.182 +        return ::CreateFileA(path, GENERIC_WRITE|GENERIC_READ,
   1.183 +            (!exclusiveAccess) ? (FILE_SHARE_READ|FILE_SHARE_WRITE) : 0x0, 
   1.184 +            NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
   1.185 +    }
   1.186 +
   1.187 +    // Helper functions to fill in HIDDeviceDesc from open device handle.
   1.188 +    bool initVendorProductVersion(HANDLE hidDev, HIDDeviceDesc* desc) const;
   1.189 +    bool initUsage(HANDLE hidDev, HIDDeviceDesc* desc) const;
   1.190 +    void initStrings(HANDLE hidDev, HIDDeviceDesc* desc) const;
   1.191 +
   1.192 +    bool getFullDesc(HANDLE hidDev, HIDDeviceDesc* desc) const;
   1.193 +};
   1.194 +
   1.195 +}} // namespace OVR::Win32
   1.196 +
   1.197 +#endif // OVR_Win32_HIDDevice_h