1 /* DirectInput Generic Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
6 * Copyright 2009 Aric Stewart, CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "joystick_private.h"
30 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
35 /******************************************************************************
36 * SetProperty : change input device properties
38 HRESULT WINAPI JoystickAGenericImpl_SetProperty(
39 LPDIRECTINPUTDEVICE8A iface,
43 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
46 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
49 WARN("invalid parameter: ph == NULL\n");
50 return DIERR_INVALIDPARAM;
54 _dump_DIPROPHEADER(ph);
56 if (IS_DIPROP(rguid)) {
57 switch (LOWORD(rguid)) {
58 case (DWORD_PTR)DIPROP_RANGE: {
59 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
60 if (ph->dwHow == DIPH_DEVICE) {
61 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
62 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
63 This->props[i].lMin = pr->lMin;
64 This->props[i].lMax = pr->lMax;
67 int obj = find_property(&This->base.data_format, ph);
69 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
71 This->props[obj].lMin = pr->lMin;
72 This->props[obj].lMax = pr->lMax;
78 case (DWORD_PTR)DIPROP_DEADZONE: {
79 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
80 if (ph->dwHow == DIPH_DEVICE) {
81 TRACE("deadzone(%d) all\n", pd->dwData);
82 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
83 This->props[i].lDeadZone = pd->dwData;
85 int obj = find_property(&This->base.data_format, ph);
87 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
89 This->props[obj].lDeadZone = pd->dwData;
95 case (DWORD_PTR)DIPROP_SATURATION: {
96 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
97 if (ph->dwHow == DIPH_DEVICE) {
98 TRACE("saturation(%d) all\n", pd->dwData);
99 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
100 This->props[i].lSaturation = pd->dwData;
102 int obj = find_property(&This->base.data_format, ph);
104 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
106 This->props[obj].lSaturation = pd->dwData;
113 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
120 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
122 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
123 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
124 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
125 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
126 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
127 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
128 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
129 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
130 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
131 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
132 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
133 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
134 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
135 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
136 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
137 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
138 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
139 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
143 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(
144 LPDIRECTINPUTDEVICE8A iface,
145 LPDIDEVCAPS lpDIDevCaps)
147 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
150 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
152 if (lpDIDevCaps == NULL) {
153 WARN("invalid pointer\n");
157 size = lpDIDevCaps->dwSize;
159 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
160 WARN("invalid parameter\n");
161 return DIERR_INVALIDPARAM;
164 CopyMemory(lpDIDevCaps, &This->devcaps, size);
165 lpDIDevCaps->dwSize = size;
167 if (TRACE_ON(dinput))
168 _dump_DIDEVCAPS(lpDIDevCaps);
173 /******************************************************************************
174 * GetObjectInfo : get object info
176 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
177 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
179 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
180 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
181 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
184 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
185 if (res != DI_OK) return res;
187 if (pdidoi->dwType & DIDFT_AXIS)
188 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
189 else if (pdidoi->dwType & DIDFT_POV)
190 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
191 else if (pdidoi->dwType & DIDFT_BUTTON)
192 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
194 _dump_OBJECTINSTANCEW(pdidoi);
198 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
199 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
202 DIDEVICEOBJECTINSTANCEW didoiW;
203 DWORD dwSize = pdidoi->dwSize;
205 didoiW.dwSize = sizeof(didoiW);
206 res = JoystickWGenericImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
207 if (res != DI_OK) return res;
209 memset(pdidoi, 0, pdidoi->dwSize);
210 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
211 pdidoi->dwSize = dwSize;
212 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
213 sizeof(pdidoi->tszName), NULL, NULL);
218 /******************************************************************************
219 * GetProperty : get input device properties
221 HRESULT WINAPI JoystickAGenericImpl_GetProperty(
222 LPDIRECTINPUTDEVICE8A iface,
224 LPDIPROPHEADER pdiph)
226 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
228 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
230 if (TRACE_ON(dinput))
231 _dump_DIPROPHEADER(pdiph);
233 if (IS_DIPROP(rguid)) {
234 switch (LOWORD(rguid)) {
235 case (DWORD_PTR) DIPROP_RANGE: {
236 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
237 int obj = find_property(&This->base.data_format, pdiph);
239 /* The app is querying the current range of the axis
240 * return the lMin and lMax values */
242 pr->lMin = This->props[obj].lMin;
243 pr->lMax = This->props[obj].lMax;
244 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
249 case (DWORD_PTR) DIPROP_DEADZONE: {
250 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
251 int obj = find_property(&This->base.data_format, pdiph);
254 pd->dwData = This->props[obj].lDeadZone;
255 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
260 case (DWORD_PTR) DIPROP_SATURATION: {
261 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
262 int obj = find_property(&This->base.data_format, pdiph);
265 pd->dwData = This->props[obj].lSaturation;
266 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
272 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
279 /******************************************************************************
280 * GetDeviceInfo : get information about a device's identity
282 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
283 LPDIRECTINPUTDEVICE8A iface,
284 LPDIDEVICEINSTANCEA pdidi)
286 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
288 TRACE("(%p,%p)\n", iface, pdidi);
291 WARN("invalid pointer\n");
295 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
296 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
297 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
298 return DIERR_INVALIDPARAM;
301 /* Return joystick */
302 pdidi->guidInstance = This->guidInstance;
303 pdidi->guidProduct = This->guidProduct;
304 /* we only support traditional joysticks for now */
305 pdidi->dwDevType = This->devcaps.dwDevType;
306 strcpy(pdidi->tszInstanceName, "Joystick");
307 strcpy(pdidi->tszProductName, This->name);
308 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
309 pdidi->guidFFDriver = GUID_NULL;
310 pdidi->wUsagePage = 0;
317 /******************************************************************************
318 * GetDeviceInfo : get information about a device's identity
320 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
321 LPDIRECTINPUTDEVICE8W iface,
322 LPDIDEVICEINSTANCEW pdidi)
324 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
326 TRACE("(%p,%p)\n", iface, pdidi);
328 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
329 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
330 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
331 return DIERR_INVALIDPARAM;
334 /* Return joystick */
335 pdidi->guidInstance = This->guidInstance;
336 pdidi->guidProduct = This->guidProduct;
337 /* we only support traditional joysticks for now */
338 pdidi->dwDevType = This->devcaps.dwDevType;
339 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
340 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
341 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
342 pdidi->guidFFDriver = GUID_NULL;
343 pdidi->wUsagePage = 0;
350 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
352 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
354 TRACE("(%p)\n",This);
356 if (!This->base.acquired) {
357 WARN("not acquired\n");
358 return DIERR_NOTACQUIRED;
361 This->joy_polldev(This);
365 /******************************************************************************
366 * GetDeviceState : returns the "state" of the joystick.
369 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(
370 LPDIRECTINPUTDEVICE8A iface,
374 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
376 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
378 if (!This->base.acquired) {
379 WARN("not acquired\n");
380 return DIERR_NOTACQUIRED;
383 /* update joystick state */
384 This->joy_polldev(This);
386 /* convert and copy data to user supplied buffer */
387 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
393 * This maps the read value (from the input event) to a value in the
396 * Dead zone is in % multiplied by a 100 (range 0..10000)
398 LONG joystick_map_axis(ObjProps *props, int val)
401 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
402 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
405 val -= (props->lDevMin + props->lDevMax) / 2;
407 /* Remove dead zone */
408 if (abs( val ) <= dead_zone / 2)
411 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
413 /* Scale and map the value from the device range into the required range */
414 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
415 (props->lMin + props->lMax) / 2;
417 /* Clamp in case or rounding errors */
418 if (ret > props->lMax) ret = props->lMax;
419 else if (ret < props->lMin) ret = props->lMin;
421 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
422 props->lDevMin, dead_zone, props->lDevMax,
423 props->lMin, props->lDeadZone, props->lMax,
430 * Maps POV x & y event values to a DX "clock" position:
437 DWORD joystick_map_pov(const POINTL *p)
440 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
442 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
444 return p->y < 0 ? 0 : !p->y ? -1 : 18000;
448 * Setup the dinput options.
451 HRESULT setup_dinput_options(JoystickGenericImpl *This, const int *default_axis_map)
453 char buffer[MAX_PATH+16];
459 get_app_key(&hkey, &appkey);
463 if (!get_config_key(hkey, appkey, "DefaultDeadZone", buffer, sizeof(buffer)))
465 This->deadzone = atoi(buffer);
466 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, This->deadzone);
469 This->axis_map = HeapAlloc(GetProcessHeap(), 0, This->device_axis_count * sizeof(int));
470 if (!This->axis_map) return DIERR_OUTOFMEMORY;
472 if (!get_config_key(hkey, appkey, This->name, buffer, sizeof(buffer)))
474 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
475 "Slider1", "Slider2",
476 "POV1", "POV2", "POV3", "POV4"};
477 const char *delim = ",";
479 TRACE("\"%s\" = \"%s\"\n", This->name, buffer);
481 if ((ptr = strtok(buffer, delim)) != NULL)
487 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
489 if (!strcmp(ptr, axis_names[i]))
491 if (!strncmp(ptr, "POV", 3))
495 WARN("Only 4 POVs supported - ignoring extra\n");
500 /* Pov takes two axes */
501 This->axis_map[tokens++] = i;
509 FIXME("Only 8 Axes supported - ignoring extra\n");
519 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
521 ERR("invalid joystick axis type: \"%s\"\n", ptr);
525 This->axis_map[tokens] = i;
527 } while ((ptr = strtok(NULL, delim)) != NULL);
529 if (tokens != This->device_axis_count)
531 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n",
532 This->device_axis_count, axis, pov, tokens);
533 while (tokens < This->device_axis_count)
535 This->axis_map[tokens] = -1;
545 if (default_axis_map)
547 /* Use default mapping from the driver */
548 for (i = 0; i < This->device_axis_count; i++)
550 This->axis_map[i] = default_axis_map[i];
551 tokens = default_axis_map[i];
556 else if (tokens < 15)
560 This->axis_map[i] = default_axis_map[i];
566 /* No config - set default mapping. */
567 for (i = 0; i < This->device_axis_count; i++)
570 This->axis_map[i] = axis++;
573 This->axis_map[i++] = 8 + pov;
574 This->axis_map[i ] = 8 + pov++;
577 This->axis_map[i] = -1;
581 This->devcaps.dwAxes = axis;
582 This->devcaps.dwPOVs = pov;
584 if (appkey) RegCloseKey(appkey);
585 if (hkey) RegCloseKey(hkey);