Converted to the new debug interface, using script written by Patrik
[wine/multimedia.git] / multimedia / joystick.c
bloba17a7d2d6fef451b58ff4e67500a13e30ae6344d
1 /*
2 * joystick functions
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.
16 #include <unistd.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/errno.h>
22 #include "winuser.h"
23 #include "winbase.h"
24 #include "mmsystem.h"
25 #include "debugtools.h"
27 DEFAULT_DEBUG_CHANNEL(mmsys)
29 #define MAXJOYDRIVERS 4
31 static int count_use[MAXJOYDRIVERS] = {0, 0, 0, 0};
32 static int dev_stat;
33 static int joy_nr_open = 0;
34 static BOOL16 joyCaptured = FALSE;
35 static HWND16 CaptureWnd[MAXJOYDRIVERS] = {0, 0};
36 static int joy_dev[MAXJOYDRIVERS] = {-1, -1,-1,-1};
37 static JOYINFO16 joyCapData[MAXJOYDRIVERS];
38 static unsigned int joy_threshold[MAXJOYDRIVERS] = {0, 0, 0, 0};
40 struct js_status
42 int buttons;
43 int x;
44 int y;
48 /**************************************************************************
49 * joyOpenDriver [internal]
51 BOOL16 joyOpenDriver(WORD wID)
53 char dev_name[] = "/dev/js%d";
54 char buf[20];
56 if (wID>3) return FALSE;
57 if (joy_dev[wID] >= 0) return TRUE;
58 sprintf(buf,dev_name,wID);
59 if ((joy_dev[wID] = open(buf, O_RDONLY)) >= 0) {
60 joy_nr_open++;
61 return TRUE;
62 } else
63 return FALSE;
66 /**************************************************************************
67 * joyCloseDriver [internal]
69 void joyCloseDriver(WORD wID)
71 if (joy_dev[wID] >= 0) {
72 close(joy_dev[wID]);
73 joy_dev[wID] = -1;
74 joy_nr_open--;
78 /**************************************************************************
79 * joySendMessages [internal]
81 void joySendMessages(void)
83 int joy;
84 struct js_status js;
86 if (joy_nr_open)
88 for (joy=0; joy < MAXJOYDRIVERS; joy++)
89 if (joy_dev[joy] >= 0) {
90 if (count_use[joy] > 250) {
91 joyCloseDriver(joy);
92 count_use[joy] = 0;
94 count_use[joy]++;
95 } else
96 return;
98 if (joyCaptured == FALSE) return;
100 TRACE(" --\n");
102 for (joy=0; joy < MAXJOYDRIVERS; joy++) {
103 if (joyOpenDriver(joy) == FALSE) continue;
104 dev_stat = read(joy_dev[joy], &js, sizeof(js));
105 if (dev_stat == sizeof(js)) {
106 js.x = js.x*37;
107 js.y = js.y*37;
108 if ((joyCapData[joy].wXpos != js.x) || (joyCapData[joy].wYpos != js.y)) {
109 SendMessageA(CaptureWnd[joy], MM_JOY1MOVE + joy, js.buttons, MAKELONG(js.x, js.y));
110 joyCapData[joy].wXpos = js.x;
111 joyCapData[joy].wYpos = js.y;
113 if (joyCapData[joy].wButtons != js.buttons) {
114 unsigned int ButtonChanged = (WORD)(joyCapData[joy].wButtons ^ js.buttons)<<8;
115 if (joyCapData[joy].wButtons < js.buttons)
116 SendMessageA(CaptureWnd[joy], MM_JOY1BUTTONDOWN + joy, ButtonChanged, MAKELONG(js.x, js.y));
117 else
118 if (joyCapData[joy].wButtons > js.buttons)
119 SendMessageA(CaptureWnd[joy], MM_JOY1BUTTONUP
120 + joy, ButtonChanged, MAKELONG(js.x, js.y));
121 joyCapData[joy].wButtons = js.buttons;
128 /**************************************************************************
129 * JoyGetNumDevs [MMSYSTEM.101]
131 UINT WINAPI joyGetNumDevs(void)
133 return joyGetNumDevs16();
136 /**************************************************************************
137 * JoyGetNumDevs [MMSYSTEM.101]
139 UINT16 WINAPI joyGetNumDevs16(void)
141 int joy;
142 UINT16 joy_cnt = 0;
144 for (joy=0; joy<MAXJOYDRIVERS; joy++)
145 if (joyOpenDriver(joy) == TRUE) {
146 joyCloseDriver(joy);
147 joy_cnt++;
149 TRACE("returning %d\n", joy_cnt);
150 if (!joy_cnt) ERR("No joystick found - "
151 "perhaps get joystick-0.8.0.tar.gz and load"
152 "it as module or use Linux >= 2.1.45 to be "
153 "able to use joysticks.\n");
154 return joy_cnt;
157 /**************************************************************************
158 * JoyGetDevCaps [WINMM.27]
160 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps,UINT wSize)
162 JOYCAPS16 jc16;
163 MMRESULT16 ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
165 lpCaps->wMid = jc16.wMid;
166 lpCaps->wPid = jc16.wPid;
167 strcpy(lpCaps->szPname,jc16.szPname);
168 lpCaps->wXmin = jc16.wXmin;
169 lpCaps->wXmax = jc16.wXmax;
170 lpCaps->wYmin = jc16.wYmin;
171 lpCaps->wYmax = jc16.wYmax;
172 lpCaps->wZmin = jc16.wZmin;
173 lpCaps->wZmax = jc16.wZmax;
174 lpCaps->wNumButtons = jc16.wNumButtons;
175 lpCaps->wPeriodMin = jc16.wPeriodMin;
176 lpCaps->wPeriodMax = jc16.wPeriodMax;
178 lpCaps->wRmin = jc16.wRmin;
179 lpCaps->wRmax = jc16.wRmax;
180 lpCaps->wUmin = jc16.wUmin;
181 lpCaps->wUmax = jc16.wUmax;
182 lpCaps->wVmin = jc16.wVmin;
183 lpCaps->wVmax = jc16.wVmax;
184 lpCaps->wCaps = jc16.wCaps;
185 lpCaps->wMaxAxes = jc16.wMaxAxes;
186 lpCaps->wNumAxes = jc16.wNumAxes;
187 lpCaps->wMaxButtons = jc16.wMaxButtons;
188 strcpy(lpCaps->szRegKey,jc16.szRegKey);
189 strcpy(lpCaps->szOEMVxD,jc16.szOEMVxD);
190 return ret;
193 /**************************************************************************
194 * JoyGetDevCaps [WINMM.28]
196 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps,UINT wSize)
198 JOYCAPS16 jc16;
199 MMRESULT16 ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
201 lpCaps->wMid = jc16.wMid;
202 lpCaps->wPid = jc16.wPid;
203 lstrcpyAtoW(lpCaps->szPname,jc16.szPname);
204 lpCaps->wXmin = jc16.wXmin;
205 lpCaps->wXmax = jc16.wXmax;
206 lpCaps->wYmin = jc16.wYmin;
207 lpCaps->wYmax = jc16.wYmax;
208 lpCaps->wZmin = jc16.wZmin;
209 lpCaps->wZmax = jc16.wZmax;
210 lpCaps->wNumButtons = jc16.wNumButtons;
211 lpCaps->wPeriodMin = jc16.wPeriodMin;
212 lpCaps->wPeriodMax = jc16.wPeriodMax;
214 lpCaps->wRmin = jc16.wRmin;
215 lpCaps->wRmax = jc16.wRmax;
216 lpCaps->wUmin = jc16.wUmin;
217 lpCaps->wUmax = jc16.wUmax;
218 lpCaps->wVmin = jc16.wVmin;
219 lpCaps->wVmax = jc16.wVmax;
220 lpCaps->wCaps = jc16.wCaps;
221 lpCaps->wMaxAxes = jc16.wMaxAxes;
222 lpCaps->wNumAxes = jc16.wNumAxes;
223 lpCaps->wMaxButtons = jc16.wMaxButtons;
224 lstrcpyAtoW(lpCaps->szRegKey,jc16.szRegKey);
225 lstrcpyAtoW(lpCaps->szOEMVxD,jc16.szOEMVxD);
226 return ret;
228 /**************************************************************************
229 * JoyGetDevCaps [MMSYSTEM.102]
231 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
233 TRACE("(%04X, %p, %d);\n",
234 wID, lpCaps, wSize);
235 if (joyOpenDriver(wID) == TRUE) {
236 lpCaps->wMid = MM_MICROSOFT;
237 lpCaps->wPid = MM_PC_JOYSTICK;
238 strcpy(lpCaps->szPname, "WineJoy"); /* joystick product name */
239 lpCaps->wXmin = 0; /* FIXME */
240 lpCaps->wXmax = 0xffff;
241 lpCaps->wYmin = 0;
242 lpCaps->wYmax = 0xffff;
243 lpCaps->wZmin = 0;
244 lpCaps->wZmax = 0xffff;
245 lpCaps->wNumButtons = 2;
246 lpCaps->wPeriodMin = 0;
247 lpCaps->wPeriodMax = 50; /* FIXME end */
248 if (wSize == sizeof(JOYCAPS16)) {
249 /* complete 95 structure */
250 lpCaps->wRmin = 0;
251 lpCaps->wRmax = 0xffff;
252 lpCaps->wUmin = 0;
253 lpCaps->wUmax = 0xffff;
254 lpCaps->wVmin = 0;
255 lpCaps->wVmax = 0xffff;
256 lpCaps->wCaps = 0;
257 lpCaps->wMaxAxes = 6;
258 lpCaps->wNumAxes = 2;
259 lpCaps->wMaxButtons = 3;
260 strcpy(lpCaps->szRegKey,"");
261 strcpy(lpCaps->szOEMVxD,"");
263 joyCloseDriver(wID);
264 return JOYERR_NOERROR;
266 else
267 return MMSYSERR_NODRIVER;
270 /**************************************************************************
271 * JoyGetPosEx [WINMM.31]
273 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
275 /* FIXME: implement it */
276 return MMSYSERR_NODRIVER;
279 /**************************************************************************
280 * JoyGetPos [WINMM.30]
282 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
284 JOYINFO16 ji;
285 MMRESULT16 ret = joyGetPos16(wID,&ji);
287 lpInfo->wXpos = ji.wXpos;
288 lpInfo->wYpos = ji.wYpos;
289 lpInfo->wZpos = ji.wZpos;
290 lpInfo->wButtons = ji.wButtons;
291 return ret;
294 /**************************************************************************
295 * JoyGetPos [MMSYSTEM.103]
297 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
299 struct js_status js;
301 TRACE("(%04X, %p)\n", wID, lpInfo);
302 if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
303 dev_stat = read(joy_dev[wID], &js, sizeof(js));
304 if (dev_stat != sizeof(js)) {
305 joyCloseDriver(wID);
306 return JOYERR_UNPLUGGED; /* FIXME: perhaps wrong, but what should I return else ? */
308 count_use[wID] = 0;
309 js.x = js.x*37;
310 js.y = js.y*37;
311 lpInfo->wXpos = js.x; /* FIXME: perhaps multiply it somehow ? */
312 lpInfo->wYpos = js.y;
313 lpInfo->wZpos = 0; /* FIXME: Don't know what to do with this value as joystick driver doesn't provide a Z value */
314 lpInfo->wButtons = js.buttons;
315 TRACE("x: %d, y: %d, buttons: %d\n", js.x, js.y, js.buttons);
316 return JOYERR_NOERROR;
319 /**************************************************************************
320 * JoyGetThreshold [WINMM.32]
322 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
324 UINT16 thresh;
325 MMRESULT16 ret = joyGetThreshold16(wID,&thresh);
327 *lpThreshold = thresh;
328 return ret;
331 /**************************************************************************
332 * JoyGetThreshold [MMSYSTEM.104]
334 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
336 TRACE("(%04X, %p);\n", wID, lpThreshold);
337 if (wID >= MAXJOYDRIVERS) return JOYERR_PARMS;
338 *lpThreshold = joy_threshold[wID];
339 return JOYERR_NOERROR;
342 /**************************************************************************
343 * JoyReleaseCapture [WINMM.33]
345 MMRESULT WINAPI joyReleaseCapture(UINT wID)
347 return joyReleaseCapture16(wID);
350 /**************************************************************************
351 * JoyReleaseCapture [MMSYSTEM.105]
353 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
355 TRACE("(%04X);\n", wID);
356 joyCaptured = FALSE;
357 joyCloseDriver(wID);
358 joy_dev[wID] = -1;
359 CaptureWnd[wID] = 0;
360 return JOYERR_NOERROR;
363 /**************************************************************************
364 * JoySetCapture [MMSYSTEM.106]
366 MMRESULT WINAPI joySetCapture(HWND hWnd,UINT wID,UINT wPeriod,BOOL bChanged)
368 return joySetCapture16(hWnd,wID,wPeriod,bChanged);
371 /**************************************************************************
372 * JoySetCapture [MMSYSTEM.106]
374 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd,UINT16 wID,UINT16 wPeriod,BOOL16 bChanged)
377 TRACE("(%04X, %04X, %d, %d);\n",
378 hWnd, wID, wPeriod, bChanged);
380 if (!CaptureWnd[wID]) {
381 if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
382 joyCaptured = TRUE;
383 CaptureWnd[wID] = hWnd;
384 return JOYERR_NOERROR;
386 else
387 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
390 /**************************************************************************
391 * JoySetThreshold [WINMM.35]
393 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
395 return joySetThreshold16(wID,wThreshold);
397 /**************************************************************************
398 * JoySetThreshold [MMSYSTEM.107]
400 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
402 TRACE("(%04X, %d);\n", wID, wThreshold);
404 if (wID > 3) return JOYERR_PARMS;
405 joy_threshold[wID] = wThreshold;
406 return JOYERR_NOERROR;
409 /**************************************************************************
410 * JoySetCalibration [MMSYSTEM.109]
412 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
414 FIXME("(%04X): stub.\n", wID);
415 return JOYERR_NOCANDO;