Small fixes.
[wine/multimedia.git] / multimedia / joystick.c
blob102f13af6346a3dab6d6a53411d91425c9d2b27f
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 "windows.h"
23 #include "ldt.h"
24 #include "user.h"
25 #include "driver.h"
26 #include "mmsystem.h"
27 #include "debug.h"
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 (joy_dev[wID] >= 0) return TRUE;
57 sprintf(buf,dev_name,wID);
58 if ((joy_dev[wID] = open(buf, O_RDONLY)) >= 0) {
59 joy_nr_open++;
60 return TRUE;
61 } else
62 return FALSE;
65 /**************************************************************************
66 * joyCloseDriver [internal]
68 void joyCloseDriver(WORD wID)
70 if (joy_dev[wID] >= 0) {
71 close(joy_dev[wID]);
72 joy_dev[wID] = -1;
73 joy_nr_open--;
77 /**************************************************************************
78 * joySendMessages [internal]
80 void joySendMessages(void)
82 int joy;
83 struct js_status js;
85 if (joy_nr_open)
86 for (joy=0; joy < MAXJOYDRIVERS; joy++)
87 if (joy_dev[joy] >= 0) {
88 if (count_use[joy] > 250) {
89 joyCloseDriver(joy);
90 count_use[joy] = 0;
92 count_use[joy]++;
93 } else
94 return;
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)) {
103 js.x = js.x*37;
104 js.y = js.y*37;
105 if ((joyCapData[joy].wXpos != js.x) || (joyCapData[joy].wYpos != js.y)) {
106 SendMessage32A(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 SendMessage32A(CaptureWnd[joy], MM_JOY1BUTTONDOWN + joy, ButtonChanged, MAKELONG(js.x, js.y));
114 else
115 if (joyCapData[joy].wButtons > js.buttons)
116 SendMessage32A(CaptureWnd[joy], MM_JOY1BUTTONUP
117 + joy, ButtonChanged, MAKELONG(js.x, js.y));
118 joyCapData[joy].wButtons = js.buttons;
125 /**************************************************************************
126 * JoyGetNumDevs [MMSYSTEM.101]
128 UINT32 WINAPI joyGetNumDevs32(void)
130 return joyGetNumDevs16();
133 /**************************************************************************
134 * JoyGetNumDevs [MMSYSTEM.101]
136 UINT16 WINAPI joyGetNumDevs16(void)
138 int joy;
139 UINT16 joy_cnt = 0;
141 for (joy=0; joy<MAXJOYDRIVERS; joy++)
142 if (joyOpenDriver(joy) == TRUE) {
143 joyCloseDriver(joy);
144 joy_cnt++;
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");
151 return joy_cnt;
154 /**************************************************************************
155 * JoyGetDevCaps [WINMM.27]
157 MMRESULT32 WINAPI joyGetDevCaps32A(UINT32 wID, LPJOYCAPS32A lpCaps,UINT32 wSize)
159 JOYCAPS16 jc16;
160 MMRESULT16 ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
162 lpCaps->wMid = jc16.wMid;
163 lpCaps->wPid = jc16.wPid;
164 lstrcpy32A(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 lstrcpy32A(lpCaps->szRegKey,jc16.szRegKey);
186 lstrcpy32A(lpCaps->szOEMVxD,jc16.szOEMVxD);
187 return ret;
190 /**************************************************************************
191 * JoyGetDevCaps [WINMM.28]
193 MMRESULT32 WINAPI joyGetDevCaps32W(UINT32 wID, LPJOYCAPS32W lpCaps,UINT32 wSize)
195 JOYCAPS16 jc16;
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);
223 return ret;
225 /**************************************************************************
226 * JoyGetDevCaps [MMSYSTEM.102]
228 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
230 TRACE(mmsys, "(%04X, %p, %d);\n",
231 wID, lpCaps, wSize);
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;
238 lpCaps->wYmin = 0;
239 lpCaps->wYmax = 0xffff;
240 lpCaps->wZmin = 0;
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 */
247 lpCaps->wRmin = 0;
248 lpCaps->wRmax = 0xffff;
249 lpCaps->wUmin = 0;
250 lpCaps->wUmax = 0xffff;
251 lpCaps->wVmin = 0;
252 lpCaps->wVmax = 0xffff;
253 lpCaps->wCaps = 0;
254 lpCaps->wMaxAxes = 6;
255 lpCaps->wNumAxes = 2;
256 lpCaps->wMaxButtons = 3;
257 strcpy(lpCaps->szRegKey,"");
258 strcpy(lpCaps->szOEMVxD,"");
260 joyCloseDriver(wID);
261 return JOYERR_NOERROR;
263 else
264 return MMSYSERR_NODRIVER;
267 /**************************************************************************
268 * JoyGetPosEx [WINMM.31]
270 MMRESULT32 WINAPI joyGetPosEx(UINT32 wID, LPJOYINFOEX lpInfo)
272 /* FIXME: implement it */
273 return MMSYSERR_NODRIVER;
276 /**************************************************************************
277 * JoyGetPos [WINMM.30]
279 MMRESULT32 WINAPI joyGetPos32(UINT32 wID, LPJOYINFO32 lpInfo)
281 JOYINFO16 ji;
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;
288 return ret;
291 /**************************************************************************
292 * JoyGetPos [MMSYSTEM.103]
294 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
296 struct js_status js;
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)) {
302 joyCloseDriver(wID);
303 return JOYERR_UNPLUGGED; /* FIXME: perhaps wrong, but what should I return else ? */
305 count_use[wID] = 0;
306 js.x = js.x*37;
307 js.y = js.y*37;
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 MMRESULT32 WINAPI joyGetThreshold32(UINT32 wID, LPUINT32 lpThreshold)
321 UINT16 thresh;
322 MMRESULT16 ret = joyGetThreshold16(wID,&thresh);
324 *lpThreshold = thresh;
325 return ret;
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 MMRESULT32 WINAPI joyReleaseCapture32(UINT32 wID)
344 return joyReleaseCapture16(wID);
347 /**************************************************************************
348 * JoyReleaseCapture [MMSYSTEM.105]
350 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
352 TRACE(mmsys, "(%04X);\n", wID);
353 joyCaptured = FALSE;
354 joyCloseDriver(wID);
355 joy_dev[wID] = -1;
356 CaptureWnd[wID] = 0;
357 return JOYERR_NOERROR;
360 /**************************************************************************
361 * JoySetCapture [MMSYSTEM.106]
363 MMRESULT32 WINAPI joySetCapture32(HWND32 hWnd,UINT32 wID,UINT32 wPeriod,BOOL32 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;
379 joyCaptured = TRUE;
380 CaptureWnd[wID] = hWnd;
381 return JOYERR_NOERROR;
383 else
384 return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
387 /**************************************************************************
388 * JoySetThreshold [WINMM.35]
390 MMRESULT32 WINAPI joySetThreshold32(UINT32 wID, UINT32 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;