1 /* DirectInput Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "wine/port.h"
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
46 #ifdef HAVE_SYS_ERRNO_H
47 # include <sys/errno.h>
49 #ifdef HAVE_LINUX_IOCTL_H
50 # include <linux/ioctl.h>
52 #ifdef HAVE_LINUX_JOYSTICK_H
53 # include <linux/joystick.h>
56 #ifdef HAVE_SYS_POLL_H
57 # include <sys/poll.h>
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
68 #include "dinput_private.h"
69 #include "device_private.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
73 #ifdef HAVE_LINUX_22_JOYSTICK_API
75 #define JOYDEV_NEW "/dev/input/js"
76 #define JOYDEV_OLD "/dev/js"
78 typedef struct JoystickImpl JoystickImpl;
79 static const IDirectInputDevice8AVtbl JoystickAvt;
80 static const IDirectInputDevice8WVtbl JoystickWvt;
83 struct IDirectInputDevice2AImpl base;
87 /* joystick private */
89 DIJOYSTATE2 js; /* wine data */
100 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
104 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
107 static void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
109 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
110 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
111 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
112 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
113 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
114 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
115 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
116 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
117 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
118 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
119 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
120 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
121 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
122 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
123 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
124 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
125 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
126 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
130 #define MAX_JOYSTICKS 64
131 static INT joystick_devices_count = -1;
132 static LPSTR joystick_devices[MAX_JOYSTICKS];
134 static INT find_joystick_devices(void)
138 if (joystick_devices_count != -1) return joystick_devices_count;
140 joystick_devices_count = 0;
141 for (i = 0; i < MAX_JOYSTICKS; i++)
143 CHAR device_name[MAX_PATH], *str;
147 len = sprintf(device_name, "%s%d", JOYDEV_NEW, i) + 1;
148 if ((fd = open(device_name, O_RDONLY)) < 0)
150 len = sprintf(device_name, "%s%d", JOYDEV_OLD, i) + 1;
151 if ((fd = open(device_name, O_RDONLY)) < 0) continue;
154 if (!(str = HeapAlloc(GetProcessHeap(), 0, len))) break;
155 memcpy(str, device_name, len);
157 joystick_devices[joystick_devices_count++] = str;
160 return joystick_devices_count;
163 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
167 if (id >= find_joystick_devices()) return FALSE;
169 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
170 WARN("force feedback not supported\n");
174 if ((dwDevType == 0) ||
175 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
176 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
177 /* check whether we have a joystick */
178 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
180 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
184 /* Return joystick */
185 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
186 lpddi->guidInstance.Data3 = id;
187 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
188 /* we only support traditional joysticks for now */
189 if (version >= 0x0800)
190 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
192 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
193 sprintf(lpddi->tszInstanceName, "Joystick %d", id);
194 #if defined(JSIOCGNAME)
195 if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
196 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
197 strcpy(lpddi->tszProductName, "Wine Joystick");
200 strcpy(lpddi->tszProductName, "Wine Joystick");
203 lpddi->guidFFDriver = GUID_NULL;
205 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], lpddi->tszProductName);
212 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
218 if (id >= find_joystick_devices()) return FALSE;
220 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
221 WARN("force feedback not supported\n");
225 if ((dwDevType == 0) ||
226 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
227 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
228 /* check whether we have a joystick */
229 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
231 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
235 /* Return joystick */
236 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
237 lpddi->guidInstance.Data3 = id;
238 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
239 /* we only support traditional joysticks for now */
240 if (version >= 0x0800)
241 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
243 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
244 sprintf(friendly, "Joystick %d", id);
245 MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
246 #if defined(JSIOCGNAME)
247 if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
248 WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
249 strcpy(name, "Wine Joystick");
252 strcpy(name, "Wine Joystick");
254 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
255 lpddi->guidFFDriver = GUID_NULL;
257 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], name);
265 * Setup the dinput options.
268 static HRESULT setup_dinput_options(JoystickImpl * device)
270 char buffer[MAX_PATH+16];
273 buffer[MAX_PATH]='\0';
275 get_app_key(&hkey, &appkey);
279 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
280 device->deadzone = atoi(buffer);
281 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
284 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
288 const char *delim = ",";
290 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
292 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
293 if (device->axis_map == 0)
294 return DIERR_OUTOFMEMORY;
296 if ((ptr = strtok(buffer, delim)) != NULL) {
298 if (strcmp(ptr, "X") == 0) {
299 device->axis_map[tokens] = 0;
301 } else if (strcmp(ptr, "Y") == 0) {
302 device->axis_map[tokens] = 1;
304 } else if (strcmp(ptr, "Z") == 0) {
305 device->axis_map[tokens] = 2;
307 } else if (strcmp(ptr, "Rx") == 0) {
308 device->axis_map[tokens] = 3;
310 } else if (strcmp(ptr, "Ry") == 0) {
311 device->axis_map[tokens] = 4;
313 } else if (strcmp(ptr, "Rz") == 0) {
314 device->axis_map[tokens] = 5;
316 } else if (strcmp(ptr, "Slider1") == 0) {
317 device->axis_map[tokens] = 6;
319 } else if (strcmp(ptr, "Slider2") == 0) {
320 device->axis_map[tokens] = 7;
322 } else if (strcmp(ptr, "POV1") == 0) {
323 device->axis_map[tokens++] = 8;
324 device->axis_map[tokens] = 8;
326 } else if (strcmp(ptr, "POV2") == 0) {
327 device->axis_map[tokens++] = 9;
328 device->axis_map[tokens] = 9;
330 } else if (strcmp(ptr, "POV3") == 0) {
331 device->axis_map[tokens++] = 10;
332 device->axis_map[tokens] = 10;
334 } else if (strcmp(ptr, "POV4") == 0) {
335 device->axis_map[tokens++] = 11;
336 device->axis_map[tokens] = 11;
339 ERR("invalid joystick axis type: %s\n", ptr);
340 device->axis_map[tokens] = tokens;
345 } while ((ptr = strtok(NULL, delim)) != NULL);
347 if (tokens != device->devcaps.dwAxes) {
348 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
349 while (tokens < device->axes) {
350 device->axis_map[tokens] = tokens;
356 device->devcaps.dwAxes = axis;
357 device->devcaps.dwPOVs = pov;
361 RegCloseKey( appkey );
369 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
370 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
373 JoystickImpl* newDevice;
376 LPDIDATAFORMAT df = NULL;
379 TRACE("%s %p %p %p %hu\n", debugstr_guid(rguid), jvt, dinput, pdev, index);
381 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
382 if (newDevice == 0) {
383 WARN("out of memory\n");
385 return DIERR_OUTOFMEMORY;
388 if (!lstrcpynA(newDevice->dev, joystick_devices[index], sizeof(newDevice->dev)) ||
389 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
391 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
392 HeapFree(GetProcessHeap(), 0, newDevice);
393 return DIERR_DEVICENOTREG;
396 /* get the device name */
397 #if defined(JSIOCGNAME)
398 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
399 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
400 strcpy(name, "Wine Joystick");
403 strcpy(name, "Wine Joystick");
406 /* copy the device name */
407 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
408 strcpy(newDevice->name, name);
411 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
412 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
417 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
418 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
419 newDevice->buttons = 2;
423 newDevice->base.lpVtbl = jvt;
424 newDevice->base.ref = 1;
425 newDevice->base.dinput = dinput;
426 newDevice->base.guid = *rguid;
427 InitializeCriticalSection(&newDevice->base.crit);
428 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
430 /* setup_dinput_options may change these */
431 newDevice->deadzone = 0;
432 newDevice->devcaps.dwButtons = newDevice->buttons;
433 newDevice->devcaps.dwAxes = newDevice->axes;
434 newDevice->devcaps.dwPOVs = 0;
436 /* do any user specified configuration */
437 hr = setup_dinput_options(newDevice);
441 if (newDevice->axis_map == 0) {
442 newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
443 if (newDevice->axis_map == 0)
446 for (i = 0; i < newDevice->axes; i++)
447 newDevice->axis_map[i] = i;
450 /* Create copy of default data format */
451 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
452 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
454 /* Axes include POVs */
455 df->dwNumObjs = newDevice->axes + newDevice->buttons;
456 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
458 for (i = 0; i < newDevice->axes; i++)
460 int wine_obj = newDevice->axis_map[i];
462 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
464 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
467 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
468 i++; /* POV takes 2 axes */
471 for (i = 0; i < newDevice->buttons; i++)
473 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
474 df->rgodf[idx ].pguid = &GUID_Button;
475 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
477 newDevice->base.data_format.wine_df = df;
479 /* create default properties */
480 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
481 if (newDevice->props == 0)
484 /* initialize default properties */
485 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
486 newDevice->props[i].lDevMin = -32767;
487 newDevice->props[i].lDevMax = +32767;
488 newDevice->props[i].lMin = 0;
489 newDevice->props[i].lMax = 0xffff;
490 newDevice->props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
491 newDevice->props[i].lSaturation = 0;
494 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
496 newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
497 newDevice->devcaps.dwFlags = DIDC_ATTACHED;
498 if (newDevice->base.dinput->dwVersion >= 0x0800)
499 newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
501 newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
502 newDevice->devcaps.dwFFSamplePeriod = 0;
503 newDevice->devcaps.dwFFMinTimeResolution = 0;
504 newDevice->devcaps.dwFirmwareRevision = 0;
505 newDevice->devcaps.dwHardwareRevision = 0;
506 newDevice->devcaps.dwFFDriverVersion = 0;
508 if (TRACE_ON(dinput)) {
509 _dump_DIDATAFORMAT(newDevice->base.data_format.wine_df);
510 for (i = 0; i < (newDevice->axes); i++)
511 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
512 _dump_DIDEVCAPS(&newDevice->devcaps);
515 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
520 hr = DIERR_OUTOFMEMORY;
522 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
523 HeapFree(GetProcessHeap(), 0, df);
524 release_DataFormat(&newDevice->base.data_format);
525 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
526 HeapFree(GetProcessHeap(),0,newDevice->name);
527 HeapFree(GetProcessHeap(),0,newDevice->props);
528 HeapFree(GetProcessHeap(),0,newDevice);
534 /******************************************************************************
535 * get_joystick_index : Get the joystick index from a given GUID
537 static unsigned short get_joystick_index(REFGUID guid)
539 GUID wine_joystick = DInput_Wine_Joystick_GUID;
540 GUID dev_guid = *guid;
542 wine_joystick.Data3 = 0;
545 /* for the standard joystick GUID use index 0 */
546 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
548 /* for the wine joystick GUIDs use the index stored in Data3 */
549 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
551 return MAX_JOYSTICKS;
554 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
556 unsigned short index;
558 TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
559 find_joystick_devices();
562 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
563 joystick_devices_count && index < joystick_devices_count)
565 if ((riid == NULL) ||
566 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
567 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
568 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
569 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
571 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
574 WARN("no interface\n");
575 return DIERR_NOINTERFACE;
578 return DIERR_DEVICENOTREG;
581 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
583 unsigned short index;
585 TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
586 find_joystick_devices();
589 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
590 joystick_devices_count && index < joystick_devices_count)
592 if ((riid == NULL) ||
593 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
594 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
595 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
596 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
598 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
600 WARN("no interface\n");
601 return DIERR_NOINTERFACE;
604 WARN("invalid device GUID %s\n",debugstr_guid(rguid));
605 return DIERR_DEVICENOTREG;
610 const struct dinput_device joystick_linux_device = {
611 "Wine Linux joystick driver",
614 joydev_create_deviceA,
615 joydev_create_deviceW
618 /******************************************************************************
619 * Acquire : gets exclusive control of the joystick
621 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
623 JoystickImpl *This = (JoystickImpl *)iface;
625 TRACE("(%p)\n",This);
627 if (This->base.acquired) {
628 WARN("already acquired\n");
632 /* open the joystick device */
633 if (This->joyfd==-1) {
634 TRACE("opening joystick device %s\n", This->dev);
636 This->joyfd=open(This->dev,O_RDONLY);
637 if (This->joyfd==-1) {
638 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
639 return DIERR_NOTFOUND;
643 This->base.acquired = 1;
648 /******************************************************************************
649 * Unacquire : frees the joystick
651 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
653 JoystickImpl *This = (JoystickImpl *)iface;
656 TRACE("(%p)\n",This);
658 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
660 if (This->joyfd!=-1) {
661 TRACE("closing joystick device\n");
670 static void joy_polldev(JoystickImpl *This) {
673 TRACE("(%p)\n", This);
675 if (This->joyfd==-1) {
684 plfd.fd = This->joyfd;
685 plfd.events = POLLIN;
686 if (poll(&plfd,1,0) != 1)
688 /* we have one event, so we can read */
689 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
692 TRACE("js_event: type 0x%x, number %d, value %d\n",
693 jse.type,jse.number,jse.value);
694 if (jse.type & JS_EVENT_BUTTON)
696 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
697 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
699 else if (jse.type & JS_EVENT_AXIS)
701 int number = This->axis_map[jse.number]; /* wine format object index */
703 if (number < 0) return;
704 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
705 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
707 TRACE("changing axis %d => %d\n", jse.number, number);
710 case 0: This->js.lX = value; break;
711 case 1: This->js.lY = value; break;
712 case 2: This->js.lZ = value; break;
713 case 3: This->js.lRx = value; break;
714 case 4: This->js.lRy = value; break;
715 case 5: This->js.lRz = value; break;
716 case 6: This->js.rglSlider[0] = value; break;
717 case 7: This->js.rglSlider[1] = value; break;
718 case 8: case 9: case 10: case 11:
720 int idx = number - 8;
723 This->povs[idx].y = jse.value;
725 This->povs[idx].x = jse.value;
727 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
731 WARN("axis %d not supported\n", number);
735 queue_event((LPDIRECTINPUTDEVICE8A)This,
736 id_to_offset(&This->base.data_format, inst_id),
737 value, jse.time, This->base.dinput->evsequence++);
741 /******************************************************************************
742 * GetDeviceState : returns the "state" of the joystick.
745 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
746 LPDIRECTINPUTDEVICE8A iface,
750 JoystickImpl *This = (JoystickImpl *)iface;
752 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
754 if (!This->base.acquired) {
755 WARN("not acquired\n");
756 return DIERR_NOTACQUIRED;
759 /* update joystick state */
762 /* convert and copy data to user supplied buffer */
763 fill_DataFormat(ptr, &This->js, &This->base.data_format);
768 /******************************************************************************
769 * SetProperty : change input device properties
771 static HRESULT WINAPI JoystickAImpl_SetProperty(
772 LPDIRECTINPUTDEVICE8A iface,
776 JoystickImpl *This = (JoystickImpl *)iface;
779 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
782 WARN("invalid parameter: ph == NULL\n");
783 return DIERR_INVALIDPARAM;
786 if (TRACE_ON(dinput))
787 _dump_DIPROPHEADER(ph);
789 if (!HIWORD(rguid)) {
790 switch (LOWORD(rguid)) {
791 case (DWORD)DIPROP_RANGE: {
792 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
793 if (ph->dwHow == DIPH_DEVICE) {
794 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
795 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
796 This->props[i].lMin = pr->lMin;
797 This->props[i].lMax = pr->lMax;
800 int obj = find_property(&This->base.data_format, ph);
802 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
804 This->props[obj].lMin = pr->lMin;
805 This->props[obj].lMax = pr->lMax;
811 case (DWORD)DIPROP_DEADZONE: {
812 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
813 if (ph->dwHow == DIPH_DEVICE) {
814 TRACE("deadzone(%d) all\n", pd->dwData);
815 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
816 This->props[i].lDeadZone = pd->dwData;
818 int obj = find_property(&This->base.data_format, ph);
820 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
822 This->props[obj].lDeadZone = pd->dwData;
828 case (DWORD)DIPROP_SATURATION: {
829 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
830 if (ph->dwHow == DIPH_DEVICE) {
831 TRACE("saturation(%d) all\n", pd->dwData);
832 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
833 This->props[i].lSaturation = pd->dwData;
835 int obj = find_property(&This->base.data_format, ph);
837 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
839 This->props[obj].lSaturation = pd->dwData;
846 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
853 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
854 LPDIRECTINPUTDEVICE8A iface,
855 LPDIDEVCAPS lpDIDevCaps)
857 JoystickImpl *This = (JoystickImpl *)iface;
860 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
862 if (lpDIDevCaps == NULL) {
863 WARN("invalid pointer\n");
867 size = lpDIDevCaps->dwSize;
869 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
870 WARN("invalid parameter\n");
871 return DIERR_INVALIDPARAM;
874 CopyMemory(lpDIDevCaps, &This->devcaps, size);
875 lpDIDevCaps->dwSize = size;
877 if (TRACE_ON(dinput))
878 _dump_DIDEVCAPS(lpDIDevCaps);
883 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
885 JoystickImpl *This = (JoystickImpl *)iface;
887 TRACE("(%p)\n",This);
889 if (!This->base.acquired) {
890 WARN("not acquired\n");
891 return DIERR_NOTACQUIRED;
898 /******************************************************************************
899 * GetProperty : get input device properties
901 static HRESULT WINAPI JoystickAImpl_GetProperty(
902 LPDIRECTINPUTDEVICE8A iface,
904 LPDIPROPHEADER pdiph)
906 JoystickImpl *This = (JoystickImpl *)iface;
908 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
910 if (TRACE_ON(dinput))
911 _dump_DIPROPHEADER(pdiph);
913 if (!HIWORD(rguid)) {
914 switch (LOWORD(rguid)) {
915 case (DWORD) DIPROP_RANGE: {
916 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
917 int obj = find_property(&This->base.data_format, pdiph);
919 /* The app is querying the current range of the axis
920 * return the lMin and lMax values */
922 pr->lMin = This->props[obj].lMin;
923 pr->lMax = This->props[obj].lMax;
924 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
929 case (DWORD) DIPROP_DEADZONE: {
930 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
931 int obj = find_property(&This->base.data_format, pdiph);
934 pd->dwData = This->props[obj].lDeadZone;
935 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
940 case (DWORD) DIPROP_SATURATION: {
941 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
942 int obj = find_property(&This->base.data_format, pdiph);
945 pd->dwData = This->props[obj].lSaturation;
946 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
952 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
959 /******************************************************************************
960 * GetObjectInfo : get object info
962 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
963 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
965 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
966 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
967 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
970 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
971 if (res != DI_OK) return res;
973 if (pdidoi->dwType & DIDFT_AXIS)
974 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
975 else if (pdidoi->dwType & DIDFT_POV)
976 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
977 else if (pdidoi->dwType & DIDFT_BUTTON)
978 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
980 _dump_OBJECTINSTANCEW(pdidoi);
984 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
985 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
988 DIDEVICEOBJECTINSTANCEW didoiW;
989 DWORD dwSize = pdidoi->dwSize;
991 didoiW.dwSize = sizeof(didoiW);
992 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
993 if (res != DI_OK) return res;
995 memset(pdidoi, 0, pdidoi->dwSize);
996 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
997 pdidoi->dwSize = dwSize;
998 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
999 sizeof(pdidoi->tszName), NULL, NULL);
1004 /******************************************************************************
1005 * GetDeviceInfo : get information about a device's identity
1007 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1008 LPDIRECTINPUTDEVICE8A iface,
1009 LPDIDEVICEINSTANCEA pdidi)
1011 JoystickImpl *This = (JoystickImpl *)iface;
1013 TRACE("(%p,%p)\n", iface, pdidi);
1015 if (pdidi == NULL) {
1016 WARN("invalid pointer\n");
1020 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1021 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1022 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1023 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1024 sizeof(DIDEVICEINSTANCEA));
1025 return DIERR_INVALIDPARAM;
1028 /* Return joystick */
1029 pdidi->guidInstance = GUID_Joystick;
1030 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1031 /* we only support traditional joysticks for now */
1032 pdidi->dwDevType = This->devcaps.dwDevType;
1033 strcpy(pdidi->tszInstanceName, "Joystick");
1034 strcpy(pdidi->tszProductName, This->name);
1035 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1036 pdidi->guidFFDriver = GUID_NULL;
1037 pdidi->wUsagePage = 0;
1044 /******************************************************************************
1045 * GetDeviceInfo : get information about a device's identity
1047 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1048 LPDIRECTINPUTDEVICE8W iface,
1049 LPDIDEVICEINSTANCEW pdidi)
1051 JoystickImpl *This = (JoystickImpl *)iface;
1053 TRACE("(%p,%p)\n", iface, pdidi);
1055 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1056 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1057 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1058 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1059 sizeof(DIDEVICEINSTANCEW));
1060 return DIERR_INVALIDPARAM;
1063 /* Return joystick */
1064 pdidi->guidInstance = GUID_Joystick;
1065 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1066 /* we only support traditional joysticks for now */
1067 pdidi->dwDevType = This->devcaps.dwDevType;
1068 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1069 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1070 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1071 pdidi->guidFFDriver = GUID_NULL;
1072 pdidi->wUsagePage = 0;
1079 static const IDirectInputDevice8AVtbl JoystickAvt =
1081 IDirectInputDevice2AImpl_QueryInterface,
1082 IDirectInputDevice2AImpl_AddRef,
1083 IDirectInputDevice2AImpl_Release,
1084 JoystickAImpl_GetCapabilities,
1085 IDirectInputDevice2AImpl_EnumObjects,
1086 JoystickAImpl_GetProperty,
1087 JoystickAImpl_SetProperty,
1088 JoystickAImpl_Acquire,
1089 JoystickAImpl_Unacquire,
1090 JoystickAImpl_GetDeviceState,
1091 IDirectInputDevice2AImpl_GetDeviceData,
1092 IDirectInputDevice2AImpl_SetDataFormat,
1093 IDirectInputDevice2AImpl_SetEventNotification,
1094 IDirectInputDevice2AImpl_SetCooperativeLevel,
1095 JoystickAImpl_GetObjectInfo,
1096 JoystickAImpl_GetDeviceInfo,
1097 IDirectInputDevice2AImpl_RunControlPanel,
1098 IDirectInputDevice2AImpl_Initialize,
1099 IDirectInputDevice2AImpl_CreateEffect,
1100 IDirectInputDevice2AImpl_EnumEffects,
1101 IDirectInputDevice2AImpl_GetEffectInfo,
1102 IDirectInputDevice2AImpl_GetForceFeedbackState,
1103 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1104 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1105 IDirectInputDevice2AImpl_Escape,
1107 IDirectInputDevice2AImpl_SendDeviceData,
1108 IDirectInputDevice7AImpl_EnumEffectsInFile,
1109 IDirectInputDevice7AImpl_WriteEffectToFile,
1110 IDirectInputDevice8AImpl_BuildActionMap,
1111 IDirectInputDevice8AImpl_SetActionMap,
1112 IDirectInputDevice8AImpl_GetImageInfo
1115 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1116 # define XCAST(fun) (typeof(JoystickWvt.fun))
1118 # define XCAST(fun) (void*)
1121 static const IDirectInputDevice8WVtbl JoystickWvt =
1123 IDirectInputDevice2WImpl_QueryInterface,
1124 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1125 XCAST(Release)IDirectInputDevice2AImpl_Release,
1126 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1127 IDirectInputDevice2WImpl_EnumObjects,
1128 XCAST(GetProperty)JoystickAImpl_GetProperty,
1129 XCAST(SetProperty)JoystickAImpl_SetProperty,
1130 XCAST(Acquire)JoystickAImpl_Acquire,
1131 XCAST(Unacquire)JoystickAImpl_Unacquire,
1132 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1133 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1134 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1135 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1136 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1137 IDirectInputDevice2WImpl_GetObjectInfo,
1138 JoystickWImpl_GetDeviceInfo,
1139 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1140 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1141 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1142 IDirectInputDevice2WImpl_EnumEffects,
1143 IDirectInputDevice2WImpl_GetEffectInfo,
1144 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1145 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1146 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1147 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1148 XCAST(Poll)JoystickAImpl_Poll,
1149 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1150 IDirectInputDevice7WImpl_EnumEffectsInFile,
1151 IDirectInputDevice7WImpl_WriteEffectToFile,
1152 IDirectInputDevice8WImpl_BuildActionMap,
1153 IDirectInputDevice8WImpl_SetActionMap,
1154 IDirectInputDevice8WImpl_GetImageInfo
1158 #else /* HAVE_LINUX_22_JOYSTICK_API */
1160 const struct dinput_device joystick_linux_device = {
1161 "Wine Linux joystick driver",
1168 #endif /* HAVE_LINUX_22_JOYSTICK_API */