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.
22 #include <sys/ioctl.h>
23 #ifdef HAVE_SYS_ERRNO_H
24 #include <sys/errno.h>
29 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(mmsys)
33 #define MAXJOYDRIVERS 4
35 static int count_use[MAXJOYDRIVERS] = {0, 0, 0, 0};
37 static int joy_nr_open = 0;
38 static BOOL16 joyCaptured = FALSE;
39 static HWND16 CaptureWnd[MAXJOYDRIVERS] = {0, 0};
40 static int joy_dev[MAXJOYDRIVERS] = {-1, -1,-1,-1};
41 static JOYINFO16 joyCapData[MAXJOYDRIVERS];
42 static unsigned int joy_threshold[MAXJOYDRIVERS] = {0, 0, 0, 0};
52 /**************************************************************************
53 * joyOpenDriver [internal]
55 BOOL16 joyOpenDriver(WORD wID)
57 char dev_name[] = "/dev/js%d";
60 if (wID>3) return FALSE;
61 if (joy_dev[wID] >= 0) return TRUE;
62 sprintf(buf,dev_name,wID);
63 if ((joy_dev[wID] = open(buf, O_RDONLY)) >= 0) {
70 /**************************************************************************
71 * joyCloseDriver [internal]
73 void joyCloseDriver(WORD wID)
75 if (joy_dev[wID] >= 0) {
82 /**************************************************************************
83 * joySendMessages [internal]
85 void joySendMessages(void)
92 for (joy=0; joy < MAXJOYDRIVERS; joy++)
93 if (joy_dev[joy] >= 0) {
94 if (count_use[joy] > 250) {
102 if (joyCaptured == FALSE) return;
106 for (joy=0; joy < MAXJOYDRIVERS; joy++) {
107 if (joyOpenDriver(joy) == FALSE) continue;
108 dev_stat = read(joy_dev[joy], &js, sizeof(js));
109 if (dev_stat == sizeof(js)) {
112 if ((joyCapData[joy].wXpos != js.x) || (joyCapData[joy].wYpos != js.y)) {
113 SendMessageA(CaptureWnd[joy], MM_JOY1MOVE + joy, js.buttons, MAKELONG(js.x, js.y));
114 joyCapData[joy].wXpos = js.x;
115 joyCapData[joy].wYpos = js.y;
117 if (joyCapData[joy].wButtons != js.buttons) {
118 unsigned int ButtonChanged = (WORD)(joyCapData[joy].wButtons ^ js.buttons)<<8;
119 if (joyCapData[joy].wButtons < js.buttons)
120 SendMessageA(CaptureWnd[joy], MM_JOY1BUTTONDOWN + joy, ButtonChanged, MAKELONG(js.x, js.y));
122 if (joyCapData[joy].wButtons > js.buttons)
123 SendMessageA(CaptureWnd[joy], MM_JOY1BUTTONUP
124 + joy, ButtonChanged, MAKELONG(js.x, js.y));
125 joyCapData[joy].wButtons = js.buttons;
132 /**************************************************************************
133 * JoyGetNumDevs [MMSYSTEM.101]
135 UINT WINAPI joyGetNumDevs(void)
137 return joyGetNumDevs16();
140 /**************************************************************************
141 * JoyGetNumDevs [MMSYSTEM.101]
143 UINT16 WINAPI joyGetNumDevs16(void)
148 for (joy=0; joy<MAXJOYDRIVERS; joy++)
149 if (joyOpenDriver(joy) == TRUE) {
153 TRACE("returning %d\n", joy_cnt);
154 if (!joy_cnt) ERR("No joystick found - "
155 "perhaps get joystick-0.8.0.tar.gz and load"
156 "it as module or use Linux >= 2.1.45 to be "
157 "able to use joysticks.\n");
161 /**************************************************************************
162 * JoyGetDevCaps [WINMM.27]
164 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps,UINT wSize)
167 MMRESULT16 ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
169 lpCaps->wMid = jc16.wMid;
170 lpCaps->wPid = jc16.wPid;
171 strcpy(lpCaps->szPname,jc16.szPname);
172 lpCaps->wXmin = jc16.wXmin;
173 lpCaps->wXmax = jc16.wXmax;
174 lpCaps->wYmin = jc16.wYmin;
175 lpCaps->wYmax = jc16.wYmax;
176 lpCaps->wZmin = jc16.wZmin;
177 lpCaps->wZmax = jc16.wZmax;
178 lpCaps->wNumButtons = jc16.wNumButtons;
179 lpCaps->wPeriodMin = jc16.wPeriodMin;
180 lpCaps->wPeriodMax = jc16.wPeriodMax;
182 lpCaps->wRmin = jc16.wRmin;
183 lpCaps->wRmax = jc16.wRmax;
184 lpCaps->wUmin = jc16.wUmin;
185 lpCaps->wUmax = jc16.wUmax;
186 lpCaps->wVmin = jc16.wVmin;
187 lpCaps->wVmax = jc16.wVmax;
188 lpCaps->wCaps = jc16.wCaps;
189 lpCaps->wMaxAxes = jc16.wMaxAxes;
190 lpCaps->wNumAxes = jc16.wNumAxes;
191 lpCaps->wMaxButtons = jc16.wMaxButtons;
192 strcpy(lpCaps->szRegKey,jc16.szRegKey);
193 strcpy(lpCaps->szOEMVxD,jc16.szOEMVxD);
197 /**************************************************************************
198 * JoyGetDevCaps [WINMM.28]
200 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps,UINT wSize)
203 MMRESULT16 ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
205 lpCaps->wMid = jc16.wMid;
206 lpCaps->wPid = jc16.wPid;
207 lstrcpyAtoW(lpCaps->szPname,jc16.szPname);
208 lpCaps->wXmin = jc16.wXmin;
209 lpCaps->wXmax = jc16.wXmax;
210 lpCaps->wYmin = jc16.wYmin;
211 lpCaps->wYmax = jc16.wYmax;
212 lpCaps->wZmin = jc16.wZmin;
213 lpCaps->wZmax = jc16.wZmax;
214 lpCaps->wNumButtons = jc16.wNumButtons;
215 lpCaps->wPeriodMin = jc16.wPeriodMin;
216 lpCaps->wPeriodMax = jc16.wPeriodMax;
218 lpCaps->wRmin = jc16.wRmin;
219 lpCaps->wRmax = jc16.wRmax;
220 lpCaps->wUmin = jc16.wUmin;
221 lpCaps->wUmax = jc16.wUmax;
222 lpCaps->wVmin = jc16.wVmin;
223 lpCaps->wVmax = jc16.wVmax;
224 lpCaps->wCaps = jc16.wCaps;
225 lpCaps->wMaxAxes = jc16.wMaxAxes;
226 lpCaps->wNumAxes = jc16.wNumAxes;
227 lpCaps->wMaxButtons = jc16.wMaxButtons;
228 lstrcpyAtoW(lpCaps->szRegKey,jc16.szRegKey);
229 lstrcpyAtoW(lpCaps->szOEMVxD,jc16.szOEMVxD);
232 /**************************************************************************
233 * JoyGetDevCaps [MMSYSTEM.102]
235 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
237 TRACE("(%04X, %p, %d);\n",
239 if (joyOpenDriver(wID) == TRUE) {
240 lpCaps->wMid = MM_MICROSOFT;
241 lpCaps->wPid = MM_PC_JOYSTICK;
242 strcpy(lpCaps->szPname, "WineJoy"); /* joystick product name */
243 lpCaps->wXmin = 0; /* FIXME */
244 lpCaps->wXmax = 0xffff;
246 lpCaps->wYmax = 0xffff;
248 lpCaps->wZmax = 0xffff;
249 lpCaps->wNumButtons = 2;
250 lpCaps->wPeriodMin = 0;
251 lpCaps->wPeriodMax = 50; /* FIXME end */
252 if (wSize == sizeof(JOYCAPS16)) {
253 /* complete 95 structure */
255 lpCaps->wRmax = 0xffff;
257 lpCaps->wUmax = 0xffff;
259 lpCaps->wVmax = 0xffff;
261 lpCaps->wMaxAxes = 6;
262 lpCaps->wNumAxes = 2;
263 lpCaps->wMaxButtons = 3;
264 strcpy(lpCaps->szRegKey,"");
265 strcpy(lpCaps->szOEMVxD,"");
268 return JOYERR_NOERROR;
271 return MMSYSERR_NODRIVER;
274 /**************************************************************************
275 * JoyGetPosEx [WINMM.31]
277 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
279 /* FIXME: implement it */
280 return MMSYSERR_NODRIVER;
283 /**************************************************************************
284 * JoyGetPosEx [WINMM.31]
286 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
288 return joyGetPosEx(wID, lpInfo);
291 /**************************************************************************
292 * JoyGetPos [WINMM.30]
294 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
297 MMRESULT16 ret = joyGetPos16(wID,&ji);
299 lpInfo->wXpos = ji.wXpos;
300 lpInfo->wYpos = ji.wYpos;
301 lpInfo->wZpos = ji.wZpos;
302 lpInfo->wButtons = ji.wButtons;
306 /**************************************************************************
307 * JoyGetPos [MMSYSTEM.103]
309 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
313 TRACE("(%04X, %p)\n", wID, lpInfo);
314 if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
315 dev_stat = read(joy_dev[wID], &js, sizeof(js));
316 if (dev_stat != sizeof(js)) {
318 return JOYERR_UNPLUGGED; /* FIXME: perhaps wrong, but what should I return else ? */
323 lpInfo->wXpos = js.x; /* FIXME: perhaps multiply it somehow ? */
324 lpInfo->wYpos = js.y;
325 lpInfo->wZpos = 0; /* FIXME: Don't know what to do with this value as joystick driver doesn't provide a Z value */
326 lpInfo->wButtons = js.buttons;
327 TRACE("x: %d, y: %d, buttons: %d\n", js.x, js.y, js.buttons);
328 return JOYERR_NOERROR;
331 /**************************************************************************
332 * JoyGetThreshold [WINMM.32]
334 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
337 MMRESULT16 ret = joyGetThreshold16(wID,&thresh);
339 *lpThreshold = thresh;
343 /**************************************************************************
344 * JoyGetThreshold [MMSYSTEM.104]
346 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
348 TRACE("(%04X, %p);\n", wID, lpThreshold);
349 if (wID >= MAXJOYDRIVERS) return JOYERR_PARMS;
350 *lpThreshold = joy_threshold[wID];
351 return JOYERR_NOERROR;
354 /**************************************************************************
355 * JoyReleaseCapture [WINMM.33]
357 MMRESULT WINAPI joyReleaseCapture(UINT wID)
359 return joyReleaseCapture16(wID);
362 /**************************************************************************
363 * JoyReleaseCapture [MMSYSTEM.105]
365 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
367 TRACE("(%04X);\n", wID);
372 return JOYERR_NOERROR;
375 /**************************************************************************
376 * JoySetCapture [MMSYSTEM.106]
378 MMRESULT WINAPI joySetCapture(HWND hWnd,UINT wID,UINT wPeriod,BOOL bChanged)
380 return joySetCapture16(hWnd,wID,wPeriod,bChanged);
383 /**************************************************************************
384 * JoySetCapture [MMSYSTEM.106]
386 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd,UINT16 wID,UINT16 wPeriod,BOOL16 bChanged)
389 TRACE("(%04X, %04X, %d, %d);\n",
390 hWnd, wID, wPeriod, bChanged);
392 if (!CaptureWnd[wID]) {
393 if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
395 CaptureWnd[wID] = hWnd;
396 return JOYERR_NOERROR;
399 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
402 /**************************************************************************
403 * JoySetThreshold [WINMM.35]
405 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
407 return joySetThreshold16(wID,wThreshold);
409 /**************************************************************************
410 * JoySetThreshold [MMSYSTEM.107]
412 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
414 TRACE("(%04X, %d);\n", wID, wThreshold);
416 if (wID > 3) return JOYERR_PARMS;
417 joy_threshold[wID] = wThreshold;
418 return JOYERR_NOERROR;
421 /**************************************************************************
422 * JoySetCalibration [MMSYSTEM.109]
424 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
426 FIXME("(%04X): stub.\n", wID);
427 return JOYERR_NOCANDO;