ControllerWindow.cs 54 KB

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