ControllerWindow.cs 51 KB

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