Exemple #1
0
 public void CopyTo(DS4State state)
 {
     state.ReportTimeStamp = ReportTimeStamp;
     state.Square = Square;
     state.Triangle = Triangle;
     state.Circle = Circle;
     state.Cross = Cross;
     state.DpadUp = DpadUp;
     state.DpadDown = DpadDown;
     state.DpadLeft = DpadLeft;
     state.DpadRight = DpadRight;
     state.L1 = L1;
     state.L2 = L2;
     state.L3 = L3;
     state.R1 = R1;
     state.R2 = R2;
     state.R3 = R3;
     state.Share = Share;
     state.Options = Options;
     state.PS = PS;
     state.Touch1 = Touch1;
     state.Touch1Identifier = Touch1Identifier;
     state.Touch2 = Touch2;
     state.Touch2Identifier = Touch2Identifier;
     state.TouchLeft = TouchLeft;
     state.TouchRight = TouchRight;
     state.TouchButton = TouchButton;
     state.TouchPacketCounter = TouchPacketCounter;
     state.LX = LX;
     state.RX = RX;
     state.LY = LY;
     state.RY = RY;
     state.FrameCounter = FrameCounter;
     state.Battery = Battery;
 }
Exemple #2
0
 public DS4State(DS4State state)
 {
     ReportTimeStamp = state.ReportTimeStamp;
     Square = state.Square;
     Triangle = state.Triangle;
     Circle = state.Circle;
     Cross = state.Cross;
     DpadUp = state.DpadUp;
     DpadDown = state.DpadDown;
     DpadLeft = state.DpadLeft;
     DpadRight = state.DpadRight;
     L1 = state.L1;
     L2 = state.L2;
     L3 = state.L3;
     R1 = state.R1;
     R2 = state.R2;
     R3 = state.R3;
     Share = state.Share;
     Options = state.Options;
     PS = state.PS;
     Touch1 = state.Touch1;
     TouchRight = state.TouchRight;
     TouchLeft = state.TouchLeft;
     Touch1Identifier = state.Touch1Identifier;
     Touch2 = state.Touch2;
     Touch2Identifier = state.Touch2Identifier;
     TouchButton = state.TouchButton;
     TouchPacketCounter = state.TouchPacketCounter;
     LX = state.LX;
     RX = state.RX;
     LY = state.LY;
     RY = state.RY;
     FrameCounter = state.FrameCounter;
     Battery = state.Battery;
 }
Exemple #3
0
        public void handleSixaxis(byte[] gyro, byte[] accel, DS4State state)
        {
            //bool touchPadIsDown = sensors.TouchButton;
            /*if (!PacketChanged(data, touchPacketOffset) && touchPadIsDown == lastTouchPadIsDown)
            {
                if (SixAxisUnchanged != null)
                    SixAxisUnchanged(this, EventArgs.Empty);
                return;
            }*/
            /* byte touchID1 = (byte)(data[0 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F);
             byte touchID2 = (byte)(data[4 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F);*/
            int currentX = (Int16)((UInt16)(gyro[0] << 8) | gyro[1]) / 256;
            int currentY = (Int16)((UInt16)(gyro[2] << 8) | gyro[3]) / 256;
            int currentZ = (Int16)((UInt16)(gyro[4] << 8) | gyro[5]) / 256;
            int AccelX = (Int16)((UInt16)(accel[2] << 8) | accel[3]) / 256;
            int AccelY = (Int16)((UInt16)(accel[0] << 8) | accel[1]) / 256;
            int AccelZ = (Int16)((UInt16)(accel[4] << 8) | accel[5]) / 256;
            SixAxisEventArgs args;
            //if (sensors.Touch1 || sensors.Touch2)
            {
                if (SixAxisMoved != null)
                {
                    SixAxis sPrev, now;
                    sPrev = new SixAxis(lastGyroX, lastGyroY, lastGyroZ, lastAX, lastAY, lastAZ);
                    now = new SixAxis(currentX, currentY, currentZ, AccelX, AccelY, AccelZ, sPrev);
                    args = new SixAxisEventArgs(state.ReportTimeStamp, now);
                    SixAxisMoved(this, args);
                }

                lastGyroX = currentX;
                lastGyroY = currentY;
                lastGyroZ = currentZ;
                lastAX = AccelX;
                lastAY = AccelY;
                lastAZ = AccelZ;
            }
            if (AccelX != 0 || AccelY != 0 || AccelZ != 0)
            {
                if (SixAccelMoved != null)
                {
                    SixAxis sPrev, now;
                    sPrev = new SixAxis(lastGyroX, lastGyroY, lastGyroZ, lastAX, lastAY, lastAZ);
                    now = new SixAxis(currentX, currentY, currentZ, AccelX, AccelY, AccelZ, sPrev);
                    args = new SixAxisEventArgs(state.ReportTimeStamp, now);
                    SixAccelMoved(this, args);
                }

                lastGyroX = currentX;
                lastGyroY = currentY;
                lastGyroZ = currentZ;
                lastAX = AccelX;
                lastAY = AccelY;
                lastAZ = AccelZ;
            }
        }
