comctl32/monthcal: Changing MCS_MULTISELECT isn't allowed after creation, set default...
[wine] / dlls / dinput / joystick_linuxinput.c
1 /*              DirectInput Joystick device
2  *
3  * Copyright 1998,2000 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  * Copyright 2005 Daniel Remenak
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <assert.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #ifdef HAVE_SYS_TIME_H
35 # include <sys/time.h>
36 #endif
37 #include <fcntl.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 # include <sys/ioctl.h>
40 #endif
41 #include <errno.h>
42 #ifdef HAVE_SYS_ERRNO_H
43 # include <sys/errno.h>
44 #endif
45 #ifdef HAVE_LINUX_INPUT_H
46 # include <linux/input.h>
47 # undef SW_MAX
48 # if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
49 #  define HAVE_CORRECT_LINUXINPUT_H
50 # endif
51 #endif
52 #ifdef HAVE_SYS_POLL_H
53 # include <sys/poll.h>
54 #endif
55
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
58 #include "wine/list.h"
59 #include "windef.h"
60 #include "winbase.h"
61 #include "winerror.h"
62 #include "winreg.h"
63 #include "dinput.h"
64
65 #include "dinput_private.h"
66 #include "device_private.h"
67 #include "joystick_private.h"
68
69 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
70
71 #ifdef HAVE_CORRECT_LINUXINPUT_H
72
73 #define EVDEVPREFIX     "/dev/input/event"
74
75 /* Wine joystick driver object instances */
76 #define WINE_JOYSTICK_MAX_AXES    8
77 #define WINE_JOYSTICK_MAX_POVS    4
78 #define WINE_JOYSTICK_MAX_BUTTONS 128
79
80 struct wine_input_absinfo {
81     LONG value;
82     LONG minimum;
83     LONG maximum;
84     LONG fuzz;
85     LONG flat;
86 };
87
88 /* implemented in effect_linuxinput.c */
89 HRESULT linuxinput_create_effect(int* fd, REFGUID rguid, struct list *parent_list_entry, LPDIRECTINPUTEFFECT* peff);
90 HRESULT linuxinput_get_info_A(int fd, REFGUID rguid, LPDIEFFECTINFOA info);
91 HRESULT linuxinput_get_info_W(int fd, REFGUID rguid, LPDIEFFECTINFOW info);
92
93 typedef struct JoystickImpl JoystickImpl;
94 static const IDirectInputDevice8AVtbl JoystickAvt;
95 static const IDirectInputDevice8WVtbl JoystickWvt;
96
97 struct JoyDev {
98         char *device;
99         char *name;
100         GUID guid;
101
102         int has_ff;
103         int num_effects;
104
105         /* data returned by EVIOCGBIT for caps, EV_ABS, EV_KEY, and EV_FF */
106         BYTE                            evbits[(EV_MAX+7)/8];
107         BYTE                            absbits[(ABS_MAX+7)/8];
108         BYTE                            keybits[(KEY_MAX+7)/8];
109         BYTE                            ffbits[(FF_MAX+7)/8];   
110
111         /* data returned by the EVIOCGABS() ioctl */
112         struct wine_input_absinfo       axes[ABS_MAX];
113 };
114
115 struct JoystickImpl
116 {
117         struct JoystickGenericImpl      generic;
118         struct JoyDev                  *joydev;
119
120         /* joystick private */
121         int                             joyfd;
122
123         int                             dev_axes_to_di[ABS_MAX];
124         POINTL                          povs[4];
125
126         /* LUT for KEY_ to offset in rgbButtons */
127         BYTE                            buttons[KEY_MAX];
128
129         /* Force feedback variables */
130         struct list                     ff_effects;
131         int                             ff_state;
132         int                             ff_autocenter;
133         int                             ff_gain;
134 };
135
136 static void fake_current_js_state(JoystickImpl *ji);
137 static void find_joydevs(void);
138 static void joy_polldev(JoystickGenericImpl *This);
139
140 /* This GUID is slightly different from the linux joystick one. Take note. */
141 static const GUID DInput_Wine_Joystick_Base_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
142   0x9e573eda,
143   0x7734,
144   0x11d2,
145   {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
146 };
147
148 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
149
150 #define MAX_JOYDEV 64
151
152 static int have_joydevs = -1;
153 static struct JoyDev *joydevs = NULL;
154
155 static void find_joydevs(void)
156 {
157     int i;
158
159     if (InterlockedCompareExchange(&have_joydevs, 0, -1) != -1)
160         /* Someone beat us to it */
161         return;
162
163     for (i = 0; i < MAX_JOYDEV; i++)
164     {
165         char buf[MAX_PATH];
166         struct JoyDev joydev = {0};
167         int fd;
168         int no_ff_check = 0;
169         int j;
170         struct JoyDev *new_joydevs;
171
172         snprintf(buf, sizeof(buf), EVDEVPREFIX"%d", i);
173
174         if ((fd = open(buf, O_RDWR)) == -1)
175         {
176             fd = open(buf, O_RDONLY);
177             no_ff_check = 1;
178         }
179
180         if (fd == -1)
181         {
182             WARN("Failed to open \"%s\": %d %s\n", buf, errno, strerror(errno));
183             continue;
184         }
185
186         if (ioctl(fd, EVIOCGBIT(0, sizeof(joydev.evbits)), joydev.evbits) == -1)
187         {
188             WARN("ioct(EVIOCGBIT, 0) failed: %d %s\n", errno, strerror(errno));
189             close(fd);
190             continue;
191         }
192         if (ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(joydev.absbits)), joydev.absbits) == -1)
193         {
194             WARN("ioct(EVIOCGBIT, EV_ABS) failed: %d %s\n", errno, strerror(errno));
195             close(fd);
196             continue;
197         }
198         if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(joydev.keybits)), joydev.keybits) == -1)
199         {
200             WARN("ioct(EVIOCGBIT, EV_KEY) failed: %d %s\n", errno, strerror(errno));
201             close(fd);
202             continue;
203         }
204
205         /* A true joystick has at least axis X and Y, and at least 1
206          * button. copied from linux/drivers/input/joydev.c */
207         if (!test_bit(joydev.absbits, ABS_X) || !test_bit(joydev.absbits, ABS_Y) ||
208             !(test_bit(joydev.keybits, BTN_TRIGGER) ||
209               test_bit(joydev.keybits, BTN_A) ||
210               test_bit(joydev.keybits, BTN_1)))
211         {
212             close(fd);
213             continue;
214         }
215
216         if (!(joydev.device = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + 1)))
217         {
218             close(fd);
219             continue;
220         }
221         strcpy(joydev.device, buf);
222
223         buf[MAX_PATH - 1] = 0;
224         if (ioctl(fd, EVIOCGNAME(MAX_PATH - 1), buf) != -1 &&
225             (joydev.name = HeapAlloc(GetProcessHeap(), 0, strlen(buf) + 1)))
226             strcpy(joydev.name, buf);
227         else
228             joydev.name = joydev.device;
229
230         joydev.guid = DInput_Wine_Joystick_Base_GUID;
231         joydev.guid.Data3 += have_joydevs;
232
233         TRACE("Found a joystick on %s: %s (%s)\n", 
234             joydev.device, joydev.name, 
235             debugstr_guid(&joydev.guid)
236             );
237
238 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
239         if (!no_ff_check &&
240             test_bit(joydev.evbits, EV_FF) &&
241             ioctl(fd, EVIOCGBIT(EV_FF, sizeof(joydev.ffbits)), joydev.ffbits) != -1 &&
242             ioctl(fd, EVIOCGEFFECTS, &joydev.num_effects) != -1 &&
243             joydev.num_effects > 0)
244         {
245             TRACE(" ... with force feedback\n");
246             joydev.has_ff = 1;
247         }
248 #endif
249
250         for (j = 0; j < ABS_MAX;j ++)
251         {
252             if (!test_bit(joydev.absbits, j)) continue;
253             if (ioctl(fd, EVIOCGABS(j), &(joydev.axes[j])) != -1)
254             {
255               TRACE(" ... with axis %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
256                   j,
257                   joydev.axes[j].value,
258                   joydev.axes[j].minimum,
259                   joydev.axes[j].maximum,
260                   joydev.axes[j].fuzz,
261                   joydev.axes[j].flat
262                   );
263             }
264         }
265
266         if (!have_joydevs)
267             new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
268         else
269             new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joydevs, (1 + have_joydevs) * sizeof(struct JoyDev));
270
271         if (!new_joydevs)
272         {
273             close(fd);
274             continue;
275         }
276         joydevs = new_joydevs;
277         memcpy(joydevs + have_joydevs, &joydev, sizeof(joydev));
278         have_joydevs++;
279
280         close(fd);
281     }
282 }
283
284 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
285 {
286     DWORD dwSize = lpddi->dwSize;
287
288     TRACE("%d %p\n", dwSize, lpddi);
289     memset(lpddi, 0, dwSize);
290
291     lpddi->dwSize       = dwSize;
292     lpddi->guidInstance = joydevs[id].guid;
293     lpddi->guidProduct  = DInput_Wine_Joystick_Base_GUID;
294     lpddi->guidFFDriver = GUID_NULL;
295
296     if (version >= 0x0800)
297         lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
298     else
299         lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
300
301     strcpy(lpddi->tszInstanceName, joydevs[id].name);
302     strcpy(lpddi->tszProductName, joydevs[id].name);
303 }
304
305 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
306 {
307     DWORD dwSize = lpddi->dwSize;
308
309     TRACE("%d %p\n", dwSize, lpddi);
310     memset(lpddi, 0, dwSize);
311
312     lpddi->dwSize       = dwSize;
313     lpddi->guidInstance = joydevs[id].guid;
314     lpddi->guidProduct  = DInput_Wine_Joystick_Base_GUID;
315     lpddi->guidFFDriver = GUID_NULL;
316
317     if (version >= 0x0800)
318         lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
319     else
320         lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
321
322     MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
323     MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszProductName, MAX_PATH);
324 }
325
326 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
327 {
328   find_joydevs();
329
330   if (id >= have_joydevs) {
331     return FALSE;
332   }
333
334   if (!((dwDevType == 0) ||
335         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
336         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
337     return FALSE;
338
339 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
340   if (dwFlags & DIEDFL_FORCEFEEDBACK)
341     return FALSE;
342 #endif
343
344   if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
345     fill_joystick_dideviceinstanceA(lpddi, version, id);
346     return TRUE;
347   }
348   return FALSE;
349 }
350
351 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
352 {
353   find_joydevs();
354
355   if (id >= have_joydevs) {
356     return FALSE;
357   }
358
359   if (!((dwDevType == 0) ||
360         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
361         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
362     return FALSE;
363
364 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
365   if (dwFlags & DIEDFL_FORCEFEEDBACK)
366     return FALSE;
367 #endif
368
369   if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
370     fill_joystick_dideviceinstanceW(lpddi, version, id);
371     return TRUE;
372   }
373   return FALSE;
374 }
375
376 static JoystickImpl *alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, unsigned short index)
377 {
378     JoystickImpl* newDevice;
379     LPDIDATAFORMAT df = NULL;
380     int i, idx = 0;
381     int default_axis_map[WINE_JOYSTICK_MAX_AXES + WINE_JOYSTICK_MAX_POVS*2];
382
383     newDevice = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(JoystickImpl));
384     if (!newDevice) return NULL;
385
386     newDevice->generic.base.lpVtbl = jvt;
387     newDevice->generic.base.ref    = 1;
388     newDevice->generic.base.guid   = *rguid;
389     newDevice->generic.base.dinput = dinput;
390     newDevice->generic.joy_polldev = joy_polldev;
391     newDevice->joyfd       = -1;
392     newDevice->joydev      = &joydevs[index];
393     newDevice->generic.name        = newDevice->joydev->name;
394     list_init(&newDevice->ff_effects);
395 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
396     newDevice->ff_state    = FF_STATUS_STOPPED;
397 #endif
398     /* There is no way in linux to query force feedback autocenter status.
399        Instead, track it with ff_autocenter, and assume it's initialy
400        enabled. */
401     newDevice->ff_autocenter = 1;
402     newDevice->ff_gain = 0xFFFF;
403     InitializeCriticalSection(&newDevice->generic.base.crit);
404     newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
405
406     /* Count number of available axes - supported Axis & POVs */
407     for (i = 0; i < WINE_JOYSTICK_MAX_AXES; i++)
408     {
409         if (test_bit(newDevice->joydev->absbits, i))
410         {
411             newDevice->generic.device_axis_count++;
412             newDevice->dev_axes_to_di[i] = idx;
413             newDevice->generic.props[idx].lDevMin = newDevice->joydev->axes[i].minimum;
414             newDevice->generic.props[idx].lDevMax = newDevice->joydev->axes[i].maximum;
415             default_axis_map[idx] = i;
416             idx++;
417         }
418         else
419             newDevice->dev_axes_to_di[i] = -1;
420     }
421
422     for (i = 0; i < WINE_JOYSTICK_MAX_POVS; i++)
423     {
424         if (test_bit(newDevice->joydev->absbits, ABS_HAT0X + i * 2) &&
425             test_bit(newDevice->joydev->absbits, ABS_HAT0Y + i * 2))
426         {
427             newDevice->generic.device_axis_count += 2;
428             newDevice->generic.props[idx  ].lDevMin = newDevice->joydev->axes[ABS_HAT0X + i * 2].minimum;
429             newDevice->generic.props[idx  ].lDevMax = newDevice->joydev->axes[ABS_HAT0X + i * 2].maximum;
430             newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = idx;
431             newDevice->generic.props[idx+1].lDevMin = newDevice->joydev->axes[ABS_HAT0Y + i * 2].minimum;
432             newDevice->generic.props[idx+1].lDevMax = newDevice->joydev->axes[ABS_HAT0Y + i * 2].maximum;
433             newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = idx + 1;
434
435             default_axis_map[idx] = default_axis_map[idx + 1] = WINE_JOYSTICK_MAX_AXES + i;
436             idx += 2;
437         }
438         else
439             newDevice->dev_axes_to_di[ABS_HAT0X + i * 2] = newDevice->dev_axes_to_di[ABS_HAT0Y + i * 2] = -1;
440     }
441
442     /* do any user specified configuration */
443     if (setup_dinput_options(&newDevice->generic, default_axis_map) != DI_OK) goto failed;
444
445     /* Create copy of default data format */
446     if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto failed;
447     memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
448     if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto failed;
449
450
451     /* Construct internal data format */
452
453     /* Supported Axis & POVs */
454     for (i = 0, idx = 0; i < newDevice->generic.device_axis_count; i++)
455     {
456         int wine_obj = newDevice->generic.axis_map[i];
457
458         if (wine_obj < 0) continue;
459
460         memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
461         if (wine_obj < 8)
462             df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
463         else
464         {
465             df->rgodf[idx].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
466             i++; /* POV takes 2 axes */
467         }
468
469         newDevice->generic.props[idx].lMin        = 0;
470         newDevice->generic.props[idx].lMax        = 0xffff;
471         newDevice->generic.props[idx].lSaturation = 0;
472         newDevice->generic.props[idx].lDeadZone   = newDevice->generic.deadzone;
473
474         /* Linux supports force-feedback on X & Y axes only */
475         if (newDevice->joydev->has_ff && (i == 0 || i == 1))
476             df->rgodf[idx].dwFlags |= DIDOI_FFACTUATOR;
477
478         idx++;
479     }
480
481     /* Buttons can be anywhere, so check all */
482     for (i = 0; i < KEY_MAX && newDevice->generic.devcaps.dwButtons < WINE_JOYSTICK_MAX_BUTTONS; i++)
483     {
484         if (!test_bit(newDevice->joydev->keybits, i)) continue;
485
486         memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[newDevice->generic.devcaps.dwButtons + 12], df->dwObjSize);
487         newDevice->buttons[i] = 0x80 | newDevice->generic.devcaps.dwButtons;
488         df->rgodf[idx  ].pguid = &GUID_Button;
489         df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(newDevice->generic.devcaps.dwButtons++) | DIDFT_PSHBUTTON;
490     }
491     df->dwNumObjs = idx;
492     newDevice->generic.base.data_format.wine_df = df;
493
494     fake_current_js_state(newDevice);
495
496     /* Fill the caps */
497     newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
498     newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
499     if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
500         newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
501     else
502         newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
503
504     if (newDevice->joydev->has_ff)
505         newDevice->generic.devcaps.dwFlags |= DIDC_FORCEFEEDBACK;
506
507     IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->generic.base.dinput);
508     return newDevice;
509
510 failed:
511     if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
512     HeapFree(GetProcessHeap(), 0, df);
513     HeapFree(GetProcessHeap(), 0, newDevice->generic.axis_map);
514     HeapFree(GetProcessHeap(), 0, newDevice);
515     return NULL;
516 }
517
518 /******************************************************************************
519   *     get_joystick_index : Get the joystick index from a given GUID
520   */
521 static unsigned short get_joystick_index(REFGUID guid)
522 {
523     GUID wine_joystick = DInput_Wine_Joystick_Base_GUID;
524     GUID dev_guid = *guid;
525
526     wine_joystick.Data3 = 0;
527     dev_guid.Data3 = 0;
528
529     /* for the standard joystick GUID use index 0 */
530     if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
531
532     /* for the wine joystick GUIDs use the index stored in Data3 */
533     if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3 - DInput_Wine_Joystick_Base_GUID.Data3;
534
535     return MAX_JOYDEV;
536 }
537
538 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
539 {
540     unsigned short index;
541
542     find_joydevs();
543
544     if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
545         have_joydevs && index < have_joydevs)
546     {
547         if ((riid == NULL) ||
548             IsEqualGUID(&IID_IDirectInputDeviceA,  riid) ||
549             IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
550             IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
551             IsEqualGUID(&IID_IDirectInputDevice8A, riid))
552         {
553             *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput, index);
554             TRACE("Created a Joystick device (%p)\n", *pdev);
555
556             if (*pdev == NULL)
557             {
558                 ERR("out of memory\n");
559                 return DIERR_OUTOFMEMORY;
560             }
561             return DI_OK;
562         }
563
564         WARN("no interface\n");
565         return DIERR_NOINTERFACE;
566     }
567
568     return DIERR_DEVICENOTREG;
569 }
570
571
572 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
573 {
574     unsigned short index;
575
576     find_joydevs();
577
578     if ((index = get_joystick_index(rguid)) < MAX_JOYDEV &&
579         have_joydevs && index < have_joydevs)
580     {
581         if ((riid == NULL) ||
582             IsEqualGUID(&IID_IDirectInputDeviceW,  riid) ||
583             IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
584             IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
585             IsEqualGUID(&IID_IDirectInputDevice8W, riid))
586         {
587             *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput, index);
588             TRACE("Created a Joystick device (%p)\n", *pdev);
589
590             if (*pdev == NULL)
591             {
592                 ERR("out of memory\n");
593                 return DIERR_OUTOFMEMORY;
594             }
595             return DI_OK;
596         }
597         WARN("no interface\n");
598         return DIERR_NOINTERFACE;
599     }
600
601     WARN("invalid device GUID\n");
602     return DIERR_DEVICENOTREG;
603 }
604
605 const struct dinput_device joystick_linuxinput_device = {
606   "Wine Linux-input joystick driver",
607   joydev_enum_deviceA,
608   joydev_enum_deviceW,
609   joydev_create_deviceA,
610   joydev_create_deviceW
611 };
612
613 /******************************************************************************
614   *     Acquire : gets exclusive control of the joystick
615   */
616 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
617 {
618     JoystickImpl *This = (JoystickImpl *)iface;
619     HRESULT res;
620
621     TRACE("(this=%p)\n",This);
622
623     if ((res = IDirectInputDevice2AImpl_Acquire(iface)) != DI_OK)
624     {
625         WARN("Failed to acquire: %x\n", res);
626         return res;
627     }
628
629     if ((This->joyfd = open(This->joydev->device, O_RDWR)) == -1)
630     {
631         if ((This->joyfd = open(This->joydev->device, O_RDONLY)) == -1)
632         {
633             /* Couldn't open the device at all */
634             ERR("Failed to open device %s: %d %s\n", This->joydev->device, errno, strerror(errno));
635             IDirectInputDevice2AImpl_Unacquire(iface);
636             return DIERR_NOTFOUND;
637         }
638         else
639         {
640             /* Couldn't open in r/w but opened in read-only. */
641             WARN("Could not open %s in read-write mode.  Force feedback will be disabled.\n", This->joydev->device);
642         }
643     }
644     else
645     {
646         struct input_event event;
647
648         event.type = EV_FF;
649         event.code = FF_GAIN;
650         event.value = This->ff_gain;
651         if (write(This->joyfd, &event, sizeof(event)) == -1)
652             ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
653         if (!This->ff_autocenter)
654         {
655             /* Disable autocenter. */
656             event.code = FF_AUTOCENTER;
657             event.value = 0;
658             if (write(This->joyfd, &event, sizeof(event)) == -1)
659                 ERR("Failed disabling autocenter: %d %s\n", errno, strerror(errno));
660         }
661     }
662
663     return DI_OK;
664 }
665
666 /******************************************************************************
667   *     Unacquire : frees the joystick
668   */
669 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
670 {
671     JoystickImpl *This = (JoystickImpl *)iface;
672     HRESULT res;
673
674     TRACE("(this=%p)\n",This);
675     res = IDirectInputDevice2AImpl_Unacquire(iface);
676     if (res==DI_OK && This->joyfd!=-1) {
677       effect_list_item *itr;
678       struct input_event event;
679
680       /* For each known effect:
681        * - stop it
682        * - unload it
683        * But, unlike DISFFC_RESET, do not release the effect.
684        */
685       LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry) {
686           IDirectInputEffect_Stop(itr->ref);
687           IDirectInputEffect_Unload(itr->ref);
688       }
689
690       /* Enable autocenter. */
691       event.type = EV_FF;
692       event.code = FF_AUTOCENTER;
693       /* TODO: Read autocenter strengh before disabling it, and use it here
694        * instead of 0xFFFF (maximum strengh).
695        */
696       event.value = 0xFFFF;
697       if (write(This->joyfd, &event, sizeof(event)) == -1)
698         ERR("Failed to set autocenter to %04x: %d %s\n", event.value, errno, strerror(errno));
699
700       close(This->joyfd);
701       This->joyfd = -1;
702     }
703     return res;
704 }
705
706 /* 
707  * set the current state of the js device as it would be with the middle
708  * values on the axes
709  */
710 #define CENTER_AXIS(a) \
711     (ji->dev_axes_to_di[a] == -1 ? 0 : joystick_map_axis( &ji->generic.props[ji->dev_axes_to_di[a]], \
712                                                           ji->joydev->axes[a].value ))
713 static void fake_current_js_state(JoystickImpl *ji)
714 {
715     int i;
716
717     /* center the axes */
718     ji->generic.js.lX           = CENTER_AXIS(ABS_X);
719     ji->generic.js.lY           = CENTER_AXIS(ABS_Y);
720     ji->generic.js.lZ           = CENTER_AXIS(ABS_Z);
721     ji->generic.js.lRx          = CENTER_AXIS(ABS_RX);
722     ji->generic.js.lRy          = CENTER_AXIS(ABS_RY);
723     ji->generic.js.lRz          = CENTER_AXIS(ABS_RZ);
724     ji->generic.js.rglSlider[0] = CENTER_AXIS(ABS_THROTTLE);
725     ji->generic.js.rglSlider[1] = CENTER_AXIS(ABS_RUDDER);
726
727     /* POV center is -1 */
728     for (i = 0; i < 4; i++)
729         ji->generic.js.rgdwPOV[i] = -1;
730 }
731 #undef CENTER_AXIS
732
733 /* convert wine format offset to user format object index */
734 static void joy_polldev(JoystickGenericImpl *iface)
735 {
736     struct pollfd plfd;
737     struct input_event ie;
738     JoystickImpl *This = (JoystickImpl*) iface;
739
740     if (This->joyfd==-1)
741         return;
742
743     while (1)
744     {
745         LONG value = 0;
746         int inst_id = -1;
747
748         plfd.fd = This->joyfd;
749         plfd.events = POLLIN;
750
751         if (poll(&plfd,1,0) != 1)
752             return;
753
754         /* we have one event, so we can read */
755         if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
756             return;
757
758         TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
759         switch (ie.type) {
760         case EV_KEY:    /* button */
761         {
762             int btn = This->buttons[ie.code];
763
764             TRACE("(%p) %d -> %d\n", This, ie.code, btn);
765             if (btn & 0x80)
766             {
767                 btn &= 0x7F;
768                 inst_id = DIDFT_MAKEINSTANCE(btn) | DIDFT_PSHBUTTON;
769                 This->generic.js.rgbButtons[btn] = value = ie.value ? 0x80 : 0x00;
770             }
771             break;
772         }
773         case EV_ABS:
774         {
775             int axis = This->dev_axes_to_di[ie.code];
776
777             /* User axis remapping */
778             if (axis < 0) break;
779             axis = This->generic.axis_map[axis];
780             if (axis < 0) break;
781
782             inst_id = axis < 8 ?  DIDFT_MAKEINSTANCE(axis) | DIDFT_ABSAXIS :
783                                   DIDFT_MAKEINSTANCE(axis - 8) | DIDFT_POV;
784             value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], ie.value);
785
786             switch (axis) {
787             case 0: This->generic.js.lX  = value; break;
788             case 1: This->generic.js.lY  = value; break;
789             case 2: This->generic.js.lZ  = value; break;
790             case 3: This->generic.js.lRx = value; break;
791             case 4: This->generic.js.lRy = value; break;
792             case 5: This->generic.js.lRz = value; break;
793             case 6: This->generic.js.rglSlider[0] = value; break;
794             case 7: This->generic.js.rglSlider[1] = value; break;
795             case 8: case 9: case 10: case 11:
796             {
797                 int idx = axis - 8;
798
799                 if (ie.code % 2)
800                     This->povs[idx].y = ie.value;
801                 else
802                     This->povs[idx].x = ie.value;
803
804                 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
805                 break;
806             }
807             default:
808                 FIXME("unhandled joystick axis event (code %d, value %d)\n",ie.code,ie.value);
809             }
810             break;
811         }
812 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
813         case EV_FF_STATUS:
814             This->ff_state = ie.value;
815             break;
816 #endif
817 #ifdef EV_SYN
818         case EV_SYN:
819             /* there is nothing to do */
820             break;
821 #endif
822         default:
823             FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
824             break;
825         }
826         if (inst_id >= 0)
827             queue_event((LPDIRECTINPUTDEVICE8A)This,
828                         id_to_offset(&This->generic.base.data_format, inst_id),
829                         value, ie.time.tv_usec, This->generic.base.dinput->evsequence++);
830     }
831 }
832
833 /******************************************************************************
834   *     SetProperty : change input device properties
835   */
836 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
837                                             REFGUID rguid,
838                                             LPCDIPROPHEADER ph)
839 {
840   JoystickImpl *This = (JoystickImpl *)iface;
841
842   if (!ph) {
843     WARN("invalid argument\n");
844     return DIERR_INVALIDPARAM;
845   }
846
847   TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
848   TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
849         ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
850
851   if (!HIWORD(rguid)) {
852     switch (LOWORD(rguid)) {
853     case (DWORD_PTR)DIPROP_CALIBRATIONMODE: {
854       LPCDIPROPDWORD    pd = (LPCDIPROPDWORD)ph;
855       FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
856       break;
857     }
858     case (DWORD_PTR)DIPROP_AUTOCENTER: {
859       LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
860
861       TRACE("autocenter(%d)\n", pd->dwData);
862       This->ff_autocenter = pd->dwData == DIPROPAUTOCENTER_ON;
863
864       break;
865     }
866     case (DWORD_PTR)DIPROP_FFGAIN: {
867       LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
868
869       TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
870       This->ff_gain = MulDiv(pd->dwData, 0xFFFF, 10000);
871       if (This->generic.base.acquired) {
872         /* Update immediately. */
873         struct input_event event;
874
875         event.type = EV_FF;
876         event.code = FF_GAIN;
877         event.value = This->ff_gain;
878         if (write(This->joyfd, &event, sizeof(event)) == -1)
879           ERR("Failed to set gain (%i): %d %s\n", This->ff_gain, errno, strerror(errno));
880       }
881       break;
882     }
883     default:
884       return JoystickAGenericImpl_SetProperty(iface, rguid, ph);
885     }
886   }
887   return DI_OK;
888 }
889
890 /******************************************************************************
891   *     GetProperty : get input device properties
892   */
893 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
894                                                 REFGUID rguid,
895                                                 LPDIPROPHEADER pdiph)
896 {
897     JoystickImpl *This = (JoystickImpl *)iface;
898
899     TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
900     _dump_DIPROPHEADER(pdiph);
901
902     if (HIWORD(rguid)) return DI_OK;
903
904     switch (LOWORD(rguid)) {
905     case (DWORD_PTR) DIPROP_AUTOCENTER:
906     {
907         LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
908
909         pd->dwData = This->ff_autocenter ? DIPROPAUTOCENTER_ON : DIPROPAUTOCENTER_OFF;
910         TRACE("autocenter(%d)\n", pd->dwData);
911         break;
912     }
913     case (DWORD_PTR) DIPROP_FFGAIN:
914     {
915         LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
916
917         pd->dwData = MulDiv(This->ff_gain, 10000, 0xFFFF);
918         TRACE("DIPROP_FFGAIN(%d)\n", pd->dwData);
919         break;
920     }
921
922     default:
923         return JoystickAGenericImpl_GetProperty(iface, rguid, pdiph);
924     }
925
926     return DI_OK;
927 }
928
929 /****************************************************************************** 
930   *     CreateEffect - Create a new FF effect with the specified params
931   */
932 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface,
933                                                  REFGUID rguid,
934                                                  LPCDIEFFECT lpeff,
935                                                  LPDIRECTINPUTEFFECT *ppdef,
936                                                  LPUNKNOWN pUnkOuter)
937 {
938 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
939     effect_list_item* new_effect = NULL;
940     HRESULT retval = DI_OK;
941 #endif
942
943     JoystickImpl* This = (JoystickImpl*)iface;
944     TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
945
946 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
947     TRACE("not available (compiled w/o ff support)\n");
948     *ppdef = NULL;
949     return DI_OK;
950 #else
951
952     if (!(new_effect = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_effect))))
953         return DIERR_OUTOFMEMORY;
954
955     retval = linuxinput_create_effect(&This->joyfd, rguid, &new_effect->entry, &new_effect->ref);
956     if (retval != DI_OK)
957     {
958         HeapFree(GetProcessHeap(), 0, new_effect);
959         return retval;
960     }
961
962     if (lpeff != NULL)
963     {
964         retval = IDirectInputEffect_SetParameters(new_effect->ref, lpeff, 0);
965
966         if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
967         {
968             HeapFree(GetProcessHeap(), 0, new_effect);
969             return retval;
970         }
971     }
972
973     list_add_tail(&This->ff_effects, &new_effect->entry);
974     *ppdef = new_effect->ref;
975
976     if (pUnkOuter != NULL)
977         FIXME("Interface aggregation not implemented.\n");
978
979     return DI_OK;
980
981 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
982
983
984 /*******************************************************************************
985  *      EnumEffects - Enumerate available FF effects
986  */
987 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
988                                                 LPDIENUMEFFECTSCALLBACKA lpCallback,
989                                                 LPVOID pvRef,
990                                                 DWORD dwEffType)
991 {
992 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
993     DIEFFECTINFOA dei; /* feif */
994     DWORD type = DIEFT_GETTYPE(dwEffType);
995     JoystickImpl* This = (JoystickImpl*)iface;
996
997     TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
998
999     dei.dwSize = sizeof(DIEFFECTINFOA);          
1000
1001     if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1002         && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1003         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1004         (*lpCallback)(&dei, pvRef);
1005     }
1006
1007     if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1008         && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1009         if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1010             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1011             (*lpCallback)(&dei, pvRef);
1012         }
1013         if (test_bit(This->joydev->ffbits, FF_SINE)) {
1014             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1015             (*lpCallback)(&dei, pvRef);
1016         }
1017         if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1018             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1019             (*lpCallback)(&dei, pvRef);
1020         }
1021         if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1022             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1023             (*lpCallback)(&dei, pvRef);
1024         }
1025         if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1026             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1027             (*lpCallback)(&dei, pvRef);
1028         }
1029     } 
1030
1031     if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1032         && test_bit(This->joydev->ffbits, FF_RAMP)) {
1033         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1034         (*lpCallback)(&dei, pvRef);
1035     }
1036
1037     if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1038         if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1039             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1040             (*lpCallback)(&dei, pvRef);
1041         }
1042         if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1043             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1044             (*lpCallback)(&dei, pvRef);
1045         }
1046         if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1047             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1048             (*lpCallback)(&dei, pvRef);
1049         }
1050         if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1051             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1052             (*lpCallback)(&dei, pvRef);
1053         }
1054     }
1055
1056 #endif
1057
1058     return DI_OK;
1059 }
1060
1061 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1062                                                 LPDIENUMEFFECTSCALLBACKW lpCallback,
1063                                                 LPVOID pvRef,
1064                                                 DWORD dwEffType)
1065 {
1066 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1067     /* seems silly to duplicate all this code but all the structures and functions
1068      * are actually different (A/W) */
1069     DIEFFECTINFOW dei; /* feif */
1070     DWORD type = DIEFT_GETTYPE(dwEffType);
1071     JoystickImpl* This = (JoystickImpl*)iface;
1072     int xfd = This->joyfd;
1073
1074     TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1075
1076     dei.dwSize = sizeof(DIEFFECTINFOW);          
1077
1078     if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1079         && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1080         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1081         (*lpCallback)(&dei, pvRef);
1082     }
1083
1084     if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1085         && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1086         if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1087             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1088             (*lpCallback)(&dei, pvRef);
1089         }
1090         if (test_bit(This->joydev->ffbits, FF_SINE)) {
1091             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1092             (*lpCallback)(&dei, pvRef);
1093         }
1094         if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1095             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1096             (*lpCallback)(&dei, pvRef);
1097         }
1098         if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1099             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1100             (*lpCallback)(&dei, pvRef);
1101         }
1102         if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1103             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1104             (*lpCallback)(&dei, pvRef);
1105         }
1106     } 
1107
1108     if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1109         && test_bit(This->joydev->ffbits, FF_RAMP)) {
1110         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1111         (*lpCallback)(&dei, pvRef);
1112     }
1113
1114     if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1115         if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1116             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1117             (*lpCallback)(&dei, pvRef);
1118         }
1119         if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1120             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1121             (*lpCallback)(&dei, pvRef);
1122         }
1123         if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1124             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1125             (*lpCallback)(&dei, pvRef);
1126         }
1127         if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1128             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1129             (*lpCallback)(&dei, pvRef);
1130         }
1131     }
1132
1133     /* return to unacquired state if that's where it was */
1134     if (xfd == -1)
1135         IDirectInputDevice8_Unacquire(iface);
1136 #endif
1137
1138     return DI_OK;
1139 }
1140
1141 /*******************************************************************************
1142  *      GetEffectInfo - Get information about a particular effect 
1143  */
1144 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1145                                                   LPDIEFFECTINFOA pdei,
1146                                                   REFGUID guid)
1147 {
1148     JoystickImpl* This = (JoystickImpl*)iface;
1149
1150     TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1151
1152 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1153     return linuxinput_get_info_A(This->joyfd, guid, pdei); 
1154 #else
1155     return DI_OK;
1156 #endif
1157 }
1158
1159 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1160                                                   LPDIEFFECTINFOW pdei,
1161                                                   REFGUID guid)
1162 {
1163     JoystickImpl* This = (JoystickImpl*)iface;
1164             
1165     TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1166         
1167 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1168     return linuxinput_get_info_W(This->joyfd, guid, pdei);
1169 #else
1170     return DI_OK;
1171 #endif
1172 }
1173
1174 /*******************************************************************************
1175  *      GetForceFeedbackState - Get information about the device's FF state 
1176  */
1177 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(
1178         LPDIRECTINPUTDEVICE8A iface,
1179         LPDWORD pdwOut)
1180 {
1181     JoystickImpl* This = (JoystickImpl*)iface;
1182
1183     TRACE("(this=%p,%p)\n", This, pdwOut);
1184
1185     (*pdwOut) = 0;
1186
1187 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1188     /* DIGFFS_STOPPED is the only mandatory flag to report */
1189     if (This->ff_state == FF_STATUS_STOPPED)
1190         (*pdwOut) |= DIGFFS_STOPPED;
1191 #endif
1192
1193     return DI_OK;
1194 }
1195
1196 /*******************************************************************************
1197  *      SendForceFeedbackCommand - Send a command to the device's FF system
1198  */
1199 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(
1200         LPDIRECTINPUTDEVICE8A iface,
1201         DWORD dwFlags)
1202 {
1203     JoystickImpl* This = (JoystickImpl*)iface;
1204     TRACE("(this=%p,%d)\n", This, dwFlags);
1205
1206 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1207     switch (dwFlags)
1208     {
1209     case DISFFC_STOPALL:
1210     {
1211         /* Stop all effects */
1212         effect_list_item *itr;
1213
1214         LIST_FOR_EACH_ENTRY(itr, &This->ff_effects, effect_list_item, entry)
1215             IDirectInputEffect_Stop(itr->ref);
1216         break;
1217     }
1218
1219     case DISFFC_RESET:
1220     {
1221         effect_list_item *itr, *ptr;
1222
1223         /* Stop, unload, release and free all effects */
1224         /* This returns the device to its "bare" state */
1225         LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1226             IDirectInputEffect_Release(itr->ref);
1227         break;
1228     }
1229     case DISFFC_PAUSE:
1230     case DISFFC_CONTINUE:
1231         FIXME("No support for Pause or Continue in linux\n");
1232         break;
1233
1234     case DISFFC_SETACTUATORSOFF:
1235     case DISFFC_SETACTUATORSON:
1236         FIXME("No direct actuator control in linux\n");
1237         break;
1238
1239     default:
1240         FIXME("Unknown Force Feedback Command!\n");
1241         return DIERR_INVALIDPARAM;
1242     }
1243     return DI_OK;
1244 #else
1245     return DIERR_UNSUPPORTED;
1246 #endif
1247 }
1248
1249 /*******************************************************************************
1250  *      EnumCreatedEffectObjects - Enumerate all the effects that have been
1251  *              created for this device.
1252  */
1253 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(
1254         LPDIRECTINPUTDEVICE8A iface,
1255         LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1256         LPVOID pvRef,
1257         DWORD dwFlags)
1258 {
1259     /* this function is safe to call on non-ff-enabled builds */
1260     JoystickImpl* This = (JoystickImpl*)iface;
1261     effect_list_item *itr, *ptr;
1262
1263     TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1264
1265     if (!lpCallback)
1266         return DIERR_INVALIDPARAM;
1267
1268     if (dwFlags != 0)
1269         FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1270
1271     LIST_FOR_EACH_ENTRY_SAFE(itr, ptr, &This->ff_effects, effect_list_item, entry)
1272         (*lpCallback)(itr->ref, pvRef);
1273
1274     return DI_OK;
1275 }
1276
1277 /******************************************************************************
1278   *     GetDeviceInfo : get information about a device's identity
1279   */
1280 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1281                                                   LPDIDEVICEINSTANCEA pdidi)
1282 {
1283     JoystickImpl *This = (JoystickImpl *)iface;
1284
1285     TRACE("(%p) %p\n", This, pdidi);
1286
1287     if (pdidi == NULL) return E_POINTER;
1288     if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1289         (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1290         return DIERR_INVALIDPARAM;
1291
1292     fill_joystick_dideviceinstanceA(pdidi, This->generic.base.dinput->dwVersion,
1293                                     get_joystick_index(&This->generic.base.guid));
1294     return DI_OK;
1295 }
1296
1297 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1298                                                   LPDIDEVICEINSTANCEW pdidi)
1299 {
1300     JoystickImpl *This = (JoystickImpl *)iface;
1301
1302     TRACE("(%p) %p\n", This, pdidi);
1303
1304     if (pdidi == NULL) return E_POINTER;
1305     if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1306         (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1307         return DIERR_INVALIDPARAM;
1308
1309     fill_joystick_dideviceinstanceW(pdidi, This->generic.base.dinput->dwVersion,
1310                                     get_joystick_index(&This->generic.base.guid));
1311     return DI_OK;
1312 }
1313
1314 static const IDirectInputDevice8AVtbl JoystickAvt =
1315 {
1316         IDirectInputDevice2AImpl_QueryInterface,
1317         IDirectInputDevice2AImpl_AddRef,
1318         IDirectInputDevice2AImpl_Release,
1319         JoystickAGenericImpl_GetCapabilities,
1320         IDirectInputDevice2AImpl_EnumObjects,
1321         JoystickAImpl_GetProperty,
1322         JoystickAImpl_SetProperty,
1323         JoystickAImpl_Acquire,
1324         JoystickAImpl_Unacquire,
1325         JoystickAGenericImpl_GetDeviceState,
1326         IDirectInputDevice2AImpl_GetDeviceData,
1327         IDirectInputDevice2AImpl_SetDataFormat,
1328         IDirectInputDevice2AImpl_SetEventNotification,
1329         IDirectInputDevice2AImpl_SetCooperativeLevel,
1330         JoystickAGenericImpl_GetObjectInfo,
1331         JoystickAImpl_GetDeviceInfo,
1332         IDirectInputDevice2AImpl_RunControlPanel,
1333         IDirectInputDevice2AImpl_Initialize,
1334         JoystickAImpl_CreateEffect,
1335         JoystickAImpl_EnumEffects,
1336         JoystickAImpl_GetEffectInfo,
1337         JoystickAImpl_GetForceFeedbackState,
1338         JoystickAImpl_SendForceFeedbackCommand,
1339         JoystickAImpl_EnumCreatedEffectObjects,
1340         IDirectInputDevice2AImpl_Escape,
1341         JoystickAGenericImpl_Poll,
1342         IDirectInputDevice2AImpl_SendDeviceData,
1343         IDirectInputDevice7AImpl_EnumEffectsInFile,
1344         IDirectInputDevice7AImpl_WriteEffectToFile,
1345         IDirectInputDevice8AImpl_BuildActionMap,
1346         IDirectInputDevice8AImpl_SetActionMap,
1347         IDirectInputDevice8AImpl_GetImageInfo
1348 };
1349
1350 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1351 # define XCAST(fun)     (typeof(JoystickWvt.fun))
1352 #else
1353 # define XCAST(fun)     (void*)
1354 #endif
1355
1356 static const IDirectInputDevice8WVtbl JoystickWvt =
1357 {
1358         IDirectInputDevice2WImpl_QueryInterface,
1359         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1360         XCAST(Release)IDirectInputDevice2AImpl_Release,
1361         XCAST(GetCapabilities)JoystickAGenericImpl_GetCapabilities,
1362         IDirectInputDevice2WImpl_EnumObjects,
1363         XCAST(GetProperty)JoystickAImpl_GetProperty,
1364         XCAST(SetProperty)JoystickAImpl_SetProperty,
1365         XCAST(Acquire)JoystickAImpl_Acquire,
1366         XCAST(Unacquire)JoystickAImpl_Unacquire,
1367         XCAST(GetDeviceState)JoystickAGenericImpl_GetDeviceState,
1368         XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1369         XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1370         XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1371         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1372         JoystickWGenericImpl_GetObjectInfo,
1373         JoystickWImpl_GetDeviceInfo,
1374         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1375         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1376         XCAST(CreateEffect)JoystickAImpl_CreateEffect,
1377         JoystickWImpl_EnumEffects,
1378         JoystickWImpl_GetEffectInfo,
1379         XCAST(GetForceFeedbackState)JoystickAImpl_GetForceFeedbackState,
1380         XCAST(SendForceFeedbackCommand)JoystickAImpl_SendForceFeedbackCommand,
1381         XCAST(EnumCreatedEffectObjects)JoystickAImpl_EnumCreatedEffectObjects,
1382         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1383         XCAST(Poll)JoystickAGenericImpl_Poll,
1384         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1385         IDirectInputDevice7WImpl_EnumEffectsInFile,
1386         IDirectInputDevice7WImpl_WriteEffectToFile,
1387         IDirectInputDevice8WImpl_BuildActionMap,
1388         IDirectInputDevice8WImpl_SetActionMap,
1389         IDirectInputDevice8WImpl_GetImageInfo
1390 };
1391 #undef XCAST
1392
1393 #else  /* HAVE_CORRECT_LINUXINPUT_H */
1394
1395 const struct dinput_device joystick_linuxinput_device = {
1396   "Wine Linux-input joystick driver",
1397   NULL,
1398   NULL,
1399   NULL,
1400   NULL
1401 };
1402
1403 #endif  /* HAVE_CORRECT_LINUXINPUT_H */