ControllerWindow.cs 54 KB

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