コード例 #1
0
    EDeviceActivityLevel getHeadActivityLevel()
    {
        CVRSystem            cvrSystem = SteamVR.instance.hmd;
        EDeviceActivityLevel actlev    = cvrSystem.GetTrackedDeviceActivityLevel(OpenVR.k_unTrackedDeviceIndex_Hmd);

        return(actlev);
    }
コード例 #2
0
        void KeepAlive()
        {
            KeepAliveCounter++;

            if (StopRequested)
            {
                Stop();
                return;
            }

            if (VRSys == null)
            {
                if (KeepAliveCounter % InitializationDivider != 0) // do not attempt initialization on every loop.
                {
                    return;
                }

                // ***** INITIALIZATION ******

                //if (!OpenVR.IsHmdPresent()) // Note that this also leaks memory

                // To avoid a memory leak, check that SteamVR is running before trying to Initialize
                if (System.Diagnostics.Process.GetProcessesByName(Config.SteamVRProcessName).Any())
                {
                    if (InitAttemptCount >= InitAttemptLimit)
                    {
                        Stop(true); // no point to keep looping and eating memory forever
                    }

                    InitAttemptCount++;
                    OpenVRConnStatus = OpenVRConnectionStatus.Initializing;
                    // do not carelessly call OpenVR.Init(), it will leak memory
                    VRSys = OpenVR.Init(ref LastOpenVRError, EVRApplicationType.VRApplication_Background);
                    if (LastOpenVRError != EVRInitError.None || VRSys == null)
                    {
                        if (LastOpenVRError == EVRInitError.Init_HmdNotFound || LastOpenVRError == EVRInitError.Init_HmdNotFoundPresenceFailed)
                        {
                            OpenVRConnStatus = OpenVRConnectionStatus.NoHMD;
                            Thread.Sleep(SleepTimeAfterQuit);
                        }
                        return;
                    }

                    bool hmdFound = false;
                    // check devices and find HMD index (but I suppose it's always 0) - Documentation is vague.
                    // For example, what's the purpose of OpenVR.k_unTrackedDeviceIndex_Hmd? What about multiple HMDs...
                    for (uint i = 0; i < OpenVR.k_unMaxTrackedDeviceCount; i++)
                    {
                        if (VRSys.IsTrackedDeviceConnected(i))
                        {
                            ETrackedDeviceClass c = VRSys.GetTrackedDeviceClass(i);
                            if (c == ETrackedDeviceClass.HMD)
                            {
                                HmdIndex = i;
                                hmdFound = true;
                                break;
                            }
                        }
                    }

                    if (!hmdFound‬)
                    {
                        EndCurrentSession();
                        OpenVRConnStatus = OpenVRConnectionStatus.NoHMD;
                        Thread.Sleep(SleepTimeAfterQuit);

                        return;
                    }

                    PoseArray = new TrackedDevicePose_t[HmdIndex + 1];
                    for (int i = 0; i < PoseArray.Length; i++)
                    {
                        PoseArray[i] = new TrackedDevicePose_t();
                    }

                    if (SteamVRWasOffBefore)
                    {
                        // Wait a bit more before reporting OK and allowing hmd queries when SteamVR is started AFTER CG.
                        // The initial yaw values from the API sometimes threw the counter off by one half-turn.
                        // Maybe there's a small window at the beginning when the headset readings are not stable...
                        // This is very hard to reproduce/test as it happens so rarely. Shooting in the dark here.
                        Thread.Sleep(3000);
                        SteamVRWasOffBefore = false;
                    }
                    else
                    {
                        Thread.Sleep(500);
                    }

                    OpenVRConnStatus = OpenVRConnectionStatus.AllOK;
                }
                else
                {
                    SteamVRWasOffBefore = true;
                    OpenVRConnStatus    = OpenVRConnectionStatus.NoSteamVR;
                }
            }
            else // VRSys != null (connection has been initialized)
            {
                // Check quit request
                VRSys.PollNextEvent(ref NextVREvent, (uint)System.Runtime.InteropServices.Marshal.SizeOf(NextVREvent));
                // this doesn't always work... I suppose the quit event can fly by when the poll rate is relatively low
                // It seems that SteamVR kills the VR processes that don't get Quit event with PollNextEvent().
                if (NextVREvent.eventType == (uint)EVREventType.VREvent_Quit)
                {
                    OpenVRConnStatus = OpenVRConnectionStatus.SteamVRQuit; // to immediately prevent native methods from being called
                    EndCurrentSession();
                    OpenVRConnStatus = OpenVRConnectionStatus.SteamVRQuit; // again to get correct status (changed in EndCurrentSession())
                    // a good sleep before starting to poll steamvr -process again
                    Thread.Sleep(SleepTimeAfterQuit);
                }
                else
                {
                    bool interaction = (VRSys.GetTrackedDeviceActivityLevel(HmdIndex) == EDeviceActivityLevel.k_EDeviceActivityLevel_UserInteraction);
                    if (interaction == HMDUserInteraction_previousReading)
                    {
                        HMDUserInteractionCounter++;
                    }
                    else
                    {
                        HMDUserInteractionCounter = 0;
                    }

                    HMDUserInteraction_previousReading = interaction;

                    // some buffer to filter out false flags (and conveniently some delay for notifications)
                    // ... although false flags are not really possible the way OpenVR currently works
                    if (HMDUserInteractionCounter > HMDUserInteractionDivider)
                    {
                        if (HMDUserInteraction_buffered != interaction)
                        {
                            HMDUserInteraction_buffered = interaction;

                            if (interaction)
                            {
                                Worker.ReportProgress(1, null);
                            }
                            else
                            {
                                Worker.ReportProgress(2, null);
                            }
                        }
                        HMDUserInteractionCounter = 0;
                    }
                }
            }
        }