ControllerWindow.cs 46 KB

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