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