NpadManager.cs 10 KB

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