Exemple #4
0
        private void StateMonitor()
        {
            DS4State state = new DS4State();
            while (true)
            {
                if (Controller != null && !Suspended)
                {
                    try
                    {
                        // Update controller state
                        state = Controller.getCurrentState();
                        var lState = Controller.getPreviousState();

                        // Face Buttons
                        if (state.Circle && lState.Circle)
                            DoButtonDown(DS4Button.Circle);
                        if (!state.Circle && buttonStates[(int)DS4Button.Circle])
                            DoButtonUp(DS4Button.Circle);

                        if (state.Cross && lState.Cross)
                            DoButtonDown(DS4Button.Cross);
                        if (!state.Cross && buttonStates[(int)DS4Button.Cross])
                            DoButtonUp(DS4Button.Cross);

                        if (state.Triangle && lState.Triangle)
                            DoButtonDown(DS4Button.Triangle);
                        if (!state.Triangle && buttonStates[(int)DS4Button.Triangle])
                            DoButtonUp(DS4Button.Triangle);

                        if (state.Square && lState.Square)
                            DoButtonDown(DS4Button.Square);
                        if (!state.Square && buttonStates[(int)DS4Button.Square])
                            DoButtonUp(DS4Button.Square);

                        // D-Pad Directions
                        if (state.DpadDown && lState.DpadDown)
                            DoButtonDown(DS4Button.DpadDown);
                        if (!state.DpadDown && buttonStates[(int)DS4Button.DpadDown])
                            DoButtonUp(DS4Button.DpadDown);

                        if (state.DpadUp && lState.DpadUp)
                            DoButtonDown(DS4Button.DpadUp);
                        if (!state.DpadUp && buttonStates[(int)DS4Button.DpadUp])
                            DoButtonUp(DS4Button.DpadUp);

                        if (state.DpadLeft && lState.DpadLeft)
                            DoButtonDown(DS4Button.DpadLeft);
                        if (!state.DpadLeft && buttonStates[(int)DS4Button.DpadLeft])
                            DoButtonUp(DS4Button.DpadLeft);

                        if (state.DpadRight && lState.DpadRight)
                            DoButtonDown(DS4Button.DpadRight);
                        if (!state.DpadRight && buttonStates[(int)DS4Button.DpadRight])
                            DoButtonUp(DS4Button.DpadRight);

                        // L1/R1 triggers
                        if (state.L1 && lState.L1)
                            DoButtonDown(DS4Button.L1);
                        if (!state.L1 && buttonStates[(int)DS4Button.L1])
                            DoButtonUp(DS4Button.L1);

                        if (state.R1 && lState.R1)
                            DoButtonDown(DS4Button.R1);
                        if (!state.R1 && buttonStates[(int)DS4Button.R1])
                            DoButtonUp(DS4Button.R1);

                        // Handle L2/R2 triggers as buttons
                        if (TriggerMode == TriggerMode.Buttons)
                        {
                            if (AxisToFloat(state.L2) > (TriggerSensitivity.L2 / 100))
                                DoButtonDown(DS4Button.L2);
                            if (!(AxisToFloat(state.L2) > (TriggerSensitivity.L2 / 100)) && buttonStates[(int)DS4Button.L2])
                                DoButtonUp(DS4Button.L2);
                            if (AxisToFloat(state.R2) > (TriggerSensitivity.R2 / 100))
                                DoButtonDown(DS4Button.R2);
                            if (!(AxisToFloat(state.R2) > (TriggerSensitivity.R2 / 100)) && buttonStates[(int)DS4Button.R2])
                                DoButtonUp(DS4Button.R2);
                        }

                        // L3/R3
                        if (state.L3 && lState.L3)
                            DoButtonDown(DS4Button.L3);
                        if (!state.L3 && buttonStates[(int)DS4Button.L3])
                            DoButtonUp(DS4Button.L3);

                        if (state.R3 && lState.R3)
                            DoButtonDown(DS4Button.R3);
                        if (!state.R3 && buttonStates[(int)DS4Button.R3])
                            DoButtonUp(DS4Button.R3);

                        // Share, Options, PS
                        if (state.Share && lState.Share)
                            DoButtonDown(DS4Button.Share);
                        if (!state.Share && buttonStates[(int)DS4Button.Share])
                            DoButtonUp(DS4Button.Share);

                        if (state.Options && lState.Options)
                            DoButtonDown(DS4Button.Options);
                        if (!state.Options && buttonStates[(int)DS4Button.Options])
                            DoButtonUp(DS4Button.Options);

                        if (state.PS && lState.PS)
                            DoButtonDown(DS4Button.PS);
                        if (!state.PS && buttonStates[(int)DS4Button.PS])
                            DoButtonUp(DS4Button.PS);

                        bool didTouch = false;

                        if (Controller.Touchpad.lastTouchPadY1 < 200 && state.TouchButton)
                        { DoButtonDown(DS4Button.TouchUpper); didTouch = true; }
                        if (!state.TouchButton && buttonStates[(int)DS4Button.TouchUpper])
                        { DoButtonUp(DS4Button.TouchUpper); didTouch = true; }

                        if (!didTouch)
                        {
                            // Touchpad click left/right
                            if (state.TouchLeft && state.TouchButton)
                                DoButtonDown(DS4Button.TouchLeft);
                            if (!state.TouchButton && buttonStates[(int)DS4Button.TouchLeft])
                                DoButtonUp(DS4Button.TouchLeft);

                            if (state.TouchRight && state.TouchButton)
                                DoButtonDown(DS4Button.TouchRight);
                            if (!state.TouchButton && buttonStates[(int)DS4Button.TouchRight])
                                DoButtonUp(DS4Button.TouchRight);
                        }

                        // Process axis threshold states
                        if (EvaluateThreshold(GetStickAxis(state.LX), axisThresholds[(int)DS4Axis.Lx]) && !axisStates[(int)DS4Axis.Lx])
                        {
                            AxisThresholdReached(DS4Axis.Lx);
                            axisStates[(int)DS4Axis.Lx] = true;
                        }

                        if (!EvaluateThreshold(GetStickAxis(state.LX), axisThresholds[(int)DS4Axis.Lx]) && axisStates[(int)DS4Axis.Lx])
                        {
                            AxisThresholdDropped(DS4Axis.Lx);
                            axisStates[(int)DS4Axis.Lx] = false;
                        }

                        if (EvaluateThreshold(GetStickAxis(state.LY), axisThresholds[(int)DS4Axis.Ly]) && !axisStates[(int)DS4Axis.Ly])
                        {
                            AxisThresholdReached(DS4Axis.Ly);
                            axisStates[(int)DS4Axis.Ly] = true;
                        }

                        if (!EvaluateThreshold(GetStickAxis(state.LY), axisThresholds[(int)DS4Axis.Ly]) && axisStates[(int)DS4Axis.Ly])
                        {
                            AxisThresholdDropped(DS4Axis.Ly);
                            axisStates[(int)DS4Axis.Ly] = false;
                        }

                        if (EvaluateThreshold(GetStickAxis(state.RX), axisThresholds[(int)DS4Axis.Rx]) && !axisStates[(int)DS4Axis.Rx])
                        {
                            AxisThresholdReached(DS4Axis.Rx);
                            axisStates[(int)DS4Axis.Rx] = true;
                        }

                        if (!EvaluateThreshold(GetStickAxis(state.RX), axisThresholds[(int)DS4Axis.Rx]) && axisStates[(int)DS4Axis.Rx])
                        {
                            AxisThresholdDropped(DS4Axis.Rx);
                            axisStates[(int)DS4Axis.Rx] = false;
                        }

                        if (EvaluateThreshold(GetStickAxis(state.RY), axisThresholds[(int)DS4Axis.Ry]) && !axisStates[(int)DS4Axis.Ry])
                        {
                            AxisThresholdReached(DS4Axis.Ry);
                            axisStates[(int)DS4Axis.Ry] = true;
                        }

                        if (!EvaluateThreshold(GetStickAxis(state.RY), axisThresholds[(int)DS4Axis.Ry]) && axisStates[(int)DS4Axis.Ry])
                        {
                            AxisThresholdDropped(DS4Axis.Ry);
                            axisStates[(int)DS4Axis.Ry] = false;
                        }
                    }
                    catch
                    {
                        // the controller was probably removed
                    }
                }

                Thread.Sleep(5);
            }
        }
Exemple #5
0
        public override void ConvertAndSendReport(DS4State state, int device)
        {
            Xbox360Buttons tempButtons = 0;

            unchecked
            {
                if (state.Share)
                {
                    tempButtons |= Xbox360Buttons.Back;
                }
                if (state.L3)
                {
                    tempButtons |= Xbox360Buttons.LeftThumb;
                }
                if (state.R3)
                {
                    tempButtons |= Xbox360Buttons.RightThumb;
                }
                if (state.Options)
                {
                    tempButtons |= Xbox360Buttons.Start;
                }

                if (state.DpadUp)
                {
                    tempButtons |= Xbox360Buttons.Up;
                }
                if (state.DpadRight)
                {
                    tempButtons |= Xbox360Buttons.Right;
                }
                if (state.DpadDown)
                {
                    tempButtons |= Xbox360Buttons.Down;
                }
                if (state.DpadLeft)
                {
                    tempButtons |= Xbox360Buttons.Left;
                }

                if (state.L1)
                {
                    tempButtons |= Xbox360Buttons.LeftShoulder;
                }
                if (state.R1)
                {
                    tempButtons |= Xbox360Buttons.RightShoulder;
                }

                if (state.Triangle)
                {
                    tempButtons |= Xbox360Buttons.Y;
                }
                if (state.Circle)
                {
                    tempButtons |= Xbox360Buttons.B;
                }
                if (state.Cross)
                {
                    tempButtons |= Xbox360Buttons.A;
                }
                if (state.Square)
                {
                    tempButtons |= Xbox360Buttons.X;
                }
                if (state.PS)
                {
                    tempButtons |= Xbox360Buttons.Guide;
                }

                report.SetButtons(tempButtons);
            }

            report.LeftTrigger  = state.L2;
            report.RightTrigger = state.R2;

            SASteeringWheelEmulationAxisType steeringWheelMappedAxis = Global.GetSASteeringWheelEmulationAxis(device);

            switch (steeringWheelMappedAxis)
            {
            case SASteeringWheelEmulationAxisType.None:
                report.LeftThumbX  = AxisScale(state.LX, false);
                report.LeftThumbY  = AxisScale(state.LY, true);
                report.RightThumbX = AxisScale(state.RX, false);
                report.RightThumbY = AxisScale(state.RY, true);
                break;

            case SASteeringWheelEmulationAxisType.LX:
                report.LeftThumbX  = (short)state.SASteeringWheelEmulationUnit;
                report.LeftThumbY  = AxisScale(state.LY, true);
                report.RightThumbX = AxisScale(state.RX, false);
                report.RightThumbY = AxisScale(state.RY, true);
                break;

            case SASteeringWheelEmulationAxisType.LY:
                report.LeftThumbX  = AxisScale(state.LX, false);
                report.LeftThumbY  = (short)state.SASteeringWheelEmulationUnit;
                report.RightThumbX = AxisScale(state.RX, false);
                report.RightThumbY = AxisScale(state.RY, true);
                break;

            case SASteeringWheelEmulationAxisType.RX:
                report.LeftThumbX  = AxisScale(state.LX, false);
                report.LeftThumbY  = AxisScale(state.LY, true);
                report.RightThumbX = (short)state.SASteeringWheelEmulationUnit;
                report.RightThumbY = AxisScale(state.RY, true);
                break;

            case SASteeringWheelEmulationAxisType.RY:
                report.LeftThumbX  = AxisScale(state.LX, false);
                report.LeftThumbY  = AxisScale(state.LY, true);
                report.RightThumbX = AxisScale(state.RX, false);
                report.RightThumbY = (short)state.SASteeringWheelEmulationUnit;
                break;

            case SASteeringWheelEmulationAxisType.L2R2:
                report.LeftTrigger = report.RightTrigger = 0;
                if (state.SASteeringWheelEmulationUnit >= 0)
                {
                    report.LeftTrigger = (byte)state.SASteeringWheelEmulationUnit;
                }
                else
                {
                    report.RightTrigger = (byte)state.SASteeringWheelEmulationUnit;
                }
                goto case SASteeringWheelEmulationAxisType.None;

            case SASteeringWheelEmulationAxisType.VJoy1X:
            case SASteeringWheelEmulationAxisType.VJoy2X:
                VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, VJoyFeeder.HID_USAGES.HID_USAGE_X);
                goto case SASteeringWheelEmulationAxisType.None;

            case SASteeringWheelEmulationAxisType.VJoy1Y:
            case SASteeringWheelEmulationAxisType.VJoy2Y:
                VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, VJoyFeeder.HID_USAGES.HID_USAGE_Y);
                goto case SASteeringWheelEmulationAxisType.None;

            case SASteeringWheelEmulationAxisType.VJoy1Z:
            case SASteeringWheelEmulationAxisType.VJoy2Z:
                VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, VJoyFeeder.HID_USAGES.HID_USAGE_Z);
                goto case SASteeringWheelEmulationAxisType.None;

            default:
                // Should never come here but just in case use the NONE case as default handler....
                goto case SASteeringWheelEmulationAxisType.None;
            }

            Controller.SendReport(report);
        }
