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_DeviceImpl_h
00017 #define OVR_DeviceImpl_h
00018
00019 #include "OVR_Device.h"
00020 #include "Kernel/OVR_Atomic.h"
00021 #include "Kernel/OVR_Log.h"
00022 #include "Kernel/OVR_System.h"
00023
00024 #include "Kernel/OVR_Threads.h"
00025 #include "OVR_ThreadCommandQueue.h"
00026 #include "OVR_HIDDevice.h"
00027
00028 namespace OVR {
00029
00030 class DeviceManagerImpl;
00031 class DeviceFactory;
00032
00033 enum
00034 {
00035 Oculus_VendorId = 0x2833
00036 };
00037
00038
00039
00040
00041 class SharedLock
00042 {
00043 public:
00044 SharedLock() : UseCount(0) {}
00045
00046 Lock* GetLockAddRef();
00047 void ReleaseLock(Lock* plock);
00048
00049 private:
00050 Lock* toLock() { return (Lock*)Buffer; }
00051
00052
00053 volatile int UseCount;
00054 UInt64 Buffer[(sizeof(Lock)+sizeof(UInt64)-1)/sizeof(UInt64)];
00055 };
00056
00057
00058
00059
00060
00061 class MessageHandlerRef : public ListNode<MessageHandlerRef>
00062 {
00063 public:
00064 MessageHandlerRef(DeviceBase* device);
00065 ~MessageHandlerRef();
00066
00067 void SetHandler(MessageHandler* hander);
00068
00069
00070 void SetHandler_NTS(MessageHandler* hander);
00071
00072 void Call(const Message& msg)
00073 {
00074 Lock::Locker lockScope(pLock);
00075 if (pHandler)
00076 pHandler->OnMessage(msg);
00077 }
00078
00079 Lock* GetLock() const { return pLock; }
00080
00081
00082
00083 MessageHandler* GetHandler() const { return pHandler; }
00084 DeviceBase* GetDevice() const { return pDevice; }
00085
00086 private:
00087 Lock* pLock;
00088 DeviceBase* pDevice;
00089 MessageHandler* pHandler;
00090 };
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104 class DeviceManagerLock : public RefCountBase<DeviceManagerLock>
00105 {
00106 public:
00107 Lock CreateLock;
00108 DeviceManagerImpl* pManager;
00109
00110 DeviceManagerLock() : pManager(0) { }
00111 };
00112
00113
00114
00115
00116
00117
00118
00119 class DeviceCreateDesc : public ListNode<DeviceCreateDesc>, public NewOverrideBase
00120 {
00121 void operator = (const DeviceCreateDesc&) { }
00122 public:
00123 DeviceCreateDesc(DeviceFactory* factory, DeviceType type)
00124 : pFactory(factory), Type(type), pLock(0), HandleCount(0), pDevice(0), Enumerated(true)
00125 {
00126 pNext = pPrev = 0;
00127 }
00128
00129 virtual ~DeviceCreateDesc()
00130 {
00131 OVR_ASSERT(!pDevice);
00132 if (pNext)
00133 RemoveNode();
00134 }
00135
00136 DeviceManagerImpl* GetManagerImpl() const { return pLock->pManager; }
00137 Lock* GetLock() const { return &pLock->CreateLock; }
00138
00139
00140
00141 void AddRef();
00142 void Release();
00143
00144
00145
00146
00147
00148
00149 virtual DeviceCreateDesc* Clone() const = 0;
00150
00151
00152 virtual DeviceBase* NewDeviceInstance() = 0;
00153
00154 virtual bool GetDeviceInfo(DeviceInfo* info) const = 0;
00155
00156
00157 enum MatchResult
00158 {
00159 Match_None,
00160 Match_Found,
00161 Match_Candidate
00162 };
00163
00164
00165
00166
00167 virtual MatchResult MatchDevice(const DeviceCreateDesc& other,
00168 DeviceCreateDesc** pcandidate) const = 0;
00169
00170
00171
00172
00173
00174
00175 virtual bool UpdateMatchedCandidate(
00176 const DeviceCreateDesc& desc, bool* newDeviceFlag = NULL)
00177 { OVR_UNUSED2(desc, newDeviceFlag); return false; }
00178
00179
00180 virtual bool MatchHIDDevice(const HIDDeviceDesc&) const { return false; }
00181
00182
00183 virtual bool MatchDevice(const String& ) { return false; }
00184
00185 DeviceFactory* const pFactory;
00186 const DeviceType Type;
00187
00188
00189 Ptr<DeviceManagerLock> pLock;
00190
00191
00192
00193
00194
00195
00196 AtomicInt<UInt32> HandleCount;
00197
00198 DeviceBase* pDevice;
00199
00200 bool Enumerated;
00201 };
00202
00203
00204
00205
00206
00207 class DeviceCommon
00208 {
00209 public:
00210 AtomicInt<UInt32> RefCount;
00211 Ptr<DeviceCreateDesc> pCreateDesc;
00212 Ptr<DeviceBase> pParent;
00213 MessageHandlerRef HandlerRef;
00214
00215 DeviceCommon(DeviceCreateDesc* createDesc, DeviceBase* device, DeviceBase* parent)
00216 : RefCount(1), pCreateDesc(createDesc), pParent(parent), HandlerRef(device)
00217 {
00218 }
00219
00220
00221 void DeviceAddRef();
00222 void DeviceRelease();
00223
00224 Lock* GetLock() const { return pCreateDesc->GetLock(); }
00225
00226 virtual bool Initialize(DeviceBase* parent) = 0;
00227 virtual void Shutdown() = 0;
00228 };
00229
00230
00231
00232
00233
00234
00235 template<class B>
00236 class DeviceImpl : public B, public DeviceCommon
00237 {
00238 public:
00239 DeviceImpl(DeviceCreateDesc* createDesc, DeviceBase* parent)
00240 : DeviceCommon(createDesc, getThis(), parent)
00241 {
00242 }
00243
00244
00245 DeviceManagerImpl* GetManagerImpl() const { return pCreateDesc->pLock->pManager; }
00246
00247
00248 DeviceImpl* getThis() { return this; }
00249
00250
00251 virtual DeviceCommon* getDeviceCommon() const { return (DeviceCommon*)this; }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263 };
00264
00265
00266
00267
00268
00269
00270
00271
00272 class DeviceFactory : public ListNode<DeviceFactory>, public NewOverrideBase
00273 {
00274 public:
00275
00276 DeviceFactory() : pManager(0)
00277 {
00278 pNext = pPrev = 0;
00279 }
00280 virtual ~DeviceFactory() { }
00281
00282 DeviceManagerImpl* GetManagerImpl() { return pManager; }
00283
00284
00285 virtual bool AddedToManager(DeviceManagerImpl* manager)
00286 {
00287 OVR_ASSERT(pManager == 0);
00288 pManager = manager;
00289 return true;
00290 }
00291
00292 virtual void RemovedFromManager()
00293 {
00294 pManager = 0;
00295 }
00296
00297
00298
00299
00300
00301 class EnumerateVisitor
00302 {
00303 public:
00304 virtual void Visit(const DeviceCreateDesc& createDesc) = 0;
00305 };
00306
00307
00308
00309 virtual void EnumerateDevices(EnumerateVisitor& visitor) = 0;
00310
00311
00312
00313 virtual bool MatchVendorProduct(UInt16 vendorId, UInt16 productId) const
00314 {
00315 OVR_UNUSED2(vendorId, productId);
00316 return false;
00317 }
00318
00319
00320
00321 virtual bool DetectHIDDevice(DeviceManager* pdevMgr, const HIDDeviceDesc& desc)
00322 {
00323 OVR_UNUSED2(pdevMgr, desc);
00324 return false;
00325 }
00326
00327 protected:
00328 DeviceManagerImpl* pManager;
00329 };
00330
00331
00332
00333
00334
00335
00336
00337
00338 class DeviceManagerImpl : public DeviceImpl<OVR::DeviceManager>, public ThreadCommandQueue
00339 {
00340 public:
00341 DeviceManagerImpl();
00342 ~DeviceManagerImpl();
00343
00344
00345 static DeviceCreateDesc* CreateManagerDesc();
00346
00347
00348
00349 virtual bool Initialize(DeviceBase* parent);
00350 virtual void Shutdown();
00351
00352
00353
00354
00355 virtual ProfileManager* GetProfileManager() const { return pProfileManager.GetPtr(); }
00356
00357
00358
00359 virtual ThreadCommandQueue* GetThreadQueue() = 0;
00360
00361
00362 virtual ThreadId GetThreadId() const = 0;
00363
00364 virtual DeviceEnumerator<> EnumerateDevicesEx(const DeviceEnumerationArgs& args);
00365
00366
00367
00368 void AddFactory(DeviceFactory* factory)
00369 {
00370
00371 Lock::Locker scopeLock(GetLock());
00372 Factories.PushBack(factory);
00373 factory->AddedToManager(this);
00374 }
00375
00376 void CallOnDeviceAdded(DeviceCreateDesc* desc)
00377 {
00378 HandlerRef.Call(MessageDeviceStatus(Message_DeviceAdded, this, DeviceHandle(desc)));
00379 }
00380 void CallOnDeviceRemoved(DeviceCreateDesc* desc)
00381 {
00382 HandlerRef.Call(MessageDeviceStatus(Message_DeviceRemoved, this, DeviceHandle(desc)));
00383 }
00384
00385
00386 static DeviceCommon* GetDeviceCommon(DeviceBase* device)
00387 {
00388 return device->getDeviceCommon();
00389 }
00390
00391
00392
00393 DeviceBase* CreateDevice_MgrThread(DeviceCreateDesc* createDesc, DeviceBase* parent = 0);
00394 Void ReleaseDevice_MgrThread(DeviceBase* device);
00395
00396
00397
00398 virtual Void EnumerateAllFactoryDevices();
00399
00400 virtual Void EnumerateFactoryDevices(DeviceFactory* factory);
00401
00402 virtual HIDDeviceManager* GetHIDDeviceManager() const
00403 {
00404 return HidDeviceManager;
00405 }
00406
00407
00408
00409 virtual Ptr<DeviceCreateDesc> AddDevice_NeedsLock(const DeviceCreateDesc& createDesc);
00410
00411
00412 Ptr<DeviceCreateDesc> FindDevice(const String& path, DeviceType = Device_None);
00413
00414
00415 Ptr<DeviceCreateDesc> FindHIDDevice(const HIDDeviceDesc&);
00416 void DetectHIDDevice(const HIDDeviceDesc&);
00417
00418
00419 List<DeviceCreateDesc> Devices;
00420
00421
00422 List<DeviceFactory> Factories;
00423
00424 protected:
00425 Ptr<HIDDeviceManager> HidDeviceManager;
00426 Ptr<ProfileManager> pProfileManager;
00427 };
00428
00429
00430 }
00431
00432 #endif