dinput: Update existing joystick values after setting the range property.
[wine.git] / dlls / dinput / joystick.c
blob7da8aac84eff867f1139994f4bceeecfef50e96d
1 /* DirectInput Generic Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2009 Aric Stewart, CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 * To Do:
25 * dead zone
26 * force feedback
29 #include "joystick_private.h"
30 #include "wine/debug.h"
31 #include "winreg.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
35 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
37 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface), JoystickGenericImpl, base);
39 static inline JoystickGenericImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
41 return CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface), JoystickGenericImpl, base);
43 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickGenericImpl *This)
45 return &This->base.IDirectInputDevice8A_iface;
47 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickGenericImpl *This)
49 return &This->base.IDirectInputDevice8W_iface;
52 BOOL device_disabled_registry(const char* name)
54 static const char disabled_str[] = "disabled";
55 static const char joystick_key[] = "Joysticks";
56 char buffer[MAX_PATH];
57 HKEY hkey, appkey, temp;
58 BOOL do_disable = FALSE;
60 get_app_key(&hkey, &appkey);
62 /* Joystick settings are in the 'joysticks' subkey */
63 if (appkey)
65 if (RegOpenKeyA(appkey, joystick_key, &temp)) temp = 0;
66 RegCloseKey(appkey);
67 appkey = temp;
69 if (hkey)
71 if (RegOpenKeyA(hkey, joystick_key, &temp)) temp = 0;
72 RegCloseKey(hkey);
73 hkey = temp;
76 /* Look for the "controllername"="disabled" key */
77 if (!get_config_key(hkey, appkey, name, buffer, sizeof(buffer)))
78 if (!strcmp(disabled_str, buffer))
80 TRACE("Disabling joystick '%s' based on registry key.\n", name);
81 do_disable = TRUE;
84 if (appkey) RegCloseKey(appkey);
85 if (hkey) RegCloseKey(hkey);
87 return do_disable;
90 /******************************************************************************
91 * SetProperty : change input device properties
93 HRESULT WINAPI JoystickWGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
95 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
96 DWORD i;
97 ObjProps remap_props;
99 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
101 if (ph == NULL) {
102 WARN("invalid parameter: ph == NULL\n");
103 return DIERR_INVALIDPARAM;
106 if (TRACE_ON(dinput))
107 _dump_DIPROPHEADER(ph);
109 if (IS_DIPROP(rguid)) {
110 switch (LOWORD(rguid)) {
111 case (DWORD_PTR)DIPROP_RANGE: {
112 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
113 if (ph->dwHow == DIPH_DEVICE) {
114 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
115 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
117 remap_props.lDevMin = This->props[i].lMin;
118 remap_props.lDevMax = This->props[i].lMax;
120 remap_props.lDeadZone = This->props[i].lDeadZone;
121 remap_props.lSaturation = This->props[i].lSaturation;
123 remap_props.lMin = pr->lMin;
124 remap_props.lMax = pr->lMax;
126 switch (This->base.data_format.offsets[i]) {
127 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
128 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
129 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
130 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
131 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
132 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
133 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
134 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
135 default: break;
138 This->props[i].lMin = pr->lMin;
139 This->props[i].lMax = pr->lMax;
141 } else {
142 int obj = find_property(&This->base.data_format, ph);
144 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
145 if (obj >= 0) {
147 /*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*/
149 remap_props.lDevMin = This->props[obj].lMin;
150 remap_props.lDevMax = This->props[obj].lMax;
152 remap_props.lDeadZone = This->props[obj].lDeadZone;
153 remap_props.lSaturation = This->props[obj].lSaturation;
155 remap_props.lMin = pr->lMin;
156 remap_props.lMax = pr->lMax;
158 switch (ph->dwObj) {
159 case DIJOFS_X : This->js.lX = joystick_map_axis(&remap_props, This->js.lX); break;
160 case DIJOFS_Y : This->js.lY = joystick_map_axis(&remap_props, This->js.lY); break;
161 case DIJOFS_Z : This->js.lZ = joystick_map_axis(&remap_props, This->js.lZ); break;
162 case DIJOFS_RX : This->js.lRx = joystick_map_axis(&remap_props, This->js.lRx); break;
163 case DIJOFS_RY : This->js.lRy = joystick_map_axis(&remap_props, This->js.lRy); break;
164 case DIJOFS_RZ : This->js.lRz = joystick_map_axis(&remap_props, This->js.lRz); break;
165 case DIJOFS_SLIDER(0): This->js.rglSlider[0] = joystick_map_axis(&remap_props, This->js.rglSlider[0]); break;
166 case DIJOFS_SLIDER(1): This->js.rglSlider[1] = joystick_map_axis(&remap_props, This->js.rglSlider[1]); break;
167 default: break;
170 This->props[obj].lMin = pr->lMin;
171 This->props[obj].lMax = pr->lMax;
172 return DI_OK;
175 break;
177 case (DWORD_PTR)DIPROP_DEADZONE: {
178 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
179 if (ph->dwHow == DIPH_DEVICE) {
180 TRACE("deadzone(%d) all\n", pd->dwData);
181 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
182 This->props[i].lDeadZone = pd->dwData;
183 } else {
184 int obj = find_property(&This->base.data_format, ph);
186 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
187 if (obj >= 0) {
188 This->props[obj].lDeadZone = pd->dwData;
189 return DI_OK;
192 break;
194 case (DWORD_PTR)DIPROP_SATURATION: {
195 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
196 if (ph->dwHow == DIPH_DEVICE) {
197 TRACE("saturation(%d) all\n", pd->dwData);
198 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
199 This->props[i].lSaturation = pd->dwData;
200 } else {
201 int obj = find_property(&This->base.data_format, ph);
203 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
204 if (obj >= 0) {
205 This->props[obj].lSaturation = pd->dwData;
206 return DI_OK;
209 break;
211 default:
212 return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
216 return DI_OK;
219 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
221 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
222 return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
225 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
227 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
228 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
229 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
230 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
231 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
232 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
233 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
234 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
235 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
236 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
237 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
238 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
239 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
240 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
241 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
242 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
243 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
244 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
248 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
250 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
251 int size;
253 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
255 if (lpDIDevCaps == NULL) {
256 WARN("invalid pointer\n");
257 return E_POINTER;
260 size = lpDIDevCaps->dwSize;
262 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
263 WARN("invalid parameter\n");
264 return DIERR_INVALIDPARAM;
267 CopyMemory(lpDIDevCaps, &This->devcaps, size);
268 lpDIDevCaps->dwSize = size;
270 if (TRACE_ON(dinput))
271 _dump_DIDEVCAPS(lpDIDevCaps);
273 return DI_OK;
276 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
278 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
279 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
282 /******************************************************************************
283 * GetObjectInfo : get object info
285 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
286 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
288 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
289 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
290 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
291 HRESULT res;
293 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
294 if (res != DI_OK) return res;
296 if (pdidoi->dwType & DIDFT_AXIS)
297 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
298 else if (pdidoi->dwType & DIDFT_POV)
299 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
300 else if (pdidoi->dwType & DIDFT_BUTTON)
301 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
303 _dump_OBJECTINSTANCEW(pdidoi);
304 return res;
307 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
308 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
310 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
311 HRESULT res;
312 DIDEVICEOBJECTINSTANCEW didoiW;
313 DWORD dwSize = pdidoi->dwSize;
315 didoiW.dwSize = sizeof(didoiW);
316 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
317 if (res != DI_OK) return res;
319 memset(pdidoi, 0, pdidoi->dwSize);
320 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
321 pdidoi->dwSize = dwSize;
322 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
323 sizeof(pdidoi->tszName), NULL, NULL);
325 return res;
328 /******************************************************************************
329 * GetProperty : get input device properties
331 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
333 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
335 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
337 if (TRACE_ON(dinput))
338 _dump_DIPROPHEADER(pdiph);
340 if (IS_DIPROP(rguid)) {
341 switch (LOWORD(rguid)) {
342 case (DWORD_PTR) DIPROP_RANGE: {
343 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
344 int obj = find_property(&This->base.data_format, pdiph);
346 /* The app is querying the current range of the axis
347 * return the lMin and lMax values */
348 if (obj >= 0) {
349 pr->lMin = This->props[obj].lMin;
350 pr->lMax = This->props[obj].lMax;
351 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
352 return DI_OK;
354 break;
356 case (DWORD_PTR) DIPROP_DEADZONE: {
357 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
358 int obj = find_property(&This->base.data_format, pdiph);
360 if (obj >= 0) {
361 pd->dwData = This->props[obj].lDeadZone;
362 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
363 return DI_OK;
365 break;
367 case (DWORD_PTR) DIPROP_SATURATION: {
368 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
369 int obj = find_property(&This->base.data_format, pdiph);
371 if (obj >= 0) {
372 pd->dwData = This->props[obj].lSaturation;
373 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
374 return DI_OK;
376 break;
378 case (DWORD_PTR) DIPROP_INSTANCENAME: {
379 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
380 DIDEVICEINSTANCEW didev;
382 didev.dwSize = sizeof(didev);
384 IDirectInputDevice_GetDeviceInfo(iface, &didev);
385 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
387 return DI_OK;
389 default:
390 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
394 return DI_OK;
397 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
399 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
400 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
403 /******************************************************************************
404 * GetDeviceInfo : get information about a device's identity
406 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
407 LPDIRECTINPUTDEVICE8A iface,
408 LPDIDEVICEINSTANCEA pdidi)
410 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
412 TRACE("(%p,%p)\n", iface, pdidi);
414 if (pdidi == NULL) {
415 WARN("invalid pointer\n");
416 return E_POINTER;
419 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
420 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
421 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
422 return DIERR_INVALIDPARAM;
425 /* Return joystick */
426 pdidi->guidInstance = This->guidInstance;
427 pdidi->guidProduct = This->guidProduct;
428 /* we only support traditional joysticks for now */
429 pdidi->dwDevType = This->devcaps.dwDevType;
430 strcpy(pdidi->tszInstanceName, "Joystick");
431 strcpy(pdidi->tszProductName, This->name);
432 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
433 pdidi->guidFFDriver = GUID_NULL;
434 pdidi->wUsagePage = 0;
435 pdidi->wUsage = 0;
438 return DI_OK;
441 /******************************************************************************
442 * GetDeviceInfo : get information about a device's identity
444 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
445 LPDIRECTINPUTDEVICE8W iface,
446 LPDIDEVICEINSTANCEW pdidi)
448 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
450 TRACE("(%p,%p)\n", iface, pdidi);
452 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
453 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
454 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
455 return DIERR_INVALIDPARAM;
458 /* Return joystick */
459 pdidi->guidInstance = This->guidInstance;
460 pdidi->guidProduct = This->guidProduct;
461 /* we only support traditional joysticks for now */
462 pdidi->dwDevType = This->devcaps.dwDevType;
463 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
464 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
465 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
466 pdidi->guidFFDriver = GUID_NULL;
467 pdidi->wUsagePage = 0;
468 pdidi->wUsage = 0;
471 return DI_OK;
474 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
476 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
478 TRACE("(%p)\n",This);
480 if (!This->base.acquired) {
481 WARN("not acquired\n");
482 return DIERR_NOTACQUIRED;
485 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
486 return DI_OK;
489 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
491 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
492 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
495 /******************************************************************************
496 * GetDeviceState : returns the "state" of the joystick.
499 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
501 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
503 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
505 if (!This->base.acquired) {
506 WARN("not acquired\n");
507 return DIERR_NOTACQUIRED;
510 /* update joystick state */
511 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
513 /* convert and copy data to user supplied buffer */
514 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
516 return DI_OK;
519 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
521 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
522 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
526 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
527 LPDIACTIONFORMATW lpdiaf,
528 LPCWSTR lpszUserName,
529 DWORD dwFlags)
531 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
532 unsigned int i, j;
533 int has_actions = 0;
534 DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
535 DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
537 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
539 for (i=0; i < lpdiaf->dwNumActions; i++)
541 DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
542 DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
543 DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
545 /* Don't touch an user configured action */
546 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
548 /* Only consider actions of the right genre */
549 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
551 for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
553 if (type & object_types[j])
555 /* Assure that the object exists */
556 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
558 if (odf != NULL)
560 lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
561 lpdiaf->rgoAction[i].guidInstance = This->base.guid;
562 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
564 has_actions = 1;
566 /* No need to try other types if the action was already mapped */
567 break;
573 if (!has_actions) return DI_NOEFFECT;
575 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
578 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
579 LPDIACTIONFORMATA lpdiaf,
580 LPCSTR lpszUserName,
581 DWORD dwFlags)
583 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
584 DIACTIONFORMATW diafW;
585 HRESULT hr;
586 WCHAR *lpszUserNameW = NULL;
587 int username_size;
589 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
590 _copy_diactionformatAtoW(&diafW, lpdiaf);
592 if (lpszUserName != NULL)
594 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
595 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
596 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
599 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
601 _copy_diactionformatWtoA(lpdiaf, &diafW);
602 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
603 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
605 return hr;
608 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
609 LPDIACTIONFORMATW lpdiaf,
610 LPCWSTR lpszUserName,
611 DWORD dwFlags)
613 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
615 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
617 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
620 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
621 LPDIACTIONFORMATA lpdiaf,
622 LPCSTR lpszUserName,
623 DWORD dwFlags)
625 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
626 DIACTIONFORMATW diafW;
627 HRESULT hr;
628 WCHAR *lpszUserNameW = NULL;
629 int username_size;
631 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
632 _copy_diactionformatAtoW(&diafW, lpdiaf);
634 if (lpszUserName != NULL)
636 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
637 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
638 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
641 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
643 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
644 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
646 return hr;
650 * This maps the read value (from the input event) to a value in the
651 * 'wanted' range.
652 * Notes:
653 * Dead zone is in % multiplied by a 100 (range 0..10000)
655 LONG joystick_map_axis(ObjProps *props, int val)
657 LONG ret;
658 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
659 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
661 /* Center input */
662 val -= (props->lDevMin + props->lDevMax) / 2;
664 /* Remove dead zone */
665 if (abs( val ) <= dead_zone / 2)
666 val = 0;
667 else
668 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
670 /* Scale and map the value from the device range into the required range */
671 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
672 (props->lMin + props->lMax) / 2;
674 /* Clamp in case or rounding errors */
675 if (ret > props->lMax) ret = props->lMax;
676 else if (ret < props->lMin) ret = props->lMin;
678 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
679 props->lDevMin, dead_zone, props->lDevMax,
680 props->lMin, props->lDeadZone, props->lMax,
681 val, ret );
683 return ret;
687 * Maps POV x & y event values to a DX "clock" position:
689 * 31500 4500
690 * 27000 -1 9000
691 * 22500 13500
692 * 18000
694 DWORD joystick_map_pov(const POINTL *p)
696 if (p->x > 0)
697 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
698 else if (p->x < 0)
699 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
700 else
701 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
705 * Setup the dinput options.
708 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
710 char buffer[MAX_PATH+16];
711 HKEY hkey, appkey;
712 int tokens = 0;
713 int axis = 0;
714 int pov = 0;
716 get_app_key(&hkey, &appkey);
718 /* get options */
720 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
722 This->deadzone = atoi(buffer);
723 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
726 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
727 if (!This->axis_map) return DIERR_OUTOFMEMORY;
729 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
731 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
732 "Slider1", "Slider2",
733 "POV1", "POV2", "POV3", "POV4"};
734 const char *delim = ",";
735 char * ptr;
736 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
738 if ((ptr = strtok(buffer, delim)) != NULL)
742 int i;
744 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
746 if (!strcmp(ptr, axis_names[i]))
748 if (!strncmp(ptr, "POV", 3))
750 if (pov >= 4)
752 WARN("Only 4 POVs supported - ignoring extra\n");
753 i = -1;
755 else
757 /* Pov takes two axes */
758 This->axis_map[tokens++] = i;
759 pov++;
762 else
764 if (axis >= 8)
766 FIXME("Only 8 Axes supported - ignoring extra\n");
767 i = -1;
769 else
770 axis++;
772 break;
776 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
778 ERR("invalid joystick axis type: \"%s\"\n", ptr);
779 i = -1;
782 This->axis_map[tokens] = i;
783 tokens++;
784 } while ((ptr = strtok(NULL, delim)) != NULL);
786 if (tokens != This->device_axis_count)
788 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
789 This->device_axis_count, axis, pov, tokens);
790 while (tokens < This->device_axis_count)
792 This->axis_map[tokens] = -1;
793 tokens++;
798 else
800 int i;
802 if (default_axis_map)
804 /* Use default mapping from the driver */
805 for (i = 0; i < This->device_axis_count; i++)
807 This->axis_map[i] = default_axis_map[i];
808 tokens = default_axis_map[i];
809 if (tokens < 0)
810 continue;
811 if (tokens < 8)
812 axis++;
813 else if (tokens < 15)
815 i++;
816 pov++;
817 This->axis_map[i] = default_axis_map[i];
821 else
823 /* No config - set default mapping. */
824 for (i = 0; i < This->device_axis_count; i++)
826 if (i < 8)
827 This->axis_map[i] = axis++;
828 else if (i < 15)
830 This->axis_map[i++] = 8 + pov;
831 This->axis_map[i ] = 8 + pov++;
833 else
834 This->axis_map[i] = -1;
838 This->devcaps.dwAxes = axis;
839 This->devcaps.dwPOVs = pov;
841 if (appkey) RegCloseKey(appkey);
842 if (hkey) RegCloseKey(hkey);
844 return DI_OK;