Exemple #6
0
        public override void ConvertandSendReport(DS4State state, int device)
        {
            if (!connected)
            {
                return;
            }

            DualShock4Buttons        tempButtons = 0;
            DualShock4DPadValues     tempDPad    = DualShock4DPadValues.None;
            DualShock4SpecialButtons tempSpecial = 0;

            unchecked
            {
                if (state.Share)
                {
                    tempButtons |= DualShock4Buttons.Share;
                }
                if (state.L3)
                {
                    tempButtons |= DualShock4Buttons.ThumbLeft;
                }
                if (state.R3)
                {
                    tempButtons |= DualShock4Buttons.ThumbRight;
                }
                if (state.Options)
                {
                    tempButtons |= DualShock4Buttons.Options;
                }

                if (state.DpadUp && state.DpadRight)
                {
                    tempDPad = DualShock4DPadValues.Northeast;
                }
                else if (state.DpadUp && state.DpadLeft)
                {
                    tempDPad = DualShock4DPadValues.Northwest;
                }
                else if (state.DpadUp)
                {
                    tempDPad = DualShock4DPadValues.North;
                }
                else if (state.DpadRight && state.DpadDown)
                {
                    tempDPad = DualShock4DPadValues.Southeast;
                }
                else if (state.DpadRight)
                {
                    tempDPad = DualShock4DPadValues.East;
                }
                else if (state.DpadDown && state.DpadLeft)
                {
                    tempDPad = DualShock4DPadValues.Southwest;
                }
                else if (state.DpadDown)
                {
                    tempDPad = DualShock4DPadValues.South;
                }
                else if (state.DpadLeft)
                {
                    tempDPad = DualShock4DPadValues.West;
                }

                /*if (state.DpadUp) tempDPad = (state.DpadRight) ? DualShock4DPadValues.Northeast : DualShock4DPadValues.North;
                 * if (state.DpadRight) tempDPad = (state.DpadDown) ? DualShock4DPadValues.Southeast : DualShock4DPadValues.East;
                 * if (state.DpadDown) tempDPad = (state.DpadLeft) ? DualShock4DPadValues.Southwest : DualShock4DPadValues.South;
                 * if (state.DpadLeft) tempDPad = (state.DpadUp) ? DualShock4DPadValues.Northwest : DualShock4DPadValues.West;
                 */

                if (state.L1)
                {
                    tempButtons |= DualShock4Buttons.ShoulderLeft;
                }
                if (state.R1)
                {
                    tempButtons |= DualShock4Buttons.ShoulderRight;
                }
                //if (state.L2Btn) tempButtons |= DualShock4Buttons.TriggerLeft;
                //if (state.R2Btn) tempButtons |= DualShock4Buttons.TriggerRight;
                if (state.L2 > 0)
                {
                    tempButtons |= DualShock4Buttons.TriggerLeft;
                }
                if (state.R2 > 0)
                {
                    tempButtons |= DualShock4Buttons.TriggerRight;
                }

                if (state.Triangle)
                {
                    tempButtons |= DualShock4Buttons.Triangle;
                }
                if (state.Circle)
                {
                    tempButtons |= DualShock4Buttons.Circle;
                }
                if (state.Cross)
                {
                    tempButtons |= DualShock4Buttons.Cross;
                }
                if (state.Square)
                {
                    tempButtons |= DualShock4Buttons.Square;
                }
                if (state.PS)
                {
                    tempSpecial |= DualShock4SpecialButtons.Ps;
                }
                if (state.TouchButton)
                {
                    tempSpecial |= DualShock4SpecialButtons.Touchpad;
                }
                //report.SetButtonsFull(tempButtons);
                report.Buttons = (ushort)tempButtons;
                report.SetDPad(tempDPad);
                report.SpecialButtons = (byte)tempSpecial;
            }


            report.LeftTrigger  = state.L2;
            report.RightTrigger = state.R2;

            SASteeringWheelEmulationAxisType steeringWheelMappedAxis = Global.GetSASteeringWheelEmulationAxis(device);

            switch (steeringWheelMappedAxis)
            {
            case SASteeringWheelEmulationAxisType.None:
                report.LeftThumbX  = state.LX;
                report.LeftThumbY  = state.LY;
                report.RightThumbX = state.RX;
                report.RightThumbY = state.RY;
                break;

            case SASteeringWheelEmulationAxisType.LX:
                report.LeftThumbX  = (byte)state.SASteeringWheelEmulationUnit;
                report.LeftThumbY  = state.LY;
                report.RightThumbX = state.RX;
                report.RightThumbY = state.RY;
                break;

            case SASteeringWheelEmulationAxisType.LY:
                report.LeftThumbX  = state.LX;
                report.LeftThumbY  = (byte)state.SASteeringWheelEmulationUnit;
                report.RightThumbX = state.RX;
                report.RightThumbY = state.RY;
                break;

            case SASteeringWheelEmulationAxisType.RX:
                report.LeftThumbX  = state.LX;
                report.LeftThumbY  = state.LY;
                report.RightThumbX = (byte)state.SASteeringWheelEmulationUnit;
                report.RightThumbY = state.RY;
                break;

            case SASteeringWheelEmulationAxisType.RY:
                report.LeftThumbX  = state.LX;
                report.LeftThumbY  = state.LY;
                report.RightThumbX = state.RX;
                report.RightThumbY = (byte)state.SASteeringWheelEmulationUnit;
                break;

            case SASteeringWheelEmulationAxisType.L2R2:
                report.LeftTrigger = report.RightTrigger = 0;
                if (state.SASteeringWheelEmulationUnit >= 0)
                {
                    report.LeftTrigger = (Byte)state.SASteeringWheelEmulationUnit;
                }
                else
                {
                    report.RightTrigger = (Byte)state.SASteeringWheelEmulationUnit;
                }
                goto case SASteeringWheelEmulationAxisType.None;

            case SASteeringWheelEmulationAxisType.VJoy1X:
            case SASteeringWheelEmulationAxisType.VJoy2X:
                DS4Windows.VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, DS4Windows.VJoyFeeder.HID_USAGES.HID_USAGE_X);
                goto case SASteeringWheelEmulationAxisType.None;

            case SASteeringWheelEmulationAxisType.VJoy1Y:
            case SASteeringWheelEmulationAxisType.VJoy2Y:
                DS4Windows.VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, DS4Windows.VJoyFeeder.HID_USAGES.HID_USAGE_Y);
                goto case SASteeringWheelEmulationAxisType.None;

            case SASteeringWheelEmulationAxisType.VJoy1Z:
            case SASteeringWheelEmulationAxisType.VJoy2Z:
                DS4Windows.VJoyFeeder.vJoyFeeder.FeedAxisValue(state.SASteeringWheelEmulationUnit, ((((uint)steeringWheelMappedAxis) - ((uint)SASteeringWheelEmulationAxisType.VJoy1X)) / 3) + 1, DS4Windows.VJoyFeeder.HID_USAGES.HID_USAGE_Z);
                goto case SASteeringWheelEmulationAxisType.None;

            default:
                // Should never come here but just in case use the NONE case as default handler....
                goto case SASteeringWheelEmulationAxisType.None;
            }

            cont.SendReport(report);
        }
