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