InputConfiguration.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. using Ryujinx.Ava.UI.ViewModels;
  2. using Ryujinx.Common.Configuration.Hid;
  3. using Ryujinx.Common.Configuration.Hid.Controller;
  4. using Ryujinx.Common.Configuration.Hid.Controller.Motion;
  5. using Ryujinx.Common.Configuration.Hid.Keyboard;
  6. using System;
  7. namespace Ryujinx.Ava.UI.Models
  8. {
  9. internal class InputConfiguration<Key, Stick> : BaseModel
  10. {
  11. private float _deadzoneRight;
  12. private float _triggerThreshold;
  13. private float _deadzoneLeft;
  14. private double _gyroDeadzone;
  15. private int _sensitivity;
  16. private bool enableMotion;
  17. private float weakRumble;
  18. private float strongRumble;
  19. private float _rangeLeft;
  20. private float _rangeRight;
  21. public InputBackendType Backend { get; set; }
  22. /// <summary>
  23. /// Controller id
  24. /// </summary>
  25. public string Id { get; set; }
  26. /// <summary>
  27. /// Controller's Type
  28. /// </summary>
  29. public ControllerType ControllerType { get; set; }
  30. /// <summary>
  31. /// Player's Index for the controller
  32. /// </summary>
  33. public PlayerIndex PlayerIndex { get; set; }
  34. public Stick LeftJoystick { get; set; }
  35. public bool LeftInvertStickX { get; set; }
  36. public bool LeftInvertStickY { get; set; }
  37. public bool RightRotate90 { get; set; }
  38. public Key LeftControllerStickButton { get; set; }
  39. public Stick RightJoystick { get; set; }
  40. public bool RightInvertStickX { get; set; }
  41. public bool RightInvertStickY { get; set; }
  42. public bool LeftRotate90 { get; set; }
  43. public Key RightControllerStickButton { get; set; }
  44. public float DeadzoneLeft
  45. {
  46. get => _deadzoneLeft;
  47. set
  48. {
  49. _deadzoneLeft = MathF.Round(value, 3);
  50. OnPropertyChanged();
  51. }
  52. }
  53. public float RangeLeft
  54. {
  55. get => _rangeLeft;
  56. set
  57. {
  58. _rangeLeft = MathF.Round(value, 3);
  59. OnPropertyChanged();
  60. }
  61. }
  62. public float DeadzoneRight
  63. {
  64. get => _deadzoneRight;
  65. set
  66. {
  67. _deadzoneRight = MathF.Round(value, 3);
  68. OnPropertyChanged();
  69. }
  70. }
  71. public float RangeRight
  72. {
  73. get => _rangeRight;
  74. set
  75. {
  76. _rangeRight = MathF.Round(value, 3);
  77. OnPropertyChanged();
  78. }
  79. }
  80. public float TriggerThreshold
  81. {
  82. get => _triggerThreshold;
  83. set
  84. {
  85. _triggerThreshold = MathF.Round(value, 3);
  86. OnPropertyChanged();
  87. }
  88. }
  89. public MotionInputBackendType MotionBackend { get; set; }
  90. public Key ButtonMinus { get; set; }
  91. public Key ButtonL { get; set; }
  92. public Key ButtonZl { get; set; }
  93. public Key LeftButtonSl { get; set; }
  94. public Key LeftButtonSr { get; set; }
  95. public Key DpadUp { get; set; }
  96. public Key DpadDown { get; set; }
  97. public Key DpadLeft { get; set; }
  98. public Key DpadRight { get; set; }
  99. public Key ButtonPlus { get; set; }
  100. public Key ButtonR { get; set; }
  101. public Key ButtonZr { get; set; }
  102. public Key RightButtonSl { get; set; }
  103. public Key RightButtonSr { get; set; }
  104. public Key ButtonX { get; set; }
  105. public Key ButtonB { get; set; }
  106. public Key ButtonY { get; set; }
  107. public Key ButtonA { get; set; }
  108. public Key LeftStickUp { get; set; }
  109. public Key LeftStickDown { get; set; }
  110. public Key LeftStickLeft { get; set; }
  111. public Key LeftStickRight { get; set; }
  112. public Key LeftKeyboardStickButton { get; set; }
  113. public Key RightStickUp { get; set; }
  114. public Key RightStickDown { get; set; }
  115. public Key RightStickLeft { get; set; }
  116. public Key RightStickRight { get; set; }
  117. public Key RightKeyboardStickButton { get; set; }
  118. public int Sensitivity
  119. {
  120. get => _sensitivity;
  121. set
  122. {
  123. _sensitivity = value;
  124. OnPropertyChanged();
  125. }
  126. }
  127. public double GyroDeadzone
  128. {
  129. get => _gyroDeadzone;
  130. set
  131. {
  132. _gyroDeadzone = Math.Round(value, 3);
  133. OnPropertyChanged();
  134. }
  135. }
  136. public bool EnableMotion
  137. {
  138. get => enableMotion; set
  139. {
  140. enableMotion = value;
  141. OnPropertyChanged();
  142. }
  143. }
  144. public bool EnableCemuHookMotion { get; set; }
  145. public int Slot { get; set; }
  146. public int AltSlot { get; set; }
  147. public bool MirrorInput { get; set; }
  148. public string DsuServerHost { get; set; }
  149. public int DsuServerPort { get; set; }
  150. public bool EnableRumble { get; set; }
  151. public float WeakRumble
  152. {
  153. get => weakRumble; set
  154. {
  155. weakRumble = value;
  156. OnPropertyChanged();
  157. }
  158. }
  159. public float StrongRumble
  160. {
  161. get => strongRumble; set
  162. {
  163. strongRumble = value;
  164. OnPropertyChanged();
  165. }
  166. }
  167. public InputConfiguration(InputConfig config)
  168. {
  169. if (config != null)
  170. {
  171. Backend = config.Backend;
  172. Id = config.Id;
  173. ControllerType = config.ControllerType;
  174. PlayerIndex = config.PlayerIndex;
  175. if (config is StandardKeyboardInputConfig keyboardConfig)
  176. {
  177. LeftStickUp = (Key)(object)keyboardConfig.LeftJoyconStick.StickUp;
  178. LeftStickDown = (Key)(object)keyboardConfig.LeftJoyconStick.StickDown;
  179. LeftStickLeft = (Key)(object)keyboardConfig.LeftJoyconStick.StickLeft;
  180. LeftStickRight = (Key)(object)keyboardConfig.LeftJoyconStick.StickRight;
  181. LeftKeyboardStickButton = (Key)(object)keyboardConfig.LeftJoyconStick.StickButton;
  182. RightStickUp = (Key)(object)keyboardConfig.RightJoyconStick.StickUp;
  183. RightStickDown = (Key)(object)keyboardConfig.RightJoyconStick.StickDown;
  184. RightStickLeft = (Key)(object)keyboardConfig.RightJoyconStick.StickLeft;
  185. RightStickRight = (Key)(object)keyboardConfig.RightJoyconStick.StickRight;
  186. RightKeyboardStickButton = (Key)(object)keyboardConfig.RightJoyconStick.StickButton;
  187. ButtonA = (Key)(object)keyboardConfig.RightJoycon.ButtonA;
  188. ButtonB = (Key)(object)keyboardConfig.RightJoycon.ButtonB;
  189. ButtonX = (Key)(object)keyboardConfig.RightJoycon.ButtonX;
  190. ButtonY = (Key)(object)keyboardConfig.RightJoycon.ButtonY;
  191. ButtonR = (Key)(object)keyboardConfig.RightJoycon.ButtonR;
  192. RightButtonSl = (Key)(object)keyboardConfig.RightJoycon.ButtonSl;
  193. RightButtonSr = (Key)(object)keyboardConfig.RightJoycon.ButtonSr;
  194. ButtonZr = (Key)(object)keyboardConfig.RightJoycon.ButtonZr;
  195. ButtonPlus = (Key)(object)keyboardConfig.RightJoycon.ButtonPlus;
  196. DpadUp = (Key)(object)keyboardConfig.LeftJoycon.DpadUp;
  197. DpadDown = (Key)(object)keyboardConfig.LeftJoycon.DpadDown;
  198. DpadLeft = (Key)(object)keyboardConfig.LeftJoycon.DpadLeft;
  199. DpadRight = (Key)(object)keyboardConfig.LeftJoycon.DpadRight;
  200. ButtonMinus = (Key)(object)keyboardConfig.LeftJoycon.ButtonMinus;
  201. LeftButtonSl = (Key)(object)keyboardConfig.LeftJoycon.ButtonSl;
  202. LeftButtonSr = (Key)(object)keyboardConfig.LeftJoycon.ButtonSr;
  203. ButtonZl = (Key)(object)keyboardConfig.LeftJoycon.ButtonZl;
  204. ButtonL = (Key)(object)keyboardConfig.LeftJoycon.ButtonL;
  205. }
  206. else if (config is StandardControllerInputConfig controllerConfig)
  207. {
  208. LeftJoystick = (Stick)(object)controllerConfig.LeftJoyconStick.Joystick;
  209. LeftInvertStickX = controllerConfig.LeftJoyconStick.InvertStickX;
  210. LeftInvertStickY = controllerConfig.LeftJoyconStick.InvertStickY;
  211. LeftRotate90 = controllerConfig.LeftJoyconStick.Rotate90CW;
  212. LeftControllerStickButton = (Key)(object)controllerConfig.LeftJoyconStick.StickButton;
  213. RightJoystick = (Stick)(object)controllerConfig.RightJoyconStick.Joystick;
  214. RightInvertStickX = controllerConfig.RightJoyconStick.InvertStickX;
  215. RightInvertStickY = controllerConfig.RightJoyconStick.InvertStickY;
  216. RightRotate90 = controllerConfig.RightJoyconStick.Rotate90CW;
  217. RightControllerStickButton = (Key)(object)controllerConfig.RightJoyconStick.StickButton;
  218. ButtonA = (Key)(object)controllerConfig.RightJoycon.ButtonA;
  219. ButtonB = (Key)(object)controllerConfig.RightJoycon.ButtonB;
  220. ButtonX = (Key)(object)controllerConfig.RightJoycon.ButtonX;
  221. ButtonY = (Key)(object)controllerConfig.RightJoycon.ButtonY;
  222. ButtonR = (Key)(object)controllerConfig.RightJoycon.ButtonR;
  223. RightButtonSl = (Key)(object)controllerConfig.RightJoycon.ButtonSl;
  224. RightButtonSr = (Key)(object)controllerConfig.RightJoycon.ButtonSr;
  225. ButtonZr = (Key)(object)controllerConfig.RightJoycon.ButtonZr;
  226. ButtonPlus = (Key)(object)controllerConfig.RightJoycon.ButtonPlus;
  227. DpadUp = (Key)(object)controllerConfig.LeftJoycon.DpadUp;
  228. DpadDown = (Key)(object)controllerConfig.LeftJoycon.DpadDown;
  229. DpadLeft = (Key)(object)controllerConfig.LeftJoycon.DpadLeft;
  230. DpadRight = (Key)(object)controllerConfig.LeftJoycon.DpadRight;
  231. ButtonMinus = (Key)(object)controllerConfig.LeftJoycon.ButtonMinus;
  232. LeftButtonSl = (Key)(object)controllerConfig.LeftJoycon.ButtonSl;
  233. LeftButtonSr = (Key)(object)controllerConfig.LeftJoycon.ButtonSr;
  234. ButtonZl = (Key)(object)controllerConfig.LeftJoycon.ButtonZl;
  235. ButtonL = (Key)(object)controllerConfig.LeftJoycon.ButtonL;
  236. DeadzoneLeft = controllerConfig.DeadzoneLeft;
  237. DeadzoneRight = controllerConfig.DeadzoneRight;
  238. RangeLeft = controllerConfig.RangeLeft;
  239. RangeRight = controllerConfig.RangeRight;
  240. TriggerThreshold = controllerConfig.TriggerThreshold;
  241. if (controllerConfig.Motion != null)
  242. {
  243. EnableMotion = controllerConfig.Motion.EnableMotion;
  244. MotionBackend = controllerConfig.Motion.MotionBackend;
  245. GyroDeadzone = controllerConfig.Motion.GyroDeadzone;
  246. Sensitivity = controllerConfig.Motion.Sensitivity;
  247. if (controllerConfig.Motion is CemuHookMotionConfigController cemuHook)
  248. {
  249. EnableCemuHookMotion = true;
  250. DsuServerHost = cemuHook.DsuServerHost;
  251. DsuServerPort = cemuHook.DsuServerPort;
  252. Slot = cemuHook.Slot;
  253. AltSlot = cemuHook.AltSlot;
  254. MirrorInput = cemuHook.MirrorInput;
  255. }
  256. if (controllerConfig.Rumble != null)
  257. {
  258. EnableRumble = controllerConfig.Rumble.EnableRumble;
  259. WeakRumble = controllerConfig.Rumble.WeakRumble;
  260. StrongRumble = controllerConfig.Rumble.StrongRumble;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. public InputConfiguration()
  267. {
  268. }
  269. public InputConfig GetConfig()
  270. {
  271. if (Backend == InputBackendType.WindowKeyboard)
  272. {
  273. return new StandardKeyboardInputConfig()
  274. {
  275. Id = Id,
  276. Backend = Backend,
  277. PlayerIndex = PlayerIndex,
  278. ControllerType = ControllerType,
  279. LeftJoycon = new LeftJoyconCommonConfig<Ryujinx.Common.Configuration.Hid.Key>()
  280. {
  281. DpadUp = (Ryujinx.Common.Configuration.Hid.Key)(object)DpadUp,
  282. DpadDown = (Ryujinx.Common.Configuration.Hid.Key)(object)DpadDown,
  283. DpadLeft = (Ryujinx.Common.Configuration.Hid.Key)(object)DpadLeft,
  284. DpadRight = (Ryujinx.Common.Configuration.Hid.Key)(object)DpadRight,
  285. ButtonL = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonL,
  286. ButtonZl = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonZl,
  287. ButtonSl = (Ryujinx.Common.Configuration.Hid.Key)(object)LeftButtonSl,
  288. ButtonSr = (Ryujinx.Common.Configuration.Hid.Key)(object)LeftButtonSr,
  289. ButtonMinus = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonMinus
  290. },
  291. RightJoycon = new RightJoyconCommonConfig<Ryujinx.Common.Configuration.Hid.Key>()
  292. {
  293. ButtonA = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonA,
  294. ButtonB = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonB,
  295. ButtonX = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonX,
  296. ButtonY = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonY,
  297. ButtonPlus = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonPlus,
  298. ButtonSl = (Ryujinx.Common.Configuration.Hid.Key)(object)RightButtonSl,
  299. ButtonSr = (Ryujinx.Common.Configuration.Hid.Key)(object)RightButtonSr,
  300. ButtonR = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonR,
  301. ButtonZr = (Ryujinx.Common.Configuration.Hid.Key)(object)ButtonZr
  302. },
  303. LeftJoyconStick = new JoyconConfigKeyboardStick<Ryujinx.Common.Configuration.Hid.Key>()
  304. {
  305. StickUp = (Ryujinx.Common.Configuration.Hid.Key)(object)LeftStickUp,
  306. StickDown = (Ryujinx.Common.Configuration.Hid.Key)(object)LeftStickDown,
  307. StickRight = (Ryujinx.Common.Configuration.Hid.Key)(object)LeftStickRight,
  308. StickLeft = (Ryujinx.Common.Configuration.Hid.Key)(object)LeftStickLeft,
  309. StickButton = (Ryujinx.Common.Configuration.Hid.Key)(object)LeftKeyboardStickButton
  310. },
  311. RightJoyconStick = new JoyconConfigKeyboardStick<Ryujinx.Common.Configuration.Hid.Key>()
  312. {
  313. StickUp = (Ryujinx.Common.Configuration.Hid.Key)(object)RightStickUp,
  314. StickDown = (Ryujinx.Common.Configuration.Hid.Key)(object)RightStickDown,
  315. StickLeft = (Ryujinx.Common.Configuration.Hid.Key)(object)RightStickLeft,
  316. StickRight = (Ryujinx.Common.Configuration.Hid.Key)(object)RightStickRight,
  317. StickButton = (Ryujinx.Common.Configuration.Hid.Key)(object)RightKeyboardStickButton
  318. },
  319. Version = InputConfig.CurrentVersion
  320. };
  321. }
  322. else if (Backend == InputBackendType.GamepadSDL2)
  323. {
  324. var config = new StandardControllerInputConfig()
  325. {
  326. Id = Id,
  327. Backend = Backend,
  328. PlayerIndex = PlayerIndex,
  329. ControllerType = ControllerType,
  330. LeftJoycon = new LeftJoyconCommonConfig<GamepadInputId>()
  331. {
  332. DpadUp = (GamepadInputId)(object)DpadUp,
  333. DpadDown = (GamepadInputId)(object)DpadDown,
  334. DpadLeft = (GamepadInputId)(object)DpadLeft,
  335. DpadRight = (GamepadInputId)(object)DpadRight,
  336. ButtonL = (GamepadInputId)(object)ButtonL,
  337. ButtonZl = (GamepadInputId)(object)ButtonZl,
  338. ButtonSl = (GamepadInputId)(object)LeftButtonSl,
  339. ButtonSr = (GamepadInputId)(object)LeftButtonSr,
  340. ButtonMinus = (GamepadInputId)(object)ButtonMinus,
  341. },
  342. RightJoycon = new RightJoyconCommonConfig<GamepadInputId>()
  343. {
  344. ButtonA = (GamepadInputId)(object)ButtonA,
  345. ButtonB = (GamepadInputId)(object)ButtonB,
  346. ButtonX = (GamepadInputId)(object)ButtonX,
  347. ButtonY = (GamepadInputId)(object)ButtonY,
  348. ButtonPlus = (GamepadInputId)(object)ButtonPlus,
  349. ButtonSl = (GamepadInputId)(object)RightButtonSl,
  350. ButtonSr = (GamepadInputId)(object)RightButtonSr,
  351. ButtonR = (GamepadInputId)(object)ButtonR,
  352. ButtonZr = (GamepadInputId)(object)ButtonZr,
  353. },
  354. LeftJoyconStick = new JoyconConfigControllerStick<GamepadInputId, StickInputId>()
  355. {
  356. Joystick = (StickInputId)(object)LeftJoystick,
  357. InvertStickX = LeftInvertStickX,
  358. InvertStickY = LeftInvertStickY,
  359. Rotate90CW = LeftRotate90,
  360. StickButton = (GamepadInputId)(object)LeftControllerStickButton,
  361. },
  362. RightJoyconStick = new JoyconConfigControllerStick<GamepadInputId, StickInputId>()
  363. {
  364. Joystick = (StickInputId)(object)RightJoystick,
  365. InvertStickX = RightInvertStickX,
  366. InvertStickY = RightInvertStickY,
  367. Rotate90CW = RightRotate90,
  368. StickButton = (GamepadInputId)(object)RightControllerStickButton,
  369. },
  370. Rumble = new RumbleConfigController()
  371. {
  372. EnableRumble = EnableRumble,
  373. WeakRumble = WeakRumble,
  374. StrongRumble = StrongRumble
  375. },
  376. Version = InputConfig.CurrentVersion,
  377. DeadzoneLeft = DeadzoneLeft,
  378. DeadzoneRight = DeadzoneRight,
  379. RangeLeft = RangeLeft,
  380. RangeRight = RangeRight,
  381. TriggerThreshold = TriggerThreshold,
  382. Motion = EnableCemuHookMotion
  383. ? new CemuHookMotionConfigController()
  384. {
  385. DsuServerHost = DsuServerHost,
  386. DsuServerPort = DsuServerPort,
  387. Slot = Slot,
  388. AltSlot = AltSlot,
  389. MirrorInput = MirrorInput,
  390. MotionBackend = MotionInputBackendType.CemuHook
  391. }
  392. : new StandardMotionConfigController()
  393. {
  394. MotionBackend = MotionInputBackendType.GamepadDriver
  395. }
  396. };
  397. config.Motion.Sensitivity = Sensitivity;
  398. config.Motion.EnableMotion = EnableMotion;
  399. config.Motion.GyroDeadzone = GyroDeadzone;
  400. return config;
  401. }
  402. return null;
  403. }
  404. }
  405. }