Release 1.5.29.
[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     ObjProps remap_props;
98
99     TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
100
101     if (ph == NULL) {
102         WARN("invalid parameter: ph == NULL\n");
103         return DIERR_INVALIDPARAM;
104     }
105
106     if (TRACE_ON(dinput))
107         _dump_DIPROPHEADER(ph);
108
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++) {
116
117                     remap_props.lDevMin = This->props[i].lMin;
118                     remap_props.lDevMax = This->props[i].lMax;
119
120                     remap_props.lDeadZone = This->props[i].lDeadZone;
121                     remap_props.lSaturation = This->props[i].lSaturation;
122
123                     remap_props.lMin = pr->lMin;
124                     remap_props.lMax = pr->lMax;
125
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;
136                     }
137
138                     This->props[i].lMin = pr->lMin;
139                     This->props[i].lMax = pr->lMax;
140                 }
141             } else {
142                 int obj = find_property(&This->base.data_format, ph);
143
144                 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
145                 if (obj >= 0) {
146
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*/
148
149                     remap_props.lDevMin = This->props[obj].lMin;
150                     remap_props.lDevMax = This->props[obj].lMax;
151
152                     remap_props.lDeadZone = This->props[obj].lDeadZone;
153                     remap_props.lSaturation = This->props[obj].lSaturation;
154
155                     remap_props.lMin = pr->lMin;
156                     remap_props.lMax = pr->lMax;
157
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;
168                     }
169
170                     This->props[obj].lMin = pr->lMin;
171                     This->props[obj].lMax = pr->lMax;
172                     return DI_OK;
173                 }
174             }
175             break;
176         }
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);
185
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;
190                 }
191             }
192             break;
193         }
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);
202
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;
207                 }
208             }
209             break;
210         }
211         default:
212             return IDirectInputDevice2WImpl_SetProperty(iface, rguid, ph);
213         }
214     }
215
216     return DI_OK;
217 }
218
219 HRESULT WINAPI JoystickAGenericImpl_SetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPCDIPROPHEADER ph)
220 {
221     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
222     return JoystickWGenericImpl_SetProperty(IDirectInputDevice8W_from_impl(This), rguid, ph);
223 }
224
225 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
226 {
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);
245     }
246 }
247
248 HRESULT WINAPI JoystickWGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8W iface, LPDIDEVCAPS lpDIDevCaps)
249 {
250     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
251     int size;
252
253     TRACE("%p->(%p)\n",iface,lpDIDevCaps);
254
255     if (lpDIDevCaps == NULL) {
256         WARN("invalid pointer\n");
257         return E_POINTER;
258     }
259
260     size = lpDIDevCaps->dwSize;
261
262     if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
263         WARN("invalid parameter\n");
264         return DIERR_INVALIDPARAM;
265     }
266
267     CopyMemory(lpDIDevCaps, &This->devcaps, size);
268     lpDIDevCaps->dwSize = size;
269
270     if (TRACE_ON(dinput))
271         _dump_DIDEVCAPS(lpDIDevCaps);
272
273     return DI_OK;
274 }
275
276 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(LPDIRECTINPUTDEVICE8A iface, LPDIDEVCAPS lpDIDevCaps)
277 {
278     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
279     return JoystickWGenericImpl_GetCapabilities(IDirectInputDevice8W_from_impl(This), lpDIDevCaps);
280 }
281
282 /******************************************************************************
283   *     GetObjectInfo : get object info
284   */
285 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
286         LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
287 {
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;
292
293     res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
294     if (res != DI_OK) return res;
295
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));
302
303     _dump_OBJECTINSTANCEW(pdidoi);
304     return res;
305 }
306
307 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
308         LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
309 {
310     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
311     HRESULT res;
312     DIDEVICEOBJECTINSTANCEW didoiW;
313     DWORD dwSize = pdidoi->dwSize;
314
315     didoiW.dwSize = sizeof(didoiW);
316     res = JoystickWGenericImpl_GetObjectInfo(IDirectInputDevice8W_from_impl(This), &didoiW, dwObj, dwHow);
317     if (res != DI_OK) return res;
318
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);
324
325     return res;
326 }
327
328 /******************************************************************************
329   *     GetProperty : get input device properties
330   */
331 HRESULT WINAPI JoystickWGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
332 {
333     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
334
335     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
336
337     if (TRACE_ON(dinput))
338         _dump_DIPROPHEADER(pdiph);
339
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);
345
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;
353             }
354             break;
355         }
356         case (DWORD_PTR) DIPROP_DEADZONE: {
357             LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
358             int obj = find_property(&This->base.data_format, pdiph);
359
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;
364             }
365             break;
366         }
367         case (DWORD_PTR) DIPROP_SATURATION: {
368             LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
369             int obj = find_property(&This->base.data_format, pdiph);
370
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;
375             }
376             break;
377         }
378         case (DWORD_PTR) DIPROP_INSTANCENAME: {
379             DIPROPSTRING *ps = (DIPROPSTRING*) pdiph;
380             DIDEVICEINSTANCEW didev;
381
382             didev.dwSize = sizeof(didev);
383
384             IDirectInputDevice_GetDeviceInfo(iface, &didev);
385             lstrcpynW(ps->wsz, didev.tszInstanceName, MAX_PATH);
386
387             return DI_OK;
388         }
389         default:
390             return IDirectInputDevice2WImpl_GetProperty(iface, rguid, pdiph);
391         }
392     }
393
394     return DI_OK;
395 }
396
397 HRESULT WINAPI JoystickAGenericImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
398 {
399     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
400     return JoystickWGenericImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
401 }
402
403 /******************************************************************************
404   *     GetDeviceInfo : get information about a device's identity
405   */
406 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
407     LPDIRECTINPUTDEVICE8A iface,
408     LPDIDEVICEINSTANCEA pdidi)
409 {
410     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
411
412     TRACE("(%p,%p)\n", iface, pdidi);
413
414     if (pdidi == NULL) {
415         WARN("invalid pointer\n");
416         return E_POINTER;
417     }
418
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;
423     }
424
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;
436     }
437
438     return DI_OK;
439 }
440
441 /******************************************************************************
442   *     GetDeviceInfo : get information about a device's identity
443   */
444 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
445     LPDIRECTINPUTDEVICE8W iface,
446     LPDIDEVICEINSTANCEW pdidi)
447 {
448     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
449
450     TRACE("(%p,%p)\n", iface, pdidi);
451
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;
456     }
457
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;
469     }
470
471     return DI_OK;
472 }
473
474 HRESULT WINAPI JoystickWGenericImpl_Poll(LPDIRECTINPUTDEVICE8W iface)
475 {
476     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
477
478     TRACE("(%p)\n",This);
479
480     if (!This->base.acquired) {
481         WARN("not acquired\n");
482         return DIERR_NOTACQUIRED;
483     }
484
485     This->joy_polldev(IDirectInputDevice8A_from_impl(This));
486     return DI_OK;
487 }
488
489 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
490 {
491     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
492     return JoystickWGenericImpl_Poll(IDirectInputDevice8W_from_impl(This));
493 }
494
495 /******************************************************************************
496   *     GetDeviceState : returns the "state" of the joystick.
497   *
498   */
499 HRESULT WINAPI JoystickWGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8W iface, DWORD len, LPVOID ptr)
500 {
501     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
502
503     TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
504
505     if (!This->base.acquired) {
506         WARN("not acquired\n");
507         return DIERR_NOTACQUIRED;
508     }
509
510     /* update joystick state */
511     This->joy_polldev(IDirectInputDevice8A_from_impl(This));
512
513     /* convert and copy data to user supplied buffer */
514     fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
515
516     return DI_OK;
517 }
518
519 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(LPDIRECTINPUTDEVICE8A iface, DWORD len, LPVOID ptr)
520 {
521     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
522     return JoystickWGenericImpl_GetDeviceState(IDirectInputDevice8W_from_impl(This), len, ptr);
523 }
524
525
526 HRESULT WINAPI JoystickWGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8W iface,
527                                                    LPDIACTIONFORMATW lpdiaf,
528                                                    LPCWSTR lpszUserName,
529                                                    DWORD dwFlags)
530 {
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 };
536
537     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
538
539     for (i=0; i < lpdiaf->dwNumActions; i++)
540     {
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;
544
545         /* Don't touch an user configured action */
546         if (lpdiaf->rgoAction[i].dwHow == DIAH_USERCONFIG) continue;
547
548         /* Only consider actions of the right genre */
549         if (lpdiaf->dwGenre != genre && genre != DIGENRE_ANY) continue;
550
551         for (j=0; j < sizeof(object_types)/sizeof(object_types[0]); j++)
552         {
553             if (type & object_types[j])
554             {
555                 /* Assure that the object exists */
556                 LPDIOBJECTDATAFORMAT odf = dataformat_to_odf_by_type(This->base.data_format.wine_df, inst, object_types[j]);
557
558                 if (odf != NULL)
559                 {
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;
563
564                     has_actions = 1;
565
566                     /* No need to try other types if the action was already mapped */
567                     break;
568                 }
569             }
570         }
571     }
572
573     if (!has_actions) return DI_NOEFFECT;
574
575     return IDirectInputDevice8WImpl_BuildActionMap(iface, lpdiaf, lpszUserName, dwFlags);
576 }
577
578 HRESULT WINAPI JoystickAGenericImpl_BuildActionMap(LPDIRECTINPUTDEVICE8A iface,
579                                                    LPDIACTIONFORMATA lpdiaf,
580                                                    LPCSTR lpszUserName,
581                                                    DWORD dwFlags)
582 {
583     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
584     DIACTIONFORMATW diafW;
585     HRESULT hr;
586     WCHAR *lpszUserNameW = NULL;
587     int username_size;
588
589     diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
590     _copy_diactionformatAtoW(&diafW, lpdiaf);
591
592     if (lpszUserName != NULL)
593     {
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);
597     }
598
599     hr = JoystickWGenericImpl_BuildActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
600
601     _copy_diactionformatWtoA(lpdiaf, &diafW);
602     HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
603     HeapFree(GetProcessHeap(), 0, lpszUserNameW);
604
605     return hr;
606 }
607
608 HRESULT WINAPI JoystickWGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8W iface,
609                                                  LPDIACTIONFORMATW lpdiaf,
610                                                  LPCWSTR lpszUserName,
611                                                  DWORD dwFlags)
612 {
613     JoystickGenericImpl *This = impl_from_IDirectInputDevice8W(iface);
614
615     FIXME("(%p)->(%p,%s,%08x): semi-stub !\n", iface, lpdiaf, debugstr_w(lpszUserName), dwFlags);
616
617     return _set_action_map(iface, lpdiaf, lpszUserName, dwFlags, This->base.data_format.wine_df);
618 }
619
620 HRESULT WINAPI JoystickAGenericImpl_SetActionMap(LPDIRECTINPUTDEVICE8A iface,
621                                                  LPDIACTIONFORMATA lpdiaf,
622                                                  LPCSTR lpszUserName,
623                                                  DWORD dwFlags)
624 {
625     JoystickGenericImpl *This = impl_from_IDirectInputDevice8A(iface);
626     DIACTIONFORMATW diafW;
627     HRESULT hr;
628     WCHAR *lpszUserNameW = NULL;
629     int username_size;
630
631     diafW.rgoAction = HeapAlloc(GetProcessHeap(), 0, sizeof(DIACTIONW)*lpdiaf->dwNumActions);
632     _copy_diactionformatAtoW(&diafW, lpdiaf);
633
634     if (lpszUserName != NULL)
635     {
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);
639     }
640
641     hr = JoystickWGenericImpl_SetActionMap(&This->base.IDirectInputDevice8W_iface, &diafW, lpszUserNameW, dwFlags);
642
643     HeapFree(GetProcessHeap(), 0, diafW.rgoAction);
644     HeapFree(GetProcessHeap(), 0, lpszUserNameW);
645
646     return hr;
647 }
648
649 /*
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)
654  */
655 LONG joystick_map_axis(ObjProps *props, int val)
656 {
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;
660
661     /* Center input */
662     val -= (props->lDevMin + props->lDevMax) / 2;
663
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;
669
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;
673
674     /* Clamp in case or rounding errors */
675     if      (ret > props->lMax) ret = props->lMax;
676     else if (ret < props->lMin) ret = props->lMin;
677
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 );
682
683     return ret;
684 }
685
686 /*
687  * Maps POV x & y event values to a DX "clock" position:
688  *         0
689  *   31500    4500
690  * 27000  -1    9000
691  *   22500   13500
692  *       18000
693  */
694 DWORD joystick_map_pov(const POINTL *p)
695 {
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;
702 }
703
704 /*
705  * Setup the dinput options.
706  */
707
708 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
709 {
710     char buffer[MAX_PATH+16];
711     HKEY hkey, appkey;
712     int tokens = 0;
713     int axis = 0;
714     int pov = 0;
715
716     get_app_key(&hkey, &appkey);
717
718     /* get options */
719
720     if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
721     {
722         This->deadzone = atoi(buffer);
723         TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
724     }
725
726     This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
727     if (!This->axis_map) return DIERR_OUTOFMEMORY;
728
729     if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
730     {
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);
737
738         if ((ptr = strtok(buffer, delim)) != NULL)
739         {
740             do
741             {
742                 int i;
743
744                 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
745                 {
746                     if (!strcmp(ptr, axis_names[i]))
747                     {
748                         if (!strncmp(ptr, "POV", 3))
749                         {
750                             if (pov >= 4)
751                             {
752                                 WARN("Only 4 POVs supported - ignoring extra\n");
753                                 i = -1;
754                             }
755                             else
756                             {
757                                 /* Pov takes two axes */
758                                 This->axis_map[tokens++] = i;
759                                 pov++;
760                             }
761                         }
762                         else
763                         {
764                             if (axis >= 8)
765                             {
766                                 FIXME("Only 8 Axes supported - ignoring extra\n");
767                                 i = -1;
768                             }
769                             else
770                                 axis++;
771                         }
772                         break;
773                     }
774                 }
775
776                 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
777                 {
778                     ERR("invalid joystick axis type: \"%s\"\n", ptr);
779                     i = -1;
780                 }
781
782                 This->axis_map[tokens] = i;
783                 tokens++;
784             } while ((ptr = strtok(NULL, delim)) != NULL);
785
786             if (tokens != This->device_axis_count)
787             {
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)
791                 {
792                     This->axis_map[tokens] = -1;
793                     tokens++;
794                 }
795             }
796         }
797     }
798     else
799     {
800         int i;
801
802         if (default_axis_map)
803         {
804             /* Use default mapping from the driver */
805             for (i = 0; i < This->device_axis_count; i++)
806             {
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)
814                 {
815                     i++;
816                     pov++;
817                     This->axis_map[i] = default_axis_map[i];
818                 }
819             }
820         }
821         else
822         {
823             /* No config - set default mapping. */
824             for (i = 0; i < This->device_axis_count; i++)
825             {
826                 if (i < 8)
827                     This->axis_map[i] = axis++;
828                 else if (i < 15)
829                 {
830                     This->axis_map[i++] = 8 + pov;
831                     This->axis_map[i  ] = 8 + pov++;
832                 }
833                 else
834                     This->axis_map[i] = -1;
835             }
836         }
837     }
838     This->devcaps.dwAxes = axis;
839     This->devcaps.dwPOVs = pov;
840
841     if (appkey) RegCloseKey(appkey);
842     if (hkey)   RegCloseKey(hkey);
843
844     return DI_OK;
845 }