Beispiel #1
0
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m.Msg == ScpDevice.WM_DEVICECHANGE)
                {
                    string Path;
                    ScpDevice.DEV_BROADCAST_HDR hdr;
                    var Type = m.WParam.ToInt32();

                    hdr =
                        (ScpDevice.DEV_BROADCAST_HDR)
                        Marshal.PtrToStructure(m.LParam, typeof(ScpDevice.DEV_BROADCAST_HDR));

                    if (hdr.dbch_devicetype == ScpDevice.DBT_DEVTYP_DEVICEINTERFACE)
                    {
                        ScpDevice.DEV_BROADCAST_DEVICEINTERFACE_M deviceInterface;

                        deviceInterface =
                            (ScpDevice.DEV_BROADCAST_DEVICEINTERFACE_M)
                            Marshal.PtrToStructure(m.LParam, typeof(ScpDevice.DEV_BROADCAST_DEVICEINTERFACE_M));

                        Path = new string(deviceInterface.dbcc_name);
                        Path = Path.Substring(0, Path.IndexOf('\0')).ToUpper();

                        switch (Type)
                        {
                        case ScpDevice.DBT_DEVICEARRIVAL:

                            if (usbDevice.State != DsState.Connected)
                            {
                                usbDevice.Close();
                                usbDevice = new UsbDs3();

                                if (usbDevice.Open(Path))
                                {
                                    usbDevice.Start();
                                }
                            }
                            break;

                        case ScpDevice.DBT_DEVICEREMOVECOMPLETE:

                            if (Path == usbDevice.Path && usbDevice.State == DsState.Connected)
                            {
                                usbDevice.Close();
                            }

                            break;
                        }
                    }
                }
            }
            catch
            {
            }

            base.WndProc(ref m);
        }
Beispiel #2
0
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m.Msg == ScpDevice.WM_DEVICECHANGE)
                {
                    String Path;
                    ScpDevice.DEV_BROADCAST_HDR hdr;
                    Int32 Type = m.WParam.ToInt32();

                    hdr = (ScpDevice.DEV_BROADCAST_HDR)Marshal.PtrToStructure(m.LParam, typeof(ScpDevice.DEV_BROADCAST_HDR));

                    if (hdr.dbch_devicetype == ScpDevice.DBT_DEVTYP_DEVICEINTERFACE)
                    {
                        ScpDevice.DEV_BROADCAST_DEVICEINTERFACE_M deviceInterface;

                        deviceInterface = (ScpDevice.DEV_BROADCAST_DEVICEINTERFACE_M)Marshal.PtrToStructure(m.LParam, typeof(ScpDevice.DEV_BROADCAST_DEVICEINTERFACE_M));

                        Path = new String(deviceInterface.dbcc_name);
                        Path = Path.Substring(0, Path.IndexOf('\0')).ToUpper();

                        switch (Type)
                        {
                            case ScpDevice.DBT_DEVICEARRIVAL:

                                if (usbDevice.State != DsState.Connected)
                                {
                                    usbDevice.Close();
                                    usbDevice = new UsbDs3();

                                    if (usbDevice.Open(Path)) usbDevice.Start();
                                }
                                break;

                            case ScpDevice.DBT_DEVICEREMOVECOMPLETE:

                                if (Path == usbDevice.Path && usbDevice.State == DsState.Connected)
                                {
                                    usbDevice.Close();
                                }

                                break;
                        }
                    }
                }
            }
            catch { }

            base.WndProc(ref m);
        }
Beispiel #3
0
        public override bool Start()
        {
            m_Started = true;

            byte index = 0;

            // enumerate DS4 devices
            for (byte instance = 0; instance < _devices.Length && index < _devices.Length; instance++)
            {
                try
                {
                    UsbDevice current = new UsbDs4();
                    current.PadId = (DsPadId)index;

                    if (current.Open(instance))
                    {
                        if (LogArrival(current))
                        {
                            current.HidReportReceived += OnHidReportReceived;

                            _devices[index++] = current;
                        }
                        else
                        {
                            current.Close();
                        }
                    }
                    else
                    {
                        current.Close();
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Unexpected error: {0}", ex);
                    break;
                }
            }

            // enumerate DS3 devices
            for (byte instance = 0; instance < _devices.Length && index < _devices.Length; instance++)
            {
                try
                {
                    UsbDevice current = new UsbDs3();
                    current.PadId = (DsPadId)index;

                    if (current.Open(instance))
                    {
                        if (!Apply3RdPartyWorkaroundsForDs3(ref current, instance))
                        {
                            continue;
                        }

                        // notify bus of new device
                        if (LogArrival(current))
                        {
                            // listen for HID reports
                            current.HidReportReceived += OnHidReportReceived;

                            _devices[index++] = current;
                        }
                        else
                        {
                            current.Close();
                        }
                    }
                    else
                    {
                        current.Close();
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Unexpected error: {0}", ex);
                    break;
                }
            }

            var hidDevices = UsbGenericGamepad.LocalHidDevices;

            // enumerate generic devices
            for (byte instance = 0; instance < hidDevices.Count && index < _devices.Length; instance++)
            {
                try
                {
                    // try to create supported gamepad
                    var current = UsbGenericGamepad.DeviceFactory(hidDevices[instance].DevicePath);

                    // try next on fail
                    if (current == null)
                    {
                        continue;
                    }

                    // try next if opening failed
                    if (!current.Open(hidDevices[instance].DevicePath))
                    {
                        continue;
                    }

                    current.PadId = (DsPadId)index;

                    // notify bus of new device
                    if (LogArrival(current))
                    {
                        // listen for HID reports
                        current.HidReportReceived += OnHidReportReceived;
                        current.Start();

                        _devices[index++] = current;
                    }
                    else
                    {
                        current.Close();
                    }
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Unexpected error: {0}", ex);
                    break;
                }
            }

            try
            {
                for (index = 0; index < _devices.Length; index++)
                {
                    if (_devices[index].State == DsState.Reserved)
                    {
                        _devices[index].Start();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Unexpected error: {0}", ex);
            }

            return(base.Start());
        }
Beispiel #4
0
        public override DsPadId Notify(ScpDevice.Notified notification, string Class, string path)
        {
            Log.InfoFormat("++ 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.Debug("-- DualShock 3 Arrival Event");
                }

                if (classGuid == UsbDs4.DeviceClassGuid)
                {
                    arrived = new UsbDs4();
                    Log.Debug("-- DualShock 4 Arrival Event");
                }

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

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

                    Log.Debug("-- Generic Gamepad Arrival Event");
                }

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

                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);
        }
Beispiel #5
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());
                    t.Stop();
                }
            }
            break;
            }

            return(DsPadId.None);
        }