Exemple #7
0
        public virtual void touchesEnded(object sender, TouchpadEventArgs arg)
        {
            s          = dev.getCurrentStateRef();
            slideright = slideleft = false;
            swipeUp    = swipeDown = swipeLeft = swipeRight = false;
            swipeUpB   = swipeDownB = swipeLeftB = swipeRightB = 0;
            byte tapSensitivity = Global.getTapSensitivity(deviceNum);

            if (tapSensitivity != 0 && !Global.getUseTPforControls(deviceNum))
            {
                if (secondtouchbegin)
                {
                    tappedOnce       = false;
                    secondtouchbegin = false;
                }

                DateTime test = arg.timeStamp;
                if (test <= (pastTime + TimeSpan.FromMilliseconds((double)tapSensitivity * 2)) && !arg.touchButtonPressed && !tappedOnce)
                {
                    if (Math.Abs(firstTouch.hwX - arg.touches[0].hwX) < 10 && Math.Abs(firstTouch.hwY - arg.touches[0].hwY) < 10)
                    {
                        if (Global.getDoubleTap(deviceNum))
                        {
                            tappedOnce = true;
                            firstTap   = arg.timeStamp;
                            TimeofEnd  = DateTime.Now; //since arg can't be used in synthesizeMouseButtons
                        }
                        else
                        {
                            Mapping.MapClick(deviceNum, Mapping.Click.Left); //this way no delay if disabled
                        }
                    }
                }
            }
            else
            {
                if (Global.getUseTPforControls(deviceNum) == false)
                {
                    int[] disArray = Global.getTouchDisInvertTriggers(deviceNum);
                    tempBool = true;
                    for (int i = 0, arlen = disArray.Length; tempBool && i < arlen; i++)
                    {
                        if (getDS4ControlsByName(disArray[i]) == false)
                        {
                            tempBool = false;
                        }
                    }

                    if (Global.getTrackballMode(deviceNum))
                    {
                        if (!trackballActive)
                        {
                            double currentWeight = 1.0;
                            double finalWeight = 0.0;
                            double x_out = 0.0, y_out = 0.0;
                            int    idx = -1;
                            for (int i = 0; i < TRACKBALL_BUFFER_LEN && idx != trackballBufferHead; i++)
                            {
                                idx            = (trackballBufferTail - i - 1 + TRACKBALL_BUFFER_LEN) % TRACKBALL_BUFFER_LEN;
                                x_out         += trackballXBuffer[idx] * currentWeight;
                                y_out         += trackballYBuffer[idx] * currentWeight;
                                finalWeight   += currentWeight;
                                currentWeight *= 1.0;
                            }

                            x_out        /= finalWeight;
                            trackballXVel = x_out;
                            y_out        /= finalWeight;
                            trackballYVel = y_out;

                            trackballActive = true;
                        }

                        double tempAngle = Math.Atan2(-trackballYVel, trackballXVel);
                        double normX     = Math.Abs(Math.Cos(tempAngle));
                        double normY     = Math.Abs(Math.Sin(tempAngle));
                        int    signX     = Math.Sign(trackballXVel);
                        int    signY     = Math.Sign(trackballYVel);

                        double trackXvDecay = Math.Min(Math.Abs(trackballXVel), trackballAccel * s.elapsedTime * normX);
                        double trackYvDecay = Math.Min(Math.Abs(trackballYVel), trackballAccel * s.elapsedTime * normY);
                        double xVNew        = trackballXVel - (trackXvDecay * signX);
                        double yVNew        = trackballYVel - (trackYvDecay * signY);
                        double xMotion      = (xVNew * s.elapsedTime) / TRACKBALL_SCALE;
                        double yMotion      = (yVNew * s.elapsedTime) / TRACKBALL_SCALE;
                        if (xMotion != 0.0)
                        {
                            xMotion += trackballDXRemain;
                        }
                        else
                        {
                            trackballDXRemain = 0.0;
                        }

                        int dx = (int)xMotion;
                        trackballDXRemain = xMotion - dx;

                        if (yMotion != 0.0)
                        {
                            yMotion += trackballDYRemain;
                        }
                        else
                        {
                            trackballDYRemain = 0.0;
                        }

                        int dy = (int)yMotion;
                        trackballDYRemain = yMotion - dy;

                        trackballXVel = xVNew;
                        trackballYVel = yVNew;

                        if (dx == 0 && dy == 0)
                        {
                            trackballActive = false;
                        }
                        else
                        {
                            cursor.TouchMoveCursor(dx, dy, tempBool);
                        }
                    }
                }
            }

            synthesizeMouseButtons();
        }
Exemple #8
0
        public virtual void touchUnchanged(object sender, EventArgs unused)
        {
            s = dev.getCurrentStateRef();

            if (trackballActive)
            {
                if (Global.getUseTPforControls(deviceNum) == false)
                {
                    int[] disArray = Global.getTouchDisInvertTriggers(deviceNum);
                    tempBool = true;
                    for (int i = 0, arlen = disArray.Length; tempBool && i < arlen; i++)
                    {
                        if (getDS4ControlsByName(disArray[i]) == false)
                        {
                            tempBool = false;
                        }
                    }

                    double tempAngle    = Math.Atan2(-trackballYVel, trackballXVel);
                    double normX        = Math.Abs(Math.Cos(tempAngle));
                    double normY        = Math.Abs(Math.Sin(tempAngle));
                    int    signX        = Math.Sign(trackballXVel);
                    int    signY        = Math.Sign(trackballYVel);
                    double trackXvDecay = Math.Min(Math.Abs(trackballXVel), trackballAccel * s.elapsedTime * normX);
                    double trackYvDecay = Math.Min(Math.Abs(trackballYVel), trackballAccel * s.elapsedTime * normY);
                    double xVNew        = trackballXVel - (trackXvDecay * signX);
                    double yVNew        = trackballYVel - (trackYvDecay * signY);
                    double xMotion      = (xVNew * s.elapsedTime) / TRACKBALL_SCALE;
                    double yMotion      = (yVNew * s.elapsedTime) / TRACKBALL_SCALE;
                    if (xMotion != 0.0)
                    {
                        xMotion += trackballDXRemain;
                    }
                    else
                    {
                        trackballDXRemain = 0.0;
                    }

                    int dx = (int)xMotion;
                    trackballDXRemain = xMotion - dx;

                    if (yMotion != 0.0)
                    {
                        yMotion += trackballDYRemain;
                    }
                    else
                    {
                        trackballDYRemain = 0.0;
                    }

                    int dy = (int)yMotion;
                    trackballDYRemain = yMotion - dy;

                    trackballXVel = xVNew;
                    trackballYVel = yVNew;

                    if (dx == 0 && dy == 0)
                    {
                        trackballActive = false;
                    }
                    else
                    {
                        cursor.TouchMoveCursor(dx, dy, tempBool);
                    }
                }
            }

            if (s.TouchButton)
            {
                synthesizeMouseButtons();
            }
        }
Exemple #9
0
 public void getCurrentState(DS4State state)
 {
     cState.CopyTo(state);
 }
Exemple #10
0
        public virtual void touchesMoved(object sender, TouchpadEventArgs arg)
        {
            s = dev.getCurrentStateRef();

            if (Global.getUseTPforControls(deviceNum) == false)
            {
                int[] disArray = Global.getTouchDisInvertTriggers(deviceNum);
                tempBool = true;
                for (int i = 0, arlen = disArray.Length; tempBool && i < arlen; i++)
                {
                    if (getDS4ControlsByName(disArray[i]) == false)
                    {
                        tempBool = false;
                    }
                }

                if (Global.getTrackballMode(deviceNum))
                {
                    int iIndex = trackballBufferTail;
                    trackballXBuffer[iIndex] = (arg.touches[0].deltaX * TRACKBALL_SCALE) / dev.getCurrentStateRef().elapsedTime;
                    trackballYBuffer[iIndex] = (arg.touches[0].deltaY * TRACKBALL_SCALE) / dev.getCurrentStateRef().elapsedTime;
                    trackballBufferTail      = (iIndex + 1) % TRACKBALL_BUFFER_LEN;
                    if (trackballBufferHead == trackballBufferTail)
                    {
                        trackballBufferHead = (trackballBufferHead + 1) % TRACKBALL_BUFFER_LEN;
                    }
                }

                cursor.touchesMoved(arg, dragging || dragging2, tempBool);
                wheel.touchesMoved(arg, dragging || dragging2);
            }
            else
            {
                if (!(swipeUp || swipeDown || swipeLeft || swipeRight) && arg.touches.Length == 1)
                {
                    if (arg.touches[0].hwX - firstTouch.hwX > 400)
                    {
                        swipeRight = true;
                    }
                    if (arg.touches[0].hwX - firstTouch.hwX < -400)
                    {
                        swipeLeft = true;
                    }
                    if (arg.touches[0].hwY - firstTouch.hwY > 300)
                    {
                        swipeDown = true;
                    }
                    if (arg.touches[0].hwY - firstTouch.hwY < -300)
                    {
                        swipeUp = true;
                    }
                }

                swipeUpB    = (byte)Math.Min(255, Math.Max(0, (firstTouch.hwY - arg.touches[0].hwY) * 1.5f));
                swipeDownB  = (byte)Math.Min(255, Math.Max(0, (arg.touches[0].hwY - firstTouch.hwY) * 1.5f));
                swipeLeftB  = (byte)Math.Min(255, Math.Max(0, firstTouch.hwX - arg.touches[0].hwX));
                swipeRightB = (byte)Math.Min(255, Math.Max(0, arg.touches[0].hwX - firstTouch.hwX));
            }

            if (Math.Abs(firstTouch.hwY - arg.touches[0].hwY) < 50 && arg.touches.Length == 2)
            {
                if (arg.touches[0].hwX - firstTouch.hwX > 200 && !slideleft)
                {
                    slideright = true;
                }
                else if (firstTouch.hwX - arg.touches[0].hwX > 200 && !slideright)
                {
                    slideleft = true;
                }
            }

            synthesizeMouseButtons();
        }
