Assorted spelling fixes.
[wine] / dlls / 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 BOOL device_disabled_registry(const char* name)
53 {
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;
59
60     get_app_key(&hkey, &appkey);
61
62     /* Joystick settings are in the 'joysticks' subkey */
63     if (appkey)
64     {
65         if (RegOpenKeyA(appkey, joystick_key, &temp)) temp = 0;
66         RegCloseKey(appkey);
67         appkey = temp;
68     }
69     if (hkey)
70     {
71         if (RegOpenKeyA(hkey, joystick_key, &temp)) temp = 0;
72         RegCloseKey(hkey);
73         hkey = temp;
74     }
75
76     /* Look for the "controllername"="disabled" key */
77     if (!get_config_key(hkey, appkey, name, buffer, sizeof(buffer)))
78         if (!strcmp(disabled_str, buffer))
79         {
80             TRACE("Disabling joystick '%s' based on registry key.\n", name);
81             do_disable = TRUE;
82         }
83
84     if (appkey) RegCloseKey(appkey);
85     if (hkey)   RegCloseKey(hkey);
86
87     return do_disable;
88 }
89
90 /******************************************************************************
91   *     SetProperty : change input device properties
92   */
93 HRESULT WINAPI JoystickWGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPCDIPROPHEADER ph)
94 {
95     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
96     DWORD i;
97
98     TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
99
100     if (ph == NULL) {
101         WARN("invalid parameter: ph == NULL\n");
102         return DIERR_INVALIDPARAM;
103     }
104
105     if (TRACE_ON(dinput))
106         _dump_DIPROPHEADER(ph);
107
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;
117                 }
118             } else {
119                 int obj = find_property(&This->base.data_format, ph);
120
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;
126                 }
127             }
128             break;
129         }
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);
138
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;
143                 }
144             }
145             break;
146         }
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);
155
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;
160                 }
161             }
162             break;
163         }
164         default:
165             return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
166         }
167     }
168
169     return DI_OK;
170 }
171
172 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
173 {
174     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
175     return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
176 }
177
178 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
179 {
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);
198     }
199 }
200
201 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
202 {
203     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
204     int size;
205
206     TRACE("%p->(%p)\n",iface,lpDIDevCaps);
207
208     if (lpDIDevCaps == NULL) {
209         WARN("invalid pointer\n");
210         return E_POINTER;
211     }
212
213     size = lpDIDevCaps->dwSize;
214
215     if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
216         WARN("invalid parameter\n");
217         return DIERR_INVALIDPARAM;
218     }
219
220     CopyMemory(lpDIDevCaps, &This->devcaps, size);
221     lpDIDevCaps->dwSize = size;
222
223     if (TRACE_ON(dinput))
224         _dump_DIDEVCAPS(lpDIDevCaps);
225
226     return DI_OK;
227 }
228
229 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
230 {
231     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
232     return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
233 }
234
235 /******************************************************************************
236   *     GetObjectInfo : get object info
237   */
238 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
239         LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
240 {
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;
245
246     res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
247     if (res != DI_OK) return res;
248
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));
255
256     _dump_OBJECTINSTANCEW(pdidoi);
257     return res;
258 }
259
260 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
261         LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
262 {
263     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
264     HRESULT res;
265     DIDEVICEOBJECTINSTANCEW didoiW;
266     DWORD dwSize = pdidoi->dwSize;
267
268     didoiW.dwSize = sizeof(didoiW);
269     res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
270     if (res != DI_OK) return res;
271
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);
277
278     return res;
279 }
280
281 /******************************************************************************
282   *     GetProperty : get input device properties
283   */
284 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
285 {
286     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
287
288     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
289
290     if (TRACE_ON(dinput))
291         _dump_DIPROPHEADER(pdiph);
292
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);
298
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;
306             }
307             break;
308         }
309         case (DWORD_PTR) DIPROP_DEADZONE: {
310             LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
311             int obj = find_property(&This->base.data_format, pdiph);
312
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;
317             }
318             break;
319         }
320         case (DWORD_PTR) DIPROP_SATURATION: {
321             LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
322             int obj = find_property(&This->base.data_format, pdiph);
323
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;
328             }
329             break;
330         }
331         default:
332             return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
333         }
334     }
335
336     return DI_OK;
337 }
338
339 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
340 {
341     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
342     return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
343 }
344
345 /******************************************************************************
346   *     GetDeviceInfo : get information about a device's identity
347   */
348 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
349     LPDIRECTINPUTDEVICE8A iface,
350     LPDIDEVICEINSTANCEA pdidi)
351 {
352     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
353
354     TRACE("(%p,%p)\n", iface, pdidi);
355
356     if (pdidi == NULL) {
357         WARN("invalid pointer\n");
358         return E_POINTER;
359     }
360
361     if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
362         (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
363         WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
364         return DIERR_INVALIDPARAM;
365     }
366
367     /* Return joystick */
368     pdidi->guidInstance = This->guidInstance;
369     pdidi->guidProduct = This->guidProduct;
370     /* we only support traditional joysticks for now */
371     pdidi->dwDevType = This->devcaps.dwDevType;
372     strcpy(pdidi->tszInstanceName, "Joystick");
373     strcpy(pdidi->tszProductName, This->name);
374     if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
375         pdidi->guidFFDriver = GUID_NULL;
376         pdidi->wUsagePage = 0;
377         pdidi->wUsage = 0;
378     }
379
380     return DI_OK;
381 }
382
383 /******************************************************************************
384   *     GetDeviceInfo : get information about a device's identity
385   */
386 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
387     LPDIRECTINPUTDEVICE8W iface,
388     LPDIDEVICEINSTANCEW pdidi)
389 {
390     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
391
392     TRACE("(%p,%p)\n", iface, pdidi);
393
394     if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
395         (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
396         WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
397         return DIERR_INVALIDPARAM;
398     }
399
400     /* Return joystick */
401     pdidi->guidInstance = This->guidInstance;
402     pdidi->guidProduct = This->guidProduct;
403     /* we only support traditional joysticks for now */
404     pdidi->dwDevType = This->devcaps.dwDevType;
405     MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
406     MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
407     if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
408         pdidi->guidFFDriver = GUID_NULL;
409         pdidi->wUsagePage = 0;
410         pdidi->wUsage = 0;
411     }
412
413     return DI_OK;
414 }
415
416 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
417 {
418     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
419
420     TRACE("(%p)\n",This);
421
422     if (!This->base.acquired) {
423         WARN("not acquired\n");
424         return DIERR_NOTACQUIRED;
425     }
426
427     This->joy_polldev(IDirectInputDevice8A_from_impl(This));
428     return DI_OK;
429 }
430
431 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
432 {
433     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
434     return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
435 }
436
437 /******************************************************************************
438   *     GetDeviceState : returns the "state" of the joystick.
439   *
440   */
441 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
442 {
443     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
444
445     TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
446
447     if (!This->base.acquired) {
448         WARN("not acquired\n");
449         return DIERR_NOTACQUIRED;
450     }
451
452     /* update joystick state */
453     This->joy_polldev(IDirectInputDevice8A_from_impl(This));
454
455     /* convert and copy data to user supplied buffer */
456     fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
457
458     return DI_OK;
459 }
460
461 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
462 {
463     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
464     return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
465 }
466
467
468 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
469                                                    LPDIACTIONFORMATW lpdiaf,
470                                                    LPCWSTR lpszUserName,
471                                                    DWORD dwFlags)
472 {
473     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
474     int i, j, has_actions = 0;
475     DWORD object_types[] = { DIDFT_AXIS, DIDFT_BUTTON };
476     DWORD type_map[] = { DIDFT_RELAXIS, DIDFT_PSHBUTTON };
477
478     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
479
480     for (i=0; i < lpdiaf->dwNumActions; i++)
481     {
482         DWORD inst = (0x000000ff & (lpdiaf->rgoAction[i].dwSemantic)) - 1;
483         DWORD type = 0x000000ff & (lpdiaf->rgoAction[i].dwSemantic >> 8);
484         DWORD genre = 0xff000000 & lpdiaf->rgoAction[i].dwSemantic;
485
486         /* Don't touch an user configured action */
487         if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
488
489         /* Only consider actions of the right genre */
490         if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
491
492         for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
493         {
494             if (type & object_types[j])
495             {
496                 /* Assure that the object exists */
497                 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
498
499                 if (odf != NULL)
500                 {
501                     lpdiaf->rgoAction[i].dwObjID = type_map[j] | (0x0000ff00 & (inst << 8));
502                     lpdiaf->rgoAction[i].guidInstance = This->base.guid;
503                     lpdiaf->rgoAction[i].dwHow = DIAH_DEFAULT;
504
505                     has_actions = 1;
506
507                     /* No need to try other types if the action was already mapped */
508                     break;
509                 }
510             }
511         }
512     }
513
514     if (!has_actions) return DI_NOEFFECT;
515
516     return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
517 }
518
519 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
520                                                    LPDIACTIONFORMATA lpdiaf,
521                                                    LPCSTR lpszUserName,
522                                                    DWORD dwFlags)
523 {
524     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
525     DIACTIONFORMATW diafW;
526     HRESULT hr;
527     WCHAR *lpszUserNameW = NULL;
528     int username_size;
529
530     diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
531     _copy_diactionformatAtoW(&diafW, lpdiaf);
532
533     if (lpszUserName != NULL)
534     {
535         username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
536         lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
537         MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
538     }
539
540     hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
541
542     _copy_diactionformatWtoA(lpdiaf, &diafW);
543     HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
544     HeapFree(GetProcessHeap(), 0, lpszUserNameW);
545
546     return hr;
547 }
548
549 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
550                                                  LPDIACTIONFORMATW lpdiaf,
551                                                  LPCWSTR lpszUserName,
552                                                  DWORD dwFlags)
553 {
554     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
555
556     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
557
558     return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
559 }
560
561 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
562                                                  LPDIACTIONFORMATA lpdiaf,
563                                                  LPCSTR lpszUserName,
564                                                  DWORD dwFlags)
565 {
566     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
567     DIACTIONFORMATW diafW;
568     HRESULT hr;
569     WCHAR *lpszUserNameW = NULL;
570     int username_size;
571
572     diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
573     _copy_diactionformatAtoW(&diafW, lpdiaf);
574
575     if (lpszUserName != NULL)
576     {
577         username_size = MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, NULL, 0);
578         lpszUserNameW = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR)*username_size);
579         MultiByteToWideChar(CP_ACP, 0, lpszUserName, -1, lpszUserNameW, username_size);
580     }
581
582     hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
583
584     HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
585     HeapFree(GetProcessHeap(), 0, lpszUserNameW);
586
587     return hr;
588 }
589
590 /*
591  * This maps the read value (from the input event) to a value in the
592  * 'wanted' range.
593  * Notes:
594  *   Dead zone is in % multiplied by a 100 (range 0..10000)
595  */
596 LONG joystick_map_axis(ObjProps *props, int val)
597 {
598     LONG ret;
599     LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
600     LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
601
602     /* Center input */
603     val -= (props->lDevMin + props->lDevMax) / 2;
604
605     /* Remove dead zone */
606     if (abs( val ) <= dead_zone / 2)
607         val = 0;
608     else
609         val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
610
611     /* Scale and map the value from the device range into the required range */
612     ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
613           (props->lMin + props->lMax) / 2;
614
615     /* Clamp in case or rounding errors */
616     if      (ret > props->lMax) ret = props->lMax;
617     else if (ret < props->lMin) ret = props->lMin;
618
619     TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
620            props->lDevMin, dead_zone, props->lDevMax,
621            props->lMin, props->lDeadZone, props->lMax,
622            val, ret );
623
624     return ret;
625 }
626
627 /*
628  * Maps POV x & y event values to a DX "clock" position:
629  *         0
630  *   31500    4500
631  * 27000  -1    9000
632  *   22500   13500
633  *       18000
634  */
635 DWORD joystick_map_pov(const POINTL *p)
636 {
637     if (p->x > 0)
638         return p->y < 0 ?  4500 : !p->y ?  9000 : 13500;
639     else if (p->x < 0)
640         return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
641     else
642         return p->y < 0 ?     0 : !p->y ?    -1 : 18000;
643 }
644
645 /*
646  * Setup the dinput options.
647  */
648
649 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
650 {
651     char buffer[MAX_PATH+16];
652     HKEY hkey, appkey;
653     int tokens = 0;
654     int axis = 0;
655     int pov = 0;
656
657     get_app_key(&hkey, &appkey);
658
659     /* get options */
660
661     if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
662     {
663         This->deadzone = atoi(buffer);
664         TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
665     }
666
667     This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
668     if (!This->axis_map) return DIERR_OUTOFMEMORY;
669
670     if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
671     {
672         static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
673                                            "Slider1", "Slider2",
674                                            "POV1", "POV2", "POV3", "POV4"};
675         const char *delim = ",";
676         char * ptr;
677         TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
678
679         if ((ptr = strtok(buffer, delim)) != NULL)
680         {
681             do
682             {
683                 int i;
684
685                 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
686                 {
687                     if (!strcmp(ptr, axis_names[i]))
688                     {
689                         if (!strncmp(ptr, "POV", 3))
690                         {
691                             if (pov >= 4)
692                             {
693                                 WARN("Only 4 POVs supported - ignoring extra\n");
694                                 i = -1;
695                             }
696                             else
697                             {
698                                 /* Pov takes two axes */
699                                 This->axis_map[tokens++] = i;
700                                 pov++;
701                             }
702                         }
703                         else
704                         {
705                             if (axis >= 8)
706                             {
707                                 FIXME("Only 8 Axes supported - ignoring extra\n");
708                                 i = -1;
709                             }
710                             else
711                                 axis++;
712                         }
713                         break;
714                     }
715                 }
716
717                 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
718                 {
719                     ERR("invalid joystick axis type: \"%s\"\n", ptr);
720                     i = -1;
721                 }
722
723                 This->axis_map[tokens] = i;
724                 tokens++;
725             } while ((ptr = strtok(NULL, delim)) != NULL);
726
727             if (tokens != This->device_axis_count)
728             {
729                 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
730                     This->device_axis_count, axis, pov, tokens);
731                 while (tokens < This->device_axis_count)
732                 {
733                     This->axis_map[tokens] = -1;
734                     tokens++;
735                 }
736             }
737         }
738     }
739     else
740     {
741         int i;
742
743         if (default_axis_map)
744         {
745             /* Use default mapping from the driver */
746             for (i = 0; i < This->device_axis_count; i++)
747             {
748                 This->axis_map[i] = default_axis_map[i];
749                 tokens = default_axis_map[i];
750                 if (tokens < 0)
751                     continue;
752                 if (tokens < 8)
753                     axis++;
754                 else if (tokens < 15)
755                 {
756                     i++;
757                     pov++;
758                     This->axis_map[i] = default_axis_map[i];
759                 }
760             }
761         }
762         else
763         {
764             /* No config - set default mapping. */
765             for (i = 0; i < This->device_axis_count; i++)
766             {
767                 if (i < 8)
768                     This->axis_map[i] = axis++;
769                 else if (i < 15)
770                 {
771                     This->axis_map[i++] = 8 + pov;
772                     This->axis_map[i  ] = 8 + pov++;
773                 }
774                 else
775                     This->axis_map[i] = -1;
776             }
777         }
778     }
779     This->devcaps.dwAxes = axis;
780     This->devcaps.dwPOVs = pov;
781
782     if (appkey) RegCloseKey(appkey);
783     if (hkey)   RegCloseKey(hkey);
784
785     return DI_OK;
786 }