Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef OVR_HIDDeviceImpl_h
00017 #define OVR_HIDDeviceImpl_h
00018
00019
00020 #include "OVR_DeviceImpl.h"
00021
00022 namespace OVR {
00023
00024
00025 class HIDDeviceCreateDesc : public DeviceCreateDesc
00026 {
00027 public:
00028 HIDDeviceCreateDesc(DeviceFactory* factory, DeviceType type, const HIDDeviceDesc& hidDesc)
00029 : DeviceCreateDesc(factory, type), HIDDesc(hidDesc) { }
00030 HIDDeviceCreateDesc(const HIDDeviceCreateDesc& other)
00031 : DeviceCreateDesc(other.pFactory, other.Type), HIDDesc(other.HIDDesc) { }
00032
00033 virtual bool MatchDevice(const String& path)
00034 {
00035
00036 return HIDDesc.Path.CompareNoCase(path) == 0;
00037 }
00038
00039 HIDDeviceDesc HIDDesc;
00040 };
00041
00042
00043 template<class B>
00044 class HIDDeviceImpl : public DeviceImpl<B>, public HIDDevice::HIDHandler
00045 {
00046 public:
00047 HIDDeviceImpl(HIDDeviceCreateDesc* createDesc, DeviceBase* parent)
00048 : DeviceImpl<B>(createDesc, parent)
00049 {
00050 }
00051
00052
00053 virtual void OnDeviceMessage(HIDDeviceMessageType messageType)
00054 {
00055 MessageType handlerMessageType;
00056 switch (messageType) {
00057 case HIDDeviceMessage_DeviceAdded:
00058 handlerMessageType = Message_DeviceAdded;
00059 break;
00060
00061 case HIDDeviceMessage_DeviceRemoved:
00062 handlerMessageType = Message_DeviceRemoved;
00063 break;
00064
00065 default: OVR_ASSERT(0); return;
00066 }
00067
00068
00069 {
00070 Lock::Locker scopeLock(this->HandlerRef.GetLock());
00071
00072 if (this->HandlerRef.GetHandler())
00073 {
00074 MessageDeviceStatus status(handlerMessageType, this, OVR::DeviceHandle(this->pCreateDesc));
00075 this->HandlerRef.GetHandler()->OnMessage(status);
00076 }
00077 }
00078
00079
00080 DeviceManagerImpl* manager = this->GetManagerImpl();
00081 switch (handlerMessageType) {
00082 case Message_DeviceAdded:
00083 manager->CallOnDeviceAdded(this->pCreateDesc);
00084 break;
00085
00086 case Message_DeviceRemoved:
00087 manager->CallOnDeviceRemoved(this->pCreateDesc);
00088 break;
00089
00090 default:;
00091 }
00092 }
00093
00094 virtual bool Initialize(DeviceBase* parent)
00095 {
00096
00097 HIDDeviceDesc& hidDesc = *getHIDDesc();
00098 HIDDeviceManager* pManager = GetHIDDeviceManager();
00099
00100
00101 HIDDevice* device = pManager->Open(hidDesc.Path);
00102 if (!device)
00103 {
00104 return false;
00105 }
00106
00107 InternalDevice = *device;
00108 InternalDevice->SetHandler(this);
00109
00110
00111 DeviceImpl<B>::pParent = parent;
00112
00113 return true;
00114 }
00115
00116 virtual void Shutdown()
00117 {
00118 InternalDevice->SetHandler(NULL);
00119
00120
00121 this->HandlerRef.SetHandler(0);
00122
00123 DeviceImpl<B>::pParent.Clear();
00124 }
00125
00126 DeviceManager* GetDeviceManager()
00127 {
00128 return DeviceImpl<B>::pCreateDesc->GetManagerImpl();
00129 }
00130
00131 HIDDeviceManager* GetHIDDeviceManager()
00132 {
00133 return DeviceImpl<B>::pCreateDesc->GetManagerImpl()->GetHIDDeviceManager();
00134 }
00135
00136
00137 struct WriteData
00138 {
00139 enum { BufferSize = 64 };
00140 UByte Buffer[64];
00141 UPInt Size;
00142
00143 WriteData(UByte* data, UPInt size) : Size(size)
00144 {
00145 OVR_ASSERT(size <= BufferSize);
00146 memcpy(Buffer, data, size);
00147 }
00148 };
00149
00150 bool SetFeatureReport(UByte* data, UInt32 length)
00151 {
00152 WriteData writeData(data, length);
00153
00154
00155 bool result = false;
00156
00157 ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
00158 if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::setFeatureReport, &result, writeData))
00159 return false;
00160
00161 return result;
00162 }
00163
00164 bool setFeatureReport(const WriteData& data)
00165 {
00166 return InternalDevice->SetFeatureReport((UByte*) data.Buffer, (UInt32) data.Size);
00167 }
00168
00169 bool GetFeatureReport(UByte* data, UInt32 length)
00170 {
00171 bool result = false;
00172
00173 ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
00174 if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::getFeatureReport, &result, data, length))
00175 return false;
00176
00177 return result;
00178 }
00179
00180 bool getFeatureReport(UByte* data, UInt32 length)
00181 {
00182 return InternalDevice->GetFeatureReport(data, length);
00183 }
00184
00185 protected:
00186 HIDDevice* GetInternalDevice() const
00187 {
00188 return InternalDevice;
00189 }
00190
00191 HIDDeviceDesc* getHIDDesc() const
00192 { return &getCreateDesc()->HIDDesc; }
00193
00194 HIDDeviceCreateDesc* getCreateDesc() const
00195 { return (HIDDeviceCreateDesc*) &(*DeviceImpl<B>::pCreateDesc); }
00196
00197 private:
00198 Ptr<HIDDevice> InternalDevice;
00199 };
00200
00201 }
00202
00203 #endif