Reading joystick 5 when we only support 4 should fail instead of
[wine] / multimedia / joystick.c
1 /*
2  * joystick functions
3  *
4  * Copyright 1997 Andreas Mohr
5  *
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.
14  */
15
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"
28
29 #define MAXJOYDRIVERS   4
30
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};
39
40 struct js_status
41 {
42     int buttons;
43     int x;
44     int y;
45 };
46
47
48 /**************************************************************************
49  *                              joyOpenDriver           [internal]
50  */
51 BOOL16 joyOpenDriver(WORD wID)
52 {
53         char dev_name[] = "/dev/js%d";
54         char    buf[20];
55
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;
64 }
65
66 /**************************************************************************
67  *                              joyCloseDriver           [internal]
68  */
69 void joyCloseDriver(WORD wID)
70 {
71         if (joy_dev[wID] >= 0) {
72                 close(joy_dev[wID]);
73                 joy_dev[wID] = -1;
74                 joy_nr_open--;
75         }
76 }
77
78 /**************************************************************************
79  *                              joySendMessages           [internal]
80  */
81 void joySendMessages(void)
82 {
83         int joy;
84         struct js_status js;
85
86         if (joy_nr_open)
87         {
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;
93                         }
94                         count_use[joy]++;
95                 } else
96                         return;
97         }
98         if (joyCaptured == FALSE) return;
99
100         TRACE(mmsys, " --\n");
101
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                                 SendMessage32A(CaptureWnd[joy], MM_JOY1MOVE + joy, js.buttons, MAKELONG(js.x, js.y));
110                                 joyCapData[joy].wXpos = js.x;
111                                 joyCapData[joy].wYpos = js.y;
112                         }
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                                 SendMessage32A(CaptureWnd[joy], MM_JOY1BUTTONDOWN + joy, ButtonChanged, MAKELONG(js.x, js.y));
117                                 else
118                                 if (joyCapData[joy].wButtons > js.buttons)
119                                 SendMessage32A(CaptureWnd[joy], MM_JOY1BUTTONUP
120 + joy, ButtonChanged, MAKELONG(js.x, js.y));
121                                 joyCapData[joy].wButtons = js.buttons;
122                         }
123                 }
124         }
125 }
126
127
128 /**************************************************************************
129  *                              JoyGetNumDevs           [MMSYSTEM.101]
130  */
131 UINT32 WINAPI joyGetNumDevs32(void)
132 {
133         return joyGetNumDevs16();
134 }
135
136 /**************************************************************************
137  *                              JoyGetNumDevs           [MMSYSTEM.101]
138  */
139 UINT16 WINAPI joyGetNumDevs16(void)
140 {
141     int joy;
142     UINT16 joy_cnt = 0;
143
144     for (joy=0; joy<MAXJOYDRIVERS; joy++)
145         if (joyOpenDriver(joy) == TRUE) {               
146                 joyCloseDriver(joy);
147                 joy_cnt++;
148     }
149     TRACE(mmsys, "returning %d\n", joy_cnt);
150     if (!joy_cnt) ERR(mmsys, "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;
155 }
156
157 /**************************************************************************
158  *                              JoyGetDevCaps           [WINMM.27]
159  */
160 MMRESULT32 WINAPI joyGetDevCaps32A(UINT32 wID, LPJOYCAPS32A lpCaps,UINT32 wSize)
161 {
162         JOYCAPS16       jc16;
163         MMRESULT16      ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
164
165         lpCaps->wMid = jc16.wMid;
166         lpCaps->wPid = jc16.wPid;
167         lstrcpy32A(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;
177
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         lstrcpy32A(lpCaps->szRegKey,jc16.szRegKey);
189         lstrcpy32A(lpCaps->szOEMVxD,jc16.szOEMVxD);
190         return ret;
191 }
192
193 /**************************************************************************
194  *                              JoyGetDevCaps           [WINMM.28]
195  */
196 MMRESULT32 WINAPI joyGetDevCaps32W(UINT32 wID, LPJOYCAPS32W lpCaps,UINT32 wSize)
197 {
198         JOYCAPS16       jc16;
199         MMRESULT16      ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
200
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;
213
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;
227 }
228 /**************************************************************************
229  *                              JoyGetDevCaps           [MMSYSTEM.102]
230  */
231 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
232 {
233     TRACE(mmsys, "(%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,"");
262         }
263         joyCloseDriver(wID);
264         return JOYERR_NOERROR;
265     }
266     else
267     return MMSYSERR_NODRIVER;
268 }
269
270 /**************************************************************************
271  *                              JoyGetPosEx             [WINMM.31]
272  */
273 MMRESULT32 WINAPI joyGetPosEx(UINT32 wID, LPJOYINFOEX lpInfo)
274 {
275         /* FIXME: implement it */
276         return MMSYSERR_NODRIVER;
277 }
278
279 /**************************************************************************
280  *                              JoyGetPos               [WINMM.30]
281  */
282 MMRESULT32 WINAPI joyGetPos32(UINT32 wID, LPJOYINFO32 lpInfo)
283 {
284         JOYINFO16       ji;
285         MMRESULT16      ret = joyGetPos16(wID,&ji);
286
287         lpInfo->wXpos = ji.wXpos;
288         lpInfo->wYpos = ji.wYpos;
289         lpInfo->wZpos = ji.wZpos;
290         lpInfo->wButtons = ji.wButtons;
291         return ret;
292 }
293
294 /**************************************************************************
295  *                              JoyGetPos               [MMSYSTEM.103]
296  */
297 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
298 {
299         struct js_status js;
300
301         TRACE(mmsys, "(%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 ? */
307         }
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(mmsys, "x: %d, y: %d, buttons: %d\n", js.x, js.y, js.buttons);
316         return JOYERR_NOERROR;
317 }
318
319 /**************************************************************************
320  *                              JoyGetThreshold         [WINMM.32]
321  */
322 MMRESULT32 WINAPI joyGetThreshold32(UINT32 wID, LPUINT32 lpThreshold)
323 {
324         UINT16          thresh;
325         MMRESULT16      ret = joyGetThreshold16(wID,&thresh);
326
327         *lpThreshold = thresh;
328         return ret;
329 }
330
331 /**************************************************************************
332  *                              JoyGetThreshold         [MMSYSTEM.104]
333  */
334 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
335 {
336     TRACE(mmsys, "(%04X, %p);\n", wID, lpThreshold);
337     if (wID >= MAXJOYDRIVERS) return JOYERR_PARMS;
338     *lpThreshold = joy_threshold[wID];
339     return JOYERR_NOERROR;
340 }
341
342 /**************************************************************************
343  *                              JoyReleaseCapture       [WINMM.33]
344  */
345 MMRESULT32 WINAPI joyReleaseCapture32(UINT32 wID)
346 {
347         return joyReleaseCapture16(wID);
348 }
349
350 /**************************************************************************
351  *                              JoyReleaseCapture       [MMSYSTEM.105]
352  */
353 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
354 {
355     TRACE(mmsys, "(%04X);\n", wID);
356     joyCaptured = FALSE;
357     joyCloseDriver(wID);
358     joy_dev[wID] = -1;
359     CaptureWnd[wID] = 0;
360     return JOYERR_NOERROR;
361 }
362
363 /**************************************************************************
364  *                              JoySetCapture           [MMSYSTEM.106]
365  */
366 MMRESULT32 WINAPI joySetCapture32(HWND32 hWnd,UINT32 wID,UINT32 wPeriod,BOOL32 bChanged)
367 {
368         return joySetCapture16(hWnd,wID,wPeriod,bChanged);
369 }
370
371 /**************************************************************************
372  *                              JoySetCapture           [MMSYSTEM.106]
373  */
374 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd,UINT16 wID,UINT16 wPeriod,BOOL16 bChanged)
375 {
376
377     TRACE(mmsys, "(%04X, %04X, %d, %d);\n",
378             hWnd, wID, wPeriod, bChanged);
379
380     if (!CaptureWnd[wID]) {
381         if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
382         joyCaptured = TRUE;
383         CaptureWnd[wID] = hWnd;
384         return JOYERR_NOERROR;
385     }
386     else
387     return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
388 }
389
390 /**************************************************************************
391  *                              JoySetThreshold         [WINMM.35]
392  */
393 MMRESULT32 WINAPI joySetThreshold32(UINT32 wID, UINT32 wThreshold)
394 {
395         return joySetThreshold16(wID,wThreshold);
396 }
397 /**************************************************************************
398  *                              JoySetThreshold         [MMSYSTEM.107]
399  */
400 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
401 {
402     TRACE(mmsys, "(%04X, %d);\n", wID, wThreshold);
403
404     if (wID > 3) return JOYERR_PARMS;
405     joy_threshold[wID] = wThreshold;
406     return JOYERR_NOERROR;
407 }
408
409 /**************************************************************************
410  *                              JoySetCalibration       [MMSYSTEM.109]
411  */
412 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
413 {
414     FIXME(mmsys, "(%04X): stub.\n", wID);
415     return JOYERR_NOCANDO;
416 }