xref: /OK3568_Linux_fs/kernel/Documentation/input/joydev/joystick-api.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. _joystick-api:
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun=====================
4*4882a593SmuzhiyunProgramming Interface
5*4882a593Smuzhiyun=====================
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun:Author: Ragnar Hojland Espinosa <ragnar@macula.net> - 7 Aug 1998
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunIntroduction
10*4882a593Smuzhiyun============
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun.. important::
13*4882a593Smuzhiyun   This document describes legacy ``js`` interface. Newer clients are
14*4882a593Smuzhiyun   encouraged to switch to the generic event (``evdev``) interface.
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunThe 1.0 driver uses a new, event based approach to the joystick driver.
17*4882a593SmuzhiyunInstead of the user program polling for the joystick values, the joystick
18*4882a593Smuzhiyundriver now reports only any changes of its state. See joystick-api.txt,
19*4882a593Smuzhiyunjoystick.h and jstest.c included in the joystick package for more
20*4882a593Smuzhiyuninformation. The joystick device can be used in either blocking or
21*4882a593Smuzhiyunnonblocking mode, and supports select() calls.
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunFor backward compatibility the old (v0.x) interface is still included.
24*4882a593SmuzhiyunAny call to the joystick driver using the old interface will return values
25*4882a593Smuzhiyunthat are compatible to the old interface. This interface is still limited
26*4882a593Smuzhiyunto 2 axes, and applications using it usually decode only 2 buttons, although
27*4882a593Smuzhiyunthe driver provides up to 32.
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunInitialization
30*4882a593Smuzhiyun==============
31*4882a593Smuzhiyun
32*4882a593SmuzhiyunOpen the joystick device following the usual semantics (that is, with open).
33*4882a593SmuzhiyunSince the driver now reports events instead of polling for changes,
34*4882a593Smuzhiyunimmediately after the open it will issue a series of synthetic events
35*4882a593Smuzhiyun(JS_EVENT_INIT) that you can read to obtain the initial state of the
36*4882a593Smuzhiyunjoystick.
37*4882a593Smuzhiyun
38*4882a593SmuzhiyunBy default, the device is opened in blocking mode::
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun	int fd = open ("/dev/input/js0", O_RDONLY);
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun
43*4882a593SmuzhiyunEvent Reading
44*4882a593Smuzhiyun=============
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun::
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun	struct js_event e;
49*4882a593Smuzhiyun	read (fd, &e, sizeof(e));
50*4882a593Smuzhiyun
51*4882a593Smuzhiyunwhere js_event is defined as::
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun	struct js_event {
54*4882a593Smuzhiyun		__u32 time;     /* event timestamp in milliseconds */
55*4882a593Smuzhiyun		__s16 value;    /* value */
56*4882a593Smuzhiyun		__u8 type;      /* event type */
57*4882a593Smuzhiyun		__u8 number;    /* axis/button number */
58*4882a593Smuzhiyun	};
59*4882a593Smuzhiyun
60*4882a593SmuzhiyunIf the read is successful, it will return sizeof(e), unless you wanted to read
61*4882a593Smuzhiyunmore than one event per read as described in section 3.1.
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun
64*4882a593Smuzhiyunjs_event.type
65*4882a593Smuzhiyun-------------
66*4882a593Smuzhiyun
67*4882a593SmuzhiyunThe possible values of ``type`` are::
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun	#define JS_EVENT_BUTTON         0x01    /* button pressed/released */
70*4882a593Smuzhiyun	#define JS_EVENT_AXIS           0x02    /* joystick moved */
71*4882a593Smuzhiyun	#define JS_EVENT_INIT           0x80    /* initial state of device */
72*4882a593Smuzhiyun
73*4882a593SmuzhiyunAs mentioned above, the driver will issue synthetic JS_EVENT_INIT ORed
74*4882a593Smuzhiyunevents on open. That is, if it's issuing a INIT BUTTON event, the
75*4882a593Smuzhiyuncurrent type value will be::
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun	int type = JS_EVENT_BUTTON | JS_EVENT_INIT;	/* 0x81 */
78*4882a593Smuzhiyun
79*4882a593SmuzhiyunIf you choose not to differentiate between synthetic or real events
80*4882a593Smuzhiyunyou can turn off the JS_EVENT_INIT bits::
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun	type &= ~JS_EVENT_INIT;				/* 0x01 */
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun
85*4882a593Smuzhiyunjs_event.number
86*4882a593Smuzhiyun---------------
87*4882a593Smuzhiyun
88*4882a593SmuzhiyunThe values of ``number`` correspond to the axis or button that
89*4882a593Smuzhiyungenerated the event. Note that they carry separate numeration (that
90*4882a593Smuzhiyunis, you have both an axis 0 and a button 0). Generally,
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun        =============== =======
93*4882a593Smuzhiyun	Axis		number
94*4882a593Smuzhiyun        =============== =======
95*4882a593Smuzhiyun	1st Axis X	0
96*4882a593Smuzhiyun	1st Axis Y	1
97*4882a593Smuzhiyun	2nd Axis X	2
98*4882a593Smuzhiyun	2nd Axis Y	3
99*4882a593Smuzhiyun	...and so on
100*4882a593Smuzhiyun        =============== =======
101*4882a593Smuzhiyun
102*4882a593SmuzhiyunHats vary from one joystick type to another. Some can be moved in 8
103*4882a593Smuzhiyundirections, some only in 4, The driver, however, always reports a hat as two
104*4882a593Smuzhiyunindependent axis, even if the hardware doesn't allow independent movement.
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun
107*4882a593Smuzhiyunjs_event.value
108*4882a593Smuzhiyun--------------
109*4882a593Smuzhiyun
110*4882a593SmuzhiyunFor an axis, ``value`` is a signed integer between -32767 and +32767
111*4882a593Smuzhiyunrepresenting the position of the joystick along that axis. If you
112*4882a593Smuzhiyundon't read a 0 when the joystick is ``dead``, or if it doesn't span the
113*4882a593Smuzhiyunfull range, you should recalibrate it (with, for example, jscal).
114*4882a593Smuzhiyun
115*4882a593SmuzhiyunFor a button, ``value`` for a press button event is 1 and for a release
116*4882a593Smuzhiyunbutton event is 0.
117*4882a593Smuzhiyun
118*4882a593SmuzhiyunThough this::
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun	if (js_event.type == JS_EVENT_BUTTON) {
121*4882a593Smuzhiyun		buttons_state ^= (1 << js_event.number);
122*4882a593Smuzhiyun	}
123*4882a593Smuzhiyun
124*4882a593Smuzhiyunmay work well if you handle JS_EVENT_INIT events separately,
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun::
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun	if ((js_event.type & ~JS_EVENT_INIT) == JS_EVENT_BUTTON) {
129*4882a593Smuzhiyun		if (js_event.value)
130*4882a593Smuzhiyun			buttons_state |= (1 << js_event.number);
131*4882a593Smuzhiyun		else
132*4882a593Smuzhiyun			buttons_state &= ~(1 << js_event.number);
133*4882a593Smuzhiyun	}
134*4882a593Smuzhiyun
135*4882a593Smuzhiyunis much safer since it can't lose sync with the driver. As you would
136*4882a593Smuzhiyunhave to write a separate handler for JS_EVENT_INIT events in the first
137*4882a593Smuzhiyunsnippet, this ends up being shorter.
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun
140*4882a593Smuzhiyunjs_event.time
141*4882a593Smuzhiyun-------------
142*4882a593Smuzhiyun
143*4882a593SmuzhiyunThe time an event was generated is stored in ``js_event.time``. It's a time
144*4882a593Smuzhiyunin milliseconds since ... well, since sometime in the past.  This eases the
145*4882a593Smuzhiyuntask of detecting double clicks, figuring out if movement of axis and button
146*4882a593Smuzhiyunpresses happened at the same time, and similar.
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun
149*4882a593SmuzhiyunReading
150*4882a593Smuzhiyun=======
151*4882a593Smuzhiyun
152*4882a593SmuzhiyunIf you open the device in blocking mode, a read will block (that is,
153*4882a593Smuzhiyunwait) forever until an event is generated and effectively read. There
154*4882a593Smuzhiyunare two alternatives if you can't afford to wait forever (which is,
155*4882a593Smuzhiyunadmittedly, a long time;)
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun	a) use select to wait until there's data to be read on fd, or
158*4882a593Smuzhiyun	   until it timeouts. There's a good example on the select(2)
159*4882a593Smuzhiyun	   man page.
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun	b) open the device in non-blocking mode (O_NONBLOCK)
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun
164*4882a593SmuzhiyunO_NONBLOCK
165*4882a593Smuzhiyun----------
166*4882a593Smuzhiyun
167*4882a593SmuzhiyunIf read returns -1 when reading in O_NONBLOCK mode, this isn't
168*4882a593Smuzhiyunnecessarily a "real" error (check errno(3)); it can just mean there
169*4882a593Smuzhiyunare no events pending to be read on the driver queue. You should read
170*4882a593Smuzhiyunall events on the queue (that is, until you get a -1).
171*4882a593Smuzhiyun
172*4882a593SmuzhiyunFor example,
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun::
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun	while (1) {
177*4882a593Smuzhiyun		while (read (fd, &e, sizeof(e)) > 0) {
178*4882a593Smuzhiyun			process_event (e);
179*4882a593Smuzhiyun		}
180*4882a593Smuzhiyun		/* EAGAIN is returned when the queue is empty */
181*4882a593Smuzhiyun		if (errno != EAGAIN) {
182*4882a593Smuzhiyun			/* error */
183*4882a593Smuzhiyun		}
184*4882a593Smuzhiyun		/* do something interesting with processed events */
185*4882a593Smuzhiyun	}
186*4882a593Smuzhiyun
187*4882a593SmuzhiyunOne reason for emptying the queue is that if it gets full you'll start
188*4882a593Smuzhiyunmissing events since the queue is finite, and older events will get
189*4882a593Smuzhiyunoverwritten.
190*4882a593Smuzhiyun
191*4882a593SmuzhiyunThe other reason is that you want to know all what happened, and not
192*4882a593Smuzhiyundelay the processing till later.
193*4882a593Smuzhiyun
194*4882a593SmuzhiyunWhy can get the queue full? Because you don't empty the queue as
195*4882a593Smuzhiyunmentioned, or because too much time elapses from one read to another
196*4882a593Smuzhiyunand too many events to store in the queue get generated. Note that
197*4882a593Smuzhiyunhigh system load may contribute to space those reads even more.
198*4882a593Smuzhiyun
199*4882a593SmuzhiyunIf time between reads is enough to fill the queue and lose an event,
200*4882a593Smuzhiyunthe driver will switch to startup mode and next time you read it,
201*4882a593Smuzhiyunsynthetic events (JS_EVENT_INIT) will be generated to inform you of
202*4882a593Smuzhiyunthe actual state of the joystick.
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun.. note::
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun As of version 1.2.8, the queue is circular and able to hold 64
208*4882a593Smuzhiyun events. You can increment this size bumping up JS_BUFF_SIZE in
209*4882a593Smuzhiyun joystick.h and recompiling the driver.
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun
212*4882a593SmuzhiyunIn the above code, you might as well want to read more than one event
213*4882a593Smuzhiyunat a time using the typical read(2) functionality. For that, you would
214*4882a593Smuzhiyunreplace the read above with something like::
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun	struct js_event mybuffer[0xff];
217*4882a593Smuzhiyun	int i = read (fd, mybuffer, sizeof(mybuffer));
218*4882a593Smuzhiyun
219*4882a593SmuzhiyunIn this case, read would return -1 if the queue was empty, or some
220*4882a593Smuzhiyunother value in which the number of events read would be i /
221*4882a593Smuzhiyunsizeof(js_event)  Again, if the buffer was full, it's a good idea to
222*4882a593Smuzhiyunprocess the events and keep reading it until you empty the driver queue.
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun
225*4882a593SmuzhiyunIOCTLs
226*4882a593Smuzhiyun======
227*4882a593Smuzhiyun
228*4882a593SmuzhiyunThe joystick driver defines the following ioctl(2) operations::
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun				/* function			3rd arg  */
231*4882a593Smuzhiyun	#define JSIOCGAXES	/* get number of axes		char	 */
232*4882a593Smuzhiyun	#define JSIOCGBUTTONS	/* get number of buttons	char	 */
233*4882a593Smuzhiyun	#define JSIOCGVERSION	/* get driver version		int	 */
234*4882a593Smuzhiyun	#define JSIOCGNAME(len) /* get identifier string	char	 */
235*4882a593Smuzhiyun	#define JSIOCSCORR	/* set correction values	&js_corr */
236*4882a593Smuzhiyun	#define JSIOCGCORR	/* get correction values	&js_corr */
237*4882a593Smuzhiyun
238*4882a593SmuzhiyunFor example, to read the number of axes::
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun	char number_of_axes;
241*4882a593Smuzhiyun	ioctl (fd, JSIOCGAXES, &number_of_axes);
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun
244*4882a593SmuzhiyunJSIOGCVERSION
245*4882a593Smuzhiyun-------------
246*4882a593Smuzhiyun
247*4882a593SmuzhiyunJSIOGCVERSION is a good way to check in run-time whether the running
248*4882a593Smuzhiyundriver is 1.0+ and supports the event interface. If it is not, the
249*4882a593SmuzhiyunIOCTL will fail. For a compile-time decision, you can test the
250*4882a593SmuzhiyunJS_VERSION symbol::
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun	#ifdef JS_VERSION
253*4882a593Smuzhiyun	#if JS_VERSION > 0xsomething
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun
256*4882a593SmuzhiyunJSIOCGNAME
257*4882a593Smuzhiyun----------
258*4882a593Smuzhiyun
259*4882a593SmuzhiyunJSIOCGNAME(len) allows you to get the name string of the joystick - the same
260*4882a593Smuzhiyunas is being printed at boot time. The 'len' argument is the length of the
261*4882a593Smuzhiyunbuffer provided by the application asking for the name. It is used to avoid
262*4882a593Smuzhiyunpossible overrun should the name be too long::
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun	char name[128];
265*4882a593Smuzhiyun	if (ioctl(fd, JSIOCGNAME(sizeof(name)), name) < 0)
266*4882a593Smuzhiyun		strncpy(name, "Unknown", sizeof(name));
267*4882a593Smuzhiyun	printf("Name: %s\n", name);
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun
270*4882a593SmuzhiyunJSIOC[SG]CORR
271*4882a593Smuzhiyun-------------
272*4882a593Smuzhiyun
273*4882a593SmuzhiyunFor usage on JSIOC[SG]CORR I suggest you to look into jscal.c  They are
274*4882a593Smuzhiyunnot needed in a normal program, only in joystick calibration software
275*4882a593Smuzhiyunsuch as jscal or kcmjoy. These IOCTLs and data types aren't considered
276*4882a593Smuzhiyunto be in the stable part of the API, and therefore may change without
277*4882a593Smuzhiyunwarning in following releases of the driver.
278*4882a593Smuzhiyun
279*4882a593SmuzhiyunBoth JSIOCSCORR and JSIOCGCORR expect &js_corr to be able to hold
280*4882a593Smuzhiyuninformation for all axis. That is, struct js_corr corr[MAX_AXIS];
281*4882a593Smuzhiyun
282*4882a593Smuzhiyunstruct js_corr is defined as::
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun	struct js_corr {
285*4882a593Smuzhiyun		__s32 coef[8];
286*4882a593Smuzhiyun		__u16 prec;
287*4882a593Smuzhiyun		__u16 type;
288*4882a593Smuzhiyun	};
289*4882a593Smuzhiyun
290*4882a593Smuzhiyunand ``type``::
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun	#define JS_CORR_NONE            0x00    /* returns raw values */
293*4882a593Smuzhiyun	#define JS_CORR_BROKEN          0x01    /* broken line */
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun
296*4882a593SmuzhiyunBackward compatibility
297*4882a593Smuzhiyun======================
298*4882a593Smuzhiyun
299*4882a593SmuzhiyunThe 0.x joystick driver API is quite limited and its usage is deprecated.
300*4882a593SmuzhiyunThe driver offers backward compatibility, though. Here's a quick summary::
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun	struct JS_DATA_TYPE js;
303*4882a593Smuzhiyun	while (1) {
304*4882a593Smuzhiyun		if (read (fd, &js, JS_RETURN) != JS_RETURN) {
305*4882a593Smuzhiyun			/* error */
306*4882a593Smuzhiyun		}
307*4882a593Smuzhiyun		usleep (1000);
308*4882a593Smuzhiyun	}
309*4882a593Smuzhiyun
310*4882a593SmuzhiyunAs you can figure out from the example, the read returns immediately,
311*4882a593Smuzhiyunwith the actual state of the joystick::
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun	struct JS_DATA_TYPE {
314*4882a593Smuzhiyun		int buttons;    /* immediate button state */
315*4882a593Smuzhiyun		int x;          /* immediate x axis value */
316*4882a593Smuzhiyun		int y;          /* immediate y axis value */
317*4882a593Smuzhiyun	};
318*4882a593Smuzhiyun
319*4882a593Smuzhiyunand JS_RETURN is defined as::
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun	#define JS_RETURN       sizeof(struct JS_DATA_TYPE)
322*4882a593Smuzhiyun
323*4882a593SmuzhiyunTo test the state of the buttons,
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun::
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun	first_button_state  = js.buttons & 1;
328*4882a593Smuzhiyun	second_button_state = js.buttons & 2;
329*4882a593Smuzhiyun
330*4882a593SmuzhiyunThe axis values do not have a defined range in the original 0.x driver,
331*4882a593Smuzhiyunexcept for that the values are non-negative. The 1.2.8+ drivers use a
332*4882a593Smuzhiyunfixed range for reporting the values, 1 being the minimum, 128 the
333*4882a593Smuzhiyuncenter, and 255 maximum value.
334*4882a593Smuzhiyun
335*4882a593SmuzhiyunThe v0.8.0.2 driver also had an interface for 'digital joysticks', (now
336*4882a593Smuzhiyuncalled Multisystem joysticks in this driver), under /dev/djsX. This driver
337*4882a593Smuzhiyundoesn't try to be compatible with that interface.
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun
340*4882a593SmuzhiyunFinal Notes
341*4882a593Smuzhiyun===========
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun::
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun  ____/|	Comments, additions, and specially corrections are welcome.
346*4882a593Smuzhiyun  \ o.O|	Documentation valid for at least version 1.2.8 of the joystick
347*4882a593Smuzhiyun   =(_)=	driver and as usual, the ultimate source for documentation is
348*4882a593Smuzhiyun     U		to "Use The Source Luke" or, at your convenience, Vojtech ;)
349