ControllerWindow.cs 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. int.TryParse(_dsuServerPort.Buffer.Text, out int port);
  418. return new KeyboardConfig
  419. {
  420. Index = int.Parse(_inputDevice.ActiveId.Split("/")[1]),
  421. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  422. PlayerIndex = _playerIndex,
  423. LeftJoycon = new NpadKeyboardLeft
  424. {
  425. StickUp = lStickUp,
  426. StickDown = lStickDown,
  427. StickLeft = lStickLeft,
  428. StickRight = lStickRight,
  429. StickButton = lStickButton,
  430. DPadUp = lDPadUp,
  431. DPadDown = lDPadDown,
  432. DPadLeft = lDPadLeft,
  433. DPadRight = lDPadRight,
  434. ButtonMinus = lButtonMinus,
  435. ButtonL = lButtonL,
  436. ButtonZl = lButtonZl,
  437. ButtonSl = lButtonSl,
  438. ButtonSr = lButtonSr
  439. },
  440. RightJoycon = new NpadKeyboardRight
  441. {
  442. StickUp = rStickUp,
  443. StickDown = rStickDown,
  444. StickLeft = rStickLeft,
  445. StickRight = rStickRight,
  446. StickButton = rStickButton,
  447. ButtonA = rButtonA,
  448. ButtonB = rButtonB,
  449. ButtonX = rButtonX,
  450. ButtonY = rButtonY,
  451. ButtonPlus = rButtonPlus,
  452. ButtonR = rButtonR,
  453. ButtonZr = rButtonZr,
  454. ButtonSl = rButtonSl,
  455. ButtonSr = rButtonSr
  456. },
  457. EnableMotion = _enableMotion.Active,
  458. MirrorInput = _mirrorInput.Active,
  459. Slot = (int)_slotNumber.Value,
  460. AltSlot = (int)_altSlotNumber.Value,
  461. Sensitivity = (int)_sensitivity.Value,
  462. GyroDeadzone = _gyroDeadzone.Value,
  463. DsuServerHost = _dsuServerHost.Buffer.Text,
  464. DsuServerPort = port
  465. };
  466. }
  467. if (_inputDevice.ActiveId.StartsWith("controller"))
  468. {
  469. Enum.TryParse(_lStickX.Label, out ControllerInputId lStickX);
  470. Enum.TryParse(_lStickY.Label, out ControllerInputId lStickY);
  471. Enum.TryParse(_lStickButton.Label, out ControllerInputId lStickButton);
  472. Enum.TryParse(_minus.Label, out ControllerInputId lButtonMinus);
  473. Enum.TryParse(_l.Label, out ControllerInputId lButtonL);
  474. Enum.TryParse(_zL.Label, out ControllerInputId lButtonZl);
  475. Enum.TryParse(_lSl.Label, out ControllerInputId lButtonSl);
  476. Enum.TryParse(_lSr.Label, out ControllerInputId lButtonSr);
  477. Enum.TryParse(_dpadUp.Label, out ControllerInputId lDPadUp);
  478. Enum.TryParse(_dpadDown.Label, out ControllerInputId lDPadDown);
  479. Enum.TryParse(_dpadLeft.Label, out ControllerInputId lDPadLeft);
  480. Enum.TryParse(_dpadRight.Label, out ControllerInputId lDPadRight);
  481. Enum.TryParse(_rStickX.Label, out ControllerInputId rStickX);
  482. Enum.TryParse(_rStickY.Label, out ControllerInputId rStickY);
  483. Enum.TryParse(_rStickButton.Label, out ControllerInputId rStickButton);
  484. Enum.TryParse(_a.Label, out ControllerInputId rButtonA);
  485. Enum.TryParse(_b.Label, out ControllerInputId rButtonB);
  486. Enum.TryParse(_x.Label, out ControllerInputId rButtonX);
  487. Enum.TryParse(_y.Label, out ControllerInputId rButtonY);
  488. Enum.TryParse(_plus.Label, out ControllerInputId rButtonPlus);
  489. Enum.TryParse(_r.Label, out ControllerInputId rButtonR);
  490. Enum.TryParse(_zR.Label, out ControllerInputId rButtonZr);
  491. Enum.TryParse(_rSl.Label, out ControllerInputId rButtonSl);
  492. Enum.TryParse(_rSr.Label, out ControllerInputId rButtonSr);
  493. int.TryParse(_dsuServerPort.Buffer.Text, out int port);
  494. return new ControllerConfig
  495. {
  496. Index = int.Parse(_inputDevice.ActiveId.Split("/")[1]),
  497. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  498. PlayerIndex = _playerIndex,
  499. DeadzoneLeft = (float)_controllerDeadzoneLeft.Value,
  500. DeadzoneRight = (float)_controllerDeadzoneRight.Value,
  501. TriggerThreshold = (float)_controllerTriggerThreshold.Value,
  502. LeftJoycon = new NpadControllerLeft
  503. {
  504. InvertStickX = _invertLStickX.Active,
  505. StickX = lStickX,
  506. InvertStickY = _invertLStickY.Active,
  507. StickY = lStickY,
  508. StickButton = lStickButton,
  509. ButtonMinus = lButtonMinus,
  510. ButtonL = lButtonL,
  511. ButtonZl = lButtonZl,
  512. ButtonSl = lButtonSl,
  513. ButtonSr = lButtonSr,
  514. DPadUp = lDPadUp,
  515. DPadDown = lDPadDown,
  516. DPadLeft = lDPadLeft,
  517. DPadRight = lDPadRight
  518. },
  519. RightJoycon = new NpadControllerRight
  520. {
  521. InvertStickX = _invertRStickX.Active,
  522. StickX = rStickX,
  523. InvertStickY = _invertRStickY.Active,
  524. StickY = rStickY,
  525. StickButton = rStickButton,
  526. ButtonA = rButtonA,
  527. ButtonB = rButtonB,
  528. ButtonX = rButtonX,
  529. ButtonY = rButtonY,
  530. ButtonPlus = rButtonPlus,
  531. ButtonR = rButtonR,
  532. ButtonZr = rButtonZr,
  533. ButtonSl = rButtonSl,
  534. ButtonSr = rButtonSr
  535. },
  536. EnableMotion = _enableMotion.Active,
  537. MirrorInput = _mirrorInput.Active,
  538. Slot = (int)_slotNumber.Value,
  539. AltSlot = (int)_altSlotNumber.Value,
  540. Sensitivity = (int)_sensitivity.Value,
  541. GyroDeadzone = _gyroDeadzone.Value,
  542. DsuServerHost = _dsuServerHost.Buffer.Text,
  543. DsuServerPort = port
  544. };
  545. }
  546. if (!_inputDevice.ActiveId.StartsWith("disabled"))
  547. {
  548. GtkDialog.CreateErrorDialog("Some fields entered where invalid and therefore your config was not saved.");
  549. }
  550. return null;
  551. }
  552. private static bool IsAnyKeyPressed(out Key pressedKey, int index)
  553. {
  554. KeyboardState keyboardState = KeyboardController.GetKeyboardState(index);
  555. foreach (Key key in Enum.GetValues(typeof(Key)))
  556. {
  557. if (keyboardState.IsKeyDown((OpenTK.Input.Key)key))
  558. {
  559. pressedKey = key;
  560. return true;
  561. }
  562. }
  563. pressedKey = Key.Unbound;
  564. return false;
  565. }
  566. private static bool IsAnyButtonPressed(out ControllerInputId pressedButton, int index, double triggerThreshold)
  567. {
  568. JoystickState joystickState = Joystick.GetState(index);
  569. JoystickCapabilities joystickCapabilities = Joystick.GetCapabilities(index);
  570. //Buttons
  571. for (int i = 0; i != joystickCapabilities.ButtonCount; i++)
  572. {
  573. if (joystickState.IsButtonDown(i))
  574. {
  575. Enum.TryParse($"Button{i}", out pressedButton);
  576. return true;
  577. }
  578. }
  579. //Axis
  580. for (int i = 0; i != joystickCapabilities.AxisCount; i++)
  581. {
  582. if (joystickState.GetAxis(i) > 0.5f && joystickState.GetAxis(i) > triggerThreshold)
  583. {
  584. Enum.TryParse($"Axis{i}", out pressedButton);
  585. return true;
  586. }
  587. }
  588. //Hats
  589. for (int i = 0; i != joystickCapabilities.HatCount; i++)
  590. {
  591. JoystickHatState hatState = joystickState.GetHat((JoystickHat)i);
  592. string pos = null;
  593. if (hatState.IsUp) pos = "Up";
  594. if (hatState.IsDown) pos = "Down";
  595. if (hatState.IsLeft) pos = "Left";
  596. if (hatState.IsRight) pos = "Right";
  597. if (pos == null) continue;
  598. Enum.TryParse($"Hat{i}{pos}", out pressedButton);
  599. return true;
  600. }
  601. pressedButton = ControllerInputId.Unbound;
  602. return false;
  603. }
  604. private string GetProfileBasePath()
  605. {
  606. string path = AppDataManager.ProfilesDirPath;
  607. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  608. {
  609. path = System.IO.Path.Combine(path, "keyboard");
  610. }
  611. else if (_inputDevice.ActiveId.StartsWith("controller"))
  612. {
  613. path = System.IO.Path.Combine(path, "controller");
  614. }
  615. return path;
  616. }
  617. //Events
  618. private void InputDevice_Changed(object sender, EventArgs args)
  619. {
  620. SetAvailableOptions();
  621. SetControllerSpecificFields();
  622. if (_inputDevice.ActiveId != null) SetProfiles();
  623. }
  624. private void Controller_Changed(object sender, EventArgs args)
  625. {
  626. SetControllerSpecificFields();
  627. }
  628. private void RefreshInputDevicesButton_Pressed(object sender, EventArgs args)
  629. {
  630. UpdateInputDeviceList();
  631. _refreshInputDevicesButton.SetStateFlags(0, true);
  632. }
  633. private void Button_Pressed(object sender, EventArgs args)
  634. {
  635. if (_isWaitingForInput)
  636. {
  637. return;
  638. }
  639. _isWaitingForInput = true;
  640. Thread inputThread = new Thread(() =>
  641. {
  642. Button button = (ToggleButton)sender;
  643. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  644. {
  645. Key pressedKey;
  646. int index = int.Parse(_inputDevice.ActiveId.Split("/")[1]);
  647. while (!IsAnyKeyPressed(out pressedKey, index))
  648. {
  649. if (Mouse.GetState().IsAnyButtonDown || Keyboard.GetState().IsKeyDown(OpenTK.Input.Key.Escape))
  650. {
  651. Application.Invoke(delegate
  652. {
  653. button.SetStateFlags(0, true);
  654. });
  655. _isWaitingForInput = false;
  656. return;
  657. }
  658. }
  659. Application.Invoke(delegate
  660. {
  661. button.Label = pressedKey.ToString();
  662. button.SetStateFlags(0, true);
  663. });
  664. }
  665. else if (_inputDevice.ActiveId.StartsWith("controller"))
  666. {
  667. ControllerInputId pressedButton;
  668. int index = int.Parse(_inputDevice.ActiveId.Split("/")[1]);
  669. while (!IsAnyButtonPressed(out pressedButton, index, _controllerTriggerThreshold.Value))
  670. {
  671. if (Mouse.GetState().IsAnyButtonDown || Keyboard.GetState().IsAnyKeyDown)
  672. {
  673. Application.Invoke(delegate
  674. {
  675. button.SetStateFlags(0, true);
  676. });
  677. _isWaitingForInput = false;
  678. return;
  679. }
  680. }
  681. Application.Invoke(delegate
  682. {
  683. button.Label = pressedButton.ToString();
  684. button.SetStateFlags(0, true);
  685. });
  686. }
  687. _isWaitingForInput = false;
  688. });
  689. inputThread.Name = "GUI.InputThread";
  690. inputThread.IsBackground = true;
  691. inputThread.Start();
  692. }
  693. private void SetProfiles()
  694. {
  695. string basePath = GetProfileBasePath();
  696. if (!Directory.Exists(basePath))
  697. {
  698. Directory.CreateDirectory(basePath);
  699. }
  700. _profile.RemoveAll();
  701. _profile.Append("default", "Default");
  702. foreach (string profile in Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories))
  703. {
  704. _profile.Append(System.IO.Path.GetFileName(profile), System.IO.Path.GetFileNameWithoutExtension(profile));
  705. }
  706. }
  707. private void ProfileLoad_Activated(object sender, EventArgs args)
  708. {
  709. ((ToggleButton)sender).SetStateFlags(0, true);
  710. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == null) return;
  711. InputConfig config = null;
  712. int pos = _profile.Active;
  713. if (_profile.ActiveId == "default")
  714. {
  715. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  716. {
  717. config = new KeyboardConfig
  718. {
  719. Index = 0,
  720. ControllerType = ControllerType.JoyconPair,
  721. LeftJoycon = new NpadKeyboardLeft
  722. {
  723. StickUp = Key.W,
  724. StickDown = Key.S,
  725. StickLeft = Key.A,
  726. StickRight = Key.D,
  727. StickButton = Key.F,
  728. DPadUp = Key.Up,
  729. DPadDown = Key.Down,
  730. DPadLeft = Key.Left,
  731. DPadRight = Key.Right,
  732. ButtonMinus = Key.Minus,
  733. ButtonL = Key.E,
  734. ButtonZl = Key.Q,
  735. ButtonSl = Key.Unbound,
  736. ButtonSr = Key.Unbound
  737. },
  738. RightJoycon = new NpadKeyboardRight
  739. {
  740. StickUp = Key.I,
  741. StickDown = Key.K,
  742. StickLeft = Key.J,
  743. StickRight = Key.L,
  744. StickButton = Key.H,
  745. ButtonA = Key.Z,
  746. ButtonB = Key.X,
  747. ButtonX = Key.C,
  748. ButtonY = Key.V,
  749. ButtonPlus = Key.Plus,
  750. ButtonR = Key.U,
  751. ButtonZr = Key.O,
  752. ButtonSl = Key.Unbound,
  753. ButtonSr = Key.Unbound
  754. },
  755. EnableMotion = false,
  756. MirrorInput = false,
  757. Slot = 0,
  758. AltSlot = 0,
  759. Sensitivity = 100,
  760. GyroDeadzone = 1,
  761. DsuServerHost = "127.0.0.1",
  762. DsuServerPort = 26760
  763. };
  764. }
  765. else if (_inputDevice.ActiveId.StartsWith("controller"))
  766. {
  767. config = new ControllerConfig
  768. {
  769. Index = 0,
  770. ControllerType = ControllerType.ProController,
  771. DeadzoneLeft = 0.1f,
  772. DeadzoneRight = 0.1f,
  773. TriggerThreshold = 0.5f,
  774. LeftJoycon = new NpadControllerLeft
  775. {
  776. StickX = ControllerInputId.Axis0,
  777. StickY = ControllerInputId.Axis1,
  778. StickButton = ControllerInputId.Button8,
  779. DPadUp = ControllerInputId.Hat0Up,
  780. DPadDown = ControllerInputId.Hat0Down,
  781. DPadLeft = ControllerInputId.Hat0Left,
  782. DPadRight = ControllerInputId.Hat0Right,
  783. ButtonMinus = ControllerInputId.Button6,
  784. ButtonL = ControllerInputId.Button4,
  785. ButtonZl = ControllerInputId.Axis2,
  786. ButtonSl = ControllerInputId.Unbound,
  787. ButtonSr = ControllerInputId.Unbound,
  788. InvertStickX = false,
  789. InvertStickY = false
  790. },
  791. RightJoycon = new NpadControllerRight
  792. {
  793. StickX = ControllerInputId.Axis3,
  794. StickY = ControllerInputId.Axis4,
  795. StickButton = ControllerInputId.Button9,
  796. ButtonA = ControllerInputId.Button1,
  797. ButtonB = ControllerInputId.Button0,
  798. ButtonX = ControllerInputId.Button3,
  799. ButtonY = ControllerInputId.Button2,
  800. ButtonPlus = ControllerInputId.Button7,
  801. ButtonR = ControllerInputId.Button5,
  802. ButtonZr = ControllerInputId.Axis5,
  803. ButtonSl = ControllerInputId.Unbound,
  804. ButtonSr = ControllerInputId.Unbound,
  805. InvertStickX = false,
  806. InvertStickY = false
  807. },
  808. EnableMotion = false,
  809. MirrorInput = false,
  810. Slot = 0,
  811. AltSlot = 0,
  812. Sensitivity = 100,
  813. GyroDeadzone = 1,
  814. DsuServerHost = "127.0.0.1",
  815. DsuServerPort = 26760
  816. };
  817. }
  818. }
  819. else
  820. {
  821. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  822. if (!File.Exists(path))
  823. {
  824. if (pos >= 0)
  825. {
  826. _profile.Remove(pos);
  827. }
  828. return;
  829. }
  830. try
  831. {
  832. using (Stream stream = File.OpenRead(path))
  833. {
  834. config = JsonHelper.Deserialize<ControllerConfig>(stream);
  835. }
  836. }
  837. catch (JsonException)
  838. {
  839. try
  840. {
  841. using (Stream stream = File.OpenRead(path))
  842. {
  843. config = JsonHelper.Deserialize<KeyboardConfig>(stream);
  844. }
  845. }
  846. catch { }
  847. }
  848. }
  849. SetValues(config);
  850. }
  851. private void ProfileAdd_Activated(object sender, EventArgs args)
  852. {
  853. ((ToggleButton)sender).SetStateFlags(0, true);
  854. if (_inputDevice.ActiveId == "disabled") return;
  855. InputConfig inputConfig = GetValues();
  856. ProfileDialog profileDialog = new ProfileDialog();
  857. if (inputConfig == null) return;
  858. if (profileDialog.Run() == (int)ResponseType.Ok)
  859. {
  860. string path = System.IO.Path.Combine(GetProfileBasePath(), profileDialog.FileName);
  861. string jsonString;
  862. if (inputConfig is KeyboardConfig keyboardConfig)
  863. {
  864. jsonString = JsonHelper.Serialize(keyboardConfig, true);
  865. }
  866. else
  867. {
  868. jsonString = JsonHelper.Serialize(inputConfig as ControllerConfig, true);
  869. }
  870. File.WriteAllText(path, jsonString);
  871. }
  872. profileDialog.Dispose();
  873. SetProfiles();
  874. }
  875. private void ProfileRemove_Activated(object sender, EventArgs args)
  876. {
  877. ((ToggleButton) sender).SetStateFlags(0, true);
  878. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == "default" || _profile.ActiveId == null) return;
  879. MessageDialog confirmDialog = GtkDialog.CreateConfirmationDialog("Deleting Profile", "This action is irreversible, are your sure you want to continue?");
  880. if (confirmDialog.Run() == (int)ResponseType.Yes)
  881. {
  882. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  883. if (File.Exists(path))
  884. {
  885. File.Delete(path);
  886. }
  887. SetProfiles();
  888. }
  889. }
  890. private void SaveToggle_Activated(object sender, EventArgs args)
  891. {
  892. InputConfig inputConfig = GetValues();
  893. var newConfig = new List<InputConfig>();
  894. newConfig.AddRange(ConfigurationState.Instance.Hid.InputConfig.Value);
  895. if (_inputConfig == null && inputConfig != null)
  896. {
  897. newConfig.Add(inputConfig);
  898. }
  899. else
  900. {
  901. if (_inputDevice.ActiveId == "disabled")
  902. {
  903. newConfig.Remove(_inputConfig);
  904. }
  905. else if (inputConfig != null)
  906. {
  907. int index = newConfig.IndexOf(_inputConfig);
  908. newConfig[index] = inputConfig;
  909. }
  910. }
  911. // Atomically replace and signal input change.
  912. // NOTE: Do not modify InputConfig.Value directly as other code depends on the on-change event.
  913. ConfigurationState.Instance.Hid.InputConfig.Value = newConfig;
  914. MainWindow.SaveConfig();
  915. Dispose();
  916. }
  917. private void CloseToggle_Activated(object sender, EventArgs args)
  918. {
  919. Dispose();
  920. }
  921. }
  922. }