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