makefiles: Generate rules for installing IDL include files.
[wine.git] / dlls / winmm / joystick.c
blob6ac67f40ad52aae02c04914b35b50eaf8bce69cd
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 JOYINFO ji;
88 LONG pos;
89 unsigned buttonChange;
91 for (i = 0; i < MAXJOYSTICK; i++) {
92 joy = &JOY_Sticks[i];
94 if (joy->hCapture != hWnd) continue;
96 joyGetPos(i, &ji);
97 pos = MAKELONG(ji.wXpos, ji.wYpos);
99 if (!joy->bChanged ||
100 abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
101 abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
102 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
103 joy->ji.wXpos = ji.wXpos;
104 joy->ji.wYpos = ji.wYpos;
106 if (!joy->bChanged ||
107 abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
108 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
109 joy->ji.wZpos = ji.wZpos;
111 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
112 if (ji.wButtons & buttonChange)
113 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
114 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
115 if (joy->ji.wButtons & buttonChange)
116 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
117 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
118 joy->ji.wButtons = ji.wButtons;
123 /**************************************************************************
124 * joyConfigChanged [WINMM.@]
126 MMRESULT WINAPI joyConfigChanged(DWORD flags)
128 FIXME("(%x) - stub\n", flags);
130 if (flags)
131 return JOYERR_PARMS;
133 return JOYERR_NOERROR;
136 /**************************************************************************
137 * joyGetNumDevs [WINMM.@]
139 UINT WINAPI DECLSPEC_HOTPATCH joyGetNumDevs(void)
141 UINT ret = 0;
142 int i;
144 for (i = 0; i < MAXJOYSTICK; i++) {
145 if (JOY_LoadDriver(i)) {
146 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0, 0);
149 return ret;
152 /**************************************************************************
153 * joyGetDevCapsW [WINMM.@]
155 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetDevCapsW(UINT_PTR wID, LPJOYCAPSW lpCaps, UINT wSize)
157 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
158 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
160 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
161 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
163 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (LPARAM)lpCaps, wSize);
166 /**************************************************************************
167 * joyGetDevCapsA [WINMM.@]
169 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetDevCapsA(UINT_PTR wID, LPJOYCAPSA lpCaps, UINT wSize)
171 JOYCAPSW jcw;
172 MMRESULT ret;
174 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
176 ret = joyGetDevCapsW(wID, &jcw, sizeof(jcw));
178 if (ret == JOYERR_NOERROR)
180 lpCaps->wMid = jcw.wMid;
181 lpCaps->wPid = jcw.wPid;
182 WideCharToMultiByte( CP_ACP, 0, jcw.szPname, -1, lpCaps->szPname,
183 sizeof(lpCaps->szPname), NULL, NULL );
184 lpCaps->wXmin = jcw.wXmin;
185 lpCaps->wXmax = jcw.wXmax;
186 lpCaps->wYmin = jcw.wYmin;
187 lpCaps->wYmax = jcw.wYmax;
188 lpCaps->wZmin = jcw.wZmin;
189 lpCaps->wZmax = jcw.wZmax;
190 lpCaps->wNumButtons = jcw.wNumButtons;
191 lpCaps->wPeriodMin = jcw.wPeriodMin;
192 lpCaps->wPeriodMax = jcw.wPeriodMax;
194 if (wSize >= sizeof(JOYCAPSA)) { /* Win95 extensions ? */
195 lpCaps->wRmin = jcw.wRmin;
196 lpCaps->wRmax = jcw.wRmax;
197 lpCaps->wUmin = jcw.wUmin;
198 lpCaps->wUmax = jcw.wUmax;
199 lpCaps->wVmin = jcw.wVmin;
200 lpCaps->wVmax = jcw.wVmax;
201 lpCaps->wCaps = jcw.wCaps;
202 lpCaps->wMaxAxes = jcw.wMaxAxes;
203 lpCaps->wNumAxes = jcw.wNumAxes;
204 lpCaps->wMaxButtons = jcw.wMaxButtons;
205 WideCharToMultiByte( CP_ACP, 0, jcw.szRegKey, -1, lpCaps->szRegKey,
206 sizeof(lpCaps->szRegKey), NULL, NULL );
207 WideCharToMultiByte( CP_ACP, 0, jcw.szOEMVxD, -1, lpCaps->szOEMVxD,
208 sizeof(lpCaps->szOEMVxD), NULL, NULL );
212 return ret;
215 /**************************************************************************
216 * joyGetPosEx [WINMM.@]
218 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
220 TRACE("(%d, %p);\n", wID, lpInfo);
222 if (!lpInfo) return MMSYSERR_INVALPARAM;
223 if (wID >= MAXJOYSTICK || lpInfo->dwSize < sizeof(JOYINFOEX)) return JOYERR_PARMS;
224 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
226 lpInfo->dwXpos = 0;
227 lpInfo->dwYpos = 0;
228 lpInfo->dwZpos = 0;
229 lpInfo->dwRpos = 0;
230 lpInfo->dwUpos = 0;
231 lpInfo->dwVpos = 0;
232 lpInfo->dwButtons = 0;
233 lpInfo->dwButtonNumber = 0;
234 lpInfo->dwPOV = 0;
235 lpInfo->dwReserved1 = 0;
236 lpInfo->dwReserved2 = 0;
238 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (LPARAM)lpInfo, 0);
241 /**************************************************************************
242 * joyGetPos [WINMM.@]
244 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
246 TRACE("(%d, %p);\n", wID, lpInfo);
248 if (!lpInfo) return MMSYSERR_INVALPARAM;
249 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
250 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
252 lpInfo->wXpos = 0;
253 lpInfo->wYpos = 0;
254 lpInfo->wZpos = 0;
255 lpInfo->wButtons = 0;
257 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (LPARAM)lpInfo, 0);
260 /**************************************************************************
261 * joyGetThreshold [WINMM.@]
263 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
265 TRACE("(%04X, %p);\n", wID, lpThreshold);
267 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
269 *lpThreshold = JOY_Sticks[wID].threshold;
270 return JOYERR_NOERROR;
273 /**************************************************************************
274 * joyReleaseCapture [WINMM.@]
276 MMRESULT WINAPI joyReleaseCapture(UINT wID)
278 TRACE("(%04X);\n", wID);
280 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
281 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
282 if (JOY_Sticks[wID].hCapture)
284 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
285 JOY_Sticks[wID].hCapture = 0;
286 JOY_Sticks[wID].wTimer = 0;
288 else
289 TRACE("Joystick is not captured, ignoring request.\n");
291 return JOYERR_NOERROR;
294 /**************************************************************************
295 * joySetCapture [WINMM.@]
297 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
299 TRACE("(%p, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
301 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
302 if (wPeriod<JOY_PERIOD_MIN) wPeriod = JOY_PERIOD_MIN;
303 else if(wPeriod>JOY_PERIOD_MAX) wPeriod = JOY_PERIOD_MAX;
304 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
306 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
307 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
309 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
310 return JOYERR_UNPLUGGED;
312 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
313 return JOYERR_NOCANDO;
315 JOY_Sticks[wID].hCapture = hWnd;
316 JOY_Sticks[wID].bChanged = bChanged;
318 return JOYERR_NOERROR;
321 /**************************************************************************
322 * joySetThreshold [WINMM.@]
324 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
326 TRACE("(%04X, %d);\n", wID, wThreshold);
328 if (wID >= MAXJOYSTICK || wThreshold > 65535) return MMSYSERR_INVALPARAM;
330 JOY_Sticks[wID].threshold = wThreshold;
332 return JOYERR_NOERROR;