ControllerWindow.cs 52 KB

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