NpadManager.cs 10 KB

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