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
        }
Example #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);
        }
Example #3
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>
        /// <param name="output">The target Xbox data array.</param>
        public void Parse(ScpHidReport inputReport, byte[] output)
        {
            var input = inputReport.RawBytes;

            var serial = IndexToSerial(input[0]);

            for (var index = 0; index < ReportSize; index++)
            {
                output[index] = 0x00;
            }

            output[0] = 0x1C;
            output[4] = (byte)((serial >> 0) & 0xFF);
            output[5] = (byte)((serial >> 8) & 0xFF);
            output[6] = (byte)((serial >> 16) & 0xFF);
            output[7] = (byte)((serial >> 24) & 0xFF);
            output[9] = 0x14;

            var xButton = X360Button.None;

            if (inputReport.PadState == DsState.Connected) // Pad is active
            {
                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;
                    }

                    output[(uint)X360Axis.BT_Lo] = (byte)((uint)xButton >> 0 & 0xFF);
                    output[(uint)X360Axis.BT_Hi] = (byte)((uint)xButton >> 8 & 0xFF);

                    // trigger
                    output[(uint)X360Axis.LT] = inputReport[Ds3Axis.L2].Value;
                    output[(uint)X360Axis.RT] = inputReport[Ds3Axis.R2].Value;

                    if (!DsMath.DeadZone(GlobalConfiguration.Instance.DeadZoneL,
                                         inputReport[Ds3Axis.Lx].Value,
                                         inputReport[Ds3Axis.Ly].Value))
                    // Left Stick DeadZone
                    {
                        var thumbLx = +DsMath.Scale(inputReport[Ds3Axis.Lx].Value, GlobalConfiguration.Instance.FlipLX);
                        var thumbLy = -DsMath.Scale(inputReport[Ds3Axis.Ly].Value, GlobalConfiguration.Instance.FlipLY);

                        output[(uint)X360Axis.LX_Lo] = (byte)((thumbLx >> 0) & 0xFF);       // LX
                        output[(uint)X360Axis.LX_Hi] = (byte)((thumbLx >> 8) & 0xFF);

                        output[(uint)X360Axis.LY_Lo] = (byte)((thumbLy >> 0) & 0xFF);       // LY
                        output[(uint)X360Axis.LY_Hi] = (byte)((thumbLy >> 8) & 0xFF);
                    }

                    if (!DsMath.DeadZone(GlobalConfiguration.Instance.DeadZoneR,
                                         inputReport[Ds3Axis.Rx].Value,
                                         inputReport[Ds3Axis.Ry].Value))
                    // Right Stick DeadZone
                    {
                        var thumbRx = +DsMath.Scale(inputReport[Ds3Axis.Rx].Value, GlobalConfiguration.Instance.FlipRX);
                        var thumbRy = -DsMath.Scale(inputReport[Ds3Axis.Ry].Value, GlobalConfiguration.Instance.FlipRY);

                        output[(uint)X360Axis.RX_Lo] = (byte)((thumbRx >> 0) & 0xFF);       // RX
                        output[(uint)X360Axis.RX_Hi] = (byte)((thumbRx >> 8) & 0xFF);

                        output[(uint)X360Axis.RY_Lo] = (byte)((thumbRy >> 0) & 0xFF);       // RY
                        output[(uint)X360Axis.RY_Hi] = (byte)((thumbRy >> 8) & 0xFF);
                    }
                }
                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;
                    }

                    output[(uint)X360Axis.BT_Lo] = (byte)((uint)xButton >> 0 & 0xFF);
                    output[(uint)X360Axis.BT_Hi] = (byte)((uint)xButton >> 8 & 0xFF);

                    output[(uint)X360Axis.LT] = inputReport[Ds4Axis.L2].Value;
                    output[(uint)X360Axis.RT] = inputReport[Ds4Axis.R2].Value;

                    if (!DsMath.DeadZone(GlobalConfiguration.Instance.DeadZoneL,
                                         inputReport[Ds4Axis.Lx].Value,
                                         inputReport[Ds4Axis.Ly].Value))
                    // Left Stick DeadZone
                    {
                        var thumbLx = +DsMath.Scale(inputReport[Ds4Axis.Lx].Value, GlobalConfiguration.Instance.FlipLX);
                        var thumbLy = -DsMath.Scale(inputReport[Ds4Axis.Ly].Value, GlobalConfiguration.Instance.FlipLY);

                        output[(uint)X360Axis.LX_Lo] = (byte)((thumbLx >> 0) & 0xFF);       // LX
                        output[(uint)X360Axis.LX_Hi] = (byte)((thumbLx >> 8) & 0xFF);

                        output[(uint)X360Axis.LY_Lo] = (byte)((thumbLy >> 0) & 0xFF);       // LY
                        output[(uint)X360Axis.LY_Hi] = (byte)((thumbLy >> 8) & 0xFF);
                    }

                    if (!DsMath.DeadZone(GlobalConfiguration.Instance.DeadZoneR,
                                         inputReport[Ds4Axis.Rx].Value,
                                         inputReport[Ds4Axis.Ry].Value))
                    // Right Stick DeadZone
                    {
                        var thumbRx = +DsMath.Scale(inputReport[Ds4Axis.Rx].Value, GlobalConfiguration.Instance.FlipRX);
                        var thumbRy = -DsMath.Scale(inputReport[Ds4Axis.Ry].Value, GlobalConfiguration.Instance.FlipRY);

                        output[(uint)X360Axis.RX_Lo] = (byte)((thumbRx >> 0) & 0xFF);       // RX
                        output[(uint)X360Axis.RX_Hi] = (byte)((thumbRx >> 8) & 0xFF);

                        output[(uint)X360Axis.RY_Lo] = (byte)((thumbRy >> 0) & 0xFF);       // RY
                        output[(uint)X360Axis.RY_Hi] = (byte)((thumbRy >> 8) & 0xFF);
                    }
                }
                break;
                }
            }
        }