Represents a generic Usb device.
Наследование: ScpDevice, IDsDevice
Пример #1
0
        public override bool Open()
        {
            for (byte pad = 0; pad < _devices.Length; pad++)
            {
                _devices[pad] = new UsbDevice { PadId = (DsPadId)pad };
            }

            return base.Open();
        }
Пример #2
0
        /// <summary>
        ///     Checks if the given USB device is a 3rd party device and applies common workarounds.
        /// </summary>
        /// <param name="current">The device to check.</param>
        /// <param name="instance">The device instance.</param>
        /// <param name="path">The device path.</param>
        /// <returns></returns>
        private static bool Apply3RdPartyWorkaroundsForDs3(ref UsbDevice current, byte instance = 0x00, string path = default(string))
        {
            if (!(current is UsbDs3))
                return true;

            var padId = current.PadId;

            #region Afterglow AP.2 Wireless Controller for PS3 workaround

            // if Afterglow AP.2 Wireless Controller for PS3 is detected...
            if (current.VendorId == 0x0E6F && current.ProductId == 0x0214)
            {
                Log.InfoFormat(
                    "Afterglow AP.2 Wireless Controller for PS3 detected [VID: {0:X4}] [PID: {1:X4}], workaround applied",
                    current.VendorId, current.ProductId);
                // ...close device...
                current.Close();
                // ...and create customized object
                current = new UsbDs3Afterglow()
                {
                    PadId = padId
                };

                // open and continue plug-in procedure on success
                return (!string.IsNullOrEmpty(path)) ? current.Open(path) : current.Open(instance);
            }

            #endregion

            #region Quad Stick workaround

            // if Quad Stick is detected...
            if (current.VendorId == 0x16D0 && current.ProductId == 0x092B)
            {
                Log.InfoFormat(
                    "Quad Stick detected [VID: {0:X4}] [PID: {1:X4}], workaround applied",
                    current.VendorId, current.ProductId);
                // ...close device...
                current.Close();
                // ...and create customized object
                current = new UsbDs3QuadStick()
                {
                    PadId = padId
                };

                // open and continue plug-in procedure on success
                return (!string.IsNullOrEmpty(path)) ? current.Open(path) : current.Open(instance);
            }

            #endregion

            return true;
        }
Пример #3
0
        public override DsPadId Notify(ScpDevice.Notified notification, string Class, string path)
        {
            Log.InfoFormat("++ Notify [{0}] [{1}] [{2}]", notification, Class, path);

            switch (notification)
            {
                case ScpDevice.Notified.Arrival:
                {
                    var arrived = new UsbDevice();

                    if (string.Equals(Class, UsbDs3.USB_CLASS_GUID, StringComparison.CurrentCultureIgnoreCase))
                    {
                        arrived = new UsbDs3();
                        Log.Debug("-- DS3 Arrival Event");
                    }

                    if (string.Equals(Class, UsbDs4.USB_CLASS_GUID, StringComparison.CurrentCultureIgnoreCase))
                    {
                        arrived = new UsbDs4();
                        Log.Debug("-- DS4 Arrival Event");
                    }

                    Log.InfoFormat("Arrival event for GUID {0} received", Class);

                    if (arrived.Open(path))
                    {
                        Log.InfoFormat("-- Device Arrival [{0}]", arrived.Local);

                        if (!Apply3RdPartyWorkaroundsForDs3(ref arrived, path: path)) break;

                        if (LogArrival(arrived))
                        {
                            if (_devices[(byte) arrived.PadId].IsShutdown)
                            {
                                _devices[(byte) arrived.PadId].IsShutdown = false;

                                _devices[(byte) arrived.PadId].Close();
                                _devices[(byte) arrived.PadId] = arrived;

                                return arrived.PadId;
                            }
                            arrived.HidReportReceived += OnHidReportReceived;

                            _devices[(byte) arrived.PadId].Close();
                            _devices[(byte) arrived.PadId] = arrived;

                            if (m_Started) arrived.Start();
                            return arrived.PadId;
                        }
                    }

                    arrived.Close();
                }
                    break;

                case ScpDevice.Notified.Removal:
                {
                    foreach (var t in _devices.Where(t => t.State == DsState.Connected && path == t.Path))
                    {
                        Log.InfoFormat("-- Device Removal [{0}]", t.Local);

                        // play disconnect sound
                        if (GlobalConfiguration.Instance.IsUsbDisconnectSoundEnabled)
                            AudioPlayer.Instance.PlayCustomFile(GlobalConfiguration.Instance.UsbDisconnectSoundFile);

                        t.Stop();
                    }
                }
                    break;
            }

            return DsPadId.None;
        }
