ControllerWindow.cs 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. using Gtk;
  2. using Ryujinx.Common.Configuration;
  3. using Ryujinx.Common.Configuration.Hid;
  4. using Ryujinx.Common.Configuration.Hid.Controller;
  5. using Ryujinx.Common.Configuration.Hid.Keyboard;
  6. using Ryujinx.Common.Utilities;
  7. using Ryujinx.Configuration;
  8. using Ryujinx.Input;
  9. using Ryujinx.Input.GTK3;
  10. using Ryujinx.Ui.Widgets;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Text.Json;
  16. using System.Threading;
  17. using GUI = Gtk.Builder.ObjectAttribute;
  18. using Key = Ryujinx.Common.Configuration.Hid.Key;
  19. using ConfigGamepadInputId = Ryujinx.Common.Configuration.Hid.Controller.GamepadInputId;
  20. using ConfigStickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
  21. using Ryujinx.Common.Configuration.Hid.Controller.Motion;
  22. using Ryujinx.Common.Logging;
  23. using Ryujinx.Input.Assigner;
  24. namespace Ryujinx.Ui.Windows
  25. {
  26. public class ControllerWindow : Window
  27. {
  28. private readonly PlayerIndex _playerIndex;
  29. private readonly InputConfig _inputConfig;
  30. private bool _isWaitingForInput;
  31. #pragma warning disable CS0649, IDE0044
  32. [GUI] Adjustment _controllerStrongRumble;
  33. [GUI] Adjustment _controllerWeakRumble;
  34. [GUI] Adjustment _controllerDeadzoneLeft;
  35. [GUI] Adjustment _controllerDeadzoneRight;
  36. [GUI] Adjustment _controllerRangeLeft;
  37. [GUI] Adjustment _controllerRangeRight;
  38. [GUI] Adjustment _controllerTriggerThreshold;
  39. [GUI] Adjustment _slotNumber;
  40. [GUI] Adjustment _altSlotNumber;
  41. [GUI] Adjustment _sensitivity;
  42. [GUI] Adjustment _gyroDeadzone;
  43. [GUI] CheckButton _enableMotion;
  44. [GUI] CheckButton _enableCemuHook;
  45. [GUI] CheckButton _mirrorInput;
  46. [GUI] Entry _dsuServerHost;
  47. [GUI] Entry _dsuServerPort;
  48. [GUI] ComboBoxText _inputDevice;
  49. [GUI] ComboBoxText _profile;
  50. [GUI] Box _settingsBox;
  51. [GUI] Box _motionAltBox;
  52. [GUI] Box _motionBox;
  53. [GUI] Box _dsuServerHostBox;
  54. [GUI] Box _dsuServerPortBox;
  55. [GUI] Box _motionControllerSlot;
  56. [GUI] Grid _leftStickKeyboard;
  57. [GUI] Grid _leftStickController;
  58. [GUI] Box _deadZoneLeftBox;
  59. [GUI] Box _rangeLeftBox;
  60. [GUI] Grid _rightStickKeyboard;
  61. [GUI] Grid _rightStickController;
  62. [GUI] Box _deadZoneRightBox;
  63. [GUI] Box _rangeRightBox;
  64. [GUI] Grid _leftSideTriggerBox;
  65. [GUI] Grid _rightSideTriggerBox;
  66. [GUI] Box _triggerThresholdBox;
  67. [GUI] ComboBoxText _controllerType;
  68. [GUI] ToggleButton _lStick;
  69. [GUI] CheckButton _invertLStickX;
  70. [GUI] CheckButton _invertLStickY;
  71. [GUI] 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.GetObject("_controllerWin").Handle)
  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.GetExecutingAssembly(), "Ryujinx.Ui.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 GetShrinkedGamepadName(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}", GetShrinkedGamepadName($"{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}", GetShrinkedGamepadName($"{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. _controllerImage.Pixbuf = _controllerType.ActiveId switch
  335. {
  336. "ProController" => new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Controller_ProCon.svg", 400, 400),
  337. "JoyconLeft" => new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Controller_JoyConLeft.svg", 400, 500),
  338. "JoyconRight" => new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Controller_JoyConRight.svg", 400, 500),
  339. _ => new Gdk.Pixbuf(Assembly.GetExecutingAssembly(), "Ryujinx.Ui.Resources.Controller_JoyConPair.svg", 400, 500),
  340. };
  341. }
  342. private void ClearValues()
  343. {
  344. _lStick.Label = "Unbound";
  345. _lStickUp.Label = "Unbound";
  346. _lStickDown.Label = "Unbound";
  347. _lStickLeft.Label = "Unbound";
  348. _lStickRight.Label = "Unbound";
  349. _lStickButton.Label = "Unbound";
  350. _dpadUp.Label = "Unbound";
  351. _dpadDown.Label = "Unbound";
  352. _dpadLeft.Label = "Unbound";
  353. _dpadRight.Label = "Unbound";
  354. _minus.Label = "Unbound";
  355. _l.Label = "Unbound";
  356. _zL.Label = "Unbound";
  357. _lSl.Label = "Unbound";
  358. _lSr.Label = "Unbound";
  359. _rStick.Label = "Unbound";
  360. _rStickUp.Label = "Unbound";
  361. _rStickDown.Label = "Unbound";
  362. _rStickLeft.Label = "Unbound";
  363. _rStickRight.Label = "Unbound";
  364. _rStickButton.Label = "Unbound";
  365. _a.Label = "Unbound";
  366. _b.Label = "Unbound";
  367. _x.Label = "Unbound";
  368. _y.Label = "Unbound";
  369. _plus.Label = "Unbound";
  370. _r.Label = "Unbound";
  371. _zR.Label = "Unbound";
  372. _rSl.Label = "Unbound";
  373. _rSr.Label = "Unbound";
  374. _controllerStrongRumble.Value = 1;
  375. _controllerWeakRumble.Value = 1;
  376. _controllerDeadzoneLeft.Value = 0;
  377. _controllerDeadzoneRight.Value = 0;
  378. _controllerRangeLeft.Value = 1;
  379. _controllerRangeRight.Value = 1;
  380. _controllerTriggerThreshold.Value = 0;
  381. _mirrorInput.Active = false;
  382. _enableMotion.Active = false;
  383. _enableCemuHook.Active = false;
  384. _slotNumber.Value = 0;
  385. _altSlotNumber.Value = 0;
  386. _sensitivity.Value = 100;
  387. _gyroDeadzone.Value = 1;
  388. _dsuServerHost.Buffer.Text = "";
  389. _dsuServerPort.Buffer.Text = "";
  390. _enableRumble.Active = false;
  391. }
  392. private void SetValues(InputConfig config)
  393. {
  394. switch (config)
  395. {
  396. case StandardKeyboardInputConfig keyboardConfig:
  397. if (!_controllerType.SetActiveId(keyboardConfig.ControllerType.ToString()))
  398. {
  399. _controllerType.SetActiveId(_playerIndex == PlayerIndex.Handheld
  400. ? ControllerType.Handheld.ToString()
  401. : ControllerType.ProController.ToString());
  402. }
  403. _lStickUp.Label = keyboardConfig.LeftJoyconStick.StickUp.ToString();
  404. _lStickDown.Label = keyboardConfig.LeftJoyconStick.StickDown.ToString();
  405. _lStickLeft.Label = keyboardConfig.LeftJoyconStick.StickLeft.ToString();
  406. _lStickRight.Label = keyboardConfig.LeftJoyconStick.StickRight.ToString();
  407. _lStickButton.Label = keyboardConfig.LeftJoyconStick.StickButton.ToString();
  408. _dpadUp.Label = keyboardConfig.LeftJoycon.DpadUp.ToString();
  409. _dpadDown.Label = keyboardConfig.LeftJoycon.DpadDown.ToString();
  410. _dpadLeft.Label = keyboardConfig.LeftJoycon.DpadLeft.ToString();
  411. _dpadRight.Label = keyboardConfig.LeftJoycon.DpadRight.ToString();
  412. _minus.Label = keyboardConfig.LeftJoycon.ButtonMinus.ToString();
  413. _l.Label = keyboardConfig.LeftJoycon.ButtonL.ToString();
  414. _zL.Label = keyboardConfig.LeftJoycon.ButtonZl.ToString();
  415. _lSl.Label = keyboardConfig.LeftJoycon.ButtonSl.ToString();
  416. _lSr.Label = keyboardConfig.LeftJoycon.ButtonSr.ToString();
  417. _rStickUp.Label = keyboardConfig.RightJoyconStick.StickUp.ToString();
  418. _rStickDown.Label = keyboardConfig.RightJoyconStick.StickDown.ToString();
  419. _rStickLeft.Label = keyboardConfig.RightJoyconStick.StickLeft.ToString();
  420. _rStickRight.Label = keyboardConfig.RightJoyconStick.StickRight.ToString();
  421. _rStickButton.Label = keyboardConfig.RightJoyconStick.StickButton.ToString();
  422. _a.Label = keyboardConfig.RightJoycon.ButtonA.ToString();
  423. _b.Label = keyboardConfig.RightJoycon.ButtonB.ToString();
  424. _x.Label = keyboardConfig.RightJoycon.ButtonX.ToString();
  425. _y.Label = keyboardConfig.RightJoycon.ButtonY.ToString();
  426. _plus.Label = keyboardConfig.RightJoycon.ButtonPlus.ToString();
  427. _r.Label = keyboardConfig.RightJoycon.ButtonR.ToString();
  428. _zR.Label = keyboardConfig.RightJoycon.ButtonZr.ToString();
  429. _rSl.Label = keyboardConfig.RightJoycon.ButtonSl.ToString();
  430. _rSr.Label = keyboardConfig.RightJoycon.ButtonSr.ToString();
  431. break;
  432. case StandardControllerInputConfig controllerConfig:
  433. if (!_controllerType.SetActiveId(controllerConfig.ControllerType.ToString()))
  434. {
  435. _controllerType.SetActiveId(_playerIndex == PlayerIndex.Handheld
  436. ? ControllerType.Handheld.ToString()
  437. : ControllerType.ProController.ToString());
  438. }
  439. _lStick.Label = controllerConfig.LeftJoyconStick.Joystick.ToString();
  440. _invertLStickX.Active = controllerConfig.LeftJoyconStick.InvertStickX;
  441. _invertLStickY.Active = controllerConfig.LeftJoyconStick.InvertStickY;
  442. _rotateL90CW.Active = controllerConfig.LeftJoyconStick.Rotate90CW;
  443. _lStickButton.Label = controllerConfig.LeftJoyconStick.StickButton.ToString();
  444. _dpadUp.Label = controllerConfig.LeftJoycon.DpadUp.ToString();
  445. _dpadDown.Label = controllerConfig.LeftJoycon.DpadDown.ToString();
  446. _dpadLeft.Label = controllerConfig.LeftJoycon.DpadLeft.ToString();
  447. _dpadRight.Label = controllerConfig.LeftJoycon.DpadRight.ToString();
  448. _minus.Label = controllerConfig.LeftJoycon.ButtonMinus.ToString();
  449. _l.Label = controllerConfig.LeftJoycon.ButtonL.ToString();
  450. _zL.Label = controllerConfig.LeftJoycon.ButtonZl.ToString();
  451. _lSl.Label = controllerConfig.LeftJoycon.ButtonSl.ToString();
  452. _lSr.Label = controllerConfig.LeftJoycon.ButtonSr.ToString();
  453. _rStick.Label = controllerConfig.RightJoyconStick.Joystick.ToString();
  454. _invertRStickX.Active = controllerConfig.RightJoyconStick.InvertStickX;
  455. _invertRStickY.Active = controllerConfig.RightJoyconStick.InvertStickY;
  456. _rotateR90CW.Active = controllerConfig.RightJoyconStick.Rotate90CW;
  457. _rStickButton.Label = controllerConfig.RightJoyconStick.StickButton.ToString();
  458. _a.Label = controllerConfig.RightJoycon.ButtonA.ToString();
  459. _b.Label = controllerConfig.RightJoycon.ButtonB.ToString();
  460. _x.Label = controllerConfig.RightJoycon.ButtonX.ToString();
  461. _y.Label = controllerConfig.RightJoycon.ButtonY.ToString();
  462. _plus.Label = controllerConfig.RightJoycon.ButtonPlus.ToString();
  463. _r.Label = controllerConfig.RightJoycon.ButtonR.ToString();
  464. _zR.Label = controllerConfig.RightJoycon.ButtonZr.ToString();
  465. _rSl.Label = controllerConfig.RightJoycon.ButtonSl.ToString();
  466. _rSr.Label = controllerConfig.RightJoycon.ButtonSr.ToString();
  467. _controllerStrongRumble.Value = controllerConfig.Rumble.StrongRumble;
  468. _controllerWeakRumble.Value = controllerConfig.Rumble.WeakRumble;
  469. _enableRumble.Active = controllerConfig.Rumble.EnableRumble;
  470. _controllerDeadzoneLeft.Value = controllerConfig.DeadzoneLeft;
  471. _controllerDeadzoneRight.Value = controllerConfig.DeadzoneRight;
  472. _controllerRangeLeft.Value = controllerConfig.RangeLeft;
  473. _controllerRangeRight.Value = controllerConfig.RangeRight;
  474. _controllerTriggerThreshold.Value = controllerConfig.TriggerThreshold;
  475. _sensitivity.Value = controllerConfig.Motion.Sensitivity;
  476. _gyroDeadzone.Value = controllerConfig.Motion.GyroDeadzone;
  477. _enableMotion.Active = controllerConfig.Motion.EnableMotion;
  478. _enableCemuHook.Active = controllerConfig.Motion.MotionBackend == MotionInputBackendType.CemuHook;
  479. // If both stick ranges are 0 (usually indicative of an outdated profile load) then both sticks will be set to 1.0.
  480. if (_controllerRangeLeft.Value <= 0.0 && _controllerRangeRight.Value <= 0.0)
  481. {
  482. _controllerRangeLeft.Value = 1.0;
  483. _controllerRangeRight.Value = 1.0;
  484. Logger.Info?.Print(LogClass.Application, $"{config.PlayerIndex} stick range reset. Save the profile now to update your configuration");
  485. }
  486. if (controllerConfig.Motion is CemuHookMotionConfigController cemuHookMotionConfig)
  487. {
  488. _slotNumber.Value = cemuHookMotionConfig.Slot;
  489. _altSlotNumber.Value = cemuHookMotionConfig.AltSlot;
  490. _mirrorInput.Active = cemuHookMotionConfig.MirrorInput;
  491. _dsuServerHost.Buffer.Text = cemuHookMotionConfig.DsuServerHost;
  492. _dsuServerPort.Buffer.Text = cemuHookMotionConfig.DsuServerPort.ToString();
  493. }
  494. break;
  495. }
  496. }
  497. private InputConfig GetValues()
  498. {
  499. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  500. {
  501. Enum.TryParse(_lStickUp.Label, out Key lStickUp);
  502. Enum.TryParse(_lStickDown.Label, out Key lStickDown);
  503. Enum.TryParse(_lStickLeft.Label, out Key lStickLeft);
  504. Enum.TryParse(_lStickRight.Label, out Key lStickRight);
  505. Enum.TryParse(_lStickButton.Label, out Key lStickButton);
  506. Enum.TryParse(_dpadUp.Label, out Key lDPadUp);
  507. Enum.TryParse(_dpadDown.Label, out Key lDPadDown);
  508. Enum.TryParse(_dpadLeft.Label, out Key lDPadLeft);
  509. Enum.TryParse(_dpadRight.Label, out Key lDPadRight);
  510. Enum.TryParse(_minus.Label, out Key lButtonMinus);
  511. Enum.TryParse(_l.Label, out Key lButtonL);
  512. Enum.TryParse(_zL.Label, out Key lButtonZl);
  513. Enum.TryParse(_lSl.Label, out Key lButtonSl);
  514. Enum.TryParse(_lSr.Label, out Key lButtonSr);
  515. Enum.TryParse(_rStickUp.Label, out Key rStickUp);
  516. Enum.TryParse(_rStickDown.Label, out Key rStickDown);
  517. Enum.TryParse(_rStickLeft.Label, out Key rStickLeft);
  518. Enum.TryParse(_rStickRight.Label, out Key rStickRight);
  519. Enum.TryParse(_rStickButton.Label, out Key rStickButton);
  520. Enum.TryParse(_a.Label, out Key rButtonA);
  521. Enum.TryParse(_b.Label, out Key rButtonB);
  522. Enum.TryParse(_x.Label, out Key rButtonX);
  523. Enum.TryParse(_y.Label, out Key rButtonY);
  524. Enum.TryParse(_plus.Label, out Key rButtonPlus);
  525. Enum.TryParse(_r.Label, out Key rButtonR);
  526. Enum.TryParse(_zR.Label, out Key rButtonZr);
  527. Enum.TryParse(_rSl.Label, out Key rButtonSl);
  528. Enum.TryParse(_rSr.Label, out Key rButtonSr);
  529. return new StandardKeyboardInputConfig
  530. {
  531. Backend = InputBackendType.WindowKeyboard,
  532. Version = InputConfig.CurrentVersion,
  533. Id = _inputDevice.ActiveId.Split("/")[1],
  534. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  535. PlayerIndex = _playerIndex,
  536. LeftJoycon = new LeftJoyconCommonConfig<Key>
  537. {
  538. ButtonMinus = lButtonMinus,
  539. ButtonL = lButtonL,
  540. ButtonZl = lButtonZl,
  541. ButtonSl = lButtonSl,
  542. ButtonSr = lButtonSr,
  543. DpadUp = lDPadUp,
  544. DpadDown = lDPadDown,
  545. DpadLeft = lDPadLeft,
  546. DpadRight = lDPadRight
  547. },
  548. LeftJoyconStick = new JoyconConfigKeyboardStick<Key>
  549. {
  550. StickUp = lStickUp,
  551. StickDown = lStickDown,
  552. StickLeft = lStickLeft,
  553. StickRight = lStickRight,
  554. StickButton = lStickButton,
  555. },
  556. RightJoycon = new RightJoyconCommonConfig<Key>
  557. {
  558. ButtonA = rButtonA,
  559. ButtonB = rButtonB,
  560. ButtonX = rButtonX,
  561. ButtonY = rButtonY,
  562. ButtonPlus = rButtonPlus,
  563. ButtonR = rButtonR,
  564. ButtonZr = rButtonZr,
  565. ButtonSl = rButtonSl,
  566. ButtonSr = rButtonSr
  567. },
  568. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  569. {
  570. StickUp = rStickUp,
  571. StickDown = rStickDown,
  572. StickLeft = rStickLeft,
  573. StickRight = rStickRight,
  574. StickButton = rStickButton,
  575. },
  576. };
  577. }
  578. if (_inputDevice.ActiveId.StartsWith("controller"))
  579. {
  580. Enum.TryParse(_lStick.Label, out ConfigStickInputId lStick);
  581. Enum.TryParse(_lStickButton.Label, out ConfigGamepadInputId lStickButton);
  582. Enum.TryParse(_minus.Label, out ConfigGamepadInputId lButtonMinus);
  583. Enum.TryParse(_l.Label, out ConfigGamepadInputId lButtonL);
  584. Enum.TryParse(_zL.Label, out ConfigGamepadInputId lButtonZl);
  585. Enum.TryParse(_lSl.Label, out ConfigGamepadInputId lButtonSl);
  586. Enum.TryParse(_lSr.Label, out ConfigGamepadInputId lButtonSr);
  587. Enum.TryParse(_dpadUp.Label, out ConfigGamepadInputId lDPadUp);
  588. Enum.TryParse(_dpadDown.Label, out ConfigGamepadInputId lDPadDown);
  589. Enum.TryParse(_dpadLeft.Label, out ConfigGamepadInputId lDPadLeft);
  590. Enum.TryParse(_dpadRight.Label, out ConfigGamepadInputId lDPadRight);
  591. Enum.TryParse(_rStick.Label, out ConfigStickInputId rStick);
  592. Enum.TryParse(_rStickButton.Label, out ConfigGamepadInputId rStickButton);
  593. Enum.TryParse(_a.Label, out ConfigGamepadInputId rButtonA);
  594. Enum.TryParse(_b.Label, out ConfigGamepadInputId rButtonB);
  595. Enum.TryParse(_x.Label, out ConfigGamepadInputId rButtonX);
  596. Enum.TryParse(_y.Label, out ConfigGamepadInputId rButtonY);
  597. Enum.TryParse(_plus.Label, out ConfigGamepadInputId rButtonPlus);
  598. Enum.TryParse(_r.Label, out ConfigGamepadInputId rButtonR);
  599. Enum.TryParse(_zR.Label, out ConfigGamepadInputId rButtonZr);
  600. Enum.TryParse(_rSl.Label, out ConfigGamepadInputId rButtonSl);
  601. Enum.TryParse(_rSr.Label, out ConfigGamepadInputId rButtonSr);
  602. int.TryParse(_dsuServerPort.Buffer.Text, out int port);
  603. MotionConfigController motionConfig;
  604. if (_enableCemuHook.Active)
  605. {
  606. motionConfig = new CemuHookMotionConfigController
  607. {
  608. MotionBackend = MotionInputBackendType.CemuHook,
  609. EnableMotion = _enableMotion.Active,
  610. Sensitivity = (int)_sensitivity.Value,
  611. GyroDeadzone = _gyroDeadzone.Value,
  612. MirrorInput = _mirrorInput.Active,
  613. Slot = (int)_slotNumber.Value,
  614. AltSlot = (int)_altSlotNumber.Value,
  615. DsuServerHost = _dsuServerHost.Buffer.Text,
  616. DsuServerPort = port
  617. };
  618. }
  619. else
  620. {
  621. motionConfig = new StandardMotionConfigController
  622. {
  623. MotionBackend = MotionInputBackendType.GamepadDriver,
  624. EnableMotion = _enableMotion.Active,
  625. Sensitivity = (int)_sensitivity.Value,
  626. GyroDeadzone = _gyroDeadzone.Value,
  627. };
  628. }
  629. return new StandardControllerInputConfig
  630. {
  631. Backend = InputBackendType.GamepadSDL2,
  632. Version = InputConfig.CurrentVersion,
  633. Id = _inputDevice.ActiveId.Split("/")[1].Split(" ")[0],
  634. ControllerType = Enum.Parse<ControllerType>(_controllerType.ActiveId),
  635. PlayerIndex = _playerIndex,
  636. DeadzoneLeft = (float)_controllerDeadzoneLeft.Value,
  637. DeadzoneRight = (float)_controllerDeadzoneRight.Value,
  638. RangeLeft = (float)_controllerRangeLeft.Value,
  639. RangeRight = (float)_controllerRangeRight.Value,
  640. TriggerThreshold = (float)_controllerTriggerThreshold.Value,
  641. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  642. {
  643. ButtonMinus = lButtonMinus,
  644. ButtonL = lButtonL,
  645. ButtonZl = lButtonZl,
  646. ButtonSl = lButtonSl,
  647. ButtonSr = lButtonSr,
  648. DpadUp = lDPadUp,
  649. DpadDown = lDPadDown,
  650. DpadLeft = lDPadLeft,
  651. DpadRight = lDPadRight
  652. },
  653. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  654. {
  655. InvertStickX = _invertLStickX.Active,
  656. Joystick = lStick,
  657. InvertStickY = _invertLStickY.Active,
  658. StickButton = lStickButton,
  659. Rotate90CW = _rotateL90CW.Active,
  660. },
  661. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  662. {
  663. ButtonA = rButtonA,
  664. ButtonB = rButtonB,
  665. ButtonX = rButtonX,
  666. ButtonY = rButtonY,
  667. ButtonPlus = rButtonPlus,
  668. ButtonR = rButtonR,
  669. ButtonZr = rButtonZr,
  670. ButtonSl = rButtonSl,
  671. ButtonSr = rButtonSr
  672. },
  673. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  674. {
  675. InvertStickX = _invertRStickX.Active,
  676. Joystick = rStick,
  677. InvertStickY = _invertRStickY.Active,
  678. StickButton = rStickButton,
  679. Rotate90CW = _rotateR90CW.Active,
  680. },
  681. Motion = motionConfig,
  682. Rumble = new RumbleConfigController
  683. {
  684. StrongRumble = (float)_controllerStrongRumble.Value,
  685. WeakRumble = (float)_controllerWeakRumble.Value,
  686. EnableRumble = _enableRumble.Active
  687. }
  688. };
  689. }
  690. if (!_inputDevice.ActiveId.StartsWith("disabled"))
  691. {
  692. GtkDialog.CreateErrorDialog("Invalid data detected in one or more fields; the configuration was not saved.");
  693. }
  694. return null;
  695. }
  696. private string GetProfileBasePath()
  697. {
  698. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  699. {
  700. return System.IO.Path.Combine(AppDataManager.ProfilesDirPath, "keyboard");
  701. }
  702. else if (_inputDevice.ActiveId.StartsWith("controller"))
  703. {
  704. return System.IO.Path.Combine(AppDataManager.ProfilesDirPath, "controller");
  705. }
  706. return AppDataManager.ProfilesDirPath;
  707. }
  708. //
  709. // Events
  710. //
  711. private void InputDevice_Changed(object sender, EventArgs args)
  712. {
  713. SetAvailableOptions();
  714. SetControllerSpecificFields();
  715. _selectedGamepad?.Dispose();
  716. _selectedGamepad = null;
  717. if (_inputDevice.ActiveId != null)
  718. {
  719. SetProfiles();
  720. string id = GetCurrentGamepadId();
  721. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  722. {
  723. if (_inputConfig is StandardKeyboardInputConfig)
  724. {
  725. SetValues(_inputConfig);
  726. }
  727. if (_mainWindow.InputManager.KeyboardDriver is GTK3KeyboardDriver)
  728. {
  729. // 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...
  730. _selectedGamepad = _gtk3KeyboardDriver.GetGamepad(id);
  731. }
  732. else
  733. {
  734. _selectedGamepad = _mainWindow.InputManager.KeyboardDriver.GetGamepad(id);
  735. }
  736. }
  737. else if (_inputDevice.ActiveId.StartsWith("controller"))
  738. {
  739. if (_inputConfig is StandardControllerInputConfig)
  740. {
  741. SetValues(_inputConfig);
  742. }
  743. _selectedGamepad = _mainWindow.InputManager.GamepadDriver.GetGamepad(id);
  744. }
  745. }
  746. }
  747. private string GetCurrentGamepadId()
  748. {
  749. if (_inputDevice.ActiveId == null || _inputDevice.ActiveId == "disabled")
  750. {
  751. return null;
  752. }
  753. return _inputDevice.ActiveId.Split("/")[1].Split(" ")[0];
  754. }
  755. private void Controller_Changed(object sender, EventArgs args)
  756. {
  757. SetControllerSpecificFields();
  758. }
  759. private IButtonAssigner CreateButtonAssigner(bool forStick)
  760. {
  761. IButtonAssigner assigner;
  762. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  763. {
  764. assigner = new KeyboardKeyAssigner((IKeyboard)_selectedGamepad);
  765. }
  766. else if (_inputDevice.ActiveId.StartsWith("controller"))
  767. {
  768. assigner = new GamepadButtonAssigner(_selectedGamepad, (float)_controllerTriggerThreshold.Value, forStick);
  769. }
  770. else
  771. {
  772. throw new Exception("Controller not supported");
  773. }
  774. return assigner;
  775. }
  776. private void HandleButtonPressed(ToggleButton button, bool forStick)
  777. {
  778. if (_isWaitingForInput)
  779. {
  780. button.Active = false;
  781. return;
  782. }
  783. _mousePressed = false;
  784. ButtonPressEvent += MouseClick;
  785. IButtonAssigner assigner = CreateButtonAssigner(forStick);
  786. _isWaitingForInput = true;
  787. // Open GTK3 keyboard for cancel operations
  788. IKeyboard keyboard = (IKeyboard)_gtk3KeyboardDriver.GetGamepad("0");
  789. Thread inputThread = new Thread(() =>
  790. {
  791. assigner.Initialize();
  792. while (true)
  793. {
  794. Thread.Sleep(10);
  795. assigner.ReadInput();
  796. if (_mousePressed || keyboard.IsPressed(Ryujinx.Input.Key.Escape) || assigner.HasAnyButtonPressed() || assigner.ShouldCancel())
  797. {
  798. break;
  799. }
  800. }
  801. string pressedButton = assigner.GetPressedButton();
  802. Application.Invoke(delegate
  803. {
  804. if (_middleMousePressed)
  805. {
  806. button.Label = "Unbound";
  807. }
  808. else if (pressedButton != "")
  809. {
  810. button.Label = pressedButton;
  811. }
  812. _middleMousePressed = false;
  813. ButtonPressEvent -= MouseClick;
  814. keyboard.Dispose();
  815. button.Active = false;
  816. _isWaitingForInput = false;
  817. });
  818. });
  819. inputThread.Name = "GUI.InputThread";
  820. inputThread.IsBackground = true;
  821. inputThread.Start();
  822. }
  823. private void Button_Pressed(object sender, EventArgs args)
  824. {
  825. HandleButtonPressed((ToggleButton)sender, false);
  826. }
  827. private void ButtonForStick_Pressed(object sender, EventArgs args)
  828. {
  829. HandleButtonPressed((ToggleButton)sender, true);
  830. }
  831. private void MouseClick(object sender, ButtonPressEventArgs args)
  832. {
  833. _mousePressed = true;
  834. _middleMousePressed = args.Event.Button == 2;
  835. }
  836. private void SetProfiles()
  837. {
  838. _profile.RemoveAll();
  839. string basePath = GetProfileBasePath();
  840. if (!Directory.Exists(basePath))
  841. {
  842. Directory.CreateDirectory(basePath);
  843. }
  844. if (_inputDevice.ActiveId == null|| _inputDevice.ActiveId.Equals("disabled"))
  845. {
  846. _profile.Append("default", "None");
  847. }
  848. else
  849. {
  850. _profile.Append("default", "Default");
  851. foreach (string profile in Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories))
  852. {
  853. _profile.Append(System.IO.Path.GetFileName(profile), System.IO.Path.GetFileNameWithoutExtension(profile));
  854. }
  855. }
  856. _profile.SetActiveId("default");
  857. }
  858. private void ProfileLoad_Activated(object sender, EventArgs args)
  859. {
  860. ((ToggleButton)sender).SetStateFlags(StateFlags.Normal, true);
  861. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == null) return;
  862. InputConfig config = null;
  863. int pos = _profile.Active;
  864. if (_profile.ActiveId == "default")
  865. {
  866. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  867. {
  868. config = new StandardKeyboardInputConfig
  869. {
  870. Version = InputConfig.CurrentVersion,
  871. Backend = InputBackendType.WindowKeyboard,
  872. Id = null,
  873. ControllerType = ControllerType.ProController,
  874. LeftJoycon = new LeftJoyconCommonConfig<Key>
  875. {
  876. DpadUp = Key.Up,
  877. DpadDown = Key.Down,
  878. DpadLeft = Key.Left,
  879. DpadRight = Key.Right,
  880. ButtonMinus = Key.Minus,
  881. ButtonL = Key.E,
  882. ButtonZl = Key.Q,
  883. ButtonSl = Key.Unbound,
  884. ButtonSr = Key.Unbound
  885. },
  886. LeftJoyconStick = new JoyconConfigKeyboardStick<Key>
  887. {
  888. StickUp = Key.W,
  889. StickDown = Key.S,
  890. StickLeft = Key.A,
  891. StickRight = Key.D,
  892. StickButton = Key.F,
  893. },
  894. RightJoycon = new RightJoyconCommonConfig<Key>
  895. {
  896. ButtonA = Key.Z,
  897. ButtonB = Key.X,
  898. ButtonX = Key.C,
  899. ButtonY = Key.V,
  900. ButtonPlus = Key.Plus,
  901. ButtonR = Key.U,
  902. ButtonZr = Key.O,
  903. ButtonSl = Key.Unbound,
  904. ButtonSr = Key.Unbound
  905. },
  906. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  907. {
  908. StickUp = Key.I,
  909. StickDown = Key.K,
  910. StickLeft = Key.J,
  911. StickRight = Key.L,
  912. StickButton = Key.H,
  913. }
  914. };
  915. }
  916. else if (_inputDevice.ActiveId.StartsWith("controller"))
  917. {
  918. bool isNintendoStyle = _inputDevice.ActiveText.Contains("Nintendo");
  919. config = new StandardControllerInputConfig
  920. {
  921. Version = InputConfig.CurrentVersion,
  922. Backend = InputBackendType.GamepadSDL2,
  923. Id = null,
  924. ControllerType = ControllerType.JoyconPair,
  925. DeadzoneLeft = 0.1f,
  926. DeadzoneRight = 0.1f,
  927. RangeLeft = 1.0f,
  928. RangeRight = 1.0f,
  929. TriggerThreshold = 0.5f,
  930. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  931. {
  932. DpadUp = ConfigGamepadInputId.DpadUp,
  933. DpadDown = ConfigGamepadInputId.DpadDown,
  934. DpadLeft = ConfigGamepadInputId.DpadLeft,
  935. DpadRight = ConfigGamepadInputId.DpadRight,
  936. ButtonMinus = ConfigGamepadInputId.Minus,
  937. ButtonL = ConfigGamepadInputId.LeftShoulder,
  938. ButtonZl = ConfigGamepadInputId.LeftTrigger,
  939. ButtonSl = ConfigGamepadInputId.Unbound,
  940. ButtonSr = ConfigGamepadInputId.Unbound,
  941. },
  942. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  943. {
  944. Joystick = ConfigStickInputId.Left,
  945. StickButton = ConfigGamepadInputId.LeftStick,
  946. InvertStickX = false,
  947. InvertStickY = false,
  948. Rotate90CW = false,
  949. },
  950. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  951. {
  952. ButtonA = isNintendoStyle ? ConfigGamepadInputId.A : ConfigGamepadInputId.B,
  953. ButtonB = isNintendoStyle ? ConfigGamepadInputId.B : ConfigGamepadInputId.A,
  954. ButtonX = isNintendoStyle ? ConfigGamepadInputId.X : ConfigGamepadInputId.Y,
  955. ButtonY = isNintendoStyle ? ConfigGamepadInputId.Y : ConfigGamepadInputId.X,
  956. ButtonPlus = ConfigGamepadInputId.Plus,
  957. ButtonR = ConfigGamepadInputId.RightShoulder,
  958. ButtonZr = ConfigGamepadInputId.RightTrigger,
  959. ButtonSl = ConfigGamepadInputId.Unbound,
  960. ButtonSr = ConfigGamepadInputId.Unbound,
  961. },
  962. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  963. {
  964. Joystick = ConfigStickInputId.Right,
  965. StickButton = ConfigGamepadInputId.RightStick,
  966. InvertStickX = false,
  967. InvertStickY = false,
  968. Rotate90CW = false,
  969. },
  970. Motion = new StandardMotionConfigController
  971. {
  972. MotionBackend = MotionInputBackendType.GamepadDriver,
  973. EnableMotion = true,
  974. Sensitivity = 100,
  975. GyroDeadzone = 1,
  976. },
  977. Rumble = new RumbleConfigController
  978. {
  979. StrongRumble = 1f,
  980. WeakRumble = 1f,
  981. EnableRumble = false
  982. }
  983. };
  984. }
  985. }
  986. else
  987. {
  988. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  989. if (!File.Exists(path))
  990. {
  991. if (pos >= 0)
  992. {
  993. _profile.Remove(pos);
  994. }
  995. return;
  996. }
  997. try
  998. {
  999. using (Stream stream = File.OpenRead(path))
  1000. {
  1001. config = JsonHelper.Deserialize<InputConfig>(stream);
  1002. }
  1003. }
  1004. catch (JsonException) { }
  1005. }
  1006. SetValues(config);
  1007. }
  1008. private void ProfileAdd_Activated(object sender, EventArgs args)
  1009. {
  1010. ((ToggleButton)sender).SetStateFlags(StateFlags.Normal, true);
  1011. if (_inputDevice.ActiveId == "disabled") return;
  1012. InputConfig inputConfig = GetValues();
  1013. ProfileDialog profileDialog = new ProfileDialog();
  1014. if (inputConfig == null) return;
  1015. if (profileDialog.Run() == (int)ResponseType.Ok)
  1016. {
  1017. string path = System.IO.Path.Combine(GetProfileBasePath(), profileDialog.FileName);
  1018. string jsonString;
  1019. jsonString = JsonHelper.Serialize(inputConfig, true);
  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. }