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