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