-
Notifications
You must be signed in to change notification settings - Fork 505
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ensure gamepads connect automatically to first player without controller #405
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
using LibHac.Common; | ||
using Ryujinx.Common.Configuration.Hid; | ||
using Ryujinx.Common.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Ryujinx.Input.HLE | ||
{ | ||
|
@@ -41,6 +46,76 @@ protected virtual void Dispose(bool disposing) | |
} | ||
} | ||
|
||
private void removeSDPWhenExternalPadsConnected(List<Tuple<string,string>> availableDevices) { | ||
//remove all steam virtual gamepads | ||
availableDevices.RemoveAll(a => a.Item2 == "Steam Virtual Gamepad"); | ||
//remove Steam Deck Controller if external controllers are connected (docked game mode) | ||
if (availableDevices.Count > 1) { | ||
var steamDeckPad = availableDevices.FindFirst( a => a.Item2 == "Steam Deck Controller"); | ||
if (steamDeckPad.HasValue) { | ||
availableDevices.Remove(steamDeckPad.Value); | ||
} | ||
} | ||
} | ||
|
||
private List<Tuple<string,string>> getGamepadsDescriptions() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var result = new List<Tuple<string,string>> (); | ||
foreach (string id in GamepadDriver.GamepadsIds) { | ||
result.Add(Tuple.Create(id,GamepadDriver.GetGamepad(id).Name)); | ||
} | ||
return result; | ||
} | ||
|
||
private void LinkDevicesToPlayers(List<InputConfig> _inputConfig) { | ||
var _availableDevices = getGamepadsDescriptions(); | ||
removeSDPWhenExternalPadsConnected(_availableDevices); | ||
var _playersWithNoDevices = new List<PlayerIndex>(); | ||
//Remove all used Devices in current Config and at the same time list player with missing Devices | ||
foreach(PlayerIndex _playerId in Enum.GetValues(typeof(PlayerIndex))) | ||
{ | ||
var _config = _inputConfig.Find(inputConfig => inputConfig.PlayerIndex == _playerId); | ||
if (_config != null && _config.Backend != InputBackendType.WindowKeyboard) | ||
{ | ||
//check device id of the player is in the existing/connected devices | ||
var _connectedDevice = _availableDevices.FindFirst(d => d.Item1 == _config.Id); | ||
if (_connectedDevice.HasValue) | ||
{ | ||
_availableDevices.Remove(_connectedDevice.Value); | ||
} | ||
else | ||
{ | ||
_playersWithNoDevices.Add(_playerId); | ||
} | ||
} | ||
} | ||
|
||
var hasChanges = _playersWithNoDevices.Count() > 0 && _availableDevices.Count() > 0; | ||
if (hasChanges) | ||
{ | ||
Logger.Info?.Print(LogClass.Configuration, $"Controllers configuration changed. Updating players configuration..."); | ||
for (int i = 0; i < _playersWithNoDevices.Count; i++) | ||
{ | ||
var _playerId = _playersWithNoDevices[i]; | ||
var _config = _inputConfig.Find(inputConfig => inputConfig.PlayerIndex == _playerId); | ||
if (_config != null && _availableDevices.Count > 0) | ||
{ | ||
var _device = _availableDevices.First(); | ||
var deviceId = _device.Item1; | ||
var deviceName = _device.Item2; | ||
_config.Id = _device.Item1; | ||
Logger.Info?.Print(LogClass.Configuration, $"Link Player {_playerId} to Device {deviceName}"); | ||
_availableDevices.Remove(_device); | ||
} | ||
} | ||
Logger.Info?.Print(LogClass.Configuration, $"Updated players configuration to sync with Controllers configuration changes."); | ||
} | ||
} | ||
|
||
public void AddUpdaterForConfiguration(List<InputConfig> _inputConfig) { | ||
GamepadDriver.OnGamepadConnected += id => LinkDevicesToPlayers(_inputConfig); | ||
GamepadDriver.OnGamepadDisconnected += id => LinkDevicesToPlayers(_inputConfig); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
GC.SuppressFinalize(this); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,7 +97,7 @@ public PlayerIndex PlayerId | |
{ | ||
if (IsModified) | ||
{ | ||
|
||
_playerIdChoose = value; | ||
return; | ||
} | ||
|
@@ -367,12 +367,12 @@ private void LoadInputDriver() | |
|
||
private void HandleOnGamepadDisconnected(string id) | ||
{ | ||
Dispatcher.UIThread.Post(LoadDevices); | ||
Dispatcher.UIThread.Post(RefreshDevicesAndCurrentPlayerConfiguration); | ||
} | ||
|
||
private void HandleOnGamepadConnected(string id) | ||
{ | ||
Dispatcher.UIThread.Post(LoadDevices); | ||
Dispatcher.UIThread.Post(RefreshDevicesAndCurrentPlayerConfiguration); | ||
} | ||
|
||
private string GetCurrentGamepadId() | ||
|
@@ -441,6 +441,21 @@ private static string GetShortGamepadId(string str) | |
return str[(str.IndexOf(Hyphen) + Offset)..]; | ||
} | ||
|
||
public void RefreshDevicesAndCurrentPlayerConfiguration() | ||
{ | ||
LoadDevices(); | ||
|
||
//update Device for current user based on new configuration. | ||
var config = ConfigurationState.Instance.Hid.InputConfig.Value.Find(inputConfig => inputConfig.PlayerIndex == PlayerId); | ||
var device = Devices.FindFirst(d => d.Id==config.Id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please apply proper spacing. |
||
if (device.HasValue) { | ||
Device=Devices.IndexOf(device); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too |
||
} else { | ||
//0 is the None Device | ||
Device = 0; | ||
} | ||
} | ||
|
||
public void LoadDevices() | ||
{ | ||
string GetGamepadName(IGamepad gamepad, int controllerNumber) | ||
|
@@ -464,24 +479,27 @@ string GetUniqueGamepadName(IGamepad gamepad, ref int controllerNumber) | |
DeviceList.Clear(); | ||
Devices.Add((DeviceType.None, Disabled, LocaleManager.Instance[LocaleKeys.ControllerSettingsDeviceDisabled])); | ||
|
||
int controllerNumber = 0; | ||
int controllerNumber = 1; | ||
foreach (string id in _mainWindow.InputManager.KeyboardDriver.GamepadsIds) | ||
{ | ||
using IGamepad gamepad = _mainWindow.InputManager.KeyboardDriver.GetGamepad(id); | ||
|
||
if (gamepad != null) | ||
{ | ||
controllerNumber++; | ||
Devices.Add((DeviceType.Keyboard, id, $"{GetShortGamepadName(gamepad.Name)}")); | ||
} | ||
} | ||
|
||
controllerNumber = 1; | ||
foreach (string id in _mainWindow.InputManager.GamepadDriver.GamepadsIds) | ||
{ | ||
using IGamepad gamepad = _mainWindow.InputManager.GamepadDriver.GetGamepad(id); | ||
|
||
if (gamepad != null) | ||
{ | ||
string name = GetUniqueGamepadName(gamepad, ref controllerNumber); | ||
controllerNumber++; | ||
Devices.Add((DeviceType.Controller, id, name)); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RemoveSdpWhenExternalPadsConnected