ControllerWindow.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. using Gtk;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Common.Configuration.Hid;
  4. using Ryujinx.Common.Configuration.Hid.Controller;
  5. using Ryujinx.Common.Configuration.Hid.Keyboard;
  6. using Ryujinx.Common.Utilities;
  7. using Ryujinx.Ui.Common.Configuration;
  8. using Ryujinx.Input;
  9. using Ryujinx.Input.GTK3;
  10. using Ryujinx.Ui.Widgets;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Text.Json;
  16. using System.Threading;
  17. using GUI = Gtk.Builder.ObjectAttribute;
  18. using Key = Ryujinx.Common.Configuration.Hid.Key;
  19. using ConfigGamepadInputId = Ryujinx.Common.Configuration.Hid.Controller.GamepadInputId;
  20. using ConfigStickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
  21. using Ryujinx.Common.Configuration.Hid.Controller.Motion;
  22. using Ryujinx.Common.Logging;
  23. using Ryujinx.Input.Assigner;
  24. namespace Ryujinx.Ui.Windows
  25. {
  26. public class ControllerWindow : Window
  27. {
  28. private readonly PlayerIndex _playerIndex;
  29. private readonly InputConfig _inputConfig;
  30. private bool _isWaitingForInput;
  31. #pragma warning disable CS0649, IDE0044
  32. [GUI] Adjustment _controllerStrongRumble;
  33. [GUI] Adjustment _controllerWeakRumble;
  34. [GUI] Adjustment _controllerDeadzoneLeft;
  35. [GUI] Adjustment _controllerDeadzoneRight;
  36. [GUI] Adjustment _controllerRangeLeft;
  37. [GUI] Adjustment _controllerRangeRight;
  38. [GUI] Adjustment _controllerTriggerThreshold;
  39. [GUI] Adjustment _slotNumber;
  40. [GUI] Adjustment _altSlotNumber;
  41. [GUI] Adjustment _sensitivity;
  42. [GUI] Adjustment _gyroDeadzone;
  43. [GUI] CheckButton _enableMotion;
  44. [GUI] CheckButton _enableCemuHook;
  45. [GUI] CheckButton _mirrorInput;
  46. [GUI] Entry _dsuServerHost;
  47. [GUI] Entry _dsuServerPort;
  48. [GUI] ComboBoxText _inputDevice;
  49. [GUI] ComboBoxText _profile;
  50. [GUI] Box _settingsBox;
  51. [GUI] Box _motionAltBox;
  52. [GUI] Box _motionBox;
  53. [GUI] Box _dsuServerHostBox;
  54. [GUI] Box _dsuServerPortBox;
  55. [GUI] Box _motionControllerSlot;
  56. [GUI] Grid _leftStickKeyboard;
  57. [GUI] Grid _leftStickController;
  58. [GUI] Box _deadZoneLeftBox;
  59. [GUI] Box _rangeLeftBox;
  60. [GUI] Grid _rightStickKeyboard;
  61. [GUI] Grid _rightStickController;
  62. [GUI] Box _deadZoneRightBox;
  63. [GUI] Box _rangeRightBox;
  64. [GUI] Grid _leftSideTriggerBox;
  65. [GUI] Grid _rightSideTriggerBox;
  66. [GUI] Box _triggerThresholdBox;
  67. [GUI] ComboBoxText _controllerType;
  68. [GUI] ToggleButton _lStick;
  69. [GUI] CheckButton _invertLStickX;
  70. [GUI] CheckButton _invertLStickY;
  71. [GUI] 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.Substring(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. }