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"
32 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
34 /******************************************************************************
35 * Acquire : gets exclusive control of the joystick
37 HRESULT WINAPI JoystickAGenericImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
39 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
43 if (This->base.acquired) {
44 WARN("already acquired\n");
48 This->base.acquired = 1;
53 /******************************************************************************
54 * Unacquire : frees the joystick
56 HRESULT WINAPI JoystickAGenericImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
58 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
63 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
68 /******************************************************************************
69 * SetProperty : change input device properties
71 HRESULT WINAPI JoystickAGenericImpl_SetProperty(
72 LPDIRECTINPUTDEVICE8A iface,
76 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
79 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
82 WARN("invalid parameter: ph == NULL\n");
83 return DIERR_INVALIDPARAM;
87 _dump_DIPROPHEADER(ph);
90 switch (LOWORD(rguid)) {
91 case (DWORD_PTR)DIPROP_RANGE: {
92 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
93 if (ph->dwHow == DIPH_DEVICE) {
94 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
95 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
96 This->props[i].lMin = pr->lMin;
97 This->props[i].lMax = pr->lMax;
100 int obj = find_property(&This->base.data_format, ph);
102 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
104 This->props[obj].lMin = pr->lMin;
105 This->props[obj].lMax = pr->lMax;
111 case (DWORD_PTR)DIPROP_DEADZONE: {
112 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
113 if (ph->dwHow == DIPH_DEVICE) {
114 TRACE("deadzone(%d) all\n", pd->dwData);
115 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
116 This->props[i].lDeadZone = pd->dwData;
118 int obj = find_property(&This->base.data_format, ph);
120 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
122 This->props[obj].lDeadZone = pd->dwData;
128 case (DWORD_PTR)DIPROP_SATURATION: {
129 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
130 if (ph->dwHow == DIPH_DEVICE) {
131 TRACE("saturation(%d) all\n", pd->dwData);
132 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
133 This->props[i].lSaturation = pd->dwData;
135 int obj = find_property(&This->base.data_format, ph);
137 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
139 This->props[obj].lSaturation = pd->dwData;
146 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
153 void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
155 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
156 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
157 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
158 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
159 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
160 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
161 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
162 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
163 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
164 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
165 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
166 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
167 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
168 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
169 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
170 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
171 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
172 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
176 HRESULT WINAPI JoystickAGenericImpl_GetCapabilities(
177 LPDIRECTINPUTDEVICE8A iface,
178 LPDIDEVCAPS lpDIDevCaps)
180 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
183 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
185 if (lpDIDevCaps == NULL) {
186 WARN("invalid pointer\n");
190 size = lpDIDevCaps->dwSize;
192 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
193 WARN("invalid parameter\n");
194 return DIERR_INVALIDPARAM;
197 CopyMemory(lpDIDevCaps, &This->devcaps, size);
198 lpDIDevCaps->dwSize = size;
200 if (TRACE_ON(dinput))
201 _dump_DIDEVCAPS(lpDIDevCaps);
206 /******************************************************************************
207 * GetObjectInfo : get object info
209 HRESULT WINAPI JoystickWGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
210 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
212 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
213 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
214 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
217 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
218 if (res != DI_OK) return res;
220 if (pdidoi->dwType & DIDFT_AXIS)
221 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
222 else if (pdidoi->dwType & DIDFT_POV)
223 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
224 else if (pdidoi->dwType & DIDFT_BUTTON)
225 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
227 _dump_OBJECTINSTANCEW(pdidoi);
231 HRESULT WINAPI JoystickAGenericImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
232 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
235 DIDEVICEOBJECTINSTANCEW didoiW;
236 DWORD dwSize = pdidoi->dwSize;
238 didoiW.dwSize = sizeof(didoiW);
239 res = JoystickWGenericImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
240 if (res != DI_OK) return res;
242 memset(pdidoi, 0, pdidoi->dwSize);
243 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
244 pdidoi->dwSize = dwSize;
245 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
246 sizeof(pdidoi->tszName), NULL, NULL);
251 /******************************************************************************
252 * GetProperty : get input device properties
254 HRESULT WINAPI JoystickAGenericImpl_GetProperty(
255 LPDIRECTINPUTDEVICE8A iface,
257 LPDIPROPHEADER pdiph)
259 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
261 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
263 if (TRACE_ON(dinput))
264 _dump_DIPROPHEADER(pdiph);
266 if (!HIWORD(rguid)) {
267 switch (LOWORD(rguid)) {
268 case (DWORD_PTR) DIPROP_RANGE: {
269 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
270 int obj = find_property(&This->base.data_format, pdiph);
272 /* The app is querying the current range of the axis
273 * return the lMin and lMax values */
275 pr->lMin = This->props[obj].lMin;
276 pr->lMax = This->props[obj].lMax;
277 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
282 case (DWORD_PTR) DIPROP_DEADZONE: {
283 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
284 int obj = find_property(&This->base.data_format, pdiph);
287 pd->dwData = This->props[obj].lDeadZone;
288 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
293 case (DWORD_PTR) DIPROP_SATURATION: {
294 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
295 int obj = find_property(&This->base.data_format, pdiph);
298 pd->dwData = This->props[obj].lSaturation;
299 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
305 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
312 /******************************************************************************
313 * GetDeviceInfo : get information about a device's identity
315 HRESULT WINAPI JoystickAGenericImpl_GetDeviceInfo(
316 LPDIRECTINPUTDEVICE8A iface,
317 LPDIDEVICEINSTANCEA pdidi)
319 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
321 TRACE("(%p,%p)\n", iface, pdidi);
324 WARN("invalid pointer\n");
328 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
329 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
330 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
331 return DIERR_INVALIDPARAM;
334 /* Return joystick */
335 pdidi->guidInstance = GUID_Joystick;
336 pdidi->guidProduct = This->guidProduct;
337 /* we only support traditional joysticks for now */
338 pdidi->dwDevType = This->devcaps.dwDevType;
339 strcpy(pdidi->tszInstanceName, "Joystick");
340 strcpy(pdidi->tszProductName, This->name);
341 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
342 pdidi->guidFFDriver = GUID_NULL;
343 pdidi->wUsagePage = 0;
350 /******************************************************************************
351 * GetDeviceInfo : get information about a device's identity
353 HRESULT WINAPI JoystickWGenericImpl_GetDeviceInfo(
354 LPDIRECTINPUTDEVICE8W iface,
355 LPDIDEVICEINSTANCEW pdidi)
357 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
359 TRACE("(%p,%p)\n", iface, pdidi);
361 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
362 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
363 WARN("invalid parameter: pdidi->dwSize = %d\n", pdidi->dwSize);
364 return DIERR_INVALIDPARAM;
367 /* Return joystick */
368 pdidi->guidInstance = GUID_Joystick;
369 pdidi->guidProduct = This->guidProduct;
370 /* we only support traditional joysticks for now */
371 pdidi->dwDevType = This->devcaps.dwDevType;
372 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
373 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
374 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
375 pdidi->guidFFDriver = GUID_NULL;
376 pdidi->wUsagePage = 0;
383 HRESULT WINAPI JoystickAGenericImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
385 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
387 TRACE("(%p)\n",This);
389 if (!This->base.acquired) {
390 WARN("not acquired\n");
391 return DIERR_NOTACQUIRED;
394 This->joy_polldev(This);
398 /******************************************************************************
399 * GetDeviceState : returns the "state" of the joystick.
402 HRESULT WINAPI JoystickAGenericImpl_GetDeviceState(
403 LPDIRECTINPUTDEVICE8A iface,
407 JoystickGenericImpl *This = (JoystickGenericImpl *)iface;
409 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
411 if (!This->base.acquired) {
412 WARN("not acquired\n");
413 return DIERR_NOTACQUIRED;
416 /* update joystick state */
417 This->joy_polldev(This);
419 /* convert and copy data to user supplied buffer */
420 fill_DataFormat(ptr, len, &This->js, &This->base.data_format);
426 * This maps the read value (from the input event) to a value in the
429 * Dead zone is in % multiplied by a 100 (range 0..10000)
431 LONG joystick_map_axis(ObjProps *props, int val)
434 LONG dead_zone = MulDiv( props->lDeadZone, props->lDevMax - props->lDevMin, 10000 );
435 LONG dev_range = props->lDevMax - props->lDevMin - dead_zone;
438 val -= (props->lDevMin + props->lDevMax) / 2;
440 /* Remove dead zone */
441 if (abs( val ) <= dead_zone / 2)
444 val = val < 0 ? val + dead_zone / 2 : val - dead_zone / 2;
446 /* Scale and map the value from the device range into the required range */
447 ret = MulDiv( val, props->lMax - props->lMin, dev_range ) +
448 (props->lMin + props->lMax) / 2;
450 /* Clamp in case or rounding errors */
451 if (ret > props->lMax) ret = props->lMax;
452 else if (ret < props->lMin) ret = props->lMin;
454 TRACE( "(%d <%d> %d) -> (%d <%d> %d): val=%d ret=%d\n",
455 props->lDevMin, dead_zone, props->lDevMax,
456 props->lMin, props->lDeadZone, props->lMax,
463 * Maps POV x & y event values to a DX "clock" position:
470 DWORD joystick_map_pov(POINTL *p)
473 return p->y < 0 ? 4500 : !p->y ? 9000 : 13500;
475 return p->y < 0 ? 31500 : !p->y ? 27000 : 22500;
477 return p->y < 0 ? 0 : !p->y ? -1 : 18000;