コード例 #1
0
 private void OnGamepadRemoved(object?sender, Gamepad gamepad)
 {
     if (gamepads.TryGetValue(gamepad, out UwpGamepad? currentGamepad))
     {
         gamepads.Remove(gamepad);
         Gamepads.Remove(currentGamepad);
     }
 }
コード例 #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);
            }
        }