ControllerSettingsViewModel.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. using Avalonia.Collections;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Svg.Skia;
  5. using Avalonia.Threading;
  6. using Ryujinx.Ava.Common.Locale;
  7. using Ryujinx.Ava.Input;
  8. using Ryujinx.Ava.UI.Helpers;
  9. using Ryujinx.Ava.UI.Models;
  10. using Ryujinx.Ava.UI.Windows;
  11. using Ryujinx.Common;
  12. using Ryujinx.Common.Configuration;
  13. using Ryujinx.Common.Configuration.Hid;
  14. using Ryujinx.Common.Configuration.Hid.Controller;
  15. using Ryujinx.Common.Configuration.Hid.Controller.Motion;
  16. using Ryujinx.Common.Configuration.Hid.Keyboard;
  17. using Ryujinx.Common.Logging;
  18. using Ryujinx.Common.Utilities;
  19. using Ryujinx.Input;
  20. using Ryujinx.Ui.Common.Configuration;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Collections.ObjectModel;
  24. using System.IO;
  25. using System.Linq;
  26. using System.Text.Json;
  27. using ConfigGamepadInputId = Ryujinx.Common.Configuration.Hid.Controller.GamepadInputId;
  28. using ConfigStickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
  29. using Key = Ryujinx.Common.Configuration.Hid.Key;
  30. namespace Ryujinx.Ava.UI.ViewModels
  31. {
  32. public class ControllerSettingsViewModel : BaseModel, IDisposable
  33. {
  34. private const string Disabled = "disabled";
  35. private const string ProControllerResource = "Ryujinx.Ui.Common/Resources/Controller_ProCon.svg";
  36. private const string JoyConPairResource = "Ryujinx.Ui.Common/Resources/Controller_JoyConPair.svg";
  37. private const string JoyConLeftResource = "Ryujinx.Ui.Common/Resources/Controller_JoyConLeft.svg";
  38. private const string JoyConRightResource = "Ryujinx.Ui.Common/Resources/Controller_JoyConRight.svg";
  39. private const string KeyboardString = "keyboard";
  40. private const string ControllerString = "controller";
  41. private readonly MainWindow _mainWindow;
  42. private PlayerIndex _playerId;
  43. private int _controller;
  44. private int _controllerNumber = 0;
  45. private string _controllerImage;
  46. private int _device;
  47. private object _configuration;
  48. private string _profileName;
  49. private bool _isLoaded;
  50. private readonly UserControl _owner;
  51. public IGamepadDriver AvaloniaKeyboardDriver { get; }
  52. public IGamepad SelectedGamepad { get; private set; }
  53. public ObservableCollection<PlayerModel> PlayerIndexes { get; set; }
  54. public ObservableCollection<(DeviceType Type, string Id, string Name)> Devices { get; set; }
  55. internal ObservableCollection<ControllerModel> Controllers { get; set; }
  56. public AvaloniaList<string> ProfilesList { get; set; }
  57. public AvaloniaList<string> DeviceList { get; set; }
  58. // XAML Flags
  59. public bool ShowSettings => _device > 0;
  60. public bool IsController => _device > 1;
  61. public bool IsKeyboard => !IsController;
  62. public bool IsRight { get; set; }
  63. public bool IsLeft { get; set; }
  64. public bool IsModified { get; set; }
  65. public object Configuration
  66. {
  67. get => _configuration;
  68. set
  69. {
  70. _configuration = value;
  71. OnPropertyChanged();
  72. }
  73. }
  74. public PlayerIndex PlayerId
  75. {
  76. get => _playerId;
  77. set
  78. {
  79. if (IsModified)
  80. {
  81. return;
  82. }
  83. IsModified = false;
  84. _playerId = value;
  85. if (!Enum.IsDefined(typeof(PlayerIndex), _playerId))
  86. {
  87. _playerId = PlayerIndex.Player1;
  88. }
  89. LoadConfiguration();
  90. LoadDevice();
  91. LoadProfiles();
  92. _isLoaded = true;
  93. OnPropertyChanged();
  94. }
  95. }
  96. public int Controller
  97. {
  98. get => _controller;
  99. set
  100. {
  101. _controller = value;
  102. if (_controller == -1)
  103. {
  104. _controller = 0;
  105. }
  106. if (Controllers.Count > 0 && value < Controllers.Count && _controller > -1)
  107. {
  108. ControllerType controller = Controllers[_controller].Type;
  109. IsLeft = true;
  110. IsRight = true;
  111. switch (controller)
  112. {
  113. case ControllerType.Handheld:
  114. ControllerImage = JoyConPairResource;
  115. break;
  116. case ControllerType.ProController:
  117. ControllerImage = ProControllerResource;
  118. break;
  119. case ControllerType.JoyconPair:
  120. ControllerImage = JoyConPairResource;
  121. break;
  122. case ControllerType.JoyconLeft:
  123. ControllerImage = JoyConLeftResource;
  124. IsRight = false;
  125. break;
  126. case ControllerType.JoyconRight:
  127. ControllerImage = JoyConRightResource;
  128. IsLeft = false;
  129. break;
  130. }
  131. LoadInputDriver();
  132. LoadProfiles();
  133. }
  134. OnPropertyChanged();
  135. NotifyChanges();
  136. }
  137. }
  138. public string ControllerImage
  139. {
  140. get => _controllerImage;
  141. set
  142. {
  143. _controllerImage = value;
  144. OnPropertyChanged();
  145. OnPropertyChanged(nameof(Image));
  146. }
  147. }
  148. public SvgImage Image
  149. {
  150. get
  151. {
  152. SvgImage image = new SvgImage();
  153. if (!string.IsNullOrWhiteSpace(_controllerImage))
  154. {
  155. SvgSource source = new SvgSource();
  156. source.Load(EmbeddedResources.GetStream(_controllerImage));
  157. image.Source = source;
  158. }
  159. return image;
  160. }
  161. }
  162. public string ProfileName
  163. {
  164. get => _profileName; set
  165. {
  166. _profileName = value;
  167. OnPropertyChanged();
  168. }
  169. }
  170. public int Device
  171. {
  172. get => _device;
  173. set
  174. {
  175. _device = value < 0 ? 0 : value;
  176. if (_device >= Devices.Count)
  177. {
  178. return;
  179. }
  180. var selected = Devices[_device].Type;
  181. if (selected != DeviceType.None)
  182. {
  183. LoadControllers();
  184. if (_isLoaded)
  185. {
  186. LoadConfiguration(LoadDefaultConfiguration());
  187. }
  188. }
  189. OnPropertyChanged();
  190. NotifyChanges();
  191. }
  192. }
  193. public InputConfig Config { get; set; }
  194. public ControllerSettingsViewModel(UserControl owner) : this()
  195. {
  196. _owner = owner;
  197. if (Program.PreviewerDetached)
  198. {
  199. _mainWindow =
  200. (MainWindow)((IClassicDesktopStyleApplicationLifetime)Avalonia.Application.Current
  201. .ApplicationLifetime).MainWindow;
  202. AvaloniaKeyboardDriver = new AvaloniaKeyboardDriver(owner);
  203. _mainWindow.InputManager.GamepadDriver.OnGamepadConnected += HandleOnGamepadConnected;
  204. _mainWindow.InputManager.GamepadDriver.OnGamepadDisconnected += HandleOnGamepadDisconnected;
  205. if (_mainWindow.ViewModel.AppHost != null)
  206. {
  207. _mainWindow.ViewModel.AppHost.NpadManager.BlockInputUpdates();
  208. }
  209. _isLoaded = false;
  210. LoadDevices();
  211. PlayerId = PlayerIndex.Player1;
  212. }
  213. }
  214. public ControllerSettingsViewModel()
  215. {
  216. PlayerIndexes = new ObservableCollection<PlayerModel>();
  217. Controllers = new ObservableCollection<ControllerModel>();
  218. Devices = new ObservableCollection<(DeviceType Type, string Id, string Name)>();
  219. ProfilesList = new AvaloniaList<string>();
  220. DeviceList = new AvaloniaList<string>();
  221. ControllerImage = ProControllerResource;
  222. PlayerIndexes.Add(new(PlayerIndex.Player1, LocaleManager.Instance[LocaleKeys.ControllerSettingsPlayer1]));
  223. PlayerIndexes.Add(new(PlayerIndex.Player2, LocaleManager.Instance[LocaleKeys.ControllerSettingsPlayer2]));
  224. PlayerIndexes.Add(new(PlayerIndex.Player3, LocaleManager.Instance[LocaleKeys.ControllerSettingsPlayer3]));
  225. PlayerIndexes.Add(new(PlayerIndex.Player4, LocaleManager.Instance[LocaleKeys.ControllerSettingsPlayer4]));
  226. PlayerIndexes.Add(new(PlayerIndex.Player5, LocaleManager.Instance[LocaleKeys.ControllerSettingsPlayer5]));
  227. PlayerIndexes.Add(new(PlayerIndex.Player6, LocaleManager.Instance[LocaleKeys.ControllerSettingsPlayer6]));
  228. PlayerIndexes.Add(new(PlayerIndex.Player7, LocaleManager.Instance[LocaleKeys.ControllerSettingsPlayer7]));
  229. PlayerIndexes.Add(new(PlayerIndex.Player8, LocaleManager.Instance[LocaleKeys.ControllerSettingsPlayer8]));
  230. PlayerIndexes.Add(new(PlayerIndex.Handheld, LocaleManager.Instance[LocaleKeys.ControllerSettingsHandheld]));
  231. }
  232. private void LoadConfiguration(InputConfig inputConfig = null)
  233. {
  234. Config = inputConfig ?? ConfigurationState.Instance.Hid.InputConfig.Value.Find(inputConfig => inputConfig.PlayerIndex == _playerId);
  235. if (Config is StandardKeyboardInputConfig keyboardInputConfig)
  236. {
  237. Configuration = new InputConfiguration<Key, ConfigStickInputId>(keyboardInputConfig);
  238. }
  239. if (Config is StandardControllerInputConfig controllerInputConfig)
  240. {
  241. Configuration = new InputConfiguration<ConfigGamepadInputId, ConfigStickInputId>(controllerInputConfig);
  242. }
  243. }
  244. public void LoadDevice()
  245. {
  246. if (Config == null || Config.Backend == InputBackendType.Invalid)
  247. {
  248. Device = 0;
  249. }
  250. else
  251. {
  252. var type = DeviceType.None;
  253. if (Config is StandardKeyboardInputConfig)
  254. {
  255. type = DeviceType.Keyboard;
  256. }
  257. if (Config is StandardControllerInputConfig)
  258. {
  259. type = DeviceType.Controller;
  260. }
  261. var item = Devices.FirstOrDefault(x => x.Type == type && x.Id == Config.Id);
  262. if (item != default)
  263. {
  264. Device = Devices.ToList().FindIndex(x => x.Id == item.Id);
  265. }
  266. else
  267. {
  268. Device = 0;
  269. }
  270. }
  271. }
  272. public async void ShowMotionConfig()
  273. {
  274. await MotionSettingsWindow.Show(this);
  275. }
  276. public async void ShowRumbleConfig()
  277. {
  278. await RumbleSettingsWindow.Show(this);
  279. }
  280. private void LoadInputDriver()
  281. {
  282. if (_device < 0)
  283. {
  284. return;
  285. }
  286. string id = GetCurrentGamepadId();
  287. var type = Devices[Device].Type;
  288. if (type == DeviceType.None)
  289. {
  290. return;
  291. }
  292. else if (type == DeviceType.Keyboard)
  293. {
  294. if (_mainWindow.InputManager.KeyboardDriver is AvaloniaKeyboardDriver)
  295. {
  296. // NOTE: To get input in this window, we need to bind a custom keyboard driver instead of using the InputManager one as the main window isn't focused...
  297. SelectedGamepad = AvaloniaKeyboardDriver.GetGamepad(id);
  298. }
  299. else
  300. {
  301. SelectedGamepad = _mainWindow.InputManager.KeyboardDriver.GetGamepad(id);
  302. }
  303. }
  304. else
  305. {
  306. SelectedGamepad = _mainWindow.InputManager.GamepadDriver.GetGamepad(id);
  307. }
  308. }
  309. private void HandleOnGamepadDisconnected(string id)
  310. {
  311. Dispatcher.UIThread.Post(() =>
  312. {
  313. LoadDevices();
  314. });
  315. }
  316. private void HandleOnGamepadConnected(string id)
  317. {
  318. Dispatcher.UIThread.Post(() =>
  319. {
  320. LoadDevices();
  321. });
  322. }
  323. private string GetCurrentGamepadId()
  324. {
  325. if (_device < 0)
  326. {
  327. return string.Empty;
  328. }
  329. var device = Devices[Device];
  330. if (device.Type == DeviceType.None)
  331. {
  332. return null;
  333. }
  334. return device.Id.Split(" ")[0];
  335. }
  336. public void LoadControllers()
  337. {
  338. Controllers.Clear();
  339. if (_playerId == PlayerIndex.Handheld)
  340. {
  341. Controllers.Add(new(ControllerType.Handheld, LocaleManager.Instance[LocaleKeys.ControllerSettingsControllerTypeHandheld]));
  342. Controller = 0;
  343. }
  344. else
  345. {
  346. Controllers.Add(new(ControllerType.ProController, LocaleManager.Instance[LocaleKeys.ControllerSettingsControllerTypeProController]));
  347. Controllers.Add(new(ControllerType.JoyconPair, LocaleManager.Instance[LocaleKeys.ControllerSettingsControllerTypeJoyConPair]));
  348. Controllers.Add(new(ControllerType.JoyconLeft, LocaleManager.Instance[LocaleKeys.ControllerSettingsControllerTypeJoyConLeft]));
  349. Controllers.Add(new(ControllerType.JoyconRight, LocaleManager.Instance[LocaleKeys.ControllerSettingsControllerTypeJoyConRight]));
  350. if (Config != null && Controllers.ToList().FindIndex(x => x.Type == Config.ControllerType) != -1)
  351. {
  352. Controller = Controllers.ToList().FindIndex(x => x.Type == Config.ControllerType);
  353. }
  354. else
  355. {
  356. Controller = 0;
  357. }
  358. }
  359. }
  360. private static string GetShortGamepadName(string str)
  361. {
  362. const string Ellipsis = "...";
  363. const int MaxSize = 50;
  364. if (str.Length > MaxSize)
  365. {
  366. return $"{str.AsSpan(0, MaxSize - Ellipsis.Length)}{Ellipsis}";
  367. }
  368. return str;
  369. }
  370. private static string GetShortGamepadId(string str)
  371. {
  372. const string Hyphen = "-";
  373. const int Offset = 1;
  374. return str.Substring(str.IndexOf(Hyphen) + Offset);
  375. }
  376. public void LoadDevices()
  377. {
  378. lock (Devices)
  379. {
  380. Devices.Clear();
  381. DeviceList.Clear();
  382. Devices.Add((DeviceType.None, Disabled, LocaleManager.Instance[LocaleKeys.ControllerSettingsDeviceDisabled]));
  383. foreach (string id in _mainWindow.InputManager.KeyboardDriver.GamepadsIds)
  384. {
  385. using IGamepad gamepad = _mainWindow.InputManager.KeyboardDriver.GetGamepad(id);
  386. if (gamepad != null)
  387. {
  388. Devices.Add((DeviceType.Keyboard, id, $"{GetShortGamepadName(gamepad.Name)}"));
  389. }
  390. }
  391. foreach (string id in _mainWindow.InputManager.GamepadDriver.GamepadsIds)
  392. {
  393. using IGamepad gamepad = _mainWindow.InputManager.GamepadDriver.GetGamepad(id);
  394. if (gamepad != null)
  395. {
  396. if (Devices.Any(controller => GetShortGamepadId(controller.Id) == GetShortGamepadId(gamepad.Id)))
  397. {
  398. _controllerNumber++;
  399. }
  400. Devices.Add((DeviceType.Controller, id, $"{GetShortGamepadName(gamepad.Name)} ({_controllerNumber})"));
  401. }
  402. }
  403. _controllerNumber = 0;
  404. DeviceList.AddRange(Devices.Select(x => x.Name));
  405. Device = Math.Min(Device, DeviceList.Count);
  406. }
  407. }
  408. private string GetProfileBasePath()
  409. {
  410. string path = AppDataManager.ProfilesDirPath;
  411. var type = Devices[Device == -1 ? 0 : Device].Type;
  412. if (type == DeviceType.Keyboard)
  413. {
  414. path = Path.Combine(path, KeyboardString);
  415. }
  416. else if (type == DeviceType.Controller)
  417. {
  418. path = Path.Combine(path, ControllerString);
  419. }
  420. return path;
  421. }
  422. private void LoadProfiles()
  423. {
  424. ProfilesList.Clear();
  425. string basePath = GetProfileBasePath();
  426. if (!Directory.Exists(basePath))
  427. {
  428. Directory.CreateDirectory(basePath);
  429. }
  430. ProfilesList.Add((LocaleManager.Instance[LocaleKeys.ControllerSettingsProfileDefault]));
  431. foreach (string profile in Directory.GetFiles(basePath, "*.json", SearchOption.AllDirectories))
  432. {
  433. ProfilesList.Add(Path.GetFileNameWithoutExtension(profile));
  434. }
  435. if (string.IsNullOrWhiteSpace(ProfileName))
  436. {
  437. ProfileName = LocaleManager.Instance[LocaleKeys.ControllerSettingsProfileDefault];
  438. }
  439. }
  440. public InputConfig LoadDefaultConfiguration()
  441. {
  442. var activeDevice = Devices.FirstOrDefault();
  443. if (Devices.Count > 0 && Device < Devices.Count && Device >= 0)
  444. {
  445. activeDevice = Devices[Device];
  446. }
  447. InputConfig config;
  448. if (activeDevice.Type == DeviceType.Keyboard)
  449. {
  450. string id = activeDevice.Id;
  451. config = new StandardKeyboardInputConfig
  452. {
  453. Version = InputConfig.CurrentVersion,
  454. Backend = InputBackendType.WindowKeyboard,
  455. Id = id,
  456. ControllerType = ControllerType.ProController,
  457. LeftJoycon = new LeftJoyconCommonConfig<Key>
  458. {
  459. DpadUp = Key.Up,
  460. DpadDown = Key.Down,
  461. DpadLeft = Key.Left,
  462. DpadRight = Key.Right,
  463. ButtonMinus = Key.Minus,
  464. ButtonL = Key.E,
  465. ButtonZl = Key.Q,
  466. ButtonSl = Key.Unbound,
  467. ButtonSr = Key.Unbound
  468. },
  469. LeftJoyconStick =
  470. new JoyconConfigKeyboardStick<Key>
  471. {
  472. StickUp = Key.W,
  473. StickDown = Key.S,
  474. StickLeft = Key.A,
  475. StickRight = Key.D,
  476. StickButton = Key.F
  477. },
  478. RightJoycon = new RightJoyconCommonConfig<Key>
  479. {
  480. ButtonA = Key.Z,
  481. ButtonB = Key.X,
  482. ButtonX = Key.C,
  483. ButtonY = Key.V,
  484. ButtonPlus = Key.Plus,
  485. ButtonR = Key.U,
  486. ButtonZr = Key.O,
  487. ButtonSl = Key.Unbound,
  488. ButtonSr = Key.Unbound
  489. },
  490. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  491. {
  492. StickUp = Key.I,
  493. StickDown = Key.K,
  494. StickLeft = Key.J,
  495. StickRight = Key.L,
  496. StickButton = Key.H
  497. }
  498. };
  499. }
  500. else if (activeDevice.Type == DeviceType.Controller)
  501. {
  502. bool isNintendoStyle = Devices.ToList().Find(x => x.Id == activeDevice.Id).Name.Contains("Nintendo");
  503. string id = activeDevice.Id.Split(" ")[0];
  504. config = new StandardControllerInputConfig
  505. {
  506. Version = InputConfig.CurrentVersion,
  507. Backend = InputBackendType.GamepadSDL2,
  508. Id = id,
  509. ControllerType = ControllerType.ProController,
  510. DeadzoneLeft = 0.1f,
  511. DeadzoneRight = 0.1f,
  512. RangeLeft = 1.0f,
  513. RangeRight = 1.0f,
  514. TriggerThreshold = 0.5f,
  515. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  516. {
  517. DpadUp = ConfigGamepadInputId.DpadUp,
  518. DpadDown = ConfigGamepadInputId.DpadDown,
  519. DpadLeft = ConfigGamepadInputId.DpadLeft,
  520. DpadRight = ConfigGamepadInputId.DpadRight,
  521. ButtonMinus = ConfigGamepadInputId.Minus,
  522. ButtonL = ConfigGamepadInputId.LeftShoulder,
  523. ButtonZl = ConfigGamepadInputId.LeftTrigger,
  524. ButtonSl = ConfigGamepadInputId.Unbound,
  525. ButtonSr = ConfigGamepadInputId.Unbound
  526. },
  527. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  528. {
  529. Joystick = ConfigStickInputId.Left,
  530. StickButton = ConfigGamepadInputId.LeftStick,
  531. InvertStickX = false,
  532. InvertStickY = false
  533. },
  534. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  535. {
  536. ButtonA = isNintendoStyle ? ConfigGamepadInputId.A : ConfigGamepadInputId.B,
  537. ButtonB = isNintendoStyle ? ConfigGamepadInputId.B : ConfigGamepadInputId.A,
  538. ButtonX = isNintendoStyle ? ConfigGamepadInputId.X : ConfigGamepadInputId.Y,
  539. ButtonY = isNintendoStyle ? ConfigGamepadInputId.Y : ConfigGamepadInputId.X,
  540. ButtonPlus = ConfigGamepadInputId.Plus,
  541. ButtonR = ConfigGamepadInputId.RightShoulder,
  542. ButtonZr = ConfigGamepadInputId.RightTrigger,
  543. ButtonSl = ConfigGamepadInputId.Unbound,
  544. ButtonSr = ConfigGamepadInputId.Unbound
  545. },
  546. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  547. {
  548. Joystick = ConfigStickInputId.Right,
  549. StickButton = ConfigGamepadInputId.RightStick,
  550. InvertStickX = false,
  551. InvertStickY = false
  552. },
  553. Motion = new StandardMotionConfigController
  554. {
  555. MotionBackend = MotionInputBackendType.GamepadDriver,
  556. EnableMotion = true,
  557. Sensitivity = 100,
  558. GyroDeadzone = 1
  559. },
  560. Rumble = new RumbleConfigController
  561. {
  562. StrongRumble = 1f,
  563. WeakRumble = 1f,
  564. EnableRumble = false
  565. }
  566. };
  567. }
  568. else
  569. {
  570. config = new InputConfig();
  571. }
  572. config.PlayerIndex = _playerId;
  573. return config;
  574. }
  575. public async void LoadProfile()
  576. {
  577. if (Device == 0)
  578. {
  579. return;
  580. }
  581. InputConfig config = null;
  582. if (string.IsNullOrWhiteSpace(ProfileName))
  583. {
  584. return;
  585. }
  586. if (ProfileName == LocaleManager.Instance[LocaleKeys.ControllerSettingsProfileDefault])
  587. {
  588. config = LoadDefaultConfiguration();
  589. }
  590. else
  591. {
  592. string path = Path.Combine(GetProfileBasePath(), ProfileName + ".json");
  593. if (!File.Exists(path))
  594. {
  595. var index = ProfilesList.IndexOf(ProfileName);
  596. if (index != -1)
  597. {
  598. ProfilesList.RemoveAt(index);
  599. }
  600. return;
  601. }
  602. try
  603. {
  604. using (Stream stream = File.OpenRead(path))
  605. {
  606. config = JsonHelper.Deserialize<InputConfig>(stream);
  607. }
  608. }
  609. catch (JsonException) { }
  610. catch (InvalidOperationException)
  611. {
  612. Logger.Error?.Print(LogClass.Configuration, $"Profile {ProfileName} is incompatible with the current input configuration system.");
  613. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance.UpdateAndGetDynamicValue(LocaleKeys.DialogProfileInvalidProfileErrorMessage, ProfileName));
  614. return;
  615. }
  616. }
  617. if (config != null)
  618. {
  619. _isLoaded = false;
  620. LoadConfiguration(config);
  621. LoadDevice();
  622. _isLoaded = true;
  623. NotifyChanges();
  624. }
  625. }
  626. public async void SaveProfile()
  627. {
  628. if (Device == 0)
  629. {
  630. return;
  631. }
  632. if (Configuration == null)
  633. {
  634. return;
  635. }
  636. if (ProfileName == LocaleManager.Instance[LocaleKeys.ControllerSettingsProfileDefault])
  637. {
  638. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogProfileDefaultProfileOverwriteErrorMessage]);
  639. return;
  640. }
  641. else
  642. {
  643. bool validFileName = ProfileName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1;
  644. if (validFileName)
  645. {
  646. string path = Path.Combine(GetProfileBasePath(), ProfileName + ".json");
  647. InputConfig config = null;
  648. if (IsKeyboard)
  649. {
  650. config = (Configuration as InputConfiguration<Key, ConfigStickInputId>).GetConfig();
  651. }
  652. else if (IsController)
  653. {
  654. config = (Configuration as InputConfiguration<GamepadInputId, ConfigStickInputId>).GetConfig();
  655. }
  656. config.ControllerType = Controllers[_controller].Type;
  657. string jsonString = JsonHelper.Serialize(config, true);
  658. await File.WriteAllTextAsync(path, jsonString);
  659. LoadProfiles();
  660. }
  661. else
  662. {
  663. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogProfileInvalidProfileNameErrorMessage]);
  664. }
  665. }
  666. }
  667. public async void RemoveProfile()
  668. {
  669. if (Device == 0 || ProfileName == LocaleManager.Instance[LocaleKeys.ControllerSettingsProfileDefault] || ProfilesList.IndexOf(ProfileName) == -1)
  670. {
  671. return;
  672. }
  673. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(
  674. LocaleManager.Instance[LocaleKeys.DialogProfileDeleteProfileTitle],
  675. LocaleManager.Instance[LocaleKeys.DialogProfileDeleteProfileMessage],
  676. LocaleManager.Instance[LocaleKeys.InputDialogYes],
  677. LocaleManager.Instance[LocaleKeys.InputDialogNo],
  678. LocaleManager.Instance[LocaleKeys.RyujinxConfirm]);
  679. if (result == UserResult.Yes)
  680. {
  681. string path = Path.Combine(GetProfileBasePath(), ProfileName + ".json");
  682. if (File.Exists(path))
  683. {
  684. File.Delete(path);
  685. }
  686. LoadProfiles();
  687. }
  688. }
  689. public void Save()
  690. {
  691. IsModified = false;
  692. List<InputConfig> newConfig = new();
  693. newConfig.AddRange(ConfigurationState.Instance.Hid.InputConfig.Value);
  694. newConfig.Remove(newConfig.Find(x => x == null));
  695. if (Device == 0)
  696. {
  697. newConfig.Remove(newConfig.Find(x => x.PlayerIndex == this.PlayerId));
  698. }
  699. else
  700. {
  701. var device = Devices[Device];
  702. if (device.Type == DeviceType.Keyboard)
  703. {
  704. var inputConfig = Configuration as InputConfiguration<Key, ConfigStickInputId>;
  705. inputConfig.Id = device.Id;
  706. }
  707. else
  708. {
  709. var inputConfig = Configuration as InputConfiguration<GamepadInputId, ConfigStickInputId>;
  710. inputConfig.Id = device.Id.Split(" ")[0];
  711. }
  712. var config = !IsController
  713. ? (Configuration as InputConfiguration<Key, ConfigStickInputId>).GetConfig()
  714. : (Configuration as InputConfiguration<GamepadInputId, ConfigStickInputId>).GetConfig();
  715. config.ControllerType = Controllers[_controller].Type;
  716. config.PlayerIndex = _playerId;
  717. int i = newConfig.FindIndex(x => x.PlayerIndex == PlayerId);
  718. if (i == -1)
  719. {
  720. newConfig.Add(config);
  721. }
  722. else
  723. {
  724. newConfig[i] = config;
  725. }
  726. }
  727. _mainWindow.ViewModel.AppHost?.NpadManager.ReloadConfiguration(newConfig, ConfigurationState.Instance.Hid.EnableKeyboard, ConfigurationState.Instance.Hid.EnableMouse);
  728. // Atomically replace and signal input change.
  729. // NOTE: Do not modify InputConfig.Value directly as other code depends on the on-change event.
  730. ConfigurationState.Instance.Hid.InputConfig.Value = newConfig;
  731. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  732. }
  733. public void NotifyChange(string property)
  734. {
  735. OnPropertyChanged(property);
  736. }
  737. public void NotifyChanges()
  738. {
  739. OnPropertyChanged(nameof(Configuration));
  740. OnPropertyChanged(nameof(IsController));
  741. OnPropertyChanged(nameof(ShowSettings));
  742. OnPropertyChanged(nameof(IsKeyboard));
  743. OnPropertyChanged(nameof(IsRight));
  744. OnPropertyChanged(nameof(IsLeft));
  745. }
  746. public void Dispose()
  747. {
  748. _mainWindow.InputManager.GamepadDriver.OnGamepadConnected -= HandleOnGamepadConnected;
  749. _mainWindow.InputManager.GamepadDriver.OnGamepadDisconnected -= HandleOnGamepadDisconnected;
  750. _mainWindow.ViewModel.AppHost?.NpadManager.UnblockInputUpdates();
  751. SelectedGamepad?.Dispose();
  752. AvaloniaKeyboardDriver.Dispose();
  753. }
  754. }
  755. }