mscoree: Implement IMetahostPolicy GetRequestedRuntime.
[wine/wine-gecko.git] / dlls / dinput / joystick.c
blob4eddd892b06bf2655f0f3fef8e589576e40162a9
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_DEVICE ? "DIDEVTYPE_DEVICE" :
410 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
411 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
412 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
413 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
414 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
415 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
416 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
417 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
418 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
419 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
420 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
421 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
422 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
426 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
428 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
429 int size;
431 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
433 if (lpDIDevCaps == NULL) {
434 WARN("invalid pointer\n");
435 return E_POINTER;
438 size = lpDIDevCaps->dwSize;
440 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
441 WARN("invalid parameter\n");
442 return DIERR_INVALIDPARAM;
445 CopyMemory(lpDIDevCaps, &This->devcaps, size);
446 lpDIDevCaps->dwSize = size;
448 if (TRACE_ON(dinput))
449 _dump_DIDEVCAPS(lpDIDevCaps);
451 return DI_OK;
454 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
456 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
457 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
460 /******************************************************************************
461 * GetObjectInfo : get object info
463 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
464 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
466 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
467 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
468 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
469 HRESULT res;
471 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
472 if (res != DI_OK) return res;
474 if (pdidoi->dwType & DIDFT_AXIS)
475 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
476 else if (pdidoi->dwType & DIDFT_POV)
477 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
478 else if (pdidoi->dwType & DIDFT_BUTTON)
479 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
481 _dump_OBJECTINSTANCEW(pdidoi);
482 return res;
485 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
486 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
488 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
489 HRESULT res;
490 DIDEVICEOBJECTINSTANCEW didoiW;
491 DWORD dwSize = pdidoi->dwSize;
493 didoiW.dwSize = sizeof(didoiW);
494 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
495 if (res != DI_OK) return res;
497 memset(pdidoi, 0, pdidoi->dwSize);
498 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
499 pdidoi->dwSize = dwSize;
500 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
501 sizeof(pdidoi->tszName), NULL, NULL);
503 return res;
506 /******************************************************************************
507 * GetProperty : get input device properties
509 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
511 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
513 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
515 if (TRACE_ON(dinput))
516 _dump_DIPROPHEADER(pdiph);
518 if (IS_DIPROP(rguid)) {
519 switch (LOWORD(rguid)) {
520 case (DWORD_PTR) DIPROP_RANGE: {
521 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
522 int obj = find_property(&This->base.data_format, pdiph);
524 /* The app is querying the current range of the axis
525 * return the lMin and lMax values */
526 if (obj >= 0) {
527 pr->lMin = This->props[obj].lMin;
528 pr->lMax = This->props[obj].lMax;
529 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
530 return DI_OK;
532 break;
534 case (DWORD_PTR) DIPROP_DEADZONE: {
535 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
536 int obj = find_property(&This->base.data_format, pdiph);
538 if (obj >= 0) {
539 pd->dwData = This->props[obj].lDeadZone;
540 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
541 return DI_OK;
543 break;
545 case (DWORD_PTR) DIPROP_SATURATION: {
546 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
547 int obj = find_property(&This->base.data_format, pdiph);
549 if (obj >= 0) {
550 pd->dwData = This->props[obj].lSaturation;
551 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
552 return DI_OK;
554 break;
556 case (DWORD_PTR) DIPROP_INSTANCENAME: {
557 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
558 DIDEVICEINSTANCEW didev;
560 didev.dwSize = sizeof(didev);
562 IDirectInputDevice_GetDeviceInfo(iface, &didev);
563 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
565 return DI_OK;
567 default:
568 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
572 return DI_OK;
575 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
577 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
578 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
581 /******************************************************************************
582 * GetDeviceInfo : get information about a device's identity
584 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
585 LPDIRECTINPUTDEVICE8A iface,
586 LPDIDEVICEINSTANCEA pdidi)
588 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
590 TRACE("(%p,%p)\n", iface, pdidi);
592 if (pdidi == NULL) {
593 WARN("invalid pointer\n");
594 return E_POINTER;
597 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
598 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
599 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
600 return DIERR_INVALIDPARAM;
603 /* Return joystick */
604 pdidi->guidInstance = This->guidInstance;
605 pdidi->guidProduct = This->guidProduct;
606 /* we only support traditional joysticks for now */
607 pdidi->dwDevType = This->devcaps.dwDevType;
608 strcpy(pdidi->tszInstanceName, "Joystick");
609 strcpy(pdidi->tszProductName, This->name);
610 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
611 pdidi->guidFFDriver = GUID_NULL;
612 pdidi->wUsagePage = 0;
613 pdidi->wUsage = 0;
616 return DI_OK;
619 /******************************************************************************
620 * GetDeviceInfo : get information about a device's identity
622 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
623 LPDIRECTINPUTDEVICE8W iface,
624 LPDIDEVICEINSTANCEW pdidi)
626 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
628 TRACE("(%p,%p)\n", iface, pdidi);
630 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
631 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
632 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
633 return DIERR_INVALIDPARAM;
636 /* Return joystick */
637 pdidi->guidInstance = This->guidInstance;
638 pdidi->guidProduct = This->guidProduct;
639 /* we only support traditional joysticks for now */
640 pdidi->dwDevType = This->devcaps.dwDevType;
641 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
642 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
643 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
644 pdidi->guidFFDriver = GUID_NULL;
645 pdidi->wUsagePage = 0;
646 pdidi->wUsage = 0;
649 return DI_OK;
652 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
654 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
656 TRACE("(%p)\n",This);
658 if (!This->base.acquired) {
659 WARN("not acquired\n");
660 return DIERR_NOTACQUIRED;
663 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
664 return DI_OK;
667 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
669 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
670 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
673 /******************************************************************************
674 * GetDeviceState : returns the "state" of the joystick.
677 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
679 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
681 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
683 if (!This->base.acquired) {
684 WARN("not acquired\n");
685 return DIERR_NOTACQUIRED;
688 /* update joystick state */
689 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
691 /* convert and copy data to user supplied buffer */
692 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
694 return DI_OK;
697 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
699 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
700 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
704 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
705 LPDIACTIONFORMATW lpdiaf,
706 LPCWSTR lpszUserName,
707 DWORD dwFlags)
709 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
710 unsigned int i, j;
711 BOOL has_actions = FALSE;
712 DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
713 DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
715 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
717 for (i=0; i < lpdiaf->dwNumActions; i++)
719 DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
720 DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
721 DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
723 /* Don't touch an user configured action */
724 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
726 /* Only consider actions of the right genre */
727 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
729 for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
731 if (type & object_types[j])
733 /* Assure that the object exists */
734 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
736 if (odf != NULL)
738 lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
739 lpdiaf->rgoAction[i].guidInstance = This->base.guid;
740 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
742 has_actions = TRUE;
744 /* No need to try other types if the action was already mapped */
745 break;
751 if (!has_actions) return DI_NOEFFECT;
753 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
756 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
757 LPDIACTIONFORMATA lpdiaf,
758 LPCSTR lpszUserName,
759 DWORD dwFlags)
761 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
762 DIACTIONFORMATW diafW;
763 HRESULT hr;
764 WCHAR *lpszUserNameW = NULL;
765 int username_size;
767 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
768 _copy_diactionformatAtoW(&diafW, lpdiaf);
770 if (lpszUserName != NULL)
772 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
773 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
774 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
777 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
779 _copy_diactionformatWtoA(lpdiaf, &diafW);
780 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
781 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
783 return hr;
786 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
787 LPDIACTIONFORMATW lpdiaf,
788 LPCWSTR lpszUserName,
789 DWORD dwFlags)
791 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
793 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
795 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
798 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
799 LPDIACTIONFORMATA lpdiaf,
800 LPCSTR lpszUserName,
801 DWORD dwFlags)
803 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
804 DIACTIONFORMATW diafW;
805 HRESULT hr;
806 WCHAR *lpszUserNameW = NULL;
807 int username_size;
809 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
810 _copy_diactionformatAtoW(&diafW, lpdiaf);
812 if (lpszUserName != NULL)
814 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
815 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
816 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
819 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
821 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
822 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
824 return hr;
828 * This maps the read value (from the input event) to a value in the
829 * 'wanted' range.
830 * Notes:
831 * Dead zone is in % multiplied by a 100 (range 0..10000)
833 LONG joystick_map_axis(ObjProps *props, int val)
835 LONG ret;
836 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
837 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
839 /* Center input */
840 val -= (props->lDevMin + props->lDevMax) / 2;
842 /* Remove dead zone */
843 if (abs( val ) <= dead_zone / 2)
844 val = 0;
845 else
846 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
848 /* Scale and map the value from the device range into the required range */
849 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
850 (props->lMin + props->lMax) / 2;
852 /* Clamp in case or rounding errors */
853 if (ret > props->lMax) ret = props->lMax;
854 else if (ret < props->lMin) ret = props->lMin;
856 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
857 props->lDevMin, dead_zone, props->lDevMax,
858 props->lMin, props->lDeadZone, props->lMax,
859 val, ret );
861 return ret;
865 * Maps POV x & y event values to a DX "clock" position:
867 * 31500 4500
868 * 27000 -1 9000
869 * 22500 13500
870 * 18000
872 DWORD joystick_map_pov(const POINTL *p)
874 if (p->x > 0)
875 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
876 else if (p->x < 0)
877 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
878 else
879 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
883 * Setup the dinput options.
886 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
888 char buffer[MAX_PATH+16];
889 HKEY hkey, appkey;
890 int tokens = 0;
891 int axis = 0;
892 int pov = 0;
894 get_app_key(&hkey, &appkey);
896 /* get options */
898 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
900 This->deadzone = atoi(buffer);
901 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
904 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
905 if (!This->axis_map) return DIERR_OUTOFMEMORY;
907 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
909 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
910 "Slider1", "Slider2",
911 "POV1", "POV2", "POV3", "POV4"};
912 const char *delim = ",";
913 char * ptr;
914 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
916 if ((ptr = strtok(buffer, delim)) != NULL)
920 int i;
922 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
924 if (!strcmp(ptr, axis_names[i]))
926 if (!strncmp(ptr, "POV", 3))
928 if (pov >= 4)
930 WARN("Only 4 POVs supported - ignoring extra\n");
931 i = -1;
933 else
935 /* Pov takes two axes */
936 This->axis_map[tokens++] = i;
937 pov++;
940 else
942 if (axis >= 8)
944 FIXME("Only 8 Axes supported - ignoring extra\n");
945 i = -1;
947 else
948 axis++;
950 break;
954 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
956 ERR("invalid joystick axis type: \"%s\"\n", ptr);
957 i = -1;
960 This->axis_map[tokens] = i;
961 tokens++;
962 } while ((ptr = strtok(NULL, delim)) != NULL);
964 if (tokens != This->device_axis_count)
966 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
967 This->device_axis_count, axis, pov, tokens);
968 while (tokens < This->device_axis_count)
970 This->axis_map[tokens] = -1;
971 tokens++;
976 else
978 int i;
980 if (default_axis_map)
982 /* Use default mapping from the driver */
983 for (i = 0; i < This->device_axis_count; i++)
985 This->axis_map[i] = default_axis_map[i];
986 tokens = default_axis_map[i];
987 if (tokens < 0)
988 continue;
989 if (tokens < 8)
990 axis++;
991 else if (tokens < 15)
993 i++;
994 pov++;
995 This->axis_map[i] = default_axis_map[i];
999 else
1001 /* No config - set default mapping. */
1002 for (i = 0; i < This->device_axis_count; i++)
1004 if (i < 8)
1005 This->axis_map[i] = axis++;
1006 else if (i < 15)
1008 This->axis_map[i++] = 8 + pov;
1009 This->axis_map[i ] = 8 + pov++;
1011 else
1012 This->axis_map[i] = -1;
1016 This->devcaps.dwAxes = axis;
1017 This->devcaps.dwPOVs = pov;
1019 if (appkey) RegCloseKey(appkey);
1020 if (hkey) RegCloseKey(hkey);
1022 return DI_OK;