Exemple #11
0
        public override void ConvertandSendReport(DS4State state, int device)
        {
            DualShock4Buttons        tempButtons = 0;
            DualShock4DPadValues     tempDPad    = DualShock4DPadValues.None;
            DualShock4SpecialButtons tempSpecial = 0;

            unchecked
            {
                if (state.Share)
                {
                    tempButtons |= DualShock4Buttons.Share;
                }
                if (state.L3)
                {
                    tempButtons |= DualShock4Buttons.ThumbLeft;
                }
                if (state.R3)
                {
                    tempButtons |= DualShock4Buttons.ThumbRight;
                }
                if (state.Options)
                {
                    tempButtons |= DualShock4Buttons.Options;
                }

                if (state.DpadUp && state.DpadRight)
                {
                    tempDPad = DualShock4DPadValues.Northeast;
                }
                else if (state.DpadUp && state.DpadLeft)
                {
                    tempDPad = DualShock4DPadValues.Northwest;
                }
                else if (state.DpadUp)
                {
                    tempDPad = DualShock4DPadValues.North;
                }
                else if (state.DpadRight && state.DpadDown)
                {
                    tempDPad = DualShock4DPadValues.Southeast;
                }
                else if (state.DpadRight)
                {
                    tempDPad = DualShock4DPadValues.East;
                }
                else if (state.DpadDown && state.DpadLeft)
                {
                    tempDPad = DualShock4DPadValues.Southwest;
                }
                else if (state.DpadDown)
                {
                    tempDPad = DualShock4DPadValues.South;
                }
                else if (state.DpadLeft)
                {
                    tempDPad = DualShock4DPadValues.West;
                }

                /*if (state.DpadUp) tempDPad = (state.DpadRight) ? DualShock4DPadValues.Northeast : DualShock4DPadValues.North;
                 * if (state.DpadRight) tempDPad = (state.DpadDown) ? DualShock4DPadValues.Southeast : DualShock4DPadValues.East;
                 * if (state.DpadDown) tempDPad = (state.DpadLeft) ? DualShock4DPadValues.Southwest : DualShock4DPadValues.South;
                 * if (state.DpadLeft) tempDPad = (state.DpadUp) ? DualShock4DPadValues.Northwest : DualShock4DPadValues.West;
                 */

                if (state.L1)
                {
                    tempButtons |= DualShock4Buttons.ShoulderLeft;
                }
                if (state.R1)
                {
                    tempButtons |= DualShock4Buttons.ShoulderRight;
                }
                //if (state.L2Btn) tempButtons |= DualShock4Buttons.TriggerLeft;
                //if (state.R2Btn) tempButtons |= DualShock4Buttons.TriggerRight;
                if (state.L2 > 0)
                {
                    tempButtons |= DualShock4Buttons.TriggerLeft;
                }
                if (state.R2 > 0)
                {
                    tempButtons |= DualShock4Buttons.TriggerRight;
                }

                if (state.Triangle)
                {
                    tempButtons |= DualShock4Buttons.Triangle;
                }
                if (state.Circle)
                {
                    tempButtons |= DualShock4Buttons.Circle;
                }
                if (state.Cross)
                {
                    tempButtons |= DualShock4Buttons.Cross;
                }
                if (state.Square)
                {
                    tempButtons |= DualShock4Buttons.Square;
                }
                if (state.PS)
                {
                    tempSpecial |= DualShock4SpecialButtons.Ps;
                }
                if (state.TouchButton)
                {
                    tempSpecial |= DualShock4SpecialButtons.Touchpad;
                }
                //report.SetButtonsFull(tempButtons);
                report.Buttons = (ushort)tempButtons;
                report.SetDPad(tempDPad);
                report.SpecialButtons = (byte)tempSpecial;
            }


            report.LeftTrigger  = state.L2;
            report.RightTrigger = state.R2;
            report.LeftThumbX   = state.LX;
            report.LeftThumbY   = state.LY;
            report.RightThumbX  = state.RX;
            report.RightThumbY  = state.RY;

            cont.SendReport(report);
        }
Exemple #12
0
        // Called every time a new input report has arrived
        protected virtual void On_Report(object sender, EventArgs e)
        {
            DS4Device device = (DS4Device)sender;

            int ind = -1;

            for (int i = 0, arlength = DS4_CONTROLLER_COUNT; ind == -1 && i < arlength; i++)
            {
                DS4Device tempDev = DS4Controllers[i];
                if (tempDev != null && device == tempDev)
                {
                    ind = i;
                }
            }

            if (ind != -1)
            {
                if (getFlushHIDQueue(ind))
                {
                    device.FlushHID();
                }

                string devError = tempStrings[ind] = device.error;
                if (!string.IsNullOrEmpty(devError))
                {
                    device.getUiContext()?.Post(new SendOrPostCallback(delegate(object state)
                    {
                        LogDebug(devError);
                    }), null);
                }

                if (inWarnMonitor[ind])
                {
                    int flashWhenLateAt = getFlashWhenLateAt();
                    if (!lag[ind] && device.Latency >= flashWhenLateAt)
                    {
                        lag[ind] = true;
                        device.getUiContext()?.Post(new SendOrPostCallback(delegate(object state)
                        {
                            LagFlashWarning(ind, true);
                        }), null);
                    }
                    else if (lag[ind] && device.Latency < flashWhenLateAt)
                    {
                        lag[ind] = false;
                        device.getUiContext()?.Post(new SendOrPostCallback(delegate(object state)
                        {
                            LagFlashWarning(ind, false);
                        }), null);
                    }
                }
                else
                {
                    if (DateTime.UtcNow - device.firstActive > TimeSpan.FromSeconds(5))
                    {
                        inWarnMonitor[ind] = true;
                    }
                }

                device.getCurrentState(CurrentState[ind]);
                DS4State cState = CurrentState[ind];
                DS4State pState = device.getPreviousStateRef();
                //device.getPreviousState(PreviousState[ind]);
                //DS4State pState = PreviousState[ind];

                if (device.firstReport && device.IsAlive())
                {
                    device.firstReport = false;
                    device.getUiContext()?.Post(new SendOrPostCallback(delegate(object state)
                    {
                        OnDeviceStatusChanged(this, ind);
                    }), null);
                }
                else if (pState.Battery != cState.Battery || device.oldCharging != device.isCharging())
                {
                    byte tempBattery  = currentBattery[ind] = cState.Battery;
                    bool tempCharging = charging[ind] = device.isCharging();
                    device.getUiContext()?.Post(new SendOrPostCallback(delegate(object state)
                    {
                        OnBatteryStatusChange(this, ind, tempBattery, tempCharging);
                    }), null);
                }

                if (getEnableTouchToggle(ind))
                {
                    CheckForTouchToggle(ind, cState, pState);
                }

                cState = Mapping.SetCurveAndDeadzone(ind, cState, TempState[ind]);

                if (!recordingMacro && (!string.IsNullOrEmpty(tempprofilename[ind]) ||
                                        containsCustomAction(ind) || containsCustomExtras(ind) ||
                                        getProfileActionCount(ind) > 0))
                {
                    Mapping.MapCustom(ind, cState, MappedState[ind], ExposedState[ind], touchPad[ind], this);
                    cState = MappedState[ind];
                }

                if (!useDInputOnly[ind])
                {
                    x360Bus.Parse(cState, processingData[ind].Report, ind);
                    // We push the translated Xinput state, and simultaneously we
                    // pull back any possible rumble data coming from Xinput consumers.
                    if (x360Bus.Report(processingData[ind].Report, processingData[ind].Rumble))
                    {
                        byte Big   = processingData[ind].Rumble[3];
                        byte Small = processingData[ind].Rumble[4];

                        if (processingData[ind].Rumble[1] == 0x08)
                        {
                            setRumble(Big, Small, ind);
                        }
                    }
                }

                // Output any synthetic events.
                Mapping.Commit(ind);

                // Update the GUI/whatever.
                DS4LightBar.updateLightBar(device, ind);
            }
        }
