ControllerWindow.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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. public ControllerWindow(MainWindow mainWindow, PlayerIndex controllerId) : this(mainWindow, new Builder("Ryujinx.Ui.Windows.ControllerWindow.glade"), controllerId) { }
  103. private ControllerWindow(MainWindow mainWindow, Builder builder, PlayerIndex controllerId) : base(builder.GetObject("_controllerWin").Handle)
  104. {
  105. _mainWindow = mainWindow;
  106. _mousePressed = false;
  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 (pressedButton != "")
  763. {
  764. button.Label = pressedButton;
  765. }
  766. ButtonPressEvent -= MouseClick;
  767. keyboard.Dispose();
  768. button.Active = false;
  769. _isWaitingForInput = false;
  770. });
  771. });
  772. inputThread.Name = "GUI.InputThread";
  773. inputThread.IsBackground = true;
  774. inputThread.Start();
  775. }
  776. private void Button_Pressed(object sender, EventArgs args)
  777. {
  778. HandleButtonPressed((ToggleButton)sender, false);
  779. }
  780. private void ButtonForStick_Pressed(object sender, EventArgs args)
  781. {
  782. HandleButtonPressed((ToggleButton)sender, true);
  783. }
  784. private void MouseClick(object sender, ButtonPressEventArgs args)
  785. {
  786. _mousePressed = true;
  787. }
  788. private void SetProfiles()
  789. {
  790. _profile.RemoveAll();
  791. string basePath = GetProfileBasePath();
  792. if (!Directory.Exists(basePath))
  793. {
  794. Directory.CreateDirectory(basePath);
  795. }
  796. if (_inputDevice.ActiveId == null|| _inputDevice.ActiveId.Equals("disabled"))
  797. {
  798. _profile.Append("default", "None");
  799. }
  800. else
  801. {
  802. _profile.Append("default", "Default");
  803. foreach (string profile in Directory.GetFiles(basePath, "*.*", SearchOption.AllDirectories))
  804. {
  805. _profile.Append(System.IO.Path.GetFileName(profile), System.IO.Path.GetFileNameWithoutExtension(profile));
  806. }
  807. }
  808. _profile.SetActiveId("default");
  809. }
  810. private void ProfileLoad_Activated(object sender, EventArgs args)
  811. {
  812. ((ToggleButton)sender).SetStateFlags(StateFlags.Normal, true);
  813. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == null) return;
  814. InputConfig config = null;
  815. int pos = _profile.Active;
  816. if (_profile.ActiveId == "default")
  817. {
  818. if (_inputDevice.ActiveId.StartsWith("keyboard"))
  819. {
  820. config = new StandardKeyboardInputConfig
  821. {
  822. Version = InputConfig.CurrentVersion,
  823. Backend = InputBackendType.WindowKeyboard,
  824. Id = null,
  825. ControllerType = ControllerType.JoyconPair,
  826. LeftJoycon = new LeftJoyconCommonConfig<Key>
  827. {
  828. DpadUp = Key.Up,
  829. DpadDown = Key.Down,
  830. DpadLeft = Key.Left,
  831. DpadRight = Key.Right,
  832. ButtonMinus = Key.Minus,
  833. ButtonL = Key.E,
  834. ButtonZl = Key.Q,
  835. ButtonSl = Key.Unbound,
  836. ButtonSr = Key.Unbound
  837. },
  838. LeftJoyconStick = new JoyconConfigKeyboardStick<Key>
  839. {
  840. StickUp = Key.W,
  841. StickDown = Key.S,
  842. StickLeft = Key.A,
  843. StickRight = Key.D,
  844. StickButton = Key.F,
  845. },
  846. RightJoycon = new RightJoyconCommonConfig<Key>
  847. {
  848. ButtonA = Key.Z,
  849. ButtonB = Key.X,
  850. ButtonX = Key.C,
  851. ButtonY = Key.V,
  852. ButtonPlus = Key.Plus,
  853. ButtonR = Key.U,
  854. ButtonZr = Key.O,
  855. ButtonSl = Key.Unbound,
  856. ButtonSr = Key.Unbound
  857. },
  858. RightJoyconStick = new JoyconConfigKeyboardStick<Key>
  859. {
  860. StickUp = Key.I,
  861. StickDown = Key.K,
  862. StickLeft = Key.J,
  863. StickRight = Key.L,
  864. StickButton = Key.H,
  865. }
  866. };
  867. }
  868. else if (_inputDevice.ActiveId.StartsWith("controller"))
  869. {
  870. bool isNintendoStyle = _inputDevice.ActiveText.Contains("Nintendo");
  871. config = new StandardControllerInputConfig
  872. {
  873. Version = InputConfig.CurrentVersion,
  874. Backend = InputBackendType.GamepadSDL2,
  875. Id = null,
  876. ControllerType = ControllerType.JoyconPair,
  877. DeadzoneLeft = 0.1f,
  878. DeadzoneRight = 0.1f,
  879. TriggerThreshold = 0.5f,
  880. LeftJoycon = new LeftJoyconCommonConfig<ConfigGamepadInputId>
  881. {
  882. DpadUp = ConfigGamepadInputId.DpadUp,
  883. DpadDown = ConfigGamepadInputId.DpadDown,
  884. DpadLeft = ConfigGamepadInputId.DpadLeft,
  885. DpadRight = ConfigGamepadInputId.DpadRight,
  886. ButtonMinus = ConfigGamepadInputId.Minus,
  887. ButtonL = ConfigGamepadInputId.LeftShoulder,
  888. ButtonZl = ConfigGamepadInputId.LeftTrigger,
  889. ButtonSl = ConfigGamepadInputId.Unbound,
  890. ButtonSr = ConfigGamepadInputId.Unbound,
  891. },
  892. LeftJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  893. {
  894. Joystick = ConfigStickInputId.Left,
  895. StickButton = ConfigGamepadInputId.LeftStick,
  896. InvertStickX = false,
  897. InvertStickY = false,
  898. },
  899. RightJoycon = new RightJoyconCommonConfig<ConfigGamepadInputId>
  900. {
  901. ButtonA = isNintendoStyle ? ConfigGamepadInputId.A : ConfigGamepadInputId.B,
  902. ButtonB = isNintendoStyle ? ConfigGamepadInputId.B : ConfigGamepadInputId.A,
  903. ButtonX = isNintendoStyle ? ConfigGamepadInputId.X : ConfigGamepadInputId.Y,
  904. ButtonY = isNintendoStyle ? ConfigGamepadInputId.Y : ConfigGamepadInputId.X,
  905. ButtonPlus = ConfigGamepadInputId.Plus,
  906. ButtonR = ConfigGamepadInputId.RightShoulder,
  907. ButtonZr = ConfigGamepadInputId.RightTrigger,
  908. ButtonSl = ConfigGamepadInputId.Unbound,
  909. ButtonSr = ConfigGamepadInputId.Unbound,
  910. },
  911. RightJoyconStick = new JoyconConfigControllerStick<ConfigGamepadInputId, ConfigStickInputId>
  912. {
  913. Joystick = ConfigStickInputId.Right,
  914. StickButton = ConfigGamepadInputId.RightStick,
  915. InvertStickX = false,
  916. InvertStickY = false,
  917. },
  918. Motion = new StandardMotionConfigController
  919. {
  920. MotionBackend = MotionInputBackendType.GamepadDriver,
  921. EnableMotion = true,
  922. Sensitivity = 100,
  923. GyroDeadzone = 1,
  924. }
  925. };
  926. }
  927. }
  928. else
  929. {
  930. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  931. if (!File.Exists(path))
  932. {
  933. if (pos >= 0)
  934. {
  935. _profile.Remove(pos);
  936. }
  937. return;
  938. }
  939. try
  940. {
  941. using (Stream stream = File.OpenRead(path))
  942. {
  943. config = JsonHelper.Deserialize<InputConfig>(stream);
  944. }
  945. }
  946. catch (JsonException) { }
  947. }
  948. SetValues(config);
  949. }
  950. private void ProfileAdd_Activated(object sender, EventArgs args)
  951. {
  952. ((ToggleButton)sender).SetStateFlags(StateFlags.Normal, true);
  953. if (_inputDevice.ActiveId == "disabled") return;
  954. InputConfig inputConfig = GetValues();
  955. ProfileDialog profileDialog = new ProfileDialog();
  956. if (inputConfig == null) return;
  957. if (profileDialog.Run() == (int)ResponseType.Ok)
  958. {
  959. string path = System.IO.Path.Combine(GetProfileBasePath(), profileDialog.FileName);
  960. string jsonString;
  961. jsonString = JsonHelper.Serialize(inputConfig, true);
  962. File.WriteAllText(path, jsonString);
  963. }
  964. profileDialog.Dispose();
  965. SetProfiles();
  966. }
  967. private void ProfileRemove_Activated(object sender, EventArgs args)
  968. {
  969. ((ToggleButton) sender).SetStateFlags(StateFlags.Normal, true);
  970. if (_inputDevice.ActiveId == "disabled" || _profile.ActiveId == "default" || _profile.ActiveId == null) return;
  971. MessageDialog confirmDialog = GtkDialog.CreateConfirmationDialog("Deleting Profile", "This action is irreversible, are your sure you want to continue?");
  972. if (confirmDialog.Run() == (int)ResponseType.Yes)
  973. {
  974. string path = System.IO.Path.Combine(GetProfileBasePath(), _profile.ActiveId);
  975. if (File.Exists(path))
  976. {
  977. File.Delete(path);
  978. }
  979. SetProfiles();
  980. }
  981. }
  982. private void SaveToggle_Activated(object sender, EventArgs args)
  983. {
  984. InputConfig inputConfig = GetValues();
  985. var newConfig = new List<InputConfig>();
  986. newConfig.AddRange(ConfigurationState.Instance.Hid.InputConfig.Value);
  987. if (_inputConfig == null && inputConfig != null)
  988. {
  989. newConfig.Add(inputConfig);
  990. }
  991. else
  992. {
  993. if (_inputDevice.ActiveId == "disabled")
  994. {
  995. newConfig.Remove(_inputConfig);
  996. }
  997. else if (inputConfig != null)
  998. {
  999. int index = newConfig.IndexOf(_inputConfig);
  1000. newConfig[index] = inputConfig;
  1001. }
  1002. }
  1003. if (_mainWindow.RendererWidget != null)
  1004. {
  1005. _mainWindow.RendererWidget.NpadManager.ReloadConfiguration(newConfig);
  1006. }
  1007. // Atomically replace and signal input change.
  1008. // NOTE: Do not modify InputConfig.Value directly as other code depends on the on-change event.
  1009. ConfigurationState.Instance.Hid.InputConfig.Value = newConfig;
  1010. ConfigurationState.Instance.ToFileFormat().SaveConfig(Program.ConfigurationPath);
  1011. Dispose();
  1012. }
  1013. private void CloseToggle_Activated(object sender, EventArgs args)
  1014. {
  1015. Dispose();
  1016. }
  1017. }
  1018. }