ControllerSettingsViewModel.cs 30 KB

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