joystick-api.txt 9.5 KB

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