NpadManager.cs 10 KB

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