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 #include <sys/errno.h>
26 #define MAXJOYDRIVERS 4
28 static int count_use
[MAXJOYDRIVERS
] = {0, 0, 0, 0};
30 static int joy_nr_open
= 0;
31 static BOOL16 joyCaptured
= FALSE
;
32 static HWND16 CaptureWnd
[MAXJOYDRIVERS
] = {0, 0};
33 static int joy_dev
[MAXJOYDRIVERS
] = {-1, -1,-1,-1};
34 static JOYINFO16 joyCapData
[MAXJOYDRIVERS
];
35 static unsigned int joy_threshold
[MAXJOYDRIVERS
] = {0, 0, 0, 0};
45 /**************************************************************************
46 * joyOpenDriver [internal]
48 BOOL16
joyOpenDriver(WORD wID
)
50 char dev_name
[] = "/dev/js%d";
53 if (wID
>3) return FALSE
;
54 if (joy_dev
[wID
] >= 0) return TRUE
;
55 sprintf(buf
,dev_name
,wID
);
56 if ((joy_dev
[wID
] = open(buf
, O_RDONLY
)) >= 0) {
63 /**************************************************************************
64 * joyCloseDriver [internal]
66 void joyCloseDriver(WORD wID
)
68 if (joy_dev
[wID
] >= 0) {
75 /**************************************************************************
76 * joySendMessages [internal]
78 void joySendMessages(void)
85 for (joy
=0; joy
< MAXJOYDRIVERS
; joy
++)
86 if (joy_dev
[joy
] >= 0) {
87 if (count_use
[joy
] > 250) {
95 if (joyCaptured
== FALSE
) return;
97 TRACE(mmsys
, " --\n");
99 for (joy
=0; joy
< MAXJOYDRIVERS
; joy
++) {
100 if (joyOpenDriver(joy
) == FALSE
) continue;
101 dev_stat
= read(joy_dev
[joy
], &js
, sizeof(js
));
102 if (dev_stat
== sizeof(js
)) {
105 if ((joyCapData
[joy
].wXpos
!= js
.x
) || (joyCapData
[joy
].wYpos
!= js
.y
)) {
106 SendMessageA(CaptureWnd
[joy
], MM_JOY1MOVE
+ joy
, js
.buttons
, MAKELONG(js
.x
, js
.y
));
107 joyCapData
[joy
].wXpos
= js
.x
;
108 joyCapData
[joy
].wYpos
= js
.y
;
110 if (joyCapData
[joy
].wButtons
!= js
.buttons
) {
111 unsigned int ButtonChanged
= (WORD
)(joyCapData
[joy
].wButtons
^ js
.buttons
)<<8;
112 if (joyCapData
[joy
].wButtons
< js
.buttons
)
113 SendMessageA(CaptureWnd
[joy
], MM_JOY1BUTTONDOWN
+ joy
, ButtonChanged
, MAKELONG(js
.x
, js
.y
));
115 if (joyCapData
[joy
].wButtons
> js
.buttons
)
116 SendMessageA(CaptureWnd
[joy
], MM_JOY1BUTTONUP
117 + joy
, ButtonChanged
, MAKELONG(js
.x
, js
.y
));
118 joyCapData
[joy
].wButtons
= js
.buttons
;
125 /**************************************************************************
126 * JoyGetNumDevs [MMSYSTEM.101]
128 UINT WINAPI
joyGetNumDevs(void)
130 return joyGetNumDevs16();
133 /**************************************************************************
134 * JoyGetNumDevs [MMSYSTEM.101]
136 UINT16 WINAPI
joyGetNumDevs16(void)
141 for (joy
=0; joy
<MAXJOYDRIVERS
; joy
++)
142 if (joyOpenDriver(joy
) == TRUE
) {
146 TRACE(mmsys
, "returning %d\n", joy_cnt
);
147 if (!joy_cnt
) ERR(mmsys
, "No joystick found - "
148 "perhaps get joystick-0.8.0.tar.gz and load"
149 "it as module or use Linux >= 2.1.45 to be "
150 "able to use joysticks.\n");
154 /**************************************************************************
155 * JoyGetDevCaps [WINMM.27]
157 MMRESULT WINAPI
joyGetDevCapsA(UINT wID
, LPJOYCAPSA lpCaps
,UINT wSize
)
160 MMRESULT16 ret
= joyGetDevCaps16(wID
,&jc16
,sizeof(jc16
));
162 lpCaps
->wMid
= jc16
.wMid
;
163 lpCaps
->wPid
= jc16
.wPid
;
164 strcpy(lpCaps
->szPname
,jc16
.szPname
);
165 lpCaps
->wXmin
= jc16
.wXmin
;
166 lpCaps
->wXmax
= jc16
.wXmax
;
167 lpCaps
->wYmin
= jc16
.wYmin
;
168 lpCaps
->wYmax
= jc16
.wYmax
;
169 lpCaps
->wZmin
= jc16
.wZmin
;
170 lpCaps
->wZmax
= jc16
.wZmax
;
171 lpCaps
->wNumButtons
= jc16
.wNumButtons
;
172 lpCaps
->wPeriodMin
= jc16
.wPeriodMin
;
173 lpCaps
->wPeriodMax
= jc16
.wPeriodMax
;
175 lpCaps
->wRmin
= jc16
.wRmin
;
176 lpCaps
->wRmax
= jc16
.wRmax
;
177 lpCaps
->wUmin
= jc16
.wUmin
;
178 lpCaps
->wUmax
= jc16
.wUmax
;
179 lpCaps
->wVmin
= jc16
.wVmin
;
180 lpCaps
->wVmax
= jc16
.wVmax
;
181 lpCaps
->wCaps
= jc16
.wCaps
;
182 lpCaps
->wMaxAxes
= jc16
.wMaxAxes
;
183 lpCaps
->wNumAxes
= jc16
.wNumAxes
;
184 lpCaps
->wMaxButtons
= jc16
.wMaxButtons
;
185 strcpy(lpCaps
->szRegKey
,jc16
.szRegKey
);
186 strcpy(lpCaps
->szOEMVxD
,jc16
.szOEMVxD
);
190 /**************************************************************************
191 * JoyGetDevCaps [WINMM.28]
193 MMRESULT WINAPI
joyGetDevCapsW(UINT wID
, LPJOYCAPSW lpCaps
,UINT wSize
)
196 MMRESULT16 ret
= joyGetDevCaps16(wID
,&jc16
,sizeof(jc16
));
198 lpCaps
->wMid
= jc16
.wMid
;
199 lpCaps
->wPid
= jc16
.wPid
;
200 lstrcpyAtoW(lpCaps
->szPname
,jc16
.szPname
);
201 lpCaps
->wXmin
= jc16
.wXmin
;
202 lpCaps
->wXmax
= jc16
.wXmax
;
203 lpCaps
->wYmin
= jc16
.wYmin
;
204 lpCaps
->wYmax
= jc16
.wYmax
;
205 lpCaps
->wZmin
= jc16
.wZmin
;
206 lpCaps
->wZmax
= jc16
.wZmax
;
207 lpCaps
->wNumButtons
= jc16
.wNumButtons
;
208 lpCaps
->wPeriodMin
= jc16
.wPeriodMin
;
209 lpCaps
->wPeriodMax
= jc16
.wPeriodMax
;
211 lpCaps
->wRmin
= jc16
.wRmin
;
212 lpCaps
->wRmax
= jc16
.wRmax
;
213 lpCaps
->wUmin
= jc16
.wUmin
;
214 lpCaps
->wUmax
= jc16
.wUmax
;
215 lpCaps
->wVmin
= jc16
.wVmin
;
216 lpCaps
->wVmax
= jc16
.wVmax
;
217 lpCaps
->wCaps
= jc16
.wCaps
;
218 lpCaps
->wMaxAxes
= jc16
.wMaxAxes
;
219 lpCaps
->wNumAxes
= jc16
.wNumAxes
;
220 lpCaps
->wMaxButtons
= jc16
.wMaxButtons
;
221 lstrcpyAtoW(lpCaps
->szRegKey
,jc16
.szRegKey
);
222 lstrcpyAtoW(lpCaps
->szOEMVxD
,jc16
.szOEMVxD
);
225 /**************************************************************************
226 * JoyGetDevCaps [MMSYSTEM.102]
228 MMRESULT16 WINAPI
joyGetDevCaps16(UINT16 wID
, LPJOYCAPS16 lpCaps
, UINT16 wSize
)
230 TRACE(mmsys
, "(%04X, %p, %d);\n",
232 if (joyOpenDriver(wID
) == TRUE
) {
233 lpCaps
->wMid
= MM_MICROSOFT
;
234 lpCaps
->wPid
= MM_PC_JOYSTICK
;
235 strcpy(lpCaps
->szPname
, "WineJoy"); /* joystick product name */
236 lpCaps
->wXmin
= 0; /* FIXME */
237 lpCaps
->wXmax
= 0xffff;
239 lpCaps
->wYmax
= 0xffff;
241 lpCaps
->wZmax
= 0xffff;
242 lpCaps
->wNumButtons
= 2;
243 lpCaps
->wPeriodMin
= 0;
244 lpCaps
->wPeriodMax
= 50; /* FIXME end */
245 if (wSize
== sizeof(JOYCAPS16
)) {
246 /* complete 95 structure */
248 lpCaps
->wRmax
= 0xffff;
250 lpCaps
->wUmax
= 0xffff;
252 lpCaps
->wVmax
= 0xffff;
254 lpCaps
->wMaxAxes
= 6;
255 lpCaps
->wNumAxes
= 2;
256 lpCaps
->wMaxButtons
= 3;
257 strcpy(lpCaps
->szRegKey
,"");
258 strcpy(lpCaps
->szOEMVxD
,"");
261 return JOYERR_NOERROR
;
264 return MMSYSERR_NODRIVER
;
267 /**************************************************************************
268 * JoyGetPosEx [WINMM.31]
270 MMRESULT WINAPI
joyGetPosEx(UINT wID
, LPJOYINFOEX lpInfo
)
272 /* FIXME: implement it */
273 return MMSYSERR_NODRIVER
;
276 /**************************************************************************
277 * JoyGetPos [WINMM.30]
279 MMRESULT WINAPI
joyGetPos(UINT wID
, LPJOYINFO lpInfo
)
282 MMRESULT16 ret
= joyGetPos16(wID
,&ji
);
284 lpInfo
->wXpos
= ji
.wXpos
;
285 lpInfo
->wYpos
= ji
.wYpos
;
286 lpInfo
->wZpos
= ji
.wZpos
;
287 lpInfo
->wButtons
= ji
.wButtons
;
291 /**************************************************************************
292 * JoyGetPos [MMSYSTEM.103]
294 MMRESULT16 WINAPI
joyGetPos16(UINT16 wID
, LPJOYINFO16 lpInfo
)
298 TRACE(mmsys
, "(%04X, %p)\n", wID
, lpInfo
);
299 if (joyOpenDriver(wID
) == FALSE
) return MMSYSERR_NODRIVER
;
300 dev_stat
= read(joy_dev
[wID
], &js
, sizeof(js
));
301 if (dev_stat
!= sizeof(js
)) {
303 return JOYERR_UNPLUGGED
; /* FIXME: perhaps wrong, but what should I return else ? */
308 lpInfo
->wXpos
= js
.x
; /* FIXME: perhaps multiply it somehow ? */
309 lpInfo
->wYpos
= js
.y
;
310 lpInfo
->wZpos
= 0; /* FIXME: Don't know what to do with this value as joystick driver doesn't provide a Z value */
311 lpInfo
->wButtons
= js
.buttons
;
312 TRACE(mmsys
, "x: %d, y: %d, buttons: %d\n", js
.x
, js
.y
, js
.buttons
);
313 return JOYERR_NOERROR
;
316 /**************************************************************************
317 * JoyGetThreshold [WINMM.32]
319 MMRESULT WINAPI
joyGetThreshold(UINT wID
, LPUINT lpThreshold
)
322 MMRESULT16 ret
= joyGetThreshold16(wID
,&thresh
);
324 *lpThreshold
= thresh
;
328 /**************************************************************************
329 * JoyGetThreshold [MMSYSTEM.104]
331 MMRESULT16 WINAPI
joyGetThreshold16(UINT16 wID
, LPUINT16 lpThreshold
)
333 TRACE(mmsys
, "(%04X, %p);\n", wID
, lpThreshold
);
334 if (wID
>= MAXJOYDRIVERS
) return JOYERR_PARMS
;
335 *lpThreshold
= joy_threshold
[wID
];
336 return JOYERR_NOERROR
;
339 /**************************************************************************
340 * JoyReleaseCapture [WINMM.33]
342 MMRESULT WINAPI
joyReleaseCapture(UINT wID
)
344 return joyReleaseCapture16(wID
);
347 /**************************************************************************
348 * JoyReleaseCapture [MMSYSTEM.105]
350 MMRESULT16 WINAPI
joyReleaseCapture16(UINT16 wID
)
352 TRACE(mmsys
, "(%04X);\n", wID
);
357 return JOYERR_NOERROR
;
360 /**************************************************************************
361 * JoySetCapture [MMSYSTEM.106]
363 MMRESULT WINAPI
joySetCapture(HWND hWnd
,UINT wID
,UINT wPeriod
,BOOL bChanged
)
365 return joySetCapture16(hWnd
,wID
,wPeriod
,bChanged
);
368 /**************************************************************************
369 * JoySetCapture [MMSYSTEM.106]
371 MMRESULT16 WINAPI
joySetCapture16(HWND16 hWnd
,UINT16 wID
,UINT16 wPeriod
,BOOL16 bChanged
)
374 TRACE(mmsys
, "(%04X, %04X, %d, %d);\n",
375 hWnd
, wID
, wPeriod
, bChanged
);
377 if (!CaptureWnd
[wID
]) {
378 if (joyOpenDriver(wID
) == FALSE
) return MMSYSERR_NODRIVER
;
380 CaptureWnd
[wID
] = hWnd
;
381 return JOYERR_NOERROR
;
384 return JOYERR_NOCANDO
; /* FIXME: what should be returned ? */
387 /**************************************************************************
388 * JoySetThreshold [WINMM.35]
390 MMRESULT WINAPI
joySetThreshold(UINT wID
, UINT wThreshold
)
392 return joySetThreshold16(wID
,wThreshold
);
394 /**************************************************************************
395 * JoySetThreshold [MMSYSTEM.107]
397 MMRESULT16 WINAPI
joySetThreshold16(UINT16 wID
, UINT16 wThreshold
)
399 TRACE(mmsys
, "(%04X, %d);\n", wID
, wThreshold
);
401 if (wID
> 3) return JOYERR_PARMS
;
402 joy_threshold
[wID
] = wThreshold
;
403 return JOYERR_NOERROR
;
406 /**************************************************************************
407 * JoySetCalibration [MMSYSTEM.109]
409 MMRESULT16 WINAPI
joySetCalibration16(UINT16 wID
)
411 FIXME(mmsys
, "(%04X): stub.\n", wID
);
412 return JOYERR_NOCANDO
;