1 /* DirectInput Generic Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2009 Aric Stewart, CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "joystick_private.h"
32 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dinput
);
37 #define VID_MICROSOFT 0x045e
39 static const WORD PID_XBOX_CONTROLLERS
[] = {
40 0x0202, /* Xbox Controller */
41 0x0285, /* Xbox Controller S */
42 0x0289, /* Xbox Controller S */
43 0x028e, /* Xbox360 Controller */
44 0x028f, /* Xbox360 Wireless Controller */
45 0x02d1, /* Xbox One Controller */
46 0x02dd, /* Xbox One Controller (Covert Forces/Firmware 2015) */
47 0x02e0, /* Xbox One X Controller */
48 0x02e3, /* Xbox One Elite Controller */
49 0x02e6, /* Wireless XBox Controller Dongle */
50 0x02ea, /* Xbox One S Controller */
51 0x02fd, /* Xbox One S Controller (Firmware 2017) */
52 0x0719, /* Xbox 360 Wireless Adapter */
55 /* Windows uses this GUID for guidProduct on non-keyboard/mouse devices.
56 * Data1 contains the device VID (low word) and PID (high word).
57 * Data4 ends with the ASCII bytes "PIDVID".
59 const GUID DInput_PIDVID_Product_GUID
= { /* device_pidvid-0000-0000-0000-504944564944 */
60 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44}
63 static inline JoystickGenericImpl
*impl_from_IDirectInputDevice8A(IDirectInputDevice8A
*iface
)
65 return CONTAINING_RECORD(CONTAINING_RECORD(iface
, IDirectInputDeviceImpl
, IDirectInputDevice8A_iface
), JoystickGenericImpl
, base
);
67 static inline JoystickGenericImpl
*impl_from_IDirectInputDevice8W(IDirectInputDevice8W
*iface
)
69 return CONTAINING_RECORD(CONTAINING_RECORD(iface
, IDirectInputDeviceImpl
, IDirectInputDevice8W_iface
), JoystickGenericImpl
, base
);
71 static inline IDirectInputDevice8A
*IDirectInputDevice8A_from_impl(JoystickGenericImpl
*This
)
73 return &This
->base
.IDirectInputDevice8A_iface
;
75 static inline IDirectInputDevice8W
*IDirectInputDevice8W_from_impl(JoystickGenericImpl
*This
)
77 return &This
->base
.IDirectInputDevice8W_iface
;
80 DWORD
typeFromGUID(REFGUID guid
)
82 if (IsEqualGUID(guid
, &GUID_ConstantForce
)) {
83 return DIEFT_CONSTANTFORCE
;
84 } else if (IsEqualGUID(guid
, &GUID_Square
)
85 || IsEqualGUID(guid
, &GUID_Sine
)
86 || IsEqualGUID(guid
, &GUID_Triangle
)
87 || IsEqualGUID(guid
, &GUID_SawtoothUp
)
88 || IsEqualGUID(guid
, &GUID_SawtoothDown
)) {
89 return DIEFT_PERIODIC
;
90 } else if (IsEqualGUID(guid
, &GUID_RampForce
)) {
91 return DIEFT_RAMPFORCE
;
92 } else if (IsEqualGUID(guid
, &GUID_Spring
)
93 || IsEqualGUID(guid
, &GUID_Damper
)
94 || IsEqualGUID(guid
, &GUID_Inertia
)
95 || IsEqualGUID(guid
, &GUID_Friction
)) {
96 return DIEFT_CONDITION
;
97 } else if (IsEqualGUID(guid
, &GUID_CustomForce
)) {
98 return DIEFT_CUSTOMFORCE
;
100 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid
));
105 DWORD
get_device_type(DWORD version
, BOOL is_joystick
)
108 return version
>= 0x0800 ? DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8) :
109 DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
111 return version
>= 0x0800 ? DI8DEVTYPE_GAMEPAD
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8) :
112 DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_GAMEPAD
<< 8);
115 static void _dump_DIEFFECT_flags(DWORD dwFlags
)
117 if (TRACE_ON(dinput
)) {
119 static const struct {
123 #define FE(x) { x, #x}
126 FE(DIEFF_OBJECTOFFSETS
),
131 for (i
= 0; i
< ARRAY_SIZE(flags
); i
++)
132 if (flags
[i
].mask
& dwFlags
)
133 TRACE("%s ", flags
[i
].name
);
138 static void _dump_DIENVELOPE(LPCDIENVELOPE env
)
140 if (env
->dwSize
!= sizeof(DIENVELOPE
)) {
141 WARN("Non-standard DIENVELOPE structure size %d.\n", env
->dwSize
);
143 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
144 env
->dwAttackLevel
, env
->dwAttackTime
, env
->dwFadeLevel
, env
->dwFadeTime
);
147 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc
)
149 TRACE("Constant force has magnitude %d\n", frc
->lMagnitude
);
152 static void _dump_DIPERIODIC(LPCDIPERIODIC frc
)
154 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
155 frc
->dwMagnitude
, frc
->lOffset
, frc
->dwPhase
, frc
->dwPeriod
);
158 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc
)
160 TRACE("Ramp force has start %d, end %d\n",
161 frc
->lStart
, frc
->lEnd
);
164 static void _dump_DICONDITION(LPCDICONDITION frc
)
166 TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
167 frc
->lOffset
, frc
->lPositiveCoefficient
, frc
->lNegativeCoefficient
,
168 frc
->dwPositiveSaturation
, frc
->dwNegativeSaturation
, frc
->lDeadBand
);
171 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc
)
174 TRACE("Custom force uses %d channels, sample period %d. Has %d samples at %p.\n",
175 frc
->cChannels
, frc
->dwSamplePeriod
, frc
->cSamples
, frc
->rglForceData
);
176 if (frc
->cSamples
% frc
->cChannels
!= 0)
177 WARN("Custom force has a non-integral samples-per-channel count!\n");
178 if (TRACE_ON(dinput
)) {
179 TRACE("Custom force data (time aligned, axes in order):\n");
180 for (i
= 1; i
<= frc
->cSamples
; ++i
) {
181 TRACE("%d ", frc
->rglForceData
[i
]);
182 if (i
% frc
->cChannels
== 0)
188 void dump_DIEFFECT(LPCDIEFFECT eff
, REFGUID guid
, DWORD dwFlags
)
190 DWORD type
= typeFromGUID(guid
);
193 TRACE("Dumping DIEFFECT structure:\n");
194 TRACE(" - dwSize: %d\n", eff
->dwSize
);
195 if ((eff
->dwSize
!= sizeof(DIEFFECT
)) && (eff
->dwSize
!= sizeof(DIEFFECT_DX5
))) {
196 WARN("Non-standard DIEFFECT structure size %d\n", eff
->dwSize
);
198 TRACE(" - dwFlags: %d\n", eff
->dwFlags
);
200 _dump_DIEFFECT_flags(eff
->dwFlags
);
201 TRACE(" - dwDuration: %d\n", eff
->dwDuration
);
202 TRACE(" - dwGain: %d\n", eff
->dwGain
);
204 if (eff
->dwGain
> 10000)
205 WARN("dwGain is out of range (>10,000)\n");
207 TRACE(" - dwTriggerButton: %d\n", eff
->dwTriggerButton
);
208 TRACE(" - dwTriggerRepeatInterval: %d\n", eff
->dwTriggerRepeatInterval
);
209 TRACE(" - rglDirection: %p\n", eff
->rglDirection
);
210 if (dwFlags
& DIEP_DIRECTION
) {
212 for (i
= 0; i
< eff
->cAxes
; ++i
)
213 TRACE("%d ", eff
->rglDirection
[i
]);
216 TRACE(" - cbTypeSpecificParams: %d\n", eff
->cbTypeSpecificParams
);
217 TRACE(" - lpvTypeSpecificParams: %p\n", eff
->lpvTypeSpecificParams
);
219 /* Only trace some members if dwFlags indicates they have data */
220 if (dwFlags
& DIEP_AXES
) {
221 TRACE(" - cAxes: %d\n", eff
->cAxes
);
222 TRACE(" - rgdwAxes: %p\n", eff
->rgdwAxes
);
224 if (TRACE_ON(dinput
) && eff
->rgdwAxes
) {
226 for (i
= 0; i
< eff
->cAxes
; ++i
)
227 TRACE("%d ", eff
->rgdwAxes
[i
]);
232 if (dwFlags
& DIEP_ENVELOPE
) {
233 TRACE(" - lpEnvelope: %p\n", eff
->lpEnvelope
);
234 if (eff
->lpEnvelope
!= NULL
)
235 _dump_DIENVELOPE(eff
->lpEnvelope
);
238 if (eff
->dwSize
> sizeof(DIEFFECT_DX5
))
239 TRACE(" - dwStartDelay: %d\n", eff
->dwStartDelay
);
241 if (type
== DIEFT_CONSTANTFORCE
) {
242 if (eff
->cbTypeSpecificParams
!= sizeof(DICONSTANTFORCE
)) {
243 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
245 _dump_DICONSTANTFORCE(eff
->lpvTypeSpecificParams
);
247 } else if (type
== DIEFT_PERIODIC
) {
248 if (eff
->cbTypeSpecificParams
!= sizeof(DIPERIODIC
)) {
249 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
251 _dump_DIPERIODIC(eff
->lpvTypeSpecificParams
);
253 } else if (type
== DIEFT_RAMPFORCE
) {
254 if (eff
->cbTypeSpecificParams
!= sizeof(DIRAMPFORCE
)) {
255 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
257 _dump_DIRAMPFORCE(eff
->lpvTypeSpecificParams
);
259 } else if (type
== DIEFT_CONDITION
) {
260 if (eff
->cbTypeSpecificParams
== sizeof(DICONDITION
)) {
261 _dump_DICONDITION(eff
->lpvTypeSpecificParams
);
262 } else if (eff
->cbTypeSpecificParams
== 2 * sizeof(DICONDITION
)) {
263 DICONDITION
*condition
= eff
->lpvTypeSpecificParams
;
264 _dump_DICONDITION(&condition
[0]);
265 _dump_DICONDITION(&condition
[1]);
267 WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
269 } else if (type
== DIEFT_CUSTOMFORCE
) {
270 if (eff
->cbTypeSpecificParams
!= sizeof(DICUSTOMFORCE
)) {
271 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
273 _dump_DICUSTOMFORCE(eff
->lpvTypeSpecificParams
);
278 BOOL
device_disabled_registry(const char* name
)
280 static const char disabled_str
[] = "disabled";
281 static const char joystick_key
[] = "Joysticks";
282 char buffer
[MAX_PATH
];
283 HKEY hkey
, appkey
, temp
;
284 BOOL do_disable
= FALSE
;
286 get_app_key(&hkey
, &appkey
);
288 /* Joystick settings are in the 'joysticks' subkey */
291 if (RegOpenKeyA(appkey
, joystick_key
, &temp
)) temp
= 0;
297 if (RegOpenKeyA(hkey
, joystick_key
, &temp
)) temp
= 0;
302 /* Look for the "controllername"="disabled" key */
303 if (!get_config_key(hkey
, appkey
, name
, buffer
, sizeof(buffer
)))
304 if (!strcmp(disabled_str
, buffer
))
306 TRACE("Disabling joystick '%s' based on registry key.\n", name
);
310 if (appkey
) RegCloseKey(appkey
);
311 if (hkey
) RegCloseKey(hkey
);
316 BOOL
is_xinput_device(const DIDEVCAPS
*devcaps
, WORD vid
, WORD pid
)
320 if (vid
== VID_MICROSOFT
)
322 for (i
= 0; i
< ARRAY_SIZE(PID_XBOX_CONTROLLERS
); i
++)
323 if (pid
== PID_XBOX_CONTROLLERS
[i
]) return TRUE
;
326 return (devcaps
->dwAxes
== 6 && devcaps
->dwButtons
>= 14);
329 /******************************************************************************
330 * SetProperty : change input device properties
332 HRESULT WINAPI
JoystickWGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface
, REFGUID rguid
, LPCDIPROPHEADER ph
)
334 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
336 ObjProps remap_props
;
338 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(rguid
),ph
);
341 WARN("invalid parameter: ph == NULL\n");
342 return DIERR_INVALIDPARAM
;
345 if (TRACE_ON(dinput
))
346 _dump_DIPROPHEADER(ph
);
348 if (IS_DIPROP(rguid
)) {
349 switch (LOWORD(rguid
)) {
350 case (DWORD_PTR
)DIPROP_RANGE
: {
351 LPCDIPROPRANGE pr
= (LPCDIPROPRANGE
)ph
;
352 if (ph
->dwHow
== DIPH_DEVICE
) {
354 /* Many games poll the joystick immediately after setting the range
355 * for calibration purposes, so the old values need to be remapped
356 * to the new range before it does so */
358 TRACE("proprange(%d,%d) all\n", pr
->lMin
, pr
->lMax
);
359 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++) {
361 remap_props
.lDevMin
= This
->props
[i
].lMin
;
362 remap_props
.lDevMax
= This
->props
[i
].lMax
;
364 remap_props
.lDeadZone
= This
->props
[i
].lDeadZone
;
365 remap_props
.lSaturation
= This
->props
[i
].lSaturation
;
367 remap_props
.lMin
= pr
->lMin
;
368 remap_props
.lMax
= pr
->lMax
;
370 switch (This
->base
.data_format
.wine_df
->rgodf
[i
].dwOfs
) {
371 case DIJOFS_X
: This
->js
.lX
= joystick_map_axis(&remap_props
, This
->js
.lX
); break;
372 case DIJOFS_Y
: This
->js
.lY
= joystick_map_axis(&remap_props
, This
->js
.lY
); break;
373 case DIJOFS_Z
: This
->js
.lZ
= joystick_map_axis(&remap_props
, This
->js
.lZ
); break;
374 case DIJOFS_RX
: This
->js
.lRx
= joystick_map_axis(&remap_props
, This
->js
.lRx
); break;
375 case DIJOFS_RY
: This
->js
.lRy
= joystick_map_axis(&remap_props
, This
->js
.lRy
); break;
376 case DIJOFS_RZ
: This
->js
.lRz
= joystick_map_axis(&remap_props
, This
->js
.lRz
); break;
377 case DIJOFS_SLIDER(0): This
->js
.rglSlider
[0] = joystick_map_axis(&remap_props
, This
->js
.rglSlider
[0]); break;
378 case DIJOFS_SLIDER(1): This
->js
.rglSlider
[1] = joystick_map_axis(&remap_props
, This
->js
.rglSlider
[1]); break;
382 This
->props
[i
].lMin
= pr
->lMin
;
383 This
->props
[i
].lMax
= pr
->lMax
;
386 int obj
= find_property(&This
->base
.data_format
, ph
);
388 TRACE("proprange(%d,%d) obj=%d\n", pr
->lMin
, pr
->lMax
, obj
);
391 remap_props
.lDevMin
= This
->props
[obj
].lMin
;
392 remap_props
.lDevMax
= This
->props
[obj
].lMax
;
394 remap_props
.lDeadZone
= This
->props
[obj
].lDeadZone
;
395 remap_props
.lSaturation
= This
->props
[obj
].lSaturation
;
397 remap_props
.lMin
= pr
->lMin
;
398 remap_props
.lMax
= pr
->lMax
;
400 switch (This
->base
.data_format
.wine_df
->rgodf
[obj
].dwOfs
) {
401 case DIJOFS_X
: This
->js
.lX
= joystick_map_axis(&remap_props
, This
->js
.lX
); break;
402 case DIJOFS_Y
: This
->js
.lY
= joystick_map_axis(&remap_props
, This
->js
.lY
); break;
403 case DIJOFS_Z
: This
->js
.lZ
= joystick_map_axis(&remap_props
, This
->js
.lZ
); break;
404 case DIJOFS_RX
: This
->js
.lRx
= joystick_map_axis(&remap_props
, This
->js
.lRx
); break;
405 case DIJOFS_RY
: This
->js
.lRy
= joystick_map_axis(&remap_props
, This
->js
.lRy
); break;
406 case DIJOFS_RZ
: This
->js
.lRz
= joystick_map_axis(&remap_props
, This
->js
.lRz
); break;
407 case DIJOFS_SLIDER(0): This
->js
.rglSlider
[0] = joystick_map_axis(&remap_props
, This
->js
.rglSlider
[0]); break;
408 case DIJOFS_SLIDER(1): This
->js
.rglSlider
[1] = joystick_map_axis(&remap_props
, This
->js
.rglSlider
[1]); break;
412 This
->props
[obj
].lMin
= pr
->lMin
;
413 This
->props
[obj
].lMax
= pr
->lMax
;
419 case (DWORD_PTR
)DIPROP_DEADZONE
: {
420 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
421 if (ph
->dwHow
== DIPH_DEVICE
) {
422 TRACE("deadzone(%d) all\n", pd
->dwData
);
423 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++)
424 This
->props
[i
].lDeadZone
= pd
->dwData
;
426 int obj
= find_property(&This
->base
.data_format
, ph
);
428 TRACE("deadzone(%d) obj=%d\n", pd
->dwData
, obj
);
430 This
->props
[obj
].lDeadZone
= pd
->dwData
;
436 case (DWORD_PTR
)DIPROP_SATURATION
: {
437 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
438 if (ph
->dwHow
== DIPH_DEVICE
) {
439 TRACE("saturation(%d) all\n", pd
->dwData
);
440 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++)
441 This
->props
[i
].lSaturation
= pd
->dwData
;
443 int obj
= find_property(&This
->base
.data_format
, ph
);
445 TRACE("saturation(%d) obj=%d\n", pd
->dwData
, obj
);
447 This
->props
[obj
].lSaturation
= pd
->dwData
;
453 case (DWORD_PTR
)DIPROP_CALIBRATIONMODE
: {
454 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
455 FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd
->dwData
);
459 return IDirectInputDevice2WImpl_SetProperty(iface
, rguid
, ph
);
466 HRESULT WINAPI
JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface
, REFGUID rguid
, LPCDIPROPHEADER ph
)
468 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
469 return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This
), rguid
, ph
);
472 #define DEBUG_TYPE(x) case (x): str = #x; break
473 void _dump_DIDEVCAPS(const DIDEVCAPS
*lpDIDevCaps
)
475 int type
= GET_DIDEVICE_TYPE(lpDIDevCaps
->dwDevType
);
476 const char *str
, *hid
= "";
477 TRACE("dwSize: %d\n", lpDIDevCaps
->dwSize
);
478 TRACE("dwFlags: %08x\n", lpDIDevCaps
->dwFlags
);
481 /* Direct X <= 7 definitions */
482 DEBUG_TYPE(DIDEVTYPE_DEVICE
);
483 DEBUG_TYPE(DIDEVTYPE_MOUSE
);
484 DEBUG_TYPE(DIDEVTYPE_KEYBOARD
);
485 DEBUG_TYPE(DIDEVTYPE_JOYSTICK
);
486 /* Direct X >= 8 definitions */
487 DEBUG_TYPE(DI8DEVTYPE_DEVICE
);
488 DEBUG_TYPE(DI8DEVTYPE_MOUSE
);
489 DEBUG_TYPE(DI8DEVTYPE_KEYBOARD
);
490 DEBUG_TYPE(DI8DEVTYPE_JOYSTICK
);
491 DEBUG_TYPE(DI8DEVTYPE_GAMEPAD
);
492 DEBUG_TYPE(DI8DEVTYPE_DRIVING
);
493 DEBUG_TYPE(DI8DEVTYPE_FLIGHT
);
494 DEBUG_TYPE(DI8DEVTYPE_1STPERSON
);
495 DEBUG_TYPE(DI8DEVTYPE_DEVICECTRL
);
496 DEBUG_TYPE(DI8DEVTYPE_SCREENPOINTER
);
497 DEBUG_TYPE(DI8DEVTYPE_REMOTE
);
498 DEBUG_TYPE(DI8DEVTYPE_SUPPLEMENTAL
);
499 default: str
= "UNKNOWN";
502 if (lpDIDevCaps
->dwDevType
& DIDEVTYPE_HID
)
505 TRACE("dwDevType: %08x %s%s\n", lpDIDevCaps
->dwDevType
, str
, hid
);
506 TRACE("dwAxes: %d\n", lpDIDevCaps
->dwAxes
);
507 TRACE("dwButtons: %d\n", lpDIDevCaps
->dwButtons
);
508 TRACE("dwPOVs: %d\n", lpDIDevCaps
->dwPOVs
);
509 if (lpDIDevCaps
->dwSize
> sizeof(DIDEVCAPS_DX3
)) {
510 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps
->dwFFSamplePeriod
);
511 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps
->dwFFMinTimeResolution
);
512 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps
->dwFirmwareRevision
);
513 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps
->dwHardwareRevision
);
514 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps
->dwFFDriverVersion
);
519 HRESULT WINAPI
JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface
, LPDIDEVCAPS lpDIDevCaps
)
521 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
524 TRACE("%p->(%p)\n",This
,lpDIDevCaps
);
526 if (lpDIDevCaps
== NULL
) {
527 WARN("invalid pointer\n");
531 size
= lpDIDevCaps
->dwSize
;
533 if (!(size
== sizeof(DIDEVCAPS
) || size
== sizeof(DIDEVCAPS_DX3
))) {
534 WARN("invalid parameter\n");
535 return DIERR_INVALIDPARAM
;
538 CopyMemory(lpDIDevCaps
, &This
->devcaps
, size
);
539 lpDIDevCaps
->dwSize
= size
;
541 if (TRACE_ON(dinput
))
542 _dump_DIDEVCAPS(lpDIDevCaps
);
547 HRESULT WINAPI
JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface
, LPDIDEVCAPS lpDIDevCaps
)
549 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
550 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This
), lpDIDevCaps
);
553 /******************************************************************************
554 * GetObjectInfo : get object info
556 HRESULT WINAPI
JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface
,
557 LPDIDEVICEOBJECTINSTANCEW pdidoi
, DWORD dwObj
, DWORD dwHow
)
559 static const WCHAR axisW
[] = {'A','x','i','s',' ','%','d',0};
560 static const WCHAR povW
[] = {'P','O','V',' ','%','d',0};
561 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',' ','%','d',0};
564 res
= IDirectInputDevice2WImpl_GetObjectInfo(iface
, pdidoi
, dwObj
, dwHow
);
565 if (res
!= DI_OK
) return res
;
567 if (pdidoi
->dwType
& DIDFT_AXIS
) {
568 sprintfW(pdidoi
->tszName
, axisW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
569 pdidoi
->dwFlags
|= DIDOI_ASPECTPOSITION
;
570 } else if (pdidoi
->dwType
& DIDFT_POV
)
571 sprintfW(pdidoi
->tszName
, povW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
572 else if (pdidoi
->dwType
& DIDFT_BUTTON
)
573 sprintfW(pdidoi
->tszName
, buttonW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
575 _dump_OBJECTINSTANCEW(pdidoi
);
579 HRESULT WINAPI
JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface
,
580 LPDIDEVICEOBJECTINSTANCEA pdidoi
, DWORD dwObj
, DWORD dwHow
)
582 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
584 DIDEVICEOBJECTINSTANCEW didoiW
;
585 DWORD dwSize
= pdidoi
->dwSize
;
587 didoiW
.dwSize
= sizeof(didoiW
);
588 res
= JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This
), &didoiW
, dwObj
, dwHow
);
589 if (res
!= DI_OK
) return res
;
591 memset(pdidoi
, 0, pdidoi
->dwSize
);
592 memcpy(pdidoi
, &didoiW
, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW
, tszName
));
593 pdidoi
->dwSize
= dwSize
;
594 WideCharToMultiByte(CP_ACP
, 0, didoiW
.tszName
, -1, pdidoi
->tszName
,
595 sizeof(pdidoi
->tszName
), NULL
, NULL
);
600 /******************************************************************************
601 * GetProperty : get input device properties
603 HRESULT WINAPI
JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface
, REFGUID rguid
, LPDIPROPHEADER pdiph
)
605 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
607 TRACE("(%p,%s,%p)\n", This
, debugstr_guid(rguid
), pdiph
);
609 if (TRACE_ON(dinput
))
610 _dump_DIPROPHEADER(pdiph
);
612 if (IS_DIPROP(rguid
)) {
613 switch (LOWORD(rguid
)) {
614 case (DWORD_PTR
) DIPROP_RANGE
: {
615 LPDIPROPRANGE pr
= (LPDIPROPRANGE
)pdiph
;
616 int obj
= find_property(&This
->base
.data_format
, pdiph
);
618 /* The app is querying the current range of the axis
619 * return the lMin and lMax values */
621 pr
->lMin
= This
->props
[obj
].lMin
;
622 pr
->lMax
= This
->props
[obj
].lMax
;
623 TRACE("range(%d, %d) obj=%d\n", pr
->lMin
, pr
->lMax
, obj
);
628 case (DWORD_PTR
) DIPROP_DEADZONE
: {
629 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
630 int obj
= find_property(&This
->base
.data_format
, pdiph
);
633 pd
->dwData
= This
->props
[obj
].lDeadZone
;
634 TRACE("deadzone(%d) obj=%d\n", pd
->dwData
, obj
);
639 case (DWORD_PTR
) DIPROP_SATURATION
: {
640 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
641 int obj
= find_property(&This
->base
.data_format
, pdiph
);
644 pd
->dwData
= This
->props
[obj
].lSaturation
;
645 TRACE("saturation(%d) obj=%d\n", pd
->dwData
, obj
);
650 case (DWORD_PTR
) DIPROP_PRODUCTNAME
:
651 case (DWORD_PTR
) DIPROP_INSTANCENAME
: {
652 DIPROPSTRING
*ps
= (DIPROPSTRING
*) pdiph
;
653 DIDEVICEINSTANCEW didev
;
655 didev
.dwSize
= sizeof(didev
);
657 IDirectInputDevice_GetDeviceInfo(iface
, &didev
);
658 if (LOWORD(rguid
) == (DWORD_PTR
) DIPROP_PRODUCTNAME
)
659 lstrcpynW(ps
->wsz
, didev
.tszProductName
, MAX_PATH
);
661 lstrcpynW(ps
->wsz
, didev
.tszInstanceName
, MAX_PATH
);
666 return IDirectInputDevice2WImpl_GetProperty(iface
, rguid
, pdiph
);
673 HRESULT WINAPI
JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface
, REFGUID rguid
, LPDIPROPHEADER pdiph
)
675 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
676 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This
), rguid
, pdiph
);
679 /******************************************************************************
680 * GetDeviceInfo : get information about a device's identity
682 HRESULT WINAPI
JoystickAGenericImpl_GetDeviceInfo(
683 LPDIRECTINPUTDEVICE8A iface
,
684 LPDIDEVICEINSTANCEA pdidi
)
686 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
690 TRACE("(%p,%p)\n", This
, pdidi
);
693 WARN("invalid pointer\n");
697 if ((pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3A
)) &&
698 (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEA
))) {
699 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi
->dwSize
);
700 return DIERR_INVALIDPARAM
;
703 /* Try to get joystick index */
704 pd
.diph
.dwSize
= sizeof(pd
);
705 pd
.diph
.dwHeaderSize
= sizeof(pd
.diph
);
707 pd
.diph
.dwHow
= DIPH_DEVICE
;
708 if (SUCCEEDED(IDirectInputDevice2_GetProperty(iface
, DIPROP_JOYSTICKID
, &pd
.diph
)))
711 /* Return joystick */
712 pdidi
->guidInstance
= This
->guidInstance
;
713 pdidi
->guidProduct
= This
->guidProduct
;
714 /* we only support traditional joysticks for now */
715 pdidi
->dwDevType
= This
->devcaps
.dwDevType
;
716 snprintf(pdidi
->tszInstanceName
, MAX_PATH
, "Joystick %d", index
);
717 lstrcpynA(pdidi
->tszProductName
, This
->name
, MAX_PATH
);
718 if (pdidi
->dwSize
> sizeof(DIDEVICEINSTANCE_DX3A
)) {
719 pdidi
->guidFFDriver
= GUID_NULL
;
720 pdidi
->wUsagePage
= 0;
727 /******************************************************************************
728 * GetDeviceInfo : get information about a device's identity
730 HRESULT WINAPI
JoystickWGenericImpl_GetDeviceInfo(
731 LPDIRECTINPUTDEVICE8W iface
,
732 LPDIDEVICEINSTANCEW pdidi
)
734 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
735 CHAR buffer
[MAX_PATH
];
739 TRACE("(%p,%p)\n", iface
, pdidi
);
741 if ((pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3W
)) &&
742 (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEW
))) {
743 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi
->dwSize
);
744 return DIERR_INVALIDPARAM
;
747 /* Try to get joystick index */
748 pd
.diph
.dwSize
= sizeof(pd
);
749 pd
.diph
.dwHeaderSize
= sizeof(pd
.diph
);
751 pd
.diph
.dwHow
= DIPH_DEVICE
;
752 if (SUCCEEDED(IDirectInputDevice2_GetProperty(iface
, DIPROP_JOYSTICKID
, &pd
.diph
)))
755 /* Return joystick */
756 pdidi
->guidInstance
= This
->guidInstance
;
757 pdidi
->guidProduct
= This
->guidProduct
;
758 /* we only support traditional joysticks for now */
759 pdidi
->dwDevType
= This
->devcaps
.dwDevType
;
760 snprintf(buffer
, sizeof(buffer
), "Joystick %d", index
);
761 MultiByteToWideChar(CP_ACP
, 0, buffer
, -1, pdidi
->tszInstanceName
, MAX_PATH
);
762 MultiByteToWideChar(CP_ACP
, 0, This
->name
, -1, pdidi
->tszProductName
, MAX_PATH
);
763 if (pdidi
->dwSize
> sizeof(DIDEVICEINSTANCE_DX3W
)) {
764 pdidi
->guidFFDriver
= GUID_NULL
;
765 pdidi
->wUsagePage
= 0;
772 HRESULT WINAPI
JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface
)
774 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
776 TRACE("(%p)\n",This
);
778 if (!This
->base
.acquired
) {
779 WARN("not acquired\n");
780 return DIERR_NOTACQUIRED
;
783 This
->joy_polldev(IDirectInputDevice8A_from_impl(This
));
787 HRESULT WINAPI
JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface
)
789 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
790 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This
));
793 /******************************************************************************
794 * GetDeviceState : returns the "state" of the joystick.
797 HRESULT WINAPI
JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface
, DWORD len
, LPVOID ptr
)
799 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
801 TRACE("(%p,0x%08x,%p)\n", This
, len
, ptr
);
803 if (!This
->base
.acquired
) {
804 WARN("not acquired\n");
805 return DIERR_NOTACQUIRED
;
808 /* update joystick state */
809 This
->joy_polldev(IDirectInputDevice8A_from_impl(This
));
811 /* convert and copy data to user supplied buffer */
812 fill_DataFormat(ptr
, len
, &This
->js
, &This
->base
.data_format
);
817 HRESULT WINAPI
JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface
, DWORD len
, LPVOID ptr
)
819 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
820 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This
), len
, ptr
);
824 HRESULT WINAPI
JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface
,
825 LPDIACTIONFORMATW lpdiaf
,
826 LPCWSTR lpszUserName
,
829 static const DWORD object_types
[] = { DIDFT_AXIS
, DIDFT_BUTTON
};
830 static const DWORD type_map
[] = { DIDFT_RELAXIS
, DIDFT_PSHBUTTON
};
831 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
833 BOOL has_actions
= FALSE
;
835 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", This
, lpdiaf
, debugstr_w(lpszUserName
), dwFlags
);
837 for (i
=0; i
< lpdiaf
->dwNumActions
; i
++)
839 DWORD inst
= (0x000000ff & (lpdiaf
->rgoAction
[i
].dwSemantic
)) - 1;
840 DWORD type
= 0x000000ff & (lpdiaf
->rgoAction
[i
].dwSemantic
>> 8);
841 DWORD genre
= 0xff000000 & lpdiaf
->rgoAction
[i
].dwSemantic
;
843 /* Don't touch a user configured action */
844 if (lpdiaf
->rgoAction
[i
].dwHow
== DIAH_USERCONFIG
) continue;
846 /* Only consider actions of the right genre */
847 if (lpdiaf
->dwGenre
!= genre
&& genre
!= DIGENRE_ANY
) continue;
849 for (j
= 0; j
< ARRAY_SIZE(object_types
); j
++)
851 if (type
& object_types
[j
])
853 /* Ensure that the object exists */
854 LPDIOBJECTDATAFORMAT odf
= dataformat_to_odf_by_type(This
->base
.data_format
.wine_df
, inst
, object_types
[j
]);
858 lpdiaf
->rgoAction
[i
].dwObjID
= type_map
[j
] | (0x0000ff00 & (inst
<< 8));
859 lpdiaf
->rgoAction
[i
].guidInstance
= This
->base
.guid
;
860 lpdiaf
->rgoAction
[i
].dwHow
= DIAH_DEFAULT
;
864 /* No need to try other types if the action was already mapped */
871 if (!has_actions
) return DI_NOEFFECT
;
873 return IDirectInputDevice8WImpl_BuildActionMap(iface
, lpdiaf
, lpszUserName
, dwFlags
);
876 HRESULT WINAPI
JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface
,
877 LPDIACTIONFORMATA lpdiaf
,
881 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
882 DIACTIONFORMATW diafW
;
884 WCHAR
*lpszUserNameW
= NULL
;
887 diafW
.rgoAction
= HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW
)*lpdiaf
->dwNumActions
);
888 _copy_diactionformatAtoW(&diafW
, lpdiaf
);
890 if (lpszUserName
!= NULL
)
892 username_size
= MultiByteToWideChar(CP_ACP
, 0, lpszUserName
, -1, NULL
, 0);
893 lpszUserNameW
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*username_size
);
894 MultiByteToWideChar(CP_ACP
, 0, lpszUserName
, -1, lpszUserNameW
, username_size
);
897 hr
= JoystickWGenericImpl_BuildActionMap(&This
->base
.IDirectInputDevice8W_iface
, &diafW
, lpszUserNameW
, dwFlags
);
899 _copy_diactionformatWtoA(lpdiaf
, &diafW
);
900 HeapFree(GetProcessHeap(), 0, diafW
.rgoAction
);
901 HeapFree(GetProcessHeap(), 0, lpszUserNameW
);
906 HRESULT WINAPI
JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface
,
907 LPDIACTIONFORMATW lpdiaf
,
908 LPCWSTR lpszUserName
,
911 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8W(iface
);
913 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", This
, lpdiaf
, debugstr_w(lpszUserName
), dwFlags
);
915 return _set_action_map(iface
, lpdiaf
, lpszUserName
, dwFlags
, This
->base
.data_format
.wine_df
);
918 HRESULT WINAPI
JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface
,
919 LPDIACTIONFORMATA lpdiaf
,
923 JoystickGenericImpl
*This
= impl_from_IDirectInputDevice8A(iface
);
924 DIACTIONFORMATW diafW
;
926 WCHAR
*lpszUserNameW
= NULL
;
929 diafW
.rgoAction
= HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW
)*lpdiaf
->dwNumActions
);
930 _copy_diactionformatAtoW(&diafW
, lpdiaf
);
932 if (lpszUserName
!= NULL
)
934 username_size
= MultiByteToWideChar(CP_ACP
, 0, lpszUserName
, -1, NULL
, 0);
935 lpszUserNameW
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
)*username_size
);
936 MultiByteToWideChar(CP_ACP
, 0, lpszUserName
, -1, lpszUserNameW
, username_size
);
939 hr
= JoystickWGenericImpl_SetActionMap(&This
->base
.IDirectInputDevice8W_iface
, &diafW
, lpszUserNameW
, dwFlags
);
941 HeapFree(GetProcessHeap(), 0, diafW
.rgoAction
);
942 HeapFree(GetProcessHeap(), 0, lpszUserNameW
);
948 * This maps the read value (from the input event) to a value in the
951 * Dead zone is in % multiplied by a 100 (range 0..10000)
953 LONG
joystick_map_axis(ObjProps
*props
, int val
)
956 LONG dead_zone
= MulDiv( props
->lDeadZone
, props
->lDevMax
- props
->lDevMin
, 10000 );
957 LONG dev_range
= props
->lDevMax
- props
->lDevMin
- dead_zone
;
960 val
-= (props
->lDevMin
+ props
->lDevMax
) / 2;
962 /* Remove dead zone */
963 if (abs( val
) <= dead_zone
/ 2)
966 val
= val
< 0 ? val
+ dead_zone
/ 2 : val
- dead_zone
/ 2;
968 /* Scale and map the value from the device range into the required range */
969 ret
= MulDiv( val
, props
->lMax
- props
->lMin
, dev_range
) +
970 (props
->lMin
+ props
->lMax
) / 2;
972 /* Clamp in case or rounding errors */
973 if (ret
> props
->lMax
) ret
= props
->lMax
;
974 else if (ret
< props
->lMin
) ret
= props
->lMin
;
976 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
977 props
->lDevMin
, dead_zone
, props
->lDevMax
,
978 props
->lMin
, props
->lDeadZone
, props
->lMax
,
985 * Maps POV x & y event values to a DX "clock" position:
992 DWORD
joystick_map_pov(const POINTL
*p
)
995 return p
->y
< 0 ? 4500 : !p
->y
? 9000 : 13500;
997 return p
->y
< 0 ? 31500 : !p
->y
? 27000 : 22500;
999 return p
->y
< 0 ? 0 : !p
->y
? -1 : 18000;
1003 * Setup the dinput options.
1006 HRESULT
setup_dinput_options(JoystickGenericImpl
*This
, const int *default_axis_map
)
1008 char buffer
[MAX_PATH
+16];
1014 get_app_key(&hkey
, &appkey
);
1018 if (!get_config_key(hkey
, appkey
, "DefaultDeadZone", buffer
, sizeof(buffer
)))
1020 This
->deadzone
= atoi(buffer
);
1021 TRACE("setting default deadzone to: \"%s\" %d\n", buffer
, This
->deadzone
);
1024 This
->axis_map
= HeapAlloc(GetProcessHeap(), 0, This
->device_axis_count
* sizeof(int));
1025 if (!This
->axis_map
) return DIERR_OUTOFMEMORY
;
1027 if (!get_config_key(hkey
, appkey
, This
->name
, buffer
, sizeof(buffer
)))
1029 static const char *axis_names
[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
1030 "Slider1", "Slider2",
1031 "POV1", "POV2", "POV3", "POV4"};
1032 const char *delim
= ",";
1034 TRACE("\"%s\" = \"%s\"\n", This
->name
, buffer
);
1036 if ((ptr
= strtok(buffer
, delim
)) != NULL
)
1042 for (i
= 0; i
< ARRAY_SIZE(axis_names
); i
++)
1044 if (!strcmp(ptr
, axis_names
[i
]))
1046 if (!strncmp(ptr
, "POV", 3))
1050 WARN("Only 4 POVs supported - ignoring extra\n");
1055 /* Pov takes two axes */
1056 This
->axis_map
[tokens
++] = i
;
1064 FIXME("Only 8 Axes supported - ignoring extra\n");
1074 if (i
== ARRAY_SIZE(axis_names
))
1076 ERR("invalid joystick axis type: \"%s\"\n", ptr
);
1080 This
->axis_map
[tokens
] = i
;
1082 } while ((ptr
= strtok(NULL
, delim
)) != NULL
);
1084 if (tokens
!= This
->device_axis_count
)
1086 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
1087 This
->device_axis_count
, axis
, pov
, tokens
);
1088 while (tokens
< This
->device_axis_count
)
1090 This
->axis_map
[tokens
] = -1;
1100 if (default_axis_map
)
1102 /* Use default mapping from the driver */
1103 for (i
= 0; i
< This
->device_axis_count
; i
++)
1105 This
->axis_map
[i
] = default_axis_map
[i
];
1106 tokens
= default_axis_map
[i
];
1111 else if (tokens
< 15)
1115 This
->axis_map
[i
] = default_axis_map
[i
];
1121 /* No config - set default mapping. */
1122 for (i
= 0; i
< This
->device_axis_count
; i
++)
1125 This
->axis_map
[i
] = axis
++;
1128 This
->axis_map
[i
++] = 8 + pov
;
1129 This
->axis_map
[i
] = 8 + pov
++;
1132 This
->axis_map
[i
] = -1;
1136 This
->devcaps
.dwAxes
= axis
;
1137 This
->devcaps
.dwPOVs
= pov
;
1139 if (appkey
) RegCloseKey(appkey
);
1140 if (hkey
) RegCloseKey(hkey
);