NpadManager.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using Ryujinx.Common.Configuration.Hid;
  2. using Ryujinx.Common.Configuration.Hid.Controller;
  3. using Ryujinx.Common.Configuration.Hid.Keyboard;
  4. using Ryujinx.HLE.HOS.Services.Hid;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.Linq;
  9. using System.Runtime.CompilerServices;
  10. using CemuHookClient = Ryujinx.Input.Motion.CemuHook.Client;
  11. using ControllerType = Ryujinx.Common.Configuration.Hid.ControllerType;
  12. using PlayerIndex = Ryujinx.HLE.HOS.Services.Hid.PlayerIndex;
  13. using Switch = Ryujinx.HLE.Switch;
  14. namespace Ryujinx.Input.HLE
  15. {
  16. public class NpadManager : IDisposable
  17. {
  18. private readonly CemuHookClient _cemuHookClient;
  19. private readonly object _lock = new();
  20. private bool _blockInputUpdates;
  21. private const int MaxControllers = 9;
  22. private readonly NpadController[] _controllers;
  23. private readonly IGamepadDriver _keyboardDriver;
  24. private readonly IGamepadDriver _gamepadDriver;
  25. private readonly IGamepadDriver _mouseDriver;
  26. private bool _isDisposed;
  27. private List<InputConfig> _inputConfig;
  28. private bool _enableKeyboard;
  29. private bool _enableMouse;
  30. private Switch _device;
  31. public NpadManager(IGamepadDriver keyboardDriver, IGamepadDriver gamepadDriver, IGamepadDriver mouseDriver)
  32. {
  33. _controllers = new NpadController[MaxControllers];
  34. _cemuHookClient = new CemuHookClient(this);
  35. _keyboardDriver = keyboardDriver;
  36. _gamepadDriver = gamepadDriver;
  37. _mouseDriver = mouseDriver;
  38. _inputConfig = new List<InputConfig>();
  39. _gamepadDriver.OnGamepadConnected += HandleOnGamepadConnected;
  40. _gamepadDriver.OnGamepadDisconnected += HandleOnGamepadDisconnected;
  41. }
  42. private void RefreshInputConfigForHLE()
  43. {
  44. lock (_lock)
  45. {
  46. List<InputConfig> validInputs = new();
  47. foreach (var inputConfigEntry in _inputConfig)
  48. {
  49. if (_controllers[(int)inputConfigEntry.PlayerIndex] != null)
  50. {
  51. validInputs.Add(inputConfigEntry);
  52. }
  53. }
  54. _device.Hid.RefreshInputConfig(validInputs);
  55. }
  56. }
  57. private void HandleOnGamepadDisconnected(string obj)
  58. {
  59. // Force input reload
  60. lock (_lock)
  61. {
  62. // Forcibly disconnect any controllers with this ID.
  63. for (int i = 0; i < _controllers.Length; i++)
  64. {
  65. if (_controllers[i]?.Id == obj)
  66. {
  67. _controllers[i]?.Dispose();
  68. _controllers[i] = null;
  69. }
  70. }
  71. ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse);
  72. }
  73. }
  74. private void HandleOnGamepadConnected(string id)
  75. {
  76. // Force input reload
  77. ReloadConfiguration(_inputConfig, _enableKeyboard, _enableMouse);
  78. }
  79. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  80. private bool DriverConfigurationUpdate(ref NpadController controller, InputConfig config)
  81. {
  82. IGamepadDriver targetDriver = _gamepadDriver;
  83. if (config is StandardControllerInputConfig)
  84. {
  85. targetDriver = _gamepadDriver;
  86. }
  87. else if (config is StandardKeyboardInputConfig)
  88. {
  89. targetDriver = _keyboardDriver;
  90. }
  91. Debug.Assert(targetDriver != null, "Unknown input configuration!");
  92. if (controller.GamepadDriver != targetDriver || controller.Id != config.Id)
  93. {
  94. return controller.UpdateDriverConfiguration(targetDriver, config);
  95. }
  96. return controller.GamepadDriver != null;
  97. }
  98. public void ReloadConfiguration(List<InputConfig> inputConfig, bool enableKeyboard, bool enableMouse)
  99. {
  100. lock (_lock)
  101. {
  102. NpadController[] oldControllers = _controllers.ToArray();
  103. List<InputConfig> validInputs = new();
  104. foreach (InputConfig inputConfigEntry in inputConfig)
  105. {
  106. NpadController controller;
  107. int index = (int)inputConfigEntry.PlayerIndex;
  108. if (oldControllers[index] != null)
  109. {
  110. // Try reuse the existing controller.
  111. controller = oldControllers[index];
  112. oldControllers[index] = null;
  113. }
  114. else
  115. {
  116. controller = new(_cemuHookClient);
  117. }
  118. bool isValid = DriverConfigurationUpdate(ref controller, inputConfigEntry);
  119. if (!isValid)
  120. {
  121. _controllers[index] = null;
  122. controller.Dispose();
  123. }
  124. else
  125. {
  126. _controllers[index] = controller;
  127. validInputs.Add(inputConfigEntry);
  128. }
  129. }
  130. for (int i = 0; i < oldControllers.Length; i++)
  131. {
  132. // Disconnect any controllers that weren't reused by the new configuration.
  133. oldControllers[i]?.Dispose();
  134. oldControllers[i] = null;
  135. }
  136. _inputConfig = inputConfig;
  137. _enableKeyboard = enableKeyboard;
  138. _enableMouse = enableMouse;
  139. _device.Hid.RefreshInputConfig(validInputs);
  140. }
  141. }
  142. public void UnblockInputUpdates()
  143. {
  144. lock (_lock)
  145. {
  146. foreach (InputConfig inputConfig in _inputConfig)
  147. {
  148. _controllers[(int)inputConfig.PlayerIndex]?.GamepadDriver?.Clear();
  149. }
  150. _blockInputUpdates = false;
  151. }
  152. }
  153. public void BlockInputUpdates()
  154. {
  155. lock (_lock)
  156. {
  157. _blockInputUpdates = true;
  158. }
  159. }
  160. public void Initialize(Switch device, List<InputConfig> inputConfig, bool enableKeyboard, bool enableMouse)
  161. {
  162. _device = device;
  163. _device.Configuration.RefreshInputConfig = RefreshInputConfigForHLE;
  164. ReloadConfiguration(inputConfig, enableKeyboard, enableMouse);
  165. }
  166. public void Update(float aspectRatio = 1)
  167. {
  168. lock (_lock)
  169. {
  170. List<GamepadInput> hleInputStates = new();
  171. List<SixAxisInput> hleMotionStates = new(NpadDevices.MaxControllers);
  172. KeyboardInput? hleKeyboardInput = null;
  173. foreach (InputConfig inputConfig in _inputConfig)
  174. {
  175. GamepadInput inputState = default;
  176. (SixAxisInput, SixAxisInput) motionState = default;
  177. NpadController controller = _controllers[(int)inputConfig.PlayerIndex];
  178. PlayerIndex playerIndex = (PlayerIndex)inputConfig.PlayerIndex;
  179. bool isJoyconPair = false;
  180. // Do we allow input updates and is a controller connected?
  181. if (!_blockInputUpdates && controller != null)
  182. {
  183. DriverConfigurationUpdate(ref controller, inputConfig);
  184. controller.UpdateUserConfiguration(inputConfig);
  185. controller.Update();
  186. controller.UpdateRumble(_device.Hid.Npads.GetRumbleQueue(playerIndex));
  187. inputState = controller.GetHLEInputState();
  188. inputState.Buttons |= _device.Hid.UpdateStickButtons(inputState.LStick, inputState.RStick);
  189. isJoyconPair = inputConfig.ControllerType == ControllerType.JoyconPair;
  190. var altMotionState = isJoyconPair ? controller.GetHLEMotionState(true) : default;
  191. motionState = (controller.GetHLEMotionState(), altMotionState);
  192. }
  193. else
  194. {
  195. // Ensure that orientation isn't null
  196. motionState.Item1.Orientation = new float[9];
  197. }
  198. inputState.PlayerId = playerIndex;
  199. motionState.Item1.PlayerId = playerIndex;
  200. hleInputStates.Add(inputState);
  201. hleMotionStates.Add(motionState.Item1);
  202. if (isJoyconPair && !motionState.Item2.Equals(default))
  203. {
  204. motionState.Item2.PlayerId = playerIndex;
  205. hleMotionStates.Add(motionState.Item2);
  206. }
  207. }
  208. if (!_blockInputUpdates && _enableKeyboard)
  209. {
  210. hleKeyboardInput = NpadController.GetHLEKeyboardInput(_keyboardDriver);
  211. }
  212. _device.Hid.Npads.Update(hleInputStates);
  213. _device.Hid.Npads.UpdateSixAxis(hleMotionStates);
  214. if (hleKeyboardInput.HasValue)
  215. {
  216. _device.Hid.Keyboard.Update(hleKeyboardInput.Value);
  217. }
  218. if (_enableMouse)
  219. {
  220. var mouse = _mouseDriver.GetGamepad("0") as IMouse;
  221. var mouseInput = IMouse.GetMouseStateSnapshot(mouse);
  222. uint buttons = 0;
  223. if (mouseInput.IsPressed(MouseButton.Button1))
  224. {
  225. buttons |= 1 << 0;
  226. }
  227. if (mouseInput.IsPressed(MouseButton.Button2))
  228. {
  229. buttons |= 1 << 1;
  230. }
  231. if (mouseInput.IsPressed(MouseButton.Button3))
  232. {
  233. buttons |= 1 << 2;
  234. }
  235. if (mouseInput.IsPressed(MouseButton.Button4))
  236. {
  237. buttons |= 1 << 3;
  238. }
  239. if (mouseInput.IsPressed(MouseButton.Button5))
  240. {
  241. buttons |= 1 << 4;
  242. }
  243. var position = IMouse.GetScreenPosition(mouseInput.Position, mouse.ClientSize, aspectRatio);
  244. _device.Hid.Mouse.Update((int)position.X, (int)position.Y, buttons, (int)mouseInput.Scroll.X, (int)mouseInput.Scroll.Y, true);
  245. }
  246. else
  247. {
  248. _device.Hid.Mouse.Update(0, 0);
  249. }
  250. _device.TamperMachine.UpdateInput(hleInputStates);
  251. }
  252. }
  253. internal InputConfig GetPlayerInputConfigByIndex(int index)
  254. {
  255. lock (_lock)
  256. {
  257. return _inputConfig.Find(x => x.PlayerIndex == (Common.Configuration.Hid.PlayerIndex)index);
  258. }
  259. }
  260. protected virtual void Dispose(bool disposing)
  261. {
  262. if (disposing)
  263. {
  264. lock (_lock)
  265. {
  266. if (!_isDisposed)
  267. {
  268. _cemuHookClient.Dispose();
  269. _gamepadDriver.OnGamepadConnected -= HandleOnGamepadConnected;
  270. _gamepadDriver.OnGamepadDisconnected -= HandleOnGamepadDisconnected;
  271. for (int i = 0; i < _controllers.Length; i++)
  272. {
  273. _controllers[i]?.Dispose();
  274. }
  275. _isDisposed = true;
  276. }
  277. }
  278. }
  279. }
  280. public void Dispose()
  281. {
  282. GC.SuppressFinalize(this);
  283. Dispose(true);
  284. }
  285. }
  286. }