Convert HMMIO to a void*.
[wine] / dlls / 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 #include <unistd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #ifdef HAVE_SYS_IOCTL_H
32 #include <sys/ioctl.h>
33 #endif
34
35 #include "mmsystem.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "wingdi.h"
39 #include "winuser.h"
40
41 #include "wine/mmsystem16.h"
42 #include "winemm.h"
43
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(mmsys);
47
48 #define MAXJOYSTICK     (JOYSTICKID2 + 1)
49 #define JOY_PERIOD_MIN  (10)    /* min Capture time period */
50 #define JOY_PERIOD_MAX  (1000)  /* max Capture time period */
51
52 typedef struct tagWINE_JOYSTICK {
53     JOYINFO     ji;
54     HWND        hCapture;
55     UINT        wTimer;
56     DWORD       threshold;
57     BOOL        bChanged;
58     HDRVR       hDriver;
59 } WINE_JOYSTICK;
60
61 static  WINE_JOYSTICK   JOY_Sticks[MAXJOYSTICK];
62
63 /**************************************************************************
64  *                              JOY_LoadDriver          [internal]
65  */
66 static  BOOL JOY_LoadDriver(DWORD dwJoyID)
67 {
68     if (dwJoyID >= MAXJOYSTICK)
69         return FALSE;
70     if (JOY_Sticks[dwJoyID].hDriver)
71         return TRUE;
72
73     JOY_Sticks[dwJoyID].hDriver = OpenDriverA("joystick.drv", 0, dwJoyID);
74     return (JOY_Sticks[dwJoyID].hDriver != 0);
75 }
76
77 /**************************************************************************
78  *                              JOY_Timer               [internal]
79  */
80 static  void    CALLBACK        JOY_Timer(HWND hWnd, UINT wMsg, UINT wTimer, DWORD dwTime)
81 {
82     int                 i;
83     WINE_JOYSTICK*      joy;
84     JOYINFO             ji;
85     LONG                pos;
86     unsigned            buttonChange;
87
88     for (i = 0; i < MAXJOYSTICK; i++) {
89         joy = &JOY_Sticks[i];
90
91         if (joy->hCapture != hWnd) continue;
92
93         joyGetPos(i, &ji);
94         pos = MAKELONG(ji.wXpos, ji.wYpos);
95
96         if (!joy->bChanged ||
97             abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
98             abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
99             SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
100             joy->ji.wXpos = ji.wXpos;
101             joy->ji.wYpos = ji.wYpos;
102         }
103         if (!joy->bChanged ||
104             abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
105             SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
106             joy->ji.wZpos = ji.wZpos;
107         }
108         if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
109             if (ji.wButtons & buttonChange)
110                 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
111                              (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
112             if (joy->ji.wButtons & buttonChange)
113                 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
114                              (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
115             joy->ji.wButtons = ji.wButtons;
116         }
117     }
118 }
119
120 /**************************************************************************
121  *                              joyGetNumDevs           [WINMM.@]
122  */
123 UINT WINAPI joyGetNumDevs(void)
124 {
125     UINT        ret = 0;
126     int         i;
127
128     for (i = 0; i < MAXJOYSTICK; i++) {
129         if (JOY_LoadDriver(i)) {
130             ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
131         }
132     }
133     return ret;
134 }
135
136 /**************************************************************************
137  *                              joyGetNumDevs           [MMSYSTEM.101]
138  */
139 UINT16 WINAPI joyGetNumDevs16(void)
140 {
141     return joyGetNumDevs();
142 }
143
144 /**************************************************************************
145  *                              joyGetDevCapsA          [WINMM.@]
146  */
147 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps, UINT wSize)
148 {
149     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
150     if (!JOY_LoadDriver(wID))   return MMSYSERR_NODRIVER;
151
152     lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
153     lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
154
155     return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (DWORD)lpCaps, wSize);
156 }
157
158 /**************************************************************************
159  *                              joyGetDevCapsW          [WINMM.@]
160  */
161 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps, UINT wSize)
162 {
163     JOYCAPSA    jca;
164     MMRESULT    ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
165
166     if (ret != JOYERR_NOERROR) return ret;
167     lpCaps->wMid = jca.wMid;
168     lpCaps->wPid = jca.wPid;
169     MultiByteToWideChar( CP_ACP, 0, jca.szPname, -1, lpCaps->szPname,
170                          sizeof(lpCaps->szPname)/sizeof(WCHAR) );
171     lpCaps->wXmin = jca.wXmin;
172     lpCaps->wXmax = jca.wXmax;
173     lpCaps->wYmin = jca.wYmin;
174     lpCaps->wYmax = jca.wYmax;
175     lpCaps->wZmin = jca.wZmin;
176     lpCaps->wZmax = jca.wZmax;
177     lpCaps->wNumButtons = jca.wNumButtons;
178     lpCaps->wPeriodMin = jca.wPeriodMin;
179     lpCaps->wPeriodMax = jca.wPeriodMax;
180
181     if (wSize >= sizeof(JOYCAPSW)) { /* Win95 extensions ? */
182         lpCaps->wRmin = jca.wRmin;
183         lpCaps->wRmax = jca.wRmax;
184         lpCaps->wUmin = jca.wUmin;
185         lpCaps->wUmax = jca.wUmax;
186         lpCaps->wVmin = jca.wVmin;
187         lpCaps->wVmax = jca.wVmax;
188         lpCaps->wCaps = jca.wCaps;
189         lpCaps->wMaxAxes = jca.wMaxAxes;
190         lpCaps->wNumAxes = jca.wNumAxes;
191         lpCaps->wMaxButtons = jca.wMaxButtons;
192         MultiByteToWideChar( CP_ACP, 0, jca.szRegKey, -1, lpCaps->szRegKey,
193                          sizeof(lpCaps->szRegKey)/sizeof(WCHAR) );
194         MultiByteToWideChar( CP_ACP, 0, jca.szOEMVxD, -1, lpCaps->szOEMVxD,
195                          sizeof(lpCaps->szOEMVxD)/sizeof(WCHAR) );
196     }
197
198     return ret;
199 }
200
201 /**************************************************************************
202  *                              joyGetDevCaps           [MMSYSTEM.102]
203  */
204 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
205 {
206     JOYCAPSA    jca;
207     MMRESULT    ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
208
209     if (ret != JOYERR_NOERROR) return ret;
210     lpCaps->wMid = jca.wMid;
211     lpCaps->wPid = jca.wPid;
212     strcpy(lpCaps->szPname, jca.szPname);
213     lpCaps->wXmin = jca.wXmin;
214     lpCaps->wXmax = jca.wXmax;
215     lpCaps->wYmin = jca.wYmin;
216     lpCaps->wYmax = jca.wYmax;
217     lpCaps->wZmin = jca.wZmin;
218     lpCaps->wZmax = jca.wZmax;
219     lpCaps->wNumButtons = jca.wNumButtons;
220     lpCaps->wPeriodMin = jca.wPeriodMin;
221     lpCaps->wPeriodMax = jca.wPeriodMax;
222
223     if (wSize >= sizeof(JOYCAPS16)) { /* Win95 extensions ? */
224         lpCaps->wRmin = jca.wRmin;
225         lpCaps->wRmax = jca.wRmax;
226         lpCaps->wUmin = jca.wUmin;
227         lpCaps->wUmax = jca.wUmax;
228         lpCaps->wVmin = jca.wVmin;
229         lpCaps->wVmax = jca.wVmax;
230         lpCaps->wCaps = jca.wCaps;
231         lpCaps->wMaxAxes = jca.wMaxAxes;
232         lpCaps->wNumAxes = jca.wNumAxes;
233         lpCaps->wMaxButtons = jca.wMaxButtons;
234         strcpy(lpCaps->szRegKey, jca.szRegKey);
235         strcpy(lpCaps->szOEMVxD, jca.szOEMVxD);
236     }
237
238     return ret;
239 }
240
241 /**************************************************************************
242  *                              joyGetPosEx             [WINMM.@]
243  */
244 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
245 {
246     TRACE("(%d, %p);\n", wID, lpInfo);
247
248     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
249     if (!JOY_LoadDriver(wID))   return MMSYSERR_NODRIVER;
250
251     lpInfo->dwXpos = 0;
252     lpInfo->dwYpos = 0;
253     lpInfo->dwZpos = 0;
254     lpInfo->dwRpos = 0;
255     lpInfo->dwUpos = 0;
256     lpInfo->dwVpos = 0;
257     lpInfo->dwButtons = 0;
258     lpInfo->dwButtonNumber = 0;
259     lpInfo->dwPOV = 0;
260     lpInfo->dwReserved1 = 0;
261     lpInfo->dwReserved2 = 0;
262
263     return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
264 }
265
266 /**************************************************************************
267  *                              joyGetPosEx           [MMSYSTEM.110]
268  */
269 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
270 {
271     return joyGetPosEx(wID, lpInfo);
272 }
273
274 /**************************************************************************
275  *                              joyGetPos               [WINMM.@]
276  */
277 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
278 {
279     TRACE("(%d, %p);\n", wID, lpInfo);
280
281     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
282     if (!JOY_LoadDriver(wID))   return MMSYSERR_NODRIVER;
283
284     lpInfo->wXpos = 0;
285     lpInfo->wYpos = 0;
286     lpInfo->wZpos = 0;
287     lpInfo->wButtons = 0;
288
289     return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
290 }
291
292 /**************************************************************************
293  *                              joyGetPos               [MMSYSTEM.103]
294  */
295 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
296 {
297     JOYINFO     ji;
298     MMRESULT    ret;
299
300     TRACE("(%d, %p);\n", wID, lpInfo);
301
302     if ((ret = joyGetPos(wID, &ji)) == JOYERR_NOERROR) {
303         lpInfo->wXpos = ji.wXpos;
304         lpInfo->wYpos = ji.wYpos;
305         lpInfo->wZpos = ji.wZpos;
306         lpInfo->wButtons = ji.wButtons;
307     }
308     return ret;
309 }
310
311 /**************************************************************************
312  *                              joyGetThreshold         [WINMM.@]
313  */
314 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
315 {
316     TRACE("(%04X, %p);\n", wID, lpThreshold);
317
318     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
319
320     *lpThreshold = JOY_Sticks[wID].threshold;
321     return JOYERR_NOERROR;
322 }
323
324 /**************************************************************************
325  *                              joyGetThreshold         [MMSYSTEM.104]
326  */
327 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
328 {
329     TRACE("(%04X, %p);\n", wID, lpThreshold);
330
331     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
332
333     *lpThreshold = JOY_Sticks[wID].threshold;
334     return JOYERR_NOERROR;
335 }
336
337 /**************************************************************************
338  *                              joyReleaseCapture       [WINMM.@]
339  */
340 MMRESULT WINAPI joyReleaseCapture(UINT wID)
341 {
342     TRACE("(%04X);\n", wID);
343
344     if (wID >= MAXJOYSTICK)             return JOYERR_PARMS;
345     if (!JOY_LoadDriver(wID))           return MMSYSERR_NODRIVER;
346     if (!JOY_Sticks[wID].hCapture)      return JOYERR_NOCANDO;
347
348     KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
349     JOY_Sticks[wID].hCapture = 0;
350     JOY_Sticks[wID].wTimer = 0;
351
352     return JOYERR_NOERROR;
353 }
354
355 /**************************************************************************
356  *                              joyReleaseCapture       [MMSYSTEM.105]
357  */
358 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
359 {
360     return joyReleaseCapture(wID);
361 }
362
363 /**************************************************************************
364  *                              joySetCapture           [WINMM.@]
365  */
366 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
367 {
368     TRACE("(%04X, %04X, %d, %d);\n",  hWnd, wID, wPeriod, bChanged);
369
370     if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
371     if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
372     if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
373
374     if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
375         return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
376
377     if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
378         return JOYERR_UNPLUGGED;
379
380     if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
381         return JOYERR_NOCANDO;
382
383     JOY_Sticks[wID].hCapture = hWnd;
384     JOY_Sticks[wID].bChanged = bChanged;
385
386     return JOYERR_NOERROR;
387 }
388
389 /**************************************************************************
390  *                              joySetCapture           [MMSYSTEM.106]
391  */
392 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd, UINT16 wID, UINT16 wPeriod, BOOL16 bChanged)
393 {
394     return joySetCapture16(hWnd, wID, wPeriod, bChanged);
395 }
396
397 /**************************************************************************
398  *                              joySetThreshold         [WINMM.@]
399  */
400 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
401 {
402     TRACE("(%04X, %d);\n", wID, wThreshold);
403
404     if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
405
406     JOY_Sticks[wID].threshold = wThreshold;
407
408     return JOYERR_NOERROR;
409 }
410
411 /**************************************************************************
412  *                              joySetThreshold         [MMSYSTEM.107]
413  */
414 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
415 {
416     return joySetThreshold16(wID,wThreshold);
417 }
418
419 /**************************************************************************
420  *                              joySetCalibration       [MMSYSTEM.109]
421  */
422 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
423 {
424     FIXME("(%04X): stub.\n", wID);
425     return JOYERR_NOCANDO;
426 }