Exemple #13
0
        private bool ReportToBuffer(DS4State hidReport, byte[] outputData, ref int outIdx)
        {
            unchecked
            {
                outputData[outIdx] = 0;

                if (hidReport.DpadLeft)
                {
                    outputData[outIdx] |= 0x80;
                }
                if (hidReport.DpadDown)
                {
                    outputData[outIdx] |= 0x40;
                }
                if (hidReport.DpadRight)
                {
                    outputData[outIdx] |= 0x20;
                }
                if (hidReport.DpadUp)
                {
                    outputData[outIdx] |= 0x10;
                }

                if (hidReport.Options)
                {
                    outputData[outIdx] |= 0x08;
                }
                if (hidReport.R3)
                {
                    outputData[outIdx] |= 0x04;
                }
                if (hidReport.L3)
                {
                    outputData[outIdx] |= 0x02;
                }
                if (hidReport.Share)
                {
                    outputData[outIdx] |= 0x01;
                }

                outputData[++outIdx] = 0;

                if (hidReport.Square)
                {
                    outputData[outIdx] |= 0x80;
                }
                if (hidReport.Cross)
                {
                    outputData[outIdx] |= 0x40;
                }
                if (hidReport.Circle)
                {
                    outputData[outIdx] |= 0x20;
                }
                if (hidReport.Triangle)
                {
                    outputData[outIdx] |= 0x10;
                }

                if (hidReport.R1)
                {
                    outputData[outIdx] |= 0x08;
                }
                if (hidReport.L1)
                {
                    outputData[outIdx] |= 0x04;
                }
                if (hidReport.R2Btn)
                {
                    outputData[outIdx] |= 0x02;
                }
                if (hidReport.L2Btn)
                {
                    outputData[outIdx] |= 0x01;
                }

                outputData[++outIdx] = (hidReport.PS) ? (byte)1 : (byte)0;
                outputData[++outIdx] = (hidReport.TouchButton) ? (byte)1 : (byte)0;

                //Left stick
                outputData[++outIdx] = hidReport.LX;
                outputData[++outIdx] = hidReport.LY;
                outputData[outIdx]   = (byte)(255 - outputData[outIdx]); //invert Y by convention

                //Right stick
                outputData[++outIdx] = hidReport.RX;
                outputData[++outIdx] = hidReport.RY;
                outputData[outIdx]   = (byte)(255 - outputData[outIdx]); //invert Y by convention

                //we don't have analog buttons on DS4 :(
                outputData[++outIdx] = hidReport.DpadLeft ? (byte)0xFF : (byte)0x00;
                outputData[++outIdx] = hidReport.DpadDown ? (byte)0xFF : (byte)0x00;
                outputData[++outIdx] = hidReport.DpadRight ? (byte)0xFF : (byte)0x00;
                outputData[++outIdx] = hidReport.DpadUp ? (byte)0xFF : (byte)0x00;

                outputData[++outIdx] = hidReport.Square ? (byte)0xFF : (byte)0x00;
                outputData[++outIdx] = hidReport.Cross ? (byte)0xFF : (byte)0x00;
                outputData[++outIdx] = hidReport.Circle ? (byte)0xFF : (byte)0x00;
                outputData[++outIdx] = hidReport.Triangle ? (byte)0xFF : (byte)0x00;

                outputData[++outIdx] = hidReport.R1 ? (byte)0xFF : (byte)0x00;
                outputData[++outIdx] = hidReport.L1 ? (byte)0xFF : (byte)0x00;

                outputData[++outIdx] = hidReport.R2;
                outputData[++outIdx] = hidReport.L2;

                outIdx++;

                //DS4 only: touchpad points
                for (int i = 0; i < 2; i++)
                {
                    var tpad = (i == 0) ? hidReport.TrackPadTouch0 : hidReport.TrackPadTouch1;

                    outputData[outIdx++] = tpad.IsActive ? (byte)1 : (byte)0;
                    outputData[outIdx++] = (byte)tpad.Id;
                    Array.Copy(BitConverter.GetBytes((ushort)tpad.X), 0, outputData, outIdx, 2);
                    outIdx += 2;
                    Array.Copy(BitConverter.GetBytes((ushort)tpad.Y), 0, outputData, outIdx, 2);
                    outIdx += 2;
                }

                //motion timestamp
                if (hidReport.Motion != null)
                {
                    Array.Copy(BitConverter.GetBytes((ulong)hidReport.totalMicroSec), 0, outputData, outIdx, 8);
                }
                else
                {
                    Array.Clear(outputData, outIdx, 8);
                }

                outIdx += 8;

                //accelerometer
                if (hidReport.Motion != null)
                {
                    Array.Copy(BitConverter.GetBytes((float)hidReport.Motion.accelXG), 0, outputData, outIdx, 4);
                    outIdx += 4;
                    Array.Copy(BitConverter.GetBytes((float)hidReport.Motion.accelYG), 0, outputData, outIdx, 4);
                    outIdx += 4;
                    Array.Copy(BitConverter.GetBytes((float)-hidReport.Motion.accelZG), 0, outputData, outIdx, 4);
                    outIdx += 4;
                }
                else
                {
                    Array.Clear(outputData, outIdx, 12);
                    outIdx += 12;
                }

                //gyroscope
                if (hidReport.Motion != null)
                {
                    Array.Copy(BitConverter.GetBytes((float)hidReport.Motion.angVelPitch), 0, outputData, outIdx, 4);
                    outIdx += 4;
                    Array.Copy(BitConverter.GetBytes((float)hidReport.Motion.angVelYaw), 0, outputData, outIdx, 4);
                    outIdx += 4;
                    Array.Copy(BitConverter.GetBytes((float)hidReport.Motion.angVelRoll), 0, outputData, outIdx, 4);
                    outIdx += 4;
                }
                else
                {
                    Array.Clear(outputData, outIdx, 12);
                    outIdx += 12;
                }
            }

            return(true);
        }
Exemple #14
0
        public void NewReportIncoming(ref DualShockPadMeta padMeta, DS4State hidReport, byte[] outputData)
        {
            if (!running)
            {
                return;
            }

            var clientsList = new List <IPEndPoint>();
            var now         = DateTime.UtcNow;

            lock (clients)
            {
                var clientsToDelete = new List <IPEndPoint>();

                foreach (var cl in clients)
                {
                    const double TimeoutLimit = 5;

                    if ((now - cl.Value.AllPadsTime).TotalSeconds < TimeoutLimit)
                    {
                        clientsList.Add(cl.Key);
                    }
                    else if ((padMeta.PadId < cl.Value.PadIdsTime.Length) &&
                             (now - cl.Value.PadIdsTime[(byte)padMeta.PadId]).TotalSeconds < TimeoutLimit)
                    {
                        clientsList.Add(cl.Key);
                    }
                    else if (cl.Value.PadMacsTime.ContainsKey(padMeta.PadMacAddress) &&
                             (now - cl.Value.PadMacsTime[padMeta.PadMacAddress]).TotalSeconds < TimeoutLimit)
                    {
                        clientsList.Add(cl.Key);
                    }
                    else //check if this client is totally dead, and remove it if so
                    {
                        bool clientOk = false;
                        for (int i = 0; i < cl.Value.PadIdsTime.Length; i++)
                        {
                            var dur = (now - cl.Value.PadIdsTime[i]).TotalSeconds;
                            if (dur < TimeoutLimit)
                            {
                                clientOk = true;
                                break;
                            }
                        }
                        if (!clientOk)
                        {
                            foreach (var dict in cl.Value.PadMacsTime)
                            {
                                var dur = (now - dict.Value).TotalSeconds;
                                if (dur < TimeoutLimit)
                                {
                                    clientOk = true;
                                    break;
                                }
                            }

                            if (!clientOk)
                            {
                                clientsToDelete.Add(cl.Key);
                            }
                        }
                    }
                }

                foreach (var delCl in clientsToDelete)
                {
                    clients.Remove(delCl);
                }
                clientsToDelete.Clear();
                clientsToDelete = null;
            }

            if (clientsList.Count <= 0)
            {
                return;
            }

            unchecked
            {
                //byte[] outputData = new byte[100];
                int outIdx = BeginPacket(outputData, 1001);
                Array.Copy(BitConverter.GetBytes((uint)MessageType.DSUS_PadDataRsp), 0, outputData, outIdx, 4);
                outIdx += 4;

                outputData[outIdx++] = (byte)padMeta.PadId;
                outputData[outIdx++] = (byte)padMeta.PadState;
                outputData[outIdx++] = (byte)padMeta.Model;
                outputData[outIdx++] = (byte)padMeta.ConnectionType;
                {
                    byte[] padMac = padMeta.PadMacAddress.GetAddressBytes();
                    outputData[outIdx++] = padMac[0];
                    outputData[outIdx++] = padMac[1];
                    outputData[outIdx++] = padMac[2];
                    outputData[outIdx++] = padMac[3];
                    outputData[outIdx++] = padMac[4];
                    outputData[outIdx++] = padMac[5];
                }
                outputData[outIdx++] = (byte)padMeta.BatteryStatus;
                outputData[outIdx++] = padMeta.IsActive ? (byte)1 : (byte)0;

                Array.Copy(BitConverter.GetBytes((uint)hidReport.PacketCounter), 0, outputData, outIdx, 4);
                outIdx += 4;

                if (!ReportToBuffer(hidReport, outputData, ref outIdx))
                {
                    return;
                }
                else
                {
                    FinishPacket(outputData);
                }

                foreach (var cl in clientsList)
                {
                    //try { udpSock.SendTo(outputData, cl); }
                    int temp = 0;
                    poolLock.EnterWriteLock();
                    temp    = listInd;
                    listInd = ++listInd % 40;
                    SocketAsyncEventArgs args = argsList[temp];
                    poolLock.ExitWriteLock();

                    args.RemoteEndPoint = cl;
                    Array.Copy(outputData, args.Buffer, outputData.Length);
                    try {
                        udpSock.SendToAsync(args);
                    }
                    catch (SocketException ex) { }
                }
            }

            clientsList.Clear();
            clientsList = null;
        }
