InputTestRuntime.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using System;
  2. using System.Collections.Generic;
  3. using NUnit.Framework;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using Unity.Collections;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using UnityEngine.InputSystem.Layouts;
  8. using UnityEngine.InputSystem.Utilities;
  9. #if UNITY_EDITOR
  10. using UnityEditor;
  11. #endif
  12. namespace UnityEngine.InputSystem
  13. {
  14. /// <summary>
  15. /// An implementation of <see cref="IInputRuntime"/> for use during tests.
  16. /// </summary>
  17. /// <remarks>
  18. /// This class is only available in the editor and in development players.
  19. ///
  20. /// The test runtime replaces the services usually supplied by <see cref="UnityEngineInternal.Input.NativeInputSystem"/>.
  21. /// </remarks>
  22. /// <seealso cref="InputTestFixture.runtime"/>
  23. internal class InputTestRuntime : IInputRuntime, IDisposable
  24. {
  25. public unsafe delegate long DeviceCommandCallback(int deviceId, InputDeviceCommand* command);
  26. public bool hasFocus => m_HasFocus;
  27. ~InputTestRuntime()
  28. {
  29. Dispose();
  30. }
  31. public int AllocateDeviceId()
  32. {
  33. var result = m_NextDeviceId;
  34. ++m_NextDeviceId;
  35. return result;
  36. }
  37. public unsafe void Update(InputUpdateType type)
  38. {
  39. if (!onShouldRunUpdate.Invoke(type))
  40. return;
  41. lock (m_Lock)
  42. {
  43. if (m_NewDeviceDiscoveries != null && m_NewDeviceDiscoveries.Count > 0)
  44. {
  45. if (onDeviceDiscovered != null)
  46. foreach (var entry in m_NewDeviceDiscoveries)
  47. onDeviceDiscovered(entry.Key, entry.Value);
  48. m_NewDeviceDiscoveries.Clear();
  49. }
  50. onBeforeUpdate?.Invoke(type);
  51. // Advance time *after* onBeforeUpdate so that events generated from onBeforeUpdate
  52. // don't get bumped into the following update.
  53. if (type == InputUpdateType.Dynamic && !dontAdvanceTimeNextDynamicUpdate)
  54. currentTime += advanceTimeEachDynamicUpdate;
  55. if (onUpdate != null)
  56. {
  57. var buffer = new InputEventBuffer(
  58. (InputEvent*)NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_EventBuffer),
  59. m_EventCount, m_EventWritePosition, m_EventBuffer.Length);
  60. onUpdate(type, ref buffer);
  61. m_EventCount = buffer.eventCount;
  62. m_EventWritePosition = (int)buffer.sizeInBytes;
  63. if (NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(buffer.data) !=
  64. NativeArrayUnsafeUtility.GetUnsafeBufferPointerWithoutChecks(m_EventBuffer))
  65. m_EventBuffer = buffer.data;
  66. }
  67. else
  68. {
  69. m_EventCount = 0;
  70. m_EventWritePosition = 0;
  71. }
  72. dontAdvanceTimeNextDynamicUpdate = false;
  73. }
  74. }
  75. public unsafe void QueueEvent(InputEvent* eventPtr)
  76. {
  77. var eventSize = eventPtr->sizeInBytes;
  78. var alignedEventSize = eventSize.AlignToMultipleOf(4);
  79. lock (m_Lock)
  80. {
  81. eventPtr->eventId = m_NextEventId;
  82. eventPtr->handled = false;
  83. ++m_NextEventId;
  84. // Enlarge buffer, if we have to.
  85. if ((m_EventWritePosition + alignedEventSize) > m_EventBuffer.Length)
  86. {
  87. var newBufferSize = m_EventBuffer.Length + Mathf.Max((int)alignedEventSize, 1024);
  88. var newBuffer = new NativeArray<byte>(newBufferSize, Allocator.Persistent);
  89. UnsafeUtility.MemCpy(newBuffer.GetUnsafePtr(), m_EventBuffer.GetUnsafePtr(), m_EventWritePosition);
  90. m_EventBuffer.Dispose();
  91. m_EventBuffer = newBuffer;
  92. }
  93. // Copy event.
  94. UnsafeUtility.MemCpy((byte*)m_EventBuffer.GetUnsafePtr() + m_EventWritePosition, eventPtr, eventSize);
  95. m_EventWritePosition += (int)alignedEventSize;
  96. ++m_EventCount;
  97. }
  98. }
  99. public void SetDeviceCommandCallback(InputDevice device, DeviceCommandCallback callback)
  100. {
  101. SetDeviceCommandCallback(device.deviceId, callback);
  102. }
  103. public void SetDeviceCommandCallback(int deviceId, DeviceCommandCallback callback)
  104. {
  105. lock (m_Lock)
  106. {
  107. if (m_DeviceCommandCallbacks == null)
  108. m_DeviceCommandCallbacks = new List<KeyValuePair<int, DeviceCommandCallback>>();
  109. else
  110. {
  111. for (var i = 0; i < m_DeviceCommandCallbacks.Count; ++i)
  112. {
  113. if (m_DeviceCommandCallbacks[i].Key == deviceId)
  114. {
  115. m_DeviceCommandCallbacks[i] = new KeyValuePair<int, DeviceCommandCallback>(deviceId, callback);
  116. return;
  117. }
  118. }
  119. }
  120. m_DeviceCommandCallbacks.Add(new KeyValuePair<int, DeviceCommandCallback>(deviceId, callback));
  121. }
  122. }
  123. public void SetDeviceCommandCallback<TCommand>(int deviceId, TCommand result)
  124. where TCommand : struct, IInputDeviceCommandInfo
  125. {
  126. bool? receivedCommand = null;
  127. unsafe
  128. {
  129. SetDeviceCommandCallback(deviceId,
  130. (id, commandPtr) =>
  131. {
  132. if (commandPtr->type == result.typeStatic)
  133. {
  134. Assert.That(receivedCommand.HasValue, Is.False);
  135. receivedCommand = true;
  136. UnsafeUtility.MemCpy(commandPtr, UnsafeUtility.AddressOf(ref result),
  137. UnsafeUtility.SizeOf<TCommand>());
  138. return InputDeviceCommand.GenericSuccess;
  139. }
  140. return InputDeviceCommand.GenericFailure;
  141. });
  142. }
  143. }
  144. public unsafe long DeviceCommand(int deviceId, InputDeviceCommand* commandPtr)
  145. {
  146. lock (m_Lock)
  147. {
  148. if (commandPtr->type == QueryPairedUserAccountCommand.Type)
  149. {
  150. foreach (var pairing in userAccountPairings)
  151. {
  152. if (pairing.deviceId != deviceId)
  153. continue;
  154. var queryPairedUser = (QueryPairedUserAccountCommand*)commandPtr;
  155. queryPairedUser->handle = pairing.userHandle;
  156. queryPairedUser->name = pairing.userName;
  157. queryPairedUser->id = pairing.userId;
  158. return (long)QueryPairedUserAccountCommand.Result.DevicePairedToUserAccount;
  159. }
  160. }
  161. var result = InputDeviceCommand.GenericFailure;
  162. if (m_DeviceCommandCallbacks != null)
  163. foreach (var entry in m_DeviceCommandCallbacks)
  164. {
  165. if (entry.Key == deviceId)
  166. {
  167. result = entry.Value(deviceId, commandPtr);
  168. if (result >= 0)
  169. return result;
  170. }
  171. }
  172. return result;
  173. }
  174. }
  175. public void InvokePlayerFocusChanged(bool newFocusState)
  176. {
  177. m_HasFocus = newFocusState;
  178. onPlayerFocusChanged?.Invoke(newFocusState);
  179. }
  180. public void PlayerFocusLost()
  181. {
  182. InvokePlayerFocusChanged(false);
  183. }
  184. public void PlayerFocusGained()
  185. {
  186. InvokePlayerFocusChanged(true);
  187. }
  188. public int ReportNewInputDevice(string deviceDescriptor, int deviceId = InputDevice.InvalidDeviceId)
  189. {
  190. lock (m_Lock)
  191. {
  192. if (deviceId == InputDevice.InvalidDeviceId)
  193. deviceId = AllocateDeviceId();
  194. if (m_NewDeviceDiscoveries == null)
  195. m_NewDeviceDiscoveries = new List<KeyValuePair<int, string>>();
  196. m_NewDeviceDiscoveries.Add(new KeyValuePair<int, string>(deviceId, deviceDescriptor));
  197. return deviceId;
  198. }
  199. }
  200. public int ReportNewInputDevice(InputDeviceDescription description, int deviceId = InputDevice.InvalidDeviceId,
  201. ulong userHandle = 0, string userName = null, string userId = null)
  202. {
  203. deviceId = ReportNewInputDevice(description.ToJson(), deviceId);
  204. // If we have user information, automatically set up
  205. if (userHandle != 0)
  206. AssociateInputDeviceWithUser(deviceId, userHandle, userName, userId);
  207. return deviceId;
  208. }
  209. public int ReportNewInputDevice<TDevice>(int deviceId = InputDevice.InvalidDeviceId,
  210. ulong userHandle = 0, string userName = null, string userId = null)
  211. where TDevice : InputDevice
  212. {
  213. return ReportNewInputDevice(
  214. new InputDeviceDescription {deviceClass = typeof(TDevice).Name, interfaceName = "Test"}, deviceId,
  215. userHandle, userName, userId);
  216. }
  217. public unsafe void ReportInputDeviceRemoved(int deviceId)
  218. {
  219. var removeEvent = DeviceRemoveEvent.Create(deviceId);
  220. var removeEventPtr = UnsafeUtility.AddressOf(ref removeEvent);
  221. QueueEvent((InputEvent*)removeEventPtr);
  222. }
  223. public void ReportInputDeviceRemoved(InputDevice device)
  224. {
  225. if (device == null)
  226. throw new ArgumentNullException(nameof(device));
  227. ReportInputDeviceRemoved(device.deviceId);
  228. }
  229. public void AssociateInputDeviceWithUser(int deviceId, ulong userHandle, string userName = null, string userId = null)
  230. {
  231. var existingIndex = -1;
  232. for (var i = 0; i < userAccountPairings.Count; ++i)
  233. if (userAccountPairings[i].deviceId == deviceId)
  234. {
  235. existingIndex = i;
  236. break;
  237. }
  238. if (userHandle == 0)
  239. {
  240. if (existingIndex != -1)
  241. userAccountPairings.RemoveAt(existingIndex);
  242. }
  243. else if (existingIndex != -1)
  244. {
  245. userAccountPairings[existingIndex] =
  246. new PairedUser
  247. {
  248. deviceId = deviceId,
  249. userHandle = userHandle,
  250. userName = userName,
  251. userId = userId,
  252. };
  253. }
  254. else
  255. {
  256. userAccountPairings.Add(
  257. new PairedUser
  258. {
  259. deviceId = deviceId,
  260. userHandle = userHandle,
  261. userName = userName,
  262. userId = userId,
  263. });
  264. }
  265. }
  266. public void AssociateInputDeviceWithUser(InputDevice device, ulong userHandle, string userName = null, string userId = null)
  267. {
  268. AssociateInputDeviceWithUser(device.deviceId, userHandle, userName, userId);
  269. }
  270. public struct PairedUser
  271. {
  272. public int deviceId;
  273. public ulong userHandle;
  274. public string userName;
  275. public string userId;
  276. }
  277. public InputUpdateDelegate onUpdate { get; set; }
  278. public Action<InputUpdateType> onBeforeUpdate { get; set; }
  279. public Func<InputUpdateType, bool> onShouldRunUpdate { get; set; }
  280. public Action<int, string> onDeviceDiscovered { get; set; }
  281. public Action onShutdown { get; set; }
  282. public Action<bool> onPlayerFocusChanged { get; set; }
  283. public float pollingFrequency { get; set; }
  284. public double currentTime { get; set; }
  285. public double currentTimeForFixedUpdate { get; set; }
  286. public float unscaledGameTime { get; set; } = 1;
  287. public double advanceTimeEachDynamicUpdate { get; set; } = 1.0 / 60;
  288. public bool dontAdvanceTimeNextDynamicUpdate { get; set; }
  289. public bool runInBackground { get; set; } = false;
  290. public ScreenOrientation screenOrientation { set; get; } = ScreenOrientation.Portrait;
  291. public List<PairedUser> userAccountPairings
  292. {
  293. get
  294. {
  295. if (m_UserPairings == null)
  296. m_UserPairings = new List<PairedUser>();
  297. return m_UserPairings;
  298. }
  299. }
  300. public void Dispose()
  301. {
  302. m_EventBuffer.Dispose();
  303. GC.SuppressFinalize(this);
  304. }
  305. public double currentTimeOffsetToRealtimeSinceStartup
  306. {
  307. get => m_CurrentTimeOffsetToRealtimeSinceStartup;
  308. set
  309. {
  310. m_CurrentTimeOffsetToRealtimeSinceStartup = value;
  311. InputRuntime.s_CurrentTimeOffsetToRealtimeSinceStartup = value;
  312. }
  313. }
  314. public bool isInBatchMode { get; set; }
  315. #if UNITY_EDITOR
  316. public bool isInPlayMode { get; set; } = true;
  317. public bool isPaused { get; set; }
  318. public Action<PlayModeStateChange> onPlayModeChanged { get; set; }
  319. public Action onProjectChange { get; set; }
  320. #endif
  321. public int eventCount => m_EventCount;
  322. private bool m_HasFocus = true;
  323. private int m_NextDeviceId = 1;
  324. private int m_NextEventId = 1;
  325. internal int m_EventCount;
  326. private int m_EventWritePosition;
  327. private NativeArray<byte> m_EventBuffer = new NativeArray<byte>(1024 * 1024, Allocator.Persistent);
  328. private List<PairedUser> m_UserPairings;
  329. private List<KeyValuePair<int, string>> m_NewDeviceDiscoveries;
  330. private List<KeyValuePair<int, DeviceCommandCallback>> m_DeviceCommandCallbacks;
  331. private object m_Lock = new object();
  332. private double m_CurrentTimeOffsetToRealtimeSinceStartup;
  333. #if UNITY_ANALYTICS || UNITY_EDITOR
  334. public Action<string, int, int> onRegisterAnalyticsEvent { get; set; }
  335. public Action<string, object> onSendAnalyticsEvent { get; set; }
  336. public void RegisterAnalyticsEvent(string name, int maxPerHour, int maxPropertiesPerEvent)
  337. {
  338. onRegisterAnalyticsEvent?.Invoke(name, maxPerHour, maxPropertiesPerEvent);
  339. }
  340. public void SendAnalyticsEvent(string name, object data)
  341. {
  342. onSendAnalyticsEvent?.Invoke(name, data);
  343. }
  344. #endif // UNITY_ANALYTICS || UNITY_EDITOR
  345. }
  346. }