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