1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
5 * Copyright 1997 Andreas Mohr
6 * 2000 Wolfgang Schwotzer
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
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(winmm);
51 #define MAXJOYSTICK (JOYSTICKID2 + 1)
52 #define JOY_PERIOD_MIN (10) /* min Capture time period */
53 #define JOY_PERIOD_MAX (1000) /* max Capture time period */
55 typedef struct tagWINE_JOYSTICK {
64 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
66 /**************************************************************************
67 * JOY_LoadDriver [internal]
69 static BOOL JOY_LoadDriver(DWORD dwJoyID)
71 if (dwJoyID >= MAXJOYSTICK)
73 if (JOY_Sticks[dwJoyID].hDriver)
76 JOY_Sticks[dwJoyID].hDriver = OpenDriverA("joystick.drv", 0, dwJoyID);
77 return (JOY_Sticks[dwJoyID].hDriver != 0);
80 /**************************************************************************
81 * JOY_Timer [internal]
83 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT wTimer, DWORD dwTime)
89 unsigned buttonChange;
91 for (i = 0; i < MAXJOYSTICK; i++) {
94 if (joy->hCapture != hWnd) continue;
97 pos = MAKELONG(ji.wXpos, ji.wYpos);
100 abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
101 abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
102 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
103 joy->ji.wXpos = ji.wXpos;
104 joy->ji.wYpos = ji.wYpos;
106 if (!joy->bChanged ||
107 abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
108 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
109 joy->ji.wZpos = ji.wZpos;
111 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
112 if (ji.wButtons & buttonChange)
113 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
114 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
115 if (joy->ji.wButtons & buttonChange)
116 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
117 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
118 joy->ji.wButtons = ji.wButtons;
123 /**************************************************************************
124 * joyGetNumDevs [WINMM.@]
126 UINT WINAPI joyGetNumDevs(void)
131 for (i = 0; i < MAXJOYSTICK; i++) {
132 if (JOY_LoadDriver(i)) {
133 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
139 /**************************************************************************
140 * joyGetDevCapsA [WINMM.@]
142 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps, UINT wSize)
144 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
145 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
147 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
148 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
150 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (DWORD)lpCaps, wSize);
153 /**************************************************************************
154 * joyGetDevCapsW [WINMM.@]
156 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps, UINT wSize)
159 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
161 if (ret != JOYERR_NOERROR) return ret;
162 lpCaps->wMid = jca.wMid;
163 lpCaps->wPid = jca.wPid;
164 MultiByteToWideChar( CP_ACP, 0, jca.szPname, -1, lpCaps->szPname,
165 sizeof(lpCaps->szPname)/sizeof(WCHAR) );
166 lpCaps->wXmin = jca.wXmin;
167 lpCaps->wXmax = jca.wXmax;
168 lpCaps->wYmin = jca.wYmin;
169 lpCaps->wYmax = jca.wYmax;
170 lpCaps->wZmin = jca.wZmin;
171 lpCaps->wZmax = jca.wZmax;
172 lpCaps->wNumButtons = jca.wNumButtons;
173 lpCaps->wPeriodMin = jca.wPeriodMin;
174 lpCaps->wPeriodMax = jca.wPeriodMax;
176 if (wSize >= sizeof(JOYCAPSW)) { /* Win95 extensions ? */
177 lpCaps->wRmin = jca.wRmin;
178 lpCaps->wRmax = jca.wRmax;
179 lpCaps->wUmin = jca.wUmin;
180 lpCaps->wUmax = jca.wUmax;
181 lpCaps->wVmin = jca.wVmin;
182 lpCaps->wVmax = jca.wVmax;
183 lpCaps->wCaps = jca.wCaps;
184 lpCaps->wMaxAxes = jca.wMaxAxes;
185 lpCaps->wNumAxes = jca.wNumAxes;
186 lpCaps->wMaxButtons = jca.wMaxButtons;
187 MultiByteToWideChar( CP_ACP, 0, jca.szRegKey, -1, lpCaps->szRegKey,
188 sizeof(lpCaps->szRegKey)/sizeof(WCHAR) );
189 MultiByteToWideChar( CP_ACP, 0, jca.szOEMVxD, -1, lpCaps->szOEMVxD,
190 sizeof(lpCaps->szOEMVxD)/sizeof(WCHAR) );
196 /**************************************************************************
197 * joyGetPosEx [WINMM.@]
199 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
201 TRACE("(%d, %p);\n", wID, lpInfo);
203 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
204 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
212 lpInfo->dwButtons = 0;
213 lpInfo->dwButtonNumber = 0;
215 lpInfo->dwReserved1 = 0;
216 lpInfo->dwReserved2 = 0;
218 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
221 /**************************************************************************
222 * joyGetPos [WINMM.@]
224 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
226 TRACE("(%d, %p);\n", wID, lpInfo);
228 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
229 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
234 lpInfo->wButtons = 0;
236 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
239 /**************************************************************************
240 * joyGetThreshold [WINMM.@]
242 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
244 TRACE("(%04X, %p);\n", wID, lpThreshold);
246 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
248 *lpThreshold = JOY_Sticks[wID].threshold;
249 return JOYERR_NOERROR;
252 /**************************************************************************
253 * joyReleaseCapture [WINMM.@]
255 MMRESULT WINAPI joyReleaseCapture(UINT wID)
257 TRACE("(%04X);\n", wID);
259 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
260 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
261 if (!JOY_Sticks[wID].hCapture) return JOYERR_NOCANDO;
263 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
264 JOY_Sticks[wID].hCapture = 0;
265 JOY_Sticks[wID].wTimer = 0;
267 return JOYERR_NOERROR;
270 /**************************************************************************
271 * joySetCapture [WINMM.@]
273 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
275 TRACE("(%p, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
277 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
278 if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
279 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
281 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
282 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
284 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
285 return JOYERR_UNPLUGGED;
287 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
288 return JOYERR_NOCANDO;
290 JOY_Sticks[wID].hCapture = hWnd;
291 JOY_Sticks[wID].bChanged = bChanged;
293 return JOYERR_NOERROR;
296 /**************************************************************************
297 * joySetThreshold [WINMM.@]
299 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
301 TRACE("(%04X, %d);\n", wID, wThreshold);
303 if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
305 JOY_Sticks[wID].threshold = wThreshold;
307 return JOYERR_NOERROR;