dinput: Move axes mapping array and default dead-zone into generic joystick class.
[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_SYS_ERRNO_H
47 # include <sys/errno.h>
48 #endif
49 #ifdef HAVE_LINUX_IOCTL_H
50 # include <linux/ioctl.h>
51 #endif
52 #ifdef HAVE_LINUX_JOYSTICK_H
53 # include <linux/joystick.h>
54 # undef SW_MAX
55 #endif
56 #ifdef HAVE_SYS_POLL_H
57 # include <sys/poll.h>
58 #endif
59
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
62 #include "windef.h"
63 #include "winbase.h"
64 #include "winerror.h"
65 #include "winreg.h"
66 #include "dinput.h"
67
68 #include "dinput_private.h"
69 #include "device_private.h"
70 #include "joystick_private.h"
71
72 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
73
74 #ifdef HAVE_LINUX_22_JOYSTICK_API
75
76 #define JOYDEV_NEW "/dev/input/js"
77 #define JOYDEV_OLD "/dev/js"
78
79 typedef struct JoystickImpl JoystickImpl;
80 static const IDirectInputDevice8AVtbl JoystickAvt;
81 static const IDirectInputDevice8WVtbl JoystickWvt;
82 struct JoystickImpl
83 {
84         struct JoystickGenericImpl generic;
85
86         char                            dev[32];
87
88         /* joystick private */
89         int                             joyfd;
90         int                             axes;
91         POINTL                          povs[4];
92 };
93
94 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
95   0x9e573ed9,
96   0x7734,
97   0x11d2,
98   {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
99 };
100
101 #define MAX_JOYSTICKS 64
102 static INT joystick_devices_count = -1;
103 static LPSTR joystick_devices[MAX_JOYSTICKS];
104
105 static void joy_polldev(JoystickGenericImpl *This);
106
107 static INT find_joystick_devices(void)
108 {
109     INT i;
110
111     if (joystick_devices_count != -1) return joystick_devices_count;
112
113     joystick_devices_count = 0;
114     for (i = 0; i < MAX_JOYSTICKS; i++)
115     {
116         CHAR device_name[MAX_PATH], *str;
117         INT len;
118         int fd;
119
120         len = sprintf(device_name, "%s%d", JOYDEV_NEW, i) + 1;
121         if ((fd = open(device_name, O_RDONLY)) < 0)
122         {
123             len = sprintf(device_name, "%s%d", JOYDEV_OLD, i) + 1;
124             if ((fd = open(device_name, O_RDONLY)) < 0) continue;
125         }
126
127         close(fd);
128
129         if (!(str = HeapAlloc(GetProcessHeap(), 0, len))) break;
130         memcpy(str, device_name, len);
131
132         joystick_devices[joystick_devices_count++] = str;
133     }
134
135     return joystick_devices_count;
136 }
137
138 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
139 {
140     int fd = -1;
141
142     if (id >= find_joystick_devices()) return FALSE;
143
144     if (dwFlags & DIEDFL_FORCEFEEDBACK) {
145         WARN("force feedback not supported\n");
146         return FALSE;
147     }
148
149     if ((dwDevType == 0) ||
150         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
151         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
152         /* check whether we have a joystick */
153         if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
154         {
155             WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
156             return FALSE;
157         }
158
159         /* Return joystick */
160         lpddi->guidInstance = DInput_Wine_Joystick_GUID;
161         lpddi->guidInstance.Data3 = id;
162         lpddi->guidProduct = DInput_Wine_Joystick_GUID;
163         /* we only support traditional joysticks for now */
164         if (version >= 0x0800)
165             lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
166         else
167             lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
168         sprintf(lpddi->tszInstanceName, "Joystick %d", id);
169 #if defined(JSIOCGNAME)
170         if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
171             WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
172             strcpy(lpddi->tszProductName, "Wine Joystick");
173         }
174 #else
175         strcpy(lpddi->tszProductName, "Wine Joystick");
176 #endif
177
178         lpddi->guidFFDriver = GUID_NULL;
179         close(fd);
180         TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], lpddi->tszProductName);
181         return TRUE;
182     }
183
184     return FALSE;
185 }
186
187 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
188 {
189     int fd = -1;
190     char name[MAX_PATH];
191     char friendly[32];
192
193     if (id >= find_joystick_devices()) return FALSE;
194
195     if (dwFlags & DIEDFL_FORCEFEEDBACK) {
196         WARN("force feedback not supported\n");
197         return FALSE;
198     }
199
200     if ((dwDevType == 0) ||
201         ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
202         (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
203         /* check whether we have a joystick */
204         if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
205         {
206             WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
207             return FALSE;
208         }
209
210         /* Return joystick */
211         lpddi->guidInstance = DInput_Wine_Joystick_GUID;
212         lpddi->guidInstance.Data3 = id;
213         lpddi->guidProduct = DInput_Wine_Joystick_GUID;
214         /* we only support traditional joysticks for now */
215         if (version >= 0x0800)
216             lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
217         else
218             lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
219         sprintf(friendly, "Joystick %d", id);
220         MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
221 #if defined(JSIOCGNAME)
222         if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
223             WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
224             strcpy(name, "Wine Joystick");
225         }
226 #else
227         strcpy(name, "Wine Joystick");
228 #endif
229         MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
230         lpddi->guidFFDriver = GUID_NULL;
231         close(fd);
232         TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], name);
233         return TRUE;
234     }
235
236     return FALSE;
237 }
238
239 /*
240  * Setup the dinput options.
241  */
242
243 static HRESULT setup_dinput_options(JoystickImpl * device)
244 {
245     char buffer[MAX_PATH+16];
246     HKEY hkey, appkey;
247     int tokens = 0;
248     int axis = 0;
249     int pov = 0;
250
251     buffer[MAX_PATH]='\0';
252
253     get_app_key(&hkey, &appkey);
254
255     /* get options */
256
257     if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
258         device->generic.deadzone = atoi(buffer);
259         TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->generic.deadzone);
260     }
261
262     device->generic.axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
263     if (!device->generic.axis_map) return DIERR_OUTOFMEMORY;
264
265     if (!get_config_key( hkey, appkey, device->generic.name, buffer, MAX_PATH )) {
266         static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
267                                            "Slider1", "Slider2",
268                                            "POV1", "POV2", "POV3", "POV4"};
269         const char *delim = ",";
270         char * ptr;
271         TRACE("\"%s\" = \"%s\"\n", device->generic.name, buffer);
272
273         if ((ptr = strtok(buffer, delim)) != NULL) {
274             do {
275                 int i;
276
277                 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
278                     if (!strcmp(ptr, axis_names[i]))
279                     {
280                         if (!strncmp(ptr, "POV", 3))
281                         {
282                             if (pov >= 4)
283                             {
284                                 WARN("Only 4 POVs supported - ignoring extra\n");
285                                 i = -1;
286                             }
287                             else
288                             {
289                                 /* Pov takes two axes */
290                                 device->generic.axis_map[tokens++] = i;
291                                 pov++;
292                             }
293                         }
294                         else
295                         {
296                             if (axis >= 8)
297                             {
298                                 FIXME("Only 8 Axes supported - ignoring extra\n");
299                                 i = -1;
300                             }
301                             else
302                                 axis++;
303                         }
304                         break;
305                     }
306
307                 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
308                 {
309                     ERR("invalid joystick axis type: \"%s\"\n", ptr);
310                     i = -1;
311                 }
312
313                 device->generic.axis_map[tokens] = i;
314                 tokens++;
315             } while ((ptr = strtok(NULL, delim)) != NULL);
316
317             if (tokens != device->axes) {
318                 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
319                 while (tokens < device->axes) {
320                     device->generic.axis_map[tokens] = -1;
321                     tokens++;
322                 }
323             }
324         }
325     }
326     else
327     {
328         for (tokens = 0; tokens < device->axes; tokens++)
329         {
330             if (tokens < 8)
331                 device->generic.axis_map[tokens] = axis++;
332             else if (tokens < 16)
333             {
334                 device->generic.axis_map[tokens++] = 8 + pov;
335                 device->generic.axis_map[tokens  ] = 8 + pov++;
336             }
337             else
338                 device->generic.axis_map[tokens] = -1;
339         }
340     }
341     device->generic.devcaps.dwAxes = axis;
342     device->generic.devcaps.dwPOVs = pov;
343
344     if (appkey)
345         RegCloseKey( appkey );
346
347     if (hkey)
348         RegCloseKey( hkey );
349
350     return DI_OK;
351 }
352
353 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
354     LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
355 {
356     DWORD i;
357     JoystickImpl* newDevice;
358     char name[MAX_PATH];
359     HRESULT hr;
360     LPDIDATAFORMAT df = NULL;
361     int idx = 0;
362
363     TRACE("%s %p %p %p %hu\n", debugstr_guid(rguid), jvt, dinput, pdev, index);
364
365     newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
366     if (newDevice == 0) {
367         WARN("out of memory\n");
368         *pdev = 0;
369         return DIERR_OUTOFMEMORY;
370     }
371
372     if (!lstrcpynA(newDevice->dev, joystick_devices[index], sizeof(newDevice->dev)) ||
373         (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
374     {
375         WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
376         HeapFree(GetProcessHeap(), 0, newDevice);
377         return DIERR_DEVICENOTREG;
378     }
379
380     newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
381     newDevice->generic.guidInstance.Data3 = index;
382     newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
383     newDevice->generic.joy_polldev = joy_polldev;
384
385     /* get the device name */
386 #if defined(JSIOCGNAME)
387     if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
388         WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
389         strcpy(name, "Wine Joystick");
390     }
391 #else
392     strcpy(name, "Wine Joystick");
393 #endif
394
395     /* copy the device name */
396     newDevice->generic.name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
397     strcpy(newDevice->generic.name, name);
398
399 #ifdef JSIOCGAXES
400     if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
401         WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
402         newDevice->axes = 2;
403     }
404 #endif
405 #ifdef JSIOCGBUTTONS
406     if (ioctl(newDevice->joyfd, JSIOCGBUTTONS, &newDevice->generic.devcaps.dwButtons) < 0) {
407         WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
408         newDevice->generic.devcaps.dwButtons = 2;
409     }
410 #endif
411
412     if (newDevice->generic.devcaps.dwButtons > 128)
413     {
414         WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
415         newDevice->generic.devcaps.dwButtons = 128;
416     }
417
418     newDevice->generic.base.lpVtbl = jvt;
419     newDevice->generic.base.ref = 1;
420     newDevice->generic.base.dinput = dinput;
421     newDevice->generic.base.guid = *rguid;
422     InitializeCriticalSection(&newDevice->generic.base.crit);
423     newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
424
425     /* setup_dinput_options may change these */
426     newDevice->generic.deadzone = 0;
427
428     /* do any user specified configuration */
429     hr = setup_dinput_options(newDevice);
430     if (hr != DI_OK)
431         goto FAILED1;
432
433     /* Create copy of default data format */
434     if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
435     memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
436
437     df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
438     if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
439
440     for (i = 0; i < newDevice->axes; i++)
441     {
442         int wine_obj = newDevice->generic.axis_map[i];
443
444         if (wine_obj < 0) continue;
445
446         memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
447         if (wine_obj < 8)
448             df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
449         else
450         {
451             df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
452             i++; /* POV takes 2 axes */
453         }
454     }
455     for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
456     {
457         memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
458         df->rgodf[idx  ].pguid = &GUID_Button;
459         df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
460     }
461     newDevice->generic.base.data_format.wine_df = df;
462
463     /* initialize default properties */
464     for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
465         newDevice->generic.props[i].lDevMin = -32767;
466         newDevice->generic.props[i].lDevMax = +32767;
467         newDevice->generic.props[i].lMin = 0;
468         newDevice->generic.props[i].lMax = 0xffff;
469         newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
470         newDevice->generic.props[i].lSaturation = 0;
471     }
472
473     IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->generic.base.dinput);
474
475     newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
476     newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
477     if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
478         newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
479     else
480         newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
481     newDevice->generic.devcaps.dwFFSamplePeriod = 0;
482     newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
483     newDevice->generic.devcaps.dwFirmwareRevision = 0;
484     newDevice->generic.devcaps.dwHardwareRevision = 0;
485     newDevice->generic.devcaps.dwFFDriverVersion = 0;
486
487     if (TRACE_ON(dinput)) {
488         _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
489        for (i = 0; i < (newDevice->axes); i++)
490            TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
491         _dump_DIDEVCAPS(&newDevice->generic.devcaps);
492     }
493
494     *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
495
496     return DI_OK;
497
498 FAILED:
499     hr = DIERR_OUTOFMEMORY;
500 FAILED1:
501     if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
502     HeapFree(GetProcessHeap(), 0, df);
503     release_DataFormat(&newDevice->generic.base.data_format);
504     HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
505     HeapFree(GetProcessHeap(),0,newDevice->generic.name);
506     HeapFree(GetProcessHeap(),0,newDevice);
507     *pdev = 0;
508
509     return hr;
510 }
511
512 /******************************************************************************
513   *     get_joystick_index : Get the joystick index from a given GUID
514   */
515 static unsigned short get_joystick_index(REFGUID guid)
516 {
517     GUID wine_joystick = DInput_Wine_Joystick_GUID;
518     GUID dev_guid = *guid;
519
520     wine_joystick.Data3 = 0;
521     dev_guid.Data3 = 0;
522
523     /* for the standard joystick GUID use index 0 */
524     if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
525
526     /* for the wine joystick GUIDs use the index stored in Data3 */
527     if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
528
529     return MAX_JOYSTICKS;
530 }
531
532 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
533 {
534     unsigned short index;
535
536     TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
537     find_joystick_devices();
538     *pdev = NULL;
539
540     if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
541         joystick_devices_count && index < joystick_devices_count)
542     {
543         if ((riid == NULL) ||
544             IsEqualGUID(&IID_IDirectInputDeviceA,  riid) ||
545             IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
546             IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
547             IsEqualGUID(&IID_IDirectInputDevice8A, riid))
548         {
549             return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
550         }
551
552         WARN("no interface\n");
553         return DIERR_NOINTERFACE;
554     }
555
556     return DIERR_DEVICENOTREG;
557 }
558
559 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
560 {
561     unsigned short index;
562
563     TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
564     find_joystick_devices();
565     *pdev = NULL;
566
567     if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
568         joystick_devices_count && index < joystick_devices_count)
569     {
570         if ((riid == NULL) ||
571             IsEqualGUID(&IID_IDirectInputDeviceW,  riid) ||
572             IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
573             IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
574             IsEqualGUID(&IID_IDirectInputDevice8W, riid))
575         {
576             return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
577         }
578         WARN("no interface\n");
579         return DIERR_NOINTERFACE;
580     }
581
582     WARN("invalid device GUID %s\n",debugstr_guid(rguid));
583     return DIERR_DEVICENOTREG;
584 }
585
586 #undef MAX_JOYSTICKS
587
588 const struct dinput_device joystick_linux_device = {
589   "Wine Linux joystick driver",
590   joydev_enum_deviceA,
591   joydev_enum_deviceW,
592   joydev_create_deviceA,
593   joydev_create_deviceW
594 };
595
596 /******************************************************************************
597   *     Acquire : gets exclusive control of the joystick
598   */
599 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
600 {
601     JoystickImpl *This = (JoystickImpl *)iface;
602     HRESULT res;
603
604     TRACE("(%p)\n",This);
605
606     res = IDirectInputDevice2AImpl_Acquire(iface);
607     if (res != DI_OK)
608         return res;
609
610     /* open the joystick device */
611     if (This->joyfd==-1) {
612         TRACE("opening joystick device %s\n", This->dev);
613
614         This->joyfd=open(This->dev,O_RDONLY);
615         if (This->joyfd==-1) {
616             ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
617             IDirectInputDevice2AImpl_Unacquire(iface);
618             return DIERR_NOTFOUND;
619         }
620     }
621
622     return DI_OK;
623 }
624
625 /******************************************************************************
626   *     Unacquire : frees the joystick
627   */
628 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
629 {
630     JoystickImpl *This = (JoystickImpl *)iface;
631     HRESULT res;
632
633     TRACE("(%p)\n",This);
634
635     res = IDirectInputDevice2AImpl_Unacquire(iface);
636
637     if (res != DI_OK)
638         return res;
639
640     if (This->joyfd!=-1) {
641         TRACE("closing joystick device\n");
642         close(This->joyfd);
643         This->joyfd = -1;
644         return DI_OK;
645     }
646
647     return DI_NOEFFECT;
648 }
649
650 static void joy_polldev(JoystickGenericImpl *This_in) {
651     struct pollfd plfd;
652     struct      js_event jse;
653     JoystickImpl *This = (JoystickImpl*) This_in;
654
655     TRACE("(%p)\n", This);
656
657     if (This->joyfd==-1) {
658         WARN("no device\n");
659         return;
660     }
661     while (1)
662     {
663         LONG value;
664         int inst_id = -1;
665
666         plfd.fd = This->joyfd;
667         plfd.events = POLLIN;
668         if (poll(&plfd,1,0) != 1)
669             return;
670         /* we have one event, so we can read */
671         if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
672             return;
673         }
674         TRACE("js_event: type 0x%x, number %d, value %d\n",
675               jse.type,jse.number,jse.value);
676         if (jse.type & JS_EVENT_BUTTON)
677         {
678             if (jse.number >= This->generic.devcaps.dwButtons) return;
679
680             inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
681             This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
682         }
683         else if (jse.type & JS_EVENT_AXIS)
684         {
685             int number = This->generic.axis_map[jse.number];    /* wine format object index */
686
687             if (number < 0) return;
688             inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
689             value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
690
691             TRACE("changing axis %d => %d\n", jse.number, number);
692             switch (number)
693             {
694                 case 0: This->generic.js.lX  = value; break;
695                 case 1: This->generic.js.lY  = value; break;
696                 case 2: This->generic.js.lZ  = value; break;
697                 case 3: This->generic.js.lRx = value; break;
698                 case 4: This->generic.js.lRy = value; break;
699                 case 5: This->generic.js.lRz = value; break;
700                 case 6: This->generic.js.rglSlider[0] = value; break;
701                 case 7: This->generic.js.rglSlider[1] = value; break;
702                 case 8: case 9: case 10: case 11:
703                 {
704                     int idx = number - 8;
705
706                     if (jse.number % 2)
707                         This->povs[idx].y = jse.value;
708                     else
709                         This->povs[idx].x = jse.value;
710
711                     This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
712                     break;
713                 }
714                 default:
715                     WARN("axis %d not supported\n", number);
716             }
717         }
718         if (inst_id >= 0)
719             queue_event((LPDIRECTINPUTDEVICE8A)This,
720                         id_to_offset(&This->generic.base.data_format, inst_id),
721                         value, jse.time, This->generic.base.dinput->evsequence++);
722     }
723 }
724
725 static const IDirectInputDevice8AVtbl JoystickAvt =
726 {
727         IDirectInputDevice2AImpl_QueryInterface,
728         IDirectInputDevice2AImpl_AddRef,
729         IDirectInputDevice2AImpl_Release,
730         JoystickAGenericImpl_GetCapabilities,
731         IDirectInputDevice2AImpl_EnumObjects,
732         JoystickAGenericImpl_GetProperty,
733         JoystickAGenericImpl_SetProperty,
734         JoystickLinuxAImpl_Acquire,
735         JoystickLinuxAImpl_Unacquire,
736         JoystickAGenericImpl_GetDeviceState,
737         IDirectInputDevice2AImpl_GetDeviceData,
738         IDirectInputDevice2AImpl_SetDataFormat,
739         IDirectInputDevice2AImpl_SetEventNotification,
740         IDirectInputDevice2AImpl_SetCooperativeLevel,
741         JoystickAGenericImpl_GetObjectInfo,
742         JoystickAGenericImpl_GetDeviceInfo,
743         IDirectInputDevice2AImpl_RunControlPanel,
744         IDirectInputDevice2AImpl_Initialize,
745         IDirectInputDevice2AImpl_CreateEffect,
746         IDirectInputDevice2AImpl_EnumEffects,
747         IDirectInputDevice2AImpl_GetEffectInfo,
748         IDirectInputDevice2AImpl_GetForceFeedbackState,
749         IDirectInputDevice2AImpl_SendForceFeedbackCommand,
750         IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
751         IDirectInputDevice2AImpl_Escape,
752         JoystickAGenericImpl_Poll,
753         IDirectInputDevice2AImpl_SendDeviceData,
754         IDirectInputDevice7AImpl_EnumEffectsInFile,
755         IDirectInputDevice7AImpl_WriteEffectToFile,
756         IDirectInputDevice8AImpl_BuildActionMap,
757         IDirectInputDevice8AImpl_SetActionMap,
758         IDirectInputDevice8AImpl_GetImageInfo
759 };
760
761 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
762 # define XCAST(fun)     (typeof(JoystickWvt.fun))
763 #else
764 # define XCAST(fun)     (void*)
765 #endif
766
767 static const IDirectInputDevice8WVtbl JoystickWvt =
768 {
769         IDirectInputDevice2WImpl_QueryInterface,
770         XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
771         XCAST(Release)IDirectInputDevice2AImpl_Release,
772         XCAST(GetCapabilities)JoystickAGenericImpl_GetCapabilities,
773         IDirectInputDevice2WImpl_EnumObjects,
774         XCAST(GetProperty)JoystickAGenericImpl_GetProperty,
775         XCAST(SetProperty)JoystickAGenericImpl_SetProperty,
776         XCAST(Acquire)JoystickLinuxAImpl_Acquire,
777         XCAST(Unacquire)JoystickLinuxAImpl_Unacquire,
778         XCAST(GetDeviceState)JoystickAGenericImpl_GetDeviceState,
779         XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
780         XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
781         XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
782         XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
783         JoystickWGenericImpl_GetObjectInfo,
784         JoystickWGenericImpl_GetDeviceInfo,
785         XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
786         XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
787         XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
788         IDirectInputDevice2WImpl_EnumEffects,
789         IDirectInputDevice2WImpl_GetEffectInfo,
790         XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
791         XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
792         XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
793         XCAST(Escape)IDirectInputDevice2AImpl_Escape,
794         XCAST(Poll)JoystickAGenericImpl_Poll,
795         XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
796         IDirectInputDevice7WImpl_EnumEffectsInFile,
797         IDirectInputDevice7WImpl_WriteEffectToFile,
798         IDirectInputDevice8WImpl_BuildActionMap,
799         IDirectInputDevice8WImpl_SetActionMap,
800         IDirectInputDevice8WImpl_GetImageInfo
801 };
802 #undef XCAST
803
804 #else  /* HAVE_LINUX_22_JOYSTICK_API */
805
806 const struct dinput_device joystick_linux_device = {
807   "Wine Linux joystick driver",
808   NULL,
809   NULL,
810   NULL,
811   NULL
812 };
813
814 #endif  /* HAVE_LINUX_22_JOYSTICK_API */