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