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