oleaut32: Use a saner calling convention for the marshaller asm thunks.
[wine] / dlls / dinput / joystick_linux.c
1 /*              DirectInput Joystick device
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998,1999 Lionel Ulmer
5  * Copyright 2000-2001 TransGaming Technologies Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /*
23  * To Do:
24  *      dead zone
25  *      force feedback
26  */
27
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
40 #endif
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #include <errno.h>
46 #ifdef HAVE_LINUX_IOCTL_H
47 # include <linux/ioctl.h>
48 #endif
49 #ifdef HAVE_LINUX_JOYSTICK_H
50 # include <linux/joystick.h>
51 # undef SW_MAX
52 #endif
53 #ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
55 #endif
56
57 #include "wine/debug.h"
58 #include "wine/unicode.h"
59 #include "windef.h"
60 #include "winbase.h"
61 #include "winerror.h"
62 #include "dinput.h"
63
64 #include "dinput_private.h"
65 #include "device_private.h"
66 #include "joystick_private.h"
67
68 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
69
70 #ifdef HAVE_LINUX_22_JOYSTICK_API
71
72 #define JOYDEV_NEW "/dev/input/js"
73 #define JOYDEV_OLD "/dev/js"
74 #define JOYDEVDRIVER " (js)"
75
76 struct JoyDev
77 {
78     char device[MAX_PATH];
79     char name[MAX_PATH];
80
81     BYTE axis_count;
82     BYTE button_count;
83     int  *dev_axes_map;
84 };
85
86 typedef struct JoystickImpl JoystickImpl;
87 static const IDirectInputDevice8AVtbl JoystickAvt;
88 static const IDirectInputDevice8WVtbl JoystickWvt;
89 struct JoystickImpl
90 {
91         struct JoystickGenericImpl generic;
92
93         struct JoyDev                  *joydev;
94
95         /* joystick private */
96         int                             joyfd;
97         POINTL                          povs[4];
98 };
99
100 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
101 {
102     return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
103            JoystickGenericImpl, base), JoystickImpl, generic);
104 }
105 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
106 {
107     return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
108            JoystickGenericImpl, base), JoystickImpl, generic);
109 }
110 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickImpl *This)
111 {
112     return &This->generic.base.IDirectInputDevice8A_iface;
113 }
114 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
115 {
116     return &This->generic.base.IDirectInputDevice8W_iface;
117 }
118
119 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
120   0x9e573ed9,
121   0x7734,
122   0x11d2,
123   {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
124 };
125
126 #define MAX_JOYSTICKS 64
127 static INT joystick_devices_count = -1;
128 static struct JoyDev *joystick_devices;
129
130 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
131
132 static INT find_joystick_devices(void)
133 {
134     INT i;
135
136     if (joystick_devices_count != -1) return joystick_devices_count;
137
138     joystick_devices_count = 0;
139     for (i = 0; i < MAX_JOYSTICKS; i++)
140     {
141         int fd;
142         struct JoyDev joydev, *new_joydevs;
143         BYTE axes_map[ABS_MAX + 1];
144
145         snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_NEW, i);
146         if ((fd = open(joydev.device, O_RDONLY)) < 0)
147         {
148             snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_OLD, i);
149             if ((fd = open(joydev.device, O_RDONLY)) < 0) continue;
150         }
151
152         strcpy(joydev.name, "Wine Joystick");
153 #if defined(JSIOCGNAME)
154         if (ioctl(fd, JSIOCGNAME(sizeof(joydev.name) - sizeof(JOYDEVDRIVER)), joydev.name) < 0)
155             WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
156 #endif
157
158         /* Append driver name */
159         strcat(joydev.name, JOYDEVDRIVER);
160
161         if (device_disabled_registry(joydev.name)) {
162             close(fd);
163             continue;
164         }
165
166 #ifdef JSIOCGAXES
167         if (ioctl(fd, JSIOCGAXES, &joydev.axis_count) < 0)
168         {
169             WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", joydev.device, strerror(errno));
170             joydev.axis_count = 2;
171         }
172 #endif
173 #ifdef JSIOCGBUTTONS
174         if (ioctl(fd, JSIOCGBUTTONS, &joydev.button_count) < 0)
175         {
176             WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", joydev.device, strerror(errno));
177             joydev.button_count = 2;
178         }
179 #endif
180
181         if (ioctl(fd, JSIOCGAXMAP, axes_map) < 0)
182         {
183             WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
184             joydev.dev_axes_map = NULL;
185         }
186         else
187             if ((joydev.dev_axes_map = HeapAlloc(GetProcessHeap(), 0, joydev.axis_count * sizeof(int))))
188             {
189                 INT j;
190
191                 /* Remap to DI numbers */
192                 for (j = 0; j < joydev.axis_count; j++)
193                     if (axes_map[j] < 8)
194                         /* Axis match 1-to-1 */
195                         joydev.dev_axes_map[j] = j;
196                     else if (axes_map[j] == 16 ||
197                              axes_map[j] == 17)
198                         /* POV axis */
199                         joydev.dev_axes_map[j] = 8;
200                     else
201                         joydev.dev_axes_map[j] = -1;
202             }
203
204         close(fd);
205
206         if (!joystick_devices_count)
207             new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
208         else
209             new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
210                                       (joystick_devices_count + 1) * sizeof(struct JoyDev));
211         if (!new_joydevs) continue;
212
213         TRACE("Found a joystick on %s: %s\n  with %d axes and %d buttons\n", joydev.device,
214               joydev.name, joydev.axis_count, joydev.button_count);
215
216         joystick_devices = new_joydevs;
217         joystick_devices[joystick_devices_count++] = joydev;
218     }
219
220     return joystick_devices_count;
221 }
222
223 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
224 {
225     int fd = -1;
226
227     if (id >= find_joystick_devices()) return FALSE;
228
229     if (dwFlags & DIEDFL_FORCEFEEDBACK) {
230         WARN("force feedback not supported\n");
231         return FALSE;
232     }
233
234     if ((dwDevType == 0) ||
235         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
236         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
237         /* check whether we have a joystick */
238         if ((fd = open(joystick_devices[id].device, O_RDONLY)) < 0)
239         {
240             WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].name, strerror(errno));
241             return FALSE;
242         }
243
244         /* Return joystick */
245         lpddi->guidInstance = DInput_Wine_Joystick_GUID;
246         lpddi->guidInstance.Data3 = id;
247         lpddi->guidProduct = DInput_Wine_Joystick_GUID;
248         /* we only support traditional joysticks for now */
249         if (version >= 0x0800)
250             lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
251         else
252             lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
253
254         strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
255         strcpy(lpddi->tszProductName,  joystick_devices[id].name);
256
257         lpddi->guidFFDriver = GUID_NULL;
258         close(fd);
259         TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, lpddi->tszProductName);
260         return TRUE;
261     }
262
263     return FALSE;
264 }
265
266 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
267 {
268     int fd = -1;
269
270     if (id >= find_joystick_devices()) return FALSE;
271
272     if (dwFlags & DIEDFL_FORCEFEEDBACK) {
273         WARN("force feedback not supported\n");
274         return FALSE;
275     }
276
277     if ((dwDevType == 0) ||
278         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
279         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
280         /* check whether we have a joystick */
281         if ((fd = open(joystick_devices[id].device, O_RDONLY)) < 0)
282         {
283             WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
284             return FALSE;
285         }
286
287         /* Return joystick */
288         lpddi->guidInstance = DInput_Wine_Joystick_GUID;
289         lpddi->guidInstance.Data3 = id;
290         lpddi->guidProduct = DInput_Wine_Joystick_GUID;
291         /* we only support traditional joysticks for now */
292         if (version >= 0x0800)
293             lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
294         else
295             lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
296
297         MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
298         MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
299         lpddi->guidFFDriver = GUID_NULL;
300         close(fd);
301         TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
302         return TRUE;
303     }
304
305     return FALSE;
306 }
307
308 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
309                             JoystickImpl **pdev, unsigned short index)
310 {
311     DWORD i;
312     JoystickImpl* newDevice;
313     HRESULT hr;
314     LPDIDATAFORMAT df = NULL;
315     int idx = 0;
316
317     TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
318
319     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
320     if (newDevice == 0) {
321         WARN("out of memory\n");
322         *pdev = 0;
323         return DIERR_OUTOFMEMORY;
324     }
325
326     newDevice->joydev = &joystick_devices[index];
327     newDevice->joyfd = -1;
328     newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
329     newDevice->generic.guidInstance.Data3 = index;
330     newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
331     newDevice->generic.joy_polldev = joy_polldev;
332     newDevice->generic.name        = newDevice->joydev->name;
333     newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
334     newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
335
336     if (newDevice->generic.devcaps.dwButtons > 128)
337     {
338         WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
339         newDevice->generic.devcaps.dwButtons = 128;
340     }
341
342     newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
343     newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
344     newDevice->generic.base.ref = 1;
345     newDevice->generic.base.dinput = dinput;
346     newDevice->generic.base.guid = *rguid;
347     InitializeCriticalSection(&newDevice->generic.base.crit);
348     newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
349
350     /* setup_dinput_options may change these */
351     newDevice->generic.deadzone = 0;
352
353     /* do any user specified configuration */
354     hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
355     if (hr != DI_OK)
356         goto FAILED1;
357
358     /* Create copy of default data format */
359     if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
360     memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
361
362     df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
363     if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
364
365     for (i = 0; i < newDevice->generic.device_axis_count; i++)
366     {
367         int wine_obj = newDevice->generic.axis_map[i];
368
369         if (wine_obj < 0) continue;
370
371         memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
372         if (wine_obj < 8)
373             df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
374         else
375         {
376             df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
377             i++; /* POV takes 2 axes */
378         }
379     }
380     for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
381     {
382         memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
383         df->rgodf[idx  ].pguid = &GUID_Button;
384         df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
385     }
386     newDevice->generic.base.data_format.wine_df = df;
387
388     /* initialize default properties */
389     for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
390         newDevice->generic.props[i].lDevMin = -32767;
391         newDevice->generic.props[i].lDevMax = +32767;
392         newDevice->generic.props[i].lMin = 0;
393         newDevice->generic.props[i].lMax = 0xffff;
394         newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
395         newDevice->generic.props[i].lSaturation = 0;
396     }
397
398     IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
399
400     EnterCriticalSection(&dinput->crit);
401     list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
402     LeaveCriticalSection(&dinput->crit);
403
404     newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
405     newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
406     if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
407         newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
408     else
409         newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
410     newDevice->generic.devcaps.dwFFSamplePeriod = 0;
411     newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
412     newDevice->generic.devcaps.dwFirmwareRevision = 0;
413     newDevice->generic.devcaps.dwHardwareRevision = 0;
414     newDevice->generic.devcaps.dwFFDriverVersion = 0;
415
416     if (TRACE_ON(dinput)) {
417         _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
418        for (i = 0; i < (newDevice->generic.device_axis_count); i++)
419            TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
420         _dump_DIDEVCAPS(&newDevice->generic.devcaps);
421     }
422
423     *pdev = newDevice;
424
425     return DI_OK;
426
427 FAILED:
428     hr = DIERR_OUTOFMEMORY;
429 FAILED1:
430     if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
431     HeapFree(GetProcessHeap(), 0, df);
432     release_DataFormat(&newDevice->generic.base.data_format);
433     HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
434     HeapFree(GetProcessHeap(),0,newDevice);
435     *pdev = 0;
436
437     return hr;
438 }
439
440 /******************************************************************************
441   *     get_joystick_index : Get the joystick index from a given GUID
442   */
443 static unsigned short get_joystick_index(REFGUID guid)
444 {
445     GUID wine_joystick = DInput_Wine_Joystick_GUID;
446     GUID dev_guid = *guid;
447
448     wine_joystick.Data3 = 0;
449     dev_guid.Data3 = 0;
450
451     /* for the standard joystick GUID use index 0 */
452     if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
453
454     /* for the wine joystick GUIDs use the index stored in Data3 */
455     if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
456
457     return MAX_JOYSTICKS;
458 }
459
460 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
461 {
462     unsigned short index;
463
464     TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
465     find_joystick_devices();
466     *pdev = NULL;
467
468     if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
469         joystick_devices_count && index < joystick_devices_count)
470     {
471         JoystickImpl *This;
472         HRESULT hr;
473
474         if (riid == NULL)
475             ;/* nothing */
476         else if (IsEqualGUID(&IID_IDirectInputDeviceA,  riid) ||
477                  IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
478                  IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
479                  IsEqualGUID(&IID_IDirectInputDevice8A, riid))
480         {
481             unicode = 0;
482         }
483         else if (IsEqualGUID(&IID_IDirectInputDeviceW,  riid) ||
484                  IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
485                  IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
486                  IsEqualGUID(&IID_IDirectInputDevice8W, riid))
487         {
488             unicode = 1;
489         }
490         else
491         {
492             WARN("no interface\n");
493             return DIERR_NOINTERFACE;
494         }
495
496         hr = alloc_device(rguid, dinput, &This, index);
497         if (!This) return hr;
498
499         if (unicode)
500             *pdev = &This->generic.base.IDirectInputDevice8W_iface;
501         else
502             *pdev = &This->generic.base.IDirectInputDevice8A_iface;
503
504         return hr;
505     }
506
507     return DIERR_DEVICENOTREG;
508 }
509
510 #undef MAX_JOYSTICKS
511
512 const struct dinput_device joystick_linux_device = {
513   "Wine Linux joystick driver",
514   joydev_enum_deviceA,
515   joydev_enum_deviceW,
516   joydev_create_device
517 };
518
519 /******************************************************************************
520   *     Acquire : gets exclusive control of the joystick
521   */
522 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
523 {
524     JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
525     HRESULT res;
526
527     TRACE("(%p)\n",This);
528
529     res = IDirectInputDevice2WImpl_Acquire(iface);
530     if (res != DI_OK)
531         return res;
532
533     /* open the joystick device */
534     if (This->joyfd==-1) {
535         TRACE("opening joystick device %s\n", This->joydev->device);
536
537         This->joyfd = open(This->joydev->device, O_RDONLY);
538         if (This->joyfd==-1) {
539             ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
540             IDirectInputDevice2WImpl_Unacquire(iface);
541             return DIERR_NOTFOUND;
542         }
543     }
544
545     return DI_OK;
546 }
547
548 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
549 {
550     JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
551     return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
552 }
553
554 /******************************************************************************
555   *     GetProperty : get input device properties
556   */
557 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
558 {
559     JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
560
561     TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
562     _dump_DIPROPHEADER(pdiph);
563
564     if (!IS_DIPROP(rguid)) return DI_OK;
565
566     switch (LOWORD(rguid)) {
567
568         case (DWORD_PTR) DIPROP_JOYSTICKID:
569         {
570             LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
571
572             pd->dwData = get_joystick_index(&This->generic.base.guid);
573             TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
574             break;
575         }
576
577     default:
578         return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
579     }
580
581     return DI_OK;
582 }
583
584 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
585 {
586     JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
587     return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
588 }
589
590 /******************************************************************************
591   *     Unacquire : frees the joystick
592   */
593 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
594 {
595     JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
596     HRESULT res;
597
598     TRACE("(%p)\n",This);
599
600     res = IDirectInputDevice2WImpl_Unacquire(iface);
601
602     if (res != DI_OK)
603         return res;
604
605     if (This->joyfd!=-1) {
606         TRACE("closing joystick device\n");
607         close(This->joyfd);
608         This->joyfd = -1;
609         return DI_OK;
610     }
611
612     return DI_NOEFFECT;
613 }
614
615 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
616 {
617     JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
618     return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
619 }
620
621 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
622 {
623     struct pollfd plfd;
624     struct js_event jse;
625     JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
626
627     TRACE("(%p)\n", This);
628
629     if (This->joyfd==-1) {
630         WARN("no device\n");
631         return;
632     }
633     while (1)
634     {
635         LONG value;
636         int inst_id = -1;
637
638         plfd.fd = This->joyfd;
639         plfd.events = POLLIN;
640         if (poll(&plfd,1,0) != 1)
641             return;
642         /* we have one event, so we can read */
643         if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
644             return;
645         }
646         TRACE("js_event: type 0x%x, number %d, value %d\n",
647               jse.type,jse.number,jse.value);
648         if (jse.type & JS_EVENT_BUTTON)
649         {
650             if (jse.number >= This->generic.devcaps.dwButtons) return;
651
652             inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
653             This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
654         }
655         else if (jse.type & JS_EVENT_AXIS)
656         {
657             int number = This->generic.axis_map[jse.number];    /* wine format object index */
658
659             if (number < 0) return;
660             inst_id = number < 8 ?  DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
661                                     DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
662             value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
663
664             TRACE("changing axis %d => %d\n", jse.number, number);
665             switch (number)
666             {
667                 case 0: This->generic.js.lX  = value; break;
668                 case 1: This->generic.js.lY  = value; break;
669                 case 2: This->generic.js.lZ  = value; break;
670                 case 3: This->generic.js.lRx = value; break;
671                 case 4: This->generic.js.lRy = value; break;
672                 case 5: This->generic.js.lRz = value; break;
673                 case 6: This->generic.js.rglSlider[0] = value; break;
674                 case 7: This->generic.js.rglSlider[1] = value; break;
675                 case 8: case 9: case 10: case 11:
676                 {
677                     int idx = number - 8;
678
679                     if (jse.number % 2)
680                         This->povs[idx].y = jse.value;
681                     else
682                         This->povs[idx].x = jse.value;
683
684                     This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
685                     break;
686                 }
687                 default:
688                     WARN("axis %d not supported\n", number);
689             }
690         }
691         if (inst_id >= 0)
692             queue_event(iface, inst_id,
693                         value, jse.time, This->generic.base.dinput->evsequence++);
694     }
695 }
696
697 static const IDirectInputDevice8AVtbl JoystickAvt =
698 {
699         IDirectInputDevice2AImpl_QueryInterface,
700         IDirectInputDevice2AImpl_AddRef,
701         IDirectInputDevice2AImpl_Release,
702         JoystickAGenericImpl_GetCapabilities,
703         IDirectInputDevice2AImpl_EnumObjects,
704         JoystickLinuxAImpl_GetProperty,
705         JoystickAGenericImpl_SetProperty,
706         JoystickLinuxAImpl_Acquire,
707         JoystickLinuxAImpl_Unacquire,
708         JoystickAGenericImpl_GetDeviceState,
709         IDirectInputDevice2AImpl_GetDeviceData,
710         IDirectInputDevice2AImpl_SetDataFormat,
711         IDirectInputDevice2AImpl_SetEventNotification,
712         IDirectInputDevice2AImpl_SetCooperativeLevel,
713         JoystickAGenericImpl_GetObjectInfo,
714         JoystickAGenericImpl_GetDeviceInfo,
715         IDirectInputDevice2AImpl_RunControlPanel,
716         IDirectInputDevice2AImpl_Initialize,
717         IDirectInputDevice2AImpl_CreateEffect,
718         IDirectInputDevice2AImpl_EnumEffects,
719         IDirectInputDevice2AImpl_GetEffectInfo,
720         IDirectInputDevice2AImpl_GetForceFeedbackState,
721         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
722         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
723         IDirectInputDevice2AImpl_Escape,
724         JoystickAGenericImpl_Poll,
725         IDirectInputDevice2AImpl_SendDeviceData,
726         IDirectInputDevice7AImpl_EnumEffectsInFile,
727         IDirectInputDevice7AImpl_WriteEffectToFile,
728         JoystickAGenericImpl_BuildActionMap,
729         JoystickAGenericImpl_SetActionMap,
730         IDirectInputDevice8AImpl_GetImageInfo
731 };
732
733 static const IDirectInputDevice8WVtbl JoystickWvt =
734 {
735     IDirectInputDevice2WImpl_QueryInterface,
736     IDirectInputDevice2WImpl_AddRef,
737     IDirectInputDevice2WImpl_Release,
738     JoystickWGenericImpl_GetCapabilities,
739     IDirectInputDevice2WImpl_EnumObjects,
740     JoystickLinuxWImpl_GetProperty,
741     JoystickWGenericImpl_SetProperty,
742     JoystickLinuxWImpl_Acquire,
743     JoystickLinuxWImpl_Unacquire,
744     JoystickWGenericImpl_GetDeviceState,
745     IDirectInputDevice2WImpl_GetDeviceData,
746     IDirectInputDevice2WImpl_SetDataFormat,
747     IDirectInputDevice2WImpl_SetEventNotification,
748     IDirectInputDevice2WImpl_SetCooperativeLevel,
749     JoystickWGenericImpl_GetObjectInfo,
750     JoystickWGenericImpl_GetDeviceInfo,
751     IDirectInputDevice2WImpl_RunControlPanel,
752     IDirectInputDevice2WImpl_Initialize,
753     IDirectInputDevice2WImpl_CreateEffect,
754     IDirectInputDevice2WImpl_EnumEffects,
755     IDirectInputDevice2WImpl_GetEffectInfo,
756     IDirectInputDevice2WImpl_GetForceFeedbackState,
757     IDirectInputDevice2WImpl_SendForceFeedbackCommand,
758     IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
759     IDirectInputDevice2WImpl_Escape,
760     JoystickWGenericImpl_Poll,
761     IDirectInputDevice2WImpl_SendDeviceData,
762     IDirectInputDevice7WImpl_EnumEffectsInFile,
763     IDirectInputDevice7WImpl_WriteEffectToFile,
764     JoystickWGenericImpl_BuildActionMap,
765     JoystickWGenericImpl_SetActionMap,
766     IDirectInputDevice8WImpl_GetImageInfo
767 };
768
769 #else  /* HAVE_LINUX_22_JOYSTICK_API */
770
771 const struct dinput_device joystick_linux_device = {
772   "Wine Linux joystick driver",
773   NULL,
774   NULL,
775   NULL
776 };
777
778 #endif  /* HAVE_LINUX_22_JOYSTICK_API */