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