void IObservable <ListenerAcceptedClientArgs> .Notify(ListenerAcceptedClientArgs eventArgs)
 {
     if (ControllerConnected != null)
     {
         ControllerConnected.Invoke(this, eventArgs);
     }
 }
 void IObserver <ListenerAcceptedClientArgs> .OnNotified(object sender, ListenerAcceptedClientArgs eventArgs)
 {
     if (ControllerConnected != null)
     {
         ControllerConnected.Invoke(this, eventArgs);
     }
 }
Esempio n. 3
0
        public void PollDevices()
        {
            var devices = NativeMethods.GetRawInputDeviceList();

            foreach (var device in devices)
            {
                if (device.DeviceType == InputDeviceType.HID && !_processed.Contains(device.DeviceHandle))
                {
                    if (NativeMethods.TryRegisterPS4Controller(device.DeviceHandle, out var controller, out var product))
                    {
                        _processed.Add(device.DeviceHandle);
                        Controllers.Add(controller);
                        controller.Disconnected += (s, e) =>
                        {
                            _processed.Remove(device.DeviceHandle);
                            Controllers.Remove(controller);
                        };
                        ControllerConnected?.Invoke(controller);
                    }
                    else if (product != null)
                    {
                        _processed.Add(device.DeviceHandle);
                    }
                }
            }
        /// <summary>
        /// Raises the <see cref="ControllerConnected"/> event.
        /// </summary>
        /// <param name="inEventArgs">The event data.</param>
        internal static void onControllerConnected(ControllerConnectionEventArgs inEventArgs)
        {
            // Written, 16.07.2018

            if (ControllerConnected != null)
            {
                ControllerConnected.Invoke(null, inEventArgs);
            }
        }
        public static void Initialize(Game game, int ps4PollIntervalInFrames)
        {
            game.Exiting += (s, e) =>
            {
                foreach (var controller in _controllers)
                {
                    //Make sure any native handles are cleaned up.
                    if (controller is Ps4Controller ps4)
                    {
                        ps4.Close();
                    }
                }
            };

            for (var i = 0; i < GamePad.MaximumGamePadCount; i++)
            {
                var controller = new XInputController(i);
                _controllers.Add(controller);
                controller.Connected    += (s, e) => ControllerConnected(null, (IController)s);
                controller.Disconnected += (s, e) => ControllerDisconnected(null, (IController)s);
            }

#if Windows
            _pollInterval = ps4PollIntervalInFrames;
            if (_pollInterval > 0)
            {
                _ps4Manager = new Ps4Manager((controller) =>
                {
                    var index = _controllers.Add(controller);
                    controller.SetIndex(index);
                    ControllerConnected?.Invoke(null, controller);
                    controller.Disconnected += (s, e) =>
                    {
                        var c = (IController)s;
                        ControllerDisconnected?.Invoke(null, c);
                        _controllers.FastRemove(c.Index);
                    };
                }, false);
                _frames = _pollInterval;
            }
#endif
        }