Exemple #15
0
 public DS4StateExposed(DS4State state)
 {
     _state = state;
 }
Exemple #16
0
 public DS4StateExposed()
 {
     _state = new DS4State();
 }
Exemple #17
0
 public void getPreviousState(DS4State state)
 {
     pState.CopyTo(state);
 }
Exemple #18
0
        public void handleTouchpad(byte[] data, DS4State sensors, int touchPacketOffset = 0)
        {
            bool touchPadIsDown = sensors.TouchButton;
            if (!PacketChanged(data, touchPacketOffset) && touchPadIsDown == lastTouchPadIsDown)
            {
                if (TouchUnchanged != null)
                    TouchUnchanged(this, EventArgs.Empty);
                return;
            }
            byte touchID1 = (byte)(data[0 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F);
            byte touchID2 = (byte)(data[4 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F);
            int currentX1 = data[1 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] + ((data[2 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF) * 255);
            int currentY1 = ((data[2 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF0) >> 4) + (data[3 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] * 16);
            int currentX2 = data[5 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] + ((data[6 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF) * 255);
            int currentY2 = ((data[6 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF0) >> 4) + (data[7 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] * 16);

            TouchpadEventArgs args;
            if (sensors.Touch1 || sensors.Touch2)
            {
                if ((sensors.Touch1 && !lastIsActive1) || (sensors.Touch2 && !lastIsActive2))
                {
                    if (TouchesBegan != null)
                    {
                        if (sensors.Touch1 && sensors.Touch2)
                            args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX1, currentY1, touchID1), new Touch(currentX2, currentY2, touchID2));
                        else if (sensors.Touch1)
                            args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX1, currentY1, touchID1));
                        else
                            args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX2, currentY2, touchID2));

                        TouchesBegan(this, args);
                    }
                }
                else if (sensors.Touch1 == lastIsActive1 && sensors.Touch2 == lastIsActive2 && TouchesMoved != null)
                {
                    Touch tPrev, t0, t1;

                    if (sensors.Touch1 && sensors.Touch2)
                    {
                        tPrev = new Touch(lastTouchPadX1, lastTouchPadY1, lastTouchID1);
                        t0 = new Touch(currentX1, currentY1, touchID1, tPrev);
                        tPrev = new Touch(lastTouchPadX2, lastTouchPadY2, lastTouchID2);
                        t1 = new Touch(currentX2, currentY2, touchID2, tPrev);
                    }
                    else if (sensors.Touch1)
                    {
                        tPrev = new Touch(lastTouchPadX1, lastTouchPadY1, lastTouchID1);
                        t0 = new Touch(currentX1, currentY1, touchID1, tPrev);
                        t1 = null;
                    }
                    else
                    {
                        tPrev = new Touch(lastTouchPadX2, lastTouchPadY2, lastTouchID2);
                        t0 = new Touch(currentX2, currentY2, touchID2, tPrev);
                        t1 = null;
                    }
                    args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1);

                    TouchesMoved(this, args);
                }

                if (!lastTouchPadIsDown && touchPadIsDown && TouchButtonDown != null)
                {
                    if (sensors.Touch1 && sensors.Touch2)
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX1, currentY1, touchID1), new Touch(currentX2, currentY2, touchID2));
                    else if (sensors.Touch1)
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX1, currentY1, touchID1));
                    else
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX2, currentY2, touchID2));

                    TouchButtonDown(this, args);
                }
                else if (lastTouchPadIsDown && !touchPadIsDown && TouchButtonUp != null)
                {
                    if (sensors.Touch1 && sensors.Touch2)
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX1, currentY1, touchID1), new Touch(currentX2, currentY2, touchID2));
                    else if (sensors.Touch1)
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX1, currentY1, touchID1));
                    else
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(currentX2, currentY2, touchID2));

                    TouchButtonUp(this, args);
                }

                if (sensors.Touch1)
                {
                    lastTouchPadX1 = currentX1;
                    lastTouchPadY1 = currentY1;
                }
                if (sensors.Touch2)
                {
                    lastTouchPadX2 = currentX2;
                    lastTouchPadY2 = currentY2;
                }
                lastTouchPadIsDown = touchPadIsDown;
            }
            else
            {
                if (touchPadIsDown && !lastTouchPadIsDown)
                {
                    if (TouchButtonDown != null)
                        TouchButtonDown(this, new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, null, null));
                }
                else if (!touchPadIsDown && lastTouchPadIsDown)
                {
                    if (TouchButtonUp != null)
                        TouchButtonUp(this, new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, null, null));
                }

                if ((lastIsActive1 || lastIsActive2) && TouchesEnded != null)
                {
                    if (lastIsActive1 && lastIsActive2)
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(lastTouchPadX1, lastTouchPadY1, touchID1), new Touch(lastTouchPadX2, lastTouchPadY2, touchID2));
                    else if (lastIsActive1)
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(lastTouchPadX1, lastTouchPadY1, touchID1));
                    else
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, new Touch(lastTouchPadX2, lastTouchPadY2, touchID2));

                    TouchesEnded(this, args);
                }
            }

            lastIsActive1 = sensors.Touch1;
            lastIsActive2 = sensors.Touch2;
            lastTouchID1 = touchID1;
            lastTouchID2 = touchID2;
            lastTouchPadIsDown = touchPadIsDown;
        }
