ControllerSettingsViewModel.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 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.AppHost != null)
  206. {
  207. _mainWindow.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["ControllerSettingsPlayer1"]));
  223. PlayerIndexes.Add(new(PlayerIndex.Player2, LocaleManager.Instance["ControllerSettingsPlayer2"]));
  224. PlayerIndexes.Add(new(PlayerIndex.Player3, LocaleManager.Instance["ControllerSettingsPlayer3"]));
  225. PlayerIndexes.Add(new(PlayerIndex.Player4, LocaleManager.Instance["ControllerSettingsPlayer4"]));
  226. PlayerIndexes.Add(new(PlayerIndex.Player5, LocaleManager.Instance["ControllerSettingsPlayer5"]));
  227. PlayerIndexes.Add(new(PlayerIndex.Player6, LocaleManager.Instance["ControllerSettingsPlayer6"]));
  228. PlayerIndexes.Add(new(PlayerIndex.Player7, LocaleManager.Instance["ControllerSettingsPlayer7"]));
  229. PlayerIndexes.Add(new(PlayerIndex.Player8, LocaleManager.Instance["ControllerSettingsPlayer8"]));
  230. PlayerIndexes.Add(new(PlayerIndex.Handheld, LocaleManager.Instance["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["ControllerSettingsControllerTypeHandheld"]));
  342. Controller = 0;
  343. }
  344. else
  345. {
  346. Controllers.Add(new(ControllerType.ProController, LocaleManager.Instance["ControllerSettingsControllerTypeProController"]));
  347. Controllers.Add(new(ControllerType.JoyconPair, LocaleManager.Instance["ControllerSettingsControllerTypeJoyConPair"]));
  348. Controllers.Add(new(ControllerType.JoyconLeft, LocaleManager.Instance["ControllerSettingsControllerTypeJoyConLeft"]));
  349. Controllers.Add(new(ControllerType.JoyconRight, LocaleManager.Instance["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.Substring(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["ControllerSettingsDeviceDisabled"]));
  383. foreach (string id in _mainWindow.InputManager.KeyboardDriver.GamepadsIds)
  384. {
  385. using IGamepad gamepad = _mainWindow.InputManager.KeyboardDriver.GetGamepad(id);
  386. Logger.Info?.Print(LogClass.Configuration, $"{GetShortGamepadName(gamepad.Name)} has been connected with ID: {gamepad.Id}");
  387. if (gamepad != null)
  388. {
  389. Devices.Add((DeviceType.Keyboard, id, $"{GetShortGamepadName(gamepad.Name)}"));
  390. }
  391. }
  392. foreach (string id in _mainWindow.InputManager.GamepadDriver.GamepadsIds)
  393. {
  394. using IGamepad gamepad = _mainWindow.InputManager.GamepadDriver.GetGamepad(id);
  395. Logger.Info?.Print(LogClass.Configuration, $"{GetShortGamepadName(gamepad.Name)} has been connected with ID: {gamepad.Id}");
  396. if (gamepad != null)
  397. {
  398. if (Devices.Any(controller => GetShortGamepadId(controller.Id) == GetShortGamepadId(gamepad.Id)))
  399. {
  400. _controllerNumber++;
  401. }
  402. Devices.Add((DeviceType.Controller, id, $"{GetShortGamepadName(gamepad.Name)} ({_controllerNumber})"));
  403. }
  404. }
  405. _controllerNumber = 0;
  406. DeviceList.AddRange(Devices.Select(x => x.Name));
  407. Device = Math.Min(Device, DeviceList.Count);
  408. }
  409. }
  410. private string GetProfileBasePath()
  411. {
  412. string path = AppDataManager.ProfilesDirPath;
  413. var type = Devices[Device == -1 ? 0 : Device].Type;
  414. if (type == DeviceType.Keyboard)
  415. {
  416. path = Path.Combine(path, KeyboardString);
  417. }
  418. else if (type == DeviceType.Controller)
  419. {
  420. path = Path.Combine(path, ControllerString);
  421. }
  422. return path;
  423. }
  424. private void LoadProfiles()
  425. {
  426. ProfilesList.Clear();
  427. string basePath = GetProfileBasePath();
  428. if (!Directory.Exists(basePath))
  429. {
  430. Directory.CreateDirectory(basePath);
  431. }
  432. ProfilesList.Add((LocaleManager.Instance["ControllerSettingsProfileDefault"]));
  433. foreach (string profile in Directory.GetFiles(basePath, "*.json", SearchOption.AllDirectories))
  434. {
  435. ProfilesList.Add(Path.GetFileNameWithoutExtension(profile));
  436. }
  437. if (string.IsNullOrWhiteSpace(ProfileName))
  438. {
  439. ProfileName = LocaleManager.Instance["ControllerSettingsProfileDefault"];
  440. }
  441. }
  442. public InputConfig LoadDefaultConfiguration()
  443. {
  444. var activeDevice = Devices.FirstOrDefault();
  445. if (Devices.Count > 0 && Device < Devices.Count && Device >= 0)
  446. {
  447. activeDevice = Devices[Device];
  448. }
  449. InputConfig config;
  450. if (activeDevice.Type == DeviceType.Keyboard)
  451. {
  452. string id = activeDevice.Id;
  453. config = new StandardKeyboardInputConfig
  454. {
  455. Version = Ryujinx.Common.Configuration.Hid.InputConfig.CurrentVersion,
  456. Backend = InputBackendType.WindowKeyboard,
  457. Id = id,
  458. ControllerType = ControllerType.ProController,
  459. LeftJoycon = new LeftJoyconCommonConfig<Key>
  460. {
  461. DpadUp = Key.Up,
  462. DpadDown = Key.Down,
  463. DpadLeft = Key.Left,
  464. DpadRight = Key.Right,
  465. ButtonMinus = Key.Minus,
  466. ButtonL = Key.E,
  467. ButtonZl = Key.Q,
  468. ButtonSl = Key.Unbound,
  469. ButtonSr = Key.Unbound
  470. },
  471. LeftJoyconStick =
  472. new JoyconConfigKeyboardStick<Key>
  473. {
  474. StickUp = Key.W,
  475. StickDown = Key.S,
  476. StickLeft = Key.A,
  477. StickRight = Key.D,
  478. StickButton = Key.F
  479. },
  480. RightJoycon = new RightJoyconCommonConfig<Key>
  481. {
  482. ButtonA = Key.Z,
  483. ButtonB = Key.X,
  484. ButtonX = Key.C,
  485. ButtonY = Key.V,
  486. ButtonPlus = Key.Plus,
  487. ButtonR = Key.U,
  488. ButtonZr = Key.O,
  489. ButtonSl = Key.Unbound,
  490. ButtonSr = Key.Unbound
  491. },
  492. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  493. {
  494. StickUp = Key.I,
  495. StickDown = Key.K,
  496. StickLeft = Key.J,
  497. StickRight = Key.L,
  498. StickButton = Key.H
  499. }
  500. };
  501. }
  502. else if (activeDevice.Type == DeviceType.Controller)
  503. {
  504. bool isNintendoStyle = Devices.ToList().Find(x => x.Id == activeDevice.Id).Name.Contains("Nintendo");
  505. string id = activeDevice.Id.Split(" ")[0];
  506. config = new StandardControllerInputConfig
  507. {
  508. Version = Ryujinx.Common.Configuration.Hid.InputConfig.CurrentVersion,
  509. Backend = InputBackendType.GamepadSDL2,
  510. Id = id,
  511. ControllerType = ControllerType.ProController,
  512. DeadzoneLeft = 0.1f,
  513. DeadzoneRight = 0.1f,
  514. RangeLeft = 1.0f,
  515. RangeRight = 1.0f,
  516. TriggerThreshold = 0.5f,
  517. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  518. {
  519. DpadUp = ConfigGamepadInputId.DpadUp,
  520. DpadDown = ConfigGamepadInputId.DpadDown,
  521. DpadLeft = ConfigGamepadInputId.DpadLeft,
  522. DpadRight = ConfigGamepadInputId.DpadRight,
  523. ButtonMinus = ConfigGamepadInputId.Minus,
  524. ButtonL = ConfigGamepadInputId.LeftShoulder,
  525. ButtonZl = ConfigGamepadInputId.LeftTrigger,
  526. ButtonSl = ConfigGamepadInputId.Unbound,
  527. ButtonSr = ConfigGamepadInputId.Unbound
  528. },
  529. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  530. {
  531. Joystick = ConfigStickInputId.Left,
  532. StickButton = ConfigGamepadInputId.LeftStick,
  533. InvertStickX = false,
  534. InvertStickY = false
  535. },
  536. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  537. {
  538. ButtonA = isNintendoStyle ? ConfigGamepadInputId.A : ConfigGamepadInputId.B,
  539. ButtonB = isNintendoStyle ? ConfigGamepadInputId.B : ConfigGamepadInputId.A,
  540. ButtonX = isNintendoStyle ? ConfigGamepadInputId.X : ConfigGamepadInputId.Y,
  541. ButtonY = isNintendoStyle ? ConfigGamepadInputId.Y : ConfigGamepadInputId.X,
  542. ButtonPlus = ConfigGamepadInputId.Plus,
  543. ButtonR = ConfigGamepadInputId.RightShoulder,
  544. ButtonZr = ConfigGamepadInputId.RightTrigger,
  545. ButtonSl = ConfigGamepadInputId.Unbound,
  546. ButtonSr = ConfigGamepadInputId.Unbound
  547. },
  548. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  549. {
  550. Joystick = ConfigStickInputId.Right,
  551. StickButton = ConfigGamepadInputId.RightStick,
  552. InvertStickX = false,
  553. InvertStickY = false
  554. },
  555. Motion = new StandardMotionConfigController
  556. {
  557. MotionBackend = MotionInputBackendType.GamepadDriver,
  558. EnableMotion = true,
  559. Sensitivity = 100,
  560. GyroDeadzone = 1
  561. },
  562. Rumble = new RumbleConfigController
  563. {
  564. StrongRumble = 1f,
  565. WeakRumble = 1f,
  566. EnableRumble = false
  567. }
  568. };
  569. }
  570. else
  571. {
  572. config = new InputConfig();
  573. }
  574. config.PlayerIndex = _playerId;
  575. return config;
  576. }
  577. public async void LoadProfile()
  578. {
  579. if (Device == 0)
  580. {
  581. return;
  582. }
  583. InputConfig config = null;
  584. if (string.IsNullOrWhiteSpace(ProfileName))
  585. {
  586. return;
  587. }
  588. if (ProfileName == LocaleManager.Instance["ControllerSettingsProfileDefault"])
  589. {
  590. config = LoadDefaultConfiguration();
  591. }
  592. else
  593. {
  594. string path = Path.Combine(GetProfileBasePath(), ProfileName + ".json");
  595. if (!File.Exists(path))
  596. {
  597. var index = ProfilesList.IndexOf(ProfileName);
  598. if (index != -1)
  599. {
  600. ProfilesList.RemoveAt(index);
  601. }
  602. return;
  603. }
  604. try
  605. {
  606. using (Stream stream = File.OpenRead(path))
  607. {
  608. config = JsonHelper.Deserialize<InputConfig>(stream);
  609. }
  610. }
  611. catch (JsonException) { }
  612. catch (InvalidOperationException)
  613. {
  614. Logger.Error?.Print(LogClass.Configuration, $"Profile {ProfileName} is incompatible with the current input configuration system.");
  615. await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance["DialogProfileInvalidProfileErrorMessage"], ProfileName));
  616. return;
  617. }
  618. }
  619. if (config != null)
  620. {
  621. _isLoaded = false;
  622. LoadConfiguration(config);
  623. LoadDevice();
  624. _isLoaded = true;
  625. NotifyChanges();
  626. }
  627. }
  628. public async void SaveProfile()
  629. {
  630. if (Device == 0)
  631. {
  632. return;
  633. }
  634. if (Configuration == null)
  635. {
  636. return;
  637. }
  638. if (ProfileName == LocaleManager.Instance["ControllerSettingsProfileDefault"])
  639. {
  640. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogProfileDefaultProfileOverwriteErrorMessage"]);
  641. return;
  642. }
  643. else
  644. {
  645. bool validFileName = ProfileName.IndexOfAny(Path.GetInvalidFileNameChars()) == -1;
  646. if (validFileName)
  647. {
  648. string path = Path.Combine(GetProfileBasePath(), ProfileName + ".json");
  649. InputConfig config = null;
  650. if (IsKeyboard)
  651. {
  652. config = (Configuration as InputConfiguration<Key, ConfigStickInputId>).GetConfig();
  653. }
  654. else if (IsController)
  655. {
  656. config = (Configuration as InputConfiguration<GamepadInputId, ConfigStickInputId>).GetConfig();
  657. }
  658. config.ControllerType = Controllers[_controller].Type;
  659. string jsonString = JsonHelper.Serialize(config, true);
  660. await File.WriteAllTextAsync(path, jsonString);
  661. LoadProfiles();
  662. }
  663. else
  664. {
  665. await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogProfileInvalidProfileNameErrorMessage"]);
  666. }
  667. }
  668. }
  669. public async void RemoveProfile()
  670. {
  671. if (Device == 0 || ProfileName == LocaleManager.Instance["ControllerSettingsProfileDefault"] || ProfilesList.IndexOf(ProfileName) == -1)
  672. {
  673. return;
  674. }
  675. UserResult result = await ContentDialogHelper.CreateConfirmationDialog(
  676. LocaleManager.Instance["DialogProfileDeleteProfileTitle"],
  677. LocaleManager.Instance["DialogProfileDeleteProfileMessage"],
  678. LocaleManager.Instance["InputDialogYes"],
  679. LocaleManager.Instance["InputDialogNo"],
  680. LocaleManager.Instance["RyujinxConfirm"]);
  681. if (result == UserResult.Yes)
  682. {
  683. string path = Path.Combine(GetProfileBasePath(), ProfileName + ".json");
  684. if (File.Exists(path))
  685. {
  686. File.Delete(path);
  687. }
  688. LoadProfiles();
  689. }
  690. }
  691. public void Save()
  692. {
  693. IsModified = false;
  694. List<InputConfig> newConfig = new();
  695. newConfig.AddRange(ConfigurationState.Instance.Hid.InputConfig.Value);
  696. newConfig.Remove(newConfig.Find(x => x == null));
  697. if (Device == 0)
  698. {
  699. newConfig.Remove(newConfig.Find(x => x.PlayerIndex == this.PlayerId));
  700. }
  701. else
  702. {
  703. var device = Devices[Device];
  704. if (device.Type == DeviceType.Keyboard)
  705. {
  706. var inputConfig = Configuration as InputConfiguration<Key, ConfigStickInputId>;
  707. inputConfig.Id = device.Id;
  708. }
  709. else
  710. {
  711. var inputConfig = Configuration as InputConfiguration<GamepadInputId, ConfigStickInputId>;
  712. inputConfig.Id = device.Id.Split(" ")[0];
  713. }
  714. var config = !IsController
  715. ? (Configuration as InputConfiguration<Key, ConfigStickInputId>).GetConfig()
  716. : (Configuration as InputConfiguration<GamepadInputId, ConfigStickInputId>).GetConfig();
  717. config.ControllerType = Controllers[_controller].Type;
  718. config.PlayerIndex = _playerId;
  719. int i = newConfig.FindIndex(x => x.PlayerIndex == PlayerId);
  720. if (i == -1)
  721. {
  722. newConfig.Add(config);
  723. }
  724. else
  725. {
  726. newConfig[i] = config;
  727. }
  728. }
  729. _mainWindow.AppHost?.NpadManager.ReloadConfiguration(newConfig, ConfigurationState.Instance.Hid.EnableKeyboard, ConfigurationState.Instance.Hid.EnableMouse);
  730. // Atomically replace and signal input change.
  731. // NOTE: Do not modify InputConfig.Value directly as other code depends on the on-change event.
  732. ConfigurationState.Instance.Hid.InputConfig.Value = newConfig;
  733. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  734. }
  735. public void NotifyChange(string property)
  736. {
  737. OnPropertyChanged(property);
  738. }
  739. public void NotifyChanges()
  740. {
  741. OnPropertyChanged(nameof(Configuration));
  742. OnPropertyChanged(nameof(IsController));
  743. OnPropertyChanged(nameof(ShowSettings));
  744. OnPropertyChanged(nameof(IsKeyboard));
  745. OnPropertyChanged(nameof(IsRight));
  746. OnPropertyChanged(nameof(IsLeft));
  747. }
  748. public void Dispose()
  749. {
  750. _mainWindow.InputManager.GamepadDriver.OnGamepadConnected -= HandleOnGamepadConnected;
  751. _mainWindow.InputManager.GamepadDriver.OnGamepadDisconnected -= HandleOnGamepadDisconnected;
  752. _mainWindow.AppHost?.NpadManager.UnblockInputUpdates();
  753. SelectedGamepad?.Dispose();
  754. AvaloniaKeyboardDriver.Dispose();
  755. }
  756. }
  757. }