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"
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
42 #include <sys/fcntl.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
47 #ifdef HAVE_SYS_ERRNO_H
48 # include <sys/errno.h>
50 #ifdef HAVE_LINUX_IOCTL_H
51 # include <linux/ioctl.h>
53 #ifdef HAVE_LINUX_JOYSTICK_H
54 # include <linux/joystick.h>
57 #ifdef HAVE_SYS_POLL_H
58 # include <sys/poll.h>
61 #include "wine/debug.h"
62 #include "wine/unicode.h"
69 #include "dinput_private.h"
70 #include "device_private.h"
72 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
74 #ifdef HAVE_LINUX_22_JOYSTICK_API
76 #define JOYDEV_NEW "/dev/input/js"
77 #define JOYDEV_OLD "/dev/js"
91 typedef struct JoystickImpl JoystickImpl;
92 static const IDirectInputDevice8AVtbl JoystickAvt;
93 static const IDirectInputDevice8WVtbl JoystickWvt;
96 struct IDirectInputDevice2AImpl base;
100 /* joystick private */
102 DIJOYSTATE2 js; /* wine data */
113 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
117 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
120 static 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 #define MAX_JOYSTICKS 64
144 static INT joystick_devices_count = -1;
145 static LPSTR joystick_devices[MAX_JOYSTICKS];
147 static INT find_joystick_devices(void)
151 if (joystick_devices_count != -1) return joystick_devices_count;
153 for (i = 0; i < MAX_JOYSTICKS; i++)
155 CHAR device_name[MAX_PATH], *str;
159 len = sprintf(device_name, "%s%d", JOYDEV_NEW, i) + 1;
160 if ((fd = open(device_name, O_RDONLY)) < 0)
162 len = sprintf(device_name, "%s%d", JOYDEV_OLD, i) + 1;
163 if ((fd = open(device_name, O_RDONLY)) < 0) continue;
166 if (!(str = HeapAlloc(GetProcessHeap(), 0, len))) break;
167 memcpy(str, device_name, len);
169 joystick_devices[++joystick_devices_count] = str;
172 return joystick_devices_count;
176 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
180 if (id > find_joystick_devices()) return FALSE;
182 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
183 WARN("force feedback not supported\n");
187 if ((dwDevType == 0) ||
188 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
189 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
190 /* check whether we have a joystick */
191 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
193 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
197 /* Return joystick */
198 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
199 lpddi->guidInstance.Data3 = id;
200 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
201 /* we only support traditional joysticks for now */
202 if (version >= 0x0800)
203 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
205 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
206 sprintf(lpddi->tszInstanceName, "Joystick %d", id);
207 #if defined(JSIOCGNAME)
208 if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
209 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
210 strcpy(lpddi->tszProductName, "Wine Joystick");
213 strcpy(lpddi->tszProductName, "Wine Joystick");
216 lpddi->guidFFDriver = GUID_NULL;
218 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], lpddi->tszProductName);
225 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
231 if (id > find_joystick_devices()) return FALSE;
233 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
234 WARN("force feedback not supported\n");
238 if ((dwDevType == 0) ||
239 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
240 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
241 /* check whether we have a joystick */
242 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
244 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
248 /* Return joystick */
249 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
250 lpddi->guidInstance.Data3 = id;
251 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
252 /* we only support traditional joysticks for now */
253 if (version >= 0x0800)
254 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
256 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
257 sprintf(friendly, "Joystick %d", id);
258 MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
259 #if defined(JSIOCGNAME)
260 if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
261 WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
262 strcpy(name, "Wine Joystick");
265 strcpy(name, "Wine Joystick");
267 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
268 lpddi->guidFFDriver = GUID_NULL;
270 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], name);
278 * Get a config key from either the app-specific or the default config
281 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
282 char *buffer, DWORD size )
284 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size ))
287 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size ))
290 return ERROR_FILE_NOT_FOUND;
294 * Setup the dinput options.
297 static HRESULT setup_dinput_options(JoystickImpl * device)
299 char buffer[MAX_PATH+16];
300 HKEY hkey, appkey = 0;
303 buffer[MAX_PATH]='\0';
305 /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
306 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", &hkey)) hkey = 0;
308 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
309 if (len && len < MAX_PATH) {
311 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
312 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
314 char *p, *appname = buffer;
315 if ((p = strrchr( appname, '/' ))) appname = p + 1;
316 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
317 strcat( appname, "\\DirectInput" );
318 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
319 RegCloseKey( tmpkey );
325 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
326 device->deadzone = atoi(buffer);
327 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
330 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
334 const char *delim = ",";
336 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
338 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
339 if (device->axis_map == 0)
340 return DIERR_OUTOFMEMORY;
342 if ((ptr = strtok(buffer, delim)) != NULL) {
344 if (strcmp(ptr, "X") == 0) {
345 device->axis_map[tokens] = 0;
347 } else if (strcmp(ptr, "Y") == 0) {
348 device->axis_map[tokens] = 1;
350 } else if (strcmp(ptr, "Z") == 0) {
351 device->axis_map[tokens] = 2;
353 } else if (strcmp(ptr, "Rx") == 0) {
354 device->axis_map[tokens] = 3;
356 } else if (strcmp(ptr, "Ry") == 0) {
357 device->axis_map[tokens] = 4;
359 } else if (strcmp(ptr, "Rz") == 0) {
360 device->axis_map[tokens] = 5;
362 } else if (strcmp(ptr, "Slider1") == 0) {
363 device->axis_map[tokens] = 6;
365 } else if (strcmp(ptr, "Slider2") == 0) {
366 device->axis_map[tokens] = 7;
368 } else if (strcmp(ptr, "POV1") == 0) {
369 device->axis_map[tokens++] = 8;
370 device->axis_map[tokens] = 8;
372 } else if (strcmp(ptr, "POV2") == 0) {
373 device->axis_map[tokens++] = 9;
374 device->axis_map[tokens] = 9;
376 } else if (strcmp(ptr, "POV3") == 0) {
377 device->axis_map[tokens++] = 10;
378 device->axis_map[tokens] = 10;
380 } else if (strcmp(ptr, "POV4") == 0) {
381 device->axis_map[tokens++] = 11;
382 device->axis_map[tokens] = 11;
385 ERR("invalid joystick axis type: %s\n", ptr);
386 device->axis_map[tokens] = tokens;
391 } while ((ptr = strtok(NULL, delim)) != NULL);
393 if (tokens != device->devcaps.dwAxes) {
394 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
395 while (tokens < device->axes) {
396 device->axis_map[tokens] = tokens;
402 device->devcaps.dwAxes = axis;
403 device->devcaps.dwPOVs = pov;
407 RegCloseKey( appkey );
415 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, LPDIRECTINPUTDEVICEA* pdev)
418 JoystickImpl* newDevice;
421 LPDIDATAFORMAT df = NULL;
424 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
425 if (newDevice == 0) {
426 WARN("out of memory\n");
428 return DIERR_OUTOFMEMORY;
431 if (!lstrcpynA(newDevice->dev, joystick_devices[rguid->Data3], sizeof(newDevice->dev)) ||
432 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
434 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
435 HeapFree(GetProcessHeap(), 0, newDevice);
436 return DIERR_DEVICENOTREG;
439 /* get the device name */
440 #if defined(JSIOCGNAME)
441 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
442 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
443 strcpy(name, "Wine Joystick");
446 strcpy(name, "Wine Joystick");
449 /* copy the device name */
450 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
451 strcpy(newDevice->name, name);
454 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
455 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
460 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
461 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
462 newDevice->buttons = 2;
466 newDevice->base.lpVtbl = jvt;
467 newDevice->base.ref = 1;
468 newDevice->base.dinput = dinput;
469 CopyMemory(&newDevice->base.guid, rguid, sizeof(*rguid));
470 InitializeCriticalSection(&newDevice->base.crit);
471 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
473 /* setup_dinput_options may change these */
474 newDevice->deadzone = 0;
475 newDevice->devcaps.dwButtons = newDevice->buttons;
476 newDevice->devcaps.dwAxes = newDevice->axes;
477 newDevice->devcaps.dwPOVs = 0;
479 /* do any user specified configuration */
480 hr = setup_dinput_options(newDevice);
484 if (newDevice->axis_map == 0) {
485 newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
486 if (newDevice->axis_map == 0)
489 for (i = 0; i < newDevice->axes; i++)
490 newDevice->axis_map[i] = i;
493 /* Create copy of default data format */
494 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
495 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
497 /* Axes include POVs */
498 df->dwNumObjs = newDevice->axes + newDevice->buttons;
499 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
501 for (i = 0; i < newDevice->axes; i++)
503 int wine_obj = newDevice->axis_map[i];
505 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
507 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
509 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
511 for (i = 0; i < newDevice->buttons; i++)
513 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
514 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
516 newDevice->base.data_format.wine_df = df;
518 /* create default properties */
519 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
520 if (newDevice->props == 0)
523 /* initialize default properties */
524 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
525 newDevice->props[i].lMin = 0;
526 newDevice->props[i].lMax = 0xffff;
527 newDevice->props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
528 newDevice->props[i].lSaturation = 0;
531 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
533 newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
534 newDevice->devcaps.dwFlags = DIDC_ATTACHED;
535 if (newDevice->base.dinput->dwVersion >= 0x0800)
536 newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
538 newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
539 newDevice->devcaps.dwFFSamplePeriod = 0;
540 newDevice->devcaps.dwFFMinTimeResolution = 0;
541 newDevice->devcaps.dwFirmwareRevision = 0;
542 newDevice->devcaps.dwHardwareRevision = 0;
543 newDevice->devcaps.dwFFDriverVersion = 0;
545 if (TRACE_ON(dinput)) {
546 _dump_DIDATAFORMAT(newDevice->base.data_format.wine_df);
547 for (i = 0; i < (newDevice->axes); i++)
548 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
549 _dump_DIDEVCAPS(&newDevice->devcaps);
552 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
557 hr = DIERR_OUTOFMEMORY;
559 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
560 HeapFree(GetProcessHeap(), 0, df);
561 release_DataFormat(&newDevice->base.data_format);
562 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
563 HeapFree(GetProcessHeap(),0,newDevice->name);
564 HeapFree(GetProcessHeap(),0,newDevice->props);
565 HeapFree(GetProcessHeap(),0,newDevice);
571 static BOOL IsJoystickGUID(REFGUID guid)
573 GUID wine_joystick = DInput_Wine_Joystick_GUID;
574 GUID dev_guid = *guid;
576 wine_joystick.Data3 = 0;
579 return IsEqualGUID(&wine_joystick, &dev_guid);
582 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
584 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
585 (IsJoystickGUID(rguid))) {
586 if ((riid == NULL) ||
587 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
588 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
589 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
590 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
591 return alloc_device(rguid, &JoystickAvt, dinput, pdev);
593 WARN("no interface\n");
595 return DIERR_NOINTERFACE;
599 WARN("invalid device GUID\n");
601 return DIERR_DEVICENOTREG;
604 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
606 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
607 (IsJoystickGUID(rguid))) {
608 if ((riid == NULL) ||
609 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
610 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
611 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
612 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
613 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev);
615 WARN("no interface\n");
617 return DIERR_NOINTERFACE;
621 WARN("invalid device GUID\n");
623 return DIERR_DEVICENOTREG;
626 const struct dinput_device joystick_linux_device = {
627 "Wine Linux joystick driver",
630 joydev_create_deviceA,
631 joydev_create_deviceW
634 /******************************************************************************
635 * Acquire : gets exclusive control of the joystick
637 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
639 JoystickImpl *This = (JoystickImpl *)iface;
641 TRACE("(%p)\n",This);
643 if (This->base.acquired) {
644 WARN("already acquired\n");
648 /* open the joystick device */
649 if (This->joyfd==-1) {
650 TRACE("opening joystick device %s\n", This->dev);
652 This->joyfd=open(This->dev,O_RDONLY);
653 if (This->joyfd==-1) {
654 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
655 return DIERR_NOTFOUND;
659 This->base.acquired = 1;
664 /******************************************************************************
665 * Unacquire : frees the joystick
667 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
669 JoystickImpl *This = (JoystickImpl *)iface;
672 TRACE("(%p)\n",This);
674 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
676 if (This->joyfd!=-1) {
677 TRACE("closing joystick device\n");
686 static LONG map_axis(JoystickImpl * This, short val, short index)
689 double fmin = This->props[index].lMin;
690 double fmax = This->props[index].lMax;
693 fret = (((fval + 32767.0) * (fmax - fmin)) / (32767.0*2.0)) + fmin;
703 static LONG calculate_pov(JoystickImpl *This, int index)
705 if (This->povs[index].lX < -16384) {
706 if (This->povs[index].lY < -16384)
707 This->js.rgdwPOV[index] = 31500;
708 else if (This->povs[index].lY > 16384)
709 This->js.rgdwPOV[index] = 22500;
711 This->js.rgdwPOV[index] = 27000;
712 } else if (This->povs[index].lX > 16384) {
713 if (This->povs[index].lY < -16384)
714 This->js.rgdwPOV[index] = 4500;
715 else if (This->povs[index].lY > 16384)
716 This->js.rgdwPOV[index] = 13500;
718 This->js.rgdwPOV[index] = 9000;
720 if (This->povs[index].lY < -16384)
721 This->js.rgdwPOV[index] = 0;
722 else if (This->povs[index].lY > 16384)
723 This->js.rgdwPOV[index] = 18000;
725 This->js.rgdwPOV[index] = -1;
728 return This->js.rgdwPOV[index];
731 static void joy_polldev(JoystickImpl *This) {
734 TRACE("(%p)\n", This);
736 if (This->joyfd==-1) {
745 plfd.fd = This->joyfd;
746 plfd.events = POLLIN;
747 if (poll(&plfd,1,0) != 1)
749 /* we have one event, so we can read */
750 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
753 TRACE("js_event: type 0x%x, number %d, value %d\n",
754 jse.type,jse.number,jse.value);
755 if (jse.type & JS_EVENT_BUTTON)
757 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
758 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
760 else if (jse.type & JS_EVENT_AXIS)
762 int number = This->axis_map[jse.number]; /* wine format object index */
766 inst_id = DIDFT_MAKEINSTANCE(jse.number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
767 value = map_axis(This, jse.value, number);
768 /* FIXME do deadzone and saturation here */
770 TRACE("changing axis %d => %d\n", jse.number, number);
782 This->js.lRx = value;
785 This->js.lRy = value;
788 This->js.lRz = value;
791 This->js.rglSlider[0] = value;
794 This->js.rglSlider[1] = value;
797 /* FIXME don't go off array */
798 if (This->axis_map[jse.number + 1] == number)
799 This->povs[0].lX = jse.value;
800 else if (This->axis_map[jse.number - 1] == number)
801 This->povs[0].lY = jse.value;
802 value = calculate_pov(This, 0);
805 if (This->axis_map[jse.number + 1] == number)
806 This->povs[1].lX = jse.value;
807 else if (This->axis_map[jse.number - 1] == number)
808 This->povs[1].lY = jse.value;
809 value = calculate_pov(This, 1);
812 if (This->axis_map[jse.number + 1] == number)
813 This->povs[2].lX = jse.value;
814 else if (This->axis_map[jse.number - 1] == number)
815 This->povs[2].lY = jse.value;
816 value = calculate_pov(This, 2);
819 if (This->axis_map[jse.number + 1] == number)
820 This->povs[3].lX = jse.value;
821 else if (This->axis_map[jse.number - 1] == number)
822 This->povs[3].lY = jse.value;
823 value = calculate_pov(This, 3);
827 WARN("axis %d not supported\n", number);
830 queue_event((LPDIRECTINPUTDEVICE8A)This,
831 id_to_offset(&This->base.data_format, inst_id),
832 value, jse.time, This->base.dinput->evsequence++);
836 /******************************************************************************
837 * GetDeviceState : returns the "state" of the joystick.
840 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
841 LPDIRECTINPUTDEVICE8A iface,
845 JoystickImpl *This = (JoystickImpl *)iface;
847 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
849 if (!This->base.acquired) {
850 WARN("not acquired\n");
851 return DIERR_NOTACQUIRED;
854 /* update joystick state */
857 /* convert and copy data to user supplied buffer */
858 fill_DataFormat(ptr, &This->js, &This->base.data_format);
863 /******************************************************************************
864 * SetProperty : change input device properties
866 static HRESULT WINAPI JoystickAImpl_SetProperty(
867 LPDIRECTINPUTDEVICE8A iface,
871 JoystickImpl *This = (JoystickImpl *)iface;
874 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
877 WARN("invalid parameter: ph == NULL\n");
878 return DIERR_INVALIDPARAM;
881 if (TRACE_ON(dinput))
882 _dump_DIPROPHEADER(ph);
884 if (!HIWORD(rguid)) {
885 switch (LOWORD(rguid)) {
886 case (DWORD)DIPROP_RANGE: {
887 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
888 if (ph->dwHow == DIPH_DEVICE) {
889 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
890 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
891 This->props[i].lMin = pr->lMin;
892 This->props[i].lMax = pr->lMax;
895 int obj = find_property(&This->base.data_format, ph);
897 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
899 This->props[obj].lMin = pr->lMin;
900 This->props[obj].lMax = pr->lMax;
906 case (DWORD)DIPROP_DEADZONE: {
907 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
908 if (ph->dwHow == DIPH_DEVICE) {
909 TRACE("deadzone(%d) all\n", pd->dwData);
910 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
911 This->props[i].lDeadZone = pd->dwData;
913 int obj = find_property(&This->base.data_format, ph);
915 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
917 This->props[obj].lDeadZone = pd->dwData;
923 case (DWORD)DIPROP_SATURATION: {
924 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
925 if (ph->dwHow == DIPH_DEVICE) {
926 TRACE("saturation(%d) all\n", pd->dwData);
927 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
928 This->props[i].lSaturation = pd->dwData;
930 int obj = find_property(&This->base.data_format, ph);
932 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
934 This->props[obj].lSaturation = pd->dwData;
941 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
948 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
949 LPDIRECTINPUTDEVICE8A iface,
950 LPDIDEVCAPS lpDIDevCaps)
952 JoystickImpl *This = (JoystickImpl *)iface;
955 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
957 if (lpDIDevCaps == NULL) {
958 WARN("invalid pointer\n");
962 size = lpDIDevCaps->dwSize;
964 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
965 WARN("invalid parameter\n");
966 return DIERR_INVALIDPARAM;
969 CopyMemory(lpDIDevCaps, &This->devcaps, size);
970 lpDIDevCaps->dwSize = size;
972 if (TRACE_ON(dinput))
973 _dump_DIDEVCAPS(lpDIDevCaps);
978 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
980 JoystickImpl *This = (JoystickImpl *)iface;
982 TRACE("(%p)\n",This);
984 if (!This->base.acquired) {
985 WARN("not acquired\n");
986 return DIERR_NOTACQUIRED;
993 /******************************************************************************
994 * GetProperty : get input device properties
996 static HRESULT WINAPI JoystickAImpl_GetProperty(
997 LPDIRECTINPUTDEVICE8A iface,
999 LPDIPROPHEADER pdiph)
1001 JoystickImpl *This = (JoystickImpl *)iface;
1003 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
1005 if (TRACE_ON(dinput))
1006 _dump_DIPROPHEADER(pdiph);
1008 if (!HIWORD(rguid)) {
1009 switch (LOWORD(rguid)) {
1010 case (DWORD) DIPROP_RANGE: {
1011 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
1012 int obj = find_property(&This->base.data_format, pdiph);
1014 /* The app is querying the current range of the axis
1015 * return the lMin and lMax values */
1017 pr->lMin = This->props[obj].lMin;
1018 pr->lMax = This->props[obj].lMax;
1019 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
1024 case (DWORD) DIPROP_DEADZONE: {
1025 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1026 int obj = find_property(&This->base.data_format, pdiph);
1029 pd->dwData = This->props[obj].lDeadZone;
1030 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
1035 case (DWORD) DIPROP_SATURATION: {
1036 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1037 int obj = find_property(&This->base.data_format, pdiph);
1040 pd->dwData = This->props[obj].lSaturation;
1041 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
1047 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
1054 /******************************************************************************
1055 * GetObjectInfo : get object info
1057 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
1058 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
1060 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
1061 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
1062 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
1065 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
1066 if (res != DI_OK) return res;
1068 if (pdidoi->dwType & DIDFT_AXIS)
1069 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
1070 else if (pdidoi->dwType & DIDFT_POV)
1071 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
1072 else if (pdidoi->dwType & DIDFT_BUTTON)
1073 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
1075 _dump_OBJECTINSTANCEW(pdidoi);
1079 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
1080 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1083 DIDEVICEOBJECTINSTANCEW didoiW;
1084 DWORD dwSize = pdidoi->dwSize;
1086 didoiW.dwSize = sizeof(didoiW);
1087 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1088 if (res != DI_OK) return res;
1090 memset(pdidoi, 0, pdidoi->dwSize);
1091 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1092 pdidoi->dwSize = dwSize;
1093 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1094 sizeof(pdidoi->tszName), NULL, NULL);
1099 /******************************************************************************
1100 * GetDeviceInfo : get information about a device's identity
1102 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1103 LPDIRECTINPUTDEVICE8A iface,
1104 LPDIDEVICEINSTANCEA pdidi)
1106 JoystickImpl *This = (JoystickImpl *)iface;
1108 TRACE("(%p,%p)\n", iface, pdidi);
1110 if (pdidi == NULL) {
1111 WARN("invalid pointer\n");
1115 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1116 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1117 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1118 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1119 sizeof(DIDEVICEINSTANCEA));
1120 return DIERR_INVALIDPARAM;
1123 /* Return joystick */
1124 pdidi->guidInstance = GUID_Joystick;
1125 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1126 /* we only support traditional joysticks for now */
1127 pdidi->dwDevType = This->devcaps.dwDevType;
1128 strcpy(pdidi->tszInstanceName, "Joystick");
1129 strcpy(pdidi->tszProductName, This->name);
1130 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1131 pdidi->guidFFDriver = GUID_NULL;
1132 pdidi->wUsagePage = 0;
1139 /******************************************************************************
1140 * GetDeviceInfo : get information about a device's identity
1142 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1143 LPDIRECTINPUTDEVICE8W iface,
1144 LPDIDEVICEINSTANCEW pdidi)
1146 JoystickImpl *This = (JoystickImpl *)iface;
1148 TRACE("(%p,%p)\n", iface, pdidi);
1150 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1151 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1152 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1153 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1154 sizeof(DIDEVICEINSTANCEW));
1155 return DIERR_INVALIDPARAM;
1158 /* Return joystick */
1159 pdidi->guidInstance = GUID_Joystick;
1160 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1161 /* we only support traditional joysticks for now */
1162 pdidi->dwDevType = This->devcaps.dwDevType;
1163 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1164 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1165 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1166 pdidi->guidFFDriver = GUID_NULL;
1167 pdidi->wUsagePage = 0;
1174 static const IDirectInputDevice8AVtbl JoystickAvt =
1176 IDirectInputDevice2AImpl_QueryInterface,
1177 IDirectInputDevice2AImpl_AddRef,
1178 IDirectInputDevice2AImpl_Release,
1179 JoystickAImpl_GetCapabilities,
1180 IDirectInputDevice2AImpl_EnumObjects,
1181 JoystickAImpl_GetProperty,
1182 JoystickAImpl_SetProperty,
1183 JoystickAImpl_Acquire,
1184 JoystickAImpl_Unacquire,
1185 JoystickAImpl_GetDeviceState,
1186 IDirectInputDevice2AImpl_GetDeviceData,
1187 IDirectInputDevice2AImpl_SetDataFormat,
1188 IDirectInputDevice2AImpl_SetEventNotification,
1189 IDirectInputDevice2AImpl_SetCooperativeLevel,
1190 JoystickAImpl_GetObjectInfo,
1191 JoystickAImpl_GetDeviceInfo,
1192 IDirectInputDevice2AImpl_RunControlPanel,
1193 IDirectInputDevice2AImpl_Initialize,
1194 IDirectInputDevice2AImpl_CreateEffect,
1195 IDirectInputDevice2AImpl_EnumEffects,
1196 IDirectInputDevice2AImpl_GetEffectInfo,
1197 IDirectInputDevice2AImpl_GetForceFeedbackState,
1198 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1199 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1200 IDirectInputDevice2AImpl_Escape,
1202 IDirectInputDevice2AImpl_SendDeviceData,
1203 IDirectInputDevice7AImpl_EnumEffectsInFile,
1204 IDirectInputDevice7AImpl_WriteEffectToFile,
1205 IDirectInputDevice8AImpl_BuildActionMap,
1206 IDirectInputDevice8AImpl_SetActionMap,
1207 IDirectInputDevice8AImpl_GetImageInfo
1210 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1211 # define XCAST(fun) (typeof(JoystickWvt.fun))
1213 # define XCAST(fun) (void*)
1216 static const IDirectInputDevice8WVtbl JoystickWvt =
1218 IDirectInputDevice2WImpl_QueryInterface,
1219 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1220 XCAST(Release)IDirectInputDevice2AImpl_Release,
1221 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1222 IDirectInputDevice2WImpl_EnumObjects,
1223 XCAST(GetProperty)JoystickAImpl_GetProperty,
1224 XCAST(SetProperty)JoystickAImpl_SetProperty,
1225 XCAST(Acquire)JoystickAImpl_Acquire,
1226 XCAST(Unacquire)JoystickAImpl_Unacquire,
1227 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1228 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1229 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1230 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1231 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1232 IDirectInputDevice2WImpl_GetObjectInfo,
1233 JoystickWImpl_GetDeviceInfo,
1234 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1235 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1236 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1237 IDirectInputDevice2WImpl_EnumEffects,
1238 IDirectInputDevice2WImpl_GetEffectInfo,
1239 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1240 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1241 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1242 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1243 XCAST(Poll)JoystickAImpl_Poll,
1244 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1245 IDirectInputDevice7WImpl_EnumEffectsInFile,
1246 IDirectInputDevice7WImpl_WriteEffectToFile,
1247 IDirectInputDevice8WImpl_BuildActionMap,
1248 IDirectInputDevice8WImpl_SetActionMap,
1249 IDirectInputDevice8WImpl_GetImageInfo
1253 #else /* HAVE_LINUX_22_JOYSTICK_API */
1255 const struct dinput_device joystick_linux_device = {
1256 "Wine Linux joystick driver",
1263 #endif /* HAVE_LINUX_22_JOYSTICK_API */