opengl32: Update the extension list.
[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)) continue;
162
163 #ifdef JSIOCGAXES
164         if (ioctl(fd, JSIOCGAXES, &joydev.axis_count) < 0)
165         {
166             WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", joydev.device, strerror(errno));
167             joydev.axis_count = 2;
168         }
169 #endif
170 #ifdef JSIOCGBUTTONS
171         if (ioctl(fd, JSIOCGBUTTONS, &joydev.button_count) < 0)
172         {
173             WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", joydev.device, strerror(errno));
174             joydev.button_count = 2;
175         }
176 #endif
177
178         if (ioctl(fd, JSIOCGAXMAP, axes_map) < 0)
179         {
180             WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
181             joydev.dev_axes_map = NULL;
182         }
183         else
184             if ((joydev.dev_axes_map = HeapAlloc(GetProcessHeap(), 0, joydev.axis_count * sizeof(int))))
185             {
186                 INT j;
187
188                 /* Remap to DI numbers */
189                 for (j = 0; j < joydev.axis_count; j++)
190                     if (axes_map[j] < 8)
191                         /* Axis match 1-to-1 */
192                         joydev.dev_axes_map[j] = j;
193                     else if (axes_map[j] == 16 ||
194                              axes_map[j] == 17)
195                         /* POV axis */
196                         joydev.dev_axes_map[j] = 8;
197                     else
198                         joydev.dev_axes_map[j] = -1;
199             }
200
201         close(fd);
202
203         if (!joystick_devices_count)
204             new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
205         else
206             new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
207                                       (joystick_devices_count + 1) * sizeof(struct JoyDev));
208         if (!new_joydevs) continue;
209
210         TRACE("Found a joystick on %s: %s\n  with %d axes and %d buttons\n", joydev.device,
211               joydev.name, joydev.axis_count, joydev.button_count);
212
213         joystick_devices = new_joydevs;
214         joystick_devices[joystick_devices_count++] = joydev;
215     }
216
217     return joystick_devices_count;
218 }
219
220 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
221 {
222     int fd = -1;
223
224     if (id >= find_joystick_devices()) return FALSE;
225
226     if (dwFlags & DIEDFL_FORCEFEEDBACK) {
227         WARN("force feedback not supported\n");
228         return FALSE;
229     }
230
231     if ((dwDevType == 0) ||
232         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
233         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
234         /* check whether we have a joystick */
235         if ((fd = open(joystick_devices[id].device, O_RDONLY)) < 0)
236         {
237             WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].name, strerror(errno));
238             return FALSE;
239         }
240
241         /* Return joystick */
242         lpddi->guidInstance = DInput_Wine_Joystick_GUID;
243         lpddi->guidInstance.Data3 = id;
244         lpddi->guidProduct = DInput_Wine_Joystick_GUID;
245         /* we only support traditional joysticks for now */
246         if (version >= 0x0800)
247             lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
248         else
249             lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
250
251         strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
252         strcpy(lpddi->tszProductName,  joystick_devices[id].name);
253
254         lpddi->guidFFDriver = GUID_NULL;
255         close(fd);
256         TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, lpddi->tszProductName);
257         return TRUE;
258     }
259
260     return FALSE;
261 }
262
263 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
264 {
265     int fd = -1;
266
267     if (id >= find_joystick_devices()) return FALSE;
268
269     if (dwFlags & DIEDFL_FORCEFEEDBACK) {
270         WARN("force feedback not supported\n");
271         return FALSE;
272     }
273
274     if ((dwDevType == 0) ||
275         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
276         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
277         /* check whether we have a joystick */
278         if ((fd = open(joystick_devices[id].device, O_RDONLY)) < 0)
279         {
280             WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
281             return FALSE;
282         }
283
284         /* Return joystick */
285         lpddi->guidInstance = DInput_Wine_Joystick_GUID;
286         lpddi->guidInstance.Data3 = id;
287         lpddi->guidProduct = DInput_Wine_Joystick_GUID;
288         /* we only support traditional joysticks for now */
289         if (version >= 0x0800)
290             lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
291         else
292             lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
293
294         MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
295         MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
296         lpddi->guidFFDriver = GUID_NULL;
297         close(fd);
298         TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
299         return TRUE;
300     }
301
302     return FALSE;
303 }
304
305 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
306                             JoystickImpl **pdev, unsigned short index)
307 {
308     DWORD i;
309     JoystickImpl* newDevice;
310     HRESULT hr;
311     LPDIDATAFORMAT df = NULL;
312     int idx = 0;
313
314     TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
315
316     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
317     if (newDevice == 0) {
318         WARN("out of memory\n");
319         *pdev = 0;
320         return DIERR_OUTOFMEMORY;
321     }
322
323     newDevice->joydev = &joystick_devices[index];
324     newDevice->joyfd = -1;
325     newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
326     newDevice->generic.guidInstance.Data3 = index;
327     newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
328     newDevice->generic.joy_polldev = joy_polldev;
329     newDevice->generic.name        = newDevice->joydev->name;
330     newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
331     newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
332
333     if (newDevice->generic.devcaps.dwButtons > 128)
334     {
335         WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
336         newDevice->generic.devcaps.dwButtons = 128;
337     }
338
339     newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
340     newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
341     newDevice->generic.base.ref = 1;
342     newDevice->generic.base.dinput = dinput;
343     newDevice->generic.base.guid = *rguid;
344     InitializeCriticalSection(&newDevice->generic.base.crit);
345     newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
346
347     /* setup_dinput_options may change these */
348     newDevice->generic.deadzone = 0;
349
350     /* do any user specified configuration */
351     hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
352     if (hr != DI_OK)
353         goto FAILED1;
354
355     /* Create copy of default data format */
356     if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
357     memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
358
359     df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
360     if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
361
362     for (i = 0; i < newDevice->generic.device_axis_count; i++)
363     {
364         int wine_obj = newDevice->generic.axis_map[i];
365
366         if (wine_obj < 0) continue;
367
368         memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
369         if (wine_obj < 8)
370             df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
371         else
372         {
373             df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
374             i++; /* POV takes 2 axes */
375         }
376     }
377     for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
378     {
379         memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
380         df->rgodf[idx  ].pguid = &GUID_Button;
381         df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
382     }
383     newDevice->generic.base.data_format.wine_df = df;
384
385     /* initialize default properties */
386     for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
387         newDevice->generic.props[i].lDevMin = -32767;
388         newDevice->generic.props[i].lDevMax = +32767;
389         newDevice->generic.props[i].lMin = 0;
390         newDevice->generic.props[i].lMax = 0xffff;
391         newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
392         newDevice->generic.props[i].lSaturation = 0;
393     }
394
395     IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
396
397     EnterCriticalSection(&dinput->crit);
398     list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
399     LeaveCriticalSection(&dinput->crit);
400
401     newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
402     newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
403     if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
404         newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
405     else
406         newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
407     newDevice->generic.devcaps.dwFFSamplePeriod = 0;
408     newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
409     newDevice->generic.devcaps.dwFirmwareRevision = 0;
410     newDevice->generic.devcaps.dwHardwareRevision = 0;
411     newDevice->generic.devcaps.dwFFDriverVersion = 0;
412
413     if (TRACE_ON(dinput)) {
414         _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
415        for (i = 0; i < (newDevice->generic.device_axis_count); i++)
416            TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
417         _dump_DIDEVCAPS(&newDevice->generic.devcaps);
418     }
419
420     *pdev = newDevice;
421
422     return DI_OK;
423
424 FAILED:
425     hr = DIERR_OUTOFMEMORY;
426 FAILED1:
427     if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
428     HeapFree(GetProcessHeap(), 0, df);
429     release_DataFormat(&newDevice->generic.base.data_format);
430     HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
431     HeapFree(GetProcessHeap(),0,newDevice);
432     *pdev = 0;
433
434     return hr;
435 }
436
437 /******************************************************************************
438   *     get_joystick_index : Get the joystick index from a given GUID
439   */
440 static unsigned short get_joystick_index(REFGUID guid)
441 {
442     GUID wine_joystick = DInput_Wine_Joystick_GUID;
443     GUID dev_guid = *guid;
444
445     wine_joystick.Data3 = 0;
446     dev_guid.Data3 = 0;
447
448     /* for the standard joystick GUID use index 0 */
449     if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
450
451     /* for the wine joystick GUIDs use the index stored in Data3 */
452     if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
453
454     return MAX_JOYSTICKS;
455 }
456
457 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
458 {
459     unsigned short index;
460
461     TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
462     find_joystick_devices();
463     *pdev = NULL;
464
465     if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
466         joystick_devices_count && index < joystick_devices_count)
467     {
468         JoystickImpl *This;
469         HRESULT hr;
470
471         if (riid == NULL)
472             ;/* nothing */
473         else if (IsEqualGUID(&IID_IDirectInputDeviceA,  riid) ||
474                  IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
475                  IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
476                  IsEqualGUID(&IID_IDirectInputDevice8A, riid))
477         {
478             unicode = 0;
479         }
480         else if (IsEqualGUID(&IID_IDirectInputDeviceW,  riid) ||
481                  IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
482                  IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
483                  IsEqualGUID(&IID_IDirectInputDevice8W, riid))
484         {
485             unicode = 1;
486         }
487         else
488         {
489             WARN("no interface\n");
490             return DIERR_NOINTERFACE;
491         }
492
493         hr = alloc_device(rguid, dinput, &This, index);
494         if (!This) return hr;
495
496         if (unicode)
497             *pdev = &This->generic.base.IDirectInputDevice8W_iface;
498         else
499             *pdev = &This->generic.base.IDirectInputDevice8A_iface;
500
501         return hr;
502     }
503
504     return DIERR_DEVICENOTREG;
505 }
506
507 #undef MAX_JOYSTICKS
508
509 const struct dinput_device joystick_linux_device = {
510   "Wine Linux joystick driver",
511   joydev_enum_deviceA,
512   joydev_enum_deviceW,
513   joydev_create_device
514 };
515
516 /******************************************************************************
517   *     Acquire : gets exclusive control of the joystick
518   */
519 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
520 {
521     JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
522     HRESULT res;
523
524     TRACE("(%p)\n",This);
525
526     res = IDirectInputDevice2WImpl_Acquire(iface);
527     if (res != DI_OK)
528         return res;
529
530     /* open the joystick device */
531     if (This->joyfd==-1) {
532         TRACE("opening joystick device %s\n", This->joydev->device);
533
534         This->joyfd = open(This->joydev->device, O_RDONLY);
535         if (This->joyfd==-1) {
536             ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
537             IDirectInputDevice2WImpl_Unacquire(iface);
538             return DIERR_NOTFOUND;
539         }
540     }
541
542     return DI_OK;
543 }
544
545 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
546 {
547     JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
548     return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
549 }
550
551 /******************************************************************************
552   *     GetProperty : get input device properties
553   */
554 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
555 {
556     JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
557
558     TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
559     _dump_DIPROPHEADER(pdiph);
560
561     if (!IS_DIPROP(rguid)) return DI_OK;
562
563     switch (LOWORD(rguid)) {
564
565         case (DWORD_PTR) DIPROP_JOYSTICKID:
566         {
567             LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
568
569             pd->dwData = get_joystick_index(&This->generic.base.guid);
570             TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
571             break;
572         }
573
574     default:
575         return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
576     }
577
578     return DI_OK;
579 }
580
581 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
582 {
583     JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
584     return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
585 }
586
587 /******************************************************************************
588   *     Unacquire : frees the joystick
589   */
590 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
591 {
592     JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
593     HRESULT res;
594
595     TRACE("(%p)\n",This);
596
597     res = IDirectInputDevice2WImpl_Unacquire(iface);
598
599     if (res != DI_OK)
600         return res;
601
602     if (This->joyfd!=-1) {
603         TRACE("closing joystick device\n");
604         close(This->joyfd);
605         This->joyfd = -1;
606         return DI_OK;
607     }
608
609     return DI_NOEFFECT;
610 }
611
612 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
613 {
614     JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
615     return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
616 }
617
618 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
619 {
620     struct pollfd plfd;
621     struct js_event jse;
622     JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
623
624     TRACE("(%p)\n", This);
625
626     if (This->joyfd==-1) {
627         WARN("no device\n");
628         return;
629     }
630     while (1)
631     {
632         LONG value;
633         int inst_id = -1;
634
635         plfd.fd = This->joyfd;
636         plfd.events = POLLIN;
637         if (poll(&plfd,1,0) != 1)
638             return;
639         /* we have one event, so we can read */
640         if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
641             return;
642         }
643         TRACE("js_event: type 0x%x, number %d, value %d\n",
644               jse.type,jse.number,jse.value);
645         if (jse.type & JS_EVENT_BUTTON)
646         {
647             if (jse.number >= This->generic.devcaps.dwButtons) return;
648
649             inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
650             This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
651         }
652         else if (jse.type & JS_EVENT_AXIS)
653         {
654             int number = This->generic.axis_map[jse.number];    /* wine format object index */
655
656             if (number < 0) return;
657             inst_id = number < 8 ?  DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
658                                     DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
659             value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
660
661             TRACE("changing axis %d => %d\n", jse.number, number);
662             switch (number)
663             {
664                 case 0: This->generic.js.lX  = value; break;
665                 case 1: This->generic.js.lY  = value; break;
666                 case 2: This->generic.js.lZ  = value; break;
667                 case 3: This->generic.js.lRx = value; break;
668                 case 4: This->generic.js.lRy = value; break;
669                 case 5: This->generic.js.lRz = value; break;
670                 case 6: This->generic.js.rglSlider[0] = value; break;
671                 case 7: This->generic.js.rglSlider[1] = value; break;
672                 case 8: case 9: case 10: case 11:
673                 {
674                     int idx = number - 8;
675
676                     if (jse.number % 2)
677                         This->povs[idx].y = jse.value;
678                     else
679                         This->povs[idx].x = jse.value;
680
681                     This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
682                     break;
683                 }
684                 default:
685                     WARN("axis %d not supported\n", number);
686             }
687         }
688         if (inst_id >= 0)
689             queue_event(iface, inst_id,
690                         value, jse.time, This->generic.base.dinput->evsequence++);
691     }
692 }
693
694 static const IDirectInputDevice8AVtbl JoystickAvt =
695 {
696         IDirectInputDevice2AImpl_QueryInterface,
697         IDirectInputDevice2AImpl_AddRef,
698         IDirectInputDevice2AImpl_Release,
699         JoystickAGenericImpl_GetCapabilities,
700         IDirectInputDevice2AImpl_EnumObjects,
701         JoystickLinuxAImpl_GetProperty,
702         JoystickAGenericImpl_SetProperty,
703         JoystickLinuxAImpl_Acquire,
704         JoystickLinuxAImpl_Unacquire,
705         JoystickAGenericImpl_GetDeviceState,
706         IDirectInputDevice2AImpl_GetDeviceData,
707         IDirectInputDevice2AImpl_SetDataFormat,
708         IDirectInputDevice2AImpl_SetEventNotification,
709         IDirectInputDevice2AImpl_SetCooperativeLevel,
710         JoystickAGenericImpl_GetObjectInfo,
711         JoystickAGenericImpl_GetDeviceInfo,
712         IDirectInputDevice2AImpl_RunControlPanel,
713         IDirectInputDevice2AImpl_Initialize,
714         IDirectInputDevice2AImpl_CreateEffect,
715         IDirectInputDevice2AImpl_EnumEffects,
716         IDirectInputDevice2AImpl_GetEffectInfo,
717         IDirectInputDevice2AImpl_GetForceFeedbackState,
718         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
719         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
720         IDirectInputDevice2AImpl_Escape,
721         JoystickAGenericImpl_Poll,
722         IDirectInputDevice2AImpl_SendDeviceData,
723         IDirectInputDevice7AImpl_EnumEffectsInFile,
724         IDirectInputDevice7AImpl_WriteEffectToFile,
725         JoystickAGenericImpl_BuildActionMap,
726         JoystickAGenericImpl_SetActionMap,
727         IDirectInputDevice8AImpl_GetImageInfo
728 };
729
730 static const IDirectInputDevice8WVtbl JoystickWvt =
731 {
732     IDirectInputDevice2WImpl_QueryInterface,
733     IDirectInputDevice2WImpl_AddRef,
734     IDirectInputDevice2WImpl_Release,
735     JoystickWGenericImpl_GetCapabilities,
736     IDirectInputDevice2WImpl_EnumObjects,
737     JoystickLinuxWImpl_GetProperty,
738     JoystickWGenericImpl_SetProperty,
739     JoystickLinuxWImpl_Acquire,
740     JoystickLinuxWImpl_Unacquire,
741     JoystickWGenericImpl_GetDeviceState,
742     IDirectInputDevice2WImpl_GetDeviceData,
743     IDirectInputDevice2WImpl_SetDataFormat,
744     IDirectInputDevice2WImpl_SetEventNotification,
745     IDirectInputDevice2WImpl_SetCooperativeLevel,
746     JoystickWGenericImpl_GetObjectInfo,
747     JoystickWGenericImpl_GetDeviceInfo,
748     IDirectInputDevice2WImpl_RunControlPanel,
749     IDirectInputDevice2WImpl_Initialize,
750     IDirectInputDevice2WImpl_CreateEffect,
751     IDirectInputDevice2WImpl_EnumEffects,
752     IDirectInputDevice2WImpl_GetEffectInfo,
753     IDirectInputDevice2WImpl_GetForceFeedbackState,
754     IDirectInputDevice2WImpl_SendForceFeedbackCommand,
755     IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
756     IDirectInputDevice2WImpl_Escape,
757     JoystickWGenericImpl_Poll,
758     IDirectInputDevice2WImpl_SendDeviceData,
759     IDirectInputDevice7WImpl_EnumEffectsInFile,
760     IDirectInputDevice7WImpl_WriteEffectToFile,
761     JoystickWGenericImpl_BuildActionMap,
762     JoystickWGenericImpl_SetActionMap,
763     IDirectInputDevice8WImpl_GetImageInfo
764 };
765
766 #else  /* HAVE_LINUX_22_JOYSTICK_API */
767
768 const struct dinput_device joystick_linux_device = {
769   "Wine Linux joystick driver",
770   NULL,
771   NULL,
772   NULL
773 };
774
775 #endif  /* HAVE_LINUX_22_JOYSTICK_API */