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