winefile: Don't access uninitialized value.
[wine/hacks.git] / dlls / dinput / joystick_linux.c
blob9dead0a9c781d94dc6b16e8da8e92d35263a2121
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
23 * To Do:
24 * dead zone
25 * force feedback
28 #include "config.h"
29 #include "wine/port.h"
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
40 #endif
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #include <errno.h>
46 #ifdef HAVE_SYS_ERRNO_H
47 # include <sys/errno.h>
48 #endif
49 #ifdef HAVE_LINUX_IOCTL_H
50 # include <linux/ioctl.h>
51 #endif
52 #ifdef HAVE_LINUX_JOYSTICK_H
53 # include <linux/joystick.h>
54 # undef SW_MAX
55 #endif
56 #ifdef HAVE_SYS_POLL_H
57 # include <sys/poll.h>
58 #endif
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
62 #include "windef.h"
63 #include "winbase.h"
64 #include "winerror.h"
65 #include "winreg.h"
66 #include "dinput.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;
81 struct JoystickImpl
83 struct IDirectInputDevice2AImpl base;
85 char dev[32];
87 /* joystick private */
88 int joyfd;
89 DIJOYSTATE2 js; /* wine data */
90 ObjProps *props;
91 char *name;
92 DIDEVCAPS devcaps;
93 LONG deadzone;
94 int *axis_map;
95 int axes;
96 POINTL povs[4];
99 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
100 0x9e573ed9,
101 0x7734,
102 0x11d2,
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)
135 INT i;
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;
143 INT len;
144 int fd;
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;
153 close(fd);
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)
166 int fd = -1;
168 if (id >= find_joystick_devices()) return FALSE;
170 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
171 WARN("force feedback not supported\n");
172 return FALSE;
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));
182 return FALSE;
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);
192 else
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");
200 #else
201 strcpy(lpddi->tszProductName, "Wine Joystick");
202 #endif
204 lpddi->guidFFDriver = GUID_NULL;
205 close(fd);
206 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], lpddi->tszProductName);
207 return TRUE;
210 return FALSE;
213 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
215 int fd = -1;
216 char name[MAX_PATH];
217 char friendly[32];
219 if (id >= find_joystick_devices()) return FALSE;
221 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
222 WARN("force feedback not supported\n");
223 return FALSE;
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));
233 return FALSE;
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);
243 else
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");
252 #else
253 strcpy(name, "Wine Joystick");
254 #endif
255 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
256 lpddi->guidFFDriver = GUID_NULL;
257 close(fd);
258 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], name);
259 return TRUE;
262 return FALSE;
266 * Setup the dinput options.
269 static HRESULT setup_dinput_options(JoystickImpl * device)
271 char buffer[MAX_PATH+16];
272 HKEY hkey, appkey;
273 int tokens = 0;
274 int axis = 0;
275 int pov = 0;
277 buffer[MAX_PATH]='\0';
279 get_app_key(&hkey, &appkey);
281 /* get options */
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 = ",";
296 char * ptr;
297 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
299 if ((ptr = strtok(buffer, delim)) != NULL) {
300 do {
301 int i;
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))
308 if (pov >= 4)
310 WARN("Only 4 POVs supported - ignoring extra\n");
311 i = -1;
313 else
315 /* Pov takes two axes */
316 device->axis_map[tokens++] = i;
317 pov++;
320 else
322 if (axis >= 8)
324 FIXME("Only 8 Axes supported - ignoring extra\n");
325 i = -1;
327 else
328 axis++;
330 break;
333 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
335 ERR("invalid joystick axis type: \"%s\"\n", ptr);
336 i = -1;
339 device->axis_map[tokens] = i;
340 tokens++;
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;
347 tokens++;
352 else
354 for (tokens = 0; tokens < device->axes; tokens++)
356 if (tokens < 8)
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++;
363 else
364 device->axis_map[tokens] = -1;
367 device->devcaps.dwAxes = axis;
368 device->devcaps.dwPOVs = pov;
370 if (appkey)
371 RegCloseKey( appkey );
373 if (hkey)
374 RegCloseKey( hkey );
376 return DI_OK;
379 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
380 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
382 DWORD i;
383 JoystickImpl* newDevice;
384 char name[MAX_PATH];
385 HRESULT hr;
386 LPDIDATAFORMAT df = NULL;
387 int idx = 0;
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");
394 *pdev = 0;
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");
412 #else
413 strcpy(name, "Wine Joystick");
414 #endif
416 /* copy the device name */
417 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
418 strcpy(newDevice->name, name);
420 #ifdef JSIOCGAXES
421 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
422 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
423 newDevice->axes = 2;
425 #endif
426 #ifdef JSIOCGBUTTONS
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;
431 #endif
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);
451 if (hr != DI_OK)
452 goto FAILED1;
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);
468 if (wine_obj < 8)
469 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
470 else
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)
487 goto FAILED;
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);
505 else
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;
522 return DI_OK;
524 FAILED:
525 hr = DIERR_OUTOFMEMORY;
526 FAILED1:
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);
534 *pdev = 0;
536 return hr;
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;
548 dev_guid.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();
565 *pdev = NULL;
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();
592 *pdev = NULL;
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;
613 #undef MAX_JOYSTICKS
615 const struct dinput_device joystick_linux_device = {
616 "Wine Linux joystick driver",
617 joydev_enum_deviceA,
618 joydev_enum_deviceW,
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");
634 return S_FALSE;
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;
650 return DI_OK;
653 /******************************************************************************
654 * Unacquire : frees the joystick
656 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
658 JoystickImpl *This = (JoystickImpl *)iface;
659 HRESULT res;
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");
667 close(This->joyfd);
668 This->joyfd = -1;
669 return DI_OK;
672 return DI_NOEFFECT;
675 static void joy_polldev(JoystickImpl *This) {
676 struct pollfd plfd;
677 struct js_event jse;
678 TRACE("(%p)\n", This);
680 if (This->joyfd==-1) {
681 WARN("no device\n");
682 return;
684 while (1)
686 LONG value;
687 int inst_id = -1;
689 plfd.fd = This->joyfd;
690 plfd.events = POLLIN;
691 if (poll(&plfd,1,0) != 1)
692 return;
693 /* we have one event, so we can read */
694 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
695 return;
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 if (jse.number >= This->devcaps.dwButtons) return;
703 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
704 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
706 else if (jse.type & JS_EVENT_AXIS)
708 int number = This->axis_map[jse.number]; /* wine format object index */
710 if (number < 0) return;
711 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
712 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
714 TRACE("changing axis %d => %d\n", jse.number, number);
715 switch (number)
717 case 0: This->js.lX = value; break;
718 case 1: This->js.lY = value; break;
719 case 2: This->js.lZ = value; break;
720 case 3: This->js.lRx = value; break;
721 case 4: This->js.lRy = value; break;
722 case 5: This->js.lRz = value; break;
723 case 6: This->js.rglSlider[0] = value; break;
724 case 7: This->js.rglSlider[1] = value; break;
725 case 8: case 9: case 10: case 11:
727 int idx = number - 8;
729 if (jse.number % 2)
730 This->povs[idx].y = jse.value;
731 else
732 This->povs[idx].x = jse.value;
734 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
735 break;
737 default:
738 WARN("axis %d not supported\n", number);
741 if (inst_id >= 0)
742 queue_event((LPDIRECTINPUTDEVICE8A)This,
743 id_to_offset(&This->base.data_format, inst_id),
744 value, jse.time, This->base.dinput->evsequence++);
748 /******************************************************************************
749 * GetDeviceState : returns the "state" of the joystick.
752 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
753 LPDIRECTINPUTDEVICE8A iface,
754 DWORD len,
755 LPVOID ptr)
757 JoystickImpl *This = (JoystickImpl *)iface;
759 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
761 if (!This->base.acquired) {
762 WARN("not acquired\n");
763 return DIERR_NOTACQUIRED;
766 /* update joystick state */
767 joy_polldev(This);
769 /* convert and copy data to user supplied buffer */
770 fill_DataFormat(ptr, &This->js, &This->base.data_format);
772 return DI_OK;
775 /******************************************************************************
776 * SetProperty : change input device properties
778 static HRESULT WINAPI JoystickAImpl_SetProperty(
779 LPDIRECTINPUTDEVICE8A iface,
780 REFGUID rguid,
781 LPCDIPROPHEADER ph)
783 JoystickImpl *This = (JoystickImpl *)iface;
784 int i;
786 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
788 if (ph == NULL) {
789 WARN("invalid parameter: ph == NULL\n");
790 return DIERR_INVALIDPARAM;
793 if (TRACE_ON(dinput))
794 _dump_DIPROPHEADER(ph);
796 if (!HIWORD(rguid)) {
797 switch (LOWORD(rguid)) {
798 case (DWORD)DIPROP_RANGE: {
799 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
800 if (ph->dwHow == DIPH_DEVICE) {
801 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
802 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
803 This->props[i].lMin = pr->lMin;
804 This->props[i].lMax = pr->lMax;
806 } else {
807 int obj = find_property(&This->base.data_format, ph);
809 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
810 if (obj >= 0) {
811 This->props[obj].lMin = pr->lMin;
812 This->props[obj].lMax = pr->lMax;
813 return DI_OK;
816 break;
818 case (DWORD)DIPROP_DEADZONE: {
819 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
820 if (ph->dwHow == DIPH_DEVICE) {
821 TRACE("deadzone(%d) all\n", pd->dwData);
822 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
823 This->props[i].lDeadZone = pd->dwData;
824 } else {
825 int obj = find_property(&This->base.data_format, ph);
827 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
828 if (obj >= 0) {
829 This->props[obj].lDeadZone = pd->dwData;
830 return DI_OK;
833 break;
835 case (DWORD)DIPROP_SATURATION: {
836 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
837 if (ph->dwHow == DIPH_DEVICE) {
838 TRACE("saturation(%d) all\n", pd->dwData);
839 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
840 This->props[i].lSaturation = pd->dwData;
841 } else {
842 int obj = find_property(&This->base.data_format, ph);
844 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
845 if (obj >= 0) {
846 This->props[obj].lSaturation = pd->dwData;
847 return DI_OK;
850 break;
852 default:
853 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
857 return DI_OK;
860 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
861 LPDIRECTINPUTDEVICE8A iface,
862 LPDIDEVCAPS lpDIDevCaps)
864 JoystickImpl *This = (JoystickImpl *)iface;
865 int size;
867 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
869 if (lpDIDevCaps == NULL) {
870 WARN("invalid pointer\n");
871 return E_POINTER;
874 size = lpDIDevCaps->dwSize;
876 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
877 WARN("invalid parameter\n");
878 return DIERR_INVALIDPARAM;
881 CopyMemory(lpDIDevCaps, &This->devcaps, size);
882 lpDIDevCaps->dwSize = size;
884 if (TRACE_ON(dinput))
885 _dump_DIDEVCAPS(lpDIDevCaps);
887 return DI_OK;
890 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
892 JoystickImpl *This = (JoystickImpl *)iface;
894 TRACE("(%p)\n",This);
896 if (!This->base.acquired) {
897 WARN("not acquired\n");
898 return DIERR_NOTACQUIRED;
901 joy_polldev(This);
902 return DI_OK;
905 /******************************************************************************
906 * GetProperty : get input device properties
908 static HRESULT WINAPI JoystickAImpl_GetProperty(
909 LPDIRECTINPUTDEVICE8A iface,
910 REFGUID rguid,
911 LPDIPROPHEADER pdiph)
913 JoystickImpl *This = (JoystickImpl *)iface;
915 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
917 if (TRACE_ON(dinput))
918 _dump_DIPROPHEADER(pdiph);
920 if (!HIWORD(rguid)) {
921 switch (LOWORD(rguid)) {
922 case (DWORD) DIPROP_RANGE: {
923 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
924 int obj = find_property(&This->base.data_format, pdiph);
926 /* The app is querying the current range of the axis
927 * return the lMin and lMax values */
928 if (obj >= 0) {
929 pr->lMin = This->props[obj].lMin;
930 pr->lMax = This->props[obj].lMax;
931 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
932 return DI_OK;
934 break;
936 case (DWORD) DIPROP_DEADZONE: {
937 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
938 int obj = find_property(&This->base.data_format, pdiph);
940 if (obj >= 0) {
941 pd->dwData = This->props[obj].lDeadZone;
942 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
943 return DI_OK;
945 break;
947 case (DWORD) DIPROP_SATURATION: {
948 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
949 int obj = find_property(&This->base.data_format, pdiph);
951 if (obj >= 0) {
952 pd->dwData = This->props[obj].lSaturation;
953 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
954 return DI_OK;
956 break;
958 default:
959 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
963 return DI_OK;
966 /******************************************************************************
967 * GetObjectInfo : get object info
969 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
970 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
972 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
973 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
974 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
975 HRESULT res;
977 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
978 if (res != DI_OK) return res;
980 if (pdidoi->dwType & DIDFT_AXIS)
981 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
982 else if (pdidoi->dwType & DIDFT_POV)
983 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
984 else if (pdidoi->dwType & DIDFT_BUTTON)
985 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
987 _dump_OBJECTINSTANCEW(pdidoi);
988 return res;
991 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
992 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
994 HRESULT res;
995 DIDEVICEOBJECTINSTANCEW didoiW;
996 DWORD dwSize = pdidoi->dwSize;
998 didoiW.dwSize = sizeof(didoiW);
999 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1000 if (res != DI_OK) return res;
1002 memset(pdidoi, 0, pdidoi->dwSize);
1003 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1004 pdidoi->dwSize = dwSize;
1005 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1006 sizeof(pdidoi->tszName), NULL, NULL);
1008 return res;
1011 /******************************************************************************
1012 * GetDeviceInfo : get information about a device's identity
1014 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1015 LPDIRECTINPUTDEVICE8A iface,
1016 LPDIDEVICEINSTANCEA pdidi)
1018 JoystickImpl *This = (JoystickImpl *)iface;
1020 TRACE("(%p,%p)\n", iface, pdidi);
1022 if (pdidi == NULL) {
1023 WARN("invalid pointer\n");
1024 return E_POINTER;
1027 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1028 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1029 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1030 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1031 sizeof(DIDEVICEINSTANCEA));
1032 return DIERR_INVALIDPARAM;
1035 /* Return joystick */
1036 pdidi->guidInstance = GUID_Joystick;
1037 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1038 /* we only support traditional joysticks for now */
1039 pdidi->dwDevType = This->devcaps.dwDevType;
1040 strcpy(pdidi->tszInstanceName, "Joystick");
1041 strcpy(pdidi->tszProductName, This->name);
1042 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1043 pdidi->guidFFDriver = GUID_NULL;
1044 pdidi->wUsagePage = 0;
1045 pdidi->wUsage = 0;
1048 return DI_OK;
1051 /******************************************************************************
1052 * GetDeviceInfo : get information about a device's identity
1054 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1055 LPDIRECTINPUTDEVICE8W iface,
1056 LPDIDEVICEINSTANCEW pdidi)
1058 JoystickImpl *This = (JoystickImpl *)iface;
1060 TRACE("(%p,%p)\n", iface, pdidi);
1062 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1063 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1064 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1065 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1066 sizeof(DIDEVICEINSTANCEW));
1067 return DIERR_INVALIDPARAM;
1070 /* Return joystick */
1071 pdidi->guidInstance = GUID_Joystick;
1072 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1073 /* we only support traditional joysticks for now */
1074 pdidi->dwDevType = This->devcaps.dwDevType;
1075 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1076 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1077 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1078 pdidi->guidFFDriver = GUID_NULL;
1079 pdidi->wUsagePage = 0;
1080 pdidi->wUsage = 0;
1083 return DI_OK;
1086 static const IDirectInputDevice8AVtbl JoystickAvt =
1088 IDirectInputDevice2AImpl_QueryInterface,
1089 IDirectInputDevice2AImpl_AddRef,
1090 IDirectInputDevice2AImpl_Release,
1091 JoystickAImpl_GetCapabilities,
1092 IDirectInputDevice2AImpl_EnumObjects,
1093 JoystickAImpl_GetProperty,
1094 JoystickAImpl_SetProperty,
1095 JoystickAImpl_Acquire,
1096 JoystickAImpl_Unacquire,
1097 JoystickAImpl_GetDeviceState,
1098 IDirectInputDevice2AImpl_GetDeviceData,
1099 IDirectInputDevice2AImpl_SetDataFormat,
1100 IDirectInputDevice2AImpl_SetEventNotification,
1101 IDirectInputDevice2AImpl_SetCooperativeLevel,
1102 JoystickAImpl_GetObjectInfo,
1103 JoystickAImpl_GetDeviceInfo,
1104 IDirectInputDevice2AImpl_RunControlPanel,
1105 IDirectInputDevice2AImpl_Initialize,
1106 IDirectInputDevice2AImpl_CreateEffect,
1107 IDirectInputDevice2AImpl_EnumEffects,
1108 IDirectInputDevice2AImpl_GetEffectInfo,
1109 IDirectInputDevice2AImpl_GetForceFeedbackState,
1110 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1111 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1112 IDirectInputDevice2AImpl_Escape,
1113 JoystickAImpl_Poll,
1114 IDirectInputDevice2AImpl_SendDeviceData,
1115 IDirectInputDevice7AImpl_EnumEffectsInFile,
1116 IDirectInputDevice7AImpl_WriteEffectToFile,
1117 IDirectInputDevice8AImpl_BuildActionMap,
1118 IDirectInputDevice8AImpl_SetActionMap,
1119 IDirectInputDevice8AImpl_GetImageInfo
1122 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1123 # define XCAST(fun) (typeof(JoystickWvt.fun))
1124 #else
1125 # define XCAST(fun) (void*)
1126 #endif
1128 static const IDirectInputDevice8WVtbl JoystickWvt =
1130 IDirectInputDevice2WImpl_QueryInterface,
1131 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1132 XCAST(Release)IDirectInputDevice2AImpl_Release,
1133 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1134 IDirectInputDevice2WImpl_EnumObjects,
1135 XCAST(GetProperty)JoystickAImpl_GetProperty,
1136 XCAST(SetProperty)JoystickAImpl_SetProperty,
1137 XCAST(Acquire)JoystickAImpl_Acquire,
1138 XCAST(Unacquire)JoystickAImpl_Unacquire,
1139 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1140 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1141 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1142 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1143 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1144 IDirectInputDevice2WImpl_GetObjectInfo,
1145 JoystickWImpl_GetDeviceInfo,
1146 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1147 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1148 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1149 IDirectInputDevice2WImpl_EnumEffects,
1150 IDirectInputDevice2WImpl_GetEffectInfo,
1151 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1152 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1153 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1154 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1155 XCAST(Poll)JoystickAImpl_Poll,
1156 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1157 IDirectInputDevice7WImpl_EnumEffectsInFile,
1158 IDirectInputDevice7WImpl_WriteEffectToFile,
1159 IDirectInputDevice8WImpl_BuildActionMap,
1160 IDirectInputDevice8WImpl_SetActionMap,
1161 IDirectInputDevice8WImpl_GetImageInfo
1163 #undef XCAST
1165 #else /* HAVE_LINUX_22_JOYSTICK_API */
1167 const struct dinput_device joystick_linux_device = {
1168 "Wine Linux joystick driver",
1169 NULL,
1170 NULL,
1171 NULL,
1172 NULL
1175 #endif /* HAVE_LINUX_22_JOYSTICK_API */