Release 980503
[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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <fcntl.h>
21 #include <sys/ioctl.h>
22 #include <sys/errno.h>
23 #include "windows.h"
24 #include "ldt.h"
25 #include "user.h"
26 #include "driver.h"
27 #include "mmsystem.h"
28 #include "debug.h"
29
30 #define MAXJOYDRIVERS   4
31
32 static int count_use[MAXJOYDRIVERS] = {0, 0, 0, 0};
33 static int dev_stat;
34 static int joy_nr_open = 0;
35 static BOOL16 joyCaptured = FALSE;
36 static HWND16 CaptureWnd[MAXJOYDRIVERS] = {0, 0};
37 static int joy_dev[MAXJOYDRIVERS] = {-1, -1,-1,-1};
38 static JOYINFO16 joyCapData[MAXJOYDRIVERS];
39 static unsigned int joy_threshold[MAXJOYDRIVERS] = {0, 0, 0, 0};
40
41 struct js_status
42 {
43     int buttons;
44     int x;
45     int y;
46 };
47
48
49 /**************************************************************************
50  *                              joyOpenDriver           [internal]
51  */
52 BOOL16 joyOpenDriver(WORD wID)
53 {
54         char dev_name[] = "/dev/js%d";
55         char    buf[20];
56
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         for (joy=0; joy < MAXJOYDRIVERS; joy++) 
88                 if (joy_dev[joy] >= 0) {
89                         if (count_use[joy] > 250) {
90                                 joyCloseDriver(joy);
91                                 count_use[joy] = 0;
92                         }
93                         count_use[joy]++;
94                 } else
95                         return;
96         if (joyCaptured == FALSE) return;
97
98         TRACE(mmsys, " --\n");
99
100         for (joy=0; joy < MAXJOYDRIVERS; joy++) {
101                 if (joyOpenDriver(joy) == FALSE) continue;
102                 dev_stat = read(joy_dev[joy], &js, sizeof(js));
103                 if (dev_stat == sizeof(js)) {
104                         js.x = js.x*37;
105                         js.y = js.y*37;
106                         if ((joyCapData[joy].wXpos != js.x) || (joyCapData[joy].wYpos != js.y)) {
107                                 SendMessage32A(CaptureWnd[joy], MM_JOY1MOVE + joy, js.buttons, MAKELONG(js.x, js.y));
108                                 joyCapData[joy].wXpos = js.x;
109                                 joyCapData[joy].wYpos = js.y;
110                         }
111                         if (joyCapData[joy].wButtons != js.buttons) {
112                                 unsigned int ButtonChanged = (WORD)(joyCapData[joy].wButtons ^ js.buttons)<<8;
113                                 if (joyCapData[joy].wButtons < js.buttons)
114                                 SendMessage32A(CaptureWnd[joy], MM_JOY1BUTTONDOWN + joy, ButtonChanged, MAKELONG(js.x, js.y));
115                                 else
116                                 if (joyCapData[joy].wButtons > js.buttons)
117                                 SendMessage32A(CaptureWnd[joy], MM_JOY1BUTTONUP
118 + joy, ButtonChanged, MAKELONG(js.x, js.y));
119                                 joyCapData[joy].wButtons = js.buttons;
120                         }
121                 }
122         }
123 }
124
125
126 /**************************************************************************
127  *                              JoyGetNumDevs           [MMSYSTEM.101]
128  */
129 UINT32 WINAPI joyGetNumDevs32(void)
130 {
131         return joyGetNumDevs16();
132 }
133
134 /**************************************************************************
135  *                              JoyGetNumDevs           [MMSYSTEM.101]
136  */
137 UINT16 WINAPI joyGetNumDevs16(void)
138 {
139     int joy;
140     UINT16 joy_cnt = 0;
141
142     for (joy=0; joy<MAXJOYDRIVERS; joy++)
143         if (joyOpenDriver(joy) == TRUE) {               
144                 joyCloseDriver(joy);
145                 joy_cnt++;
146     }
147     TRACE(mmsys, "returning %d\n", joy_cnt);
148     if (!joy_cnt) ERR(mmsys, "No joystick found - "
149                           "perhaps get joystick-0.8.0.tar.gz and load"
150                           "it as module or use Linux >= 2.1.45 to be "
151                           "able to use joysticks.\n");
152     return joy_cnt;
153 }
154
155 /**************************************************************************
156  *                              JoyGetDevCaps           [WINMM.27]
157  */
158 MMRESULT32 WINAPI joyGetDevCaps32A(UINT32 wID, LPJOYCAPS32A lpCaps,UINT32 wSize)
159 {
160         JOYCAPS16       jc16;
161         MMRESULT16      ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
162
163         lpCaps->wMid = jc16.wMid;
164         lpCaps->wPid = jc16.wPid;
165         lstrcpy32A(lpCaps->szPname,jc16.szPname);
166         lpCaps->wXmin = jc16.wXmin;
167         lpCaps->wXmax = jc16.wXmax;
168         lpCaps->wYmin = jc16.wYmin;
169         lpCaps->wYmax = jc16.wYmax;
170         lpCaps->wZmin = jc16.wZmin;
171         lpCaps->wZmax = jc16.wZmax;
172         lpCaps->wNumButtons = jc16.wNumButtons;
173         lpCaps->wPeriodMin = jc16.wPeriodMin;
174         lpCaps->wPeriodMax = jc16.wPeriodMax;
175
176         lpCaps->wRmin = jc16.wRmin;
177         lpCaps->wRmax = jc16.wRmax;
178         lpCaps->wUmin = jc16.wUmin;
179         lpCaps->wUmax = jc16.wUmax;
180         lpCaps->wVmin = jc16.wVmin;
181         lpCaps->wVmax = jc16.wVmax;
182         lpCaps->wCaps = jc16.wCaps;
183         lpCaps->wMaxAxes = jc16.wMaxAxes;
184         lpCaps->wNumAxes = jc16.wNumAxes;
185         lpCaps->wMaxButtons = jc16.wMaxButtons;
186         lstrcpy32A(lpCaps->szRegKey,jc16.szRegKey);
187         lstrcpy32A(lpCaps->szOEMVxD,jc16.szOEMVxD);
188         return ret;
189 }
190
191 /**************************************************************************
192  *                              JoyGetDevCaps           [WINMM.28]
193  */
194 MMRESULT32 WINAPI joyGetDevCaps32W(UINT32 wID, LPJOYCAPS32W lpCaps,UINT32 wSize)
195 {
196         JOYCAPS16       jc16;
197         MMRESULT16      ret = joyGetDevCaps16(wID,&jc16,sizeof(jc16));
198
199         lpCaps->wMid = jc16.wMid;
200         lpCaps->wPid = jc16.wPid;
201         lstrcpyAtoW(lpCaps->szPname,jc16.szPname);
202         lpCaps->wXmin = jc16.wXmin;
203         lpCaps->wXmax = jc16.wXmax;
204         lpCaps->wYmin = jc16.wYmin;
205         lpCaps->wYmax = jc16.wYmax;
206         lpCaps->wZmin = jc16.wZmin;
207         lpCaps->wZmax = jc16.wZmax;
208         lpCaps->wNumButtons = jc16.wNumButtons;
209         lpCaps->wPeriodMin = jc16.wPeriodMin;
210         lpCaps->wPeriodMax = jc16.wPeriodMax;
211
212         lpCaps->wRmin = jc16.wRmin;
213         lpCaps->wRmax = jc16.wRmax;
214         lpCaps->wUmin = jc16.wUmin;
215         lpCaps->wUmax = jc16.wUmax;
216         lpCaps->wVmin = jc16.wVmin;
217         lpCaps->wVmax = jc16.wVmax;
218         lpCaps->wCaps = jc16.wCaps;
219         lpCaps->wMaxAxes = jc16.wMaxAxes;
220         lpCaps->wNumAxes = jc16.wNumAxes;
221         lpCaps->wMaxButtons = jc16.wMaxButtons;
222         lstrcpyAtoW(lpCaps->szRegKey,jc16.szRegKey);
223         lstrcpyAtoW(lpCaps->szOEMVxD,jc16.szOEMVxD);
224         return ret;
225 }
226 /**************************************************************************
227  *                              JoyGetDevCaps           [MMSYSTEM.102]
228  */
229 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
230 {
231     TRACE(mmsys, "(%04X, %p, %d);\n",
232             wID, lpCaps, wSize);
233     if (joyOpenDriver(wID) == TRUE) {
234         lpCaps->wMid = MM_MICROSOFT;
235         lpCaps->wPid = MM_PC_JOYSTICK;
236         strcpy(lpCaps->szPname, "WineJoy"); /* joystick product name */
237         lpCaps->wXmin = 0; /* FIXME */
238         lpCaps->wXmax = 0xffff;
239         lpCaps->wYmin = 0;
240         lpCaps->wYmax = 0xffff;
241         lpCaps->wZmin = 0;
242         lpCaps->wZmax = 0xffff;
243         lpCaps->wNumButtons = 2;
244         lpCaps->wPeriodMin = 0;
245         lpCaps->wPeriodMax = 50; /* FIXME end */
246         if (wSize == sizeof(JOYCAPS16)) {
247                 /* complete 95 structure */
248                 lpCaps->wRmin = 0;
249                 lpCaps->wRmax = 0xffff;
250                 lpCaps->wUmin = 0;
251                 lpCaps->wUmax = 0xffff;
252                 lpCaps->wVmin = 0;
253                 lpCaps->wVmax = 0xffff;
254                 lpCaps->wCaps = 0;
255                 lpCaps->wMaxAxes = 6;
256                 lpCaps->wNumAxes = 2;
257                 lpCaps->wMaxButtons = 3;
258                 strcpy(lpCaps->szRegKey,"");
259                 strcpy(lpCaps->szOEMVxD,"");
260         }
261         joyCloseDriver(wID);
262         return JOYERR_NOERROR;
263     }
264     else
265     return MMSYSERR_NODRIVER;
266 }
267
268 /**************************************************************************
269  *                              JoyGetPosEx             [WINMM.31]
270  */
271 MMRESULT32 WINAPI joyGetPosEx(UINT32 wID, LPJOYINFOEX lpInfo)
272 {
273         /* FIXME: implement it */
274         return MMSYSERR_NODRIVER;
275 }
276
277 /**************************************************************************
278  *                              JoyGetPos               [WINMM.30]
279  */
280 MMRESULT32 WINAPI joyGetPos32(UINT32 wID, LPJOYINFO32 lpInfo)
281 {
282         JOYINFO16       ji;
283         MMRESULT16      ret = joyGetPos16(wID,&ji);
284
285         lpInfo->wXpos = ji.wXpos;
286         lpInfo->wYpos = ji.wYpos;
287         lpInfo->wZpos = ji.wZpos;
288         lpInfo->wButtons = ji.wButtons;
289         return ret;
290 }
291
292 /**************************************************************************
293  *                              JoyGetPos               [MMSYSTEM.103]
294  */
295 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
296 {
297         struct js_status js;
298
299         TRACE(mmsys, "(%04X, %p)\n", wID, lpInfo);
300         if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
301         dev_stat = read(joy_dev[wID], &js, sizeof(js));
302         if (dev_stat != sizeof(js)) {
303                 joyCloseDriver(wID);
304                 return JOYERR_UNPLUGGED; /* FIXME: perhaps wrong, but what should I return else ? */
305         }
306         count_use[wID] = 0;
307         js.x = js.x*37;
308         js.y = js.y*37;
309         lpInfo->wXpos = js.x;   /* FIXME: perhaps multiply it somehow ? */
310         lpInfo->wYpos = js.y;
311         lpInfo->wZpos = 0; /* FIXME: Don't know what to do with this value as joystick driver doesn't provide a Z value */
312         lpInfo->wButtons = js.buttons;
313         TRACE(mmsys, "x: %d, y: %d, buttons: %d\n", js.x, js.y, js.buttons);
314         return JOYERR_NOERROR;
315 }
316
317 /**************************************************************************
318  *                              JoyGetThreshold         [WINMM.32]
319  */
320 MMRESULT32 WINAPI joyGetThreshold32(UINT32 wID, LPUINT32 lpThreshold)
321 {
322         UINT16          thresh;
323         MMRESULT16      ret = joyGetThreshold16(wID,&thresh);
324
325         *lpThreshold = thresh;
326         return ret;
327 }
328
329 /**************************************************************************
330  *                              JoyGetThreshold         [MMSYSTEM.104]
331  */
332 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
333 {
334     TRACE(mmsys, "(%04X, %p);\n", wID, lpThreshold);
335     if (wID >= MAXJOYDRIVERS) return JOYERR_PARMS;
336     *lpThreshold = joy_threshold[wID];
337     return JOYERR_NOERROR;
338 }
339
340 /**************************************************************************
341  *                              JoyReleaseCapture       [WINMM.33]
342  */
343 MMRESULT32 WINAPI joyReleaseCapture32(UINT32 wID)
344 {
345         return joyReleaseCapture16(wID);
346 }
347
348 /**************************************************************************
349  *                              JoyReleaseCapture       [MMSYSTEM.105]
350  */
351 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
352 {
353     TRACE(mmsys, "(%04X);\n", wID);
354     joyCaptured = FALSE;
355     joyCloseDriver(wID);
356     joy_dev[wID] = -1;
357     CaptureWnd[wID] = 0;
358     return JOYERR_NOERROR;
359 }
360
361 /**************************************************************************
362  *                              JoySetCapture           [MMSYSTEM.106]
363  */
364 MMRESULT32 WINAPI joySetCapture32(HWND32 hWnd,UINT32 wID,UINT32 wPeriod,BOOL32 bChanged)
365 {
366         return joySetCapture16(hWnd,wID,wPeriod,bChanged);
367 }
368
369 /**************************************************************************
370  *                              JoySetCapture           [MMSYSTEM.106]
371  */
372 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd,UINT16 wID,UINT16 wPeriod,BOOL16 bChanged)
373 {
374
375     TRACE(mmsys, "(%04X, %04X, %d, %d);\n",
376             hWnd, wID, wPeriod, bChanged);
377
378     if (!CaptureWnd[wID]) {
379         if (joyOpenDriver(wID) == FALSE) return MMSYSERR_NODRIVER;
380         joyCaptured = TRUE;
381         CaptureWnd[wID] = hWnd;
382         return JOYERR_NOERROR;
383     }
384     else
385     return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
386 }
387
388 /**************************************************************************
389  *                              JoySetThreshold         [WINMM.35]
390  */
391 MMRESULT32 WINAPI joySetThreshold32(UINT32 wID, UINT32 wThreshold)
392 {
393         return joySetThreshold16(wID,wThreshold);
394 }
395 /**************************************************************************
396  *                              JoySetThreshold         [MMSYSTEM.107]
397  */
398 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
399 {
400     TRACE(mmsys, "(%04X, %d);\n", wID, wThreshold);
401
402     if (wID > 3) return JOYERR_PARMS;
403     joy_threshold[wID] = wThreshold;
404     return JOYERR_NOERROR;
405 }
406
407 /**************************************************************************
408  *                              JoySetCalibration       [MMSYSTEM.109]
409  */
410 MMRESULT16 WINAPI joySetCalibration16(UINT16 wID)
411 {
412     FIXME(mmsys, "(%04X): stub.\n", wID);
413     return JOYERR_NOCANDO;
414 }