shell32: Initialize fAnyOperationsAborted in SHFileOperation.
[wine.git] / dlls / dinput / joystick.c
blobd32acec1997cbd175a5d6c160e64e7b84b089723
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;
98 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
100 if (ph == NULL) {
101 WARN("invalid parameter: ph == NULL\n");
102 return DIERR_INVALIDPARAM;
105 if (TRACE_ON(dinput))
106 _dump_DIPROPHEADER(ph);
108 if (IS_DIPROP(rguid)) {
109 switch (LOWORD(rguid)) {
110 case (DWORD_PTR)DIPROP_RANGE: {
111 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
112 if (ph->dwHow == DIPH_DEVICE) {
113 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
114 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
115 This->props[i].lMin = pr->lMin;
116 This->props[i].lMax = pr->lMax;
118 } else {
119 int obj = find_property(&This->base.data_format, ph);
121 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
122 if (obj >= 0) {
123 This->props[obj].lMin = pr->lMin;
124 This->props[obj].lMax = pr->lMax;
125 return DI_OK;
128 break;
130 case (DWORD_PTR)DIPROP_DEADZONE: {
131 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
132 if (ph->dwHow == DIPH_DEVICE) {
133 TRACE("deadzone(%d) all\n", pd->dwData);
134 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
135 This->props[i].lDeadZone = pd->dwData;
136 } else {
137 int obj = find_property(&This->base.data_format, ph);
139 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
140 if (obj >= 0) {
141 This->props[obj].lDeadZone = pd->dwData;
142 return DI_OK;
145 break;
147 case (DWORD_PTR)DIPROP_SATURATION: {
148 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
149 if (ph->dwHow == DIPH_DEVICE) {
150 TRACE("saturation(%d) all\n", pd->dwData);
151 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
152 This->props[i].lSaturation = pd->dwData;
153 } else {
154 int obj = find_property(&This->base.data_format, ph);
156 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
157 if (obj >= 0) {
158 This->props[obj].lSaturation = pd->dwData;
159 return DI_OK;
162 break;
164 default:
165 return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
169 return DI_OK;
172 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
174 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
175 return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
178 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
180 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
181 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
182 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
183 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
184 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
185 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
186 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
187 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
188 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
189 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
190 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
191 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
192 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
193 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
194 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
195 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
196 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
197 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
201 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
203 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
204 int size;
206 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
208 if (lpDIDevCaps == NULL) {
209 WARN("invalid pointer\n");
210 return E_POINTER;
213 size = lpDIDevCaps->dwSize;
215 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
216 WARN("invalid parameter\n");
217 return DIERR_INVALIDPARAM;
220 CopyMemory(lpDIDevCaps, &This->devcaps, size);
221 lpDIDevCaps->dwSize = size;
223 if (TRACE_ON(dinput))
224 _dump_DIDEVCAPS(lpDIDevCaps);
226 return DI_OK;
229 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
231 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
232 return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
235 /******************************************************************************
236 * GetObjectInfo : get object info
238 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
239 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
241 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
242 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
243 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
244 HRESULT res;
246 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
247 if (res != DI_OK) return res;
249 if (pdidoi->dwType & DIDFT_AXIS)
250 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
251 else if (pdidoi->dwType & DIDFT_POV)
252 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
253 else if (pdidoi->dwType & DIDFT_BUTTON)
254 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
256 _dump_OBJECTINSTANCEW(pdidoi);
257 return res;
260 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
261 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
263 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
264 HRESULT res;
265 DIDEVICEOBJECTINSTANCEW didoiW;
266 DWORD dwSize = pdidoi->dwSize;
268 didoiW.dwSize = sizeof(didoiW);
269 res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
270 if (res != DI_OK) return res;
272 memset(pdidoi, 0, pdidoi->dwSize);
273 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
274 pdidoi->dwSize = dwSize;
275 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
276 sizeof(pdidoi->tszName), NULL, NULL);
278 return res;
281 /******************************************************************************
282 * GetProperty : get input device properties
284 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
286 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
288 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
290 if (TRACE_ON(dinput))
291 _dump_DIPROPHEADER(pdiph);
293 if (IS_DIPROP(rguid)) {
294 switch (LOWORD(rguid)) {
295 case (DWORD_PTR) DIPROP_RANGE: {
296 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
297 int obj = find_property(&This->base.data_format, pdiph);
299 /* The app is querying the current range of the axis
300 * return the lMin and lMax values */
301 if (obj >= 0) {
302 pr->lMin = This->props[obj].lMin;
303 pr->lMax = This->props[obj].lMax;
304 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
305 return DI_OK;
307 break;
309 case (DWORD_PTR) DIPROP_DEADZONE: {
310 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
311 int obj = find_property(&This->base.data_format, pdiph);
313 if (obj >= 0) {
314 pd->dwData = This->props[obj].lDeadZone;
315 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
316 return DI_OK;
318 break;
320 case (DWORD_PTR) DIPROP_SATURATION: {
321 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
322 int obj = find_property(&This->base.data_format, pdiph);
324 if (obj >= 0) {
325 pd->dwData = This->props[obj].lSaturation;
326 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
327 return DI_OK;
329 break;
331 case (DWORD_PTR) DIPROP_INSTANCENAME: {
332 DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
333 DIDEVICEINSTANCEW didev;
335 didev.dwSize = sizeof(didev);
337 IDirectInputDevice_GetDeviceInfo(iface, &didev);
338 lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
340 return DI_OK;
342 default:
343 return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
347 return DI_OK;
350 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
352 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
353 return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
356 /******************************************************************************
357 * GetDeviceInfo : get information about a device's identity
359 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
360 LPDIRECTINPUTDEVICE8A iface,
361 LPDIDEVICEINSTANCEA pdidi)
363 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
365 TRACE("(%p,%p)\n", iface, pdidi);
367 if (pdidi == NULL) {
368 WARN("invalid pointer\n");
369 return E_POINTER;
372 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
373 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
374 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
375 return DIERR_INVALIDPARAM;
378 /* Return joystick */
379 pdidi->guidInstance = This->guidInstance;
380 pdidi->guidProduct = This->guidProduct;
381 /* we only support traditional joysticks for now */
382 pdidi->dwDevType = This->devcaps.dwDevType;
383 strcpy(pdidi->tszInstanceName, "Joystick");
384 strcpy(pdidi->tszProductName, This->name);
385 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
386 pdidi->guidFFDriver = GUID_NULL;
387 pdidi->wUsagePage = 0;
388 pdidi->wUsage = 0;
391 return DI_OK;
394 /******************************************************************************
395 * GetDeviceInfo : get information about a device's identity
397 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
398 LPDIRECTINPUTDEVICE8W iface,
399 LPDIDEVICEINSTANCEW pdidi)
401 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
403 TRACE("(%p,%p)\n", iface, pdidi);
405 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
406 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
407 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
408 return DIERR_INVALIDPARAM;
411 /* Return joystick */
412 pdidi->guidInstance = This->guidInstance;
413 pdidi->guidProduct = This->guidProduct;
414 /* we only support traditional joysticks for now */
415 pdidi->dwDevType = This->devcaps.dwDevType;
416 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
417 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
418 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
419 pdidi->guidFFDriver = GUID_NULL;
420 pdidi->wUsagePage = 0;
421 pdidi->wUsage = 0;
424 return DI_OK;
427 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
429 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
431 TRACE("(%p)\n",This);
433 if (!This->base.acquired) {
434 WARN("not acquired\n");
435 return DIERR_NOTACQUIRED;
438 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
439 return DI_OK;
442 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
444 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
445 return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
448 /******************************************************************************
449 * GetDeviceState : returns the "state" of the joystick.
452 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
454 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
456 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
458 if (!This->base.acquired) {
459 WARN("not acquired\n");
460 return DIERR_NOTACQUIRED;
463 /* update joystick state */
464 This->joy_polldev(IDirectInputDevice8A_from_impl(This));
466 /* convert and copy data to user supplied buffer */
467 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
469 return DI_OK;
472 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
474 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
475 return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
479 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
480 LPDIACTIONFORMATW lpdiaf,
481 LPCWSTR lpszUserName,
482 DWORD dwFlags)
484 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
485 int i, j, has_actions = 0;
486 DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
487 DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
489 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
491 for (i=0; i < lpdiaf->dwNumActions; i++)
493 DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
494 DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
495 DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
497 /* Don't touch an user configured action */
498 if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
500 /* Only consider actions of the right genre */
501 if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
503 for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
505 if (type & object_types[j])
507 /* Assure that the object exists */
508 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
510 if (odf != NULL)
512 lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
513 lpdiaf->rgoAction[i].guidInstance = This->base.guid;
514 lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
516 has_actions = 1;
518 /* No need to try other types if the action was already mapped */
519 break;
525 if (!has_actions) return DI_NOEFFECT;
527 return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
530 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
531 LPDIACTIONFORMATA lpdiaf,
532 LPCSTR lpszUserName,
533 DWORD dwFlags)
535 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
536 DIACTIONFORMATW diafW;
537 HRESULT hr;
538 WCHAR *lpszUserNameW = NULL;
539 int username_size;
541 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
542 _copy_diactionformatAtoW(&diafW, lpdiaf);
544 if (lpszUserName != NULL)
546 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
547 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
548 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
551 hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
553 _copy_diactionformatWtoA(lpdiaf, &diafW);
554 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
555 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
557 return hr;
560 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
561 LPDIACTIONFORMATW lpdiaf,
562 LPCWSTR lpszUserName,
563 DWORD dwFlags)
565 JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
567 FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
569 return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
572 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
573 LPDIACTIONFORMATA lpdiaf,
574 LPCSTR lpszUserName,
575 DWORD dwFlags)
577 JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
578 DIACTIONFORMATW diafW;
579 HRESULT hr;
580 WCHAR *lpszUserNameW = NULL;
581 int username_size;
583 diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
584 _copy_diactionformatAtoW(&diafW, lpdiaf);
586 if (lpszUserName != NULL)
588 username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
589 lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
590 MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
593 hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
595 HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
596 HeapFree(GetProcessHeap(), 0, lpszUserNameW);
598 return hr;
602 * This maps the read value (from the input event) to a value in the
603 * 'wanted' range.
604 * Notes:
605 * Dead zone is in % multiplied by a 100 (range 0..10000)
607 LONG joystick_map_axis(ObjProps *props, int val)
609 LONG ret;
610 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
611 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
613 /* Center input */
614 val -= (props->lDevMin + props->lDevMax) / 2;
616 /* Remove dead zone */
617 if (abs( val ) <= dead_zone / 2)
618 val = 0;
619 else
620 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
622 /* Scale and map the value from the device range into the required range */
623 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
624 (props->lMin + props->lMax) / 2;
626 /* Clamp in case or rounding errors */
627 if (ret > props->lMax) ret = props->lMax;
628 else if (ret < props->lMin) ret = props->lMin;
630 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
631 props->lDevMin, dead_zone, props->lDevMax,
632 props->lMin, props->lDeadZone, props->lMax,
633 val, ret );
635 return ret;
639 * Maps POV x & y event values to a DX "clock" position:
641 * 31500 4500
642 * 27000 -1 9000
643 * 22500 13500
644 * 18000
646 DWORD joystick_map_pov(const POINTL *p)
648 if (p->x > 0)
649 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
650 else if (p->x < 0)
651 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
652 else
653 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
657 * Setup the dinput options.
660 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
662 char buffer[MAX_PATH+16];
663 HKEY hkey, appkey;
664 int tokens = 0;
665 int axis = 0;
666 int pov = 0;
668 get_app_key(&hkey, &appkey);
670 /* get options */
672 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
674 This->deadzone = atoi(buffer);
675 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
678 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
679 if (!This->axis_map) return DIERR_OUTOFMEMORY;
681 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
683 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
684 "Slider1", "Slider2",
685 "POV1", "POV2", "POV3", "POV4"};
686 const char *delim = ",";
687 char * ptr;
688 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
690 if ((ptr = strtok(buffer, delim)) != NULL)
694 int i;
696 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
698 if (!strcmp(ptr, axis_names[i]))
700 if (!strncmp(ptr, "POV", 3))
702 if (pov >= 4)
704 WARN("Only 4 POVs supported - ignoring extra\n");
705 i = -1;
707 else
709 /* Pov takes two axes */
710 This->axis_map[tokens++] = i;
711 pov++;
714 else
716 if (axis >= 8)
718 FIXME("Only 8 Axes supported - ignoring extra\n");
719 i = -1;
721 else
722 axis++;
724 break;
728 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
730 ERR("invalid joystick axis type: \"%s\"\n", ptr);
731 i = -1;
734 This->axis_map[tokens] = i;
735 tokens++;
736 } while ((ptr = strtok(NULL, delim)) != NULL);
738 if (tokens != This->device_axis_count)
740 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
741 This->device_axis_count, axis, pov, tokens);
742 while (tokens < This->device_axis_count)
744 This->axis_map[tokens] = -1;
745 tokens++;
750 else
752 int i;
754 if (default_axis_map)
756 /* Use default mapping from the driver */
757 for (i = 0; i < This->device_axis_count; i++)
759 This->axis_map[i] = default_axis_map[i];
760 tokens = default_axis_map[i];
761 if (tokens < 0)
762 continue;
763 if (tokens < 8)
764 axis++;
765 else if (tokens < 15)
767 i++;
768 pov++;
769 This->axis_map[i] = default_axis_map[i];
773 else
775 /* No config - set default mapping. */
776 for (i = 0; i < This->device_axis_count; i++)
778 if (i < 8)
779 This->axis_map[i] = axis++;
780 else if (i < 15)
782 This->axis_map[i++] = 8 + pov;
783 This->axis_map[i ] = 8 + pov++;
785 else
786 This->axis_map[i] = -1;
790 This->devcaps.dwAxes = axis;
791 This->devcaps.dwPOVs = pov;
793 if (appkey) RegCloseKey(appkey);
794 if (hkey) RegCloseKey(hkey);
796 return DI_OK;