Beispiel #1
0
        static void Main(string[] args)
        {
            if (!IsSingleInstance())
            {
                Environment.Exit(0);
            }
            NIcon = new NotifyIcon();
            //ScpBus scpBus = null;
            ScpBus scpBus = new ScpBus();

            scpBus.UnplugAll();
            global_scpBus = scpBus;

            handler = new ConsoleEventDelegate(ConsoleEventCallback);
            SetConsoleCtrlHandler(handler, true);

            Thread.Sleep(400);
            var controllersManager = new Thread(() => ManageControllers(scpBus));


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                try
                {
                    using (var pi = new ProcessIcon())
                    {
                        pi.Display();
                        controllersManager.Start();
                        Application.Run();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Program Terminated Unexpectedly",
                                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                controllersManager.Abort();
                scpBus.UnplugAll();
                foreach (var device in Gamepads.Select(g => g.Device))
                {
                    device.CloseDevice();
                }
                singleInstanceMutex.ReleaseMutex();
            }
            finally
            {
                Environment.Exit(0);
            }
        }
Beispiel #2
0
        private static void ManageControllers(ScpBus scpBus)
        {
            var nrConnected = 0;
            //while (true)
            {
                var compatibleDevices = HidDevices.Enumerate(0x18D1, 0x9400).ToList();
                var existingDevices   = Gamepads.Select(g => g.Device).ToList();
                var newDevices        = compatibleDevices.Where(d => !existingDevices.Select(e => e.DevicePath).Contains(d.DevicePath));
                foreach (var gamepad in Gamepads.ToList())
                {
                    if (!gamepad.check_connected())
                    {
                        gamepad.unplug();
                        Gamepads.Remove(gamepad);
                    }
                }
                foreach (var deviceInstance in newDevices)
                {
                    var device = deviceInstance;
                    try
                    {
                        device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive);
                    }
                    catch
                    {
                        InformUser("Could not open gamepad in exclusive mode. Try reconnecting the device.");
                        var instanceId = devicePathToInstanceId(deviceInstance.DevicePath);
                        if (TryReEnableDevice(instanceId))
                        {
                            try
                            {
                                device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive);
                                InformUser("Opened in exclusive mode.");
                            }
                            catch
                            {
                                device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite);
                                InformUser("Opened in shared mode.");
                            }
                        }
                        else
                        {
                            device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite);
                            InformUser("Opened in shared mode.");
                        }
                    }

                    //byte[] vibration = { 0x05, 0x00, 0x00, 0x00, 0x00 };
                    //if (device.Write(vibration) == false)
                    //{
                    //	InformUser("Could not write to gamepad (is it closed?), skipping");
                    //	device.CloseDevice();
                    //	continue;
                    //}

                    byte[] serialNumber;
                    byte[] product;
                    device.ReadSerialNumber(out serialNumber);
                    device.ReadProduct(out product);


                    var usedIndexes = Gamepads.Select(g => g.Index);
                    var index       = 1;
                    while (usedIndexes.Contains(index))
                    {
                        index++;
                    }
                    Gamepads.Add(new StadiaController(device, scpBus, index));
                }
                if (Gamepads.Count != nrConnected)
                {
                    InformUser($"{Gamepads.Count} controllers connected");
                    nrConnected = Gamepads.Count;
                }
                //Thread.Sleep(1000);
            }
        }
Beispiel #3
0
        private static void ManageControllers(ScpBus scpBus)
        {
            var nrConnected = 0;

            while (true)
            {
                var compatibleDevices = HidDevices.Enumerate(0x2717, 0x3144).ToList();
                var existingDevices   = Gamepads.Select(g => g.Device).ToList();
                var newDevices        = compatibleDevices.Where(d => !existingDevices.Contains(d));
                foreach (var deviceInstance in newDevices)
                {
                    var device = deviceInstance;
                    try
                    {
                        device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive);
                    }
                    catch
                    {
                        InformUser("Could not open gamepad in exclusive mode. Try reconnecting the device.");
                        var instanceId = devicePathToInstanceId(deviceInstance.DevicePath);
                        if (TryReEnableDevice(instanceId))
                        {
                            try
                            {
                                device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.Exclusive);
                                //InformUser("Opened in exclusive mode.");
                            }
                            catch
                            {
                                device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite);
                                //InformUser("Opened in shared mode.");
                            }
                        }
                        else
                        {
                            device.OpenDevice(DeviceMode.Overlapped, DeviceMode.Overlapped, ShareMode.ShareRead | ShareMode.ShareWrite);
                            //InformUser("Opened in shared mode.");
                        }
                    }

                    byte[] vibration = { 0x20, 0x00, 0x00 };
                    if (device.WriteFeatureData(vibration) == false)
                    {
                        InformUser("Could not write to gamepad (is it closed?), skipping");
                        device.CloseDevice();
                        continue;
                    }

                    byte[] serialNumber;
                    byte[] product;
                    device.ReadSerialNumber(out serialNumber);
                    device.ReadProduct(out product);


                    Gamepads.Add(new Xiaomi_gamepad(device, scpBus, Gamepads.Count + 1));
                }
                if (Gamepads.Count != nrConnected)
                {
                    InformUser($"{Gamepads.Count} controllers connected");
                }
                nrConnected = Gamepads.Count;
                if (nrConnected == 4)
                {
                    Thread.Sleep(10000);
                    continue;
                }
                Thread.Sleep(5000);
            }
        }