4 * Copyright 1997 Andreas Mohr
6 * nearly all joystick functions can be regarded as obsolete,
7 * as Linux (2.1.x) now supports extended joysticks
8 * with a completely new joystick driver interface
9 * new driver's docu says:
10 * "For backward compatibility the old interface is still included,
11 * but will be dropped in the future."
12 * Thus we should implement the new interface and at most keep the old
13 * routines for backward compatibility.
20 #include <sys/ioctl.h>
21 #ifdef HAVE_SYS_ERRNO_H
22 #include <sys/errno.h>
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(mmsys)
31 #define MAXJOYDRIVERS 4
33 static int count_use[MAXJOYDRIVERS] = {0, 0, 0, 0};
35 static int joy_nr_open = 0;
36 static BOOL16 joyCaptured = FALSE;
37 static HWND16 CaptureWnd[MAXJOYDRIVERS] = {0, 0};
38 static int joy_dev[MAXJOYDRIVERS] = {-1, -1,-1,-1};
39 static JOYINFO16 joyCapData[MAXJOYDRIVERS];
40 static unsigned int joy_threshold[MAXJOYDRIVERS] = {0, 0, 0, 0};
50 /**************************************************************************
51 * joyOpenDriver [internal]
53 BOOL16 joyOpenDriver(WORD wID)
55 char dev_name[] = "/dev/js%d";
58 if (wID>3) return FALSE;
59 if (joy_dev[wID] >= 0) return TRUE;
60 sprintf(buf,dev_name,wID);
61 if ((joy_dev[wID] = open(buf, O_RDONLY)) >= 0) {
68 /**************************************************************************
69 * joyCloseDriver [internal]
71 void joyCloseDriver(WORD wID)
73 if (joy_dev[wID] >= 0) {
80 /**************************************************************************
81 * joySendMessages [internal]
83 void joySendMessages(void)
90 for (joy=0; joy < MAXJOYDRIVERS; joy++)
91 if (joy_dev[joy] >= 0) {
92 if (count_use[joy] > 250) {
100 if (joyCaptured == FALSE) return;
104 for (joy=0; joy < MAXJOYDRIVERS; joy++) {
105 if (joyOpenDriver(joy) == FALSE) continue;
106 dev_stat = read(joy_dev[joy], &js, sizeof(js));
107 if (dev_stat == sizeof(js)) {
110 if ((joyCapData[joy].wXpos != js.x) || (joyCapData[joy].wYpos != js.y)) {
111 SendMessageA(CaptureWnd[joy], MM_JOY1MOVE + joy, js.buttons, MAKELONG(js.x, js.y));
112 joyCapData[joy].wXpos = js.x;
113 joyCapData[joy].wYpos = js.y;
115 if (joyCapData[joy].wButtons != js.buttons) {
116 unsigned int ButtonChanged = (WORD)(joyCapData[joy].wButtons ^ js.buttons)<<8;
117 if (joyCapData[joy].wButtons < js.buttons)
118 SendMessageA(CaptureWnd[joy], MM_JOY1BUTTONDOWN + joy, ButtonChanged, MAKELONG(js.x, js.y));
120 if (joyCapData[joy].wButtons > js.buttons)
121 SendMessageA(CaptureWnd[joy], MM_JOY1BUTTONUP
122 + joy, ButtonChanged, MAKELONG(js.x, js.y));
123 joyCapData[joy].wButtons = js.buttons;
130 /**************************************************************************
131 * JoyGetNumDevs [MMSYSTEM.101]
133 UINT WINAPI joyGetNumDevs(void)
135 return joyGetNumDevs16();
138 /**************************************************************************
139 * JoyGetNumDevs [MMSYSTEM.101]
141 UINT16 WINAPI joyGetNumDevs16(void)
146 for (joy=0; joy<MAXJOYDRIVERS; joy++)
147 if (joyOpenDriver(joy) == TRUE) {
151 TRACE("returning %d\n", joy_cnt);
152 if (!joy_cnt) ERR("No joystick found - "
153 "perhaps get joystick-0.8.0.tar.gz and load"
154 "it as module or use Linux >= 2.1.45 to be "
155 "able to use joysticks.\n");
159 /**************************************************************************
160 * JoyGetDevCaps [WINMM.27]
162 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps,UINT wSize)
165 MMRESULT16 ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
167 lpCaps->wMid = jc16.wMid;
168 lpCaps->wPid = jc16.wPid;
169 strcpy(lpCaps->szPname,jc16.szPname);
170 lpCaps->wXmin = jc16.wXmin;
171 lpCaps->wXmax = jc16.wXmax;
172 lpCaps->wYmin = jc16.wYmin;
173 lpCaps->wYmax = jc16.wYmax;
174 lpCaps->wZmin = jc16.wZmin;
175 lpCaps->wZmax = jc16.wZmax;
176 lpCaps->wNumButtons = jc16.wNumButtons;
177 lpCaps->wPeriodMin = jc16.wPeriodMin;
178 lpCaps->wPeriodMax = jc16.wPeriodMax;
180 lpCaps->wRmin = jc16.wRmin;
181 lpCaps->wRmax = jc16.wRmax;
182 lpCaps->wUmin = jc16.wUmin;
183 lpCaps->wUmax = jc16.wUmax;
184 lpCaps->wVmin = jc16.wVmin;
185 lpCaps->wVmax = jc16.wVmax;
186 lpCaps->wCaps = jc16.wCaps;
187 lpCaps->wMaxAxes = jc16.wMaxAxes;
188 lpCaps->wNumAxes = jc16.wNumAxes;
189 lpCaps->wMaxButtons = jc16.wMaxButtons;
190 strcpy(lpCaps->szRegKey,jc16.szRegKey);
191 strcpy(lpCaps->szOEMVxD,jc16.szOEMVxD);
195 /**************************************************************************
196 * JoyGetDevCaps [WINMM.28]
198 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps,UINT wSize)
201 MMRESULT16 ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
203 lpCaps->wMid = jc16.wMid;
204 lpCaps->wPid = jc16.wPid;
205 lstrcpyAtoW(lpCaps->szPname,jc16.szPname);
206 lpCaps->wXmin = jc16.wXmin;
207 lpCaps->wXmax = jc16.wXmax;
208 lpCaps->wYmin = jc16.wYmin;
209 lpCaps->wYmax = jc16.wYmax;
210 lpCaps->wZmin = jc16.wZmin;
211 lpCaps->wZmax = jc16.wZmax;
212 lpCaps->wNumButtons = jc16.wNumButtons;
213 lpCaps->wPeriodMin = jc16.wPeriodMin;
214 lpCaps->wPeriodMax = jc16.wPeriodMax;
216 lpCaps->wRmin = jc16.wRmin;
217 lpCaps->wRmax = jc16.wRmax;
218 lpCaps->wUmin = jc16.wUmin;
219 lpCaps->wUmax = jc16.wUmax;
220 lpCaps->wVmin = jc16.wVmin;
221 lpCaps->wVmax = jc16.wVmax;
222 lpCaps->wCaps = jc16.wCaps;
223 lpCaps->wMaxAxes = jc16.wMaxAxes;
224 lpCaps->wNumAxes = jc16.wNumAxes;
225 lpCaps->wMaxButtons = jc16.wMaxButtons;
226 lstrcpyAtoW(lpCaps->szRegKey,jc16.szRegKey);
227 lstrcpyAtoW(lpCaps->szOEMVxD,jc16.szOEMVxD);
230 /**************************************************************************
231 * JoyGetDevCaps [MMSYSTEM.102]
233 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
235 TRACE("(%04X, %p, %d);\n",
237 if (joyOpenDriver(wID) == TRUE) {
238 lpCaps->wMid = MM_MICROSOFT;
239 lpCaps->wPid = MM_PC_JOYSTICK;
240 strcpy(lpCaps->szPname, "WineJoy"); /* joystick product name */
241 lpCaps->wXmin = 0; /* FIXME */
242 lpCaps->wXmax = 0xffff;
244 lpCaps->wYmax = 0xffff;
246 lpCaps->wZmax = 0xffff;
247 lpCaps->wNumButtons = 2;
248 lpCaps->wPeriodMin = 0;
249 lpCaps->wPeriodMax = 50; /* FIXME end */
250 if (wSize == sizeof(JOYCAPS16)) {
251 /* complete 95 structure */
253 lpCaps->wRmax = 0xffff;
255 lpCaps->wUmax = 0xffff;
257 lpCaps->wVmax = 0xffff;
259 lpCaps->wMaxAxes = 6;
260 lpCaps->wNumAxes = 2;
261 lpCaps->wMaxButtons = 3;
262 strcpy(lpCaps->szRegKey,"");
263 strcpy(lpCaps->szOEMVxD,"");
266 return JOYERR_NOERROR;
269 return MMSYSERR_NODRIVER;
272 /**************************************************************************
273 * JoyGetPosEx [WINMM.31]
275 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
277 /* FIXME: implement it */
278 return MMSYSERR_NODRIVER;
281 /**************************************************************************
282 * JoyGetPosEx [WINMM.31]
284 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
286 return joyGetPosEx(wID, lpInfo);
289 /**************************************************************************
290 * JoyGetPos [WINMM.30]
292 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
295 MMRESULT16 ret = joyGetPos16(wID,&ji);
297 lpInfo->wXpos = ji.wXpos;
298 lpInfo->wYpos = ji.wYpos;
299 lpInfo->wZpos = ji.wZpos;
300 lpInfo->wButtons = ji.wButtons;
304 /**************************************************************************
305 * JoyGetPos [MMSYSTEM.103]
307 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
311 TRACE("(%04X, %p)\n", wID, lpInfo);
312 if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
313 dev_stat = read(joy_dev[wID], &js, sizeof(js));
314 if (dev_stat != sizeof(js)) {
316 return JOYERR_UNPLUGGED; /* FIXME: perhaps wrong, but what should I return else ? */
321 lpInfo->wXpos = js.x; /* FIXME: perhaps multiply it somehow ? */
322 lpInfo->wYpos = js.y;
323 lpInfo->wZpos = 0; /* FIXME: Don't know what to do with this value as joystick driver doesn't provide a Z value */
324 lpInfo->wButtons = js.buttons;
325 TRACE("x: %d, y: %d, buttons: %d\n", js.x, js.y, js.buttons);
326 return JOYERR_NOERROR;
329 /**************************************************************************
330 * JoyGetThreshold [WINMM.32]
332 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
335 MMRESULT16 ret = joyGetThreshold16(wID,&thresh);
337 *lpThreshold = thresh;
341 /**************************************************************************
342 * JoyGetThreshold [MMSYSTEM.104]
344 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
346 TRACE("(%04X, %p);\n", wID, lpThreshold);
347 if (wID >= MAXJOYDRIVERS) return JOYERR_PARMS;
348 *lpThreshold = joy_threshold[wID];
349 return JOYERR_NOERROR;
352 /**************************************************************************
353 * JoyReleaseCapture [WINMM.33]
355 MMRESULT WINAPI joyReleaseCapture(UINT wID)
357 return joyReleaseCapture16(wID);
360 /**************************************************************************
361 * JoyReleaseCapture [MMSYSTEM.105]
363 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
365 TRACE("(%04X);\n", wID);
370 return JOYERR_NOERROR;
373 /**************************************************************************
374 * JoySetCapture [MMSYSTEM.106]
376 MMRESULT WINAPI joySetCapture(HWND hWnd,UINT wID,UINT wPeriod,BOOL bChanged)
378 return joySetCapture16(hWnd,wID,wPeriod,bChanged);
381 /**************************************************************************
382 * JoySetCapture [MMSYSTEM.106]
384 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd,UINT16 wID,UINT16 wPeriod,BOOL16 bChanged)
387 TRACE("(%04X, %04X, %d, %d);\n",
388 hWnd, wID, wPeriod, bChanged);
390 if (!CaptureWnd[wID]) {
391 if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
393 CaptureWnd[wID] = hWnd;
394 return JOYERR_NOERROR;
397 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
400 /**************************************************************************
401 * JoySetThreshold [WINMM.35]
403 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
405 return joySetThreshold16(wID,wThreshold);
407 /**************************************************************************
408 * JoySetThreshold [MMSYSTEM.107]
410 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
412 TRACE("(%04X, %d);\n", wID, wThreshold);
414 if (wID > 3) return JOYERR_PARMS;
415 joy_threshold[wID] = wThreshold;
416 return JOYERR_NOERROR;
419 /**************************************************************************
420 * JoySetCalibration [MMSYSTEM.109]
422 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
424 FIXME("(%04X): stub.\n", wID);
425 return JOYERR_NOCANDO;