Small, suboptimal, fix for 32 bit cdaudio open routines. Needs proper
[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 (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;
63 }
64
65 /**************************************************************************
66  *                              joyCloseDriver           [internal]
67  */
68 void joyCloseDriver(WORD wID)
69 {
70         if (joy_dev[wID] >= 0) {
71                 close(joy_dev[wID]);
72                 joy_dev[wID] = -1;
73                 joy_nr_open--;
74         }
75 }
76
77 /**************************************************************************
78  *                              joySendMessages           [internal]
79  */
80 void joySendMessages(void)
81 {
82         int joy;
83         struct js_status js;
84
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;
91                         }
92                         count_use[joy]++;
93                 } else
94                         return;
95         if (joyCaptured == FALSE) return;
96
97         TRACE(mmsys, " --\n");
98
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;
109                         }
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;
119                         }
120                 }
121         }
122 }
123
124
125 /**************************************************************************
126  *                              JoyGetNumDevs           [MMSYSTEM.101]
127  */
128 UINT32 WINAPI joyGetNumDevs32(void)
129 {
130         return joyGetNumDevs16();
131 }
132
133 /**************************************************************************
134  *                              JoyGetNumDevs           [MMSYSTEM.101]
135  */
136 UINT16 WINAPI joyGetNumDevs16(void)
137 {
138     int joy;
139     UINT16 joy_cnt = 0;
140
141     for (joy=0; joy<MAXJOYDRIVERS; joy++)
142         if (joyOpenDriver(joy) == TRUE) {               
143                 joyCloseDriver(joy);
144                 joy_cnt++;
145     }
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;
152 }
153
154 /**************************************************************************
155  *                              JoyGetDevCaps           [WINMM.27]
156  */
157 MMRESULT32 WINAPI joyGetDevCaps32A(UINT32 wID, LPJOYCAPS32A lpCaps,UINT32 wSize)
158 {
159         JOYCAPS16       jc16;
160         MMRESULT16      ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
161
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;
174
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;
188 }
189
190 /**************************************************************************
191  *                              JoyGetDevCaps           [WINMM.28]
192  */
193 MMRESULT32 WINAPI joyGetDevCaps32W(UINT32 wID, LPJOYCAPS32W lpCaps,UINT32 wSize)
194 {
195         JOYCAPS16       jc16;
196         MMRESULT16      ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
197
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;
210
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;
224 }
225 /**************************************************************************
226  *                              JoyGetDevCaps           [MMSYSTEM.102]
227  */
228 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
229 {
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,"");
259         }
260         joyCloseDriver(wID);
261         return JOYERR_NOERROR;
262     }
263     else
264     return MMSYSERR_NODRIVER;
265 }
266
267 /**************************************************************************
268  *                              JoyGetPosEx             [WINMM.31]
269  */
270 MMRESULT32 WINAPI joyGetPosEx(UINT32 wID, LPJOYINFOEX lpInfo)
271 {
272         /* FIXME: implement it */
273         return MMSYSERR_NODRIVER;
274 }
275
276 /**************************************************************************
277  *                              JoyGetPos               [WINMM.30]
278  */
279 MMRESULT32 WINAPI joyGetPos32(UINT32 wID, LPJOYINFO32 lpInfo)
280 {
281         JOYINFO16       ji;
282         MMRESULT16      ret = joyGetPos16(wID,&ji);
283
284         lpInfo->wXpos = ji.wXpos;
285         lpInfo->wYpos = ji.wYpos;
286         lpInfo->wZpos = ji.wZpos;
287         lpInfo->wButtons = ji.wButtons;
288         return ret;
289 }
290
291 /**************************************************************************
292  *                              JoyGetPos               [MMSYSTEM.103]
293  */
294 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
295 {
296         struct js_status js;
297
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 ? */
304         }
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;
314 }
315
316 /**************************************************************************
317  *                              JoyGetThreshold         [WINMM.32]
318  */
319 MMRESULT32 WINAPI joyGetThreshold32(UINT32 wID, LPUINT32 lpThreshold)
320 {
321         UINT16          thresh;
322         MMRESULT16      ret = joyGetThreshold16(wID,&thresh);
323
324         *lpThreshold = thresh;
325         return ret;
326 }
327
328 /**************************************************************************
329  *                              JoyGetThreshold         [MMSYSTEM.104]
330  */
331 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
332 {
333     TRACE(mmsys, "(%04X, %p);\n", wID, lpThreshold);
334     if (wID >= MAXJOYDRIVERS) return JOYERR_PARMS;
335     *lpThreshold = joy_threshold[wID];
336     return JOYERR_NOERROR;
337 }
338
339 /**************************************************************************
340  *                              JoyReleaseCapture       [WINMM.33]
341  */
342 MMRESULT32 WINAPI joyReleaseCapture32(UINT32 wID)
343 {
344         return joyReleaseCapture16(wID);
345 }
346
347 /**************************************************************************
348  *                              JoyReleaseCapture       [MMSYSTEM.105]
349  */
350 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
351 {
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;
358 }
359
360 /**************************************************************************
361  *                              JoySetCapture           [MMSYSTEM.106]
362  */
363 MMRESULT32 WINAPI joySetCapture32(HWND32 hWnd,UINT32 wID,UINT32 wPeriod,BOOL32 bChanged)
364 {
365         return joySetCapture16(hWnd,wID,wPeriod,bChanged);
366 }
367
368 /**************************************************************************
369  *                              JoySetCapture           [MMSYSTEM.106]
370  */
371 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd,UINT16 wID,UINT16 wPeriod,BOOL16 bChanged)
372 {
373
374     TRACE(mmsys, "(%04X, %04X, %d, %d);\n",
375             hWnd, wID, wPeriod, bChanged);
376
377     if (!CaptureWnd[wID]) {
378         if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
379         joyCaptured = TRUE;
380         CaptureWnd[wID] = hWnd;
381         return JOYERR_NOERROR;
382     }
383     else
384     return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
385 }
386
387 /**************************************************************************
388  *                              JoySetThreshold         [WINMM.35]
389  */
390 MMRESULT32 WINAPI joySetThreshold32(UINT32 wID, UINT32 wThreshold)
391 {
392         return joySetThreshold16(wID,wThreshold);
393 }
394 /**************************************************************************
395  *                              JoySetThreshold         [MMSYSTEM.107]
396  */
397 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
398 {
399     TRACE(mmsys, "(%04X, %d);\n", wID, wThreshold);
400
401     if (wID > 3) return JOYERR_PARMS;
402     joy_threshold[wID] = wThreshold;
403     return JOYERR_NOERROR;
404 }
405
406 /**************************************************************************
407  *                              JoySetCalibration       [MMSYSTEM.109]
408  */
409 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
410 {
411     FIXME(mmsys, "(%04X): stub.\n", wID);
412     return JOYERR_NOCANDO;
413 }