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