Exemple #19
0
        public DS4Controls GetActiveInputControl(int ind)
        {
            DS4State        cState = CurrentState[ind];
            DS4StateExposed eState = ExposedState[ind];
            Mouse           tp     = touchPad[ind];
            DS4Controls     result = DS4Controls.None;

            if (DS4Controllers[ind] != null)
            {
                if (Mapping.getBoolButtonMapping(cState.Cross))
                {
                    result = DS4Controls.Cross;
                }
                else if (Mapping.getBoolButtonMapping(cState.Circle))
                {
                    result = DS4Controls.Circle;
                }
                else if (Mapping.getBoolButtonMapping(cState.Triangle))
                {
                    result = DS4Controls.Triangle;
                }
                else if (Mapping.getBoolButtonMapping(cState.Square))
                {
                    result = DS4Controls.Square;
                }
                else if (Mapping.getBoolButtonMapping(cState.L1))
                {
                    result = DS4Controls.L1;
                }
                else if (Mapping.getBoolTriggerMapping(cState.L2))
                {
                    result = DS4Controls.L2;
                }
                else if (Mapping.getBoolButtonMapping(cState.L3))
                {
                    result = DS4Controls.L3;
                }
                else if (Mapping.getBoolButtonMapping(cState.R1))
                {
                    result = DS4Controls.R1;
                }
                else if (Mapping.getBoolTriggerMapping(cState.R2))
                {
                    result = DS4Controls.R2;
                }
                else if (Mapping.getBoolButtonMapping(cState.R3))
                {
                    result = DS4Controls.R3;
                }
                else if (Mapping.getBoolButtonMapping(cState.DpadUp))
                {
                    result = DS4Controls.DpadUp;
                }
                else if (Mapping.getBoolButtonMapping(cState.DpadDown))
                {
                    result = DS4Controls.DpadDown;
                }
                else if (Mapping.getBoolButtonMapping(cState.DpadLeft))
                {
                    result = DS4Controls.DpadLeft;
                }
                else if (Mapping.getBoolButtonMapping(cState.DpadRight))
                {
                    result = DS4Controls.DpadRight;
                }
                else if (Mapping.getBoolButtonMapping(cState.Share))
                {
                    result = DS4Controls.Share;
                }
                else if (Mapping.getBoolButtonMapping(cState.Options))
                {
                    result = DS4Controls.Options;
                }
                else if (Mapping.getBoolButtonMapping(cState.PS))
                {
                    result = DS4Controls.PS;
                }
                else if (Mapping.getBoolAxisDirMapping(cState.LX, true))
                {
                    result = DS4Controls.LXPos;
                }
                else if (Mapping.getBoolAxisDirMapping(cState.LX, false))
                {
                    result = DS4Controls.LXNeg;
                }
                else if (Mapping.getBoolAxisDirMapping(cState.LY, true))
                {
                    result = DS4Controls.LYPos;
                }
                else if (Mapping.getBoolAxisDirMapping(cState.LY, false))
                {
                    result = DS4Controls.LYNeg;
                }
                else if (Mapping.getBoolAxisDirMapping(cState.RX, true))
                {
                    result = DS4Controls.RXPos;
                }
                else if (Mapping.getBoolAxisDirMapping(cState.RX, false))
                {
                    result = DS4Controls.RXNeg;
                }
                else if (Mapping.getBoolAxisDirMapping(cState.RY, true))
                {
                    result = DS4Controls.RYPos;
                }
                else if (Mapping.getBoolAxisDirMapping(cState.RY, false))
                {
                    result = DS4Controls.RYNeg;
                }
                else if (Mapping.getBoolTouchMapping(tp.leftDown))
                {
                    result = DS4Controls.TouchLeft;
                }
                else if (Mapping.getBoolTouchMapping(tp.rightDown))
                {
                    result = DS4Controls.TouchRight;
                }
                else if (Mapping.getBoolTouchMapping(tp.multiDown))
                {
                    result = DS4Controls.TouchMulti;
                }
                else if (Mapping.getBoolTouchMapping(tp.upperDown))
                {
                    result = DS4Controls.TouchUpper;
                }
            }

            return(result);
        }
Exemple #20
0
        public void handleTouchpad(byte[] data, DS4State sensors, int touchPacketOffset = 0)
        {
            PreTouchProcess?.Invoke(this, EventArgs.Empty);

            bool touchPadIsDown = sensors.TouchButton;

            if (!PacketChanged(data, touchPacketOffset) && touchPadIsDown == lastTouchPadIsDown)
            {
                TouchUnchanged?.Invoke(this, EventArgs.Empty);
                return;
            }

            Array.Copy(data, TOUCHPAD_DATA_OFFSET + touchPacketOffset, previousPacket, 0, 8);
            byte touchID1  = (byte)(data[0 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F);
            byte touchID2  = (byte)(data[4 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x7F);
            int  currentX1 = ((data[2 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x0F) << 8) | data[1 + TOUCHPAD_DATA_OFFSET + touchPacketOffset];
            int  currentY1 = (data[3 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] << 4) | ((data[2 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF0) >> 4);
            int  currentX2 = ((data[6 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0x0F) << 8) | data[5 + TOUCHPAD_DATA_OFFSET + touchPacketOffset];
            int  currentY2 = (data[7 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] << 4) | ((data[6 + TOUCHPAD_DATA_OFFSET + touchPacketOffset] & 0xF0) >> 4);

            TouchpadEventArgs args;

            if (sensors.Touch1 || sensors.Touch2)
            {
                if ((sensors.Touch1 && !lastIsActive1) || (sensors.Touch2 && !lastIsActive2))
                {
                    if (TouchesBegan != null)
                    {
                        if (sensors.Touch1 && sensors.Touch2)
                        {
                            t0.populate(currentX1, currentY1, touchID1); t1.populate(currentX2, currentY2, touchID2);
                            args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1);
                        }
                        else if (sensors.Touch1)
                        {
                            t0.populate(currentX1, currentY1, touchID1);
                            args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0);
                        }
                        else
                        {
                            t0.populate(currentX2, currentY2, touchID2);
                            args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0);
                        }

                        TouchesBegan(this, args);
                    }
                }
                else if (sensors.Touch1 == lastIsActive1 && sensors.Touch2 == lastIsActive2 && TouchesMoved != null)
                {
                    Touch currentT0, currentT1;

                    if (sensors.Touch1 && sensors.Touch2)
                    {
                        tPrev0.populate(lastTouchPadX1, lastTouchPadY1, lastTouchID1);
                        t0.populate(currentX1, currentY1, touchID1, tPrev0);
                        currentT0 = t0;

                        tPrev1.populate(lastTouchPadX2, lastTouchPadY2, lastTouchID2);
                        t1.populate(currentX2, currentY2, touchID2, tPrev1);
                        currentT1 = t1;
                    }
                    else if (sensors.Touch1)
                    {
                        tPrev0.populate(lastTouchPadX1, lastTouchPadY1, lastTouchID1);
                        t0.populate(currentX1, currentY1, touchID1, tPrev0);
                        currentT0 = t0;
                        currentT1 = null;
                    }
                    else
                    {
                        tPrev0.populate(lastTouchPadX2, lastTouchPadY2, lastTouchID2);
                        t0.populate(currentX2, currentY2, touchID2, tPrev0);
                        currentT0 = t0;
                        currentT1 = null;
                    }

                    args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, currentT0, currentT1);

                    TouchesMoved(this, args);
                }

                if (!lastTouchPadIsDown && touchPadIsDown && TouchButtonDown != null)
                {
                    if (sensors.Touch1 && sensors.Touch2)
                    {
                        t0.populate(currentX1, currentY1, touchID1);
                        t1.populate(currentX2, currentY2, touchID2);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1);
                    }
                    else if (sensors.Touch1)
                    {
                        t0.populate(currentX1, currentY1, touchID1);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0);
                    }
                    else
                    {
                        t0.populate(currentX2, currentY2, touchID2);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0);
                    }

                    TouchButtonDown(this, args);
                }
                else if (lastTouchPadIsDown && !touchPadIsDown && TouchButtonUp != null)
                {
                    if (sensors.Touch1 && sensors.Touch2)
                    {
                        t0.populate(currentX1, currentY1, touchID1);
                        t1.populate(currentX2, currentY2, touchID2);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1);
                    }
                    else if (sensors.Touch1)
                    {
                        t0.populate(currentX1, currentY1, touchID1);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0);
                    }
                    else
                    {
                        t0.populate(currentX2, currentY2, touchID2);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0);
                    }

                    TouchButtonUp(this, args);
                }

                if (sensors.Touch1)
                {
                    lastTouchPadX1 = currentX1;
                    lastTouchPadY1 = currentY1;
                }
                if (sensors.Touch2)
                {
                    lastTouchPadX2 = currentX2;
                    lastTouchPadY2 = currentY2;
                }

                lastTouchPadIsDown = touchPadIsDown;
            }
            else
            {
                if (touchPadIsDown && !lastTouchPadIsDown)
                {
                    TouchButtonDown?.Invoke(this, new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, null, null));
                }
                else if (!touchPadIsDown && lastTouchPadIsDown)
                {
                    TouchButtonUp?.Invoke(this, new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, null, null));
                }

                if ((lastIsActive1 || lastIsActive2) && TouchesEnded != null)
                {
                    if (lastIsActive1 && lastIsActive2)
                    {
                        t0.populate(lastTouchPadX1, lastTouchPadY1, touchID1);
                        t1.populate(lastTouchPadX2, lastTouchPadY2, touchID2);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0, t1);
                    }
                    else if (lastIsActive1)
                    {
                        t0.populate(lastTouchPadX1, lastTouchPadY1, touchID1);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0);
                    }
                    else
                    {
                        t0.populate(lastTouchPadX2, lastTouchPadY2, touchID2);
                        args = new TouchpadEventArgs(sensors.ReportTimeStamp, sensors.TouchButton, t0);
                    }

                    TouchesEnded(this, args);
                }
            }

            lastIsActive1      = sensors.Touch1;
            lastIsActive2      = sensors.Touch2;
            lastTouchID1       = touchID1;
            lastTouchID2       = touchID2;
            lastTouchPadIsDown = touchPadIsDown;
        }
Exemple #21
0
 public void getExposedState(DS4StateExposed expState, DS4State state)
 {
     cState.CopyTo(state);
     expState.Accel = accel;
     expState.Gyro = gyro;
 }