1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
5 * Copyright 1997 Andreas Mohr
6 * 2000 Wolfgang Schwotzer
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h>
41 #include "wine/mmsystem16.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(mmsys);
48 #define MAXJOYSTICK (JOYSTICKID2 + 1)
49 #define JOY_PERIOD_MIN (10) /* min Capture time period */
50 #define JOY_PERIOD_MAX (1000) /* max Capture time period */
52 typedef struct tagWINE_JOYSTICK {
61 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
63 /**************************************************************************
64 * JOY_LoadDriver [internal]
66 static BOOL JOY_LoadDriver(DWORD dwJoyID)
68 if (dwJoyID >= MAXJOYSTICK)
70 if (JOY_Sticks[dwJoyID].hDriver)
73 JOY_Sticks[dwJoyID].hDriver = OpenDriverA("joystick.drv", 0, dwJoyID);
74 return (JOY_Sticks[dwJoyID].hDriver != 0);
77 /**************************************************************************
78 * JOY_Timer [internal]
80 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT wTimer, DWORD dwTime)
86 unsigned buttonChange;
88 for (i = 0; i < MAXJOYSTICK; i++) {
91 if (joy->hCapture != hWnd) continue;
94 pos = MAKELONG(ji.wXpos, ji.wYpos);
97 abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
98 abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
99 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
100 joy->ji.wXpos = ji.wXpos;
101 joy->ji.wYpos = ji.wYpos;
103 if (!joy->bChanged ||
104 abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
105 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
106 joy->ji.wZpos = ji.wZpos;
108 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
109 if (ji.wButtons & buttonChange)
110 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
111 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
112 if (joy->ji.wButtons & buttonChange)
113 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
114 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
115 joy->ji.wButtons = ji.wButtons;
120 /**************************************************************************
121 * joyGetNumDevs [WINMM.@]
123 UINT WINAPI joyGetNumDevs(void)
128 for (i = 0; i < MAXJOYSTICK; i++) {
129 if (JOY_LoadDriver(i)) {
130 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
136 /**************************************************************************
137 * joyGetNumDevs [MMSYSTEM.101]
139 UINT16 WINAPI joyGetNumDevs16(void)
141 return joyGetNumDevs();
144 /**************************************************************************
145 * joyGetDevCapsA [WINMM.@]
147 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps, UINT wSize)
149 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
150 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
152 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
153 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
155 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (DWORD)lpCaps, wSize);
158 /**************************************************************************
159 * joyGetDevCapsW [WINMM.@]
161 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps, UINT wSize)
164 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
166 if (ret != JOYERR_NOERROR) return ret;
167 lpCaps->wMid = jca.wMid;
168 lpCaps->wPid = jca.wPid;
169 MultiByteToWideChar( CP_ACP, 0, jca.szPname, -1, lpCaps->szPname,
170 sizeof(lpCaps->szPname)/sizeof(WCHAR) );
171 lpCaps->wXmin = jca.wXmin;
172 lpCaps->wXmax = jca.wXmax;
173 lpCaps->wYmin = jca.wYmin;
174 lpCaps->wYmax = jca.wYmax;
175 lpCaps->wZmin = jca.wZmin;
176 lpCaps->wZmax = jca.wZmax;
177 lpCaps->wNumButtons = jca.wNumButtons;
178 lpCaps->wPeriodMin = jca.wPeriodMin;
179 lpCaps->wPeriodMax = jca.wPeriodMax;
181 if (wSize >= sizeof(JOYCAPSW)) { /* Win95 extensions ? */
182 lpCaps->wRmin = jca.wRmin;
183 lpCaps->wRmax = jca.wRmax;
184 lpCaps->wUmin = jca.wUmin;
185 lpCaps->wUmax = jca.wUmax;
186 lpCaps->wVmin = jca.wVmin;
187 lpCaps->wVmax = jca.wVmax;
188 lpCaps->wCaps = jca.wCaps;
189 lpCaps->wMaxAxes = jca.wMaxAxes;
190 lpCaps->wNumAxes = jca.wNumAxes;
191 lpCaps->wMaxButtons = jca.wMaxButtons;
192 MultiByteToWideChar( CP_ACP, 0, jca.szRegKey, -1, lpCaps->szRegKey,
193 sizeof(lpCaps->szRegKey)/sizeof(WCHAR) );
194 MultiByteToWideChar( CP_ACP, 0, jca.szOEMVxD, -1, lpCaps->szOEMVxD,
195 sizeof(lpCaps->szOEMVxD)/sizeof(WCHAR) );
201 /**************************************************************************
202 * joyGetDevCaps [MMSYSTEM.102]
204 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
207 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
209 if (ret != JOYERR_NOERROR) return ret;
210 lpCaps->wMid = jca.wMid;
211 lpCaps->wPid = jca.wPid;
212 strcpy(lpCaps->szPname, jca.szPname);
213 lpCaps->wXmin = jca.wXmin;
214 lpCaps->wXmax = jca.wXmax;
215 lpCaps->wYmin = jca.wYmin;
216 lpCaps->wYmax = jca.wYmax;
217 lpCaps->wZmin = jca.wZmin;
218 lpCaps->wZmax = jca.wZmax;
219 lpCaps->wNumButtons = jca.wNumButtons;
220 lpCaps->wPeriodMin = jca.wPeriodMin;
221 lpCaps->wPeriodMax = jca.wPeriodMax;
223 if (wSize >= sizeof(JOYCAPS16)) { /* Win95 extensions ? */
224 lpCaps->wRmin = jca.wRmin;
225 lpCaps->wRmax = jca.wRmax;
226 lpCaps->wUmin = jca.wUmin;
227 lpCaps->wUmax = jca.wUmax;
228 lpCaps->wVmin = jca.wVmin;
229 lpCaps->wVmax = jca.wVmax;
230 lpCaps->wCaps = jca.wCaps;
231 lpCaps->wMaxAxes = jca.wMaxAxes;
232 lpCaps->wNumAxes = jca.wNumAxes;
233 lpCaps->wMaxButtons = jca.wMaxButtons;
234 strcpy(lpCaps->szRegKey, jca.szRegKey);
235 strcpy(lpCaps->szOEMVxD, jca.szOEMVxD);
241 /**************************************************************************
242 * joyGetPosEx [WINMM.@]
244 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
246 TRACE("(%d, %p);\n", wID, lpInfo);
248 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
249 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
257 lpInfo->dwButtons = 0;
258 lpInfo->dwButtonNumber = 0;
260 lpInfo->dwReserved1 = 0;
261 lpInfo->dwReserved2 = 0;
263 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
266 /**************************************************************************
267 * joyGetPosEx [MMSYSTEM.110]
269 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
271 return joyGetPosEx(wID, lpInfo);
274 /**************************************************************************
275 * joyGetPos [WINMM.@]
277 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
279 TRACE("(%d, %p);\n", wID, lpInfo);
281 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
282 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
287 lpInfo->wButtons = 0;
289 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
292 /**************************************************************************
293 * joyGetPos [MMSYSTEM.103]
295 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
300 TRACE("(%d, %p);\n", wID, lpInfo);
302 if ((ret = joyGetPos(wID, &ji)) == JOYERR_NOERROR) {
303 lpInfo->wXpos = ji.wXpos;
304 lpInfo->wYpos = ji.wYpos;
305 lpInfo->wZpos = ji.wZpos;
306 lpInfo->wButtons = ji.wButtons;
311 /**************************************************************************
312 * joyGetThreshold [WINMM.@]
314 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
316 TRACE("(%04X, %p);\n", wID, lpThreshold);
318 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
320 *lpThreshold = JOY_Sticks[wID].threshold;
321 return JOYERR_NOERROR;
324 /**************************************************************************
325 * joyGetThreshold [MMSYSTEM.104]
327 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
329 TRACE("(%04X, %p);\n", wID, lpThreshold);
331 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
333 *lpThreshold = JOY_Sticks[wID].threshold;
334 return JOYERR_NOERROR;
337 /**************************************************************************
338 * joyReleaseCapture [WINMM.@]
340 MMRESULT WINAPI joyReleaseCapture(UINT wID)
342 TRACE("(%04X);\n", wID);
344 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
345 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
346 if (!JOY_Sticks[wID].hCapture) return JOYERR_NOCANDO;
348 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
349 JOY_Sticks[wID].hCapture = 0;
350 JOY_Sticks[wID].wTimer = 0;
352 return JOYERR_NOERROR;
355 /**************************************************************************
356 * joyReleaseCapture [MMSYSTEM.105]
358 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
360 return joyReleaseCapture(wID);
363 /**************************************************************************
364 * joySetCapture [WINMM.@]
366 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
368 TRACE("(%04X, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
370 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
371 if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
372 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
374 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
375 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
377 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
378 return JOYERR_UNPLUGGED;
380 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
381 return JOYERR_NOCANDO;
383 JOY_Sticks[wID].hCapture = hWnd;
384 JOY_Sticks[wID].bChanged = bChanged;
386 return JOYERR_NOERROR;
389 /**************************************************************************
390 * joySetCapture [MMSYSTEM.106]
392 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd, UINT16 wID, UINT16 wPeriod, BOOL16 bChanged)
394 return joySetCapture16(hWnd, wID, wPeriod, bChanged);
397 /**************************************************************************
398 * joySetThreshold [WINMM.@]
400 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
402 TRACE("(%04X, %d);\n", wID, wThreshold);
404 if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
406 JOY_Sticks[wID].threshold = wThreshold;
408 return JOYERR_NOERROR;
411 /**************************************************************************
412 * joySetThreshold [MMSYSTEM.107]
414 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
416 return joySetThreshold16(wID,wThreshold);
419 /**************************************************************************
420 * joySetCalibration [MMSYSTEM.109]
422 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
424 FIXME("(%04X): stub.\n", wID);
425 return JOYERR_NOCANDO;