Пример #4
0
        public override DsPadId Notify(ScpDevice.Notified notification, string Class, string path)
        {
            Log.DebugFormat("++ Notify [{0}] [{1}] [{2}]", notification, Class, path);

            var classGuid = Guid.Parse(Class);

            switch (notification)
            {
                case ScpDevice.Notified.Arrival:
                    {
                        var arrived = new UsbDevice();

                        if (classGuid == UsbDs3.DeviceClassGuid)
                        {
                            arrived = new UsbDs3();
                            Log.Info("DualShock 3 plugged in via Usb");
                        }

                        if (classGuid == UsbDs4.DeviceClassGuid)
                        {
                            arrived = new UsbDs4();
                            Log.Info("DualShock 4 plugged in via Usb");
                        }

                        if (classGuid == UsbGenericGamepad.DeviceClassGuid)
                        {
                            arrived = UsbGenericGamepad.DeviceFactory(path);

                            // unknown or unsupported device
                            if (arrived == null)
                                break;

                            Log.Debug("Generic Gamepad plugged in via Usb");
                        }

                        Log.DebugFormat("Arrival event for GUID {0} received", classGuid);

                        if (arrived.Open(path))
                        {
                            Log.DebugFormat("Device MAC address: {0}", arrived.DeviceAddress.AsFriendlyName());

                            if (!Apply3RdPartyWorkaroundsForDs3(ref arrived, path: path)) break;

                            if (LogArrival(arrived))
                            {
                                if (_devices[(byte) arrived.PadId].IsShutdown)
                                {
                                    _devices[(byte) arrived.PadId].IsShutdown = false;

                                    _devices[(byte) arrived.PadId].Close();
                                    _devices[(byte) arrived.PadId] = arrived;

                                    return arrived.PadId;
                                }
                                arrived.HidReportReceived += OnHidReportReceived;

                                _devices[(byte) arrived.PadId].Close();
                                _devices[(byte) arrived.PadId] = arrived;

                                if (m_Started) arrived.Start();
                                return arrived.PadId;
                            }
                        }
                        else
                        {
                            Log.FatalFormat("Couldn't open device {0}", path);
                        }

                        arrived.Close();
                    }
                    break;

                case ScpDevice.Notified.Removal:
                    {
                        foreach (var t in _devices.Where(t => t.State == DsState.Connected && path == t.Path))
                        {
                            Log.InfoFormat("Device with MAC address {0} unplugged from Usb", t.DeviceAddress.AsFriendlyName());

                            // play disconnect sound
                            if (GlobalConfiguration.Instance.IsUsbDisconnectSoundEnabled)
                                AudioPlayer.Instance.PlayCustomFile(GlobalConfiguration.Instance.UsbDisconnectSoundFile);

                            t.Stop();
                        }
                    }
                    break;
            }

            return DsPadId.None;
        }
        /// <summary>
        ///     Checks if the given USB device is a 3rd party device and applies common workarounds.
        /// </summary>
        /// <param name="current">The device to check.</param>
        /// <param name="instance">The device instance.</param>
        /// <param name="path">The device path.</param>
        /// <returns></returns>
        private static bool Apply3RdPartyWorkaroundsForDs3(ref UsbDevice current, byte instance = 0x00, string path = default(string))
        {
            var padId = current.PadId;

            #region Afterglow AP.2 Wireless Controller for PS3 workaround

            // if Afterglow AP.2 Wireless Controller for PS3 is detected...
            if (current.VendorId == 0x0E6F && current.ProductId == 0x0214)
            {
                Log.InfoFormat(
                    "Afterglow AP.2 Wireless Controller for PS3 detected [VID: {0:X4}] [PID: {1:X4}], workaround applied",
                    current.VendorId, current.ProductId);
                // ...close device...
                current.Close();
                // ...and create customized object
                current = new UsbDs3Afterglow()
                {
                    PadId = padId
                };

                // open and continue plug-in procedure on success
                return (!string.IsNullOrEmpty(path)) ? current.Open(path) : current.Open(instance);
            }

            #endregion

            #region Quad Stick workaround

            // if Quad Stick is detected...
            if (current.VendorId == 0x16D0 && current.ProductId == 0x092B)
            {
                Log.InfoFormat(
                    "Quad Stick detected [VID: {0:X4}] [PID: {1:X4}], workaround applied",
                    current.VendorId, current.ProductId);
                // ...close device...
                current.Close();
                // ...and create customized object
                current = new UsbDs3QuadStick()
                {
                    PadId = padId
                };

                // open and continue plug-in procedure on success
                return (!string.IsNullOrEmpty(path)) ? current.Open(path) : current.Open(instance);
            }

            #endregion

            #region DragonRise Inc. USB Gamepad SNES

            if (current.VendorId == 0x0079 && current.ProductId == 0x0011)
            {
                Log.InfoFormat(
                    "DragonRise Inc. USB Gamepad SNES detected [VID: {0:X4}] [PID: {1:X4}], workaround applied",
                    current.VendorId, current.ProductId);
                // ...close device...
                current.Close();
                // ...and create customized object
                current = new UsbSnesGamepad()
                {
                    PadId = padId
                };

                // open and continue plug-in procedure on success
                return (!string.IsNullOrEmpty(path)) ? current.Open(path) : current.Open(instance);
            }

            #endregion

            #region LSI Logic Gamepad

            if (current.VendorId == 0x0079 && current.ProductId == 0x0006)
            {
                Log.InfoFormat(
                    "LSI Logic Gamepad detected [VID: {0:X4}] [PID: {1:X4}], workaround applied",
                    current.VendorId, current.ProductId);
                // ...close device...
                current.Close();
                // ...and create customized object
                current = new UsbLsiLogicGamepad()
                {
                    PadId = padId
                };

                // open and continue plug-in procedure on success
                return (!string.IsNullOrEmpty(path)) ? current.Open(path) : current.Open(instance);
            }

            #endregion

            #region ShanWan Wireless Gamepad

            if (current.VendorId == 0x2563 && current.ProductId == 0x0523)
            {
                Log.InfoFormat(
                    "ShanWan Wireless Gamepad detected [VID: {0:X4}] [PID: {1:X4}], workaround applied",
                    current.VendorId, current.ProductId);
                // ...close device...
                current.Close();
                // ...and create customized object
                current = new UsbShanWanWirelessGamepad()
                {
                    PadId = padId
                };

                // open and continue plug-in procedure on success
                return (!string.IsNullOrEmpty(path)) ? current.Open(path) : current.Open(instance);
            }

            #endregion

            #region GameStop PC Advanced Controller

            if (current.VendorId == 0x11FF && current.ProductId == 0x3331)
            {
                Log.InfoFormat(
                    "GameStop PC Advanced Controller detected [VID: {0:X4}] [PID: {1:X4}], workaround applied",
                    current.VendorId, current.ProductId);
                // ...close device...
                current.Close();
                // ...and create customized object
                current = new UsbGameStopPcAdvanced()
                {
                    PadId = padId
                };

                // open and continue plug-in procedure on success
                return (!string.IsNullOrEmpty(path)) ? current.Open(path) : current.Open(instance);
            }

            #endregion

            return true;
        }