oculus1

view 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
line source
1 /************************************************************************************
3 Filename : OVR_HIDDeviceImpl.h
4 Content : Implementation of HIDDevice.
5 Created : March 7, 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_HIDDeviceImpl_h
17 #define OVR_HIDDeviceImpl_h
19 //#include "OVR_Device.h"
20 #include "OVR_DeviceImpl.h"
22 namespace OVR {
24 //-------------------------------------------------------------------------------------
25 class HIDDeviceCreateDesc : public DeviceCreateDesc
26 {
27 public:
28 HIDDeviceCreateDesc(DeviceFactory* factory, DeviceType type, const HIDDeviceDesc& hidDesc)
29 : DeviceCreateDesc(factory, type), HIDDesc(hidDesc) { }
30 HIDDeviceCreateDesc(const HIDDeviceCreateDesc& other)
31 : DeviceCreateDesc(other.pFactory, other.Type), HIDDesc(other.HIDDesc) { }
33 virtual bool MatchDevice(const String& path)
34 {
35 // should it be case insensitive?
36 return HIDDesc.Path.CompareNoCase(path) == 0;
37 }
39 HIDDeviceDesc HIDDesc;
40 };
42 //-------------------------------------------------------------------------------------
43 template<class B>
44 class HIDDeviceImpl : public DeviceImpl<B>, public HIDDevice::HIDHandler
45 {
46 public:
47 HIDDeviceImpl(HIDDeviceCreateDesc* createDesc, DeviceBase* parent)
48 : DeviceImpl<B>(createDesc, parent)
49 {
50 }
52 // HIDDevice::Handler interface.
53 virtual void OnDeviceMessage(HIDDeviceMessageType messageType)
54 {
55 MessageType handlerMessageType;
56 switch (messageType) {
57 case HIDDeviceMessage_DeviceAdded:
58 handlerMessageType = Message_DeviceAdded;
59 break;
61 case HIDDeviceMessage_DeviceRemoved:
62 handlerMessageType = Message_DeviceRemoved;
63 break;
65 default: OVR_ASSERT(0); return;
66 }
68 // Do device notification.
69 {
70 Lock::Locker scopeLock(this->HandlerRef.GetLock());
72 if (this->HandlerRef.GetHandler())
73 {
74 MessageDeviceStatus status(handlerMessageType, this, OVR::DeviceHandle(this->pCreateDesc));
75 this->HandlerRef.GetHandler()->OnMessage(status);
76 }
77 }
79 // Do device manager notification.
80 DeviceManagerImpl* manager = this->GetManagerImpl();
81 switch (handlerMessageType) {
82 case Message_DeviceAdded:
83 manager->CallOnDeviceAdded(this->pCreateDesc);
84 break;
86 case Message_DeviceRemoved:
87 manager->CallOnDeviceRemoved(this->pCreateDesc);
88 break;
90 default:;
91 }
92 }
94 virtual bool Initialize(DeviceBase* parent)
95 {
96 // Open HID device.
97 HIDDeviceDesc& hidDesc = *getHIDDesc();
98 HIDDeviceManager* pManager = GetHIDDeviceManager();
101 HIDDevice* device = pManager->Open(hidDesc.Path);
102 if (!device)
103 {
104 return false;
105 }
107 InternalDevice = *device;
108 InternalDevice->SetHandler(this);
110 // AddRef() to parent, forcing chain to stay alive.
111 DeviceImpl<B>::pParent = parent;
113 return true;
114 }
116 virtual void Shutdown()
117 {
118 InternalDevice->SetHandler(NULL);
120 // Remove the handler, if any.
121 this->HandlerRef.SetHandler(0);
123 DeviceImpl<B>::pParent.Clear();
124 }
126 DeviceManager* GetDeviceManager()
127 {
128 return DeviceImpl<B>::pCreateDesc->GetManagerImpl();
129 }
131 HIDDeviceManager* GetHIDDeviceManager()
132 {
133 return DeviceImpl<B>::pCreateDesc->GetManagerImpl()->GetHIDDeviceManager();
134 }
137 struct WriteData
138 {
139 enum { BufferSize = 64 };
140 UByte Buffer[64];
141 UPInt Size;
143 WriteData(UByte* data, UPInt size) : Size(size)
144 {
145 OVR_ASSERT(size <= BufferSize);
146 memcpy(Buffer, data, size);
147 }
148 };
150 bool SetFeatureReport(UByte* data, UInt32 length)
151 {
152 WriteData writeData(data, length);
154 // Push call with wait.
155 bool result = false;
157 ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
158 if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::setFeatureReport, &result, writeData))
159 return false;
161 return result;
162 }
164 bool setFeatureReport(const WriteData& data)
165 {
166 return InternalDevice->SetFeatureReport((UByte*) data.Buffer, (UInt32) data.Size);
167 }
169 bool GetFeatureReport(UByte* data, UInt32 length)
170 {
171 bool result = false;
173 ThreadCommandQueue* pQueue = this->GetManagerImpl()->GetThreadQueue();
174 if (!pQueue->PushCallAndWaitResult(this, &HIDDeviceImpl::getFeatureReport, &result, data, length))
175 return false;
177 return result;
178 }
180 bool getFeatureReport(UByte* data, UInt32 length)
181 {
182 return InternalDevice->GetFeatureReport(data, length);
183 }
185 protected:
186 HIDDevice* GetInternalDevice() const
187 {
188 return InternalDevice;
189 }
191 HIDDeviceDesc* getHIDDesc() const
192 { return &getCreateDesc()->HIDDesc; }
194 HIDDeviceCreateDesc* getCreateDesc() const
195 { return (HIDDeviceCreateDesc*) &(*DeviceImpl<B>::pCreateDesc); }
197 private:
198 Ptr<HIDDevice> InternalDevice;
199 };
201 } // namespace OVR
203 #endif