opengl32: Avoid generating a wrapper for internal functions when we can call the...
[wine] / dlls / dinput / joystick_linux.c
1 /*              DirectInput Joystick device
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /*
23  * To Do:
24  *      dead zone
25  *      force feedback
26  */
27
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #include <errno.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 #endif
42 #include <sys/fcntl.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
45 #endif
46 #include <errno.h>
47 #ifdef HAVE_SYS_ERRNO_H
48 # include <sys/errno.h>
49 #endif
50 #ifdef HAVE_LINUX_IOCTL_H
51 # include <linux/ioctl.h>
52 #endif
53 #ifdef HAVE_LINUX_JOYSTICK_H
54 # include <linux/joystick.h>
55 #endif
56 #ifdef HAVE_SYS_POLL_H
57 # include <sys/poll.h>
58 #endif
59
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
62 #include "windef.h"
63 #include "winbase.h"
64 #include "winerror.h"
65 #include "winreg.h"
66 #include "dinput.h"
67
68 #include "dinput_private.h"
69 #include "device_private.h"
70
71 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
72
73 #ifdef HAVE_LINUX_22_JOYSTICK_API
74
75 #define JOYDEV_NEW "/dev/input/js"
76 #define JOYDEV_OLD "/dev/js"
77
78 typedef struct {
79     LONG lMin;
80     LONG lMax;
81     LONG lDeadZone;
82     LONG lSaturation;
83 } ObjProps;
84
85 typedef struct {
86     LONG lX;
87     LONG lY;
88 } POV;
89
90 typedef struct JoystickImpl JoystickImpl;
91 static const IDirectInputDevice8AVtbl JoystickAvt;
92 static const IDirectInputDevice8WVtbl JoystickWvt;
93 struct JoystickImpl
94 {
95         struct IDirectInputDevice2AImpl base;
96
97         char                            dev[32];
98
99         /* The 'parent' DInput */
100         IDirectInputImpl               *dinput;
101
102         /* joystick private */
103         int                             joyfd;
104         DIJOYSTATE2                     js;             /* wine data */
105         LPDIDATAFORMAT                  user_df;        /* user defined format */
106         DataFormat                      *transform;     /* wine to user format converter */
107         int                             *offsets;       /* object offsets */
108         ObjProps                        *props;
109         char                            *name;
110         DIDEVCAPS                       devcaps;
111         LONG                            deadzone;
112         int                             *axis_map;
113         int                             axes;
114         int                             buttons;
115         POV                             povs[4];
116 };
117
118 static GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
119   0x9e573ed9,
120   0x7734,
121   0x11d2,
122   {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
123 };
124
125 static void _dump_DIDEVCAPS(LPDIDEVCAPS lpDIDevCaps)
126 {
127     TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
128     TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
129     TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
130           lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
131           lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
132           lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
133           lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
134           lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
135           lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
136     TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
137     TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
138     TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
139     if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
140         TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
141         TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
142         TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
143         TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
144         TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
145     }
146 }
147
148 static int joydev_get_device(char *dev, int id)
149 {
150     int ret;
151     sprintf(dev, "%s%d", JOYDEV_NEW, id);
152     if ((ret = open(dev, O_RDONLY)) < 0) {
153         sprintf(dev, "%s%d", JOYDEV_OLD, id);
154         if ((ret = open(dev, O_RDONLY)) < 0) {
155             return -1;
156         }
157     }
158     return ret;
159 }
160
161 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
162 {
163     int fd = -1;
164     char dev[32];
165
166     if (dwFlags & DIEDFL_FORCEFEEDBACK) {
167         WARN("force feedback not supported\n");
168         return FALSE;
169     }
170
171     if ((dwDevType == 0) ||
172         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
173         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
174         /* check whether we have a joystick */
175         if ((fd = joydev_get_device(dev, id)) < 0) {
176             WARN("open(%s,O_RDONLY) failed: %s\n", dev, strerror(errno));
177             return FALSE;
178         }
179
180         /* Return joystick */
181         lpddi->guidInstance = DInput_Wine_Joystick_GUID;
182         lpddi->guidInstance.Data3 = id;
183         lpddi->guidProduct = DInput_Wine_Joystick_GUID;
184         /* we only support traditional joysticks for now */
185         if (version >= 0x0800)
186             lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
187         else
188             lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
189         sprintf(lpddi->tszInstanceName, "Joystick %d", id);
190 #if defined(JSIOCGNAME)
191         if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
192             WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", dev, strerror(errno));
193             strcpy(lpddi->tszProductName, "Wine Joystick");
194         }
195 #else
196         strcpy(lpddi->tszProductName, "Wine Joystick");
197 #endif
198
199         lpddi->guidFFDriver = GUID_NULL;
200         close(fd);
201         TRACE("Enumerating the linux Joystick device: %s (%s)\n", dev, lpddi->tszProductName);
202         return TRUE;
203     }
204
205     return FALSE;
206 }
207
208 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
209 {
210     int fd = -1;
211     char name[MAX_PATH];
212     char dev[32];
213     char friendly[32];
214
215     if (dwFlags & DIEDFL_FORCEFEEDBACK) {
216         WARN("force feedback not supported\n");
217         return FALSE;
218     }
219
220     if ((dwDevType == 0) ||
221         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
222         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
223         /* check whether we have a joystick */
224         if ((fd = joydev_get_device(dev, id)) < 0) {
225             WARN("open(%s,O_RDONLY) failed: %s\n", dev, strerror(errno));
226             return FALSE;
227         }
228
229         /* Return joystick */
230         lpddi->guidInstance = DInput_Wine_Joystick_GUID;
231         lpddi->guidInstance.Data3 = id;
232         lpddi->guidProduct = DInput_Wine_Joystick_GUID;
233         /* we only support traditional joysticks for now */
234         if (version >= 0x0800)
235             lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
236         else
237             lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
238         sprintf(friendly, "Joystick %d", id);
239         MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
240 #if defined(JSIOCGNAME)
241         if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
242             WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", dev, strerror(errno));
243             strcpy(name, "Wine Joystick");
244         }
245 #else
246         strcpy(name, "Wine Joystick");
247 #endif
248         MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
249         lpddi->guidFFDriver = GUID_NULL;
250         close(fd);
251         TRACE("Enumerating the linux Joystick device: %s (%s)\n",dev,name);
252         return TRUE;
253     }
254
255     return FALSE;
256 }
257
258 /*
259  * Get a config key from either the app-specific or the default config
260  */
261
262 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
263                                     char *buffer, DWORD size )
264 {
265     if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size ))
266         return 0;
267
268     if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size ))
269         return 0;
270
271     return ERROR_FILE_NOT_FOUND;
272 }
273
274 /*
275  * Setup the dinput options.
276  */
277
278 static HRESULT setup_dinput_options(JoystickImpl * device)
279 {
280     char buffer[MAX_PATH+16];
281     HKEY hkey, appkey = 0;
282     DWORD len;
283
284     buffer[MAX_PATH]='\0';
285
286     /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
287     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", &hkey)) hkey = 0;
288
289     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
290     if (len && len < MAX_PATH) {
291         HKEY tmpkey;
292         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
293         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
294         {
295             char *p, *appname = buffer;
296             if ((p = strrchr( appname, '/' ))) appname = p + 1;
297             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
298             strcat( appname, "\\DirectInput" );
299             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
300             RegCloseKey( tmpkey );
301         }
302     }
303
304     /* get options */
305
306     if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
307         device->deadzone = atoi(buffer);
308         TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
309     }
310
311     if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
312         int tokens = 0;
313         int axis = 0;
314         int pov = 0;
315         const char *delim = ",";
316         char * ptr;
317         TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
318
319         device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
320         if (device->axis_map == 0)
321             return DIERR_OUTOFMEMORY;
322
323         if ((ptr = strtok(buffer, delim)) != NULL) {
324             do {
325                 if (strcmp(ptr, "X") == 0) {
326                     device->axis_map[tokens] = 0;
327                     axis++;
328                 } else if (strcmp(ptr, "Y") == 0) {
329                     device->axis_map[tokens] = 1;
330                     axis++;
331                 } else if (strcmp(ptr, "Z") == 0) {
332                     device->axis_map[tokens] = 2;
333                     axis++;
334                 } else if (strcmp(ptr, "Rx") == 0) {
335                     device->axis_map[tokens] = 3;
336                     axis++;
337                 } else if (strcmp(ptr, "Ry") == 0) {
338                     device->axis_map[tokens] = 4;
339                     axis++;
340                 } else if (strcmp(ptr, "Rz") == 0) {
341                     device->axis_map[tokens] = 5;
342                     axis++;
343                 } else if (strcmp(ptr, "Slider1") == 0) {
344                     device->axis_map[tokens] = 6;
345                     axis++;
346                 } else if (strcmp(ptr, "Slider2") == 0) {
347                     device->axis_map[tokens] = 7;
348                     axis++;
349                 } else if (strcmp(ptr, "POV1") == 0) {
350                     device->axis_map[tokens++] = 8;
351                     device->axis_map[tokens] = 8;
352                     pov++;
353                 } else if (strcmp(ptr, "POV2") == 0) {
354                     device->axis_map[tokens++] = 9;
355                     device->axis_map[tokens] = 9;
356                     pov++;
357                 } else if (strcmp(ptr, "POV3") == 0) {
358                     device->axis_map[tokens++] = 10;
359                     device->axis_map[tokens] = 10;
360                     pov++;
361                 } else if (strcmp(ptr, "POV4") == 0) {
362                     device->axis_map[tokens++] = 11;
363                     device->axis_map[tokens] = 11;
364                     pov++;
365                 } else {
366                     ERR("invalid joystick axis type: %s\n", ptr);
367                     device->axis_map[tokens] = tokens;
368                     axis++;
369                 }
370
371                 tokens++;
372             } while ((ptr = strtok(NULL, delim)) != NULL);
373
374             if (tokens != device->devcaps.dwAxes) {
375                 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
376                 while (tokens < device->axes) {
377                     device->axis_map[tokens] = tokens;
378                     tokens++;
379                 }
380             }
381         }
382
383         device->devcaps.dwAxes = axis;
384         device->devcaps.dwPOVs = pov;
385     }
386
387     if (appkey)
388         RegCloseKey( appkey );
389
390     if (hkey)
391         RegCloseKey( hkey );
392
393     return DI_OK;
394 }
395
396 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, LPDIRECTINPUTDEVICEA* pdev)
397 {
398     DWORD i;
399     JoystickImpl* newDevice;
400     char name[MAX_PATH];
401     HRESULT hr;
402
403     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
404     if (newDevice == 0) {
405         WARN("out of memory\n");
406         *pdev = 0;
407         return DIERR_OUTOFMEMORY;
408     }
409
410     if ((newDevice->joyfd = joydev_get_device(newDevice->dev, rguid->Data3)) < 0) {
411         WARN("open(%s,O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
412         HeapFree(GetProcessHeap(), 0, newDevice);
413         return DIERR_DEVICENOTREG;
414     }
415
416     /* get the device name */
417 #if defined(JSIOCGNAME)
418     if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
419         WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
420         strcpy(name, "Wine Joystick");
421     }
422 #else
423     strcpy(name, "Wine Joystick");
424 #endif
425
426     /* copy the device name */
427     newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
428     strcpy(newDevice->name, name);
429
430 #ifdef JSIOCGAXES
431     if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
432         WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
433         newDevice->axes = 2;
434     }
435 #endif
436 #ifdef JSIOCGBUTTONS
437     if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
438         WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
439         newDevice->buttons = 2;
440     }
441 #endif
442
443     newDevice->base.lpVtbl = jvt;
444     newDevice->base.ref = 1;
445     newDevice->dinput = dinput;
446     CopyMemory(&newDevice->base.guid, rguid, sizeof(*rguid));
447     InitializeCriticalSection(&newDevice->base.crit);
448     newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)"DINPUT_joystick";
449
450     /* setup_dinput_options may change these */
451     newDevice->deadzone = 5000;
452     newDevice->devcaps.dwButtons = newDevice->buttons;
453     newDevice->devcaps.dwAxes = newDevice->axes;
454     newDevice->devcaps.dwPOVs = 0;
455
456     /* do any user specified configuration */
457     hr = setup_dinput_options(newDevice);
458     if (hr != DI_OK)
459         goto FAILED1;
460
461     if (newDevice->axis_map == 0) {
462         newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
463         if (newDevice->axis_map == 0)
464             goto FAILED;
465
466         for (i = 0; i < newDevice->axes; i++)
467             newDevice->axis_map[i] = i;
468     }
469
470     /* wine uses DIJOYSTATE2 as it's internal format so copy
471      * the already defined format c_dfDIJoystick2 */
472     newDevice->user_df = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwSize);
473     if (newDevice->user_df == 0)
474         goto FAILED;
475
476     CopyMemory(newDevice->user_df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
477
478     /* copy default objects */
479     newDevice->user_df->rgodf = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*c_dfDIJoystick2.dwObjSize);
480     if (newDevice->user_df->rgodf == 0)
481         goto FAILED;
482
483     CopyMemory(newDevice->user_df->rgodf,c_dfDIJoystick2.rgodf,c_dfDIJoystick2.dwNumObjs*c_dfDIJoystick2.dwObjSize);
484
485     /* create default properties */
486     newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
487     if (newDevice->props == 0)
488         goto FAILED;
489
490     /* initialize default properties */
491     for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
492         newDevice->props[i].lMin = 0;
493         newDevice->props[i].lMax = 0xffff;
494         newDevice->props[i].lDeadZone = newDevice->deadzone;    /* % * 1000 */
495         newDevice->props[i].lSaturation = 0;
496     }
497
498     /* create an offsets array */
499     newDevice->offsets = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,c_dfDIJoystick2.dwNumObjs*sizeof(int));
500     if (newDevice->offsets == 0)
501         goto FAILED;
502
503     /* create the default transform filter */
504     newDevice->transform = create_DataFormat(&c_dfDIJoystick2, newDevice->user_df, newDevice->offsets);
505
506     IDirectInputDevice_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->dinput);
507
508     newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
509     newDevice->devcaps.dwFlags = DIDC_ATTACHED;
510     if (newDevice->dinput->dwVersion >= 0x0800)
511         newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
512     else
513         newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
514     newDevice->devcaps.dwFFSamplePeriod = 0;
515     newDevice->devcaps.dwFFMinTimeResolution = 0;
516     newDevice->devcaps.dwFirmwareRevision = 0;
517     newDevice->devcaps.dwHardwareRevision = 0;
518     newDevice->devcaps.dwFFDriverVersion = 0;
519
520     if (TRACE_ON(dinput)) {
521         _dump_DIDATAFORMAT(newDevice->user_df);
522        for (i = 0; i < (newDevice->axes); i++)
523            TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
524         _dump_DIDEVCAPS(&newDevice->devcaps);
525     }
526
527     *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
528
529     return DI_OK;
530
531 FAILED:
532     hr = DIERR_OUTOFMEMORY;
533 FAILED1:
534     HeapFree(GetProcessHeap(),0,newDevice->axis_map);
535     HeapFree(GetProcessHeap(),0,newDevice->name);
536     HeapFree(GetProcessHeap(),0,newDevice->props);
537     HeapFree(GetProcessHeap(),0,newDevice->user_df->rgodf);
538     HeapFree(GetProcessHeap(),0,newDevice->user_df);
539     HeapFree(GetProcessHeap(),0,newDevice);
540     *pdev = 0;
541
542     return hr;
543 }
544
545 static BOOL IsJoystickGUID(REFGUID guid)
546 {
547     GUID wine_joystick = DInput_Wine_Joystick_GUID;
548     GUID dev_guid = *guid;
549
550     wine_joystick.Data3 = 0;
551     dev_guid.Data3 = 0;
552
553     return IsEqualGUID(&wine_joystick, &dev_guid);
554 }
555
556 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
557 {
558   if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
559       (IsJoystickGUID(rguid))) {
560     if ((riid == NULL) ||
561         IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
562         IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
563         IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
564         IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
565       return alloc_device(rguid, &JoystickAvt, dinput, pdev);
566     } else {
567       WARN("no interface\n");
568       *pdev = 0;
569       return DIERR_NOINTERFACE;
570     }
571   }
572
573   WARN("invalid device GUID\n");
574   *pdev = 0;
575   return DIERR_DEVICENOTREG;
576 }
577
578 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
579 {
580   if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
581       (IsJoystickGUID(rguid))) {
582     if ((riid == NULL) ||
583         IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
584         IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
585         IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
586         IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
587       return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev);
588     } else {
589       WARN("no interface\n");
590       *pdev = 0;
591       return DIERR_NOINTERFACE;
592     }
593   }
594
595   WARN("invalid device GUID\n");
596   *pdev = 0;
597   return DIERR_DEVICENOTREG;
598 }
599
600 const struct dinput_device joystick_linux_device = {
601   "Wine Linux joystick driver",
602   joydev_enum_deviceA,
603   joydev_enum_deviceW,
604   joydev_create_deviceA,
605   joydev_create_deviceW
606 };
607
608 /******************************************************************************
609  *      Joystick
610  */
611 static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
612 {
613     JoystickImpl *This = (JoystickImpl *)iface;
614     ULONG ref;
615
616     ref = InterlockedDecrement(&This->base.ref);
617     if (ref)
618         return ref;
619
620     /* Free the device name */
621     HeapFree(GetProcessHeap(),0,This->name);
622
623     /* Free the axis map */
624     HeapFree(GetProcessHeap(),0,This->axis_map);
625
626     /* Free the data queue */
627     HeapFree(GetProcessHeap(), 0, This->base.data_queue);
628
629     /* Free the DataFormat */
630     HeapFree(GetProcessHeap(), 0, This->user_df->rgodf);
631     HeapFree(GetProcessHeap(), 0, This->user_df);
632
633     /* Free the properties */
634     HeapFree(GetProcessHeap(), 0, This->props);
635
636     /* Free the offsets array */
637     HeapFree(GetProcessHeap(),0,This->offsets);
638
639     /* release the data transform filter */
640     release_DataFormat(This->transform);
641
642     This->base.crit.DebugInfo->Spare[0] = 0;
643     DeleteCriticalSection(&This->base.crit);
644     IDirectInputDevice_Release((LPDIRECTINPUTDEVICE8A)This->dinput);
645
646     HeapFree(GetProcessHeap(),0,This);
647     return 0;
648 }
649
650 /******************************************************************************
651   *   SetDataFormat : the application can choose the format of the data
652   *   the device driver sends back with GetDeviceState.
653   */
654 static HRESULT WINAPI JoystickAImpl_SetDataFormat(
655     LPDIRECTINPUTDEVICE8A iface,
656     LPCDIDATAFORMAT df)
657 {
658     JoystickImpl *This = (JoystickImpl *)iface;
659     unsigned int i;
660     LPDIDATAFORMAT new_df = 0;
661     LPDIOBJECTDATAFORMAT new_rgodf = 0;
662     ObjProps * new_props = 0;
663
664     TRACE("(%p,%p)\n",This,df);
665
666     if (df == NULL) {
667         WARN("invalid pointer\n");
668         return E_POINTER;
669     }
670
671     if (df->dwSize != sizeof(*df)) {
672         WARN("invalid argument\n");
673         return DIERR_INVALIDPARAM;
674     }
675
676     if (This->base.acquired) {
677         WARN("acquired\n");
678         return DIERR_ACQUIRED;
679     }
680
681     if (TRACE_ON(dinput))
682         _dump_DIDATAFORMAT(df);
683
684     /* Store the new data format */
685     new_df = HeapAlloc(GetProcessHeap(),0,df->dwSize);
686     if (new_df == 0)
687         goto FAILED;
688
689     new_rgodf = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*df->dwObjSize);
690     if (new_rgodf == 0)
691         goto FAILED;
692
693     new_props = HeapAlloc(GetProcessHeap(),0,df->dwNumObjs*sizeof(ObjProps));
694     if (new_props == 0)
695         goto FAILED;
696
697     HeapFree(GetProcessHeap(),0,This->user_df);
698     HeapFree(GetProcessHeap(),0,This->user_df->rgodf);
699     HeapFree(GetProcessHeap(),0,This->props);
700     release_DataFormat(This->transform);
701
702     This->user_df = new_df;
703     CopyMemory(This->user_df, df, df->dwSize);
704     This->user_df->rgodf = new_rgodf;
705     CopyMemory(This->user_df->rgodf,df->rgodf,df->dwNumObjs*df->dwObjSize);
706     This->props = new_props;
707     for (i = 0; i < df->dwNumObjs; i++) {
708         This->props[i].lMin = 0;
709         This->props[i].lMax = 0xffff;
710         This->props[i].lDeadZone = 1000;
711         This->props[i].lSaturation = 0;
712     }
713     This->transform = create_DataFormat(&c_dfDIJoystick2, This->user_df, This->offsets);
714
715     return DI_OK;
716
717 FAILED:
718     WARN("out of memory\n");
719     HeapFree(GetProcessHeap(),0,new_props);
720     HeapFree(GetProcessHeap(),0,new_rgodf);
721     HeapFree(GetProcessHeap(),0,new_df);
722     return DIERR_OUTOFMEMORY;
723 }
724
725 /******************************************************************************
726   *     Acquire : gets exclusive control of the joystick
727   */
728 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
729 {
730     JoystickImpl *This = (JoystickImpl *)iface;
731
732     TRACE("(%p)\n",This);
733
734     if (This->base.acquired) {
735         WARN("already acquired\n");
736         return S_FALSE;
737     }
738
739     /* open the joystick device */
740     if (This->joyfd==-1) {
741         TRACE("opening joystick device %s\n", This->dev);
742
743         This->joyfd=open(This->dev,O_RDONLY);
744         if (This->joyfd==-1) {
745             ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
746             return DIERR_NOTFOUND;
747         }
748     }
749
750     This->base.acquired = 1;
751
752     return DI_OK;
753 }
754
755 /******************************************************************************
756   *     Unacquire : frees the joystick
757   */
758 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
759 {
760     JoystickImpl *This = (JoystickImpl *)iface;
761     HRESULT res;
762
763     TRACE("(%p)\n",This);
764
765     if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
766
767     if (This->joyfd!=-1) {
768         TRACE("closing joystick device\n");
769         close(This->joyfd);
770         This->joyfd = -1;
771         return DI_OK;
772     }
773
774     return DI_NOEFFECT;
775 }
776
777 static LONG map_axis(JoystickImpl * This, short val, short index)
778 {
779     double    fval = val;
780     double    fmin = This->props[index].lMin;
781     double    fmax = This->props[index].lMax;
782     double    fret;
783
784     fret = (((fval + 32767.0) * (fmax - fmin)) / (32767.0*2.0)) + fmin;
785
786     if (fret >= 0.0)
787         fret += 0.5;
788     else
789         fret -= 0.5;
790
791     return fret;
792 }
793
794 static LONG calculate_pov(JoystickImpl *This, int index)
795 {
796     if (This->povs[index].lX < -16384) {
797         if (This->povs[index].lY < -16384)
798             This->js.rgdwPOV[index] = 31500;
799         else if (This->povs[index].lY > 16384)
800             This->js.rgdwPOV[index] = 22500;
801         else
802             This->js.rgdwPOV[index] = 27000;
803     } else if (This->povs[index].lX > 16384) {
804         if (This->povs[index].lY < -16384)
805             This->js.rgdwPOV[index] = 4500;
806         else if (This->povs[index].lY > 16384)
807             This->js.rgdwPOV[index] = 13500;
808         else
809             This->js.rgdwPOV[index] = 9000;
810     } else {
811         if (This->povs[index].lY < -16384)
812             This->js.rgdwPOV[index] = 0;
813         else if (This->povs[index].lY > 16384)
814             This->js.rgdwPOV[index] = 18000;
815         else
816             This->js.rgdwPOV[index] = -1;
817     }
818
819     return This->js.rgdwPOV[index];
820 }
821
822 static void joy_polldev(JoystickImpl *This) {
823     struct pollfd plfd;
824     struct      js_event jse;
825     TRACE("(%p)\n", This);
826
827     if (This->joyfd==-1) {
828         WARN("no device\n");
829         return;
830     }
831     while (1) {
832         plfd.fd = This->joyfd;
833         plfd.events = POLLIN;
834         if (poll(&plfd,1,0) != 1)
835             return;
836         /* we have one event, so we can read */
837         if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
838             return;
839         }
840         TRACE("js_event: type 0x%x, number %d, value %d\n",
841               jse.type,jse.number,jse.value);
842         if (jse.type & JS_EVENT_BUTTON) {
843             int offset = This->offsets[jse.number + 12];
844             int value = jse.value?0x80:0x00;
845
846             This->js.rgbButtons[jse.number] = value;
847             queue_event((LPDIRECTINPUTDEVICE8A)This, offset, value, jse.time, This->dinput->evsequence++);
848         } else if (jse.type & JS_EVENT_AXIS) {
849             int number = This->axis_map[jse.number];    /* wine format object index */
850             if (number < 12) {
851                 int offset = This->offsets[number];
852                 int index = offset_to_object(This->user_df, offset);
853                 LONG value = map_axis(This, jse.value, index);
854
855                 /* FIXME do deadzone and saturation here */
856
857                 TRACE("changing axis %d => %d\n", jse.number, number);
858                 switch (number) {
859                 case 0:
860                     This->js.lX = value;
861                     break;
862                 case 1:
863                     This->js.lY = value;
864                     break;
865                 case 2:
866                     This->js.lZ = value;
867                     break;
868                 case 3:
869                     This->js.lRx = value;
870                     break;
871                 case 4:
872                     This->js.lRy = value;
873                     break;
874                 case 5:
875                     This->js.lRz = value;
876                     break;
877                 case 6:
878                     This->js.rglSlider[0] = value;
879                     break;
880                 case 7:
881                     This->js.rglSlider[1] = value;
882                     break;
883                 case 8:
884                     /* FIXME don't go off array */
885                     if (This->axis_map[jse.number + 1] == number)
886                         This->povs[0].lX = jse.value;
887                     else if (This->axis_map[jse.number - 1] == number)
888                         This->povs[0].lY = jse.value;
889                     value = calculate_pov(This, 0);
890                     break;
891                 case 9:
892                     if (This->axis_map[jse.number + 1] == number)
893                         This->povs[1].lX = jse.value;
894                     else if (This->axis_map[jse.number - 1] == number)
895                         This->povs[1].lY = jse.value;
896                     value = calculate_pov(This, 1);
897                     break;
898                 case 10:
899                     if (This->axis_map[jse.number + 1] == number)
900                         This->povs[2].lX = jse.value;
901                     else if (This->axis_map[jse.number - 1] == number)
902                         This->povs[2].lY = jse.value;
903                     value = calculate_pov(This, 2);
904                     break;
905                 case 11:
906                     if (This->axis_map[jse.number + 1] == number)
907                         This->povs[3].lX = jse.value;
908                     else if (This->axis_map[jse.number - 1] == number)
909                         This->povs[3].lY = jse.value;
910                     value = calculate_pov(This, 3);
911                     break;
912                 }
913
914                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, value, jse.time, This->dinput->evsequence++);
915             } else
916                 WARN("axis %d not supported\n", number);
917         }
918     }
919 }
920
921 /******************************************************************************
922   *     GetDeviceState : returns the "state" of the joystick.
923   *
924   */
925 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
926     LPDIRECTINPUTDEVICE8A iface,
927     DWORD len,
928     LPVOID ptr)
929 {
930     JoystickImpl *This = (JoystickImpl *)iface;
931
932     TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
933
934     if (!This->base.acquired) {
935         WARN("not acquired\n");
936         return DIERR_NOTACQUIRED;
937     }
938
939     /* update joystick state */
940     joy_polldev(This);
941
942     /* convert and copy data to user supplied buffer */
943     fill_DataFormat(ptr, &This->js, This->transform);
944
945     return DI_OK;
946 }
947
948 /******************************************************************************
949   *     SetProperty : change input device properties
950   */
951 static HRESULT WINAPI JoystickAImpl_SetProperty(
952     LPDIRECTINPUTDEVICE8A iface,
953     REFGUID rguid,
954     LPCDIPROPHEADER ph)
955 {
956     JoystickImpl *This = (JoystickImpl *)iface;
957     int i;
958
959     TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
960
961     if (ph == NULL) {
962         WARN("invalid parameter: ph == NULL\n");
963         return DIERR_INVALIDPARAM;
964     }
965
966     if (TRACE_ON(dinput))
967         _dump_DIPROPHEADER(ph);
968
969     if (!HIWORD(rguid)) {
970         switch (LOWORD(rguid)) {
971         case (DWORD)DIPROP_RANGE: {
972             LPCDIPROPRANGE      pr = (LPCDIPROPRANGE)ph;
973             if (ph->dwHow == DIPH_DEVICE) {
974                 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
975                 for (i = 0; i < This->user_df->dwNumObjs; i++) {
976                     This->props[i].lMin = pr->lMin;
977                     This->props[i].lMax = pr->lMax;
978                 }
979             } else {
980                 int obj = find_property(This->user_df, ph);
981                 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
982                 if (obj >= 0) {
983                     This->props[obj].lMin = pr->lMin;
984                     This->props[obj].lMax = pr->lMax;
985                     return DI_OK;
986                 }
987             }
988             break;
989         }
990         case (DWORD)DIPROP_DEADZONE: {
991             LPCDIPROPDWORD      pd = (LPCDIPROPDWORD)ph;
992             if (ph->dwHow == DIPH_DEVICE) {
993                 TRACE("deadzone(%d) all\n", pd->dwData);
994                 for (i = 0; i < This->user_df->dwNumObjs; i++)
995                     This->props[i].lDeadZone  = pd->dwData;
996             } else {
997                 int obj = find_property(This->user_df, ph);
998                 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
999                 if (obj >= 0) {
1000                     This->props[obj].lDeadZone  = pd->dwData;
1001                     return DI_OK;
1002                 }
1003             }
1004             break;
1005         }
1006         case (DWORD)DIPROP_SATURATION: {
1007             LPCDIPROPDWORD      pd = (LPCDIPROPDWORD)ph;
1008             if (ph->dwHow == DIPH_DEVICE) {
1009                 TRACE("saturation(%d) all\n", pd->dwData);
1010                 for (i = 0; i < This->user_df->dwNumObjs; i++)
1011                     This->props[i].lSaturation = pd->dwData;
1012             } else {
1013                 int obj = find_property(This->user_df, ph);
1014                 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
1015                 if (obj >= 0) {
1016                     This->props[obj].lSaturation = pd->dwData;
1017                     return DI_OK;
1018                 }
1019             }
1020             break;
1021         }
1022         default:
1023             return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
1024         }
1025     }
1026
1027     return DI_OK;
1028 }
1029
1030 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
1031         LPDIRECTINPUTDEVICE8A iface,
1032         LPDIDEVCAPS lpDIDevCaps)
1033 {
1034     JoystickImpl *This = (JoystickImpl *)iface;
1035     int size;
1036
1037     TRACE("%p->(%p)\n",iface,lpDIDevCaps);
1038
1039     if (lpDIDevCaps == NULL) {
1040         WARN("invalid pointer\n");
1041         return E_POINTER;
1042     }
1043
1044     size = lpDIDevCaps->dwSize;
1045
1046     if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
1047         WARN("invalid parameter\n");
1048         return DIERR_INVALIDPARAM;
1049     }
1050
1051     CopyMemory(lpDIDevCaps, &This->devcaps, size);
1052     lpDIDevCaps->dwSize = size;
1053
1054     if (TRACE_ON(dinput))
1055         _dump_DIDEVCAPS(lpDIDevCaps);
1056
1057     return DI_OK;
1058 }
1059
1060 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
1061 {
1062     JoystickImpl *This = (JoystickImpl *)iface;
1063
1064     TRACE("(%p)\n",This);
1065
1066     if (!This->base.acquired) {
1067         WARN("not acquired\n");
1068         return DIERR_NOTACQUIRED;
1069     }
1070
1071     joy_polldev(This);
1072     return DI_OK;
1073 }
1074
1075 /******************************************************************************
1076   *     EnumObjects : enumerate the different buttons and axis...
1077   */
1078 static HRESULT WINAPI JoystickAImpl_EnumObjects(
1079         LPDIRECTINPUTDEVICE8A iface,
1080         LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
1081         LPVOID lpvRef,
1082         DWORD dwFlags)
1083 {
1084   JoystickImpl *This = (JoystickImpl *)iface;
1085   DIDEVICEOBJECTINSTANCEA ddoi;
1086   BYTE i;
1087   int user_offset;
1088   int user_object;
1089
1090   TRACE("(this=%p,%p,%p,%08x)\n", This, lpCallback, lpvRef, dwFlags);
1091   if (TRACE_ON(dinput)) {
1092     TRACE("  - flags = ");
1093     _dump_EnumObjects_flags(dwFlags);
1094     TRACE("\n");
1095   }
1096
1097   /* Only the fields till dwFFMaxForce are relevant */
1098   ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
1099
1100   /* For the joystick, do as is done in the GetCapabilities function */
1101   if ((dwFlags == DIDFT_ALL) ||
1102       (dwFlags & DIDFT_AXIS) ||
1103       (dwFlags & DIDFT_POV)) {
1104     int pov[4] = { 0, 0, 0, 0 };
1105     int axes = 0;
1106     int povs = 0;
1107
1108     for (i = 0; i < This->axes; i++) {
1109       int wine_obj = This->axis_map[i];
1110       BOOL skip = FALSE;
1111
1112       switch (wine_obj) {
1113       case 0:
1114         ddoi.guidType = GUID_XAxis;
1115         break;
1116       case 1:
1117         ddoi.guidType = GUID_YAxis;
1118         break;
1119       case 2:
1120         ddoi.guidType = GUID_ZAxis;
1121         break;
1122       case 3:
1123         ddoi.guidType = GUID_RxAxis;
1124         break;
1125       case 4:
1126         ddoi.guidType = GUID_RyAxis;
1127         break;
1128       case 5:
1129         ddoi.guidType = GUID_RzAxis;
1130         break;
1131       case 6:
1132         ddoi.guidType = GUID_Slider;
1133         break;
1134       case 7:
1135         ddoi.guidType = GUID_Slider;
1136         break;
1137       case 8:
1138         pov[0]++;
1139         ddoi.guidType = GUID_POV;
1140         break;
1141       case 9:
1142         pov[1]++;
1143         ddoi.guidType = GUID_POV;
1144         break;
1145       case 10:
1146         pov[2]++;
1147         ddoi.guidType = GUID_POV;
1148         break;
1149       case 11:
1150         pov[3]++;
1151         ddoi.guidType = GUID_POV;
1152         break;
1153       default:
1154         ddoi.guidType = GUID_Unknown;
1155       }
1156       if (wine_obj < 8) {
1157           user_offset = This->offsets[wine_obj];        /* get user offset from wine index */
1158           user_object = offset_to_object(This->user_df, user_offset);
1159
1160           ddoi.dwType = This->user_df->rgodf[user_object].dwType & 0x00ffffff;
1161           ddoi.dwOfs =  This->user_df->rgodf[user_object].dwOfs;
1162           sprintf(ddoi.tszName, "Axis %d", axes);
1163           axes++;
1164       } else {
1165           if (pov[wine_obj - 8] < 2) {
1166               user_offset = This->offsets[wine_obj];    /* get user offset from wine index */
1167               user_object = offset_to_object(This->user_df, user_offset);
1168
1169               ddoi.dwType = This->user_df->rgodf[user_object].dwType & 0x00ffffff;
1170               ddoi.dwOfs =  This->user_df->rgodf[user_object].dwOfs;
1171               sprintf(ddoi.tszName, "POV %d", povs);
1172               povs++;
1173           } else
1174               skip = TRUE;
1175       }
1176       if (!skip) {
1177           _dump_OBJECTINSTANCEA(&ddoi);
1178           if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE)
1179               return DI_OK;
1180       }
1181     }
1182   }
1183
1184   if ((dwFlags == DIDFT_ALL) ||
1185       (dwFlags & DIDFT_BUTTON)) {
1186
1187     /* The DInput SDK says that GUID_Button is only for mouse buttons but well... */
1188     ddoi.guidType = GUID_Button;
1189
1190     for (i = 0; i < This->buttons; i++) {
1191       user_offset = This->offsets[i + 12];      /* get user offset from wine index */
1192       user_object = offset_to_object(This->user_df, user_offset);
1193       ddoi.guidType = GUID_Button;
1194       ddoi.dwType = This->user_df->rgodf[user_object].dwType & 0x00ffffff;
1195       ddoi.dwOfs =  This->user_df->rgodf[user_object].dwOfs;
1196       sprintf(ddoi.tszName, "Button %d", i);
1197       _dump_OBJECTINSTANCEA(&ddoi);
1198       if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) return DI_OK;
1199     }
1200   }
1201
1202   return DI_OK;
1203 }
1204
1205 /******************************************************************************
1206   *     EnumObjects : enumerate the different buttons and axis...
1207   */
1208 static HRESULT WINAPI JoystickWImpl_EnumObjects(
1209         LPDIRECTINPUTDEVICE8W iface,
1210         LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
1211         LPVOID lpvRef,
1212         DWORD dwFlags)
1213 {
1214   JoystickImpl *This = (JoystickImpl *)iface;
1215
1216   device_enumobjects_AtoWcb_data data;
1217
1218   data.lpCallBack = lpCallback;
1219   data.lpvRef = lpvRef;
1220
1221   return JoystickAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
1222 }
1223
1224 /******************************************************************************
1225   *     GetProperty : get input device properties
1226   */
1227 static HRESULT WINAPI JoystickAImpl_GetProperty(
1228     LPDIRECTINPUTDEVICE8A iface,
1229     REFGUID rguid,
1230     LPDIPROPHEADER pdiph)
1231 {
1232     JoystickImpl *This = (JoystickImpl *)iface;
1233
1234     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
1235
1236     if (TRACE_ON(dinput))
1237         _dump_DIPROPHEADER(pdiph);
1238
1239     if (!HIWORD(rguid)) {
1240         switch (LOWORD(rguid)) {
1241         case (DWORD) DIPROP_RANGE: {
1242             LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
1243             int obj = find_property(This->user_df, pdiph);
1244             /* The app is querying the current range of the axis
1245              * return the lMin and lMax values */
1246             if (obj >= 0) {
1247                 pr->lMin = This->props[obj].lMin;
1248                 pr->lMax = This->props[obj].lMax;
1249                 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
1250                 return DI_OK;
1251             }
1252             break;
1253         }
1254         case (DWORD) DIPROP_DEADZONE: {
1255             LPDIPROPDWORD       pd = (LPDIPROPDWORD)pdiph;
1256             int obj = find_property(This->user_df, pdiph);
1257             if (obj >= 0) {
1258                 pd->dwData = This->props[obj].lDeadZone;
1259                 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
1260                 return DI_OK;
1261             }
1262             break;
1263         }
1264         case (DWORD) DIPROP_SATURATION: {
1265             LPDIPROPDWORD       pd = (LPDIPROPDWORD)pdiph;
1266             int obj = find_property(This->user_df, pdiph);
1267             if (obj >= 0) {
1268                 pd->dwData = This->props[obj].lSaturation;
1269                 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
1270                 return DI_OK;
1271             }
1272             break;
1273         }
1274         default:
1275             return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
1276         }
1277     }
1278
1279     return DI_OK;
1280 }
1281
1282 /******************************************************************************
1283   *     GetObjectInfo : get object info
1284   */
1285 HRESULT WINAPI JoystickAImpl_GetObjectInfo(
1286         LPDIRECTINPUTDEVICE8A iface,
1287         LPDIDEVICEOBJECTINSTANCEA pdidoi,
1288         DWORD dwObj,
1289         DWORD dwHow)
1290 {
1291     JoystickImpl *This = (JoystickImpl *)iface;
1292     DIDEVICEOBJECTINSTANCEA didoiA;
1293     unsigned int i;
1294
1295     TRACE("(%p,%p,%d,0x%08x(%s))\n",
1296           iface, pdidoi, dwObj, dwHow,
1297           dwHow == DIPH_BYOFFSET ? "DIPH_BYOFFSET" :
1298           dwHow == DIPH_BYID ? "DIPH_BYID" :
1299           dwHow == DIPH_BYUSAGE ? "DIPH_BYUSAGE" :
1300           "UNKNOWN");
1301
1302     if (pdidoi == NULL) {
1303         WARN("invalid parameter: pdidoi = NULL\n");
1304         return DIERR_INVALIDPARAM;
1305     }
1306
1307     if ((pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCEA)) &&
1308         (pdidoi->dwSize != sizeof(DIDEVICEOBJECTINSTANCE_DX3A))) {
1309         WARN("invalid parameter: pdidoi->dwSize = %d != %d or %d\n",
1310              pdidoi->dwSize, sizeof(DIDEVICEOBJECTINSTANCEA),
1311              sizeof(DIDEVICEOBJECTINSTANCE_DX3A));
1312         return DIERR_INVALIDPARAM;
1313     }
1314
1315     ZeroMemory(&didoiA, sizeof(didoiA));
1316     didoiA.dwSize = pdidoi->dwSize;
1317
1318     switch (dwHow) {
1319     case DIPH_BYOFFSET: {
1320         int axis = 0;
1321         int pov = 0;
1322         int button = 0;
1323         for (i = 0; i < This->user_df->dwNumObjs; i++) {
1324             if (This->user_df->rgodf[i].dwOfs == dwObj) {
1325                 if (This->user_df->rgodf[i].pguid)
1326                     didoiA.guidType = *This->user_df->rgodf[i].pguid;
1327                 else
1328                     didoiA.guidType = GUID_NULL;
1329
1330                 didoiA.dwOfs = dwObj;
1331                 didoiA.dwType = This->user_df->rgodf[i].dwType;
1332                 didoiA.dwFlags = This->user_df->rgodf[i].dwFlags;
1333
1334                 if (DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) & DIDFT_AXIS)
1335                     sprintf(didoiA.tszName, "Axis %d", axis);
1336                 else if (DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) & DIDFT_POV)
1337                     sprintf(didoiA.tszName, "POV %d", pov);
1338                 else if (DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) & DIDFT_BUTTON)
1339                     sprintf(didoiA.tszName, "Button %d", button);
1340
1341                 CopyMemory(pdidoi, &didoiA, pdidoi->dwSize);
1342                 return DI_OK;
1343             }
1344
1345             if (DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) & DIDFT_AXIS)
1346                 axis++;
1347             else if (DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) & DIDFT_POV)
1348                 pov++;
1349             else if (DIDFT_GETTYPE(This->user_df->rgodf[i].dwType) & DIDFT_BUTTON)
1350                 button++;
1351         }
1352         break;
1353     }
1354     case DIPH_BYID:
1355         FIXME("dwHow = DIPH_BYID not implemented\n");
1356         break;
1357     case DIPH_BYUSAGE:
1358         FIXME("dwHow = DIPH_BYUSAGE not implemented\n");
1359         break;
1360     default:
1361         WARN("invalid parameter: dwHow = %08x\n", dwHow);
1362         return DIERR_INVALIDPARAM;
1363     }
1364
1365     CopyMemory(pdidoi, &didoiA, pdidoi->dwSize);
1366
1367     return DI_OK;
1368 }
1369
1370 /******************************************************************************
1371   *     GetDeviceInfo : get information about a device's identity
1372   */
1373 HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1374     LPDIRECTINPUTDEVICE8A iface,
1375     LPDIDEVICEINSTANCEA pdidi)
1376 {
1377     JoystickImpl *This = (JoystickImpl *)iface;
1378
1379     TRACE("(%p,%p)\n", iface, pdidi);
1380
1381     if (pdidi == NULL) {
1382         WARN("invalid pointer\n");
1383         return E_POINTER;
1384     }
1385
1386     if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1387         (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1388         WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1389              pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1390              sizeof(DIDEVICEINSTANCEA));
1391         return DIERR_INVALIDPARAM;
1392     }
1393
1394     /* Return joystick */
1395     pdidi->guidInstance = GUID_Joystick;
1396     pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1397     /* we only support traditional joysticks for now */
1398     pdidi->dwDevType = This->devcaps.dwDevType;
1399     strcpy(pdidi->tszInstanceName, "Joystick");
1400     strcpy(pdidi->tszProductName, This->name);
1401     if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1402         pdidi->guidFFDriver = GUID_NULL;
1403         pdidi->wUsagePage = 0;
1404         pdidi->wUsage = 0;
1405     }
1406
1407     return DI_OK;
1408 }
1409
1410 /******************************************************************************
1411   *     GetDeviceInfo : get information about a device's identity
1412   */
1413 HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1414     LPDIRECTINPUTDEVICE8W iface,
1415     LPDIDEVICEINSTANCEW pdidi)
1416 {
1417     JoystickImpl *This = (JoystickImpl *)iface;
1418
1419     TRACE("(%p,%p)\n", iface, pdidi);
1420
1421     if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1422         (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1423         WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1424              pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1425              sizeof(DIDEVICEINSTANCEW));
1426         return DIERR_INVALIDPARAM;
1427     }
1428
1429     /* Return joystick */
1430     pdidi->guidInstance = GUID_Joystick;
1431     pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1432     /* we only support traditional joysticks for now */
1433     pdidi->dwDevType = This->devcaps.dwDevType;
1434     MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1435     MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1436     if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1437         pdidi->guidFFDriver = GUID_NULL;
1438         pdidi->wUsagePage = 0;
1439         pdidi->wUsage = 0;
1440     }
1441
1442     return DI_OK;
1443 }
1444
1445 static const IDirectInputDevice8AVtbl JoystickAvt =
1446 {
1447         IDirectInputDevice2AImpl_QueryInterface,
1448         IDirectInputDevice2AImpl_AddRef,
1449         JoystickAImpl_Release,
1450         JoystickAImpl_GetCapabilities,
1451         JoystickAImpl_EnumObjects,
1452         JoystickAImpl_GetProperty,
1453         JoystickAImpl_SetProperty,
1454         JoystickAImpl_Acquire,
1455         JoystickAImpl_Unacquire,
1456         JoystickAImpl_GetDeviceState,
1457         IDirectInputDevice2AImpl_GetDeviceData,
1458         JoystickAImpl_SetDataFormat,
1459         IDirectInputDevice2AImpl_SetEventNotification,
1460         IDirectInputDevice2AImpl_SetCooperativeLevel,
1461         JoystickAImpl_GetObjectInfo,
1462         JoystickAImpl_GetDeviceInfo,
1463         IDirectInputDevice2AImpl_RunControlPanel,
1464         IDirectInputDevice2AImpl_Initialize,
1465         IDirectInputDevice2AImpl_CreateEffect,
1466         IDirectInputDevice2AImpl_EnumEffects,
1467         IDirectInputDevice2AImpl_GetEffectInfo,
1468         IDirectInputDevice2AImpl_GetForceFeedbackState,
1469         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1470         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1471         IDirectInputDevice2AImpl_Escape,
1472         JoystickAImpl_Poll,
1473         IDirectInputDevice2AImpl_SendDeviceData,
1474         IDirectInputDevice7AImpl_EnumEffectsInFile,
1475         IDirectInputDevice7AImpl_WriteEffectToFile,
1476         IDirectInputDevice8AImpl_BuildActionMap,
1477         IDirectInputDevice8AImpl_SetActionMap,
1478         IDirectInputDevice8AImpl_GetImageInfo
1479 };
1480
1481 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1482 # define XCAST(fun)     (typeof(SysJoystickWvt.fun))
1483 #else
1484 # define XCAST(fun)     (void*)
1485 #endif
1486
1487 static const IDirectInputDevice8WVtbl SysJoystickWvt =
1488 {
1489         IDirectInputDevice2WImpl_QueryInterface,
1490         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1491         XCAST(Release)JoystickAImpl_Release,
1492         XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1493         JoystickWImpl_EnumObjects,
1494         XCAST(GetProperty)JoystickAImpl_GetProperty,
1495         XCAST(SetProperty)JoystickAImpl_SetProperty,
1496         XCAST(Acquire)JoystickAImpl_Acquire,
1497         XCAST(Unacquire)JoystickAImpl_Unacquire,
1498         XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1499         XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1500         XCAST(SetDataFormat)JoystickAImpl_SetDataFormat,
1501         XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1502         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1503         IDirectInputDevice2WImpl_GetObjectInfo,
1504         JoystickWImpl_GetDeviceInfo,
1505         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1506         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1507         XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1508         IDirectInputDevice2WImpl_EnumEffects,
1509         IDirectInputDevice2WImpl_GetEffectInfo,
1510         XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1511         XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1512         XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1513         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1514         XCAST(Poll)JoystickAImpl_Poll,
1515         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1516         IDirectInputDevice7WImpl_EnumEffectsInFile,
1517         IDirectInputDevice7WImpl_WriteEffectToFile,
1518         IDirectInputDevice8WImpl_BuildActionMap,
1519         IDirectInputDevice8WImpl_SetActionMap,
1520         IDirectInputDevice8WImpl_GetImageInfo
1521 };
1522 #undef XCAST
1523
1524 #else  /* HAVE_LINUX_22_JOYSTICK_API */
1525
1526 const struct dinput_device joystick_linux_device = {
1527   "Wine Linux joystick driver",
1528   NULL,
1529   NULL,
1530   NULL,
1531   NULL
1532 };
1533
1534 #endif  /* HAVE_LINUX_22_JOYSTICK_API */