dinput/tests: Avoid assigning values twice (PVS-Studio).
[wine/multimedia.git] / dlls / dinput / joystick.c
blob547ba561245fdb1e4564f334374f81cc334ce2ba
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 "joystick_private.h"
30 #include "wine/debug.h"
31 #include "winreg.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
35 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
37 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface), JoystickGenericImpl, base);
39 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
41 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface), JoystickGenericImpl, base);
43 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickGenericImpl *This)
45 return &This->base.IDirectInputDevice8A_iface;
47 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickGenericImpl *This)
49 return &This->base.IDirectInputDevice8W_iface;
52 DWORD typeFromGUID(REFGUID guid)
54 if (IsEqualGUID(guid, &GUID_ConstantForce)) {
55 return DIEFT_CONSTANTFORCE;
56 } else if (IsEqualGUID(guid, &GUID_Square)
57 || IsEqualGUID(guid, &GUID_Sine)
58 || IsEqualGUID(guid, &GUID_Triangle)
59 || IsEqualGUID(guid, &GUID_SawtoothUp)
60 || IsEqualGUID(guid, &GUID_SawtoothDown)) {
61 return DIEFT_PERIODIC;
62 } else if (IsEqualGUID(guid, &GUID_RampForce)) {
63 return DIEFT_RAMPFORCE;
64 } else if (IsEqualGUID(guid, &GUID_Spring)
65 || IsEqualGUID(guid, &GUID_Damper)
66 || IsEqualGUID(guid, &GUID_Inertia)
67 || IsEqualGUID(guid, &GUID_Friction)) {
68 return DIEFT_CONDITION;
69 } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
70 return DIEFT_CUSTOMFORCE;
71 } else {
72 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
73 return 0;
77 static void _dump_DIEFFECT_flags(DWORD dwFlags)
79 if (TRACE_ON(dinput)) {
80 unsigned int i;
81 static const struct {
82 DWORD mask;
83 const char *name;
84 } flags[] = {
85 #define FE(x) { x, #x}
86 FE(DIEFF_CARTESIAN),
87 FE(DIEFF_OBJECTIDS),
88 FE(DIEFF_OBJECTOFFSETS),
89 FE(DIEFF_POLAR),
90 FE(DIEFF_SPHERICAL)
91 #undef FE
93 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
94 if (flags[i].mask & dwFlags)
95 TRACE("%s ", flags[i].name);
96 TRACE("\n");
100 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
102 if (env->dwSize != sizeof(DIENVELOPE)) {
103 WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
105 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
106 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
109 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
111 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
114 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
116 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
117 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
120 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
122 TRACE("Ramp force has start %d, end %d\n",
123 frc->lStart, frc->lEnd);
126 static void _dump_DICONDITION(LPCDICONDITION frc)
128 TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
129 frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
130 frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
133 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
135 unsigned int i;
136 TRACE("Custom force uses %d channels, sample period %d. Has %d samples at %p.\n",
137 frc->cChannels, frc->dwSamplePeriod, frc->cSamples, frc->rglForceData);
138 if (frc->cSamples % frc->cChannels != 0)
139 WARN("Custom force has a non-integral samples-per-channel count!\n");
140 if (TRACE_ON(dinput)) {
141 TRACE("Custom force data (time aligned, axes in order):\n");
142 for (i = 1; i <= frc->cSamples; ++i) {
143 TRACE("%d ", frc->rglForceData[i]);
144 if (i % frc->cChannels == 0)
145 TRACE("\n");
150 void dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid, DWORD dwFlags)
152 DWORD type = typeFromGUID(guid);
153 unsigned int i;
155 TRACE("Dumping DIEFFECT structure:\n");
156 TRACE(" - dwSize: %d\n", eff->dwSize);
157 if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
158 WARN("Non-standard DIEFFECT structure size %d\n", eff->dwSize);
160 TRACE(" - dwFlags: %d\n", eff->dwFlags);
161 TRACE(" ");
162 _dump_DIEFFECT_flags(eff->dwFlags);
163 TRACE(" - dwDuration: %d\n", eff->dwDuration);
164 TRACE(" - dwGain: %d\n", eff->dwGain);
166 if (eff->dwGain > 10000)
167 WARN("dwGain is out of range (>10,000)\n");
169 TRACE(" - dwTriggerButton: %d\n", eff->dwTriggerButton);
170 TRACE(" - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
171 TRACE(" - rglDirection: %p\n", eff->rglDirection);
172 TRACE(" - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
173 TRACE(" - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
175 /* Only trace some members if dwFlags indicates they have data */
176 if (dwFlags & DIEP_AXES) {
177 TRACE(" - cAxes: %d\n", eff->cAxes);
178 TRACE(" - rgdwAxes: %p\n", eff->rgdwAxes);
180 if (TRACE_ON(dinput) && eff->rgdwAxes) {
181 TRACE(" ");
182 for (i = 0; i < eff->cAxes; ++i)
183 TRACE("%d ", eff->rgdwAxes[i]);
184 TRACE("\n");
188 if (dwFlags & DIEP_ENVELOPE) {
189 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
190 if (eff->lpEnvelope != NULL)
191 _dump_DIENVELOPE(eff->lpEnvelope);
194 if (eff->dwSize > sizeof(DIEFFECT_DX5))
195 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
197 if (type == DIEFT_CONSTANTFORCE) {
198 if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
199 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
200 } else {
201 _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
203 } else if (type == DIEFT_PERIODIC) {
204 if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
205 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
206 } else {
207 _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
209 } else if (type == DIEFT_RAMPFORCE) {
210 if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
211 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
212 } else {
213 _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
215 } else if (type == DIEFT_CONDITION) {
216 if (eff->cbTypeSpecificParams != sizeof(DICONDITION)) {
217 WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
218 } else {
219 _dump_DICONDITION(eff->lpvTypeSpecificParams);
221 } else if (type == DIEFT_CUSTOMFORCE) {
222 if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
223 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
224 } else {
225 _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
230 BOOL device_disabled_registry(const char* name)
232 static const char disabled_str[] = "disabled";
233 static const char joystick_key[] = "Joysticks";
234 char buffer[MAX_PATH];
235 HKEY hkey, appkey, temp;
236 BOOL do_disable = FALSE;
238 get_app_key(&hkey, &appkey);
240 /* Joystick settings are in the 'joysticks' subkey */
241 if (appkey)
243 if (RegOpenKeyA(appkey, joystick_key, &temp)) temp = 0;
244 RegCloseKey(appkey);
245 appkey = temp;
247 if (hkey)
249 if (RegOpenKeyA(hkey, joystick_key, &temp)) temp = 0;
250 RegCloseKey(hkey);
251 hkey = temp;
254 /* Look for the "controllername"="disabled" key */
255 if (!get_config_key(hkey, appkey, name, buffer, sizeof(buffer)))
256 if (!strcmp(disabled_str, buffer))
258 TRACE("Disabling joystick '%s' based on registry key.\n", name);
259 do_disable = TRUE;
262 if (appkey) RegCloseKey(appkey);
263 if (hkey) RegCloseKey(hkey);
265 return do_disable;
268 /******************************************************************************
269 * SetProperty : change input device properties
271 HRESULT WINAPI JoystickWGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
273 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
274 DWORD i;
275 ObjProps remap_props;
277 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
279 if (ph == NULL) {
280 WARN("invalid parameter: ph == NULL\n");
281 return DIERR_INVALIDPARAM;
284 if (TRACE_ON(dinput))
285 _dump_DIPROPHEADER(ph);
287 if (IS_DIPROP(rguid)) {
288 switch (LOWORD(rguid)) {
289 case (DWORD_PTR)DIPROP_RANGE: {
290 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
291 if (ph->dwHow == DIPH_DEVICE) {
292 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
293 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
295 remap_props.lDevMin = This->props[i].lMin;
296 remap_props.lDevMax = This->props[i].lMax;
298 remap_props.lDeadZone = This->props[i].lDeadZone;
299 remap_props.lSaturation = This->props[i].lSaturation;
301 remap_props.lMin = pr->lMin;
302 remap_props.lMax = pr->lMax;
304 switch (This->base.data_format.wine_df->rgodf[i].dwOfs) {
305 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
306 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
307 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
308 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
309 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
310 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
311 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
312 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
313 default: break;
316 This->props[i].lMin = pr->lMin;
317 This->props[i].lMax = pr->lMax;
319 } else {
320 int obj = find_property(&This->base.data_format, ph);
322 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
323 if (obj >= 0) {
325 /*ePSXe polls the joystick immediately after setting the range for calibration purposes, so the old values need to be remapped to the new range before it does so*/
327 remap_props.lDevMin = This->props[obj].lMin;
328 remap_props.lDevMax = This->props[obj].lMax;
330 remap_props.lDeadZone = This->props[obj].lDeadZone;
331 remap_props.lSaturation = This->props[obj].lSaturation;
333 remap_props.lMin = pr->lMin;
334 remap_props.lMax = pr->lMax;
336 switch (ph->dwObj) {
337 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
338 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
339 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
340 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
341 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
342 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
343 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
344 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
345 default: break;
348 This->props[obj].lMin = pr->lMin;
349 This->props[obj].lMax = pr->lMax;
350 return DI_OK;
353 break;
355 case (DWORD_PTR)DIPROP_DEADZONE: {
356 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
357 if (ph->dwHow == DIPH_DEVICE) {
358 TRACE("deadzone(%d) all\n", pd->dwData);
359 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
360 This->props[i].lDeadZone = pd->dwData;
361 } else {
362 int obj = find_property(&This->base.data_format, ph);
364 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
365 if (obj >= 0) {
366 This->props[obj].lDeadZone = pd->dwData;
367 return DI_OK;
370 break;
372 case (DWORD_PTR)DIPROP_SATURATION: {
373 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
374 if (ph->dwHow == DIPH_DEVICE) {
375 TRACE("saturation(%d) all\n", pd->dwData);
376 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
377 This->props[i].lSaturation = pd->dwData;
378 } else {
379 int obj = find_property(&This->base.data_format, ph);
381 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
382 if (obj >= 0) {
383 This->props[obj].lSaturation = pd->dwData;
384 return DI_OK;
387 break;
389 default:
390 return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
394 return DI_OK;
397 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
399 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
400 return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
403 #define DEBUG_TYPE(x) case (x): str = #x; break
404 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
406 int type = GET_DIDEVICE_TYPE(lpDIDevCaps->dwDevType);
407 const char *str;
408 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
409 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
410 switch(type)
412 /* Direct X <= 7 definitions */
413 DEBUG_TYPE(DIDEVTYPE_DEVICE);
414 DEBUG_TYPE(DIDEVTYPE_MOUSE);
415 DEBUG_TYPE(DIDEVTYPE_KEYBOARD);
416 DEBUG_TYPE(DIDEVTYPE_JOYSTICK);
417 DEBUG_TYPE(DIDEVTYPE_HID);
418 /* Direct X >= 8 definitions */
419 DEBUG_TYPE(DI8DEVTYPE_DEVICE);
420 DEBUG_TYPE(DI8DEVTYPE_MOUSE);
421 DEBUG_TYPE(DI8DEVTYPE_KEYBOARD);
422 DEBUG_TYPE(DI8DEVTYPE_JOYSTICK);
423 DEBUG_TYPE(DI8DEVTYPE_GAMEPAD);
424 DEBUG_TYPE(DI8DEVTYPE_DRIVING);
425 DEBUG_TYPE(DI8DEVTYPE_FLIGHT);
426 DEBUG_TYPE(DI8DEVTYPE_1STPERSON);
427 DEBUG_TYPE(DI8DEVTYPE_DEVICECTRL);
428 DEBUG_TYPE(DI8DEVTYPE_SCREENPOINTER);
429 DEBUG_TYPE(DI8DEVTYPE_REMOTE);
430 DEBUG_TYPE(DI8DEVTYPE_SUPPLEMENTAL);
431 default: str = "UNKNOWN";
434 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType, str);
435 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
436 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
437 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
438 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
439 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
440 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
441 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
442 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
443 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
446 #undef DEBUG_TYPE
448 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
450 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
451 int size;
453 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
455 if (lpDIDevCaps == NULL) {
456 WARN("invalid pointer\n");
457 return E_POINTER;
460 size = lpDIDevCaps->dwSize;
462 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
463 WARN("invalid parameter\n");
464 return DIERR_INVALIDPARAM;
467 CopyMemory(lpDIDevCaps, &This->devcaps, size);
468 lpDIDevCaps->dwSize = size;
470 if (TRACE_ON(dinput))
471 _dump_DIDEVCAPS(lpDIDevCaps);
473 return DI_OK;
476 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
478 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
479 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
482 /******************************************************************************
483 * GetObjectInfo : get object info
485 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
486 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
488 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
489 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
490 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
491 HRESULT res;
493 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
494 if (res != DI_OK) return res;
496 if (pdidoi->dwType & DIDFT_AXIS)
497 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
498 else if (pdidoi->dwType & DIDFT_POV)
499 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
500 else if (pdidoi->dwType & DIDFT_BUTTON)
501 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
503 _dump_OBJECTINSTANCEW(pdidoi);
504 return res;
507 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
508 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
510 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
511 HRESULT res;
512 DIDEVICEOBJECTINSTANCEW didoiW;
513 DWORD dwSize = pdidoi->dwSize;
515 didoiW.dwSize = sizeof(didoiW);
516 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
517 if (res != DI_OK) return res;
519 memset(pdidoi, 0, pdidoi->dwSize);
520 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
521 pdidoi->dwSize = dwSize;
522 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
523 sizeof(pdidoi->tszName), NULL, NULL);
525 return res;
528 /******************************************************************************
529 * GetProperty : get input device properties
531 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
533 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
535 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
537 if (TRACE_ON(dinput))
538 _dump_DIPROPHEADER(pdiph);
540 if (IS_DIPROP(rguid)) {
541 switch (LOWORD(rguid)) {
542 case (DWORD_PTR) DIPROP_RANGE: {
543 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
544 int obj = find_property(&This->base.data_format, pdiph);
546 /* The app is querying the current range of the axis
547 * return the lMin and lMax values */
548 if (obj >= 0) {
549 pr->lMin = This->props[obj].lMin;
550 pr->lMax = This->props[obj].lMax;
551 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
552 return DI_OK;
554 break;
556 case (DWORD_PTR) DIPROP_DEADZONE: {
557 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
558 int obj = find_property(&This->base.data_format, pdiph);
560 if (obj >= 0) {
561 pd->dwData = This->props[obj].lDeadZone;
562 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
563 return DI_OK;
565 break;
567 case (DWORD_PTR) DIPROP_SATURATION: {
568 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
569 int obj = find_property(&This->base.data_format, pdiph);
571 if (obj >= 0) {
572 pd->dwData = This->props[obj].lSaturation;
573 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
574 return DI_OK;
576 break;
578 case (DWORD_PTR) DIPROP_INSTANCENAME: {
579 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
580 DIDEVICEINSTANCEW didev;
582 didev.dwSize = sizeof(didev);
584 IDirectInputDevice_GetDeviceInfo(iface, &didev);
585 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
587 return DI_OK;
589 default:
590 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
594 return DI_OK;
597 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
599 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
600 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
603 /******************************************************************************
604 * GetDeviceInfo : get information about a device's identity
606 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
607 LPDIRECTINPUTDEVICE8A iface,
608 LPDIDEVICEINSTANCEA pdidi)
610 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
612 TRACE("(%p,%p)\n", iface, pdidi);
614 if (pdidi == NULL) {
615 WARN("invalid pointer\n");
616 return E_POINTER;
619 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
620 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
621 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
622 return DIERR_INVALIDPARAM;
625 /* Return joystick */
626 pdidi->guidInstance = This->guidInstance;
627 pdidi->guidProduct = This->guidProduct;
628 /* we only support traditional joysticks for now */
629 pdidi->dwDevType = This->devcaps.dwDevType;
630 strcpy(pdidi->tszInstanceName, "Joystick");
631 strcpy(pdidi->tszProductName, This->name);
632 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
633 pdidi->guidFFDriver = GUID_NULL;
634 pdidi->wUsagePage = 0;
635 pdidi->wUsage = 0;
638 return DI_OK;
641 /******************************************************************************
642 * GetDeviceInfo : get information about a device's identity
644 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
645 LPDIRECTINPUTDEVICE8W iface,
646 LPDIDEVICEINSTANCEW pdidi)
648 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
650 TRACE("(%p,%p)\n", iface, pdidi);
652 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
653 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
654 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
655 return DIERR_INVALIDPARAM;
658 /* Return joystick */
659 pdidi->guidInstance = This->guidInstance;
660 pdidi->guidProduct = This->guidProduct;
661 /* we only support traditional joysticks for now */
662 pdidi->dwDevType = This->devcaps.dwDevType;
663 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
664 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
665 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
666 pdidi->guidFFDriver = GUID_NULL;
667 pdidi->wUsagePage = 0;
668 pdidi->wUsage = 0;
671 return DI_OK;
674 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
676 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
678 TRACE("(%p)\n",This);
680 if (!This->base.acquired) {
681 WARN("not acquired\n");
682 return DIERR_NOTACQUIRED;
685 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
686 return DI_OK;
689 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
691 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
692 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
695 /******************************************************************************
696 * GetDeviceState : returns the "state" of the joystick.
699 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
701 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
703 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
705 if (!This->base.acquired) {
706 WARN("not acquired\n");
707 return DIERR_NOTACQUIRED;
710 /* update joystick state */
711 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
713 /* convert and copy data to user supplied buffer */
714 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
716 return DI_OK;
719 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
721 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
722 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
726 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
727 LPDIACTIONFORMATW lpdiaf,
728 LPCWSTR lpszUserName,
729 DWORD dwFlags)
731 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
732 unsigned int i, j;
733 BOOL has_actions = FALSE;
734 DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
735 DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
737 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
739 for (i=0; i < lpdiaf->dwNumActions; i++)
741 DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
742 DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
743 DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
745 /* Don't touch a user configured action */
746 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
748 /* Only consider actions of the right genre */
749 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
751 for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
753 if (type & object_types[j])
755 /* Ensure that the object exists */
756 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
758 if (odf != NULL)
760 lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
761 lpdiaf->rgoAction[i].guidInstance = This->base.guid;
762 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
764 has_actions = TRUE;
766 /* No need to try other types if the action was already mapped */
767 break;
773 if (!has_actions) return DI_NOEFFECT;
775 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
778 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
779 LPDIACTIONFORMATA lpdiaf,
780 LPCSTR lpszUserName,
781 DWORD dwFlags)
783 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
784 DIACTIONFORMATW diafW;
785 HRESULT hr;
786 WCHAR *lpszUserNameW = NULL;
787 int username_size;
789 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
790 _copy_diactionformatAtoW(&diafW, lpdiaf);
792 if (lpszUserName != NULL)
794 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
795 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
796 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
799 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
801 _copy_diactionformatWtoA(lpdiaf, &diafW);
802 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
803 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
805 return hr;
808 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
809 LPDIACTIONFORMATW lpdiaf,
810 LPCWSTR lpszUserName,
811 DWORD dwFlags)
813 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
815 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
817 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
820 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
821 LPDIACTIONFORMATA lpdiaf,
822 LPCSTR lpszUserName,
823 DWORD dwFlags)
825 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
826 DIACTIONFORMATW diafW;
827 HRESULT hr;
828 WCHAR *lpszUserNameW = NULL;
829 int username_size;
831 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
832 _copy_diactionformatAtoW(&diafW, lpdiaf);
834 if (lpszUserName != NULL)
836 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
837 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
838 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
841 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
843 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
844 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
846 return hr;
850 * This maps the read value (from the input event) to a value in the
851 * 'wanted' range.
852 * Notes:
853 * Dead zone is in % multiplied by a 100 (range 0..10000)
855 LONG joystick_map_axis(ObjProps *props, int val)
857 LONG ret;
858 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
859 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
861 /* Center input */
862 val -= (props->lDevMin + props->lDevMax) / 2;
864 /* Remove dead zone */
865 if (abs( val ) <= dead_zone / 2)
866 val = 0;
867 else
868 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
870 /* Scale and map the value from the device range into the required range */
871 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
872 (props->lMin + props->lMax) / 2;
874 /* Clamp in case or rounding errors */
875 if (ret > props->lMax) ret = props->lMax;
876 else if (ret < props->lMin) ret = props->lMin;
878 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
879 props->lDevMin, dead_zone, props->lDevMax,
880 props->lMin, props->lDeadZone, props->lMax,
881 val, ret );
883 return ret;
887 * Maps POV x & y event values to a DX "clock" position:
889 * 31500 4500
890 * 27000 -1 9000
891 * 22500 13500
892 * 18000
894 DWORD joystick_map_pov(const POINTL *p)
896 if (p->x > 0)
897 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
898 else if (p->x < 0)
899 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
900 else
901 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
905 * Setup the dinput options.
908 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
910 char buffer[MAX_PATH+16];
911 HKEY hkey, appkey;
912 int tokens = 0;
913 int axis = 0;
914 int pov = 0;
916 get_app_key(&hkey, &appkey);
918 /* get options */
920 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
922 This->deadzone = atoi(buffer);
923 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
926 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
927 if (!This->axis_map) return DIERR_OUTOFMEMORY;
929 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
931 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
932 "Slider1", "Slider2",
933 "POV1", "POV2", "POV3", "POV4"};
934 const char *delim = ",";
935 char * ptr;
936 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
938 if ((ptr = strtok(buffer, delim)) != NULL)
942 int i;
944 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
946 if (!strcmp(ptr, axis_names[i]))
948 if (!strncmp(ptr, "POV", 3))
950 if (pov >= 4)
952 WARN("Only 4 POVs supported - ignoring extra\n");
953 i = -1;
955 else
957 /* Pov takes two axes */
958 This->axis_map[tokens++] = i;
959 pov++;
962 else
964 if (axis >= 8)
966 FIXME("Only 8 Axes supported - ignoring extra\n");
967 i = -1;
969 else
970 axis++;
972 break;
976 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
978 ERR("invalid joystick axis type: \"%s\"\n", ptr);
979 i = -1;
982 This->axis_map[tokens] = i;
983 tokens++;
984 } while ((ptr = strtok(NULL, delim)) != NULL);
986 if (tokens != This->device_axis_count)
988 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
989 This->device_axis_count, axis, pov, tokens);
990 while (tokens < This->device_axis_count)
992 This->axis_map[tokens] = -1;
993 tokens++;
998 else
1000 int i;
1002 if (default_axis_map)
1004 /* Use default mapping from the driver */
1005 for (i = 0; i < This->device_axis_count; i++)
1007 This->axis_map[i] = default_axis_map[i];
1008 tokens = default_axis_map[i];
1009 if (tokens < 0)
1010 continue;
1011 if (tokens < 8)
1012 axis++;
1013 else if (tokens < 15)
1015 i++;
1016 pov++;
1017 This->axis_map[i] = default_axis_map[i];
1021 else
1023 /* No config - set default mapping. */
1024 for (i = 0; i < This->device_axis_count; i++)
1026 if (i < 8)
1027 This->axis_map[i] = axis++;
1028 else if (i < 15)
1030 This->axis_map[i++] = 8 + pov;
1031 This->axis_map[i ] = 8 + pov++;
1033 else
1034 This->axis_map[i] = -1;
1038 This->devcaps.dwAxes = axis;
1039 This->devcaps.dwPOVs = pov;
1041 if (appkey) RegCloseKey(appkey);
1042 if (hkey) RegCloseKey(hkey);
1044 return DI_OK;