Пример #1
0
        public static uint XInputGetState(uint dwUserIndex, ref XINPUT_STATE pState)
        {
            #if !EXPERIMENTAL
            return OriginalXInputGetStateFunction.Value(dwUserIndex, ref pState);
            #else
            if (OriginalXInputGetStateFunction.Value(dwUserIndex, ref pState) == ResultWin32.ERROR_SUCCESS)
            {
                return ResultWin32.ERROR_SUCCESS;
            }

            try
            {
                ScpHidReport report = null;

                while (dwUserIndex == 0 && (report = Proxy.GetReport(dwUserIndex)) == null)
                {
                    Thread.Sleep(100);
                }

                if (report == null || report.PadState != DsState.Connected)
                {
                    return ResultWin32.ERROR_DEVICE_NOT_CONNECTED;
                }

                var xPad = new XINPUT_GAMEPAD();

                pState.dwPacketNumber = report.PacketCounter;

                switch (report.Model)
                {
                    case DsModel.DS3:
                    {
                        // select & start
                        xPad.wButtons |= (ushort) report[Ds3Button.Select].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.Start].Xbox360Button;

                        // d-pad
                        xPad.wButtons |= (ushort) report[Ds3Button.Up].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.Right].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.Down].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.Left].Xbox360Button;

                        // shoulders
                        xPad.wButtons |= (ushort) report[Ds3Button.L1].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.R1].Xbox360Button;

                        // face buttons
                        xPad.wButtons |= (ushort) report[Ds3Button.Triangle].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.Circle].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.Cross].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.Square].Xbox360Button;

                        // PS/Guide
                        xPad.wButtons |= (ushort) report[Ds3Button.Ps].Xbox360Button;

                        // thumbs
                        xPad.wButtons |= (ushort) report[Ds3Button.L3].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds3Button.R3].Xbox360Button;

                        // triggers
                        xPad.bLeftTrigger = report[Ds3Axis.L2].Value;
                        xPad.bRightTrigger = report[Ds3Axis.R2].Value;

                        // thumb axes
                        xPad.sThumbLX = (short) +DsMath.Scale(report[Ds3Axis.Lx].Value, false);
                        xPad.sThumbLY = (short) -DsMath.Scale(report[Ds3Axis.Ly].Value, false);
                        xPad.sThumbRX = (short) +DsMath.Scale(report[Ds3Axis.Rx].Value, false);
                        xPad.sThumbRY = (short) -DsMath.Scale(report[Ds3Axis.Ry].Value, false);
                    }
                        break;
                    case DsModel.DS4:
                    {
                        // select & start
                        xPad.wButtons |= (ushort) report[Ds4Button.Share].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.Options].Xbox360Button;

                        // d-pad
                        xPad.wButtons |= (ushort) report[Ds4Button.Up].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.Right].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.Down].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.Left].Xbox360Button;

                        // shoulders
                        xPad.wButtons |= (ushort) report[Ds4Button.L1].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.R1].Xbox360Button;

                        // face buttons
                        xPad.wButtons |= (ushort) report[Ds4Button.Triangle].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.Circle].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.Cross].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.Square].Xbox360Button;

                        // PS/Guide
                        xPad.wButtons |= (ushort) report[Ds4Button.Ps].Xbox360Button;

                        // thumbs
                        xPad.wButtons |= (ushort) report[Ds4Button.L3].Xbox360Button;
                        xPad.wButtons |= (ushort) report[Ds4Button.R3].Xbox360Button;

                        // triggers
                        xPad.bLeftTrigger = report[Ds4Axis.L2].Value;
                        xPad.bRightTrigger = report[Ds4Axis.R2].Value;

                        // thumb axes
                        xPad.sThumbLX = (short) +DsMath.Scale(report[Ds4Axis.Lx].Value, false);
                        xPad.sThumbLY = (short) -DsMath.Scale(report[Ds4Axis.Ly].Value, false);
                        xPad.sThumbRX = (short) +DsMath.Scale(report[Ds4Axis.Rx].Value, false);
                        xPad.sThumbRY = (short) -DsMath.Scale(report[Ds4Axis.Ry].Value, false);
                    }
                        break;
                }

                pState.Gamepad = xPad;
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Unexpected error: {0}", ex);
                return ResultWin32.ERROR_DEVICE_NOT_CONNECTED;
            }

            return ResultWin32.ERROR_SUCCESS;
            #endif
        }
