user32/tests: Fix a memory leak (valgrind).
[wine.git] / dlls / dinput / joystick.c
blob0d61f572e671d4654c8a69bf11cf4ec7409163cd
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 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
405 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
406 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
407 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
408 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
409 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
410 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
411 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
412 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
413 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
414 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
415 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
416 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
417 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
418 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
419 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
420 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
421 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
425 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
427 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
428 int size;
430 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
432 if (lpDIDevCaps == NULL) {
433 WARN("invalid pointer\n");
434 return E_POINTER;
437 size = lpDIDevCaps->dwSize;
439 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
440 WARN("invalid parameter\n");
441 return DIERR_INVALIDPARAM;
444 CopyMemory(lpDIDevCaps, &This->devcaps, size);
445 lpDIDevCaps->dwSize = size;
447 if (TRACE_ON(dinput))
448 _dump_DIDEVCAPS(lpDIDevCaps);
450 return DI_OK;
453 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
455 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
456 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
459 /******************************************************************************
460 * GetObjectInfo : get object info
462 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
463 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
465 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
466 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
467 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
468 HRESULT res;
470 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
471 if (res != DI_OK) return res;
473 if (pdidoi->dwType & DIDFT_AXIS)
474 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
475 else if (pdidoi->dwType & DIDFT_POV)
476 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
477 else if (pdidoi->dwType & DIDFT_BUTTON)
478 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
480 _dump_OBJECTINSTANCEW(pdidoi);
481 return res;
484 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
485 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
487 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
488 HRESULT res;
489 DIDEVICEOBJECTINSTANCEW didoiW;
490 DWORD dwSize = pdidoi->dwSize;
492 didoiW.dwSize = sizeof(didoiW);
493 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
494 if (res != DI_OK) return res;
496 memset(pdidoi, 0, pdidoi->dwSize);
497 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
498 pdidoi->dwSize = dwSize;
499 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
500 sizeof(pdidoi->tszName), NULL, NULL);
502 return res;
505 /******************************************************************************
506 * GetProperty : get input device properties
508 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
510 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
512 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
514 if (TRACE_ON(dinput))
515 _dump_DIPROPHEADER(pdiph);
517 if (IS_DIPROP(rguid)) {
518 switch (LOWORD(rguid)) {
519 case (DWORD_PTR) DIPROP_RANGE: {
520 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
521 int obj = find_property(&This->base.data_format, pdiph);
523 /* The app is querying the current range of the axis
524 * return the lMin and lMax values */
525 if (obj >= 0) {
526 pr->lMin = This->props[obj].lMin;
527 pr->lMax = This->props[obj].lMax;
528 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
529 return DI_OK;
531 break;
533 case (DWORD_PTR) DIPROP_DEADZONE: {
534 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
535 int obj = find_property(&This->base.data_format, pdiph);
537 if (obj >= 0) {
538 pd->dwData = This->props[obj].lDeadZone;
539 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
540 return DI_OK;
542 break;
544 case (DWORD_PTR) DIPROP_SATURATION: {
545 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
546 int obj = find_property(&This->base.data_format, pdiph);
548 if (obj >= 0) {
549 pd->dwData = This->props[obj].lSaturation;
550 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
551 return DI_OK;
553 break;
555 case (DWORD_PTR) DIPROP_INSTANCENAME: {
556 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
557 DIDEVICEINSTANCEW didev;
559 didev.dwSize = sizeof(didev);
561 IDirectInputDevice_GetDeviceInfo(iface, &didev);
562 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
564 return DI_OK;
566 default:
567 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
571 return DI_OK;
574 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
576 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
577 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
580 /******************************************************************************
581 * GetDeviceInfo : get information about a device's identity
583 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
584 LPDIRECTINPUTDEVICE8A iface,
585 LPDIDEVICEINSTANCEA pdidi)
587 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
589 TRACE("(%p,%p)\n", iface, pdidi);
591 if (pdidi == NULL) {
592 WARN("invalid pointer\n");
593 return E_POINTER;
596 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
597 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
598 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
599 return DIERR_INVALIDPARAM;
602 /* Return joystick */
603 pdidi->guidInstance = This->guidInstance;
604 pdidi->guidProduct = This->guidProduct;
605 /* we only support traditional joysticks for now */
606 pdidi->dwDevType = This->devcaps.dwDevType;
607 strcpy(pdidi->tszInstanceName, "Joystick");
608 strcpy(pdidi->tszProductName, This->name);
609 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
610 pdidi->guidFFDriver = GUID_NULL;
611 pdidi->wUsagePage = 0;
612 pdidi->wUsage = 0;
615 return DI_OK;
618 /******************************************************************************
619 * GetDeviceInfo : get information about a device's identity
621 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
622 LPDIRECTINPUTDEVICE8W iface,
623 LPDIDEVICEINSTANCEW pdidi)
625 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
627 TRACE("(%p,%p)\n", iface, pdidi);
629 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
630 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
631 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
632 return DIERR_INVALIDPARAM;
635 /* Return joystick */
636 pdidi->guidInstance = This->guidInstance;
637 pdidi->guidProduct = This->guidProduct;
638 /* we only support traditional joysticks for now */
639 pdidi->dwDevType = This->devcaps.dwDevType;
640 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
641 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
642 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
643 pdidi->guidFFDriver = GUID_NULL;
644 pdidi->wUsagePage = 0;
645 pdidi->wUsage = 0;
648 return DI_OK;
651 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
653 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
655 TRACE("(%p)\n",This);
657 if (!This->base.acquired) {
658 WARN("not acquired\n");
659 return DIERR_NOTACQUIRED;
662 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
663 return DI_OK;
666 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
668 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
669 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
672 /******************************************************************************
673 * GetDeviceState : returns the "state" of the joystick.
676 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
678 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
680 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
682 if (!This->base.acquired) {
683 WARN("not acquired\n");
684 return DIERR_NOTACQUIRED;
687 /* update joystick state */
688 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
690 /* convert and copy data to user supplied buffer */
691 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
693 return DI_OK;
696 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
698 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
699 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
703 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
704 LPDIACTIONFORMATW lpdiaf,
705 LPCWSTR lpszUserName,
706 DWORD dwFlags)
708 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
709 unsigned int i, j;
710 BOOL has_actions = FALSE;
711 DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
712 DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
714 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
716 for (i=0; i < lpdiaf->dwNumActions; i++)
718 DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
719 DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
720 DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
722 /* Don't touch an user configured action */
723 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
725 /* Only consider actions of the right genre */
726 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
728 for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
730 if (type & object_types[j])
732 /* Assure that the object exists */
733 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
735 if (odf != NULL)
737 lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
738 lpdiaf->rgoAction[i].guidInstance = This->base.guid;
739 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
741 has_actions = TRUE;
743 /* No need to try other types if the action was already mapped */
744 break;
750 if (!has_actions) return DI_NOEFFECT;
752 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
755 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
756 LPDIACTIONFORMATA lpdiaf,
757 LPCSTR lpszUserName,
758 DWORD dwFlags)
760 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
761 DIACTIONFORMATW diafW;
762 HRESULT hr;
763 WCHAR *lpszUserNameW = NULL;
764 int username_size;
766 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
767 _copy_diactionformatAtoW(&diafW, lpdiaf);
769 if (lpszUserName != NULL)
771 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
772 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
773 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
776 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
778 _copy_diactionformatWtoA(lpdiaf, &diafW);
779 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
780 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
782 return hr;
785 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
786 LPDIACTIONFORMATW lpdiaf,
787 LPCWSTR lpszUserName,
788 DWORD dwFlags)
790 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
792 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
794 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
797 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
798 LPDIACTIONFORMATA lpdiaf,
799 LPCSTR lpszUserName,
800 DWORD dwFlags)
802 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
803 DIACTIONFORMATW diafW;
804 HRESULT hr;
805 WCHAR *lpszUserNameW = NULL;
806 int username_size;
808 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
809 _copy_diactionformatAtoW(&diafW, lpdiaf);
811 if (lpszUserName != NULL)
813 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
814 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
815 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
818 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
820 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
821 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
823 return hr;
827 * This maps the read value (from the input event) to a value in the
828 * 'wanted' range.
829 * Notes:
830 * Dead zone is in % multiplied by a 100 (range 0..10000)
832 LONG joystick_map_axis(ObjProps *props, int val)
834 LONG ret;
835 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
836 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
838 /* Center input */
839 val -= (props->lDevMin + props->lDevMax) / 2;
841 /* Remove dead zone */
842 if (abs( val ) <= dead_zone / 2)
843 val = 0;
844 else
845 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
847 /* Scale and map the value from the device range into the required range */
848 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
849 (props->lMin + props->lMax) / 2;
851 /* Clamp in case or rounding errors */
852 if (ret > props->lMax) ret = props->lMax;
853 else if (ret < props->lMin) ret = props->lMin;
855 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
856 props->lDevMin, dead_zone, props->lDevMax,
857 props->lMin, props->lDeadZone, props->lMax,
858 val, ret );
860 return ret;
864 * Maps POV x & y event values to a DX "clock" position:
866 * 31500 4500
867 * 27000 -1 9000
868 * 22500 13500
869 * 18000
871 DWORD joystick_map_pov(const POINTL *p)
873 if (p->x > 0)
874 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
875 else if (p->x < 0)
876 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
877 else
878 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
882 * Setup the dinput options.
885 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
887 char buffer[MAX_PATH+16];
888 HKEY hkey, appkey;
889 int tokens = 0;
890 int axis = 0;
891 int pov = 0;
893 get_app_key(&hkey, &appkey);
895 /* get options */
897 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
899 This->deadzone = atoi(buffer);
900 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
903 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
904 if (!This->axis_map) return DIERR_OUTOFMEMORY;
906 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
908 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
909 "Slider1", "Slider2",
910 "POV1", "POV2", "POV3", "POV4"};
911 const char *delim = ",";
912 char * ptr;
913 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
915 if ((ptr = strtok(buffer, delim)) != NULL)
919 int i;
921 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
923 if (!strcmp(ptr, axis_names[i]))
925 if (!strncmp(ptr, "POV", 3))
927 if (pov >= 4)
929 WARN("Only 4 POVs supported - ignoring extra\n");
930 i = -1;
932 else
934 /* Pov takes two axes */
935 This->axis_map[tokens++] = i;
936 pov++;
939 else
941 if (axis >= 8)
943 FIXME("Only 8 Axes supported - ignoring extra\n");
944 i = -1;
946 else
947 axis++;
949 break;
953 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
955 ERR("invalid joystick axis type: \"%s\"\n", ptr);
956 i = -1;
959 This->axis_map[tokens] = i;
960 tokens++;
961 } while ((ptr = strtok(NULL, delim)) != NULL);
963 if (tokens != This->device_axis_count)
965 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
966 This->device_axis_count, axis, pov, tokens);
967 while (tokens < This->device_axis_count)
969 This->axis_map[tokens] = -1;
970 tokens++;
975 else
977 int i;
979 if (default_axis_map)
981 /* Use default mapping from the driver */
982 for (i = 0; i < This->device_axis_count; i++)
984 This->axis_map[i] = default_axis_map[i];
985 tokens = default_axis_map[i];
986 if (tokens < 0)
987 continue;
988 if (tokens < 8)
989 axis++;
990 else if (tokens < 15)
992 i++;
993 pov++;
994 This->axis_map[i] = default_axis_map[i];
998 else
1000 /* No config - set default mapping. */
1001 for (i = 0; i < This->device_axis_count; i++)
1003 if (i < 8)
1004 This->axis_map[i] = axis++;
1005 else if (i < 15)
1007 This->axis_map[i++] = 8 + pov;
1008 This->axis_map[i ] = 8 + pov++;
1010 else
1011 This->axis_map[i] = -1;
1015 This->devcaps.dwAxes = axis;
1016 This->devcaps.dwPOVs = pov;
1018 if (appkey) RegCloseKey(appkey);
1019 if (hkey) RegCloseKey(hkey);
1021 return DI_OK;