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