Source code of Windows XP (NT5)
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.

321 lines
11 KiB

  1. <HEAD>
  2. <TITLE>DirectInput Input Semantics</TITLE>
  3. </HEAD>
  4. <BODY BGCOLOR=#FFFFFF TEXT=#000000 LINK=#000000 VLINK=#808080 ALINK=#000000>
  5. </BODY>
  6. <H2>DirectInput Input Semantics</H2>
  7. <ADDRESS>
  8. Raymond Chen<br>
  9. Microsoft Corporation<br>
  10. 7 November 1997
  11. </ADDRESS>
  12. <h3>Abstract</h3>
  13. <p>
  14. A common problem faced by gaming device manufacturers is ensuring that
  15. applications use the gaming device in the best possible way.
  16. There is currently no way for a device to express high-level semantic
  17. information that can be used by an application to assign, for example,
  18. proper motion semantics to each input control.
  19. A solution to this problem is proposed.
  20. <h3>The Variety of Hardware</h3>
  21. <p>
  22. High degree-of-freedom devices are growing in popularity.
  23. These types of devices reveal a failing in the way axes are currently
  24. assigned: Different applications interpret (for example) the X axis
  25. differently. Flight simulator type applications typically interpret
  26. the X axis as a bank/roll control. Driving games and first-person shooting
  27. games, on the other hand, typically interpret it as a turning control.
  28. And still other types of games might interpret it as controlling
  29. left/right translation ("sliding").
  30. <p>
  31. Currently, hardware vendors include a software component which dynamically
  32. reconfigures the hardware based on guesses as to the semantics expected
  33. by the currently-running application.
  34. This technique is error-prone and leaves the hardware vendor in a constant
  35. game of catch-up, releasing updates to the software to accomodate
  36. new games. Consequently, it is not suitable as a long-term solution.
  37. <h3>The HID Approach</h3>
  38. <p>
  39. The USB HID committee attempted to address this problem by defining
  40. "usages" which express information about the intended use of the device.
  41. Unfortunately, this approach has a few failings:
  42. <ul>
  43. <li>Hardware vendors need to express capabilities generically in order
  44. for applications to be able to use them.
  45. <a href=hid.htm>A separate document</a>
  46. describes the usages a gaming device must use in order to be
  47. usable by the greatest number of applications.
  48. <li>The list of HID usages describe what things are, rather than what
  49. they are for. For example, the Generic/Y usage merely indicates
  50. that the control is used for Y movement of unspecified type,
  51. but not whether it should be use for dive/climb control,
  52. vertical translation, forward translation, or even jump/crouch.
  53. </ul>
  54. <h3>Semantics</h3>
  55. <p>
  56. This document proposes a new concept, tentatively named "Semantics".
  57. (Suggestions for alternate names welcome. "Behavior" is a possibility,
  58. although it tends to imply some sort of AI.)
  59. <p>
  60. A semantic expresses what application behavior should result from
  61. the user's operation of the control.
  62. <p>
  63. A list of semantics would be agreed to by the gaming community.
  64. <h3>How devices express semantics</h3>
  65. <p>
  66. A hardware device can express the semantics that can be applied
  67. to each control, in descending order of priority. For example, the
  68. X axis on a joystick could be listed as
  69. <ul>
  70. <li>horizontal turning (yaw) - Preferred use
  71. <li>clockwise/counter-clockwise rotation (bank/roll)
  72. <li>horizontal translation (slide)
  73. </ul>
  74. This information would be recorded in the registry "type key" associated
  75. with the device. (Information of this ilk is already kept in the type key.)
  76. The INF file distributed with a hardware device would establish these
  77. semantics. In the absence of semantic information, DirectInput would
  78. apply a default set of semantics.
  79. <h3>How Applications Request Semantics</h3>
  80. <p>
  81. One of the goals of this proposal is to shift the onus of establishing
  82. mappings between game behaviors and device controls
  83. from the application to DirectInput.
  84. Doing this would accomplish several things.
  85. <ul>
  86. <li>Applications would become easier to write,
  87. since less code would be necessary.
  88. <li>Applications would become easier for the user to customize,
  89. since a central configuration (e.g., control panel) can
  90. establish the way all applications treat a device.
  91. <li>Applications would be able to take advantage of devices
  92. that ship after the application has been released.
  93. <li>Backwards compatibility would be maintained,
  94. since DirectInput knows the identity of the application
  95. (and its version) and can therefore modify its behavior
  96. as necessary.
  97. </ul>
  98. <p>
  99. According to this proposal, the application would present to DirectInput
  100. a list of semantics it desires from the device. Example:
  101. <pre>
  102. LPDIDATAFORMAT pdf;
  103. HRESULT hres;
  104. DWORD rgSemantics[] = {
  105. DISEM_AXIS_BANK , /* Bank/roll control */
  106. DISEM_AXIS_CLIMB , /* Climb/dive control */
  107. DISEM_AXIS_THROTTLE , /* Throttle (velocity) control */
  108. DISEM_BUTTON_FIREWEAPON , /* Fire selected weapon */
  109. DISEM_BUTTON_WEAPONSELECTUP , /* Select next weapon */
  110. DISEM_BUTTON_WEAPONSELECTDOWN, /* Select previous weapon */
  111. DISEM_BUTTON_SHOWMAP , /* Show/hide onscreen map */
  112. DISEM_BUTTON_ANY , /* For special game feature */
  113. };
  114. hres = pdev-&gt;BuildDataFormat(rgSemantics, &pdf);
  115. if (SUCCEEDED(hres)) {
  116. hres = pdev-&gt;SetDataFormat(pdf);
  117. pdev-&gt;FreeDataFormat(pdf);
  118. }
  119. </pre>
  120. Observe that this is a simple extension of what applications already do,
  121. except that instead of using a fixed data format, the application asks
  122. DirectInput to build a custom data format based on its semantics requirements.
  123. <p>
  124. The <code>rgSemantics</code> array describes the controls which the
  125. application requests.
  126. The
  127. <code>BuildDataFormat</code> method compares this structure against
  128. the capabilities of the device and determines how semantics should be
  129. assigned to controls.
  130. Note the special semantic named <code>DISEM_BUTTON_ANY</code> which acts
  131. as a catch-all that matches any button (just like in the old days).
  132. <p>
  133. The application can look to see which semantics got assigned to which
  134. controls (or perhaps to no control if all compatible controls are already
  135. in use) by inspecting the <code>DIDATAFORMAT</code> structure. For example,
  136. the above sample application could check if DirectInput successfully found
  137. a control for use as a throttle as follows:
  138. <pre>
  139. // Do this before pdev->FreeDataFormat(pdf), of course
  140. //
  141. // rgSemantics[2] = DISEM_AXIS_THROTTLE, corresponding to rgodf[2]
  142. if (!pdf->rgodf[2].dwType) {
  143. // Unable to find a throttle on the device
  144. }
  145. </pre>
  146. Most game applications provide keyboard equivalents for all functions, so
  147. there would typically be no need for checking if a particular semantic
  148. was supported on the gaming device.
  149. <p>
  150. This is merely the basic idea; there are a lot of details that are not
  151. covered. For example, if the application selected
  152. <code>DISEM_POV_GLANCE</code> to request a control that can be used to
  153. glance around the environment (turning the head without turning the body),
  154. this can be expressed in a device either with a single control (a hatswitch)
  155. or with a pair of controls (two axes), a quartet of controls (four buttons
  156. arranged in a diamond pattern) or even a quintet (a diamond pattern with a
  157. center button). It is also not clear how relative and absolute controls
  158. should be managed.
  159. <p>
  160. One approach
  161. is to add a translation layer to the data retrieval functions
  162. as well as to the data format functions. So the application can assume
  163. that it will always receive the information as two <code>LONG</code>s
  164. (say), one describing horizontal glance information and one describing
  165. vertical glance information. DirectInput would do the work of mapping
  166. the hatswitch or buttons into <code>LONG</code>s. However, this
  167. leaves open the question of what numerical value to assign to a glance
  168. action triggered by a button rather than an axis.
  169. <p>
  170. Another approach is to split <code>BuildDataFormat</code> into two
  171. separate methods, <code>CreateEmptyDataFormat</code> and
  172. <code>AddToDataFormat</code>. An application can then, for example,
  173. use the following code to select the best control for glancing:
  174. <pre>
  175. hres = pdev-&gt;CreateEmptyDataFormat(&pdf);
  176. if (FAILED(hres)) {
  177. goto panic;
  178. }
  179. //
  180. // 1 = number of semantics we are adding this time
  181. // DIADF_ALL = fail if not all semantics are available
  182. //
  183. DWORD dwSem = DISEM_POV_GLANCE;
  184. hres = pdev-&gt;AddToDataFormat(pdf, &dwSem, 1, DIADF_ALL);
  185. if (SUCCEEDED(hres)) {
  186. GlanceViaPOV = TRUE;
  187. } else {
  188. // Couldn't find it on a POV; try it on a pair of axes
  189. DWORD rgSem[2] = { DISEM_AXIS_GLANCEUPDOWN,
  190. DISEM_AXIS_GLANCELEFTRIGHT };
  191. hres = pdev-&gt;AddToDataFormat(pdf, rgSem, 2, DIADF_ALL);
  192. if (SUCCEEDED(hres)) {
  193. GlanceViaAxes = TRUE;
  194. } else {
  195. // If I were really into it, I could try glancing via buttons
  196. }
  197. }
  198. </pre>
  199. The downside is that this requires vendors to write code, which
  200. results in the impression that "DirectInput is hard to use".
  201. (Be honest, that's what you were thinking when you saw that code
  202. snippet.)
  203. <h3>Associating Semantics to Controls</h3>
  204. <p>
  205. Via the Control Panel, the end-user can adjust the list of semantics
  206. associated with each control. This would be a simple list control
  207. with a "Move Up/Down" control (to reorder items) and "Add" and
  208. "Remove" buttons to allow the user to change the contents of the list.
  209. <h3>The Mapping Algorithm</h3>
  210. <p>
  211. For each control requested by the application, consult the list of
  212. all controls on the device not already assigned by a previous step.
  213. Among the controls which support the requested semantics, choose the
  214. one whose semantics appears earliest in its corresponding list.
  215. For example, if there are two axes that claim to be usable as
  216. "Translate left/right", but one of them lists the capability as its
  217. primary behavior, whereas the other lists it in third place, then the
  218. behavior will be assigned to the first axis.
  219. <h3>The List of Semantics</h3>
  220. <p>
  221. The list of semantics would be agreed upon by the members of the gaming
  222. community. Here follows a list of possibilities.
  223. <ul>
  224. <li>Axis motion
  225. <ul>
  226. <li>Translation ("slide", no rotation).
  227. <li>Rotation ("turn", no translation)
  228. <li>Tilt ("lean", no change in position -- same as rotation?)
  229. </ul>
  230. <li>Axis actions
  231. <ul>
  232. <li>Jump/Crouch
  233. <li>Zoom in/out
  234. <li>Throttle
  235. <li>Left throttle, right throttle (tank games)
  236. <li>Various turret controls (tank games)
  237. </ul>
  238. <li>Button actions
  239. <ul>
  240. <li>Weapon operations
  241. <ul>
  242. <li>Select first weapon (second, third, etc.), next, previous
  243. <li>Fire first weapon (second, third, etc.)
  244. <li>Fire selected weapon
  245. </ul>
  246. <li>Character operations
  247. <ul>
  248. <li>Select first object (second, third, etc.), next, previous
  249. <li>Manipulate first object (second, third, etc.)
  250. <li>Manipulate selected object
  251. <li>Jump
  252. <li>Crouch
  253. <li>Zoom in
  254. <li>Zoom out
  255. </ul>
  256. <li>Fighting operations
  257. <ul>
  258. <li>Kick (various intensities), punch (various intensities)
  259. </ul>
  260. </ul>
  261. <li>Flight simulation
  262. <ul>
  263. <li>Gear, flaps, trim, etc.
  264. </ul>
  265. </ul>
  266. <h3>References</h3>
  267. <p>
  268. <cite>
  269. <a href=http://www.usb.org/>Universal Serial Bus</a>
  270. HID Usage Tables</cite>, Version 1.0,
  271. USB Implementers Forum.
  272. <p>
  273. <cite>
  274. <a href=http://www.microsoft.com/directx/resources/dx5sdk.htm>
  275. DirectX 5.0 SDK</a>
  276. </cite>, Microsoft Corporation.