Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "OVR_DeviceImpl.h"
00017 #include "Kernel/OVR_Atomic.h"
00018 #include "Kernel/OVR_Log.h"
00019 #include "Kernel/OVR_System.h"
00020
00021 #include "OVR_DeviceImpl.h"
00022 #include "OVR_SensorImpl.h"
00023 #include "OVR_Profile.h"
00024
00025 namespace OVR {
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 enum { LockInitMarker = 0xFFFFFFFF };
00038
00039 Lock* SharedLock::GetLockAddRef()
00040 {
00041 int oldUseCount;
00042
00043 do {
00044 oldUseCount = UseCount;
00045 if (oldUseCount == LockInitMarker)
00046 continue;
00047
00048 if (oldUseCount == 0)
00049 {
00050
00051 if (AtomicOps<int>::CompareAndSet_Sync(&UseCount, 0, LockInitMarker))
00052 {
00053 Construct<Lock>(Buffer);
00054 do { }
00055 while (!AtomicOps<int>::CompareAndSet_Sync(&UseCount, LockInitMarker, 1));
00056 return toLock();
00057 }
00058 continue;
00059 }
00060
00061 } while (!AtomicOps<int>::CompareAndSet_NoSync(&UseCount, oldUseCount, oldUseCount + 1));
00062
00063 return toLock();
00064 }
00065
00066 void SharedLock::ReleaseLock(Lock* plock)
00067 {
00068 OVR_UNUSED(plock);
00069 OVR_ASSERT(plock == toLock());
00070
00071 int oldUseCount;
00072
00073 do {
00074 oldUseCount = UseCount;
00075 OVR_ASSERT(oldUseCount != LockInitMarker);
00076
00077 if (oldUseCount == 1)
00078 {
00079
00080 if (AtomicOps<int>::CompareAndSet_Sync(&UseCount, 1, LockInitMarker))
00081 {
00082 Destruct<Lock>(toLock());
00083
00084 do { }
00085 while (!AtomicOps<int>::CompareAndSet_Sync(&UseCount, LockInitMarker, 0));
00086
00087 return;
00088 }
00089 continue;
00090 }
00091
00092 } while (!AtomicOps<int>::CompareAndSet_NoSync(&UseCount, oldUseCount, oldUseCount - 1));
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 static SharedLock MessageHandlerSharedLock;
00106
00107
00108 class MessageHandlerImpl
00109 {
00110 public:
00111 MessageHandlerImpl()
00112 : pLock(MessageHandlerSharedLock.GetLockAddRef())
00113 {
00114 }
00115 ~MessageHandlerImpl()
00116 {
00117 MessageHandlerSharedLock.ReleaseLock(pLock);
00118 pLock = 0;
00119 }
00120
00121 static MessageHandlerImpl* FromHandler(MessageHandler* handler)
00122 { return (MessageHandlerImpl*)&handler->Internal; }
00123 static const MessageHandlerImpl* FromHandler(const MessageHandler* handler)
00124 { return (const MessageHandlerImpl*)&handler->Internal; }
00125
00126
00127
00128 Lock* pLock;
00129
00130 List<MessageHandlerRef> UseList;
00131 };
00132
00133
00134 MessageHandlerRef::MessageHandlerRef(DeviceBase* device)
00135 : pLock(MessageHandlerSharedLock.GetLockAddRef()), pDevice(device), pHandler(0)
00136 {
00137 }
00138
00139 MessageHandlerRef::~MessageHandlerRef()
00140 {
00141 {
00142 Lock::Locker lockScope(pLock);
00143 if (pHandler)
00144 {
00145 pHandler = 0;
00146 RemoveNode();
00147 }
00148 }
00149 MessageHandlerSharedLock.ReleaseLock(pLock);
00150 pLock = 0;
00151 }
00152
00153 void MessageHandlerRef::SetHandler(MessageHandler* handler)
00154 {
00155 OVR_ASSERT(!handler ||
00156 MessageHandlerImpl::FromHandler(handler)->pLock == pLock);
00157 Lock::Locker lockScope(pLock);
00158 SetHandler_NTS(handler);
00159 }
00160
00161 void MessageHandlerRef::SetHandler_NTS(MessageHandler* handler)
00162 {
00163 if (pHandler != handler)
00164 {
00165 if (pHandler)
00166 RemoveNode();
00167 pHandler = handler;
00168
00169 if (handler)
00170 {
00171 MessageHandlerImpl* handlerImpl = MessageHandlerImpl::FromHandler(handler);
00172 handlerImpl->UseList.PushBack(this);
00173 }
00174
00175 }
00176 }
00177
00178
00179 MessageHandler::MessageHandler()
00180 {
00181 OVR_COMPILER_ASSERT(sizeof(Internal) > sizeof(MessageHandlerImpl));
00182 Construct<MessageHandlerImpl>(Internal);
00183 }
00184
00185 MessageHandler::~MessageHandler()
00186 {
00187 MessageHandlerImpl* handlerImpl = MessageHandlerImpl::FromHandler(this);
00188 {
00189 Lock::Locker lockedScope(handlerImpl->pLock);
00190 OVR_ASSERT_LOG(handlerImpl->UseList.IsEmpty(),
00191 ("~MessageHandler %p - Handler still active; call RemoveHandlerFromDevices", this));
00192 }
00193
00194 Destruct<MessageHandlerImpl>(handlerImpl);
00195 }
00196
00197 bool MessageHandler::IsHandlerInstalled() const
00198 {
00199 const MessageHandlerImpl* handlerImpl = MessageHandlerImpl::FromHandler(this);
00200 Lock::Locker lockedScope(handlerImpl->pLock);
00201 return handlerImpl->UseList.IsEmpty() != true;
00202 }
00203
00204
00205 void MessageHandler::RemoveHandlerFromDevices()
00206 {
00207 MessageHandlerImpl* handlerImpl = MessageHandlerImpl::FromHandler(this);
00208 Lock::Locker lockedScope(handlerImpl->pLock);
00209
00210 while(!handlerImpl->UseList.IsEmpty())
00211 {
00212 MessageHandlerRef* use = handlerImpl->UseList.GetFirst();
00213 use->SetHandler_NTS(0);
00214 }
00215 }
00216
00217 Lock* MessageHandler::GetHandlerLock() const
00218 {
00219 const MessageHandlerImpl* handlerImpl = MessageHandlerImpl::FromHandler(this);
00220 return handlerImpl->pLock;
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230 void DeviceBase::AddRef()
00231 {
00232 getDeviceCommon()->DeviceAddRef();
00233 }
00234 void DeviceBase::Release()
00235 {
00236 getDeviceCommon()->DeviceRelease();
00237 }
00238 DeviceBase* DeviceBase::GetParent() const
00239 {
00240 return getDeviceCommon()->pParent.GetPtr();
00241 }
00242 DeviceManager* DeviceBase::GetManager() const
00243 {
00244 return getDeviceCommon()->pCreateDesc->GetManagerImpl();
00245 }
00246
00247 void DeviceBase::SetMessageHandler(MessageHandler* handler)
00248 {
00249 getDeviceCommon()->HandlerRef.SetHandler(handler);
00250 }
00251 MessageHandler* DeviceBase::GetMessageHandler() const
00252 {
00253 return getDeviceCommon()->HandlerRef.GetHandler();
00254 }
00255
00256 DeviceType DeviceBase::GetType() const
00257 {
00258 return getDeviceCommon()->pCreateDesc->Type;
00259 }
00260
00261 bool DeviceBase::GetDeviceInfo(DeviceInfo* info) const
00262 {
00263 return getDeviceCommon()->pCreateDesc->GetDeviceInfo(info);
00264
00265
00266 }
00267
00268
00269 Lock* DeviceBase::GetHandlerLock() const
00270 {
00271 return getDeviceCommon()->HandlerRef.GetLock();
00272 }
00273
00274
00275 class DeviceManagerCreateDesc : public DeviceCreateDesc
00276 {
00277 public:
00278 DeviceManagerCreateDesc(DeviceFactory* factory)
00279 : DeviceCreateDesc(factory, Device_Manager) { }
00280
00281
00282 virtual DeviceCreateDesc* Clone() const { return 0; }
00283 virtual MatchResult MatchDevice(const DeviceCreateDesc&,
00284 DeviceCreateDesc**) const { return Match_None; }
00285 virtual DeviceBase* NewDeviceInstance() { return 0; }
00286 virtual bool GetDeviceInfo(DeviceInfo*) const { return false; }
00287 };
00288
00289
00290
00291
00292 DeviceManagerImpl::DeviceManagerImpl()
00293 : DeviceImpl<OVR::DeviceManager>(CreateManagerDesc(), 0)
00294
00295 {
00296 if (pCreateDesc)
00297 {
00298 pCreateDesc->pLock->pManager = this;
00299 }
00300 }
00301
00302 DeviceManagerImpl::~DeviceManagerImpl()
00303 {
00304
00305 OVR_ASSERT(!pCreateDesc->pDevice);
00306
00307
00308 while(!Factories.IsEmpty())
00309 {
00310 DeviceFactory* factory = Factories.GetFirst();
00311 factory->RemovedFromManager();
00312 factory->RemoveNode();
00313 }
00314 }
00315
00316 DeviceCreateDesc* DeviceManagerImpl::CreateManagerDesc()
00317 {
00318 DeviceCreateDesc* managerDesc = new DeviceManagerCreateDesc(0);
00319 if (managerDesc)
00320 {
00321 managerDesc->pLock = *new DeviceManagerLock;
00322 }
00323 return managerDesc;
00324 }
00325
00326 bool DeviceManagerImpl::Initialize(DeviceBase* parent)
00327 {
00328 OVR_UNUSED(parent);
00329 if (!pCreateDesc || !pCreateDesc->pLock)
00330 return false;
00331
00332 pProfileManager = *ProfileManager::Create();
00333
00334 return true;
00335 }
00336
00337 void DeviceManagerImpl::Shutdown()
00338 {
00339
00340
00341 while(!Devices.IsEmpty())
00342 {
00343 DeviceCreateDesc* devDesc = Devices.GetFirst();
00344 OVR_ASSERT(!devDesc->pDevice);
00345 devDesc->Enumerated = false;
00346 devDesc->RemoveNode();
00347 devDesc->pNext = devDesc->pPrev = 0;
00348
00349 if (devDesc->HandleCount == 0)
00350 {
00351 delete devDesc;
00352 }
00353 }
00354 Devices.Clear();
00355
00356
00357 OVR_ASSERT(pCreateDesc->pDevice == 0);
00358 OVR_ASSERT(pCreateDesc->pLock->pManager == 0);
00359
00360 pProfileManager.Clear();
00361 }
00362
00363
00364
00365 DeviceBase* DeviceManagerImpl::CreateDevice_MgrThread(DeviceCreateDesc* createDesc, DeviceBase* parent)
00366 {
00367
00368
00369 OVR_ASSERT(createDesc->pLock->pManager);
00370
00371 Lock::Locker devicesLock(GetLock());
00372
00373
00374 if (createDesc->pDevice)
00375 {
00376 createDesc->pDevice->AddRef();
00377 return createDesc->pDevice;
00378 }
00379
00380 if (!parent)
00381 parent = this;
00382
00383 DeviceBase* device = createDesc->NewDeviceInstance();
00384
00385 if (device)
00386 {
00387 if (device->getDeviceCommon()->Initialize(parent))
00388 {
00389 createDesc->pDevice = device;
00390 }
00391 else
00392 {
00393
00394
00395 delete device;
00396 device = 0;
00397 }
00398 }
00399
00400 return device;
00401 }
00402
00403 Void DeviceManagerImpl::ReleaseDevice_MgrThread(DeviceBase* device)
00404 {
00405
00406
00407 Ptr<DeviceCreateDesc> descKeepAlive;
00408 Lock::Locker devicesLock(GetLock());
00409 DeviceCommon* devCommon = device->getDeviceCommon();
00410
00411 while(1)
00412 {
00413 UInt32 refCount = devCommon->RefCount;
00414
00415 if (refCount > 1)
00416 {
00417 if (devCommon->RefCount.CompareAndSet_NoSync(refCount, refCount-1))
00418 {
00419
00420
00421 return 0;
00422 }
00423 }
00424 else if (devCommon->RefCount.CompareAndSet_NoSync(1, 0))
00425 {
00426
00427 break;
00428 }
00429 }
00430
00431
00432
00433
00434
00435
00436 descKeepAlive = devCommon->pCreateDesc;
00437 descKeepAlive->pDevice = 0;
00438 devCommon->Shutdown();
00439 delete device;
00440 return 0;
00441 }
00442
00443
00444
00445 Void DeviceManagerImpl::EnumerateAllFactoryDevices()
00446 {
00447
00448
00449
00450
00451
00452 Lock::Locker deviceLock(GetLock());
00453
00454 DeviceCreateDesc* devDesc, *nextdevDesc;
00455
00456
00457 for(devDesc = Devices.GetFirst();
00458 !Devices.IsNull(devDesc); devDesc = devDesc->pNext)
00459 {
00460
00461 devDesc->Enumerated = false;
00462 }
00463
00464
00465 DeviceFactory* factory = Factories.GetFirst();
00466 while(!Factories.IsNull(factory))
00467 {
00468 EnumerateFactoryDevices(factory);
00469 factory = factory->pNext;
00470 }
00471
00472
00473
00474 for(devDesc = Devices.GetFirst();
00475 !Devices.IsNull(devDesc); devDesc = nextdevDesc)
00476 {
00477
00478 nextdevDesc = devDesc->pNext;
00479
00480
00481
00482 if (!devDesc->Enumerated)
00483 {
00484
00485 CallOnDeviceRemoved(devDesc);
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496 }
00497 }
00498
00499 return 0;
00500 }
00501
00502 Ptr<DeviceCreateDesc> DeviceManagerImpl::AddDevice_NeedsLock(
00503 const DeviceCreateDesc& createDesc)
00504 {
00505
00506 DeviceCreateDesc* descCandidate = 0;
00507
00508 for(DeviceCreateDesc* devDesc = Devices.GetFirst();
00509 !Devices.IsNull(devDesc); devDesc = devDesc->pNext)
00510 {
00511 DeviceCreateDesc::MatchResult mr = devDesc->MatchDevice(createDesc, &descCandidate);
00512 if (mr == DeviceCreateDesc::Match_Found)
00513 {
00514 devDesc->Enumerated = true;
00515 if (!devDesc->pDevice)
00516 CallOnDeviceAdded(devDesc);
00517 return devDesc;
00518 }
00519 }
00520
00521
00522 if (descCandidate)
00523 {
00524 bool newDevice = false;
00525 if (descCandidate->UpdateMatchedCandidate(createDesc, &newDevice))
00526 {
00527 descCandidate->Enumerated = true;
00528 if (!descCandidate->pDevice || newDevice)
00529 CallOnDeviceAdded(descCandidate);
00530 return descCandidate;
00531 }
00532 }
00533
00534
00535
00536
00537 DeviceCreateDesc* desc = createDesc.Clone();
00538 desc->pLock = pCreateDesc->pLock;
00539 Devices.PushBack(desc);
00540 desc->Enumerated = true;
00541
00542 CallOnDeviceAdded(desc);
00543
00544 return desc;
00545 }
00546
00547 Ptr<DeviceCreateDesc> DeviceManagerImpl::FindDevice(
00548 const String& path,
00549 DeviceType deviceType)
00550 {
00551 Lock::Locker deviceLock(GetLock());
00552 DeviceCreateDesc* devDesc;
00553
00554 for (devDesc = Devices.GetFirst();
00555 !Devices.IsNull(devDesc); devDesc = devDesc->pNext)
00556 {
00557 if ((deviceType == Device_None || deviceType == devDesc->Type) &&
00558 devDesc->MatchDevice(path))
00559 return devDesc;
00560 }
00561 return NULL;
00562 }
00563
00564 Ptr<DeviceCreateDesc> DeviceManagerImpl::FindHIDDevice(const HIDDeviceDesc& hidDevDesc)
00565 {
00566 Lock::Locker deviceLock(GetLock());
00567 DeviceCreateDesc* devDesc;
00568
00569 for (devDesc = Devices.GetFirst();
00570 !Devices.IsNull(devDesc); devDesc = devDesc->pNext)
00571 {
00572 if (devDesc->MatchHIDDevice(hidDevDesc))
00573 return devDesc;
00574 }
00575 return NULL;
00576 }
00577
00578 void DeviceManagerImpl::DetectHIDDevice(const HIDDeviceDesc& hidDevDesc)
00579 {
00580 Lock::Locker deviceLock(GetLock());
00581 DeviceFactory* factory = Factories.GetFirst();
00582 while(!Factories.IsNull(factory))
00583 {
00584 if (factory->DetectHIDDevice(this, hidDevDesc))
00585 break;
00586 factory = factory->pNext;
00587 }
00588
00589 }
00590
00591
00592 Void DeviceManagerImpl::EnumerateFactoryDevices(DeviceFactory* factory)
00593 {
00594
00595 class FactoryEnumerateVisitor : public DeviceFactory::EnumerateVisitor
00596 {
00597 DeviceManagerImpl* pManager;
00598 DeviceFactory* pFactory;
00599 public:
00600 FactoryEnumerateVisitor(DeviceManagerImpl* manager, DeviceFactory* factory)
00601 : pManager(manager), pFactory(factory) { }
00602
00603 virtual void Visit(const DeviceCreateDesc& createDesc)
00604 {
00605 pManager->AddDevice_NeedsLock(createDesc);
00606 }
00607 };
00608
00609 FactoryEnumerateVisitor newDeviceVisitor(this, factory);
00610 factory->EnumerateDevices(newDeviceVisitor);
00611
00612
00613 return 0;
00614 }
00615
00616
00617 DeviceEnumerator<> DeviceManagerImpl::EnumerateDevicesEx(const DeviceEnumerationArgs& args)
00618 {
00619 Lock::Locker deviceLock(GetLock());
00620
00621 if (Devices.IsEmpty())
00622 return DeviceEnumerator<>();
00623
00624 DeviceCreateDesc* firstDeviceDesc = Devices.GetFirst();
00625 DeviceEnumerator<> e = enumeratorFromHandle(DeviceHandle(firstDeviceDesc), args);
00626
00627 if (!args.MatchRule(firstDeviceDesc->Type, firstDeviceDesc->Enumerated))
00628 {
00629 e.Next();
00630 }
00631
00632 return e;
00633 }
00634
00635
00636
00637
00638 void DeviceCommon::DeviceAddRef()
00639 {
00640 RefCount++;
00641 }
00642
00643 void DeviceCommon::DeviceRelease()
00644 {
00645 while(1)
00646 {
00647 UInt32 refCount = RefCount;
00648 OVR_ASSERT(refCount > 0);
00649
00650 if (refCount == 1)
00651 {
00652 DeviceManagerImpl* manager = pCreateDesc->GetManagerImpl();
00653 ThreadCommandQueue* queue = manager->GetThreadQueue();
00654
00655
00656
00657
00658
00659 if (!queue->PushCall(manager, &DeviceManagerImpl::ReleaseDevice_MgrThread,
00660 pCreateDesc->pDevice))
00661 {
00662
00663
00664 OVR_ASSERT(false);
00665 }
00666
00667
00668 break;
00669 }
00670 else if (RefCount.CompareAndSet_NoSync(refCount, refCount-1))
00671 {
00672 break;
00673 }
00674 }
00675 }
00676
00677
00678
00679
00680
00681
00682
00683 void DeviceCreateDesc::AddRef()
00684 {
00685
00686
00687 HandleCount++;
00688 }
00689
00690 void DeviceCreateDesc::Release()
00691 {
00692 while(1)
00693 {
00694 UInt32 handleCount = HandleCount;
00695
00696 OVR_ASSERT(handleCount > 0);
00697
00698
00699 if (handleCount == 1)
00700 {
00701 Ptr<DeviceManagerLock> lockKeepAlive;
00702 Lock::Locker deviceLockScope(GetLock());
00703
00704 if (!HandleCount.CompareAndSet_NoSync(handleCount, 0))
00705 continue;
00706
00707 OVR_ASSERT(pDevice == 0);
00708
00709
00710
00711 if (!GetManagerImpl() || !Enumerated)
00712 {
00713 lockKeepAlive = pLock;
00714
00715
00716 if (pNext)
00717 {
00718 RemoveNode();
00719 pNext = pPrev = 0;
00720 }
00721
00722 delete this;
00723 }
00724
00725
00726
00727 break;
00728 }
00729 else if (HandleCount.CompareAndSet_NoSync(handleCount, handleCount-1))
00730 {
00731 break;
00732 }
00733 }
00734 }
00735
00736 HMDDevice* HMDDevice::Disconnect(SensorDevice* psensor)
00737 {
00738 if (!psensor)
00739 return NULL;
00740
00741 OVR::DeviceManager* manager = GetManager();
00742 if (manager)
00743 {
00744
00745 Ptr<DeviceCreateDesc> desc = getDeviceCommon()->pCreateDesc;
00746 if (desc)
00747 {
00748 class Visitor : public DeviceFactory::EnumerateVisitor
00749 {
00750 Ptr<DeviceCreateDesc> Desc;
00751 public:
00752 Visitor(DeviceCreateDesc* desc) : Desc(desc) {}
00753 virtual void Visit(const DeviceCreateDesc& createDesc)
00754 {
00755 Lock::Locker lock(Desc->GetLock());
00756 Desc->UpdateMatchedCandidate(createDesc);
00757 }
00758 } visitor(desc);
00759
00760
00761 SensorDisplayInfoImpl displayInfo;
00762
00763 if (psensor->GetFeatureReport(displayInfo.Buffer, SensorDisplayInfoImpl::PacketSize))
00764 {
00765 displayInfo.Unpack();
00766
00767
00768
00769 if (displayInfo.DistortionType & SensorDisplayInfoImpl::Mask_BaseFmt)
00770 {
00771 SensorDeviceImpl::EnumerateHMDFromSensorDisplayInfo(displayInfo, visitor);
00772 }
00773 }
00774 }
00775 }
00776 return this;
00777 }
00778
00779 bool HMDDevice::IsDisconnected() const
00780 {
00781 OVR::HMDInfo info;
00782 GetDeviceInfo(&info);
00783
00784
00785 return (strlen(info.DisplayDeviceName) == 0);
00786 }
00787
00788
00789 }
00790