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