Beispiel #1
0
 public void SetVibration(UInt16 Left, UInt16 Right)
 {
     XInputNative.XInputVibration pVibration = new XInputNative.XInputVibration()
     {
         LeftMotorSpeed  = Left,
         RightMotorSpeed = Right,
     };
     XInputNative.XInputSetState(UserIndex + 1, ref pVibration);
 }
Beispiel #2
0
        public void ScanNow()
        {
            lock (lock_device_list)
            {
                try
                {
                    for (byte i = 0; i < MAX_SLOT; i++)
                    {
                        XInputNative.XInputCapabilities data = new XInputNative.XInputCapabilities();

                        if (DeviceCache[i] == null)
                        {
                            if (XInputNative.XInputGetCapabilities(i + 1, 0, ref data) == 0)
                            {
                                if (DeviceCache[i] == null)
                                {
                                    DeviceCache[i] = new XInputDevice(i);
                                }
                                DeviceAddedEventHandler threadSafeEventHandler = DeviceAdded;
                                threadSafeEventHandler?.Invoke(this, DeviceCache[i]);
                            }
                        }
                        else
                        {
                            bool connected = XInputNative.XInputGetCapabilities(i + 1, 0, ref data) == 0;
                            if (!connected)
                            {
                                DeviceRemovedEventHandler threadSafeEventHandler = DeviceRemoved;
                                threadSafeEventHandler?.Invoke(this, DeviceCache[i].UniqueKey);
                                DeviceCache[i] = null;
                            }
                        }
                    }
                }
                catch { }
            }
        }
Beispiel #3
0
        public void StartReading()
        {
            lock (readingLock)
            {
                if (DeviceReport == null)
                {
                    reading = false;
                }

                if (reading)
                {
                    return;
                }

                reading = true;

                CreateSendingQueue();

                Stopwatch InputTimer = new Stopwatch();

                readingThread = new Thread(() =>
                {
                    while (reading)
                    {
                        if (DeviceReport == null)
                        {
                            break;
                        }

                        try
                        {
                            InputTimer.Restart();

                            //XInputNative.XInputCapabilitiesEx data = new XInputNative.XInputCapabilitiesEx();
                            //if (XInputNative.XInputGetCapabilitiesEx(1, (int)internalDevice.UserIndex, 0, ref data) == 0)

                            XInputNative.XInputState data = new XInputNative.XInputState();
                            if (XInputNative.XInputGetStateEx(UserIndex + 1, ref data) == 0)
                            {
                                //if (!IsConnected)
                                //{
                                //    // TODO notify of unplug event
                                //    IsConnected = true;
                                //}
                                sendingQueue.EnqueueTask(new XInputReport()
                                {
                                    //Connected = IsConnected,
                                    wButtons      = (UInt16)data.Gamepad.wButtons,
                                    bLeftTrigger  = data.Gamepad.bLeftTrigger,
                                    bRightTrigger = data.Gamepad.bRightTrigger,
                                    sThumbLX      = data.Gamepad.sThumbLX,
                                    sThumbLY      = data.Gamepad.sThumbLY,
                                    sThumbRX      = data.Gamepad.sThumbRX,
                                    sThumbRY      = data.Gamepad.sThumbRY,
                                });
                            }

                            /*else if (IsConnected)
                             * {
                             *  IsConnected = false;
                             *  sendingQueue.EnqueueTask(new XInputReport()
                             *  {
                             *      Connected = IsConnected,
                             *  });
                             * }
                             * else
                             * {
                             *  // poll every 1 second if the device's state is disconnected
                             *  // might need to move polling rate out, which would require a temporary polling rate for an unpopulated node
                             *  int PollingRate = 1000;
                             *  if (PollingRate > 0)
                             *  {
                             *      int SleepTime = 0;
                             *      while (SleepTime < (PollingRate / 1000))
                             *      {
                             *          Thread.Sleep(1000);
                             *          SleepTime++;
                             *      }
                             *      Thread.Sleep(PollingRate % 1000);
                             *  }
                             * }*/

                            /*var State = internalDevice.GetState();
                             *
                             * sendingQueue.EnqueueTask(new XInputReport()
                             * {
                             *  Connected = internalDevice.IsConnected,
                             *  wButtons = (UInt16)State.Gamepad.Buttons,
                             *  bLeftTrigger = State.Gamepad.LeftTrigger,
                             *  bRightTrigger = State.Gamepad.RightTrigger,
                             *  sThumbLX = State.Gamepad.LeftThumbX,
                             *  sThumbLY = State.Gamepad.LeftThumbY,
                             *  sThumbRX = State.Gamepad.RightThumbX,
                             *  sThumbRY = State.Gamepad.RightThumbY,
                             * });*/

                            InputTimer.Stop();
                            if (InputTimer.ElapsedMilliseconds < 10)
                            {
                                Thread.Sleep((int)(10 - InputTimer.ElapsedMilliseconds));
                            }
                        }
                        catch
                        {
                            reading = false;
                        }
                    }
                    reading = false;
                });
                readingThread.Start();
            }
        }