4 * Copyright 1997 Andreas Mohr
6 * nearly all joystick functions can be regarded as obsolete,
7 * as Linux (2.1.x) now supports extended joysticks
8 * with a completely new joystick driver interface
9 * new driver's docu says:
10 * "For backward compatibility the old interface is still included,
11 * but will be dropped in the future."
12 * Thus we should implement the new interface and at most keep the old
13 * routines for backward compatibility.
20 #include <sys/ioctl.h>
21 #include <sys/errno.h>
26 DEFAULT_DEBUG_CHANNEL(mmsys
)
28 #define MAXJOYDRIVERS 4
30 static int count_use
[MAXJOYDRIVERS
] = {0, 0, 0, 0};
32 static int joy_nr_open
= 0;
33 static BOOL16 joyCaptured
= FALSE
;
34 static HWND16 CaptureWnd
[MAXJOYDRIVERS
] = {0, 0};
35 static int joy_dev
[MAXJOYDRIVERS
] = {-1, -1,-1,-1};
36 static JOYINFO16 joyCapData
[MAXJOYDRIVERS
];
37 static unsigned int joy_threshold
[MAXJOYDRIVERS
] = {0, 0, 0, 0};
47 /**************************************************************************
48 * joyOpenDriver [internal]
50 BOOL16
joyOpenDriver(WORD wID
)
52 char dev_name
[] = "/dev/js%d";
55 if (wID
>3) return FALSE
;
56 if (joy_dev
[wID
] >= 0) return TRUE
;
57 sprintf(buf
,dev_name
,wID
);
58 if ((joy_dev
[wID
] = open(buf
, O_RDONLY
)) >= 0) {
65 /**************************************************************************
66 * joyCloseDriver [internal]
68 void joyCloseDriver(WORD wID
)
70 if (joy_dev
[wID
] >= 0) {
77 /**************************************************************************
78 * joySendMessages [internal]
80 void joySendMessages(void)
87 for (joy
=0; joy
< MAXJOYDRIVERS
; joy
++)
88 if (joy_dev
[joy
] >= 0) {
89 if (count_use
[joy
] > 250) {
97 if (joyCaptured
== FALSE
) return;
99 TRACE(mmsys
, " --\n");
101 for (joy
=0; joy
< MAXJOYDRIVERS
; joy
++) {
102 if (joyOpenDriver(joy
) == FALSE
) continue;
103 dev_stat
= read(joy_dev
[joy
], &js
, sizeof(js
));
104 if (dev_stat
== sizeof(js
)) {
107 if ((joyCapData
[joy
].wXpos
!= js
.x
) || (joyCapData
[joy
].wYpos
!= js
.y
)) {
108 SendMessageA(CaptureWnd
[joy
], MM_JOY1MOVE
+ joy
, js
.buttons
, MAKELONG(js
.x
, js
.y
));
109 joyCapData
[joy
].wXpos
= js
.x
;
110 joyCapData
[joy
].wYpos
= js
.y
;
112 if (joyCapData
[joy
].wButtons
!= js
.buttons
) {
113 unsigned int ButtonChanged
= (WORD
)(joyCapData
[joy
].wButtons
^ js
.buttons
)<<8;
114 if (joyCapData
[joy
].wButtons
< js
.buttons
)
115 SendMessageA(CaptureWnd
[joy
], MM_JOY1BUTTONDOWN
+ joy
, ButtonChanged
, MAKELONG(js
.x
, js
.y
));
117 if (joyCapData
[joy
].wButtons
> js
.buttons
)
118 SendMessageA(CaptureWnd
[joy
], MM_JOY1BUTTONUP
119 + joy
, ButtonChanged
, MAKELONG(js
.x
, js
.y
));
120 joyCapData
[joy
].wButtons
= js
.buttons
;
127 /**************************************************************************
128 * JoyGetNumDevs [MMSYSTEM.101]
130 UINT WINAPI
joyGetNumDevs(void)
132 return joyGetNumDevs16();
135 /**************************************************************************
136 * JoyGetNumDevs [MMSYSTEM.101]
138 UINT16 WINAPI
joyGetNumDevs16(void)
143 for (joy
=0; joy
<MAXJOYDRIVERS
; joy
++)
144 if (joyOpenDriver(joy
) == TRUE
) {
148 TRACE(mmsys
, "returning %d\n", joy_cnt
);
149 if (!joy_cnt
) ERR(mmsys
, "No joystick found - "
150 "perhaps get joystick-0.8.0.tar.gz and load"
151 "it as module or use Linux >= 2.1.45 to be "
152 "able to use joysticks.\n");
156 /**************************************************************************
157 * JoyGetDevCaps [WINMM.27]
159 MMRESULT WINAPI
joyGetDevCapsA(UINT wID
, LPJOYCAPSA lpCaps
,UINT wSize
)
162 MMRESULT16 ret
= joyGetDevCaps16(wID
,&jc16
,sizeof(jc16
));
164 lpCaps
->wMid
= jc16
.wMid
;
165 lpCaps
->wPid
= jc16
.wPid
;
166 strcpy(lpCaps
->szPname
,jc16
.szPname
);
167 lpCaps
->wXmin
= jc16
.wXmin
;
168 lpCaps
->wXmax
= jc16
.wXmax
;
169 lpCaps
->wYmin
= jc16
.wYmin
;
170 lpCaps
->wYmax
= jc16
.wYmax
;
171 lpCaps
->wZmin
= jc16
.wZmin
;
172 lpCaps
->wZmax
= jc16
.wZmax
;
173 lpCaps
->wNumButtons
= jc16
.wNumButtons
;
174 lpCaps
->wPeriodMin
= jc16
.wPeriodMin
;
175 lpCaps
->wPeriodMax
= jc16
.wPeriodMax
;
177 lpCaps
->wRmin
= jc16
.wRmin
;
178 lpCaps
->wRmax
= jc16
.wRmax
;
179 lpCaps
->wUmin
= jc16
.wUmin
;
180 lpCaps
->wUmax
= jc16
.wUmax
;
181 lpCaps
->wVmin
= jc16
.wVmin
;
182 lpCaps
->wVmax
= jc16
.wVmax
;
183 lpCaps
->wCaps
= jc16
.wCaps
;
184 lpCaps
->wMaxAxes
= jc16
.wMaxAxes
;
185 lpCaps
->wNumAxes
= jc16
.wNumAxes
;
186 lpCaps
->wMaxButtons
= jc16
.wMaxButtons
;
187 strcpy(lpCaps
->szRegKey
,jc16
.szRegKey
);
188 strcpy(lpCaps
->szOEMVxD
,jc16
.szOEMVxD
);
192 /**************************************************************************
193 * JoyGetDevCaps [WINMM.28]
195 MMRESULT WINAPI
joyGetDevCapsW(UINT wID
, LPJOYCAPSW lpCaps
,UINT wSize
)
198 MMRESULT16 ret
= joyGetDevCaps16(wID
,&jc16
,sizeof(jc16
));
200 lpCaps
->wMid
= jc16
.wMid
;
201 lpCaps
->wPid
= jc16
.wPid
;
202 lstrcpyAtoW(lpCaps
->szPname
,jc16
.szPname
);
203 lpCaps
->wXmin
= jc16
.wXmin
;
204 lpCaps
->wXmax
= jc16
.wXmax
;
205 lpCaps
->wYmin
= jc16
.wYmin
;
206 lpCaps
->wYmax
= jc16
.wYmax
;
207 lpCaps
->wZmin
= jc16
.wZmin
;
208 lpCaps
->wZmax
= jc16
.wZmax
;
209 lpCaps
->wNumButtons
= jc16
.wNumButtons
;
210 lpCaps
->wPeriodMin
= jc16
.wPeriodMin
;
211 lpCaps
->wPeriodMax
= jc16
.wPeriodMax
;
213 lpCaps
->wRmin
= jc16
.wRmin
;
214 lpCaps
->wRmax
= jc16
.wRmax
;
215 lpCaps
->wUmin
= jc16
.wUmin
;
216 lpCaps
->wUmax
= jc16
.wUmax
;
217 lpCaps
->wVmin
= jc16
.wVmin
;
218 lpCaps
->wVmax
= jc16
.wVmax
;
219 lpCaps
->wCaps
= jc16
.wCaps
;
220 lpCaps
->wMaxAxes
= jc16
.wMaxAxes
;
221 lpCaps
->wNumAxes
= jc16
.wNumAxes
;
222 lpCaps
->wMaxButtons
= jc16
.wMaxButtons
;
223 lstrcpyAtoW(lpCaps
->szRegKey
,jc16
.szRegKey
);
224 lstrcpyAtoW(lpCaps
->szOEMVxD
,jc16
.szOEMVxD
);
227 /**************************************************************************
228 * JoyGetDevCaps [MMSYSTEM.102]
230 MMRESULT16 WINAPI
joyGetDevCaps16(UINT16 wID
, LPJOYCAPS16 lpCaps
, UINT16 wSize
)
232 TRACE(mmsys
, "(%04X, %p, %d);\n",
234 if (joyOpenDriver(wID
) == TRUE
) {
235 lpCaps
->wMid
= MM_MICROSOFT
;
236 lpCaps
->wPid
= MM_PC_JOYSTICK
;
237 strcpy(lpCaps
->szPname
, "WineJoy"); /* joystick product name */
238 lpCaps
->wXmin
= 0; /* FIXME */
239 lpCaps
->wXmax
= 0xffff;
241 lpCaps
->wYmax
= 0xffff;
243 lpCaps
->wZmax
= 0xffff;
244 lpCaps
->wNumButtons
= 2;
245 lpCaps
->wPeriodMin
= 0;
246 lpCaps
->wPeriodMax
= 50; /* FIXME end */
247 if (wSize
== sizeof(JOYCAPS16
)) {
248 /* complete 95 structure */
250 lpCaps
->wRmax
= 0xffff;
252 lpCaps
->wUmax
= 0xffff;
254 lpCaps
->wVmax
= 0xffff;
256 lpCaps
->wMaxAxes
= 6;
257 lpCaps
->wNumAxes
= 2;
258 lpCaps
->wMaxButtons
= 3;
259 strcpy(lpCaps
->szRegKey
,"");
260 strcpy(lpCaps
->szOEMVxD
,"");
263 return JOYERR_NOERROR
;
266 return MMSYSERR_NODRIVER
;
269 /**************************************************************************
270 * JoyGetPosEx [WINMM.31]
272 MMRESULT WINAPI
joyGetPosEx(UINT wID
, LPJOYINFOEX lpInfo
)
274 /* FIXME: implement it */
275 return MMSYSERR_NODRIVER
;
278 /**************************************************************************
279 * JoyGetPos [WINMM.30]
281 MMRESULT WINAPI
joyGetPos(UINT wID
, LPJOYINFO lpInfo
)
284 MMRESULT16 ret
= joyGetPos16(wID
,&ji
);
286 lpInfo
->wXpos
= ji
.wXpos
;
287 lpInfo
->wYpos
= ji
.wYpos
;
288 lpInfo
->wZpos
= ji
.wZpos
;
289 lpInfo
->wButtons
= ji
.wButtons
;
293 /**************************************************************************
294 * JoyGetPos [MMSYSTEM.103]
296 MMRESULT16 WINAPI
joyGetPos16(UINT16 wID
, LPJOYINFO16 lpInfo
)
300 TRACE(mmsys
, "(%04X, %p)\n", wID
, lpInfo
);
301 if (joyOpenDriver(wID
) == FALSE
) return MMSYSERR_NODRIVER
;
302 dev_stat
= read(joy_dev
[wID
], &js
, sizeof(js
));
303 if (dev_stat
!= sizeof(js
)) {
305 return JOYERR_UNPLUGGED
; /* FIXME: perhaps wrong, but what should I return else ? */
310 lpInfo
->wXpos
= js
.x
; /* FIXME: perhaps multiply it somehow ? */
311 lpInfo
->wYpos
= js
.y
;
312 lpInfo
->wZpos
= 0; /* FIXME: Don't know what to do with this value as joystick driver doesn't provide a Z value */
313 lpInfo
->wButtons
= js
.buttons
;
314 TRACE(mmsys
, "x: %d, y: %d, buttons: %d\n", js
.x
, js
.y
, js
.buttons
);
315 return JOYERR_NOERROR
;
318 /**************************************************************************
319 * JoyGetThreshold [WINMM.32]
321 MMRESULT WINAPI
joyGetThreshold(UINT wID
, LPUINT lpThreshold
)
324 MMRESULT16 ret
= joyGetThreshold16(wID
,&thresh
);
326 *lpThreshold
= thresh
;
330 /**************************************************************************
331 * JoyGetThreshold [MMSYSTEM.104]
333 MMRESULT16 WINAPI
joyGetThreshold16(UINT16 wID
, LPUINT16 lpThreshold
)
335 TRACE(mmsys
, "(%04X, %p);\n", wID
, lpThreshold
);
336 if (wID
>= MAXJOYDRIVERS
) return JOYERR_PARMS
;
337 *lpThreshold
= joy_threshold
[wID
];
338 return JOYERR_NOERROR
;
341 /**************************************************************************
342 * JoyReleaseCapture [WINMM.33]
344 MMRESULT WINAPI
joyReleaseCapture(UINT wID
)
346 return joyReleaseCapture16(wID
);
349 /**************************************************************************
350 * JoyReleaseCapture [MMSYSTEM.105]
352 MMRESULT16 WINAPI
joyReleaseCapture16(UINT16 wID
)
354 TRACE(mmsys
, "(%04X);\n", wID
);
359 return JOYERR_NOERROR
;
362 /**************************************************************************
363 * JoySetCapture [MMSYSTEM.106]
365 MMRESULT WINAPI
joySetCapture(HWND hWnd
,UINT wID
,UINT wPeriod
,BOOL bChanged
)
367 return joySetCapture16(hWnd
,wID
,wPeriod
,bChanged
);
370 /**************************************************************************
371 * JoySetCapture [MMSYSTEM.106]
373 MMRESULT16 WINAPI
joySetCapture16(HWND16 hWnd
,UINT16 wID
,UINT16 wPeriod
,BOOL16 bChanged
)
376 TRACE(mmsys
, "(%04X, %04X, %d, %d);\n",
377 hWnd
, wID
, wPeriod
, bChanged
);
379 if (!CaptureWnd
[wID
]) {
380 if (joyOpenDriver(wID
) == FALSE
) return MMSYSERR_NODRIVER
;
382 CaptureWnd
[wID
] = hWnd
;
383 return JOYERR_NOERROR
;
386 return JOYERR_NOCANDO
; /* FIXME: what should be returned ? */
389 /**************************************************************************
390 * JoySetThreshold [WINMM.35]
392 MMRESULT WINAPI
joySetThreshold(UINT wID
, UINT wThreshold
)
394 return joySetThreshold16(wID
,wThreshold
);
396 /**************************************************************************
397 * JoySetThreshold [MMSYSTEM.107]
399 MMRESULT16 WINAPI
joySetThreshold16(UINT16 wID
, UINT16 wThreshold
)
401 TRACE(mmsys
, "(%04X, %d);\n", wID
, wThreshold
);
403 if (wID
> 3) return JOYERR_PARMS
;
404 joy_threshold
[wID
] = wThreshold
;
405 return JOYERR_NOERROR
;
408 /**************************************************************************
409 * JoySetCalibration [MMSYSTEM.109]
411 MMRESULT16 WINAPI
joySetCalibration16(UINT16 wID
)
413 FIXME(mmsys
, "(%04X): stub.\n", wID
);
414 return JOYERR_NOCANDO
;