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 MultiByteToWideChar( CP_ACP, 0, jca.szPname, -1, lpCaps->szPname,
148 sizeof(lpCaps->szPname)/sizeof(WCHAR) );
149 lpCaps->wXmin = jca.wXmin;
150 lpCaps->wXmax = jca.wXmax;
151 lpCaps->wYmin = jca.wYmin;
152 lpCaps->wYmax = jca.wYmax;
153 lpCaps->wZmin = jca.wZmin;
154 lpCaps->wZmax = jca.wZmax;
155 lpCaps->wNumButtons = jca.wNumButtons;
156 lpCaps->wPeriodMin = jca.wPeriodMin;
157 lpCaps->wPeriodMax = jca.wPeriodMax;
159 if (wSize >= sizeof(JOYCAPSW)) { /* Win95 extensions ? */
160 lpCaps->wRmin = jca.wRmin;
161 lpCaps->wRmax = jca.wRmax;
162 lpCaps->wUmin = jca.wUmin;
163 lpCaps->wUmax = jca.wUmax;
164 lpCaps->wVmin = jca.wVmin;
165 lpCaps->wVmax = jca.wVmax;
166 lpCaps->wCaps = jca.wCaps;
167 lpCaps->wMaxAxes = jca.wMaxAxes;
168 lpCaps->wNumAxes = jca.wNumAxes;
169 lpCaps->wMaxButtons = jca.wMaxButtons;
170 MultiByteToWideChar( CP_ACP, 0, jca.szRegKey, -1, lpCaps->szRegKey,
171 sizeof(lpCaps->szRegKey)/sizeof(WCHAR) );
172 MultiByteToWideChar( CP_ACP, 0, jca.szOEMVxD, -1, lpCaps->szOEMVxD,
173 sizeof(lpCaps->szOEMVxD)/sizeof(WCHAR) );
179 /**************************************************************************
180 * joyGetDevCaps [MMSYSTEM.102]
182 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
185 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
187 if (ret != JOYERR_NOERROR) return ret;
188 lpCaps->wMid = jca.wMid;
189 lpCaps->wPid = jca.wPid;
190 strcpy(lpCaps->szPname, jca.szPname);
191 lpCaps->wXmin = jca.wXmin;
192 lpCaps->wXmax = jca.wXmax;
193 lpCaps->wYmin = jca.wYmin;
194 lpCaps->wYmax = jca.wYmax;
195 lpCaps->wZmin = jca.wZmin;
196 lpCaps->wZmax = jca.wZmax;
197 lpCaps->wNumButtons = jca.wNumButtons;
198 lpCaps->wPeriodMin = jca.wPeriodMin;
199 lpCaps->wPeriodMax = jca.wPeriodMax;
201 if (wSize >= sizeof(JOYCAPS16)) { /* Win95 extensions ? */
202 lpCaps->wRmin = jca.wRmin;
203 lpCaps->wRmax = jca.wRmax;
204 lpCaps->wUmin = jca.wUmin;
205 lpCaps->wUmax = jca.wUmax;
206 lpCaps->wVmin = jca.wVmin;
207 lpCaps->wVmax = jca.wVmax;
208 lpCaps->wCaps = jca.wCaps;
209 lpCaps->wMaxAxes = jca.wMaxAxes;
210 lpCaps->wNumAxes = jca.wNumAxes;
211 lpCaps->wMaxButtons = jca.wMaxButtons;
212 strcpy(lpCaps->szRegKey, jca.szRegKey);
213 strcpy(lpCaps->szOEMVxD, jca.szOEMVxD);
219 /**************************************************************************
220 * joyGetPosEx [WINMM.32]
222 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
224 TRACE("(%d, %p);\n", wID, lpInfo);
226 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
227 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
235 lpInfo->dwButtons = 0;
236 lpInfo->dwButtonNumber = 0;
238 lpInfo->dwReserved1 = 0;
239 lpInfo->dwReserved2 = 0;
241 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
244 /**************************************************************************
245 * joyGetPosEx16 [MMSYSTEM.]
247 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
249 return joyGetPosEx(wID, lpInfo);
252 /**************************************************************************
253 * joyGetPos [WINMM.31]
255 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
257 TRACE("(%d, %p);\n", wID, lpInfo);
259 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
260 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
265 lpInfo->wButtons = 0;
267 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
270 /**************************************************************************
271 * joyGetPos16 [MMSYSTEM.103]
273 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
278 TRACE("(%d, %p);\n", wID, lpInfo);
280 if ((ret = joyGetPos(wID, &ji)) == JOYERR_NOERROR) {
281 lpInfo->wXpos = ji.wXpos;
282 lpInfo->wYpos = ji.wYpos;
283 lpInfo->wZpos = ji.wZpos;
284 lpInfo->wButtons = ji.wButtons;
289 /**************************************************************************
290 * joyGetThreshold [WINMM.36]
292 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
294 TRACE("(%04X, %p);\n", wID, lpThreshold);
296 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
298 *lpThreshold = JOY_Sticks[wID].threshold;
299 return JOYERR_NOERROR;
302 /**************************************************************************
303 * joyGetThreshold [MMSYSTEM.104]
305 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
307 TRACE("(%04X, %p);\n", wID, lpThreshold);
309 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
311 *lpThreshold = JOY_Sticks[wID].threshold;
312 return JOYERR_NOERROR;
315 /**************************************************************************
316 * joyReleaseCapture [WINMM.34]
318 MMRESULT WINAPI joyReleaseCapture(UINT wID)
320 TRACE("(%04X);\n", wID);
322 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
323 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
324 if (!JOY_Sticks[wID].hCapture) return JOYERR_NOCANDO;
326 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
327 JOY_Sticks[wID].hCapture = 0;
328 JOY_Sticks[wID].wTimer = 0;
330 return JOYERR_NOERROR;
333 /**************************************************************************
334 * joyReleaseCapture [MMSYSTEM.105]
336 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
338 return joyReleaseCapture(wID);
341 /**************************************************************************
342 * joySetCapture [WINMM.35]
344 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
346 TRACE("(%04X, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
348 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
349 if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
350 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
352 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
353 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
355 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
356 return JOYERR_UNPLUGGED;
358 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
359 return JOYERR_NOCANDO;
361 JOY_Sticks[wID].hCapture = hWnd;
362 JOY_Sticks[wID].bChanged = bChanged;
364 return JOYERR_NOERROR;
367 /**************************************************************************
368 * joySetCapture [MMSYSTEM.106]
370 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd, UINT16 wID, UINT16 wPeriod, BOOL16 bChanged)
372 return joySetCapture16(hWnd, wID, wPeriod, bChanged);
375 /**************************************************************************
376 * joySetThreshold [WINMM.36]
378 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
380 TRACE("(%04X, %d);\n", wID, wThreshold);
382 if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
384 JOY_Sticks[wID].threshold = wThreshold;
386 return JOYERR_NOERROR;
389 /**************************************************************************
390 * joySetThreshold [MMSYSTEM.107]
392 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
394 return joySetThreshold16(wID,wThreshold);
397 /**************************************************************************
398 * joySetCalibration [MMSYSTEM.109]
400 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
402 FIXME("(%04X): stub.\n", wID);
403 return JOYERR_NOCANDO;