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