162306a36Sopenharmony_ci.. _joystick-api: 262306a36Sopenharmony_ci 362306a36Sopenharmony_ci===================== 462306a36Sopenharmony_ciProgramming Interface 562306a36Sopenharmony_ci===================== 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci:Author: Ragnar Hojland Espinosa <ragnar@macula.net> - 7 Aug 1998 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciIntroduction 1062306a36Sopenharmony_ci============ 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci.. important:: 1362306a36Sopenharmony_ci This document describes legacy ``js`` interface. Newer clients are 1462306a36Sopenharmony_ci encouraged to switch to the generic event (``evdev``) interface. 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciThe 1.0 driver uses a new, event based approach to the joystick driver. 1762306a36Sopenharmony_ciInstead of the user program polling for the joystick values, the joystick 1862306a36Sopenharmony_cidriver now reports only any changes of its state. See joystick-api.txt, 1962306a36Sopenharmony_cijoystick.h and jstest.c included in the joystick package for more 2062306a36Sopenharmony_ciinformation. The joystick device can be used in either blocking or 2162306a36Sopenharmony_cinonblocking mode, and supports select() calls. 2262306a36Sopenharmony_ci 2362306a36Sopenharmony_ciFor backward compatibility the old (v0.x) interface is still included. 2462306a36Sopenharmony_ciAny call to the joystick driver using the old interface will return values 2562306a36Sopenharmony_cithat are compatible to the old interface. This interface is still limited 2662306a36Sopenharmony_cito 2 axes, and applications using it usually decode only 2 buttons, although 2762306a36Sopenharmony_cithe driver provides up to 32. 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ciInitialization 3062306a36Sopenharmony_ci============== 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_ciOpen the joystick device following the usual semantics (that is, with open). 3362306a36Sopenharmony_ciSince the driver now reports events instead of polling for changes, 3462306a36Sopenharmony_ciimmediately after the open it will issue a series of synthetic events 3562306a36Sopenharmony_ci(JS_EVENT_INIT) that you can read to obtain the initial state of the 3662306a36Sopenharmony_cijoystick. 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ciBy default, the device is opened in blocking mode:: 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci int fd = open ("/dev/input/js0", O_RDONLY); 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci 4362306a36Sopenharmony_ciEvent Reading 4462306a36Sopenharmony_ci============= 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci:: 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ci struct js_event e; 4962306a36Sopenharmony_ci read (fd, &e, sizeof(e)); 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ciwhere js_event is defined as:: 5262306a36Sopenharmony_ci 5362306a36Sopenharmony_ci struct js_event { 5462306a36Sopenharmony_ci __u32 time; /* event timestamp in milliseconds */ 5562306a36Sopenharmony_ci __s16 value; /* value */ 5662306a36Sopenharmony_ci __u8 type; /* event type */ 5762306a36Sopenharmony_ci __u8 number; /* axis/button number */ 5862306a36Sopenharmony_ci }; 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ciIf the read is successful, it will return sizeof(e), unless you wanted to read 6162306a36Sopenharmony_cimore than one event per read as described in section 3.1. 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_cijs_event.type 6562306a36Sopenharmony_ci------------- 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ciThe possible values of ``type`` are:: 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci #define JS_EVENT_BUTTON 0x01 /* button pressed/released */ 7062306a36Sopenharmony_ci #define JS_EVENT_AXIS 0x02 /* joystick moved */ 7162306a36Sopenharmony_ci #define JS_EVENT_INIT 0x80 /* initial state of device */ 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ciAs mentioned above, the driver will issue synthetic JS_EVENT_INIT ORed 7462306a36Sopenharmony_cievents on open. That is, if it's issuing an INIT BUTTON event, the 7562306a36Sopenharmony_cicurrent type value will be:: 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci int type = JS_EVENT_BUTTON | JS_EVENT_INIT; /* 0x81 */ 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ciIf you choose not to differentiate between synthetic or real events 8062306a36Sopenharmony_ciyou can turn off the JS_EVENT_INIT bits:: 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_ci type &= ~JS_EVENT_INIT; /* 0x01 */ 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_cijs_event.number 8662306a36Sopenharmony_ci--------------- 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ciThe values of ``number`` correspond to the axis or button that 8962306a36Sopenharmony_cigenerated the event. Note that they carry separate numeration (that 9062306a36Sopenharmony_ciis, you have both an axis 0 and a button 0). Generally, 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci =============== ======= 9362306a36Sopenharmony_ci Axis number 9462306a36Sopenharmony_ci =============== ======= 9562306a36Sopenharmony_ci 1st Axis X 0 9662306a36Sopenharmony_ci 1st Axis Y 1 9762306a36Sopenharmony_ci 2nd Axis X 2 9862306a36Sopenharmony_ci 2nd Axis Y 3 9962306a36Sopenharmony_ci ...and so on 10062306a36Sopenharmony_ci =============== ======= 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_ciHats vary from one joystick type to another. Some can be moved in 8 10362306a36Sopenharmony_cidirections, some only in 4. The driver, however, always reports a hat as two 10462306a36Sopenharmony_ciindependent axes, even if the hardware doesn't allow independent movement. 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_cijs_event.value 10862306a36Sopenharmony_ci-------------- 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ciFor an axis, ``value`` is a signed integer between -32767 and +32767 11162306a36Sopenharmony_cirepresenting the position of the joystick along that axis. If you 11262306a36Sopenharmony_cidon't read a 0 when the joystick is ``dead``, or if it doesn't span the 11362306a36Sopenharmony_cifull range, you should recalibrate it (with, for example, jscal). 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ciFor a button, ``value`` for a press button event is 1 and for a release 11662306a36Sopenharmony_cibutton event is 0. 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ciThough this:: 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_ci if (js_event.type == JS_EVENT_BUTTON) { 12162306a36Sopenharmony_ci buttons_state ^= (1 << js_event.number); 12262306a36Sopenharmony_ci } 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_cimay work well if you handle JS_EVENT_INIT events separately, 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ci:: 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_ci if ((js_event.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) { 12962306a36Sopenharmony_ci if (js_event.value) 13062306a36Sopenharmony_ci buttons_state |= (1 << js_event.number); 13162306a36Sopenharmony_ci else 13262306a36Sopenharmony_ci buttons_state &= ~(1 << js_event.number); 13362306a36Sopenharmony_ci } 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ciis much safer since it can't lose sync with the driver. As you would 13662306a36Sopenharmony_cihave to write a separate handler for JS_EVENT_INIT events in the first 13762306a36Sopenharmony_cisnippet, this ends up being shorter. 13862306a36Sopenharmony_ci 13962306a36Sopenharmony_ci 14062306a36Sopenharmony_cijs_event.time 14162306a36Sopenharmony_ci------------- 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ciThe time an event was generated is stored in ``js_event.time``. It's a time 14462306a36Sopenharmony_ciin milliseconds since ... well, since sometime in the past. This eases the 14562306a36Sopenharmony_citask of detecting double clicks, figuring out if movement of axis and button 14662306a36Sopenharmony_cipresses happened at the same time, and similar. 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci 14962306a36Sopenharmony_ciReading 15062306a36Sopenharmony_ci======= 15162306a36Sopenharmony_ci 15262306a36Sopenharmony_ciIf you open the device in blocking mode, a read will block (that is, 15362306a36Sopenharmony_ciwait) forever until an event is generated and effectively read. There 15462306a36Sopenharmony_ciare two alternatives if you can't afford to wait forever (which is, 15562306a36Sopenharmony_ciadmittedly, a long time;) 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci a) use select to wait until there's data to be read on fd, or 15862306a36Sopenharmony_ci until it timeouts. There's a good example on the select(2) 15962306a36Sopenharmony_ci man page. 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci b) open the device in non-blocking mode (O_NONBLOCK) 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci 16462306a36Sopenharmony_ciO_NONBLOCK 16562306a36Sopenharmony_ci---------- 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ciIf read returns -1 when reading in O_NONBLOCK mode, this isn't 16862306a36Sopenharmony_cinecessarily a "real" error (check errno(3)); it can just mean there 16962306a36Sopenharmony_ciare no events pending to be read on the driver queue. You should read 17062306a36Sopenharmony_ciall events on the queue (that is, until you get a -1). 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ciFor example, 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ci:: 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci while (1) { 17762306a36Sopenharmony_ci while (read (fd, &e, sizeof(e)) > 0) { 17862306a36Sopenharmony_ci process_event (e); 17962306a36Sopenharmony_ci } 18062306a36Sopenharmony_ci /* EAGAIN is returned when the queue is empty */ 18162306a36Sopenharmony_ci if (errno != EAGAIN) { 18262306a36Sopenharmony_ci /* error */ 18362306a36Sopenharmony_ci } 18462306a36Sopenharmony_ci /* do something interesting with processed events */ 18562306a36Sopenharmony_ci } 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ciOne reason for emptying the queue is that if it gets full you'll start 18862306a36Sopenharmony_cimissing events since the queue is finite, and older events will get 18962306a36Sopenharmony_cioverwritten. 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ciThe other reason is that you want to know all that happened, and not 19262306a36Sopenharmony_cidelay the processing till later. 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ciWhy can the queue get full? Because you don't empty the queue as 19562306a36Sopenharmony_cimentioned, or because too much time elapses from one read to another 19662306a36Sopenharmony_ciand too many events to store in the queue get generated. Note that 19762306a36Sopenharmony_cihigh system load may contribute to space those reads even more. 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ciIf time between reads is enough to fill the queue and lose an event, 20062306a36Sopenharmony_cithe driver will switch to startup mode and next time you read it, 20162306a36Sopenharmony_cisynthetic events (JS_EVENT_INIT) will be generated to inform you of 20262306a36Sopenharmony_cithe actual state of the joystick. 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci.. note:: 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci As of version 1.2.8, the queue is circular and able to hold 64 20862306a36Sopenharmony_ci events. You can increment this size bumping up JS_BUFF_SIZE in 20962306a36Sopenharmony_ci joystick.h and recompiling the driver. 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ciIn the above code, you might as well want to read more than one event 21362306a36Sopenharmony_ciat a time using the typical read(2) functionality. For that, you would 21462306a36Sopenharmony_cireplace the read above with something like:: 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci struct js_event mybuffer[0xff]; 21762306a36Sopenharmony_ci int i = read (fd, mybuffer, sizeof(mybuffer)); 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ciIn this case, read would return -1 if the queue was empty, or some 22062306a36Sopenharmony_ciother value in which the number of events read would be i / 22162306a36Sopenharmony_cisizeof(js_event) Again, if the buffer was full, it's a good idea to 22262306a36Sopenharmony_ciprocess the events and keep reading it until you empty the driver queue. 22362306a36Sopenharmony_ci 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ciIOCTLs 22662306a36Sopenharmony_ci====== 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ciThe joystick driver defines the following ioctl(2) operations:: 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ci /* function 3rd arg */ 23162306a36Sopenharmony_ci #define JSIOCGAXES /* get number of axes char */ 23262306a36Sopenharmony_ci #define JSIOCGBUTTONS /* get number of buttons char */ 23362306a36Sopenharmony_ci #define JSIOCGVERSION /* get driver version int */ 23462306a36Sopenharmony_ci #define JSIOCGNAME(len) /* get identifier string char */ 23562306a36Sopenharmony_ci #define JSIOCSCORR /* set correction values &js_corr */ 23662306a36Sopenharmony_ci #define JSIOCGCORR /* get correction values &js_corr */ 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ciFor example, to read the number of axes:: 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci char number_of_axes; 24162306a36Sopenharmony_ci ioctl (fd, JSIOCGAXES, &number_of_axes); 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ciJSIOGCVERSION 24562306a36Sopenharmony_ci------------- 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ciJSIOGCVERSION is a good way to check in run-time whether the running 24862306a36Sopenharmony_cidriver is 1.0+ and supports the event interface. If it is not, the 24962306a36Sopenharmony_ciIOCTL will fail. For a compile-time decision, you can test the 25062306a36Sopenharmony_ciJS_VERSION symbol:: 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci #ifdef JS_VERSION 25362306a36Sopenharmony_ci #if JS_VERSION > 0xsomething 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ci 25662306a36Sopenharmony_ciJSIOCGNAME 25762306a36Sopenharmony_ci---------- 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ciJSIOCGNAME(len) allows you to get the name string of the joystick - the same 26062306a36Sopenharmony_cias is being printed at boot time. The 'len' argument is the length of the 26162306a36Sopenharmony_cibuffer provided by the application asking for the name. It is used to avoid 26262306a36Sopenharmony_cipossible overrun should the name be too long:: 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci char name[128]; 26562306a36Sopenharmony_ci if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0) 26662306a36Sopenharmony_ci strscpy(name, "Unknown", sizeof(name)); 26762306a36Sopenharmony_ci printf("Name: %s\n", name); 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci 27062306a36Sopenharmony_ciJSIOC[SG]CORR 27162306a36Sopenharmony_ci------------- 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ciFor usage on JSIOC[SG]CORR I suggest you to look into jscal.c They are 27462306a36Sopenharmony_cinot needed in a normal program, only in joystick calibration software 27562306a36Sopenharmony_cisuch as jscal or kcmjoy. These IOCTLs and data types aren't considered 27662306a36Sopenharmony_cito be in the stable part of the API, and therefore may change without 27762306a36Sopenharmony_ciwarning in following releases of the driver. 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ciBoth JSIOCSCORR and JSIOCGCORR expect &js_corr to be able to hold 28062306a36Sopenharmony_ciinformation for all axes. That is, struct js_corr corr[MAX_AXIS]; 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_cistruct js_corr is defined as:: 28362306a36Sopenharmony_ci 28462306a36Sopenharmony_ci struct js_corr { 28562306a36Sopenharmony_ci __s32 coef[8]; 28662306a36Sopenharmony_ci __u16 prec; 28762306a36Sopenharmony_ci __u16 type; 28862306a36Sopenharmony_ci }; 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ciand ``type``:: 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci #define JS_CORR_NONE 0x00 /* returns raw values */ 29362306a36Sopenharmony_ci #define JS_CORR_BROKEN 0x01 /* broken line */ 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ciBackward compatibility 29762306a36Sopenharmony_ci====================== 29862306a36Sopenharmony_ci 29962306a36Sopenharmony_ciThe 0.x joystick driver API is quite limited and its usage is deprecated. 30062306a36Sopenharmony_ciThe driver offers backward compatibility, though. Here's a quick summary:: 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci struct JS_DATA_TYPE js; 30362306a36Sopenharmony_ci while (1) { 30462306a36Sopenharmony_ci if (read (fd, &js, JS_RETURN) != JS_RETURN) { 30562306a36Sopenharmony_ci /* error */ 30662306a36Sopenharmony_ci } 30762306a36Sopenharmony_ci usleep (1000); 30862306a36Sopenharmony_ci } 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ciAs you can figure out from the example, the read returns immediately, 31162306a36Sopenharmony_ciwith the actual state of the joystick:: 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_ci struct JS_DATA_TYPE { 31462306a36Sopenharmony_ci int buttons; /* immediate button state */ 31562306a36Sopenharmony_ci int x; /* immediate x axis value */ 31662306a36Sopenharmony_ci int y; /* immediate y axis value */ 31762306a36Sopenharmony_ci }; 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ciand JS_RETURN is defined as:: 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci #define JS_RETURN sizeof(struct JS_DATA_TYPE) 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_ciTo test the state of the buttons, 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci:: 32662306a36Sopenharmony_ci 32762306a36Sopenharmony_ci first_button_state = js.buttons & 1; 32862306a36Sopenharmony_ci second_button_state = js.buttons & 2; 32962306a36Sopenharmony_ci 33062306a36Sopenharmony_ciThe axis values do not have a defined range in the original 0.x driver, 33162306a36Sopenharmony_ciexcept that the values are non-negative. The 1.2.8+ drivers use a 33262306a36Sopenharmony_cifixed range for reporting the values, 1 being the minimum, 128 the 33362306a36Sopenharmony_cicenter, and 255 maximum value. 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ciThe v0.8.0.2 driver also had an interface for 'digital joysticks', (now 33662306a36Sopenharmony_cicalled Multisystem joysticks in this driver), under /dev/djsX. This driver 33762306a36Sopenharmony_cidoesn't try to be compatible with that interface. 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_ciFinal Notes 34162306a36Sopenharmony_ci=========== 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci:: 34462306a36Sopenharmony_ci 34562306a36Sopenharmony_ci ____/| Comments, additions, and specially corrections are welcome. 34662306a36Sopenharmony_ci \ o.O| Documentation valid for at least version 1.2.8 of the joystick 34762306a36Sopenharmony_ci =(_)= driver and as usual, the ultimate source for documentation is 34862306a36Sopenharmony_ci U to "Use The Source Luke" or, at your convenience, Vojtech ;) 349