push 4764fdcba48f6a6df3263056e605233f2bb574ff
[wine/hacks.git] / dlls / dinput / joystick_linux.c
blob4b1165f4c3503550c63a62ffbcd2503283f0d176
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 #include <errno.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #ifdef HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 #endif
42 #include <sys/fcntl.h>
43 #ifdef HAVE_SYS_IOCTL_H
44 # include <sys/ioctl.h>
45 #endif
46 #include <errno.h>
47 #ifdef HAVE_SYS_ERRNO_H
48 # include <sys/errno.h>
49 #endif
50 #ifdef HAVE_LINUX_IOCTL_H
51 # include <linux/ioctl.h>
52 #endif
53 #ifdef HAVE_LINUX_JOYSTICK_H
54 # include <linux/joystick.h>
55 # undef SW_MAX
56 #endif
57 #ifdef HAVE_SYS_POLL_H
58 # include <sys/poll.h>
59 #endif
61 #include "wine/debug.h"
62 #include "wine/unicode.h"
63 #include "windef.h"
64 #include "winbase.h"
65 #include "winerror.h"
66 #include "winreg.h"
67 #include "dinput.h"
69 #include "dinput_private.h"
70 #include "device_private.h"
72 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
74 #ifdef HAVE_LINUX_22_JOYSTICK_API
76 #define JOYDEV_NEW "/dev/input/js"
77 #define JOYDEV_OLD "/dev/js"
79 typedef struct JoystickImpl JoystickImpl;
80 static const IDirectInputDevice8AVtbl JoystickAvt;
81 static const IDirectInputDevice8WVtbl JoystickWvt;
82 struct JoystickImpl
84 struct IDirectInputDevice2AImpl base;
86 char dev[32];
88 /* joystick private */
89 int joyfd;
90 DIJOYSTATE2 js; /* wine data */
91 ObjProps *props;
92 char *name;
93 DIDEVCAPS devcaps;
94 LONG deadzone;
95 int *axis_map;
96 int axes;
97 int buttons;
98 POINTL povs[4];
101 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
102 0x9e573ed9,
103 0x7734,
104 0x11d2,
105 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
108 static void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
110 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
111 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
112 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
113 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
114 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
115 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
116 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
117 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
118 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
119 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
120 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
121 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
122 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
123 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
124 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
125 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
126 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
127 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
131 #define MAX_JOYSTICKS 64
132 static INT joystick_devices_count = -1;
133 static LPSTR joystick_devices[MAX_JOYSTICKS];
135 static INT find_joystick_devices(void)
137 INT i;
139 if (joystick_devices_count != -1) return joystick_devices_count;
141 for (i = 0; i < MAX_JOYSTICKS; i++)
143 CHAR device_name[MAX_PATH], *str;
144 INT len;
145 int fd;
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;
162 #undef MAX_JOYSTICKS
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 * Get a config key from either the app-specific or the default config
269 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
270 char *buffer, DWORD size )
272 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size ))
273 return 0;
275 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size ))
276 return 0;
278 return ERROR_FILE_NOT_FOUND;
282 * Setup the dinput options.
285 static HRESULT setup_dinput_options(JoystickImpl * device)
287 char buffer[MAX_PATH+16];
288 HKEY hkey, appkey = 0;
289 DWORD len;
291 buffer[MAX_PATH]='\0';
293 /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
294 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", &hkey)) hkey = 0;
296 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
297 if (len && len < MAX_PATH) {
298 HKEY tmpkey;
299 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
300 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
302 char *p, *appname = buffer;
303 if ((p = strrchr( appname, '/' ))) appname = p + 1;
304 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
305 strcat( appname, "\\DirectInput" );
306 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
307 RegCloseKey( tmpkey );
311 /* get options */
313 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
314 device->deadzone = atoi(buffer);
315 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
318 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
319 int tokens = 0;
320 int axis = 0;
321 int pov = 0;
322 const char *delim = ",";
323 char * ptr;
324 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
326 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
327 if (device->axis_map == 0)
328 return DIERR_OUTOFMEMORY;
330 if ((ptr = strtok(buffer, delim)) != NULL) {
331 do {
332 if (strcmp(ptr, "X") == 0) {
333 device->axis_map[tokens] = 0;
334 axis++;
335 } else if (strcmp(ptr, "Y") == 0) {
336 device->axis_map[tokens] = 1;
337 axis++;
338 } else if (strcmp(ptr, "Z") == 0) {
339 device->axis_map[tokens] = 2;
340 axis++;
341 } else if (strcmp(ptr, "Rx") == 0) {
342 device->axis_map[tokens] = 3;
343 axis++;
344 } else if (strcmp(ptr, "Ry") == 0) {
345 device->axis_map[tokens] = 4;
346 axis++;
347 } else if (strcmp(ptr, "Rz") == 0) {
348 device->axis_map[tokens] = 5;
349 axis++;
350 } else if (strcmp(ptr, "Slider1") == 0) {
351 device->axis_map[tokens] = 6;
352 axis++;
353 } else if (strcmp(ptr, "Slider2") == 0) {
354 device->axis_map[tokens] = 7;
355 axis++;
356 } else if (strcmp(ptr, "POV1") == 0) {
357 device->axis_map[tokens++] = 8;
358 device->axis_map[tokens] = 8;
359 pov++;
360 } else if (strcmp(ptr, "POV2") == 0) {
361 device->axis_map[tokens++] = 9;
362 device->axis_map[tokens] = 9;
363 pov++;
364 } else if (strcmp(ptr, "POV3") == 0) {
365 device->axis_map[tokens++] = 10;
366 device->axis_map[tokens] = 10;
367 pov++;
368 } else if (strcmp(ptr, "POV4") == 0) {
369 device->axis_map[tokens++] = 11;
370 device->axis_map[tokens] = 11;
371 pov++;
372 } else {
373 ERR("invalid joystick axis type: %s\n", ptr);
374 device->axis_map[tokens] = tokens;
375 axis++;
378 tokens++;
379 } while ((ptr = strtok(NULL, delim)) != NULL);
381 if (tokens != device->devcaps.dwAxes) {
382 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
383 while (tokens < device->axes) {
384 device->axis_map[tokens] = tokens;
385 tokens++;
390 device->devcaps.dwAxes = axis;
391 device->devcaps.dwPOVs = pov;
394 if (appkey)
395 RegCloseKey( appkey );
397 if (hkey)
398 RegCloseKey( hkey );
400 return DI_OK;
403 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput, LPDIRECTINPUTDEVICEA* pdev)
405 DWORD i;
406 JoystickImpl* newDevice;
407 char name[MAX_PATH];
408 HRESULT hr;
409 LPDIDATAFORMAT df = NULL;
410 int idx = 0;
412 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
413 if (newDevice == 0) {
414 WARN("out of memory\n");
415 *pdev = 0;
416 return DIERR_OUTOFMEMORY;
419 if (!lstrcpynA(newDevice->dev, joystick_devices[rguid->Data3], sizeof(newDevice->dev)) ||
420 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
422 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
423 HeapFree(GetProcessHeap(), 0, newDevice);
424 return DIERR_DEVICENOTREG;
427 /* get the device name */
428 #if defined(JSIOCGNAME)
429 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
430 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
431 strcpy(name, "Wine Joystick");
433 #else
434 strcpy(name, "Wine Joystick");
435 #endif
437 /* copy the device name */
438 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
439 strcpy(newDevice->name, name);
441 #ifdef JSIOCGAXES
442 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
443 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
444 newDevice->axes = 2;
446 #endif
447 #ifdef JSIOCGBUTTONS
448 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
449 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
450 newDevice->buttons = 2;
452 #endif
454 newDevice->base.lpVtbl = jvt;
455 newDevice->base.ref = 1;
456 newDevice->base.dinput = dinput;
457 CopyMemory(&newDevice->base.guid, rguid, sizeof(*rguid));
458 InitializeCriticalSection(&newDevice->base.crit);
459 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
461 /* setup_dinput_options may change these */
462 newDevice->deadzone = 0;
463 newDevice->devcaps.dwButtons = newDevice->buttons;
464 newDevice->devcaps.dwAxes = newDevice->axes;
465 newDevice->devcaps.dwPOVs = 0;
467 /* do any user specified configuration */
468 hr = setup_dinput_options(newDevice);
469 if (hr != DI_OK)
470 goto FAILED1;
472 if (newDevice->axis_map == 0) {
473 newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
474 if (newDevice->axis_map == 0)
475 goto FAILED;
477 for (i = 0; i < newDevice->axes; i++)
478 newDevice->axis_map[i] = i;
481 /* Create copy of default data format */
482 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
483 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
485 /* Axes include POVs */
486 df->dwNumObjs = newDevice->axes + newDevice->buttons;
487 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
489 for (i = 0; i < newDevice->axes; i++)
491 int wine_obj = newDevice->axis_map[i];
493 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
494 if (wine_obj < 8)
495 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
496 else
498 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
499 i++; /* POV takes 2 axes */
502 for (i = 0; i < newDevice->buttons; i++)
504 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
505 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
507 newDevice->base.data_format.wine_df = df;
509 /* create default properties */
510 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
511 if (newDevice->props == 0)
512 goto FAILED;
514 /* initialize default properties */
515 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
516 newDevice->props[i].lDevMin = -32767;
517 newDevice->props[i].lDevMax = +32767;
518 newDevice->props[i].lMin = 0;
519 newDevice->props[i].lMax = 0xffff;
520 newDevice->props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
521 newDevice->props[i].lSaturation = 0;
524 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
526 newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
527 newDevice->devcaps.dwFlags = DIDC_ATTACHED;
528 if (newDevice->base.dinput->dwVersion >= 0x0800)
529 newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
530 else
531 newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
532 newDevice->devcaps.dwFFSamplePeriod = 0;
533 newDevice->devcaps.dwFFMinTimeResolution = 0;
534 newDevice->devcaps.dwFirmwareRevision = 0;
535 newDevice->devcaps.dwHardwareRevision = 0;
536 newDevice->devcaps.dwFFDriverVersion = 0;
538 if (TRACE_ON(dinput)) {
539 _dump_DIDATAFORMAT(newDevice->base.data_format.wine_df);
540 for (i = 0; i < (newDevice->axes); i++)
541 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
542 _dump_DIDEVCAPS(&newDevice->devcaps);
545 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
547 return DI_OK;
549 FAILED:
550 hr = DIERR_OUTOFMEMORY;
551 FAILED1:
552 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
553 HeapFree(GetProcessHeap(), 0, df);
554 release_DataFormat(&newDevice->base.data_format);
555 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
556 HeapFree(GetProcessHeap(),0,newDevice->name);
557 HeapFree(GetProcessHeap(),0,newDevice->props);
558 HeapFree(GetProcessHeap(),0,newDevice);
559 *pdev = 0;
561 return hr;
564 static BOOL IsJoystickGUID(REFGUID guid)
566 GUID wine_joystick = DInput_Wine_Joystick_GUID;
567 GUID dev_guid = *guid;
569 wine_joystick.Data3 = 0;
570 dev_guid.Data3 = 0;
572 return IsEqualGUID(&wine_joystick, &dev_guid);
575 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
577 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
578 (IsJoystickGUID(rguid))) {
579 if ((riid == NULL) ||
580 IsEqualGUID(&IID_IDirectInputDeviceA,riid) ||
581 IsEqualGUID(&IID_IDirectInputDevice2A,riid) ||
582 IsEqualGUID(&IID_IDirectInputDevice7A,riid) ||
583 IsEqualGUID(&IID_IDirectInputDevice8A,riid)) {
584 return alloc_device(rguid, &JoystickAvt, dinput, pdev);
585 } else {
586 WARN("no interface\n");
587 *pdev = 0;
588 return DIERR_NOINTERFACE;
592 WARN("invalid device GUID\n");
593 *pdev = 0;
594 return DIERR_DEVICENOTREG;
597 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
599 if ((IsEqualGUID(&GUID_Joystick,rguid)) ||
600 (IsJoystickGUID(rguid))) {
601 if ((riid == NULL) ||
602 IsEqualGUID(&IID_IDirectInputDeviceW,riid) ||
603 IsEqualGUID(&IID_IDirectInputDevice2W,riid) ||
604 IsEqualGUID(&IID_IDirectInputDevice7W,riid) ||
605 IsEqualGUID(&IID_IDirectInputDevice8W,riid)) {
606 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev);
607 } else {
608 WARN("no interface\n");
609 *pdev = 0;
610 return DIERR_NOINTERFACE;
614 WARN("invalid device GUID\n");
615 *pdev = 0;
616 return DIERR_DEVICENOTREG;
619 const struct dinput_device joystick_linux_device = {
620 "Wine Linux joystick driver",
621 joydev_enum_deviceA,
622 joydev_enum_deviceW,
623 joydev_create_deviceA,
624 joydev_create_deviceW
627 /******************************************************************************
628 * Acquire : gets exclusive control of the joystick
630 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
632 JoystickImpl *This = (JoystickImpl *)iface;
634 TRACE("(%p)\n",This);
636 if (This->base.acquired) {
637 WARN("already acquired\n");
638 return S_FALSE;
641 /* open the joystick device */
642 if (This->joyfd==-1) {
643 TRACE("opening joystick device %s\n", This->dev);
645 This->joyfd=open(This->dev,O_RDONLY);
646 if (This->joyfd==-1) {
647 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
648 return DIERR_NOTFOUND;
652 This->base.acquired = 1;
654 return DI_OK;
657 /******************************************************************************
658 * Unacquire : frees the joystick
660 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
662 JoystickImpl *This = (JoystickImpl *)iface;
663 HRESULT res;
665 TRACE("(%p)\n",This);
667 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
669 if (This->joyfd!=-1) {
670 TRACE("closing joystick device\n");
671 close(This->joyfd);
672 This->joyfd = -1;
673 return DI_OK;
676 return DI_NOEFFECT;
679 static void joy_polldev(JoystickImpl *This) {
680 struct pollfd plfd;
681 struct js_event jse;
682 TRACE("(%p)\n", This);
684 if (This->joyfd==-1) {
685 WARN("no device\n");
686 return;
688 while (1)
690 LONG value;
691 int inst_id = -1;
693 plfd.fd = This->joyfd;
694 plfd.events = POLLIN;
695 if (poll(&plfd,1,0) != 1)
696 return;
697 /* we have one event, so we can read */
698 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
699 return;
701 TRACE("js_event: type 0x%x, number %d, value %d\n",
702 jse.type,jse.number,jse.value);
703 if (jse.type & JS_EVENT_BUTTON)
705 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
706 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
708 else if (jse.type & JS_EVENT_AXIS)
710 int number = This->axis_map[jse.number]; /* wine format object index */
712 if (number < 0) return;
713 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
714 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
716 TRACE("changing axis %d => %d\n", jse.number, number);
717 switch (number)
719 case 0: This->js.lX = value; break;
720 case 1: This->js.lY = value; break;
721 case 2: This->js.lZ = value; break;
722 case 3: This->js.lRx = value; break;
723 case 4: This->js.lRy = value; break;
724 case 5: This->js.lRz = value; break;
725 case 6: This->js.rglSlider[0] = value; break;
726 case 7: This->js.rglSlider[1] = value; break;
727 case 8: case 9: case 10: case 11:
729 int idx = number - 8;
731 if (jse.number % 2)
732 This->povs[idx].y = jse.value;
733 else
734 This->povs[idx].x = jse.value;
736 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
737 break;
739 default:
740 WARN("axis %d not supported\n", number);
743 if (inst_id >= 0)
744 queue_event((LPDIRECTINPUTDEVICE8A)This,
745 id_to_offset(&This->base.data_format, inst_id),
746 value, jse.time, This->base.dinput->evsequence++);
750 /******************************************************************************
751 * GetDeviceState : returns the "state" of the joystick.
754 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
755 LPDIRECTINPUTDEVICE8A iface,
756 DWORD len,
757 LPVOID ptr)
759 JoystickImpl *This = (JoystickImpl *)iface;
761 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
763 if (!This->base.acquired) {
764 WARN("not acquired\n");
765 return DIERR_NOTACQUIRED;
768 /* update joystick state */
769 joy_polldev(This);
771 /* convert and copy data to user supplied buffer */
772 fill_DataFormat(ptr, &This->js, &This->base.data_format);
774 return DI_OK;
777 /******************************************************************************
778 * SetProperty : change input device properties
780 static HRESULT WINAPI JoystickAImpl_SetProperty(
781 LPDIRECTINPUTDEVICE8A iface,
782 REFGUID rguid,
783 LPCDIPROPHEADER ph)
785 JoystickImpl *This = (JoystickImpl *)iface;
786 int i;
788 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
790 if (ph == NULL) {
791 WARN("invalid parameter: ph == NULL\n");
792 return DIERR_INVALIDPARAM;
795 if (TRACE_ON(dinput))
796 _dump_DIPROPHEADER(ph);
798 if (!HIWORD(rguid)) {
799 switch (LOWORD(rguid)) {
800 case (DWORD)DIPROP_RANGE: {
801 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
802 if (ph->dwHow == DIPH_DEVICE) {
803 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
804 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
805 This->props[i].lMin = pr->lMin;
806 This->props[i].lMax = pr->lMax;
808 } else {
809 int obj = find_property(&This->base.data_format, ph);
811 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
812 if (obj >= 0) {
813 This->props[obj].lMin = pr->lMin;
814 This->props[obj].lMax = pr->lMax;
815 return DI_OK;
818 break;
820 case (DWORD)DIPROP_DEADZONE: {
821 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
822 if (ph->dwHow == DIPH_DEVICE) {
823 TRACE("deadzone(%d) all\n", pd->dwData);
824 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
825 This->props[i].lDeadZone = pd->dwData;
826 } else {
827 int obj = find_property(&This->base.data_format, ph);
829 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
830 if (obj >= 0) {
831 This->props[obj].lDeadZone = pd->dwData;
832 return DI_OK;
835 break;
837 case (DWORD)DIPROP_SATURATION: {
838 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
839 if (ph->dwHow == DIPH_DEVICE) {
840 TRACE("saturation(%d) all\n", pd->dwData);
841 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
842 This->props[i].lSaturation = pd->dwData;
843 } else {
844 int obj = find_property(&This->base.data_format, ph);
846 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
847 if (obj >= 0) {
848 This->props[obj].lSaturation = pd->dwData;
849 return DI_OK;
852 break;
854 default:
855 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
859 return DI_OK;
862 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
863 LPDIRECTINPUTDEVICE8A iface,
864 LPDIDEVCAPS lpDIDevCaps)
866 JoystickImpl *This = (JoystickImpl *)iface;
867 int size;
869 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
871 if (lpDIDevCaps == NULL) {
872 WARN("invalid pointer\n");
873 return E_POINTER;
876 size = lpDIDevCaps->dwSize;
878 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
879 WARN("invalid parameter\n");
880 return DIERR_INVALIDPARAM;
883 CopyMemory(lpDIDevCaps, &This->devcaps, size);
884 lpDIDevCaps->dwSize = size;
886 if (TRACE_ON(dinput))
887 _dump_DIDEVCAPS(lpDIDevCaps);
889 return DI_OK;
892 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
894 JoystickImpl *This = (JoystickImpl *)iface;
896 TRACE("(%p)\n",This);
898 if (!This->base.acquired) {
899 WARN("not acquired\n");
900 return DIERR_NOTACQUIRED;
903 joy_polldev(This);
904 return DI_OK;
907 /******************************************************************************
908 * GetProperty : get input device properties
910 static HRESULT WINAPI JoystickAImpl_GetProperty(
911 LPDIRECTINPUTDEVICE8A iface,
912 REFGUID rguid,
913 LPDIPROPHEADER pdiph)
915 JoystickImpl *This = (JoystickImpl *)iface;
917 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
919 if (TRACE_ON(dinput))
920 _dump_DIPROPHEADER(pdiph);
922 if (!HIWORD(rguid)) {
923 switch (LOWORD(rguid)) {
924 case (DWORD) DIPROP_RANGE: {
925 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
926 int obj = find_property(&This->base.data_format, pdiph);
928 /* The app is querying the current range of the axis
929 * return the lMin and lMax values */
930 if (obj >= 0) {
931 pr->lMin = This->props[obj].lMin;
932 pr->lMax = This->props[obj].lMax;
933 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
934 return DI_OK;
936 break;
938 case (DWORD) DIPROP_DEADZONE: {
939 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
940 int obj = find_property(&This->base.data_format, pdiph);
942 if (obj >= 0) {
943 pd->dwData = This->props[obj].lDeadZone;
944 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
945 return DI_OK;
947 break;
949 case (DWORD) DIPROP_SATURATION: {
950 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
951 int obj = find_property(&This->base.data_format, pdiph);
953 if (obj >= 0) {
954 pd->dwData = This->props[obj].lSaturation;
955 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
956 return DI_OK;
958 break;
960 default:
961 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
965 return DI_OK;
968 /******************************************************************************
969 * GetObjectInfo : get object info
971 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
972 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
974 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
975 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
976 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
977 HRESULT res;
979 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
980 if (res != DI_OK) return res;
982 if (pdidoi->dwType & DIDFT_AXIS)
983 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
984 else if (pdidoi->dwType & DIDFT_POV)
985 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
986 else if (pdidoi->dwType & DIDFT_BUTTON)
987 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
989 _dump_OBJECTINSTANCEW(pdidoi);
990 return res;
993 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
994 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
996 HRESULT res;
997 DIDEVICEOBJECTINSTANCEW didoiW;
998 DWORD dwSize = pdidoi->dwSize;
1000 didoiW.dwSize = sizeof(didoiW);
1001 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1002 if (res != DI_OK) return res;
1004 memset(pdidoi, 0, pdidoi->dwSize);
1005 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1006 pdidoi->dwSize = dwSize;
1007 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1008 sizeof(pdidoi->tszName), NULL, NULL);
1010 return res;
1013 /******************************************************************************
1014 * GetDeviceInfo : get information about a device's identity
1016 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1017 LPDIRECTINPUTDEVICE8A iface,
1018 LPDIDEVICEINSTANCEA pdidi)
1020 JoystickImpl *This = (JoystickImpl *)iface;
1022 TRACE("(%p,%p)\n", iface, pdidi);
1024 if (pdidi == NULL) {
1025 WARN("invalid pointer\n");
1026 return E_POINTER;
1029 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1030 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1031 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1032 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1033 sizeof(DIDEVICEINSTANCEA));
1034 return DIERR_INVALIDPARAM;
1037 /* Return joystick */
1038 pdidi->guidInstance = GUID_Joystick;
1039 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1040 /* we only support traditional joysticks for now */
1041 pdidi->dwDevType = This->devcaps.dwDevType;
1042 strcpy(pdidi->tszInstanceName, "Joystick");
1043 strcpy(pdidi->tszProductName, This->name);
1044 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1045 pdidi->guidFFDriver = GUID_NULL;
1046 pdidi->wUsagePage = 0;
1047 pdidi->wUsage = 0;
1050 return DI_OK;
1053 /******************************************************************************
1054 * GetDeviceInfo : get information about a device's identity
1056 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1057 LPDIRECTINPUTDEVICE8W iface,
1058 LPDIDEVICEINSTANCEW pdidi)
1060 JoystickImpl *This = (JoystickImpl *)iface;
1062 TRACE("(%p,%p)\n", iface, pdidi);
1064 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1065 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1066 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1067 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1068 sizeof(DIDEVICEINSTANCEW));
1069 return DIERR_INVALIDPARAM;
1072 /* Return joystick */
1073 pdidi->guidInstance = GUID_Joystick;
1074 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1075 /* we only support traditional joysticks for now */
1076 pdidi->dwDevType = This->devcaps.dwDevType;
1077 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1078 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1079 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1080 pdidi->guidFFDriver = GUID_NULL;
1081 pdidi->wUsagePage = 0;
1082 pdidi->wUsage = 0;
1085 return DI_OK;
1088 static const IDirectInputDevice8AVtbl JoystickAvt =
1090 IDirectInputDevice2AImpl_QueryInterface,
1091 IDirectInputDevice2AImpl_AddRef,
1092 IDirectInputDevice2AImpl_Release,
1093 JoystickAImpl_GetCapabilities,
1094 IDirectInputDevice2AImpl_EnumObjects,
1095 JoystickAImpl_GetProperty,
1096 JoystickAImpl_SetProperty,
1097 JoystickAImpl_Acquire,
1098 JoystickAImpl_Unacquire,
1099 JoystickAImpl_GetDeviceState,
1100 IDirectInputDevice2AImpl_GetDeviceData,
1101 IDirectInputDevice2AImpl_SetDataFormat,
1102 IDirectInputDevice2AImpl_SetEventNotification,
1103 IDirectInputDevice2AImpl_SetCooperativeLevel,
1104 JoystickAImpl_GetObjectInfo,
1105 JoystickAImpl_GetDeviceInfo,
1106 IDirectInputDevice2AImpl_RunControlPanel,
1107 IDirectInputDevice2AImpl_Initialize,
1108 IDirectInputDevice2AImpl_CreateEffect,
1109 IDirectInputDevice2AImpl_EnumEffects,
1110 IDirectInputDevice2AImpl_GetEffectInfo,
1111 IDirectInputDevice2AImpl_GetForceFeedbackState,
1112 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1113 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1114 IDirectInputDevice2AImpl_Escape,
1115 JoystickAImpl_Poll,
1116 IDirectInputDevice2AImpl_SendDeviceData,
1117 IDirectInputDevice7AImpl_EnumEffectsInFile,
1118 IDirectInputDevice7AImpl_WriteEffectToFile,
1119 IDirectInputDevice8AImpl_BuildActionMap,
1120 IDirectInputDevice8AImpl_SetActionMap,
1121 IDirectInputDevice8AImpl_GetImageInfo
1124 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1125 # define XCAST(fun) (typeof(JoystickWvt.fun))
1126 #else
1127 # define XCAST(fun) (void*)
1128 #endif
1130 static const IDirectInputDevice8WVtbl JoystickWvt =
1132 IDirectInputDevice2WImpl_QueryInterface,
1133 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1134 XCAST(Release)IDirectInputDevice2AImpl_Release,
1135 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1136 IDirectInputDevice2WImpl_EnumObjects,
1137 XCAST(GetProperty)JoystickAImpl_GetProperty,
1138 XCAST(SetProperty)JoystickAImpl_SetProperty,
1139 XCAST(Acquire)JoystickAImpl_Acquire,
1140 XCAST(Unacquire)JoystickAImpl_Unacquire,
1141 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1142 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1143 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1144 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1145 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1146 IDirectInputDevice2WImpl_GetObjectInfo,
1147 JoystickWImpl_GetDeviceInfo,
1148 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1149 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1150 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1151 IDirectInputDevice2WImpl_EnumEffects,
1152 IDirectInputDevice2WImpl_GetEffectInfo,
1153 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1154 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1155 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1156 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1157 XCAST(Poll)JoystickAImpl_Poll,
1158 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1159 IDirectInputDevice7WImpl_EnumEffectsInFile,
1160 IDirectInputDevice7WImpl_WriteEffectToFile,
1161 IDirectInputDevice8WImpl_BuildActionMap,
1162 IDirectInputDevice8WImpl_SetActionMap,
1163 IDirectInputDevice8WImpl_GetImageInfo
1165 #undef XCAST
1167 #else /* HAVE_LINUX_22_JOYSTICK_API */
1169 const struct dinput_device joystick_linux_device = {
1170 "Wine Linux joystick driver",
1171 NULL,
1172 NULL,
1173 NULL,
1174 NULL
1177 #endif /* HAVE_LINUX_22_JOYSTICK_API */