Пример #2
0
        /// <summary>
        ///     Translates an <see cref="ScpHidReport"/> to an Xbox 360 compatible byte array.
        /// </summary>
        /// <param name="inputReport">The <see cref="ScpHidReport"/> to translate.</param>
        /// <returns>The translated data as <see cref="XINPUT_GAMEPAD"/> structure.</returns>
        public XINPUT_GAMEPAD Parse(ScpHidReport inputReport)
        {
            var xButton = X360Button.None;
            var output = new XINPUT_GAMEPAD();

            switch (inputReport.Model)
            {
                case DsModel.DS3:
                {
                    // select & start
                    if (inputReport[Ds3Button.Select].IsPressed) xButton |= X360Button.Back;
                    if (inputReport[Ds3Button.Start].IsPressed) xButton |= X360Button.Start;

                    // d-pad
                    if (inputReport[Ds3Button.Up].IsPressed) xButton |= X360Button.Up;
                    if (inputReport[Ds3Button.Right].IsPressed) xButton |= X360Button.Right;
                    if (inputReport[Ds3Button.Down].IsPressed) xButton |= X360Button.Down;
                    if (inputReport[Ds3Button.Left].IsPressed) xButton |= X360Button.Left;

                    // shoulders
                    if (inputReport[Ds3Button.L1].IsPressed) xButton |= X360Button.LB;
                    if (inputReport[Ds3Button.R1].IsPressed) xButton |= X360Button.RB;

                    // face buttons
                    if (inputReport[Ds3Button.Triangle].IsPressed) xButton |= X360Button.Y;
                    if (inputReport[Ds3Button.Circle].IsPressed) xButton |= X360Button.B;
                    if (inputReport[Ds3Button.Cross].IsPressed) xButton |= X360Button.A;
                    if (inputReport[Ds3Button.Square].IsPressed) xButton |= X360Button.X;

                    // PS/Guide
                    if (inputReport[Ds3Button.Ps].IsPressed) xButton |= X360Button.Guide;

                    // thumbs
                    if (inputReport[Ds3Button.L3].IsPressed) xButton |= X360Button.LS;
                    if (inputReport[Ds3Button.R3].IsPressed) xButton |= X360Button.RS;

                    // face buttons
                    output.wButtons = (ushort) xButton;

                    // trigger
                    output.bLeftTrigger = inputReport[Ds3Axis.L2].Value;
                    output.bRightTrigger = inputReport[Ds3Axis.R2].Value;

                    if (!DsMath.DeadZone(GlobalConfiguration.Instance.DeadZoneL,
                        inputReport[Ds3Axis.Lx].Value,
                        inputReport[Ds3Axis.Ly].Value))
                        // Left Stick DeadZone
                    {
                        output.sThumbLX =
                            (short)
                                +DsMath.Scale(inputReport[Ds3Axis.Lx].Value, GlobalConfiguration.Instance.FlipLX);
                        output.sThumbLY =
                            (short)
                                -DsMath.Scale(inputReport[Ds3Axis.Ly].Value, GlobalConfiguration.Instance.FlipLY);
                    }

                    if (!DsMath.DeadZone(GlobalConfiguration.Instance.DeadZoneR,
                        inputReport[Ds3Axis.Rx].Value,
                        inputReport[Ds3Axis.Ry].Value))
                        // Right Stick DeadZone
                    {
                        output.sThumbRX =
                            (short)
                                +DsMath.Scale(inputReport[Ds3Axis.Rx].Value, GlobalConfiguration.Instance.FlipRX);
                        output.sThumbRY =
                            (short)
                                -DsMath.Scale(inputReport[Ds3Axis.Ry].Value, GlobalConfiguration.Instance.FlipRY);
                    }
                }
                    break;

                case DsModel.DS4:
                {
                    if (inputReport[Ds4Button.Share].IsPressed) xButton |= X360Button.Back;
                    if (inputReport[Ds4Button.Options].IsPressed) xButton |= X360Button.Start;

                    if (inputReport[Ds4Button.Up].IsPressed) xButton |= X360Button.Up;
                    if (inputReport[Ds4Button.Right].IsPressed) xButton |= X360Button.Right;
                    if (inputReport[Ds4Button.Down].IsPressed) xButton |= X360Button.Down;
                    if (inputReport[Ds4Button.Left].IsPressed) xButton |= X360Button.Left;

                    if (inputReport[Ds4Button.L1].IsPressed) xButton |= X360Button.LB;
                    if (inputReport[Ds4Button.R1].IsPressed) xButton |= X360Button.RB;

                    if (inputReport[Ds4Button.Triangle].IsPressed) xButton |= X360Button.Y;
                    if (inputReport[Ds4Button.Circle].IsPressed) xButton |= X360Button.B;
                    if (inputReport[Ds4Button.Cross].IsPressed) xButton |= X360Button.A;
                    if (inputReport[Ds4Button.Square].IsPressed) xButton |= X360Button.X;

                    if (inputReport[Ds4Button.Ps].IsPressed) xButton |= X360Button.Guide;

                    if (inputReport[Ds4Button.L3].IsPressed) xButton |= X360Button.LS;
                    if (inputReport[Ds4Button.R3].IsPressed) xButton |= X360Button.RS;

                    // face buttons
                    output.wButtons = (ushort) xButton;

                    // trigger
                    output.bLeftTrigger = inputReport[Ds4Axis.L2].Value;
                    output.bRightTrigger = inputReport[Ds4Axis.R2].Value;

                    if (!DsMath.DeadZone(GlobalConfiguration.Instance.DeadZoneL,
                        inputReport[Ds4Axis.Lx].Value,
                        inputReport[Ds4Axis.Ly].Value))
                        // Left Stick DeadZone
                    {
                        output.sThumbLX =
                            (short)
                                +DsMath.Scale(inputReport[Ds4Axis.Lx].Value, GlobalConfiguration.Instance.FlipLX);
                        output.sThumbLY =
                            (short)
                                -DsMath.Scale(inputReport[Ds4Axis.Ly].Value, GlobalConfiguration.Instance.FlipLY);
                    }

                    if (!DsMath.DeadZone(GlobalConfiguration.Instance.DeadZoneR,
                        inputReport[Ds4Axis.Rx].Value,
                        inputReport[Ds4Axis.Ry].Value))
                        // Right Stick DeadZone
                    {
                        output.sThumbRX =
                            (short)
                                +DsMath.Scale(inputReport[Ds4Axis.Rx].Value, GlobalConfiguration.Instance.FlipRX);
                        output.sThumbRY =
                            (short)
                                -DsMath.Scale(inputReport[Ds4Axis.Ry].Value, GlobalConfiguration.Instance.FlipRY);
                    }
                }
                    break;
            }

            return output;
        }