ControllerWindow.cs 46 KB

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