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