public bool OculusHMDConnected() { try { DetectResult detection = OculusWrap.Detect(1000); if (detection.IsOculusHMDConnected) { return(true); } } catch (Exception) { // intentionally ignore and just assume not connected } return(false); }
void KeepAlive() { KeepAliveCounter++; if (StopRequested) { Stop(); return; } if (RequireHome) { if (KeepAliveCounter % OculusHomeDivider == 0) // to save CPU, do not check every loop { if (System.Diagnostics.Process.GetProcessesByName(Config.OculusHomeProcessName).Any() == false) { if (OculusStatus != OculusConnectionStatus.WaitingHome) { EndCurrentSession(); HomePresent = false; OculusStatus = OculusConnectionStatus.WaitingHome; } } else { HomePresent = true; } } if (!HomePresent) { return; } } if (OculusStatus == OculusConnectionStatus.AllOk) { // poll quit message on every lap LastOculusResult = OculusWrap.GetSessionStatus(SessionPtr, ref SesStatus); if (!OculusWrap.OVR_SUCCESS(LastOculusResult) || (SesStatus.ShouldQuit || SesStatus.DisplayLost || !SesStatus.HmdPresent)) { //StopRequested = true; OculusStatus = OculusConnectionStatus.OculusVRQuit; // to immediately prevent native methods from being called EndCurrentSession(); OculusStatus = OculusConnectionStatus.OculusVRQuit; // again to get correct status (changed in EndCurrentSession()) // a good sleep before starting to poll OculusVR again Thread.Sleep(15000); } else if (OculusWrap.OVR_SUCCESS(LastOculusResult)) { bool mounted = SesStatus.HmdMounted; if (mounted == HMDMounted_previousReading) { HMDMountCounter++; } else { HMDMountCounter = 0; } HMDMounted_previousReading = mounted; if (HMDMountCounter > HMDMountDivider) // some buffer to filter out false flags (and conveniently some delay for notifications) { if (HMDMounted_buffered != mounted) { HMDMounted_buffered = mounted; if (mounted) { Worker.ReportProgress(1, null); } else { Worker.ReportProgress(2, null); } } HMDMountCounter = 0; } } return; } // This point is reached only when connection not yet created // ******** INITIALIZATION & CREATION *********** if (KeepAliveCounter % InitializationDivider != 0) // do not try initialization every loop { return; } if (OculusStatus == OculusConnectionStatus.Initialized) { LastOculusResult = OculusWrap.Create(ref SessionPtr, ref pLuid); if (OculusWrap.OVR_SUCCESS(LastOculusResult)) { OculusStatus = OculusConnectionStatus.AllOk; SessionCreated = true; } } else { DetectResult detection = OculusWrap.Detect(1000); if (detection.IsOculusHMDConnected) { uint libMinorVersion = 0; LastOculusResult = Result.LibVersion; // To initialize directly from LibOVRRT-dll, the requested version is required. // --> Try initialization until reaching the first valid version. (Future proofing.) // (Oculus library defines a lowest requestable version, but it can change in the future.) while (!OculusWrap.OVR_SUCCESS(LastOculusResult) && libMinorVersion < 100) { InitParams par = new InitParams(InitFlags.RequestVersion | InitFlags.Invisible, libMinorVersion); LastOculusResult = OculusWrap.Initialize(par); libMinorVersion++; } if (OculusWrap.OVR_SUCCESS(LastOculusResult)) { OculusStatus = OculusConnectionStatus.Initialized; SessionInitialized = true; } else { OculusStatus = OculusConnectionStatus.InitFail; } } else if (detection.IsOculusServiceRunning) { OculusStatus = OculusConnectionStatus.NoHMD; } else { OculusStatus = OculusConnectionStatus.NoService; } } }