GamepadInputConfig.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. using Avalonia.Media;
  2. using Ryujinx.Ava.UI.ViewModels;
  3. using Ryujinx.Common.Configuration.Hid;
  4. using Ryujinx.Common.Configuration.Hid.Controller;
  5. using Ryujinx.Common.Configuration.Hid.Controller.Motion;
  6. using System;
  7. namespace Ryujinx.Ava.UI.Models.Input
  8. {
  9. public class GamepadInputConfig : BaseModel
  10. {
  11. public bool EnableCemuHookMotion { get; set; }
  12. public string DsuServerHost { get; set; }
  13. public int DsuServerPort { get; set; }
  14. public int Slot { get; set; }
  15. public int AltSlot { get; set; }
  16. public bool MirrorInput { get; set; }
  17. public int Sensitivity { get; set; }
  18. public double GyroDeadzone { get; set; }
  19. public float WeakRumble { get; set; }
  20. public float StrongRumble { get; set; }
  21. public string Id { get; set; }
  22. public ControllerType ControllerType { get; set; }
  23. public PlayerIndex PlayerIndex { get; set; }
  24. private StickInputId _leftJoystick;
  25. public StickInputId LeftJoystick
  26. {
  27. get => _leftJoystick;
  28. set
  29. {
  30. _leftJoystick = value;
  31. OnPropertyChanged();
  32. }
  33. }
  34. private bool _leftInvertStickX;
  35. public bool LeftInvertStickX
  36. {
  37. get => _leftInvertStickX;
  38. set
  39. {
  40. _leftInvertStickX = value;
  41. OnPropertyChanged();
  42. }
  43. }
  44. private bool _leftInvertStickY;
  45. public bool LeftInvertStickY
  46. {
  47. get => _leftInvertStickY;
  48. set
  49. {
  50. _leftInvertStickY = value;
  51. OnPropertyChanged();
  52. }
  53. }
  54. private bool _leftRotate90;
  55. public bool LeftRotate90
  56. {
  57. get => _leftRotate90;
  58. set
  59. {
  60. _leftRotate90 = value;
  61. OnPropertyChanged();
  62. }
  63. }
  64. private GamepadInputId _leftStickButton;
  65. public GamepadInputId LeftStickButton
  66. {
  67. get => _leftStickButton;
  68. set
  69. {
  70. _leftStickButton = value;
  71. OnPropertyChanged();
  72. }
  73. }
  74. private StickInputId _rightJoystick;
  75. public StickInputId RightJoystick
  76. {
  77. get => _rightJoystick;
  78. set
  79. {
  80. _rightJoystick = value;
  81. OnPropertyChanged();
  82. }
  83. }
  84. private bool _rightInvertStickX;
  85. public bool RightInvertStickX
  86. {
  87. get => _rightInvertStickX;
  88. set
  89. {
  90. _rightInvertStickX = value;
  91. OnPropertyChanged();
  92. }
  93. }
  94. private bool _rightInvertStickY;
  95. public bool RightInvertStickY
  96. {
  97. get => _rightInvertStickY;
  98. set
  99. {
  100. _rightInvertStickY = value;
  101. OnPropertyChanged();
  102. }
  103. }
  104. private bool _rightRotate90;
  105. public bool RightRotate90
  106. {
  107. get => _rightRotate90;
  108. set
  109. {
  110. _rightRotate90 = value;
  111. OnPropertyChanged();
  112. }
  113. }
  114. private GamepadInputId _rightStickButton;
  115. public GamepadInputId RightStickButton
  116. {
  117. get => _rightStickButton;
  118. set
  119. {
  120. _rightStickButton = value;
  121. OnPropertyChanged();
  122. }
  123. }
  124. private GamepadInputId _dpadUp;
  125. public GamepadInputId DpadUp
  126. {
  127. get => _dpadUp;
  128. set
  129. {
  130. _dpadUp = value;
  131. OnPropertyChanged();
  132. }
  133. }
  134. private GamepadInputId _dpadDown;
  135. public GamepadInputId DpadDown
  136. {
  137. get => _dpadDown;
  138. set
  139. {
  140. _dpadDown = value;
  141. OnPropertyChanged();
  142. }
  143. }
  144. private GamepadInputId _dpadLeft;
  145. public GamepadInputId DpadLeft
  146. {
  147. get => _dpadLeft;
  148. set
  149. {
  150. _dpadLeft = value;
  151. OnPropertyChanged();
  152. }
  153. }
  154. private GamepadInputId _dpadRight;
  155. public GamepadInputId DpadRight
  156. {
  157. get => _dpadRight;
  158. set
  159. {
  160. _dpadRight = value;
  161. OnPropertyChanged();
  162. }
  163. }
  164. private GamepadInputId _buttonL;
  165. public GamepadInputId ButtonL
  166. {
  167. get => _buttonL;
  168. set
  169. {
  170. _buttonL = value;
  171. OnPropertyChanged();
  172. }
  173. }
  174. private GamepadInputId _buttonMinus;
  175. public GamepadInputId ButtonMinus
  176. {
  177. get => _buttonMinus;
  178. set
  179. {
  180. _buttonMinus = value;
  181. OnPropertyChanged();
  182. }
  183. }
  184. private GamepadInputId _leftButtonSl;
  185. public GamepadInputId LeftButtonSl
  186. {
  187. get => _leftButtonSl;
  188. set
  189. {
  190. _leftButtonSl = value;
  191. OnPropertyChanged();
  192. }
  193. }
  194. private GamepadInputId _leftButtonSr;
  195. public GamepadInputId LeftButtonSr
  196. {
  197. get => _leftButtonSr;
  198. set
  199. {
  200. _leftButtonSr = value;
  201. OnPropertyChanged();
  202. }
  203. }
  204. private GamepadInputId _buttonZl;
  205. public GamepadInputId ButtonZl
  206. {
  207. get => _buttonZl;
  208. set
  209. {
  210. _buttonZl = value;
  211. OnPropertyChanged();
  212. }
  213. }
  214. private GamepadInputId _buttonA;
  215. public GamepadInputId ButtonA
  216. {
  217. get => _buttonA;
  218. set
  219. {
  220. _buttonA = value;
  221. OnPropertyChanged();
  222. }
  223. }
  224. private GamepadInputId _buttonB;
  225. public GamepadInputId ButtonB
  226. {
  227. get => _buttonB;
  228. set
  229. {
  230. _buttonB = value;
  231. OnPropertyChanged();
  232. }
  233. }
  234. private GamepadInputId _buttonX;
  235. public GamepadInputId ButtonX
  236. {
  237. get => _buttonX;
  238. set
  239. {
  240. _buttonX = value;
  241. OnPropertyChanged();
  242. }
  243. }
  244. private GamepadInputId _buttonY;
  245. public GamepadInputId ButtonY
  246. {
  247. get => _buttonY;
  248. set
  249. {
  250. _buttonY = value;
  251. OnPropertyChanged();
  252. }
  253. }
  254. private GamepadInputId _buttonR;
  255. public GamepadInputId ButtonR
  256. {
  257. get => _buttonR;
  258. set
  259. {
  260. _buttonR = value;
  261. OnPropertyChanged();
  262. }
  263. }
  264. private GamepadInputId _buttonPlus;
  265. public GamepadInputId ButtonPlus
  266. {
  267. get => _buttonPlus;
  268. set
  269. {
  270. _buttonPlus = value;
  271. OnPropertyChanged();
  272. }
  273. }
  274. private GamepadInputId _rightButtonSl;
  275. public GamepadInputId RightButtonSl
  276. {
  277. get => _rightButtonSl;
  278. set
  279. {
  280. _rightButtonSl = value;
  281. OnPropertyChanged();
  282. }
  283. }
  284. private GamepadInputId _rightButtonSr;
  285. public GamepadInputId RightButtonSr
  286. {
  287. get => _rightButtonSr;
  288. set
  289. {
  290. _rightButtonSr = value;
  291. OnPropertyChanged();
  292. }
  293. }
  294. private GamepadInputId _buttonZr;
  295. public GamepadInputId ButtonZr
  296. {
  297. get => _buttonZr;
  298. set
  299. {
  300. _buttonZr = value;
  301. OnPropertyChanged();
  302. }
  303. }
  304. private float _deadzoneLeft;
  305. public float DeadzoneLeft
  306. {
  307. get => _deadzoneLeft;
  308. set
  309. {
  310. _deadzoneLeft = MathF.Round(value, 3);
  311. OnPropertyChanged();
  312. }
  313. }
  314. private float _deadzoneRight;
  315. public float DeadzoneRight
  316. {
  317. get => _deadzoneRight;
  318. set
  319. {
  320. _deadzoneRight = MathF.Round(value, 3);
  321. OnPropertyChanged();
  322. }
  323. }
  324. private float _rangeLeft;
  325. public float RangeLeft
  326. {
  327. get => _rangeLeft;
  328. set
  329. {
  330. _rangeLeft = MathF.Round(value, 3);
  331. OnPropertyChanged();
  332. }
  333. }
  334. private float _rangeRight;
  335. public float RangeRight
  336. {
  337. get => _rangeRight;
  338. set
  339. {
  340. _rangeRight = MathF.Round(value, 3);
  341. OnPropertyChanged();
  342. }
  343. }
  344. private float _triggerThreshold;
  345. public float TriggerThreshold
  346. {
  347. get => _triggerThreshold;
  348. set
  349. {
  350. _triggerThreshold = MathF.Round(value, 3);
  351. OnPropertyChanged();
  352. }
  353. }
  354. private bool _enableLedChanging;
  355. public bool EnableLedChanging
  356. {
  357. get => _enableLedChanging;
  358. set
  359. {
  360. _enableLedChanging = value;
  361. OnPropertyChanged();
  362. }
  363. }
  364. private Color _ledColor;
  365. public Color LedColor
  366. {
  367. get => _ledColor;
  368. set
  369. {
  370. _ledColor = value;
  371. OnPropertyChanged();
  372. }
  373. }
  374. private bool _enableMotion;
  375. public bool EnableMotion
  376. {
  377. get => _enableMotion;
  378. set
  379. {
  380. _enableMotion = value;
  381. OnPropertyChanged();
  382. }
  383. }
  384. private bool _enableRumble;
  385. public bool EnableRumble
  386. {
  387. get => _enableRumble;
  388. set
  389. {
  390. _enableRumble = value;
  391. OnPropertyChanged();
  392. }
  393. }
  394. public GamepadInputConfig(InputConfig config)
  395. {
  396. if (config != null)
  397. {
  398. Id = config.Id;
  399. ControllerType = config.ControllerType;
  400. PlayerIndex = config.PlayerIndex;
  401. if (config is not StandardControllerInputConfig controllerInput)
  402. {
  403. return;
  404. }
  405. LeftJoystick = controllerInput.LeftJoyconStick.Joystick;
  406. LeftInvertStickX = controllerInput.LeftJoyconStick.InvertStickX;
  407. LeftInvertStickY = controllerInput.LeftJoyconStick.InvertStickY;
  408. LeftRotate90 = controllerInput.LeftJoyconStick.Rotate90CW;
  409. LeftStickButton = controllerInput.LeftJoyconStick.StickButton;
  410. RightJoystick = controllerInput.RightJoyconStick.Joystick;
  411. RightInvertStickX = controllerInput.RightJoyconStick.InvertStickX;
  412. RightInvertStickY = controllerInput.RightJoyconStick.InvertStickY;
  413. RightRotate90 = controllerInput.RightJoyconStick.Rotate90CW;
  414. RightStickButton = controllerInput.RightJoyconStick.StickButton;
  415. DpadUp = controllerInput.LeftJoycon.DpadUp;
  416. DpadDown = controllerInput.LeftJoycon.DpadDown;
  417. DpadLeft = controllerInput.LeftJoycon.DpadLeft;
  418. DpadRight = controllerInput.LeftJoycon.DpadRight;
  419. ButtonL = controllerInput.LeftJoycon.ButtonL;
  420. ButtonMinus = controllerInput.LeftJoycon.ButtonMinus;
  421. LeftButtonSl = controllerInput.LeftJoycon.ButtonSl;
  422. LeftButtonSr = controllerInput.LeftJoycon.ButtonSr;
  423. ButtonZl = controllerInput.LeftJoycon.ButtonZl;
  424. ButtonA = controllerInput.RightJoycon.ButtonA;
  425. ButtonB = controllerInput.RightJoycon.ButtonB;
  426. ButtonX = controllerInput.RightJoycon.ButtonX;
  427. ButtonY = controllerInput.RightJoycon.ButtonY;
  428. ButtonR = controllerInput.RightJoycon.ButtonR;
  429. ButtonPlus = controllerInput.RightJoycon.ButtonPlus;
  430. RightButtonSl = controllerInput.RightJoycon.ButtonSl;
  431. RightButtonSr = controllerInput.RightJoycon.ButtonSr;
  432. ButtonZr = controllerInput.RightJoycon.ButtonZr;
  433. DeadzoneLeft = controllerInput.DeadzoneLeft;
  434. DeadzoneRight = controllerInput.DeadzoneRight;
  435. RangeLeft = controllerInput.RangeLeft;
  436. RangeRight = controllerInput.RangeRight;
  437. TriggerThreshold = controllerInput.TriggerThreshold;
  438. if (controllerInput.Motion != null)
  439. {
  440. EnableMotion = controllerInput.Motion.EnableMotion;
  441. GyroDeadzone = controllerInput.Motion.GyroDeadzone;
  442. Sensitivity = controllerInput.Motion.Sensitivity;
  443. if (controllerInput.Motion is CemuHookMotionConfigController cemuHook)
  444. {
  445. EnableCemuHookMotion = true;
  446. DsuServerHost = cemuHook.DsuServerHost;
  447. DsuServerPort = cemuHook.DsuServerPort;
  448. Slot = cemuHook.Slot;
  449. AltSlot = cemuHook.AltSlot;
  450. MirrorInput = cemuHook.MirrorInput;
  451. }
  452. }
  453. if (controllerInput.Rumble != null)
  454. {
  455. EnableRumble = controllerInput.Rumble.EnableRumble;
  456. WeakRumble = controllerInput.Rumble.WeakRumble;
  457. StrongRumble = controllerInput.Rumble.StrongRumble;
  458. }
  459. if (controllerInput.Led != null)
  460. {
  461. EnableLedChanging = controllerInput.Led.EnableLed;
  462. uint rawColor = controllerInput.Led.LedColor;
  463. byte alpha = (byte)(rawColor >> 24);
  464. byte red = (byte)(rawColor >> 16);
  465. byte green = (byte)(rawColor >> 8);
  466. byte blue = (byte)(rawColor % 256);
  467. LedColor = new Color(alpha, red, green, blue);
  468. }
  469. }
  470. }
  471. public InputConfig GetConfig()
  472. {
  473. StandardControllerInputConfig config = new()
  474. {
  475. Id = Id,
  476. Backend = InputBackendType.GamepadSDL2,
  477. PlayerIndex = PlayerIndex,
  478. ControllerType = ControllerType,
  479. LeftJoycon = new LeftJoyconCommonConfig<GamepadInputId>
  480. {
  481. DpadUp = DpadUp,
  482. DpadDown = DpadDown,
  483. DpadLeft = DpadLeft,
  484. DpadRight = DpadRight,
  485. ButtonL = ButtonL,
  486. ButtonMinus = ButtonMinus,
  487. ButtonSl = LeftButtonSl,
  488. ButtonSr = LeftButtonSr,
  489. ButtonZl = ButtonZl,
  490. },
  491. RightJoycon = new RightJoyconCommonConfig<GamepadInputId>
  492. {
  493. ButtonA = ButtonA,
  494. ButtonB = ButtonB,
  495. ButtonX = ButtonX,
  496. ButtonY = ButtonY,
  497. ButtonPlus = ButtonPlus,
  498. ButtonSl = RightButtonSl,
  499. ButtonSr = RightButtonSr,
  500. ButtonR = ButtonR,
  501. ButtonZr = ButtonZr,
  502. },
  503. LeftJoyconStick = new JoyconConfigControllerStick<GamepadInputId, StickInputId>
  504. {
  505. Joystick = LeftJoystick,
  506. InvertStickX = LeftInvertStickX,
  507. InvertStickY = LeftInvertStickY,
  508. Rotate90CW = LeftRotate90,
  509. StickButton = LeftStickButton,
  510. },
  511. RightJoyconStick = new JoyconConfigControllerStick<GamepadInputId, StickInputId>
  512. {
  513. Joystick = RightJoystick,
  514. InvertStickX = RightInvertStickX,
  515. InvertStickY = RightInvertStickY,
  516. Rotate90CW = RightRotate90,
  517. StickButton = RightStickButton,
  518. },
  519. Rumble = new RumbleConfigController
  520. {
  521. EnableRumble = EnableRumble,
  522. WeakRumble = WeakRumble,
  523. StrongRumble = StrongRumble,
  524. },
  525. Led = new LedConfigController
  526. {
  527. EnableLed = EnableLedChanging,
  528. LedColor = LedColor.ToUInt32()
  529. },
  530. Version = InputConfig.CurrentVersion,
  531. DeadzoneLeft = DeadzoneLeft,
  532. DeadzoneRight = DeadzoneRight,
  533. RangeLeft = RangeLeft,
  534. RangeRight = RangeRight,
  535. TriggerThreshold = TriggerThreshold,
  536. };
  537. if (EnableCemuHookMotion)
  538. {
  539. config.Motion = new CemuHookMotionConfigController
  540. {
  541. EnableMotion = EnableMotion,
  542. MotionBackend = MotionInputBackendType.CemuHook,
  543. GyroDeadzone = GyroDeadzone,
  544. Sensitivity = Sensitivity,
  545. DsuServerHost = DsuServerHost,
  546. DsuServerPort = DsuServerPort,
  547. Slot = Slot,
  548. AltSlot = AltSlot,
  549. MirrorInput = MirrorInput,
  550. };
  551. }
  552. else
  553. {
  554. config.Motion = new StandardMotionConfigController
  555. {
  556. EnableMotion = EnableMotion,
  557. MotionBackend = MotionInputBackendType.GamepadDriver,
  558. GyroDeadzone = GyroDeadzone,
  559. Sensitivity = Sensitivity,
  560. };
  561. }
  562. return config;
  563. }
  564. }
  565. }