18c2ecf20Sopenharmony_ci.. _joystick-api: 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ci===================== 48c2ecf20Sopenharmony_ciProgramming Interface 58c2ecf20Sopenharmony_ci===================== 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci:Author: Ragnar Hojland Espinosa <ragnar@macula.net> - 7 Aug 1998 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ciIntroduction 108c2ecf20Sopenharmony_ci============ 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci.. important:: 138c2ecf20Sopenharmony_ci This document describes legacy ``js`` interface. Newer clients are 148c2ecf20Sopenharmony_ci encouraged to switch to the generic event (``evdev``) interface. 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ciThe 1.0 driver uses a new, event based approach to the joystick driver. 178c2ecf20Sopenharmony_ciInstead of the user program polling for the joystick values, the joystick 188c2ecf20Sopenharmony_cidriver now reports only any changes of its state. See joystick-api.txt, 198c2ecf20Sopenharmony_cijoystick.h and jstest.c included in the joystick package for more 208c2ecf20Sopenharmony_ciinformation. The joystick device can be used in either blocking or 218c2ecf20Sopenharmony_cinonblocking mode, and supports select() calls. 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciFor backward compatibility the old (v0.x) interface is still included. 248c2ecf20Sopenharmony_ciAny call to the joystick driver using the old interface will return values 258c2ecf20Sopenharmony_cithat are compatible to the old interface. This interface is still limited 268c2ecf20Sopenharmony_cito 2 axes, and applications using it usually decode only 2 buttons, although 278c2ecf20Sopenharmony_cithe driver provides up to 32. 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ciInitialization 308c2ecf20Sopenharmony_ci============== 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ciOpen the joystick device following the usual semantics (that is, with open). 338c2ecf20Sopenharmony_ciSince the driver now reports events instead of polling for changes, 348c2ecf20Sopenharmony_ciimmediately after the open it will issue a series of synthetic events 358c2ecf20Sopenharmony_ci(JS_EVENT_INIT) that you can read to obtain the initial state of the 368c2ecf20Sopenharmony_cijoystick. 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ciBy default, the device is opened in blocking mode:: 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci int fd = open ("/dev/input/js0", O_RDONLY); 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ciEvent Reading 448c2ecf20Sopenharmony_ci============= 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci:: 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci struct js_event e; 498c2ecf20Sopenharmony_ci read (fd, &e, sizeof(e)); 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ciwhere js_event is defined as:: 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci struct js_event { 548c2ecf20Sopenharmony_ci __u32 time; /* event timestamp in milliseconds */ 558c2ecf20Sopenharmony_ci __s16 value; /* value */ 568c2ecf20Sopenharmony_ci __u8 type; /* event type */ 578c2ecf20Sopenharmony_ci __u8 number; /* axis/button number */ 588c2ecf20Sopenharmony_ci }; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ciIf the read is successful, it will return sizeof(e), unless you wanted to read 618c2ecf20Sopenharmony_cimore than one event per read as described in section 3.1. 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cijs_event.type 658c2ecf20Sopenharmony_ci------------- 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ciThe possible values of ``type`` are:: 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci #define JS_EVENT_BUTTON 0x01 /* button pressed/released */ 708c2ecf20Sopenharmony_ci #define JS_EVENT_AXIS 0x02 /* joystick moved */ 718c2ecf20Sopenharmony_ci #define JS_EVENT_INIT 0x80 /* initial state of device */ 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ciAs mentioned above, the driver will issue synthetic JS_EVENT_INIT ORed 748c2ecf20Sopenharmony_cievents on open. That is, if it's issuing a INIT BUTTON event, the 758c2ecf20Sopenharmony_cicurrent type value will be:: 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci int type = JS_EVENT_BUTTON | JS_EVENT_INIT; /* 0x81 */ 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ciIf you choose not to differentiate between synthetic or real events 808c2ecf20Sopenharmony_ciyou can turn off the JS_EVENT_INIT bits:: 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci type &= ~JS_EVENT_INIT; /* 0x01 */ 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_cijs_event.number 868c2ecf20Sopenharmony_ci--------------- 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ciThe values of ``number`` correspond to the axis or button that 898c2ecf20Sopenharmony_cigenerated the event. Note that they carry separate numeration (that 908c2ecf20Sopenharmony_ciis, you have both an axis 0 and a button 0). Generally, 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci =============== ======= 938c2ecf20Sopenharmony_ci Axis number 948c2ecf20Sopenharmony_ci =============== ======= 958c2ecf20Sopenharmony_ci 1st Axis X 0 968c2ecf20Sopenharmony_ci 1st Axis Y 1 978c2ecf20Sopenharmony_ci 2nd Axis X 2 988c2ecf20Sopenharmony_ci 2nd Axis Y 3 998c2ecf20Sopenharmony_ci ...and so on 1008c2ecf20Sopenharmony_ci =============== ======= 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ciHats vary from one joystick type to another. Some can be moved in 8 1038c2ecf20Sopenharmony_cidirections, some only in 4, The driver, however, always reports a hat as two 1048c2ecf20Sopenharmony_ciindependent axis, even if the hardware doesn't allow independent movement. 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_cijs_event.value 1088c2ecf20Sopenharmony_ci-------------- 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ciFor an axis, ``value`` is a signed integer between -32767 and +32767 1118c2ecf20Sopenharmony_cirepresenting the position of the joystick along that axis. If you 1128c2ecf20Sopenharmony_cidon't read a 0 when the joystick is ``dead``, or if it doesn't span the 1138c2ecf20Sopenharmony_cifull range, you should recalibrate it (with, for example, jscal). 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ciFor a button, ``value`` for a press button event is 1 and for a release 1168c2ecf20Sopenharmony_cibutton event is 0. 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ciThough this:: 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci if (js_event.type == JS_EVENT_BUTTON) { 1218c2ecf20Sopenharmony_ci buttons_state ^= (1 << js_event.number); 1228c2ecf20Sopenharmony_ci } 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_cimay work well if you handle JS_EVENT_INIT events separately, 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci:: 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci if ((js_event.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) { 1298c2ecf20Sopenharmony_ci if (js_event.value) 1308c2ecf20Sopenharmony_ci buttons_state |= (1 << js_event.number); 1318c2ecf20Sopenharmony_ci else 1328c2ecf20Sopenharmony_ci buttons_state &= ~(1 << js_event.number); 1338c2ecf20Sopenharmony_ci } 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ciis much safer since it can't lose sync with the driver. As you would 1368c2ecf20Sopenharmony_cihave to write a separate handler for JS_EVENT_INIT events in the first 1378c2ecf20Sopenharmony_cisnippet, this ends up being shorter. 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cijs_event.time 1418c2ecf20Sopenharmony_ci------------- 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ciThe time an event was generated is stored in ``js_event.time``. It's a time 1448c2ecf20Sopenharmony_ciin milliseconds since ... well, since sometime in the past. This eases the 1458c2ecf20Sopenharmony_citask of detecting double clicks, figuring out if movement of axis and button 1468c2ecf20Sopenharmony_cipresses happened at the same time, and similar. 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciReading 1508c2ecf20Sopenharmony_ci======= 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ciIf you open the device in blocking mode, a read will block (that is, 1538c2ecf20Sopenharmony_ciwait) forever until an event is generated and effectively read. There 1548c2ecf20Sopenharmony_ciare two alternatives if you can't afford to wait forever (which is, 1558c2ecf20Sopenharmony_ciadmittedly, a long time;) 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci a) use select to wait until there's data to be read on fd, or 1588c2ecf20Sopenharmony_ci until it timeouts. There's a good example on the select(2) 1598c2ecf20Sopenharmony_ci man page. 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci b) open the device in non-blocking mode (O_NONBLOCK) 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ciO_NONBLOCK 1658c2ecf20Sopenharmony_ci---------- 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ciIf read returns -1 when reading in O_NONBLOCK mode, this isn't 1688c2ecf20Sopenharmony_cinecessarily a "real" error (check errno(3)); it can just mean there 1698c2ecf20Sopenharmony_ciare no events pending to be read on the driver queue. You should read 1708c2ecf20Sopenharmony_ciall events on the queue (that is, until you get a -1). 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ciFor example, 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci:: 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci while (1) { 1778c2ecf20Sopenharmony_ci while (read (fd, &e, sizeof(e)) > 0) { 1788c2ecf20Sopenharmony_ci process_event (e); 1798c2ecf20Sopenharmony_ci } 1808c2ecf20Sopenharmony_ci /* EAGAIN is returned when the queue is empty */ 1818c2ecf20Sopenharmony_ci if (errno != EAGAIN) { 1828c2ecf20Sopenharmony_ci /* error */ 1838c2ecf20Sopenharmony_ci } 1848c2ecf20Sopenharmony_ci /* do something interesting with processed events */ 1858c2ecf20Sopenharmony_ci } 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ciOne reason for emptying the queue is that if it gets full you'll start 1888c2ecf20Sopenharmony_cimissing events since the queue is finite, and older events will get 1898c2ecf20Sopenharmony_cioverwritten. 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ciThe other reason is that you want to know all what happened, and not 1928c2ecf20Sopenharmony_cidelay the processing till later. 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ciWhy can get the queue full? Because you don't empty the queue as 1958c2ecf20Sopenharmony_cimentioned, or because too much time elapses from one read to another 1968c2ecf20Sopenharmony_ciand too many events to store in the queue get generated. Note that 1978c2ecf20Sopenharmony_cihigh system load may contribute to space those reads even more. 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ciIf time between reads is enough to fill the queue and lose an event, 2008c2ecf20Sopenharmony_cithe driver will switch to startup mode and next time you read it, 2018c2ecf20Sopenharmony_cisynthetic events (JS_EVENT_INIT) will be generated to inform you of 2028c2ecf20Sopenharmony_cithe actual state of the joystick. 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci.. note:: 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci As of version 1.2.8, the queue is circular and able to hold 64 2088c2ecf20Sopenharmony_ci events. You can increment this size bumping up JS_BUFF_SIZE in 2098c2ecf20Sopenharmony_ci joystick.h and recompiling the driver. 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ciIn the above code, you might as well want to read more than one event 2138c2ecf20Sopenharmony_ciat a time using the typical read(2) functionality. For that, you would 2148c2ecf20Sopenharmony_cireplace the read above with something like:: 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci struct js_event mybuffer[0xff]; 2178c2ecf20Sopenharmony_ci int i = read (fd, mybuffer, sizeof(mybuffer)); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ciIn this case, read would return -1 if the queue was empty, or some 2208c2ecf20Sopenharmony_ciother value in which the number of events read would be i / 2218c2ecf20Sopenharmony_cisizeof(js_event) Again, if the buffer was full, it's a good idea to 2228c2ecf20Sopenharmony_ciprocess the events and keep reading it until you empty the driver queue. 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ciIOCTLs 2268c2ecf20Sopenharmony_ci====== 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ciThe joystick driver defines the following ioctl(2) operations:: 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci /* function 3rd arg */ 2318c2ecf20Sopenharmony_ci #define JSIOCGAXES /* get number of axes char */ 2328c2ecf20Sopenharmony_ci #define JSIOCGBUTTONS /* get number of buttons char */ 2338c2ecf20Sopenharmony_ci #define JSIOCGVERSION /* get driver version int */ 2348c2ecf20Sopenharmony_ci #define JSIOCGNAME(len) /* get identifier string char */ 2358c2ecf20Sopenharmony_ci #define JSIOCSCORR /* set correction values &js_corr */ 2368c2ecf20Sopenharmony_ci #define JSIOCGCORR /* get correction values &js_corr */ 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ciFor example, to read the number of axes:: 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci char number_of_axes; 2418c2ecf20Sopenharmony_ci ioctl (fd, JSIOCGAXES, &number_of_axes); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ciJSIOGCVERSION 2458c2ecf20Sopenharmony_ci------------- 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ciJSIOGCVERSION is a good way to check in run-time whether the running 2488c2ecf20Sopenharmony_cidriver is 1.0+ and supports the event interface. If it is not, the 2498c2ecf20Sopenharmony_ciIOCTL will fail. For a compile-time decision, you can test the 2508c2ecf20Sopenharmony_ciJS_VERSION symbol:: 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci #ifdef JS_VERSION 2538c2ecf20Sopenharmony_ci #if JS_VERSION > 0xsomething 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci 2568c2ecf20Sopenharmony_ciJSIOCGNAME 2578c2ecf20Sopenharmony_ci---------- 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ciJSIOCGNAME(len) allows you to get the name string of the joystick - the same 2608c2ecf20Sopenharmony_cias is being printed at boot time. The 'len' argument is the length of the 2618c2ecf20Sopenharmony_cibuffer provided by the application asking for the name. It is used to avoid 2628c2ecf20Sopenharmony_cipossible overrun should the name be too long:: 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci char name[128]; 2658c2ecf20Sopenharmony_ci if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0) 2668c2ecf20Sopenharmony_ci strncpy(name, "Unknown", sizeof(name)); 2678c2ecf20Sopenharmony_ci printf("Name: %s\n", name); 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ciJSIOC[SG]CORR 2718c2ecf20Sopenharmony_ci------------- 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ciFor usage on JSIOC[SG]CORR I suggest you to look into jscal.c They are 2748c2ecf20Sopenharmony_cinot needed in a normal program, only in joystick calibration software 2758c2ecf20Sopenharmony_cisuch as jscal or kcmjoy. These IOCTLs and data types aren't considered 2768c2ecf20Sopenharmony_cito be in the stable part of the API, and therefore may change without 2778c2ecf20Sopenharmony_ciwarning in following releases of the driver. 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ciBoth JSIOCSCORR and JSIOCGCORR expect &js_corr to be able to hold 2808c2ecf20Sopenharmony_ciinformation for all axis. That is, struct js_corr corr[MAX_AXIS]; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_cistruct js_corr is defined as:: 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci struct js_corr { 2858c2ecf20Sopenharmony_ci __s32 coef[8]; 2868c2ecf20Sopenharmony_ci __u16 prec; 2878c2ecf20Sopenharmony_ci __u16 type; 2888c2ecf20Sopenharmony_ci }; 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ciand ``type``:: 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci #define JS_CORR_NONE 0x00 /* returns raw values */ 2938c2ecf20Sopenharmony_ci #define JS_CORR_BROKEN 0x01 /* broken line */ 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_ciBackward compatibility 2978c2ecf20Sopenharmony_ci====================== 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ciThe 0.x joystick driver API is quite limited and its usage is deprecated. 3008c2ecf20Sopenharmony_ciThe driver offers backward compatibility, though. Here's a quick summary:: 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci struct JS_DATA_TYPE js; 3038c2ecf20Sopenharmony_ci while (1) { 3048c2ecf20Sopenharmony_ci if (read (fd, &js, JS_RETURN) != JS_RETURN) { 3058c2ecf20Sopenharmony_ci /* error */ 3068c2ecf20Sopenharmony_ci } 3078c2ecf20Sopenharmony_ci usleep (1000); 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ciAs you can figure out from the example, the read returns immediately, 3118c2ecf20Sopenharmony_ciwith the actual state of the joystick:: 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_ci struct JS_DATA_TYPE { 3148c2ecf20Sopenharmony_ci int buttons; /* immediate button state */ 3158c2ecf20Sopenharmony_ci int x; /* immediate x axis value */ 3168c2ecf20Sopenharmony_ci int y; /* immediate y axis value */ 3178c2ecf20Sopenharmony_ci }; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ciand JS_RETURN is defined as:: 3208c2ecf20Sopenharmony_ci 3218c2ecf20Sopenharmony_ci #define JS_RETURN sizeof(struct JS_DATA_TYPE) 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ciTo test the state of the buttons, 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci:: 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_ci first_button_state = js.buttons & 1; 3288c2ecf20Sopenharmony_ci second_button_state = js.buttons & 2; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ciThe axis values do not have a defined range in the original 0.x driver, 3318c2ecf20Sopenharmony_ciexcept for that the values are non-negative. The 1.2.8+ drivers use a 3328c2ecf20Sopenharmony_cifixed range for reporting the values, 1 being the minimum, 128 the 3338c2ecf20Sopenharmony_cicenter, and 255 maximum value. 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ciThe v0.8.0.2 driver also had an interface for 'digital joysticks', (now 3368c2ecf20Sopenharmony_cicalled Multisystem joysticks in this driver), under /dev/djsX. This driver 3378c2ecf20Sopenharmony_cidoesn't try to be compatible with that interface. 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ciFinal Notes 3418c2ecf20Sopenharmony_ci=========== 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci:: 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci ____/| Comments, additions, and specially corrections are welcome. 3468c2ecf20Sopenharmony_ci \ o.O| Documentation valid for at least version 1.2.8 of the joystick 3478c2ecf20Sopenharmony_ci =(_)= driver and as usual, the ultimate source for documentation is 3488c2ecf20Sopenharmony_ci U to "Use The Source Luke" or, at your convenience, Vojtech ;) 349