joystick.c 9.46 KB
Newer Older
1
/* -*- tab-width: 8; c-basic-offset: 4 -*- */
Alexandre Julliard's avatar
Alexandre Julliard committed
2
/*
Alexandre Julliard's avatar
Alexandre Julliard committed
3
 * joystick functions
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 * Copyright 1997 Andreas Mohr
6 7
 *	     2000 Wolfgang Schwotzer
 *                Eric Pouech
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
22 23
 */

24 25
#include "config.h"

26 27 28
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
29
#include <stdarg.h>
30
#include <stdio.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
31 32 33
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
34
#ifdef HAVE_SYS_IOCTL_H
Alexandre Julliard's avatar
Alexandre Julliard committed
35
#include <sys/ioctl.h>
36
#endif
37

38
#include "windef.h"
39
#include "winbase.h"
40
#include "mmsystem.h"
41
#include "wingdi.h"
42
#include "winuser.h"
43
#include "winnls.h"
44

45
#include "mmddk.h"
46

47
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
48

49
WINE_DEFAULT_DEBUG_CHANNEL(winmm);
50

51
#define MAXJOYSTICK (JOYSTICKID2 + 30)
52 53
#define JOY_PERIOD_MIN	(10)	/* min Capture time period */
#define JOY_PERIOD_MAX	(1000)	/* max Capture time period */
Alexandre Julliard's avatar
Alexandre Julliard committed
54

55 56 57 58 59 60
typedef struct tagWINE_JOYSTICK {
    JOYINFO	ji;
    HWND	hCapture;
    UINT	wTimer;
    DWORD	threshold;
    BOOL	bChanged;
61
    HDRVR	hDriver;
62
} WINE_JOYSTICK;
Alexandre Julliard's avatar
Alexandre Julliard committed
63

64
static	WINE_JOYSTICK	JOY_Sticks[MAXJOYSTICK];
Alexandre Julliard's avatar
Alexandre Julliard committed
65 66

/**************************************************************************
67
 * 				JOY_LoadDriver		[internal]
Alexandre Julliard's avatar
Alexandre Julliard committed
68
 */
