The real, definitive, Visual C++ support branch. Accept no substitutes
[reactos.git] / dll / win32 / winmm / joystick.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * joystick functions
4 *
5 * Copyright 1997 Andreas Mohr
6 * 2000 Wolfgang Schwotzer
7 * Eric Pouech
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include "config.h"
25
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
37
38 #include "windef.h"
39 #include "winbase.h"
40 #include "mmsystem.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "winemm.h"
45
46 #include "mmddk.h"
47
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
51
52 #define MAXJOYSTICK (JOYSTICKID2 + 1)
53 #define JOY_PERIOD_MIN (10) /* min Capture time period */
54 #define JOY_PERIOD_MAX (1000) /* max Capture time period */
55
56 typedef struct tagWINE_JOYSTICK {
57 JOYINFO ji;
58 HWND hCapture;
59 UINT wTimer;
60 DWORD threshold;
61 BOOL bChanged;
62 HDRVR hDriver;
63 } WINE_JOYSTICK;
64
65 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
66
67 /**************************************************************************
68 * JOY_LoadDriver [internal]
69 */
70 static BOOL JOY_LoadDriver(DWORD dwJoyID)
71 {
72 if (dwJoyID >= MAXJOYSTICK)
73 return FALSE;
74 if (JOY_Sticks[dwJoyID].hDriver)
75 return TRUE;
76
77 JOY_Sticks[dwJoyID].hDriver = OpenDriverA("joystick.drv", 0, dwJoyID);
78 return (JOY_Sticks[dwJoyID].hDriver != 0);
79 }
80
81 /**************************************************************************
82 * JOY_Timer [internal]
83 */
84 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT wTimer, DWORD dwTime)
85 {
86 int i;
87 WINE_JOYSTICK* joy;
88 JOYINFO ji;
89 LONG pos;
90 unsigned buttonChange;
91
92 for (i = 0; i < MAXJOYSTICK; i++) {
93 joy = &JOY_Sticks[i];
94
95 if (joy->hCapture != hWnd) continue;
96
97 joyGetPos(i, &ji);
98 pos = MAKELONG(ji.wXpos, ji.wYpos);
99
100 if (!joy->bChanged ||
101 abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
102 abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
103 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
104 joy->ji.wXpos = ji.wXpos;
105 joy->ji.wYpos = ji.wYpos;
106 }
107 if (!joy->bChanged ||
108 abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
109 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
110 joy->ji.wZpos = ji.wZpos;
111 }
112 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
113 if (ji.wButtons & buttonChange)
114 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
115 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
116 if (joy->ji.wButtons & buttonChange)
117 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
118 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
119 joy->ji.wButtons = ji.wButtons;
120 }
121 }
122 }
123
124 /**************************************************************************
125 * joyGetNumDevs [WINMM.@]
126 */
127 UINT WINAPI joyGetNumDevs(void)
128 {
129 UINT ret = 0;
130 int i;
131
132 for (i = 0; i < MAXJOYSTICK; i++) {
133 if (JOY_LoadDriver(i)) {
134 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
135 }
136 }
137 return ret;
138 }
139
140 /**************************************************************************
141 * joyGetDevCapsW [WINMM.@]
142 */
143 MMRESULT WINAPI joyGetDevCapsW(UINT_PTR wID, LPJOYCAPSW lpCaps, UINT wSize)
144 {
145 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
146 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
147
148 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
149 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
150
151 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (DWORD)lpCaps, wSize);
152 }
153
154 /**************************************************************************
155 * joyGetDevCapsA [WINMM.@]
156 */
157 MMRESULT WINAPI joyGetDevCapsA(UINT_PTR wID, LPJOYCAPSA lpCaps, UINT wSize)
158 {
159 JOYCAPSW jcw;
160 MMRESULT ret;
161
162 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
163
164 ret = joyGetDevCapsW(wID, &jcw, sizeof(jcw));
165
166 if (ret == JOYERR_NOERROR)
167 {
168 lpCaps->wMid = jcw.wMid;
169 lpCaps->wPid = jcw.wPid;
170 WideCharToMultiByte( CP_ACP, 0, jcw.szPname, -1, lpCaps->szPname,
171 sizeof(lpCaps->szPname), NULL, NULL );
172 lpCaps->wXmin = jcw.wXmin;
173 lpCaps->wXmax = jcw.wXmax;
174 lpCaps->wYmin = jcw.wYmin;
175 lpCaps->wYmax = jcw.wYmax;
176 lpCaps->wZmin = jcw.wZmin;
177 lpCaps->wZmax = jcw.wZmax;
178 lpCaps->wNumButtons = jcw.wNumButtons;
179 lpCaps->wPeriodMin = jcw.wPeriodMin;
180 lpCaps->wPeriodMax = jcw.wPeriodMax;
181
182 if (wSize >= sizeof(JOYCAPSA)) { /* Win95 extensions ? */
183 lpCaps->wRmin = jcw.wRmin;
184 lpCaps->wRmax = jcw.wRmax;
185 lpCaps->wUmin = jcw.wUmin;
186 lpCaps->wUmax = jcw.wUmax;
187 lpCaps->wVmin = jcw.wVmin;
188 lpCaps->wVmax = jcw.wVmax;
189 lpCaps->wCaps = jcw.wCaps;
190 lpCaps->wMaxAxes = jcw.wMaxAxes;
191 lpCaps->wNumAxes = jcw.wNumAxes;
192 lpCaps->wMaxButtons = jcw.wMaxButtons;
193 WideCharToMultiByte( CP_ACP, 0, jcw.szRegKey, -1, lpCaps->szRegKey,
194 sizeof(lpCaps->szRegKey), NULL, NULL );
195 WideCharToMultiByte( CP_ACP, 0, jcw.szOEMVxD, -1, lpCaps->szOEMVxD,
196 sizeof(lpCaps->szOEMVxD), NULL, NULL );
197 }
198 }
199
200 return ret;
201 }
202
203 /**************************************************************************
204 * joyGetPosEx [WINMM.@]
205 */
206 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
207 {
208 TRACE("(%d, %p);\n", wID, lpInfo);
209
210 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
211 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
212
213 lpInfo->dwXpos = 0;
214 lpInfo->dwYpos = 0;
215 lpInfo->dwZpos = 0;
216 lpInfo->dwRpos = 0;
217 lpInfo->dwUpos = 0;
218 lpInfo->dwVpos = 0;
219 lpInfo->dwButtons = 0;
220 lpInfo->dwButtonNumber = 0;
221 lpInfo->dwPOV = 0;
222 lpInfo->dwReserved1 = 0;
223 lpInfo->dwReserved2 = 0;
224
225 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
226 }
227
228 /**************************************************************************
229 * joyGetPos [WINMM.@]
230 */
231 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
232 {
233 TRACE("(%d, %p);\n", wID, lpInfo);
234
235 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
236 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
237
238 lpInfo->wXpos = 0;
239 lpInfo->wYpos = 0;
240 lpInfo->wZpos = 0;
241 lpInfo->wButtons = 0;
242
243 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
244 }
245
246 /**************************************************************************
247 * joyGetThreshold [WINMM.@]
248 */
249 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
250 {
251 TRACE("(%04X, %p);\n", wID, lpThreshold);
252
253 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
254
255 *lpThreshold = JOY_Sticks[wID].threshold;
256 return JOYERR_NOERROR;
257 }
258
259 /**************************************************************************
260 * joyReleaseCapture [WINMM.@]
261 */
262 MMRESULT WINAPI joyReleaseCapture(UINT wID)
263 {
264 TRACE("(%04X);\n", wID);
265
266 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
267 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
268 if (!JOY_Sticks[wID].hCapture) return JOYERR_NOCANDO;
269
270 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
271 JOY_Sticks[wID].hCapture = 0;
272 JOY_Sticks[wID].wTimer = 0;
273
274 return JOYERR_NOERROR;
275 }
276
277 /**************************************************************************
278 * joySetCapture [WINMM.@]
279 */
280 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
281 {
282 TRACE("(%p, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
283
284 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
285 if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
286 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
287
288 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
289 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
290
291 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
292 return JOYERR_UNPLUGGED;
293
294 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
295 return JOYERR_NOCANDO;
296
297 JOY_Sticks[wID].hCapture = hWnd;
298 JOY_Sticks[wID].bChanged = bChanged;
299
300 return JOYERR_NOERROR;
301 }
302
303 /**************************************************************************
304 * joySetThreshold [WINMM.@]
305 */
306 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
307 {
308 TRACE("(%04X, %d);\n", wID, wThreshold);
309
310 if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
311
312 JOY_Sticks[wID].threshold = wThreshold;
313
314 return JOYERR_NOERROR;
315 }