예제 #1
0
        private bool UpdateEventState()
        {
            /*var vrEvents = new List<VREvent_t>();
             * var vrEvent = new VREvent_t();
             * try
             * {
             *  while (OpenVR.System.PollNextEvent(ref vrEvent, (uint)(Marshal.SizeOf(vrEvent))))
             *  {
             *      vrEvents.Add(vrEvent);
             *  }
             * } */

            // #6 Update action set
            if (mActionSetArray == null)
            {
                var actionSet = new VRActiveActionSet_t
                {
                    ulActionSet          = actionHandle,
                    ulRestrictedToDevice = OpenVR.k_ulInvalidActionSetHandle,
                    nPriority            = 0
                };
                mActionSetArray = new VRActiveActionSet_t[] { actionSet };
            }

            var errorUAS = OpenVR.Input.UpdateActionState(mActionSetArray, (uint)Marshal.SizeOf(typeof(VRActiveActionSet_t)));

            if (errorUAS != EVRInputError.None)
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        private static void InitActionSet()
        {
            ulong handleLegacy = 0;

            OpenVR.Input.GetActionSetHandle("/actions/default", ref handleLegacy);
            actionSet    = new VRActiveActionSet_t[1];
            actionSet[0] = new VRActiveActionSet_t
            {
                ulActionSet          = handleLegacy,
                ulRestrictedToDevice = 0
            };
        }
        private static void UpdateDebugText()
        {
            StringBuilder stringBuilder = new StringBuilder();

            for (int activeIndex = 0; activeIndex < rawActiveActionSetArray.Length; activeIndex++)
            {
                VRActiveActionSet_t set = rawActiveActionSetArray[activeIndex];
                stringBuilder.Append(set.nPriority);
                stringBuilder.Append("\t");
                stringBuilder.Append(SteamVR_Input_Source.GetSource(set.ulRestrictedToDevice));
                stringBuilder.Append("\t");
                stringBuilder.Append(GetSetFromHandle(set.ulActionSet).GetShortName());
                stringBuilder.Append("\n");
            }

            debugActiveSetListText = stringBuilder.ToString();
        }
        private static void UpdateActionSetsArray()
        {
            List <VRActiveActionSet_t> activeActionSetsList = new List <VRActiveActionSet_t>();

            SteamVR_Input_Sources[] sources = SteamVR_Input_Source.GetAllSources();

            for (int actionSetIndex = 0; actionSetIndex < SteamVR_Input.actionSets.Length; actionSetIndex++)
            {
                SteamVR_ActionSet set = SteamVR_Input.actionSets[actionSetIndex];

                for (int sourceIndex = 0; sourceIndex < sources.Length; sourceIndex++)
                {
                    SteamVR_Input_Sources source = sources[sourceIndex];

                    if (set.ReadRawSetActive(source))
                    {
                        VRActiveActionSet_t activeSet = new VRActiveActionSet_t();
                        activeSet.ulActionSet = set.handle;
                        activeSet.nPriority   = set.ReadRawSetPriority(source);
                        //TODO: Action set priority
                        //activeSet.nPriority = priorityToUse;
                        activeSet.ulRestrictedToDevice = SteamVR_Input_Source.GetHandle(source);

                        int insertionIndex = 0;
                        for (insertionIndex = 0; insertionIndex < activeActionSetsList.Count; insertionIndex++)
                        {
                            if (activeActionSetsList[insertionIndex].nPriority > activeSet.nPriority)
                            {
                                break;
                            }
                        }
                        activeActionSetsList.Insert(insertionIndex, activeSet);
                    }
                }
            }

            changed = false;

            rawActiveActionSetArray = activeActionSetsList.ToArray();

            if (Application.isEditor || updateDebugTextInBuilds)
            {
                UpdateDebugText();
            }
        }
예제 #5
0
        internal static void UpdateActionState(List <ulong> handles)
        {
            VRActiveActionSet_t[] activeActionSets = new VRActiveActionSet_t[handles.Count];

            for (int i = 0; i < handles.Count; i++)
            {
                activeActionSets[i] = new VRActiveActionSet_t
                {
                    ulActionSet          = handles[i],
                    ulRestrictedToDevice = OpenVR.k_ulInvalidInputValueHandle
                };
            }

            EVRInputError error = OpenVR.Input.UpdateActionState(activeActionSets, (uint)Marshal.SizeOf(typeof(VRActiveActionSet_t)));

            if (error != EVRInputError.None && error != EVRInputError.NoData)
            {
                throw new OpenVRInputException($"Could not update action states: {error}", error);
            }
        }
예제 #6
0
        //---------------アクション更新-------------------

        //VRActiveActionSetを生成する。これを配列に突っ込んでUpdateActionSetStateを呼び出す
        public void AddActiveActionSet(ActiveActionSets ActionSets, string ActionSetPath, string RestrictInputSourcePath = "", string SecondaryActionSetPath = "")
        {
            VRActiveActionSet_t sets = new VRActiveActionSet_t();

            sets.ulActionSet          = GetActionSetHandle(ActionSetPath);  //更新対象
            sets.ulRestrictedToDevice = OpenVR.k_ulInvalidInputValueHandle; //制限なし
            sets.ulSecondaryActionSet = OpenVR.k_ulInvalidActionSetHandle;  //無効値を設定

            //制約デバイスが指定されているならば適用
            if (RestrictInputSourcePath != "")
            {
                sets.ulRestrictedToDevice = GetInputSourceHandle(RestrictInputSourcePath); //制約デバイス
            }
            //セカンダリアクションセットが指定されているならば適用
            if (SecondaryActionSetPath != "")
            {
                sets.ulSecondaryActionSet = GetActionSetHandle(SecondaryActionSetPath); //無効値を設定
            }

            ActionSets.Add(sets);
        }
예제 #7
0
        private static void Worker()
        {
            Thread.CurrentThread.IsBackground = true;
            while (true)
            {
                // Getting events
                var vrEvents = new List <VREvent_t>();
                var vrEvent  = new VREvent_t();
                try
                {
                    while (OpenVR.System.PollNextEvent(ref vrEvent, Utils.SizeOf(vrEvent)))
                    {
                        vrEvents.Add(vrEvent);
                    }
                }
                catch (Exception e)
                {
                    Utils.PrintWarning($"Could not get evemt: {e.Message}");
                }

                // Priting events
                foreach (var e in vrEvents)
                {
                    var pid = e.data.process.pid;
                    if ((EVREventType)vrEvent.eventType != EVREventType.VREvent_None)
                    {
                        var name    = Enum.GetName(typeof(EVREventType), e.eventType);
                        var message = $"[{pid}] {name}";
                        if (pid == 0)
                        {
                            Utils.PrintVerbose(message);
                        }
                        else if (name.ToLower().Contains("fail"))
                        {
                            Utils.PrintWarning(message);
                        }
                        else if (name.ToLower().Contains("error"))
                        {
                            Utils.PrintError(message);
                        }
                        else if (name.ToLower().Contains("success"))
                        {
                            Utils.PrintInfo(message);
                        }
                        else
                        {
                            Utils.Print(message);
                        }
                    }
                }

                // Update action set
                // Seems I need the action set when updating the state of actions.
                var actionSet = new VRActiveActionSet_t();
                actionSet.ulActionSet = mActionSetHandle;
                var actionSetArr = new VRActiveActionSet_t[1] {
                    actionSet
                };

                // But I cannot get the size of an array so I supply the one for the set inside the array.
                // No really sure what I am actually supposed to do here (or above).
                var errorUAS = OpenVR.Input.UpdateActionState(actionSetArr, Utils.SizeOf(actionSet));
                if (errorUAS != EVRInputError.None)
                {
                    Utils.PrintError($"UpdateActionState Error: {Enum.GetName(typeof(EVRInputError), errorUAS)}");
                }

                // Get input actions
                var roles = new ETrackedControllerRole[] { ETrackedControllerRole.LeftHand, ETrackedControllerRole.RightHand };
                foreach (var role in roles)
                {
                    // Get device to restrict to, appears mandatory, makes sense for shared actions.
                    uint index = OpenVR.System.GetTrackedDeviceIndexForControllerRole(role);

                    // Load action data
                    var action = new InputDigitalActionData_t(); // I assume this is used for boolean inputs.
                    var size   = Utils.SizeOf(action);
                    var error  = OpenVR.Input.GetDigitalActionData(mActionHandle, ref action, size, index);

                    // Result
                    if (error != mLastError)
                    {
                        mLastError = error;
                        Utils.PrintError($"DigitalActionDataError: {Enum.GetName(typeof(EVRInputError), error)}");
                    }

                    Debug.WriteLine(action.bActive + " " + action.bChanged);
                    if (action.bChanged)
                    {
                        Utils.PrintInfo($"Action state changed to: {action.bState}");
                    }
                }

                // Restrict rate
                Thread.Sleep(1000 / 30);
            }
        }
예제 #8
0
        public void Run()
        {
            var ev     = new VREvent_t();
            var evSize = (uint)Marshal.SizeOf(typeof(VREvent_t));

            var actionSets = new VRActiveActionSet_t[]
            {
                new VRActiveActionSet_t
                {
                    ulActionSet = inputSet,
                },
            };
            var actionSetSize = (uint)Marshal.SizeOf(typeof(VRActiveActionSet_t));

            var actionData     = new InputDigitalActionData_t();
            var actionDataSize = (uint)Marshal.SizeOf(typeof(InputDigitalActionData_t));

            Console.WriteLine("Brightness panic button is running. Input bindings may be changed through SteamVR's input bindings.");

            var sleepTime = (int)Math.Round(1000 * DT);

            while (running)
            {
                while (vrSystem.PollNextEvent(ref ev, evSize))
                {
                    switch ((EVREventType)ev.eventType)
                    {
                    case EVREventType.VREvent_DriverRequestedQuit:
                    case EVREventType.VREvent_Quit:
                        vrSystem.AcknowledgeQuit_Exiting();
                        running = false;
                        break;
                    }
                }

                var inputError = vrInput.UpdateActionState(actionSets, actionSetSize);
                if (inputError != EVRInputError.None)
                {
                    var message = inputError.ToString();
                    throw new Exception($"Failed to update action state: {message}");
                }

                inputError = vrInput.GetDigitalActionData(inputActivate, ref actionData, actionDataSize, 0);
                if (inputError != EVRInputError.None)
                {
                    var message = inputError.ToString();
                    throw new Exception($"Failed to get Activate action state: {message}");
                }
                var activatePressed = actionData.bChanged && actionData.bState;

                inputError = vrInput.GetDigitalActionData(inputResetAuto, ref actionData, actionDataSize, 0);
                if (inputError != EVRInputError.None)
                {
                    var message = inputError.ToString();
                    throw new Exception($"Failed to get Reset (Auto) action state: {message}");
                }
                var resetAutoPressed = actionData.bChanged && actionData.bState;

                inputError = vrInput.GetDigitalActionData(inputResetHold, ref actionData, actionDataSize, 0);
                if (inputError != EVRInputError.None)
                {
                    var message = inputError.ToString();
                    throw new Exception($"Failed to get Reset (Hold) action state: {message}");
                }
                var resetHoldChanged = actionData.bChanged;
                var resetHoldHeld    = actionData.bState;

                if (activatePressed)
                {
                    TriggerActivate();
                }

                if (initialBrightness.HasValue && resetAutoPressed)
                {
                    resetting = !resetting;
                    if (resetting)
                    {
                        Console.WriteLine("Starting automatic reset.");
                    }
                    else
                    {
                        Console.WriteLine("Cancelling automatic reset.");
                    }
                }

                if (initialBrightness.HasValue && resetHoldChanged)
                {
                    resetting = resetHoldHeld;
                    if (resetting)
                    {
                        Console.WriteLine("Starting held reset.");
                    }
                    else
                    {
                        Console.WriteLine("Cancelling held reset.");
                    }
                }

                if (resetting)
                {
                    resetting = TriggerReset(DT * RESET_SPEED);
                    if (resetting && (resetAutoPressed || resetHoldChanged))
                    {
                        try
                        {
                            resetSound.Play();
                        }
                        catch (FileNotFoundException) {}
                    }
                }

                Thread.Sleep(sleepTime);
            }
        }
예제 #9
0
        private static void Worker()
        {
            Thread.CurrentThread.IsBackground = true;
            while (true)
            {
                TrackableDeviceInfo.UpdateTrackableDevicePosition();

                // Getting events
                var vrEvents = new List <VREvent_t>();
                var vrEvent  = new VREvent_t();
                try
                {
                    while (OpenVR.System.PollNextEvent(ref vrEvent, Utils.SizeOf(vrEvent)))
                    {
                        vrEvents.Add(vrEvent);
                    }
                }
                catch (Exception e)
                {
                    Utils.PrintWarning($"Could not get events: {e.Message}");
                }

                // Printing events
                foreach (var e in vrEvents)
                {
                    var pid = e.data.process.pid;
                    if (e.eventType == (uint)EVREventType.VREvent_Input_HapticVibration)
                    {
                        ETrackedControllerRole DeviceType = OpenVR.System.GetControllerRoleForTrackedDeviceIndex(e.data.process.pid);
                        if (DeviceType != ETrackedControllerRole.LeftHand && DeviceType != ETrackedControllerRole.RightHand)
                        {
                            continue;
                        }
                        NewVibrationEvent(DeviceType, e.data.hapticVibration);
                    }
#if DEBUG
                    if ((EVREventType)vrEvent.eventType != EVREventType.VREvent_None)
                    {
                        var name    = Enum.GetName(typeof(EVREventType), e.eventType);
                        var message = $"[{pid}] {name}";
                        if (pid == 0)
                        {
                            Utils.PrintVerbose(message);
                        }
                        else if (name == null)
                        {
                            Utils.PrintVerbose(message);
                        }
                        else if (name.ToLower().Contains("fail"))
                        {
                            Utils.PrintWarning(message);
                        }
                        else if (name.ToLower().Contains("error"))
                        {
                            Utils.PrintError(message);
                        }
                        else if (name.ToLower().Contains("success"))
                        {
                            Utils.PrintInfo(message);
                        }
                        else
                        {
                            Utils.Print(message);
                        }
                    }
#endif
                }

                // #6 Update action set
                if (mActionSetArray == null)
                {
                    var actionSet = new VRActiveActionSet_t
                    {
                        ulActionSet          = mActionSetHandle,
                        ulRestrictedToDevice = OpenVR.k_ulInvalidActionSetHandle,
                        nPriority            = 0
                    };
                    mActionSetArray = new VRActiveActionSet_t[] { actionSet };
                }

                var errorUAS = OpenVR.Input.UpdateActionState(mActionSetArray, (uint)Marshal.SizeOf(typeof(VRActiveActionSet_t)));
                if (errorUAS != EVRInputError.None)
                {
                    Utils.PrintError($"UpdateActionState Error: {Enum.GetName(typeof(EVRInputError), errorUAS)}");
                }

                // #7 Load input action data
                leftController.UpdateAllState();
                rightController.UpdateAllState();

                // Restrict rate
                Thread.Sleep((int)(1000 / DataFrameRate));
            }
        }
예제 #10
0
 public void Add(VRActiveActionSet_t set)
 {
     List.Add(set);
 }