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