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"
49 #ifdef HAVE_SYS_IOCTL_H
50 #include <sys/ioctl.h>
52 #ifdef HAVE_LINUX_IOCTL_H
53 #include <linux/ioctl.h>
55 #ifdef HAVE_LINUX_JOYSTICK_H
56 #include <linux/joystick.h>
60 #define JOYDEV_NEW "/dev/input/js%d"
61 #define JOYDEV_OLD "/dev/js%d"
63 #ifdef HAVE_SYS_ERRNO_H
64 #include <sys/errno.h>
73 #include "wine/debug.h"
75 #include "wine/unicode.h"
77 WINE_DEFAULT_DEBUG_CHANNEL(joystick
);
79 #ifdef HAVE_LINUX_JOYSTICK_H
81 #define MAXJOYSTICK (JOYSTICKID2 + 30)
83 typedef struct tagWINE_JSTCK
{
86 /* Some extra info we need to make this actually work under the
88 First of all, we cannot keep closing and reopening the device file -
89 that blows away the state of the stick device, and we lose events. So, we
90 need to open the low-level device once, and close it when we are done.
92 Secondly, the event API only gives us what's changed. However, Windows apps
93 want the whole state every time, so we have to cache the data.
96 int dev
; /* Linux level device file descriptor */
106 char axesMap
[ABS_MAX
+ 1];
109 static WINE_JSTCK JSTCK_Data
[MAXJOYSTICK
];
111 /**************************************************************************
112 * JSTCK_drvGet [internal]
114 static WINE_JSTCK
*JSTCK_drvGet(DWORD_PTR dwDevID
)
118 if ((dwDevID
- (DWORD_PTR
)JSTCK_Data
) % sizeof(JSTCK_Data
[0]) != 0)
120 p
= (dwDevID
- (DWORD_PTR
)JSTCK_Data
) / sizeof(JSTCK_Data
[0]);
121 if (p
< 0 || p
>= MAXJOYSTICK
|| !((WINE_JSTCK
*)dwDevID
)->in_use
)
124 return (WINE_JSTCK
*)dwDevID
;
127 /**************************************************************************
128 * JSTCK_drvOpen [internal]
130 static LRESULT
JSTCK_drvOpen(LPSTR str
, DWORD dwIntf
)
132 if (dwIntf
>= MAXJOYSTICK
|| JSTCK_Data
[dwIntf
].in_use
)
135 JSTCK_Data
[dwIntf
].joyIntf
= dwIntf
;
136 JSTCK_Data
[dwIntf
].in_use
= 1;
137 return (LRESULT
)&JSTCK_Data
[dwIntf
];
140 /**************************************************************************
141 * JSTCK_drvClose [internal]
143 static LRESULT
JSTCK_drvClose(DWORD_PTR dwDevID
)
145 WINE_JSTCK
* jstck
= JSTCK_drvGet(dwDevID
);
165 /**************************************************************************
166 * JSTCK_OpenDevice [internal]
168 static int JSTCK_OpenDevice(WINE_JSTCK
* jstick
)
176 sprintf(buf
, JOYDEV_NEW
, jstick
->joyIntf
);
177 #ifdef HAVE_LINUX_22_JOYSTICK_API
178 flags
= O_RDONLY
| O_NONBLOCK
;
182 if ((jstick
->dev
= open(buf
, flags
)) < 0) {
183 sprintf(buf
, JOYDEV_OLD
, jstick
->joyIntf
);
184 if ((jstick
->dev
= open(buf
, flags
)) < 0)
187 #ifdef HAVE_LINUX_22_JOYSTICK_API
188 ioctl(jstick
->dev
, JSIOCGAXMAP
, jstick
->axesMap
);
194 /**************************************************************************
195 * JoyGetDevCaps [MMSYSTEM.102]
197 static LRESULT
JSTCK_GetDevCaps(DWORD_PTR dwDevID
, LPJOYCAPSW lpCaps
, DWORD dwSize
)
200 #ifdef HAVE_LINUX_22_JOYSTICK_API
204 char identString
[MAXPNAMELEN
];
208 static const WCHAR ini
[] = {'W','i','n','e',' ','J','o','y','s','t','i','c','k',' ','D','r','i','v','e','r',0};
211 if ((jstck
= JSTCK_drvGet(dwDevID
)) == NULL
)
212 return MMSYSERR_NODRIVER
;
214 #ifdef HAVE_LINUX_22_JOYSTICK_API
215 if ((dev
= JSTCK_OpenDevice(jstck
)) < 0) return JOYERR_PARMS
;
216 ioctl(dev
, JSIOCGAXES
, &nrOfAxes
);
217 ioctl(dev
, JSIOCGBUTTONS
, &nrOfButtons
);
218 ioctl(dev
, JSIOCGVERSION
, &driverVersion
);
219 ioctl(dev
, JSIOCGNAME(sizeof(identString
)), identString
);
220 TRACE("Driver: 0x%06x, Name: %s, #Axes: %d, #Buttons: %d\n",
221 driverVersion
, identString
, nrOfAxes
, nrOfButtons
);
222 lpCaps
->wMid
= MM_MICROSOFT
;
223 lpCaps
->wPid
= MM_PC_JOYSTICK
;
224 MultiByteToWideChar(CP_UNIXCP
, 0, identString
, -1, lpCaps
->szPname
, MAXPNAMELEN
);
225 lpCaps
->szPname
[MAXPNAMELEN
-1] = '\0';
227 lpCaps
->wXmax
= 0xFFFF;
229 lpCaps
->wYmax
= 0xFFFF;
231 lpCaps
->wZmax
= (nrOfAxes
>= 3) ? 0xFFFF : 0;
233 /* Half-Life won't allow you to map an axis event to things like
234 "next weapon" and "use". Linux reports the hat on my stick as
235 axis U and V. So, IFF BODGE_THE_HAT is defined, lie through our
236 teeth and say we have 32 buttons, and we will map the axes to
237 the high buttons. Really, perhaps this should be a registry entry,
238 or even a parameter to the Linux joystick driver (which would completely
239 remove the need for this.)
241 lpCaps
->wNumButtons
= 32;
243 lpCaps
->wNumButtons
= nrOfButtons
;
245 if (dwSize
== sizeof(JOYCAPSW
)) {
246 /* complete 95 structure */
248 lpCaps
->wRmax
= 0xFFFF;
250 lpCaps
->wUmax
= 0xFFFF;
252 lpCaps
->wVmax
= 0xFFFF;
253 lpCaps
->wMaxAxes
= 6; /* same as MS Joystick Driver */
254 lpCaps
->wNumAxes
= 0; /* nr of axes in use */
255 lpCaps
->wMaxButtons
= 32; /* same as MS Joystick Driver */
256 lpCaps
->szRegKey
[0] = 0;
257 lpCaps
->szOEMVxD
[0] = 0;
259 for (i
= 0; i
< nrOfAxes
; i
++) {
260 switch (jstck
->axesMap
[i
]) {
266 case 6: /* Throttle */
268 lpCaps
->wCaps
|= JOYCAPS_HASZ
;
273 lpCaps
->wCaps
|= JOYCAPS_HASR
;
277 lpCaps
->wCaps
|= JOYCAPS_HASU
;
281 lpCaps
->wCaps
|= JOYCAPS_HASV
;
283 case 16: /* Hat 0 X */
284 case 17: /* Hat 0 Y */
285 lpCaps
->wCaps
|= JOYCAPS_HASPOV
| JOYCAPS_POV4DIR
;
286 /* TODO: JOYCAPS_POVCTS handling */
289 WARN("Unknown axis %hhu(%u). Skipped.\n", jstck
->axesMap
[i
], i
);
294 lpCaps
->wMid
= MM_MICROSOFT
;
295 lpCaps
->wPid
= MM_PC_JOYSTICK
;
296 strcpyW(lpCaps
->szPname
, ini
); /* joystick product name */
298 lpCaps
->wXmax
= 0xFFFF;
300 lpCaps
->wYmax
= 0xFFFF;
303 lpCaps
->wNumButtons
= 2;
304 if (dwSize
== sizeof(JOYCAPSW
)) {
305 /* complete 95 structure */
313 lpCaps
->wMaxAxes
= 2;
314 lpCaps
->wNumAxes
= 2;
315 lpCaps
->wMaxButtons
= 4;
316 lpCaps
->szRegKey
[0] = 0;
317 lpCaps
->szOEMVxD
[0] = 0;
321 return JOYERR_NOERROR
;
324 /**************************************************************************
325 * JSTCK_GetPos [internal]
327 static LRESULT
JSTCK_GetPosEx(DWORD_PTR dwDevID
, LPJOYINFOEX lpInfo
)
331 #ifdef HAVE_LINUX_22_JOYSTICK_API
338 if ((jstck
= JSTCK_drvGet(dwDevID
)) == NULL
)
339 return MMSYSERR_NODRIVER
;
341 if ((dev
= JSTCK_OpenDevice(jstck
)) < 0) return JOYERR_PARMS
;
343 #ifdef HAVE_LINUX_22_JOYSTICK_API
344 while ((read(dev
, &ev
, sizeof(struct js_event
))) > 0) {
345 if (ev
.type
== (JS_EVENT_AXIS
)) {
346 switch (jstck
->axesMap
[ev
.number
]) {
354 case 6: /* Throttle */
367 case 16: /* Hat 0 X */
368 jstck
->pov_x
= ev
.value
;
370 case 17: /* Hat 0 Y */
371 jstck
->pov_y
= ev
.value
;
374 FIXME("Unknown joystick event '%d'\n", ev
.number
);
376 } else if (ev
.type
== (JS_EVENT_BUTTON
)) {
378 jstck
->buttons
|= (1 << ev
.number
);
379 /* FIXME: what to do for this field when
380 * multiple buttons are depressed ?
382 if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
383 lpInfo
->dwButtonNumber
= ev
.number
+ 1;
386 jstck
->buttons
&= ~(1 << ev
.number
);
389 /* EAGAIN is returned when the queue is empty */
390 if (errno
!= EAGAIN
) {
391 /* FIXME: error should not be ignored */
392 ERR("Error while reading joystick state (%s)\n", strerror(errno
));
394 /* Now, copy the cached values into Window's structure... */
395 if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
396 lpInfo
->dwButtons
= jstck
->buttons
;
397 if (lpInfo
->dwFlags
& JOY_RETURNX
)
398 lpInfo
->dwXpos
= jstck
->x
+ 32767;
399 if (lpInfo
->dwFlags
& JOY_RETURNY
)
400 lpInfo
->dwYpos
= jstck
->y
+ 32767;
401 if (lpInfo
->dwFlags
& JOY_RETURNZ
)
402 lpInfo
->dwZpos
= jstck
->z
+ 32767;
403 if (lpInfo
->dwFlags
& JOY_RETURNR
)
404 lpInfo
->dwRpos
= jstck
->r
+ 32767;
405 # ifdef BODGE_THE_HAT
406 else if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
409 lpInfo
->dwButtons
|= 1<<7;
410 else if (jstck
->r
< 0)
411 lpInfo
->dwButtons
|= 1<<8;
414 if (lpInfo
->dwFlags
& JOY_RETURNU
)
415 lpInfo
->dwUpos
= jstck
->u
+ 32767;
416 # ifdef BODGE_THE_HAT
417 else if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
420 lpInfo
->dwButtons
|= 1<<9;
421 else if (jstck
->u
< 0)
422 lpInfo
->dwButtons
|= 1<<10;
425 if (lpInfo
->dwFlags
& JOY_RETURNV
)
426 lpInfo
->dwVpos
= jstck
->v
+ 32767;
427 if (lpInfo
->dwFlags
& JOY_RETURNPOV
) {
428 if (jstck
->pov_y
> 0) {
429 if (jstck
->pov_x
< 0)
430 lpInfo
->dwPOV
= 22500; /* SW */
431 else if (jstck
->pov_x
> 0)
432 lpInfo
->dwPOV
= 13500; /* SE */
434 lpInfo
->dwPOV
= 18000; /* S, JOY_POVBACKWARD */
435 } else if (jstck
->pov_y
< 0) {
436 if (jstck
->pov_x
< 0)
437 lpInfo
->dwPOV
= 31500; /* NW */
438 else if (jstck
->pov_x
> 0)
439 lpInfo
->dwPOV
= 4500; /* NE */
441 lpInfo
->dwPOV
= 0; /* N, JOY_POVFORWARD */
442 } else if (jstck
->pov_x
< 0)
443 lpInfo
->dwPOV
= 27000; /* W, JOY_POVLEFT */
444 else if (jstck
->pov_x
> 0)
445 lpInfo
->dwPOV
= 9000; /* E, JOY_POVRIGHT */
447 lpInfo
->dwPOV
= JOY_POVCENTERED
; /* Center */
451 dev_stat
= read(dev
, &js
, sizeof(js
));
452 if (dev_stat
!= sizeof(js
)) {
453 return JOYERR_UNPLUGGED
; /* FIXME: perhaps wrong, but what should I return else ? */
457 if (lpInfo
->dwFlags
& JOY_RETURNX
)
458 lpInfo
->dwXpos
= js
.x
; /* FIXME: perhaps multiply it somehow ? */
459 if (lpInfo
->dwFlags
& JOY_RETURNY
)
460 lpInfo
->dwYpos
= js
.y
;
461 if (lpInfo
->dwFlags
& JOY_RETURNBUTTONS
)
462 lpInfo
->dwButtons
= js
.buttons
;
465 TRACE("x: %d, y: %d, z: %d, r: %d, u: %d, v: %d, buttons: 0x%04x, flags: 0x%04x (fd %d)\n",
466 lpInfo
->dwXpos
, lpInfo
->dwYpos
, lpInfo
->dwZpos
,
467 lpInfo
->dwRpos
, lpInfo
->dwUpos
, lpInfo
->dwVpos
,
468 lpInfo
->dwButtons
, lpInfo
->dwFlags
, dev
471 return JOYERR_NOERROR
;
474 /**************************************************************************
475 * JSTCK_GetPos [internal]
477 static LRESULT
JSTCK_GetPos(DWORD_PTR dwDevID
, LPJOYINFO lpInfo
)
482 memset(&ji
, 0, sizeof(ji
));
484 ji
.dwSize
= sizeof(ji
);
485 ji
.dwFlags
= JOY_RETURNX
| JOY_RETURNY
| JOY_RETURNZ
| JOY_RETURNBUTTONS
;
486 ret
= JSTCK_GetPosEx(dwDevID
, &ji
);
487 if (ret
== JOYERR_NOERROR
) {
488 lpInfo
->wXpos
= ji
.dwXpos
;
489 lpInfo
->wYpos
= ji
.dwYpos
;
490 lpInfo
->wZpos
= ji
.dwZpos
;
491 lpInfo
->wButtons
= ji
.dwButtons
;
497 /**************************************************************************
498 * DriverProc (JOYSTICK.@)
500 LRESULT CALLBACK
JSTCK_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
501 LPARAM dwParam1
, LPARAM dwParam2
)
503 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
504 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
507 case DRV_LOAD
: return 1;
508 case DRV_FREE
: return 1;
509 case DRV_OPEN
: return JSTCK_drvOpen((LPSTR
)dwParam1
, dwParam2
);
510 case DRV_CLOSE
: return JSTCK_drvClose(dwDevID
);
511 case DRV_ENABLE
: return 1;
512 case DRV_DISABLE
: return 1;
513 case DRV_QUERYCONFIGURE
: return 1;
514 case DRV_CONFIGURE
: MessageBoxA(0, "JoyStick MultiMedia Driver !", "JoyStick Driver", MB_OK
); return 1;
515 case DRV_INSTALL
: return DRVCNF_RESTART
;
516 case DRV_REMOVE
: return DRVCNF_RESTART
;
518 case JDD_GETNUMDEVS
: return 1;
519 case JDD_GETDEVCAPS
: return JSTCK_GetDevCaps(dwDevID
, (LPJOYCAPSW
)dwParam1
, dwParam2
);
520 case JDD_GETPOS
: return JSTCK_GetPos(dwDevID
, (LPJOYINFO
)dwParam1
);
521 case JDD_SETCALIBRATION
:
522 case JDD_CONFIGCHANGED
: return JOYERR_NOCANDO
;
523 case JDD_GETPOSEX
: return JSTCK_GetPosEx(dwDevID
, (LPJOYINFOEX
)dwParam1
);
525 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
531 /**************************************************************************
532 * DriverProc (JOYSTICK.@)
534 LRESULT CALLBACK
JSTCK_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
535 LPARAM dwParam1
, LPARAM dwParam2
)
537 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
538 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
547 case DRV_QUERYCONFIGURE
: return 0;
548 case DRV_CONFIGURE
: MessageBoxA(0, "JoyStick MultiMedia Driver !", "JoyStick Driver", MB_OK
); return 1;
549 case DRV_INSTALL
: return DRVCNF_RESTART
;
550 case DRV_REMOVE
: return DRVCNF_RESTART
;
552 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);