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
29 #include "wine/port.h"
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
46 #ifdef HAVE_LINUX_IOCTL_H
47 # include <linux/ioctl.h>
49 #ifdef HAVE_LINUX_JOYSTICK_H
50 # include <linux/joystick.h>
53 #ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
57 #include "wine/debug.h"
58 #include "wine/unicode.h"
65 #include "dinput_private.h"
66 #include "device_private.h"
67 #include "joystick_private.h"
69 #ifdef HAVE_LINUX_22_JOYSTICK_API
71 WINE_DEFAULT_DEBUG_CHANNEL(dinput
);
73 #define JOYDEV_NEW "/dev/input/js"
74 #define JOYDEV_OLD "/dev/js"
75 #define JOYDEVDRIVER " (js)"
79 char device
[MAX_PATH
];
87 WORD vendor_id
, product_id
, bus_type
;
92 typedef struct JoystickImpl JoystickImpl
;
93 static const IDirectInputDevice8AVtbl JoystickAvt
;
94 static const IDirectInputDevice8WVtbl JoystickWvt
;
97 struct JoystickGenericImpl generic
;
99 struct JoyDev
*joydev
;
101 /* joystick private */
106 static inline JoystickImpl
*impl_from_IDirectInputDevice8A(IDirectInputDevice8A
*iface
)
108 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface
, IDirectInputDeviceImpl
, IDirectInputDevice8A_iface
),
109 JoystickGenericImpl
, base
), JoystickImpl
, generic
);
111 static inline JoystickImpl
*impl_from_IDirectInputDevice8W(IDirectInputDevice8W
*iface
)
113 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface
, IDirectInputDeviceImpl
, IDirectInputDevice8W_iface
),
114 JoystickGenericImpl
, base
), JoystickImpl
, generic
);
117 static inline IDirectInputDevice8W
*IDirectInputDevice8W_from_impl(JoystickImpl
*This
)
119 return &This
->generic
.base
.IDirectInputDevice8W_iface
;
122 static const GUID DInput_Wine_Joystick_GUID
= { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
126 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
129 #define MAX_JOYSTICKS 64
130 static INT joystick_devices_count
= -1;
131 static struct JoyDev
*joystick_devices
;
133 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface
);
135 #define SYS_PATH_FORMAT "/sys/class/input/js%d/device/id/%s"
136 static BOOL
read_sys_id_variable(int index
, const char *property
, WORD
*value
)
138 char sys_path
[sizeof(SYS_PATH_FORMAT
) + 16], id_str
[5];
142 sprintf(sys_path
, SYS_PATH_FORMAT
, index
, property
);
143 if ((sys_fd
= open(sys_path
, O_RDONLY
)) != -1)
145 if (read(sys_fd
, id_str
, 4) == 4)
148 *value
= strtol(id_str
, NULL
, 16);
156 #undef SYS_PATH_FORMAT
158 static INT
find_joystick_devices(void)
162 if (joystick_devices_count
!= -1) return joystick_devices_count
;
164 joystick_devices_count
= 0;
165 for (i
= 0; i
< MAX_JOYSTICKS
; i
++)
168 struct JoyDev joydev
, *new_joydevs
;
169 BYTE axes_map
[ABS_MAX
+ 1];
170 SHORT btn_map
[KEY_MAX
- BTN_MISC
+ 1];
173 snprintf(joydev
.device
, sizeof(joydev
.device
), "%s%d", JOYDEV_NEW
, i
);
174 if ((fd
= open(joydev
.device
, O_RDONLY
)) == -1)
176 snprintf(joydev
.device
, sizeof(joydev
.device
), "%s%d", JOYDEV_OLD
, i
);
177 if ((fd
= open(joydev
.device
, O_RDONLY
)) == -1) continue;
180 strcpy(joydev
.name
, "Wine Joystick");
181 #if defined(JSIOCGNAME)
182 if (ioctl(fd
, JSIOCGNAME(sizeof(joydev
.name
) - sizeof(JOYDEVDRIVER
)), joydev
.name
) < 0)
183 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev
.device
, strerror(errno
));
186 /* Append driver name */
187 strcat(joydev
.name
, JOYDEVDRIVER
);
189 if (device_disabled_registry(joydev
.name
)) {
195 if (ioctl(fd
, JSIOCGAXES
, &joydev
.axis_count
) < 0)
197 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defaulting to 2\n", joydev
.device
, strerror(errno
));
198 joydev
.axis_count
= 2;
201 WARN("reading number of joystick axes unsupported in this platform, defaulting to 2\n");
202 joydev
.axis_count
= 2;
205 if (ioctl(fd
, JSIOCGBUTTONS
, &joydev
.button_count
) < 0)
207 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defaulting to 2\n", joydev
.device
, strerror(errno
));
208 joydev
.button_count
= 2;
211 WARN("reading number of joystick buttons unsupported in this platform, defaulting to 2\n");
212 joydev
.button_count
= 2;
215 joydev
.is_joystick
= FALSE
;
216 if (ioctl(fd
, JSIOCGBTNMAP
, btn_map
) < 0)
218 WARN("ioctl(%s,JSIOCGBTNMAP) failed: %s\n", joydev
.device
, strerror(errno
));
223 /* in lieu of properly reporting HID usage, detect presence of
224 * "joystick buttons" and report those devices as joysticks instead of
226 for (j
= 0; !joydev
.is_joystick
&& j
< joydev
.button_count
; j
++)
243 joydev
.is_joystick
= TRUE
;
257 TRACE("Non-joystick detected. Skipping\n");
262 if (ioctl(fd
, JSIOCGAXMAP
, axes_map
) < 0)
264 WARN("ioctl(%s,JSIOCGAXMAP) failed: %s\n", joydev
.device
, strerror(errno
));
265 joydev
.dev_axes_map
= NULL
;
268 if ((joydev
.dev_axes_map
= HeapAlloc(GetProcessHeap(), 0, joydev
.axis_count
* sizeof(int))))
270 INT j
, found_axes
= 0;
272 /* Remap to DI numbers */
273 for (j
= 0; j
< joydev
.axis_count
; j
++)
277 /* Axis match 1-to-1 */
278 joydev
.dev_axes_map
[j
] = j
;
281 else if (axes_map
[j
] <= 10)
283 /* Axes 8 through 10 are Wheel, Gas and Brake,
284 * remap to 0, 1 and 2
286 joydev
.dev_axes_map
[j
] = axes_map
[j
] - 8;
289 else if (axes_map
[j
] == 16 ||
293 joydev
.dev_axes_map
[j
] = 8;
297 joydev
.dev_axes_map
[j
] = -1;
300 /* If no axes were configured but there are axes assume a 1-to-1 (wii controller) */
301 if (joydev
.axis_count
&& !found_axes
)
303 int axes_limit
= min(joydev
.axis_count
, 8); /* generic driver limit */
305 ERR("Incoherent joystick data, advertised %d axes, detected 0. Assuming 1-to-1.\n",
307 for (j
= 0; j
< axes_limit
; j
++)
308 joydev
.dev_axes_map
[j
] = j
;
310 joydev
.axis_count
= axes_limit
;
314 /* Find vendor_id and product_id in sysfs */
315 joydev
.vendor_id
= 0;
316 joydev
.product_id
= 0;
318 read_sys_id_variable(i
, "vendor", &joydev
.vendor_id
);
319 read_sys_id_variable(i
, "product", &joydev
.product_id
);
320 read_sys_id_variable(i
, "bustype", &joydev
.bus_type
);
322 if (joydev
.vendor_id
== 0 || joydev
.product_id
== 0)
324 joydev
.guid_product
= DInput_Wine_Joystick_GUID
;
328 /* Concatenate product_id with vendor_id to mimic Windows behaviour */
329 joydev
.guid_product
= DInput_PIDVID_Product_GUID
;
330 joydev
.guid_product
.Data1
= MAKELONG(joydev
.vendor_id
, joydev
.product_id
);
335 if (!joystick_devices_count
)
336 new_joydevs
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev
));
338 new_joydevs
= HeapReAlloc(GetProcessHeap(), 0, joystick_devices
,
339 (joystick_devices_count
+ 1) * sizeof(struct JoyDev
));
340 if (!new_joydevs
) continue;
342 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev
.device
,
343 joydev
.name
, joydev
.axis_count
, joydev
.button_count
);
345 joystick_devices
= new_joydevs
;
346 joystick_devices
[joystick_devices_count
++] = joydev
;
349 return joystick_devices_count
;
352 static void fill_joystick_dideviceinstanceW(LPDIDEVICEINSTANCEW lpddi
, DWORD version
, int id
)
354 DWORD dwSize
= lpddi
->dwSize
;
356 TRACE("%d %p\n", dwSize
, lpddi
);
357 memset(lpddi
, 0, dwSize
);
359 /* Return joystick */
360 lpddi
->dwSize
= dwSize
;
361 lpddi
->guidInstance
= DInput_Wine_Joystick_GUID
;
362 lpddi
->guidInstance
.Data3
= id
;
363 lpddi
->guidProduct
= joystick_devices
[id
].guid_product
;
364 lpddi
->dwDevType
= get_device_type(version
, joystick_devices
[id
].is_joystick
);
366 /* Assume the joystick as HID if it is attached to USB bus and has a valid VID/PID */
367 if (joystick_devices
[id
].bus_type
== BUS_USB
&&
368 joystick_devices
[id
].vendor_id
&& joystick_devices
[id
].product_id
)
370 lpddi
->dwDevType
|= DIDEVTYPE_HID
;
371 lpddi
->wUsagePage
= 0x01; /* Desktop */
372 if (joystick_devices
[id
].is_joystick
)
373 lpddi
->wUsage
= 0x04; /* Joystick */
375 lpddi
->wUsage
= 0x05; /* Game Pad */
378 MultiByteToWideChar(CP_ACP
, 0, joystick_devices
[id
].name
, -1, lpddi
->tszInstanceName
, MAX_PATH
);
379 MultiByteToWideChar(CP_ACP
, 0, joystick_devices
[id
].name
, -1, lpddi
->tszProductName
, MAX_PATH
);
380 lpddi
->guidFFDriver
= GUID_NULL
;
383 static void fill_joystick_dideviceinstanceA(LPDIDEVICEINSTANCEA lpddi
, DWORD version
, int id
)
385 DIDEVICEINSTANCEW lpddiW
;
386 DWORD dwSize
= lpddi
->dwSize
;
388 lpddiW
.dwSize
= sizeof(lpddiW
);
389 fill_joystick_dideviceinstanceW(&lpddiW
, version
, id
);
391 TRACE("%d %p\n", dwSize
, lpddi
);
392 memset(lpddi
, 0, dwSize
);
395 lpddi
->dwSize
= dwSize
;
396 lpddi
->guidInstance
= lpddiW
.guidInstance
;
397 lpddi
->guidProduct
= lpddiW
.guidProduct
;
398 lpddi
->dwDevType
= lpddiW
.dwDevType
;
399 strcpy(lpddi
->tszInstanceName
, joystick_devices
[id
].name
);
400 strcpy(lpddi
->tszProductName
, joystick_devices
[id
].name
);
401 lpddi
->guidFFDriver
= lpddiW
.guidFFDriver
;
402 lpddi
->wUsagePage
= lpddiW
.wUsagePage
;
403 lpddi
->wUsage
= lpddiW
.wUsage
;
406 static HRESULT
joydev_enum_deviceA(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEA lpddi
, DWORD version
, int id
)
410 if (id
>= find_joystick_devices()) return E_FAIL
;
412 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
413 WARN("force feedback not supported\n");
417 if ((dwDevType
== 0) ||
418 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
>= 0x0300 && version
< 0x0800)) ||
419 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
420 /* check whether we have a joystick */
421 if ((fd
= open(joystick_devices
[id
].device
, O_RDONLY
)) == -1)
423 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices
[id
].device
, strerror(errno
));
426 fill_joystick_dideviceinstanceA( lpddi
, version
, id
);
428 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices
[id
].device
, joystick_devices
[id
].name
);
435 static HRESULT
joydev_enum_deviceW(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEW lpddi
, DWORD version
, int id
)
439 if (id
>= find_joystick_devices()) return E_FAIL
;
441 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
442 WARN("force feedback not supported\n");
446 if ((dwDevType
== 0) ||
447 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
>= 0x0300 && version
< 0x0800)) ||
448 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
449 /* check whether we have a joystick */
450 if ((fd
= open(joystick_devices
[id
].device
, O_RDONLY
)) == -1)
452 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices
[id
].device
, strerror(errno
));
455 fill_joystick_dideviceinstanceW( lpddi
, version
, id
);
457 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices
[id
].device
, joystick_devices
[id
].name
);
464 static HRESULT
alloc_device(REFGUID rguid
, IDirectInputImpl
*dinput
,
465 JoystickImpl
**pdev
, unsigned short index
)
468 JoystickImpl
* newDevice
;
470 LPDIDATAFORMAT df
= NULL
;
472 DIDEVICEINSTANCEW ddi
;
474 TRACE("%s %p %p %hu\n", debugstr_guid(rguid
), dinput
, pdev
, index
);
476 newDevice
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(JoystickImpl
));
477 if (newDevice
== 0) {
478 WARN("out of memory\n");
480 return DIERR_OUTOFMEMORY
;
483 newDevice
->joydev
= &joystick_devices
[index
];
484 newDevice
->joyfd
= -1;
485 newDevice
->generic
.guidInstance
= DInput_Wine_Joystick_GUID
;
486 newDevice
->generic
.guidInstance
.Data3
= index
;
487 newDevice
->generic
.guidProduct
= DInput_Wine_Joystick_GUID
;
488 newDevice
->generic
.joy_polldev
= joy_polldev
;
489 newDevice
->generic
.name
= newDevice
->joydev
->name
;
490 newDevice
->generic
.device_axis_count
= newDevice
->joydev
->axis_count
;
491 newDevice
->generic
.devcaps
.dwButtons
= newDevice
->joydev
->button_count
;
493 if (newDevice
->generic
.devcaps
.dwButtons
> 128)
495 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice
->generic
.devcaps
.dwButtons
);
496 newDevice
->generic
.devcaps
.dwButtons
= 128;
499 newDevice
->generic
.base
.IDirectInputDevice8A_iface
.lpVtbl
= &JoystickAvt
;
500 newDevice
->generic
.base
.IDirectInputDevice8W_iface
.lpVtbl
= &JoystickWvt
;
501 newDevice
->generic
.base
.ref
= 1;
502 newDevice
->generic
.base
.dinput
= dinput
;
503 newDevice
->generic
.base
.guid
= *rguid
;
504 InitializeCriticalSection(&newDevice
->generic
.base
.crit
);
505 newDevice
->generic
.base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JoystickImpl*->generic.base.crit");
507 /* setup_dinput_options may change these */
508 newDevice
->generic
.deadzone
= 0;
510 /* do any user specified configuration */
511 hr
= setup_dinput_options(&newDevice
->generic
, newDevice
->joydev
->dev_axes_map
);
515 /* Create copy of default data format */
516 if (!(df
= HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2
.dwSize
))) goto FAILED
;
517 memcpy(df
, &c_dfDIJoystick2
, c_dfDIJoystick2
.dwSize
);
519 df
->dwNumObjs
= newDevice
->generic
.devcaps
.dwAxes
+ newDevice
->generic
.devcaps
.dwPOVs
+ newDevice
->generic
.devcaps
.dwButtons
;
520 if (!(df
->rgodf
= HeapAlloc(GetProcessHeap(), 0, df
->dwNumObjs
* df
->dwObjSize
))) goto FAILED
;
522 for (i
= 0; i
< newDevice
->generic
.device_axis_count
; i
++)
524 int wine_obj
= newDevice
->generic
.axis_map
[i
];
526 if (wine_obj
< 0) continue;
528 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[wine_obj
], df
->dwObjSize
);
530 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(wine_obj
) | DIDFT_ABSAXIS
;
533 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(wine_obj
- 8) | DIDFT_POV
;
534 i
++; /* POV takes 2 axes */
537 for (i
= 0; i
< newDevice
->generic
.devcaps
.dwButtons
; i
++)
539 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[i
+ 12], df
->dwObjSize
);
540 df
->rgodf
[idx
].pguid
= &GUID_Button
;
541 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(i
) | DIDFT_PSHBUTTON
;
543 newDevice
->generic
.base
.data_format
.wine_df
= df
;
545 /* initialize default properties */
546 for (i
= 0; i
< c_dfDIJoystick2
.dwNumObjs
; i
++) {
547 newDevice
->generic
.props
[i
].lDevMin
= -32767;
548 newDevice
->generic
.props
[i
].lDevMax
= +32767;
549 newDevice
->generic
.props
[i
].lMin
= 0;
550 newDevice
->generic
.props
[i
].lMax
= 0xffff;
551 newDevice
->generic
.props
[i
].lDeadZone
= newDevice
->generic
.deadzone
; /* % * 1000 */
552 newDevice
->generic
.props
[i
].lSaturation
= 0;
555 IDirectInput_AddRef(&newDevice
->generic
.base
.dinput
->IDirectInput7A_iface
);
557 newDevice
->generic
.devcaps
.dwSize
= sizeof(newDevice
->generic
.devcaps
);
558 newDevice
->generic
.devcaps
.dwFlags
= DIDC_ATTACHED
;
560 ddi
.dwSize
= sizeof(ddi
);
561 fill_joystick_dideviceinstanceW(&ddi
, newDevice
->generic
.base
.dinput
->dwVersion
, index
);
562 newDevice
->generic
.devcaps
.dwDevType
= ddi
.dwDevType
;
564 newDevice
->generic
.devcaps
.dwFFSamplePeriod
= 0;
565 newDevice
->generic
.devcaps
.dwFFMinTimeResolution
= 0;
566 newDevice
->generic
.devcaps
.dwFirmwareRevision
= 0;
567 newDevice
->generic
.devcaps
.dwHardwareRevision
= 0;
568 newDevice
->generic
.devcaps
.dwFFDriverVersion
= 0;
570 if (TRACE_ON(dinput
)) {
571 _dump_DIDATAFORMAT(newDevice
->generic
.base
.data_format
.wine_df
);
572 for (i
= 0; i
< (newDevice
->generic
.device_axis_count
); i
++)
573 TRACE("axis_map[%d] = %d\n", i
, newDevice
->generic
.axis_map
[i
]);
574 _dump_DIDEVCAPS(&newDevice
->generic
.devcaps
);
582 hr
= DIERR_OUTOFMEMORY
;
584 if (df
) HeapFree(GetProcessHeap(), 0, df
->rgodf
);
585 HeapFree(GetProcessHeap(), 0, df
);
586 release_DataFormat(&newDevice
->generic
.base
.data_format
);
587 HeapFree(GetProcessHeap(),0,newDevice
->generic
.axis_map
);
588 HeapFree(GetProcessHeap(),0,newDevice
);
594 /******************************************************************************
595 * get_joystick_index : Get the joystick index from a given GUID
597 static unsigned short get_joystick_index(REFGUID guid
)
599 GUID wine_joystick
= DInput_Wine_Joystick_GUID
;
600 GUID dev_guid
= *guid
;
602 wine_joystick
.Data3
= 0;
605 /* for the standard joystick GUID use index 0 */
606 if(IsEqualGUID(&GUID_Joystick
,guid
)) return 0;
608 /* for the wine joystick GUIDs use the index stored in Data3 */
609 if(IsEqualGUID(&wine_joystick
, &dev_guid
)) return guid
->Data3
;
611 return MAX_JOYSTICKS
;
614 static HRESULT
joydev_create_device(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPVOID
*pdev
, int unicode
)
616 unsigned short index
;
618 TRACE("%p %s %s %p %i\n", dinput
, debugstr_guid(rguid
), debugstr_guid(riid
), pdev
, unicode
);
619 find_joystick_devices();
622 if ((index
= get_joystick_index(rguid
)) < MAX_JOYSTICKS
&&
623 joystick_devices_count
&& index
< joystick_devices_count
)
630 else if (IsEqualGUID(&IID_IDirectInputDeviceA
, riid
) ||
631 IsEqualGUID(&IID_IDirectInputDevice2A
, riid
) ||
632 IsEqualGUID(&IID_IDirectInputDevice7A
, riid
) ||
633 IsEqualGUID(&IID_IDirectInputDevice8A
, riid
))
637 else if (IsEqualGUID(&IID_IDirectInputDeviceW
, riid
) ||
638 IsEqualGUID(&IID_IDirectInputDevice2W
, riid
) ||
639 IsEqualGUID(&IID_IDirectInputDevice7W
, riid
) ||
640 IsEqualGUID(&IID_IDirectInputDevice8W
, riid
))
646 WARN("no interface\n");
647 return DIERR_NOINTERFACE
;
650 hr
= alloc_device(rguid
, dinput
, &This
, index
);
651 if (!This
) return hr
;
654 *pdev
= &This
->generic
.base
.IDirectInputDevice8W_iface
;
656 *pdev
= &This
->generic
.base
.IDirectInputDevice8A_iface
;
661 return DIERR_DEVICENOTREG
;
666 const struct dinput_device joystick_linux_device
= {
667 "Wine Linux joystick driver",
673 /******************************************************************************
674 * Acquire : gets exclusive control of the joystick
676 static HRESULT WINAPI
JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface
)
678 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
681 TRACE("(%p)\n",This
);
683 res
= IDirectInputDevice2WImpl_Acquire(iface
);
687 /* open the joystick device */
688 if (This
->joyfd
==-1) {
689 TRACE("opening joystick device %s\n", This
->joydev
->device
);
691 This
->joyfd
= open(This
->joydev
->device
, O_RDONLY
);
692 if (This
->joyfd
==-1) {
693 ERR("open(%s) failed: %s\n", This
->joydev
->device
, strerror(errno
));
694 IDirectInputDevice2WImpl_Unacquire(iface
);
695 return DIERR_NOTFOUND
;
702 static HRESULT WINAPI
JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface
)
704 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
705 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This
));
708 /******************************************************************************
709 * GetProperty : get input device properties
711 static HRESULT WINAPI
JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface
, REFGUID rguid
, LPDIPROPHEADER pdiph
)
713 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
715 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(rguid
), pdiph
);
716 _dump_DIPROPHEADER(pdiph
);
718 if (!IS_DIPROP(rguid
)) return DI_OK
;
720 switch (LOWORD(rguid
)) {
722 case (DWORD_PTR
) DIPROP_VIDPID
:
724 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
726 if (!This
->joydev
->product_id
|| !This
->joydev
->vendor_id
)
727 return DIERR_UNSUPPORTED
;
728 pd
->dwData
= MAKELONG(This
->joydev
->vendor_id
, This
->joydev
->product_id
);
729 TRACE("DIPROP_VIDPID(%08x)\n", pd
->dwData
);
732 case (DWORD_PTR
) DIPROP_JOYSTICKID
:
734 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
736 pd
->dwData
= get_joystick_index(&This
->generic
.base
.guid
);
737 TRACE("DIPROP_JOYSTICKID(%d)\n", pd
->dwData
);
741 case (DWORD_PTR
) DIPROP_GUIDANDPATH
:
743 static const WCHAR formatW
[] = {'\\','\\','?','\\','h','i','d','#','v','i','d','_','%','0','4','x','&',
744 'p','i','d','_','%','0','4','x','&','%','s','_','%','h','u',0};
745 static const WCHAR miW
[] = {'m','i',0};
746 static const WCHAR igW
[] = {'i','g',0};
749 LPDIPROPGUIDANDPATH pd
= (LPDIPROPGUIDANDPATH
)pdiph
;
750 WORD vid
= This
->joydev
->vendor_id
;
751 WORD pid
= This
->joydev
->product_id
;
754 return DIERR_UNSUPPORTED
;
756 is_gamepad
= is_xinput_device(&This
->generic
.devcaps
, vid
, pid
);
757 pd
->guidClass
= GUID_DEVCLASS_HIDCLASS
;
758 sprintfW(pd
->wszPath
, formatW
, vid
, pid
, is_gamepad
? igW
: miW
, get_joystick_index(&This
->generic
.base
.guid
));
760 TRACE("DIPROP_GUIDANDPATH(%s, %s): returning fake path\n", debugstr_guid(&pd
->guidClass
), debugstr_w(pd
->wszPath
));
765 return JoystickWGenericImpl_GetProperty(iface
, rguid
, pdiph
);
771 static HRESULT WINAPI
JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface
, REFGUID rguid
, LPDIPROPHEADER pdiph
)
773 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
774 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This
), rguid
, pdiph
);
777 /******************************************************************************
778 * GetDeviceInfo : get information about a device's identity
780 static HRESULT WINAPI
JoystickLinuxAImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8A iface
, LPDIDEVICEINSTANCEA ddi
)
782 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
784 TRACE("(%p) %p\n", This
, ddi
);
786 if (ddi
== NULL
) return E_POINTER
;
787 if ((ddi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3A
)) &&
788 (ddi
->dwSize
!= sizeof(DIDEVICEINSTANCEA
)))
789 return DIERR_INVALIDPARAM
;
791 fill_joystick_dideviceinstanceA( ddi
, This
->generic
.base
.dinput
->dwVersion
,
792 get_joystick_index(&This
->generic
.base
.guid
) );
796 static HRESULT WINAPI
JoystickLinuxWImpl_GetDeviceInfo(LPDIRECTINPUTDEVICE8W iface
, LPDIDEVICEINSTANCEW ddi
)
798 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
800 TRACE("(%p) %p\n", This
, ddi
);
802 if (ddi
== NULL
) return E_POINTER
;
803 if ((ddi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3W
)) &&
804 (ddi
->dwSize
!= sizeof(DIDEVICEINSTANCEW
)))
805 return DIERR_INVALIDPARAM
;
807 fill_joystick_dideviceinstanceW( ddi
, This
->generic
.base
.dinput
->dwVersion
,
808 get_joystick_index(&This
->generic
.base
.guid
) );
812 /******************************************************************************
813 * Unacquire : frees the joystick
815 static HRESULT WINAPI
JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface
)
817 JoystickImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
820 TRACE("(%p)\n",This
);
822 res
= IDirectInputDevice2WImpl_Unacquire(iface
);
827 if (This
->joyfd
!=-1) {
828 TRACE("closing joystick device\n");
837 static HRESULT WINAPI
JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface
)
839 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
840 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This
));
843 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface
)
847 JoystickImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
849 TRACE("(%p)\n", This
);
851 if (This
->joyfd
==-1) {
860 plfd
.fd
= This
->joyfd
;
861 plfd
.events
= POLLIN
;
862 if (poll(&plfd
,1,0) != 1)
864 /* we have one event, so we can read */
865 if (sizeof(jse
)!=read(This
->joyfd
,&jse
,sizeof(jse
))) {
868 TRACE("js_event: type 0x%x, number %d, value %d\n",
869 jse
.type
,jse
.number
,jse
.value
);
870 if (jse
.type
& JS_EVENT_BUTTON
)
872 if (jse
.number
>= This
->generic
.devcaps
.dwButtons
) return;
874 inst_id
= DIDFT_MAKEINSTANCE(jse
.number
) | DIDFT_PSHBUTTON
;
875 This
->generic
.js
.rgbButtons
[jse
.number
] = value
= jse
.value
? 0x80 : 0x00;
877 else if (jse
.type
& JS_EVENT_AXIS
)
879 int number
= This
->generic
.axis_map
[jse
.number
]; /* wine format object index */
881 if (number
< 0) return;
882 inst_id
= number
< 8 ? DIDFT_MAKEINSTANCE(number
) | DIDFT_ABSAXIS
:
883 DIDFT_MAKEINSTANCE(number
- 8) | DIDFT_POV
;
884 value
= joystick_map_axis(&This
->generic
.props
[id_to_object(This
->generic
.base
.data_format
.wine_df
, inst_id
)], jse
.value
);
886 TRACE("changing axis %d => %d\n", jse
.number
, number
);
889 case 0: This
->generic
.js
.lX
= value
; break;
890 case 1: This
->generic
.js
.lY
= value
; break;
891 case 2: This
->generic
.js
.lZ
= value
; break;
892 case 3: This
->generic
.js
.lRx
= value
; break;
893 case 4: This
->generic
.js
.lRy
= value
; break;
894 case 5: This
->generic
.js
.lRz
= value
; break;
895 case 6: This
->generic
.js
.rglSlider
[0] = value
; break;
896 case 7: This
->generic
.js
.rglSlider
[1] = value
; break;
897 case 8: case 9: case 10: case 11:
899 int idx
= number
- 8;
902 This
->povs
[idx
].y
= jse
.value
;
904 This
->povs
[idx
].x
= jse
.value
;
906 This
->generic
.js
.rgdwPOV
[idx
] = value
= joystick_map_pov(&This
->povs
[idx
]);
910 WARN("axis %d not supported\n", number
);
914 queue_event(iface
, inst_id
, value
, GetCurrentTime(), This
->generic
.base
.dinput
->evsequence
++);
918 static const IDirectInputDevice8AVtbl JoystickAvt
=
920 IDirectInputDevice2AImpl_QueryInterface
,
921 IDirectInputDevice2AImpl_AddRef
,
922 IDirectInputDevice2AImpl_Release
,
923 JoystickAGenericImpl_GetCapabilities
,
924 IDirectInputDevice2AImpl_EnumObjects
,
925 JoystickLinuxAImpl_GetProperty
,
926 JoystickAGenericImpl_SetProperty
,
927 JoystickLinuxAImpl_Acquire
,
928 JoystickLinuxAImpl_Unacquire
,
929 JoystickAGenericImpl_GetDeviceState
,
930 IDirectInputDevice2AImpl_GetDeviceData
,
931 IDirectInputDevice2AImpl_SetDataFormat
,
932 IDirectInputDevice2AImpl_SetEventNotification
,
933 IDirectInputDevice2AImpl_SetCooperativeLevel
,
934 JoystickAGenericImpl_GetObjectInfo
,
935 JoystickLinuxAImpl_GetDeviceInfo
,
936 IDirectInputDevice2AImpl_RunControlPanel
,
937 IDirectInputDevice2AImpl_Initialize
,
938 IDirectInputDevice2AImpl_CreateEffect
,
939 IDirectInputDevice2AImpl_EnumEffects
,
940 IDirectInputDevice2AImpl_GetEffectInfo
,
941 IDirectInputDevice2AImpl_GetForceFeedbackState
,
942 IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
943 IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
944 IDirectInputDevice2AImpl_Escape
,
945 JoystickAGenericImpl_Poll
,
946 IDirectInputDevice2AImpl_SendDeviceData
,
947 IDirectInputDevice7AImpl_EnumEffectsInFile
,
948 IDirectInputDevice7AImpl_WriteEffectToFile
,
949 JoystickAGenericImpl_BuildActionMap
,
950 JoystickAGenericImpl_SetActionMap
,
951 IDirectInputDevice8AImpl_GetImageInfo
954 static const IDirectInputDevice8WVtbl JoystickWvt
=
956 IDirectInputDevice2WImpl_QueryInterface
,
957 IDirectInputDevice2WImpl_AddRef
,
958 IDirectInputDevice2WImpl_Release
,
959 JoystickWGenericImpl_GetCapabilities
,
960 IDirectInputDevice2WImpl_EnumObjects
,
961 JoystickLinuxWImpl_GetProperty
,
962 JoystickWGenericImpl_SetProperty
,
963 JoystickLinuxWImpl_Acquire
,
964 JoystickLinuxWImpl_Unacquire
,
965 JoystickWGenericImpl_GetDeviceState
,
966 IDirectInputDevice2WImpl_GetDeviceData
,
967 IDirectInputDevice2WImpl_SetDataFormat
,
968 IDirectInputDevice2WImpl_SetEventNotification
,
969 IDirectInputDevice2WImpl_SetCooperativeLevel
,
970 JoystickWGenericImpl_GetObjectInfo
,
971 JoystickLinuxWImpl_GetDeviceInfo
,
972 IDirectInputDevice2WImpl_RunControlPanel
,
973 IDirectInputDevice2WImpl_Initialize
,
974 IDirectInputDevice2WImpl_CreateEffect
,
975 IDirectInputDevice2WImpl_EnumEffects
,
976 IDirectInputDevice2WImpl_GetEffectInfo
,
977 IDirectInputDevice2WImpl_GetForceFeedbackState
,
978 IDirectInputDevice2WImpl_SendForceFeedbackCommand
,
979 IDirectInputDevice2WImpl_EnumCreatedEffectObjects
,
980 IDirectInputDevice2WImpl_Escape
,
981 JoystickWGenericImpl_Poll
,
982 IDirectInputDevice2WImpl_SendDeviceData
,
983 IDirectInputDevice7WImpl_EnumEffectsInFile
,
984 IDirectInputDevice7WImpl_WriteEffectToFile
,
985 JoystickWGenericImpl_BuildActionMap
,
986 JoystickWGenericImpl_SetActionMap
,
987 IDirectInputDevice8WImpl_GetImageInfo
990 #else /* HAVE_LINUX_22_JOYSTICK_API */
992 const struct dinput_device joystick_linux_device
= {
993 "Wine Linux joystick driver",
999 #endif /* HAVE_LINUX_22_JOYSTICK_API */