Пример #1
0
        unsafe void UpdateButtons(RawInput *rin, Device stick)
        {
            stick.ClearButtons();

            for (int i = 0; i < stick.ButtonCaps.Count; i++)
            {
                short * usage_list   = stackalloc short[(int)JoystickButton.Last + 1];
                int     usage_length = (int)JoystickButton.Last;
                HIDPage page         = stick.ButtonCaps[i].UsagePage;
                short   collection   = stick.ButtonCaps[i].LinkCollection;

                HidProtocolStatus status = HidProtocol.GetUsages(
                    HidProtocolReportType.Input,
                    page, 0, usage_list, ref usage_length,
                    PreparsedData,
                    new IntPtr((void *)&rin->Data.HID.RawData),
                    rin->Data.HID.Size);

                if (status != HidProtocolStatus.Success)
                {
                    Debug.Print("[WinRawJoystick] HidProtocol.GetUsages() failed with {0}",
                                Marshal.GetLastWin32Error());
                    continue;
                }

                for (int j = 0; j < usage_length; j++)
                {
                    short usage = *(usage_list + j);
                    stick.SetButton(collection, page, usage, true);
                }
            }
        }
Пример #2
0
        private unsafe void UpdateAxes(RawInput *rin, Device stick)
        {
            for (int i = 0; i < stick.AxisCaps.Count; i++)
            {
                if (stick.AxisCaps[i].IsRange)
                {
                    Debug.Print("[{0}] Axis range collections not implemented. Please report your controller type at https://github.com/opentk/opentk/issues",
                                GetType().Name);
                    continue;
                }

                HIDPage page       = stick.AxisCaps[i].UsagePage;
                short   usage      = stick.AxisCaps[i].NotRange.Usage;
                uint    value      = 0;
                short   collection = stick.AxisCaps[i].LinkCollection;

                HidProtocolStatus status = HidProtocol.GetUsageValue(
                    HidProtocolReportType.Input,
                    page, 0, usage, ref value,
                    PreparsedData,
                    new IntPtr((void *)&rin->Data.HID.RawData),
                    rin->Data.HID.Size);

                if (status != HidProtocolStatus.Success)
                {
                    Debug.Print("[{0}] HidProtocol.GetScaledUsageValue() failed. Error: {1}",
                                GetType().Name, status);
                    continue;
                }

                if (page == HIDPage.GenericDesktop && (HIDUsageGD)usage == HIDUsageGD.Hatswitch)
                {
                    stick.SetHat(collection, page, usage, GetHatPosition(value, stick.AxisCaps[i]));
                }
                else
                {
                    if (stick.AxisCaps[i].LogicalMin > 0)
                    {
                        short scaled_value = (short)HidHelper.ScaleValue(
                            (int)((long)value + stick.AxisCaps[i].LogicalMin),
                            stick.AxisCaps[i].LogicalMin, stick.AxisCaps[i].LogicalMax,
                            Int16.MinValue, Int16.MaxValue);
                        stick.SetAxis(collection, page, usage, scaled_value);
                    }
                    else
                    {
                        //If our stick returns a minimum value below zero, we should not add this to our value
                        //before attempting to scale it, as this then inverts the value
                        short scaled_value = (short)HidHelper.ScaleValue(
                            (int)(long)value,
                            stick.AxisCaps[i].LogicalMin, stick.AxisCaps[i].LogicalMax,
                            Int16.MinValue, Int16.MaxValue);
                        stick.SetAxis(collection, page, usage, scaled_value);
                    }
                }
            }
        }