[DINPUT]
[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 "dinput_private.h"
30
31 #include "joystick_private.h"
32
33 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
34 {
35 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface), JoystickGenericImpl, base);
36 }
37 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
38 {
39 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface), JoystickGenericImpl, base);
40 }
41 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickGenericImpl *This)
42 {
43 return &This->base.IDirectInputDevice8A_iface;
44 }
45 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickGenericImpl *This)
46 {
47 return &This->base.IDirectInputDevice8W_iface;
48 }
49
50 DWORD typeFromGUID(REFGUID guid)
51 {
52 if (IsEqualGUID(guid, &GUID_ConstantForce)) {
53 return DIEFT_CONSTANTFORCE;
54 } else if (IsEqualGUID(guid, &GUID_Square)
55 || IsEqualGUID(guid, &GUID_Sine)
56 || IsEqualGUID(guid, &GUID_Triangle)
57 || IsEqualGUID(guid, &GUID_SawtoothUp)
58 || IsEqualGUID(guid, &GUID_SawtoothDown)) {
59 return DIEFT_PERIODIC;
60 } else if (IsEqualGUID(guid, &GUID_RampForce)) {
61 return DIEFT_RAMPFORCE;
62 } else if (IsEqualGUID(guid, &GUID_Spring)
63 || IsEqualGUID(guid, &GUID_Damper)
64 || IsEqualGUID(guid, &GUID_Inertia)
65 || IsEqualGUID(guid, &GUID_Friction)) {
66 return DIEFT_CONDITION;
67 } else if (IsEqualGUID(guid, &GUID_CustomForce)) {
68 return DIEFT_CUSTOMFORCE;
69 } else {
70 WARN("GUID (%s) is not a known force type\n", _dump_dinput_GUID(guid));
71 return 0;
72 }
73 }
74
75 static void _dump_DIEFFECT_flags(DWORD dwFlags)
76 {
77 if (TRACE_ON(dinput)) {
78 unsigned int i;
79 static const struct {
80 DWORD mask;
81 const char *name;
82 } flags[] = {
83 #define FE(x) { x, #x}
84 FE(DIEFF_CARTESIAN),
85 FE(DIEFF_OBJECTIDS),
86 FE(DIEFF_OBJECTOFFSETS),
87 FE(DIEFF_POLAR),
88 FE(DIEFF_SPHERICAL)
89 #undef FE
90 };
91 for (i = 0; i < (sizeof(flags) / sizeof(flags[0])); i++)
92 if (flags[i].mask & dwFlags)
93 TRACE("%s ", flags[i].name);
94 TRACE("\n");
95 }
96 }
97
98 static void _dump_DIENVELOPE(LPCDIENVELOPE env)
99 {
100 if (env->dwSize != sizeof(DIENVELOPE)) {
101 WARN("Non-standard DIENVELOPE structure size %d.\n", env->dwSize);
102 }
103 TRACE("Envelope has attack (level: %d time: %d), fade (level: %d time: %d)\n",
104 env->dwAttackLevel, env->dwAttackTime, env->dwFadeLevel, env->dwFadeTime);
105 }
106
107 static void _dump_DICONSTANTFORCE(LPCDICONSTANTFORCE frc)
108 {
109 TRACE("Constant force has magnitude %d\n", frc->lMagnitude);
110 }
111
112 static void _dump_DIPERIODIC(LPCDIPERIODIC frc)
113 {
114 TRACE("Periodic force has magnitude %d, offset %d, phase %d, period %d\n",
115 frc->dwMagnitude, frc->lOffset, frc->dwPhase, frc->dwPeriod);
116 }
117
118 static void _dump_DIRAMPFORCE(LPCDIRAMPFORCE frc)
119 {
120 TRACE("Ramp force has start %d, end %d\n",
121 frc->lStart, frc->lEnd);
122 }
123
124 static void _dump_DICONDITION(LPCDICONDITION frc)
125 {
126 TRACE("Condition has offset %d, pos/neg coefficients %d and %d, pos/neg saturations %d and %d, deadband %d\n",
127 frc->lOffset, frc->lPositiveCoefficient, frc->lNegativeCoefficient,
128 frc->dwPositiveSaturation, frc->dwNegativeSaturation, frc->lDeadBand);
129 }
130
131 static void _dump_DICUSTOMFORCE(LPCDICUSTOMFORCE frc)
132 {
133 unsigned int i;
134 TRACE("Custom force uses %d channels, sample period %d. Has %d samples at %p.\n",
135 frc->cChannels, frc->dwSamplePeriod, frc->cSamples, frc->rglForceData);
136 if (frc->cSamples % frc->cChannels != 0)
137 WARN("Custom force has a non-integral samples-per-channel count!\n");
138 if (TRACE_ON(dinput)) {
139 TRACE("Custom force data (time aligned, axes in order):\n");
140 for (i = 1; i <= frc->cSamples; ++i) {
141 TRACE("%d ", frc->rglForceData[i]);
142 if (i % frc->cChannels == 0)
143 TRACE("\n");
144 }
145 }
146 }
147
148 void dump_DIEFFECT(LPCDIEFFECT eff, REFGUID guid, DWORD dwFlags)
149 {
150 DWORD type = typeFromGUID(guid);
151 unsigned int i;
152
153 TRACE("Dumping DIEFFECT structure:\n");
154 TRACE(" - dwSize: %d\n", eff->dwSize);
155 if ((eff->dwSize != sizeof(DIEFFECT)) && (eff->dwSize != sizeof(DIEFFECT_DX5))) {
156 WARN("Non-standard DIEFFECT structure size %d\n", eff->dwSize);
157 }
158 TRACE(" - dwFlags: %d\n", eff->dwFlags);
159 TRACE(" ");
160 _dump_DIEFFECT_flags(eff->dwFlags);
161 TRACE(" - dwDuration: %d\n", eff->dwDuration);
162 TRACE(" - dwGain: %d\n", eff->dwGain);
163
164 if (eff->dwGain > 10000)
165 WARN("dwGain is out of range (>10,000)\n");
166
167 TRACE(" - dwTriggerButton: %d\n", eff->dwTriggerButton);
168 TRACE(" - dwTriggerRepeatInterval: %d\n", eff->dwTriggerRepeatInterval);
169 TRACE(" - rglDirection: %p\n", eff->rglDirection);
170 TRACE(" - cbTypeSpecificParams: %d\n", eff->cbTypeSpecificParams);
171 TRACE(" - lpvTypeSpecificParams: %p\n", eff->lpvTypeSpecificParams);
172
173 /* Only trace some members if dwFlags indicates they have data */
174 if (dwFlags & DIEP_AXES) {
175 TRACE(" - cAxes: %d\n", eff->cAxes);
176 TRACE(" - rgdwAxes: %p\n", eff->rgdwAxes);
177
178 if (TRACE_ON(dinput) && eff->rgdwAxes) {
179 TRACE(" ");
180 for (i = 0; i < eff->cAxes; ++i)
181 TRACE("%d ", eff->rgdwAxes[i]);
182 TRACE("\n");
183 }
184 }
185
186 if (dwFlags & DIEP_ENVELOPE) {
187 TRACE(" - lpEnvelope: %p\n", eff->lpEnvelope);
188 if (eff->lpEnvelope != NULL)
189 _dump_DIENVELOPE(eff->lpEnvelope);
190 }
191
192 if (eff->dwSize > sizeof(DIEFFECT_DX5))
193 TRACE(" - dwStartDelay: %d\n", eff->dwStartDelay);
194
195 if (type == DIEFT_CONSTANTFORCE) {
196 if (eff->cbTypeSpecificParams != sizeof(DICONSTANTFORCE)) {
197 WARN("Effect claims to be a constant force but the type-specific params are the wrong size!\n");
198 } else {
199 _dump_DICONSTANTFORCE(eff->lpvTypeSpecificParams);
200 }
201 } else if (type == DIEFT_PERIODIC) {
202 if (eff->cbTypeSpecificParams != sizeof(DIPERIODIC)) {
203 WARN("Effect claims to be a periodic force but the type-specific params are the wrong size!\n");
204 } else {
205 _dump_DIPERIODIC(eff->lpvTypeSpecificParams);
206 }
207 } else if (type == DIEFT_RAMPFORCE) {
208 if (eff->cbTypeSpecificParams != sizeof(DIRAMPFORCE)) {
209 WARN("Effect claims to be a ramp force but the type-specific params are the wrong size!\n");
210 } else {
211 _dump_DIRAMPFORCE(eff->lpvTypeSpecificParams);
212 }
213 } else if (type == DIEFT_CONDITION) {
214 if (eff->cbTypeSpecificParams != sizeof(DICONDITION)) {
215 WARN("Effect claims to be a condition but the type-specific params are the wrong size!\n");
216 } else {
217 _dump_DICONDITION(eff->lpvTypeSpecificParams);
218 }
219 } else if (type == DIEFT_CUSTOMFORCE) {
220 if (eff->cbTypeSpecificParams != sizeof(DICUSTOMFORCE)) {
221 WARN("Effect claims to be a custom force but the type-specific params are the wrong size!\n");
222 } else {
223 _dump_DICUSTOMFORCE(eff->lpvTypeSpecificParams);
224 }
225 }
226 }
227
228 BOOL device_disabled_registry(const char* name)
229 {
230 static const char disabled_str[] = "disabled";
231 static const char joystick_key[] = "Joysticks";
232 char buffer[MAX_PATH];
233 HKEY hkey, appkey, temp;
234 BOOL do_disable = FALSE;
235
236 get_app_key(&hkey, &appkey);
237
238 /* Joystick settings are in the 'joysticks' subkey */
239 if (appkey)
240 {
241 if (RegOpenKeyA(appkey, joystick_key, &temp)) temp = 0;
242 RegCloseKey(appkey);
243 appkey = temp;
244 }
245 if (hkey)
246 {
247 if (RegOpenKeyA(hkey, joystick_key, &temp)) temp = 0;
248 RegCloseKey(hkey);
249 hkey = temp;
250 }
251
252 /* Look for the "controllername"="disabled" key */
253 if (!get_config_key(hkey, appkey, name, buffer, sizeof(buffer)))
254 if (!strcmp(disabled_str, buffer))
255 {
256 TRACE("Disabling joystick '%s' based on registry key.\n", name);
257 do_disable = TRUE;
258 }
259
260 if (appkey) RegCloseKey(appkey);
261 if (hkey) RegCloseKey(hkey);
262
263 return do_disable;
264 }
265
266 /******************************************************************************
267 * SetProperty : change input device properties
268 */
269 HRESULT WINAPI JoystickWGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
270 {
271 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
272 DWORD i;
273 ObjProps remap_props;
274
275 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
276
277 if (ph == NULL) {
278 WARN("invalid parameter: ph == NULL\n");
279 return DIERR_INVALIDPARAM;
280 }
281
282 if (TRACE_ON(dinput))
283 _dump_DIPROPHEADER(ph);
284
285 if (IS_DIPROP(rguid)) {
286 switch (LOWORD(rguid)) {
287 case (DWORD_PTR)DIPROP_RANGE: {
288 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
289 if (ph->dwHow == DIPH_DEVICE) {
290 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
291 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
292
293 remap_props.lDevMin = This->props[i].lMin;
294 remap_props.lDevMax = This->props[i].lMax;
295
296 remap_props.lDeadZone = This->props[i].lDeadZone;
297 remap_props.lSaturation = This->props[i].lSaturation;
298
299 remap_props.lMin = pr->lMin;
300 remap_props.lMax = pr->lMax;
301
302 switch (This->base.data_format.wine_df->rgodf[i].dwOfs) {
303 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
304 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
305 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
306 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
307 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
308 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
309 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
310 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
311 default: break;
312 }
313
314 This->props[i].lMin = pr->lMin;
315 This->props[i].lMax = pr->lMax;
316 }
317 } else {
318 int obj = find_property(&This->base.data_format, ph);
319
320 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
321 if (obj >= 0) {
322
323 /*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*/
324
325 remap_props.lDevMin = This->props[obj].lMin;
326 remap_props.lDevMax = This->props[obj].lMax;
327
328 remap_props.lDeadZone = This->props[obj].lDeadZone;
329 remap_props.lSaturation = This->props[obj].lSaturation;
330
331 remap_props.lMin = pr->lMin;
332 remap_props.lMax = pr->lMax;
333
334 switch (ph->dwObj) {
335 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
336 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
337 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
338 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
339 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
340 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
341 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
342 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
343 default: break;
344 }
345
346 This->props[obj].lMin = pr->lMin;
347 This->props[obj].lMax = pr->lMax;
348 return DI_OK;
349 }
350 }
351 break;
352 }
353 case (DWORD_PTR)DIPROP_DEADZONE: {
354 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
355 if (ph->dwHow == DIPH_DEVICE) {
356 TRACE("deadzone(%d) all\n", pd->dwData);
357 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
358 This->props[i].lDeadZone = pd->dwData;
359 } else {
360 int obj = find_property(&This->base.data_format, ph);
361
362 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
363 if (obj >= 0) {
364 This->props[obj].lDeadZone = pd->dwData;
365 return DI_OK;
366 }
367 }
368 break;
369 }
370 case (DWORD_PTR)DIPROP_SATURATION: {
371 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
372 if (ph->dwHow == DIPH_DEVICE) {
373 TRACE("saturation(%d) all\n", pd->dwData);
374 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
375 This->props[i].lSaturation = pd->dwData;
376 } else {
377 int obj = find_property(&This->base.data_format, ph);
378
379 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
380 if (obj >= 0) {
381 This->props[obj].lSaturation = pd->dwData;
382 return DI_OK;
383 }
384 }
385 break;
386 }
387 default:
388 return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
389 }
390 }
391
392 return DI_OK;
393 }
394
395 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
396 {
397 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
398 return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
399 }
400
401 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
402 {
403 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
404 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
405 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
406 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
407 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
408 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
409 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
410 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
411 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
412 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
413 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
414 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
415 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
416 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
417 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
418 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
419 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
420 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
421 }
422 }
423
424 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
425 {
426 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
427 int size;
428
429 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
430
431 if (lpDIDevCaps == NULL) {
432 WARN("invalid pointer\n");
433 return E_POINTER;
434 }
435
436 size = lpDIDevCaps->dwSize;
437
438 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
439 WARN("invalid parameter\n");
440 return DIERR_INVALIDPARAM;
441 }
442
443 CopyMemory(lpDIDevCaps, &This->devcaps, size);
444 lpDIDevCaps->dwSize = size;
445
446 if (TRACE_ON(dinput))
447 _dump_DIDEVCAPS(lpDIDevCaps);
448
449 return DI_OK;
450 }
451
452 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
453 {
454 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
455 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
456 }
457
458 /******************************************************************************
459 * GetObjectInfo : get object info
460 */
461 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
462 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
463 {
464 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
465 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
466 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
467 HRESULT res;
468
469 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
470 if (res != DI_OK) return res;
471
472 if (pdidoi->dwType & DIDFT_AXIS)
473 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
474 else if (pdidoi->dwType & DIDFT_POV)
475 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
476 else if (pdidoi->dwType & DIDFT_BUTTON)
477 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
478
479 _dump_OBJECTINSTANCEW(pdidoi);
480 return res;
481 }
482
483 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
484 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
485 {
486 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
487 HRESULT res;
488 DIDEVICEOBJECTINSTANCEW didoiW;
489 DWORD dwSize = pdidoi->dwSize;
490
491 didoiW.dwSize = sizeof(didoiW);
492 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
493 if (res != DI_OK) return res;
494
495 memset(pdidoi, 0, pdidoi->dwSize);
496 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
497 pdidoi->dwSize = dwSize;
498 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
499 sizeof(pdidoi->tszName), NULL, NULL);
500
501 return res;
502 }
503
504 /******************************************************************************
505 * GetProperty : get input device properties
506 */
507 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
508 {
509 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
510
511 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
512
513 if (TRACE_ON(dinput))
514 _dump_DIPROPHEADER(pdiph);
515
516 if (IS_DIPROP(rguid)) {
517 switch (LOWORD(rguid)) {
518 case (DWORD_PTR) DIPROP_RANGE: {
519 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
520 int obj = find_property(&This->base.data_format, pdiph);
521
522 /* The app is querying the current range of the axis
523 * return the lMin and lMax values */
524 if (obj >= 0) {
525 pr->lMin = This->props[obj].lMin;
526 pr->lMax = This->props[obj].lMax;
527 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
528 return DI_OK;
529 }
530 break;
531 }
532 case (DWORD_PTR) DIPROP_DEADZONE: {
533 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
534 int obj = find_property(&This->base.data_format, pdiph);
535
536 if (obj >= 0) {
537 pd->dwData = This->props[obj].lDeadZone;
538 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
539 return DI_OK;
540 }
541 break;
542 }
543 case (DWORD_PTR) DIPROP_SATURATION: {
544 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
545 int obj = find_property(&This->base.data_format, pdiph);
546
547 if (obj >= 0) {
548 pd->dwData = This->props[obj].lSaturation;
549 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
550 return DI_OK;
551 }
552 break;
553 }
554 case (DWORD_PTR) DIPROP_INSTANCENAME: {
555 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
556 DIDEVICEINSTANCEW didev;
557
558 didev.dwSize = sizeof(didev);
559
560 IDirectInputDevice_GetDeviceInfo(iface, &didev);
561 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
562
563 return DI_OK;
564 }
565 default:
566 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
567 }
568 }
569
570 return DI_OK;
571 }
572
573 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
574 {
575 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
576 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
577 }
578
579 /******************************************************************************
580 * GetDeviceInfo : get information about a device's identity
581 */
582 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
583 LPDIRECTINPUTDEVICE8A iface,
584 LPDIDEVICEINSTANCEA pdidi)
585 {
586 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
587
588 TRACE("(%p,%p)\n", iface, pdidi);
589
590 if (pdidi == NULL) {
591 WARN("invalid pointer\n");
592 return E_POINTER;
593 }
594
595 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
596 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
597 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
598 return DIERR_INVALIDPARAM;
599 }
600
601 /* Return joystick */
602 pdidi->guidInstance = This->guidInstance;
603 pdidi->guidProduct = This->guidProduct;
604 /* we only support traditional joysticks for now */
605 pdidi->dwDevType = This->devcaps.dwDevType;
606 strcpy(pdidi->tszInstanceName, "Joystick");
607 strcpy(pdidi->tszProductName, This->name);
608 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
609 pdidi->guidFFDriver = GUID_NULL;
610 pdidi->wUsagePage = 0;
611 pdidi->wUsage = 0;
612 }
613
614 return DI_OK;
615 }
616
617 /******************************************************************************
618 * GetDeviceInfo : get information about a device's identity
619 */
620 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
621 LPDIRECTINPUTDEVICE8W iface,
622 LPDIDEVICEINSTANCEW pdidi)
623 {
624 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
625
626 TRACE("(%p,%p)\n", iface, pdidi);
627
628 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
629 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
630 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
631 return DIERR_INVALIDPARAM;
632 }
633
634 /* Return joystick */
635 pdidi->guidInstance = This->guidInstance;
636 pdidi->guidProduct = This->guidProduct;
637 /* we only support traditional joysticks for now */
638 pdidi->dwDevType = This->devcaps.dwDevType;
639 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
640 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
641 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
642 pdidi->guidFFDriver = GUID_NULL;
643 pdidi->wUsagePage = 0;
644 pdidi->wUsage = 0;
645 }
646
647 return DI_OK;
648 }
649
650 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
651 {
652 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
653
654 TRACE("(%p)\n",This);
655
656 if (!This->base.acquired) {
657 WARN("not acquired\n");
658 return DIERR_NOTACQUIRED;
659 }
660
661 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
662 return DI_OK;
663 }
664
665 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
666 {
667 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
668 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
669 }
670
671 /******************************************************************************
672 * GetDeviceState : returns the "state" of the joystick.
673 *
674 */
675 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
676 {
677 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
678
679 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
680
681 if (!This->base.acquired) {
682 WARN("not acquired\n");
683 return DIERR_NOTACQUIRED;
684 }
685
686 /* update joystick state */
687 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
688
689 /* convert and copy data to user supplied buffer */
690 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
691
692 return DI_OK;
693 }
694
695 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
696 {
697 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
698 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
699 }
700
701
702 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
703 LPDIACTIONFORMATW lpdiaf,
704 LPCWSTR lpszUserName,
705 DWORD dwFlags)
706 {
707 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
708 unsigned int i, j;
709 int has_actions = 0;
710 DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
711 DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
712
713 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
714
715 for (i=0; i < lpdiaf->dwNumActions; i++)
716 {
717 DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
718 DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
719 DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
720
721 /* Don't touch an user configured action */
722 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
723
724 /* Only consider actions of the right genre */
725 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
726
727 for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
728 {
729 if (type & object_types[j])
730 {
731 /* Assure that the object exists */
732 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
733
734 if (odf != NULL)
735 {
736 lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
737 lpdiaf->rgoAction[i].guidInstance = This->base.guid;
738 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
739
740 has_actions = 1;
741
742 /* No need to try other types if the action was already mapped */
743 break;
744 }
745 }
746 }
747 }
748
749 if (!has_actions) return DI_NOEFFECT;
750
751 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
752 }
753
754 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
755 LPDIACTIONFORMATA lpdiaf,
756 LPCSTR lpszUserName,
757 DWORD dwFlags)
758 {
759 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
760 DIACTIONFORMATW diafW;
761 HRESULT hr;
762 WCHAR *lpszUserNameW = NULL;
763 int username_size;
764
765 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
766 _copy_diactionformatAtoW(&diafW, lpdiaf);
767
768 if (lpszUserName != NULL)
769 {
770 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
771 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
772 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
773 }
774
775 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
776
777 _copy_diactionformatWtoA(lpdiaf, &diafW);
778 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
779 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
780
781 return hr;
782 }
783
784 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
785 LPDIACTIONFORMATW lpdiaf,
786 LPCWSTR lpszUserName,
787 DWORD dwFlags)
788 {
789 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
790
791 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
792
793 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
794 }
795
796 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
797 LPDIACTIONFORMATA lpdiaf,
798 LPCSTR lpszUserName,
799 DWORD dwFlags)
800 {
801 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
802 DIACTIONFORMATW diafW;
803 HRESULT hr;
804 WCHAR *lpszUserNameW = NULL;
805 int username_size;
806
807 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
808 _copy_diactionformatAtoW(&diafW, lpdiaf);
809
810 if (lpszUserName != NULL)
811 {
812 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
813 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
814 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
815 }
816
817 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
818
819 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
820 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
821
822 return hr;
823 }
824
825 /*
826 * This maps the read value (from the input event) to a value in the
827 * 'wanted' range.
828 * Notes:
829 * Dead zone is in % multiplied by a 100 (range 0..10000)
830 */
831 LONG joystick_map_axis(ObjProps *props, int val)
832 {
833 LONG ret;
834 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
835 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
836
837 /* Center input */
838 val -= (props->lDevMin + props->lDevMax) / 2;
839
840 /* Remove dead zone */
841 if (abs( val ) <= dead_zone / 2)
842 val = 0;
843 else
844 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
845
846 /* Scale and map the value from the device range into the required range */
847 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
848 (props->lMin + props->lMax) / 2;
849
850 /* Clamp in case or rounding errors */
851 if (ret > props->lMax) ret = props->lMax;
852 else if (ret < props->lMin) ret = props->lMin;
853
854 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
855 props->lDevMin, dead_zone, props->lDevMax,
856 props->lMin, props->lDeadZone, props->lMax,
857 val, ret );
858
859 return ret;
860 }
861
862 /*
863 * Maps POV x & y event values to a DX "clock" position:
864 * 0
865 * 31500 4500
866 * 27000 -1 9000
867 * 22500 13500
868 * 18000
869 */
870 DWORD joystick_map_pov(const POINTL *p)
871 {
872 if (p->x > 0)
873 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
874 else if (p->x < 0)
875 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
876 else
877 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
878 }
879
880 /*
881 * Setup the dinput options.
882 */
883
884 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
885 {
886 char buffer[MAX_PATH+16];
887 HKEY hkey, appkey;
888 int tokens = 0;
889 int axis = 0;
890 int pov = 0;
891
892 get_app_key(&hkey, &appkey);
893
894 /* get options */
895
896 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
897 {
898 This->deadzone = atoi(buffer);
899 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
900 }
901
902 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
903 if (!This->axis_map) return DIERR_OUTOFMEMORY;
904
905 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
906 {
907 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
908 "Slider1", "Slider2",
909 "POV1", "POV2", "POV3", "POV4"};
910 const char *delim = ",";
911 char * ptr;
912 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
913
914 if ((ptr = strtok(buffer, delim)) != NULL)
915 {
916 do
917 {
918 int i;
919
920 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
921 {
922 if (!strcmp(ptr, axis_names[i]))
923 {
924 if (!strncmp(ptr, "POV", 3))
925 {
926 if (pov >= 4)
927 {
928 WARN("Only 4 POVs supported - ignoring extra\n");
929 i = -1;
930 }
931 else
932 {
933 /* Pov takes two axes */
934 This->axis_map[tokens++] = i;
935 pov++;
936 }
937 }
938 else
939 {
940 if (axis >= 8)
941 {
942 FIXME("Only 8 Axes supported - ignoring extra\n");
943 i = -1;
944 }
945 else
946 axis++;
947 }
948 break;
949 }
950 }
951
952 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
953 {
954 ERR("invalid joystick axis type: \"%s\"\n", ptr);
955 i = -1;
956 }
957
958 This->axis_map[tokens] = i;
959 tokens++;
960 } while ((ptr = strtok(NULL, delim)) != NULL);
961
962 if (tokens != This->device_axis_count)
963 {
964 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
965 This->device_axis_count, axis, pov, tokens);
966 while (tokens < This->device_axis_count)
967 {
968 This->axis_map[tokens] = -1;
969 tokens++;
970 }
971 }
972 }
973 }
974 else
975 {
976 int i;
977
978 if (default_axis_map)
979 {
980 /* Use default mapping from the driver */
981 for (i = 0; i < This->device_axis_count; i++)
982 {
983 This->axis_map[i] = default_axis_map[i];
984 tokens = default_axis_map[i];
985 if (tokens < 0)
986 continue;
987 if (tokens < 8)
988 axis++;
989 else if (tokens < 15)
990 {
991 i++;
992 pov++;
993 This->axis_map[i] = default_axis_map[i];
994 }
995 }
996 }
997 else
998 {
999 /* No config - set default mapping. */
1000 for (i = 0; i < This->device_axis_count; i++)
1001 {
1002 if (i < 8)
1003 This->axis_map[i] = axis++;
1004 else if (i < 15)
1005 {
1006 This->axis_map[i++] = 8 + pov;
1007 This->axis_map[i ] = 8 + pov++;
1008 }
1009 else
1010 This->axis_map[i] = -1;
1011 }
1012 }
1013 }
1014 This->devcaps.dwAxes = axis;
1015 This->devcaps.dwPOVs = pov;
1016
1017 if (appkey) RegCloseKey(appkey);
1018 if (hkey) RegCloseKey(hkey);
1019
1020 return DI_OK;
1021 }