Back to home page

OSCL-LXR

 
 

    


0001 .. _joystick-api:
0002 
0003 =====================
0004 Programming Interface
0005 =====================
0006 
0007 :Author: Ragnar Hojland Espinosa <ragnar@macula.net> - 7 Aug 1998
0008 
0009 Introduction
0010 ============
0011 
0012 .. important::
0013    This document describes legacy ``js`` interface. Newer clients are
0014    encouraged to switch to the generic event (``evdev``) interface.
0015 
0016 The 1.0 driver uses a new, event based approach to the joystick driver.
0017 Instead of the user program polling for the joystick values, the joystick
0018 driver now reports only any changes of its state. See joystick-api.txt,
0019 joystick.h and jstest.c included in the joystick package for more
0020 information. The joystick device can be used in either blocking or
0021 nonblocking mode, and supports select() calls.
0022 
0023 For backward compatibility the old (v0.x) interface is still included.
0024 Any call to the joystick driver using the old interface will return values
0025 that are compatible to the old interface. This interface is still limited
0026 to 2 axes, and applications using it usually decode only 2 buttons, although
0027 the driver provides up to 32.
0028 
0029 Initialization
0030 ==============
0031 
0032 Open the joystick device following the usual semantics (that is, with open).
0033 Since the driver now reports events instead of polling for changes,
0034 immediately after the open it will issue a series of synthetic events
0035 (JS_EVENT_INIT) that you can read to obtain the initial state of the
0036 joystick.
0037 
0038 By default, the device is opened in blocking mode::
0039 
0040         int fd = open ("/dev/input/js0", O_RDONLY);
0041 
0042 
0043 Event Reading
0044 =============
0045 
0046 ::
0047 
0048         struct js_event e;
0049         read (fd, &e, sizeof(e));
0050 
0051 where js_event is defined as::
0052 
0053         struct js_event {
0054                 __u32 time;     /* event timestamp in milliseconds */
0055                 __s16 value;    /* value */
0056                 __u8 type;      /* event type */
0057                 __u8 number;    /* axis/button number */
0058         };
0059 
0060 If the read is successful, it will return sizeof(e), unless you wanted to read
0061 more than one event per read as described in section 3.1.
0062 
0063 
0064 js_event.type
0065 -------------
0066 
0067 The possible values of ``type`` are::
0068 
0069         #define JS_EVENT_BUTTON         0x01    /* button pressed/released */
0070         #define JS_EVENT_AXIS           0x02    /* joystick moved */
0071         #define JS_EVENT_INIT           0x80    /* initial state of device */
0072 
0073 As mentioned above, the driver will issue synthetic JS_EVENT_INIT ORed
0074 events on open. That is, if it's issuing an INIT BUTTON event, the
0075 current type value will be::
0076 
0077         int type = JS_EVENT_BUTTON | JS_EVENT_INIT;     /* 0x81 */
0078 
0079 If you choose not to differentiate between synthetic or real events
0080 you can turn off the JS_EVENT_INIT bits::
0081 
0082         type &= ~JS_EVENT_INIT;                         /* 0x01 */
0083 
0084 
0085 js_event.number
0086 ---------------
0087 
0088 The values of ``number`` correspond to the axis or button that
0089 generated the event. Note that they carry separate numeration (that
0090 is, you have both an axis 0 and a button 0). Generally,
0091 
0092         =============== =======
0093         Axis            number
0094         =============== =======
0095         1st Axis X      0
0096         1st Axis Y      1
0097         2nd Axis X      2
0098         2nd Axis Y      3
0099         ...and so on
0100         =============== =======
0101 
0102 Hats vary from one joystick type to another. Some can be moved in 8
0103 directions, some only in 4. The driver, however, always reports a hat as two
0104 independent axes, even if the hardware doesn't allow independent movement.
0105 
0106 
0107 js_event.value
0108 --------------
0109 
0110 For an axis, ``value`` is a signed integer between -32767 and +32767
0111 representing the position of the joystick along that axis. If you
0112 don't read a 0 when the joystick is ``dead``, or if it doesn't span the
0113 full range, you should recalibrate it (with, for example, jscal).
0114 
0115 For a button, ``value`` for a press button event is 1 and for a release
0116 button event is 0.
0117 
0118 Though this::
0119 
0120         if (js_event.type == JS_EVENT_BUTTON) {
0121                 buttons_state ^= (1 << js_event.number);
0122         }
0123 
0124 may work well if you handle JS_EVENT_INIT events separately,
0125 
0126 ::
0127 
0128         if ((js_event.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) {
0129                 if (js_event.value)
0130                         buttons_state |= (1 << js_event.number);
0131                 else
0132                         buttons_state &= ~(1 << js_event.number);
0133         }
0134 
0135 is much safer since it can't lose sync with the driver. As you would
0136 have to write a separate handler for JS_EVENT_INIT events in the first
0137 snippet, this ends up being shorter.
0138 
0139 
0140 js_event.time
0141 -------------
0142 
0143 The time an event was generated is stored in ``js_event.time``. It's a time
0144 in milliseconds since ... well, since sometime in the past.  This eases the
0145 task of detecting double clicks, figuring out if movement of axis and button
0146 presses happened at the same time, and similar.
0147 
0148 
0149 Reading
0150 =======
0151 
0152 If you open the device in blocking mode, a read will block (that is,
0153 wait) forever until an event is generated and effectively read. There
0154 are two alternatives if you can't afford to wait forever (which is,
0155 admittedly, a long time;)
0156 
0157         a) use select to wait until there's data to be read on fd, or
0158            until it timeouts. There's a good example on the select(2)
0159            man page.
0160 
0161         b) open the device in non-blocking mode (O_NONBLOCK)
0162 
0163 
0164 O_NONBLOCK
0165 ----------
0166 
0167 If read returns -1 when reading in O_NONBLOCK mode, this isn't
0168 necessarily a "real" error (check errno(3)); it can just mean there
0169 are no events pending to be read on the driver queue. You should read
0170 all events on the queue (that is, until you get a -1).
0171 
0172 For example,
0173 
0174 ::
0175 
0176         while (1) {
0177                 while (read (fd, &e, sizeof(e)) > 0) {
0178                         process_event (e);
0179                 }
0180                 /* EAGAIN is returned when the queue is empty */
0181                 if (errno != EAGAIN) {
0182                         /* error */
0183                 }
0184                 /* do something interesting with processed events */
0185         }
0186 
0187 One reason for emptying the queue is that if it gets full you'll start
0188 missing events since the queue is finite, and older events will get
0189 overwritten.
0190 
0191 The other reason is that you want to know all that happened, and not
0192 delay the processing till later.
0193 
0194 Why can the queue get full? Because you don't empty the queue as
0195 mentioned, or because too much time elapses from one read to another
0196 and too many events to store in the queue get generated. Note that
0197 high system load may contribute to space those reads even more.
0198 
0199 If time between reads is enough to fill the queue and lose an event,
0200 the driver will switch to startup mode and next time you read it,
0201 synthetic events (JS_EVENT_INIT) will be generated to inform you of
0202 the actual state of the joystick.
0203 
0204 
0205 .. note::
0206 
0207  As of version 1.2.8, the queue is circular and able to hold 64
0208  events. You can increment this size bumping up JS_BUFF_SIZE in
0209  joystick.h and recompiling the driver.
0210 
0211 
0212 In the above code, you might as well want to read more than one event
0213 at a time using the typical read(2) functionality. For that, you would
0214 replace the read above with something like::
0215 
0216         struct js_event mybuffer[0xff];
0217         int i = read (fd, mybuffer, sizeof(mybuffer));
0218 
0219 In this case, read would return -1 if the queue was empty, or some
0220 other value in which the number of events read would be i /
0221 sizeof(js_event)  Again, if the buffer was full, it's a good idea to
0222 process the events and keep reading it until you empty the driver queue.
0223 
0224 
0225 IOCTLs
0226 ======
0227 
0228 The joystick driver defines the following ioctl(2) operations::
0229 
0230                                 /* function                     3rd arg  */
0231         #define JSIOCGAXES      /* get number of axes           char     */
0232         #define JSIOCGBUTTONS   /* get number of buttons        char     */
0233         #define JSIOCGVERSION   /* get driver version           int      */
0234         #define JSIOCGNAME(len) /* get identifier string        char     */
0235         #define JSIOCSCORR      /* set correction values        &js_corr */
0236         #define JSIOCGCORR      /* get correction values        &js_corr */
0237 
0238 For example, to read the number of axes::
0239 
0240         char number_of_axes;
0241         ioctl (fd, JSIOCGAXES, &number_of_axes);
0242 
0243 
0244 JSIOGCVERSION
0245 -------------
0246 
0247 JSIOGCVERSION is a good way to check in run-time whether the running
0248 driver is 1.0+ and supports the event interface. If it is not, the
0249 IOCTL will fail. For a compile-time decision, you can test the
0250 JS_VERSION symbol::
0251 
0252         #ifdef JS_VERSION
0253         #if JS_VERSION > 0xsomething
0254 
0255 
0256 JSIOCGNAME
0257 ----------
0258 
0259 JSIOCGNAME(len) allows you to get the name string of the joystick - the same
0260 as is being printed at boot time. The 'len' argument is the length of the
0261 buffer provided by the application asking for the name. It is used to avoid
0262 possible overrun should the name be too long::
0263 
0264         char name[128];
0265         if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
0266                 strscpy(name, "Unknown", sizeof(name));
0267         printf("Name: %s\n", name);
0268 
0269 
0270 JSIOC[SG]CORR
0271 -------------
0272 
0273 For usage on JSIOC[SG]CORR I suggest you to look into jscal.c  They are
0274 not needed in a normal program, only in joystick calibration software
0275 such as jscal or kcmjoy. These IOCTLs and data types aren't considered
0276 to be in the stable part of the API, and therefore may change without
0277 warning in following releases of the driver.
0278 
0279 Both JSIOCSCORR and JSIOCGCORR expect &js_corr to be able to hold
0280 information for all axes. That is, struct js_corr corr[MAX_AXIS];
0281 
0282 struct js_corr is defined as::
0283 
0284         struct js_corr {
0285                 __s32 coef[8];
0286                 __u16 prec;
0287                 __u16 type;
0288         };
0289 
0290 and ``type``::
0291 
0292         #define JS_CORR_NONE            0x00    /* returns raw values */
0293         #define JS_CORR_BROKEN          0x01    /* broken line */
0294 
0295 
0296 Backward compatibility
0297 ======================
0298 
0299 The 0.x joystick driver API is quite limited and its usage is deprecated.
0300 The driver offers backward compatibility, though. Here's a quick summary::
0301 
0302         struct JS_DATA_TYPE js;
0303         while (1) {
0304                 if (read (fd, &js, JS_RETURN) != JS_RETURN) {
0305                         /* error */
0306                 }
0307                 usleep (1000);
0308         }
0309 
0310 As you can figure out from the example, the read returns immediately,
0311 with the actual state of the joystick::
0312 
0313         struct JS_DATA_TYPE {
0314                 int buttons;    /* immediate button state */
0315                 int x;          /* immediate x axis value */
0316                 int y;          /* immediate y axis value */
0317         };
0318 
0319 and JS_RETURN is defined as::
0320 
0321         #define JS_RETURN       sizeof(struct JS_DATA_TYPE)
0322 
0323 To test the state of the buttons,
0324 
0325 ::
0326 
0327         first_button_state  = js.buttons & 1;
0328         second_button_state = js.buttons & 2;
0329 
0330 The axis values do not have a defined range in the original 0.x driver,
0331 except that the values are non-negative. The 1.2.8+ drivers use a
0332 fixed range for reporting the values, 1 being the minimum, 128 the
0333 center, and 255 maximum value.
0334 
0335 The v0.8.0.2 driver also had an interface for 'digital joysticks', (now
0336 called Multisystem joysticks in this driver), under /dev/djsX. This driver
0337 doesn't try to be compatible with that interface.
0338 
0339 
0340 Final Notes
0341 ===========
0342 
0343 ::
0344 
0345   ____/|        Comments, additions, and specially corrections are welcome.
0346   \ o.O|        Documentation valid for at least version 1.2.8 of the joystick
0347    =(_)=        driver and as usual, the ultimate source for documentation is
0348      U          to "Use The Source Luke" or, at your convenience, Vojtech ;)