コード例 #1
0
 public GamepadConfiguration(uint gamepadId, string mappingJsonRepresentation, HidDeviceRepresentation hidDevice)
 {
     GamepadId = gamepadId;
     HidDevice = hidDevice;
     var loadedMapping = JsonConvert.DeserializeObject<GamepadMapping>(mappingJsonRepresentation);
     Mapping = loadedMapping;
 }
コード例 #2
0
        public async void Start(HidDeviceRepresentation[] selectedHids, uint[] selectedGamepadIds, string[] selectedMappsingPaths)
        {
            try
            {
                var mappings = new List<string>();
                foreach (var selectedMappsingPath in selectedMappsingPaths)
                {
                    var mapping = await GetMappingFromFilePath(selectedMappsingPath);
                    mappings.Add(mapping);
                }
                
                var configs = selectedHids.Select((t, i) => new GamepadConfiguration(selectedGamepadIds[i], mappings[i], t)).ToList();

                _runningGamepadsDisposable = _gamepadManager.Start(configs).Subscribe(
                    gamepadId => { },
                    exception => { IsRunning.Value = false; });

                IsRunning.Value = true;
            }
            catch (Exception ex)
            {
                IsRunning.Value = false;
                Log(ex.Message);
            }
        }
コード例 #3
0
        public GamepadConfiguration(uint gamepadId, string mappingJsonRepresentation, HidDeviceRepresentation hidDevice)
        {
            GamepadId = gamepadId;
            HidDevice = hidDevice;
            var loadedMapping = JsonConvert.DeserializeObject <GamepadMapping>(mappingJsonRepresentation);

            Mapping = loadedMapping;
        }
コード例 #4
0
        public void Refresh()
        {
            _currentGamepadThread?.Abort();
            _currentGamepadThread = new Thread(async() =>
            {
                Thread.CurrentThread.IsBackground = true;

                var selectedConfigFile = Settings.Default.SelectedConfigFile;
                var selectedHidDevice  = Settings.Default.SelectedHidDevice;
                var vJoyId             = Settings.Default.SelectedJoyId;

                if (selectedConfigFile == string.Empty || selectedHidDevice == string.Empty)
                {
                    Log("Configuration incomplete");
                    return;
                }
                ;

                GamepadConfiguration configuration = null;
                HidDeviceRepresentation hidDeviceRepresentation = null;

                try
                {
                    configuration           = await GetConfigFromFilePath(selectedConfigFile);
                    hidDeviceRepresentation =
                        JsonConvert.DeserializeObject <HidDeviceRepresentation>(selectedHidDevice);
                }
                catch (Exception ex)
                {
                    Log($"Error while reading configuration: {ex.Message}");
                }

                Log($"Using {hidDeviceRepresentation}, vJoy {vJoyId}, configuration file: {selectedConfigFile}");

                SetupGamepad(hidDeviceRepresentation, vJoyId, configuration);
            });
            _currentGamepadThread.Start();
        }
コード例 #5
0
        public void SetupGamepad(HidDeviceRepresentation hidDeviceRepresentation, uint gamePadId,
                                 GamepadConfiguration config)
        {
            try
            {
                var device =
                    _hidDeviceLoader.GetDevices(
                        hidDeviceRepresentation.VendorId,
                        hidDeviceRepresentation.ProductId,
                        hidDeviceRepresentation.ProductVersion,
                        hidDeviceRepresentation.SerialNumber
                        ).First();


                if (device == null)
                {
                    Log(@"Failed to open device.");
                    return;
                }

                HidStream stream;
                if (!device.TryOpen(out stream))
                {
                    Log("Failed to open device.");
                    return;
                }

                var gamepad = new Gamepad(config, gamePadId);
                gamepad.ErrorOccuredEvent += Gamepad_ErrorOccuredEvent;

                Log("Successfully initialized gamepad");

                using (stream)
                {
                    while (true)
                    {
                        var bytes = new byte[device.MaxInputReportLength];
                        int count;
                        try
                        {
                            count = stream.Read(bytes, 0, bytes.Length);
                        }
                        catch (TimeoutException)
                        {
                            continue;
                        }
                        catch (Exception ex)
                        {
                            Log(ex.Message);
                            break;
                        }

                        if (count > 0)
                        {
                            gamepad.UpdateState(bytes);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log(ex.Message);
            }
        }
コード例 #6
0
        public bool PlugInToHidDeviceAndStartLoop(HidDeviceRepresentation representation)
        {
            var device =
                HidManager.HidDeviceLoader.GetDevices(
                    representation.VendorId,
                    representation.ProductId,
                    representation.ProductVersion,
                    representation.SerialNumber
                ).First();

            if (device == null)
            {
                return false;
            }

            HidStream stream;
            if (!device.TryOpen(out stream))
            {
                return false;
            }

            _updateThread?.Abort();
            _updateThread = new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;

                try
                {
                    using (stream)
                    {
                        while (true)
                        {
                            if (!Thread.CurrentThread.IsAlive)
                            {
                                break;
                            }

                            var bytes = new byte[device.MaxInputReportLength];
                            int count;
                            try
                            {
                                count = stream.Read(bytes, 0, bytes.Length);
                            }
                            catch (TimeoutException)
                            {
                                continue;
                            }
                            catch
                            {
                                break;
                            }

                            if (count > 0)
                            {
                                CurrentHidState.Value = string.Join(" ", bytes);
                            }
                        }
                    }
                }
                catch
                {
                    // ignored
                }
            });
            _updateThread.Start();

            return true;
        }
コード例 #7
0
        public bool PlugInToHidDeviceAndStartLoop(HidDeviceRepresentation representation)
        {
            var device =
                HidManager.HidDeviceLoader.GetDevices(
                    representation.VendorId,
                    representation.ProductId,
                    representation.ProductVersion,
                    representation.SerialNumber
                    ).First();


            if (device == null)
            {
                return(false);
            }

            HidStream stream;

            if (!device.TryOpen(out stream))
            {
                return(false);
            }

            _updateThread?.Abort();
            _updateThread = new Thread(() =>
            {
                Thread.CurrentThread.IsBackground = true;

                try
                {
                    using (stream)
                    {
                        while (true)
                        {
                            if (!Thread.CurrentThread.IsAlive)
                            {
                                break;
                            }

                            var bytes = new byte[device.MaxInputReportLength];
                            int count;
                            try
                            {
                                count = stream.Read(bytes, 0, bytes.Length);
                            }
                            catch (TimeoutException)
                            {
                                continue;
                            }
                            catch
                            {
                                break;
                            }

                            if (count > 0)
                            {
                                CurrentHidState.Value = string.Join(" ", bytes);
                            }
                        }
                    }
                }
                catch
                {
                    // ignored
                }
            });
            _updateThread.Start();

            return(true);
        }