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 /* The 'parent' DInput */
101 IDirectInputImpl *dinput;
103 /* joystick private */
105 DIJOYSTATE2 js; /* wine data */
116 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
120 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
123 static void _dump_DIDEVCAPS(LPDIDEVCAPS lpDIDevCaps)
125 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
126 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
127 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
128 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
129 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
130 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
131 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
132 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
133 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
134 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
135 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
136 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
137 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
138 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
139 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
140 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
141 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
142 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
146 #define MAX_JOYSTICKS 64
147 static INT joystick_devices_count = -1;
148 static LPSTR joystick_devices[MAX_JOYSTICKS];
150 static INT find_joystick_devices(void)
154 if (joystick_devices_count != -1) return joystick_devices_count;
156 for (i = 0; i < MAX_JOYSTICKS; i++)
158 CHAR device_name[MAX_PATH], *str;
162 len = sprintf(device_name, "%s%d", JOYDEV_NEW, i) + 1;
163 if ((fd = open(device_name, O_RDONLY)) < 0)
165 len = sprintf(device_name, "%s%d", JOYDEV_OLD, i) + 1;
166 if ((fd = open(device_name, O_RDONLY)) < 0) continue;
169 if (!(str = HeapAlloc(GetProcessHeap(), 0, len))) break;
170 memcpy(str, device_name, len);
172 joystick_devices[++joystick_devices_count] = str;
175 return joystick_devices_count;
179 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
183 if (id > find_joystick_devices()) return FALSE;
185 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
186 WARN("force feedback not supported\n");
190 if ((dwDevType == 0) ||
191 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
192 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
193 /* check whether we have a joystick */
194 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
196 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
200 /* Return joystick */
201 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
202 lpddi->guidInstance.Data3 = id;
203 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
204 /* we only support traditional joysticks for now */
205 if (version >= 0x0800)
206 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
208 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
209 sprintf(lpddi->tszInstanceName, "Joystick %d", id);
210 #if defined(JSIOCGNAME)
211 if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
212 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
213 strcpy(lpddi->tszProductName, "Wine Joystick");
216 strcpy(lpddi->tszProductName, "Wine Joystick");
219 lpddi->guidFFDriver = GUID_NULL;
221 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], lpddi->tszProductName);
228 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
234 if (id > find_joystick_devices()) return FALSE;
236 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
237 WARN("force feedback not supported\n");
241 if ((dwDevType == 0) ||
242 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
243 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
244 /* check whether we have a joystick */
245 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
247 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
251 /* Return joystick */
252 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
253 lpddi->guidInstance.Data3 = id;
254 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
255 /* we only support traditional joysticks for now */
256 if (version >= 0x0800)
257 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
259 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
260 sprintf(friendly, "Joystick %d", id);
261 MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
262 #if defined(JSIOCGNAME)
263 if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
264 WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
265 strcpy(name, "Wine Joystick");
268 strcpy(name, "Wine Joystick");
270 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
271 lpddi->guidFFDriver = GUID_NULL;
273 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], name);
281 * Get a config key from either the app-specific or the default config
284 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
285 char *buffer, DWORD size )
287 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size ))
290 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size ))
293 return ERROR_FILE_NOT_FOUND;
297 * Setup the dinput options.
300 static HRESULT setup_dinput_options(JoystickImpl * device)
302 char buffer[MAX_PATH+16];
303 HKEY hkey, appkey = 0;
306 buffer[MAX_PATH]='\0';
308 /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
309 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", &hkey)) hkey = 0;
311 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
312 if (len && len < MAX_PATH) {
314 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
315 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
317 char *p, *appname = buffer;
318 if ((p = strrchr( appname, '/' ))) appname = p + 1;
319 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
320 strcat( appname, "\\DirectInput" );
321 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
322 RegCloseKey( tmpkey );
328 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
329 device->deadzone = atoi(buffer);
330 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
333 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
337 const char *delim = ",";
339 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
341 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
342 if (device->axis_map == 0)
343 return DIERR_OUTOFMEMORY;
345 if ((ptr = strtok(buffer, delim)) != NULL) {
347 if (strcmp(ptr, "X") == 0) {
348 device->axis_map[tokens] = 0;
350 } else if (strcmp(ptr, "Y") == 0) {
351 device->axis_map[tokens] = 1;
353 } else if (strcmp(ptr, "Z") == 0) {
354 device->axis_map[tokens] = 2;
356 } else if (strcmp(ptr, "Rx") == 0) {
357 device->axis_map[tokens] = 3;
359 } else if (strcmp(ptr, "Ry") == 0) {
360 device->axis_map[tokens] = 4;
362 } else if (strcmp(ptr, "Rz") == 0) {
363 device->axis_map[tokens] = 5;
365 } else if (strcmp(ptr, "Slider1") == 0) {
366 device->axis_map[tokens] = 6;
368 } else if (strcmp(ptr, "Slider2") == 0) {
369 device->axis_map[tokens] = 7;
371 } else if (strcmp(ptr, "POV1") == 0) {
372 device->axis_map[tokens++] = 8;
373 device->axis_map[tokens] = 8;
375 } else if (strcmp(ptr, "POV2") == 0) {
376 device->axis_map[tokens++] = 9;
377 device->axis_map[tokens] = 9;
379 } else if (strcmp(ptr, "POV3") == 0) {
380 device->axis_map[tokens++] = 10;
381 device->axis_map[tokens] = 10;
383 } else if (strcmp(ptr, "POV4") == 0) {
384 device->axis_map[tokens++] = 11;
385 device->axis_map[tokens] = 11;
388 ERR("invalid joystick axis type: %s\n", ptr);
389 device->axis_map[tokens] = tokens;
394 } while ((ptr = strtok(NULL, delim)) != NULL);
396 if (tokens != device->devcaps.dwAxes) {
397 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
398 while (tokens < device->axes) {
399 device->axis_map[tokens] = tokens;
405 device->devcaps.dwAxes = axis;
406 device->devcaps.dwPOVs = pov;
410 RegCloseKey( appkey );
418 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, LPDIRECTINPUTDEVICEA* pdev)
421 JoystickImpl* newDevice;
424 LPDIDATAFORMAT df = NULL;
427 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
428 if (newDevice == 0) {
429 WARN("out of memory\n");
431 return DIERR_OUTOFMEMORY;
434 if (!lstrcpynA(newDevice->dev, joystick_devices[rguid->Data3], sizeof(newDevice->dev)) ||
435 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
437 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
438 HeapFree(GetProcessHeap(), 0, newDevice);
439 return DIERR_DEVICENOTREG;
442 /* get the device name */
443 #if defined(JSIOCGNAME)
444 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
445 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
446 strcpy(name, "Wine Joystick");
449 strcpy(name, "Wine Joystick");
452 /* copy the device name */
453 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
454 strcpy(newDevice->name, name);
457 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
458 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
463 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
464 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
465 newDevice->buttons = 2;
469 newDevice->base.lpVtbl = jvt;
470 newDevice->base.ref = 1;
471 newDevice->dinput = dinput;
472 CopyMemory(&newDevice->base.guid, rguid, sizeof(*rguid));
473 InitializeCriticalSection(&newDevice->base.crit);
474 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
476 /* setup_dinput_options may change these */
477 newDevice->deadzone = 0;
478 newDevice->devcaps.dwButtons = newDevice->buttons;
479 newDevice->devcaps.dwAxes = newDevice->axes;
480 newDevice->devcaps.dwPOVs = 0;
482 /* do any user specified configuration */
483 hr = setup_dinput_options(newDevice);
487 if (newDevice->axis_map == 0) {
488 newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
489 if (newDevice->axis_map == 0)
492 for (i = 0; i < newDevice->axes; i++)
493 newDevice->axis_map[i] = i;
496 /* Create copy of default data format */
497 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
498 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
500 /* Axes include POVs */
501 df->dwNumObjs = newDevice->axes + newDevice->buttons;
502 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
504 for (i = 0; i < newDevice->axes; i++)
506 int wine_obj = newDevice->axis_map[i];
508 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
510 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
512 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
514 for (i = 0; i < newDevice->buttons; i++)
516 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
517 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
519 newDevice->base.data_format.wine_df = df;
521 /* create default properties */
522 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
523 if (newDevice->props == 0)
526 /* initialize default properties */
527 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
528 newDevice->props[i].lMin = 0;
529 newDevice->props[i].lMax = 0xffff;
530 newDevice->props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
531 newDevice->props[i].lSaturation = 0;
534 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->dinput);
536 newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
537 newDevice->devcaps.dwFlags = DIDC_ATTACHED;
538 if (newDevice->dinput->dwVersion >= 0x0800)
539 newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
541 newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
542 newDevice->devcaps.dwFFSamplePeriod = 0;
543 newDevice->devcaps.dwFFMinTimeResolution = 0;
544 newDevice->devcaps.dwFirmwareRevision = 0;
545 newDevice->devcaps.dwHardwareRevision = 0;
546 newDevice->devcaps.dwFFDriverVersion = 0;
548 if (TRACE_ON(dinput)) {
549 _dump_DIDATAFORMAT(newDevice->base.data_format.wine_df);
550 for (i = 0; i < (newDevice->axes); i++)
551 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
552 _dump_DIDEVCAPS(&newDevice->devcaps);
555 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
560 hr = DIERR_OUTOFMEMORY;
562 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
563 HeapFree(GetProcessHeap(), 0, df);
564 release_DataFormat(&newDevice->base.data_format);
565 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
566 HeapFree(GetProcessHeap(),0,newDevice->name);
567 HeapFree(GetProcessHeap(),0,newDevice->props);
568 HeapFree(GetProcessHeap(),0,newDevice);
574 static BOOL IsJoystickGUID(REFGUID guid)
576 GUID wine_joystick = DInput_Wine_Joystick_GUID;
577 GUID dev_guid = *guid;
579 wine_joystick.Data3 = 0;
582 return IsEqualGUID(&wine_joystick, &dev_guid);
585 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
587 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
588 (IsJoystickGUID(rguid))) {
589 if ((riid == NULL) ||
590 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
591 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
592 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
593 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
594 return alloc_device(rguid, &JoystickAvt, dinput, pdev);
596 WARN("no interface\n");
598 return DIERR_NOINTERFACE;
602 WARN("invalid device GUID\n");
604 return DIERR_DEVICENOTREG;
607 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
609 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
610 (IsJoystickGUID(rguid))) {
611 if ((riid == NULL) ||
612 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
613 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
614 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
615 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
616 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev);
618 WARN("no interface\n");
620 return DIERR_NOINTERFACE;
624 WARN("invalid device GUID\n");
626 return DIERR_DEVICENOTREG;
629 const struct dinput_device joystick_linux_device = {
630 "Wine Linux joystick driver",
633 joydev_create_deviceA,
634 joydev_create_deviceW
637 /******************************************************************************
640 static ULONG WINAPI JoystickAImpl_Release(LPDIRECTINPUTDEVICE8A iface)
642 JoystickImpl *This = (JoystickImpl *)iface;
645 ref = InterlockedDecrement(&This->base.ref);
649 /* Free the device name */
650 HeapFree(GetProcessHeap(),0,This->name);
652 /* Free the axis map */
653 HeapFree(GetProcessHeap(),0,This->axis_map);
655 /* Free the data queue */
656 HeapFree(GetProcessHeap(), 0, This->base.data_queue);
658 /* Free the properties */
659 HeapFree(GetProcessHeap(), 0, This->props);
661 /* release the data transform filter */
662 HeapFree(GetProcessHeap(), 0, This->base.data_format.wine_df->rgodf);
663 HeapFree(GetProcessHeap(), 0, This->base.data_format.wine_df);
664 release_DataFormat(&This->base.data_format);
666 This->base.crit.DebugInfo->Spare[0] = 0;
667 IDirectInput_Release((LPDIRECTINPUTDEVICE8A)This->dinput);
668 DeleteCriticalSection(&This->base.crit);
670 HeapFree(GetProcessHeap(),0,This);
674 /******************************************************************************
675 * Acquire : gets exclusive control of the joystick
677 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
679 JoystickImpl *This = (JoystickImpl *)iface;
681 TRACE("(%p)\n",This);
683 if (This->base.acquired) {
684 WARN("already acquired\n");
688 /* open the joystick device */
689 if (This->joyfd==-1) {
690 TRACE("opening joystick device %s\n", This->dev);
692 This->joyfd=open(This->dev,O_RDONLY);
693 if (This->joyfd==-1) {
694 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
695 return DIERR_NOTFOUND;
699 This->base.acquired = 1;
704 /******************************************************************************
705 * Unacquire : frees the joystick
707 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
709 JoystickImpl *This = (JoystickImpl *)iface;
712 TRACE("(%p)\n",This);
714 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
716 if (This->joyfd!=-1) {
717 TRACE("closing joystick device\n");
726 static LONG map_axis(JoystickImpl * This, short val, short index)
729 double fmin = This->props[index].lMin;
730 double fmax = This->props[index].lMax;
733 fret = (((fval + 32767.0) * (fmax - fmin)) / (32767.0*2.0)) + fmin;
743 static LONG calculate_pov(JoystickImpl *This, int index)
745 if (This->povs[index].lX < -16384) {
746 if (This->povs[index].lY < -16384)
747 This->js.rgdwPOV[index] = 31500;
748 else if (This->povs[index].lY > 16384)
749 This->js.rgdwPOV[index] = 22500;
751 This->js.rgdwPOV[index] = 27000;
752 } else if (This->povs[index].lX > 16384) {
753 if (This->povs[index].lY < -16384)
754 This->js.rgdwPOV[index] = 4500;
755 else if (This->povs[index].lY > 16384)
756 This->js.rgdwPOV[index] = 13500;
758 This->js.rgdwPOV[index] = 9000;
760 if (This->povs[index].lY < -16384)
761 This->js.rgdwPOV[index] = 0;
762 else if (This->povs[index].lY > 16384)
763 This->js.rgdwPOV[index] = 18000;
765 This->js.rgdwPOV[index] = -1;
768 return This->js.rgdwPOV[index];
771 static void joy_polldev(JoystickImpl *This) {
774 TRACE("(%p)\n", This);
776 if (This->joyfd==-1) {
785 plfd.fd = This->joyfd;
786 plfd.events = POLLIN;
787 if (poll(&plfd,1,0) != 1)
789 /* we have one event, so we can read */
790 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
793 TRACE("js_event: type 0x%x, number %d, value %d\n",
794 jse.type,jse.number,jse.value);
795 if (jse.type & JS_EVENT_BUTTON)
797 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
798 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
800 else if (jse.type & JS_EVENT_AXIS)
802 int number = This->axis_map[jse.number]; /* wine format object index */
806 inst_id = DIDFT_MAKEINSTANCE(jse.number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
807 value = map_axis(This, jse.value, number);
808 /* FIXME do deadzone and saturation here */
810 TRACE("changing axis %d => %d\n", jse.number, number);
822 This->js.lRx = value;
825 This->js.lRy = value;
828 This->js.lRz = value;
831 This->js.rglSlider[0] = value;
834 This->js.rglSlider[1] = value;
837 /* FIXME don't go off array */
838 if (This->axis_map[jse.number + 1] == number)
839 This->povs[0].lX = jse.value;
840 else if (This->axis_map[jse.number - 1] == number)
841 This->povs[0].lY = jse.value;
842 value = calculate_pov(This, 0);
845 if (This->axis_map[jse.number + 1] == number)
846 This->povs[1].lX = jse.value;
847 else if (This->axis_map[jse.number - 1] == number)
848 This->povs[1].lY = jse.value;
849 value = calculate_pov(This, 1);
852 if (This->axis_map[jse.number + 1] == number)
853 This->povs[2].lX = jse.value;
854 else if (This->axis_map[jse.number - 1] == number)
855 This->povs[2].lY = jse.value;
856 value = calculate_pov(This, 2);
859 if (This->axis_map[jse.number + 1] == number)
860 This->povs[3].lX = jse.value;
861 else if (This->axis_map[jse.number - 1] == number)
862 This->povs[3].lY = jse.value;
863 value = calculate_pov(This, 3);
867 WARN("axis %d not supported\n", number);
870 queue_event((LPDIRECTINPUTDEVICE8A)This,
871 id_to_offset(&This->base.data_format, inst_id),
872 value, jse.time, This->dinput->evsequence++);
876 /******************************************************************************
877 * GetDeviceState : returns the "state" of the joystick.
880 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
881 LPDIRECTINPUTDEVICE8A iface,
885 JoystickImpl *This = (JoystickImpl *)iface;
887 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
889 if (!This->base.acquired) {
890 WARN("not acquired\n");
891 return DIERR_NOTACQUIRED;
894 /* update joystick state */
897 /* convert and copy data to user supplied buffer */
898 fill_DataFormat(ptr, &This->js, &This->base.data_format);
903 /******************************************************************************
904 * SetProperty : change input device properties
906 static HRESULT WINAPI JoystickAImpl_SetProperty(
907 LPDIRECTINPUTDEVICE8A iface,
911 JoystickImpl *This = (JoystickImpl *)iface;
914 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
917 WARN("invalid parameter: ph == NULL\n");
918 return DIERR_INVALIDPARAM;
921 if (TRACE_ON(dinput))
922 _dump_DIPROPHEADER(ph);
924 if (!HIWORD(rguid)) {
925 switch (LOWORD(rguid)) {
926 case (DWORD)DIPROP_RANGE: {
927 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
928 if (ph->dwHow == DIPH_DEVICE) {
929 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
930 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
931 This->props[i].lMin = pr->lMin;
932 This->props[i].lMax = pr->lMax;
935 int obj = find_property(&This->base.data_format, ph);
937 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
939 This->props[obj].lMin = pr->lMin;
940 This->props[obj].lMax = pr->lMax;
946 case (DWORD)DIPROP_DEADZONE: {
947 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
948 if (ph->dwHow == DIPH_DEVICE) {
949 TRACE("deadzone(%d) all\n", pd->dwData);
950 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
951 This->props[i].lDeadZone = pd->dwData;
953 int obj = find_property(&This->base.data_format, ph);
955 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
957 This->props[obj].lDeadZone = pd->dwData;
963 case (DWORD)DIPROP_SATURATION: {
964 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
965 if (ph->dwHow == DIPH_DEVICE) {
966 TRACE("saturation(%d) all\n", pd->dwData);
967 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
968 This->props[i].lSaturation = pd->dwData;
970 int obj = find_property(&This->base.data_format, ph);
972 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
974 This->props[obj].lSaturation = pd->dwData;
981 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
988 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
989 LPDIRECTINPUTDEVICE8A iface,
990 LPDIDEVCAPS lpDIDevCaps)
992 JoystickImpl *This = (JoystickImpl *)iface;
995 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
997 if (lpDIDevCaps == NULL) {
998 WARN("invalid pointer\n");
1002 size = lpDIDevCaps->dwSize;
1004 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
1005 WARN("invalid parameter\n");
1006 return DIERR_INVALIDPARAM;
1009 CopyMemory(lpDIDevCaps, &This->devcaps, size);
1010 lpDIDevCaps->dwSize = size;
1012 if (TRACE_ON(dinput))
1013 _dump_DIDEVCAPS(lpDIDevCaps);
1018 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
1020 JoystickImpl *This = (JoystickImpl *)iface;
1022 TRACE("(%p)\n",This);
1024 if (!This->base.acquired) {
1025 WARN("not acquired\n");
1026 return DIERR_NOTACQUIRED;
1033 /******************************************************************************
1034 * GetProperty : get input device properties
1036 static HRESULT WINAPI JoystickAImpl_GetProperty(
1037 LPDIRECTINPUTDEVICE8A iface,
1039 LPDIPROPHEADER pdiph)
1041 JoystickImpl *This = (JoystickImpl *)iface;
1043 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
1045 if (TRACE_ON(dinput))
1046 _dump_DIPROPHEADER(pdiph);
1048 if (!HIWORD(rguid)) {
1049 switch (LOWORD(rguid)) {
1050 case (DWORD) DIPROP_RANGE: {
1051 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
1052 int obj = find_property(&This->base.data_format, pdiph);
1054 /* The app is querying the current range of the axis
1055 * return the lMin and lMax values */
1057 pr->lMin = This->props[obj].lMin;
1058 pr->lMax = This->props[obj].lMax;
1059 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
1064 case (DWORD) DIPROP_DEADZONE: {
1065 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1066 int obj = find_property(&This->base.data_format, pdiph);
1069 pd->dwData = This->props[obj].lDeadZone;
1070 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
1075 case (DWORD) DIPROP_SATURATION: {
1076 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
1077 int obj = find_property(&This->base.data_format, pdiph);
1080 pd->dwData = This->props[obj].lSaturation;
1081 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
1087 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
1094 /******************************************************************************
1095 * GetObjectInfo : get object info
1097 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
1098 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
1100 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
1101 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
1102 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
1105 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
1106 if (res != DI_OK) return res;
1108 if (pdidoi->dwType & DIDFT_AXIS)
1109 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
1110 else if (pdidoi->dwType & DIDFT_POV)
1111 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
1112 else if (pdidoi->dwType & DIDFT_BUTTON)
1113 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
1115 _dump_OBJECTINSTANCEW(pdidoi);
1119 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
1120 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1123 DIDEVICEOBJECTINSTANCEW didoiW;
1124 DWORD dwSize = pdidoi->dwSize;
1126 didoiW.dwSize = sizeof(didoiW);
1127 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1128 if (res != DI_OK) return res;
1130 memset(pdidoi, 0, pdidoi->dwSize);
1131 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1132 pdidoi->dwSize = dwSize;
1133 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1134 sizeof(pdidoi->tszName), NULL, NULL);
1139 /******************************************************************************
1140 * GetDeviceInfo : get information about a device's identity
1142 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1143 LPDIRECTINPUTDEVICE8A iface,
1144 LPDIDEVICEINSTANCEA pdidi)
1146 JoystickImpl *This = (JoystickImpl *)iface;
1148 TRACE("(%p,%p)\n", iface, pdidi);
1150 if (pdidi == NULL) {
1151 WARN("invalid pointer\n");
1155 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1156 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1157 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1158 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1159 sizeof(DIDEVICEINSTANCEA));
1160 return DIERR_INVALIDPARAM;
1163 /* Return joystick */
1164 pdidi->guidInstance = GUID_Joystick;
1165 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1166 /* we only support traditional joysticks for now */
1167 pdidi->dwDevType = This->devcaps.dwDevType;
1168 strcpy(pdidi->tszInstanceName, "Joystick");
1169 strcpy(pdidi->tszProductName, This->name);
1170 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1171 pdidi->guidFFDriver = GUID_NULL;
1172 pdidi->wUsagePage = 0;
1179 /******************************************************************************
1180 * GetDeviceInfo : get information about a device's identity
1182 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1183 LPDIRECTINPUTDEVICE8W iface,
1184 LPDIDEVICEINSTANCEW pdidi)
1186 JoystickImpl *This = (JoystickImpl *)iface;
1188 TRACE("(%p,%p)\n", iface, pdidi);
1190 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1191 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1192 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1193 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1194 sizeof(DIDEVICEINSTANCEW));
1195 return DIERR_INVALIDPARAM;
1198 /* Return joystick */
1199 pdidi->guidInstance = GUID_Joystick;
1200 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1201 /* we only support traditional joysticks for now */
1202 pdidi->dwDevType = This->devcaps.dwDevType;
1203 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1204 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1205 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1206 pdidi->guidFFDriver = GUID_NULL;
1207 pdidi->wUsagePage = 0;
1214 static const IDirectInputDevice8AVtbl JoystickAvt =
1216 IDirectInputDevice2AImpl_QueryInterface,
1217 IDirectInputDevice2AImpl_AddRef,
1218 JoystickAImpl_Release,
1219 JoystickAImpl_GetCapabilities,
1220 IDirectInputDevice2AImpl_EnumObjects,
1221 JoystickAImpl_GetProperty,
1222 JoystickAImpl_SetProperty,
1223 JoystickAImpl_Acquire,
1224 JoystickAImpl_Unacquire,
1225 JoystickAImpl_GetDeviceState,
1226 IDirectInputDevice2AImpl_GetDeviceData,
1227 IDirectInputDevice2AImpl_SetDataFormat,
1228 IDirectInputDevice2AImpl_SetEventNotification,
1229 IDirectInputDevice2AImpl_SetCooperativeLevel,
1230 JoystickAImpl_GetObjectInfo,
1231 JoystickAImpl_GetDeviceInfo,
1232 IDirectInputDevice2AImpl_RunControlPanel,
1233 IDirectInputDevice2AImpl_Initialize,
1234 IDirectInputDevice2AImpl_CreateEffect,
1235 IDirectInputDevice2AImpl_EnumEffects,
1236 IDirectInputDevice2AImpl_GetEffectInfo,
1237 IDirectInputDevice2AImpl_GetForceFeedbackState,
1238 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1239 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1240 IDirectInputDevice2AImpl_Escape,
1242 IDirectInputDevice2AImpl_SendDeviceData,
1243 IDirectInputDevice7AImpl_EnumEffectsInFile,
1244 IDirectInputDevice7AImpl_WriteEffectToFile,
1245 IDirectInputDevice8AImpl_BuildActionMap,
1246 IDirectInputDevice8AImpl_SetActionMap,
1247 IDirectInputDevice8AImpl_GetImageInfo
1250 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1251 # define XCAST(fun) (typeof(SysJoystickWvt.fun))
1253 # define XCAST(fun) (void*)
1256 static const IDirectInputDevice8WVtbl SysJoystickWvt =
1258 IDirectInputDevice2WImpl_QueryInterface,
1259 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1260 XCAST(Release)JoystickAImpl_Release,
1261 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1262 IDirectInputDevice2WImpl_EnumObjects,
1263 XCAST(GetProperty)JoystickAImpl_GetProperty,
1264 XCAST(SetProperty)JoystickAImpl_SetProperty,
1265 XCAST(Acquire)JoystickAImpl_Acquire,
1266 XCAST(Unacquire)JoystickAImpl_Unacquire,
1267 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1268 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1269 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1270 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1271 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1272 IDirectInputDevice2WImpl_GetObjectInfo,
1273 JoystickWImpl_GetDeviceInfo,
1274 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1275 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1276 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1277 IDirectInputDevice2WImpl_EnumEffects,
1278 IDirectInputDevice2WImpl_GetEffectInfo,
1279 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1280 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1281 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1282 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1283 XCAST(Poll)JoystickAImpl_Poll,
1284 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1285 IDirectInputDevice7WImpl_EnumEffectsInFile,
1286 IDirectInputDevice7WImpl_WriteEffectToFile,
1287 IDirectInputDevice8WImpl_BuildActionMap,
1288 IDirectInputDevice8WImpl_SetActionMap,
1289 IDirectInputDevice8WImpl_GetImageInfo
1293 #else /* HAVE_LINUX_22_JOYSTICK_API */
1295 const struct dinput_device joystick_linux_device = {
1296 "Wine Linux joystick driver",
1303 #endif /* HAVE_LINUX_22_JOYSTICK_API */