HID.cs 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine.InputSystem.LowLevel;
  7. using UnityEngine.InputSystem.Utilities;
  8. using Unity.Collections.LowLevel.Unsafe;
  9. using UnityEngine.InputSystem.Layouts;
  10. using UnityEngine.Scripting;
  11. // HID support is currently broken in 32-bit Windows standalone players. Consider 32bit Windows players unsupported for now.
  12. #if UNITY_STANDALONE_WIN && !UNITY_64
  13. #warning The 32-bit Windows player is not currently supported by the Input System. HID input will not work in the player. Please use x86_64, if possible.
  14. #endif
  15. ////REVIEW: there will probably be lots of cases where the HID device creation process just needs a little tweaking; we should
  16. //// have better mechanism to do that without requiring to replace the entire process wholesale
  17. ////TODO: expose the layout builder so that other layout builders can use it for their own purposes
  18. ////REVIEW: how are we dealing with multiple different input reports on the same device?
  19. ////REVIEW: move the enums and structs out of here and into UnityEngine.InputSystem.HID? Or remove the "HID" name prefixes from them?
  20. ////TODO: add blacklist for devices we really don't want to use (like apple's internal trackpad)
  21. ////TODO: add a way to mark certain layouts (such as HID layouts) as fallbacks; ideally, affect the layout matching score
  22. ////TODO: enable this to handle devices that split their input into multiple reports
  23. #pragma warning disable CS0649, CS0219
  24. namespace UnityEngine.InputSystem.HID
  25. {
  26. /// <summary>
  27. /// A generic HID input device.
  28. /// </summary>
  29. /// <remarks>
  30. /// This class represents a best effort to mirror the control setup of a HID
  31. /// discovered in the system. It is used only as a fallback where we cannot
  32. /// match the device to a specific product we know of. Wherever possible we
  33. /// construct more specific device representations such as Gamepad.
  34. /// </remarks>
  35. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces")]
  36. [Preserve]
  37. public class HID : InputDevice
  38. {
  39. internal const string kHIDInterface = "HID";
  40. internal const string kHIDNamespace = "HID";
  41. /// <summary>
  42. /// Command code for querying the HID report descriptor from a device.
  43. /// </summary>
  44. /// <seealso cref="InputDevice.ExecuteCommand{TCommand}"/>
  45. public static FourCC QueryHIDReportDescriptorDeviceCommandType { get { return new FourCC('H', 'I', 'D', 'D'); } }
  46. /// <summary>
  47. /// Command code for querying the HID report descriptor size in bytes from a device.
  48. /// </summary>
  49. /// <seealso cref="InputDevice.ExecuteCommand{TCommand}"/>
  50. public static FourCC QueryHIDReportDescriptorSizeDeviceCommandType { get { return new FourCC('H', 'I', 'D', 'S'); } }
  51. public static FourCC QueryHIDParsedReportDescriptorDeviceCommandType { get { return new FourCC('H', 'I', 'D', 'P'); } }
  52. /// <summary>
  53. /// The HID device descriptor as received from the system.
  54. /// </summary>
  55. public HIDDeviceDescriptor hidDescriptor
  56. {
  57. get
  58. {
  59. if (!m_HaveParsedHIDDescriptor)
  60. {
  61. if (!string.IsNullOrEmpty(description.capabilities))
  62. m_HIDDescriptor = JsonUtility.FromJson<HIDDeviceDescriptor>(description.capabilities);
  63. m_HaveParsedHIDDescriptor = true;
  64. }
  65. return m_HIDDescriptor;
  66. }
  67. }
  68. private bool m_HaveParsedHIDDescriptor;
  69. private HIDDeviceDescriptor m_HIDDescriptor;
  70. // This is the workhorse for figuring out fallback options for HIDs attached to the system.
  71. // If the system cannot find a more specific layout for a given HID, this method will try
  72. // to produce a layout builder on the fly based on the HID descriptor received from
  73. // the device.
  74. internal static string OnFindLayoutForDevice(ref InputDeviceDescription description, string matchedLayout,
  75. InputDeviceExecuteCommandDelegate executeDeviceCommand)
  76. {
  77. // If the system found a matching layout, there's nothing for us to do.
  78. if (!string.IsNullOrEmpty(matchedLayout))
  79. return null;
  80. // If the device isn't a HID, we're not interested.
  81. if (description.interfaceName != kHIDInterface)
  82. return null;
  83. // Read HID descriptor.
  84. var hidDeviceDescriptor = ReadHIDDeviceDescriptor(ref description, executeDeviceCommand);
  85. if (!HIDSupport.supportedHIDUsages.Contains(new HIDSupport.HIDPageUsage(hidDeviceDescriptor.usagePage, hidDeviceDescriptor.usage)))
  86. return null;
  87. // Determine if there's any usable elements on the device.
  88. var hasUsableElements = false;
  89. if (hidDeviceDescriptor.elements != null)
  90. {
  91. foreach (var element in hidDeviceDescriptor.elements)
  92. {
  93. if (element.IsUsableElement())
  94. {
  95. hasUsableElements = true;
  96. break;
  97. }
  98. }
  99. }
  100. // If not, there's nothing we can do with the device.
  101. if (!hasUsableElements)
  102. return null;
  103. ////TODO: we should be able to differentiate a HID joystick from other joysticks in bindings alone
  104. // Determine base layout.
  105. var baseType = typeof(HID);
  106. var baseLayout = "HID";
  107. if (hidDeviceDescriptor.usagePage == UsagePage.GenericDesktop)
  108. {
  109. if (hidDeviceDescriptor.usage == (int)GenericDesktop.Joystick || hidDeviceDescriptor.usage == (int)GenericDesktop.Gamepad)
  110. {
  111. baseLayout = "Joystick";
  112. baseType = typeof(Joystick);
  113. }
  114. }
  115. // A HID may implement the HID interface arbitrary many times, each time with a different
  116. // usage page + usage combination. In a OS, this will typically come out as multiple separate
  117. // devices. Thus, to make layout names unique, we have to take usages into account. What we do
  118. // is we tag the usage name onto the layout name *except* if it's a joystick or gamepad. This
  119. // gives us nicer names for joysticks while still disambiguating other devices correctly.
  120. var usageName = "";
  121. if (baseLayout != "Joystick")
  122. {
  123. usageName = hidDeviceDescriptor.usagePage == UsagePage.GenericDesktop
  124. ? $" {(GenericDesktop) hidDeviceDescriptor.usage}"
  125. : $" {hidDeviceDescriptor.usagePage}-{hidDeviceDescriptor.usage}";
  126. }
  127. ////REVIEW: these layout names are impossible to bind to; come up with a better way
  128. ////TODO: match HID layouts by vendor and product ID
  129. ////REVIEW: this probably works fine for most products out there but I'm not sure it works reliably for all cases
  130. // Come up with a unique template name. HIDs are required to have product and vendor IDs.
  131. // We go with the string versions if we have them and with the numeric versions if we don't.
  132. string layoutName;
  133. var deviceMatcher = InputDeviceMatcher.FromDeviceDescription(description);
  134. if (!string.IsNullOrEmpty(description.product) && !string.IsNullOrEmpty(description.manufacturer))
  135. {
  136. layoutName = $"{kHIDNamespace}::{description.manufacturer} {description.product}{usageName}";
  137. }
  138. else if (!string.IsNullOrEmpty(description.product))
  139. {
  140. layoutName = $"{kHIDNamespace}::{description.product}{usageName}";
  141. }
  142. else
  143. {
  144. // Sanity check to make sure we really have the data we expect.
  145. if (hidDeviceDescriptor.vendorId == 0)
  146. return null;
  147. layoutName =
  148. $"{kHIDNamespace}::{hidDeviceDescriptor.vendorId:X}-{hidDeviceDescriptor.productId:X}{usageName}";
  149. deviceMatcher = deviceMatcher
  150. .WithCapability("productId", hidDeviceDescriptor.productId)
  151. .WithCapability("vendorId", hidDeviceDescriptor.vendorId);
  152. }
  153. // Also match by usage. See comment above about multiple HID interfaces on the same device.
  154. deviceMatcher = deviceMatcher
  155. .WithCapability("usage", hidDeviceDescriptor.usage)
  156. .WithCapability("usagePage", hidDeviceDescriptor.usagePage);
  157. // Register layout builder that will turn the HID descriptor into an
  158. // InputControlLayout instance.
  159. var layout = new HIDLayoutBuilder
  160. {
  161. displayName = description.product,
  162. hidDescriptor = hidDeviceDescriptor,
  163. parentLayout = baseLayout,
  164. deviceType = baseType ?? typeof(HID)
  165. };
  166. InputSystem.RegisterLayoutBuilder(() => layout.Build(),
  167. layoutName, baseLayout, deviceMatcher);
  168. return layoutName;
  169. }
  170. internal static unsafe HIDDeviceDescriptor ReadHIDDeviceDescriptor(ref InputDeviceDescription deviceDescription,
  171. InputDeviceExecuteCommandDelegate executeCommandDelegate)
  172. {
  173. if (deviceDescription.interfaceName != kHIDInterface)
  174. throw new ArgumentException(
  175. $"Device '{deviceDescription}' is not a HID");
  176. // See if we have to request a HID descriptor from the device.
  177. // We support having the descriptor directly as a JSON string in the `capabilities`
  178. // field of the device description.
  179. var needToRequestDescriptor = true;
  180. var hidDeviceDescriptor = new HIDDeviceDescriptor();
  181. if (!string.IsNullOrEmpty(deviceDescription.capabilities))
  182. {
  183. try
  184. {
  185. hidDeviceDescriptor = HIDDeviceDescriptor.FromJson(deviceDescription.capabilities);
  186. // If there's elements in the descriptor, we're good with the descriptor. If there aren't,
  187. // we go and ask the device for a full descriptor.
  188. if (hidDeviceDescriptor.elements != null && hidDeviceDescriptor.elements.Length > 0)
  189. needToRequestDescriptor = false;
  190. }
  191. catch (Exception exception)
  192. {
  193. Debug.LogError($"Could not parse HID descriptor of device '{deviceDescription}'");
  194. Debug.LogException(exception);
  195. }
  196. }
  197. ////REVIEW: we *could* switch to a single path here that supports *only* parsed descriptors but it'd
  198. //// mean having to switch *every* platform supporting HID to the hack we currently have to do
  199. //// on Windows
  200. // Request descriptor, if necessary.
  201. if (needToRequestDescriptor)
  202. {
  203. // Try to get the size of the HID descriptor from the device.
  204. var sizeOfDescriptorCommand = new InputDeviceCommand(QueryHIDReportDescriptorSizeDeviceCommandType);
  205. var sizeOfDescriptorInBytes = executeCommandDelegate(ref sizeOfDescriptorCommand);
  206. if (sizeOfDescriptorInBytes > 0)
  207. {
  208. // Now try to fetch the HID descriptor.
  209. using (var buffer =
  210. InputDeviceCommand.AllocateNative(QueryHIDReportDescriptorDeviceCommandType, (int)sizeOfDescriptorInBytes))
  211. {
  212. var commandPtr = (InputDeviceCommand*)buffer.GetUnsafePtr();
  213. if (executeCommandDelegate(ref *commandPtr) != sizeOfDescriptorInBytes)
  214. return new HIDDeviceDescriptor();
  215. // Try to parse the HID report descriptor.
  216. if (!HIDParser.ParseReportDescriptor((byte*)commandPtr->payloadPtr, (int)sizeOfDescriptorInBytes, ref hidDeviceDescriptor))
  217. return new HIDDeviceDescriptor();
  218. }
  219. // Update the descriptor on the device with the information we got.
  220. deviceDescription.capabilities = hidDeviceDescriptor.ToJson();
  221. }
  222. else
  223. {
  224. // The device may not support binary descriptors but may support parsed descriptors so
  225. // try the IOCTL for parsed descriptors next.
  226. //
  227. // This path exists pretty much only for the sake of Windows where it is not possible to get
  228. // unparsed/binary descriptors from the device (and where getting element offsets is only possible
  229. // with some dirty hacks we're performing in the native runtime).
  230. const int kMaxDescriptorBufferSize = 2 * 1024 * 1024; ////TODO: switch to larger buffer based on return code if request fails
  231. using (var buffer =
  232. InputDeviceCommand.AllocateNative(QueryHIDParsedReportDescriptorDeviceCommandType, kMaxDescriptorBufferSize))
  233. {
  234. var commandPtr = (InputDeviceCommand*)buffer.GetUnsafePtr();
  235. var utf8Length = executeCommandDelegate(ref *commandPtr);
  236. if (utf8Length < 0)
  237. return new HIDDeviceDescriptor();
  238. // Turn UTF-8 buffer into string.
  239. ////TODO: is there a way to not have to copy here?
  240. var utf8 = new byte[utf8Length];
  241. fixed(byte* utf8Ptr = utf8)
  242. {
  243. UnsafeUtility.MemCpy(utf8Ptr, commandPtr->payloadPtr, utf8Length);
  244. }
  245. var descriptorJson = Encoding.UTF8.GetString(utf8, 0, (int)utf8Length);
  246. // Try to parse the HID report descriptor.
  247. try
  248. {
  249. hidDeviceDescriptor = HIDDeviceDescriptor.FromJson(descriptorJson);
  250. }
  251. catch (Exception exception)
  252. {
  253. Debug.LogError($"Could not parse HID descriptor of device '{deviceDescription}'");
  254. Debug.LogException(exception);
  255. return new HIDDeviceDescriptor();
  256. }
  257. // Update the descriptor on the device with the information we got.
  258. deviceDescription.capabilities = descriptorJson;
  259. }
  260. }
  261. }
  262. return hidDeviceDescriptor;
  263. }
  264. public static string UsagePageToString(UsagePage usagePage)
  265. {
  266. return (int)usagePage >= 0xFF00 ? "Vendor-Defined" : usagePage.ToString();
  267. }
  268. public static string UsageToString(UsagePage usagePage, int usage)
  269. {
  270. switch (usagePage)
  271. {
  272. case UsagePage.GenericDesktop:
  273. return ((GenericDesktop)usage).ToString();
  274. case UsagePage.Simulation:
  275. return ((Simulation)usage).ToString();
  276. default:
  277. return null;
  278. }
  279. }
  280. [Serializable]
  281. private class HIDLayoutBuilder
  282. {
  283. public string displayName;
  284. public HIDDeviceDescriptor hidDescriptor;
  285. public string parentLayout;
  286. public Type deviceType;
  287. public InputControlLayout Build()
  288. {
  289. var builder = new InputControlLayout.Builder
  290. {
  291. displayName = displayName,
  292. type = deviceType,
  293. extendsLayout = parentLayout,
  294. stateFormat = new FourCC('H', 'I', 'D')
  295. };
  296. var xElement = Array.Find(hidDescriptor.elements,
  297. element => element.usagePage == UsagePage.GenericDesktop &&
  298. element.usage == (int)GenericDesktop.X);
  299. var yElement = Array.Find(hidDescriptor.elements,
  300. element => element.usagePage == UsagePage.GenericDesktop &&
  301. element.usage == (int)GenericDesktop.Y);
  302. ////REVIEW: in case the X and Y control are non-contiguous, should we even turn them into a stick
  303. ////REVIEW: there *has* to be an X and a Y for us to be able to successfully create a joystick
  304. // If GenericDesktop.X and GenericDesktop.Y are both present, turn the controls
  305. // into a stick.
  306. var haveStick = xElement.usage == (int)GenericDesktop.X && yElement.usage == (int)GenericDesktop.Y;
  307. if (haveStick)
  308. {
  309. int bitOffset, byteOffset, sizeInBits;
  310. if (xElement.reportOffsetInBits <= yElement.reportOffsetInBits)
  311. {
  312. bitOffset = xElement.reportOffsetInBits % 8;
  313. byteOffset = xElement.reportOffsetInBits / 8;
  314. sizeInBits = (yElement.reportOffsetInBits + yElement.reportSizeInBits) -
  315. xElement.reportOffsetInBits;
  316. }
  317. else
  318. {
  319. bitOffset = yElement.reportOffsetInBits % 8;
  320. byteOffset = yElement.reportOffsetInBits / 8;
  321. sizeInBits = (xElement.reportOffsetInBits + xElement.reportSizeInBits) -
  322. yElement.reportSizeInBits;
  323. }
  324. const string stickName = "stick";
  325. builder.AddControl(stickName)
  326. .WithDisplayName("Stick")
  327. .WithLayout("Stick")
  328. .WithBitOffset((uint)bitOffset)
  329. .WithByteOffset((uint)byteOffset)
  330. .WithSizeInBits((uint)sizeInBits)
  331. .WithUsages(CommonUsages.Primary2DMotion);
  332. var xElementParameters = xElement.DetermineParameters();
  333. var yElementParameters = yElement.DetermineParameters();
  334. builder.AddControl(stickName + "/x")
  335. .WithFormat(xElement.isSigned ? InputStateBlock.FormatSBit : InputStateBlock.FormatBit)
  336. .WithByteOffset((uint)(xElement.reportOffsetInBits / 8 - byteOffset))
  337. .WithBitOffset((uint)(xElement.reportOffsetInBits % 8))
  338. .WithSizeInBits((uint)xElement.reportSizeInBits)
  339. .WithParameters(xElementParameters)
  340. .WithDefaultState(xElement.DetermineDefaultState())
  341. .WithProcessors(xElement.DetermineProcessors());
  342. builder.AddControl(stickName + "/y")
  343. .WithFormat(yElement.isSigned ? InputStateBlock.FormatSBit : InputStateBlock.FormatBit)
  344. .WithByteOffset((uint)(yElement.reportOffsetInBits / 8 - byteOffset))
  345. .WithBitOffset((uint)(yElement.reportOffsetInBits % 8))
  346. .WithSizeInBits((uint)yElement.reportSizeInBits)
  347. .WithParameters(yElementParameters)
  348. .WithDefaultState(yElement.DetermineDefaultState())
  349. .WithProcessors(yElement.DetermineProcessors());
  350. // Propagate parameters needed on x and y to the four button controls.
  351. builder.AddControl(stickName + "/up")
  352. .WithParameters(
  353. StringHelpers.Join(",", yElementParameters, "clamp=2,clampMin=-1,clampMax=0,invert=true"));
  354. builder.AddControl(stickName + "/down")
  355. .WithParameters(
  356. StringHelpers.Join(",", yElementParameters, "clamp=2,clampMin=0,clampMax=1,invert=false"));
  357. builder.AddControl(stickName + "/left")
  358. .WithParameters(
  359. StringHelpers.Join(",", xElementParameters, "clamp=2,clampMin=-1,clampMax=0,invert"));
  360. builder.AddControl(stickName + "/right")
  361. .WithParameters(
  362. StringHelpers.Join(",", xElementParameters, "clamp=2,clampMin=0,clampMax=1"));
  363. }
  364. // Process HID descriptor.
  365. var elements = hidDescriptor.elements;
  366. var elementCount = elements.Length;
  367. for (var i = 0; i < elementCount; ++i)
  368. {
  369. ref var element = ref elements[i];
  370. if (element.reportType != HIDReportType.Input)
  371. continue;
  372. // Skip X and Y if we already turned them into a stick.
  373. if (haveStick && (element.Is(UsagePage.GenericDesktop, (int)GenericDesktop.X) ||
  374. element.Is(UsagePage.GenericDesktop, (int)GenericDesktop.Y)))
  375. continue;
  376. var layout = element.DetermineLayout();
  377. if (layout != null)
  378. {
  379. // Assign unique name.
  380. var name = element.DetermineName();
  381. Debug.Assert(!string.IsNullOrEmpty(name));
  382. name = StringHelpers.MakeUniqueName(name, builder.controls, x => x.name);
  383. // Add control.
  384. var control =
  385. builder.AddControl(name)
  386. .WithDisplayName(element.DetermineDisplayName())
  387. .WithLayout(layout)
  388. .WithByteOffset((uint)element.reportOffsetInBits / 8)
  389. .WithBitOffset((uint)element.reportOffsetInBits % 8)
  390. .WithSizeInBits((uint)element.reportSizeInBits)
  391. .WithFormat(element.DetermineFormat())
  392. .WithDefaultState(element.DetermineDefaultState())
  393. .WithProcessors(element.DetermineProcessors());
  394. var parameters = element.DetermineParameters();
  395. if (!string.IsNullOrEmpty(parameters))
  396. control.WithParameters(parameters);
  397. var usages = element.DetermineUsages();
  398. if (usages != null)
  399. control.WithUsages(usages);
  400. element.AddChildControls(ref element, name, ref builder);
  401. }
  402. }
  403. return builder.Build();
  404. }
  405. }
  406. public enum HIDReportType
  407. {
  408. Unknown,
  409. Input,
  410. Output,
  411. Feature
  412. }
  413. public enum HIDCollectionType
  414. {
  415. Physical = 0x00,
  416. Application = 0x01,
  417. Logical = 0x02,
  418. Report = 0x03,
  419. NamedArray = 0x04,
  420. UsageSwitch = 0x05,
  421. UsageModifier = 0x06
  422. }
  423. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "Flags", Justification = "No better term for underlying data.")]
  424. [Flags]
  425. public enum HIDElementFlags
  426. {
  427. Constant = 1 << 0,
  428. Variable = 1 << 1,
  429. Relative = 1 << 2,
  430. Wrap = 1 << 3,
  431. NonLinear = 1 << 4,
  432. NoPreferred = 1 << 5,
  433. NullState = 1 << 6,
  434. Volatile = 1 << 7,
  435. BufferedBytes = 1 << 8
  436. }
  437. /// <summary>
  438. /// Descriptor for a single report element.
  439. /// </summary>
  440. [Serializable]
  441. public struct HIDElementDescriptor
  442. {
  443. public int usage;
  444. public UsagePage usagePage;
  445. public int unit;
  446. public int unitExponent;
  447. public int logicalMin;
  448. public int logicalMax;
  449. public int physicalMin;
  450. public int physicalMax;
  451. public HIDReportType reportType;
  452. public int collectionIndex;
  453. public int reportId;
  454. public int reportSizeInBits;
  455. public int reportOffsetInBits;
  456. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1726:UsePreferredTerms", MessageId = "flags", Justification = "No better term for underlying data.")]
  457. public HIDElementFlags flags;
  458. // Fields only relevant to arrays.
  459. public int? usageMin;
  460. public int? usageMax;
  461. public bool hasNullState => (flags & HIDElementFlags.NullState) == HIDElementFlags.NullState;
  462. public bool hasPreferredState => (flags & HIDElementFlags.NoPreferred) != HIDElementFlags.NoPreferred;
  463. public bool isArray => (flags & HIDElementFlags.Variable) != HIDElementFlags.Variable;
  464. public bool isNonLinear => (flags & HIDElementFlags.NonLinear) == HIDElementFlags.NonLinear;
  465. public bool isRelative => (flags & HIDElementFlags.Relative) == HIDElementFlags.Relative;
  466. public bool isConstant => (flags & HIDElementFlags.Constant) == HIDElementFlags.Constant;
  467. public bool isWrapping => (flags & HIDElementFlags.Wrap) == HIDElementFlags.Wrap;
  468. internal bool isSigned => logicalMin < 0;
  469. internal float minFloatValue
  470. {
  471. get
  472. {
  473. var maxValue = (1 << reportSizeInBits) - 1;
  474. if (isSigned)
  475. return logicalMin / (float)((maxValue + 1) / 2);
  476. return logicalMin / (float)maxValue;
  477. }
  478. }
  479. internal float maxFloatValue
  480. {
  481. get
  482. {
  483. var maxValue = (1 << reportSizeInBits) - 1;
  484. if (isSigned)
  485. return logicalMax / (float)((maxValue + 1) / 2);
  486. return logicalMax / (float)maxValue;
  487. }
  488. }
  489. public bool Is(UsagePage usagePage, int usage)
  490. {
  491. return usagePage == this.usagePage && usage == this.usage;
  492. }
  493. internal string DetermineName()
  494. {
  495. // It's rare for HIDs to declare string names for items and HID drivers may report weird strings
  496. // plus there's no guarantee that these names are unique per item. So, we don't bother here with
  497. // device/driver-supplied names at all but rather do our own naming.
  498. switch (usagePage)
  499. {
  500. case UsagePage.Button:
  501. if (usage == 1)
  502. return "trigger";
  503. return $"button{usage}";
  504. case UsagePage.GenericDesktop:
  505. if (usage == (int)GenericDesktop.HatSwitch)
  506. return "hat";
  507. var text = ((GenericDesktop)usage).ToString();
  508. // Lower-case first letter.
  509. text = char.ToLowerInvariant(text[0]) + text.Substring(1);
  510. return text;
  511. }
  512. // Fallback that generates a somewhat useless but at least very informative name.
  513. return $"UsagePage({usagePage:X}) Usage({usage:X})";
  514. }
  515. internal string DetermineDisplayName()
  516. {
  517. switch (usagePage)
  518. {
  519. case UsagePage.Button:
  520. if (usage == 1)
  521. return "Trigger";
  522. return $"Button {usage}";
  523. case UsagePage.GenericDesktop:
  524. return ((GenericDesktop)usage).ToString();
  525. }
  526. return null;
  527. }
  528. internal bool IsUsableElement()
  529. {
  530. switch (usage)
  531. {
  532. case (int)GenericDesktop.X:
  533. case (int)GenericDesktop.Y:
  534. return usagePage == UsagePage.GenericDesktop;
  535. default:
  536. return DetermineLayout() != null;
  537. }
  538. }
  539. internal string DetermineLayout()
  540. {
  541. if (reportType != HIDReportType.Input)
  542. return null;
  543. ////TODO: deal with arrays
  544. switch (usagePage)
  545. {
  546. case UsagePage.Button:
  547. return "Button";
  548. case UsagePage.GenericDesktop:
  549. switch (usage)
  550. {
  551. case (int)GenericDesktop.X:
  552. case (int)GenericDesktop.Y:
  553. case (int)GenericDesktop.Z:
  554. case (int)GenericDesktop.Rx:
  555. case (int)GenericDesktop.Ry:
  556. case (int)GenericDesktop.Rz:
  557. case (int)GenericDesktop.Vx:
  558. case (int)GenericDesktop.Vy:
  559. case (int)GenericDesktop.Vz:
  560. case (int)GenericDesktop.Vbrx:
  561. case (int)GenericDesktop.Vbry:
  562. case (int)GenericDesktop.Vbrz:
  563. case (int)GenericDesktop.Slider:
  564. case (int)GenericDesktop.Dial:
  565. case (int)GenericDesktop.Wheel:
  566. return "Axis";
  567. case (int)GenericDesktop.Select:
  568. case (int)GenericDesktop.Start:
  569. case (int)GenericDesktop.DpadUp:
  570. case (int)GenericDesktop.DpadDown:
  571. case (int)GenericDesktop.DpadLeft:
  572. case (int)GenericDesktop.DpadRight:
  573. return "Button";
  574. case (int)GenericDesktop.HatSwitch:
  575. // Only support hat switches with 8 directions.
  576. if (logicalMax - logicalMin + 1 == 8)
  577. return "Dpad";
  578. break;
  579. }
  580. break;
  581. }
  582. return null;
  583. }
  584. internal FourCC DetermineFormat()
  585. {
  586. switch (reportSizeInBits)
  587. {
  588. case 8:
  589. if (isSigned)
  590. return InputStateBlock.FormatSByte;
  591. return InputStateBlock.FormatByte;
  592. case 16:
  593. if (isSigned)
  594. return InputStateBlock.FormatShort;
  595. return InputStateBlock.FormatUShort;
  596. case 32:
  597. if (isSigned)
  598. return InputStateBlock.FormatInt;
  599. return InputStateBlock.FormatUInt;
  600. default:
  601. // Generic bitfield value.
  602. return InputStateBlock.FormatBit;
  603. }
  604. }
  605. internal InternedString[] DetermineUsages()
  606. {
  607. if (usagePage == UsagePage.Button && usage == 1)
  608. return new[] {CommonUsages.PrimaryTrigger, CommonUsages.PrimaryAction};
  609. if (usagePage == UsagePage.Button && usage == 2)
  610. return new[] {CommonUsages.SecondaryTrigger, CommonUsages.SecondaryAction};
  611. if (usagePage == UsagePage.GenericDesktop && usage == (int)GenericDesktop.Rz)
  612. return new[] { CommonUsages.Twist };
  613. ////TODO: assign hatswitch usage to first and only to first hatswitch element
  614. return null;
  615. }
  616. internal string DetermineParameters()
  617. {
  618. if (usagePage == UsagePage.GenericDesktop)
  619. {
  620. switch (usage)
  621. {
  622. case (int)GenericDesktop.X:
  623. case (int)GenericDesktop.Z:
  624. case (int)GenericDesktop.Rx:
  625. case (int)GenericDesktop.Rz:
  626. case (int)GenericDesktop.Vx:
  627. case (int)GenericDesktop.Vz:
  628. case (int)GenericDesktop.Vbrx:
  629. case (int)GenericDesktop.Vbrz:
  630. case (int)GenericDesktop.Slider:
  631. case (int)GenericDesktop.Dial:
  632. case (int)GenericDesktop.Wheel:
  633. return DetermineAxisNormalizationParameters();
  634. // Our Ys tend to be the opposite of what most HIDs do. We can't be sure and may well
  635. // end up inverting a value here when we shouldn't but as always with the HID fallback,
  636. // let's try to do what *seems* to work with the majority of devices.
  637. case (int)GenericDesktop.Y:
  638. case (int)GenericDesktop.Ry:
  639. case (int)GenericDesktop.Vy:
  640. case (int)GenericDesktop.Vbry:
  641. return StringHelpers.Join(",", "invert", DetermineAxisNormalizationParameters());
  642. }
  643. }
  644. return null;
  645. }
  646. private string DetermineAxisNormalizationParameters()
  647. {
  648. // If we have min/max bounds on the axis values, set up normalization on the axis.
  649. // NOTE: We put the center in the middle between min/max as we can't know where the
  650. // resting point of the axis is (may be on min if it's a trigger, for example).
  651. if (logicalMin == 0 && logicalMax == 0)
  652. return "normalize,normalizeMin=0,normalizeMax=1,normalizeZero=0.5";
  653. var min = minFloatValue;
  654. var max = maxFloatValue;
  655. // Do nothing if result of floating-point conversion is already normalized.
  656. if (Mathf.Approximately(0f, min) && Mathf.Approximately(0f, max))
  657. return null;
  658. var zero = min + (max - min) / 2.0f;
  659. return string.Format(CultureInfo.InvariantCulture, "normalize,normalizeMin={0},normalizeMax={1},normalizeZero={2}", min, max, zero);
  660. }
  661. internal string DetermineProcessors()
  662. {
  663. switch (usagePage)
  664. {
  665. case UsagePage.GenericDesktop:
  666. switch (usage)
  667. {
  668. case (int)GenericDesktop.X:
  669. case (int)GenericDesktop.Y:
  670. case (int)GenericDesktop.Z:
  671. case (int)GenericDesktop.Rx:
  672. case (int)GenericDesktop.Ry:
  673. case (int)GenericDesktop.Rz:
  674. case (int)GenericDesktop.Vx:
  675. case (int)GenericDesktop.Vy:
  676. case (int)GenericDesktop.Vz:
  677. case (int)GenericDesktop.Vbrx:
  678. case (int)GenericDesktop.Vbry:
  679. case (int)GenericDesktop.Vbrz:
  680. case (int)GenericDesktop.Slider:
  681. case (int)GenericDesktop.Dial:
  682. case (int)GenericDesktop.Wheel:
  683. return "axisDeadzone";
  684. }
  685. break;
  686. }
  687. return null;
  688. }
  689. internal PrimitiveValue DetermineDefaultState()
  690. {
  691. switch (usagePage)
  692. {
  693. case UsagePage.GenericDesktop:
  694. switch (usage)
  695. {
  696. case (int)GenericDesktop.HatSwitch:
  697. // Figure out null state for hat switches.
  698. if (hasNullState)
  699. {
  700. // We're looking for a value that is out-of-range with respect to the
  701. // logical min and max but in range with respect to what we can store
  702. // in the bits we have.
  703. // Test lower bound.
  704. var minMinusOne = logicalMin - 1;
  705. if (minMinusOne >= 0)
  706. return new PrimitiveValue(minMinusOne);
  707. // Test upper bound.
  708. var maxPlusOne = logicalMax + 1;
  709. if (maxPlusOne <= (1 << reportSizeInBits) - 1)
  710. return new PrimitiveValue(maxPlusOne);
  711. }
  712. break;
  713. case (int)GenericDesktop.X:
  714. case (int)GenericDesktop.Y:
  715. case (int)GenericDesktop.Z:
  716. case (int)GenericDesktop.Rx:
  717. case (int)GenericDesktop.Ry:
  718. case (int)GenericDesktop.Rz:
  719. case (int)GenericDesktop.Vx:
  720. case (int)GenericDesktop.Vy:
  721. case (int)GenericDesktop.Vz:
  722. case (int)GenericDesktop.Vbrx:
  723. case (int)GenericDesktop.Vbry:
  724. case (int)GenericDesktop.Vbrz:
  725. case (int)GenericDesktop.Slider:
  726. case (int)GenericDesktop.Dial:
  727. case (int)GenericDesktop.Wheel:
  728. // For axes that are *NOT* stored as signed values (which we assume are
  729. // centered on 0), put the default state in the middle between the min and max.
  730. if (!isSigned)
  731. {
  732. var defaultValue = logicalMin + (logicalMax - logicalMin) / 2;
  733. if (defaultValue != 0)
  734. return new PrimitiveValue(defaultValue);
  735. }
  736. break;
  737. }
  738. break;
  739. }
  740. return new PrimitiveValue();
  741. }
  742. internal void AddChildControls(ref HIDElementDescriptor element, string controlName, ref InputControlLayout.Builder builder)
  743. {
  744. if (usagePage == UsagePage.GenericDesktop && usage == (int)GenericDesktop.HatSwitch)
  745. {
  746. // There doesn't seem to be enough specificity in the HID spec to reliably figure this case out.
  747. // Albeit detail is scarce, we could probably make some inferences based on the unit setting
  748. // of the hat switch but even then it seems there's much left to the whims of a hardware manufacturer.
  749. // Even if we know values go clockwise (HID spec doesn't really say; probably can be inferred from unit),
  750. // which direction do we start with? Is 0 degrees up or right?
  751. //
  752. // What we do here is simply make the assumption that we're dealing with degrees here, that we go clockwise,
  753. // and that 0 degrees is up (which is actually the opposite of the coordinate system suggested in 5.9 of
  754. // of the HID spec but seems to be what manufacturers are actually using in practice). Of course, if the
  755. // device we're looking at actually sets things up differently, then we end up with either an incorrectly
  756. // oriented or (worse) a non-functional hat switch.
  757. var nullValue = DetermineDefaultState();
  758. if (nullValue.isEmpty)
  759. return;
  760. ////REVIEW: this probably only works with hatswitches that have their null value at logicalMax+1
  761. builder.AddControl(controlName + "/up")
  762. .WithFormat(InputStateBlock.FormatBit)
  763. .WithLayout("DiscreteButton")
  764. .WithParameters(string.Format(CultureInfo.InvariantCulture,
  765. "minValue={0},maxValue={1},nullValue={2},wrapAtValue={3}",
  766. logicalMax, logicalMin + 1, nullValue.ToString(), logicalMax))
  767. .WithBitOffset((uint)element.reportOffsetInBits % 8)
  768. .WithSizeInBits((uint)reportSizeInBits);
  769. builder.AddControl(controlName + "/right")
  770. .WithFormat(InputStateBlock.FormatBit)
  771. .WithLayout("DiscreteButton")
  772. .WithParameters(string.Format(CultureInfo.InvariantCulture,
  773. "minValue={0},maxValue={1}",
  774. logicalMin + 1, logicalMin + 3))
  775. .WithBitOffset((uint)element.reportOffsetInBits % 8)
  776. .WithSizeInBits((uint)reportSizeInBits);
  777. builder.AddControl(controlName + "/down")
  778. .WithFormat(InputStateBlock.FormatBit)
  779. .WithLayout("DiscreteButton")
  780. .WithParameters(string.Format(CultureInfo.InvariantCulture,
  781. "minValue={0},maxValue={1}",
  782. logicalMin + 3, logicalMin + 5))
  783. .WithBitOffset((uint)element.reportOffsetInBits % 8)
  784. .WithSizeInBits((uint)reportSizeInBits);
  785. builder.AddControl(controlName + "/left")
  786. .WithFormat(InputStateBlock.FormatBit)
  787. .WithLayout("DiscreteButton")
  788. .WithParameters(string.Format(CultureInfo.InvariantCulture,
  789. "minValue={0},maxValue={1}",
  790. logicalMin + 5, logicalMin + 7))
  791. .WithBitOffset((uint)element.reportOffsetInBits % 8)
  792. .WithSizeInBits((uint)reportSizeInBits);
  793. }
  794. }
  795. }
  796. /// <summary>
  797. /// Descriptor for a collection of HID elements.
  798. /// </summary>
  799. [Serializable]
  800. public struct HIDCollectionDescriptor
  801. {
  802. public HIDCollectionType type;
  803. public int usage;
  804. public UsagePage usagePage;
  805. public int parent; // -1 if no parent.
  806. public int childCount;
  807. public int firstChild;
  808. }
  809. /// <summary>
  810. /// HID descriptor for a HID class device.
  811. /// </summary>
  812. /// <remarks>
  813. /// This is a processed view of the combined descriptors provided by a HID as defined
  814. /// in the HID specification, i.e. it's a combination of information from the USB device
  815. /// descriptor, HID class descriptor, and HID report descriptor.
  816. /// </remarks>
  817. [Serializable]
  818. public struct HIDDeviceDescriptor
  819. {
  820. /// <summary>
  821. /// USB vendor ID.
  822. /// </summary>
  823. /// <remarks>
  824. /// To get the string version of the vendor ID, see <see cref="InputDeviceDescription.manufacturer"/>
  825. /// on <see cref="InputDevice.description"/>.
  826. /// </remarks>
  827. public int vendorId;
  828. /// <summary>
  829. /// USB product ID.
  830. /// </summary>
  831. public int productId;
  832. public int usage;
  833. public UsagePage usagePage;
  834. /// <summary>
  835. /// Maximum size of individual input reports sent by the device.
  836. /// </summary>
  837. public int inputReportSize;
  838. /// <summary>
  839. /// Maximum size of individual output reports sent to the device.
  840. /// </summary>
  841. public int outputReportSize;
  842. /// <summary>
  843. /// Maximum size of individual feature reports exchanged with the device.
  844. /// </summary>
  845. public int featureReportSize;
  846. public HIDElementDescriptor[] elements;
  847. public HIDCollectionDescriptor[] collections;
  848. public string ToJson()
  849. {
  850. return JsonUtility.ToJson(this, true);
  851. }
  852. public static HIDDeviceDescriptor FromJson(string json)
  853. {
  854. return JsonUtility.FromJson<HIDDeviceDescriptor>(json);
  855. }
  856. }
  857. /// <summary>
  858. /// Helper to quickly build descriptors for arbitrary HIDs.
  859. /// </summary>
  860. public struct HIDDeviceDescriptorBuilder
  861. {
  862. public UsagePage usagePage;
  863. public int usage;
  864. public HIDDeviceDescriptorBuilder(UsagePage usagePage, int usage)
  865. : this()
  866. {
  867. this.usagePage = usagePage;
  868. this.usage = usage;
  869. }
  870. public HIDDeviceDescriptorBuilder(GenericDesktop usage)
  871. : this(UsagePage.GenericDesktop, (int)usage)
  872. {
  873. }
  874. public HIDDeviceDescriptorBuilder StartReport(HIDReportType reportType, int reportId = 1)
  875. {
  876. m_CurrentReportId = reportId;
  877. m_CurrentReportType = reportType;
  878. m_CurrentReportOffsetInBits = 8; // Report ID.
  879. return this;
  880. }
  881. public HIDDeviceDescriptorBuilder AddElement(UsagePage usagePage, int usage, int sizeInBits)
  882. {
  883. if (m_Elements == null)
  884. {
  885. m_Elements = new List<HIDElementDescriptor>();
  886. }
  887. else
  888. {
  889. // Make sure the usage and usagePage combination is unique.
  890. foreach (var element in m_Elements)
  891. {
  892. // Skip elements that aren't in the same report.
  893. if (element.reportId != m_CurrentReportId || element.reportType != m_CurrentReportType)
  894. continue;
  895. if (element.usagePage == usagePage && element.usage == usage)
  896. throw new InvalidOperationException(
  897. $"Cannot add two elements with the same usage page '{usagePage}' and usage '0x{usage:X} the to same device");
  898. }
  899. }
  900. m_Elements.Add(new HIDElementDescriptor
  901. {
  902. usage = usage,
  903. usagePage = usagePage,
  904. reportOffsetInBits = m_CurrentReportOffsetInBits,
  905. reportSizeInBits = sizeInBits,
  906. reportType = m_CurrentReportType,
  907. reportId = m_CurrentReportId
  908. });
  909. m_CurrentReportOffsetInBits += sizeInBits;
  910. return this;
  911. }
  912. public HIDDeviceDescriptorBuilder AddElement(GenericDesktop usage, int sizeInBits)
  913. {
  914. return AddElement(UsagePage.GenericDesktop, (int)usage, sizeInBits);
  915. }
  916. public HIDDeviceDescriptorBuilder WithPhysicalMinMax(int min, int max)
  917. {
  918. var index = m_Elements.Count - 1;
  919. if (index < 0)
  920. throw new InvalidOperationException("No element has been added to the descriptor yet");
  921. var element = m_Elements[index];
  922. element.physicalMin = min;
  923. element.physicalMax = max;
  924. m_Elements[index] = element;
  925. return this;
  926. }
  927. public HIDDeviceDescriptorBuilder WithLogicalMinMax(int min, int max)
  928. {
  929. var index = m_Elements.Count - 1;
  930. if (index < 0)
  931. throw new InvalidOperationException("No element has been added to the descriptor yet");
  932. var element = m_Elements[index];
  933. element.logicalMin = min;
  934. element.logicalMax = max;
  935. m_Elements[index] = element;
  936. return this;
  937. }
  938. public HIDDeviceDescriptor Finish()
  939. {
  940. var descriptor = new HIDDeviceDescriptor
  941. {
  942. usage = usage,
  943. usagePage = usagePage,
  944. elements = m_Elements?.ToArray(),
  945. collections = m_Collections?.ToArray(),
  946. };
  947. return descriptor;
  948. }
  949. private int m_CurrentReportId;
  950. private HIDReportType m_CurrentReportType;
  951. private int m_CurrentReportOffsetInBits;
  952. private List<HIDElementDescriptor> m_Elements;
  953. private List<HIDCollectionDescriptor> m_Collections;
  954. private int m_InputReportSize;
  955. private int m_OutputReportSize;
  956. private int m_FeatureReportSize;
  957. }
  958. /// <summary>
  959. /// Enumeration of HID usage pages.
  960. /// </summary>00
  961. /// <remarks>
  962. /// Note that some of the values are actually ranges.
  963. /// </remarks>
  964. /// <seealso cref="http://www.usb.org/developers/hidpage/Hut1_12v2.pdf"/>
  965. public enum UsagePage
  966. {
  967. Undefined = 0x00,
  968. GenericDesktop = 0x01,
  969. Simulation = 0x02,
  970. VRControls = 0x03,
  971. SportControls = 0x04,
  972. GameControls = 0x05,
  973. GenericDeviceControls = 0x06,
  974. Keyboard = 0x07,
  975. LEDs = 0x08,
  976. Button = 0x09,
  977. Ordinal = 0x0A,
  978. Telephony = 0x0B,
  979. Consumer = 0x0C,
  980. Digitizer = 0x0D,
  981. PID = 0x0F,
  982. Unicode = 0x10,
  983. AlphanumericDisplay = 0x14,
  984. MedicalInstruments = 0x40,
  985. Monitor = 0x80, // Starts here and goes up to 0x83.
  986. Power = 0x84, // Starts here and goes up to 0x87.
  987. BarCodeScanner = 0x8C,
  988. MagneticStripeReader = 0x8E,
  989. Camera = 0x90,
  990. Arcade = 0x91,
  991. VendorDefined = 0xFF00, // Starts here and goes up to 0xFFFF.
  992. }
  993. /// <summary>
  994. /// Usages in the GenericDesktop HID usage page.
  995. /// </summary>
  996. /// <seealso cref="http://www.usb.org/developers/hidpage/Hut1_12v2.pdf"/>
  997. public enum GenericDesktop
  998. {
  999. Undefined = 0x00,
  1000. Pointer = 0x01,
  1001. Mouse = 0x02,
  1002. Joystick = 0x04,
  1003. Gamepad = 0x05,
  1004. Keyboard = 0x06,
  1005. Keypad = 0x07,
  1006. MultiAxisController = 0x08,
  1007. TabletPCControls = 0x09,
  1008. AssistiveControl = 0x0A,
  1009. X = 0x30,
  1010. Y = 0x31,
  1011. Z = 0x32,
  1012. Rx = 0x33,
  1013. Ry = 0x34,
  1014. Rz = 0x35,
  1015. Slider = 0x36,
  1016. Dial = 0x37,
  1017. Wheel = 0x38,
  1018. HatSwitch = 0x39,
  1019. CountedBuffer = 0x3A,
  1020. ByteCount = 0x3B,
  1021. MotionWakeup = 0x3C,
  1022. Start = 0x3D,
  1023. Select = 0x3E,
  1024. Vx = 0x40,
  1025. Vy = 0x41,
  1026. Vz = 0x42,
  1027. Vbrx = 0x43,
  1028. Vbry = 0x44,
  1029. Vbrz = 0x45,
  1030. Vno = 0x46,
  1031. FeatureNotification = 0x47,
  1032. ResolutionMultiplier = 0x48,
  1033. SystemControl = 0x80,
  1034. SystemPowerDown = 0x81,
  1035. SystemSleep = 0x82,
  1036. SystemWakeUp = 0x83,
  1037. SystemContextMenu = 0x84,
  1038. SystemMainMenu = 0x85,
  1039. SystemAppMenu = 0x86,
  1040. SystemMenuHelp = 0x87,
  1041. SystemMenuExit = 0x88,
  1042. SystemMenuSelect = 0x89,
  1043. SystemMenuRight = 0x8A,
  1044. SystemMenuLeft = 0x8B,
  1045. SystemMenuUp = 0x8C,
  1046. SystemMenuDown = 0x8D,
  1047. SystemColdRestart = 0x8E,
  1048. SystemWarmRestart = 0x8F,
  1049. DpadUp = 0x90,
  1050. DpadDown = 0x91,
  1051. DpadRight = 0x92,
  1052. DpadLeft = 0x93,
  1053. SystemDock = 0xA0,
  1054. SystemUndock = 0xA1,
  1055. SystemSetup = 0xA2,
  1056. SystemBreak = 0xA3,
  1057. SystemDebuggerBreak = 0xA4,
  1058. ApplicationBreak = 0xA5,
  1059. ApplicationDebuggerBreak = 0xA6,
  1060. SystemSpeakerMute = 0xA7,
  1061. SystemHibernate = 0xA8,
  1062. SystemDisplayInvert = 0xB0,
  1063. SystemDisplayInternal = 0xB1,
  1064. SystemDisplayExternal = 0xB2,
  1065. SystemDisplayBoth = 0xB3,
  1066. SystemDisplayDual = 0xB4,
  1067. SystemDisplayToggleIntExt = 0xB5,
  1068. SystemDisplaySwapPrimarySecondary = 0xB6,
  1069. SystemDisplayLCDAutoScale = 0xB7
  1070. }
  1071. public enum Simulation
  1072. {
  1073. Undefined = 0x00,
  1074. FlightSimulationDevice = 0x01,
  1075. AutomobileSimulationDevice = 0x02,
  1076. TankSimulationDevice = 0x03,
  1077. SpaceshipSimulationDevice = 0x04,
  1078. SubmarineSimulationDevice = 0x05,
  1079. SailingSimulationDevice = 0x06,
  1080. MotorcycleSimulationDevice = 0x07,
  1081. SportsSimulationDevice = 0x08,
  1082. AirplaneSimulationDevice = 0x09,
  1083. HelicopterSimulationDevice = 0x0A,
  1084. MagicCarpetSimulationDevice = 0x0B,
  1085. BicylcleSimulationDevice = 0x0C,
  1086. FlightControlStick = 0x20,
  1087. FlightStick = 0x21,
  1088. CyclicControl = 0x22,
  1089. CyclicTrim = 0x23,
  1090. FlightYoke = 0x24,
  1091. TrackControl = 0x25,
  1092. Aileron = 0xB0,
  1093. AileronTrim = 0xB1,
  1094. AntiTorqueControl = 0xB2,
  1095. AutopilotEnable = 0xB3,
  1096. ChaffRelease = 0xB4,
  1097. CollectiveControl = 0xB5,
  1098. DiveBreak = 0xB6,
  1099. ElectronicCountermeasures = 0xB7,
  1100. Elevator = 0xB8,
  1101. ElevatorTrim = 0xB9,
  1102. Rudder = 0xBA,
  1103. Throttle = 0xBB,
  1104. FlightCommunications = 0xBC,
  1105. FlareRelease = 0xBD,
  1106. LandingGear = 0xBE,
  1107. ToeBreak = 0xBF,
  1108. Trigger = 0xC0,
  1109. WeaponsArm = 0xC1,
  1110. WeaponsSelect = 0xC2,
  1111. WingFlaps = 0xC3,
  1112. Accelerator = 0xC4,
  1113. Brake = 0xC5,
  1114. Clutch = 0xC6,
  1115. Shifter = 0xC7,
  1116. Steering = 0xC8,
  1117. TurretDirection = 0xC9,
  1118. BarrelElevation = 0xCA,
  1119. DivePlane = 0xCB,
  1120. Ballast = 0xCC,
  1121. BicycleCrank = 0xCD,
  1122. HandleBars = 0xCE,
  1123. FrontBrake = 0xCF,
  1124. RearBrake = 0xD0
  1125. }
  1126. public enum Button
  1127. {
  1128. Undefined = 0,
  1129. Primary,
  1130. Secondary,
  1131. Tertiary
  1132. }
  1133. }
  1134. }