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.

InputDeviceCommand.cs 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System.Runtime.InteropServices;
  2. using UnityEngine.InputSystem.Utilities;
  3. using Unity.Collections;
  4. using Unity.Collections.LowLevel.Unsafe;
  5. namespace UnityEngine.InputSystem.LowLevel
  6. {
  7. ////REVIEW: why is this passing the command by pointer instead of by ref?
  8. /// <summary>
  9. /// Delegate used by <see cref="InputSystem.onDeviceCommand"/>.
  10. /// </summary>
  11. public unsafe delegate long? InputDeviceCommandDelegate(InputDevice device, InputDeviceCommand* command);
  12. /// <summary>
  13. /// Delegate for executing <see cref="InputDeviceCommand"/>s inside <see cref="InputSystem.onFindLayoutForDevice"/>.
  14. /// </summary>
  15. /// <param name="command">Command to execute.</param>
  16. /// <seealso cref="InputSystem.onFindLayoutForDevice"/>
  17. /// <seealso cref="Layouts.InputDeviceFindControlLayoutDelegate"/>
  18. public delegate long InputDeviceExecuteCommandDelegate(ref InputDeviceCommand command);
  19. /// <summary>
  20. /// Data header for a command send to an <see cref="InputDevice"/>.
  21. /// </summary>
  22. /// <remarks>
  23. /// Commands are essentially synchronously processed events send directly
  24. /// to a specific device. Their primary use is to expose device-specific
  25. /// functions without having to extend the C# API used to communicate
  26. /// between input code and backend device implementations (which may sit
  27. /// in native code).
  28. ///
  29. /// Like input events, device commands use <see cref="FourCC"/> codes
  30. /// to indicate their type.
  31. /// </remarks>
  32. [StructLayout(LayoutKind.Explicit, Size = kBaseCommandSize)]
  33. public struct InputDeviceCommand : IInputDeviceCommandInfo
  34. {
  35. ////TODO: Remove kBaseCommandSize
  36. internal const int kBaseCommandSize = 8;
  37. public const int BaseCommandSize = 8;
  38. /// <summary>
  39. /// Generic failure code for <see cref="InputDevice.ExecuteCommand{TCommand}"/> calls.
  40. /// </summary>
  41. /// <remarks>
  42. /// Any negative return value for an <see cref="InputDevice.ExecuteCommand{TCommand}"/> call should be considered failure.
  43. /// </remarks>
  44. public const long GenericFailure = -1;
  45. public const long GenericSuccess = 1;
  46. [FieldOffset(0)] public FourCC type;
  47. [FieldOffset(4)] public int sizeInBytes;
  48. public int payloadSizeInBytes => sizeInBytes - kBaseCommandSize;
  49. public unsafe void* payloadPtr
  50. {
  51. get
  52. {
  53. fixed(void* thisPtr = &this)
  54. {
  55. return ((byte*)thisPtr) + kBaseCommandSize;
  56. }
  57. }
  58. }
  59. public InputDeviceCommand(FourCC type, int sizeInBytes = kBaseCommandSize)
  60. {
  61. this.type = type;
  62. this.sizeInBytes = sizeInBytes;
  63. }
  64. public static unsafe NativeArray<byte> AllocateNative(FourCC type, int payloadSize)
  65. {
  66. var sizeInBytes = payloadSize + kBaseCommandSize;
  67. var buffer = new NativeArray<byte>(sizeInBytes, Allocator.Temp);
  68. var commandPtr = (InputDeviceCommand*)NativeArrayUnsafeUtility.GetUnsafePtr(buffer);
  69. commandPtr->type = type;
  70. commandPtr->sizeInBytes = sizeInBytes;
  71. return buffer;
  72. }
  73. public FourCC typeStatic
  74. {
  75. get { return new FourCC(); }
  76. }
  77. }
  78. }