4 * Copyright 1997 Andreas Mohr
5 * Copyright 2000 Wolfgang Schwotzer
6 * Copyright 2002 David Hagood
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * - nearly all joystick functions can be regarded as obsolete,
25 * as Linux (2.1.x) now supports extended joysticks with a completely
26 * new joystick driver interface
27 * New driver's documentation says:
28 * "For backward compatibility the old interface is still included,
29 * but will be dropped in the future."
30 * Thus we should implement the new interface and at most keep the old
31 * routines for backward compatibility.
32 * - better support of enhanced joysticks (Linux 2.2 interface is available)
33 * - support more joystick drivers (like the XInput extension)
34 * - should load joystick DLL as any other driver (instead of hardcoding)
35 * the driver's name, and load it as any low lever driver.
39 #include "wine/port.h"
41 #ifdef HAVE_LINUX_22_JOYSTICK_API
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
53 #ifdef HAVE_LINUX_IOCTL_H
54 #include <linux/ioctl.h>
56 #include <linux/joystick.h>
60 #define JOYDEV_NEW "/dev/input/js%d"
61 #define JOYDEV_OLD "/dev/js%d"
68 #include "wine/debug.h"
70 #include "wine/unicode.h"
72 WINE_DEFAULT_DEBUG_CHANNEL(joystick
);
74 #define MAXJOYSTICK (JOYSTICKID2 + 30)
76 typedef struct tagWINE_JSTCK
{
79 /* Some extra info we need to make this actually work under the
81 First of all, we cannot keep closing and reopening the device file -
82 that blows away the state of the stick device, and we lose events. So, we
83 need to open the low-level device once, and close it when we are done.
85 Secondly, the event API only gives us what's changed. However, Windows apps
86 want the whole state every time, so we have to cache the data.
89 int dev
; /* Linux level device file descriptor */
99 char axesMap
[ABS_MAX
+ 1];
102 static WINE_JSTCK JSTCK_Data
[MAXJOYSTICK
];
104 /**************************************************************************
105 * JSTCK_drvGet [internal]
107 static WINE_JSTCK
*JSTCK_drvGet(DWORD_PTR dwDevID
)
111 if ((dwDevID
- (DWORD_PTR
)JSTCK_Data
) % sizeof(JSTCK_Data
[0]) != 0)
113 p
= (dwDevID
- (DWORD_PTR
)JSTCK_Data
) / sizeof(JSTCK_Data
[0]);
114 if (p
< 0 || p
>= MAXJOYSTICK
|| !((WINE_JSTCK
*)dwDevID
)->in_use
)
117 return (WINE_JSTCK
*)dwDevID
;
120 /**************************************************************************
123 LRESULT
driver_open(LPSTR str
, DWORD dwIntf
)
125 if (dwIntf
>= MAXJOYSTICK
|| JSTCK_Data
[dwIntf
].in_use
)
128 JSTCK_Data
[dwIntf
].joyIntf
= dwIntf
;
129 JSTCK_Data
[dwIntf
].dev
= -1;
130 JSTCK_Data
[dwIntf
].in_use
= TRUE
;
131 return (LRESULT
)&JSTCK_Data
[dwIntf
];
134 /**************************************************************************
137 LRESULT
driver_close(DWORD_PTR dwDevID
)
139 WINE_JSTCK
* jstck
= JSTCK_drvGet(dwDevID
);
143 jstck
->in_use
= FALSE
;
159 /**************************************************************************
160 * JSTCK_OpenDevice [internal]
162 static int JSTCK_OpenDevice(WINE_JSTCK
* jstick
)
165 int flags
, fd
, found_ix
, i
;
166 static DWORD last_attempt
;
172 now
= GetTickCount();
173 if (now
- last_attempt
< 2000)
177 flags
= O_RDONLY
| O_NONBLOCK
;
179 /* The first joystick may not be at /dev/input/js0, find the correct
180 * first or second device. For example the driver for XBOX 360 wireless
181 * receiver creates entries starting at 20.
183 for (found_ix
= i
= 0; i
< MAXJOYSTICK
; i
++) {
184 sprintf(buf
, JOYDEV_NEW
, i
);
185 if ((fd
= open(buf
, flags
)) < 0) {
186 sprintf(buf
, JOYDEV_OLD
, i
);
187 if ((fd
= open(buf
, flags
)) < 0)
191 if (found_ix
++ == jstick
->joyIntf
)
193 TRACE("Found joystick[%d] at %s\n", jstick
->joyIntf
, buf
);
203 ioctl(jstick
->dev
, JSIOCGAXMAP
, jstick
->axesMap
);
209 /**************************************************************************
210 * JoyGetDevCaps [MMSYSTEM.102]
212 LRESULT
driver_joyGetDevCaps(DWORD_PTR dwDevID
, LPJOYCAPSW lpCaps
, DWORD dwSize
)
218 char identString
[MAXPNAMELEN
];
222 if ((jstck
= JSTCK_drvGet(dwDevID
)) == NULL
)
223 return MMSYSERR_NODRIVER
;
225 if ((dev
= JSTCK_OpenDevice(jstck
)) < 0) return JOYERR_PARMS
;
226 ioctl(dev
, JSIOCGAXES
, &nrOfAxes
);
227 ioctl(dev
, JSIOCGBUTTONS
, &nrOfButtons
);
228 ioctl(dev
, JSIOCGVERSION
, &driverVersion
);
229 ioctl(dev
, JSIOCGNAME(sizeof(identString
)), identString
);
230 TRACE("Driver: 0x%06x, Name: %s, #Axes: %d, #Buttons: %d\n",
231 driverVersion
, identString
, nrOfAxes
, nrOfButtons
);
232 lpCaps
->wMid
= MM_MICROSOFT
;
233 lpCaps
->wPid
= MM_PC_JOYSTICK
;
234 MultiByteToWideChar(CP_UNIXCP
, 0, identString
, -1, lpCaps
->szPname
, MAXPNAMELEN
);
235 lpCaps
->szPname
[MAXPNAMELEN
-1] = '\0';
237 lpCaps
->wXmax
= 0xFFFF;
239 lpCaps
->wYmax
= 0xFFFF;
241 lpCaps
->wZmax
= (nrOfAxes
>= 3) ? 0xFFFF : 0;
243 /* Half-Life won't allow you to map an axis event to things like
244 "next weapon" and "use". Linux reports the hat on my stick as
245 axis U and V. So, IFF BODGE_THE_HAT is defined, lie through our
246 teeth and say we have 32 buttons, and we will map the axes to
247 the high buttons. Really, perhaps this should be a registry entry,
248 or even a parameter to the Linux joystick driver (which would completely
249 remove the need for this.)
251 lpCaps
->wNumButtons
= 32;
253 lpCaps
->wNumButtons
= nrOfButtons
;
255 if (dwSize
== sizeof(JOYCAPSW
)) {
256 /* complete 95 structure */
258 lpCaps
->wRmax
= 0xFFFF;
260 lpCaps
->wUmax
= 0xFFFF;
262 lpCaps
->wVmax
= 0xFFFF;
263 lpCaps
->wMaxAxes
= 6; /* same as MS Joystick Driver */
264 lpCaps
->wNumAxes
= 0; /* nr of axes in use */
265 lpCaps
->wMaxButtons
= 32; /* same as MS Joystick Driver */
266 lpCaps
->szRegKey
[0] = 0;
267 lpCaps
->szOEMVxD
[0] = 0;
269 for (i
= 0; i
< nrOfAxes
; i
++) {
270 switch (jstck
->axesMap
[i
]) {
278 case 6: /* Throttle */
281 lpCaps
->wCaps
|= JOYCAPS_HASZ
;
286 lpCaps
->wCaps
|= JOYCAPS_HASR
;
290 lpCaps
->wCaps
|= JOYCAPS_HASU
;
294 lpCaps
->wCaps
|= JOYCAPS_HASV
;
296 case 16: /* Hat 0 X */
297 case 17: /* Hat 0 Y */
298 lpCaps
->wCaps
|= JOYCAPS_HASPOV
| JOYCAPS_POV4DIR
;
299 /* TODO: JOYCAPS_POVCTS handling */
302 WARN("Unknown axis %hhu(%u). Skipped.\n", jstck
->axesMap
[i
], i
);
307 return JOYERR_NOERROR
;
310 /**************************************************************************
313 LRESULT
driver_joyGetPosEx(DWORD_PTR dwDevID
, LPJOYINFOEX lpInfo
)
319 if ((jstck
= JSTCK_drvGet(dwDevID
)) == NULL
)
320 return MMSYSERR_NODRIVER
;
322 if ((dev
= JSTCK_OpenDevice(jstck
)) < 0) return JOYERR_PARMS
;
324 while ((read(dev
, &ev
, sizeof(struct js_event
))) > 0) {
325 if (ev
.type
== (JS_EVENT_AXIS
)) {
326 switch (jstck
->axesMap
[ev
.number
]) {
336 case 6: /* Throttle */
350 case 16: /* Hat 0 X */
351 jstck
->pov_x
= ev
.value
;
353 case 17: /* Hat 0 Y */
354 jstck
->pov_y
= ev
.value
;
357 FIXME("Unknown joystick event '%d'\n", ev
.number
);
359 } else if (ev
.type
== (JS_EVENT_BUTTON
)) {
361 jstck
->buttons
|= (1 << ev
.number
);
362 /* FIXME: what to do for this field when
363 * multiple buttons are depressed ?
365 if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
366 lpInfo
->dwButtonNumber
= ev
.number
+ 1;
369 jstck
->buttons
&= ~(1 << ev
.number
);
372 /* EAGAIN is returned when the queue is empty */
373 if (errno
!= EAGAIN
) {
374 /* FIXME: error should not be ignored */
375 ERR("Error while reading joystick state (%s)\n", strerror(errno
));
377 /* Now, copy the cached values into Window's structure... */
378 if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
379 lpInfo
->dwButtons
= jstck
->buttons
;
380 if (lpInfo
->dwFlags
& JOY_RETURNX
)
381 lpInfo
->dwXpos
= jstck
->x
+ 32767;
382 if (lpInfo
->dwFlags
& JOY_RETURNY
)
383 lpInfo
->dwYpos
= jstck
->y
+ 32767;
384 if (lpInfo
->dwFlags
& JOY_RETURNZ
)
385 lpInfo
->dwZpos
= jstck
->z
+ 32767;
386 if (lpInfo
->dwFlags
& JOY_RETURNR
)
387 lpInfo
->dwRpos
= jstck
->r
+ 32767;
388 # ifdef BODGE_THE_HAT
389 else if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
392 lpInfo
->dwButtons
|= 1<<7;
393 else if (jstck
->r
< 0)
394 lpInfo
->dwButtons
|= 1<<8;
397 if (lpInfo
->dwFlags
& JOY_RETURNU
)
398 lpInfo
->dwUpos
= jstck
->u
+ 32767;
399 # ifdef BODGE_THE_HAT
400 else if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
403 lpInfo
->dwButtons
|= 1<<9;
404 else if (jstck
->u
< 0)
405 lpInfo
->dwButtons
|= 1<<10;
408 if (lpInfo
->dwFlags
& JOY_RETURNV
)
409 lpInfo
->dwVpos
= jstck
->v
+ 32767;
410 if (lpInfo
->dwFlags
& JOY_RETURNPOV
) {
411 if (jstck
->pov_y
> 0) {
412 if (jstck
->pov_x
< 0)
413 lpInfo
->dwPOV
= 22500; /* SW */
414 else if (jstck
->pov_x
> 0)
415 lpInfo
->dwPOV
= 13500; /* SE */
417 lpInfo
->dwPOV
= 18000; /* S, JOY_POVBACKWARD */
418 } else if (jstck
->pov_y
< 0) {
419 if (jstck
->pov_x
< 0)
420 lpInfo
->dwPOV
= 31500; /* NW */
421 else if (jstck
->pov_x
> 0)
422 lpInfo
->dwPOV
= 4500; /* NE */
424 lpInfo
->dwPOV
= 0; /* N, JOY_POVFORWARD */
425 } else if (jstck
->pov_x
< 0)
426 lpInfo
->dwPOV
= 27000; /* W, JOY_POVLEFT */
427 else if (jstck
->pov_x
> 0)
428 lpInfo
->dwPOV
= 9000; /* E, JOY_POVRIGHT */
430 lpInfo
->dwPOV
= JOY_POVCENTERED
; /* Center */
433 TRACE("x: %d, y: %d, z: %d, r: %d, u: %d, v: %d, buttons: 0x%04x, flags: 0x%04x (fd %d)\n",
434 lpInfo
->dwXpos
, lpInfo
->dwYpos
, lpInfo
->dwZpos
,
435 lpInfo
->dwRpos
, lpInfo
->dwUpos
, lpInfo
->dwVpos
,
436 lpInfo
->dwButtons
, lpInfo
->dwFlags
, dev
439 return JOYERR_NOERROR
;
442 /**************************************************************************
445 LRESULT
driver_joyGetPos(DWORD_PTR dwDevID
, LPJOYINFO lpInfo
)
450 memset(&ji
, 0, sizeof(ji
));
452 ji
.dwSize
= sizeof(ji
);
453 ji
.dwFlags
= JOY_RETURNX
| JOY_RETURNY
| JOY_RETURNZ
| JOY_RETURNBUTTONS
;
454 ret
= driver_joyGetPosEx(dwDevID
, &ji
);
455 if (ret
== JOYERR_NOERROR
) {
456 lpInfo
->wXpos
= ji
.dwXpos
;
457 lpInfo
->wYpos
= ji
.dwYpos
;
458 lpInfo
->wZpos
= ji
.dwZpos
;
459 lpInfo
->wButtons
= ji
.dwButtons
;
465 #endif /* HAVE_LINUX_JOYSTICK_H */