1 /* DirectInput Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/port.h"
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
46 #ifdef HAVE_SYS_ERRNO_H
47 # include <sys/errno.h>
49 #ifdef HAVE_LINUX_IOCTL_H
50 # include <linux/ioctl.h>
52 #ifdef HAVE_LINUX_JOYSTICK_H
53 # include <linux/joystick.h>
56 #ifdef HAVE_SYS_POLL_H
57 # include <sys/poll.h>
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
68 #include "dinput_private.h"
69 #include "device_private.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(dinput
);
73 #ifdef HAVE_LINUX_22_JOYSTICK_API
75 #define JOYDEV_NEW "/dev/input/js"
76 #define JOYDEV_OLD "/dev/js"
78 typedef struct JoystickImpl JoystickImpl
;
79 static const IDirectInputDevice8AVtbl JoystickAvt
;
80 static const IDirectInputDevice8WVtbl JoystickWvt
;
83 struct IDirectInputDevice2AImpl base
;
87 /* joystick private */
89 DIJOYSTATE2 js
; /* wine data */
99 static const GUID DInput_Wine_Joystick_GUID
= { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
103 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
106 static void _dump_DIDEVCAPS(const DIDEVCAPS
*lpDIDevCaps
)
108 TRACE("dwSize: %d\n", lpDIDevCaps
->dwSize
);
109 TRACE("dwFlags: %08x\n", lpDIDevCaps
->dwFlags
);
110 TRACE("dwDevType: %08x %s\n", lpDIDevCaps
->dwDevType
,
111 lpDIDevCaps
->dwDevType
== DIDEVTYPE_DEVICE
? "DIDEVTYPE_DEVICE" :
112 lpDIDevCaps
->dwDevType
== DIDEVTYPE_DEVICE
? "DIDEVTYPE_DEVICE" :
113 lpDIDevCaps
->dwDevType
== DIDEVTYPE_MOUSE
? "DIDEVTYPE_MOUSE" :
114 lpDIDevCaps
->dwDevType
== DIDEVTYPE_KEYBOARD
? "DIDEVTYPE_KEYBOARD" :
115 lpDIDevCaps
->dwDevType
== DIDEVTYPE_JOYSTICK
? "DIDEVTYPE_JOYSTICK" :
116 lpDIDevCaps
->dwDevType
== DIDEVTYPE_HID
? "DIDEVTYPE_HID" : "UNKNOWN");
117 TRACE("dwAxes: %d\n", lpDIDevCaps
->dwAxes
);
118 TRACE("dwButtons: %d\n", lpDIDevCaps
->dwButtons
);
119 TRACE("dwPOVs: %d\n", lpDIDevCaps
->dwPOVs
);
120 if (lpDIDevCaps
->dwSize
> sizeof(DIDEVCAPS_DX3
)) {
121 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps
->dwFFSamplePeriod
);
122 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps
->dwFFMinTimeResolution
);
123 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps
->dwFirmwareRevision
);
124 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps
->dwHardwareRevision
);
125 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps
->dwFFDriverVersion
);
129 #define MAX_JOYSTICKS 64
130 static INT joystick_devices_count
= -1;
131 static LPSTR joystick_devices
[MAX_JOYSTICKS
];
133 static INT
find_joystick_devices(void)
137 if (joystick_devices_count
!= -1) return joystick_devices_count
;
139 joystick_devices_count
= 0;
140 for (i
= 0; i
< MAX_JOYSTICKS
; i
++)
142 CHAR device_name
[MAX_PATH
], *str
;
146 len
= sprintf(device_name
, "%s%d", JOYDEV_NEW
, i
) + 1;
147 if ((fd
= open(device_name
, O_RDONLY
)) < 0)
149 len
= sprintf(device_name
, "%s%d", JOYDEV_OLD
, i
) + 1;
150 if ((fd
= open(device_name
, O_RDONLY
)) < 0) continue;
155 if (!(str
= HeapAlloc(GetProcessHeap(), 0, len
))) break;
156 memcpy(str
, device_name
, len
);
158 joystick_devices
[joystick_devices_count
++] = str
;
161 return joystick_devices_count
;
164 static BOOL
joydev_enum_deviceA(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEA lpddi
, DWORD version
, int id
)
168 if (id
>= find_joystick_devices()) return FALSE
;
170 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
171 WARN("force feedback not supported\n");
175 if ((dwDevType
== 0) ||
176 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
> 0x0300 && version
< 0x0800)) ||
177 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
178 /* check whether we have a joystick */
179 if ((fd
= open(joystick_devices
[id
], O_RDONLY
)) < 0)
181 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices
[id
], strerror(errno
));
185 /* Return joystick */
186 lpddi
->guidInstance
= DInput_Wine_Joystick_GUID
;
187 lpddi
->guidInstance
.Data3
= id
;
188 lpddi
->guidProduct
= DInput_Wine_Joystick_GUID
;
189 /* we only support traditional joysticks for now */
190 if (version
>= 0x0800)
191 lpddi
->dwDevType
= DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8);
193 lpddi
->dwDevType
= DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
194 sprintf(lpddi
->tszInstanceName
, "Joystick %d", id
);
195 #if defined(JSIOCGNAME)
196 if (ioctl(fd
,JSIOCGNAME(sizeof(lpddi
->tszProductName
)),lpddi
->tszProductName
) < 0) {
197 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices
[id
], strerror(errno
));
198 strcpy(lpddi
->tszProductName
, "Wine Joystick");
201 strcpy(lpddi
->tszProductName
, "Wine Joystick");
204 lpddi
->guidFFDriver
= GUID_NULL
;
206 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices
[id
], lpddi
->tszProductName
);
213 static BOOL
joydev_enum_deviceW(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEW lpddi
, DWORD version
, int id
)
219 if (id
>= find_joystick_devices()) return FALSE
;
221 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
222 WARN("force feedback not supported\n");
226 if ((dwDevType
== 0) ||
227 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
> 0x0300 && version
< 0x0800)) ||
228 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
229 /* check whether we have a joystick */
230 if ((fd
= open(joystick_devices
[id
], O_RDONLY
)) < 0)
232 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices
[id
], strerror(errno
));
236 /* Return joystick */
237 lpddi
->guidInstance
= DInput_Wine_Joystick_GUID
;
238 lpddi
->guidInstance
.Data3
= id
;
239 lpddi
->guidProduct
= DInput_Wine_Joystick_GUID
;
240 /* we only support traditional joysticks for now */
241 if (version
>= 0x0800)
242 lpddi
->dwDevType
= DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8);
244 lpddi
->dwDevType
= DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
245 sprintf(friendly
, "Joystick %d", id
);
246 MultiByteToWideChar(CP_ACP
, 0, friendly
, -1, lpddi
->tszInstanceName
, MAX_PATH
);
247 #if defined(JSIOCGNAME)
248 if (ioctl(fd
,JSIOCGNAME(sizeof(name
)),name
) < 0) {
249 WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices
[id
], strerror(errno
));
250 strcpy(name
, "Wine Joystick");
253 strcpy(name
, "Wine Joystick");
255 MultiByteToWideChar(CP_ACP
, 0, name
, -1, lpddi
->tszProductName
, MAX_PATH
);
256 lpddi
->guidFFDriver
= GUID_NULL
;
258 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices
[id
], name
);
266 * Setup the dinput options.
269 static HRESULT
setup_dinput_options(JoystickImpl
* device
)
271 char buffer
[MAX_PATH
+16];
277 buffer
[MAX_PATH
]='\0';
279 get_app_key(&hkey
, &appkey
);
283 if (!get_config_key( hkey
, appkey
, "DefaultDeadZone", buffer
, MAX_PATH
)) {
284 device
->deadzone
= atoi(buffer
);
285 TRACE("setting default deadzone to: \"%s\" %d\n", buffer
, device
->deadzone
);
288 device
->axis_map
= HeapAlloc(GetProcessHeap(), 0, device
->axes
* sizeof(int));
289 if (!device
->axis_map
) return DIERR_OUTOFMEMORY
;
291 if (!get_config_key( hkey
, appkey
, device
->name
, buffer
, MAX_PATH
)) {
292 static const char *axis_names
[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
293 "Slider1", "Slider2",
294 "POV1", "POV2", "POV3", "POV4"};
295 const char *delim
= ",";
297 TRACE("\"%s\" = \"%s\"\n", device
->name
, buffer
);
299 if ((ptr
= strtok(buffer
, delim
)) != NULL
) {
303 for (i
= 0; i
< sizeof(axis_names
) / sizeof(axis_names
[0]); i
++)
304 if (!strcmp(ptr
, axis_names
[i
]))
306 if (!strncmp(ptr
, "POV", 3))
310 WARN("Only 4 POVs supported - ignoring extra\n");
315 /* Pov takes two axes */
316 device
->axis_map
[tokens
++] = i
;
324 FIXME("Only 8 Axes supported - ignoring extra\n");
333 if (i
== sizeof(axis_names
) / sizeof(axis_names
[0]))
335 ERR("invalid joystick axis type: \"%s\"\n", ptr
);
339 device
->axis_map
[tokens
] = i
;
341 } while ((ptr
= strtok(NULL
, delim
)) != NULL
);
343 if (tokens
!= device
->axes
) {
344 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device
->axes
, axis
, pov
,tokens
);
345 while (tokens
< device
->axes
) {
346 device
->axis_map
[tokens
] = -1;
354 for (tokens
= 0; tokens
< device
->axes
; tokens
++)
357 device
->axis_map
[tokens
] = axis
++;
358 else if (tokens
< 16)
360 device
->axis_map
[tokens
++] = 8 + pov
;
361 device
->axis_map
[tokens
] = 8 + pov
++;
364 device
->axis_map
[tokens
] = -1;
367 device
->devcaps
.dwAxes
= axis
;
368 device
->devcaps
.dwPOVs
= pov
;
371 RegCloseKey( appkey
);
379 static HRESULT
alloc_device(REFGUID rguid
, const void *jvt
, IDirectInputImpl
*dinput
,
380 LPDIRECTINPUTDEVICEA
* pdev
, unsigned short index
)
383 JoystickImpl
* newDevice
;
386 LPDIDATAFORMAT df
= NULL
;
389 TRACE("%s %p %p %p %hu\n", debugstr_guid(rguid
), jvt
, dinput
, pdev
, index
);
391 newDevice
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(JoystickImpl
));
392 if (newDevice
== 0) {
393 WARN("out of memory\n");
395 return DIERR_OUTOFMEMORY
;
398 if (!lstrcpynA(newDevice
->dev
, joystick_devices
[index
], sizeof(newDevice
->dev
)) ||
399 (newDevice
->joyfd
= open(newDevice
->dev
, O_RDONLY
)) < 0)
401 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice
->dev
, strerror(errno
));
402 HeapFree(GetProcessHeap(), 0, newDevice
);
403 return DIERR_DEVICENOTREG
;
406 /* get the device name */
407 #if defined(JSIOCGNAME)
408 if (ioctl(newDevice
->joyfd
,JSIOCGNAME(MAX_PATH
),name
) < 0) {
409 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice
->dev
, strerror(errno
));
410 strcpy(name
, "Wine Joystick");
413 strcpy(name
, "Wine Joystick");
416 /* copy the device name */
417 newDevice
->name
= HeapAlloc(GetProcessHeap(),0,strlen(name
) + 1);
418 strcpy(newDevice
->name
, name
);
421 if (ioctl(newDevice
->joyfd
,JSIOCGAXES
,&newDevice
->axes
) < 0) {
422 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice
->dev
, strerror(errno
));
427 if (ioctl(newDevice
->joyfd
, JSIOCGBUTTONS
, &newDevice
->devcaps
.dwButtons
) < 0) {
428 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice
->dev
, strerror(errno
));
429 newDevice
->devcaps
.dwButtons
= 2;
433 if (newDevice
->devcaps
.dwButtons
> 128)
435 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice
->devcaps
.dwButtons
);
436 newDevice
->devcaps
.dwButtons
= 128;
439 newDevice
->base
.lpVtbl
= jvt
;
440 newDevice
->base
.ref
= 1;
441 newDevice
->base
.dinput
= dinput
;
442 newDevice
->base
.guid
= *rguid
;
443 InitializeCriticalSection(&newDevice
->base
.crit
);
444 newDevice
->base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JoystickImpl*->base.crit");
446 /* setup_dinput_options may change these */
447 newDevice
->deadzone
= 0;
449 /* do any user specified configuration */
450 hr
= setup_dinput_options(newDevice
);
454 /* Create copy of default data format */
455 if (!(df
= HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2
.dwSize
))) goto FAILED
;
456 memcpy(df
, &c_dfDIJoystick2
, c_dfDIJoystick2
.dwSize
);
458 df
->dwNumObjs
= newDevice
->devcaps
.dwAxes
+ newDevice
->devcaps
.dwPOVs
+ newDevice
->devcaps
.dwButtons
;
459 if (!(df
->rgodf
= HeapAlloc(GetProcessHeap(), 0, df
->dwNumObjs
* df
->dwObjSize
))) goto FAILED
;
461 for (i
= 0; i
< newDevice
->axes
; i
++)
463 int wine_obj
= newDevice
->axis_map
[i
];
465 if (wine_obj
< 0) continue;
467 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[wine_obj
], df
->dwObjSize
);
469 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(wine_obj
) | DIDFT_ABSAXIS
;
472 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(wine_obj
- 8) | DIDFT_POV
;
473 i
++; /* POV takes 2 axes */
476 for (i
= 0; i
< newDevice
->devcaps
.dwButtons
; i
++)
478 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[i
+ 12], df
->dwObjSize
);
479 df
->rgodf
[idx
].pguid
= &GUID_Button
;
480 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(i
) | DIDFT_PSHBUTTON
;
482 newDevice
->base
.data_format
.wine_df
= df
;
484 /* create default properties */
485 newDevice
->props
= HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2
.dwNumObjs
*sizeof(ObjProps
));
486 if (newDevice
->props
== 0)
489 /* initialize default properties */
490 for (i
= 0; i
< c_dfDIJoystick2
.dwNumObjs
; i
++) {
491 newDevice
->props
[i
].lDevMin
= -32767;
492 newDevice
->props
[i
].lDevMax
= +32767;
493 newDevice
->props
[i
].lMin
= 0;
494 newDevice
->props
[i
].lMax
= 0xffff;
495 newDevice
->props
[i
].lDeadZone
= newDevice
->deadzone
; /* % * 1000 */
496 newDevice
->props
[i
].lSaturation
= 0;
499 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A
)newDevice
->base
.dinput
);
501 newDevice
->devcaps
.dwSize
= sizeof(newDevice
->devcaps
);
502 newDevice
->devcaps
.dwFlags
= DIDC_ATTACHED
;
503 if (newDevice
->base
.dinput
->dwVersion
>= 0x0800)
504 newDevice
->devcaps
.dwDevType
= DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8);
506 newDevice
->devcaps
.dwDevType
= DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
507 newDevice
->devcaps
.dwFFSamplePeriod
= 0;
508 newDevice
->devcaps
.dwFFMinTimeResolution
= 0;
509 newDevice
->devcaps
.dwFirmwareRevision
= 0;
510 newDevice
->devcaps
.dwHardwareRevision
= 0;
511 newDevice
->devcaps
.dwFFDriverVersion
= 0;
513 if (TRACE_ON(dinput
)) {
514 _dump_DIDATAFORMAT(newDevice
->base
.data_format
.wine_df
);
515 for (i
= 0; i
< (newDevice
->axes
); i
++)
516 TRACE("axis_map[%d] = %d\n", i
, newDevice
->axis_map
[i
]);
517 _dump_DIDEVCAPS(&newDevice
->devcaps
);
520 *pdev
= (LPDIRECTINPUTDEVICEA
)newDevice
;
525 hr
= DIERR_OUTOFMEMORY
;
527 if (df
) HeapFree(GetProcessHeap(), 0, df
->rgodf
);
528 HeapFree(GetProcessHeap(), 0, df
);
529 release_DataFormat(&newDevice
->base
.data_format
);
530 HeapFree(GetProcessHeap(),0,newDevice
->axis_map
);
531 HeapFree(GetProcessHeap(),0,newDevice
->name
);
532 HeapFree(GetProcessHeap(),0,newDevice
->props
);
533 HeapFree(GetProcessHeap(),0,newDevice
);
539 /******************************************************************************
540 * get_joystick_index : Get the joystick index from a given GUID
542 static unsigned short get_joystick_index(REFGUID guid
)
544 GUID wine_joystick
= DInput_Wine_Joystick_GUID
;
545 GUID dev_guid
= *guid
;
547 wine_joystick
.Data3
= 0;
550 /* for the standard joystick GUID use index 0 */
551 if(IsEqualGUID(&GUID_Joystick
,guid
)) return 0;
553 /* for the wine joystick GUIDs use the index stored in Data3 */
554 if(IsEqualGUID(&wine_joystick
, &dev_guid
)) return guid
->Data3
;
556 return MAX_JOYSTICKS
;
559 static HRESULT
joydev_create_deviceA(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPDIRECTINPUTDEVICEA
* pdev
)
561 unsigned short index
;
563 TRACE("%p %s %p %p\n",dinput
, debugstr_guid(rguid
), riid
, pdev
);
564 find_joystick_devices();
567 if ((index
= get_joystick_index(rguid
)) < MAX_JOYSTICKS
&&
568 joystick_devices_count
&& index
< joystick_devices_count
)
570 if ((riid
== NULL
) ||
571 IsEqualGUID(&IID_IDirectInputDeviceA
, riid
) ||
572 IsEqualGUID(&IID_IDirectInputDevice2A
, riid
) ||
573 IsEqualGUID(&IID_IDirectInputDevice7A
, riid
) ||
574 IsEqualGUID(&IID_IDirectInputDevice8A
, riid
))
576 return alloc_device(rguid
, &JoystickAvt
, dinput
, pdev
, index
);
579 WARN("no interface\n");
580 return DIERR_NOINTERFACE
;
583 return DIERR_DEVICENOTREG
;
586 static HRESULT
joydev_create_deviceW(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPDIRECTINPUTDEVICEW
* pdev
)
588 unsigned short index
;
590 TRACE("%p %s %p %p\n",dinput
, debugstr_guid(rguid
), riid
, pdev
);
591 find_joystick_devices();
594 if ((index
= get_joystick_index(rguid
)) < MAX_JOYSTICKS
&&
595 joystick_devices_count
&& index
< joystick_devices_count
)
597 if ((riid
== NULL
) ||
598 IsEqualGUID(&IID_IDirectInputDeviceW
, riid
) ||
599 IsEqualGUID(&IID_IDirectInputDevice2W
, riid
) ||
600 IsEqualGUID(&IID_IDirectInputDevice7W
, riid
) ||
601 IsEqualGUID(&IID_IDirectInputDevice8W
, riid
))
603 return alloc_device(rguid
, &JoystickWvt
, dinput
, (LPDIRECTINPUTDEVICEA
*)pdev
, index
);
605 WARN("no interface\n");
606 return DIERR_NOINTERFACE
;
609 WARN("invalid device GUID %s\n",debugstr_guid(rguid
));
610 return DIERR_DEVICENOTREG
;
615 const struct dinput_device joystick_linux_device
= {
616 "Wine Linux joystick driver",
619 joydev_create_deviceA
,
620 joydev_create_deviceW
623 /******************************************************************************
624 * Acquire : gets exclusive control of the joystick
626 static HRESULT WINAPI
JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface
)
628 JoystickImpl
*This
= (JoystickImpl
*)iface
;
630 TRACE("(%p)\n",This
);
632 if (This
->base
.acquired
) {
633 WARN("already acquired\n");
637 /* open the joystick device */
638 if (This
->joyfd
==-1) {
639 TRACE("opening joystick device %s\n", This
->dev
);
641 This
->joyfd
=open(This
->dev
,O_RDONLY
);
642 if (This
->joyfd
==-1) {
643 ERR("open(%s) failed: %s\n", This
->dev
, strerror(errno
));
644 return DIERR_NOTFOUND
;
648 This
->base
.acquired
= 1;
653 /******************************************************************************
654 * Unacquire : frees the joystick
656 static HRESULT WINAPI
JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface
)
658 JoystickImpl
*This
= (JoystickImpl
*)iface
;
661 TRACE("(%p)\n",This
);
663 if ((res
= IDirectInputDevice2AImpl_Unacquire(iface
)) != DI_OK
) return res
;
665 if (This
->joyfd
!=-1) {
666 TRACE("closing joystick device\n");
675 static void joy_polldev(JoystickImpl
*This
) {
678 TRACE("(%p)\n", This
);
680 if (This
->joyfd
==-1) {
689 plfd
.fd
= This
->joyfd
;
690 plfd
.events
= POLLIN
;
691 if (poll(&plfd
,1,0) != 1)
693 /* we have one event, so we can read */
694 if (sizeof(jse
)!=read(This
->joyfd
,&jse
,sizeof(jse
))) {
697 TRACE("js_event: type 0x%x, number %d, value %d\n",
698 jse
.type
,jse
.number
,jse
.value
);
699 if (jse
.type
& JS_EVENT_BUTTON
)
701 if (jse
.number
>= This
->devcaps
.dwButtons
) return;
703 inst_id
= DIDFT_MAKEINSTANCE(jse
.number
) | DIDFT_PSHBUTTON
;
704 This
->js
.rgbButtons
[jse
.number
] = value
= jse
.value
? 0x80 : 0x00;
706 else if (jse
.type
& JS_EVENT_AXIS
)
708 int number
= This
->axis_map
[jse
.number
]; /* wine format object index */
710 if (number
< 0) return;
711 inst_id
= DIDFT_MAKEINSTANCE(number
) | (number
< 8 ? DIDFT_ABSAXIS
: DIDFT_POV
);
712 value
= joystick_map_axis(&This
->props
[id_to_object(This
->base
.data_format
.wine_df
, inst_id
)], jse
.value
);
714 TRACE("changing axis %d => %d\n", jse
.number
, number
);
717 case 0: This
->js
.lX
= value
; break;
718 case 1: This
->js
.lY
= value
; break;
719 case 2: This
->js
.lZ
= value
; break;
720 case 3: This
->js
.lRx
= value
; break;
721 case 4: This
->js
.lRy
= value
; break;
722 case 5: This
->js
.lRz
= value
; break;
723 case 6: This
->js
.rglSlider
[0] = value
; break;
724 case 7: This
->js
.rglSlider
[1] = value
; break;
725 case 8: case 9: case 10: case 11:
727 int idx
= number
- 8;
730 This
->povs
[idx
].y
= jse
.value
;
732 This
->povs
[idx
].x
= jse
.value
;
734 This
->js
.rgdwPOV
[idx
] = value
= joystick_map_pov(&This
->povs
[idx
]);
738 WARN("axis %d not supported\n", number
);
742 queue_event((LPDIRECTINPUTDEVICE8A
)This
,
743 id_to_offset(&This
->base
.data_format
, inst_id
),
744 value
, jse
.time
, This
->base
.dinput
->evsequence
++);
748 /******************************************************************************
749 * GetDeviceState : returns the "state" of the joystick.
752 static HRESULT WINAPI
JoystickAImpl_GetDeviceState(
753 LPDIRECTINPUTDEVICE8A iface
,
757 JoystickImpl
*This
= (JoystickImpl
*)iface
;
759 TRACE("(%p,0x%08x,%p)\n", This
, len
, ptr
);
761 if (!This
->base
.acquired
) {
762 WARN("not acquired\n");
763 return DIERR_NOTACQUIRED
;
766 /* update joystick state */
769 /* convert and copy data to user supplied buffer */
770 fill_DataFormat(ptr
, &This
->js
, &This
->base
.data_format
);
775 /******************************************************************************
776 * SetProperty : change input device properties
778 static HRESULT WINAPI
JoystickAImpl_SetProperty(
779 LPDIRECTINPUTDEVICE8A iface
,
783 JoystickImpl
*This
= (JoystickImpl
*)iface
;
786 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(rguid
),ph
);
789 WARN("invalid parameter: ph == NULL\n");
790 return DIERR_INVALIDPARAM
;
793 if (TRACE_ON(dinput
))
794 _dump_DIPROPHEADER(ph
);
796 if (!HIWORD(rguid
)) {
797 switch (LOWORD(rguid
)) {
798 case (DWORD
)DIPROP_RANGE
: {
799 LPCDIPROPRANGE pr
= (LPCDIPROPRANGE
)ph
;
800 if (ph
->dwHow
== DIPH_DEVICE
) {
801 TRACE("proprange(%d,%d) all\n", pr
->lMin
, pr
->lMax
);
802 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++) {
803 This
->props
[i
].lMin
= pr
->lMin
;
804 This
->props
[i
].lMax
= pr
->lMax
;
807 int obj
= find_property(&This
->base
.data_format
, ph
);
809 TRACE("proprange(%d,%d) obj=%d\n", pr
->lMin
, pr
->lMax
, obj
);
811 This
->props
[obj
].lMin
= pr
->lMin
;
812 This
->props
[obj
].lMax
= pr
->lMax
;
818 case (DWORD
)DIPROP_DEADZONE
: {
819 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
820 if (ph
->dwHow
== DIPH_DEVICE
) {
821 TRACE("deadzone(%d) all\n", pd
->dwData
);
822 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++)
823 This
->props
[i
].lDeadZone
= pd
->dwData
;
825 int obj
= find_property(&This
->base
.data_format
, ph
);
827 TRACE("deadzone(%d) obj=%d\n", pd
->dwData
, obj
);
829 This
->props
[obj
].lDeadZone
= pd
->dwData
;
835 case (DWORD
)DIPROP_SATURATION
: {
836 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
837 if (ph
->dwHow
== DIPH_DEVICE
) {
838 TRACE("saturation(%d) all\n", pd
->dwData
);
839 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++)
840 This
->props
[i
].lSaturation
= pd
->dwData
;
842 int obj
= find_property(&This
->base
.data_format
, ph
);
844 TRACE("saturation(%d) obj=%d\n", pd
->dwData
, obj
);
846 This
->props
[obj
].lSaturation
= pd
->dwData
;
853 return IDirectInputDevice2AImpl_SetProperty(iface
, rguid
, ph
);
860 static HRESULT WINAPI
JoystickAImpl_GetCapabilities(
861 LPDIRECTINPUTDEVICE8A iface
,
862 LPDIDEVCAPS lpDIDevCaps
)
864 JoystickImpl
*This
= (JoystickImpl
*)iface
;
867 TRACE("%p->(%p)\n",iface
,lpDIDevCaps
);
869 if (lpDIDevCaps
== NULL
) {
870 WARN("invalid pointer\n");
874 size
= lpDIDevCaps
->dwSize
;
876 if (!(size
== sizeof(DIDEVCAPS
) || size
== sizeof(DIDEVCAPS_DX3
))) {
877 WARN("invalid parameter\n");
878 return DIERR_INVALIDPARAM
;
881 CopyMemory(lpDIDevCaps
, &This
->devcaps
, size
);
882 lpDIDevCaps
->dwSize
= size
;
884 if (TRACE_ON(dinput
))
885 _dump_DIDEVCAPS(lpDIDevCaps
);
890 static HRESULT WINAPI
JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface
)
892 JoystickImpl
*This
= (JoystickImpl
*)iface
;
894 TRACE("(%p)\n",This
);
896 if (!This
->base
.acquired
) {
897 WARN("not acquired\n");
898 return DIERR_NOTACQUIRED
;
905 /******************************************************************************
906 * GetProperty : get input device properties
908 static HRESULT WINAPI
JoystickAImpl_GetProperty(
909 LPDIRECTINPUTDEVICE8A iface
,
911 LPDIPROPHEADER pdiph
)
913 JoystickImpl
*This
= (JoystickImpl
*)iface
;
915 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(rguid
), pdiph
);
917 if (TRACE_ON(dinput
))
918 _dump_DIPROPHEADER(pdiph
);
920 if (!HIWORD(rguid
)) {
921 switch (LOWORD(rguid
)) {
922 case (DWORD
) DIPROP_RANGE
: {
923 LPDIPROPRANGE pr
= (LPDIPROPRANGE
)pdiph
;
924 int obj
= find_property(&This
->base
.data_format
, pdiph
);
926 /* The app is querying the current range of the axis
927 * return the lMin and lMax values */
929 pr
->lMin
= This
->props
[obj
].lMin
;
930 pr
->lMax
= This
->props
[obj
].lMax
;
931 TRACE("range(%d, %d) obj=%d\n", pr
->lMin
, pr
->lMax
, obj
);
936 case (DWORD
) DIPROP_DEADZONE
: {
937 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
938 int obj
= find_property(&This
->base
.data_format
, pdiph
);
941 pd
->dwData
= This
->props
[obj
].lDeadZone
;
942 TRACE("deadzone(%d) obj=%d\n", pd
->dwData
, obj
);
947 case (DWORD
) DIPROP_SATURATION
: {
948 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
949 int obj
= find_property(&This
->base
.data_format
, pdiph
);
952 pd
->dwData
= This
->props
[obj
].lSaturation
;
953 TRACE("saturation(%d) obj=%d\n", pd
->dwData
, obj
);
959 return IDirectInputDevice2AImpl_GetProperty(iface
, rguid
, pdiph
);
966 /******************************************************************************
967 * GetObjectInfo : get object info
969 static HRESULT WINAPI
JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface
,
970 LPDIDEVICEOBJECTINSTANCEW pdidoi
, DWORD dwObj
, DWORD dwHow
)
972 static const WCHAR axisW
[] = {'A','x','i','s',' ','%','d',0};
973 static const WCHAR povW
[] = {'P','O','V',' ','%','d',0};
974 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',' ','%','d',0};
977 res
= IDirectInputDevice2WImpl_GetObjectInfo(iface
, pdidoi
, dwObj
, dwHow
);
978 if (res
!= DI_OK
) return res
;
980 if (pdidoi
->dwType
& DIDFT_AXIS
)
981 sprintfW(pdidoi
->tszName
, axisW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
982 else if (pdidoi
->dwType
& DIDFT_POV
)
983 sprintfW(pdidoi
->tszName
, povW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
984 else if (pdidoi
->dwType
& DIDFT_BUTTON
)
985 sprintfW(pdidoi
->tszName
, buttonW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
987 _dump_OBJECTINSTANCEW(pdidoi
);
991 static HRESULT WINAPI
JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface
,
992 LPDIDEVICEOBJECTINSTANCEA pdidoi
, DWORD dwObj
, DWORD dwHow
)
995 DIDEVICEOBJECTINSTANCEW didoiW
;
996 DWORD dwSize
= pdidoi
->dwSize
;
998 didoiW
.dwSize
= sizeof(didoiW
);
999 res
= JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W
)iface
, &didoiW
, dwObj
, dwHow
);
1000 if (res
!= DI_OK
) return res
;
1002 memset(pdidoi
, 0, pdidoi
->dwSize
);
1003 memcpy(pdidoi
, &didoiW
, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW
, tszName
));
1004 pdidoi
->dwSize
= dwSize
;
1005 WideCharToMultiByte(CP_ACP
, 0, didoiW
.tszName
, -1, pdidoi
->tszName
,
1006 sizeof(pdidoi
->tszName
), NULL
, NULL
);
1011 /******************************************************************************
1012 * GetDeviceInfo : get information about a device's identity
1014 static HRESULT WINAPI
JoystickAImpl_GetDeviceInfo(
1015 LPDIRECTINPUTDEVICE8A iface
,
1016 LPDIDEVICEINSTANCEA pdidi
)
1018 JoystickImpl
*This
= (JoystickImpl
*)iface
;
1020 TRACE("(%p,%p)\n", iface
, pdidi
);
1022 if (pdidi
== NULL
) {
1023 WARN("invalid pointer\n");
1027 if ((pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3A
)) &&
1028 (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEA
))) {
1029 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1030 pdidi
->dwSize
, sizeof(DIDEVICEINSTANCE_DX3A
),
1031 sizeof(DIDEVICEINSTANCEA
));
1032 return DIERR_INVALIDPARAM
;
1035 /* Return joystick */
1036 pdidi
->guidInstance
= GUID_Joystick
;
1037 pdidi
->guidProduct
= DInput_Wine_Joystick_GUID
;
1038 /* we only support traditional joysticks for now */
1039 pdidi
->dwDevType
= This
->devcaps
.dwDevType
;
1040 strcpy(pdidi
->tszInstanceName
, "Joystick");
1041 strcpy(pdidi
->tszProductName
, This
->name
);
1042 if (pdidi
->dwSize
> sizeof(DIDEVICEINSTANCE_DX3A
)) {
1043 pdidi
->guidFFDriver
= GUID_NULL
;
1044 pdidi
->wUsagePage
= 0;
1051 /******************************************************************************
1052 * GetDeviceInfo : get information about a device's identity
1054 static HRESULT WINAPI
JoystickWImpl_GetDeviceInfo(
1055 LPDIRECTINPUTDEVICE8W iface
,
1056 LPDIDEVICEINSTANCEW pdidi
)
1058 JoystickImpl
*This
= (JoystickImpl
*)iface
;
1060 TRACE("(%p,%p)\n", iface
, pdidi
);
1062 if ((pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3W
)) &&
1063 (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEW
))) {
1064 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1065 pdidi
->dwSize
, sizeof(DIDEVICEINSTANCE_DX3W
),
1066 sizeof(DIDEVICEINSTANCEW
));
1067 return DIERR_INVALIDPARAM
;
1070 /* Return joystick */
1071 pdidi
->guidInstance
= GUID_Joystick
;
1072 pdidi
->guidProduct
= DInput_Wine_Joystick_GUID
;
1073 /* we only support traditional joysticks for now */
1074 pdidi
->dwDevType
= This
->devcaps
.dwDevType
;
1075 MultiByteToWideChar(CP_ACP
, 0, "Joystick", -1, pdidi
->tszInstanceName
, MAX_PATH
);
1076 MultiByteToWideChar(CP_ACP
, 0, This
->name
, -1, pdidi
->tszProductName
, MAX_PATH
);
1077 if (pdidi
->dwSize
> sizeof(DIDEVICEINSTANCE_DX3W
)) {
1078 pdidi
->guidFFDriver
= GUID_NULL
;
1079 pdidi
->wUsagePage
= 0;
1086 static const IDirectInputDevice8AVtbl JoystickAvt
=
1088 IDirectInputDevice2AImpl_QueryInterface
,
1089 IDirectInputDevice2AImpl_AddRef
,
1090 IDirectInputDevice2AImpl_Release
,
1091 JoystickAImpl_GetCapabilities
,
1092 IDirectInputDevice2AImpl_EnumObjects
,
1093 JoystickAImpl_GetProperty
,
1094 JoystickAImpl_SetProperty
,
1095 JoystickAImpl_Acquire
,
1096 JoystickAImpl_Unacquire
,
1097 JoystickAImpl_GetDeviceState
,
1098 IDirectInputDevice2AImpl_GetDeviceData
,
1099 IDirectInputDevice2AImpl_SetDataFormat
,
1100 IDirectInputDevice2AImpl_SetEventNotification
,
1101 IDirectInputDevice2AImpl_SetCooperativeLevel
,
1102 JoystickAImpl_GetObjectInfo
,
1103 JoystickAImpl_GetDeviceInfo
,
1104 IDirectInputDevice2AImpl_RunControlPanel
,
1105 IDirectInputDevice2AImpl_Initialize
,
1106 IDirectInputDevice2AImpl_CreateEffect
,
1107 IDirectInputDevice2AImpl_EnumEffects
,
1108 IDirectInputDevice2AImpl_GetEffectInfo
,
1109 IDirectInputDevice2AImpl_GetForceFeedbackState
,
1110 IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
1111 IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
1112 IDirectInputDevice2AImpl_Escape
,
1114 IDirectInputDevice2AImpl_SendDeviceData
,
1115 IDirectInputDevice7AImpl_EnumEffectsInFile
,
1116 IDirectInputDevice7AImpl_WriteEffectToFile
,
1117 IDirectInputDevice8AImpl_BuildActionMap
,
1118 IDirectInputDevice8AImpl_SetActionMap
,
1119 IDirectInputDevice8AImpl_GetImageInfo
1122 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1123 # define XCAST(fun) (typeof(JoystickWvt.fun))
1125 # define XCAST(fun) (void*)
1128 static const IDirectInputDevice8WVtbl JoystickWvt
=
1130 IDirectInputDevice2WImpl_QueryInterface
,
1131 XCAST(AddRef
)IDirectInputDevice2AImpl_AddRef
,
1132 XCAST(Release
)IDirectInputDevice2AImpl_Release
,
1133 XCAST(GetCapabilities
)JoystickAImpl_GetCapabilities
,
1134 IDirectInputDevice2WImpl_EnumObjects
,
1135 XCAST(GetProperty
)JoystickAImpl_GetProperty
,
1136 XCAST(SetProperty
)JoystickAImpl_SetProperty
,
1137 XCAST(Acquire
)JoystickAImpl_Acquire
,
1138 XCAST(Unacquire
)JoystickAImpl_Unacquire
,
1139 XCAST(GetDeviceState
)JoystickAImpl_GetDeviceState
,
1140 XCAST(GetDeviceData
)IDirectInputDevice2AImpl_GetDeviceData
,
1141 XCAST(SetDataFormat
)IDirectInputDevice2AImpl_SetDataFormat
,
1142 XCAST(SetEventNotification
)IDirectInputDevice2AImpl_SetEventNotification
,
1143 XCAST(SetCooperativeLevel
)IDirectInputDevice2AImpl_SetCooperativeLevel
,
1144 IDirectInputDevice2WImpl_GetObjectInfo
,
1145 JoystickWImpl_GetDeviceInfo
,
1146 XCAST(RunControlPanel
)IDirectInputDevice2AImpl_RunControlPanel
,
1147 XCAST(Initialize
)IDirectInputDevice2AImpl_Initialize
,
1148 XCAST(CreateEffect
)IDirectInputDevice2AImpl_CreateEffect
,
1149 IDirectInputDevice2WImpl_EnumEffects
,
1150 IDirectInputDevice2WImpl_GetEffectInfo
,
1151 XCAST(GetForceFeedbackState
)IDirectInputDevice2AImpl_GetForceFeedbackState
,
1152 XCAST(SendForceFeedbackCommand
)IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
1153 XCAST(EnumCreatedEffectObjects
)IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
1154 XCAST(Escape
)IDirectInputDevice2AImpl_Escape
,
1155 XCAST(Poll
)JoystickAImpl_Poll
,
1156 XCAST(SendDeviceData
)IDirectInputDevice2AImpl_SendDeviceData
,
1157 IDirectInputDevice7WImpl_EnumEffectsInFile
,
1158 IDirectInputDevice7WImpl_WriteEffectToFile
,
1159 IDirectInputDevice8WImpl_BuildActionMap
,
1160 IDirectInputDevice8WImpl_SetActionMap
,
1161 IDirectInputDevice8WImpl_GetImageInfo
1165 #else /* HAVE_LINUX_22_JOYSTICK_API */
1167 const struct dinput_device joystick_linux_device
= {
1168 "Wine Linux joystick driver",
1175 #endif /* HAVE_LINUX_22_JOYSTICK_API */