riched32: Renamed the dlls/richedit directory to dlls/riched32.
[wine/multimedia.git] / dlls / winmm / joystick.c
blob744d1f8fda1ed4cd6ccb7ed9f7968e81c639c092
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 <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #ifdef HAVE_SYS_IOCTL_H
35 #include <sys/ioctl.h>
36 #endif
38 #include "windef.h"
39 #include "winbase.h"
40 #include "mmsystem.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "winnls.h"
45 #include "mmddk.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 {
56 JOYINFO ji;
57 HWND hCapture;
58 UINT wTimer;
59 DWORD threshold;
60 BOOL bChanged;
61 HDRVR hDriver;
62 } WINE_JOYSTICK;
64 static WINE_JOYSTICK JOY_Sticks[MAXJOYSTICK];
66 /**************************************************************************
67 * JOY_LoadDriver [internal]
69 static BOOL JOY_LoadDriver(DWORD dwJoyID)
71 if (dwJoyID >= MAXJOYSTICK)
72 return FALSE;
73 if (JOY_Sticks[dwJoyID].hDriver)
74 return TRUE;
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)
85 int i;
86 WINE_JOYSTICK* joy;
87 JOYINFO ji;
88 LONG pos;
89 unsigned buttonChange;
91 for (i = 0; i < MAXJOYSTICK; i++) {
92 joy = &JOY_Sticks[i];
94 if (joy->hCapture != hWnd) continue;
96 joyGetPos(i, &ji);
97 pos = MAKELONG(ji.wXpos, ji.wYpos);
99 if (!joy->bChanged ||
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)
128 UINT ret = 0;
129 int i;
131 for (i = 0; i < MAXJOYSTICK; i++) {
132 if (JOY_LoadDriver(i)) {
133 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
136 return ret;
139 /**************************************************************************
140 * joyGetDevCapsW [WINMM.@]
142 MMRESULT WINAPI joyGetDevCapsW(UINT_PTR wID, LPJOYCAPSW 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 * joyGetDevCapsA [WINMM.@]
156 MMRESULT WINAPI joyGetDevCapsA(UINT_PTR wID, LPJOYCAPSA lpCaps, UINT wSize)
158 JOYCAPSW jcw;
159 MMRESULT ret;
161 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
163 ret = joyGetDevCapsW(wID, &jcw, sizeof(jcw));
165 if (ret == JOYERR_NOERROR)
167 lpCaps->wMid = jcw.wMid;
168 lpCaps->wPid = jcw.wPid;
169 WideCharToMultiByte( CP_ACP, 0, jcw.szPname, -1, lpCaps->szPname,
170 sizeof(lpCaps->szPname), NULL, NULL );
171 lpCaps->wXmin = jcw.wXmin;
172 lpCaps->wXmax = jcw.wXmax;
173 lpCaps->wYmin = jcw.wYmin;
174 lpCaps->wYmax = jcw.wYmax;
175 lpCaps->wZmin = jcw.wZmin;
176 lpCaps->wZmax = jcw.wZmax;
177 lpCaps->wNumButtons = jcw.wNumButtons;
178 lpCaps->wPeriodMin = jcw.wPeriodMin;
179 lpCaps->wPeriodMax = jcw.wPeriodMax;
181 if (wSize >= sizeof(JOYCAPSA)) { /* Win95 extensions ? */
182 lpCaps->wRmin = jcw.wRmin;
183 lpCaps->wRmax = jcw.wRmax;
184 lpCaps->wUmin = jcw.wUmin;
185 lpCaps->wUmax = jcw.wUmax;
186 lpCaps->wVmin = jcw.wVmin;
187 lpCaps->wVmax = jcw.wVmax;
188 lpCaps->wCaps = jcw.wCaps;
189 lpCaps->wMaxAxes = jcw.wMaxAxes;
190 lpCaps->wNumAxes = jcw.wNumAxes;
191 lpCaps->wMaxButtons = jcw.wMaxButtons;
192 WideCharToMultiByte( CP_ACP, 0, jcw.szRegKey, -1, lpCaps->szRegKey,
193 sizeof(lpCaps->szRegKey), NULL, NULL );
194 WideCharToMultiByte( CP_ACP, 0, jcw.szOEMVxD, -1, lpCaps->szOEMVxD,
195 sizeof(lpCaps->szOEMVxD), NULL, NULL );
199 return ret;
202 /**************************************************************************
203 * joyGetPosEx [WINMM.@]
205 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
207 TRACE("(%d, %p);\n", wID, lpInfo);
209 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
210 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
212 lpInfo->dwXpos = 0;
213 lpInfo->dwYpos = 0;
214 lpInfo->dwZpos = 0;
215 lpInfo->dwRpos = 0;
216 lpInfo->dwUpos = 0;
217 lpInfo->dwVpos = 0;
218 lpInfo->dwButtons = 0;
219 lpInfo->dwButtonNumber = 0;
220 lpInfo->dwPOV = 0;
221 lpInfo->dwReserved1 = 0;
222 lpInfo->dwReserved2 = 0;
224 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
227 /**************************************************************************
228 * joyGetPos [WINMM.@]
230 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
232 TRACE("(%d, %p);\n", wID, lpInfo);
234 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
235 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
237 lpInfo->wXpos = 0;
238 lpInfo->wYpos = 0;
239 lpInfo->wZpos = 0;
240 lpInfo->wButtons = 0;
242 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
245 /**************************************************************************
246 * joyGetThreshold [WINMM.@]
248 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
250 TRACE("(%04X, %p);\n", wID, lpThreshold);
252 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
254 *lpThreshold = JOY_Sticks[wID].threshold;
255 return JOYERR_NOERROR;
258 /**************************************************************************
259 * joyReleaseCapture [WINMM.@]
261 MMRESULT WINAPI joyReleaseCapture(UINT wID)
263 TRACE("(%04X);\n", wID);
265 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
266 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
267 if (!JOY_Sticks[wID].hCapture) return JOYERR_NOCANDO;
269 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
270 JOY_Sticks[wID].hCapture = 0;
271 JOY_Sticks[wID].wTimer = 0;
273 return JOYERR_NOERROR;
276 /**************************************************************************
277 * joySetCapture [WINMM.@]
279 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
281 TRACE("(%p, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
283 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
284 if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
285 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
287 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
288 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
290 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
291 return JOYERR_UNPLUGGED;
293 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
294 return JOYERR_NOCANDO;
296 JOY_Sticks[wID].hCapture = hWnd;
297 JOY_Sticks[wID].bChanged = bChanged;
299 return JOYERR_NOERROR;
302 /**************************************************************************
303 * joySetThreshold [WINMM.@]
305 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
307 TRACE("(%04X, %d);\n", wID, wThreshold);
309 if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
311 JOY_Sticks[wID].threshold = wThreshold;
313 return JOYERR_NOERROR;