Brak opisu
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.

GetHapticCapabilitiesCommand.cs 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // ENABLE_VR is not defined on Game Core but the assembly is available with limited features when the XR module is enabled.
  2. #if UNITY_INPUT_SYSTEM_ENABLE_XR && (ENABLE_VR || UNITY_GAMECORE) || PACKAGE_DOCS_GENERATION
  3. using System.Runtime.InteropServices;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using UnityEngine.InputSystem.Utilities;
  6. namespace UnityEngine.InputSystem.XR.Haptics
  7. {
  8. /// <summary>
  9. /// Describes the haptic capabilities of a specific device.
  10. /// </summary>
  11. public struct HapticCapabilities
  12. {
  13. /// <summary>
  14. /// Initializes and returns an instance of <see cref="HapticCapabilities"/>.
  15. /// </summary>
  16. /// <param name="numChannels">The number of haptic channels available on this device.</param>
  17. /// <param name="supportsImpulse">This device supports sending a haptic impulse.</param>
  18. /// <param name="supportsBuffer">This device supports sending a haptic buffer.</param>
  19. /// <param name="frequencyHz">The buffer frequency the device operates at in Hertz.</param>
  20. /// <param name="maxBufferSize">The max amount of buffer data that can be stored by the device.</param>
  21. /// <param name="optimalBufferSize">The optimal size of a device's buffer, taking into account frequency and latency.</param>
  22. public HapticCapabilities(uint numChannels, bool supportsImpulse, bool supportsBuffer, uint frequencyHz, uint maxBufferSize, uint optimalBufferSize)
  23. {
  24. this.numChannels = numChannels;
  25. this.supportsImpulse = supportsImpulse;
  26. this.supportsBuffer = supportsBuffer;
  27. this.frequencyHz = frequencyHz;
  28. this.maxBufferSize = maxBufferSize;
  29. this.optimalBufferSize = optimalBufferSize;
  30. }
  31. /// <summary>
  32. /// Deprecated. Use <see cref="HapticCapabilities(uint, bool, bool, uint, uint, uint)"/> instead.
  33. /// This constructor did not match the native haptic capabilities struct and was missing properties.
  34. /// </summary>
  35. /// <param name="numChannels">The number of haptic channels available on this device.</param>
  36. /// <param name="frequencyHz">The buffer frequency the device operates at in Hertz.</param>
  37. /// <param name="maxBufferSize">The max amount of buffer data that can be stored by the device.</param>
  38. public HapticCapabilities(uint numChannels, uint frequencyHz, uint maxBufferSize)
  39. : this(numChannels, false, false, frequencyHz, maxBufferSize, 0U)
  40. {
  41. }
  42. /// <summary>
  43. /// The number of haptic channels available on this device.
  44. /// </summary>
  45. public uint numChannels { get; }
  46. /// <summary>
  47. /// This device supports sending a haptic impulse.
  48. /// </summary>
  49. /// <seealso cref="SendHapticImpulseCommand"/>
  50. public bool supportsImpulse { get; }
  51. /// <summary>
  52. /// This device supports sending a haptic buffer.
  53. /// </summary>
  54. /// <seealso cref="SendBufferedHapticCommand"/>
  55. public bool supportsBuffer { get; }
  56. /// <summary>
  57. /// The buffer frequency the device operates at in Hertz. This impacts how fast the device consumes buffered haptic data.
  58. /// </summary>
  59. /// <remarks>
  60. /// This value is greater than 0 if <see cref="supportsBuffer"/> is <see langword="true"/>, and 0 otherwise.
  61. /// </remarks>
  62. public uint frequencyHz { get; }
  63. /// <summary>
  64. /// The max amount of buffer data that can be stored by the device.
  65. /// </summary>
  66. public uint maxBufferSize { get; }
  67. /// <summary>
  68. /// The optimal size of a device's buffer, taking into account frequency and latency.
  69. /// </summary>
  70. public uint optimalBufferSize { get; }
  71. }
  72. /// <summary>
  73. /// Input device command struct for retrieving the haptic capabilities of a device.
  74. /// </summary>
  75. [StructLayout(LayoutKind.Explicit, Size = kSize)]
  76. public struct GetHapticCapabilitiesCommand : IInputDeviceCommandInfo
  77. {
  78. static FourCC Type => new FourCC('X', 'H', 'C', '0');
  79. // 20 bytes of data from uint(4) + bool(1) + bool(1) + padding + uint(4) + uint(4) + uint(4)
  80. const int kSize = InputDeviceCommand.kBaseCommandSize + 20;
  81. /// <inheritdoc />
  82. public FourCC typeStatic => Type;
  83. [FieldOffset(0)]
  84. InputDeviceCommand baseCommand;
  85. /// <summary>
  86. /// The number of haptic channels available on this device.
  87. /// </summary>
  88. [FieldOffset(InputDeviceCommand.kBaseCommandSize)]
  89. public uint numChannels;
  90. /// <summary>
  91. /// This device supports sending a haptic impulse.
  92. /// </summary>
  93. /// <seealso cref="SendHapticImpulseCommand"/>
  94. [FieldOffset(InputDeviceCommand.kBaseCommandSize + 4)]
  95. public bool supportsImpulse;
  96. /// <summary>
  97. /// This device supports sending a haptic buffer.
  98. /// </summary>
  99. /// <seealso cref="SendBufferedHapticCommand"/>
  100. [FieldOffset(InputDeviceCommand.kBaseCommandSize + 5)]
  101. public bool supportsBuffer;
  102. /// <summary>
  103. /// The buffer frequency the device operates at in Hertz. This impacts how fast the device consumes buffered haptic data.
  104. /// </summary>
  105. /// <remarks>
  106. /// This value is greater than 0 if <see cref="supportsBuffer"/> is <see langword="true"/>, and 0 otherwise.
  107. /// </remarks>
  108. [FieldOffset(InputDeviceCommand.kBaseCommandSize + 8)]
  109. public uint frequencyHz;
  110. /// <summary>
  111. /// The max amount of buffer data that can be stored by the device.
  112. /// </summary>
  113. [FieldOffset(InputDeviceCommand.kBaseCommandSize + 12)]
  114. public uint maxBufferSize;
  115. /// <summary>
  116. /// The optimal size of a device's buffer, taking into account frequency and latency.
  117. /// </summary>
  118. [FieldOffset(InputDeviceCommand.kBaseCommandSize + 16)]
  119. public uint optimalBufferSize;
  120. /// <summary>
  121. /// The haptic capabilities of the device, populated after this command is executed.
  122. /// </summary>
  123. public HapticCapabilities capabilities => new HapticCapabilities(numChannels, supportsImpulse, supportsBuffer, frequencyHz, maxBufferSize, optimalBufferSize);
  124. /// <summary>
  125. /// Creates and returns a new initialized input device command struct for retrieving
  126. /// the haptic capabilities of a device when executed.
  127. /// </summary>
  128. /// <returns>Returns a new command struct with the data header initialized, making it ready to execute.</returns>
  129. /// <seealso cref="InputDevice.ExecuteCommand{TCommand}(ref TCommand)"/>
  130. public static GetHapticCapabilitiesCommand Create()
  131. {
  132. return new GetHapticCapabilitiesCommand
  133. {
  134. baseCommand = new InputDeviceCommand(Type, kSize),
  135. };
  136. }
  137. }
  138. }
  139. #endif