wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / dinput / joystick.c
blobf9b89d2d70cc2fa8f15844862a4e5f1ea4726f49
1 /* DirectInput Generic Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2009 Aric Stewart, CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * To Do:
25 * dead zone
26 * force feedback
29 #include <stdio.h>
31 #include "joystick_private.h"
32 #include "wine/debug.h"
33 #include "winreg.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
37 #define VID_MICROSOFT 0x045e
39 static const WORD PID_XBOX_CONTROLLERS[] = {
40 0x0202, /* Xbox Controller */
41 0x0285, /* Xbox Controller S */
42 0x0289, /* Xbox Controller S */
43 0x028e, /* Xbox360 Controller */
44 0x028f, /* Xbox360 Wireless Controller */
45 0x02d1, /* Xbox One Controller */
46 0x02dd, /* Xbox One Controller (Covert Forces/Firmware 2015) */
47 0x02e0, /* Xbox One X Controller */
48 0x02e3, /* Xbox One Elite Controller */
49 0x02e6, /* Wireless XBox Controller Dongle */
50 0x02ea, /* Xbox One S Controller */
51 0x02fd, /* Xbox One S Controller (Firmware 2017) */
52 0x0719, /* Xbox 360 Wireless Adapter */
55 /* Windows uses this GUID for guidProduct on non-keyboard/mouse devices.
56 * Data1 contains the device VID (low word) and PID (high word).
57 * Data4 ends with the ASCII bytes "PIDVID".
59 const GUID DInput_PIDVID_Product_GUID = { /* device_pidvid-0000-0000-0000-504944564944 */
60 0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x50, 0x49, 0x44, 0x56, 0x49, 0x44}
63 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
65 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface), JoystickGenericImpl, base);
67 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
69 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface), JoystickGenericImpl, base);
71 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickGenericImpl *This)
73 return &This->base.IDirectInputDevice8A_iface;
75 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickGenericImpl *This)
77 return &This->base.IDirectInputDevice8W_iface;
80 DWORD typeFromGUID(REFGUID guid)
82 if (IsEqualGUID(guid, &GUID_ConstantForce)) {
83 return DIEFT_CONSTANTFORCE;
84 } else if (IsEqualGUID(guid, &GUID_Square)
85 || IsEqualGUID(guid, &GUID_Sine)
86 || IsEqualGUID(guid, &GUID_Triangle)
87 || IsEqualGUID(guid, &GUID_SawtoothUp)
88 || IsEqualGUID(guid, &GUID_SawtoothDown)) {
89 return DIEFT_PERIODIC;
90 } else if (IsEqualGUID(guid, &GUID_RampForce)) {
91 return DIEFT_RAMPFORCE;
92 } else if (IsEqualGUID(guid, &GUID_Spring)
93 || IsEqualGUID(guid, &GUID_Damper)
94 || IsEqualGUID(guid, &GUID_Inertia)
95 || IsEqualGUID(guid, &GUID_Friction)) {
96 return DIEFT_CONDITION;
97 } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
98 return DIEFT_CUSTOMFORCE;
99 } else {
100 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
101 return 0;
105 DWORD get_device_type(DWORD version, BOOL is_joystick)
107 if (is_joystick)
108 return version >= 0x0800 ? DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8) :
109 DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
111 return version >= 0x0800 ? DI8DEVTYPE_GAMEPAD | (DI8DEVTYPEJOYSTICK_STANDARD << 8) :
112 DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_GAMEPAD << 8);
115 static void _dump_DIEFFECT_flags(DWORD dwFlags)
117 if (TRACE_ON(dinput)) {
118 unsigned int i;
119 static const struct {
120 DWORD mask;
121 const char *name;
122 } flags[] = {
123 #define FE(x) { x, #x}
124 FE(DIEFF_CARTESIAN),
125 FE(DIEFF_OBJECTIDS),
126 FE(DIEFF_OBJECTOFFSETS),
127 FE(DIEFF_POLAR),
128 FE(DIEFF_SPHERICAL)
129 #undef FE
131 for (i = 0; i < ARRAY_SIZE(flags); i++)
132 if (flags[i].mask & dwFlags)
133 TRACE("%s ", flags[i].name);
134 TRACE("\n");
138 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
140 if (env->dwSize != sizeof(DIENVELOPE)) {
141 WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
143 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
144 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
147 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
149 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
152 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
154 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
155 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
158 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
160 TRACE("Ramp force has start %d, end %d\n",
161 frc->lStart, frc->lEnd);
164 static void _dump_DICONDITION(LPCDICONDITION frc)
166 TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
167 frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
168 frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
171 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
173 unsigned int i;
174 TRACE("Custom force uses %d channels, sample period %d. Has %d samples at %p.\n",
175 frc->cChannels, frc->dwSamplePeriod, frc->cSamples, frc->rglForceData);
176 if (frc->cSamples % frc->cChannels != 0)
177 WARN("Custom force has a non-integral samples-per-channel count!\n");
178 if (TRACE_ON(dinput)) {
179 TRACE("Custom force data (time aligned, axes in order):\n");
180 for (i = 1; i <= frc->cSamples; ++i) {
181 TRACE("%d ", frc->rglForceData[i]);
182 if (i % frc->cChannels == 0)
183 TRACE("\n");
188 void dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid, DWORD dwFlags)
190 DWORD type = typeFromGUID(guid);
191 unsigned int i;
193 TRACE("Dumping DIEFFECT structure:\n");
194 TRACE(" - dwSize: %d\n", eff->dwSize);
195 if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
196 WARN("Non-standard DIEFFECT structure size %d\n", eff->dwSize);
198 TRACE(" - dwFlags: %d\n", eff->dwFlags);
199 TRACE(" ");
200 _dump_DIEFFECT_flags(eff->dwFlags);
201 TRACE(" - dwDuration: %d\n", eff->dwDuration);
202 TRACE(" - dwGain: %d\n", eff->dwGain);
204 if (eff->dwGain > 10000)
205 WARN("dwGain is out of range (>10,000)\n");
207 TRACE(" - dwTriggerButton: %d\n", eff->dwTriggerButton);
208 TRACE(" - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
209 TRACE(" - rglDirection: %p\n", eff->rglDirection);
210 if (dwFlags & DIEP_DIRECTION && eff->rglDirection) {
211 TRACE(" ");
212 for (i = 0; i < eff->cAxes; ++i)
213 TRACE("%d ", eff->rglDirection[i]);
214 TRACE("\n");
216 TRACE(" - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
217 TRACE(" - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
219 /* Only trace some members if dwFlags indicates they have data */
220 if (dwFlags & DIEP_AXES) {
221 TRACE(" - cAxes: %d\n", eff->cAxes);
222 TRACE(" - rgdwAxes: %p\n", eff->rgdwAxes);
224 if (TRACE_ON(dinput) && eff->rgdwAxes) {
225 TRACE(" ");
226 for (i = 0; i < eff->cAxes; ++i)
227 TRACE("%d ", eff->rgdwAxes[i]);
228 TRACE("\n");
232 if (dwFlags & DIEP_ENVELOPE) {
233 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
234 if (eff->lpEnvelope != NULL)
235 _dump_DIENVELOPE(eff->lpEnvelope);
238 if (eff->dwSize > sizeof(DIEFFECT_DX5))
239 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
241 if (type == DIEFT_CONSTANTFORCE) {
242 if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
243 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
244 } else if (!eff->lpvTypeSpecificParams) {
245 WARN("Size of type-specific params is correct but pointer is NULL!\n");
246 } else {
247 _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
249 } else if (type == DIEFT_PERIODIC) {
250 if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
251 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
252 } else if (!eff->lpvTypeSpecificParams) {
253 WARN("Size of type-specific params is correct but pointer is NULL!\n");
254 } else {
255 _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
257 } else if (type == DIEFT_RAMPFORCE) {
258 if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
259 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
260 } else if (!eff->lpvTypeSpecificParams) {
261 WARN("Size of type-specific params is correct but pointer is NULL!\n");
262 } else {
263 _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
265 } else if (type == DIEFT_CONDITION) {
266 if (eff->cbTypeSpecificParams == sizeof(DICONDITION) && eff->lpvTypeSpecificParams) {
267 _dump_DICONDITION(eff->lpvTypeSpecificParams);
268 } else if (eff->cbTypeSpecificParams == 2 * sizeof(DICONDITION) && eff->lpvTypeSpecificParams) {
269 DICONDITION *condition = eff->lpvTypeSpecificParams;
270 _dump_DICONDITION(&condition[0]);
271 _dump_DICONDITION(&condition[1]);
272 } else {
273 WARN("Effect claims to be a condition but the type-specific params are the wrong size or NULL!\n");
275 } else if (type == DIEFT_CUSTOMFORCE) {
276 if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
277 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
278 } else if (!eff->lpvTypeSpecificParams) {
279 WARN("Size of type-specific params is correct but pointer is NULL!\n");
280 } else {
281 _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
286 BOOL device_disabled_registry(const char* name)
288 static const char disabled_str[] = "disabled";
289 static const char joystick_key[] = "Joysticks";
290 char buffer[MAX_PATH];
291 HKEY hkey, appkey, temp;
292 BOOL do_disable = FALSE;
294 get_app_key(&hkey, &appkey);
296 /* Joystick settings are in the 'joysticks' subkey */
297 if (appkey)
299 if (RegOpenKeyA(appkey, joystick_key, &temp)) temp = 0;
300 RegCloseKey(appkey);
301 appkey = temp;
303 if (hkey)
305 if (RegOpenKeyA(hkey, joystick_key, &temp)) temp = 0;
306 RegCloseKey(hkey);
307 hkey = temp;
310 /* Look for the "controllername"="disabled" key */
311 if (!get_config_key(hkey, appkey, name, buffer, sizeof(buffer)))
312 if (!strcmp(disabled_str, buffer))
314 TRACE("Disabling joystick '%s' based on registry key.\n", name);
315 do_disable = TRUE;
318 if (appkey) RegCloseKey(appkey);
319 if (hkey) RegCloseKey(hkey);
321 return do_disable;
324 BOOL is_xinput_device(const DIDEVCAPS *devcaps, WORD vid, WORD pid)
326 int i;
328 if (vid == VID_MICROSOFT)
330 for (i = 0; i < ARRAY_SIZE(PID_XBOX_CONTROLLERS); i++)
331 if (pid == PID_XBOX_CONTROLLERS[i]) return TRUE;
334 return (devcaps->dwAxes == 6 && devcaps->dwButtons >= 14);
337 /******************************************************************************
338 * SetProperty : change input device properties
340 HRESULT WINAPI JoystickWGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
342 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
343 DWORD i;
344 ObjProps remap_props;
346 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
348 if (ph == NULL) {
349 WARN("invalid parameter: ph == NULL\n");
350 return DIERR_INVALIDPARAM;
353 if (TRACE_ON(dinput))
354 _dump_DIPROPHEADER(ph);
356 if (IS_DIPROP(rguid)) {
357 switch (LOWORD(rguid)) {
358 case (DWORD_PTR)DIPROP_RANGE: {
359 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
360 if (ph->dwHow == DIPH_DEVICE) {
362 /* Many games poll the joystick immediately after setting the range
363 * for calibration purposes, so the old values need to be remapped
364 * to the new range before it does so */
366 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
367 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
369 remap_props.lDevMin = This->props[i].lMin;
370 remap_props.lDevMax = This->props[i].lMax;
372 remap_props.lDeadZone = This->props[i].lDeadZone;
373 remap_props.lSaturation = This->props[i].lSaturation;
375 remap_props.lMin = pr->lMin;
376 remap_props.lMax = pr->lMax;
378 switch (This->base.data_format.wine_df->rgodf[i].dwOfs) {
379 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
380 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
381 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
382 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
383 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
384 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
385 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
386 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
387 default: break;
390 This->props[i].lMin = pr->lMin;
391 This->props[i].lMax = pr->lMax;
393 } else {
394 int obj = find_property(&This->base.data_format, ph);
396 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
397 if (obj >= 0) {
399 remap_props.lDevMin = This->props[obj].lMin;
400 remap_props.lDevMax = This->props[obj].lMax;
402 remap_props.lDeadZone = This->props[obj].lDeadZone;
403 remap_props.lSaturation = This->props[obj].lSaturation;
405 remap_props.lMin = pr->lMin;
406 remap_props.lMax = pr->lMax;
408 switch (This->base.data_format.wine_df->rgodf[obj].dwOfs) {
409 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
410 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
411 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
412 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
413 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
414 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
415 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
416 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
417 default: break;
420 This->props[obj].lMin = pr->lMin;
421 This->props[obj].lMax = pr->lMax;
422 return DI_OK;
425 break;
427 case (DWORD_PTR)DIPROP_DEADZONE: {
428 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
429 if (ph->dwHow == DIPH_DEVICE) {
430 TRACE("deadzone(%d) all\n", pd->dwData);
431 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
432 This->props[i].lDeadZone = pd->dwData;
433 } else {
434 int obj = find_property(&This->base.data_format, ph);
436 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
437 if (obj >= 0) {
438 This->props[obj].lDeadZone = pd->dwData;
439 return DI_OK;
442 break;
444 case (DWORD_PTR)DIPROP_SATURATION: {
445 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
446 if (ph->dwHow == DIPH_DEVICE) {
447 TRACE("saturation(%d) all\n", pd->dwData);
448 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
449 This->props[i].lSaturation = pd->dwData;
450 } else {
451 int obj = find_property(&This->base.data_format, ph);
453 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
454 if (obj >= 0) {
455 This->props[obj].lSaturation = pd->dwData;
456 return DI_OK;
459 break;
461 case (DWORD_PTR)DIPROP_CALIBRATIONMODE: {
462 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
463 FIXME("DIPROP_CALIBRATIONMODE(%d)\n", pd->dwData);
464 break;
466 default:
467 return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
471 return DI_OK;
474 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
476 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
477 return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
480 #define DEBUG_TYPE(x) case (x): str = #x; break
481 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
483 int type = GET_DIDEVICE_TYPE(lpDIDevCaps->dwDevType);
484 const char *str, *hid = "";
485 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
486 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
487 switch(type)
489 /* Direct X <= 7 definitions */
490 DEBUG_TYPE(DIDEVTYPE_DEVICE);
491 DEBUG_TYPE(DIDEVTYPE_MOUSE);
492 DEBUG_TYPE(DIDEVTYPE_KEYBOARD);
493 DEBUG_TYPE(DIDEVTYPE_JOYSTICK);
494 /* Direct X >= 8 definitions */
495 DEBUG_TYPE(DI8DEVTYPE_DEVICE);
496 DEBUG_TYPE(DI8DEVTYPE_MOUSE);
497 DEBUG_TYPE(DI8DEVTYPE_KEYBOARD);
498 DEBUG_TYPE(DI8DEVTYPE_JOYSTICK);
499 DEBUG_TYPE(DI8DEVTYPE_GAMEPAD);
500 DEBUG_TYPE(DI8DEVTYPE_DRIVING);
501 DEBUG_TYPE(DI8DEVTYPE_FLIGHT);
502 DEBUG_TYPE(DI8DEVTYPE_1STPERSON);
503 DEBUG_TYPE(DI8DEVTYPE_DEVICECTRL);
504 DEBUG_TYPE(DI8DEVTYPE_SCREENPOINTER);
505 DEBUG_TYPE(DI8DEVTYPE_REMOTE);
506 DEBUG_TYPE(DI8DEVTYPE_SUPPLEMENTAL);
507 default: str = "UNKNOWN";
510 if (lpDIDevCaps->dwDevType & DIDEVTYPE_HID)
511 hid = " (HID)";
513 TRACE("dwDevType: %08x %s%s\n", lpDIDevCaps->dwDevType, str, hid);
514 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
515 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
516 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
517 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
518 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
519 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
520 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
521 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
522 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
525 #undef DEBUG_TYPE
527 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
529 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
530 int size;
532 TRACE("%p->(%p)\n",This,lpDIDevCaps);
534 if (lpDIDevCaps == NULL) {
535 WARN("invalid pointer\n");
536 return E_POINTER;
539 size = lpDIDevCaps->dwSize;
541 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
542 WARN("invalid parameter\n");
543 return DIERR_INVALIDPARAM;
546 CopyMemory(lpDIDevCaps, &This->devcaps, size);
547 lpDIDevCaps->dwSize = size;
549 if (TRACE_ON(dinput))
550 _dump_DIDEVCAPS(lpDIDevCaps);
552 return DI_OK;
555 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
557 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
558 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
561 /******************************************************************************
562 * GetObjectInfo : get object info
564 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
565 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
567 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
568 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
569 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
570 HRESULT res;
572 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
573 if (res != DI_OK) return res;
575 if (pdidoi->dwType & DIDFT_AXIS) {
576 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
577 pdidoi->dwFlags |= DIDOI_ASPECTPOSITION;
578 } else if (pdidoi->dwType & DIDFT_POV)
579 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
580 else if (pdidoi->dwType & DIDFT_BUTTON)
581 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
583 _dump_OBJECTINSTANCEW(pdidoi);
584 return res;
587 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
588 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
590 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
591 HRESULT res;
592 DIDEVICEOBJECTINSTANCEW didoiW;
593 DWORD dwSize = pdidoi->dwSize;
595 didoiW.dwSize = sizeof(didoiW);
596 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
597 if (res != DI_OK) return res;
599 memset(pdidoi, 0, pdidoi->dwSize);
600 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
601 pdidoi->dwSize = dwSize;
602 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
603 sizeof(pdidoi->tszName), NULL, NULL);
605 return res;
608 /******************************************************************************
609 * GetProperty : get input device properties
611 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
613 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
615 TRACE("(%p,%s,%p)\n", This, debugstr_guid(rguid), pdiph);
617 if (TRACE_ON(dinput))
618 _dump_DIPROPHEADER(pdiph);
620 if (IS_DIPROP(rguid)) {
621 switch (LOWORD(rguid)) {
622 case (DWORD_PTR) DIPROP_RANGE: {
623 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
624 int obj = find_property(&This->base.data_format, pdiph);
626 /* The app is querying the current range of the axis
627 * return the lMin and lMax values */
628 if (obj >= 0) {
629 pr->lMin = This->props[obj].lMin;
630 pr->lMax = This->props[obj].lMax;
631 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
632 return DI_OK;
634 break;
636 case (DWORD_PTR) DIPROP_DEADZONE: {
637 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
638 int obj = find_property(&This->base.data_format, pdiph);
640 if (obj >= 0) {
641 pd->dwData = This->props[obj].lDeadZone;
642 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
643 return DI_OK;
645 break;
647 case (DWORD_PTR) DIPROP_SATURATION: {
648 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
649 int obj = find_property(&This->base.data_format, pdiph);
651 if (obj >= 0) {
652 pd->dwData = This->props[obj].lSaturation;
653 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
654 return DI_OK;
656 break;
658 case (DWORD_PTR) DIPROP_PRODUCTNAME:
659 case (DWORD_PTR) DIPROP_INSTANCENAME: {
660 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
661 DIDEVICEINSTANCEW didev;
663 didev.dwSize = sizeof(didev);
665 IDirectInputDevice_GetDeviceInfo(iface, &didev);
666 if (LOWORD(rguid) == (DWORD_PTR) DIPROP_PRODUCTNAME)
667 lstrcpynW(ps->wsz, didev.tszProductName, MAX_PATH);
668 else
669 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
671 return DI_OK;
673 default:
674 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
678 return DI_OK;
681 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
683 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
684 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
687 /******************************************************************************
688 * GetDeviceInfo : get information about a device's identity
690 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
691 LPDIRECTINPUTDEVICE8A iface,
692 LPDIDEVICEINSTANCEA pdidi)
694 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
695 DIPROPDWORD pd;
696 DWORD index = 0;
698 TRACE("(%p,%p)\n", This, pdidi);
700 if (pdidi == NULL) {
701 WARN("invalid pointer\n");
702 return E_POINTER;
705 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
706 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
707 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
708 return DIERR_INVALIDPARAM;
711 /* Try to get joystick index */
712 pd.diph.dwSize = sizeof(pd);
713 pd.diph.dwHeaderSize = sizeof(pd.diph);
714 pd.diph.dwObj = 0;
715 pd.diph.dwHow = DIPH_DEVICE;
716 if (SUCCEEDED(IDirectInputDevice2_GetProperty(iface, DIPROP_JOYSTICKID, &pd.diph)))
717 index = pd.dwData;
719 /* Return joystick */
720 pdidi->guidInstance = This->base.guid;
721 pdidi->guidProduct = This->guidProduct;
722 /* we only support traditional joysticks for now */
723 pdidi->dwDevType = This->devcaps.dwDevType;
724 snprintf(pdidi->tszInstanceName, MAX_PATH, "Joystick %d", index);
725 lstrcpynA(pdidi->tszProductName, This->name, MAX_PATH);
726 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
727 pdidi->guidFFDriver = GUID_NULL;
728 pdidi->wUsagePage = 0;
729 pdidi->wUsage = 0;
732 return DI_OK;
735 /******************************************************************************
736 * GetDeviceInfo : get information about a device's identity
738 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
739 LPDIRECTINPUTDEVICE8W iface,
740 LPDIDEVICEINSTANCEW pdidi)
742 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
743 CHAR buffer[MAX_PATH];
744 DIPROPDWORD pd;
745 DWORD index = 0;
747 TRACE("(%p,%p)\n", iface, pdidi);
749 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
750 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
751 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
752 return DIERR_INVALIDPARAM;
755 /* Try to get joystick index */
756 pd.diph.dwSize = sizeof(pd);
757 pd.diph.dwHeaderSize = sizeof(pd.diph);
758 pd.diph.dwObj = 0;
759 pd.diph.dwHow = DIPH_DEVICE;
760 if (SUCCEEDED(IDirectInputDevice2_GetProperty(iface, DIPROP_JOYSTICKID, &pd.diph)))
761 index = pd.dwData;
763 /* Return joystick */
764 pdidi->guidInstance = This->base.guid;
765 pdidi->guidProduct = This->guidProduct;
766 /* we only support traditional joysticks for now */
767 pdidi->dwDevType = This->devcaps.dwDevType;
768 snprintf(buffer, sizeof(buffer), "Joystick %d", index);
769 MultiByteToWideChar(CP_ACP, 0, buffer, -1, pdidi->tszInstanceName, MAX_PATH);
770 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
771 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
772 pdidi->guidFFDriver = GUID_NULL;
773 pdidi->wUsagePage = 0;
774 pdidi->wUsage = 0;
777 return DI_OK;
780 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
782 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
784 TRACE("(%p)\n",This);
786 if (!This->base.acquired) {
787 WARN("not acquired\n");
788 return DIERR_NOTACQUIRED;
791 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
792 return DI_OK;
795 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
797 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
798 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
801 /******************************************************************************
802 * GetDeviceState : returns the "state" of the joystick.
805 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
807 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
809 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
811 if (!This->base.acquired) {
812 WARN("not acquired\n");
813 return DIERR_NOTACQUIRED;
816 /* update joystick state */
817 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
819 /* convert and copy data to user supplied buffer */
820 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
822 return DI_OK;
825 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
827 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
828 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
832 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
833 LPDIACTIONFORMATW lpdiaf,
834 LPCWSTR lpszUserName,
835 DWORD dwFlags)
837 static const DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
838 static const DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
839 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
840 unsigned int i, j;
841 BOOL has_actions = FALSE;
843 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", This, lpdiaf, debugstr_w(lpszUserName), dwFlags);
845 for (i=0; i < lpdiaf->dwNumActions; i++)
847 DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
848 DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
849 DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
851 /* Don't touch a user configured action */
852 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
854 /* Only consider actions of the right genre */
855 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
857 for (j = 0; j < ARRAY_SIZE(object_types); j++)
859 if (type & object_types[j])
861 /* Ensure that the object exists */
862 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
864 if (odf != NULL)
866 lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
867 lpdiaf->rgoAction[i].guidInstance = This->base.guid;
868 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
870 has_actions = TRUE;
872 /* No need to try other types if the action was already mapped */
873 break;
879 if (!has_actions) return DI_NOEFFECT;
881 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
884 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
885 LPDIACTIONFORMATA lpdiaf,
886 LPCSTR lpszUserName,
887 DWORD dwFlags)
889 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
890 DIACTIONFORMATW diafW;
891 HRESULT hr;
892 WCHAR *lpszUserNameW = NULL;
893 int username_size;
895 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
896 _copy_diactionformatAtoW(&diafW, lpdiaf);
898 if (lpszUserName != NULL)
900 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
901 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
902 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
905 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
907 _copy_diactionformatWtoA(lpdiaf, &diafW);
908 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
909 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
911 return hr;
914 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
915 LPDIACTIONFORMATW lpdiaf,
916 LPCWSTR lpszUserName,
917 DWORD dwFlags)
919 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
921 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", This, lpdiaf, debugstr_w(lpszUserName), dwFlags);
923 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
926 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
927 LPDIACTIONFORMATA lpdiaf,
928 LPCSTR lpszUserName,
929 DWORD dwFlags)
931 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
932 DIACTIONFORMATW diafW;
933 HRESULT hr;
934 WCHAR *lpszUserNameW = NULL;
935 int username_size;
937 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
938 _copy_diactionformatAtoW(&diafW, lpdiaf);
940 if (lpszUserName != NULL)
942 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
943 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
944 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
947 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
949 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
950 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
952 return hr;
956 * This maps the read value (from the input event) to a value in the
957 * 'wanted' range.
958 * Notes:
959 * Dead zone is in % multiplied by a 100 (range 0..10000)
961 LONG joystick_map_axis(ObjProps *props, int val)
963 LONG ret;
964 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
965 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
967 /* Center input */
968 val -= (props->lDevMin + props->lDevMax) / 2;
970 /* Remove dead zone */
971 if (abs( val ) <= dead_zone / 2)
972 val = 0;
973 else
974 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
976 /* Scale and map the value from the device range into the required range */
977 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
978 (props->lMin + props->lMax) / 2;
980 /* Clamp in case or rounding errors */
981 if (ret > props->lMax) ret = props->lMax;
982 else if (ret < props->lMin) ret = props->lMin;
984 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
985 props->lDevMin, dead_zone, props->lDevMax,
986 props->lMin, props->lDeadZone, props->lMax,
987 val, ret );
989 return ret;
993 * Maps POV x & y event values to a DX "clock" position:
995 * 31500 4500
996 * 27000 -1 9000
997 * 22500 13500
998 * 18000
1000 DWORD joystick_map_pov(const POINTL *p)
1002 if (p->x > 0)
1003 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
1004 else if (p->x < 0)
1005 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
1006 else
1007 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
1011 * Setup the dinput options.
1014 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
1016 char buffer[MAX_PATH+16];
1017 HKEY hkey, appkey;
1018 int tokens = 0;
1019 int axis = 0;
1020 int pov = 0;
1022 get_app_key(&hkey, &appkey);
1024 /* get options */
1026 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
1028 This->deadzone = atoi(buffer);
1029 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
1032 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
1033 if (!This->axis_map) return DIERR_OUTOFMEMORY;
1035 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
1037 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
1038 "Slider1", "Slider2",
1039 "POV1", "POV2", "POV3", "POV4"};
1040 const char *delim = ",";
1041 char * ptr;
1042 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
1044 if ((ptr = strtok(buffer, delim)) != NULL)
1048 int i;
1050 for (i = 0; i < ARRAY_SIZE(axis_names); i++)
1052 if (!strcmp(ptr, axis_names[i]))
1054 if (!strncmp(ptr, "POV", 3))
1056 if (pov >= 4)
1058 WARN("Only 4 POVs supported - ignoring extra\n");
1059 i = -1;
1061 else
1063 /* Pov takes two axes */
1064 This->axis_map[tokens++] = i;
1065 pov++;
1068 else
1070 if (axis >= 8)
1072 FIXME("Only 8 Axes supported - ignoring extra\n");
1073 i = -1;
1075 else
1076 axis++;
1078 break;
1082 if (i == ARRAY_SIZE(axis_names))
1084 ERR("invalid joystick axis type: \"%s\"\n", ptr);
1085 i = -1;
1088 This->axis_map[tokens] = i;
1089 tokens++;
1090 } while ((ptr = strtok(NULL, delim)) != NULL);
1092 if (tokens != This->device_axis_count)
1094 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
1095 This->device_axis_count, axis, pov, tokens);
1096 while (tokens < This->device_axis_count)
1098 This->axis_map[tokens] = -1;
1099 tokens++;
1104 else
1106 int i;
1108 if (default_axis_map)
1110 /* Use default mapping from the driver */
1111 for (i = 0; i < This->device_axis_count; i++)
1113 This->axis_map[i] = default_axis_map[i];
1114 tokens = default_axis_map[i];
1115 if (tokens < 0)
1116 continue;
1117 if (tokens < 8)
1118 axis++;
1119 else if (tokens < 15)
1121 i++;
1122 pov++;
1123 This->axis_map[i] = default_axis_map[i];
1127 else
1129 /* No config - set default mapping. */
1130 for (i = 0; i < This->device_axis_count; i++)
1132 if (i < 8)
1133 This->axis_map[i] = axis++;
1134 else if (i < 15)
1136 This->axis_map[i++] = 8 + pov;
1137 This->axis_map[i ] = 8 + pov++;
1139 else
1140 This->axis_map[i] = -1;
1144 This->devcaps.dwAxes = axis;
1145 This->devcaps.dwPOVs = pov;
1147 if (appkey) RegCloseKey(appkey);
1148 if (hkey) RegCloseKey(hkey);
1150 return DI_OK;