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