d3dx9: Use parameter data for the first constant only in d3dx_set_shader_const_state().
[wine.git] / dlls / winmm / joystick.c
blob92a7ce95d894c79f6805e0eee46ebedbf8e3fd06
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 + 30)
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 static BOOL winejoystick_missing = FALSE;
73 if (dwJoyID >= MAXJOYSTICK || winejoystick_missing)
74 return FALSE;
75 if (JOY_Sticks[dwJoyID].hDriver)
76 return TRUE;
78 JOY_Sticks[dwJoyID].hDriver = OpenDriverA("winejoystick.drv", 0, dwJoyID);
80 if (!JOY_Sticks[dwJoyID].hDriver)
82 WARN("OpenDriverA(\"winejoystick.drv\") failed\n");
84 /* The default driver is missing, don't attempt to load it again */
85 winejoystick_missing = TRUE;
88 return (JOY_Sticks[dwJoyID].hDriver != 0);
91 /**************************************************************************
92 * JOY_Timer [internal]
94 static void CALLBACK JOY_Timer(HWND hWnd, UINT wMsg, UINT_PTR wTimer, DWORD dwTime)
96 int i;
97 WINE_JOYSTICK* joy;
98 MMRESULT res;
99 JOYINFO ji;
100 LONG pos;
101 unsigned buttonChange;
103 for (i = 0; i < MAXJOYSTICK; i++) {
104 joy = &JOY_Sticks[i];
106 if (joy->hCapture != hWnd) continue;
108 res = joyGetPos(i, &ji);
109 if (res != JOYERR_NOERROR) {
110 WARN("joyGetPos failed: %08x\n", res);
111 continue;
114 pos = MAKELONG(ji.wXpos, ji.wYpos);
116 if (!joy->bChanged ||
117 abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
118 abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
119 SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
120 joy->ji.wXpos = ji.wXpos;
121 joy->ji.wYpos = ji.wYpos;
123 if (!joy->bChanged ||
124 abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
125 SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
126 joy->ji.wZpos = ji.wZpos;
128 if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
129 if (ji.wButtons & buttonChange)
130 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
131 (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
132 if (joy->ji.wButtons & buttonChange)
133 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
134 (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
135 joy->ji.wButtons = ji.wButtons;
140 /**************************************************************************
141 * joyConfigChanged [WINMM.@]
143 MMRESULT WINAPI joyConfigChanged(DWORD flags)
145 FIXME("(%x) - stub\n", flags);
147 if (flags)
148 return JOYERR_PARMS;
150 return JOYERR_NOERROR;
153 /**************************************************************************
154 * joyGetNumDevs [WINMM.@]
156 UINT WINAPI DECLSPEC_HOTPATCH joyGetNumDevs(void)
158 UINT ret = 0;
159 int i;
161 for (i = 0; i < MAXJOYSTICK; i++) {
162 if (JOY_LoadDriver(i)) {
163 ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0, 0);
166 return ret;
169 /**************************************************************************
170 * joyGetDevCapsW [WINMM.@]
172 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetDevCapsW(UINT_PTR wID, LPJOYCAPSW lpCaps, UINT wSize)
174 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
175 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
177 lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
178 lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
180 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (LPARAM)lpCaps, wSize);
183 /**************************************************************************
184 * joyGetDevCapsA [WINMM.@]
186 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetDevCapsA(UINT_PTR wID, LPJOYCAPSA lpCaps, UINT wSize)
188 JOYCAPSW jcw;
189 MMRESULT ret;
191 if (lpCaps == NULL) return MMSYSERR_INVALPARAM;
193 ret = joyGetDevCapsW(wID, &jcw, sizeof(jcw));
195 if (ret == JOYERR_NOERROR)
197 lpCaps->wMid = jcw.wMid;
198 lpCaps->wPid = jcw.wPid;
199 WideCharToMultiByte( CP_ACP, 0, jcw.szPname, -1, lpCaps->szPname,
200 sizeof(lpCaps->szPname), NULL, NULL );
201 lpCaps->wXmin = jcw.wXmin;
202 lpCaps->wXmax = jcw.wXmax;
203 lpCaps->wYmin = jcw.wYmin;
204 lpCaps->wYmax = jcw.wYmax;
205 lpCaps->wZmin = jcw.wZmin;
206 lpCaps->wZmax = jcw.wZmax;
207 lpCaps->wNumButtons = jcw.wNumButtons;
208 lpCaps->wPeriodMin = jcw.wPeriodMin;
209 lpCaps->wPeriodMax = jcw.wPeriodMax;
211 if (wSize >= sizeof(JOYCAPSA)) { /* Win95 extensions ? */
212 lpCaps->wRmin = jcw.wRmin;
213 lpCaps->wRmax = jcw.wRmax;
214 lpCaps->wUmin = jcw.wUmin;
215 lpCaps->wUmax = jcw.wUmax;
216 lpCaps->wVmin = jcw.wVmin;
217 lpCaps->wVmax = jcw.wVmax;
218 lpCaps->wCaps = jcw.wCaps;
219 lpCaps->wMaxAxes = jcw.wMaxAxes;
220 lpCaps->wNumAxes = jcw.wNumAxes;
221 lpCaps->wMaxButtons = jcw.wMaxButtons;
222 WideCharToMultiByte( CP_ACP, 0, jcw.szRegKey, -1, lpCaps->szRegKey,
223 sizeof(lpCaps->szRegKey), NULL, NULL );
224 WideCharToMultiByte( CP_ACP, 0, jcw.szOEMVxD, -1, lpCaps->szOEMVxD,
225 sizeof(lpCaps->szOEMVxD), NULL, NULL );
229 return ret;
232 /**************************************************************************
233 * joyGetPosEx [WINMM.@]
235 MMRESULT WINAPI DECLSPEC_HOTPATCH joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
237 TRACE("(%d, %p);\n", wID, lpInfo);
239 if (!lpInfo) return MMSYSERR_INVALPARAM;
240 if (wID >= MAXJOYSTICK || lpInfo->dwSize < sizeof(JOYINFOEX)) return JOYERR_PARMS;
241 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
243 lpInfo->dwXpos = 0;
244 lpInfo->dwYpos = 0;
245 lpInfo->dwZpos = 0;
246 lpInfo->dwRpos = 0;
247 lpInfo->dwUpos = 0;
248 lpInfo->dwVpos = 0;
249 lpInfo->dwButtons = 0;
250 lpInfo->dwButtonNumber = 0;
251 lpInfo->dwPOV = 0;
252 lpInfo->dwReserved1 = 0;
253 lpInfo->dwReserved2 = 0;
255 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (LPARAM)lpInfo, 0);
258 /**************************************************************************
259 * joyGetPos [WINMM.@]
261 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
263 TRACE("(%d, %p);\n", wID, lpInfo);
265 if (!lpInfo) return MMSYSERR_INVALPARAM;
266 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
267 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
269 lpInfo->wXpos = 0;
270 lpInfo->wYpos = 0;
271 lpInfo->wZpos = 0;
272 lpInfo->wButtons = 0;
274 return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (LPARAM)lpInfo, 0);
277 /**************************************************************************
278 * joyGetThreshold [WINMM.@]
280 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
282 TRACE("(%04X, %p);\n", wID, lpThreshold);
284 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
286 *lpThreshold = JOY_Sticks[wID].threshold;
287 return JOYERR_NOERROR;
290 /**************************************************************************
291 * joyReleaseCapture [WINMM.@]
293 MMRESULT WINAPI joyReleaseCapture(UINT wID)
295 TRACE("(%04X);\n", wID);
297 if (wID >= MAXJOYSTICK) return JOYERR_PARMS;
298 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
299 if (JOY_Sticks[wID].hCapture)
301 KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
302 JOY_Sticks[wID].hCapture = 0;
303 JOY_Sticks[wID].wTimer = 0;
305 else
306 TRACE("Joystick is not captured, ignoring request.\n");
308 return JOYERR_NOERROR;
311 /**************************************************************************
312 * joySetCapture [WINMM.@]
314 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
316 TRACE("(%p, %04X, %d, %d);\n", hWnd, wID, wPeriod, bChanged);
318 if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
319 if (wPeriod<JOY_PERIOD_MIN) wPeriod = JOY_PERIOD_MIN;
320 else if(wPeriod>JOY_PERIOD_MAX) wPeriod = JOY_PERIOD_MAX;
321 if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
323 if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
324 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
326 if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
327 return JOYERR_UNPLUGGED;
329 if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
330 return JOYERR_NOCANDO;
332 JOY_Sticks[wID].hCapture = hWnd;
333 JOY_Sticks[wID].bChanged = bChanged;
335 return JOYERR_NOERROR;
338 /**************************************************************************
339 * joySetThreshold [WINMM.@]
341 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
343 TRACE("(%04X, %d);\n", wID, wThreshold);
345 if (wID >= MAXJOYSTICK || wThreshold > 65535) return MMSYSERR_INVALPARAM;
347 JOY_Sticks[wID].threshold = wThreshold;
349 return JOYERR_NOERROR;