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