InputEventPtr.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. ///
  37. /// </summary>
  38. /// <exception cref="InvalidOperationException"></exception>
  39. public bool handled
  40. {
  41. get
  42. {
  43. if (!valid)
  44. return false;
  45. return m_EventPtr->handled;
  46. }
  47. set
  48. {
  49. if (!valid)
  50. throw new InvalidOperationException("The InputEventPtr is not valid.");
  51. m_EventPtr->handled = value;
  52. }
  53. }
  54. public int id
  55. {
  56. get
  57. {
  58. if (!valid)
  59. return 0;
  60. return m_EventPtr->eventId;
  61. }
  62. set
  63. {
  64. if (!valid)
  65. throw new InvalidOperationException("The InputEventPtr is not valid.");
  66. m_EventPtr->eventId = value;
  67. }
  68. }
  69. public FourCC type
  70. {
  71. get
  72. {
  73. if (!valid)
  74. return new FourCC();
  75. return m_EventPtr->type;
  76. }
  77. }
  78. public uint sizeInBytes
  79. {
  80. get
  81. {
  82. if (!valid)
  83. return 0;
  84. return m_EventPtr->sizeInBytes;
  85. }
  86. }
  87. public int deviceId
  88. {
  89. get
  90. {
  91. if (!valid)
  92. return InputDevice.InvalidDeviceId;
  93. return m_EventPtr->deviceId;
  94. }
  95. set
  96. {
  97. if (!valid)
  98. throw new InvalidOperationException("The InputEventPtr is not valid.");
  99. m_EventPtr->deviceId = value;
  100. }
  101. }
  102. public double time
  103. {
  104. get => valid ? m_EventPtr->time : 0.0;
  105. set
  106. {
  107. if (!valid)
  108. throw new InvalidOperationException("The InputEventPtr is not valid.");
  109. m_EventPtr->time = value;
  110. }
  111. }
  112. internal double internalTime
  113. {
  114. get => valid ? m_EventPtr->internalTime : 0.0;
  115. set
  116. {
  117. if (!valid)
  118. throw new InvalidOperationException("The InputEventPtr is not valid.");
  119. m_EventPtr->internalTime = value;
  120. }
  121. }
  122. public InputEvent* data => m_EventPtr;
  123. // The stateFormat, stateSizeInBytes, and stateOffset properties are very
  124. // useful for debugging.
  125. internal FourCC stateFormat
  126. {
  127. get
  128. {
  129. if (IsA<StateEvent>())
  130. return StateEvent.From(this)->stateFormat;
  131. if (IsA<DeltaStateEvent>())
  132. return DeltaStateEvent.From(this)->stateFormat;
  133. throw new InvalidOperationException("Event must be a StateEvent or DeltaStateEvent but is " + this);
  134. }
  135. }
  136. internal uint stateSizeInBytes
  137. {
  138. get
  139. {
  140. if (IsA<StateEvent>())
  141. return StateEvent.From(this)->stateSizeInBytes;
  142. if (IsA<DeltaStateEvent>())
  143. return DeltaStateEvent.From(this)->deltaStateSizeInBytes;
  144. throw new InvalidOperationException("Event must be a StateEvent or DeltaStateEvent but is " + this);
  145. }
  146. }
  147. internal uint stateOffset
  148. {
  149. get
  150. {
  151. if (IsA<DeltaStateEvent>())
  152. return DeltaStateEvent.From(this)->stateOffset;
  153. throw new InvalidOperationException("Event must be a DeltaStateEvent but is " + this);
  154. }
  155. }
  156. public bool IsA<TOtherEvent>()
  157. where TOtherEvent : struct, IInputEventTypeInfo
  158. {
  159. if (m_EventPtr == null)
  160. return false;
  161. // NOTE: Important to say `default` instead of `new TOtherEvent()` here. The latter will result in a call to
  162. // `Activator.CreateInstance` on Mono and thus allocate GC memory.
  163. TOtherEvent otherEvent = default;
  164. return m_EventPtr->type == otherEvent.typeStatic;
  165. }
  166. // NOTE: It is your responsibility to know *if* there actually another event following this one in memory.
  167. public InputEventPtr Next()
  168. {
  169. if (!valid)
  170. return new InputEventPtr();
  171. return new InputEventPtr(InputEvent.GetNextInMemory(m_EventPtr));
  172. }
  173. public override string ToString()
  174. {
  175. if (!valid)
  176. return "null";
  177. // il2cpp has a bug which makes builds fail if this is written as 'return m_EventPtr->ToString()'.
  178. // Gives an error about "trying to constrain an invalid type".
  179. // Writing it as a two-step operation like here makes it build cleanly.
  180. var eventPtr = *m_EventPtr;
  181. return eventPtr.ToString();
  182. }
  183. /// <summary>
  184. /// Return the plain pointer wrapped around by the struct.
  185. /// </summary>
  186. /// <returns>A plain pointer. Can be <c>null</c>.</returns>
  187. public InputEvent* ToPointer()
  188. {
  189. return this;
  190. }
  191. public bool Equals(InputEventPtr other)
  192. {
  193. return m_EventPtr == other.m_EventPtr || InputEvent.Equals(m_EventPtr, other.m_EventPtr);
  194. }
  195. public override bool Equals(object obj)
  196. {
  197. if (ReferenceEquals(null, obj))
  198. return false;
  199. return obj is InputEventPtr ptr && Equals(ptr);
  200. }
  201. public override int GetHashCode()
  202. {
  203. return unchecked((int)(long)m_EventPtr);
  204. }
  205. public static bool operator==(InputEventPtr left, InputEventPtr right)
  206. {
  207. return left.m_EventPtr == right.m_EventPtr;
  208. }
  209. public static bool operator!=(InputEventPtr left, InputEventPtr right)
  210. {
  211. return left.m_EventPtr != right.m_EventPtr;
  212. }
  213. public static implicit operator InputEventPtr(InputEvent* eventPtr)
  214. {
  215. return new InputEventPtr(eventPtr);
  216. }
  217. public static InputEventPtr From(InputEvent* eventPtr)
  218. {
  219. return new InputEventPtr(eventPtr);
  220. }
  221. public static implicit operator InputEvent*(InputEventPtr eventPtr)
  222. {
  223. return eventPtr.data;
  224. }
  225. // Make annoying Microsoft code analyzer happy.
  226. public static InputEvent* FromInputEventPtr(InputEventPtr eventPtr)
  227. {
  228. return eventPtr.data;
  229. }
  230. }
  231. }