nuclear@1: /************************************************************************************ nuclear@1: nuclear@1: Filename : OVR_Linux_DeviceManager.h nuclear@1: Content : Linux implementation of DeviceManager. nuclear@1: Created : nuclear@1: Authors : nuclear@1: nuclear@1: Copyright : Copyright 2012 Oculus VR, Inc. All Rights reserved. nuclear@1: nuclear@1: Use of this software is subject to the terms of the Oculus license nuclear@1: agreement provided at the time of installation or download, or which nuclear@1: otherwise accompanies this software in either electronic or hard copy form. nuclear@1: nuclear@1: *************************************************************************************/ nuclear@1: nuclear@1: #include "OVR_Linux_DeviceManager.h" nuclear@1: nuclear@1: // Sensor & HMD Factories nuclear@1: #include "OVR_LatencyTestImpl.h" nuclear@1: #include "OVR_SensorImpl.h" nuclear@1: #include "OVR_Linux_HIDDevice.h" nuclear@1: #include "OVR_Linux_HMDDevice.h" nuclear@1: nuclear@1: #include "Kernel/OVR_Timer.h" nuclear@1: #include "Kernel/OVR_Std.h" nuclear@1: #include "Kernel/OVR_Log.h" nuclear@1: nuclear@1: namespace OVR { namespace Linux { nuclear@1: nuclear@1: nuclear@1: //------------------------------------------------------------------------------------- nuclear@1: // **** Linux::DeviceManager nuclear@1: nuclear@1: DeviceManager::DeviceManager() nuclear@1: { nuclear@1: } nuclear@1: nuclear@1: DeviceManager::~DeviceManager() nuclear@1: { nuclear@1: } nuclear@1: nuclear@1: bool DeviceManager::Initialize(DeviceBase*) nuclear@1: { nuclear@1: if (!DeviceManagerImpl::Initialize(0)) nuclear@1: return false; nuclear@1: nuclear@1: pThread = *new DeviceManagerThread(); nuclear@1: if (!pThread || !pThread->Start()) nuclear@1: return false; nuclear@1: nuclear@1: // Wait for the thread to be fully up and running. nuclear@1: pThread->StartupEvent.Wait(); nuclear@1: nuclear@1: // Do this now that we know the thread's run loop. nuclear@1: HidDeviceManager = *HIDDeviceManager::CreateInternal(this); nuclear@1: nuclear@1: pCreateDesc->pDevice = this; nuclear@1: LogText("OVR::DeviceManager - initialized.\n"); nuclear@1: return true; nuclear@1: } nuclear@1: nuclear@1: void DeviceManager::Shutdown() nuclear@1: { nuclear@1: LogText("OVR::DeviceManager - shutting down.\n"); nuclear@1: nuclear@1: // Set Manager shutdown marker variable; this prevents nuclear@1: // any existing DeviceHandle objects from accessing device. nuclear@1: pCreateDesc->pLock->pManager = 0; nuclear@1: nuclear@1: // Push for thread shutdown *WITH NO WAIT*. nuclear@1: // This will have the following effect: nuclear@1: // - Exit command will get enqueued, which will be executed later on the thread itself. nuclear@1: // - Beyond this point, this DeviceManager object may be deleted by our caller. nuclear@1: // - Other commands, such as CreateDevice, may execute before ExitCommand, but they will nuclear@1: // fail gracefully due to pLock->pManager == 0. Future commands can't be enqued nuclear@1: // after pManager is null. nuclear@1: // - Once ExitCommand executes, ThreadCommand::Run loop will exit and release the last nuclear@1: // reference to the thread object. nuclear@1: pThread->PushExitCommand(false); nuclear@1: pThread.Clear(); nuclear@1: nuclear@1: DeviceManagerImpl::Shutdown(); nuclear@1: } nuclear@1: nuclear@1: ThreadCommandQueue* DeviceManager::GetThreadQueue() nuclear@1: { nuclear@1: return pThread; nuclear@1: } nuclear@1: nuclear@1: ThreadId DeviceManager::GetThreadId() const nuclear@1: { nuclear@1: return pThread->GetThreadId(); nuclear@1: } nuclear@1: nuclear@1: bool DeviceManager::GetDeviceInfo(DeviceInfo* info) const nuclear@1: { nuclear@1: if ((info->InfoClassType != Device_Manager) && nuclear@1: (info->InfoClassType != Device_None)) nuclear@1: return false; nuclear@1: nuclear@1: info->Type = Device_Manager; nuclear@1: info->Version = 0; nuclear@1: OVR_strcpy(info->ProductName, DeviceInfo::MaxNameLength, "DeviceManager"); nuclear@1: OVR_strcpy(info->Manufacturer,DeviceInfo::MaxNameLength, "Oculus VR, Inc."); nuclear@1: return true; nuclear@1: } nuclear@1: nuclear@1: DeviceEnumerator<> DeviceManager::EnumerateDevicesEx(const DeviceEnumerationArgs& args) nuclear@1: { nuclear@1: // TBD: Can this be avoided in the future, once proper device notification is in place? nuclear@1: pThread->PushCall((DeviceManagerImpl*)this, nuclear@1: &DeviceManager::EnumerateAllFactoryDevices, true); nuclear@1: nuclear@1: return DeviceManagerImpl::EnumerateDevicesEx(args); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: //------------------------------------------------------------------------------------- nuclear@1: // ***** DeviceManager Thread nuclear@1: nuclear@1: DeviceManagerThread::DeviceManagerThread() nuclear@1: : Thread(ThreadStackSize) nuclear@1: { nuclear@1: int result = pipe(CommandFd); nuclear@1: OVR_ASSERT(!result); nuclear@1: nuclear@1: AddSelectFd(NULL, CommandFd[0]); nuclear@1: } nuclear@1: nuclear@1: DeviceManagerThread::~DeviceManagerThread() nuclear@1: { nuclear@1: if (CommandFd[0]) nuclear@1: { nuclear@1: RemoveSelectFd(NULL, CommandFd[0]); nuclear@1: close(CommandFd[0]); nuclear@1: close(CommandFd[1]); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: bool DeviceManagerThread::AddSelectFd(Notifier* notify, int fd) nuclear@1: { nuclear@1: struct pollfd pfd; nuclear@1: pfd.fd = fd; nuclear@1: pfd.events = POLLIN|POLLHUP|POLLERR; nuclear@1: pfd.revents = 0; nuclear@1: nuclear@1: FdNotifiers.PushBack(notify); nuclear@1: PollFds.PushBack(pfd); nuclear@1: nuclear@1: OVR_ASSERT(FdNotifiers.GetSize() == PollFds.GetSize()); nuclear@1: return true; nuclear@1: } nuclear@1: nuclear@1: bool DeviceManagerThread::RemoveSelectFd(Notifier* notify, int fd) nuclear@1: { nuclear@1: // [0] is reserved for thread commands with notify of null, but we still nuclear@1: // can use this function to remove it. nuclear@1: for (UPInt i = 0; i < FdNotifiers.GetSize(); i++) nuclear@1: { nuclear@1: if ((FdNotifiers[i] == notify) && (PollFds[i].fd == fd)) nuclear@1: { nuclear@1: FdNotifiers.RemoveAt(i); nuclear@1: PollFds.RemoveAt(i); nuclear@1: return true; nuclear@1: } nuclear@1: } nuclear@1: return false; nuclear@1: } nuclear@1: nuclear@1: nuclear@1: nuclear@1: int DeviceManagerThread::Run() nuclear@1: { nuclear@1: ThreadCommand::PopBuffer command; nuclear@1: nuclear@1: SetThreadName("OVR::DeviceManagerThread"); nuclear@1: LogText("OVR::DeviceManagerThread - running (ThreadId=%p).\n", GetThreadId()); nuclear@1: nuclear@1: // Signal to the parent thread that initialization has finished. nuclear@1: StartupEvent.SetEvent(); nuclear@1: nuclear@1: while(!IsExiting()) nuclear@1: { nuclear@1: // PopCommand will reset event on empty queue. nuclear@1: if (PopCommand(&command)) nuclear@1: { nuclear@1: command.Execute(); nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: bool commands = 0; nuclear@1: do nuclear@1: { nuclear@1: int waitMs = -1; nuclear@1: nuclear@1: // If devices have time-dependent logic registered, get the longest wait nuclear@1: // allowed based on current ticks. nuclear@1: if (!TicksNotifiers.IsEmpty()) nuclear@1: { nuclear@1: UInt64 ticksMks = Timer::GetTicks(); nuclear@1: int waitAllowed; nuclear@1: nuclear@1: for (UPInt j = 0; j < TicksNotifiers.GetSize(); j++) nuclear@1: { nuclear@1: waitAllowed = (int)(TicksNotifiers[j]->OnTicks(ticksMks) / Timer::MksPerMs); nuclear@1: if (waitAllowed < waitMs) nuclear@1: waitMs = waitAllowed; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: // wait until there is data available on one of the devices or the timeout expires nuclear@1: int n = poll(&PollFds[0], PollFds.GetSize(), waitMs); nuclear@1: nuclear@1: if (n > 0) nuclear@1: { nuclear@1: // Iterate backwards through the list so the ordering will not be nuclear@1: // affected if the called object gets removed during the callback nuclear@1: // Also, the HID data streams are located toward the back of the list nuclear@1: // and servicing them first will allow a disconnect to be handled nuclear@1: // and cleaned directly at the device first instead of the general HID monitor nuclear@1: for (int i=PollFds.GetSize()-1; i>=0; i--) nuclear@1: { nuclear@1: if (PollFds[i].revents & POLLERR) nuclear@1: { nuclear@1: OVR_DEBUG_LOG(("poll: error on [%d]: %d", i, PollFds[i].fd)); nuclear@1: } nuclear@1: else if (PollFds[i].revents & POLLIN) nuclear@1: { nuclear@1: if (FdNotifiers[i]) nuclear@1: FdNotifiers[i]->OnEvent(i, PollFds[i].fd); nuclear@1: else if (i == 0) // command nuclear@1: { nuclear@1: char dummy[128]; nuclear@1: read(PollFds[i].fd, dummy, 128); nuclear@1: commands = 1; nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: if (PollFds[i].revents & POLLHUP) nuclear@1: PollFds[i].events = 0; nuclear@1: nuclear@1: if (PollFds[i].revents != 0) nuclear@1: { nuclear@1: n--; nuclear@1: if (n == 0) nuclear@1: break; nuclear@1: } nuclear@1: } nuclear@1: } nuclear@1: } while (PollFds.GetSize() > 0 && !commands); nuclear@1: } nuclear@1: } nuclear@1: nuclear@1: LogText("OVR::DeviceManagerThread - exiting (ThreadId=%p).\n", GetThreadId()); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: bool DeviceManagerThread::AddTicksNotifier(Notifier* notify) nuclear@1: { nuclear@1: TicksNotifiers.PushBack(notify); nuclear@1: return true; nuclear@1: } nuclear@1: nuclear@1: bool DeviceManagerThread::RemoveTicksNotifier(Notifier* notify) nuclear@1: { nuclear@1: for (UPInt i = 0; i < TicksNotifiers.GetSize(); i++) nuclear@1: { nuclear@1: if (TicksNotifiers[i] == notify) nuclear@1: { nuclear@1: TicksNotifiers.RemoveAt(i); nuclear@1: return true; nuclear@1: } nuclear@1: } nuclear@1: return false; nuclear@1: } nuclear@1: nuclear@1: } // namespace Linux nuclear@1: nuclear@1: nuclear@1: //------------------------------------------------------------------------------------- nuclear@1: // ***** Creation nuclear@1: nuclear@1: nuclear@1: // Creates a new DeviceManager and initializes OVR. nuclear@1: DeviceManager* DeviceManager::Create() nuclear@1: { nuclear@1: if (!System::IsInitialized()) nuclear@1: { nuclear@1: // Use custom message, since Log is not yet installed. nuclear@1: OVR_DEBUG_STATEMENT(Log::GetDefaultLog()-> nuclear@1: LogMessage(Log_Debug, "DeviceManager::Create failed - OVR::System not initialized"); ); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: Ptr manager = *new Linux::DeviceManager; nuclear@1: nuclear@1: if (manager) nuclear@1: { nuclear@1: if (manager->Initialize(0)) nuclear@1: { nuclear@1: manager->AddFactory(&LatencyTestDeviceFactory::Instance); nuclear@1: manager->AddFactory(&SensorDeviceFactory::Instance); nuclear@1: manager->AddFactory(&Linux::HMDDeviceFactory::Instance); nuclear@1: nuclear@1: manager->AddRef(); nuclear@1: } nuclear@1: else nuclear@1: { nuclear@1: manager.Clear(); nuclear@1: } nuclear@1: nuclear@1: } nuclear@1: nuclear@1: return manager.GetPtr(); nuclear@1: } nuclear@1: nuclear@1: nuclear@1: } // namespace OVR nuclear@1: