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