875d8ac7523aae5ac391c58118f0432a9bc4757f
[reactos.git] / reactos / dll / directx / wine / dinput / joystick.c
1 /* DirectInput Generic Joystick device
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2009 Aric Stewart, CodeWeavers
7 *
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.
12 *
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.
17 *
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
21 */
22
23 /*
24 * To Do:
25 * dead zone
26 * force feedback
27 */
28
29 #include "joystick_private.h"
30 #include <wine/debug.h>
31 #include <winreg.h>
32
33 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
34
35 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
36 {
37 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface), JoystickGenericImpl, base);
38 }
39 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
40 {
41 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface), JoystickGenericImpl, base);
42 }
43 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickGenericImpl *This)
44 {
45 return &This->base.IDirectInputDevice8A_iface;
46 }
47 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickGenericImpl *This)
48 {
49 return &This->base.IDirectInputDevice8W_iface;
50 }
51
52 DWORD typeFromGUID(REFGUID guid)
53 {
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;
74 }
75 }
76
77 static void _dump_DIEFFECT_flags(DWORD dwFlags)
78 {
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
92 };
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");
97 }
98 }
99
100 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
101 {
102 if (env->dwSize != sizeof(DIENVELOPE)) {
103 WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
104 }
105 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
106 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
107 }
108
109 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
110 {
111 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
112 }
113
114 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
115 {
116 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
117 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
118 }
119
120 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
121 {
122 TRACE("Ramp force has start %d, end %d\n",
123 frc->lStart, frc->lEnd);
124 }
125
126 static void _dump_DICONDITION(LPCDICONDITION frc)
127 {
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);
131 }
132
133 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
134 {
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");
146 }
147 }
148 }
149
150 void dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid, DWORD dwFlags)
151 {
152 DWORD type = typeFromGUID(guid);
153 unsigned int i;
154
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);
159 }
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);
165
166 if (eff->dwGain > 10000)
167 WARN("dwGain is out of range (>10,000)\n");
168
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);
174
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);
179
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");
185 }
186 }
187
188 if (dwFlags & DIEP_ENVELOPE) {
189 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
190 if (eff->lpEnvelope != NULL)
191 _dump_DIENVELOPE(eff->lpEnvelope);
192 }
193
194 if (eff->dwSize > sizeof(DIEFFECT_DX5))
195 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
196
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);
202 }
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);
208 }
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);
214 }
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);
220 }
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);
226 }
227 }
228 }
229
230 BOOL device_disabled_registry(const char* name)
231 {
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;
237
238 get_app_key(&hkey, &appkey);
239
240 /* Joystick settings are in the 'joysticks' subkey */
241 if (appkey)
242 {
243 if (RegOpenKeyA(appkey, joystick_key, &temp)) temp = 0;
244 RegCloseKey(appkey);
245 appkey = temp;
246 }
247 if (hkey)
248 {
249 if (RegOpenKeyA(hkey, joystick_key, &temp)) temp = 0;
250 RegCloseKey(hkey);
251 hkey = temp;
252 }
253
254 /* Look for the "controllername"="disabled" key */
255 if (!get_config_key(hkey, appkey, name, buffer, sizeof(buffer)))
256 if (!strcmp(disabled_str, buffer))
257 {
258 TRACE("Disabling joystick '%s' based on registry key.\n", name);
259 do_disable = TRUE;
260 }
261
262 if (appkey) RegCloseKey(appkey);
263 if (hkey) RegCloseKey(hkey);
264
265 return do_disable;
266 }
267
268 /******************************************************************************
269 * SetProperty : change input device properties
270 */
271 HRESULT WINAPI JoystickWGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
272 {
273 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
274 DWORD i;
275 ObjProps remap_props;
276
277 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
278
279 if (ph == NULL) {
280 WARN("invalid parameter: ph == NULL\n");
281 return DIERR_INVALIDPARAM;
282 }
283
284 if (TRACE_ON(dinput))
285 _dump_DIPROPHEADER(ph);
286
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++) {
294
295 remap_props.lDevMin = This->props[i].lMin;
296 remap_props.lDevMax = This->props[i].lMax;
297
298 remap_props.lDeadZone = This->props[i].lDeadZone;
299 remap_props.lSaturation = This->props[i].lSaturation;
300
301 remap_props.lMin = pr->lMin;
302 remap_props.lMax = pr->lMax;
303
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;
314 }
315
316 This->props[i].lMin = pr->lMin;
317 This->props[i].lMax = pr->lMax;
318 }
319 } else {
320 int obj = find_property(&This->base.data_format, ph);
321
322 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
323 if (obj >= 0) {
324
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*/
326
327 remap_props.lDevMin = This->props[obj].lMin;
328 remap_props.lDevMax = This->props[obj].lMax;
329
330 remap_props.lDeadZone = This->props[obj].lDeadZone;
331 remap_props.lSaturation = This->props[obj].lSaturation;
332
333 remap_props.lMin = pr->lMin;
334 remap_props.lMax = pr->lMax;
335
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;
346 }
347
348 This->props[obj].lMin = pr->lMin;
349 This->props[obj].lMax = pr->lMax;
350 return DI_OK;
351 }
352 }
353 break;
354 }
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);
363
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;
368 }
369 }
370 break;
371 }
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);
380
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;
385 }
386 }
387 break;
388 }
389 default:
390 return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
391 }
392 }
393
394 return DI_OK;
395 }
396
397 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
398 {
399 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
400 return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
401 }
402
403 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
404 {
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);
423 }
424 }
425
426 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
427 {
428 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
429 int size;
430
431 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
432
433 if (lpDIDevCaps == NULL) {
434 WARN("invalid pointer\n");
435 return E_POINTER;
436 }
437
438 size = lpDIDevCaps->dwSize;
439
440 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
441 WARN("invalid parameter\n");
442 return DIERR_INVALIDPARAM;
443 }
444
445 CopyMemory(lpDIDevCaps, &This->devcaps, size);
446 lpDIDevCaps->dwSize = size;
447
448 if (TRACE_ON(dinput))
449 _dump_DIDEVCAPS(lpDIDevCaps);
450
451 return DI_OK;
452 }
453
454 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
455 {
456 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
457 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
458 }
459
460 /******************************************************************************
461 * GetObjectInfo : get object info
462 */
463 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
464 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
465 {
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;
470
471 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
472 if (res != DI_OK) return res;
473
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));
480
481 _dump_OBJECTINSTANCEW(pdidoi);
482 return res;
483 }
484
485 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
486 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
487 {
488 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
489 HRESULT res;
490 DIDEVICEOBJECTINSTANCEW didoiW;
491 DWORD dwSize = pdidoi->dwSize;
492
493 didoiW.dwSize = sizeof(didoiW);
494 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
495 if (res != DI_OK) return res;
496
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);
502
503 return res;
504 }
505
506 /******************************************************************************
507 * GetProperty : get input device properties
508 */
509 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
510 {
511 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
512
513 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
514
515 if (TRACE_ON(dinput))
516 _dump_DIPROPHEADER(pdiph);
517
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);
523
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;
531 }
532 break;
533 }
534 case (DWORD_PTR) DIPROP_DEADZONE: {
535 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
536 int obj = find_property(&This->base.data_format, pdiph);
537
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;
542 }
543 break;
544 }
545 case (DWORD_PTR) DIPROP_SATURATION: {
546 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
547 int obj = find_property(&This->base.data_format, pdiph);
548
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;
553 }
554 break;
555 }
556 case (DWORD_PTR) DIPROP_INSTANCENAME: {
557 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
558 DIDEVICEINSTANCEW didev;
559
560 didev.dwSize = sizeof(didev);
561
562 IDirectInputDevice_GetDeviceInfo(iface, &didev);
563 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
564
565 return DI_OK;
566 }
567 default:
568 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
569 }
570 }
571
572 return DI_OK;
573 }
574
575 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
576 {
577 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
578 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
579 }
580
581 /******************************************************************************
582 * GetDeviceInfo : get information about a device's identity
583 */
584 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
585 LPDIRECTINPUTDEVICE8A iface,
586 LPDIDEVICEINSTANCEA pdidi)
587 {
588 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
589
590 TRACE("(%p,%p)\n", iface, pdidi);
591
592 if (pdidi == NULL) {
593 WARN("invalid pointer\n");
594 return E_POINTER;
595 }
596
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;
601 }
602
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;
614 }
615
616 return DI_OK;
617 }
618
619 /******************************************************************************
620 * GetDeviceInfo : get information about a device's identity
621 */
622 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
623 LPDIRECTINPUTDEVICE8W iface,
624 LPDIDEVICEINSTANCEW pdidi)
625 {
626 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
627
628 TRACE("(%p,%p)\n", iface, pdidi);
629
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;
634 }
635
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;
647 }
648
649 return DI_OK;
650 }
651
652 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
653 {
654 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
655
656 TRACE("(%p)\n",This);
657
658 if (!This->base.acquired) {
659 WARN("not acquired\n");
660 return DIERR_NOTACQUIRED;
661 }
662
663 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
664 return DI_OK;
665 }
666
667 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
668 {
669 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
670 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
671 }
672
673 /******************************************************************************
674 * GetDeviceState : returns the "state" of the joystick.
675 *
676 */
677 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
678 {
679 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
680
681 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
682
683 if (!This->base.acquired) {
684 WARN("not acquired\n");
685 return DIERR_NOTACQUIRED;
686 }
687
688 /* update joystick state */
689 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
690
691 /* convert and copy data to user supplied buffer */
692 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
693
694 return DI_OK;
695 }
696
697 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
698 {
699 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
700 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
701 }
702
703
704 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
705 LPDIACTIONFORMATW lpdiaf,
706 LPCWSTR lpszUserName,
707 DWORD dwFlags)
708 {
709 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
710 unsigned int i, j;
711 int has_actions = 0;
712 DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
713 DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
714
715 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
716
717 for (i=0; i < lpdiaf->dwNumActions; i++)
718 {
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;
722
723 /* Don't touch an user configured action */
724 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
725
726 /* Only consider actions of the right genre */
727 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
728
729 for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
730 {
731 if (type & object_types[j])
732 {
733 /* Assure that the object exists */
734 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
735
736 if (odf != NULL)
737 {
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;
741
742 has_actions = 1;
743
744 /* No need to try other types if the action was already mapped */
745 break;
746 }
747 }
748 }
749 }
750
751 if (!has_actions) return DI_NOEFFECT;
752
753 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
754 }
755
756 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
757 LPDIACTIONFORMATA lpdiaf,
758 LPCSTR lpszUserName,
759 DWORD dwFlags)
760 {
761 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
762 DIACTIONFORMATW diafW;
763 HRESULT hr;
764 WCHAR *lpszUserNameW = NULL;
765 int username_size;
766
767 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
768 _copy_diactionformatAtoW(&diafW, lpdiaf);
769
770 if (lpszUserName != NULL)
771 {
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);
775 }
776
777 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
778
779 _copy_diactionformatWtoA(lpdiaf, &diafW);
780 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
781 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
782
783 return hr;
784 }
785
786 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
787 LPDIACTIONFORMATW lpdiaf,
788 LPCWSTR lpszUserName,
789 DWORD dwFlags)
790 {
791 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
792
793 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
794
795 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
796 }
797
798 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
799 LPDIACTIONFORMATA lpdiaf,
800 LPCSTR lpszUserName,
801 DWORD dwFlags)
802 {
803 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
804 DIACTIONFORMATW diafW;
805 HRESULT hr;
806 WCHAR *lpszUserNameW = NULL;
807 int username_size;
808
809 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
810 _copy_diactionformatAtoW(&diafW, lpdiaf);
811
812 if (lpszUserName != NULL)
813 {
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);
817 }
818
819 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
820
821 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
822 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
823
824 return hr;
825 }
826
827 /*
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)
832 */
833 LONG joystick_map_axis(ObjProps *props, int val)
834 {
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;
838
839 /* Center input */
840 val -= (props->lDevMin + props->lDevMax) / 2;
841
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;
847
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;
851
852 /* Clamp in case or rounding errors */
853 if (ret > props->lMax) ret = props->lMax;
854 else if (ret < props->lMin) ret = props->lMin;
855
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 );
860
861 return ret;
862 }
863
864 /*
865 * Maps POV x & y event values to a DX "clock" position:
866 * 0
867 * 31500 4500
868 * 27000 -1 9000
869 * 22500 13500
870 * 18000
871 */
872 DWORD joystick_map_pov(const POINTL *p)
873 {
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;
880 }
881
882 /*
883 * Setup the dinput options.
884 */
885
886 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
887 {
888 char buffer[MAX_PATH+16];
889 HKEY hkey, appkey;
890 int tokens = 0;
891 int axis = 0;
892 int pov = 0;
893
894 get_app_key(&hkey, &appkey);
895
896 /* get options */
897
898 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
899 {
900 This->deadzone = atoi(buffer);
901 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
902 }
903
904 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
905 if (!This->axis_map) return DIERR_OUTOFMEMORY;
906
907 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
908 {
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);
915
916 if ((ptr = strtok(buffer, delim)) != NULL)
917 {
918 do
919 {
920 int i;
921
922 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
923 {
924 if (!strcmp(ptr, axis_names[i]))
925 {
926 if (!strncmp(ptr, "POV", 3))
927 {
928 if (pov >= 4)
929 {
930 WARN("Only 4 POVs supported - ignoring extra\n");
931 i = -1;
932 }
933 else
934 {
935 /* Pov takes two axes */
936 This->axis_map[tokens++] = i;
937 pov++;
938 }
939 }
940 else
941 {
942 if (axis >= 8)
943 {
944 FIXME("Only 8 Axes supported - ignoring extra\n");
945 i = -1;
946 }
947 else
948 axis++;
949 }
950 break;
951 }
952 }
953
954 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
955 {
956 ERR("invalid joystick axis type: \"%s\"\n", ptr);
957 i = -1;
958 }
959
960 This->axis_map[tokens] = i;
961 tokens++;
962 } while ((ptr = strtok(NULL, delim)) != NULL);
963
964 if (tokens != This->device_axis_count)
965 {
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)
969 {
970 This->axis_map[tokens] = -1;
971 tokens++;
972 }
973 }
974 }
975 }
976 else
977 {
978 int i;
979
980 if (default_axis_map)
981 {
982 /* Use default mapping from the driver */
983 for (i = 0; i < This->device_axis_count; i++)
984 {
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)
992 {
993 i++;
994 pov++;
995 This->axis_map[i] = default_axis_map[i];
996 }
997 }
998 }
999 else
1000 {
1001 /* No config - set default mapping. */
1002 for (i = 0; i < This->device_axis_count; i++)
1003 {
1004 if (i < 8)
1005 This->axis_map[i] = axis++;
1006 else if (i < 15)
1007 {
1008 This->axis_map[i++] = 8 + pov;
1009 This->axis_map[i ] = 8 + pov++;
1010 }
1011 else
1012 This->axis_map[i] = -1;
1013 }
1014 }
1015 }
1016 This->devcaps.dwAxes = axis;
1017 This->devcaps.dwPOVs = pov;
1018
1019 if (appkey) RegCloseKey(appkey);
1020 if (hkey) RegCloseKey(hkey);
1021
1022 return DI_OK;
1023 }