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