winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / dinput / joystick_linux.c
blob9921a28d7bab690c1001607be385ab870efcb3f2
1 /* DirectInput Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
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.
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.
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
23 * To Do:
24 * dead zone
25 * force feedback
28 #include "config.h"
29 #include "wine/port.h"
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
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"
64 #include "dinput_private.h"
65 #include "device_private.h"
66 #include "joystick_private.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
70 #ifdef HAVE_LINUX_22_JOYSTICK_API
72 #define JOYDEV_NEW "/dev/input/js"
73 #define JOYDEV_OLD "/dev/js"
74 #define JOYDEVDRIVER " (js)"
76 struct JoyDev
78 char device[MAX_PATH];
79 char name[MAX_PATH];
81 BYTE axis_count;
82 BYTE button_count;
83 int *dev_axes_map;
86 typedef struct JoystickImpl JoystickImpl;
87 static const IDirectInputDevice8AVtbl JoystickAvt;
88 static const IDirectInputDevice8WVtbl JoystickWvt;
89 struct JoystickImpl
91 struct JoystickGenericImpl generic;
93 struct JoyDev *joydev;
95 /* joystick private */
96 int joyfd;
97 POINTL povs[4];
100 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
102 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
103 JoystickGenericImpl, base), JoystickImpl, generic);
105 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
107 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
108 JoystickGenericImpl, base), JoystickImpl, generic);
111 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
113 return &This->generic.base.IDirectInputDevice8W_iface;
116 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
117 0x9e573ed9,
118 0x7734,
119 0x11d2,
120 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
123 #define MAX_JOYSTICKS 64
124 static INT joystick_devices_count = -1;
125 static struct JoyDev *joystick_devices;
127 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
129 static INT find_joystick_devices(void)
131 INT i;
133 if (joystick_devices_count != -1) return joystick_devices_count;
135 joystick_devices_count = 0;
136 for (i = 0; i < MAX_JOYSTICKS; i++)
138 int fd;
139 struct JoyDev joydev, *new_joydevs;
140 BYTE axes_map[ABS_MAX + 1];
142 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_NEW, i);
143 if ((fd = open(joydev.device, O_RDONLY)) < 0)
145 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_OLD, i);
146 if ((fd = open(joydev.device, O_RDONLY)) < 0) continue;
149 strcpy(joydev.name, "Wine Joystick");
150 #if defined(JSIOCGNAME)
151 if (ioctl(fd, JSIOCGNAME(sizeof(joydev.name) - sizeof(JOYDEVDRIVER)), joydev.name) < 0)
152 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
153 #endif
155 /* Append driver name */
156 strcat(joydev.name, JOYDEVDRIVER);
158 if (device_disabled_registry(joydev.name)) {
159 close(fd);
160 continue;
163 #ifdef JSIOCGAXES
164 if (ioctl(fd, JSIOCGAXES, &joydev.axis_count) < 0)
166 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", joydev.device, strerror(errno));
167 joydev.axis_count = 2;
169 #else
170 WARN("reading number of joystick axes unsupported in this platform, defaulting to 2\n");
171 joydev.axis_count = 2;
172 #endif
173 #ifdef JSIOCGBUTTONS
174 if (ioctl(fd, JSIOCGBUTTONS, &joydev.button_count) < 0)
176 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", joydev.device, strerror(errno));
177 joydev.button_count = 2;
179 #else
180 WARN("reading number of joystick buttons unsupported in this platform, defaulting to 2\n");
181 joydev.button_count = 2;
182 #endif
184 if (ioctl(fd, JSIOCGAXMAP, axes_map) < 0)
186 WARN("ioctl(%s,JSIOCGAXMAP) failed: %s\n", joydev.device, strerror(errno));
187 joydev.dev_axes_map = NULL;
189 else
190 if ((joydev.dev_axes_map = HeapAlloc(GetProcessHeap(), 0, joydev.axis_count * sizeof(int))))
192 INT j;
194 /* Remap to DI numbers */
195 for (j = 0; j < joydev.axis_count; j++)
196 if (axes_map[j] < 8)
197 /* Axis match 1-to-1 */
198 joydev.dev_axes_map[j] = j;
199 else if (axes_map[j] == 16 ||
200 axes_map[j] == 17)
201 /* POV axis */
202 joydev.dev_axes_map[j] = 8;
203 else
204 joydev.dev_axes_map[j] = -1;
207 close(fd);
209 if (!joystick_devices_count)
210 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
211 else
212 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
213 (joystick_devices_count + 1) * sizeof(struct JoyDev));
214 if (!new_joydevs) continue;
216 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev.device,
217 joydev.name, joydev.axis_count, joydev.button_count);
219 joystick_devices = new_joydevs;
220 joystick_devices[joystick_devices_count++] = joydev;
223 return joystick_devices_count;
226 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
228 DWORD dwSize = lpddi->dwSize;
230 TRACE("%d %p\n", dwSize, lpddi);
231 memset(lpddi, 0, dwSize);
233 /* Return joystick */
234 lpddi->dwSize = dwSize;
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 strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
244 strcpy(lpddi->tszProductName, joystick_devices[id].name);
246 lpddi->guidFFDriver = GUID_NULL;
249 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
251 DWORD dwSize = lpddi->dwSize;
253 TRACE("%d %p\n", dwSize, lpddi);
254 memset(lpddi, 0, dwSize);
256 /* Return joystick */
257 lpddi->dwSize = dwSize;
258 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
259 lpddi->guidInstance.Data3 = id;
260 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
261 /* we only support traditional joysticks for now */
262 if (version >= 0x0800)
263 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
264 else
265 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
267 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
268 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
269 lpddi->guidFFDriver = GUID_NULL;
272 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
274 int fd = -1;
276 if (id >= find_joystick_devices()) return E_FAIL;
278 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
279 WARN("force feedback not supported\n");
280 return S_FALSE;
283 if ((dwDevType == 0) ||
284 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
285 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
286 /* check whether we have a joystick */
287 if ((fd = open(joystick_devices[id].device, O_RDONLY)) < 0)
289 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].name, strerror(errno));
290 return S_FALSE;
292 fill_joystick_dideviceinstanceA( lpddi, version, id );
293 close(fd);
294 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, lpddi->tszProductName);
295 return S_OK;
298 return S_FALSE;
301 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
303 int fd = -1;
305 if (id >= find_joystick_devices()) return E_FAIL;
307 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
308 WARN("force feedback not supported\n");
309 return S_FALSE;
312 if ((dwDevType == 0) ||
313 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
314 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
315 /* check whether we have a joystick */
316 if ((fd = open(joystick_devices[id].device, O_RDONLY)) < 0)
318 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
319 return S_FALSE;
321 fill_joystick_dideviceinstanceW( lpddi, version, id );
322 close(fd);
323 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
324 return S_OK;
327 return S_FALSE;
330 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
331 JoystickImpl **pdev, unsigned short index)
333 DWORD i;
334 JoystickImpl* newDevice;
335 HRESULT hr;
336 LPDIDATAFORMAT df = NULL;
337 int idx = 0;
339 TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
341 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
342 if (newDevice == 0) {
343 WARN("out of memory\n");
344 *pdev = 0;
345 return DIERR_OUTOFMEMORY;
348 newDevice->joydev = &joystick_devices[index];
349 newDevice->joyfd = -1;
350 newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
351 newDevice->generic.guidInstance.Data3 = index;
352 newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
353 newDevice->generic.joy_polldev = joy_polldev;
354 newDevice->generic.name = newDevice->joydev->name;
355 newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
356 newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
358 if (newDevice->generic.devcaps.dwButtons > 128)
360 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
361 newDevice->generic.devcaps.dwButtons = 128;
364 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
365 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
366 newDevice->generic.base.ref = 1;
367 newDevice->generic.base.dinput = dinput;
368 newDevice->generic.base.guid = *rguid;
369 InitializeCriticalSection(&newDevice->generic.base.crit);
370 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
372 /* setup_dinput_options may change these */
373 newDevice->generic.deadzone = 0;
375 /* do any user specified configuration */
376 hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
377 if (hr != DI_OK)
378 goto FAILED1;
380 /* Create copy of default data format */
381 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
382 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
384 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
385 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
387 for (i = 0; i < newDevice->generic.device_axis_count; i++)
389 int wine_obj = newDevice->generic.axis_map[i];
391 if (wine_obj < 0) continue;
393 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
394 if (wine_obj < 8)
395 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
396 else
398 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
399 i++; /* POV takes 2 axes */
402 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
404 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
405 df->rgodf[idx ].pguid = &GUID_Button;
406 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
408 newDevice->generic.base.data_format.wine_df = df;
410 /* initialize default properties */
411 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
412 newDevice->generic.props[i].lDevMin = -32767;
413 newDevice->generic.props[i].lDevMax = +32767;
414 newDevice->generic.props[i].lMin = 0;
415 newDevice->generic.props[i].lMax = 0xffff;
416 newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
417 newDevice->generic.props[i].lSaturation = 0;
420 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
422 EnterCriticalSection(&dinput->crit);
423 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
424 LeaveCriticalSection(&dinput->crit);
426 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
427 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
428 if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
429 newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
430 else
431 newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
432 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
433 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
434 newDevice->generic.devcaps.dwFirmwareRevision = 0;
435 newDevice->generic.devcaps.dwHardwareRevision = 0;
436 newDevice->generic.devcaps.dwFFDriverVersion = 0;
438 if (TRACE_ON(dinput)) {
439 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
440 for (i = 0; i < (newDevice->generic.device_axis_count); i++)
441 TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
442 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
445 *pdev = newDevice;
447 return DI_OK;
449 FAILED:
450 hr = DIERR_OUTOFMEMORY;
451 FAILED1:
452 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
453 HeapFree(GetProcessHeap(), 0, df);
454 release_DataFormat(&newDevice->generic.base.data_format);
455 HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
456 HeapFree(GetProcessHeap(),0,newDevice);
457 *pdev = 0;
459 return hr;
462 /******************************************************************************
463 * get_joystick_index : Get the joystick index from a given GUID
465 static unsigned short get_joystick_index(REFGUID guid)
467 GUID wine_joystick = DInput_Wine_Joystick_GUID;
468 GUID dev_guid = *guid;
470 wine_joystick.Data3 = 0;
471 dev_guid.Data3 = 0;
473 /* for the standard joystick GUID use index 0 */
474 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
476 /* for the wine joystick GUIDs use the index stored in Data3 */
477 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
479 return MAX_JOYSTICKS;
482 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
484 unsigned short index;
486 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
487 find_joystick_devices();
488 *pdev = NULL;
490 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
491 joystick_devices_count && index < joystick_devices_count)
493 JoystickImpl *This;
494 HRESULT hr;
496 if (riid == NULL)
497 ;/* nothing */
498 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
499 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
500 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
501 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
503 unicode = 0;
505 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
506 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
507 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
508 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
510 unicode = 1;
512 else
514 WARN("no interface\n");
515 return DIERR_NOINTERFACE;
518 hr = alloc_device(rguid, dinput, &This, index);
519 if (!This) return hr;
521 if (unicode)
522 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
523 else
524 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
526 return hr;
529 return DIERR_DEVICENOTREG;
532 #undef MAX_JOYSTICKS
534 const struct dinput_device joystick_linux_device = {
535 "Wine Linux joystick driver",
536 joydev_enum_deviceA,
537 joydev_enum_deviceW,
538 joydev_create_device
541 /******************************************************************************
542 * Acquire : gets exclusive control of the joystick
544 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
546 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
547 HRESULT res;
549 TRACE("(%p)\n",This);
551 res = IDirectInputDevice2WImpl_Acquire(iface);
552 if (res != DI_OK)
553 return res;
555 /* open the joystick device */
556 if (This->joyfd==-1) {
557 TRACE("opening joystick device %s\n", This->joydev->device);
559 This->joyfd = open(This->joydev->device, O_RDONLY);
560 if (This->joyfd==-1) {
561 ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
562 IDirectInputDevice2WImpl_Unacquire(iface);
563 return DIERR_NOTFOUND;
567 return DI_OK;
570 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
572 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
573 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
576 /******************************************************************************
577 * GetProperty : get input device properties
579 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
581 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
583 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
584 _dump_DIPROPHEADER(pdiph);
586 if (!IS_DIPROP(rguid)) return DI_OK;
588 switch (LOWORD(rguid)) {
590 case (DWORD_PTR) DIPROP_JOYSTICKID:
592 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
594 pd->dwData = get_joystick_index(&This->generic.base.guid);
595 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
596 break;
599 default:
600 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
603 return DI_OK;
606 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
608 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
609 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
612 /******************************************************************************
613 * GetDeviceInfo : get information about a device's identity
615 static HRESULT WINAPI JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface, LPDIDEVICEINSTANCEA ddi)
617 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
619 TRACE("(%p) %p\n", This, ddi);
621 if (ddi == NULL) return E_POINTER;
622 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
623 (ddi->dwSize != sizeof(DIDEVICEINSTANCEA)))
624 return DIERR_INVALIDPARAM;
626 fill_joystick_dideviceinstanceA( ddi, This->generic.base.dinput->dwVersion,
627 get_joystick_index(&This->generic.base.guid) );
628 return DI_OK;
631 static HRESULT WINAPI JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface, LPDIDEVICEINSTANCEW ddi)
633 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
635 TRACE("(%p) %p\n", This, ddi);
637 if (ddi == NULL) return E_POINTER;
638 if ((ddi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
639 (ddi->dwSize != sizeof(DIDEVICEINSTANCEW)))
640 return DIERR_INVALIDPARAM;
642 fill_joystick_dideviceinstanceW( ddi, This->generic.base.dinput->dwVersion,
643 get_joystick_index(&This->generic.base.guid) );
644 return DI_OK;
647 /******************************************************************************
648 * Unacquire : frees the joystick
650 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
652 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
653 HRESULT res;
655 TRACE("(%p)\n",This);
657 res = IDirectInputDevice2WImpl_Unacquire(iface);
659 if (res != DI_OK)
660 return res;
662 if (This->joyfd!=-1) {
663 TRACE("closing joystick device\n");
664 close(This->joyfd);
665 This->joyfd = -1;
666 return DI_OK;
669 return DI_NOEFFECT;
672 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
674 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
675 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
678 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
680 struct pollfd plfd;
681 struct js_event jse;
682 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
684 TRACE("(%p)\n", This);
686 if (This->joyfd==-1) {
687 WARN("no device\n");
688 return;
690 while (1)
692 LONG value;
693 int inst_id = -1;
695 plfd.fd = This->joyfd;
696 plfd.events = POLLIN;
697 if (poll(&plfd,1,0) != 1)
698 return;
699 /* we have one event, so we can read */
700 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
701 return;
703 TRACE("js_event: type 0x%x, number %d, value %d\n",
704 jse.type,jse.number,jse.value);
705 if (jse.type & JS_EVENT_BUTTON)
707 if (jse.number >= This->generic.devcaps.dwButtons) return;
709 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
710 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
712 else if (jse.type & JS_EVENT_AXIS)
714 int number = This->generic.axis_map[jse.number]; /* wine format object index */
716 if (number < 0) return;
717 inst_id = number < 8 ? DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
718 DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
719 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
721 TRACE("changing axis %d => %d\n", jse.number, number);
722 switch (number)
724 case 0: This->generic.js.lX = value; break;
725 case 1: This->generic.js.lY = value; break;
726 case 2: This->generic.js.lZ = value; break;
727 case 3: This->generic.js.lRx = value; break;
728 case 4: This->generic.js.lRy = value; break;
729 case 5: This->generic.js.lRz = value; break;
730 case 6: This->generic.js.rglSlider[0] = value; break;
731 case 7: This->generic.js.rglSlider[1] = value; break;
732 case 8: case 9: case 10: case 11:
734 int idx = number - 8;
736 if (jse.number % 2)
737 This->povs[idx].y = jse.value;
738 else
739 This->povs[idx].x = jse.value;
741 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
742 break;
744 default:
745 WARN("axis %d not supported\n", number);
748 if (inst_id >= 0)
749 queue_event(iface, inst_id, value, GetCurrentTime(), This->generic.base.dinput->evsequence++);
753 static const IDirectInputDevice8AVtbl JoystickAvt =
755 IDirectInputDevice2AImpl_QueryInterface,
756 IDirectInputDevice2AImpl_AddRef,
757 IDirectInputDevice2AImpl_Release,
758 JoystickAGenericImpl_GetCapabilities,
759 IDirectInputDevice2AImpl_EnumObjects,
760 JoystickLinuxAImpl_GetProperty,
761 JoystickAGenericImpl_SetProperty,
762 JoystickLinuxAImpl_Acquire,
763 JoystickLinuxAImpl_Unacquire,
764 JoystickAGenericImpl_GetDeviceState,
765 IDirectInputDevice2AImpl_GetDeviceData,
766 IDirectInputDevice2AImpl_SetDataFormat,
767 IDirectInputDevice2AImpl_SetEventNotification,
768 IDirectInputDevice2AImpl_SetCooperativeLevel,
769 JoystickAGenericImpl_GetObjectInfo,
770 JoystickLinuxAImpl_GetDeviceInfo,
771 IDirectInputDevice2AImpl_RunControlPanel,
772 IDirectInputDevice2AImpl_Initialize,
773 IDirectInputDevice2AImpl_CreateEffect,
774 IDirectInputDevice2AImpl_EnumEffects,
775 IDirectInputDevice2AImpl_GetEffectInfo,
776 IDirectInputDevice2AImpl_GetForceFeedbackState,
777 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
778 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
779 IDirectInputDevice2AImpl_Escape,
780 JoystickAGenericImpl_Poll,
781 IDirectInputDevice2AImpl_SendDeviceData,
782 IDirectInputDevice7AImpl_EnumEffectsInFile,
783 IDirectInputDevice7AImpl_WriteEffectToFile,
784 JoystickAGenericImpl_BuildActionMap,
785 JoystickAGenericImpl_SetActionMap,
786 IDirectInputDevice8AImpl_GetImageInfo
789 static const IDirectInputDevice8WVtbl JoystickWvt =
791 IDirectInputDevice2WImpl_QueryInterface,
792 IDirectInputDevice2WImpl_AddRef,
793 IDirectInputDevice2WImpl_Release,
794 JoystickWGenericImpl_GetCapabilities,
795 IDirectInputDevice2WImpl_EnumObjects,
796 JoystickLinuxWImpl_GetProperty,
797 JoystickWGenericImpl_SetProperty,
798 JoystickLinuxWImpl_Acquire,
799 JoystickLinuxWImpl_Unacquire,
800 JoystickWGenericImpl_GetDeviceState,
801 IDirectInputDevice2WImpl_GetDeviceData,
802 IDirectInputDevice2WImpl_SetDataFormat,
803 IDirectInputDevice2WImpl_SetEventNotification,
804 IDirectInputDevice2WImpl_SetCooperativeLevel,
805 JoystickWGenericImpl_GetObjectInfo,
806 JoystickLinuxWImpl_GetDeviceInfo,
807 IDirectInputDevice2WImpl_RunControlPanel,
808 IDirectInputDevice2WImpl_Initialize,
809 IDirectInputDevice2WImpl_CreateEffect,
810 IDirectInputDevice2WImpl_EnumEffects,
811 IDirectInputDevice2WImpl_GetEffectInfo,
812 IDirectInputDevice2WImpl_GetForceFeedbackState,
813 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
814 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
815 IDirectInputDevice2WImpl_Escape,
816 JoystickWGenericImpl_Poll,
817 IDirectInputDevice2WImpl_SendDeviceData,
818 IDirectInputDevice7WImpl_EnumEffectsInFile,
819 IDirectInputDevice7WImpl_WriteEffectToFile,
820 JoystickWGenericImpl_BuildActionMap,
821 JoystickWGenericImpl_SetActionMap,
822 IDirectInputDevice8WImpl_GetImageInfo
825 #else /* HAVE_LINUX_22_JOYSTICK_API */
827 const struct dinput_device joystick_linux_device = {
828 "Wine Linux joystick driver",
829 NULL,
830 NULL,
831 NULL
834 #endif /* HAVE_LINUX_22_JOYSTICK_API */