reg: Do not allow combinations of /v, /ve or /va in the 'delete' function.
[wine.git] / dlls / dinput / joystick.c
blob9425c0bff664b867291ec3413ef6ec56c9f4db3d
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 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
39 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface), JoystickGenericImpl, base);
41 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
43 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface), JoystickGenericImpl, base);
45 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickGenericImpl *This)
47 return &This->base.IDirectInputDevice8A_iface;
49 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickGenericImpl *This)
51 return &This->base.IDirectInputDevice8W_iface;
54 DWORD typeFromGUID(REFGUID guid)
56 if (IsEqualGUID(guid, &GUID_ConstantForce)) {
57 return DIEFT_CONSTANTFORCE;
58 } else if (IsEqualGUID(guid, &GUID_Square)
59 || IsEqualGUID(guid, &GUID_Sine)
60 || IsEqualGUID(guid, &GUID_Triangle)
61 || IsEqualGUID(guid, &GUID_SawtoothUp)
62 || IsEqualGUID(guid, &GUID_SawtoothDown)) {
63 return DIEFT_PERIODIC;
64 } else if (IsEqualGUID(guid, &GUID_RampForce)) {
65 return DIEFT_RAMPFORCE;
66 } else if (IsEqualGUID(guid, &GUID_Spring)
67 || IsEqualGUID(guid, &GUID_Damper)
68 || IsEqualGUID(guid, &GUID_Inertia)
69 || IsEqualGUID(guid, &GUID_Friction)) {
70 return DIEFT_CONDITION;
71 } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
72 return DIEFT_CUSTOMFORCE;
73 } else {
74 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
75 return 0;
79 static void _dump_DIEFFECT_flags(DWORD dwFlags)
81 if (TRACE_ON(dinput)) {
82 unsigned int i;
83 static const struct {
84 DWORD mask;
85 const char *name;
86 } flags[] = {
87 #define FE(x) { x, #x}
88 FE(DIEFF_CARTESIAN),
89 FE(DIEFF_OBJECTIDS),
90 FE(DIEFF_OBJECTOFFSETS),
91 FE(DIEFF_POLAR),
92 FE(DIEFF_SPHERICAL)
93 #undef FE
95 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
96 if (flags[i].mask & dwFlags)
97 TRACE("%s ", flags[i].name);
98 TRACE("\n");
102 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
104 if (env->dwSize != sizeof(DIENVELOPE)) {
105 WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
107 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
108 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
111 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
113 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
116 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
118 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
119 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
122 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
124 TRACE("Ramp force has start %d, end %d\n",
125 frc->lStart, frc->lEnd);
128 static void _dump_DICONDITION(LPCDICONDITION frc)
130 TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
131 frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
132 frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
135 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
137 unsigned int i;
138 TRACE("Custom force uses %d channels, sample period %d. Has %d samples at %p.\n",
139 frc->cChannels, frc->dwSamplePeriod, frc->cSamples, frc->rglForceData);
140 if (frc->cSamples % frc->cChannels != 0)
141 WARN("Custom force has a non-integral samples-per-channel count!\n");
142 if (TRACE_ON(dinput)) {
143 TRACE("Custom force data (time aligned, axes in order):\n");
144 for (i = 1; i <= frc->cSamples; ++i) {
145 TRACE("%d ", frc->rglForceData[i]);
146 if (i % frc->cChannels == 0)
147 TRACE("\n");
152 void dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid, DWORD dwFlags)
154 DWORD type = typeFromGUID(guid);
155 unsigned int i;
157 TRACE("Dumping DIEFFECT structure:\n");
158 TRACE(" - dwSize: %d\n", eff->dwSize);
159 if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
160 WARN("Non-standard DIEFFECT structure size %d\n", eff->dwSize);
162 TRACE(" - dwFlags: %d\n", eff->dwFlags);
163 TRACE(" ");
164 _dump_DIEFFECT_flags(eff->dwFlags);
165 TRACE(" - dwDuration: %d\n", eff->dwDuration);
166 TRACE(" - dwGain: %d\n", eff->dwGain);
168 if (eff->dwGain > 10000)
169 WARN("dwGain is out of range (>10,000)\n");
171 TRACE(" - dwTriggerButton: %d\n", eff->dwTriggerButton);
172 TRACE(" - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
173 TRACE(" - rglDirection: %p\n", eff->rglDirection);
174 TRACE(" - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
175 TRACE(" - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
177 /* Only trace some members if dwFlags indicates they have data */
178 if (dwFlags & DIEP_AXES) {
179 TRACE(" - cAxes: %d\n", eff->cAxes);
180 TRACE(" - rgdwAxes: %p\n", eff->rgdwAxes);
182 if (TRACE_ON(dinput) && eff->rgdwAxes) {
183 TRACE(" ");
184 for (i = 0; i < eff->cAxes; ++i)
185 TRACE("%d ", eff->rgdwAxes[i]);
186 TRACE("\n");
190 if (dwFlags & DIEP_ENVELOPE) {
191 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
192 if (eff->lpEnvelope != NULL)
193 _dump_DIENVELOPE(eff->lpEnvelope);
196 if (eff->dwSize > sizeof(DIEFFECT_DX5))
197 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
199 if (type == DIEFT_CONSTANTFORCE) {
200 if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
201 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
202 } else {
203 _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
205 } else if (type == DIEFT_PERIODIC) {
206 if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
207 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
208 } else {
209 _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
211 } else if (type == DIEFT_RAMPFORCE) {
212 if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
213 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
214 } else {
215 _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
217 } else if (type == DIEFT_CONDITION) {
218 if (eff->cbTypeSpecificParams != sizeof(DICONDITION)) {
219 WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
220 } else {
221 _dump_DICONDITION(eff->lpvTypeSpecificParams);
223 } else if (type == DIEFT_CUSTOMFORCE) {
224 if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
225 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
226 } else {
227 _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
232 BOOL device_disabled_registry(const char* name)
234 static const char disabled_str[] = "disabled";
235 static const char joystick_key[] = "Joysticks";
236 char buffer[MAX_PATH];
237 HKEY hkey, appkey, temp;
238 BOOL do_disable = FALSE;
240 get_app_key(&hkey, &appkey);
242 /* Joystick settings are in the 'joysticks' subkey */
243 if (appkey)
245 if (RegOpenKeyA(appkey, joystick_key, &temp)) temp = 0;
246 RegCloseKey(appkey);
247 appkey = temp;
249 if (hkey)
251 if (RegOpenKeyA(hkey, joystick_key, &temp)) temp = 0;
252 RegCloseKey(hkey);
253 hkey = temp;
256 /* Look for the "controllername"="disabled" key */
257 if (!get_config_key(hkey, appkey, name, buffer, sizeof(buffer)))
258 if (!strcmp(disabled_str, buffer))
260 TRACE("Disabling joystick '%s' based on registry key.\n", name);
261 do_disable = TRUE;
264 if (appkey) RegCloseKey(appkey);
265 if (hkey) RegCloseKey(hkey);
267 return do_disable;
270 /******************************************************************************
271 * SetProperty : change input device properties
273 HRESULT WINAPI JoystickWGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
275 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
276 DWORD i;
277 ObjProps remap_props;
279 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
281 if (ph == NULL) {
282 WARN("invalid parameter: ph == NULL\n");
283 return DIERR_INVALIDPARAM;
286 if (TRACE_ON(dinput))
287 _dump_DIPROPHEADER(ph);
289 if (IS_DIPROP(rguid)) {
290 switch (LOWORD(rguid)) {
291 case (DWORD_PTR)DIPROP_RANGE: {
292 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
293 if (ph->dwHow == DIPH_DEVICE) {
294 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
295 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
297 remap_props.lDevMin = This->props[i].lMin;
298 remap_props.lDevMax = This->props[i].lMax;
300 remap_props.lDeadZone = This->props[i].lDeadZone;
301 remap_props.lSaturation = This->props[i].lSaturation;
303 remap_props.lMin = pr->lMin;
304 remap_props.lMax = pr->lMax;
306 switch (This->base.data_format.wine_df->rgodf[i].dwOfs) {
307 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
308 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
309 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
310 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
311 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
312 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
313 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
314 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
315 default: break;
318 This->props[i].lMin = pr->lMin;
319 This->props[i].lMax = pr->lMax;
321 } else {
322 int obj = find_property(&This->base.data_format, ph);
324 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
325 if (obj >= 0) {
327 /*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*/
329 remap_props.lDevMin = This->props[obj].lMin;
330 remap_props.lDevMax = This->props[obj].lMax;
332 remap_props.lDeadZone = This->props[obj].lDeadZone;
333 remap_props.lSaturation = This->props[obj].lSaturation;
335 remap_props.lMin = pr->lMin;
336 remap_props.lMax = pr->lMax;
338 switch (ph->dwObj) {
339 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
340 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
341 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
342 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
343 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
344 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
345 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
346 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
347 default: break;
350 This->props[obj].lMin = pr->lMin;
351 This->props[obj].lMax = pr->lMax;
352 return DI_OK;
355 break;
357 case (DWORD_PTR)DIPROP_DEADZONE: {
358 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
359 if (ph->dwHow == DIPH_DEVICE) {
360 TRACE("deadzone(%d) all\n", pd->dwData);
361 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
362 This->props[i].lDeadZone = pd->dwData;
363 } else {
364 int obj = find_property(&This->base.data_format, ph);
366 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
367 if (obj >= 0) {
368 This->props[obj].lDeadZone = pd->dwData;
369 return DI_OK;
372 break;
374 case (DWORD_PTR)DIPROP_SATURATION: {
375 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
376 if (ph->dwHow == DIPH_DEVICE) {
377 TRACE("saturation(%d) all\n", pd->dwData);
378 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
379 This->props[i].lSaturation = pd->dwData;
380 } else {
381 int obj = find_property(&This->base.data_format, ph);
383 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
384 if (obj >= 0) {
385 This->props[obj].lSaturation = pd->dwData;
386 return DI_OK;
389 break;
391 default:
392 return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
396 return DI_OK;
399 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
401 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
402 return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
405 #define DEBUG_TYPE(x) case (x): str = #x; break
406 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
408 int type = GET_DIDEVICE_TYPE(lpDIDevCaps->dwDevType);
409 const char *str;
410 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
411 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
412 switch(type)
414 /* Direct X <= 7 definitions */
415 DEBUG_TYPE(DIDEVTYPE_DEVICE);
416 DEBUG_TYPE(DIDEVTYPE_MOUSE);
417 DEBUG_TYPE(DIDEVTYPE_KEYBOARD);
418 DEBUG_TYPE(DIDEVTYPE_JOYSTICK);
419 DEBUG_TYPE(DIDEVTYPE_HID);
420 /* Direct X >= 8 definitions */
421 DEBUG_TYPE(DI8DEVTYPE_DEVICE);
422 DEBUG_TYPE(DI8DEVTYPE_MOUSE);
423 DEBUG_TYPE(DI8DEVTYPE_KEYBOARD);
424 DEBUG_TYPE(DI8DEVTYPE_JOYSTICK);
425 DEBUG_TYPE(DI8DEVTYPE_GAMEPAD);
426 DEBUG_TYPE(DI8DEVTYPE_DRIVING);
427 DEBUG_TYPE(DI8DEVTYPE_FLIGHT);
428 DEBUG_TYPE(DI8DEVTYPE_1STPERSON);
429 DEBUG_TYPE(DI8DEVTYPE_DEVICECTRL);
430 DEBUG_TYPE(DI8DEVTYPE_SCREENPOINTER);
431 DEBUG_TYPE(DI8DEVTYPE_REMOTE);
432 DEBUG_TYPE(DI8DEVTYPE_SUPPLEMENTAL);
433 default: str = "UNKNOWN";
436 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType, str);
437 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
438 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
439 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
440 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
441 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
442 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
443 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
444 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
445 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
448 #undef DEBUG_TYPE
450 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
452 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
453 int size;
455 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
457 if (lpDIDevCaps == NULL) {
458 WARN("invalid pointer\n");
459 return E_POINTER;
462 size = lpDIDevCaps->dwSize;
464 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
465 WARN("invalid parameter\n");
466 return DIERR_INVALIDPARAM;
469 CopyMemory(lpDIDevCaps, &This->devcaps, size);
470 lpDIDevCaps->dwSize = size;
472 if (TRACE_ON(dinput))
473 _dump_DIDEVCAPS(lpDIDevCaps);
475 return DI_OK;
478 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
480 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
481 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
484 /******************************************************************************
485 * GetObjectInfo : get object info
487 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
488 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
490 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
491 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
492 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
493 HRESULT res;
495 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
496 if (res != DI_OK) return res;
498 if (pdidoi->dwType & DIDFT_AXIS)
499 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
500 else if (pdidoi->dwType & DIDFT_POV)
501 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
502 else if (pdidoi->dwType & DIDFT_BUTTON)
503 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
505 _dump_OBJECTINSTANCEW(pdidoi);
506 return res;
509 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
510 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
512 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
513 HRESULT res;
514 DIDEVICEOBJECTINSTANCEW didoiW;
515 DWORD dwSize = pdidoi->dwSize;
517 didoiW.dwSize = sizeof(didoiW);
518 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
519 if (res != DI_OK) return res;
521 memset(pdidoi, 0, pdidoi->dwSize);
522 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
523 pdidoi->dwSize = dwSize;
524 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
525 sizeof(pdidoi->tszName), NULL, NULL);
527 return res;
530 /******************************************************************************
531 * GetProperty : get input device properties
533 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
535 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
537 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
539 if (TRACE_ON(dinput))
540 _dump_DIPROPHEADER(pdiph);
542 if (IS_DIPROP(rguid)) {
543 switch (LOWORD(rguid)) {
544 case (DWORD_PTR) DIPROP_RANGE: {
545 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
546 int obj = find_property(&This->base.data_format, pdiph);
548 /* The app is querying the current range of the axis
549 * return the lMin and lMax values */
550 if (obj >= 0) {
551 pr->lMin = This->props[obj].lMin;
552 pr->lMax = This->props[obj].lMax;
553 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
554 return DI_OK;
556 break;
558 case (DWORD_PTR) DIPROP_DEADZONE: {
559 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
560 int obj = find_property(&This->base.data_format, pdiph);
562 if (obj >= 0) {
563 pd->dwData = This->props[obj].lDeadZone;
564 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
565 return DI_OK;
567 break;
569 case (DWORD_PTR) DIPROP_SATURATION: {
570 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
571 int obj = find_property(&This->base.data_format, pdiph);
573 if (obj >= 0) {
574 pd->dwData = This->props[obj].lSaturation;
575 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
576 return DI_OK;
578 break;
580 case (DWORD_PTR) DIPROP_INSTANCENAME: {
581 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
582 DIDEVICEINSTANCEW didev;
584 didev.dwSize = sizeof(didev);
586 IDirectInputDevice_GetDeviceInfo(iface, &didev);
587 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
589 return DI_OK;
591 default:
592 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
596 return DI_OK;
599 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
601 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
602 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
605 /******************************************************************************
606 * GetDeviceInfo : get information about a device's identity
608 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
609 LPDIRECTINPUTDEVICE8A iface,
610 LPDIDEVICEINSTANCEA pdidi)
612 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
613 DIPROPDWORD pd;
614 DWORD index = 0;
616 TRACE("(%p,%p)\n", iface, pdidi);
618 if (pdidi == NULL) {
619 WARN("invalid pointer\n");
620 return E_POINTER;
623 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
624 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
625 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
626 return DIERR_INVALIDPARAM;
629 /* Try to get joystick index */
630 pd.diph.dwSize = sizeof(pd);
631 pd.diph.dwHeaderSize = sizeof(pd.diph);
632 pd.diph.dwObj = 0;
633 pd.diph.dwHow = DIPH_DEVICE;
634 if (SUCCEEDED(IDirectInputDevice2_GetProperty(iface, DIPROP_JOYSTICKID, &pd.diph)))
635 index = pd.dwData;
637 /* Return joystick */
638 pdidi->guidInstance = This->guidInstance;
639 pdidi->guidProduct = This->guidProduct;
640 /* we only support traditional joysticks for now */
641 pdidi->dwDevType = This->devcaps.dwDevType;
642 snprintf(pdidi->tszInstanceName, MAX_PATH, "Joystick %d", index);
643 strcpy(pdidi->tszProductName, This->name);
644 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
645 pdidi->guidFFDriver = GUID_NULL;
646 pdidi->wUsagePage = 0;
647 pdidi->wUsage = 0;
650 return DI_OK;
653 /******************************************************************************
654 * GetDeviceInfo : get information about a device's identity
656 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
657 LPDIRECTINPUTDEVICE8W iface,
658 LPDIDEVICEINSTANCEW pdidi)
660 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
661 CHAR buffer[MAX_PATH];
662 DIPROPDWORD pd;
663 DWORD index = 0;
665 TRACE("(%p,%p)\n", iface, pdidi);
667 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
668 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
669 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
670 return DIERR_INVALIDPARAM;
673 /* Try to get joystick index */
674 pd.diph.dwSize = sizeof(pd);
675 pd.diph.dwHeaderSize = sizeof(pd.diph);
676 pd.diph.dwObj = 0;
677 pd.diph.dwHow = DIPH_DEVICE;
678 if (SUCCEEDED(IDirectInputDevice2_GetProperty(iface, DIPROP_JOYSTICKID, &pd.diph)))
679 index = pd.dwData;
681 /* Return joystick */
682 pdidi->guidInstance = This->guidInstance;
683 pdidi->guidProduct = This->guidProduct;
684 /* we only support traditional joysticks for now */
685 pdidi->dwDevType = This->devcaps.dwDevType;
686 snprintf(buffer, sizeof(buffer), "Joystick %d", index);
687 MultiByteToWideChar(CP_ACP, 0, buffer, -1, pdidi->tszInstanceName, MAX_PATH);
688 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
689 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
690 pdidi->guidFFDriver = GUID_NULL;
691 pdidi->wUsagePage = 0;
692 pdidi->wUsage = 0;
695 return DI_OK;
698 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
700 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
702 TRACE("(%p)\n",This);
704 if (!This->base.acquired) {
705 WARN("not acquired\n");
706 return DIERR_NOTACQUIRED;
709 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
710 return DI_OK;
713 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
715 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
716 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
719 /******************************************************************************
720 * GetDeviceState : returns the "state" of the joystick.
723 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
725 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
727 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
729 if (!This->base.acquired) {
730 WARN("not acquired\n");
731 return DIERR_NOTACQUIRED;
734 /* update joystick state */
735 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
737 /* convert and copy data to user supplied buffer */
738 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
740 return DI_OK;
743 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
745 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
746 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
750 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
751 LPDIACTIONFORMATW lpdiaf,
752 LPCWSTR lpszUserName,
753 DWORD dwFlags)
755 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
756 unsigned int i, j;
757 BOOL has_actions = FALSE;
758 DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
759 DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
761 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
763 for (i=0; i < lpdiaf->dwNumActions; i++)
765 DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
766 DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
767 DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
769 /* Don't touch a user configured action */
770 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
772 /* Only consider actions of the right genre */
773 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
775 for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
777 if (type & object_types[j])
779 /* Ensure that the object exists */
780 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
782 if (odf != NULL)
784 lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
785 lpdiaf->rgoAction[i].guidInstance = This->base.guid;
786 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
788 has_actions = TRUE;
790 /* No need to try other types if the action was already mapped */
791 break;
797 if (!has_actions) return DI_NOEFFECT;
799 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
802 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
803 LPDIACTIONFORMATA lpdiaf,
804 LPCSTR lpszUserName,
805 DWORD dwFlags)
807 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
808 DIACTIONFORMATW diafW;
809 HRESULT hr;
810 WCHAR *lpszUserNameW = NULL;
811 int username_size;
813 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
814 _copy_diactionformatAtoW(&diafW, lpdiaf);
816 if (lpszUserName != NULL)
818 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
819 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
820 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
823 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
825 _copy_diactionformatWtoA(lpdiaf, &diafW);
826 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
827 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
829 return hr;
832 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
833 LPDIACTIONFORMATW lpdiaf,
834 LPCWSTR lpszUserName,
835 DWORD dwFlags)
837 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
839 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
841 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
844 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
845 LPDIACTIONFORMATA lpdiaf,
846 LPCSTR lpszUserName,
847 DWORD dwFlags)
849 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
850 DIACTIONFORMATW diafW;
851 HRESULT hr;
852 WCHAR *lpszUserNameW = NULL;
853 int username_size;
855 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
856 _copy_diactionformatAtoW(&diafW, lpdiaf);
858 if (lpszUserName != NULL)
860 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
861 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
862 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
865 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
867 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
868 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
870 return hr;
874 * This maps the read value (from the input event) to a value in the
875 * 'wanted' range.
876 * Notes:
877 * Dead zone is in % multiplied by a 100 (range 0..10000)
879 LONG joystick_map_axis(ObjProps *props, int val)
881 LONG ret;
882 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
883 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
885 /* Center input */
886 val -= (props->lDevMin + props->lDevMax) / 2;
888 /* Remove dead zone */
889 if (abs( val ) <= dead_zone / 2)
890 val = 0;
891 else
892 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
894 /* Scale and map the value from the device range into the required range */
895 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
896 (props->lMin + props->lMax) / 2;
898 /* Clamp in case or rounding errors */
899 if (ret > props->lMax) ret = props->lMax;
900 else if (ret < props->lMin) ret = props->lMin;
902 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
903 props->lDevMin, dead_zone, props->lDevMax,
904 props->lMin, props->lDeadZone, props->lMax,
905 val, ret );
907 return ret;
911 * Maps POV x & y event values to a DX "clock" position:
913 * 31500 4500
914 * 27000 -1 9000
915 * 22500 13500
916 * 18000
918 DWORD joystick_map_pov(const POINTL *p)
920 if (p->x > 0)
921 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
922 else if (p->x < 0)
923 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
924 else
925 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
929 * Setup the dinput options.
932 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
934 char buffer[MAX_PATH+16];
935 HKEY hkey, appkey;
936 int tokens = 0;
937 int axis = 0;
938 int pov = 0;
940 get_app_key(&hkey, &appkey);
942 /* get options */
944 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
946 This->deadzone = atoi(buffer);
947 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
950 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
951 if (!This->axis_map) return DIERR_OUTOFMEMORY;
953 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
955 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
956 "Slider1", "Slider2",
957 "POV1", "POV2", "POV3", "POV4"};
958 const char *delim = ",";
959 char * ptr;
960 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
962 if ((ptr = strtok(buffer, delim)) != NULL)
966 int i;
968 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
970 if (!strcmp(ptr, axis_names[i]))
972 if (!strncmp(ptr, "POV", 3))
974 if (pov >= 4)
976 WARN("Only 4 POVs supported - ignoring extra\n");
977 i = -1;
979 else
981 /* Pov takes two axes */
982 This->axis_map[tokens++] = i;
983 pov++;
986 else
988 if (axis >= 8)
990 FIXME("Only 8 Axes supported - ignoring extra\n");
991 i = -1;
993 else
994 axis++;
996 break;
1000 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
1002 ERR("invalid joystick axis type: \"%s\"\n", ptr);
1003 i = -1;
1006 This->axis_map[tokens] = i;
1007 tokens++;
1008 } while ((ptr = strtok(NULL, delim)) != NULL);
1010 if (tokens != This->device_axis_count)
1012 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
1013 This->device_axis_count, axis, pov, tokens);
1014 while (tokens < This->device_axis_count)
1016 This->axis_map[tokens] = -1;
1017 tokens++;
1022 else
1024 int i;
1026 if (default_axis_map)
1028 /* Use default mapping from the driver */
1029 for (i = 0; i < This->device_axis_count; i++)
1031 This->axis_map[i] = default_axis_map[i];
1032 tokens = default_axis_map[i];
1033 if (tokens < 0)
1034 continue;
1035 if (tokens < 8)
1036 axis++;
1037 else if (tokens < 15)
1039 i++;
1040 pov++;
1041 This->axis_map[i] = default_axis_map[i];
1045 else
1047 /* No config - set default mapping. */
1048 for (i = 0; i < This->device_axis_count; i++)
1050 if (i < 8)
1051 This->axis_map[i] = axis++;
1052 else if (i < 15)
1054 This->axis_map[i++] = 8 + pov;
1055 This->axis_map[i ] = 8 + pov++;
1057 else
1058 This->axis_map[i] = -1;
1062 This->devcaps.dwAxes = axis;
1063 This->devcaps.dwPOVs = pov;
1065 if (appkey) RegCloseKey(appkey);
1066 if (hkey) RegCloseKey(hkey);
1068 return DI_OK;