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
28 #include "dinput_private.h"
33 #ifdef HAVE_SYS_TIME_H
34 # include <sys/time.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
39 #ifdef HAVE_LINUX_IOCTL_H
40 # include <linux/ioctl.h>
42 #ifdef HAVE_LINUX_JOYSTICK_H
43 # include <linux/joystick.h>
46 #ifdef HAVE_SYS_POLL_H
47 # include <sys/poll.h>
50 #ifdef HAVE_LINUX_22_JOYSTICK_API
52 #define JOYDEV_NEW "/dev/input/js"
53 #define JOYDEV_OLD "/dev/js"
54 #define JOYDEVDRIVER " (js)"
58 char device
[MAX_PATH
];
66 WORD vendor_id
, product_id
, bus_type
;
69 typedef struct JoystickImpl JoystickImpl
;
70 static const IDirectInputDevice8AVtbl JoystickAvt
;
71 static const IDirectInputDevice8WVtbl JoystickWvt
;
74 struct JoystickGenericImpl generic
;
76 struct JoyDev
*joydev
;
78 /* joystick private */
83 static inline JoystickImpl
*impl_from_IDirectInputDevice8A(IDirectInputDevice8A
*iface
)
85 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface
, IDirectInputDeviceImpl
, IDirectInputDevice8A_iface
),
86 JoystickGenericImpl
, base
), JoystickImpl
, generic
);
88 static inline JoystickImpl
*impl_from_IDirectInputDevice8W(IDirectInputDevice8W
*iface
)
90 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface
, IDirectInputDeviceImpl
, IDirectInputDevice8W_iface
),
91 JoystickGenericImpl
, base
), JoystickImpl
, generic
);
94 static inline IDirectInputDevice8W
*IDirectInputDevice8W_from_impl(JoystickImpl
*This
)
96 return &This
->generic
.base
.IDirectInputDevice8W_iface
;
99 static const GUID DInput_Wine_Joystick_GUID
= { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
103 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
107 * Construct the GUID in the same way of Windows doing this.
108 * Data1 is concatenation of productid and vendorid.
109 * Data2 and Data3 are NULL.
110 * Data4 seems to be a constant.
112 static const GUID DInput_Wine_Joystick_Constant_Part_GUID
= {
116 {0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44}
119 #define MAX_JOYSTICKS 64
120 static INT joystick_devices_count
= -1;
121 static struct JoyDev
*joystick_devices
;
123 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface
);
125 #define SYS_PATH_FORMAT "/sys/class/input/js%d/device/id/%s"
126 static BOOL
read_sys_id_variable(int index
, const char *property
, WORD
*value
)
128 char sys_path
[sizeof(SYS_PATH_FORMAT
) + 16], id_str
[5];
132 sprintf(sys_path
, SYS_PATH_FORMAT
, index
, property
);
133 if ((sys_fd
= open(sys_path
, O_RDONLY
)) != -1)
135 if (read(sys_fd
, id_str
, 4) == 4)
138 *value
= strtol(id_str
, NULL
, 16);
146 #undef SYS_PATH_FORMAT
148 static INT
find_joystick_devices(void)
152 if (joystick_devices_count
!= -1) return joystick_devices_count
;
154 joystick_devices_count
= 0;
155 for (i
= 0; i
< MAX_JOYSTICKS
; i
++)
158 struct JoyDev joydev
, *new_joydevs
;
159 BYTE axes_map
[ABS_MAX
+ 1];
161 snprintf(joydev
.device
, sizeof(joydev
.device
), "%s%d", JOYDEV_NEW
, i
);
162 if ((fd
= open(joydev
.device
, O_RDONLY
)) == -1)
164 snprintf(joydev
.device
, sizeof(joydev
.device
), "%s%d", JOYDEV_OLD
, i
);
165 if ((fd
= open(joydev
.device
, O_RDONLY
)) == -1) continue;
168 strcpy(joydev
.name
, "Wine Joystick");
169 #if defined(JSIOCGNAME)
170 if (ioctl(fd
, JSIOCGNAME(sizeof(joydev
.name
) - sizeof(JOYDEVDRIVER
)), joydev
.name
) < 0)
171 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev
.device
, strerror(errno
));
174 /* Append driver name */
175 strcat(joydev
.name
, JOYDEVDRIVER
);
177 if (device_disabled_registry(joydev
.name
)) {
183 if (ioctl(fd
, JSIOCGAXES
, &joydev
.axis_count
) < 0)
185 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defaulting to 2\n", joydev
.device
, strerror(errno
));
186 joydev
.axis_count
= 2;
189 WARN("reading number of joystick axes unsupported in this platform, defaulting to 2\n");
190 joydev
.axis_count
= 2;
193 if (ioctl(fd
, JSIOCGBUTTONS
, &joydev
.button_count
) < 0)
195 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defaulting to 2\n", joydev
.device
, strerror(errno
));
196 joydev
.button_count
= 2;
199 WARN("reading number of joystick buttons unsupported in this platform, defaulting to 2\n");
200 joydev
.button_count
= 2;
203 if (ioctl(fd
, JSIOCGAXMAP
, axes_map
) < 0)
205 WARN("ioctl(%s,JSIOCGAXMAP) failed: %s\n", joydev
.device
, strerror(errno
));
206 joydev
.dev_axes_map
= NULL
;
209 if ((joydev
.dev_axes_map
= HeapAlloc(GetProcessHeap(), 0, joydev
.axis_count
* sizeof(int))))
211 INT j
, found_axes
= 0;
213 /* Remap to DI numbers */
214 for (j
= 0; j
< joydev
.axis_count
; j
++)
218 /* Axis match 1-to-1 */
219 joydev
.dev_axes_map
[j
] = j
;
222 else if (axes_map
[j
] == 16 ||
226 joydev
.dev_axes_map
[j
] = 8;
230 joydev
.dev_axes_map
[j
] = -1;
233 /* If no axes were configured but there are axes assume a 1-to-1 (wii controller) */
234 if (joydev
.axis_count
&& !found_axes
)
236 int axes_limit
= min(joydev
.axis_count
, 8); /* generic driver limit */
238 ERR("Incoherent joystick data, advertised %d axes, detected 0. Assuming 1-to-1.\n",
240 for (j
= 0; j
< axes_limit
; j
++)
241 joydev
.dev_axes_map
[j
] = j
;
243 joydev
.axis_count
= axes_limit
;
247 /* Find vendor_id and product_id in sysfs */
248 joydev
.vendor_id
= 0;
249 joydev
.product_id
= 0;
251 read_sys_id_variable(i
, "vendor", &joydev
.vendor_id
);
252 read_sys_id_variable(i
, "product", &joydev
.product_id
);
253 read_sys_id_variable(i
, "bustype", &joydev
.bus_type
);
255 if (joydev
.vendor_id
== 0 || joydev
.product_id
== 0)
257 joydev
.guid_product
= DInput_Wine_Joystick_GUID
;
261 /* Concatenate product_id with vendor_id to mimic Windows behaviour */
262 joydev
.guid_product
= DInput_Wine_Joystick_Constant_Part_GUID
;
263 joydev
.guid_product
.Data1
= MAKELONG(joydev
.vendor_id
, joydev
.product_id
);
268 if (!joystick_devices_count
)
269 new_joydevs
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev
));
271 new_joydevs
= HeapReAlloc(GetProcessHeap(), 0, joystick_devices
,
272 (joystick_devices_count
+ 1) * sizeof(struct JoyDev
));
273 if (!new_joydevs
) continue;
275 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev
.device
,
276 joydev
.name
, joydev
.axis_count
, joydev
.button_count
);
278 joystick_devices
= new_joydevs
;
279 joystick_devices
[joystick_devices_count
++] = joydev
;
282 return joystick_devices_count
;
285 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi
, DWORD version
, int id
)
287 DWORD dwSize
= lpddi
->dwSize
;
289 TRACE("%d %p\n", dwSize
, lpddi
);
290 memset(lpddi
, 0, dwSize
);
292 /* Return joystick */
293 lpddi
->dwSize
= dwSize
;
294 lpddi
->guidInstance
= DInput_Wine_Joystick_GUID
;
295 lpddi
->guidInstance
.Data3
= id
;
296 lpddi
->guidProduct
= joystick_devices
[id
].guid_product
;
297 /* we only support traditional joysticks for now */
298 if (version
>= 0x0800)
299 lpddi
->dwDevType
= DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8);
301 lpddi
->dwDevType
= DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
303 /* Assume the joystick as HID if it is attached to USB bus and has a valid VID/PID */
304 if (joystick_devices
[id
].bus_type
== BUS_USB
&&
305 joystick_devices
[id
].vendor_id
&& joystick_devices
[id
].product_id
)
307 lpddi
->dwDevType
|= DIDEVTYPE_HID
;
308 lpddi
->wUsagePage
= 0x01; /* Desktop */
309 if (lpddi
->dwDevType
== DI8DEVTYPE_JOYSTICK
|| lpddi
->dwDevType
== DIDEVTYPE_JOYSTICK
)
310 lpddi
->wUsage
= 0x04; /* Joystick */
312 lpddi
->wUsage
= 0x05; /* Game Pad */
315 MultiByteToWideChar(CP_ACP
, 0, joystick_devices
[id
].name
, -1, lpddi
->tszInstanceName
, MAX_PATH
);
316 MultiByteToWideChar(CP_ACP
, 0, joystick_devices
[id
].name
, -1, lpddi
->tszProductName
, MAX_PATH
);
317 lpddi
->guidFFDriver
= GUID_NULL
;
320 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi
, DWORD version
, int id
)
322 DIDEVICEINSTANCEW lpddiW
;
323 DWORD dwSize
= lpddi
->dwSize
;
325 lpddiW
.dwSize
= sizeof(lpddiW
);
326 fill_joystick_dideviceinstanceW(&lpddiW
, version
, id
);
328 TRACE("%d %p\n", dwSize
, lpddi
);
329 memset(lpddi
, 0, dwSize
);
332 lpddi
->dwSize
= dwSize
;
333 lpddi
->guidInstance
= lpddiW
.guidInstance
;
334 lpddi
->guidProduct
= lpddiW
.guidProduct
;
335 lpddi
->dwDevType
= lpddiW
.dwDevType
;
336 strcpy(lpddi
->tszInstanceName
, joystick_devices
[id
].name
);
337 strcpy(lpddi
->tszProductName
, joystick_devices
[id
].name
);
338 lpddi
->guidFFDriver
= lpddiW
.guidFFDriver
;
339 lpddi
->wUsagePage
= lpddiW
.wUsagePage
;
340 lpddi
->wUsage
= lpddiW
.wUsage
;
343 static HRESULT
joydev_enum_deviceA(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEA lpddi
, DWORD version
, int id
)
347 if (id
>= find_joystick_devices()) return E_FAIL
;
349 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
350 WARN("force feedback not supported\n");
354 if ((dwDevType
== 0) ||
355 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
> 0x0300 && version
< 0x0800)) ||
356 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
357 /* check whether we have a joystick */
358 if ((fd
= open(joystick_devices
[id
].device
, O_RDONLY
)) == -1)
360 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices
[id
].device
, strerror(errno
));
363 fill_joystick_dideviceinstanceA( lpddi
, version
, id
);
365 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices
[id
].device
, joystick_devices
[id
].name
);
372 static HRESULT
joydev_enum_deviceW(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEW lpddi
, DWORD version
, int id
)
376 if (id
>= find_joystick_devices()) return E_FAIL
;
378 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
379 WARN("force feedback not supported\n");
383 if ((dwDevType
== 0) ||
384 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
> 0x0300 && version
< 0x0800)) ||
385 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
386 /* check whether we have a joystick */
387 if ((fd
= open(joystick_devices
[id
].device
, O_RDONLY
)) == -1)
389 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices
[id
].device
, strerror(errno
));
392 fill_joystick_dideviceinstanceW( lpddi
, version
, id
);
394 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices
[id
].device
, joystick_devices
[id
].name
);
401 static HRESULT
alloc_device(REFGUID rguid
, IDirectInputImpl
*dinput
,
402 JoystickImpl
**pdev
, unsigned short index
)
405 JoystickImpl
* newDevice
;
407 LPDIDATAFORMAT df
= NULL
;
409 DIDEVICEINSTANCEW ddi
;
411 TRACE("%s %p %p %hu\n", debugstr_guid(rguid
), dinput
, pdev
, index
);
413 newDevice
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(JoystickImpl
));
414 if (newDevice
== 0) {
415 WARN("out of memory\n");
417 return DIERR_OUTOFMEMORY
;
420 newDevice
->joydev
= &joystick_devices
[index
];
421 newDevice
->joyfd
= -1;
422 newDevice
->generic
.guidInstance
= DInput_Wine_Joystick_GUID
;
423 newDevice
->generic
.guidInstance
.Data3
= index
;
424 newDevice
->generic
.guidProduct
= DInput_Wine_Joystick_GUID
;
425 newDevice
->generic
.joy_polldev
= joy_polldev
;
426 newDevice
->generic
.name
= newDevice
->joydev
->name
;
427 newDevice
->generic
.device_axis_count
= newDevice
->joydev
->axis_count
;
428 newDevice
->generic
.devcaps
.dwButtons
= newDevice
->joydev
->button_count
;
430 if (newDevice
->generic
.devcaps
.dwButtons
> 128)
432 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice
->generic
.devcaps
.dwButtons
);
433 newDevice
->generic
.devcaps
.dwButtons
= 128;
436 newDevice
->generic
.base
.IDirectInputDevice8A_iface
.lpVtbl
= &JoystickAvt
;
437 newDevice
->generic
.base
.IDirectInputDevice8W_iface
.lpVtbl
= &JoystickWvt
;
438 newDevice
->generic
.base
.ref
= 1;
439 newDevice
->generic
.base
.dinput
= dinput
;
440 newDevice
->generic
.base
.guid
= *rguid
;
441 InitializeCriticalSection(&newDevice
->generic
.base
.crit
);
442 newDevice
->generic
.base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JoystickImpl*->generic.base.crit");
444 /* setup_dinput_options may change these */
445 newDevice
->generic
.deadzone
= 0;
447 /* do any user specified configuration */
448 hr
= setup_dinput_options(&newDevice
->generic
, newDevice
->joydev
->dev_axes_map
);
452 /* Create copy of default data format */
453 if (!(df
= HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2
.dwSize
))) goto FAILED
;
454 memcpy(df
, &c_dfDIJoystick2
, c_dfDIJoystick2
.dwSize
);
456 df
->dwNumObjs
= newDevice
->generic
.devcaps
.dwAxes
+ newDevice
->generic
.devcaps
.dwPOVs
+ newDevice
->generic
.devcaps
.dwButtons
;
457 if (!(df
->rgodf
= HeapAlloc(GetProcessHeap(), 0, df
->dwNumObjs
* df
->dwObjSize
))) goto FAILED
;
459 for (i
= 0; i
< newDevice
->generic
.device_axis_count
; i
++)
461 int wine_obj
= newDevice
->generic
.axis_map
[i
];
463 if (wine_obj
< 0) continue;
465 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[wine_obj
], df
->dwObjSize
);
467 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(wine_obj
) | DIDFT_ABSAXIS
;
470 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(wine_obj
- 8) | DIDFT_POV
;
471 i
++; /* POV takes 2 axes */
474 for (i
= 0; i
< newDevice
->generic
.devcaps
.dwButtons
; i
++)
476 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[i
+ 12], df
->dwObjSize
);
477 df
->rgodf
[idx
].pguid
= &GUID_Button
;
478 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(i
) | DIDFT_PSHBUTTON
;
480 newDevice
->generic
.base
.data_format
.wine_df
= df
;
482 /* initialize default properties */
483 for (i
= 0; i
< c_dfDIJoystick2
.dwNumObjs
; i
++) {
484 newDevice
->generic
.props
[i
].lDevMin
= -32767;
485 newDevice
->generic
.props
[i
].lDevMax
= +32767;
486 newDevice
->generic
.props
[i
].lMin
= 0;
487 newDevice
->generic
.props
[i
].lMax
= 0xffff;
488 newDevice
->generic
.props
[i
].lDeadZone
= newDevice
->generic
.deadzone
; /* % * 1000 */
489 newDevice
->generic
.props
[i
].lSaturation
= 0;
492 IDirectInput_AddRef(&newDevice
->generic
.base
.dinput
->IDirectInput7A_iface
);
494 EnterCriticalSection(&dinput
->crit
);
495 list_add_tail(&dinput
->devices_list
, &newDevice
->generic
.base
.entry
);
496 LeaveCriticalSection(&dinput
->crit
);
498 newDevice
->generic
.devcaps
.dwSize
= sizeof(newDevice
->generic
.devcaps
);
499 newDevice
->generic
.devcaps
.dwFlags
= DIDC_ATTACHED
;
501 ddi
.dwSize
= sizeof(ddi
);
502 fill_joystick_dideviceinstanceW(&ddi
, newDevice
->generic
.base
.dinput
->dwVersion
, index
);
503 newDevice
->generic
.devcaps
.dwDevType
= ddi
.dwDevType
;
505 newDevice
->generic
.devcaps
.dwFFSamplePeriod
= 0;
506 newDevice
->generic
.devcaps
.dwFFMinTimeResolution
= 0;
507 newDevice
->generic
.devcaps
.dwFirmwareRevision
= 0;
508 newDevice
->generic
.devcaps
.dwHardwareRevision
= 0;
509 newDevice
->generic
.devcaps
.dwFFDriverVersion
= 0;
511 if (TRACE_ON(dinput
)) {
512 _dump_DIDATAFORMAT(newDevice
->generic
.base
.data_format
.wine_df
);
513 for (i
= 0; i
< (newDevice
->generic
.device_axis_count
); i
++)
514 TRACE("axis_map[%d] = %d\n", i
, newDevice
->generic
.axis_map
[i
]);
515 _dump_DIDEVCAPS(&newDevice
->generic
.devcaps
);
523 hr
= DIERR_OUTOFMEMORY
;
525 if (df
) HeapFree(GetProcessHeap(), 0, df
->rgodf
);
526 HeapFree(GetProcessHeap(), 0, df
);
527 release_DataFormat(&newDevice
->generic
.base
.data_format
);
528 HeapFree(GetProcessHeap(),0,newDevice
->generic
.axis_map
);
529 HeapFree(GetProcessHeap(),0,newDevice
);
535 /******************************************************************************
536 * get_joystick_index : Get the joystick index from a given GUID
538 static unsigned short get_joystick_index(REFGUID guid
)
540 GUID wine_joystick
= DInput_Wine_Joystick_GUID
;
541 GUID dev_guid
= *guid
;
543 wine_joystick
.Data3
= 0;
546 /* for the standard joystick GUID use index 0 */
547 if(IsEqualGUID(&GUID_Joystick
,guid
)) return 0;
549 /* for the wine joystick GUIDs use the index stored in Data3 */
550 if(IsEqualGUID(&wine_joystick
, &dev_guid
)) return guid
->Data3
;
552 return MAX_JOYSTICKS
;
555 static HRESULT
joydev_create_device(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPVOID
*pdev
, int unicode
)
557 unsigned short index
;
559 TRACE("%p %s %s %p %i\n", dinput
, debugstr_guid(rguid
), debugstr_guid(riid
), pdev
, unicode
);
560 find_joystick_devices();
563 if ((index
= get_joystick_index(rguid
)) < MAX_JOYSTICKS
&&
564 joystick_devices_count
&& index
< joystick_devices_count
)
571 else if (IsEqualGUID(&IID_IDirectInputDeviceA
, riid
) ||
572 IsEqualGUID(&IID_IDirectInputDevice2A
, riid
) ||
573 IsEqualGUID(&IID_IDirectInputDevice7A
, riid
) ||
574 IsEqualGUID(&IID_IDirectInputDevice8A
, riid
))
578 else if (IsEqualGUID(&IID_IDirectInputDeviceW
, riid
) ||
579 IsEqualGUID(&IID_IDirectInputDevice2W
, riid
) ||
580 IsEqualGUID(&IID_IDirectInputDevice7W
, riid
) ||
581 IsEqualGUID(&IID_IDirectInputDevice8W
, riid
))
587 WARN("no interface\n");
588 return DIERR_NOINTERFACE
;
591 hr
= alloc_device(rguid
, dinput
, &This
, index
);
592 if (!This
) return hr
;
595 *pdev
= &This
->generic
.base
.IDirectInputDevice8W_iface
;
597 *pdev
= &This
->generic
.base
.IDirectInputDevice8A_iface
;
602 return DIERR_DEVICENOTREG
;
607 const struct dinput_device joystick_linux_device
= {
608 "Wine Linux joystick driver",
614 /******************************************************************************
615 * Acquire : gets exclusive control of the joystick
617 static HRESULT WINAPI
JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface
)
619 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
622 TRACE("(%p)\n",This
);
624 res
= IDirectInputDevice2WImpl_Acquire(iface
);
628 /* open the joystick device */
629 if (This
->joyfd
==-1) {
630 TRACE("opening joystick device %s\n", This
->joydev
->device
);
632 This
->joyfd
= open(This
->joydev
->device
, O_RDONLY
);
633 if (This
->joyfd
==-1) {
634 ERR("open(%s) failed: %s\n", This
->joydev
->device
, strerror(errno
));
635 IDirectInputDevice2WImpl_Unacquire(iface
);
636 return DIERR_NOTFOUND
;
643 static HRESULT WINAPI
JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface
)
645 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
646 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This
));
649 /******************************************************************************
650 * GetProperty : get input device properties
652 static HRESULT WINAPI
JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface
, REFGUID rguid
, LPDIPROPHEADER pdiph
)
654 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
656 TRACE("(this=%p,%s,%p)\n", iface
, debugstr_guid(rguid
), pdiph
);
657 _dump_DIPROPHEADER(pdiph
);
659 if (!IS_DIPROP(rguid
)) return DI_OK
;
661 switch (LOWORD(rguid
)) {
663 case (DWORD_PTR
) DIPROP_VIDPID
:
665 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
667 if (!This
->joydev
->product_id
|| !This
->joydev
->vendor_id
)
668 return DIERR_UNSUPPORTED
;
669 pd
->dwData
= MAKELONG(This
->joydev
->vendor_id
, This
->joydev
->product_id
);
670 TRACE("DIPROP_VIDPID(%08x)\n", pd
->dwData
);
673 case (DWORD_PTR
) DIPROP_JOYSTICKID
:
675 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
677 pd
->dwData
= get_joystick_index(&This
->generic
.base
.guid
);
678 TRACE("DIPROP_JOYSTICKID(%d)\n", pd
->dwData
);
683 return JoystickWGenericImpl_GetProperty(iface
, rguid
, pdiph
);
689 static HRESULT WINAPI
JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface
, REFGUID rguid
, LPDIPROPHEADER pdiph
)
691 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
692 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This
), rguid
, pdiph
);
695 /******************************************************************************
696 * GetDeviceInfo : get information about a device's identity
698 static HRESULT WINAPI
JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface
, LPDIDEVICEINSTANCEA ddi
)
700 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
702 TRACE("(%p) %p\n", This
, ddi
);
704 if (ddi
== NULL
) return E_POINTER
;
705 if ((ddi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3A
)) &&
706 (ddi
->dwSize
!= sizeof(DIDEVICEINSTANCEA
)))
707 return DIERR_INVALIDPARAM
;
709 fill_joystick_dideviceinstanceA( ddi
, This
->generic
.base
.dinput
->dwVersion
,
710 get_joystick_index(&This
->generic
.base
.guid
) );
714 static HRESULT WINAPI
JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface
, LPDIDEVICEINSTANCEW ddi
)
716 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
718 TRACE("(%p) %p\n", This
, ddi
);
720 if (ddi
== NULL
) return E_POINTER
;
721 if ((ddi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3W
)) &&
722 (ddi
->dwSize
!= sizeof(DIDEVICEINSTANCEW
)))
723 return DIERR_INVALIDPARAM
;
725 fill_joystick_dideviceinstanceW( ddi
, This
->generic
.base
.dinput
->dwVersion
,
726 get_joystick_index(&This
->generic
.base
.guid
) );
730 /******************************************************************************
731 * Unacquire : frees the joystick
733 static HRESULT WINAPI
JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface
)
735 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
738 TRACE("(%p)\n",This
);
740 res
= IDirectInputDevice2WImpl_Unacquire(iface
);
745 if (This
->joyfd
!=-1) {
746 TRACE("closing joystick device\n");
755 static HRESULT WINAPI
JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface
)
757 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
758 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This
));
761 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface
)
765 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
767 TRACE("(%p)\n", This
);
769 if (This
->joyfd
==-1) {
778 plfd
.fd
= This
->joyfd
;
779 plfd
.events
= POLLIN
;
780 if (poll(&plfd
,1,0) != 1)
782 /* we have one event, so we can read */
783 if (sizeof(jse
)!=read(This
->joyfd
,&jse
,sizeof(jse
))) {
786 TRACE("js_event: type 0x%x, number %d, value %d\n",
787 jse
.type
,jse
.number
,jse
.value
);
788 if (jse
.type
& JS_EVENT_BUTTON
)
790 if (jse
.number
>= This
->generic
.devcaps
.dwButtons
) return;
792 inst_id
= DIDFT_MAKEINSTANCE(jse
.number
) | DIDFT_PSHBUTTON
;
793 This
->generic
.js
.rgbButtons
[jse
.number
] = value
= jse
.value
? 0x80 : 0x00;
795 else if (jse
.type
& JS_EVENT_AXIS
)
797 int number
= This
->generic
.axis_map
[jse
.number
]; /* wine format object index */
799 if (number
< 0) return;
800 inst_id
= number
< 8 ? DIDFT_MAKEINSTANCE(number
) | DIDFT_ABSAXIS
:
801 DIDFT_MAKEINSTANCE(number
- 8) | DIDFT_POV
;
802 value
= joystick_map_axis(&This
->generic
.props
[id_to_object(This
->generic
.base
.data_format
.wine_df
, inst_id
)], jse
.value
);
804 TRACE("changing axis %d => %d\n", jse
.number
, number
);
807 case 0: This
->generic
.js
.lX
= value
; break;
808 case 1: This
->generic
.js
.lY
= value
; break;
809 case 2: This
->generic
.js
.lZ
= value
; break;
810 case 3: This
->generic
.js
.lRx
= value
; break;
811 case 4: This
->generic
.js
.lRy
= value
; break;
812 case 5: This
->generic
.js
.lRz
= value
; break;
813 case 6: This
->generic
.js
.rglSlider
[0] = value
; break;
814 case 7: This
->generic
.js
.rglSlider
[1] = value
; break;
815 case 8: case 9: case 10: case 11:
817 int idx
= number
- 8;
820 This
->povs
[idx
].y
= jse
.value
;
822 This
->povs
[idx
].x
= jse
.value
;
824 This
->generic
.js
.rgdwPOV
[idx
] = value
= joystick_map_pov(&This
->povs
[idx
]);
828 WARN("axis %d not supported\n", number
);
832 queue_event(iface
, inst_id
, value
, GetCurrentTime(), This
->generic
.base
.dinput
->evsequence
++);
836 static const IDirectInputDevice8AVtbl JoystickAvt
=
838 IDirectInputDevice2AImpl_QueryInterface
,
839 IDirectInputDevice2AImpl_AddRef
,
840 IDirectInputDevice2AImpl_Release
,
841 JoystickAGenericImpl_GetCapabilities
,
842 IDirectInputDevice2AImpl_EnumObjects
,
843 JoystickLinuxAImpl_GetProperty
,
844 JoystickAGenericImpl_SetProperty
,
845 JoystickLinuxAImpl_Acquire
,
846 JoystickLinuxAImpl_Unacquire
,
847 JoystickAGenericImpl_GetDeviceState
,
848 IDirectInputDevice2AImpl_GetDeviceData
,
849 IDirectInputDevice2AImpl_SetDataFormat
,
850 IDirectInputDevice2AImpl_SetEventNotification
,
851 IDirectInputDevice2AImpl_SetCooperativeLevel
,
852 JoystickAGenericImpl_GetObjectInfo
,
853 JoystickLinuxAImpl_GetDeviceInfo
,
854 IDirectInputDevice2AImpl_RunControlPanel
,
855 IDirectInputDevice2AImpl_Initialize
,
856 IDirectInputDevice2AImpl_CreateEffect
,
857 IDirectInputDevice2AImpl_EnumEffects
,
858 IDirectInputDevice2AImpl_GetEffectInfo
,
859 IDirectInputDevice2AImpl_GetForceFeedbackState
,
860 IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
861 IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
862 IDirectInputDevice2AImpl_Escape
,
863 JoystickAGenericImpl_Poll
,
864 IDirectInputDevice2AImpl_SendDeviceData
,
865 IDirectInputDevice7AImpl_EnumEffectsInFile
,
866 IDirectInputDevice7AImpl_WriteEffectToFile
,
867 JoystickAGenericImpl_BuildActionMap
,
868 JoystickAGenericImpl_SetActionMap
,
869 IDirectInputDevice8AImpl_GetImageInfo
872 static const IDirectInputDevice8WVtbl JoystickWvt
=
874 IDirectInputDevice2WImpl_QueryInterface
,
875 IDirectInputDevice2WImpl_AddRef
,
876 IDirectInputDevice2WImpl_Release
,
877 JoystickWGenericImpl_GetCapabilities
,
878 IDirectInputDevice2WImpl_EnumObjects
,
879 JoystickLinuxWImpl_GetProperty
,
880 JoystickWGenericImpl_SetProperty
,
881 JoystickLinuxWImpl_Acquire
,
882 JoystickLinuxWImpl_Unacquire
,
883 JoystickWGenericImpl_GetDeviceState
,
884 IDirectInputDevice2WImpl_GetDeviceData
,
885 IDirectInputDevice2WImpl_SetDataFormat
,
886 IDirectInputDevice2WImpl_SetEventNotification
,
887 IDirectInputDevice2WImpl_SetCooperativeLevel
,
888 JoystickWGenericImpl_GetObjectInfo
,
889 JoystickLinuxWImpl_GetDeviceInfo
,
890 IDirectInputDevice2WImpl_RunControlPanel
,
891 IDirectInputDevice2WImpl_Initialize
,
892 IDirectInputDevice2WImpl_CreateEffect
,
893 IDirectInputDevice2WImpl_EnumEffects
,
894 IDirectInputDevice2WImpl_GetEffectInfo
,
895 IDirectInputDevice2WImpl_GetForceFeedbackState
,
896 IDirectInputDevice2WImpl_SendForceFeedbackCommand
,
897 IDirectInputDevice2WImpl_EnumCreatedEffectObjects
,
898 IDirectInputDevice2WImpl_Escape
,
899 JoystickWGenericImpl_Poll
,
900 IDirectInputDevice2WImpl_SendDeviceData
,
901 IDirectInputDevice7WImpl_EnumEffectsInFile
,
902 IDirectInputDevice7WImpl_WriteEffectToFile
,
903 JoystickWGenericImpl_BuildActionMap
,
904 JoystickWGenericImpl_SetActionMap
,
905 IDirectInputDevice8WImpl_GetImageInfo
908 #else /* HAVE_LINUX_22_JOYSTICK_API */
910 const struct dinput_device joystick_linux_device
= {
911 "Wine Linux joystick driver",
917 #endif /* HAVE_LINUX_22_JOYSTICK_API */