storage.dll16: Fix get_nth_next_small_blocknr.
[wine.git] / dlls / winmm / joystick.c
blob8a52942ca65c115378df5d9f7f756bcaf10f041b
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 "config.h"
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
36 #endif
38 #include "windef.h"
39 #include "winbase.h"
40 #include "mmsystem.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
45 #include "mmddk.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
51 #define MAXJOYSTICK (JOYSTICKID2 + 30)
52 #define JOY_PERIOD_MIN (10) /* min Capture time period */
53 #define JOY_PERIOD_MAX (1000) /* max Capture time period */
55 typedef struct tagWINE_JOYSTICK {
56 JOYINFO ji;
57 HWND hCapture;
58 UINT wTimer;
59 DWORD threshold;
60 BOOL bChanged;
61 HDRVR hDriver;
62 } WINE_JOYSTICK;
64 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
66 /**************************************************************************
67 * JOY_LoadDriver [internal]
69 static BOOL JOY_LoadDriver(DWORD dwJoyID)
71 if (dwJoyID >= MAXJOYSTICK)
72 return FALSE;
73 if (JOY_Sticks[dwJoyID].hDriver)
74 return TRUE;
76 JOY_Sticks[dwJoyID].hDriver = OpenDriverA("winejoystick.drv", 0, dwJoyID);
77 return (JOY_Sticks[dwJoyID].hDriver != 0);
80 /**************************************************************************
81 * JOY_Timer [internal]
83 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT_PTR wTimer, DWORD dwTime)
85 int i;
86 WINE_JOYSTICK* joy;
87 MMRESULT res;
88 JOYINFO ji;
89 LONG pos;
90 unsigned buttonChange;
92 for (i = 0; i < MAXJOYSTICK; i++) {
93 joy = &JOY_Sticks[i];
95 if (joy->hCapture != hWnd) continue;
97 res = joyGetPos(i, &ji);
98 if (res != JOYERR_NOERROR) {
99 WARN("joyGetPos failed: %08x\n", res);
100 continue;
103 pos = MAKELONG(ji.wXpos, ji.wYpos);
105 if (!joy->bChanged ||
106 abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
107 abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
108 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
109 joy->ji.wXpos = ji.wXpos;
110 joy->ji.wYpos = ji.wYpos;
112 if (!joy->bChanged ||
113 abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
114 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
115 joy->ji.wZpos = ji.wZpos;
117 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
118 if (ji.wButtons & buttonChange)
119 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
120 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
121 if (joy->ji.wButtons & buttonChange)
122 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
123 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
124 joy->ji.wButtons = ji.wButtons;
129 /**************************************************************************
130 * joyConfigChanged [WINMM.@]
132 MMRESULT WINAPI joyConfigChanged(DWORD flags)
134 FIXME("(%x) - stub\n", flags);
136 if (flags)
137 return JOYERR_PARMS;
139 return JOYERR_NOERROR;
142 /**************************************************************************
143 * joyGetNumDevs [WINMM.@]
145 UINT WINAPI DECLSPEC_HOTPATCH joyGetNumDevs(void)
147 UINT ret = 0;
148 int i;
150 for (i = 0; i < MAXJOYSTICK; i++) {
151 if (JOY_LoadDriver(i)) {
152 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0, 0);
155 return ret;
158 /**************************************************************************
159 * joyGetDevCapsW [WINMM.@]
161 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetDevCapsW(UINT_PTR wID, LPJOYCAPSW lpCaps, UINT wSize)
163 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
164 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
166 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
167 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
169 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (LPARAM)lpCaps, wSize);
172 /**************************************************************************
173 * joyGetDevCapsA [WINMM.@]
175 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetDevCapsA(UINT_PTR wID, LPJOYCAPSA lpCaps, UINT wSize)
177 JOYCAPSW jcw;
178 MMRESULT ret;
180 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
182 ret = joyGetDevCapsW(wID, &jcw, sizeof(jcw));
184 if (ret == JOYERR_NOERROR)
186 lpCaps->wMid = jcw.wMid;
187 lpCaps->wPid = jcw.wPid;
188 WideCharToMultiByte( CP_ACP, 0, jcw.szPname, -1, lpCaps->szPname,
189 sizeof(lpCaps->szPname), NULL, NULL );
190 lpCaps->wXmin = jcw.wXmin;
191 lpCaps->wXmax = jcw.wXmax;
192 lpCaps->wYmin = jcw.wYmin;
193 lpCaps->wYmax = jcw.wYmax;
194 lpCaps->wZmin = jcw.wZmin;
195 lpCaps->wZmax = jcw.wZmax;
196 lpCaps->wNumButtons = jcw.wNumButtons;
197 lpCaps->wPeriodMin = jcw.wPeriodMin;
198 lpCaps->wPeriodMax = jcw.wPeriodMax;
200 if (wSize >= sizeof(JOYCAPSA)) { /* Win95 extensions ? */
201 lpCaps->wRmin = jcw.wRmin;
202 lpCaps->wRmax = jcw.wRmax;
203 lpCaps->wUmin = jcw.wUmin;
204 lpCaps->wUmax = jcw.wUmax;
205 lpCaps->wVmin = jcw.wVmin;
206 lpCaps->wVmax = jcw.wVmax;
207 lpCaps->wCaps = jcw.wCaps;
208 lpCaps->wMaxAxes = jcw.wMaxAxes;
209 lpCaps->wNumAxes = jcw.wNumAxes;
210 lpCaps->wMaxButtons = jcw.wMaxButtons;
211 WideCharToMultiByte( CP_ACP, 0, jcw.szRegKey, -1, lpCaps->szRegKey,
212 sizeof(lpCaps->szRegKey), NULL, NULL );
213 WideCharToMultiByte( CP_ACP, 0, jcw.szOEMVxD, -1, lpCaps->szOEMVxD,
214 sizeof(lpCaps->szOEMVxD), NULL, NULL );
218 return ret;
221 /**************************************************************************
222 * joyGetPosEx [WINMM.@]
224 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
226 TRACE("(%d, %p);\n", wID, lpInfo);
228 if (!lpInfo) return MMSYSERR_INVALPARAM;
229 if (wID >= MAXJOYSTICK || lpInfo->dwSize < sizeof(JOYINFOEX)) return JOYERR_PARMS;
230 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
232 lpInfo->dwXpos = 0;
233 lpInfo->dwYpos = 0;
234 lpInfo->dwZpos = 0;
235 lpInfo->dwRpos = 0;
236 lpInfo->dwUpos = 0;
237 lpInfo->dwVpos = 0;
238 lpInfo->dwButtons = 0;
239 lpInfo->dwButtonNumber = 0;
240 lpInfo->dwPOV = 0;
241 lpInfo->dwReserved1 = 0;
242 lpInfo->dwReserved2 = 0;
244 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (LPARAM)lpInfo, 0);
247 /**************************************************************************
248 * joyGetPos [WINMM.@]
250 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
252 TRACE("(%d, %p);\n", wID, lpInfo);
254 if (!lpInfo) return MMSYSERR_INVALPARAM;
255 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
256 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
258 lpInfo->wXpos = 0;
259 lpInfo->wYpos = 0;
260 lpInfo->wZpos = 0;
261 lpInfo->wButtons = 0;
263 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (LPARAM)lpInfo, 0);
266 /**************************************************************************
267 * joyGetThreshold [WINMM.@]
269 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
271 TRACE("(%04X, %p);\n", wID, lpThreshold);
273 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
275 *lpThreshold = JOY_Sticks[wID].threshold;
276 return JOYERR_NOERROR;
279 /**************************************************************************
280 * joyReleaseCapture [WINMM.@]
282 MMRESULT WINAPI joyReleaseCapture(UINT wID)
284 TRACE("(%04X);\n", wID);
286 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
287 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
288 if (JOY_Sticks[wID].hCapture)
290 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
291 JOY_Sticks[wID].hCapture = 0;
292 JOY_Sticks[wID].wTimer = 0;
294 else
295 TRACE("Joystick is not captured, ignoring request.\n");
297 return JOYERR_NOERROR;
300 /**************************************************************************
301 * joySetCapture [WINMM.@]
303 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
305 TRACE("(%p, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
307 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
308 if (wPeriod<JOY_PERIOD_MIN) wPeriod = JOY_PERIOD_MIN;
309 else if(wPeriod>JOY_PERIOD_MAX) wPeriod = JOY_PERIOD_MAX;
310 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
312 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
313 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
315 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
316 return JOYERR_UNPLUGGED;
318 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
319 return JOYERR_NOCANDO;
321 JOY_Sticks[wID].hCapture = hWnd;
322 JOY_Sticks[wID].bChanged = bChanged;
324 return JOYERR_NOERROR;
327 /**************************************************************************
328 * joySetThreshold [WINMM.@]
330 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
332 TRACE("(%04X, %d);\n", wID, wThreshold);
334 if (wID >= MAXJOYSTICK || wThreshold > 65535) return MMSYSERR_INVALPARAM;
336 JOY_Sticks[wID].threshold = wThreshold;
338 return JOYERR_NOERROR;