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