ControllerWindow.cs 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216
  1. using Gtk;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Common.Configuration.Hid;
  4. using Ryujinx.Common.Configuration.Hid.Controller;
  5. using Ryujinx.Common.Configuration.Hid.Keyboard;
  6. using Ryujinx.Common.Utilities;
  7. using Ryujinx.Configuration;
  8. using Ryujinx.Input;
  9. using Ryujinx.Input.GTK3;
  10. using Ryujinx.Ui.Widgets;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Text.Json;
  16. using System.Threading;
  17. using GUI = Gtk.Builder.ObjectAttribute;
  18. using Key = Ryujinx.Common.Configuration.Hid.Key;
  19. using ConfigGamepadInputId = Ryujinx.Common.Configuration.Hid.Controller.GamepadInputId;
  20. using ConfigStickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
  21. using Ryujinx.Common.Configuration.Hid.Controller.Motion;
  22. using Ryujinx.Common.Logging;
  23. using Ryujinx.Input.Assigner;
  24. namespace Ryujinx.Ui.Windows
  25. {
  26. public class ControllerWindow : Window
  27. {
  28. private readonly PlayerIndex _playerIndex;
  29. private readonly InputConfig _inputConfig;
  30. private bool _isWaitingForInput;
  31. #pragma warning disable CS0649, IDE0044
  32. [GUI] Adjustment _controllerStrongRumble;
  33. [GUI] Adjustment _controllerWeakRumble;
  34. [GUI] Adjustment _controllerDeadzoneLeft;
  35. [GUI] Adjustment _controllerDeadzoneRight;
  36. [GUI] Adjustment _controllerRangeLeft;
  37. [GUI] Adjustment _controllerRangeRight;
  38. [GUI] Adjustment _controllerTriggerThreshold;
  39. [GUI] Adjustment _slotNumber;
  40. [GUI] Adjustment _altSlotNumber;
  41. [GUI] Adjustment _sensitivity;
  42. [GUI] Adjustment _gyroDeadzone;
  43. [GUI] CheckButton _enableMotion;
  44. [GUI] CheckButton _enableCemuHook;
  45. [GUI] CheckButton _mirrorInput;
  46. [GUI] Entry _dsuServerHost;
  47. [GUI] Entry _dsuServerPort;
  48. [GUI] ComboBoxText _inputDevice;
  49. [GUI] ComboBoxText _profile;
  50. [GUI] Box _settingsBox;
  51. [GUI] Box _motionAltBox;
  52. [GUI] Box _motionBox;
  53. [GUI] Box _dsuServerHostBox;
  54. [GUI] Box _dsuServerPortBox;
  55. [GUI] Box _motionControllerSlot;
  56. [GUI] Grid _leftStickKeyboard;
  57. [GUI] Grid _leftStickController;
  58. [GUI] Box _deadZoneLeftBox;
  59. [GUI] Box _rangeLeftBox;
  60. [GUI] Grid _rightStickKeyboard;
  61. [GUI] Grid _rightStickController;
  62. [GUI] Box _deadZoneRightBox;
  63. [GUI] Box _rangeRightBox;
  64. [GUI] Grid _leftSideTriggerBox;
  65. [GUI] Grid _rightSideTriggerBox;
  66. [GUI] Box _triggerThresholdBox;
  67. [GUI] ComboBoxText _controllerType;
  68. [GUI] ToggleButton _lStick;
  69. [GUI] CheckButton _invertLStickX;
  70. [GUI] CheckButton _invertLStickY;
  71. [GUI] ToggleButton _lStickUp;
  72. [GUI] ToggleButton _lStickDown;
  73. [GUI] ToggleButton _lStickLeft;
  74. [GUI] ToggleButton _lStickRight;
  75. [GUI] ToggleButton _lStickButton;
  76. [GUI] ToggleButton _dpadUp;
  77. [GUI] ToggleButton _dpadDown;
  78. [GUI] ToggleButton _dpadLeft;
  79. [GUI] ToggleButton _dpadRight;
  80. [GUI] ToggleButton _minus;
  81. [GUI] ToggleButton _l;
  82. [GUI] ToggleButton _zL;
  83. [GUI] ToggleButton _rStick;
  84. [GUI] CheckButton _invertRStickX;
  85. [GUI] CheckButton _invertRStickY;
  86. [GUI] ToggleButton _rStickUp;
  87. [GUI] ToggleButton _rStickDown;
  88. [GUI] ToggleButton _rStickLeft;
  89. [GUI] ToggleButton _rStickRight;
  90. [GUI] ToggleButton _rStickButton;
  91. [GUI] ToggleButton _a;
  92. [GUI] ToggleButton _b;
  93. [GUI] ToggleButton _x;
  94. [GUI] ToggleButton _y;
  95. [GUI] ToggleButton _plus;
  96. [GUI] ToggleButton _r;
  97. [GUI] ToggleButton _zR;
  98. [GUI] ToggleButton _lSl;
  99. [GUI] ToggleButton _lSr;
  100. [GUI] ToggleButton _rSl;
  101. [GUI] ToggleButton _rSr;
  102. [GUI] Image _controllerImage;
  103. [GUI] CheckButton _enableRumble;
  104. [GUI] Box _rumbleBox;
  105. #pragma warning restore CS0649, IDE0044
  106. private MainWindow _mainWindow;
  107. private IGamepadDriver _gtk3KeyboardDriver;
  108. private IGamepad _selectedGamepad;
  109. private bool _mousePressed;
  110. private bool _middleMousePressed;
  111. public ControllerWindow(MainWindow mainWindow, PlayerIndex controllerId) : this(mainWindow, new Builder("Ryujinx.Ui.Windows.ControllerWindow.glade"), controllerId) { }
  112. private ControllerWindow(MainWindow mainWindow, Builder builder, PlayerIndex controllerId) : base(builder.GetObject("_controllerWin").Handle)
  113. {
  114. _mainWindow = mainWindow;
  115. _selectedGamepad = null;
  116. // NOTE: To get input in this window, we need to bind a custom keyboard driver instead of using the InputManager one as the main window isn't focused...
  117. _gtk3KeyboardDriver = new GTK3KeyboardDriver(this);
  118. Icon = new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Logo_Ryujinx.png");
  119. builder.Autoconnect(this);
  120. _playerIndex = controllerId;
  121. _inputConfig = ConfigurationState.Instance.Hid.InputConfig.Value.Find(inputConfig => inputConfig.PlayerIndex == _playerIndex);
  122. Title = $"Ryujinx - Controller Settings - {_playerIndex}";
  123. if (_playerIndex == PlayerIndex.Handheld)
  124. {
  125. _controllerType.Append(ControllerType.Handheld.ToString(), "Handheld");
  126. _controllerType.Sensitive = false;
  127. }
  128. else
  129. {
  130. _controllerType.Append(ControllerType.ProController.ToString(), "Pro Controller");
  131. _controllerType.Append(ControllerType.JoyconPair.ToString(), "Joycon Pair");
  132. _controllerType.Append(ControllerType.JoyconLeft.ToString(), "Joycon Left");
  133. _controllerType.Append(ControllerType.JoyconRight.ToString(), "Joycon Right");
  134. }
  135. _controllerType.Active = 0; // Set initial value to first in list.
  136. // Bind Events.
  137. _lStick.Clicked += ButtonForStick_Pressed;
  138. _lStickUp.Clicked += Button_Pressed;
  139. _lStickDown.Clicked += Button_Pressed;
  140. _lStickLeft.Clicked += Button_Pressed;
  141. _lStickRight.Clicked += Button_Pressed;
  142. _lStickButton.Clicked += Button_Pressed;
  143. _dpadUp.Clicked += Button_Pressed;
  144. _dpadDown.Clicked += Button_Pressed;
  145. _dpadLeft.Clicked += Button_Pressed;
  146. _dpadRight.Clicked += Button_Pressed;
  147. _minus.Clicked += Button_Pressed;
  148. _l.Clicked += Button_Pressed;
  149. _zL.Clicked += Button_Pressed;
  150. _lSl.Clicked += Button_Pressed;
  151. _lSr.Clicked += Button_Pressed;
  152. _rStick.Clicked += ButtonForStick_Pressed;
  153. _rStickUp.Clicked += Button_Pressed;
  154. _rStickDown.Clicked += Button_Pressed;
  155. _rStickLeft.Clicked += Button_Pressed;
  156. _rStickRight.Clicked += Button_Pressed;
  157. _rStickButton.Clicked += Button_Pressed;
  158. _a.Clicked += Button_Pressed;
  159. _b.Clicked += Button_Pressed;
  160. _x.Clicked += Button_Pressed;
  161. _y.Clicked += Button_Pressed;
  162. _plus.Clicked += Button_Pressed;
  163. _r.Clicked += Button_Pressed;
  164. _zR.Clicked += Button_Pressed;
  165. _rSl.Clicked += Button_Pressed;
  166. _rSr.Clicked += Button_Pressed;
  167. _enableCemuHook.Clicked += CemuHookCheckButtonPressed;
  168. // Setup current values.
  169. UpdateInputDeviceList();
  170. SetAvailableOptions();
  171. ClearValues();
  172. if (_inputDevice.ActiveId != null)
  173. {
  174. SetCurrentValues();
  175. }
  176. mainWindow.InputManager.GamepadDriver.OnGamepadConnected += HandleOnGamepadConnected;
  177. mainWindow.InputManager.GamepadDriver.OnGamepadDisconnected += HandleOnGamepadDisconnected;
  178. if (_mainWindow.RendererWidget != null)
  179. {
  180. _mainWindow.RendererWidget.NpadManager.BlockInputUpdates();
  181. }
  182. }
  183. private void CemuHookCheckButtonPressed(object sender, EventArgs e)
  184. {
  185. UpdateCemuHookSpecificFieldsVisibility();
  186. }
  187. private void HandleOnGamepadDisconnected(string id)
  188. {
  189. Application.Invoke(delegate
  190. {
  191. UpdateInputDeviceList();
  192. });
  193. }
  194. private void HandleOnGamepadConnected(string id)
  195. {
  196. Application.Invoke(delegate
  197. {
  198. UpdateInputDeviceList();
  199. });
  200. }
  201. protected override void OnDestroyed()
  202. {
  203. _mainWindow.InputManager.GamepadDriver.OnGamepadConnected -= HandleOnGamepadConnected;
  204. _mainWindow.InputManager.GamepadDriver.OnGamepadDisconnected -= HandleOnGamepadDisconnected;
  205. if (_mainWindow.RendererWidget != null)
  206. {
  207. _mainWindow.RendererWidget.NpadManager.UnblockInputUpdates();
  208. }
  209. _selectedGamepad?.Dispose();
  210. _gtk3KeyboardDriver.Dispose();
  211. }
  212. private static string GetShrinkedGamepadName(string str)
  213. {
  214. const string ShrinkChars = "...";
  215. const int MaxSize = 50;
  216. if (str.Length > MaxSize)
  217. {
  218. return str.Substring(0, MaxSize - ShrinkChars.Length) + ShrinkChars;
  219. }
  220. return str;
  221. }
  222. private void UpdateInputDeviceList()
  223. {
  224. _inputDevice.RemoveAll();
  225. _inputDevice.Append("disabled", "Disabled");
  226. _inputDevice.SetActiveId("disabled");
  227. foreach (string id in _mainWindow.InputManager.KeyboardDriver.GamepadsIds)
  228. {
  229. IGamepad gamepad = _mainWindow.InputManager.KeyboardDriver.GetGamepad(id);
  230. if (gamepad != null)
  231. {
  232. _inputDevice.Append($"keyboard/{id}", GetShrinkedGamepadName($"{gamepad.Name} ({id})"));
  233. gamepad.Dispose();
  234. }
  235. }
  236. foreach (string id in _mainWindow.InputManager.GamepadDriver.GamepadsIds)
  237. {
  238. IGamepad gamepad = _mainWindow.InputManager.GamepadDriver.GetGamepad(id);
  239. if (gamepad != null)
  240. {
  241. _inputDevice.Append($"controller/{id}", GetShrinkedGamepadName($"{gamepad.Name} ({id})"));
  242. gamepad.Dispose();
  243. }
  244. }
  245. switch (_inputConfig)
  246. {
  247. case StandardKeyboardInputConfig keyboard:
  248. _inputDevice.SetActiveId($"keyboard/{keyboard.Id}");
  249. break;
  250. case StandardControllerInputConfig controller:
  251. _inputDevice.SetActiveId($"controller/{controller.Id}");
  252. break;
  253. }
  254. }
  255. private void UpdateCemuHookSpecificFieldsVisibility()
  256. {
  257. if (_enableCemuHook.Active)
  258. {
  259. _dsuServerHostBox.Show();
  260. _dsuServerPortBox.Show();
  261. _motionControllerSlot.Show();
  262. _motionAltBox.Show();
  263. _mirrorInput.Show();
  264. }
  265. else
  266. {
  267. _dsuServerHostBox.Hide();
  268. _dsuServerPortBox.Hide();
  269. _motionControllerSlot.Hide();
  270. _motionAltBox.Hide();
  271. _mirrorInput.Hide();
  272. }
  273. }
  274. private void SetAvailableOptions()
  275. {
  276. if (_inputDevice.ActiveId != null && _inputDevice.ActiveId.StartsWith("keyboard"))
  277. {
  278. ShowAll();
  279. _leftStickController.Hide();
  280. _rightStickController.Hide();
  281. _deadZoneLeftBox.Hide();
  282. _deadZoneRightBox.Hide();
  283. _rangeLeftBox.Hide();
  284. _rangeRightBox.Hide();
  285. _triggerThresholdBox.Hide();
  286. _motionBox.Hide();
  287. _rumbleBox.Hide();
  288. }
  289. else if (_inputDevice.ActiveId != null && _inputDevice.ActiveId.StartsWith("controller"))
  290. {
  291. ShowAll();
  292. _leftStickKeyboard.Hide();
  293. _rightStickKeyboard.Hide();
  294. UpdateCemuHookSpecificFieldsVisibility();
  295. }
  296. else
  297. {
  298. _settingsBox.Hide();
  299. }
  300. ClearValues();
  301. }
  302. private void SetCurrentValues()
  303. {
  304. SetControllerSpecificFields();
  305. SetProfiles();
  306. if (_inputDevice.ActiveId.StartsWith("keyboard") && _inputConfig is StandardKeyboardInputConfig)
  307. {
  308. SetValues(_inputConfig);
  309. }
  310. else if (_inputDevice.ActiveId.StartsWith("controller") && _inputConfig is StandardControllerInputConfig)
  311. {
  312. SetValues(_inputConfig);
  313. }
  314. }
  315. private void SetControllerSpecificFields()
  316. {
  317. _leftSideTriggerBox.Hide();
  318. _rightSideTriggerBox.Hide();
  319. _motionAltBox.Hide();
  320. switch (_controllerType.ActiveId)
  321. {
  322. case "JoyconLeft":
  323. _leftSideTriggerBox.Show();
  324. break;
  325. case "JoyconRight":
  326. _rightSideTriggerBox.Show();
  327. break;
  328. case "JoyconPair":
  329. _motionAltBox.Show();
  330. break;
  331. }
  332. _controllerImage.Pixbuf = _controllerType.ActiveId switch
  333. {
  334. "ProController" => new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Controller_ProCon.svg", 400, 400),
  335. "JoyconLeft" => new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Controller_JoyConLeft.svg", 400, 500),
  336. "JoyconRight" => new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Controller_JoyConRight.svg", 400, 500),
  337. _ => new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Controller_JoyConPair.svg", 400, 500),
  338. };
  339. }
  340. private void ClearValues()
  341. {
  342. _lStick.Label = "Unbound";
  343. _lStickUp.Label = "Unbound";
  344. _lStickDown.Label = "Unbound";
  345. _lStickLeft.Label = "Unbound";
  346. _lStickRight.Label = "Unbound";
  347. _lStickButton.Label = "Unbound";
  348. _dpadUp.Label = "Unbound";
  349. _dpadDown.Label = "Unbound";
  350. _dpadLeft.Label = "Unbound";
  351. _dpadRight.Label = "Unbound";
  352. _minus.Label = "Unbound";
  353. _l.Label = "Unbound";
  354. _zL.Label = "Unbound";
  355. _lSl.Label = "Unbound";
  356. _lSr.Label = "Unbound";
  357. _rStick.Label = "Unbound";
  358. _rStickUp.Label = "Unbound";
  359. _rStickDown.Label = "Unbound";
  360. _rStickLeft.Label = "Unbound";
  361. _rStickRight.Label = "Unbound";
  362. _rStickButton.Label = "Unbound";
  363. _a.Label = "Unbound";
  364. _b.Label = "Unbound";
  365. _x.Label = "Unbound";
  366. _y.Label = "Unbound";
  367. _plus.Label = "Unbound";
  368. _r.Label = "Unbound";
  369. _zR.Label = "Unbound";
  370. _rSl.Label = "Unbound";
  371. _rSr.Label = "Unbound";
  372. _controllerStrongRumble.Value = 1;
  373. _controllerWeakRumble.Value = 1;
  374. _controllerDeadzoneLeft.Value = 0;
  375. _controllerDeadzoneRight.Value = 0;
  376. _controllerRangeLeft.Value = 1;
  377. _controllerRangeRight.Value = 1;
  378. _controllerTriggerThreshold.Value = 0;
  379. _mirrorInput.Active = false;
  380. _enableMotion.Active = false;
  381. _enableCemuHook.Active = false;
  382. _slotNumber.Value = 0;
  383. _altSlotNumber.Value = 0;
  384. _sensitivity.Value = 100;
  385. _gyroDeadzone.Value = 1;
  386. _dsuServerHost.Buffer.Text = "";
  387. _dsuServerPort.Buffer.Text = "";
  388. _enableRumble.Active = false;
  389. }
  390. private void SetValues(InputConfig config)
  391. {
  392. switch (config)
  393. {
  394. case StandardKeyboardInputConfig keyboardConfig:
  395. if (!_controllerType.SetActiveId(keyboardConfig.ControllerType.ToString()))
  396. {
  397. _controllerType.SetActiveId(_playerIndex == PlayerIndex.Handheld
  398. ? ControllerType.Handheld.ToString()
  399. : ControllerType.ProController.ToString());
  400. }
  401. _lStickUp.Label = keyboardConfig.LeftJoyconStick.StickUp.ToString();
  402. _lStickDown.Label = keyboardConfig.LeftJoyconStick.StickDown.ToString();
  403. _lStickLeft.Label = keyboardConfig.LeftJoyconStick.StickLeft.ToString();
  404. _lStickRight.Label = keyboardConfig.LeftJoyconStick.StickRight.ToString();
  405. _lStickButton.Label = keyboardConfig.LeftJoyconStick.StickButton.ToString();
  406. _dpadUp.Label = keyboardConfig.LeftJoycon.DpadUp.ToString();
  407. _dpadDown.Label = keyboardConfig.LeftJoycon.DpadDown.ToString();
  408. _dpadLeft.Label = keyboardConfig.LeftJoycon.DpadLeft.ToString();
  409. _dpadRight.Label = keyboardConfig.LeftJoycon.DpadRight.ToString();
  410. _minus.Label = keyboardConfig.LeftJoycon.ButtonMinus.ToString();
  411. _l.Label = keyboardConfig.LeftJoycon.ButtonL.ToString();
  412. _zL.Label = keyboardConfig.LeftJoycon.ButtonZl.ToString();
  413. _lSl.Label = keyboardConfig.LeftJoycon.ButtonSl.ToString();
  414. _lSr.Label = keyboardConfig.LeftJoycon.ButtonSr.ToString();
  415. _rStickUp.Label = keyboardConfig.RightJoyconStick.StickUp.ToString();
  416. _rStickDown.Label = keyboardConfig.RightJoyconStick.StickDown.ToString();
  417. _rStickLeft.Label = keyboardConfig.RightJoyconStick.StickLeft.ToString();
  418. _rStickRight.Label = keyboardConfig.RightJoyconStick.StickRight.ToString();
  419. _rStickButton.Label = keyboardConfig.RightJoyconStick.StickButton.ToString();
  420. _a.Label = keyboardConfig.RightJoycon.ButtonA.ToString();
  421. _b.Label = keyboardConfig.RightJoycon.ButtonB.ToString();
  422. _x.Label = keyboardConfig.RightJoycon.ButtonX.ToString();
  423. _y.Label = keyboardConfig.RightJoycon.ButtonY.ToString();
  424. _plus.Label = keyboardConfig.RightJoycon.ButtonPlus.ToString();
  425. _r.Label = keyboardConfig.RightJoycon.ButtonR.ToString();
  426. _zR.Label = keyboardConfig.RightJoycon.ButtonZr.ToString();
  427. _rSl.Label = keyboardConfig.RightJoycon.ButtonSl.ToString();
  428. _rSr.Label = keyboardConfig.RightJoycon.ButtonSr.ToString();
  429. break;
  430. case StandardControllerInputConfig controllerConfig:
  431. if (!_controllerType.SetActiveId(controllerConfig.ControllerType.ToString()))
  432. {
  433. _controllerType.SetActiveId(_playerIndex == PlayerIndex.Handheld
  434. ? ControllerType.Handheld.ToString()
  435. : ControllerType.ProController.ToString());
  436. }
  437. _lStick.Label = controllerConfig.LeftJoyconStick.Joystick.ToString();
  438. _invertLStickX.Active = controllerConfig.LeftJoyconStick.InvertStickX;
  439. _invertLStickY.Active = controllerConfig.LeftJoyconStick.InvertStickY;
  440. _lStickButton.Label = controllerConfig.LeftJoyconStick.StickButton.ToString();
  441. _dpadUp.Label = controllerConfig.LeftJoycon.DpadUp.ToString();
  442. _dpadDown.Label = controllerConfig.LeftJoycon.DpadDown.ToString();
  443. _dpadLeft.Label = controllerConfig.LeftJoycon.DpadLeft.ToString();
  444. _dpadRight.Label = controllerConfig.LeftJoycon.DpadRight.ToString();
  445. _minus.Label = controllerConfig.LeftJoycon.ButtonMinus.ToString();
  446. _l.Label = controllerConfig.LeftJoycon.ButtonL.ToString();
  447. _zL.Label = controllerConfig.LeftJoycon.ButtonZl.ToString();
  448. _lSl.Label = controllerConfig.LeftJoycon.ButtonSl.ToString();
  449. _lSr.Label = controllerConfig.LeftJoycon.ButtonSr.ToString();
  450. _rStick.Label = controllerConfig.RightJoyconStick.Joystick.ToString();
  451. _invertRStickX.Active = controllerConfig.RightJoyconStick.InvertStickX;
  452. _invertRStickY.Active = controllerConfig.RightJoyconStick.InvertStickY;
  453. _rStickButton.Label = controllerConfig.RightJoyconStick.StickButton.ToString();
  454. _a.Label = controllerConfig.RightJoycon.ButtonA.ToString();
  455. _b.Label = controllerConfig.RightJoycon.ButtonB.ToString();
  456. _x.Label = controllerConfig.RightJoycon.ButtonX.ToString();
  457. _y.Label = controllerConfig.RightJoycon.ButtonY.ToString();
  458. _plus.Label = controllerConfig.RightJoycon.ButtonPlus.ToString();
  459. _r.Label = controllerConfig.RightJoycon.ButtonR.ToString();
  460. _zR.Label = controllerConfig.RightJoycon.ButtonZr.ToString();
  461. _rSl.Label = controllerConfig.RightJoycon.ButtonSl.ToString();
  462. _rSr.Label = controllerConfig.RightJoycon.ButtonSr.ToString();
  463. _controllerStrongRumble.Value = controllerConfig.Rumble.StrongRumble;
  464. _controllerWeakRumble.Value = controllerConfig.Rumble.WeakRumble;
  465. _enableRumble.Active = controllerConfig.Rumble.EnableRumble;
  466. _controllerDeadzoneLeft.Value = controllerConfig.DeadzoneLeft;
  467. _controllerDeadzoneRight.Value = controllerConfig.DeadzoneRight;
  468. _controllerRangeLeft.Value = controllerConfig.RangeLeft;
  469. _controllerRangeRight.Value = controllerConfig.RangeRight;
  470. _controllerTriggerThreshold.Value = controllerConfig.TriggerThreshold;
  471. _sensitivity.Value = controllerConfig.Motion.Sensitivity;
  472. _gyroDeadzone.Value = controllerConfig.Motion.GyroDeadzone;
  473. _enableMotion.Active = controllerConfig.Motion.EnableMotion;
  474. _enableCemuHook.Active = controllerConfig.Motion.MotionBackend == MotionInputBackendType.CemuHook;
  475. // If both stick ranges are 0 (usually indicative of an outdated profile load) then both sticks will be set to 1.0.
  476. if (_controllerRangeLeft.Value <= 0.0 && _controllerRangeRight.Value <= 0.0)
  477. {
  478. _controllerRangeLeft.Value = 1.0;
  479. _controllerRangeRight.Value = 1.0;
  480. Logger.Info?.Print(LogClass.Application, $"{config.PlayerIndex} stick range reset. Save the profile now to update your configuration");
  481. }
  482. if (controllerConfig.Motion is CemuHookMotionConfigController cemuHookMotionConfig)
  483. {
  484. _slotNumber.Value = cemuHookMotionConfig.Slot;
  485. _altSlotNumber.Value = cemuHookMotionConfig.AltSlot;
  486. _mirrorInput.Active = cemuHookMotionConfig.MirrorInput;
  487. _dsuServerHost.Buffer.Text = cemuHookMotionConfig.DsuServerHost;
  488. _dsuServerPort.Buffer.Text = cemuHookMotionConfig.DsuServerPort.ToString();
  489. }
  490. break;
  491. }
  492. }
  493. private InputConfig GetValues()
  494. {
  495. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  496. {
  497. Enum.TryParse(_lStickUp.Label, out Key lStickUp);
  498. Enum.TryParse(_lStickDown.Label, out Key lStickDown);
  499. Enum.TryParse(_lStickLeft.Label, out Key lStickLeft);
  500. Enum.TryParse(_lStickRight.Label, out Key lStickRight);
  501. Enum.TryParse(_lStickButton.Label, out Key lStickButton);
  502. Enum.TryParse(_dpadUp.Label, out Key lDPadUp);
  503. Enum.TryParse(_dpadDown.Label, out Key lDPadDown);
  504. Enum.TryParse(_dpadLeft.Label, out Key lDPadLeft);
  505. Enum.TryParse(_dpadRight.Label, out Key lDPadRight);
  506. Enum.TryParse(_minus.Label, out Key lButtonMinus);
  507. Enum.TryParse(_l.Label, out Key lButtonL);
  508. Enum.TryParse(_zL.Label, out Key lButtonZl);
  509. Enum.TryParse(_lSl.Label, out Key lButtonSl);
  510. Enum.TryParse(_lSr.Label, out Key lButtonSr);
  511. Enum.TryParse(_rStickUp.Label, out Key rStickUp);
  512. Enum.TryParse(_rStickDown.Label, out Key rStickDown);
  513. Enum.TryParse(_rStickLeft.Label, out Key rStickLeft);
  514. Enum.TryParse(_rStickRight.Label, out Key rStickRight);
  515. Enum.TryParse(_rStickButton.Label, out Key rStickButton);
  516. Enum.TryParse(_a.Label, out Key rButtonA);
  517. Enum.TryParse(_b.Label, out Key rButtonB);
  518. Enum.TryParse(_x.Label, out Key rButtonX);
  519. Enum.TryParse(_y.Label, out Key rButtonY);
  520. Enum.TryParse(_plus.Label, out Key rButtonPlus);
  521. Enum.TryParse(_r.Label, out Key rButtonR);
  522. Enum.TryParse(_zR.Label, out Key rButtonZr);
  523. Enum.TryParse(_rSl.Label, out Key rButtonSl);
  524. Enum.TryParse(_rSr.Label, out Key rButtonSr);
  525. return new StandardKeyboardInputConfig
  526. {
  527. Backend = InputBackendType.WindowKeyboard,
  528. Version = InputConfig.CurrentVersion,
  529. Id = _inputDevice.ActiveId.Split("/")[1],
  530. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  531. PlayerIndex = _playerIndex,
  532. LeftJoycon = new LeftJoyconCommonConfig<Key>
  533. {
  534. ButtonMinus = lButtonMinus,
  535. ButtonL = lButtonL,
  536. ButtonZl = lButtonZl,
  537. ButtonSl = lButtonSl,
  538. ButtonSr = lButtonSr,
  539. DpadUp = lDPadUp,
  540. DpadDown = lDPadDown,
  541. DpadLeft = lDPadLeft,
  542. DpadRight = lDPadRight
  543. },
  544. LeftJoyconStick = new JoyconConfigKeyboardStick<Key>
  545. {
  546. StickUp = lStickUp,
  547. StickDown = lStickDown,
  548. StickLeft = lStickLeft,
  549. StickRight = lStickRight,
  550. StickButton = lStickButton,
  551. },
  552. RightJoycon = new RightJoyconCommonConfig<Key>
  553. {
  554. ButtonA = rButtonA,
  555. ButtonB = rButtonB,
  556. ButtonX = rButtonX,
  557. ButtonY = rButtonY,
  558. ButtonPlus = rButtonPlus,
  559. ButtonR = rButtonR,
  560. ButtonZr = rButtonZr,
  561. ButtonSl = rButtonSl,
  562. ButtonSr = rButtonSr
  563. },
  564. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  565. {
  566. StickUp = rStickUp,
  567. StickDown = rStickDown,
  568. StickLeft = rStickLeft,
  569. StickRight = rStickRight,
  570. StickButton = rStickButton,
  571. },
  572. };
  573. }
  574. if (_inputDevice.ActiveId.StartsWith("controller"))
  575. {
  576. Enum.TryParse(_lStick.Label, out ConfigStickInputId lStick);
  577. Enum.TryParse(_lStickButton.Label, out ConfigGamepadInputId lStickButton);
  578. Enum.TryParse(_minus.Label, out ConfigGamepadInputId lButtonMinus);
  579. Enum.TryParse(_l.Label, out ConfigGamepadInputId lButtonL);
  580. Enum.TryParse(_zL.Label, out ConfigGamepadInputId lButtonZl);
  581. Enum.TryParse(_lSl.Label, out ConfigGamepadInputId lButtonSl);
  582. Enum.TryParse(_lSr.Label, out ConfigGamepadInputId lButtonSr);
  583. Enum.TryParse(_dpadUp.Label, out ConfigGamepadInputId lDPadUp);
  584. Enum.TryParse(_dpadDown.Label, out ConfigGamepadInputId lDPadDown);
  585. Enum.TryParse(_dpadLeft.Label, out ConfigGamepadInputId lDPadLeft);
  586. Enum.TryParse(_dpadRight.Label, out ConfigGamepadInputId lDPadRight);
  587. Enum.TryParse(_rStick.Label, out ConfigStickInputId rStick);
  588. Enum.TryParse(_rStickButton.Label, out ConfigGamepadInputId rStickButton);
  589. Enum.TryParse(_a.Label, out ConfigGamepadInputId rButtonA);
  590. Enum.TryParse(_b.Label, out ConfigGamepadInputId rButtonB);
  591. Enum.TryParse(_x.Label, out ConfigGamepadInputId rButtonX);
  592. Enum.TryParse(_y.Label, out ConfigGamepadInputId rButtonY);
  593. Enum.TryParse(_plus.Label, out ConfigGamepadInputId rButtonPlus);
  594. Enum.TryParse(_r.Label, out ConfigGamepadInputId rButtonR);
  595. Enum.TryParse(_zR.Label, out ConfigGamepadInputId rButtonZr);
  596. Enum.TryParse(_rSl.Label, out ConfigGamepadInputId rButtonSl);
  597. Enum.TryParse(_rSr.Label, out ConfigGamepadInputId rButtonSr);
  598. int.TryParse(_dsuServerPort.Buffer.Text, out int port);
  599. MotionConfigController motionConfig;
  600. if (_enableCemuHook.Active)
  601. {
  602. motionConfig = new CemuHookMotionConfigController
  603. {
  604. MotionBackend = MotionInputBackendType.CemuHook,
  605. EnableMotion = _enableMotion.Active,
  606. Sensitivity = (int)_sensitivity.Value,
  607. GyroDeadzone = _gyroDeadzone.Value,
  608. MirrorInput = _mirrorInput.Active,
  609. Slot = (int)_slotNumber.Value,
  610. AltSlot = (int)_altSlotNumber.Value,
  611. DsuServerHost = _dsuServerHost.Buffer.Text,
  612. DsuServerPort = port
  613. };
  614. }
  615. else
  616. {
  617. motionConfig = new StandardMotionConfigController
  618. {
  619. MotionBackend = MotionInputBackendType.GamepadDriver,
  620. EnableMotion = _enableMotion.Active,
  621. Sensitivity = (int)_sensitivity.Value,
  622. GyroDeadzone = _gyroDeadzone.Value,
  623. };
  624. }
  625. return new StandardControllerInputConfig
  626. {
  627. Backend = InputBackendType.GamepadSDL2,
  628. Version = InputConfig.CurrentVersion,
  629. Id = _inputDevice.ActiveId.Split("/")[1].Split(" ")[0],
  630. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  631. PlayerIndex = _playerIndex,
  632. DeadzoneLeft = (float)_controllerDeadzoneLeft.Value,
  633. DeadzoneRight = (float)_controllerDeadzoneRight.Value,
  634. RangeLeft = (float)_controllerRangeLeft.Value,
  635. RangeRight = (float)_controllerRangeRight.Value,
  636. TriggerThreshold = (float)_controllerTriggerThreshold.Value,
  637. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  638. {
  639. ButtonMinus = lButtonMinus,
  640. ButtonL = lButtonL,
  641. ButtonZl = lButtonZl,
  642. ButtonSl = lButtonSl,
  643. ButtonSr = lButtonSr,
  644. DpadUp = lDPadUp,
  645. DpadDown = lDPadDown,
  646. DpadLeft = lDPadLeft,
  647. DpadRight = lDPadRight
  648. },
  649. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  650. {
  651. InvertStickX = _invertLStickX.Active,
  652. Joystick = lStick,
  653. InvertStickY = _invertLStickY.Active,
  654. StickButton = lStickButton,
  655. },
  656. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  657. {
  658. ButtonA = rButtonA,
  659. ButtonB = rButtonB,
  660. ButtonX = rButtonX,
  661. ButtonY = rButtonY,
  662. ButtonPlus = rButtonPlus,
  663. ButtonR = rButtonR,
  664. ButtonZr = rButtonZr,
  665. ButtonSl = rButtonSl,
  666. ButtonSr = rButtonSr
  667. },
  668. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  669. {
  670. InvertStickX = _invertRStickX.Active,
  671. Joystick = rStick,
  672. InvertStickY = _invertRStickY.Active,
  673. StickButton = rStickButton,
  674. },
  675. Motion = motionConfig,
  676. Rumble = new RumbleConfigController
  677. {
  678. StrongRumble = (float)_controllerStrongRumble.Value,
  679. WeakRumble = (float)_controllerWeakRumble.Value,
  680. EnableRumble = _enableRumble.Active
  681. }
  682. };
  683. }
  684. if (!_inputDevice.ActiveId.StartsWith("disabled"))
  685. {
  686. GtkDialog.CreateErrorDialog("Invalid data detected in one or more fields; the configuration was not saved.");
  687. }
  688. return null;
  689. }
  690. private string GetProfileBasePath()
  691. {
  692. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  693. {
  694. return System.IO.Path.Combine(AppDataManager.ProfilesDirPath, "keyboard");
  695. }
  696. else if (_inputDevice.ActiveId.StartsWith("controller"))
  697. {
  698. return System.IO.Path.Combine(AppDataManager.ProfilesDirPath, "controller");
  699. }
  700. return AppDataManager.ProfilesDirPath;
  701. }
  702. //
  703. // Events
  704. //
  705. private void InputDevice_Changed(object sender, EventArgs args)
  706. {
  707. SetAvailableOptions();
  708. SetControllerSpecificFields();
  709. _selectedGamepad?.Dispose();
  710. _selectedGamepad = null;
  711. if (_inputDevice.ActiveId != null)
  712. {
  713. SetProfiles();
  714. string id = GetCurrentGamepadId();
  715. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  716. {
  717. if (_inputConfig is StandardKeyboardInputConfig)
  718. {
  719. SetValues(_inputConfig);
  720. }
  721. if (_mainWindow.InputManager.KeyboardDriver is GTK3KeyboardDriver)
  722. {
  723. // NOTE: To get input in this window, we need to bind a custom keyboard driver instead of using the InputManager one as the main window isn't focused...
  724. _selectedGamepad = _gtk3KeyboardDriver.GetGamepad(id);
  725. }
  726. else
  727. {
  728. _selectedGamepad = _mainWindow.InputManager.KeyboardDriver.GetGamepad(id);
  729. }
  730. }
  731. else if (_inputDevice.ActiveId.StartsWith("controller"))
  732. {
  733. if (_inputConfig is StandardControllerInputConfig)
  734. {
  735. SetValues(_inputConfig);
  736. }
  737. _selectedGamepad = _mainWindow.InputManager.GamepadDriver.GetGamepad(id);
  738. }
  739. }
  740. }
  741. private string GetCurrentGamepadId()
  742. {
  743. if (_inputDevice.ActiveId == null || _inputDevice.ActiveId == "disabled")
  744. {
  745. return null;
  746. }
  747. return _inputDevice.ActiveId.Split("/")[1].Split(" ")[0];
  748. }
  749. private void Controller_Changed(object sender, EventArgs args)
  750. {
  751. SetControllerSpecificFields();
  752. }
  753. private IButtonAssigner CreateButtonAssigner(bool forStick)
  754. {
  755. IButtonAssigner assigner;
  756. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  757. {
  758. assigner = new KeyboardKeyAssigner((IKeyboard)_selectedGamepad);
  759. }
  760. else if (_inputDevice.ActiveId.StartsWith("controller"))
  761. {
  762. assigner = new GamepadButtonAssigner(_selectedGamepad, (float)_controllerTriggerThreshold.Value, forStick);
  763. }
  764. else
  765. {
  766. throw new Exception("Controller not supported");
  767. }
  768. return assigner;
  769. }
  770. private void HandleButtonPressed(ToggleButton button, bool forStick)
  771. {
  772. if (_isWaitingForInput)
  773. {
  774. button.Active = false;
  775. return;
  776. }
  777. _mousePressed = false;
  778. ButtonPressEvent += MouseClick;
  779. IButtonAssigner assigner = CreateButtonAssigner(forStick);
  780. _isWaitingForInput = true;
  781. // Open GTK3 keyboard for cancel operations
  782. IKeyboard keyboard = (IKeyboard)_gtk3KeyboardDriver.GetGamepad("0");
  783. Thread inputThread = new Thread(() =>
  784. {
  785. assigner.Initialize();
  786. while (true)
  787. {
  788. Thread.Sleep(10);
  789. assigner.ReadInput();
  790. if (_mousePressed || keyboard.IsPressed(Ryujinx.Input.Key.Escape) || assigner.HasAnyButtonPressed() || assigner.ShouldCancel())
  791. {
  792. break;
  793. }
  794. }
  795. string pressedButton = assigner.GetPressedButton();
  796. Application.Invoke(delegate
  797. {
  798. if (_middleMousePressed)
  799. {
  800. button.Label = "Unbound";
  801. }
  802. else if (pressedButton != "")
  803. {
  804. button.Label = pressedButton;
  805. }
  806. _middleMousePressed = false;
  807. ButtonPressEvent -= MouseClick;
  808. keyboard.Dispose();
  809. button.Active = false;
  810. _isWaitingForInput = false;
  811. });
  812. });
  813. inputThread.Name = "GUI.InputThread";
  814. inputThread.IsBackground = true;
  815. inputThread.Start();
  816. }
  817. private void Button_Pressed(object sender, EventArgs args)
  818. {
  819. HandleButtonPressed((ToggleButton)sender, false);
  820. }
  821. private void ButtonForStick_Pressed(object sender, EventArgs args)
  822. {
  823. HandleButtonPressed((ToggleButton)sender, true);
  824. }
  825. private void MouseClick(object sender, ButtonPressEventArgs args)
  826. {
  827. _mousePressed = true;
  828. _middleMousePressed = args.Event.Button == 2;
  829. }
  830. private void SetProfiles()
  831. {
  832. _profile.RemoveAll();
  833. string basePath = GetProfileBasePath();
  834. if (!Directory.Exists(basePath))
  835. {
  836. Directory.CreateDirectory(basePath);
  837. }
  838. if (_inputDevice.ActiveId == null|| _inputDevice.ActiveId.Equals("disabled"))
  839. {
  840. _profile.Append("default", "None");
  841. }
  842. else
  843. {
  844. _profile.Append("default", "Default");
  845. foreach (string profile in Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories))
  846. {
  847. _profile.Append(System.IO.Path.GetFileName(profile), System.IO.Path.GetFileNameWithoutExtension(profile));
  848. }
  849. }
  850. _profile.SetActiveId("default");
  851. }
  852. private void ProfileLoad_Activated(object sender, EventArgs args)
  853. {
  854. ((ToggleButton)sender).SetStateFlags(StateFlags.Normal, true);
  855. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == null) return;
  856. InputConfig config = null;
  857. int pos = _profile.Active;
  858. if (_profile.ActiveId == "default")
  859. {
  860. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  861. {
  862. config = new StandardKeyboardInputConfig
  863. {
  864. Version = InputConfig.CurrentVersion,
  865. Backend = InputBackendType.WindowKeyboard,
  866. Id = null,
  867. ControllerType = ControllerType.ProController,
  868. LeftJoycon = new LeftJoyconCommonConfig<Key>
  869. {
  870. DpadUp = Key.Up,
  871. DpadDown = Key.Down,
  872. DpadLeft = Key.Left,
  873. DpadRight = Key.Right,
  874. ButtonMinus = Key.Minus,
  875. ButtonL = Key.E,
  876. ButtonZl = Key.Q,
  877. ButtonSl = Key.Unbound,
  878. ButtonSr = Key.Unbound
  879. },
  880. LeftJoyconStick = new JoyconConfigKeyboardStick<Key>
  881. {
  882. StickUp = Key.W,
  883. StickDown = Key.S,
  884. StickLeft = Key.A,
  885. StickRight = Key.D,
  886. StickButton = Key.F,
  887. },
  888. RightJoycon = new RightJoyconCommonConfig<Key>
  889. {
  890. ButtonA = Key.Z,
  891. ButtonB = Key.X,
  892. ButtonX = Key.C,
  893. ButtonY = Key.V,
  894. ButtonPlus = Key.Plus,
  895. ButtonR = Key.U,
  896. ButtonZr = Key.O,
  897. ButtonSl = Key.Unbound,
  898. ButtonSr = Key.Unbound
  899. },
  900. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  901. {
  902. StickUp = Key.I,
  903. StickDown = Key.K,
  904. StickLeft = Key.J,
  905. StickRight = Key.L,
  906. StickButton = Key.H,
  907. }
  908. };
  909. }
  910. else if (_inputDevice.ActiveId.StartsWith("controller"))
  911. {
  912. bool isNintendoStyle = _inputDevice.ActiveText.Contains("Nintendo");
  913. config = new StandardControllerInputConfig
  914. {
  915. Version = InputConfig.CurrentVersion,
  916. Backend = InputBackendType.GamepadSDL2,
  917. Id = null,
  918. ControllerType = ControllerType.JoyconPair,
  919. DeadzoneLeft = 0.1f,
  920. DeadzoneRight = 0.1f,
  921. RangeLeft = 1.0f,
  922. RangeRight = 1.0f,
  923. TriggerThreshold = 0.5f,
  924. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  925. {
  926. DpadUp = ConfigGamepadInputId.DpadUp,
  927. DpadDown = ConfigGamepadInputId.DpadDown,
  928. DpadLeft = ConfigGamepadInputId.DpadLeft,
  929. DpadRight = ConfigGamepadInputId.DpadRight,
  930. ButtonMinus = ConfigGamepadInputId.Minus,
  931. ButtonL = ConfigGamepadInputId.LeftShoulder,
  932. ButtonZl = ConfigGamepadInputId.LeftTrigger,
  933. ButtonSl = ConfigGamepadInputId.Unbound,
  934. ButtonSr = ConfigGamepadInputId.Unbound,
  935. },
  936. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  937. {
  938. Joystick = ConfigStickInputId.Left,
  939. StickButton = ConfigGamepadInputId.LeftStick,
  940. InvertStickX = false,
  941. InvertStickY = false,
  942. },
  943. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  944. {
  945. ButtonA = isNintendoStyle ? ConfigGamepadInputId.A : ConfigGamepadInputId.B,
  946. ButtonB = isNintendoStyle ? ConfigGamepadInputId.B : ConfigGamepadInputId.A,
  947. ButtonX = isNintendoStyle ? ConfigGamepadInputId.X : ConfigGamepadInputId.Y,
  948. ButtonY = isNintendoStyle ? ConfigGamepadInputId.Y : ConfigGamepadInputId.X,
  949. ButtonPlus = ConfigGamepadInputId.Plus,
  950. ButtonR = ConfigGamepadInputId.RightShoulder,
  951. ButtonZr = ConfigGamepadInputId.RightTrigger,
  952. ButtonSl = ConfigGamepadInputId.Unbound,
  953. ButtonSr = ConfigGamepadInputId.Unbound,
  954. },
  955. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  956. {
  957. Joystick = ConfigStickInputId.Right,
  958. StickButton = ConfigGamepadInputId.RightStick,
  959. InvertStickX = false,
  960. InvertStickY = false,
  961. },
  962. Motion = new StandardMotionConfigController
  963. {
  964. MotionBackend = MotionInputBackendType.GamepadDriver,
  965. EnableMotion = true,
  966. Sensitivity = 100,
  967. GyroDeadzone = 1,
  968. },
  969. Rumble = new RumbleConfigController
  970. {
  971. StrongRumble = 1f,
  972. WeakRumble = 1f,
  973. EnableRumble = false
  974. }
  975. };
  976. }
  977. }
  978. else
  979. {
  980. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  981. if (!File.Exists(path))
  982. {
  983. if (pos >= 0)
  984. {
  985. _profile.Remove(pos);
  986. }
  987. return;
  988. }
  989. try
  990. {
  991. using (Stream stream = File.OpenRead(path))
  992. {
  993. config = JsonHelper.Deserialize<InputConfig>(stream);
  994. }
  995. }
  996. catch (JsonException) { }
  997. }
  998. SetValues(config);
  999. }
  1000. private void ProfileAdd_Activated(object sender, EventArgs args)
  1001. {
  1002. ((ToggleButton)sender).SetStateFlags(StateFlags.Normal, true);
  1003. if (_inputDevice.ActiveId == "disabled") return;
  1004. InputConfig inputConfig = GetValues();
  1005. ProfileDialog profileDialog = new ProfileDialog();
  1006. if (inputConfig == null) return;
  1007. if (profileDialog.Run() == (int)ResponseType.Ok)
  1008. {
  1009. string path = System.IO.Path.Combine(GetProfileBasePath(), profileDialog.FileName);
  1010. string jsonString;
  1011. jsonString = JsonHelper.Serialize(inputConfig, true);
  1012. File.WriteAllText(path, jsonString);
  1013. }
  1014. profileDialog.Dispose();
  1015. SetProfiles();
  1016. }
  1017. private void ProfileRemove_Activated(object sender, EventArgs args)
  1018. {
  1019. ((ToggleButton) sender).SetStateFlags(StateFlags.Normal, true);
  1020. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == "default" || _profile.ActiveId == null) return;
  1021. MessageDialog confirmDialog = GtkDialog.CreateConfirmationDialog("Deleting Profile", "This action is irreversible, are you sure you want to continue?");
  1022. if (confirmDialog.Run() == (int)ResponseType.Yes)
  1023. {
  1024. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  1025. if (File.Exists(path))
  1026. {
  1027. File.Delete(path);
  1028. }
  1029. SetProfiles();
  1030. }
  1031. }
  1032. private void SaveToggle_Activated(object sender, EventArgs args)
  1033. {
  1034. InputConfig inputConfig = GetValues();
  1035. var newConfig = new List<InputConfig>();
  1036. newConfig.AddRange(ConfigurationState.Instance.Hid.InputConfig.Value);
  1037. if (_inputConfig == null && inputConfig != null)
  1038. {
  1039. newConfig.Add(inputConfig);
  1040. }
  1041. else
  1042. {
  1043. if (_inputDevice.ActiveId == "disabled")
  1044. {
  1045. newConfig.Remove(_inputConfig);
  1046. }
  1047. else if (inputConfig != null)
  1048. {
  1049. int index = newConfig.IndexOf(_inputConfig);
  1050. newConfig[index] = inputConfig;
  1051. }
  1052. }
  1053. if (_mainWindow.RendererWidget != null)
  1054. {
  1055. _mainWindow.RendererWidget.NpadManager.ReloadConfiguration(newConfig, ConfigurationState.Instance.Hid.EnableKeyboard, ConfigurationState.Instance.Hid.EnableMouse);
  1056. }
  1057. // Atomically replace and signal input change.
  1058. // NOTE: Do not modify InputConfig.Value directly as other code depends on the on-change event.
  1059. ConfigurationState.Instance.Hid.InputConfig.Value = newConfig;
  1060. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  1061. Dispose();
  1062. }
  1063. private void CloseToggle_Activated(object sender, EventArgs args)
  1064. {
  1065. Dispose();
  1066. }
  1067. }
  1068. }