1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
5 * Copyright 1997 Andreas Mohr
6 * 2000 Wolfgang Schwotzer
17 #include <sys/ioctl.h>
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(mmsys
);
27 #define MAXJOYSTICK (JOYSTICKID2 + 1)
28 #define JOY_PERIOD_MIN (10) /* min Capture time period */
29 #define JOY_PERIOD_MAX (1000) /* max Capture time period */
31 typedef struct tagWINE_JOYSTICK
{
40 static WINE_JOYSTICK JOY_Sticks
[MAXJOYSTICK
];
42 /**************************************************************************
43 * JOY_LoadDriver [internal]
45 static BOOL
JOY_LoadDriver(DWORD dwJoyID
)
47 if (dwJoyID
>= MAXJOYSTICK
)
49 if (JOY_Sticks
[dwJoyID
].hDriver
)
52 return JOY_Sticks
[dwJoyID
].hDriver
= OpenDriverA("joystick.drv", 0, dwJoyID
);
55 /**************************************************************************
56 * JOY_Timer [internal]
58 static void CALLBACK
JOY_Timer(HWND hWnd
, UINT wMsg
, UINT wTimer
, DWORD dwTime
)
64 unsigned buttonChange
;
66 for (i
= 0; i
< MAXJOYSTICK
; i
++) {
69 if (joy
->hCapture
!= hWnd
) continue;
72 pos
= MAKELONG(ji
.wXpos
, ji
.wYpos
);
75 abs(joy
->ji
.wXpos
- ji
.wXpos
) > joy
->threshold
||
76 abs(joy
->ji
.wYpos
- ji
.wYpos
) > joy
->threshold
) {
77 SendMessageA(joy
->hCapture
, MM_JOY1MOVE
+ i
, ji
.wButtons
, pos
);
78 joy
->ji
.wXpos
= ji
.wXpos
;
79 joy
->ji
.wYpos
= ji
.wYpos
;
82 abs(joy
->ji
.wZpos
- ji
.wZpos
) > joy
->threshold
) {
83 SendMessageA(joy
->hCapture
, MM_JOY1ZMOVE
+ i
, ji
.wButtons
, pos
);
84 joy
->ji
.wZpos
= ji
.wZpos
;
86 if ((buttonChange
= joy
->ji
.wButtons
^ ji
.wButtons
) != 0) {
87 if (ji
.wButtons
& buttonChange
)
88 SendMessageA(joy
->hCapture
, MM_JOY1BUTTONDOWN
+ i
,
89 (buttonChange
<< 8) | (ji
.wButtons
& buttonChange
), pos
);
90 if (joy
->ji
.wButtons
& buttonChange
)
91 SendMessageA(joy
->hCapture
, MM_JOY1BUTTONUP
+ i
,
92 (buttonChange
<< 8) | (joy
->ji
.wButtons
& buttonChange
), pos
);
93 joy
->ji
.wButtons
= ji
.wButtons
;
98 /**************************************************************************
99 * joyGetNumDevs [WINMM.30]
101 UINT WINAPI
joyGetNumDevs(void)
106 for (i
= 0; i
< MAXJOYSTICK
; i
++) {
107 if (JOY_LoadDriver(i
)) {
108 ret
+= SendDriverMessage(JOY_Sticks
[i
].hDriver
, JDD_GETNUMDEVS
, 0L, 0L);
114 /**************************************************************************
115 * joyGetNumDevs [MMSYSTEM.101]
117 UINT16 WINAPI
joyGetNumDevs16(void)
119 return joyGetNumDevs();
122 /**************************************************************************
123 * joyGetDevCapsA [WINMM.27]
125 MMRESULT WINAPI
joyGetDevCapsA(UINT wID
, LPJOYCAPSA lpCaps
, UINT wSize
)
127 if (wID
>= MAXJOYSTICK
) return JOYERR_PARMS
;
128 if (!JOY_LoadDriver(wID
)) return MMSYSERR_NODRIVER
;
130 lpCaps
->wPeriodMin
= JOY_PERIOD_MIN
; /* FIXME */
131 lpCaps
->wPeriodMax
= JOY_PERIOD_MAX
; /* FIXME (same as MS Joystick Driver) */
133 return SendDriverMessage(JOY_Sticks
[wID
].hDriver
, JDD_GETDEVCAPS
, (DWORD
)lpCaps
, wSize
);
136 /**************************************************************************
137 * joyGetDevCapsW [WINMM.28]
139 MMRESULT WINAPI
joyGetDevCapsW(UINT wID
, LPJOYCAPSW lpCaps
, UINT wSize
)
142 MMRESULT ret
= joyGetDevCapsA(wID
, &jca
, sizeof(jca
));
144 if (ret
!= JOYERR_NOERROR
) return ret
;
145 lpCaps
->wMid
= jca
.wMid
;
146 lpCaps
->wPid
= jca
.wPid
;
147 lstrcpyAtoW(lpCaps
->szPname
, jca
.szPname
);
148 lpCaps
->wXmin
= jca
.wXmin
;
149 lpCaps
->wXmax
= jca
.wXmax
;
150 lpCaps
->wYmin
= jca
.wYmin
;
151 lpCaps
->wYmax
= jca
.wYmax
;
152 lpCaps
->wZmin
= jca
.wZmin
;
153 lpCaps
->wZmax
= jca
.wZmax
;
154 lpCaps
->wNumButtons
= jca
.wNumButtons
;
155 lpCaps
->wPeriodMin
= jca
.wPeriodMin
;
156 lpCaps
->wPeriodMax
= jca
.wPeriodMax
;
158 if (wSize
>= sizeof(JOYCAPSW
)) { /* Win95 extensions ? */
159 lpCaps
->wRmin
= jca
.wRmin
;
160 lpCaps
->wRmax
= jca
.wRmax
;
161 lpCaps
->wUmin
= jca
.wUmin
;
162 lpCaps
->wUmax
= jca
.wUmax
;
163 lpCaps
->wVmin
= jca
.wVmin
;
164 lpCaps
->wVmax
= jca
.wVmax
;
165 lpCaps
->wCaps
= jca
.wCaps
;
166 lpCaps
->wMaxAxes
= jca
.wMaxAxes
;
167 lpCaps
->wNumAxes
= jca
.wNumAxes
;
168 lpCaps
->wMaxButtons
= jca
.wMaxButtons
;
169 lstrcpyAtoW(lpCaps
->szRegKey
, jca
.szRegKey
);
170 lstrcpyAtoW(lpCaps
->szOEMVxD
, jca
.szOEMVxD
);
176 /**************************************************************************
177 * joyGetDevCaps [MMSYSTEM.102]
179 MMRESULT16 WINAPI
joyGetDevCaps16(UINT16 wID
, LPJOYCAPS16 lpCaps
, UINT16 wSize
)
182 MMRESULT ret
= joyGetDevCapsA(wID
, &jca
, sizeof(jca
));
184 if (ret
!= JOYERR_NOERROR
) return ret
;
185 lpCaps
->wMid
= jca
.wMid
;
186 lpCaps
->wPid
= jca
.wPid
;
187 strcpy(lpCaps
->szPname
, jca
.szPname
);
188 lpCaps
->wXmin
= jca
.wXmin
;
189 lpCaps
->wXmax
= jca
.wXmax
;
190 lpCaps
->wYmin
= jca
.wYmin
;
191 lpCaps
->wYmax
= jca
.wYmax
;
192 lpCaps
->wZmin
= jca
.wZmin
;
193 lpCaps
->wZmax
= jca
.wZmax
;
194 lpCaps
->wNumButtons
= jca
.wNumButtons
;
195 lpCaps
->wPeriodMin
= jca
.wPeriodMin
;
196 lpCaps
->wPeriodMax
= jca
.wPeriodMax
;
198 if (wSize
>= sizeof(JOYCAPS16
)) { /* Win95 extensions ? */
199 lpCaps
->wRmin
= jca
.wRmin
;
200 lpCaps
->wRmax
= jca
.wRmax
;
201 lpCaps
->wUmin
= jca
.wUmin
;
202 lpCaps
->wUmax
= jca
.wUmax
;
203 lpCaps
->wVmin
= jca
.wVmin
;
204 lpCaps
->wVmax
= jca
.wVmax
;
205 lpCaps
->wCaps
= jca
.wCaps
;
206 lpCaps
->wMaxAxes
= jca
.wMaxAxes
;
207 lpCaps
->wNumAxes
= jca
.wNumAxes
;
208 lpCaps
->wMaxButtons
= jca
.wMaxButtons
;
209 strcpy(lpCaps
->szRegKey
, jca
.szRegKey
);
210 strcpy(lpCaps
->szOEMVxD
, jca
.szOEMVxD
);
216 /**************************************************************************
217 * joyGetPosEx [WINMM.32]
219 MMRESULT WINAPI
joyGetPosEx(UINT wID
, LPJOYINFOEX lpInfo
)
221 TRACE("(%d, %p);\n", wID
, lpInfo
);
223 if (wID
>= MAXJOYSTICK
) return JOYERR_PARMS
;
224 if (!JOY_LoadDriver(wID
)) return MMSYSERR_NODRIVER
;
232 lpInfo
->dwButtons
= 0;
233 lpInfo
->dwButtonNumber
= 0;
235 lpInfo
->dwReserved1
= 0;
236 lpInfo
->dwReserved2
= 0;
238 return SendDriverMessage(JOY_Sticks
[wID
].hDriver
, JDD_GETPOSEX
, (DWORD
)lpInfo
, 0L);
241 /**************************************************************************
242 * joyGetPosEx16 [MMSYSTEM.]
244 MMRESULT16 WINAPI
joyGetPosEx16(UINT16 wID
, LPJOYINFOEX lpInfo
)
246 return joyGetPosEx(wID
, lpInfo
);
249 /**************************************************************************
250 * joyGetPos [WINMM.31]
252 MMRESULT WINAPI
joyGetPos(UINT wID
, LPJOYINFO lpInfo
)
254 TRACE("(%d, %p);\n", wID
, lpInfo
);
256 if (wID
>= MAXJOYSTICK
) return JOYERR_PARMS
;
257 if (!JOY_LoadDriver(wID
)) return MMSYSERR_NODRIVER
;
262 lpInfo
->wButtons
= 0;
264 return SendDriverMessage(JOY_Sticks
[wID
].hDriver
, JDD_GETPOS
, (DWORD
)lpInfo
, 0L);
267 /**************************************************************************
268 * joyGetPos16 [MMSYSTEM.103]
270 MMRESULT16 WINAPI
joyGetPos16(UINT16 wID
, LPJOYINFO16 lpInfo
)
275 TRACE("(%d, %p);\n", wID
, lpInfo
);
277 if ((ret
= joyGetPos(wID
, &ji
)) == JOYERR_NOERROR
) {
278 lpInfo
->wXpos
= ji
.wXpos
;
279 lpInfo
->wYpos
= ji
.wYpos
;
280 lpInfo
->wZpos
= ji
.wZpos
;
281 lpInfo
->wButtons
= ji
.wButtons
;
286 /**************************************************************************
287 * joyGetThreshold [WINMM.36]
289 MMRESULT WINAPI
joyGetThreshold(UINT wID
, LPUINT lpThreshold
)
291 TRACE("(%04X, %p);\n", wID
, lpThreshold
);
293 if (wID
>= MAXJOYSTICK
) return JOYERR_PARMS
;
295 *lpThreshold
= JOY_Sticks
[wID
].threshold
;
296 return JOYERR_NOERROR
;
299 /**************************************************************************
300 * joyGetThreshold [MMSYSTEM.104]
302 MMRESULT16 WINAPI
joyGetThreshold16(UINT16 wID
, LPUINT16 lpThreshold
)
304 TRACE("(%04X, %p);\n", wID
, lpThreshold
);
306 if (wID
>= MAXJOYSTICK
) return JOYERR_PARMS
;
308 *lpThreshold
= JOY_Sticks
[wID
].threshold
;
309 return JOYERR_NOERROR
;
312 /**************************************************************************
313 * joyReleaseCapture [WINMM.34]
315 MMRESULT WINAPI
joyReleaseCapture(UINT wID
)
317 TRACE("(%04X);\n", wID
);
319 if (wID
>= MAXJOYSTICK
) return JOYERR_PARMS
;
320 if (!JOY_LoadDriver(wID
)) return MMSYSERR_NODRIVER
;
321 if (!JOY_Sticks
[wID
].hCapture
) return JOYERR_NOCANDO
;
323 KillTimer(JOY_Sticks
[wID
].hCapture
, JOY_Sticks
[wID
].wTimer
);
324 JOY_Sticks
[wID
].hCapture
= 0;
325 JOY_Sticks
[wID
].wTimer
= 0;
327 return JOYERR_NOERROR
;
330 /**************************************************************************
331 * joyReleaseCapture [MMSYSTEM.105]
333 MMRESULT16 WINAPI
joyReleaseCapture16(UINT16 wID
)
335 return joyReleaseCapture(wID
);
338 /**************************************************************************
339 * joySetCapture [WINMM.35]
341 MMRESULT WINAPI
joySetCapture(HWND hWnd
, UINT wID
, UINT wPeriod
, BOOL bChanged
)
343 TRACE("(%04X, %04X, %d, %d);\n", hWnd
, wID
, wPeriod
, bChanged
);
345 if (wID
>= MAXJOYSTICK
|| hWnd
== 0) return JOYERR_PARMS
;
346 if (wPeriod
<JOY_PERIOD_MIN
|| wPeriod
>JOY_PERIOD_MAX
) return JOYERR_PARMS
;
347 if (!JOY_LoadDriver(wID
)) return MMSYSERR_NODRIVER
;
349 if (JOY_Sticks
[wID
].hCapture
|| !IsWindow(hWnd
))
350 return JOYERR_NOCANDO
; /* FIXME: what should be returned ? */
352 if (joyGetPos(wID
, &JOY_Sticks
[wID
].ji
) != JOYERR_NOERROR
)
353 return JOYERR_UNPLUGGED
;
355 if ((JOY_Sticks
[wID
].wTimer
= SetTimer(hWnd
, 0, wPeriod
, JOY_Timer
)) == 0)
356 return JOYERR_NOCANDO
;
358 JOY_Sticks
[wID
].hCapture
= hWnd
;
359 JOY_Sticks
[wID
].bChanged
= bChanged
;
361 return JOYERR_NOERROR
;
364 /**************************************************************************
365 * joySetCapture [MMSYSTEM.106]
367 MMRESULT16 WINAPI
joySetCapture16(HWND16 hWnd
, UINT16 wID
, UINT16 wPeriod
, BOOL16 bChanged
)
369 return joySetCapture16(hWnd
, wID
, wPeriod
, bChanged
);
372 /**************************************************************************
373 * joySetThreshold [WINMM.36]
375 MMRESULT WINAPI
joySetThreshold(UINT wID
, UINT wThreshold
)
377 TRACE("(%04X, %d);\n", wID
, wThreshold
);
379 if (wID
>= MAXJOYSTICK
) return MMSYSERR_INVALPARAM
;
381 JOY_Sticks
[wID
].threshold
= wThreshold
;
383 return JOYERR_NOERROR
;
386 /**************************************************************************
387 * joySetThreshold [MMSYSTEM.107]
389 MMRESULT16 WINAPI
joySetThreshold16(UINT16 wID
, UINT16 wThreshold
)
391 return joySetThreshold16(wID
,wThreshold
);
394 /**************************************************************************
395 * joySetCalibration [MMSYSTEM.109]
397 MMRESULT16 WINAPI
joySetCalibration16(UINT16 wID
)
399 FIXME("(%04X): stub.\n", wID
);
400 return JOYERR_NOCANDO
;