ControllerWindow.cs 44 KB

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