Beispiel #6
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;
        }
Beispiel #7
0
        public override bool Start()
        {
            m_Started = true;

            byte index = 0;

            // enumerate DS4 devices
            for (byte instance = 0; instance < _devices.Length && index < _devices.Length; instance++)
            {
                try
                {
                    UsbDevice current = new UsbDs4();
                    current.PadId = (DsPadId) index;

                    if (current.Open(instance))
                    {
                        if (LogArrival(current))
                        {
                            current.HidReportReceived += OnHidReportReceived;

                            _devices[index++] = current;
                        }
                        else current.Close();
                    }
                    else current.Close();
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Unexpected error: {0}", ex);
                    break;
                }
            }

            // enumerate DS3 devices
            for (byte instance = 0; instance < _devices.Length && index < _devices.Length; instance++)
            {
                try
                {
                    UsbDevice current = new UsbDs3();
                    current.PadId = (DsPadId) index;

                    if (current.Open(instance))
                    {
                        if (!Apply3RdPartyWorkaroundsForDs3(ref current, instance)) continue;

                        // notify bus of new device
                        if (LogArrival(current))
                        {
                            // listen for HID reports
                            current.HidReportReceived += OnHidReportReceived;

                            _devices[index++] = current;
                        }
                        else current.Close();
                    }
                    else current.Close();
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Unexpected error: {0}", ex);
                    break;
                }
            }

            try
            {
                for (index = 0; index < _devices.Length; index++)
                {
                    if (_devices[index].State == DsState.Reserved)
                    {
                        _devices[index].Start();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Unexpected error: {0}", ex);
            }

            return base.Start();
        }
Beispiel #8
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;
        }
Beispiel #9
0
        public override bool Start()
        {
            m_Started = true;

            byte index = 0;

            // enumerate DS4 devices
            for (byte instance = 0; instance < _devices.Length && index < _devices.Length; instance++)
            {
                try
                {
                    UsbDevice current = new UsbDs4();
                    current.PadId = (DsPadId)index;

                    if (current.Open(instance))
                    {
                        if (LogArrival(current))
                        {
                            current.HidReportReceived += OnHidReportReceived;

                            _devices[index++] = current;
                        }
                        else current.Close();
                    }
                    else current.Close();
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Unexpected error: {0}", ex);
                    break;
                }
            }

            // enumerate DS3 devices
            for (byte instance = 0; instance < _devices.Length && index < _devices.Length; instance++)
            {
                try
                {
                    UsbDevice current = new UsbDs3();
                    current.PadId = (DsPadId)index;

                    if (current.Open(instance))
                    {
                        if (!Apply3RdPartyWorkaroundsForDs3(ref current, instance)) continue;

                        // notify bus of new device
                        if (LogArrival(current))
                        {
                            // listen for HID reports
                            current.HidReportReceived += OnHidReportReceived;

                            _devices[index++] = current;
                        }
                        else current.Close();
                    }
                    else current.Close();
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Unexpected error: {0}", ex);
                    break;
                }
            }

            var hidDevices = UsbGenericGamepad.LocalHidDevices;

            // enumerate generic devices
            for (byte instance = 0; instance < hidDevices.Count && index < _devices.Length; instance++)
            {
                try
                {
                    // try to create supported gamepad
                    var current = UsbGenericGamepad.DeviceFactory(hidDevices[instance].DevicePath);

                    // try next on fail
                    if (current == null) continue;

                    // try next if opening failed
                    if (!current.Open(hidDevices[instance].DevicePath)) continue;

                    current.PadId = (DsPadId) index;

                    // notify bus of new device
                    if (LogArrival(current))
                    {
                        // listen for HID reports
                        current.HidReportReceived += OnHidReportReceived;
                        current.Start();

                        _devices[index++] = current;
                    }
                    else current.Close();
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Unexpected error: {0}", ex);
                    break;
                }
            }

            try
            {
                // enumerate disconnected but reserved devices
                for (index = 0; index < _devices.Length; index++)
                {
                    if (_devices[index].State == DsState.Reserved)
                    {
                        _devices[index].Start();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.ErrorFormat("Unexpected error: {0}", ex);
            }

            return base.Start();
        }