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 */
100 static const GUID DInput_Wine_Joystick_GUID
= { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
104 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
107 static void _dump_DIDEVCAPS(const DIDEVCAPS
*lpDIDevCaps
)
109 TRACE("dwSize: %d\n", lpDIDevCaps
->dwSize
);
110 TRACE("dwFlags: %08x\n", lpDIDevCaps
->dwFlags
);
111 TRACE("dwDevType: %08x %s\n", lpDIDevCaps
->dwDevType
,
112 lpDIDevCaps
->dwDevType
== DIDEVTYPE_DEVICE
? "DIDEVTYPE_DEVICE" :
113 lpDIDevCaps
->dwDevType
== DIDEVTYPE_DEVICE
? "DIDEVTYPE_DEVICE" :
114 lpDIDevCaps
->dwDevType
== DIDEVTYPE_MOUSE
? "DIDEVTYPE_MOUSE" :
115 lpDIDevCaps
->dwDevType
== DIDEVTYPE_KEYBOARD
? "DIDEVTYPE_KEYBOARD" :
116 lpDIDevCaps
->dwDevType
== DIDEVTYPE_JOYSTICK
? "DIDEVTYPE_JOYSTICK" :
117 lpDIDevCaps
->dwDevType
== DIDEVTYPE_HID
? "DIDEVTYPE_HID" : "UNKNOWN");
118 TRACE("dwAxes: %d\n", lpDIDevCaps
->dwAxes
);
119 TRACE("dwButtons: %d\n", lpDIDevCaps
->dwButtons
);
120 TRACE("dwPOVs: %d\n", lpDIDevCaps
->dwPOVs
);
121 if (lpDIDevCaps
->dwSize
> sizeof(DIDEVCAPS_DX3
)) {
122 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps
->dwFFSamplePeriod
);
123 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps
->dwFFMinTimeResolution
);
124 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps
->dwFirmwareRevision
);
125 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps
->dwHardwareRevision
);
126 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps
->dwFFDriverVersion
);
130 #define MAX_JOYSTICKS 64
131 static INT joystick_devices_count
= -1;
132 static LPSTR joystick_devices
[MAX_JOYSTICKS
];
134 static INT
find_joystick_devices(void)
138 if (joystick_devices_count
!= -1) return joystick_devices_count
;
140 joystick_devices_count
= 0;
141 for (i
= 0; i
< MAX_JOYSTICKS
; i
++)
143 CHAR device_name
[MAX_PATH
], *str
;
147 len
= sprintf(device_name
, "%s%d", JOYDEV_NEW
, i
) + 1;
148 if ((fd
= open(device_name
, O_RDONLY
)) < 0)
150 len
= sprintf(device_name
, "%s%d", JOYDEV_OLD
, i
) + 1;
151 if ((fd
= open(device_name
, O_RDONLY
)) < 0) continue;
156 if (!(str
= HeapAlloc(GetProcessHeap(), 0, len
))) break;
157 memcpy(str
, device_name
, len
);
159 joystick_devices
[joystick_devices_count
++] = str
;
162 return joystick_devices_count
;
165 static BOOL
joydev_enum_deviceA(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEA lpddi
, DWORD version
, int id
)
169 if (id
>= find_joystick_devices()) return FALSE
;
171 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
172 WARN("force feedback not supported\n");
176 if ((dwDevType
== 0) ||
177 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
> 0x0300 && version
< 0x0800)) ||
178 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
179 /* check whether we have a joystick */
180 if ((fd
= open(joystick_devices
[id
], O_RDONLY
)) < 0)
182 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices
[id
], strerror(errno
));
186 /* Return joystick */
187 lpddi
->guidInstance
= DInput_Wine_Joystick_GUID
;
188 lpddi
->guidInstance
.Data3
= id
;
189 lpddi
->guidProduct
= DInput_Wine_Joystick_GUID
;
190 /* we only support traditional joysticks for now */
191 if (version
>= 0x0800)
192 lpddi
->dwDevType
= DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8);
194 lpddi
->dwDevType
= DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
195 sprintf(lpddi
->tszInstanceName
, "Joystick %d", id
);
196 #if defined(JSIOCGNAME)
197 if (ioctl(fd
,JSIOCGNAME(sizeof(lpddi
->tszProductName
)),lpddi
->tszProductName
) < 0) {
198 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices
[id
], strerror(errno
));
199 strcpy(lpddi
->tszProductName
, "Wine Joystick");
202 strcpy(lpddi
->tszProductName
, "Wine Joystick");
205 lpddi
->guidFFDriver
= GUID_NULL
;
207 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices
[id
], lpddi
->tszProductName
);
214 static BOOL
joydev_enum_deviceW(DWORD dwDevType
, DWORD dwFlags
, LPDIDEVICEINSTANCEW lpddi
, DWORD version
, int id
)
220 if (id
>= find_joystick_devices()) return FALSE
;
222 if (dwFlags
& DIEDFL_FORCEFEEDBACK
) {
223 WARN("force feedback not supported\n");
227 if ((dwDevType
== 0) ||
228 ((dwDevType
== DIDEVTYPE_JOYSTICK
) && (version
> 0x0300 && version
< 0x0800)) ||
229 (((dwDevType
== DI8DEVCLASS_GAMECTRL
) || (dwDevType
== DI8DEVTYPE_JOYSTICK
)) && (version
>= 0x0800))) {
230 /* check whether we have a joystick */
231 if ((fd
= open(joystick_devices
[id
], O_RDONLY
)) < 0)
233 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices
[id
], strerror(errno
));
237 /* Return joystick */
238 lpddi
->guidInstance
= DInput_Wine_Joystick_GUID
;
239 lpddi
->guidInstance
.Data3
= id
;
240 lpddi
->guidProduct
= DInput_Wine_Joystick_GUID
;
241 /* we only support traditional joysticks for now */
242 if (version
>= 0x0800)
243 lpddi
->dwDevType
= DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8);
245 lpddi
->dwDevType
= DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
246 sprintf(friendly
, "Joystick %d", id
);
247 MultiByteToWideChar(CP_ACP
, 0, friendly
, -1, lpddi
->tszInstanceName
, MAX_PATH
);
248 #if defined(JSIOCGNAME)
249 if (ioctl(fd
,JSIOCGNAME(sizeof(name
)),name
) < 0) {
250 WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices
[id
], strerror(errno
));
251 strcpy(name
, "Wine Joystick");
254 strcpy(name
, "Wine Joystick");
256 MultiByteToWideChar(CP_ACP
, 0, name
, -1, lpddi
->tszProductName
, MAX_PATH
);
257 lpddi
->guidFFDriver
= GUID_NULL
;
259 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices
[id
], name
);
267 * Setup the dinput options.
270 static HRESULT
setup_dinput_options(JoystickImpl
* device
)
272 char buffer
[MAX_PATH
+16];
275 buffer
[MAX_PATH
]='\0';
277 get_app_key(&hkey
, &appkey
);
281 if (!get_config_key( hkey
, appkey
, "DefaultDeadZone", buffer
, MAX_PATH
)) {
282 device
->deadzone
= atoi(buffer
);
283 TRACE("setting default deadzone to: \"%s\" %d\n", buffer
, device
->deadzone
);
286 if (!get_config_key( hkey
, appkey
, device
->name
, buffer
, MAX_PATH
)) {
290 const char *delim
= ",";
292 TRACE("\"%s\" = \"%s\"\n", device
->name
, buffer
);
294 device
->axis_map
= HeapAlloc(GetProcessHeap(), 0, device
->axes
* sizeof(int));
295 if (device
->axis_map
== 0)
296 return DIERR_OUTOFMEMORY
;
298 if ((ptr
= strtok(buffer
, delim
)) != NULL
) {
300 if (strcmp(ptr
, "X") == 0) {
301 device
->axis_map
[tokens
] = 0;
303 } else if (strcmp(ptr
, "Y") == 0) {
304 device
->axis_map
[tokens
] = 1;
306 } else if (strcmp(ptr
, "Z") == 0) {
307 device
->axis_map
[tokens
] = 2;
309 } else if (strcmp(ptr
, "Rx") == 0) {
310 device
->axis_map
[tokens
] = 3;
312 } else if (strcmp(ptr
, "Ry") == 0) {
313 device
->axis_map
[tokens
] = 4;
315 } else if (strcmp(ptr
, "Rz") == 0) {
316 device
->axis_map
[tokens
] = 5;
318 } else if (strcmp(ptr
, "Slider1") == 0) {
319 device
->axis_map
[tokens
] = 6;
321 } else if (strcmp(ptr
, "Slider2") == 0) {
322 device
->axis_map
[tokens
] = 7;
324 } else if (strcmp(ptr
, "POV1") == 0) {
325 device
->axis_map
[tokens
++] = 8;
326 device
->axis_map
[tokens
] = 8;
328 } else if (strcmp(ptr
, "POV2") == 0) {
329 device
->axis_map
[tokens
++] = 9;
330 device
->axis_map
[tokens
] = 9;
332 } else if (strcmp(ptr
, "POV3") == 0) {
333 device
->axis_map
[tokens
++] = 10;
334 device
->axis_map
[tokens
] = 10;
336 } else if (strcmp(ptr
, "POV4") == 0) {
337 device
->axis_map
[tokens
++] = 11;
338 device
->axis_map
[tokens
] = 11;
341 ERR("invalid joystick axis type: %s\n", ptr
);
342 device
->axis_map
[tokens
] = tokens
;
347 } while ((ptr
= strtok(NULL
, delim
)) != NULL
);
349 if (tokens
!= device
->devcaps
.dwAxes
) {
350 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device
->axes
, axis
, pov
,tokens
);
351 while (tokens
< device
->axes
) {
352 device
->axis_map
[tokens
] = tokens
;
358 device
->devcaps
.dwAxes
= axis
;
359 device
->devcaps
.dwPOVs
= pov
;
363 RegCloseKey( appkey
);
371 static HRESULT
alloc_device(REFGUID rguid
, const void *jvt
, IDirectInputImpl
*dinput
,
372 LPDIRECTINPUTDEVICEA
* pdev
, unsigned short index
)
375 JoystickImpl
* newDevice
;
378 LPDIDATAFORMAT df
= NULL
;
381 TRACE("%s %p %p %p %hu\n", debugstr_guid(rguid
), jvt
, dinput
, pdev
, index
);
383 newDevice
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(JoystickImpl
));
384 if (newDevice
== 0) {
385 WARN("out of memory\n");
387 return DIERR_OUTOFMEMORY
;
390 if (!lstrcpynA(newDevice
->dev
, joystick_devices
[index
], sizeof(newDevice
->dev
)) ||
391 (newDevice
->joyfd
= open(newDevice
->dev
, O_RDONLY
)) < 0)
393 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice
->dev
, strerror(errno
));
394 HeapFree(GetProcessHeap(), 0, newDevice
);
395 return DIERR_DEVICENOTREG
;
398 /* get the device name */
399 #if defined(JSIOCGNAME)
400 if (ioctl(newDevice
->joyfd
,JSIOCGNAME(MAX_PATH
),name
) < 0) {
401 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice
->dev
, strerror(errno
));
402 strcpy(name
, "Wine Joystick");
405 strcpy(name
, "Wine Joystick");
408 /* copy the device name */
409 newDevice
->name
= HeapAlloc(GetProcessHeap(),0,strlen(name
) + 1);
410 strcpy(newDevice
->name
, name
);
413 if (ioctl(newDevice
->joyfd
,JSIOCGAXES
,&newDevice
->axes
) < 0) {
414 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice
->dev
, strerror(errno
));
419 if (ioctl(newDevice
->joyfd
,JSIOCGBUTTONS
,&newDevice
->buttons
) < 0) {
420 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice
->dev
, strerror(errno
));
421 newDevice
->buttons
= 2;
425 newDevice
->base
.lpVtbl
= jvt
;
426 newDevice
->base
.ref
= 1;
427 newDevice
->base
.dinput
= dinput
;
428 newDevice
->base
.guid
= *rguid
;
429 InitializeCriticalSection(&newDevice
->base
.crit
);
430 newDevice
->base
.crit
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": JoystickImpl*->base.crit");
432 /* setup_dinput_options may change these */
433 newDevice
->deadzone
= 0;
434 newDevice
->devcaps
.dwButtons
= newDevice
->buttons
;
435 newDevice
->devcaps
.dwAxes
= newDevice
->axes
;
436 newDevice
->devcaps
.dwPOVs
= 0;
438 /* do any user specified configuration */
439 hr
= setup_dinput_options(newDevice
);
443 if (newDevice
->axis_map
== 0) {
444 newDevice
->axis_map
= HeapAlloc(GetProcessHeap(), 0, newDevice
->axes
* sizeof(int));
445 if (newDevice
->axis_map
== 0)
448 for (i
= 0; i
< newDevice
->axes
; i
++)
449 newDevice
->axis_map
[i
] = i
;
452 /* Create copy of default data format */
453 if (!(df
= HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2
.dwSize
))) goto FAILED
;
454 memcpy(df
, &c_dfDIJoystick2
, c_dfDIJoystick2
.dwSize
);
456 /* Axes include POVs */
457 df
->dwNumObjs
= newDevice
->axes
+ newDevice
->buttons
;
458 if (!(df
->rgodf
= HeapAlloc(GetProcessHeap(), 0, df
->dwNumObjs
* df
->dwObjSize
))) goto FAILED
;
460 for (i
= 0; i
< newDevice
->axes
; i
++)
462 int wine_obj
= newDevice
->axis_map
[i
];
464 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[wine_obj
], df
->dwObjSize
);
466 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(wine_obj
) | DIDFT_ABSAXIS
;
469 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(wine_obj
- 8) | DIDFT_POV
;
470 i
++; /* POV takes 2 axes */
473 for (i
= 0; i
< newDevice
->buttons
; i
++)
475 memcpy(&df
->rgodf
[idx
], &c_dfDIJoystick2
.rgodf
[i
+ 12], df
->dwObjSize
);
476 df
->rgodf
[idx
].pguid
= &GUID_Button
;
477 df
->rgodf
[idx
++].dwType
= DIDFT_MAKEINSTANCE(i
) | DIDFT_PSHBUTTON
;
479 newDevice
->base
.data_format
.wine_df
= df
;
481 /* create default properties */
482 newDevice
->props
= HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2
.dwNumObjs
*sizeof(ObjProps
));
483 if (newDevice
->props
== 0)
486 /* initialize default properties */
487 for (i
= 0; i
< c_dfDIJoystick2
.dwNumObjs
; i
++) {
488 newDevice
->props
[i
].lDevMin
= -32767;
489 newDevice
->props
[i
].lDevMax
= +32767;
490 newDevice
->props
[i
].lMin
= 0;
491 newDevice
->props
[i
].lMax
= 0xffff;
492 newDevice
->props
[i
].lDeadZone
= newDevice
->deadzone
; /* % * 1000 */
493 newDevice
->props
[i
].lSaturation
= 0;
496 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A
)newDevice
->base
.dinput
);
498 newDevice
->devcaps
.dwSize
= sizeof(newDevice
->devcaps
);
499 newDevice
->devcaps
.dwFlags
= DIDC_ATTACHED
;
500 if (newDevice
->base
.dinput
->dwVersion
>= 0x0800)
501 newDevice
->devcaps
.dwDevType
= DI8DEVTYPE_JOYSTICK
| (DI8DEVTYPEJOYSTICK_STANDARD
<< 8);
503 newDevice
->devcaps
.dwDevType
= DIDEVTYPE_JOYSTICK
| (DIDEVTYPEJOYSTICK_TRADITIONAL
<< 8);
504 newDevice
->devcaps
.dwFFSamplePeriod
= 0;
505 newDevice
->devcaps
.dwFFMinTimeResolution
= 0;
506 newDevice
->devcaps
.dwFirmwareRevision
= 0;
507 newDevice
->devcaps
.dwHardwareRevision
= 0;
508 newDevice
->devcaps
.dwFFDriverVersion
= 0;
510 if (TRACE_ON(dinput
)) {
511 _dump_DIDATAFORMAT(newDevice
->base
.data_format
.wine_df
);
512 for (i
= 0; i
< (newDevice
->axes
); i
++)
513 TRACE("axis_map[%d] = %d\n", i
, newDevice
->axis_map
[i
]);
514 _dump_DIDEVCAPS(&newDevice
->devcaps
);
517 *pdev
= (LPDIRECTINPUTDEVICEA
)newDevice
;
522 hr
= DIERR_OUTOFMEMORY
;
524 if (df
) HeapFree(GetProcessHeap(), 0, df
->rgodf
);
525 HeapFree(GetProcessHeap(), 0, df
);
526 release_DataFormat(&newDevice
->base
.data_format
);
527 HeapFree(GetProcessHeap(),0,newDevice
->axis_map
);
528 HeapFree(GetProcessHeap(),0,newDevice
->name
);
529 HeapFree(GetProcessHeap(),0,newDevice
->props
);
530 HeapFree(GetProcessHeap(),0,newDevice
);
536 /******************************************************************************
537 * get_joystick_index : Get the joystick index from a given GUID
539 static unsigned short get_joystick_index(REFGUID guid
)
541 GUID wine_joystick
= DInput_Wine_Joystick_GUID
;
542 GUID dev_guid
= *guid
;
544 wine_joystick
.Data3
= 0;
547 /* for the standard joystick GUID use index 0 */
548 if(IsEqualGUID(&GUID_Joystick
,guid
)) return 0;
550 /* for the wine joystick GUIDs use the index stored in Data3 */
551 if(IsEqualGUID(&wine_joystick
, &dev_guid
)) return guid
->Data3
;
553 return MAX_JOYSTICKS
;
556 static HRESULT
joydev_create_deviceA(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPDIRECTINPUTDEVICEA
* pdev
)
558 unsigned short index
;
560 TRACE("%p %s %p %p\n",dinput
, debugstr_guid(rguid
), riid
, pdev
);
561 find_joystick_devices();
564 if ((index
= get_joystick_index(rguid
)) < MAX_JOYSTICKS
&&
565 joystick_devices_count
&& index
< joystick_devices_count
)
567 if ((riid
== NULL
) ||
568 IsEqualGUID(&IID_IDirectInputDeviceA
, riid
) ||
569 IsEqualGUID(&IID_IDirectInputDevice2A
, riid
) ||
570 IsEqualGUID(&IID_IDirectInputDevice7A
, riid
) ||
571 IsEqualGUID(&IID_IDirectInputDevice8A
, riid
))
573 return alloc_device(rguid
, &JoystickAvt
, dinput
, pdev
, index
);
576 WARN("no interface\n");
577 return DIERR_NOINTERFACE
;
580 return DIERR_DEVICENOTREG
;
583 static HRESULT
joydev_create_deviceW(IDirectInputImpl
*dinput
, REFGUID rguid
, REFIID riid
, LPDIRECTINPUTDEVICEW
* pdev
)
585 unsigned short index
;
587 TRACE("%p %s %p %p\n",dinput
, debugstr_guid(rguid
), riid
, pdev
);
588 find_joystick_devices();
591 if ((index
= get_joystick_index(rguid
)) < MAX_JOYSTICKS
&&
592 joystick_devices_count
&& index
< joystick_devices_count
)
594 if ((riid
== NULL
) ||
595 IsEqualGUID(&IID_IDirectInputDeviceW
, riid
) ||
596 IsEqualGUID(&IID_IDirectInputDevice2W
, riid
) ||
597 IsEqualGUID(&IID_IDirectInputDevice7W
, riid
) ||
598 IsEqualGUID(&IID_IDirectInputDevice8W
, riid
))
600 return alloc_device(rguid
, &JoystickWvt
, dinput
, (LPDIRECTINPUTDEVICEA
*)pdev
, index
);
602 WARN("no interface\n");
603 return DIERR_NOINTERFACE
;
606 WARN("invalid device GUID %s\n",debugstr_guid(rguid
));
607 return DIERR_DEVICENOTREG
;
612 const struct dinput_device joystick_linux_device
= {
613 "Wine Linux joystick driver",
616 joydev_create_deviceA
,
617 joydev_create_deviceW
620 /******************************************************************************
621 * Acquire : gets exclusive control of the joystick
623 static HRESULT WINAPI
JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface
)
625 JoystickImpl
*This
= (JoystickImpl
*)iface
;
627 TRACE("(%p)\n",This
);
629 if (This
->base
.acquired
) {
630 WARN("already acquired\n");
634 /* open the joystick device */
635 if (This
->joyfd
==-1) {
636 TRACE("opening joystick device %s\n", This
->dev
);
638 This
->joyfd
=open(This
->dev
,O_RDONLY
);
639 if (This
->joyfd
==-1) {
640 ERR("open(%s) failed: %s\n", This
->dev
, strerror(errno
));
641 return DIERR_NOTFOUND
;
645 This
->base
.acquired
= 1;
650 /******************************************************************************
651 * Unacquire : frees the joystick
653 static HRESULT WINAPI
JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface
)
655 JoystickImpl
*This
= (JoystickImpl
*)iface
;
658 TRACE("(%p)\n",This
);
660 if ((res
= IDirectInputDevice2AImpl_Unacquire(iface
)) != DI_OK
) return res
;
662 if (This
->joyfd
!=-1) {
663 TRACE("closing joystick device\n");
672 static void joy_polldev(JoystickImpl
*This
) {
675 TRACE("(%p)\n", This
);
677 if (This
->joyfd
==-1) {
686 plfd
.fd
= This
->joyfd
;
687 plfd
.events
= POLLIN
;
688 if (poll(&plfd
,1,0) != 1)
690 /* we have one event, so we can read */
691 if (sizeof(jse
)!=read(This
->joyfd
,&jse
,sizeof(jse
))) {
694 TRACE("js_event: type 0x%x, number %d, value %d\n",
695 jse
.type
,jse
.number
,jse
.value
);
696 if (jse
.type
& JS_EVENT_BUTTON
)
698 inst_id
= DIDFT_MAKEINSTANCE(jse
.number
) | DIDFT_PSHBUTTON
;
699 This
->js
.rgbButtons
[jse
.number
] = value
= jse
.value
? 0x80 : 0x00;
701 else if (jse
.type
& JS_EVENT_AXIS
)
703 int number
= This
->axis_map
[jse
.number
]; /* wine format object index */
705 if (number
< 0) return;
706 inst_id
= DIDFT_MAKEINSTANCE(number
) | (number
< 8 ? DIDFT_ABSAXIS
: DIDFT_POV
);
707 value
= joystick_map_axis(&This
->props
[id_to_object(This
->base
.data_format
.wine_df
, inst_id
)], jse
.value
);
709 TRACE("changing axis %d => %d\n", jse
.number
, number
);
712 case 0: This
->js
.lX
= value
; break;
713 case 1: This
->js
.lY
= value
; break;
714 case 2: This
->js
.lZ
= value
; break;
715 case 3: This
->js
.lRx
= value
; break;
716 case 4: This
->js
.lRy
= value
; break;
717 case 5: This
->js
.lRz
= value
; break;
718 case 6: This
->js
.rglSlider
[0] = value
; break;
719 case 7: This
->js
.rglSlider
[1] = value
; break;
720 case 8: case 9: case 10: case 11:
722 int idx
= number
- 8;
725 This
->povs
[idx
].y
= jse
.value
;
727 This
->povs
[idx
].x
= jse
.value
;
729 This
->js
.rgdwPOV
[idx
] = value
= joystick_map_pov(&This
->povs
[idx
]);
733 WARN("axis %d not supported\n", number
);
737 queue_event((LPDIRECTINPUTDEVICE8A
)This
,
738 id_to_offset(&This
->base
.data_format
, inst_id
),
739 value
, jse
.time
, This
->base
.dinput
->evsequence
++);
743 /******************************************************************************
744 * GetDeviceState : returns the "state" of the joystick.
747 static HRESULT WINAPI
JoystickAImpl_GetDeviceState(
748 LPDIRECTINPUTDEVICE8A iface
,
752 JoystickImpl
*This
= (JoystickImpl
*)iface
;
754 TRACE("(%p,0x%08x,%p)\n", This
, len
, ptr
);
756 if (!This
->base
.acquired
) {
757 WARN("not acquired\n");
758 return DIERR_NOTACQUIRED
;
761 /* update joystick state */
764 /* convert and copy data to user supplied buffer */
765 fill_DataFormat(ptr
, &This
->js
, &This
->base
.data_format
);
770 /******************************************************************************
771 * SetProperty : change input device properties
773 static HRESULT WINAPI
JoystickAImpl_SetProperty(
774 LPDIRECTINPUTDEVICE8A iface
,
778 JoystickImpl
*This
= (JoystickImpl
*)iface
;
781 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(rguid
),ph
);
784 WARN("invalid parameter: ph == NULL\n");
785 return DIERR_INVALIDPARAM
;
788 if (TRACE_ON(dinput
))
789 _dump_DIPROPHEADER(ph
);
791 if (!HIWORD(rguid
)) {
792 switch (LOWORD(rguid
)) {
793 case (DWORD
)DIPROP_RANGE
: {
794 LPCDIPROPRANGE pr
= (LPCDIPROPRANGE
)ph
;
795 if (ph
->dwHow
== DIPH_DEVICE
) {
796 TRACE("proprange(%d,%d) all\n", pr
->lMin
, pr
->lMax
);
797 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++) {
798 This
->props
[i
].lMin
= pr
->lMin
;
799 This
->props
[i
].lMax
= pr
->lMax
;
802 int obj
= find_property(&This
->base
.data_format
, ph
);
804 TRACE("proprange(%d,%d) obj=%d\n", pr
->lMin
, pr
->lMax
, obj
);
806 This
->props
[obj
].lMin
= pr
->lMin
;
807 This
->props
[obj
].lMax
= pr
->lMax
;
813 case (DWORD
)DIPROP_DEADZONE
: {
814 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
815 if (ph
->dwHow
== DIPH_DEVICE
) {
816 TRACE("deadzone(%d) all\n", pd
->dwData
);
817 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++)
818 This
->props
[i
].lDeadZone
= pd
->dwData
;
820 int obj
= find_property(&This
->base
.data_format
, ph
);
822 TRACE("deadzone(%d) obj=%d\n", pd
->dwData
, obj
);
824 This
->props
[obj
].lDeadZone
= pd
->dwData
;
830 case (DWORD
)DIPROP_SATURATION
: {
831 LPCDIPROPDWORD pd
= (LPCDIPROPDWORD
)ph
;
832 if (ph
->dwHow
== DIPH_DEVICE
) {
833 TRACE("saturation(%d) all\n", pd
->dwData
);
834 for (i
= 0; i
< This
->base
.data_format
.wine_df
->dwNumObjs
; i
++)
835 This
->props
[i
].lSaturation
= pd
->dwData
;
837 int obj
= find_property(&This
->base
.data_format
, ph
);
839 TRACE("saturation(%d) obj=%d\n", pd
->dwData
, obj
);
841 This
->props
[obj
].lSaturation
= pd
->dwData
;
848 return IDirectInputDevice2AImpl_SetProperty(iface
, rguid
, ph
);
855 static HRESULT WINAPI
JoystickAImpl_GetCapabilities(
856 LPDIRECTINPUTDEVICE8A iface
,
857 LPDIDEVCAPS lpDIDevCaps
)
859 JoystickImpl
*This
= (JoystickImpl
*)iface
;
862 TRACE("%p->(%p)\n",iface
,lpDIDevCaps
);
864 if (lpDIDevCaps
== NULL
) {
865 WARN("invalid pointer\n");
869 size
= lpDIDevCaps
->dwSize
;
871 if (!(size
== sizeof(DIDEVCAPS
) || size
== sizeof(DIDEVCAPS_DX3
))) {
872 WARN("invalid parameter\n");
873 return DIERR_INVALIDPARAM
;
876 CopyMemory(lpDIDevCaps
, &This
->devcaps
, size
);
877 lpDIDevCaps
->dwSize
= size
;
879 if (TRACE_ON(dinput
))
880 _dump_DIDEVCAPS(lpDIDevCaps
);
885 static HRESULT WINAPI
JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface
)
887 JoystickImpl
*This
= (JoystickImpl
*)iface
;
889 TRACE("(%p)\n",This
);
891 if (!This
->base
.acquired
) {
892 WARN("not acquired\n");
893 return DIERR_NOTACQUIRED
;
900 /******************************************************************************
901 * GetProperty : get input device properties
903 static HRESULT WINAPI
JoystickAImpl_GetProperty(
904 LPDIRECTINPUTDEVICE8A iface
,
906 LPDIPROPHEADER pdiph
)
908 JoystickImpl
*This
= (JoystickImpl
*)iface
;
910 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(rguid
), pdiph
);
912 if (TRACE_ON(dinput
))
913 _dump_DIPROPHEADER(pdiph
);
915 if (!HIWORD(rguid
)) {
916 switch (LOWORD(rguid
)) {
917 case (DWORD
) DIPROP_RANGE
: {
918 LPDIPROPRANGE pr
= (LPDIPROPRANGE
)pdiph
;
919 int obj
= find_property(&This
->base
.data_format
, pdiph
);
921 /* The app is querying the current range of the axis
922 * return the lMin and lMax values */
924 pr
->lMin
= This
->props
[obj
].lMin
;
925 pr
->lMax
= This
->props
[obj
].lMax
;
926 TRACE("range(%d, %d) obj=%d\n", pr
->lMin
, pr
->lMax
, obj
);
931 case (DWORD
) DIPROP_DEADZONE
: {
932 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
933 int obj
= find_property(&This
->base
.data_format
, pdiph
);
936 pd
->dwData
= This
->props
[obj
].lDeadZone
;
937 TRACE("deadzone(%d) obj=%d\n", pd
->dwData
, obj
);
942 case (DWORD
) DIPROP_SATURATION
: {
943 LPDIPROPDWORD pd
= (LPDIPROPDWORD
)pdiph
;
944 int obj
= find_property(&This
->base
.data_format
, pdiph
);
947 pd
->dwData
= This
->props
[obj
].lSaturation
;
948 TRACE("saturation(%d) obj=%d\n", pd
->dwData
, obj
);
954 return IDirectInputDevice2AImpl_GetProperty(iface
, rguid
, pdiph
);
961 /******************************************************************************
962 * GetObjectInfo : get object info
964 static HRESULT WINAPI
JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface
,
965 LPDIDEVICEOBJECTINSTANCEW pdidoi
, DWORD dwObj
, DWORD dwHow
)
967 static const WCHAR axisW
[] = {'A','x','i','s',' ','%','d',0};
968 static const WCHAR povW
[] = {'P','O','V',' ','%','d',0};
969 static const WCHAR buttonW
[] = {'B','u','t','t','o','n',' ','%','d',0};
972 res
= IDirectInputDevice2WImpl_GetObjectInfo(iface
, pdidoi
, dwObj
, dwHow
);
973 if (res
!= DI_OK
) return res
;
975 if (pdidoi
->dwType
& DIDFT_AXIS
)
976 sprintfW(pdidoi
->tszName
, axisW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
977 else if (pdidoi
->dwType
& DIDFT_POV
)
978 sprintfW(pdidoi
->tszName
, povW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
979 else if (pdidoi
->dwType
& DIDFT_BUTTON
)
980 sprintfW(pdidoi
->tszName
, buttonW
, DIDFT_GETINSTANCE(pdidoi
->dwType
));
982 _dump_OBJECTINSTANCEW(pdidoi
);
986 static HRESULT WINAPI
JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface
,
987 LPDIDEVICEOBJECTINSTANCEA pdidoi
, DWORD dwObj
, DWORD dwHow
)
990 DIDEVICEOBJECTINSTANCEW didoiW
;
991 DWORD dwSize
= pdidoi
->dwSize
;
993 didoiW
.dwSize
= sizeof(didoiW
);
994 res
= JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W
)iface
, &didoiW
, dwObj
, dwHow
);
995 if (res
!= DI_OK
) return res
;
997 memset(pdidoi
, 0, pdidoi
->dwSize
);
998 memcpy(pdidoi
, &didoiW
, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW
, tszName
));
999 pdidoi
->dwSize
= dwSize
;
1000 WideCharToMultiByte(CP_ACP
, 0, didoiW
.tszName
, -1, pdidoi
->tszName
,
1001 sizeof(pdidoi
->tszName
), NULL
, NULL
);
1006 /******************************************************************************
1007 * GetDeviceInfo : get information about a device's identity
1009 static HRESULT WINAPI
JoystickAImpl_GetDeviceInfo(
1010 LPDIRECTINPUTDEVICE8A iface
,
1011 LPDIDEVICEINSTANCEA pdidi
)
1013 JoystickImpl
*This
= (JoystickImpl
*)iface
;
1015 TRACE("(%p,%p)\n", iface
, pdidi
);
1017 if (pdidi
== NULL
) {
1018 WARN("invalid pointer\n");
1022 if ((pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3A
)) &&
1023 (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEA
))) {
1024 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1025 pdidi
->dwSize
, sizeof(DIDEVICEINSTANCE_DX3A
),
1026 sizeof(DIDEVICEINSTANCEA
));
1027 return DIERR_INVALIDPARAM
;
1030 /* Return joystick */
1031 pdidi
->guidInstance
= GUID_Joystick
;
1032 pdidi
->guidProduct
= DInput_Wine_Joystick_GUID
;
1033 /* we only support traditional joysticks for now */
1034 pdidi
->dwDevType
= This
->devcaps
.dwDevType
;
1035 strcpy(pdidi
->tszInstanceName
, "Joystick");
1036 strcpy(pdidi
->tszProductName
, This
->name
);
1037 if (pdidi
->dwSize
> sizeof(DIDEVICEINSTANCE_DX3A
)) {
1038 pdidi
->guidFFDriver
= GUID_NULL
;
1039 pdidi
->wUsagePage
= 0;
1046 /******************************************************************************
1047 * GetDeviceInfo : get information about a device's identity
1049 static HRESULT WINAPI
JoystickWImpl_GetDeviceInfo(
1050 LPDIRECTINPUTDEVICE8W iface
,
1051 LPDIDEVICEINSTANCEW pdidi
)
1053 JoystickImpl
*This
= (JoystickImpl
*)iface
;
1055 TRACE("(%p,%p)\n", iface
, pdidi
);
1057 if ((pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCE_DX3W
)) &&
1058 (pdidi
->dwSize
!= sizeof(DIDEVICEINSTANCEW
))) {
1059 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1060 pdidi
->dwSize
, sizeof(DIDEVICEINSTANCE_DX3W
),
1061 sizeof(DIDEVICEINSTANCEW
));
1062 return DIERR_INVALIDPARAM
;
1065 /* Return joystick */
1066 pdidi
->guidInstance
= GUID_Joystick
;
1067 pdidi
->guidProduct
= DInput_Wine_Joystick_GUID
;
1068 /* we only support traditional joysticks for now */
1069 pdidi
->dwDevType
= This
->devcaps
.dwDevType
;
1070 MultiByteToWideChar(CP_ACP
, 0, "Joystick", -1, pdidi
->tszInstanceName
, MAX_PATH
);
1071 MultiByteToWideChar(CP_ACP
, 0, This
->name
, -1, pdidi
->tszProductName
, MAX_PATH
);
1072 if (pdidi
->dwSize
> sizeof(DIDEVICEINSTANCE_DX3W
)) {
1073 pdidi
->guidFFDriver
= GUID_NULL
;
1074 pdidi
->wUsagePage
= 0;
1081 static const IDirectInputDevice8AVtbl JoystickAvt
=
1083 IDirectInputDevice2AImpl_QueryInterface
,
1084 IDirectInputDevice2AImpl_AddRef
,
1085 IDirectInputDevice2AImpl_Release
,
1086 JoystickAImpl_GetCapabilities
,
1087 IDirectInputDevice2AImpl_EnumObjects
,
1088 JoystickAImpl_GetProperty
,
1089 JoystickAImpl_SetProperty
,
1090 JoystickAImpl_Acquire
,
1091 JoystickAImpl_Unacquire
,
1092 JoystickAImpl_GetDeviceState
,
1093 IDirectInputDevice2AImpl_GetDeviceData
,
1094 IDirectInputDevice2AImpl_SetDataFormat
,
1095 IDirectInputDevice2AImpl_SetEventNotification
,
1096 IDirectInputDevice2AImpl_SetCooperativeLevel
,
1097 JoystickAImpl_GetObjectInfo
,
1098 JoystickAImpl_GetDeviceInfo
,
1099 IDirectInputDevice2AImpl_RunControlPanel
,
1100 IDirectInputDevice2AImpl_Initialize
,
1101 IDirectInputDevice2AImpl_CreateEffect
,
1102 IDirectInputDevice2AImpl_EnumEffects
,
1103 IDirectInputDevice2AImpl_GetEffectInfo
,
1104 IDirectInputDevice2AImpl_GetForceFeedbackState
,
1105 IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
1106 IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
1107 IDirectInputDevice2AImpl_Escape
,
1109 IDirectInputDevice2AImpl_SendDeviceData
,
1110 IDirectInputDevice7AImpl_EnumEffectsInFile
,
1111 IDirectInputDevice7AImpl_WriteEffectToFile
,
1112 IDirectInputDevice8AImpl_BuildActionMap
,
1113 IDirectInputDevice8AImpl_SetActionMap
,
1114 IDirectInputDevice8AImpl_GetImageInfo
1117 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1118 # define XCAST(fun) (typeof(JoystickWvt.fun))
1120 # define XCAST(fun) (void*)
1123 static const IDirectInputDevice8WVtbl JoystickWvt
=
1125 IDirectInputDevice2WImpl_QueryInterface
,
1126 XCAST(AddRef
)IDirectInputDevice2AImpl_AddRef
,
1127 XCAST(Release
)IDirectInputDevice2AImpl_Release
,
1128 XCAST(GetCapabilities
)JoystickAImpl_GetCapabilities
,
1129 IDirectInputDevice2WImpl_EnumObjects
,
1130 XCAST(GetProperty
)JoystickAImpl_GetProperty
,
1131 XCAST(SetProperty
)JoystickAImpl_SetProperty
,
1132 XCAST(Acquire
)JoystickAImpl_Acquire
,
1133 XCAST(Unacquire
)JoystickAImpl_Unacquire
,
1134 XCAST(GetDeviceState
)JoystickAImpl_GetDeviceState
,
1135 XCAST(GetDeviceData
)IDirectInputDevice2AImpl_GetDeviceData
,
1136 XCAST(SetDataFormat
)IDirectInputDevice2AImpl_SetDataFormat
,
1137 XCAST(SetEventNotification
)IDirectInputDevice2AImpl_SetEventNotification
,
1138 XCAST(SetCooperativeLevel
)IDirectInputDevice2AImpl_SetCooperativeLevel
,
1139 IDirectInputDevice2WImpl_GetObjectInfo
,
1140 JoystickWImpl_GetDeviceInfo
,
1141 XCAST(RunControlPanel
)IDirectInputDevice2AImpl_RunControlPanel
,
1142 XCAST(Initialize
)IDirectInputDevice2AImpl_Initialize
,
1143 XCAST(CreateEffect
)IDirectInputDevice2AImpl_CreateEffect
,
1144 IDirectInputDevice2WImpl_EnumEffects
,
1145 IDirectInputDevice2WImpl_GetEffectInfo
,
1146 XCAST(GetForceFeedbackState
)IDirectInputDevice2AImpl_GetForceFeedbackState
,
1147 XCAST(SendForceFeedbackCommand
)IDirectInputDevice2AImpl_SendForceFeedbackCommand
,
1148 XCAST(EnumCreatedEffectObjects
)IDirectInputDevice2AImpl_EnumCreatedEffectObjects
,
1149 XCAST(Escape
)IDirectInputDevice2AImpl_Escape
,
1150 XCAST(Poll
)JoystickAImpl_Poll
,
1151 XCAST(SendDeviceData
)IDirectInputDevice2AImpl_SendDeviceData
,
1152 IDirectInputDevice7WImpl_EnumEffectsInFile
,
1153 IDirectInputDevice7WImpl_WriteEffectToFile
,
1154 IDirectInputDevice8WImpl_BuildActionMap
,
1155 IDirectInputDevice8WImpl_SetActionMap
,
1156 IDirectInputDevice8WImpl_GetImageInfo
1160 #else /* HAVE_LINUX_22_JOYSTICK_API */
1162 const struct dinput_device joystick_linux_device
= {
1163 "Wine Linux joystick driver",
1170 #endif /* HAVE_LINUX_22_JOYSTICK_API */