dinput: Move joystick user data format into base class.
[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 <sys/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 # if defined(EVIOCGBIT) && defined(EV_ABS) && defined(BTN_PINKIE)
48 #  define HAVE_CORRECT_LINUXINPUT_H
49 # endif
50 #endif
51 #ifdef HAVE_SYS_POLL_H
52 # include <sys/poll.h>
53 #endif
54
55 #include "wine/debug.h"
56 #include "wine/unicode.h"
57 #include "windef.h"
58 #include "winbase.h"
59 #include "winerror.h"
60 #include "dinput.h"
61
62 #include "dinput_private.h"
63 #include "device_private.h"
64
65 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
66
67 #ifdef HAVE_CORRECT_LINUXINPUT_H
68
69 #define EVDEVPREFIX     "/dev/input/event"
70
71 /* Wine joystick driver object instances */
72 #define WINE_JOYSTICK_AXIS_BASE   0
73 #define WINE_JOYSTICK_POV_BASE    6
74 #define WINE_JOYSTICK_BUTTON_BASE 8
75
76 typedef struct EffectListItem EffectListItem;
77 struct EffectListItem
78 {
79         LPDIRECTINPUTEFFECT ref;
80         struct EffectListItem* next;
81 };
82
83 /* implemented in effect_linuxinput.c */
84 HRESULT linuxinput_create_effect(int* fd, REFGUID rguid, LPDIRECTINPUTEFFECT* peff);
85 HRESULT linuxinput_get_info_A(int fd, REFGUID rguid, LPDIEFFECTINFOA info);
86 HRESULT linuxinput_get_info_W(int fd, REFGUID rguid, LPDIEFFECTINFOW info);
87
88 typedef struct JoystickImpl JoystickImpl;
89 static const IDirectInputDevice8AVtbl JoystickAvt;
90 static const IDirectInputDevice8WVtbl JoystickWvt;
91
92 struct JoyDev {
93         char *device;
94         char *name;
95         GUID guid;
96
97         int has_ff;
98         int num_effects;
99
100         /* data returned by EVIOCGBIT for caps, EV_ABS, EV_KEY, and EV_FF */
101         BYTE                            evbits[(EV_MAX+7)/8];
102         BYTE                            absbits[(ABS_MAX+7)/8];
103         BYTE                            keybits[(KEY_MAX+7)/8];
104         BYTE                            ffbits[(FF_MAX+7)/8];   
105
106 #define AXE_ABS         0
107 #define AXE_ABSMIN      1
108 #define AXE_ABSMAX      2
109 #define AXE_ABSFUZZ     3
110 #define AXE_ABSFLAT     4
111
112         /* data returned by the EVIOCGABS() ioctl */
113         int                             axes[ABS_MAX][5];
114         /* LUT for KEY_ to offset in rgbButtons */
115         BYTE                            buttons[KEY_MAX];
116
117         /* autodetecting ranges per axe by following movement */
118         LONG                            havemax[ABS_MAX];
119         LONG                            havemin[ABS_MAX];
120 };
121
122 struct JoystickImpl
123 {
124         struct IDirectInputDevice2AImpl base;
125
126         struct JoyDev                  *joydev;
127
128         /* The 'parent' DInput */
129         IDirectInputImpl               *dinput;
130
131         /* joystick private */
132         /* what range and deadzone the game wants */
133         LONG                            wantmin[ABS_MAX];
134         LONG                            wantmax[ABS_MAX];
135         LONG                            deadz[ABS_MAX];
136
137         int                             joyfd;
138
139         DIJOYSTATE2                     js;
140
141         /* Force feedback variables */
142         EffectListItem*                 top_effect;
143         int                             ff_state;
144 };
145
146 static void fake_current_js_state(JoystickImpl *ji);
147 static DWORD map_pov(int event_value, int is_x);
148 static void find_joydevs(void);
149 static int lxinput_to_user_offset(JoystickImpl *This, int ie_type, int ie_code);
150
151 /* This GUID is slightly different from the linux joystick one. Take note. */
152 static const GUID DInput_Wine_Joystick_Base_GUID = { /* 9e573eda-7734-11d2-8d4a-23903fb6bdf7 */
153   0x9e573eda,
154   0x7734,
155   0x11d2,
156   {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
157 };
158
159 #define test_bit(arr,bit) (((BYTE*)(arr))[(bit)>>3]&(1<<((bit)&7)))
160
161 #define MAX_JOYDEV 64
162
163 static int have_joydevs = -1;
164 static struct JoyDev *joydevs = NULL;
165
166 static void find_joydevs(void)
167 {
168   int i;
169
170   if (have_joydevs!=-1) {
171     return;
172   }
173
174   have_joydevs = 0;
175
176   for (i=0;i<MAX_JOYDEV;i++) {
177     char        buf[MAX_PATH];
178     struct JoyDev joydev = {0};
179     int fd;
180     int no_ff_check = 0;
181     int j, buttons;
182
183     snprintf(buf,MAX_PATH,EVDEVPREFIX"%d",i);
184     buf[MAX_PATH-1] = 0;
185
186     if ((fd=open(buf, O_RDWR))==-1) {
187       fd = open(buf, O_RDONLY);
188       no_ff_check = 1;
189     }
190
191     if (fd!=-1) {
192       if ((-1==ioctl(fd,EVIOCGBIT(0,sizeof(joydev.evbits)),joydev.evbits))) {
193         perror("EVIOCGBIT 0");
194         close(fd);
195         continue; 
196       }
197       if (-1==ioctl(fd,EVIOCGBIT(EV_ABS,sizeof(joydev.absbits)),joydev.absbits)) {
198         perror("EVIOCGBIT EV_ABS");
199         close(fd);
200         continue;
201       }
202       if (-1==ioctl(fd,EVIOCGBIT(EV_KEY,sizeof(joydev.keybits)),joydev.keybits)) {
203         perror("EVIOCGBIT EV_KEY");
204         close(fd);
205         continue;
206       }
207       /* A true joystick has at least axis X and Y, and at least 1
208        * button. copied from linux/drivers/input/joydev.c */
209       if (test_bit(joydev.absbits,ABS_X) && test_bit(joydev.absbits,ABS_Y) &&
210           (   test_bit(joydev.keybits,BTN_TRIGGER)      ||
211               test_bit(joydev.keybits,BTN_A)    ||
212               test_bit(joydev.keybits,BTN_1)
213           )
214          ) {
215
216         joydev.device = strdup(buf);
217
218         if (-1!=ioctl(fd, EVIOCGNAME(MAX_PATH-1), buf)) {
219           buf[MAX_PATH-1] = 0;
220           joydev.name = strdup(buf);
221         } else {
222           joydev.name = joydev.device;
223         }
224
225         joydev.guid = DInput_Wine_Joystick_Base_GUID;
226         joydev.guid.Data3 += have_joydevs;
227
228         TRACE("Found a joystick on %s: %s (%s)\n", 
229             joydev.device, joydev.name, 
230             debugstr_guid(&joydev.guid)
231             );
232
233 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
234         if (!no_ff_check) {
235           if ((!test_bit(joydev.evbits,EV_FF))
236               || (-1==ioctl(fd,EVIOCGBIT(EV_FF,sizeof(joydev.ffbits)),joydev.ffbits)) 
237               || (-1==ioctl(fd,EVIOCGEFFECTS,&joydev.num_effects))
238               || (joydev.num_effects <= 0)) {
239             close(fd);
240           } else {
241             TRACE(" ... with force feedback\n");
242             joydev.has_ff = 1;
243           }
244         }
245 #endif
246
247         for (j=0;j<ABS_MAX;j++) {
248           if (test_bit(joydev.absbits,j)) {
249             if (-1!=ioctl(fd,EVIOCGABS(j),&(joydev.axes[j]))) {
250               TRACE(" ... with axe %d: cur=%d, min=%d, max=%d, fuzz=%d, flat=%d\n",
251                   j,
252                   joydev.axes[j][AXE_ABS],
253                   joydev.axes[j][AXE_ABSMIN],
254                   joydev.axes[j][AXE_ABSMAX],
255                   joydev.axes[j][AXE_ABSFUZZ],
256                   joydev.axes[j][AXE_ABSFLAT]
257                   );
258               joydev.havemin[j] = joydev.axes[j][AXE_ABSMIN];
259               joydev.havemax[j] = joydev.axes[j][AXE_ABSMAX];
260             }
261           }
262         }
263
264         buttons = 0;
265         for (j=0;j<KEY_MAX;j++) {
266           if (test_bit(joydev.keybits,j)) {
267             TRACE(" ... with button %d: %d\n", j, buttons);
268             joydev.buttons[j] = 0x80 | buttons;
269             buttons++;
270           }
271         }
272
273         if (have_joydevs==0) {
274           joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
275         } else {
276           HeapReAlloc(GetProcessHeap(), 0, joydevs, (1+have_joydevs) * sizeof(struct JoyDev));
277         }
278         memcpy(joydevs+have_joydevs, &joydev, sizeof(struct JoyDev));
279         have_joydevs++;
280       }
281
282       close(fd);
283     }
284   }
285 }
286
287 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
288 {
289   find_joydevs();
290
291   if (id >= have_joydevs) {
292     return FALSE;
293   }
294  
295   if (!((dwDevType == 0) ||
296         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version < 0x0800)) ||
297         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
298     return FALSE;
299
300 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
301   if (dwFlags & DIEDFL_FORCEFEEDBACK)
302     return FALSE;
303 #endif
304
305   if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
306     lpddi->guidInstance = joydevs[id].guid;
307     lpddi->guidProduct  = DInput_Wine_Joystick_Base_GUID;
308
309     lpddi->guidFFDriver = GUID_NULL;
310     if (version >= 0x0800)
311       lpddi->dwDevType    = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
312     else
313       lpddi->dwDevType    = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
314
315     strcpy(lpddi->tszInstanceName, joydevs[id].name);
316     strcpy(lpddi->tszProductName, joydevs[id].device);
317     return TRUE;
318   }
319   return FALSE;
320 }
321
322 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
323 {
324   find_joydevs();
325
326   if (id >= have_joydevs) {
327     return FALSE;
328   }
329
330   if (!((dwDevType == 0) ||
331         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version < 0x0800)) ||
332         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))))
333     return FALSE;
334
335 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
336   if (dwFlags & DIEDFL_FORCEFEEDBACK)
337     return FALSE;
338 #endif
339
340   if (!(dwFlags & DIEDFL_FORCEFEEDBACK) || joydevs[id].has_ff) {
341     lpddi->guidInstance = joydevs[id].guid;
342     lpddi->guidProduct  = DInput_Wine_Joystick_Base_GUID;
343
344     lpddi->guidFFDriver = GUID_NULL;
345     if (version >= 0x0800)
346       lpddi->dwDevType    = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
347     else
348       lpddi->dwDevType    = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
349
350     MultiByteToWideChar(CP_ACP, 0, joydevs[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
351     MultiByteToWideChar(CP_ACP, 0, joydevs[id].device, -1, lpddi->tszProductName, MAX_PATH);
352     return TRUE;
353   }
354   return FALSE;
355 }
356
357 static JoystickImpl *alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, struct JoyDev *joydev)
358 {
359   JoystickImpl* newDevice;
360   int i;
361
362   newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
363   if (newDevice==NULL) {
364     return NULL;
365   }
366
367   newDevice->base.lpVtbl = jvt;
368   newDevice->base.ref = 1;
369   memcpy(&newDevice->base.guid, rguid, sizeof(*rguid));
370   InitializeCriticalSection(&newDevice->base.crit);
371   newDevice->joyfd = -1;
372   newDevice->dinput = dinput;
373   newDevice->joydev = joydev;
374 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
375   newDevice->ff_state = FF_STATUS_STOPPED;
376 #endif
377   for (i=0;i<ABS_MAX;i++) {
378     /* apps expect the range to be the same they would get from the
379      * GetProperty/range method */
380     newDevice->wantmin[i] = newDevice->joydev->havemin[i];
381     newDevice->wantmax[i] = newDevice->joydev->havemax[i];
382     /* TODO: 
383      * direct input defines a default for the deadzone somewhere; but as long
384      * as in map_axis the code for the dead zone is commented out its no
385      * problem
386      */
387     newDevice->deadz[i]   =  0;
388   }
389   fake_current_js_state(newDevice);
390
391   /* wine uses DIJOYSTATE2 as it's internal format */
392   newDevice->base.data_format.wine_df = &c_dfDIJoystick2;
393
394   /* create the default transform filter */
395   if (create_DataFormat(&c_dfDIJoystick2, &c_dfDIJoystick2, &newDevice->base.data_format) == DI_OK)
396     return newDevice;
397
398   HeapFree(GetProcessHeap(),0,newDevice);
399   return NULL;
400 }
401
402 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
403 {
404   int i;
405
406   find_joydevs();
407
408   for (i=0; i<have_joydevs; i++) {
409     if (IsEqualGUID(&GUID_Joystick,rguid) ||
410         IsEqualGUID(&joydevs[i].guid,rguid)
411         ) {
412       if ((riid == NULL) ||
413           IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
414           IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
415           IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
416           IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
417         *pdev = (IDirectInputDeviceA*) alloc_device(rguid, &JoystickAvt, dinput, &joydevs[i]);
418         TRACE("Creating a Joystick device (%p)\n", *pdev);
419         if (*pdev==0) {
420           ERR("out of memory\n");
421           return DIERR_OUTOFMEMORY;
422         }
423         return DI_OK;
424       } else {
425         return DIERR_NOINTERFACE;
426       }
427     }
428   }
429
430   return DIERR_DEVICENOTREG;
431 }
432
433
434 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
435 {
436   int i;
437
438   find_joydevs();
439
440   for (i=0; i<have_joydevs; i++) {
441     if (IsEqualGUID(&GUID_Joystick,rguid) ||
442         IsEqualGUID(&joydevs[i].guid,rguid)
443         ) {
444       if ((riid == NULL) ||
445           IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
446           IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
447           IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
448           IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
449         *pdev = (IDirectInputDeviceW*) alloc_device(rguid, &JoystickWvt, dinput, &joydevs[i]);
450         TRACE("Creating a Joystick device (%p)\n", *pdev);
451         if (*pdev==0) {
452           ERR("out of memory\n");
453           return DIERR_OUTOFMEMORY;
454         }
455         return DI_OK;
456       } else {
457         return DIERR_NOINTERFACE;
458       }
459     }
460   }
461
462   return DIERR_DEVICENOTREG;
463 }
464
465 const struct dinput_device joystick_linuxinput_device = {
466   "Wine Linux-input joystick driver",
467   joydev_enum_deviceA,
468   joydev_enum_deviceW,
469   joydev_create_deviceA,
470   joydev_create_deviceW
471 };
472
473 /******************************************************************************
474  *      Joystick
475  */
476 static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
477 {
478         JoystickImpl *This = (JoystickImpl *)iface;
479         ULONG ref;
480
481         ref = InterlockedDecrement(&This->base.ref);
482         if (ref)
483                 return ref;
484
485         /* Reset the FF state, free all effects, etc */
486         IDirectInputDevice8_SendForceFeedbackCommand(iface, DISFFC_RESET);
487
488         /* Free the data queue */
489         HeapFree(GetProcessHeap(), 0, This->base.data_queue);
490
491         /* release the data transform filter */
492         release_DataFormat(&This->base.data_format);
493
494         DeleteCriticalSection(&This->base.crit);
495         
496         HeapFree(GetProcessHeap(),0,This);
497         return 0;
498 }
499
500 /******************************************************************************
501   *     Acquire : gets exclusive control of the joystick
502   */
503 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
504 {
505     JoystickImpl *This = (JoystickImpl *)iface;
506     HRESULT res;
507
508     TRACE("(this=%p)\n",This);
509
510     res = IDirectInputDevice2AImpl_Acquire(iface);
511     if (res==DI_OK) {
512       if (-1==(This->joyfd=open(This->joydev->device,O_RDWR))) {
513         if (-1==(This->joyfd=open(This->joydev->device,O_RDONLY))) {
514           /* Couldn't open the device at all */
515           perror(This->joydev->device);
516           IDirectInputDevice2AImpl_Unacquire(iface);
517           return DIERR_NOTFOUND;
518         } else {
519           /* Couldn't open in r/w but opened in read-only. */
520           WARN("Could not open %s in read-write mode.  Force feedback will be disabled.\n", This->joydev->device);
521         }
522       }
523     }
524
525     return res;
526 }
527
528 /******************************************************************************
529   *     Unacquire : frees the joystick
530   */
531 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
532 {
533     JoystickImpl *This = (JoystickImpl *)iface;
534     HRESULT res;
535
536     TRACE("(this=%p)\n",This);
537     res = IDirectInputDevice2AImpl_Unacquire(iface);
538     if (res==DI_OK && This->joyfd!=-1) {
539       close(This->joyfd);
540       This->joyfd = -1;
541     }
542     return res;
543 }
544
545 /*
546  * This maps the read value (from the input event) to a value in the
547  * 'wanted' range. It also autodetects the possible range of the axe and
548  * adapts values accordingly.
549  */
550 static int
551 map_axis(JoystickImpl* This, int axis, int val) {
552     int xmin = This->joydev->axes[axis][AXE_ABSMIN];
553     int xmax = This->joydev->axes[axis][AXE_ABSMAX];
554     int hmax = This->joydev->havemax[axis];
555     int hmin = This->joydev->havemin[axis];
556     int wmin = This->wantmin[axis];
557     int wmax = This->wantmax[axis];
558     int ret;
559
560     if (val > hmax) This->joydev->havemax[axis] = hmax = val;
561     if (val < hmin) This->joydev->havemin[axis] = hmin = val;
562
563     if (xmin == xmax) return val;
564
565     /* map the value from the hmin-hmax range into the wmin-wmax range */
566     ret = MulDiv( val - hmin, wmax - wmin, hmax - hmin ) + wmin;
567
568     TRACE("xmin=%d xmax=%d hmin=%d hmax=%d wmin=%d wmax=%d val=%d ret=%d\n", xmin, xmax, hmin, hmax, wmin, wmax, val, ret);
569
570 #if 0
571     /* deadzone doesn't work comfortably enough right now. needs more testing*/
572     if ((ret > -deadz/2 ) && (ret < deadz/2)) {
573         FIXME("%d in deadzone, return mid.\n",val);
574         return (wmax-wmin)/2+wmin;
575     }
576 #endif
577     return ret;
578 }
579
580 /* 
581  * set the current state of the js device as it would be with the middle
582  * values on the axes
583  */
584 static void fake_current_js_state(JoystickImpl *ji)
585 {
586         int i;
587         /* center the axes */
588         ji->js.lX           = test_bit(ji->joydev->absbits, ABS_X)        ? map_axis(ji, ABS_X,        ji->joydev->axes[ABS_X       ][AXE_ABS]) : 0;
589         ji->js.lY           = test_bit(ji->joydev->absbits, ABS_Y)        ? map_axis(ji, ABS_Y,        ji->joydev->axes[ABS_Y       ][AXE_ABS]) : 0;
590         ji->js.lZ           = test_bit(ji->joydev->absbits, ABS_Z)        ? map_axis(ji, ABS_Z,        ji->joydev->axes[ABS_Z       ][AXE_ABS]) : 0;
591         ji->js.lRx          = test_bit(ji->joydev->absbits, ABS_RX)       ? map_axis(ji, ABS_RX,       ji->joydev->axes[ABS_RX      ][AXE_ABS]) : 0;
592         ji->js.lRy          = test_bit(ji->joydev->absbits, ABS_RY)       ? map_axis(ji, ABS_RY,       ji->joydev->axes[ABS_RY      ][AXE_ABS]) : 0;
593         ji->js.lRz          = test_bit(ji->joydev->absbits, ABS_RZ)       ? map_axis(ji, ABS_RZ,       ji->joydev->axes[ABS_RZ      ][AXE_ABS]) : 0;
594         ji->js.rglSlider[0] = test_bit(ji->joydev->absbits, ABS_THROTTLE) ? map_axis(ji, ABS_THROTTLE, ji->joydev->axes[ABS_THROTTLE][AXE_ABS]) : 0;
595         ji->js.rglSlider[1] = test_bit(ji->joydev->absbits, ABS_RUDDER)   ? map_axis(ji, ABS_RUDDER,   ji->joydev->axes[ABS_RUDDER  ][AXE_ABS]) : 0;
596         /* POV center is -1 */
597         for (i=0; i<4; i++) {
598                 ji->js.rgdwPOV[i] = -1;
599         }
600 }
601
602 /*
603  * Maps an event value to a DX "clock" position:
604  *           0
605  * 27000    -1 9000
606  *       18000
607  */
608 static DWORD map_pov(int event_value, int is_x) 
609 {
610         DWORD ret = -1;
611         if (is_x) {
612                 if (event_value<0) {
613                         ret = 27000;
614                 } else if (event_value>0) {
615                         ret = 9000;
616                 }
617         } else {
618                 if (event_value<0) {
619                         ret = 0;
620                 } else if (event_value>0) {
621                         ret = 18000;
622                 }
623         }
624         return ret;
625 }
626
627 /* defines how the linux input system offset mappings into c_dfDIJoystick2 */
628 static int
629 lxinput_to_user_offset(JoystickImpl *This, int ie_type, int ie_code )
630 {
631   int offset = -1;
632   switch (ie_type) {
633     case EV_ABS:
634       switch (ie_code) {
635         case ABS_X:                     offset = 0; break;
636         case ABS_Y:                     offset = 1; break;
637         case ABS_Z:                     offset = 2; break;
638         case ABS_RX:                    offset = 3; break;
639         case ABS_RY:                    offset = 4; break;
640         case ABS_RZ:                    offset = 5; break;
641         case ABS_THROTTLE:              offset = 6; break;
642         case ABS_RUDDER:                offset = 7; break;
643         case ABS_HAT0X: case ABS_HAT0Y: offset = 8; break;
644         case ABS_HAT1X: case ABS_HAT1Y: offset = 9; break;
645         case ABS_HAT2X: case ABS_HAT2Y: offset = 10; break;
646         case ABS_HAT3X: case ABS_HAT3Y: offset = 11; break;
647         /* XXX when adding new axes here also fix the offset for the buttons bellow */
648         default:
649           FIXME("Unhandled EV_ABS(0x%02X)\n", ie_code);
650           return -1;
651       }
652       break;
653     case EV_KEY:
654       if (ie_code >= 128) {
655         WARN("DX8 does not support more than 128 buttons\n");
656         return -1;
657       }
658       offset = 12 + ie_code; /* XXX */
659       break;
660     default:
661       FIXME("Unhandled type(0x%02X)\n", ie_type);
662       return -1;
663   }
664   return This->base.data_format.offsets[offset];
665 }
666
667 /* convert wine format offset to user format object index */
668 static void joy_polldev(JoystickImpl *This) {
669     struct pollfd plfd;
670     struct      input_event ie;
671     int         btn, offset;
672
673     if (This->joyfd==-1)
674         return;
675
676     while (1) {
677         plfd.fd = This->joyfd;
678         plfd.events = POLLIN;
679
680         if (poll(&plfd,1,0) != 1)
681             return;
682
683         /* we have one event, so we can read */
684         if (sizeof(ie)!=read(This->joyfd,&ie,sizeof(ie)))
685             return;
686
687         TRACE("input_event: type %d, code %d, value %d\n",ie.type,ie.code,ie.value);
688         switch (ie.type) {
689         case EV_KEY:    /* button */
690             btn = This->joydev->buttons[ie.code];
691             TRACE("(%p) %d -> %d\n", This, ie.code, btn);
692             if (btn&0x80) {
693               btn &= 0x7F;
694               if ((offset = lxinput_to_user_offset(This, ie.type, btn)) == -1) {
695                 return;
696               }
697               This->js.rgbButtons[btn] = ie.value?0x80:0x00;
698               queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.rgbButtons[btn],
699                           ie.time.tv_usec, This->dinput->evsequence++);
700             }
701             break;
702         case EV_ABS:
703             if ((offset = lxinput_to_user_offset(This, ie.type, ie.code)) == -1) {
704               return;
705             }
706             switch (ie.code) {
707             case ABS_X:
708                 This->js.lX = map_axis(This,ABS_X,ie.value);
709                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.lX,
710                             ie.time.tv_usec, This->dinput->evsequence++);
711                 break;
712             case ABS_Y:
713                 This->js.lY = map_axis(This,ABS_Y,ie.value);
714                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.lY,
715                             ie.time.tv_usec, This->dinput->evsequence++);
716                 break;
717             case ABS_Z:
718                 This->js.lZ = map_axis(This,ABS_Z,ie.value);
719                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.lZ,
720                             ie.time.tv_usec, This->dinput->evsequence++);
721                 break;
722             case ABS_RX:
723                 This->js.lRx = map_axis(This,ABS_RX,ie.value);
724                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.lRx,
725                             ie.time.tv_usec, This->dinput->evsequence++);
726                 break;
727             case ABS_RY:
728                 This->js.lRy = map_axis(This,ABS_RY,ie.value);
729                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.lRy,
730                             ie.time.tv_usec, This->dinput->evsequence++);
731                 break;
732             case ABS_RZ:
733                 This->js.lRz = map_axis(This,ABS_RZ,ie.value);
734                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.lRz,
735                             ie.time.tv_usec, This->dinput->evsequence++);
736                 break;
737             case ABS_THROTTLE:
738                 This->js.rglSlider[0] = map_axis(This,ABS_THROTTLE,ie.value);
739                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.rglSlider[0],
740                             ie.time.tv_usec, This->dinput->evsequence++);
741                 break;
742             case ABS_RUDDER:
743                 This->js.rglSlider[1] = map_axis(This,ABS_RUDDER,ie.value);
744                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.rglSlider[1],
745                             ie.time.tv_usec, This->dinput->evsequence++);
746                 break;
747             case ABS_HAT0X:
748             case ABS_HAT0Y:
749                 This->js.rgdwPOV[0] = map_pov(ie.value,ie.code==ABS_HAT0X);
750                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.rgdwPOV[0],
751                             ie.time.tv_usec, This->dinput->evsequence++);
752                 break;
753             case ABS_HAT1X:
754             case ABS_HAT1Y:
755                 This->js.rgdwPOV[1] = map_pov(ie.value,ie.code==ABS_HAT1X);
756                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.rgdwPOV[1],
757                             ie.time.tv_usec, This->dinput->evsequence++);
758                 break;
759             case ABS_HAT2X:
760             case ABS_HAT2Y:
761                 This->js.rgdwPOV[2] = map_pov(ie.value,ie.code==ABS_HAT2X);
762                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.rgdwPOV[2],
763                             ie.time.tv_usec, This->dinput->evsequence++);
764                 break;
765             case ABS_HAT3X:
766             case ABS_HAT3Y:
767                 This->js.rgdwPOV[3] = map_pov(ie.value,ie.code==ABS_HAT3X);
768                 queue_event((LPDIRECTINPUTDEVICE8A)This, offset, This->js.rgdwPOV[3],
769                             ie.time.tv_usec, This->dinput->evsequence++);
770                 break;
771             default:
772                 FIXME("unhandled joystick axe event (code %d, value %d)\n",ie.code,ie.value);
773                 break;
774             }
775             break;
776 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
777         case EV_FF_STATUS:
778             This->ff_state = ie.value;
779             break;
780 #endif
781 #ifdef EV_SYN
782         case EV_SYN:
783             /* there is nothing to do */
784             break;
785 #endif
786         default:
787             FIXME("joystick cannot handle type %d event (code %d)\n",ie.type,ie.code);
788             break;
789         }
790     }
791 }
792
793 /******************************************************************************
794   *     GetDeviceState : returns the "state" of the joystick.
795   *
796   */
797 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
798         LPDIRECTINPUTDEVICE8A iface,DWORD len,LPVOID ptr
799 ) {
800     JoystickImpl *This = (JoystickImpl *)iface;
801
802     TRACE("(this=%p,0x%08x,%p)\n", This, len, ptr);
803
804     if (This->joyfd==-1) {
805         WARN("not acquired\n");
806         return DIERR_NOTACQUIRED;
807     }
808
809     joy_polldev(This);
810
811     /* convert and copy data to user supplied buffer */
812     fill_DataFormat(ptr, &This->js, &This->base.data_format);
813
814     return DI_OK;
815 }
816
817 /******************************************************************************
818   *     SetProperty : change input device properties
819   */
820 static HRESULT WINAPI JoystickAImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface,
821                                             REFGUID rguid,
822                                             LPCDIPROPHEADER ph)
823 {
824   JoystickImpl *This = (JoystickImpl *)iface;
825
826   if (!ph) {
827     WARN("invalid argument\n");
828     return DIERR_INVALIDPARAM;
829   }
830
831   TRACE("(this=%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
832   TRACE("ph.dwSize = %d, ph.dwHeaderSize =%d, ph.dwObj = %d, ph.dwHow= %d\n",
833         ph->dwSize, ph->dwHeaderSize, ph->dwObj, ph->dwHow);
834
835   if (!HIWORD(rguid)) {
836     switch (LOWORD(rguid)) {
837     case (DWORD)DIPROP_RANGE: {
838       LPCDIPROPRANGE    pr = (LPCDIPROPRANGE)ph;
839
840       if (ph->dwHow == DIPH_DEVICE) {
841         int i;
842         TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
843         for (i = 0; i < This->base.data_format.user_df->dwNumObjs; i++) {
844           This->wantmin[i] = pr->lMin;
845           This->wantmax[i] = pr->lMax;
846         }
847       } else {
848         int obj = find_property(This->base.data_format.user_df, ph);
849         TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
850         if (obj >= 0) {
851           This->wantmin[obj] = pr->lMin;
852           This->wantmax[obj] = pr->lMax;
853         }
854       }
855       fake_current_js_state(This);
856       break;
857     }
858     case (DWORD)DIPROP_DEADZONE: {
859       LPCDIPROPDWORD    pd = (LPCDIPROPDWORD)ph;
860       if (ph->dwHow == DIPH_DEVICE) {
861         int i;
862         TRACE("deadzone(%d) all\n", pd->dwData);
863         for (i = 0; i < This->base.data_format.user_df->dwNumObjs; i++) {
864           This->deadz[i] = pd->dwData;
865         }
866       } else {
867         int obj = find_property(This->base.data_format.user_df, ph);
868         TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
869         if (obj >= 0) {
870           This->deadz[obj] = pd->dwData;
871         }
872       }
873       fake_current_js_state(This);
874       break;
875     }
876     case (DWORD)DIPROP_CALIBRATIONMODE: {
877       LPCDIPROPDWORD    pd = (LPCDIPROPDWORD)ph;
878       FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
879       break;
880     }
881     default:
882       return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
883     }
884   }
885   return DI_OK;
886 }
887
888 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
889         LPDIRECTINPUTDEVICE8A iface,
890         LPDIDEVCAPS lpDIDevCaps)
891 {
892     JoystickImpl *This = (JoystickImpl *)iface;
893     int         i,axes,buttons,povs;
894
895     TRACE("%p->(%p)\n",iface,lpDIDevCaps);
896
897     if (!lpDIDevCaps) {
898         WARN("invalid pointer\n");
899         return E_POINTER;
900     }
901
902     if (lpDIDevCaps->dwSize < sizeof(DIDEVCAPS)) {
903         WARN("invalid argument\n");
904         return DIERR_INVALIDPARAM;
905     }
906
907     lpDIDevCaps->dwFlags        = DIDC_ATTACHED;
908     if (This->dinput->dwVersion >= 0x0800)
909         lpDIDevCaps->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
910     else
911         lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
912
913     axes=0;
914     for (i=0;i<ABS_MAX;i++) {
915       if (!test_bit(This->joydev->absbits,i)) continue;
916       switch (i) {
917       case ABS_HAT0X: case ABS_HAT0Y:
918       case ABS_HAT1X: case ABS_HAT1Y:
919       case ABS_HAT2X: case ABS_HAT2Y:
920       case ABS_HAT3X: case ABS_HAT3Y:
921         /* will be handled as POV - see below */
922         break;
923       default:
924         axes++;
925       }
926     }
927     buttons=0;
928     for (i=0;i<KEY_MAX;i++) if (test_bit(This->joydev->keybits,i)) buttons++;
929     povs=0;
930     for (i=0; i<4; i++) {
931       if (test_bit(This->joydev->absbits,ABS_HAT0X+(i<<1)) && test_bit(This->joydev->absbits,ABS_HAT0Y+(i<<1))) {
932         povs ++;
933       }
934     }
935
936     if (This->joydev->has_ff) 
937          lpDIDevCaps->dwFlags |= DIDC_FORCEFEEDBACK;
938
939     lpDIDevCaps->dwAxes = axes;
940     lpDIDevCaps->dwButtons = buttons;
941     lpDIDevCaps->dwPOVs = povs;
942
943     return DI_OK;
944 }
945
946 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface) {
947     JoystickImpl *This = (JoystickImpl *)iface;
948     TRACE("(%p)\n",This);
949
950     if (This->joyfd==-1) {
951       return DIERR_NOTACQUIRED;
952     }
953
954     joy_polldev(This);
955     return DI_OK;
956 }
957
958 /******************************************************************************
959   *     EnumObjects : enumerate the different buttons and axis...
960   */
961 static HRESULT WINAPI JoystickAImpl_EnumObjects(
962         LPDIRECTINPUTDEVICE8A iface,
963         LPDIENUMDEVICEOBJECTSCALLBACKA lpCallback,
964         LPVOID lpvRef,
965         DWORD dwFlags)
966 {
967   JoystickImpl *This = (JoystickImpl *)iface;
968   DIDEVICEOBJECTINSTANCEA ddoi;
969   int user_offset, user_object;
970
971   TRACE("(this=%p,%p,%p,%08x)\n", This, lpCallback, lpvRef, dwFlags);
972   if (TRACE_ON(dinput)) {
973     TRACE("  - flags = ");
974     _dump_EnumObjects_flags(dwFlags);
975     TRACE("\n");
976   }
977
978   /* Only the fields till dwFFMaxForce are relevant */
979   ddoi.dwSize = FIELD_OFFSET(DIDEVICEOBJECTINSTANCEA, dwFFMaxForce);
980
981   /* For the joystick, do as is done in the GetCapabilities function */
982   /* FIXME: needs more items */
983   if ((dwFlags == DIDFT_ALL) ||
984       (dwFlags & DIDFT_AXIS)) {
985     BYTE i;
986
987     for (i = 0; i < ABS_MAX; i++) {
988       if (!test_bit(This->joydev->absbits,i)) continue;
989
990       switch (i) {
991       case ABS_X:
992         ddoi.guidType = GUID_XAxis;
993         break;
994       case ABS_Y:
995         ddoi.guidType = GUID_YAxis;
996         break;
997       case ABS_Z:
998         ddoi.guidType = GUID_ZAxis;
999         break;
1000       case ABS_RX:
1001         ddoi.guidType = GUID_RxAxis;
1002         break;
1003       case ABS_RY:
1004         ddoi.guidType = GUID_RyAxis;
1005         break;
1006       case ABS_RZ:
1007         ddoi.guidType = GUID_RzAxis;
1008         break;
1009       case ABS_THROTTLE:
1010       case ABS_RUDDER:
1011         ddoi.guidType = GUID_Slider;
1012         break;
1013       case ABS_HAT0X: case ABS_HAT0Y:
1014       case ABS_HAT1X: case ABS_HAT1Y:
1015       case ABS_HAT2X: case ABS_HAT2Y:
1016       case ABS_HAT3X: case ABS_HAT3Y:
1017         /* will be handled as POV - see below */
1018         continue;
1019       default:
1020         FIXME("unhandled abs axis 0x%02x, ignoring!\n",i);
1021         continue;
1022       }
1023       if ((user_offset = lxinput_to_user_offset(This, EV_ABS, i)) == -1) {
1024         continue;
1025       }
1026       user_object = offset_to_object(This->base.data_format.user_df, user_offset);
1027       ddoi.dwType = This->base.data_format.user_df->rgodf[user_object].dwType & 0x00ffffff;
1028       ddoi.dwOfs =  This->base.data_format.user_df->rgodf[user_object].dwOfs;
1029       /* Linux event force feedback supports only (and always) x and y axes */
1030       if (i == ABS_X || i == ABS_Y) {
1031         if (This->joydev->has_ff)
1032           ddoi.dwFlags |= DIDOI_FFACTUATOR;
1033       }
1034       sprintf(ddoi.tszName, "%d-Axis", i);
1035       _dump_OBJECTINSTANCEA(&ddoi);
1036       if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) {
1037         return DI_OK;
1038       }
1039     }
1040   }
1041
1042   if ((dwFlags == DIDFT_ALL) ||
1043       (dwFlags & DIDFT_POV)) {
1044     int i;
1045     ddoi.guidType = GUID_POV;
1046     for (i=0; i<4; i++) {
1047       if (test_bit(This->joydev->absbits,ABS_HAT0X+(i<<1)) && test_bit(This->joydev->absbits,ABS_HAT0Y+(i<<1))) {
1048         if ((user_offset = lxinput_to_user_offset(This, EV_ABS, ABS_HAT0X+i))== -1) {
1049           continue;
1050         }
1051         user_object = offset_to_object(This->base.data_format.user_df, user_offset);
1052         ddoi.dwType = This->base.data_format.user_df->rgodf[user_object].dwType & 0x00ffffff;
1053         ddoi.dwOfs =  This->base.data_format.user_df->rgodf[user_object].dwOfs;
1054         sprintf(ddoi.tszName, "%d-POV", i);
1055         _dump_OBJECTINSTANCEA(&ddoi);
1056         if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) {
1057           return DI_OK;
1058         }
1059       }
1060     }
1061   }
1062
1063   if ((dwFlags == DIDFT_ALL) ||
1064       (dwFlags & DIDFT_BUTTON)) {
1065     int i, btncount=0;
1066
1067     /*The DInput SDK says that GUID_Button is only for mouse buttons but well*/
1068
1069     ddoi.guidType = GUID_Button;
1070
1071     for (i = 0; i < KEY_MAX; i++) {
1072       if (!test_bit(This->joydev->keybits,i)) continue;
1073       if ((user_offset = lxinput_to_user_offset(This, EV_KEY, btncount)) == -1) {
1074         continue;
1075       }
1076       user_object = offset_to_object(This->base.data_format.user_df, user_offset);
1077       ddoi.dwType = This->base.data_format.user_df->rgodf[user_object].dwType & 0x00ffffff;
1078       ddoi.dwOfs =  This->base.data_format.user_df->rgodf[user_object].dwOfs;
1079       sprintf(ddoi.tszName, "%d-Button", btncount);
1080       btncount++;
1081       _dump_OBJECTINSTANCEA(&ddoi);
1082       if (lpCallback(&ddoi, lpvRef) != DIENUM_CONTINUE) {
1083         return DI_OK;
1084       }
1085     }
1086   }
1087
1088   return DI_OK;
1089 }
1090
1091 static HRESULT WINAPI JoystickWImpl_EnumObjects(LPDIRECTINPUTDEVICE8W iface,
1092                                                 LPDIENUMDEVICEOBJECTSCALLBACKW lpCallback,
1093                                                 LPVOID lpvRef,
1094                                                 DWORD dwFlags)
1095 {
1096   JoystickImpl *This = (JoystickImpl *)iface;
1097
1098   device_enumobjects_AtoWcb_data data;
1099
1100   data.lpCallBack = lpCallback;
1101   data.lpvRef = lpvRef;
1102
1103   return JoystickAImpl_EnumObjects((LPDIRECTINPUTDEVICE8A) This, (LPDIENUMDEVICEOBJECTSCALLBACKA) DIEnumDevicesCallbackAtoW, (LPVOID) &data, dwFlags);
1104 }
1105
1106 /******************************************************************************
1107   *     GetProperty : get input device properties
1108   */
1109 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
1110                                                 REFGUID rguid,
1111                                                 LPDIPROPHEADER pdiph)
1112 {
1113   JoystickImpl *This = (JoystickImpl *)iface;
1114
1115   TRACE("(this=%p,%s,%p)\n",
1116         iface, debugstr_guid(rguid), pdiph);
1117
1118   if (TRACE_ON(dinput))
1119     _dump_DIPROPHEADER(pdiph);
1120
1121   if (!HIWORD(rguid)) {
1122     switch (LOWORD(rguid)) {
1123     case (DWORD) DIPROP_RANGE: {
1124       LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
1125       int obj = find_property(This->base.data_format.user_df, pdiph);
1126       if (obj >= 0) {
1127         pr->lMin = This->joydev->havemin[obj];
1128         pr->lMax = This->joydev->havemax[obj];
1129         TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
1130       }
1131       break;
1132     }
1133
1134     default:
1135       return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
1136     }
1137   }
1138
1139
1140   return DI_OK;
1141 }
1142
1143 /****************************************************************************** 
1144   *     CreateEffect - Create a new FF effect with the specified params
1145   */
1146 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface,
1147                                                  REFGUID rguid,
1148                                                  LPCDIEFFECT lpeff,
1149                                                  LPDIRECTINPUTEFFECT *ppdef,
1150                                                  LPUNKNOWN pUnkOuter)
1151 {
1152 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1153     EffectListItem* new = NULL;
1154     HRESULT retval = DI_OK;
1155 #endif
1156
1157     JoystickImpl* This = (JoystickImpl*)iface;
1158     TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1159
1160 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1161     TRACE("not available (compiled w/o ff support)\n");
1162     *ppdef = NULL;
1163     return DI_OK;
1164 #else
1165
1166     new = HeapAlloc(GetProcessHeap(), 0, sizeof(EffectListItem));
1167     new->next = This->top_effect;
1168     This->top_effect = new;
1169
1170     retval = linuxinput_create_effect(&(This->joyfd), rguid, &(new->ref));
1171     if (retval != DI_OK)
1172         return retval;
1173
1174     if (lpeff != NULL)
1175         retval = IDirectInputEffect_SetParameters(new->ref, lpeff, 0);
1176     if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1177         return retval;
1178
1179     *ppdef = new->ref;
1180
1181     if (pUnkOuter != NULL)
1182         FIXME("Interface aggregation not implemented.\n");
1183
1184     return DI_OK;
1185
1186 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1187
1188
1189 /*******************************************************************************
1190  *      EnumEffects - Enumerate available FF effects
1191  */
1192 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1193                                                 LPDIENUMEFFECTSCALLBACKA lpCallback,
1194                                                 LPVOID pvRef,
1195                                                 DWORD dwEffType)
1196 {
1197 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1198     DIEFFECTINFOA dei; /* feif */
1199     DWORD type = DIEFT_GETTYPE(dwEffType);
1200     JoystickImpl* This = (JoystickImpl*)iface;
1201
1202     TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1203
1204     dei.dwSize = sizeof(DIEFFECTINFOA);          
1205
1206     if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1207         && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1208         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1209         (*lpCallback)(&dei, pvRef);
1210     }
1211
1212     if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1213         && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1214         if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1215             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1216             (*lpCallback)(&dei, pvRef);
1217         }
1218         if (test_bit(This->joydev->ffbits, FF_SINE)) {
1219             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1220             (*lpCallback)(&dei, pvRef);
1221         }
1222         if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1223             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1224             (*lpCallback)(&dei, pvRef);
1225         }
1226         if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1227             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1228             (*lpCallback)(&dei, pvRef);
1229         }
1230         if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1231             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1232             (*lpCallback)(&dei, pvRef);
1233         }
1234     } 
1235
1236     if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1237         && test_bit(This->joydev->ffbits, FF_RAMP)) {
1238         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1239         (*lpCallback)(&dei, pvRef);
1240     }
1241
1242     if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1243         if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1244             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1245             (*lpCallback)(&dei, pvRef);
1246         }
1247         if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1248             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1249             (*lpCallback)(&dei, pvRef);
1250         }
1251         if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1252             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1253             (*lpCallback)(&dei, pvRef);
1254         }
1255         if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1256             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1257             (*lpCallback)(&dei, pvRef);
1258         }
1259     }
1260
1261 #endif
1262
1263     return DI_OK;
1264 }
1265
1266 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1267                                                 LPDIENUMEFFECTSCALLBACKW lpCallback,
1268                                                 LPVOID pvRef,
1269                                                 DWORD dwEffType)
1270 {
1271 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1272     /* seems silly to duplicate all this code but all the structures and functions
1273      * are actually different (A/W) */
1274     DIEFFECTINFOW dei; /* feif */
1275     DWORD type = DIEFT_GETTYPE(dwEffType);
1276     JoystickImpl* This = (JoystickImpl*)iface;
1277     int xfd = This->joyfd;
1278
1279     TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1280
1281     dei.dwSize = sizeof(DIEFFECTINFOW);          
1282
1283     if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1284         && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1285         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1286         (*lpCallback)(&dei, pvRef);
1287     }
1288
1289     if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1290         && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1291         if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1292             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1293             (*lpCallback)(&dei, pvRef);
1294         }
1295         if (test_bit(This->joydev->ffbits, FF_SINE)) {
1296             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1297             (*lpCallback)(&dei, pvRef);
1298         }
1299         if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1300             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1301             (*lpCallback)(&dei, pvRef);
1302         }
1303         if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1304             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1305             (*lpCallback)(&dei, pvRef);
1306         }
1307         if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1308             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1309             (*lpCallback)(&dei, pvRef);
1310         }
1311     } 
1312
1313     if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1314         && test_bit(This->joydev->ffbits, FF_RAMP)) {
1315         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1316         (*lpCallback)(&dei, pvRef);
1317     }
1318
1319     if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1320         if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1321             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1322             (*lpCallback)(&dei, pvRef);
1323         }
1324         if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1325             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1326             (*lpCallback)(&dei, pvRef);
1327         }
1328         if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1329             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1330             (*lpCallback)(&dei, pvRef);
1331         }
1332         if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1333             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1334             (*lpCallback)(&dei, pvRef);
1335         }
1336     }
1337
1338     /* return to unacquired state if that's where it was */
1339     if (xfd == -1)
1340         IDirectInputDevice8_Unacquire(iface);
1341 #endif
1342
1343     return DI_OK;
1344 }
1345
1346 /*******************************************************************************
1347  *      GetEffectInfo - Get information about a particular effect 
1348  */
1349 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1350                                                   LPDIEFFECTINFOA pdei,
1351                                                   REFGUID guid)
1352 {
1353     JoystickImpl* This = (JoystickImpl*)iface;
1354
1355     TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1356
1357 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1358     return linuxinput_get_info_A(This->joyfd, guid, pdei); 
1359 #else
1360     return DI_OK;
1361 #endif
1362 }
1363
1364 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1365                                                   LPDIEFFECTINFOW pdei,
1366                                                   REFGUID guid)
1367 {
1368     JoystickImpl* This = (JoystickImpl*)iface;
1369             
1370     TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1371         
1372 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1373     return linuxinput_get_info_W(This->joyfd, guid, pdei);
1374 #else
1375     return DI_OK;
1376 #endif
1377 }
1378
1379 /*******************************************************************************
1380  *      GetForceFeedbackState - Get information about the device's FF state 
1381  */
1382 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(
1383         LPDIRECTINPUTDEVICE8A iface,
1384         LPDWORD pdwOut)
1385 {
1386     JoystickImpl* This = (JoystickImpl*)iface;
1387
1388     TRACE("(this=%p,%p)\n", This, pdwOut);
1389
1390     (*pdwOut) = 0;
1391
1392 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1393     /* DIGFFS_STOPPED is the only mandatory flag to report */
1394     if (This->ff_state == FF_STATUS_STOPPED)
1395         (*pdwOut) |= DIGFFS_STOPPED;
1396 #endif
1397
1398     return DI_OK;
1399 }
1400
1401 /*******************************************************************************
1402  *      SendForceFeedbackCommand - Send a command to the device's FF system
1403  */
1404 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(
1405         LPDIRECTINPUTDEVICE8A iface,
1406         DWORD dwFlags)
1407 {
1408     JoystickImpl* This = (JoystickImpl*)iface;
1409     TRACE("(this=%p,%d)\n", This, dwFlags);
1410
1411 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1412     if (dwFlags == DISFFC_STOPALL) {
1413         /* Stop all effects */
1414         EffectListItem* itr = This->top_effect;
1415         while (itr) {
1416             IDirectInputEffect_Stop(itr->ref);
1417             itr = itr->next;
1418         }
1419     } else if (dwFlags == DISFFC_RESET) {
1420         /* Stop, unload, release and free all effects */
1421         /* This returns the device to its "bare" state */
1422         while (This->top_effect) {
1423             EffectListItem* temp = This->top_effect;
1424             IDirectInputEffect_Stop(temp->ref);
1425             IDirectInputEffect_Unload(temp->ref);
1426             IDirectInputEffect_Release(temp->ref);
1427             This->top_effect = temp->next;
1428             HeapFree(GetProcessHeap(), 0, temp);
1429         }
1430     } else if (dwFlags == DISFFC_PAUSE || dwFlags == DISFFC_CONTINUE) {
1431         FIXME("No support for Pause or Continue in linux\n");
1432     } else if (dwFlags == DISFFC_SETACTUATORSOFF 
1433                 || dwFlags == DISFFC_SETACTUATORSON) {
1434         FIXME("No direct actuator control in linux\n");
1435     } else {
1436         FIXME("Unknown Force Feedback Command!\n");
1437         return DIERR_INVALIDPARAM;
1438     }
1439     return DI_OK;
1440 #else
1441     return DIERR_UNSUPPORTED;
1442 #endif
1443 }
1444
1445 /*******************************************************************************
1446  *      EnumCreatedEffectObjects - Enumerate all the effects that have been
1447  *              created for this device.
1448  */
1449 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(
1450         LPDIRECTINPUTDEVICE8A iface,
1451         LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1452         LPVOID pvRef,
1453         DWORD dwFlags)
1454 {
1455     /* this function is safe to call on non-ff-enabled builds */
1456
1457     JoystickImpl* This = (JoystickImpl*)iface;
1458     EffectListItem* itr = This->top_effect;
1459     TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1460
1461     if (!lpCallback)
1462         return DIERR_INVALIDPARAM;
1463
1464     if (dwFlags != 0)
1465         FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1466
1467     while (itr) {
1468         (*lpCallback)(itr->ref, pvRef);
1469         itr = itr->next;
1470     }
1471
1472     return DI_OK;
1473 }
1474
1475 static const IDirectInputDevice8AVtbl JoystickAvt =
1476 {
1477         IDirectInputDevice2AImpl_QueryInterface,
1478         IDirectInputDevice2AImpl_AddRef,
1479         JoystickAImpl_Release,
1480         JoystickAImpl_GetCapabilities,
1481         JoystickAImpl_EnumObjects,
1482         JoystickAImpl_GetProperty,
1483         JoystickAImpl_SetProperty,
1484         JoystickAImpl_Acquire,
1485         JoystickAImpl_Unacquire,
1486         JoystickAImpl_GetDeviceState,
1487         IDirectInputDevice2AImpl_GetDeviceData,
1488         IDirectInputDevice2AImpl_SetDataFormat,
1489         IDirectInputDevice2AImpl_SetEventNotification,
1490         IDirectInputDevice2AImpl_SetCooperativeLevel,
1491         IDirectInputDevice2AImpl_GetObjectInfo,
1492         IDirectInputDevice2AImpl_GetDeviceInfo,
1493         IDirectInputDevice2AImpl_RunControlPanel,
1494         IDirectInputDevice2AImpl_Initialize,
1495         JoystickAImpl_CreateEffect,
1496         JoystickAImpl_EnumEffects,
1497         JoystickAImpl_GetEffectInfo,
1498         JoystickAImpl_GetForceFeedbackState,
1499         JoystickAImpl_SendForceFeedbackCommand,
1500         JoystickAImpl_EnumCreatedEffectObjects,
1501         IDirectInputDevice2AImpl_Escape,
1502         JoystickAImpl_Poll,
1503         IDirectInputDevice2AImpl_SendDeviceData,
1504         IDirectInputDevice7AImpl_EnumEffectsInFile,
1505         IDirectInputDevice7AImpl_WriteEffectToFile,
1506         IDirectInputDevice8AImpl_BuildActionMap,
1507         IDirectInputDevice8AImpl_SetActionMap,
1508         IDirectInputDevice8AImpl_GetImageInfo
1509 };
1510
1511 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1512 # define XCAST(fun)     (typeof(JoystickWvt.fun))
1513 #else
1514 # define XCAST(fun)     (void*)
1515 #endif
1516
1517 static const IDirectInputDevice8WVtbl JoystickWvt =
1518 {
1519         IDirectInputDevice2WImpl_QueryInterface,
1520         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1521         XCAST(Release)JoystickAImpl_Release,
1522         XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1523         JoystickWImpl_EnumObjects,
1524         XCAST(GetProperty)JoystickAImpl_GetProperty,
1525         XCAST(SetProperty)JoystickAImpl_SetProperty,
1526         XCAST(Acquire)JoystickAImpl_Acquire,
1527         XCAST(Unacquire)JoystickAImpl_Unacquire,
1528         XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1529         XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1530         XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1531         XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1532         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1533         IDirectInputDevice2WImpl_GetObjectInfo,
1534         IDirectInputDevice2WImpl_GetDeviceInfo,
1535         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1536         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1537         XCAST(CreateEffect)JoystickAImpl_CreateEffect,
1538         JoystickWImpl_EnumEffects,
1539         JoystickWImpl_GetEffectInfo,
1540         XCAST(GetForceFeedbackState)JoystickAImpl_GetForceFeedbackState,
1541         XCAST(SendForceFeedbackCommand)JoystickAImpl_SendForceFeedbackCommand,
1542         XCAST(EnumCreatedEffectObjects)JoystickAImpl_EnumCreatedEffectObjects,
1543         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1544         XCAST(Poll)JoystickAImpl_Poll,
1545         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1546         IDirectInputDevice7WImpl_EnumEffectsInFile,
1547         IDirectInputDevice7WImpl_WriteEffectToFile,
1548         IDirectInputDevice8WImpl_BuildActionMap,
1549         IDirectInputDevice8WImpl_SetActionMap,
1550         IDirectInputDevice8WImpl_GetImageInfo
1551 };
1552 #undef XCAST
1553
1554 #else  /* HAVE_CORRECT_LINUXINPUT_H */
1555
1556 const struct dinput_device joystick_linuxinput_device = {
1557   "Wine Linux-input joystick driver",
1558   NULL,
1559   NULL,
1560   NULL,
1561   NULL
1562 };
1563
1564 #endif  /* HAVE_CORRECT_LINUXINPUT_H */