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