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