joystick-api.txt 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. Joystick API Documentation -*-Text-*-
  2. Ragnar Hojland Espinosa
  3. <ragnar@macula.net>
  4. 7 Aug 1998
  5. 1. Initialization
  6. ~~~~~~~~~~~~~~~~~
  7. Open the joystick device following the usual semantics (that is, with open).
  8. Since the driver now reports events instead of polling for changes,
  9. immediately after the open it will issue a series of synthetic events
  10. (JS_EVENT_INIT) that you can read to check the initial state of the
  11. joystick.
  12. By default, the device is opened in blocking mode.
  13. int fd = open ("/dev/input/js0", O_RDONLY);
  14. 2. Event Reading
  15. ~~~~~~~~~~~~~~~~
  16. struct js_event e;
  17. read (fd, &e, sizeof(e));
  18. where js_event is defined as
  19. struct js_event {
  20. __u32 time; /* event timestamp in milliseconds */
  21. __s16 value; /* value */
  22. __u8 type; /* event type */
  23. __u8 number; /* axis/button number */
  24. };
  25. If the read is successful, it will return sizeof(e), unless you wanted to read
  26. more than one event per read as described in section 3.1.
  27. 2.1 js_event.type
  28. ~~~~~~~~~~~~~~~~~
  29. The possible values of ``type'' are
  30. #define JS_EVENT_BUTTON 0x01 /* button pressed/released */
  31. #define JS_EVENT_AXIS 0x02 /* joystick moved */
  32. #define JS_EVENT_INIT 0x80 /* initial state of device */
  33. As mentioned above, the driver will issue synthetic JS_EVENT_INIT ORed
  34. events on open. That is, if it's issuing a INIT BUTTON event, the
  35. current type value will be
  36. int type = JS_EVENT_BUTTON | JS_EVENT_INIT; /* 0x81 */
  37. If you choose not to differentiate between synthetic or real events
  38. you can turn off the JS_EVENT_INIT bits
  39. type &= ~JS_EVENT_INIT; /* 0x01 */
  40. 2.2 js_event.number
  41. ~~~~~~~~~~~~~~~~~~~
  42. The values of ``number'' correspond to the axis or button that
  43. generated the event. Note that they carry separate numeration (that
  44. is, you have both an axis 0 and a button 0). Generally,
  45. number
  46. 1st Axis X 0
  47. 1st Axis Y 1
  48. 2nd Axis X 2
  49. 2nd Axis Y 3
  50. ...and so on
  51. Hats vary from one joystick type to another. Some can be moved in 8
  52. directions, some only in 4, The driver, however, always reports a hat as two
  53. independent axis, even if the hardware doesn't allow independent movement.
  54. 2.3 js_event.value
  55. ~~~~~~~~~~~~~~~~~~
  56. For an axis, ``value'' is a signed integer between -32767 and +32767
  57. representing the position of the joystick along that axis. If you
  58. don't read a 0 when the joystick is `dead', or if it doesn't span the
  59. full range, you should recalibrate it (with, for example, jscal).
  60. For a button, ``value'' for a press button event is 1 and for a release
  61. button event is 0.
  62. Though this
  63. if (js_event.type == JS_EVENT_BUTTON) {
  64. buttons_state ^= (1 << js_event.number);
  65. }
  66. may work well if you handle JS_EVENT_INIT events separately,
  67. if ((js_event.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) {
  68. if (js_event.value)
  69. buttons_state |= (1 << js_event.number);
  70. else
  71. buttons_state &= ~(1 << js_event.number);
  72. }
  73. is much safer since it can't lose sync with the driver. As you would
  74. have to write a separate handler for JS_EVENT_INIT events in the first
  75. snippet, this ends up being shorter.
  76. 2.4 js_event.time
  77. ~~~~~~~~~~~~~~~~~
  78. The time an event was generated is stored in ``js_event.time''. It's a time
  79. in milliseconds since ... well, since sometime in the past. This eases the
  80. task of detecting double clicks, figuring out if movement of axis and button
  81. presses happened at the same time, and similar.
  82. 3. Reading
  83. ~~~~~~~~~~
  84. If you open the device in blocking mode, a read will block (that is,
  85. wait) forever until an event is generated and effectively read. There
  86. are two alternatives if you can't afford to wait forever (which is,
  87. admittedly, a long time;)
  88. a) use select to wait until there's data to be read on fd, or
  89. until it timeouts. There's a good example on the select(2)
  90. man page.
  91. b) open the device in non-blocking mode (O_NONBLOCK)
  92. 3.1 O_NONBLOCK
  93. ~~~~~~~~~~~~~~
  94. If read returns -1 when reading in O_NONBLOCK mode, this isn't
  95. necessarily a "real" error (check errno(3)); it can just mean there
  96. are no events pending to be read on the driver queue. You should read
  97. all events on the queue (that is, until you get a -1).
  98. For example,
  99. while (1) {
  100. while (read (fd, &e, sizeof(e)) > 0) {
  101. process_event (e);
  102. }
  103. /* EAGAIN is returned when the queue is empty */
  104. if (errno != EAGAIN) {
  105. /* error */
  106. }
  107. /* do something interesting with processed events */
  108. }
  109. One reason for emptying the queue is that if it gets full you'll start
  110. missing events since the queue is finite, and older events will get
  111. overwritten.
  112. The other reason is that you want to know all what happened, and not
  113. delay the processing till later.
  114. Why can get the queue full? Because you don't empty the queue as
  115. mentioned, or because too much time elapses from one read to another
  116. and too many events to store in the queue get generated. Note that
  117. high system load may contribute to space those reads even more.
  118. If time between reads is enough to fill the queue and lose an event,
  119. the driver will switch to startup mode and next time you read it,
  120. synthetic events (JS_EVENT_INIT) will be generated to inform you of
  121. the actual state of the joystick.
  122. [As for version 1.2.8, the queue is circular and able to hold 64
  123. events. You can increment this size bumping up JS_BUFF_SIZE in
  124. joystick.h and recompiling the driver.]
  125. In the above code, you might as well want to read more than one event
  126. at a time using the typical read(2) functionality. For that, you would
  127. replace the read above with something like
  128. struct js_event mybuffer[0xff];
  129. int i = read (fd, mybuffer, sizeof(mybuffer));
  130. In this case, read would return -1 if the queue was empty, or some
  131. other value in which the number of events read would be i /
  132. sizeof(js_event) Again, if the buffer was full, it's a good idea to
  133. process the events and keep reading it until you empty the driver queue.
  134. 4. IOCTLs
  135. ~~~~~~~~~
  136. The joystick driver defines the following ioctl(2) operations.
  137. /* function 3rd arg */
  138. #define JSIOCGAXES /* get number of axes char */
  139. #define JSIOCGBUTTONS /* get number of buttons char */
  140. #define JSIOCGVERSION /* get driver version int */
  141. #define JSIOCGNAME(len) /* get identifier string char */
  142. #define JSIOCSCORR /* set correction values &js_corr */
  143. #define JSIOCGCORR /* get correction values &js_corr */
  144. For example, to read the number of axes
  145. char number_of_axes;
  146. ioctl (fd, JSIOCGAXES, &number_of_axes);
  147. 4.1 JSIOGCVERSION
  148. ~~~~~~~~~~~~~~~~~
  149. JSIOGCVERSION is a good way to check in run-time whether the running
  150. driver is 1.0+ and supports the event interface. If it is not, the
  151. IOCTL will fail. For a compile-time decision, you can test the
  152. JS_VERSION symbol
  153. #ifdef JS_VERSION
  154. #if JS_VERSION > 0xsomething
  155. 4.2 JSIOCGNAME
  156. ~~~~~~~~~~~~~~
  157. JSIOCGNAME(len) allows you to get the name string of the joystick - the same
  158. as is being printed at boot time. The 'len' argument is the length of the
  159. buffer provided by the application asking for the name. It is used to avoid
  160. possible overrun should the name be too long.
  161. char name[128];
  162. if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
  163. strncpy(name, "Unknown", sizeof(name));
  164. printf("Name: %s\n", name);
  165. 4.3 JSIOC[SG]CORR
  166. ~~~~~~~~~~~~~~~~~
  167. For usage on JSIOC[SG]CORR I suggest you to look into jscal.c They are
  168. not needed in a normal program, only in joystick calibration software
  169. such as jscal or kcmjoy. These IOCTLs and data types aren't considered
  170. to be in the stable part of the API, and therefore may change without
  171. warning in following releases of the driver.
  172. Both JSIOCSCORR and JSIOCGCORR expect &js_corr to be able to hold
  173. information for all axis. That is, struct js_corr corr[MAX_AXIS];
  174. struct js_corr is defined as
  175. struct js_corr {
  176. __s32 coef[8];
  177. __u16 prec;
  178. __u16 type;
  179. };
  180. and ``type''
  181. #define JS_CORR_NONE 0x00 /* returns raw values */
  182. #define JS_CORR_BROKEN 0x01 /* broken line */
  183. 5. Backward compatibility
  184. ~~~~~~~~~~~~~~~~~~~~~~~~~
  185. The 0.x joystick driver API is quite limited and its usage is deprecated.
  186. The driver offers backward compatibility, though. Here's a quick summary:
  187. struct JS_DATA_TYPE js;
  188. while (1) {
  189. if (read (fd, &js, JS_RETURN) != JS_RETURN) {
  190. /* error */
  191. }
  192. usleep (1000);
  193. }
  194. As you can figure out from the example, the read returns immediately,
  195. with the actual state of the joystick.
  196. struct JS_DATA_TYPE {
  197. int buttons; /* immediate button state */
  198. int x; /* immediate x axis value */
  199. int y; /* immediate y axis value */
  200. };
  201. and JS_RETURN is defined as
  202. #define JS_RETURN sizeof(struct JS_DATA_TYPE)
  203. To test the state of the buttons,
  204. first_button_state = js.buttons & 1;
  205. second_button_state = js.buttons & 2;
  206. The axis values do not have a defined range in the original 0.x driver,
  207. except for that the values are non-negative. The 1.2.8+ drivers use a
  208. fixed range for reporting the values, 1 being the minimum, 128 the
  209. center, and 255 maximum value.
  210. The v0.8.0.2 driver also had an interface for 'digital joysticks', (now
  211. called Multisystem joysticks in this driver), under /dev/djsX. This driver
  212. doesn't try to be compatible with that interface.
  213. 6. Final Notes
  214. ~~~~~~~~~~~~~~
  215. ____/| Comments, additions, and specially corrections are welcome.
  216. \ o.O| Documentation valid for at least version 1.2.8 of the joystick
  217. =(_)= driver and as usual, the ultimate source for documentation is
  218. U to "Use The Source Luke" or, at your convenience, Vojtech ;)
  219. - Ragnar
  220. EOF