jscript: Implement ScriptTypeInfo_GetTypeAttr.
[wine.git] / dlls / dinput / joystick.c
blob01611b0e444b4587594a4c67197ae94098fc8111
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
24 * To Do:
25 * dead zone
26 * force feedback
29 #include <stdio.h>
31 #include "joystick_private.h"
32 #include "wine/debug.h"
33 #include "winreg.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;
99 } else {
100 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
101 return 0;
105 DWORD get_device_type(DWORD version, BOOL is_joystick)
107 if (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)) {
118 unsigned int i;
119 static const struct {
120 DWORD mask;
121 const char *name;
122 } flags[] = {
123 #define FE(x) { x, #x}
124 FE(DIEFF_CARTESIAN),
125 FE(DIEFF_OBJECTIDS),
126 FE(DIEFF_OBJECTOFFSETS),
127 FE(DIEFF_POLAR),
128 FE(DIEFF_SPHERICAL)
129 #undef FE
131 for (i = 0; i < ARRAY_SIZE(flags); i++)
132 if (flags[i].mask & dwFlags)
133 TRACE("%s ", flags[i].name);
134 TRACE("\n");
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)
173 unsigned int i;
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)
183 TRACE("\n");
188 void dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid, DWORD dwFlags)
190 DWORD type = typeFromGUID(guid);
191 unsigned int i;
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);
199 TRACE(" ");
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) {
211 TRACE(" ");
212 for (i = 0; i < eff->cAxes; ++i)
213 TRACE("%d ", eff->rglDirection[i]);
214 TRACE("\n");
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) {
225 TRACE(" ");
226 for (i = 0; i < eff->cAxes; ++i)
227 TRACE("%d ", eff->rgdwAxes[i]);
228 TRACE("\n");
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");
244 } else {
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");
250 } else {
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");
256 } else {
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]);
266 } else {
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");
272 } else {
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 */
289 if (appkey)
291 if (RegOpenKeyA(appkey, joystick_key, &temp)) temp = 0;
292 RegCloseKey(appkey);
293 appkey = temp;
295 if (hkey)
297 if (RegOpenKeyA(hkey, joystick_key, &temp)) temp = 0;
298 RegCloseKey(hkey);
299 hkey = temp;
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);
307 do_disable = TRUE;
310 if (appkey) RegCloseKey(appkey);
311 if (hkey) RegCloseKey(hkey);
313 return do_disable;
316 BOOL is_xinput_device(const DIDEVCAPS *devcaps, WORD vid, WORD pid)
318 int i;
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);
335 DWORD i;
336 ObjProps remap_props;
338 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
340 if (ph == NULL) {
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;
379 default: break;
382 This->props[i].lMin = pr->lMin;
383 This->props[i].lMax = pr->lMax;
385 } else {
386 int obj = find_property(&This->base.data_format, ph);
388 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
389 if (obj >= 0) {
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;
409 default: break;
412 This->props[obj].lMin = pr->lMin;
413 This->props[obj].lMax = pr->lMax;
414 return DI_OK;
417 break;
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;
425 } else {
426 int obj = find_property(&This->base.data_format, ph);
428 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
429 if (obj >= 0) {
430 This->props[obj].lDeadZone = pd->dwData;
431 return DI_OK;
434 break;
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;
442 } else {
443 int obj = find_property(&This->base.data_format, ph);
445 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
446 if (obj >= 0) {
447 This->props[obj].lSaturation = pd->dwData;
448 return DI_OK;
451 break;
453 case (DWORD_PTR)DIPROP_CALIBRATIONMODE: {
454 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
455 FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
456 break;
458 default:
459 return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
463 return DI_OK;
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);
479 switch(type)
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)
503 hid = " (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);
517 #undef DEBUG_TYPE
519 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
521 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
522 int size;
524 TRACE("%p->(%p)\n",This,lpDIDevCaps);
526 if (lpDIDevCaps == NULL) {
527 WARN("invalid pointer\n");
528 return E_POINTER;
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);
544 return DI_OK;
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};
562 HRESULT res;
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);
576 return res;
579 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
580 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
582 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
583 HRESULT res;
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);
597 return res;
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 */
620 if (obj >= 0) {
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);
624 return DI_OK;
626 break;
628 case (DWORD_PTR) DIPROP_DEADZONE: {
629 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
630 int obj = find_property(&This->base.data_format, pdiph);
632 if (obj >= 0) {
633 pd->dwData = This->props[obj].lDeadZone;
634 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
635 return DI_OK;
637 break;
639 case (DWORD_PTR) DIPROP_SATURATION: {
640 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
641 int obj = find_property(&This->base.data_format, pdiph);
643 if (obj >= 0) {
644 pd->dwData = This->props[obj].lSaturation;
645 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
646 return DI_OK;
648 break;
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);
660 else
661 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
663 return DI_OK;
665 default:
666 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
670 return DI_OK;
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);
687 DIPROPDWORD pd;
688 DWORD index = 0;
690 TRACE("(%p,%p)\n", This, pdidi);
692 if (pdidi == NULL) {
693 WARN("invalid pointer\n");
694 return E_POINTER;
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);
706 pd.diph.dwObj = 0;
707 pd.diph.dwHow = DIPH_DEVICE;
708 if (SUCCEEDED(IDirectInputDevice2_GetProperty(iface, DIPROP_JOYSTICKID, &pd.diph)))
709 index = pd.dwData;
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;
721 pdidi->wUsage = 0;
724 return DI_OK;
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];
736 DIPROPDWORD pd;
737 DWORD index = 0;
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);
750 pd.diph.dwObj = 0;
751 pd.diph.dwHow = DIPH_DEVICE;
752 if (SUCCEEDED(IDirectInputDevice2_GetProperty(iface, DIPROP_JOYSTICKID, &pd.diph)))
753 index = pd.dwData;
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;
766 pdidi->wUsage = 0;
769 return DI_OK;
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));
784 return DI_OK;
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);
814 return DI_OK;
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,
827 DWORD dwFlags)
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);
832 unsigned int i, j;
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]);
856 if (odf != NULL)
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;
862 has_actions = TRUE;
864 /* No need to try other types if the action was already mapped */
865 break;
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,
878 LPCSTR lpszUserName,
879 DWORD dwFlags)
881 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
882 DIACTIONFORMATW diafW;
883 HRESULT hr;
884 WCHAR *lpszUserNameW = NULL;
885 int username_size;
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);
903 return hr;
906 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
907 LPDIACTIONFORMATW lpdiaf,
908 LPCWSTR lpszUserName,
909 DWORD dwFlags)
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,
920 LPCSTR lpszUserName,
921 DWORD dwFlags)
923 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
924 DIACTIONFORMATW diafW;
925 HRESULT hr;
926 WCHAR *lpszUserNameW = NULL;
927 int username_size;
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);
944 return hr;
948 * This maps the read value (from the input event) to a value in the
949 * 'wanted' range.
950 * Notes:
951 * Dead zone is in % multiplied by a 100 (range 0..10000)
953 LONG joystick_map_axis(ObjProps *props, int val)
955 LONG ret;
956 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
957 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
959 /* Center input */
960 val -= (props->lDevMin + props->lDevMax) / 2;
962 /* Remove dead zone */
963 if (abs( val ) <= dead_zone / 2)
964 val = 0;
965 else
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,
979 val, ret );
981 return ret;
985 * Maps POV x & y event values to a DX "clock" position:
987 * 31500 4500
988 * 27000 -1 9000
989 * 22500 13500
990 * 18000
992 DWORD joystick_map_pov(const POINTL *p)
994 if (p->x > 0)
995 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
996 else if (p->x < 0)
997 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
998 else
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];
1009 HKEY hkey, appkey;
1010 int tokens = 0;
1011 int axis = 0;
1012 int pov = 0;
1014 get_app_key(&hkey, &appkey);
1016 /* get options */
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 = ",";
1033 char * ptr;
1034 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
1036 if ((ptr = strtok(buffer, delim)) != NULL)
1040 int i;
1042 for (i = 0; i < ARRAY_SIZE(axis_names); i++)
1044 if (!strcmp(ptr, axis_names[i]))
1046 if (!strncmp(ptr, "POV", 3))
1048 if (pov >= 4)
1050 WARN("Only 4 POVs supported - ignoring extra\n");
1051 i = -1;
1053 else
1055 /* Pov takes two axes */
1056 This->axis_map[tokens++] = i;
1057 pov++;
1060 else
1062 if (axis >= 8)
1064 FIXME("Only 8 Axes supported - ignoring extra\n");
1065 i = -1;
1067 else
1068 axis++;
1070 break;
1074 if (i == ARRAY_SIZE(axis_names))
1076 ERR("invalid joystick axis type: \"%s\"\n", ptr);
1077 i = -1;
1080 This->axis_map[tokens] = i;
1081 tokens++;
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;
1091 tokens++;
1096 else
1098 int i;
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];
1107 if (tokens < 0)
1108 continue;
1109 if (tokens < 8)
1110 axis++;
1111 else if (tokens < 15)
1113 i++;
1114 pov++;
1115 This->axis_map[i] = default_axis_map[i];
1119 else
1121 /* No config - set default mapping. */
1122 for (i = 0; i < This->device_axis_count; i++)
1124 if (i < 8)
1125 This->axis_map[i] = axis++;
1126 else if (i < 15)
1128 This->axis_map[i++] = 8 + pov;
1129 This->axis_map[i ] = 8 + pov++;
1131 else
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);
1142 return DI_OK;