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.

InputEventPtr.cs 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using UnityEngine.InputSystem.Utilities;
  3. ////REVIEW: nuke this and force raw pointers on all code using events?
  4. namespace UnityEngine.InputSystem.LowLevel
  5. {
  6. /// <summary>
  7. /// Pointer to an <see cref="InputEvent"/>. Makes it easier to work with InputEvents and hides
  8. /// the unsafe operations necessary to work with them.
  9. /// </summary>
  10. /// <remarks>
  11. /// Note that event pointers generally refer to event buffers that are continually reused. This means
  12. /// that event pointers should not be held on to. Instead, to hold onto event data, manually copy
  13. /// an event to a buffer.
  14. /// </remarks>
  15. public unsafe struct InputEventPtr : IEquatable<InputEventPtr>
  16. {
  17. // C# does not allow us to have pointers to structs that have managed data members. Since
  18. // this can't be guaranteed for generic type parameters, they can't be used with pointers.
  19. // This is why we cannot make InputEventPtr generic or have a generic method that returns
  20. // a pointer to a specific type of event.
  21. private readonly InputEvent* m_EventPtr;
  22. /// <summary>
  23. /// Initialize the pointer to refer to the given event.
  24. /// </summary>
  25. /// <param name="eventPtr">Pointer to an event. Can be <c>null</c>.</param>
  26. public InputEventPtr(InputEvent* eventPtr)
  27. {
  28. m_EventPtr = eventPtr;
  29. }
  30. /// <summary>
  31. /// Whether the pointer is not <c>null</c>.
  32. /// </summary>
  33. /// <value>True if the struct refers to an event.</value>
  34. public bool valid => m_EventPtr != null;
  35. /// <summary>
  36. /// Whether the event is considered "handled" and should not be processed further.
  37. /// </summary>
  38. /// <remarks>
  39. /// This is used in two ways. Setting it from inside <see cref="InputSystem.onEvent"/> will
  40. /// cause the event to not be processed further. If it is a <see cref="StateEvent"/> or
  41. /// <see cref="DeltaStateEvent"/>, the <see cref="InputDevice"/> targeted by the event will
  42. /// not receive the state change.
  43. ///
  44. /// Setting this flag from inside a state change monitor (see <see cref="InputState.AddChangeMonitor(InputControl,IInputStateChangeMonitor,long,uint)"/>)
  45. /// will prevent other monitors on the same <see cref="IInputStateChangeMonitor"/> not receiving the state change.
  46. /// </remarks>
  47. /// <exception cref="InvalidOperationException">The event pointer instance is not <see cref="valid"/>.</exception>
  48. public bool handled
  49. {
  50. get
  51. {
  52. if (!valid)
  53. return false;
  54. return m_EventPtr->handled;
  55. }
  56. set
  57. {
  58. if (!valid)
  59. throw new InvalidOperationException("The InputEventPtr is not valid.");
  60. m_EventPtr->handled = value;
  61. }
  62. }
  63. public int id
  64. {
  65. get
  66. {
  67. if (!valid)
  68. return 0;
  69. return m_EventPtr->eventId;
  70. }
  71. set
  72. {
  73. if (!valid)
  74. throw new InvalidOperationException("The InputEventPtr is not valid.");
  75. m_EventPtr->eventId = value;
  76. }
  77. }
  78. public FourCC type
  79. {
  80. get
  81. {
  82. if (!valid)
  83. return new FourCC();
  84. return m_EventPtr->type;
  85. }
  86. }
  87. public uint sizeInBytes
  88. {
  89. get
  90. {
  91. if (!valid)
  92. return 0;
  93. return m_EventPtr->sizeInBytes;
  94. }
  95. }
  96. public int deviceId
  97. {
  98. get
  99. {
  100. if (!valid)
  101. return InputDevice.InvalidDeviceId;
  102. return m_EventPtr->deviceId;
  103. }
  104. set
  105. {
  106. if (!valid)
  107. throw new InvalidOperationException("The InputEventPtr is not valid.");
  108. m_EventPtr->deviceId = value;
  109. }
  110. }
  111. public double time
  112. {
  113. get => valid ? m_EventPtr->time : 0.0;
  114. set
  115. {
  116. if (!valid)
  117. throw new InvalidOperationException("The InputEventPtr is not valid.");
  118. m_EventPtr->time = value;
  119. }
  120. }
  121. internal double internalTime
  122. {
  123. get => valid ? m_EventPtr->internalTime : 0.0;
  124. set
  125. {
  126. if (!valid)
  127. throw new InvalidOperationException("The InputEventPtr is not valid.");
  128. m_EventPtr->internalTime = value;
  129. }
  130. }
  131. public InputEvent* data => m_EventPtr;
  132. // The stateFormat, stateSizeInBytes, and stateOffset properties are very
  133. // useful for debugging.
  134. internal FourCC stateFormat
  135. {
  136. get
  137. {
  138. var eventType = type;
  139. if (eventType == StateEvent.Type)
  140. return StateEvent.FromUnchecked(this)->stateFormat;
  141. if (eventType == DeltaStateEvent.Type)
  142. return DeltaStateEvent.FromUnchecked(this)->stateFormat;
  143. throw new InvalidOperationException("Event must be a StateEvent or DeltaStateEvent but is " + this);
  144. }
  145. }
  146. internal uint stateSizeInBytes
  147. {
  148. get
  149. {
  150. if (IsA<StateEvent>())
  151. return StateEvent.From(this)->stateSizeInBytes;
  152. if (IsA<DeltaStateEvent>())
  153. return DeltaStateEvent.From(this)->deltaStateSizeInBytes;
  154. throw new InvalidOperationException("Event must be a StateEvent or DeltaStateEvent but is " + this);
  155. }
  156. }
  157. internal uint stateOffset
  158. {
  159. get
  160. {
  161. if (IsA<DeltaStateEvent>())
  162. return DeltaStateEvent.From(this)->stateOffset;
  163. throw new InvalidOperationException("Event must be a DeltaStateEvent but is " + this);
  164. }
  165. }
  166. public bool IsA<TOtherEvent>()
  167. where TOtherEvent : struct, IInputEventTypeInfo
  168. {
  169. if (m_EventPtr == null)
  170. return false;
  171. // NOTE: Important to say `default` instead of `new TOtherEvent()` here. The latter will result in a call to
  172. // `Activator.CreateInstance` on Mono and thus allocate GC memory.
  173. TOtherEvent otherEvent = default;
  174. return m_EventPtr->type == otherEvent.typeStatic;
  175. }
  176. // NOTE: It is your responsibility to know *if* there actually another event following this one in memory.
  177. public InputEventPtr Next()
  178. {
  179. if (!valid)
  180. return new InputEventPtr();
  181. return new InputEventPtr(InputEvent.GetNextInMemory(m_EventPtr));
  182. }
  183. public override string ToString()
  184. {
  185. if (!valid)
  186. return "null";
  187. // il2cpp has a bug which makes builds fail if this is written as 'return m_EventPtr->ToString()'.
  188. // Gives an error about "trying to constrain an invalid type".
  189. // Writing it as a two-step operation like here makes it build cleanly.
  190. var eventPtr = *m_EventPtr;
  191. return eventPtr.ToString();
  192. }
  193. /// <summary>
  194. /// Return the plain pointer wrapped around by the struct.
  195. /// </summary>
  196. /// <returns>A plain pointer. Can be <c>null</c>.</returns>
  197. public InputEvent* ToPointer()
  198. {
  199. return this;
  200. }
  201. public bool Equals(InputEventPtr other)
  202. {
  203. return m_EventPtr == other.m_EventPtr || InputEvent.Equals(m_EventPtr, other.m_EventPtr);
  204. }
  205. public override bool Equals(object obj)
  206. {
  207. if (ReferenceEquals(null, obj))
  208. return false;
  209. return obj is InputEventPtr ptr && Equals(ptr);
  210. }
  211. public override int GetHashCode()
  212. {
  213. return unchecked((int)(long)m_EventPtr);
  214. }
  215. public static bool operator==(InputEventPtr left, InputEventPtr right)
  216. {
  217. return left.m_EventPtr == right.m_EventPtr;
  218. }
  219. public static bool operator!=(InputEventPtr left, InputEventPtr right)
  220. {
  221. return left.m_EventPtr != right.m_EventPtr;
  222. }
  223. public static implicit operator InputEventPtr(InputEvent* eventPtr)
  224. {
  225. return new InputEventPtr(eventPtr);
  226. }
  227. public static InputEventPtr From(InputEvent* eventPtr)
  228. {
  229. return new InputEventPtr(eventPtr);
  230. }
  231. public static implicit operator InputEvent*(InputEventPtr eventPtr)
  232. {
  233. return eventPtr.data;
  234. }
  235. // Make annoying Microsoft code analyzer happy.
  236. public static InputEvent* FromInputEventPtr(InputEventPtr eventPtr)
  237. {
  238. return eventPtr.data;
  239. }
  240. }
  241. }