private void OnGamepadAdded(object?sender, Gamepad gamepad)
 {
     if (!gamepads.ContainsKey(gamepad))
     {
         UwpGamepad newGamepad = new UwpGamepad(gamepad);
         gamepads.Add(gamepad, newGamepad);
         Gamepads.Add(newGamepad);
     }
 }
示例#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);
            }
        }
示例#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);
            }
        }