69
static	BOOL JOY_LoadDriver(DWORD dwJoyID)
Alexandre Julliard's avatar
Alexandre Julliard committed
70
{
71 72 73
    if (dwJoyID >= MAXJOYSTICK)
	return FALSE;
    if (JOY_Sticks[dwJoyID].hDriver)
74
	return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
75

76
    JOY_Sticks[dwJoyID].hDriver = OpenDriverA("winejoystick.drv", 0, dwJoyID);
77
    return (JOY_Sticks[dwJoyID].hDriver != 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
78 79
}

80
/**************************************************************************
81
 * 				JOY_Timer		[internal]
82
 */
83
static	void	CALLBACK	JOY_Timer(HWND hWnd, UINT wMsg, UINT_PTR wTimer, DWORD dwTime)
84
{
85 86 87 88 89
    int			i;
    WINE_JOYSTICK*	joy;
    JOYINFO		ji;
    LONG		pos;
    unsigned 		buttonChange;
90

91 92
    for (i = 0; i < MAXJOYSTICK; i++) {
	joy = &JOY_Sticks[i];
93 94 95

	if (joy->hCapture != hWnd) continue;

96 97
	joyGetPos(i, &ji);
	pos = MAKELONG(ji.wXpos, ji.wYpos);
98

99
	if (!joy->bChanged ||
100
	    abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
101 102 103 104
	    abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
	    SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
	    joy->ji.wXpos = ji.wXpos;
	    joy->ji.wYpos = ji.wYpos;
105
	}
106 107 108 109
	if (!joy->bChanged ||
	    abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
	    SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
	    joy->ji.wZpos = ji.wZpos;
110
	}
111 112
	if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
	    if (ji.wButtons & buttonChange)
113
		SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
114 115
			     (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
	    if (joy->ji.wButtons & buttonChange)
116
		SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
117 118 119 120
			     (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
	    joy->ji.wButtons = ji.wButtons;
	}
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
121 122
}

123 124 125 126 127 128
/**************************************************************************
 *                              joyConfigChanged        [WINMM.@]
 */
MMRESULT WINAPI joyConfigChanged(DWORD flags)
{
    FIXME("(%x) - stub\n", flags);
129 130 131 132

    if (flags)
	return JOYERR_PARMS;

133 134 135
    return JOYERR_NOERROR;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
136
/**************************************************************************
137
 * 				joyGetNumDevs		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
138
 */
139
UINT WINAPI joyGetNumDevs(void)
Alexandre Julliard's avatar
Alexandre Julliard committed
140
{
141 142
    UINT	ret = 0;
    int		i;
143

144 145
    for (i = 0; i < MAXJOYSTICK; i++) {
	if (JOY_LoadDriver(i)) {
146
            ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0, 0);
147 148 149
	}
    }
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
150 151 152
}

/**************************************************************************
153
 * 				joyGetDevCapsW		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
154
 */
155
MMRESULT WINAPI joyGetDevCapsW(UINT_PTR wID, LPJOYCAPSW lpCaps, UINT wSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
156
{
157
    if (wID >= MAXJOYSTICK)	return JOYERR_PARMS;
158
    if (!JOY_LoadDriver(wID))	return MMSYSERR_NODRIVER;
159 160 161 162

    lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
    lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */

163
    return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (LPARAM)lpCaps, wSize);
164
}
Alexandre Julliard's avatar
Alexandre Julliard committed
165 166

/**************************************************************************
167
 * 				joyGetDevCapsA		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
168
 */
169
MMRESULT WINAPI joyGetDevCapsA(UINT_PTR wID, LPJOYCAPSA lpCaps, UINT wSize)
Alexandre Julliard's avatar
Alexandre Julliard committed
170
{
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    JOYCAPSW	jcw;
    MMRESULT	ret;

    if (lpCaps == NULL) return MMSYSERR_INVALPARAM;

    ret = joyGetDevCapsW(wID, &jcw, sizeof(jcw));

    if (ret == JOYERR_NOERROR)
    {
        lpCaps->wMid = jcw.wMid;
        lpCaps->wPid = jcw.wPid;
        WideCharToMultiByte( CP_ACP, 0, jcw.szPname, -1, lpCaps->szPname,
                             sizeof(lpCaps->szPname), NULL, NULL );
        lpCaps->wXmin = jcw.wXmin;
        lpCaps->wXmax = jcw.wXmax;
        lpCaps->wYmin = jcw.wYmin;
        lpCaps->wYmax = jcw.wYmax;
        lpCaps->wZmin = jcw.wZmin;
        lpCaps->wZmax = jcw.wZmax;
        lpCaps->wNumButtons = jcw.wNumButtons;
        lpCaps->wPeriodMin = jcw.wPeriodMin;
        lpCaps->wPeriodMax = jcw.wPeriodMax;

        if (wSize >= sizeof(JOYCAPSA)) { /* Win95 extensions ? */
            lpCaps->wRmin = jcw.wRmin;
            lpCaps->wRmax = jcw.wRmax;
            lpCaps->wUmin = jcw.wUmin;
            lpCaps->wUmax = jcw.wUmax;
            lpCaps->wVmin = jcw.wVmin;
            lpCaps->wVmax = jcw.wVmax;
            lpCaps->wCaps = jcw.wCaps;
            lpCaps->wMaxAxes = jcw.wMaxAxes;
            lpCaps->wNumAxes = jcw.wNumAxes;
            lpCaps->wMaxButtons = jcw.wMaxButtons;
            WideCharToMultiByte( CP_ACP, 0, jcw.szRegKey, -1, lpCaps->szRegKey,
                                 sizeof(lpCaps->szRegKey), NULL, NULL );
            WideCharToMultiByte( CP_ACP, 0, jcw.szOEMVxD, -1, lpCaps->szOEMVxD,
                                 sizeof(lpCaps->szOEMVxD), NULL, NULL );
        }
210
    }
211

212
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
213
}
214

Alexandre Julliard's avatar
Alexandre Julliard committed
215
/**************************************************************************
216
 *                              joyGetPosEx             [WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
217
 */
218
MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
Alexandre Julliard's avatar
Alexandre Julliard committed
219
{
220
    TRACE("(%d, %p);\n", wID, lpInfo);
221

222
    if (wID >= MAXJOYSTICK)	return JOYERR_PARMS;
223
    if (!JOY_LoadDriver(wID))	return MMSYSERR_NODRIVER;
224

225 226 227 228 229 230 231 232 233 234 235 236
    lpInfo->dwXpos = 0;
    lpInfo->dwYpos = 0;
    lpInfo->dwZpos = 0;
    lpInfo->dwRpos = 0;
    lpInfo->dwUpos = 0;
    lpInfo->dwVpos = 0;
    lpInfo->dwButtons = 0;
    lpInfo->dwButtonNumber = 0;
    lpInfo->dwPOV = 0;
    lpInfo->dwReserved1 = 0;
    lpInfo->dwReserved2 = 0;

237
    return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (LPARAM)lpInfo, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
238 239
}

Alexandre Julliard's avatar
Alexandre Julliard committed
240
/**************************************************************************
241
 * 				joyGetPos	       	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
242
 */
243
MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
Alexandre Julliard's avatar
Alexandre Julliard committed
244
{
245
    TRACE("(%d, %p);\n", wID, lpInfo);
246

247
    if (wID >= MAXJOYSTICK)	return JOYERR_PARMS;
248
    if (!JOY_LoadDriver(wID))	return MMSYSERR_NODRIVER;
249

250 251 252 253 254
    lpInfo->wXpos = 0;
    lpInfo->wYpos = 0;
    lpInfo->wZpos = 0;
    lpInfo->wButtons = 0;

255
    return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (LPARAM)lpInfo, 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
256 257 258
}

/**************************************************************************
259
 * 				joyGetThreshold		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
260
 */
261
MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
Alexandre Julliard's avatar
Alexandre Julliard committed
262
{
263
    TRACE("(%04X, %p);\n", wID, lpThreshold);
264

265
    if (wID >= MAXJOYSTICK)	return JOYERR_PARMS;
266

267 268
    *lpThreshold = JOY_Sticks[wID].threshold;
    return JOYERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
269 270 271
}

/**************************************************************************
272
 * 				joyReleaseCapture	[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
273
 */
274
MMRESULT WINAPI joyReleaseCapture(UINT wID)
Alexandre Julliard's avatar
Alexandre Julliard committed
275
{
276 277 278
    TRACE("(%04X);\n", wID);

    if (wID >= MAXJOYSTICK)		return JOYERR_PARMS;
279
    if (!JOY_LoadDriver(wID))		return MMSYSERR_NODRIVER;
280 281 282 283 284 285 286
    if (!JOY_Sticks[wID].hCapture)	return JOYERR_NOCANDO;

    KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
    JOY_Sticks[wID].hCapture = 0;
    JOY_Sticks[wID].wTimer = 0;

    return JOYERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
287 288
}

Alexandre Julliard's avatar
Alexandre Julliard committed
289
/**************************************************************************
290
 * 				joySetCapture		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
291
 */
292
MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
Alexandre Julliard's avatar
Alexandre Julliard committed
293
{
294
    TRACE("(%p, %04X, %d, %d);\n",  hWnd, wID, wPeriod, bChanged);
295 296 297

    if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
    if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
298
    if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
299 300 301 302 303 304 305 306 307 308 309 310

    if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
	return JOYERR_NOCANDO; /* FIXME: what should be returned ? */

    if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
	return JOYERR_UNPLUGGED;

    if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
	return JOYERR_NOCANDO;

    JOY_Sticks[wID].hCapture = hWnd;
    JOY_Sticks[wID].bChanged = bChanged;
311

312
    return JOYERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
313 314 315
}

/**************************************************************************
316
 * 				joySetThreshold		[WINMM.@]
Alexandre Julliard's avatar
Alexandre Julliard committed
317
 */
318
MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
Alexandre Julliard's avatar
Alexandre Julliard committed
319
{
320
    TRACE("(%04X, %d);\n", wID, wThreshold);
321

322 323 324 325 326
    if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;

    JOY_Sticks[wID].threshold = wThreshold;

    return JOYERR_NOERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
327
}