mshtml: Move nodes' QueryInterface implementation to vtbl.
[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     default:
873       return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
874     }
875   }
876   return DI_OK;
877 }
878
879 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
880         LPDIRECTINPUTDEVICE8A iface,
881         LPDIDEVCAPS lpDIDevCaps)
882 {
883     JoystickImpl *This = (JoystickImpl *)iface;
884
885     TRACE("%p->(%p)\n",iface,lpDIDevCaps);
886
887     if (!lpDIDevCaps) {
888         WARN("invalid pointer\n");
889         return E_POINTER;
890     }
891
892     if (lpDIDevCaps->dwSize != sizeof(DIDEVCAPS)) {
893         WARN("invalid argument\n");
894         return DIERR_INVALIDPARAM;
895     }
896
897     lpDIDevCaps->dwFlags        = DIDC_ATTACHED;
898     if (This->base.dinput->dwVersion >= 0x0800)
899         lpDIDevCaps->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
900     else
901         lpDIDevCaps->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
902
903     if (This->joydev->has_ff) 
904          lpDIDevCaps->dwFlags |= DIDC_FORCEFEEDBACK;
905
906     lpDIDevCaps->dwAxes = This->numAxes;
907     lpDIDevCaps->dwButtons = This->numButtons;
908     lpDIDevCaps->dwPOVs = This->numPOVs;
909
910     return DI_OK;
911 }
912
913 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
914 {
915     JoystickImpl *This = (JoystickImpl *)iface;
916
917     TRACE("(%p)\n",This);
918
919     if (!This->base.acquired)
920         return DIERR_NOTACQUIRED;
921
922     joy_polldev(This);
923     return DI_OK;
924 }
925
926 /******************************************************************************
927   *     GetProperty : get input device properties
928   */
929 static HRESULT WINAPI JoystickAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface,
930                                                 REFGUID rguid,
931                                                 LPDIPROPHEADER pdiph)
932 {
933     JoystickImpl *This = (JoystickImpl *)iface;
934
935     TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
936     _dump_DIPROPHEADER(pdiph);
937
938     if (HIWORD(rguid)) return DI_OK;
939
940     switch (LOWORD(rguid)) {
941     case (DWORD) DIPROP_RANGE:
942     {
943         LPDIPROPRANGE pr = (LPDIPROPRANGE) pdiph;
944         int obj = find_property(&This->base.data_format, pdiph);
945
946         if (obj < 0) return DIERR_OBJECTNOTFOUND;
947
948         pr->lMin = This->props[obj].lMin;
949         pr->lMax = This->props[obj].lMax;
950         TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
951         break;
952     }
953     case (DWORD) DIPROP_DEADZONE:
954     {
955         LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
956         int obj = find_property(&This->base.data_format, pdiph);
957
958         if (obj < 0) return DIERR_OBJECTNOTFOUND;
959
960         pd->dwData = This->props[obj].lDeadZone;
961         TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
962         break;
963     }
964
965     default:
966         return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
967     }
968
969     return DI_OK;
970 }
971
972 /******************************************************************************
973   *     GetObjectInfo : get information about a device object such as a button
974   *                     or axis
975   */
976 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
977         LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
978 {
979     static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
980     static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
981     static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
982     HRESULT res;
983
984     res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
985     if (res != DI_OK) return res;
986
987     if      (pdidoi->dwType & DIDFT_AXIS)
988         sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
989     else if (pdidoi->dwType & DIDFT_POV)
990         sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
991     else if (pdidoi->dwType & DIDFT_BUTTON)
992         sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
993
994     _dump_OBJECTINSTANCEW(pdidoi);
995     return res;
996 }
997
998 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
999         LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1000 {
1001     HRESULT res;
1002     DIDEVICEOBJECTINSTANCEW didoiW;
1003     DWORD dwSize = pdidoi->dwSize;
1004
1005     didoiW.dwSize = sizeof(didoiW);
1006     res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1007     if (res != DI_OK) return res;
1008
1009     memset(pdidoi, 0, pdidoi->dwSize);
1010     memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1011     pdidoi->dwSize = dwSize;
1012     WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1013                         sizeof(pdidoi->tszName), NULL, NULL);
1014
1015     return res;
1016 }
1017
1018 /****************************************************************************** 
1019   *     CreateEffect - Create a new FF effect with the specified params
1020   */
1021 static HRESULT WINAPI JoystickAImpl_CreateEffect(LPDIRECTINPUTDEVICE8A iface,
1022                                                  REFGUID rguid,
1023                                                  LPCDIEFFECT lpeff,
1024                                                  LPDIRECTINPUTEFFECT *ppdef,
1025                                                  LPUNKNOWN pUnkOuter)
1026 {
1027 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1028     EffectListItem* new = NULL;
1029     HRESULT retval = DI_OK;
1030 #endif
1031
1032     JoystickImpl* This = (JoystickImpl*)iface;
1033     TRACE("(this=%p,%p,%p,%p,%p)\n", This, rguid, lpeff, ppdef, pUnkOuter);
1034
1035 #ifndef HAVE_STRUCT_FF_EFFECT_DIRECTION
1036     TRACE("not available (compiled w/o ff support)\n");
1037     *ppdef = NULL;
1038     return DI_OK;
1039 #else
1040
1041     new = HeapAlloc(GetProcessHeap(), 0, sizeof(EffectListItem));
1042     new->next = This->top_effect;
1043     This->top_effect = new;
1044
1045     retval = linuxinput_create_effect(&(This->joyfd), rguid, &(new->ref));
1046     if (retval != DI_OK)
1047         return retval;
1048
1049     if (lpeff != NULL)
1050         retval = IDirectInputEffect_SetParameters(new->ref, lpeff, 0);
1051     if (retval != DI_OK && retval != DI_DOWNLOADSKIPPED)
1052         return retval;
1053
1054     *ppdef = new->ref;
1055
1056     if (pUnkOuter != NULL)
1057         FIXME("Interface aggregation not implemented.\n");
1058
1059     return DI_OK;
1060
1061 #endif /* HAVE_STRUCT_FF_EFFECT_DIRECTION */
1062
1063
1064 /*******************************************************************************
1065  *      EnumEffects - Enumerate available FF effects
1066  */
1067 static HRESULT WINAPI JoystickAImpl_EnumEffects(LPDIRECTINPUTDEVICE8A iface,
1068                                                 LPDIENUMEFFECTSCALLBACKA lpCallback,
1069                                                 LPVOID pvRef,
1070                                                 DWORD dwEffType)
1071 {
1072 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1073     DIEFFECTINFOA dei; /* feif */
1074     DWORD type = DIEFT_GETTYPE(dwEffType);
1075     JoystickImpl* This = (JoystickImpl*)iface;
1076
1077     TRACE("(this=%p,%p,%d) type=%d\n", This, pvRef, dwEffType, type);
1078
1079     dei.dwSize = sizeof(DIEFFECTINFOA);          
1080
1081     if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1082         && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1083         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1084         (*lpCallback)(&dei, pvRef);
1085     }
1086
1087     if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1088         && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1089         if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1090             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1091             (*lpCallback)(&dei, pvRef);
1092         }
1093         if (test_bit(This->joydev->ffbits, FF_SINE)) {
1094             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1095             (*lpCallback)(&dei, pvRef);
1096         }
1097         if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1098             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1099             (*lpCallback)(&dei, pvRef);
1100         }
1101         if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1102             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1103             (*lpCallback)(&dei, pvRef);
1104         }
1105         if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1106             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1107             (*lpCallback)(&dei, pvRef);
1108         }
1109     } 
1110
1111     if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1112         && test_bit(This->joydev->ffbits, FF_RAMP)) {
1113         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1114         (*lpCallback)(&dei, pvRef);
1115     }
1116
1117     if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1118         if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1119             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1120             (*lpCallback)(&dei, pvRef);
1121         }
1122         if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1123             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1124             (*lpCallback)(&dei, pvRef);
1125         }
1126         if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1127             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1128             (*lpCallback)(&dei, pvRef);
1129         }
1130         if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1131             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1132             (*lpCallback)(&dei, pvRef);
1133         }
1134     }
1135
1136 #endif
1137
1138     return DI_OK;
1139 }
1140
1141 static HRESULT WINAPI JoystickWImpl_EnumEffects(LPDIRECTINPUTDEVICE8W iface,
1142                                                 LPDIENUMEFFECTSCALLBACKW lpCallback,
1143                                                 LPVOID pvRef,
1144                                                 DWORD dwEffType)
1145 {
1146 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1147     /* seems silly to duplicate all this code but all the structures and functions
1148      * are actually different (A/W) */
1149     DIEFFECTINFOW dei; /* feif */
1150     DWORD type = DIEFT_GETTYPE(dwEffType);
1151     JoystickImpl* This = (JoystickImpl*)iface;
1152     int xfd = This->joyfd;
1153
1154     TRACE("(this=%p,%p,%d) type=%d fd=%d\n", This, pvRef, dwEffType, type, xfd);
1155
1156     dei.dwSize = sizeof(DIEFFECTINFOW);          
1157
1158     if ((type == DIEFT_ALL || type == DIEFT_CONSTANTFORCE)
1159         && test_bit(This->joydev->ffbits, FF_CONSTANT)) {
1160         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_ConstantForce);
1161         (*lpCallback)(&dei, pvRef);
1162     }
1163
1164     if ((type == DIEFT_ALL || type == DIEFT_PERIODIC)
1165         && test_bit(This->joydev->ffbits, FF_PERIODIC)) {
1166         if (test_bit(This->joydev->ffbits, FF_SQUARE)) {
1167             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Square);
1168             (*lpCallback)(&dei, pvRef);
1169         }
1170         if (test_bit(This->joydev->ffbits, FF_SINE)) {
1171             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Sine);
1172             (*lpCallback)(&dei, pvRef);
1173         }
1174         if (test_bit(This->joydev->ffbits, FF_TRIANGLE)) {
1175             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Triangle);
1176             (*lpCallback)(&dei, pvRef);
1177         }
1178         if (test_bit(This->joydev->ffbits, FF_SAW_UP)) {
1179             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothUp);
1180             (*lpCallback)(&dei, pvRef);
1181         }
1182         if (test_bit(This->joydev->ffbits, FF_SAW_DOWN)) {
1183             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_SawtoothDown);
1184             (*lpCallback)(&dei, pvRef);
1185         }
1186     } 
1187
1188     if ((type == DIEFT_ALL || type == DIEFT_RAMPFORCE)
1189         && test_bit(This->joydev->ffbits, FF_RAMP)) {
1190         IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_RampForce);
1191         (*lpCallback)(&dei, pvRef);
1192     }
1193
1194     if (type == DIEFT_ALL || type == DIEFT_CONDITION) {
1195         if (test_bit(This->joydev->ffbits, FF_SPRING)) {
1196             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Spring);
1197             (*lpCallback)(&dei, pvRef);
1198         }
1199         if (test_bit(This->joydev->ffbits, FF_DAMPER)) {
1200             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Damper);
1201             (*lpCallback)(&dei, pvRef);
1202         }
1203         if (test_bit(This->joydev->ffbits, FF_INERTIA)) {
1204             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Inertia);
1205             (*lpCallback)(&dei, pvRef);
1206         }
1207         if (test_bit(This->joydev->ffbits, FF_FRICTION)) {
1208             IDirectInputDevice8_GetEffectInfo(iface, &dei, &GUID_Friction);
1209             (*lpCallback)(&dei, pvRef);
1210         }
1211     }
1212
1213     /* return to unacquired state if that's where it was */
1214     if (xfd == -1)
1215         IDirectInputDevice8_Unacquire(iface);
1216 #endif
1217
1218     return DI_OK;
1219 }
1220
1221 /*******************************************************************************
1222  *      GetEffectInfo - Get information about a particular effect 
1223  */
1224 static HRESULT WINAPI JoystickAImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8A iface,
1225                                                   LPDIEFFECTINFOA pdei,
1226                                                   REFGUID guid)
1227 {
1228     JoystickImpl* This = (JoystickImpl*)iface;
1229
1230     TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1231
1232 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1233     return linuxinput_get_info_A(This->joyfd, guid, pdei); 
1234 #else
1235     return DI_OK;
1236 #endif
1237 }
1238
1239 static HRESULT WINAPI JoystickWImpl_GetEffectInfo(LPDIRECTINPUTDEVICE8W iface,
1240                                                   LPDIEFFECTINFOW pdei,
1241                                                   REFGUID guid)
1242 {
1243     JoystickImpl* This = (JoystickImpl*)iface;
1244             
1245     TRACE("(this=%p,%p,%s)\n", This, pdei, _dump_dinput_GUID(guid));
1246         
1247 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1248     return linuxinput_get_info_W(This->joyfd, guid, pdei);
1249 #else
1250     return DI_OK;
1251 #endif
1252 }
1253
1254 /*******************************************************************************
1255  *      GetForceFeedbackState - Get information about the device's FF state 
1256  */
1257 static HRESULT WINAPI JoystickAImpl_GetForceFeedbackState(
1258         LPDIRECTINPUTDEVICE8A iface,
1259         LPDWORD pdwOut)
1260 {
1261     JoystickImpl* This = (JoystickImpl*)iface;
1262
1263     TRACE("(this=%p,%p)\n", This, pdwOut);
1264
1265     (*pdwOut) = 0;
1266
1267 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1268     /* DIGFFS_STOPPED is the only mandatory flag to report */
1269     if (This->ff_state == FF_STATUS_STOPPED)
1270         (*pdwOut) |= DIGFFS_STOPPED;
1271 #endif
1272
1273     return DI_OK;
1274 }
1275
1276 /*******************************************************************************
1277  *      SendForceFeedbackCommand - Send a command to the device's FF system
1278  */
1279 static HRESULT WINAPI JoystickAImpl_SendForceFeedbackCommand(
1280         LPDIRECTINPUTDEVICE8A iface,
1281         DWORD dwFlags)
1282 {
1283     JoystickImpl* This = (JoystickImpl*)iface;
1284     TRACE("(this=%p,%d)\n", This, dwFlags);
1285
1286 #ifdef HAVE_STRUCT_FF_EFFECT_DIRECTION
1287     if (dwFlags == DISFFC_STOPALL) {
1288         /* Stop all effects */
1289         EffectListItem* itr = This->top_effect;
1290         while (itr) {
1291             IDirectInputEffect_Stop(itr->ref);
1292             itr = itr->next;
1293         }
1294     } else if (dwFlags == DISFFC_RESET) {
1295         /* Stop, unload, release and free all effects */
1296         /* This returns the device to its "bare" state */
1297         while (This->top_effect) {
1298             EffectListItem* temp = This->top_effect;
1299             IDirectInputEffect_Stop(temp->ref);
1300             IDirectInputEffect_Unload(temp->ref);
1301             IDirectInputEffect_Release(temp->ref);
1302             This->top_effect = temp->next;
1303             HeapFree(GetProcessHeap(), 0, temp);
1304         }
1305     } else if (dwFlags == DISFFC_PAUSE || dwFlags == DISFFC_CONTINUE) {
1306         FIXME("No support for Pause or Continue in linux\n");
1307     } else if (dwFlags == DISFFC_SETACTUATORSOFF 
1308                 || dwFlags == DISFFC_SETACTUATORSON) {
1309         FIXME("No direct actuator control in linux\n");
1310     } else {
1311         FIXME("Unknown Force Feedback Command!\n");
1312         return DIERR_INVALIDPARAM;
1313     }
1314     return DI_OK;
1315 #else
1316     return DIERR_UNSUPPORTED;
1317 #endif
1318 }
1319
1320 /*******************************************************************************
1321  *      EnumCreatedEffectObjects - Enumerate all the effects that have been
1322  *              created for this device.
1323  */
1324 static HRESULT WINAPI JoystickAImpl_EnumCreatedEffectObjects(
1325         LPDIRECTINPUTDEVICE8A iface,
1326         LPDIENUMCREATEDEFFECTOBJECTSCALLBACK lpCallback,
1327         LPVOID pvRef,
1328         DWORD dwFlags)
1329 {
1330     /* this function is safe to call on non-ff-enabled builds */
1331
1332     JoystickImpl* This = (JoystickImpl*)iface;
1333     EffectListItem* itr = This->top_effect;
1334     TRACE("(this=%p,%p,%p,%d)\n", This, lpCallback, pvRef, dwFlags);
1335
1336     if (!lpCallback)
1337         return DIERR_INVALIDPARAM;
1338
1339     if (dwFlags != 0)
1340         FIXME("Flags specified, but no flags exist yet (DX9)!\n");
1341
1342     while (itr) {
1343         (*lpCallback)(itr->ref, pvRef);
1344         itr = itr->next;
1345     }
1346
1347     return DI_OK;
1348 }
1349
1350 /******************************************************************************
1351   *     GetDeviceInfo : get information about a device's identity
1352   */
1353 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface,
1354                                                   LPDIDEVICEINSTANCEA pdidi)
1355 {
1356     JoystickImpl *This = (JoystickImpl *)iface;
1357
1358     TRACE("(%p) %p\n", This, pdidi);
1359
1360     if (pdidi == NULL) return E_POINTER;
1361     if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1362         (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA)))
1363         return DIERR_INVALIDPARAM;
1364
1365     fill_joystick_dideviceinstanceA(pdidi, This->base.dinput->dwVersion,
1366                                     get_joystick_index(&This->base.guid));
1367     return DI_OK;
1368 }
1369
1370 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface,
1371                                                   LPDIDEVICEINSTANCEW pdidi)
1372 {
1373     JoystickImpl *This = (JoystickImpl *)iface;
1374
1375     TRACE("(%p) %p\n", This, pdidi);
1376
1377     if (pdidi == NULL) return E_POINTER;
1378     if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1379         (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW)))
1380         return DIERR_INVALIDPARAM;
1381
1382     fill_joystick_dideviceinstanceW(pdidi, This->base.dinput->dwVersion,
1383                                     get_joystick_index(&This->base.guid));
1384     return DI_OK;
1385 }
1386
1387 static const IDirectInputDevice8AVtbl JoystickAvt =
1388 {
1389         IDirectInputDevice2AImpl_QueryInterface,
1390         IDirectInputDevice2AImpl_AddRef,
1391         IDirectInputDevice2AImpl_Release,
1392         JoystickAImpl_GetCapabilities,
1393         IDirectInputDevice2AImpl_EnumObjects,
1394         JoystickAImpl_GetProperty,
1395         JoystickAImpl_SetProperty,
1396         JoystickAImpl_Acquire,
1397         JoystickAImpl_Unacquire,
1398         JoystickAImpl_GetDeviceState,
1399         IDirectInputDevice2AImpl_GetDeviceData,
1400         IDirectInputDevice2AImpl_SetDataFormat,
1401         IDirectInputDevice2AImpl_SetEventNotification,
1402         IDirectInputDevice2AImpl_SetCooperativeLevel,
1403         JoystickAImpl_GetObjectInfo,
1404         JoystickAImpl_GetDeviceInfo,
1405         IDirectInputDevice2AImpl_RunControlPanel,
1406         IDirectInputDevice2AImpl_Initialize,
1407         JoystickAImpl_CreateEffect,
1408         JoystickAImpl_EnumEffects,
1409         JoystickAImpl_GetEffectInfo,
1410         JoystickAImpl_GetForceFeedbackState,
1411         JoystickAImpl_SendForceFeedbackCommand,
1412         JoystickAImpl_EnumCreatedEffectObjects,
1413         IDirectInputDevice2AImpl_Escape,
1414         JoystickAImpl_Poll,
1415         IDirectInputDevice2AImpl_SendDeviceData,
1416         IDirectInputDevice7AImpl_EnumEffectsInFile,
1417         IDirectInputDevice7AImpl_WriteEffectToFile,
1418         IDirectInputDevice8AImpl_BuildActionMap,
1419         IDirectInputDevice8AImpl_SetActionMap,
1420         IDirectInputDevice8AImpl_GetImageInfo
1421 };
1422
1423 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1424 # define XCAST(fun)     (typeof(JoystickWvt.fun))
1425 #else
1426 # define XCAST(fun)     (void*)
1427 #endif
1428
1429 static const IDirectInputDevice8WVtbl JoystickWvt =
1430 {
1431         IDirectInputDevice2WImpl_QueryInterface,
1432         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1433         XCAST(Release)IDirectInputDevice2AImpl_Release,
1434         XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1435         IDirectInputDevice2WImpl_EnumObjects,
1436         XCAST(GetProperty)JoystickAImpl_GetProperty,
1437         XCAST(SetProperty)JoystickAImpl_SetProperty,
1438         XCAST(Acquire)JoystickAImpl_Acquire,
1439         XCAST(Unacquire)JoystickAImpl_Unacquire,
1440         XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1441         XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1442         XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1443         XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1444         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1445         JoystickWImpl_GetObjectInfo,
1446         JoystickWImpl_GetDeviceInfo,
1447         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1448         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1449         XCAST(CreateEffect)JoystickAImpl_CreateEffect,
1450         JoystickWImpl_EnumEffects,
1451         JoystickWImpl_GetEffectInfo,
1452         XCAST(GetForceFeedbackState)JoystickAImpl_GetForceFeedbackState,
1453         XCAST(SendForceFeedbackCommand)JoystickAImpl_SendForceFeedbackCommand,
1454         XCAST(EnumCreatedEffectObjects)JoystickAImpl_EnumCreatedEffectObjects,
1455         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1456         XCAST(Poll)JoystickAImpl_Poll,
1457         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1458         IDirectInputDevice7WImpl_EnumEffectsInFile,
1459         IDirectInputDevice7WImpl_WriteEffectToFile,
1460         IDirectInputDevice8WImpl_BuildActionMap,
1461         IDirectInputDevice8WImpl_SetActionMap,
1462         IDirectInputDevice8WImpl_GetImageInfo
1463 };
1464 #undef XCAST
1465
1466 #else  /* HAVE_CORRECT_LINUXINPUT_H */
1467
1468 const struct dinput_device joystick_linuxinput_device = {
1469   "Wine Linux-input joystick driver",
1470   NULL,
1471   NULL,
1472   NULL,
1473   NULL
1474 };
1475
1476 #endif  /* HAVE_CORRECT_LINUXINPUT_H */