public override bool Equals(object obj)
        {
            if ((obj == null) || (!(obj is XInputState)))
            {
                return(false);
            }
            XInputState source = (XInputState)obj;

            return((PacketNumber == source.PacketNumber) &&
                   (Gamepad.Equals(source.Gamepad)));
        }
        private void WatchControllerState(CancellationToken cancellationToken)
        {
            const int deviceCount         = 4;
            var       currentDeviceStates = new XInputState[deviceCount];

            while (!cancellationToken.IsCancellationRequested)
            {
                var isThereAnybodyOutThere = this.InputDetected != null;

                for (var deviceIndex = 0; isThereAnybodyOutThere && deviceIndex < deviceCount; deviceIndex++)
                {
                    var currentState = new XInputState();
                    var result       = XInputGetState(deviceIndex, ref currentState);
                    if (result != 0)
                    {
                        //controller is not connected
                        continue;
                    }

                    var previousDeviceState = currentDeviceStates[deviceIndex];
                    if (previousDeviceState.PacketNumber == currentState.PacketNumber)
                    {
                        //nothing has changed
                        continue;
                    }

                    var inputType = this.GetInputTypeFromStateChanges(previousDeviceState.Gamepad, currentState.Gamepad);

                    currentDeviceStates[deviceIndex] = currentState;

                    if (!inputType.HasValue)
                    {
                        continue;
                    }

                    PulseVibration(deviceIndex, 150);
                    this.InputDetected(this, inputType.Value);
                }

                if (cancellationToken.IsCancellationRequested)
                {
                    break;
                }

                Thread.Sleep(100);
            }
        }
 public void Copy(XInputState source)
 {
     PacketNumber = source.PacketNumber;
     Gamepad.Copy(source.Gamepad);
 }
 private static extern int XInputGetState(int deviceIndex, ref XInputState state);