Added include protection for unistd.h and sys/time.h.
[wine/multimedia.git] / dlls / winmm / joystick.c
blob43b6b9d226f2c66dcf23ebfce83a7192cb1699df
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 "winbase.h"
39 #include "winnls.h"
40 #include "wingdi.h"
41 #include "winuser.h"
43 #include "wine/mmsystem16.h"
44 #include "winemm.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(mmsys);
50 #define MAXJOYSTICK (JOYSTICKID2 + 1)
51 #define JOY_PERIOD_MIN (10) /* min Capture time period */
52 #define JOY_PERIOD_MAX (1000) /* max Capture time period */
54 typedef struct tagWINE_JOYSTICK {
55 JOYINFO ji;
56 HWND hCapture;
57 UINT wTimer;
58 DWORD threshold;
59 BOOL bChanged;
60 HDRVR hDriver;
61 } WINE_JOYSTICK;
63 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
65 /**************************************************************************
66 * JOY_LoadDriver [internal]
68 static BOOL JOY_LoadDriver(DWORD dwJoyID)
70 if (dwJoyID >= MAXJOYSTICK)
71 return FALSE;
72 if (JOY_Sticks[dwJoyID].hDriver)
73 return TRUE;
75 JOY_Sticks[dwJoyID].hDriver = OpenDriverA("joystick.drv", 0, dwJoyID);
76 return (JOY_Sticks[dwJoyID].hDriver != 0);
79 /**************************************************************************
80 * JOY_Timer [internal]
82 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT wTimer, DWORD dwTime)
84 int i;
85 WINE_JOYSTICK* joy;
86 JOYINFO ji;
87 LONG pos;
88 unsigned buttonChange;
90 for (i = 0; i < MAXJOYSTICK; i++) {
91 joy = &JOY_Sticks[i];
93 if (joy->hCapture != hWnd) continue;
95 joyGetPos(i, &ji);
96 pos = MAKELONG(ji.wXpos, ji.wYpos);
98 if (!joy->bChanged ||
99 abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
100 abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
101 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
102 joy->ji.wXpos = ji.wXpos;
103 joy->ji.wYpos = ji.wYpos;
105 if (!joy->bChanged ||
106 abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
107 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
108 joy->ji.wZpos = ji.wZpos;
110 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
111 if (ji.wButtons & buttonChange)
112 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
113 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
114 if (joy->ji.wButtons & buttonChange)
115 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
116 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
117 joy->ji.wButtons = ji.wButtons;
122 /**************************************************************************
123 * joyGetNumDevs [WINMM.@]
125 UINT WINAPI joyGetNumDevs(void)
127 UINT ret = 0;
128 int i;
130 for (i = 0; i < MAXJOYSTICK; i++) {
131 if (JOY_LoadDriver(i)) {
132 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
135 return ret;
138 /**************************************************************************
139 * joyGetNumDevs [MMSYSTEM.101]
141 UINT16 WINAPI joyGetNumDevs16(void)
143 return joyGetNumDevs();
146 /**************************************************************************
147 * joyGetDevCapsA [WINMM.@]
149 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps, UINT wSize)
151 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
152 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
154 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
155 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
157 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (DWORD)lpCaps, wSize);
160 /**************************************************************************
161 * joyGetDevCapsW [WINMM.@]
163 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps, UINT wSize)
165 JOYCAPSA jca;
166 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
168 if (ret != JOYERR_NOERROR) return ret;
169 lpCaps->wMid = jca.wMid;
170 lpCaps->wPid = jca.wPid;
171 MultiByteToWideChar( CP_ACP, 0, jca.szPname, -1, lpCaps->szPname,
172 sizeof(lpCaps->szPname)/sizeof(WCHAR) );
173 lpCaps->wXmin = jca.wXmin;
174 lpCaps->wXmax = jca.wXmax;
175 lpCaps->wYmin = jca.wYmin;
176 lpCaps->wYmax = jca.wYmax;
177 lpCaps->wZmin = jca.wZmin;
178 lpCaps->wZmax = jca.wZmax;
179 lpCaps->wNumButtons = jca.wNumButtons;
180 lpCaps->wPeriodMin = jca.wPeriodMin;
181 lpCaps->wPeriodMax = jca.wPeriodMax;
183 if (wSize >= sizeof(JOYCAPSW)) { /* Win95 extensions ? */
184 lpCaps->wRmin = jca.wRmin;
185 lpCaps->wRmax = jca.wRmax;
186 lpCaps->wUmin = jca.wUmin;
187 lpCaps->wUmax = jca.wUmax;
188 lpCaps->wVmin = jca.wVmin;
189 lpCaps->wVmax = jca.wVmax;
190 lpCaps->wCaps = jca.wCaps;
191 lpCaps->wMaxAxes = jca.wMaxAxes;
192 lpCaps->wNumAxes = jca.wNumAxes;
193 lpCaps->wMaxButtons = jca.wMaxButtons;
194 MultiByteToWideChar( CP_ACP, 0, jca.szRegKey, -1, lpCaps->szRegKey,
195 sizeof(lpCaps->szRegKey)/sizeof(WCHAR) );
196 MultiByteToWideChar( CP_ACP, 0, jca.szOEMVxD, -1, lpCaps->szOEMVxD,
197 sizeof(lpCaps->szOEMVxD)/sizeof(WCHAR) );
200 return ret;
203 /**************************************************************************
204 * joyGetDevCaps [MMSYSTEM.102]
206 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
208 JOYCAPSA jca;
209 MMRESULT ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
211 if (ret != JOYERR_NOERROR) return ret;
212 lpCaps->wMid = jca.wMid;
213 lpCaps->wPid = jca.wPid;
214 strcpy(lpCaps->szPname, jca.szPname);
215 lpCaps->wXmin = jca.wXmin;
216 lpCaps->wXmax = jca.wXmax;
217 lpCaps->wYmin = jca.wYmin;
218 lpCaps->wYmax = jca.wYmax;
219 lpCaps->wZmin = jca.wZmin;
220 lpCaps->wZmax = jca.wZmax;
221 lpCaps->wNumButtons = jca.wNumButtons;
222 lpCaps->wPeriodMin = jca.wPeriodMin;
223 lpCaps->wPeriodMax = jca.wPeriodMax;
225 if (wSize >= sizeof(JOYCAPS16)) { /* Win95 extensions ? */
226 lpCaps->wRmin = jca.wRmin;
227 lpCaps->wRmax = jca.wRmax;
228 lpCaps->wUmin = jca.wUmin;
229 lpCaps->wUmax = jca.wUmax;
230 lpCaps->wVmin = jca.wVmin;
231 lpCaps->wVmax = jca.wVmax;
232 lpCaps->wCaps = jca.wCaps;
233 lpCaps->wMaxAxes = jca.wMaxAxes;
234 lpCaps->wNumAxes = jca.wNumAxes;
235 lpCaps->wMaxButtons = jca.wMaxButtons;
236 strcpy(lpCaps->szRegKey, jca.szRegKey);
237 strcpy(lpCaps->szOEMVxD, jca.szOEMVxD);
240 return ret;
243 /**************************************************************************
244 * joyGetPosEx [WINMM.@]
246 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
248 TRACE("(%d, %p);\n", wID, lpInfo);
250 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
251 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
253 lpInfo->dwXpos = 0;
254 lpInfo->dwYpos = 0;
255 lpInfo->dwZpos = 0;
256 lpInfo->dwRpos = 0;
257 lpInfo->dwUpos = 0;
258 lpInfo->dwVpos = 0;
259 lpInfo->dwButtons = 0;
260 lpInfo->dwButtonNumber = 0;
261 lpInfo->dwPOV = 0;
262 lpInfo->dwReserved1 = 0;
263 lpInfo->dwReserved2 = 0;
265 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
268 /**************************************************************************
269 * joyGetPosEx [MMSYSTEM.110]
271 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
273 return joyGetPosEx(wID, lpInfo);
276 /**************************************************************************
277 * joyGetPos [WINMM.@]
279 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
281 TRACE("(%d, %p);\n", wID, lpInfo);
283 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
284 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
286 lpInfo->wXpos = 0;
287 lpInfo->wYpos = 0;
288 lpInfo->wZpos = 0;
289 lpInfo->wButtons = 0;
291 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
294 /**************************************************************************
295 * joyGetPos [MMSYSTEM.103]
297 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
299 JOYINFO ji;
300 MMRESULT ret;
302 TRACE("(%d, %p);\n", wID, lpInfo);
304 if ((ret = joyGetPos(wID, &ji)) == JOYERR_NOERROR) {
305 lpInfo->wXpos = ji.wXpos;
306 lpInfo->wYpos = ji.wYpos;
307 lpInfo->wZpos = ji.wZpos;
308 lpInfo->wButtons = ji.wButtons;
310 return ret;
313 /**************************************************************************
314 * joyGetThreshold [WINMM.@]
316 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
318 TRACE("(%04X, %p);\n", wID, lpThreshold);
320 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
322 *lpThreshold = JOY_Sticks[wID].threshold;
323 return JOYERR_NOERROR;
326 /**************************************************************************
327 * joyGetThreshold [MMSYSTEM.104]
329 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
331 TRACE("(%04X, %p);\n", wID, lpThreshold);
333 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
335 *lpThreshold = JOY_Sticks[wID].threshold;
336 return JOYERR_NOERROR;
339 /**************************************************************************
340 * joyReleaseCapture [WINMM.@]
342 MMRESULT WINAPI joyReleaseCapture(UINT wID)
344 TRACE("(%04X);\n", wID);
346 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
347 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
348 if (!JOY_Sticks[wID].hCapture) return JOYERR_NOCANDO;
350 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
351 JOY_Sticks[wID].hCapture = 0;
352 JOY_Sticks[wID].wTimer = 0;
354 return JOYERR_NOERROR;
357 /**************************************************************************
358 * joyReleaseCapture [MMSYSTEM.105]
360 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
362 return joyReleaseCapture(wID);
365 /**************************************************************************
366 * joySetCapture [WINMM.@]
368 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
370 TRACE("(%04X, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
372 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
373 if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
374 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
376 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
377 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
379 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
380 return JOYERR_UNPLUGGED;
382 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
383 return JOYERR_NOCANDO;
385 JOY_Sticks[wID].hCapture = hWnd;
386 JOY_Sticks[wID].bChanged = bChanged;
388 return JOYERR_NOERROR;
391 /**************************************************************************
392 * joySetCapture [MMSYSTEM.106]
394 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd, UINT16 wID, UINT16 wPeriod, BOOL16 bChanged)
396 return joySetCapture16(hWnd, wID, wPeriod, bChanged);
399 /**************************************************************************
400 * joySetThreshold [WINMM.@]
402 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
404 TRACE("(%04X, %d);\n", wID, wThreshold);
406 if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
408 JOY_Sticks[wID].threshold = wThreshold;
410 return JOYERR_NOERROR;
413 /**************************************************************************
414 * joySetThreshold [MMSYSTEM.107]
416 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
418 return joySetThreshold16(wID,wThreshold);
421 /**************************************************************************
422 * joySetCalibration [MMSYSTEM.109]
424 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
426 FIXME("(%04X): stub.\n", wID);
427 return JOYERR_NOCANDO;