oculus1

annotate libovr/Src/OVR_HIDDeviceImpl.h @ 17:cfe4979ab3eb

ops, minor error in the last commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Sep 2013 07:09:48 +0300
parents
children
rev   line source
nuclear@1 1 /************************************************************************************
nuclear@1 2
nuclear@1 3 Filename : OVR_HIDDeviceImpl.h
nuclear@1 4 Content : Implementation of HIDDevice.
nuclear@1 5 Created : March 7, 2013
nuclear@1 6 Authors : Lee Cooper
nuclear@1 7
nuclear@1 8 Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
nuclear@1 9
nuclear@1 10 Use of this software is subject to the terms of the Oculus license
nuclear@1 11 agreement provided at the time of installation or download, or which
nuclear@1 12 otherwise accompanies this software in either electronic or hard copy form.
nuclear@1 13
nuclear@1 14 *************************************************************************************/
nuclear@1 15
nuclear@1 16 #ifndef OVR_HIDDeviceImpl_h
nuclear@1 17 #define OVR_HIDDeviceImpl_h
nuclear@1 18
nuclear@1 19 //#include "OVR_Device.h"
nuclear@1 20 #include "OVR_DeviceImpl.h"
nuclear@1 21
nuclear@1 22 namespace OVR {
nuclear@1 23
nuclear@1 24 //-------------------------------------------------------------------------------------
nuclear@1 25 class HIDDeviceCreateDesc : public DeviceCreateDesc
nuclear@1 26 {
nuclear@1 27 public:
nuclear@1 28 HIDDeviceCreateDesc(DeviceFactory* factory, DeviceType type, const HIDDeviceDesc& hidDesc)
nuclear@1 29 : DeviceCreateDesc(factory, type), HIDDesc(hidDesc) { }
nuclear@1 30 HIDDeviceCreateDesc(const HIDDeviceCreateDesc& other)
nuclear@1 31 : DeviceCreateDesc(other.pFactory, other.Type), HIDDesc(other.HIDDesc) { }
nuclear@1 32
nuclear@1 33 virtual bool MatchDevice(const String& path)
nuclear@1 34 {
nuclear@1 35 // should it be case insensitive?
nuclear@1 36 return HIDDesc.Path.CompareNoCase(path) == 0;
nuclear@1 37 }
nuclear@1 38
nuclear@1 39 HIDDeviceDesc HIDDesc;
nuclear@1 40 };
nuclear@1 41
nuclear@1 42 //-------------------------------------------------------------------------------------
nuclear@1 43 template<class B>
nuclear@1 44 class HIDDeviceImpl : public DeviceImpl<B>, public HIDDevice::HIDHandler
nuclear@1 45 {
nuclear@1 46 public:
nuclear@1 47 HIDDeviceImpl(HIDDeviceCreateDesc* createDesc, DeviceBase* parent)
nuclear@1 48 : DeviceImpl<B>(createDesc, parent)
nuclear@1 49 {
nuclear@1 50 }
nuclear@1 51
nuclear@1 52 // HIDDevice::Handler interface.
nuclear@1 53 virtual void OnDeviceMessage(HIDDeviceMessageType messageType)
nuclear@1 54 {
nuclear@1 55 MessageType handlerMessageType;
nuclear@1 56 switch (messageType) {
nuclear@1 57 case HIDDeviceMessage_DeviceAdded:
nuclear@1 58 handlerMessageType = Message_DeviceAdded;
nuclear@1 59 break;
nuclear@1 60
nuclear@1 61 case HIDDeviceMessage_DeviceRemoved:
nuclear@1 62 handlerMessageType = Message_DeviceRemoved;
nuclear@1 63 break;
nuclear@1 64
nuclear@1 65 default: OVR_ASSERT(0); return;
nuclear@1 66 }
nuclear@1 67
nuclear@1 68 // Do device notification.
nuclear@1 69 {
nuclear@1 70 Lock::Locker scopeLock(this->HandlerRef.GetLock());
nuclear@1 71
nuclear@1 72 if (this->HandlerRef.GetHandler())
nuclear@1 73 {
nuclear@1 74 MessageDeviceStatus status(handlerMessageType, this, OVR::DeviceHandle(this->pCreateDesc));
nuclear@1 75 this->HandlerRef.GetHandler()->OnMessage(status);
nuclear@1 76 }
nuclear@1 77 }
nuclear@1 78
nuclear@1 79 // Do device manager notification.
nuclear@1 80 DeviceManagerImpl* manager = this->GetManagerImpl();
nuclear@1 81 switch (handlerMessageType) {
nuclear@1 82 case Message_DeviceAdded:
nuclear@1 83 manager->CallOnDeviceAdded(this->pCreateDesc);
nuclear@1 84 break;
nuclear@1 85
nuclear@1 86 case Message_DeviceRemoved:
nuclear@1 87 manager->CallOnDeviceRemoved(this->pCreateDesc);
nuclear@1 88 break;
nuclear@1 89
nuclear@1 90 default:;
nuclear@1 91 }
nuclear@1 92 }
nuclear@1 93
nuclear@1 94 virtual bool Initialize(DeviceBase* parent)
nuclear@1 95 {
nuclear@1 96 // Open HID device.
nuclear@1 97 HIDDeviceDesc& hidDesc = *getHIDDesc();
nuclear@1 98 HIDDeviceManager* pManager = GetHIDDeviceManager();
nuclear@1 99
nuclear@1 100
nuclear@1 101 HIDDevice* device = pManager->Open(hidDesc.Path);
nuclear@1 102 if (!device)
nuclear@1 103 {
nuclear@1 104 return false;
nuclear@1 105 }
nuclear@1 106
nuclear@1 107 InternalDevice = *device;
nuclear@1 108 InternalDevice->SetHandler(this);
nuclear@1 109
nuclear@1 110 // AddRef() to parent, forcing chain to stay alive.
nuclear@1 111 DeviceImpl<B>::pParent = parent;
nuclear@1 112
nuclear@1 113 return true;
nuclear@1 114 }
nuclear@1 115
nuclear@1 116 virtual void Shutdown()
nuclear@1 117 {
nuclear@1 118 InternalDevice->SetHandler(NULL);
nuclear@1 119
nuclear@1 120 // Remove the handler, if any.
nuclear@1 121 this->HandlerRef.SetHandler(0);
nuclear@1 122
nuclear@1 123 DeviceImpl<B>::pParent.Clear();
nuclear@1 124 }
nuclear@1 125
nuclear@1 126 DeviceManager* GetDeviceManager()
nuclear@1 127 {
nuclear@1 128 return DeviceImpl<B>::pCreateDesc->GetManagerImpl();
nuclear@1 129 }
nuclear@1 130
nuclear@1 131 HIDDeviceManager* GetHIDDeviceManager()
nuclear@1 132 {
nuclear@1 133 return DeviceImpl<B>::pCreateDesc->GetManagerImpl()->GetHIDDeviceManager();
nuclear@1 134 }
nuclear@1 135
nuclear@1 136
nuclear@1 137 struct WriteData
nuclear@1 138 {
nuclear@1 139 enum { BufferSize = 64 };
nuclear@1 140 UByte Buffer[64];
nuclear@1 141 UPInt Size;
nuclear@1 142
nuclear@1 143 WriteData(UByte* data, UPInt size) : Size(size)
nuclear@1 144 {
nuclear@1 145 OVR_ASSERT(size <= BufferSize);
nuclear@1 146 memcpy(Buffer, data, size);
nuclear@1 147 }
nuclear@1 148 };
nuclear@1 149
nuclear@1 150 bool SetFeatureReport(UByte* data, UInt32 length)
nuclear@1 151 {
nuclear@1 152 WriteData writeData(data, length);
nuclear@1 153
nuclear@1 154 // Push call with wait.
nuclear@1 155 bool result = false;
nuclear@1 156
nuclear@1 157 ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
nuclear@1 158 if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::setFeatureReport, &result, writeData))
nuclear@1 159 return false;
nuclear@1 160
nuclear@1 161 return result;
nuclear@1 162 }
nuclear@1 163
nuclear@1 164 bool setFeatureReport(const WriteData& data)
nuclear@1 165 {
nuclear@1 166 return InternalDevice->SetFeatureReport((UByte*) data.Buffer, (UInt32) data.Size);
nuclear@1 167 }
nuclear@1 168
nuclear@1 169 bool GetFeatureReport(UByte* data, UInt32 length)
nuclear@1 170 {
nuclear@1 171 bool result = false;
nuclear@1 172
nuclear@1 173 ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
nuclear@1 174 if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::getFeatureReport, &result, data, length))
nuclear@1 175 return false;
nuclear@1 176
nuclear@1 177 return result;
nuclear@1 178 }
nuclear@1 179
nuclear@1 180 bool getFeatureReport(UByte* data, UInt32 length)
nuclear@1 181 {
nuclear@1 182 return InternalDevice->GetFeatureReport(data, length);
nuclear@1 183 }
nuclear@1 184
nuclear@1 185 protected:
nuclear@1 186 HIDDevice* GetInternalDevice() const
nuclear@1 187 {
nuclear@1 188 return InternalDevice;
nuclear@1 189 }
nuclear@1 190
nuclear@1 191 HIDDeviceDesc* getHIDDesc() const
nuclear@1 192 { return &getCreateDesc()->HIDDesc; }
nuclear@1 193
nuclear@1 194 HIDDeviceCreateDesc* getCreateDesc() const
nuclear@1 195 { return (HIDDeviceCreateDesc*) &(*DeviceImpl<B>::pCreateDesc); }
nuclear@1 196
nuclear@1 197 private:
nuclear@1 198 Ptr<HIDDevice> InternalDevice;
nuclear@1 199 };
nuclear@1 200
nuclear@1 201 } // namespace OVR
nuclear@1 202
nuclear@1 203 #endif