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