- support DirectInput 8 interfaces.
[wine/multimedia.git] / dlls / winmm / joystick.c
blob41159adc05ab535f86b09c682e3da31ff2a7440b
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * joystick functions
5 * Copyright 1997 Andreas Mohr
6 * 2000 Wolfgang Schwotzer
7 * Eric Pouech
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
24 #include "config.h"
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h>
33 #endif
35 #include "mmsystem.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "wingdi.h"
39 #include "winuser.h"
41 #include "wine/mmsystem16.h"
42 #include "winemm.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 {
53 JOYINFO ji;
54 HWND hCapture;
55 UINT wTimer;
56 DWORD threshold;
57 BOOL bChanged;
58 HDRVR hDriver;
59 } WINE_JOYSTICK;
61 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
63 /**************************************************************************
64 * JOY_LoadDriver [internal]
66 static BOOL JOY_LoadDriver(DWORD dwJoyID)
68 if (dwJoyID >= MAXJOYSTICK)
69 return FALSE;
70 if (JOY_Sticks[dwJoyID].hDriver)
71 return TRUE;
73 return JOY_Sticks[dwJoyID].hDriver = OpenDriverA("joystick.drv", 0, dwJoyID);
76 /**************************************************************************
77 * JOY_Timer [internal]
79 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT wTimer, DWORD dwTime)
81 int i;
82 WINE_JOYSTICK* joy;
83 JOYINFO ji;
84 LONG pos;
85 unsigned buttonChange;
87 for (i = 0; i < MAXJOYSTICK; i++) {
88 joy = &JOY_Sticks[i];
90 if (joy->hCapture != hWnd) continue;
92 joyGetPos(i, &ji);
93 pos = MAKELONG(ji.wXpos, ji.wYpos);
95 if (!joy->bChanged ||
96 abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
97 abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
98 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
99 joy->ji.wXpos = ji.wXpos;
100 joy->ji.wYpos = ji.wYpos;
102 if (!joy->bChanged ||
103 abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
104 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
105 joy->ji.wZpos = ji.wZpos;
107 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
108 if (ji.wButtons & buttonChange)
109 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
110 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
111 if (joy->ji.wButtons & buttonChange)
112 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
113 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
114 joy->ji.wButtons = ji.wButtons;
119 /**************************************************************************
120 * joyGetNumDevs [WINMM.@]
122 UINT WINAPI joyGetNumDevs(void)
124 UINT ret = 0;
125 int i;
127 for (i = 0; i < MAXJOYSTICK; i++) {
128 if (JOY_LoadDriver(i)) {
129 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
132 return ret;
135 /**************************************************************************
136 * joyGetNumDevs [MMSYSTEM.101]
138 UINT16 WINAPI joyGetNumDevs16(void)
140 return joyGetNumDevs();
143 /**************************************************************************
144 * joyGetDevCapsA [WINMM.@]
146 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps, UINT wSize)
148 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
149 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
151 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
152 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
154 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (DWORD)lpCaps, wSize);
157 /**************************************************************************
158 * joyGetDevCapsW [WINMM.@]
160 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps, UINT wSize)
162 JOYCAPSA jca;
163 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
165 if (ret != JOYERR_NOERROR) return ret;
166 lpCaps->wMid = jca.wMid;
167 lpCaps->wPid = jca.wPid;
168 MultiByteToWideChar( CP_ACP, 0, jca.szPname, -1, lpCaps->szPname,
169 sizeof(lpCaps->szPname)/sizeof(WCHAR) );
170 lpCaps->wXmin = jca.wXmin;
171 lpCaps->wXmax = jca.wXmax;
172 lpCaps->wYmin = jca.wYmin;
173 lpCaps->wYmax = jca.wYmax;
174 lpCaps->wZmin = jca.wZmin;
175 lpCaps->wZmax = jca.wZmax;
176 lpCaps->wNumButtons = jca.wNumButtons;
177 lpCaps->wPeriodMin = jca.wPeriodMin;
178 lpCaps->wPeriodMax = jca.wPeriodMax;
180 if (wSize >= sizeof(JOYCAPSW)) { /* Win95 extensions ? */
181 lpCaps->wRmin = jca.wRmin;
182 lpCaps->wRmax = jca.wRmax;
183 lpCaps->wUmin = jca.wUmin;
184 lpCaps->wUmax = jca.wUmax;
185 lpCaps->wVmin = jca.wVmin;
186 lpCaps->wVmax = jca.wVmax;
187 lpCaps->wCaps = jca.wCaps;
188 lpCaps->wMaxAxes = jca.wMaxAxes;
189 lpCaps->wNumAxes = jca.wNumAxes;
190 lpCaps->wMaxButtons = jca.wMaxButtons;
191 MultiByteToWideChar( CP_ACP, 0, jca.szRegKey, -1, lpCaps->szRegKey,
192 sizeof(lpCaps->szRegKey)/sizeof(WCHAR) );
193 MultiByteToWideChar( CP_ACP, 0, jca.szOEMVxD, -1, lpCaps->szOEMVxD,
194 sizeof(lpCaps->szOEMVxD)/sizeof(WCHAR) );
197 return ret;
200 /**************************************************************************
201 * joyGetDevCaps [MMSYSTEM.102]
203 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
205 JOYCAPSA jca;
206 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
208 if (ret != JOYERR_NOERROR) return ret;
209 lpCaps->wMid = jca.wMid;
210 lpCaps->wPid = jca.wPid;
211 strcpy(lpCaps->szPname, jca.szPname);
212 lpCaps->wXmin = jca.wXmin;
213 lpCaps->wXmax = jca.wXmax;
214 lpCaps->wYmin = jca.wYmin;
215 lpCaps->wYmax = jca.wYmax;
216 lpCaps->wZmin = jca.wZmin;
217 lpCaps->wZmax = jca.wZmax;
218 lpCaps->wNumButtons = jca.wNumButtons;
219 lpCaps->wPeriodMin = jca.wPeriodMin;
220 lpCaps->wPeriodMax = jca.wPeriodMax;
222 if (wSize >= sizeof(JOYCAPS16)) { /* Win95 extensions ? */
223 lpCaps->wRmin = jca.wRmin;
224 lpCaps->wRmax = jca.wRmax;
225 lpCaps->wUmin = jca.wUmin;
226 lpCaps->wUmax = jca.wUmax;
227 lpCaps->wVmin = jca.wVmin;
228 lpCaps->wVmax = jca.wVmax;
229 lpCaps->wCaps = jca.wCaps;
230 lpCaps->wMaxAxes = jca.wMaxAxes;
231 lpCaps->wNumAxes = jca.wNumAxes;
232 lpCaps->wMaxButtons = jca.wMaxButtons;
233 strcpy(lpCaps->szRegKey, jca.szRegKey);
234 strcpy(lpCaps->szOEMVxD, jca.szOEMVxD);
237 return ret;
240 /**************************************************************************
241 * joyGetPosEx [WINMM.@]
243 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
245 TRACE("(%d, %p);\n", wID, lpInfo);
247 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
248 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
250 lpInfo->dwXpos = 0;
251 lpInfo->dwYpos = 0;
252 lpInfo->dwZpos = 0;
253 lpInfo->dwRpos = 0;
254 lpInfo->dwUpos = 0;
255 lpInfo->dwVpos = 0;
256 lpInfo->dwButtons = 0;
257 lpInfo->dwButtonNumber = 0;
258 lpInfo->dwPOV = 0;
259 lpInfo->dwReserved1 = 0;
260 lpInfo->dwReserved2 = 0;
262 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
265 /**************************************************************************
266 * joyGetPosEx [MMSYSTEM.110]
268 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
270 return joyGetPosEx(wID, lpInfo);
273 /**************************************************************************
274 * joyGetPos [WINMM.@]
276 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
278 TRACE("(%d, %p);\n", wID, lpInfo);
280 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
281 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
283 lpInfo->wXpos = 0;
284 lpInfo->wYpos = 0;
285 lpInfo->wZpos = 0;
286 lpInfo->wButtons = 0;
288 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
291 /**************************************************************************
292 * joyGetPos [MMSYSTEM.103]
294 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
296 JOYINFO ji;
297 MMRESULT ret;
299 TRACE("(%d, %p);\n", wID, lpInfo);
301 if ((ret = joyGetPos(wID, &ji)) == JOYERR_NOERROR) {
302 lpInfo->wXpos = ji.wXpos;
303 lpInfo->wYpos = ji.wYpos;
304 lpInfo->wZpos = ji.wZpos;
305 lpInfo->wButtons = ji.wButtons;
307 return ret;
310 /**************************************************************************
311 * joyGetThreshold [WINMM.@]
313 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
315 TRACE("(%04X, %p);\n", wID, lpThreshold);
317 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
319 *lpThreshold = JOY_Sticks[wID].threshold;
320 return JOYERR_NOERROR;
323 /**************************************************************************
324 * joyGetThreshold [MMSYSTEM.104]
326 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
328 TRACE("(%04X, %p);\n", wID, lpThreshold);
330 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
332 *lpThreshold = JOY_Sticks[wID].threshold;
333 return JOYERR_NOERROR;
336 /**************************************************************************
337 * joyReleaseCapture [WINMM.@]
339 MMRESULT WINAPI joyReleaseCapture(UINT wID)
341 TRACE("(%04X);\n", wID);
343 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
344 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
345 if (!JOY_Sticks[wID].hCapture) return JOYERR_NOCANDO;
347 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
348 JOY_Sticks[wID].hCapture = 0;
349 JOY_Sticks[wID].wTimer = 0;
351 return JOYERR_NOERROR;
354 /**************************************************************************
355 * joyReleaseCapture [MMSYSTEM.105]
357 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
359 return joyReleaseCapture(wID);
362 /**************************************************************************
363 * joySetCapture [WINMM.@]
365 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
367 TRACE("(%04X, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
369 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
370 if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
371 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
373 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
374 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
376 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
377 return JOYERR_UNPLUGGED;
379 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
380 return JOYERR_NOCANDO;
382 JOY_Sticks[wID].hCapture = hWnd;
383 JOY_Sticks[wID].bChanged = bChanged;
385 return JOYERR_NOERROR;
388 /**************************************************************************
389 * joySetCapture [MMSYSTEM.106]
391 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd, UINT16 wID, UINT16 wPeriod, BOOL16 bChanged)
393 return joySetCapture16(hWnd, wID, wPeriod, bChanged);
396 /**************************************************************************
397 * joySetThreshold [WINMM.@]
399 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
401 TRACE("(%04X, %d);\n", wID, wThreshold);
403 if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
405 JOY_Sticks[wID].threshold = wThreshold;
407 return JOYERR_NOERROR;
410 /**************************************************************************
411 * joySetThreshold [MMSYSTEM.107]
413 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
415 return joySetThreshold16(wID,wThreshold);
418 /**************************************************************************
419 * joySetCalibration [MMSYSTEM.109]
421 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
423 FIXME("(%04X): stub.\n", wID);
424 return JOYERR_NOCANDO;