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