oculus1

diff libovr/Src/OVR_HIDDeviceImpl.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/OVR_HIDDeviceImpl.h	Sat Sep 14 16:14:59 2013 +0300
     1.3 @@ -0,0 +1,203 @@
     1.4 +/************************************************************************************
     1.5 +
     1.6 +Filename    :   OVR_HIDDeviceImpl.h
     1.7 +Content     :   Implementation of HIDDevice.
     1.8 +Created     :   March 7, 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_HIDDeviceImpl_h
    1.20 +#define OVR_HIDDeviceImpl_h
    1.21 +
    1.22 +//#include "OVR_Device.h"
    1.23 +#include "OVR_DeviceImpl.h"
    1.24 +
    1.25 +namespace OVR {
    1.26 +
    1.27 +//-------------------------------------------------------------------------------------
    1.28 +class HIDDeviceCreateDesc : public DeviceCreateDesc
    1.29 +{
    1.30 +public:
    1.31 +    HIDDeviceCreateDesc(DeviceFactory* factory, DeviceType type, const HIDDeviceDesc& hidDesc)
    1.32 +        : DeviceCreateDesc(factory, type), HIDDesc(hidDesc) { }
    1.33 +    HIDDeviceCreateDesc(const HIDDeviceCreateDesc& other)
    1.34 +        : DeviceCreateDesc(other.pFactory, other.Type), HIDDesc(other.HIDDesc) { }
    1.35 +
    1.36 +    virtual bool MatchDevice(const String& path)
    1.37 +    {
    1.38 +        // should it be case insensitive?
    1.39 +        return HIDDesc.Path.CompareNoCase(path) == 0;
    1.40 +    }
    1.41 +
    1.42 +    HIDDeviceDesc HIDDesc;
    1.43 +};
    1.44 +
    1.45 +//-------------------------------------------------------------------------------------
    1.46 +template<class B>
    1.47 +class HIDDeviceImpl : public DeviceImpl<B>, public HIDDevice::HIDHandler
    1.48 +{
    1.49 +public:
    1.50 +    HIDDeviceImpl(HIDDeviceCreateDesc* createDesc, DeviceBase* parent)
    1.51 +     :  DeviceImpl<B>(createDesc, parent)        
    1.52 +    {
    1.53 +    }
    1.54 +
    1.55 +    // HIDDevice::Handler interface.
    1.56 +    virtual void OnDeviceMessage(HIDDeviceMessageType messageType)
    1.57 +    {
    1.58 +        MessageType handlerMessageType;
    1.59 +        switch (messageType) {
    1.60 +            case HIDDeviceMessage_DeviceAdded:
    1.61 +                handlerMessageType = Message_DeviceAdded;
    1.62 +                break;
    1.63 +
    1.64 +            case HIDDeviceMessage_DeviceRemoved:
    1.65 +                handlerMessageType = Message_DeviceRemoved;
    1.66 +                break;
    1.67 +
    1.68 +            default: OVR_ASSERT(0); return;
    1.69 +        }
    1.70 +
    1.71 +        // Do device notification.
    1.72 +        {
    1.73 +            Lock::Locker scopeLock(this->HandlerRef.GetLock());
    1.74 +
    1.75 +            if (this->HandlerRef.GetHandler())
    1.76 +            {
    1.77 +                MessageDeviceStatus status(handlerMessageType, this, OVR::DeviceHandle(this->pCreateDesc));
    1.78 +                this->HandlerRef.GetHandler()->OnMessage(status);
    1.79 +            }
    1.80 +        }
    1.81 +
    1.82 +        // Do device manager notification.
    1.83 +        DeviceManagerImpl*   manager = this->GetManagerImpl();
    1.84 +        switch (handlerMessageType) {
    1.85 +            case Message_DeviceAdded:
    1.86 +                manager->CallOnDeviceAdded(this->pCreateDesc);
    1.87 +                break;
    1.88 +                
    1.89 +            case Message_DeviceRemoved:
    1.90 +                manager->CallOnDeviceRemoved(this->pCreateDesc);
    1.91 +                break;
    1.92 +                
    1.93 +            default:;
    1.94 +        }
    1.95 +    }
    1.96 +
    1.97 +    virtual bool Initialize(DeviceBase* parent)
    1.98 +    {
    1.99 +        // Open HID device.
   1.100 +        HIDDeviceDesc&		hidDesc = *getHIDDesc();
   1.101 +        HIDDeviceManager*   pManager = GetHIDDeviceManager();
   1.102 +
   1.103 +
   1.104 +        HIDDevice* device = pManager->Open(hidDesc.Path);
   1.105 +        if (!device)
   1.106 +        {
   1.107 +            return false;
   1.108 +        }
   1.109 +
   1.110 +        InternalDevice = *device;
   1.111 +        InternalDevice->SetHandler(this);
   1.112 +
   1.113 +        // AddRef() to parent, forcing chain to stay alive.
   1.114 +        DeviceImpl<B>::pParent = parent;
   1.115 +
   1.116 +        return true;
   1.117 +    }
   1.118 +
   1.119 +    virtual void Shutdown()
   1.120 +    {   
   1.121 +        InternalDevice->SetHandler(NULL);
   1.122 +
   1.123 +        // Remove the handler, if any.
   1.124 +        this->HandlerRef.SetHandler(0);
   1.125 +
   1.126 +        DeviceImpl<B>::pParent.Clear();
   1.127 +    }
   1.128 +
   1.129 +    DeviceManager* GetDeviceManager()
   1.130 +    {
   1.131 +        return DeviceImpl<B>::pCreateDesc->GetManagerImpl();
   1.132 +    }
   1.133 +
   1.134 +    HIDDeviceManager* GetHIDDeviceManager()
   1.135 +    {
   1.136 +        return DeviceImpl<B>::pCreateDesc->GetManagerImpl()->GetHIDDeviceManager();
   1.137 +    }
   1.138 +
   1.139 +
   1.140 +    struct WriteData
   1.141 +    {
   1.142 +        enum { BufferSize = 64 };
   1.143 +        UByte Buffer[64];
   1.144 +        UPInt Size;
   1.145 +
   1.146 +        WriteData(UByte* data, UPInt size) : Size(size)
   1.147 +        {
   1.148 +            OVR_ASSERT(size <= BufferSize);
   1.149 +            memcpy(Buffer, data, size);
   1.150 +        }
   1.151 +    };
   1.152 +
   1.153 +    bool SetFeatureReport(UByte* data, UInt32 length)
   1.154 +    { 
   1.155 +        WriteData writeData(data, length);
   1.156 +
   1.157 +        // Push call with wait.
   1.158 +        bool result = false;
   1.159 +
   1.160 +		ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
   1.161 +        if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::setFeatureReport, &result, writeData))
   1.162 +            return false;
   1.163 +
   1.164 +        return result;
   1.165 +    }
   1.166 +
   1.167 +    bool setFeatureReport(const WriteData& data)
   1.168 +    {
   1.169 +        return InternalDevice->SetFeatureReport((UByte*) data.Buffer, (UInt32) data.Size);
   1.170 +    }
   1.171 +
   1.172 +    bool GetFeatureReport(UByte* data, UInt32 length)
   1.173 +    { 
   1.174 +        bool result = false;
   1.175 +
   1.176 +		ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
   1.177 +        if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::getFeatureReport, &result, data, length))
   1.178 +            return false;
   1.179 +
   1.180 +        return result;
   1.181 +    }
   1.182 +
   1.183 +    bool getFeatureReport(UByte* data, UInt32 length)
   1.184 +    {
   1.185 +        return InternalDevice->GetFeatureReport(data, length);
   1.186 +    }
   1.187 +
   1.188 +protected:
   1.189 +    HIDDevice* GetInternalDevice() const
   1.190 +    {
   1.191 +        return InternalDevice;
   1.192 +    }
   1.193 +
   1.194 +    HIDDeviceDesc* getHIDDesc() const
   1.195 +    { return &getCreateDesc()->HIDDesc; }
   1.196 +
   1.197 +    HIDDeviceCreateDesc* getCreateDesc() const
   1.198 +    { return (HIDDeviceCreateDesc*) &(*DeviceImpl<B>::pCreateDesc); }
   1.199 +
   1.200 +private:
   1.201 +    Ptr<HIDDevice> InternalDevice;
   1.202 +};
   1.203 +
   1.204 +} // namespace OVR
   1.205 +
   1.206 +#endif