makefiles: Add support for Automake-style silent make rules.
[wine.git] / dlls / winmm / joystick.c
blob2b1811d37780a46274e5c6364cbcec72e681f2fe
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <fcntl.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "mmsystem.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winnls.h"
37 #include "mmddk.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
43 #define MAXJOYSTICK (JOYSTICKID2 + 30)
44 #define JOY_PERIOD_MIN (10) /* min Capture time period */
45 #define JOY_PERIOD_MAX (1000) /* max Capture time period */
47 typedef struct tagWINE_JOYSTICK {
48 JOYINFO ji;
49 HWND hCapture;
50 UINT wTimer;
51 DWORD threshold;
52 BOOL bChanged;
53 HDRVR hDriver;
54 } WINE_JOYSTICK;
56 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
58 static BOOL compare_uint(unsigned int x, unsigned int y, unsigned int max_diff)
60 unsigned int diff = x > y ? x - y : y - x;
61 return diff <= max_diff;
64 /**************************************************************************
65 * JOY_LoadDriver [internal]
67 static BOOL JOY_LoadDriver(DWORD dwJoyID)
69 static BOOL winejoystick_missing = FALSE;
71 if (dwJoyID >= MAXJOYSTICK || winejoystick_missing)
72 return FALSE;
73 if (JOY_Sticks[dwJoyID].hDriver)
74 return TRUE;
76 JOY_Sticks[dwJoyID].hDriver = OpenDriverA("winejoystick.drv", 0, dwJoyID);
78 if (!JOY_Sticks[dwJoyID].hDriver)
80 WARN("OpenDriverA(\"winejoystick.drv\") failed\n");
82 /* The default driver is missing, don't attempt to load it again */
83 winejoystick_missing = TRUE;
86 return (JOY_Sticks[dwJoyID].hDriver != 0);
89 /**************************************************************************
90 * JOY_Timer [internal]
92 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT_PTR wTimer, DWORD dwTime)
94 int i;
95 WINE_JOYSTICK* joy;
96 MMRESULT res;
97 JOYINFO ji;
98 LONG pos;
99 unsigned buttonChange;
101 for (i = 0; i < MAXJOYSTICK; i++) {
102 joy = &JOY_Sticks[i];
104 if (joy->hCapture != hWnd) continue;
106 res = joyGetPos(i, &ji);
107 if (res != JOYERR_NOERROR) {
108 WARN("joyGetPos failed: %08x\n", res);
109 continue;
112 pos = MAKELONG(ji.wXpos, ji.wYpos);
114 if (!joy->bChanged ||
115 !compare_uint(joy->ji.wXpos, ji.wXpos, joy->threshold) ||
116 !compare_uint(joy->ji.wYpos, ji.wYpos, joy->threshold)) {
117 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
118 joy->ji.wXpos = ji.wXpos;
119 joy->ji.wYpos = ji.wYpos;
121 if (!joy->bChanged ||
122 !compare_uint(joy->ji.wZpos, ji.wZpos, joy->threshold)) {
123 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
124 joy->ji.wZpos = ji.wZpos;
126 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
127 if (ji.wButtons & buttonChange)
128 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
129 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
130 if (joy->ji.wButtons & buttonChange)
131 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
132 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
133 joy->ji.wButtons = ji.wButtons;
138 /**************************************************************************
139 * joyConfigChanged [WINMM.@]
141 MMRESULT WINAPI joyConfigChanged(DWORD flags)
143 FIXME("(%x) - stub\n", flags);
145 if (flags)
146 return JOYERR_PARMS;
148 return JOYERR_NOERROR;
151 /**************************************************************************
152 * joyGetNumDevs [WINMM.@]
154 UINT WINAPI DECLSPEC_HOTPATCH joyGetNumDevs(void)
156 UINT ret = 0;
157 int i;
159 for (i = 0; i < MAXJOYSTICK; i++) {
160 if (JOY_LoadDriver(i)) {
161 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0, 0);
164 return ret;
167 /**************************************************************************
168 * joyGetDevCapsW [WINMM.@]
170 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetDevCapsW(UINT_PTR wID, LPJOYCAPSW lpCaps, UINT wSize)
172 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
173 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
175 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
176 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
178 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (LPARAM)lpCaps, wSize);
181 /**************************************************************************
182 * joyGetDevCapsA [WINMM.@]
184 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetDevCapsA(UINT_PTR wID, LPJOYCAPSA lpCaps, UINT wSize)
186 JOYCAPSW jcw;
187 MMRESULT ret;
189 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
191 ret = joyGetDevCapsW(wID, &jcw, sizeof(jcw));
193 if (ret == JOYERR_NOERROR)
195 lpCaps->wMid = jcw.wMid;
196 lpCaps->wPid = jcw.wPid;
197 WideCharToMultiByte( CP_ACP, 0, jcw.szPname, -1, lpCaps->szPname,
198 sizeof(lpCaps->szPname), NULL, NULL );
199 lpCaps->wXmin = jcw.wXmin;
200 lpCaps->wXmax = jcw.wXmax;
201 lpCaps->wYmin = jcw.wYmin;
202 lpCaps->wYmax = jcw.wYmax;
203 lpCaps->wZmin = jcw.wZmin;
204 lpCaps->wZmax = jcw.wZmax;
205 lpCaps->wNumButtons = jcw.wNumButtons;
206 lpCaps->wPeriodMin = jcw.wPeriodMin;
207 lpCaps->wPeriodMax = jcw.wPeriodMax;
209 if (wSize >= sizeof(JOYCAPSA)) { /* Win95 extensions ? */
210 lpCaps->wRmin = jcw.wRmin;
211 lpCaps->wRmax = jcw.wRmax;
212 lpCaps->wUmin = jcw.wUmin;
213 lpCaps->wUmax = jcw.wUmax;
214 lpCaps->wVmin = jcw.wVmin;
215 lpCaps->wVmax = jcw.wVmax;
216 lpCaps->wCaps = jcw.wCaps;
217 lpCaps->wMaxAxes = jcw.wMaxAxes;
218 lpCaps->wNumAxes = jcw.wNumAxes;
219 lpCaps->wMaxButtons = jcw.wMaxButtons;
220 WideCharToMultiByte( CP_ACP, 0, jcw.szRegKey, -1, lpCaps->szRegKey,
221 sizeof(lpCaps->szRegKey), NULL, NULL );
222 WideCharToMultiByte( CP_ACP, 0, jcw.szOEMVxD, -1, lpCaps->szOEMVxD,
223 sizeof(lpCaps->szOEMVxD), NULL, NULL );
227 return ret;
230 /**************************************************************************
231 * joyGetPosEx [WINMM.@]
233 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
235 TRACE("(%d, %p);\n", wID, lpInfo);
237 if (!lpInfo) return MMSYSERR_INVALPARAM;
238 if (wID >= MAXJOYSTICK || lpInfo->dwSize < sizeof(JOYINFOEX)) return JOYERR_PARMS;
239 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
241 lpInfo->dwXpos = 0;
242 lpInfo->dwYpos = 0;
243 lpInfo->dwZpos = 0;
244 lpInfo->dwRpos = 0;
245 lpInfo->dwUpos = 0;
246 lpInfo->dwVpos = 0;
247 lpInfo->dwButtons = 0;
248 lpInfo->dwButtonNumber = 0;
249 lpInfo->dwPOV = 0;
250 lpInfo->dwReserved1 = 0;
251 lpInfo->dwReserved2 = 0;
253 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (LPARAM)lpInfo, 0);
256 /**************************************************************************
257 * joyGetPos [WINMM.@]
259 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
261 TRACE("(%d, %p);\n", wID, lpInfo);
263 if (!lpInfo) return MMSYSERR_INVALPARAM;
264 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
265 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
267 lpInfo->wXpos = 0;
268 lpInfo->wYpos = 0;
269 lpInfo->wZpos = 0;
270 lpInfo->wButtons = 0;
272 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (LPARAM)lpInfo, 0);
275 /**************************************************************************
276 * joyGetThreshold [WINMM.@]
278 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
280 TRACE("(%04X, %p);\n", wID, lpThreshold);
282 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
284 *lpThreshold = JOY_Sticks[wID].threshold;
285 return JOYERR_NOERROR;
288 /**************************************************************************
289 * joyReleaseCapture [WINMM.@]
291 MMRESULT WINAPI joyReleaseCapture(UINT wID)
293 TRACE("(%04X);\n", wID);
295 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
296 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
297 if (JOY_Sticks[wID].hCapture)
299 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
300 JOY_Sticks[wID].hCapture = 0;
301 JOY_Sticks[wID].wTimer = 0;
303 else
304 TRACE("Joystick is not captured, ignoring request.\n");
306 return JOYERR_NOERROR;
309 /**************************************************************************
310 * joySetCapture [WINMM.@]
312 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
314 TRACE("(%p, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
316 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
317 if (wPeriod<JOY_PERIOD_MIN) wPeriod = JOY_PERIOD_MIN;
318 else if(wPeriod>JOY_PERIOD_MAX) wPeriod = JOY_PERIOD_MAX;
319 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
321 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
322 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
324 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
325 return JOYERR_UNPLUGGED;
327 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
328 return JOYERR_NOCANDO;
330 JOY_Sticks[wID].hCapture = hWnd;
331 JOY_Sticks[wID].bChanged = bChanged;
333 return JOYERR_NOERROR;
336 /**************************************************************************
337 * joySetThreshold [WINMM.@]
339 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
341 TRACE("(%04X, %d);\n", wID, wThreshold);
343 if (wID >= MAXJOYSTICK || wThreshold > 65535) return MMSYSERR_INVALPARAM;
345 JOY_Sticks[wID].threshold = wThreshold;
347 return JOYERR_NOERROR;