Release 20030408.
[wine/gsoc-2012-control.git] / dlls / winmm / joystick.c
blobf05b179988998a34fa272cf67759653999d3c51a
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3 * joystick functions
5 * Copyright 1997 Andreas Mohr
6 * 2000 Wolfgang Schwotzer
7 * Eric Pouech
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.
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.
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
24 #include "config.h"
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #ifdef HAVE_SYS_IOCTL_H
34 #include <sys/ioctl.h>
35 #endif
37 #include "mmsystem.h"
38 #include "winnls.h"
39 #include "wingdi.h"
40 #include "winuser.h"
42 #include "winemm.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
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 */
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;
61 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
63 /**************************************************************************
64 * JOY_LoadDriver [internal]
66 static BOOL JOY_LoadDriver(DWORD dwJoyID)
68 if (dwJoyID >= MAXJOYSTICK)
69 return FALSE;
70 if (JOY_Sticks[dwJoyID].hDriver)
71 return TRUE;
73 JOY_Sticks[dwJoyID].hDriver = OpenDriverA("joystick.drv", 0, dwJoyID);
74 return (JOY_Sticks[dwJoyID].hDriver != 0);
77 /**************************************************************************
78 * JOY_Timer [internal]
80 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT wTimer, DWORD dwTime)
82 int i;
83 WINE_JOYSTICK* joy;
84 JOYINFO ji;
85 LONG pos;
86 unsigned buttonChange;
88 for (i = 0; i < MAXJOYSTICK; i++) {
89 joy = &JOY_Sticks[i];
91 if (joy->hCapture != hWnd) continue;
93 joyGetPos(i, &ji);
94 pos = MAKELONG(ji.wXpos, ji.wYpos);
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;
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;
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;
120 /**************************************************************************
121 * joyGetNumDevs [WINMM.@]
123 UINT WINAPI joyGetNumDevs(void)
125 UINT ret = 0;
126 int i;
128 for (i = 0; i < MAXJOYSTICK; i++) {
129 if (JOY_LoadDriver(i)) {
130 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
133 return ret;
136 /**************************************************************************
137 * joyGetDevCapsA [WINMM.@]
139 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps, UINT wSize)
141 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
142 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
144 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
145 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
147 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (DWORD)lpCaps, wSize);
150 /**************************************************************************
151 * joyGetDevCapsW [WINMM.@]
153 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps, UINT wSize)
155 JOYCAPSA jca;
156 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
158 if (ret != JOYERR_NOERROR) return ret;
159 lpCaps->wMid = jca.wMid;
160 lpCaps->wPid = jca.wPid;
161 MultiByteToWideChar( CP_ACP, 0, jca.szPname, -1, lpCaps->szPname,
162 sizeof(lpCaps->szPname)/sizeof(WCHAR) );
163 lpCaps->wXmin = jca.wXmin;
164 lpCaps->wXmax = jca.wXmax;
165 lpCaps->wYmin = jca.wYmin;
166 lpCaps->wYmax = jca.wYmax;
167 lpCaps->wZmin = jca.wZmin;
168 lpCaps->wZmax = jca.wZmax;
169 lpCaps->wNumButtons = jca.wNumButtons;
170 lpCaps->wPeriodMin = jca.wPeriodMin;
171 lpCaps->wPeriodMax = jca.wPeriodMax;
173 if (wSize >= sizeof(JOYCAPSW)) { /* Win95 extensions ? */
174 lpCaps->wRmin = jca.wRmin;
175 lpCaps->wRmax = jca.wRmax;
176 lpCaps->wUmin = jca.wUmin;
177 lpCaps->wUmax = jca.wUmax;
178 lpCaps->wVmin = jca.wVmin;
179 lpCaps->wVmax = jca.wVmax;
180 lpCaps->wCaps = jca.wCaps;
181 lpCaps->wMaxAxes = jca.wMaxAxes;
182 lpCaps->wNumAxes = jca.wNumAxes;
183 lpCaps->wMaxButtons = jca.wMaxButtons;
184 MultiByteToWideChar( CP_ACP, 0, jca.szRegKey, -1, lpCaps->szRegKey,
185 sizeof(lpCaps->szRegKey)/sizeof(WCHAR) );
186 MultiByteToWideChar( CP_ACP, 0, jca.szOEMVxD, -1, lpCaps->szOEMVxD,
187 sizeof(lpCaps->szOEMVxD)/sizeof(WCHAR) );
190 return ret;
193 /**************************************************************************
194 * joyGetPosEx [WINMM.@]
196 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
198 TRACE("(%d, %p);\n", wID, lpInfo);
200 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
201 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
203 lpInfo->dwXpos = 0;
204 lpInfo->dwYpos = 0;
205 lpInfo->dwZpos = 0;
206 lpInfo->dwRpos = 0;
207 lpInfo->dwUpos = 0;
208 lpInfo->dwVpos = 0;
209 lpInfo->dwButtons = 0;
210 lpInfo->dwButtonNumber = 0;
211 lpInfo->dwPOV = 0;
212 lpInfo->dwReserved1 = 0;
213 lpInfo->dwReserved2 = 0;
215 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
218 /**************************************************************************
219 * joyGetPos [WINMM.@]
221 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
223 TRACE("(%d, %p);\n", wID, lpInfo);
225 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
226 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
228 lpInfo->wXpos = 0;
229 lpInfo->wYpos = 0;
230 lpInfo->wZpos = 0;
231 lpInfo->wButtons = 0;
233 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
236 /**************************************************************************
237 * joyGetThreshold [WINMM.@]
239 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
241 TRACE("(%04X, %p);\n", wID, lpThreshold);
243 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
245 *lpThreshold = JOY_Sticks[wID].threshold;
246 return JOYERR_NOERROR;
249 /**************************************************************************
250 * joyReleaseCapture [WINMM.@]
252 MMRESULT WINAPI joyReleaseCapture(UINT wID)
254 TRACE("(%04X);\n", wID);
256 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
257 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
258 if (!JOY_Sticks[wID].hCapture) return JOYERR_NOCANDO;
260 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
261 JOY_Sticks[wID].hCapture = 0;
262 JOY_Sticks[wID].wTimer = 0;
264 return JOYERR_NOERROR;
267 /**************************************************************************
268 * joySetCapture [WINMM.@]
270 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
272 TRACE("(%p, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
274 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
275 if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
276 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
278 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
279 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
281 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
282 return JOYERR_UNPLUGGED;
284 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
285 return JOYERR_NOCANDO;
287 JOY_Sticks[wID].hCapture = hWnd;
288 JOY_Sticks[wID].bChanged = bChanged;
290 return JOYERR_NOERROR;
293 /**************************************************************************
294 * joySetThreshold [WINMM.@]
296 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
298 TRACE("(%04X, %d);\n", wID, wThreshold);
300 if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
302 JOY_Sticks[wID].threshold = wThreshold;
304 return JOYERR_NOERROR;