Keep track of per-column information inside the listview.
[wine] / dlls / winmm / joystick.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2 /*
3  * joystick functions
4  *
5  * Copyright 1997 Andreas Mohr
6  *           2000 Wolfgang Schwotzer
7  *                Eric Pouech
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25
26 #ifdef HAVE_UNISTD_H
27 # include <unistd.h>
28 #endif
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #ifdef HAVE_SYS_IOCTL_H
34 #include <sys/ioctl.h>
35 #endif
36
37 #include "mmsystem.h"
38 #include "winbase.h"
39 #include "winnls.h"
40 #include "wingdi.h"
41 #include "winuser.h"
42
43 #include "wine/mmsystem16.h"
44 #include "winemm.h"
45
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(mmsys);
49
50 #define MAXJOYSTICK     (JOYSTICKID2 + 1)
51 #define JOY_PERIOD_MIN  (10)    /* min Capture time period */
52 #define JOY_PERIOD_MAX  (1000)  /* max Capture time period */
53
54 typedef struct tagWINE_JOYSTICK {
55     JOYINFO     ji;
56     HWND        hCapture;
57     UINT        wTimer;
58     DWORD       threshold;
59     BOOL        bChanged;
60     HDRVR       hDriver;
61 } WINE_JOYSTICK;
62
63 static  WINE_JOYSTICK   JOY_Sticks[MAXJOYSTICK];
64
65 /**************************************************************************
66  *                              JOY_LoadDriver          [internal]
67  */
68 static  BOOL JOY_LoadDriver(DWORD dwJoyID)
69 {
70     if (dwJoyID >= MAXJOYSTICK)
71         return FALSE;
72     if (JOY_Sticks[dwJoyID].hDriver)
73         return TRUE;
74
75     JOY_Sticks[dwJoyID].hDriver = OpenDriverA("joystick.drv", 0, dwJoyID);
76     return (JOY_Sticks[dwJoyID].hDriver != 0);
77 }
78
79 /**************************************************************************
80  *                              JOY_Timer               [internal]
81  */
82 static  void    CALLBACK        JOY_Timer(HWND hWnd, UINT wMsg, UINT wTimer, DWORD dwTime)
83 {
84     int                 i;
85     WINE_JOYSTICK*      joy;
86     JOYINFO             ji;
87     LONG                pos;
88     unsigned            buttonChange;
89
90     for (i = 0; i < MAXJOYSTICK; i++) {
91         joy = &JOY_Sticks[i];
92
93         if (joy->hCapture != hWnd) continue;
94
95         joyGetPos(i, &ji);
96         pos = MAKELONG(ji.wXpos, ji.wYpos);
97
98         if (!joy->bChanged ||
99             abs(joy->ji.wXpos - ji.wXpos) > joy->threshold ||
100             abs(joy->ji.wYpos - ji.wYpos) > joy->threshold) {
101             SendMessageA(joy->hCapture, MM_JOY1MOVE + i, ji.wButtons, pos);
102             joy->ji.wXpos = ji.wXpos;
103             joy->ji.wYpos = ji.wYpos;
104         }
105         if (!joy->bChanged ||
106             abs(joy->ji.wZpos - ji.wZpos) > joy->threshold) {
107             SendMessageA(joy->hCapture, MM_JOY1ZMOVE + i, ji.wButtons, pos);
108             joy->ji.wZpos = ji.wZpos;
109         }
110         if ((buttonChange = joy->ji.wButtons ^ ji.wButtons) != 0) {
111             if (ji.wButtons & buttonChange)
112                 SendMessageA(joy->hCapture, MM_JOY1BUTTONDOWN + i,
113                              (buttonChange << 8) | (ji.wButtons & buttonChange), pos);
114             if (joy->ji.wButtons & buttonChange)
115                 SendMessageA(joy->hCapture, MM_JOY1BUTTONUP + i,
116                              (buttonChange << 8) | (joy->ji.wButtons & buttonChange), pos);
117             joy->ji.wButtons = ji.wButtons;
118         }
119     }
120 }
121
122 /**************************************************************************
123  *                              joyGetNumDevs           [WINMM.@]
124  */
125 UINT WINAPI joyGetNumDevs(void)
126 {
127     UINT        ret = 0;
128     int         i;
129
130     for (i = 0; i < MAXJOYSTICK; i++) {
131         if (JOY_LoadDriver(i)) {
132             ret += SendDriverMessage(JOY_Sticks[i].hDriver, JDD_GETNUMDEVS, 0L, 0L);
133         }
134     }
135     return ret;
136 }
137
138 /**************************************************************************
139  *                              joyGetNumDevs           [MMSYSTEM.101]
140  */
141 UINT16 WINAPI joyGetNumDevs16(void)
142 {
143     return joyGetNumDevs();
144 }
145
146 /**************************************************************************
147  *                              joyGetDevCapsA          [WINMM.@]
148  */
149 MMRESULT WINAPI joyGetDevCapsA(UINT wID, LPJOYCAPSA lpCaps, UINT wSize)
150 {
151     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
152     if (!JOY_LoadDriver(wID))   return MMSYSERR_NODRIVER;
153
154     lpCaps->wPeriodMin = JOY_PERIOD_MIN; /* FIXME */
155     lpCaps->wPeriodMax = JOY_PERIOD_MAX; /* FIXME (same as MS Joystick Driver) */
156
157     return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETDEVCAPS, (DWORD)lpCaps, wSize);
158 }
159
160 /**************************************************************************
161  *                              joyGetDevCapsW          [WINMM.@]
162  */
163 MMRESULT WINAPI joyGetDevCapsW(UINT wID, LPJOYCAPSW lpCaps, UINT wSize)
164 {
165     JOYCAPSA    jca;
166     MMRESULT    ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
167
168     if (ret != JOYERR_NOERROR) return ret;
169     lpCaps->wMid = jca.wMid;
170     lpCaps->wPid = jca.wPid;
171     MultiByteToWideChar( CP_ACP, 0, jca.szPname, -1, lpCaps->szPname,
172                          sizeof(lpCaps->szPname)/sizeof(WCHAR) );
173     lpCaps->wXmin = jca.wXmin;
174     lpCaps->wXmax = jca.wXmax;
175     lpCaps->wYmin = jca.wYmin;
176     lpCaps->wYmax = jca.wYmax;
177     lpCaps->wZmin = jca.wZmin;
178     lpCaps->wZmax = jca.wZmax;
179     lpCaps->wNumButtons = jca.wNumButtons;
180     lpCaps->wPeriodMin = jca.wPeriodMin;
181     lpCaps->wPeriodMax = jca.wPeriodMax;
182
183     if (wSize >= sizeof(JOYCAPSW)) { /* Win95 extensions ? */
184         lpCaps->wRmin = jca.wRmin;
185         lpCaps->wRmax = jca.wRmax;
186         lpCaps->wUmin = jca.wUmin;
187         lpCaps->wUmax = jca.wUmax;
188         lpCaps->wVmin = jca.wVmin;
189         lpCaps->wVmax = jca.wVmax;
190         lpCaps->wCaps = jca.wCaps;
191         lpCaps->wMaxAxes = jca.wMaxAxes;
192         lpCaps->wNumAxes = jca.wNumAxes;
193         lpCaps->wMaxButtons = jca.wMaxButtons;
194         MultiByteToWideChar( CP_ACP, 0, jca.szRegKey, -1, lpCaps->szRegKey,
195                          sizeof(lpCaps->szRegKey)/sizeof(WCHAR) );
196         MultiByteToWideChar( CP_ACP, 0, jca.szOEMVxD, -1, lpCaps->szOEMVxD,
197                          sizeof(lpCaps->szOEMVxD)/sizeof(WCHAR) );
198     }
199
200     return ret;
201 }
202
203 /**************************************************************************
204  *                              joyGetDevCaps           [MMSYSTEM.102]
205  */
206 MMRESULT16 WINAPI joyGetDevCaps16(UINT16 wID, LPJOYCAPS16 lpCaps, UINT16 wSize)
207 {
208     JOYCAPSA    jca;
209     MMRESULT    ret = joyGetDevCapsA(wID, &jca, sizeof(jca));
210
211     if (ret != JOYERR_NOERROR) return ret;
212     lpCaps->wMid = jca.wMid;
213     lpCaps->wPid = jca.wPid;
214     strcpy(lpCaps->szPname, jca.szPname);
215     lpCaps->wXmin = jca.wXmin;
216     lpCaps->wXmax = jca.wXmax;
217     lpCaps->wYmin = jca.wYmin;
218     lpCaps->wYmax = jca.wYmax;
219     lpCaps->wZmin = jca.wZmin;
220     lpCaps->wZmax = jca.wZmax;
221     lpCaps->wNumButtons = jca.wNumButtons;
222     lpCaps->wPeriodMin = jca.wPeriodMin;
223     lpCaps->wPeriodMax = jca.wPeriodMax;
224
225     if (wSize >= sizeof(JOYCAPS16)) { /* Win95 extensions ? */
226         lpCaps->wRmin = jca.wRmin;
227         lpCaps->wRmax = jca.wRmax;
228         lpCaps->wUmin = jca.wUmin;
229         lpCaps->wUmax = jca.wUmax;
230         lpCaps->wVmin = jca.wVmin;
231         lpCaps->wVmax = jca.wVmax;
232         lpCaps->wCaps = jca.wCaps;
233         lpCaps->wMaxAxes = jca.wMaxAxes;
234         lpCaps->wNumAxes = jca.wNumAxes;
235         lpCaps->wMaxButtons = jca.wMaxButtons;
236         strcpy(lpCaps->szRegKey, jca.szRegKey);
237         strcpy(lpCaps->szOEMVxD, jca.szOEMVxD);
238     }
239
240     return ret;
241 }
242
243 /**************************************************************************
244  *                              joyGetPosEx             [WINMM.@]
245  */
246 MMRESULT WINAPI joyGetPosEx(UINT wID, LPJOYINFOEX lpInfo)
247 {
248     TRACE("(%d, %p);\n", wID, lpInfo);
249
250     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
251     if (!JOY_LoadDriver(wID))   return MMSYSERR_NODRIVER;
252
253     lpInfo->dwXpos = 0;
254     lpInfo->dwYpos = 0;
255     lpInfo->dwZpos = 0;
256     lpInfo->dwRpos = 0;
257     lpInfo->dwUpos = 0;
258     lpInfo->dwVpos = 0;
259     lpInfo->dwButtons = 0;
260     lpInfo->dwButtonNumber = 0;
261     lpInfo->dwPOV = 0;
262     lpInfo->dwReserved1 = 0;
263     lpInfo->dwReserved2 = 0;
264
265     return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOSEX, (DWORD)lpInfo, 0L);
266 }
267
268 /**************************************************************************
269  *                              joyGetPosEx           [MMSYSTEM.110]
270  */
271 MMRESULT16 WINAPI joyGetPosEx16(UINT16 wID, LPJOYINFOEX lpInfo)
272 {
273     return joyGetPosEx(wID, lpInfo);
274 }
275
276 /**************************************************************************
277  *                              joyGetPos               [WINMM.@]
278  */
279 MMRESULT WINAPI joyGetPos(UINT wID, LPJOYINFO lpInfo)
280 {
281     TRACE("(%d, %p);\n", wID, lpInfo);
282
283     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
284     if (!JOY_LoadDriver(wID))   return MMSYSERR_NODRIVER;
285
286     lpInfo->wXpos = 0;
287     lpInfo->wYpos = 0;
288     lpInfo->wZpos = 0;
289     lpInfo->wButtons = 0;
290
291     return SendDriverMessage(JOY_Sticks[wID].hDriver, JDD_GETPOS, (DWORD)lpInfo, 0L);
292 }
293
294 /**************************************************************************
295  *                              joyGetPos               [MMSYSTEM.103]
296  */
297 MMRESULT16 WINAPI joyGetPos16(UINT16 wID, LPJOYINFO16 lpInfo)
298 {
299     JOYINFO     ji;
300     MMRESULT    ret;
301
302     TRACE("(%d, %p);\n", wID, lpInfo);
303
304     if ((ret = joyGetPos(wID, &ji)) == JOYERR_NOERROR) {
305         lpInfo->wXpos = ji.wXpos;
306         lpInfo->wYpos = ji.wYpos;
307         lpInfo->wZpos = ji.wZpos;
308         lpInfo->wButtons = ji.wButtons;
309     }
310     return ret;
311 }
312
313 /**************************************************************************
314  *                              joyGetThreshold         [WINMM.@]
315  */
316 MMRESULT WINAPI joyGetThreshold(UINT wID, LPUINT lpThreshold)
317 {
318     TRACE("(%04X, %p);\n", wID, lpThreshold);
319
320     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
321
322     *lpThreshold = JOY_Sticks[wID].threshold;
323     return JOYERR_NOERROR;
324 }
325
326 /**************************************************************************
327  *                              joyGetThreshold         [MMSYSTEM.104]
328  */
329 MMRESULT16 WINAPI joyGetThreshold16(UINT16 wID, LPUINT16 lpThreshold)
330 {
331     TRACE("(%04X, %p);\n", wID, lpThreshold);
332
333     if (wID >= MAXJOYSTICK)     return JOYERR_PARMS;
334
335     *lpThreshold = JOY_Sticks[wID].threshold;
336     return JOYERR_NOERROR;
337 }
338
339 /**************************************************************************
340  *                              joyReleaseCapture       [WINMM.@]
341  */
342 MMRESULT WINAPI joyReleaseCapture(UINT wID)
343 {
344     TRACE("(%04X);\n", wID);
345
346     if (wID >= MAXJOYSTICK)             return JOYERR_PARMS;
347     if (!JOY_LoadDriver(wID))           return MMSYSERR_NODRIVER;
348     if (!JOY_Sticks[wID].hCapture)      return JOYERR_NOCANDO;
349
350     KillTimer(JOY_Sticks[wID].hCapture, JOY_Sticks[wID].wTimer);
351     JOY_Sticks[wID].hCapture = 0;
352     JOY_Sticks[wID].wTimer = 0;
353
354     return JOYERR_NOERROR;
355 }
356
357 /**************************************************************************
358  *                              joyReleaseCapture       [MMSYSTEM.105]
359  */
360 MMRESULT16 WINAPI joyReleaseCapture16(UINT16 wID)
361 {
362     return joyReleaseCapture(wID);
363 }
364
365 /**************************************************************************
366  *                              joySetCapture           [WINMM.@]
367  */
368 MMRESULT WINAPI joySetCapture(HWND hWnd, UINT wID, UINT wPeriod, BOOL bChanged)
369 {
370     TRACE("(%04X, %04X, %d, %d);\n",  hWnd, wID, wPeriod, bChanged);
371
372     if (wID >= MAXJOYSTICK || hWnd == 0) return JOYERR_PARMS;
373     if (wPeriod<JOY_PERIOD_MIN || wPeriod>JOY_PERIOD_MAX) return JOYERR_PARMS;
374     if (!JOY_LoadDriver(wID)) return MMSYSERR_NODRIVER;
375
376     if (JOY_Sticks[wID].hCapture || !IsWindow(hWnd))
377         return JOYERR_NOCANDO; /* FIXME: what should be returned ? */
378
379     if (joyGetPos(wID, &JOY_Sticks[wID].ji) != JOYERR_NOERROR)
380         return JOYERR_UNPLUGGED;
381
382     if ((JOY_Sticks[wID].wTimer = SetTimer(hWnd, 0, wPeriod, JOY_Timer)) == 0)
383         return JOYERR_NOCANDO;
384
385     JOY_Sticks[wID].hCapture = hWnd;
386     JOY_Sticks[wID].bChanged = bChanged;
387
388     return JOYERR_NOERROR;
389 }
390
391 /**************************************************************************
392  *                              joySetCapture           [MMSYSTEM.106]
393  */
394 MMRESULT16 WINAPI joySetCapture16(HWND16 hWnd, UINT16 wID, UINT16 wPeriod, BOOL16 bChanged)
395 {
396     return joySetCapture16(hWnd, wID, wPeriod, bChanged);
397 }
398
399 /**************************************************************************
400  *                              joySetThreshold         [WINMM.@]
401  */
402 MMRESULT WINAPI joySetThreshold(UINT wID, UINT wThreshold)
403 {
404     TRACE("(%04X, %d);\n", wID, wThreshold);
405
406     if (wID >= MAXJOYSTICK) return MMSYSERR_INVALPARAM;
407
408     JOY_Sticks[wID].threshold = wThreshold;
409
410     return JOYERR_NOERROR;
411 }
412
413 /**************************************************************************
414  *                              joySetThreshold         [MMSYSTEM.107]
415  */
416 MMRESULT16 WINAPI joySetThreshold16(UINT16 wID, UINT16 wThreshold)
417 {
418     return joySetThreshold16(wID,wThreshold);
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 }