ControllerWindow.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. using Gtk;
  2. using OpenTK.Input;
  3. using Ryujinx.Common.Configuration.Hid;
  4. using Ryujinx.Common.Utilities;
  5. using Ryujinx.Configuration;
  6. using Ryujinx.HLE.FileSystem;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Reflection;
  11. using System.Text.Json;
  12. using System.Threading;
  13. using GUI = Gtk.Builder.ObjectAttribute;
  14. using Key = Ryujinx.Configuration.Hid.Key;
  15. namespace Ryujinx.Ui
  16. {
  17. public class ControllerWindow : Window
  18. {
  19. private PlayerIndex _playerIndex;
  20. private InputConfig _inputConfig;
  21. private bool _isWaitingForInput;
  22. private VirtualFileSystem _virtualFileSystem;
  23. #pragma warning disable CS0649, IDE0044
  24. [GUI] Adjustment _controllerDeadzoneLeft;
  25. [GUI] Adjustment _controllerDeadzoneRight;
  26. [GUI] Adjustment _controllerTriggerThreshold;
  27. [GUI] ComboBoxText _inputDevice;
  28. [GUI] ComboBoxText _profile;
  29. [GUI] ToggleButton _refreshInputDevicesButton;
  30. [GUI] Box _settingsBox;
  31. [GUI] Grid _leftStickKeyboard;
  32. [GUI] Grid _leftStickController;
  33. [GUI] Box _deadZoneLeftBox;
  34. [GUI] Grid _rightStickKeyboard;
  35. [GUI] Grid _rightStickController;
  36. [GUI] Box _deadZoneRightBox;
  37. [GUI] Grid _leftSideTriggerBox;
  38. [GUI] Grid _rightSideTriggerBox;
  39. [GUI] Box _triggerThresholdBox;
  40. [GUI] ComboBoxText _controllerType;
  41. [GUI] ToggleButton _lStickX;
  42. [GUI] CheckButton _invertLStickX;
  43. [GUI] ToggleButton _lStickY;
  44. [GUI] CheckButton _invertLStickY;
  45. [GUI] ToggleButton _lStickUp;
  46. [GUI] ToggleButton _lStickDown;
  47. [GUI] ToggleButton _lStickLeft;
  48. [GUI] ToggleButton _lStickRight;
  49. [GUI] ToggleButton _lStickButton;
  50. [GUI] ToggleButton _dpadUp;
  51. [GUI] ToggleButton _dpadDown;
  52. [GUI] ToggleButton _dpadLeft;
  53. [GUI] ToggleButton _dpadRight;
  54. [GUI] ToggleButton _minus;
  55. [GUI] ToggleButton _l;
  56. [GUI] ToggleButton _zL;
  57. [GUI] ToggleButton _rStickX;
  58. [GUI] CheckButton _invertRStickX;
  59. [GUI] ToggleButton _rStickY;
  60. [GUI] CheckButton _invertRStickY;
  61. [GUI] ToggleButton _rStickUp;
  62. [GUI] ToggleButton _rStickDown;
  63. [GUI] ToggleButton _rStickLeft;
  64. [GUI] ToggleButton _rStickRight;
  65. [GUI] ToggleButton _rStickButton;
  66. [GUI] ToggleButton _a;
  67. [GUI] ToggleButton _b;
  68. [GUI] ToggleButton _x;
  69. [GUI] ToggleButton _y;
  70. [GUI] ToggleButton _plus;
  71. [GUI] ToggleButton _r;
  72. [GUI] ToggleButton _zR;
  73. [GUI] ToggleButton _lSl;
  74. [GUI] ToggleButton _lSr;
  75. [GUI] ToggleButton _rSl;
  76. [GUI] ToggleButton _rSr;
  77. [GUI] Image _controllerImage;
  78. #pragma warning restore CS0649, IDE0044
  79. public ControllerWindow(PlayerIndex controllerId, VirtualFileSystem virtualFileSystem) : this(new Builder("Ryujinx.Ui.ControllerWindow.glade"), controllerId, virtualFileSystem) { }
  80. private ControllerWindow(Builder builder, PlayerIndex controllerId, VirtualFileSystem virtualFileSystem) : base(builder.GetObject("_controllerWin").Handle)
  81. {
  82. builder.Autoconnect(this);
  83. this.Icon = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.Icon.png");
  84. _playerIndex = controllerId;
  85. _virtualFileSystem = virtualFileSystem;
  86. _inputConfig = ConfigurationState.Instance.Hid.InputConfig.Value.Find(inputConfig => inputConfig.PlayerIndex == _playerIndex);
  87. Title = $"Ryujinx - Controller Settings - {_playerIndex}";
  88. if (_playerIndex == PlayerIndex.Handheld)
  89. {
  90. _controllerType.Append(ControllerType.Handheld.ToString(), "Handheld");
  91. _controllerType.Sensitive = false;
  92. }
  93. else
  94. {
  95. _controllerType.Append(ControllerType.ProController.ToString(), "Pro Controller");
  96. _controllerType.Append(ControllerType.JoyconPair.ToString(), "Joycon Pair");
  97. _controllerType.Append(ControllerType.JoyconLeft.ToString(), "Joycon Left");
  98. _controllerType.Append(ControllerType.JoyconRight.ToString(), "Joycon Right");
  99. }
  100. _controllerType.Active = 0; // Set initial value to first in list.
  101. //Bind Events
  102. _lStickX.Clicked += Button_Pressed;
  103. _lStickY.Clicked += Button_Pressed;
  104. _lStickUp.Clicked += Button_Pressed;
  105. _lStickDown.Clicked += Button_Pressed;
  106. _lStickLeft.Clicked += Button_Pressed;
  107. _lStickRight.Clicked += Button_Pressed;
  108. _lStickButton.Clicked += Button_Pressed;
  109. _dpadUp.Clicked += Button_Pressed;
  110. _dpadDown.Clicked += Button_Pressed;
  111. _dpadLeft.Clicked += Button_Pressed;
  112. _dpadRight.Clicked += Button_Pressed;
  113. _minus.Clicked += Button_Pressed;
  114. _l.Clicked += Button_Pressed;
  115. _zL.Clicked += Button_Pressed;
  116. _lSl.Clicked += Button_Pressed;
  117. _lSr.Clicked += Button_Pressed;
  118. _rStickX.Clicked += Button_Pressed;
  119. _rStickY.Clicked += Button_Pressed;
  120. _rStickUp.Clicked += Button_Pressed;
  121. _rStickDown.Clicked += Button_Pressed;
  122. _rStickLeft.Clicked += Button_Pressed;
  123. _rStickRight.Clicked += Button_Pressed;
  124. _rStickButton.Clicked += Button_Pressed;
  125. _a.Clicked += Button_Pressed;
  126. _b.Clicked += Button_Pressed;
  127. _x.Clicked += Button_Pressed;
  128. _y.Clicked += Button_Pressed;
  129. _plus.Clicked += Button_Pressed;
  130. _r.Clicked += Button_Pressed;
  131. _zR.Clicked += Button_Pressed;
  132. _rSl.Clicked += Button_Pressed;
  133. _rSr.Clicked += Button_Pressed;
  134. // Setup current values
  135. UpdateInputDeviceList();
  136. SetAvailableOptions();
  137. ClearValues();
  138. if (_inputDevice.ActiveId != null) SetCurrentValues();
  139. }
  140. private void UpdateInputDeviceList()
  141. {
  142. _inputDevice.RemoveAll();
  143. _inputDevice.Append("disabled", "Disabled");
  144. _inputDevice.SetActiveId("disabled");
  145. _inputDevice.Append($"keyboard/{KeyboardConfig.AllKeyboardsIndex}", "All keyboards");
  146. for (int i = 0; i < 20; i++)
  147. {
  148. if (KeyboardController.GetKeyboardState(i + 1).IsConnected)
  149. _inputDevice.Append($"keyboard/{i + 1}", $"Keyboard/{i + 1}");
  150. if (GamePad.GetState(i).IsConnected)
  151. _inputDevice.Append($"controller/{i}", $"Controller/{i} ({GamePad.GetName(i)})");
  152. }
  153. switch (_inputConfig)
  154. {
  155. case KeyboardConfig keyboard:
  156. _inputDevice.SetActiveId($"keyboard/{keyboard.Index}");
  157. break;
  158. case ControllerConfig controller:
  159. _inputDevice.SetActiveId($"controller/{controller.Index}");
  160. break;
  161. }
  162. }
  163. private void SetAvailableOptions()
  164. {
  165. if (_inputDevice.ActiveId != null && _inputDevice.ActiveId.StartsWith("keyboard"))
  166. {
  167. this.ShowAll();
  168. _leftStickController.Hide();
  169. _rightStickController.Hide();
  170. _deadZoneLeftBox.Hide();
  171. _deadZoneRightBox.Hide();
  172. _triggerThresholdBox.Hide();
  173. }
  174. else if (_inputDevice.ActiveId != null && _inputDevice.ActiveId.StartsWith("controller"))
  175. {
  176. this.ShowAll();
  177. _leftStickKeyboard.Hide();
  178. _rightStickKeyboard.Hide();
  179. }
  180. else
  181. {
  182. _settingsBox.Hide();
  183. }
  184. ClearValues();
  185. }
  186. private void SetCurrentValues()
  187. {
  188. SetControllerSpecificFields();
  189. SetProfiles();
  190. if (_inputDevice.ActiveId.StartsWith("keyboard") && _inputConfig is KeyboardConfig)
  191. {
  192. SetValues(_inputConfig);
  193. }
  194. else if (_inputDevice.ActiveId.StartsWith("controller") && _inputConfig is ControllerConfig)
  195. {
  196. SetValues(_inputConfig);
  197. }
  198. }
  199. private void SetControllerSpecificFields()
  200. {
  201. _leftSideTriggerBox.Hide();
  202. _rightSideTriggerBox.Hide();
  203. switch (_controllerType.ActiveId)
  204. {
  205. case "JoyconLeft":
  206. _leftSideTriggerBox.Show();
  207. break;
  208. case "JoyconRight":
  209. _rightSideTriggerBox.Show();
  210. break;
  211. }
  212. switch (_controllerType.ActiveId)
  213. {
  214. case "ProController":
  215. _controllerImage.Pixbuf = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.ProCon.svg", 400, 400);
  216. break;
  217. case "JoyconLeft":
  218. _controllerImage.Pixbuf = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.JoyConLeft.svg", 400, 400);
  219. break;
  220. case "JoyconRight":
  221. _controllerImage.Pixbuf = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.JoyConRight.svg", 400, 400);
  222. break;
  223. default:
  224. _controllerImage.Pixbuf = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.assets.JoyConPair.svg", 400, 400);
  225. break;
  226. }
  227. }
  228. private void ClearValues()
  229. {
  230. _lStickX.Label = "Unbound";
  231. _lStickY.Label = "Unbound";
  232. _lStickUp.Label = "Unbound";
  233. _lStickDown.Label = "Unbound";
  234. _lStickLeft.Label = "Unbound";
  235. _lStickRight.Label = "Unbound";
  236. _lStickButton.Label = "Unbound";
  237. _dpadUp.Label = "Unbound";
  238. _dpadDown.Label = "Unbound";
  239. _dpadLeft.Label = "Unbound";
  240. _dpadRight.Label = "Unbound";
  241. _minus.Label = "Unbound";
  242. _l.Label = "Unbound";
  243. _zL.Label = "Unbound";
  244. _lSl.Label = "Unbound";
  245. _lSr.Label = "Unbound";
  246. _rStickX.Label = "Unbound";
  247. _rStickY.Label = "Unbound";
  248. _rStickUp.Label = "Unbound";
  249. _rStickDown.Label = "Unbound";
  250. _rStickLeft.Label = "Unbound";
  251. _rStickRight.Label = "Unbound";
  252. _rStickButton.Label = "Unbound";
  253. _a.Label = "Unbound";
  254. _b.Label = "Unbound";
  255. _x.Label = "Unbound";
  256. _y.Label = "Unbound";
  257. _plus.Label = "Unbound";
  258. _r.Label = "Unbound";
  259. _zR.Label = "Unbound";
  260. _rSl.Label = "Unbound";
  261. _rSr.Label = "Unbound";
  262. _controllerDeadzoneLeft.Value = 0;
  263. _controllerDeadzoneRight.Value = 0;
  264. _controllerTriggerThreshold.Value = 0;
  265. }
  266. private void SetValues(InputConfig config)
  267. {
  268. switch (config)
  269. {
  270. case KeyboardConfig keyboardConfig:
  271. if (!_controllerType.SetActiveId(keyboardConfig.ControllerType.ToString()))
  272. {
  273. _controllerType.SetActiveId(_playerIndex == PlayerIndex.Handheld
  274. ? ControllerType.Handheld.ToString()
  275. : ControllerType.ProController.ToString());
  276. }
  277. _lStickUp.Label = keyboardConfig.LeftJoycon.StickUp.ToString();
  278. _lStickDown.Label = keyboardConfig.LeftJoycon.StickDown.ToString();
  279. _lStickLeft.Label = keyboardConfig.LeftJoycon.StickLeft.ToString();
  280. _lStickRight.Label = keyboardConfig.LeftJoycon.StickRight.ToString();
  281. _lStickButton.Label = keyboardConfig.LeftJoycon.StickButton.ToString();
  282. _dpadUp.Label = keyboardConfig.LeftJoycon.DPadUp.ToString();
  283. _dpadDown.Label = keyboardConfig.LeftJoycon.DPadDown.ToString();
  284. _dpadLeft.Label = keyboardConfig.LeftJoycon.DPadLeft.ToString();
  285. _dpadRight.Label = keyboardConfig.LeftJoycon.DPadRight.ToString();
  286. _minus.Label = keyboardConfig.LeftJoycon.ButtonMinus.ToString();
  287. _l.Label = keyboardConfig.LeftJoycon.ButtonL.ToString();
  288. _zL.Label = keyboardConfig.LeftJoycon.ButtonZl.ToString();
  289. _lSl.Label = keyboardConfig.LeftJoycon.ButtonSl.ToString();
  290. _lSr.Label = keyboardConfig.LeftJoycon.ButtonSr.ToString();
  291. _rStickUp.Label = keyboardConfig.RightJoycon.StickUp.ToString();
  292. _rStickDown.Label = keyboardConfig.RightJoycon.StickDown.ToString();
  293. _rStickLeft.Label = keyboardConfig.RightJoycon.StickLeft.ToString();
  294. _rStickRight.Label = keyboardConfig.RightJoycon.StickRight.ToString();
  295. _rStickButton.Label = keyboardConfig.RightJoycon.StickButton.ToString();
  296. _a.Label = keyboardConfig.RightJoycon.ButtonA.ToString();
  297. _b.Label = keyboardConfig.RightJoycon.ButtonB.ToString();
  298. _x.Label = keyboardConfig.RightJoycon.ButtonX.ToString();
  299. _y.Label = keyboardConfig.RightJoycon.ButtonY.ToString();
  300. _plus.Label = keyboardConfig.RightJoycon.ButtonPlus.ToString();
  301. _r.Label = keyboardConfig.RightJoycon.ButtonR.ToString();
  302. _zR.Label = keyboardConfig.RightJoycon.ButtonZr.ToString();
  303. _rSl.Label = keyboardConfig.RightJoycon.ButtonSl.ToString();
  304. _rSr.Label = keyboardConfig.RightJoycon.ButtonSr.ToString();
  305. break;
  306. case ControllerConfig controllerConfig:
  307. if (!_controllerType.SetActiveId(controllerConfig.ControllerType.ToString()))
  308. {
  309. _controllerType.SetActiveId(_playerIndex == PlayerIndex.Handheld
  310. ? ControllerType.Handheld.ToString()
  311. : ControllerType.ProController.ToString());
  312. }
  313. _lStickX.Label = controllerConfig.LeftJoycon.StickX.ToString();
  314. _invertLStickX.Active = controllerConfig.LeftJoycon.InvertStickX;
  315. _lStickY.Label = controllerConfig.LeftJoycon.StickY.ToString();
  316. _invertLStickY.Active = controllerConfig.LeftJoycon.InvertStickY;
  317. _lStickButton.Label = controllerConfig.LeftJoycon.StickButton.ToString();
  318. _dpadUp.Label = controllerConfig.LeftJoycon.DPadUp.ToString();
  319. _dpadDown.Label = controllerConfig.LeftJoycon.DPadDown.ToString();
  320. _dpadLeft.Label = controllerConfig.LeftJoycon.DPadLeft.ToString();
  321. _dpadRight.Label = controllerConfig.LeftJoycon.DPadRight.ToString();
  322. _minus.Label = controllerConfig.LeftJoycon.ButtonMinus.ToString();
  323. _l.Label = controllerConfig.LeftJoycon.ButtonL.ToString();
  324. _zL.Label = controllerConfig.LeftJoycon.ButtonZl.ToString();
  325. _lSl.Label = controllerConfig.LeftJoycon.ButtonSl.ToString();
  326. _lSr.Label = controllerConfig.LeftJoycon.ButtonSr.ToString();
  327. _rStickX.Label = controllerConfig.RightJoycon.StickX.ToString();
  328. _invertRStickX.Active = controllerConfig.RightJoycon.InvertStickX;
  329. _rStickY.Label = controllerConfig.RightJoycon.StickY.ToString();
  330. _invertRStickY.Active = controllerConfig.RightJoycon.InvertStickY;
  331. _rStickButton.Label = controllerConfig.RightJoycon.StickButton.ToString();
  332. _a.Label = controllerConfig.RightJoycon.ButtonA.ToString();
  333. _b.Label = controllerConfig.RightJoycon.ButtonB.ToString();
  334. _x.Label = controllerConfig.RightJoycon.ButtonX.ToString();
  335. _y.Label = controllerConfig.RightJoycon.ButtonY.ToString();
  336. _plus.Label = controllerConfig.RightJoycon.ButtonPlus.ToString();
  337. _r.Label = controllerConfig.RightJoycon.ButtonR.ToString();
  338. _zR.Label = controllerConfig.RightJoycon.ButtonZr.ToString();
  339. _rSl.Label = controllerConfig.RightJoycon.ButtonSl.ToString();
  340. _rSr.Label = controllerConfig.RightJoycon.ButtonSr.ToString();
  341. _controllerDeadzoneLeft.Value = controllerConfig.DeadzoneLeft;
  342. _controllerDeadzoneRight.Value = controllerConfig.DeadzoneRight;
  343. _controllerTriggerThreshold.Value = controllerConfig.TriggerThreshold;
  344. break;
  345. }
  346. }
  347. private InputConfig GetValues()
  348. {
  349. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  350. {
  351. Enum.TryParse(_lStickUp.Label, out Key lStickUp);
  352. Enum.TryParse(_lStickDown.Label, out Key lStickDown);
  353. Enum.TryParse(_lStickLeft.Label, out Key lStickLeft);
  354. Enum.TryParse(_lStickRight.Label, out Key lStickRight);
  355. Enum.TryParse(_lStickButton.Label, out Key lStickButton);
  356. Enum.TryParse(_dpadUp.Label, out Key lDPadUp);
  357. Enum.TryParse(_dpadDown.Label, out Key lDPadDown);
  358. Enum.TryParse(_dpadLeft.Label, out Key lDPadLeft);
  359. Enum.TryParse(_dpadRight.Label, out Key lDPadRight);
  360. Enum.TryParse(_minus.Label, out Key lButtonMinus);
  361. Enum.TryParse(_l.Label, out Key lButtonL);
  362. Enum.TryParse(_zL.Label, out Key lButtonZl);
  363. Enum.TryParse(_lSl.Label, out Key lButtonSl);
  364. Enum.TryParse(_lSr.Label, out Key lButtonSr);
  365. Enum.TryParse(_rStickUp.Label, out Key rStickUp);
  366. Enum.TryParse(_rStickDown.Label, out Key rStickDown);
  367. Enum.TryParse(_rStickLeft.Label, out Key rStickLeft);
  368. Enum.TryParse(_rStickRight.Label, out Key rStickRight);
  369. Enum.TryParse(_rStickButton.Label, out Key rStickButton);
  370. Enum.TryParse(_a.Label, out Key rButtonA);
  371. Enum.TryParse(_b.Label, out Key rButtonB);
  372. Enum.TryParse(_x.Label, out Key rButtonX);
  373. Enum.TryParse(_y.Label, out Key rButtonY);
  374. Enum.TryParse(_plus.Label, out Key rButtonPlus);
  375. Enum.TryParse(_r.Label, out Key rButtonR);
  376. Enum.TryParse(_zR.Label, out Key rButtonZr);
  377. Enum.TryParse(_rSl.Label, out Key rButtonSl);
  378. Enum.TryParse(_rSr.Label, out Key rButtonSr);
  379. return new KeyboardConfig
  380. {
  381. Index = int.Parse(_inputDevice.ActiveId.Split("/")[1]),
  382. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  383. PlayerIndex = _playerIndex,
  384. LeftJoycon = new NpadKeyboardLeft
  385. {
  386. StickUp = lStickUp,
  387. StickDown = lStickDown,
  388. StickLeft = lStickLeft,
  389. StickRight = lStickRight,
  390. StickButton = lStickButton,
  391. DPadUp = lDPadUp,
  392. DPadDown = lDPadDown,
  393. DPadLeft = lDPadLeft,
  394. DPadRight = lDPadRight,
  395. ButtonMinus = lButtonMinus,
  396. ButtonL = lButtonL,
  397. ButtonZl = lButtonZl,
  398. ButtonSl = lButtonSl,
  399. ButtonSr = lButtonSr
  400. },
  401. RightJoycon = new NpadKeyboardRight
  402. {
  403. StickUp = rStickUp,
  404. StickDown = rStickDown,
  405. StickLeft = rStickLeft,
  406. StickRight = rStickRight,
  407. StickButton = rStickButton,
  408. ButtonA = rButtonA,
  409. ButtonB = rButtonB,
  410. ButtonX = rButtonX,
  411. ButtonY = rButtonY,
  412. ButtonPlus = rButtonPlus,
  413. ButtonR = rButtonR,
  414. ButtonZr = rButtonZr,
  415. ButtonSl = rButtonSl,
  416. ButtonSr = rButtonSr
  417. }
  418. };
  419. }
  420. if (_inputDevice.ActiveId.StartsWith("controller"))
  421. {
  422. Enum.TryParse(_lStickX.Label, out ControllerInputId lStickX);
  423. Enum.TryParse(_lStickY.Label, out ControllerInputId lStickY);
  424. Enum.TryParse(_lStickButton.Label, out ControllerInputId lStickButton);
  425. Enum.TryParse(_minus.Label, out ControllerInputId lButtonMinus);
  426. Enum.TryParse(_l.Label, out ControllerInputId lButtonL);
  427. Enum.TryParse(_zL.Label, out ControllerInputId lButtonZl);
  428. Enum.TryParse(_lSl.Label, out ControllerInputId lButtonSl);
  429. Enum.TryParse(_lSr.Label, out ControllerInputId lButtonSr);
  430. Enum.TryParse(_dpadUp.Label, out ControllerInputId lDPadUp);
  431. Enum.TryParse(_dpadDown.Label, out ControllerInputId lDPadDown);
  432. Enum.TryParse(_dpadLeft.Label, out ControllerInputId lDPadLeft);
  433. Enum.TryParse(_dpadRight.Label, out ControllerInputId lDPadRight);
  434. Enum.TryParse(_rStickX.Label, out ControllerInputId rStickX);
  435. Enum.TryParse(_rStickY.Label, out ControllerInputId rStickY);
  436. Enum.TryParse(_rStickButton.Label, out ControllerInputId rStickButton);
  437. Enum.TryParse(_a.Label, out ControllerInputId rButtonA);
  438. Enum.TryParse(_b.Label, out ControllerInputId rButtonB);
  439. Enum.TryParse(_x.Label, out ControllerInputId rButtonX);
  440. Enum.TryParse(_y.Label, out ControllerInputId rButtonY);
  441. Enum.TryParse(_plus.Label, out ControllerInputId rButtonPlus);
  442. Enum.TryParse(_r.Label, out ControllerInputId rButtonR);
  443. Enum.TryParse(_zR.Label, out ControllerInputId rButtonZr);
  444. Enum.TryParse(_rSl.Label, out ControllerInputId rButtonSl);
  445. Enum.TryParse(_rSr.Label, out ControllerInputId rButtonSr);
  446. return new ControllerConfig
  447. {
  448. Index = int.Parse(_inputDevice.ActiveId.Split("/")[1]),
  449. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  450. PlayerIndex = _playerIndex,
  451. DeadzoneLeft = (float)_controllerDeadzoneLeft.Value,
  452. DeadzoneRight = (float)_controllerDeadzoneRight.Value,
  453. TriggerThreshold = (float)_controllerTriggerThreshold.Value,
  454. LeftJoycon = new NpadControllerLeft
  455. {
  456. InvertStickX = _invertLStickX.Active,
  457. StickX = lStickX,
  458. InvertStickY = _invertLStickY.Active,
  459. StickY = lStickY,
  460. StickButton = lStickButton,
  461. ButtonMinus = lButtonMinus,
  462. ButtonL = lButtonL,
  463. ButtonZl = lButtonZl,
  464. ButtonSl = lButtonSl,
  465. ButtonSr = lButtonSr,
  466. DPadUp = lDPadUp,
  467. DPadDown = lDPadDown,
  468. DPadLeft = lDPadLeft,
  469. DPadRight = lDPadRight
  470. },
  471. RightJoycon = new NpadControllerRight
  472. {
  473. InvertStickX = _invertRStickX.Active,
  474. StickX = rStickX,
  475. InvertStickY = _invertRStickY.Active,
  476. StickY = rStickY,
  477. StickButton = rStickButton,
  478. ButtonA = rButtonA,
  479. ButtonB = rButtonB,
  480. ButtonX = rButtonX,
  481. ButtonY = rButtonY,
  482. ButtonPlus = rButtonPlus,
  483. ButtonR = rButtonR,
  484. ButtonZr = rButtonZr,
  485. ButtonSl = rButtonSl,
  486. ButtonSr = rButtonSr
  487. }
  488. };
  489. }
  490. if (!_inputDevice.ActiveId.StartsWith("disabled"))
  491. {
  492. GtkDialog.CreateErrorDialog("Some fields entered where invalid and therefore your config was not saved.");
  493. }
  494. return null;
  495. }
  496. private static bool IsAnyKeyPressed(out Key pressedKey, int index)
  497. {
  498. KeyboardState keyboardState = KeyboardController.GetKeyboardState(index);
  499. foreach (Key key in Enum.GetValues(typeof(Key)))
  500. {
  501. if (keyboardState.IsKeyDown((OpenTK.Input.Key)key))
  502. {
  503. pressedKey = key;
  504. return true;
  505. }
  506. }
  507. pressedKey = Key.Unbound;
  508. return false;
  509. }
  510. private static bool IsAnyButtonPressed(out ControllerInputId pressedButton, int index, double triggerThreshold)
  511. {
  512. JoystickState joystickState = Joystick.GetState(index);
  513. JoystickCapabilities joystickCapabilities = Joystick.GetCapabilities(index);
  514. //Buttons
  515. for (int i = 0; i != joystickCapabilities.ButtonCount; i++)
  516. {
  517. if (joystickState.IsButtonDown(i))
  518. {
  519. Enum.TryParse($"Button{i}", out pressedButton);
  520. return true;
  521. }
  522. }
  523. //Axis
  524. for (int i = 0; i != joystickCapabilities.AxisCount; i++)
  525. {
  526. if (joystickState.GetAxis(i) > 0.5f && joystickState.GetAxis(i) > triggerThreshold)
  527. {
  528. Enum.TryParse($"Axis{i}", out pressedButton);
  529. return true;
  530. }
  531. }
  532. //Hats
  533. for (int i = 0; i != joystickCapabilities.HatCount; i++)
  534. {
  535. JoystickHatState hatState = joystickState.GetHat((JoystickHat)i);
  536. string pos = null;
  537. if (hatState.IsUp) pos = "Up";
  538. if (hatState.IsDown) pos = "Down";
  539. if (hatState.IsLeft) pos = "Left";
  540. if (hatState.IsRight) pos = "Right";
  541. if (pos == null) continue;
  542. Enum.TryParse($"Hat{i}{pos}", out pressedButton);
  543. return true;
  544. }
  545. pressedButton = ControllerInputId.Unbound;
  546. return false;
  547. }
  548. private string GetProfileBasePath()
  549. {
  550. string path = System.IO.Path.Combine(_virtualFileSystem.GetBasePath(), "profiles");
  551. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  552. {
  553. path = System.IO.Path.Combine(path, "keyboard");
  554. }
  555. else if (_inputDevice.ActiveId.StartsWith("controller"))
  556. {
  557. path = System.IO.Path.Combine(path, "controller");
  558. }
  559. return path;
  560. }
  561. //Events
  562. private void InputDevice_Changed(object sender, EventArgs args)
  563. {
  564. SetAvailableOptions();
  565. SetControllerSpecificFields();
  566. if (_inputDevice.ActiveId != null) SetProfiles();
  567. }
  568. private void Controller_Changed(object sender, EventArgs args)
  569. {
  570. SetControllerSpecificFields();
  571. }
  572. private void RefreshInputDevicesButton_Pressed(object sender, EventArgs args)
  573. {
  574. UpdateInputDeviceList();
  575. _refreshInputDevicesButton.SetStateFlags(0, true);
  576. }
  577. private void Button_Pressed(object sender, EventArgs args)
  578. {
  579. if (_isWaitingForInput)
  580. {
  581. return;
  582. }
  583. _isWaitingForInput = true;
  584. Thread inputThread = new Thread(() =>
  585. {
  586. Button button = (ToggleButton)sender;
  587. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  588. {
  589. Key pressedKey;
  590. int index = int.Parse(_inputDevice.ActiveId.Split("/")[1]);
  591. while (!IsAnyKeyPressed(out pressedKey, index))
  592. {
  593. if (Mouse.GetState().IsAnyButtonDown || Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Escape))
  594. {
  595. Application.Invoke(delegate
  596. {
  597. button.SetStateFlags(0, true);
  598. });
  599. _isWaitingForInput = false;
  600. return;
  601. }
  602. }
  603. Application.Invoke(delegate
  604. {
  605. button.Label = pressedKey.ToString();
  606. button.SetStateFlags(0, true);
  607. });
  608. }
  609. else if (_inputDevice.ActiveId.StartsWith("controller"))
  610. {
  611. ControllerInputId pressedButton;
  612. int index = int.Parse(_inputDevice.ActiveId.Split("/")[1]);
  613. while (!IsAnyButtonPressed(out pressedButton, index, _controllerTriggerThreshold.Value))
  614. {
  615. if (Mouse.GetState().IsAnyButtonDown || Keyboard.GetState().IsAnyKeyDown)
  616. {
  617. Application.Invoke(delegate
  618. {
  619. button.SetStateFlags(0, true);
  620. });
  621. _isWaitingForInput = false;
  622. return;
  623. }
  624. }
  625. Application.Invoke(delegate
  626. {
  627. button.Label = pressedButton.ToString();
  628. button.SetStateFlags(0, true);
  629. });
  630. }
  631. _isWaitingForInput = false;
  632. });
  633. inputThread.Name = "GUI.InputThread";
  634. inputThread.IsBackground = true;
  635. inputThread.Start();
  636. }
  637. private void SetProfiles()
  638. {
  639. string basePath = GetProfileBasePath();
  640. if (!Directory.Exists(basePath))
  641. {
  642. Directory.CreateDirectory(basePath);
  643. }
  644. _profile.RemoveAll();
  645. _profile.Append("default", "Default");
  646. foreach (string profile in Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories))
  647. {
  648. _profile.Append(System.IO.Path.GetFileName(profile), System.IO.Path.GetFileNameWithoutExtension(profile));
  649. }
  650. }
  651. private void ProfileLoad_Activated(object sender, EventArgs args)
  652. {
  653. ((ToggleButton)sender).SetStateFlags(0, true);
  654. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == null) return;
  655. InputConfig config = null;
  656. int pos = _profile.Active;
  657. if (_profile.ActiveId == "default")
  658. {
  659. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  660. {
  661. config = new KeyboardConfig
  662. {
  663. Index = 0,
  664. ControllerType = ControllerType.JoyconPair,
  665. LeftJoycon = new NpadKeyboardLeft
  666. {
  667. StickUp = Key.W,
  668. StickDown = Key.S,
  669. StickLeft = Key.A,
  670. StickRight = Key.D,
  671. StickButton = Key.F,
  672. DPadUp = Key.Up,
  673. DPadDown = Key.Down,
  674. DPadLeft = Key.Left,
  675. DPadRight = Key.Right,
  676. ButtonMinus = Key.Minus,
  677. ButtonL = Key.E,
  678. ButtonZl = Key.Q,
  679. ButtonSl = Key.Unbound,
  680. ButtonSr = Key.Unbound
  681. },
  682. RightJoycon = new NpadKeyboardRight
  683. {
  684. StickUp = Key.I,
  685. StickDown = Key.K,
  686. StickLeft = Key.J,
  687. StickRight = Key.L,
  688. StickButton = Key.H,
  689. ButtonA = Key.Z,
  690. ButtonB = Key.X,
  691. ButtonX = Key.C,
  692. ButtonY = Key.V,
  693. ButtonPlus = Key.Plus,
  694. ButtonR = Key.U,
  695. ButtonZr = Key.O,
  696. ButtonSl = Key.Unbound,
  697. ButtonSr = Key.Unbound
  698. }
  699. };
  700. }
  701. else if (_inputDevice.ActiveId.StartsWith("controller"))
  702. {
  703. config = new ControllerConfig
  704. {
  705. Index = 0,
  706. ControllerType = ControllerType.ProController,
  707. DeadzoneLeft = 0.1f,
  708. DeadzoneRight = 0.1f,
  709. TriggerThreshold = 0.5f,
  710. LeftJoycon = new NpadControllerLeft
  711. {
  712. StickX = ControllerInputId.Axis0,
  713. StickY = ControllerInputId.Axis1,
  714. StickButton = ControllerInputId.Button8,
  715. DPadUp = ControllerInputId.Hat0Up,
  716. DPadDown = ControllerInputId.Hat0Down,
  717. DPadLeft = ControllerInputId.Hat0Left,
  718. DPadRight = ControllerInputId.Hat0Right,
  719. ButtonMinus = ControllerInputId.Button6,
  720. ButtonL = ControllerInputId.Button4,
  721. ButtonZl = ControllerInputId.Axis2,
  722. ButtonSl = ControllerInputId.Unbound,
  723. ButtonSr = ControllerInputId.Unbound,
  724. InvertStickX = false,
  725. InvertStickY = false
  726. },
  727. RightJoycon = new NpadControllerRight
  728. {
  729. StickX = ControllerInputId.Axis3,
  730. StickY = ControllerInputId.Axis4,
  731. StickButton = ControllerInputId.Button9,
  732. ButtonA = ControllerInputId.Button1,
  733. ButtonB = ControllerInputId.Button0,
  734. ButtonX = ControllerInputId.Button3,
  735. ButtonY = ControllerInputId.Button2,
  736. ButtonPlus = ControllerInputId.Button7,
  737. ButtonR = ControllerInputId.Button5,
  738. ButtonZr = ControllerInputId.Axis5,
  739. ButtonSl = ControllerInputId.Unbound,
  740. ButtonSr = ControllerInputId.Unbound,
  741. InvertStickX = false,
  742. InvertStickY = false
  743. }
  744. };
  745. }
  746. }
  747. else
  748. {
  749. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  750. if (!File.Exists(path))
  751. {
  752. if (pos >= 0)
  753. {
  754. _profile.Remove(pos);
  755. }
  756. return;
  757. }
  758. try
  759. {
  760. using (Stream stream = File.OpenRead(path))
  761. {
  762. config = JsonHelper.Deserialize<ControllerConfig>(stream);
  763. }
  764. }
  765. catch (JsonException)
  766. {
  767. try
  768. {
  769. using (Stream stream = File.OpenRead(path))
  770. {
  771. config = JsonHelper.Deserialize<KeyboardConfig>(stream);
  772. }
  773. }
  774. catch { }
  775. }
  776. }
  777. SetValues(config);
  778. }
  779. private void ProfileAdd_Activated(object sender, EventArgs args)
  780. {
  781. ((ToggleButton)sender).SetStateFlags(0, true);
  782. if (_inputDevice.ActiveId == "disabled") return;
  783. InputConfig inputConfig = GetValues();
  784. ProfileDialog profileDialog = new ProfileDialog();
  785. if (inputConfig == null) return;
  786. if (profileDialog.Run() == (int)ResponseType.Ok)
  787. {
  788. string path = System.IO.Path.Combine(GetProfileBasePath(), profileDialog.FileName);
  789. string jsonString;
  790. if (inputConfig is KeyboardConfig keyboardConfig)
  791. {
  792. jsonString = JsonHelper.Serialize(keyboardConfig, true);
  793. }
  794. else
  795. {
  796. jsonString = JsonHelper.Serialize(inputConfig as ControllerConfig, true);
  797. }
  798. File.WriteAllText(path, jsonString);
  799. }
  800. profileDialog.Dispose();
  801. SetProfiles();
  802. }
  803. private void ProfileRemove_Activated(object sender, EventArgs args)
  804. {
  805. ((ToggleButton) sender).SetStateFlags(0, true);
  806. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == "default" || _profile.ActiveId == null) return;
  807. MessageDialog confirmDialog = GtkDialog.CreateConfirmationDialog("Deleting Profile", "This action is irreversible, are your sure you want to continue?");
  808. if (confirmDialog.Run() == (int)ResponseType.Yes)
  809. {
  810. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  811. if (File.Exists(path))
  812. {
  813. File.Delete(path);
  814. }
  815. SetProfiles();
  816. }
  817. }
  818. private void SaveToggle_Activated(object sender, EventArgs args)
  819. {
  820. InputConfig inputConfig = GetValues();
  821. var newConfig = new List<InputConfig>();
  822. newConfig.AddRange(ConfigurationState.Instance.Hid.InputConfig.Value);
  823. if (_inputConfig == null && inputConfig != null)
  824. {
  825. newConfig.Add(inputConfig);
  826. }
  827. else
  828. {
  829. if (_inputDevice.ActiveId == "disabled")
  830. {
  831. newConfig.Remove(_inputConfig);
  832. }
  833. else if (inputConfig != null)
  834. {
  835. int index = newConfig.IndexOf(_inputConfig);
  836. newConfig[index] = inputConfig;
  837. }
  838. }
  839. // Atomically replace and signal input change.
  840. // NOTE: Do not modify InputConfig.Value directly as other code depends on the on-change event.
  841. ConfigurationState.Instance.Hid.InputConfig.Value = newConfig;
  842. MainWindow.SaveConfig();
  843. Dispose();
  844. }
  845. private void CloseToggle_Activated(object sender, EventArgs args)
  846. {
  847. Dispose();
  848. }
  849. }
  850. }