No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InputEvent.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Unity.Collections.LowLevel.Unsafe;
  4. using UnityEngine.InputSystem.Utilities;
  5. using UnityEngineInternal.Input;
  6. ////REVIEW: can we get rid of the timestamp offsetting in the player and leave that complication for the editor only?
  7. namespace UnityEngine.InputSystem.LowLevel
  8. {
  9. /// <summary>
  10. /// A chunk of memory signaling a data transfer in the input system.
  11. /// </summary>
  12. /// <remarks>
  13. /// Input events are raw memory buffers akin to a byte array. For most uses of the input
  14. /// system, it is not necessary to be aware of the event stream in the background. Events
  15. /// are written to the internal event buffer by producers -- usually by the platform-specific
  16. /// backends sitting in the Unity runtime. Once per fixed or dynamic update (depending on
  17. /// what <see cref="InputSettings.updateMode"/> is set to), the input system then goes and
  18. /// flushes out the internal event buffer to process pending events.
  19. ///
  20. /// Events may signal general device-related occurrences (such as <see cref="DeviceConfigurationEvent"/>
  21. /// or <see cref="DeviceRemoveEvent"/>) or they may signal input activity. The latter kind of
  22. /// event is called "state events". In particular, these events are either <see cref="StateEvent"/>,
  23. /// only.
  24. ///
  25. /// Events are solely focused on input. To effect output on an input device (e.g. haptics
  26. /// effects), "commands" (see <see cref="InputDeviceCommand"/>) are used.
  27. ///
  28. /// Event processing can be listened to using <see cref="InputSystem.onEvent"/>. This callback
  29. /// will get triggered for each event as it is processed by the input system.
  30. ///
  31. /// Note that there is no "routing" mechanism for events, i.e. no mechanism by which the input
  32. /// system looks for a handler for a specific event. Instead, events represent low-level activity
  33. /// that the input system directly integrates into the state of its <see cref="InputDevice"/>
  34. /// instances.
  35. ///
  36. /// Each type of event is distinguished by its own <see cref="FourCC"/> type tag. The tag can
  37. /// be queried from the <see cref="type"/> property.
  38. ///
  39. /// Each event will receive a unique ID when queued to the internal event buffer. The ID can
  40. /// be queried using the <see cref="eventId"/> property. Over the lifetime of the input system,
  41. /// no two events will receive the same ID. If you repeatedly queue an event from the same
  42. /// memory buffer, each individual call of <see cref="InputSystem.QueueEvent"/> will result in
  43. /// its own unique event ID.
  44. ///
  45. /// All events are device-specific meaning that <see cref="deviceId"/> will always reference
  46. /// some device (which, however, may or may not translate to an <see cref="InputDevice"/>; that
  47. /// part depends on whether the input system was able to create an <see cref="InputDevice"/>
  48. /// based on the information received from the backend).
  49. /// </remarks>
  50. /// <seealso cref="InputEventPtr"/>
  51. // NOTE: This has to be layout compatible with native events.
  52. [StructLayout(LayoutKind.Explicit, Size = kBaseEventSize, Pack = 1)]
  53. public struct InputEvent
  54. {
  55. private const uint kHandledMask = 0x80000000;
  56. private const uint kIdMask = 0x7FFFFFFF;
  57. internal const int kBaseEventSize = NativeInputEvent.structSize;
  58. /// <summary>
  59. /// Default, invalid value for <see cref="eventId"/>. Upon being queued with
  60. /// <see cref="InputSystem.QueueEvent"/>, no event will receive this ID.
  61. /// </summary>
  62. public const int InvalidEventId = 0;
  63. internal const int kAlignment = 4;
  64. [FieldOffset(0)]
  65. private NativeInputEvent m_Event;
  66. /// <summary>
  67. /// Type code for the event.
  68. /// </summary>
  69. /// <remarks>
  70. /// Each type of event has its own unique FourCC tag. For example, state events (see <see cref="StateEvent"/>)
  71. /// are tagged with "STAT". The type tag for a specific type of event can be queried from its <c>Type</c>
  72. /// property (for example, <see cref="StateEvent.Type"/>).
  73. ///
  74. /// To check whether an event has a specific type tag, you can use <see cref="InputEventPtr.IsA{T}"/>.
  75. /// </remarks>
  76. public FourCC type
  77. {
  78. get => new FourCC((int)m_Event.type);
  79. set => m_Event.type = (NativeInputEventType)(int)value;
  80. }
  81. /// <summary>
  82. /// Total size of the event in bytes.
  83. /// </summary>
  84. /// <value>Size of the event in bytes.</value>
  85. /// <remarks>
  86. /// Events are variable-size structs. This field denotes the total size of the event
  87. /// as stored in memory. This includes the full size of this struct and not just the
  88. /// "payload" of the event.
  89. ///
  90. /// <example>
  91. /// <code>
  92. /// // Store event in private buffer:
  93. /// unsafe byte[] CopyEventData(InputEventPtr eventPtr)
  94. /// {
  95. /// var sizeInBytes = eventPtr.sizeInBytes;
  96. /// var buffer = new byte[sizeInBytes];
  97. /// fixed (byte* bufferPtr = buffer)
  98. /// {
  99. /// UnsafeUtility.MemCpy(new IntPtr(bufferPtr), eventPtr.data, sizeInBytes);
  100. /// }
  101. /// return buffer;
  102. /// }
  103. /// </code>
  104. /// </example>
  105. ///
  106. /// The maximum supported size of events is <c>ushort.MaxValue</c>, i.e. events cannot
  107. /// be larger than 64KB.
  108. /// </remarks>
  109. /// <exception cref="ArgumentException"><paramref name="value"/> exceeds <c>ushort.MaxValue</c>.</exception>
  110. public uint sizeInBytes
  111. {
  112. get => m_Event.sizeInBytes;
  113. set
  114. {
  115. if (value > ushort.MaxValue)
  116. throw new ArgumentException("Maximum event size is " + ushort.MaxValue, nameof(value));
  117. m_Event.sizeInBytes = (ushort)value;
  118. }
  119. }
  120. /// <summary>
  121. /// Unique serial ID of the event.
  122. /// </summary>
  123. /// <remarks>
  124. /// Events are assigned running IDs when they are put on an event queue (see
  125. /// <see cref="InputSystem.QueueEvent"/>).
  126. /// </remarks>
  127. /// <seealso cref="InvalidEventId"/>
  128. public int eventId
  129. {
  130. get => (int)(m_Event.eventId & kIdMask);
  131. set => m_Event.eventId = value | (int)(m_Event.eventId & ~kIdMask);
  132. }
  133. /// <summary>
  134. /// ID of the device that the event is for.
  135. /// </summary>
  136. /// <remarks>
  137. /// Device IDs are allocated by the <see cref="IInputRuntime">runtime</see>. No two devices
  138. /// will receive the same ID over an application lifecycle regardless of whether the devices
  139. /// existed at the same time or not.
  140. /// </remarks>
  141. /// <seealso cref="InputDevice.deviceId"/>
  142. /// <seealso cref="InputSystem.GetDeviceById"/>
  143. /// <seealso cref="InputDevice.InvalidDeviceId"/>
  144. public int deviceId
  145. {
  146. get => m_Event.deviceId;
  147. set => m_Event.deviceId = (ushort)value;
  148. }
  149. /// <summary>
  150. /// Time that the event was generated at.
  151. /// </summary>
  152. /// <remarks>
  153. /// Times are in seconds and progress linearly in real-time. The timeline is the
  154. /// same as for <see cref="Time.realtimeSinceStartup"/>.
  155. ///
  156. /// Note that this implies that event times will reset in the editor every time you
  157. /// go into play mode. In effect, this can result in events appearing with negative
  158. /// timestamps (i.e. the event was generated before the current zero point for
  159. /// <see cref="Time.realtimeSinceStartup"/>).
  160. /// </remarks>
  161. public double time
  162. {
  163. get => m_Event.time - InputRuntime.s_CurrentTimeOffsetToRealtimeSinceStartup;
  164. set => m_Event.time = value + InputRuntime.s_CurrentTimeOffsetToRealtimeSinceStartup;
  165. }
  166. /// <summary>
  167. /// This is the raw input timestamp without the offset to <see cref="Time.realtimeSinceStartup"/>.
  168. /// </summary>
  169. /// <remarks>
  170. /// Internally, we always store all timestamps in "input time" which is relative to the native
  171. /// function GetTimeSinceStartup(). <see cref="IInputRuntime.currentTime"/> yields the current
  172. /// time on this timeline.
  173. /// </remarks>
  174. internal double internalTime
  175. {
  176. get => m_Event.time;
  177. set => m_Event.time = value;
  178. }
  179. ////FIXME: this API isn't consistent; time seems to be internalTime whereas time property is external time
  180. public InputEvent(FourCC type, int sizeInBytes, int deviceId, double time = -1)
  181. {
  182. if (time < 0)
  183. time = InputRuntime.s_Instance.currentTime;
  184. m_Event.type = (NativeInputEventType)(int)type;
  185. m_Event.sizeInBytes = (ushort)sizeInBytes;
  186. m_Event.deviceId = (ushort)deviceId;
  187. m_Event.time = time;
  188. m_Event.eventId = InvalidEventId;
  189. }
  190. // We internally use bits inside m_EventId as flags. IDs are linearly counted up by the
  191. // native input system starting at 1 so we have plenty room.
  192. // NOTE: The native system assigns IDs when events are queued so if our handled flag
  193. // will implicitly get overwritten. Having events go back to unhandled state
  194. // when they go on the queue makes sense in itself, though, so this is fine.
  195. public bool handled
  196. {
  197. get => (m_Event.eventId & kHandledMask) == kHandledMask;
  198. set
  199. {
  200. if (value)
  201. m_Event.eventId = (int)(m_Event.eventId | kHandledMask);
  202. else
  203. m_Event.eventId = (int)(m_Event.eventId & ~kHandledMask);
  204. }
  205. }
  206. public override string ToString()
  207. {
  208. return $"id={eventId} type={type} device={deviceId} size={sizeInBytes} time={time}";
  209. }
  210. /// <summary>
  211. /// Get the next event after the given one.
  212. /// </summary>
  213. /// <param name="currentPtr">A valid event pointer.</param>
  214. /// <returns>Pointer to the next event in memory.</returns>
  215. /// <remarks>
  216. /// This method applies no checks and must only be called if there is an event following the
  217. /// given one. Also, the size of the given event must be 100% as the method will simply
  218. /// take the size and advance the given pointer by it (and aligning it to <see cref="kAlignment"/>).
  219. /// </remarks>
  220. /// <seealso cref="GetNextInMemoryChecked"/>
  221. internal static unsafe InputEvent* GetNextInMemory(InputEvent* currentPtr)
  222. {
  223. Debug.Assert(currentPtr != null, "Event pointer must not be NULL");
  224. var alignedSizeInBytes = currentPtr->sizeInBytes.AlignToMultipleOf(kAlignment);
  225. return (InputEvent*)((byte*)currentPtr + alignedSizeInBytes);
  226. }
  227. /// <summary>
  228. /// Get the next event after the given one. Throw if that would point to invalid memory as indicated
  229. /// by the given memory buffer.
  230. /// </summary>
  231. /// <param name="currentPtr">A valid event pointer to an event inside <paramref name="buffer"/>.</param>
  232. /// <param name="buffer">Event buffer in which to advance to the next event.</param>
  233. /// <returns>Pointer to the next event.</returns>
  234. /// <exception cref="InvalidOperationException">There are no more events in the given buffer.</exception>
  235. internal static unsafe InputEvent* GetNextInMemoryChecked(InputEvent* currentPtr, ref InputEventBuffer buffer)
  236. {
  237. Debug.Assert(currentPtr != null, "Event pointer must not be NULL");
  238. var alignedSizeInBytes = currentPtr->sizeInBytes.AlignToMultipleOf(kAlignment);
  239. var nextPtr = (InputEvent*)((byte*)currentPtr + alignedSizeInBytes);
  240. if (!buffer.Contains(nextPtr))
  241. throw new InvalidOperationException(
  242. $"Event '{new InputEventPtr(currentPtr)}' is last event in given buffer with size {buffer.sizeInBytes}");
  243. return nextPtr;
  244. }
  245. public static unsafe bool Equals(InputEvent* first, InputEvent* second)
  246. {
  247. if (first == second)
  248. return true;
  249. if (first == null || second == null)
  250. return false;
  251. if (first->m_Event.sizeInBytes != second->m_Event.sizeInBytes)
  252. return false;
  253. return UnsafeUtility.MemCmp(first, second, first->m_Event.sizeInBytes) == 0;
  254. }
  255. }
  256. }