d3d: Add a test for double surface locking.
[wine.git] / dlls / dinput / joystick_linux.c
blobe15e32e128b374e372b603de7ceaf7d97f134486
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 joystick_devices_count = 0;
142 for (i = 0; i < MAX_JOYSTICKS; i++)
144 CHAR device_name[MAX_PATH], *str;
145 INT len;
146 int fd;
148 len = sprintf(device_name, "%s%d", JOYDEV_NEW, i) + 1;
149 if ((fd = open(device_name, O_RDONLY)) < 0)
151 len = sprintf(device_name, "%s%d", JOYDEV_OLD, i) + 1;
152 if ((fd = open(device_name, O_RDONLY)) < 0) continue;
155 if (!(str = HeapAlloc(GetProcessHeap(), 0, len))) break;
156 memcpy(str, device_name, len);
158 joystick_devices[joystick_devices_count++] = str;
161 return joystick_devices_count;
164 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
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,
404 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
406 DWORD i;
407 JoystickImpl* newDevice;
408 char name[MAX_PATH];
409 HRESULT hr;
410 LPDIDATAFORMAT df = NULL;
411 int idx = 0;
413 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
414 if (newDevice == 0) {
415 WARN("out of memory\n");
416 *pdev = 0;
417 return DIERR_OUTOFMEMORY;
420 if (!lstrcpynA(newDevice->dev, joystick_devices[index], sizeof(newDevice->dev)) ||
421 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
423 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
424 HeapFree(GetProcessHeap(), 0, newDevice);
425 return DIERR_DEVICENOTREG;
428 /* get the device name */
429 #if defined(JSIOCGNAME)
430 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
431 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
432 strcpy(name, "Wine Joystick");
434 #else
435 strcpy(name, "Wine Joystick");
436 #endif
438 /* copy the device name */
439 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
440 strcpy(newDevice->name, name);
442 #ifdef JSIOCGAXES
443 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
444 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
445 newDevice->axes = 2;
447 #endif
448 #ifdef JSIOCGBUTTONS
449 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
450 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
451 newDevice->buttons = 2;
453 #endif
455 newDevice->base.lpVtbl = jvt;
456 newDevice->base.ref = 1;
457 newDevice->base.dinput = dinput;
458 CopyMemory(&newDevice->base.guid, rguid, sizeof(*rguid));
459 InitializeCriticalSection(&newDevice->base.crit);
460 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
462 /* setup_dinput_options may change these */
463 newDevice->deadzone = 0;
464 newDevice->devcaps.dwButtons = newDevice->buttons;
465 newDevice->devcaps.dwAxes = newDevice->axes;
466 newDevice->devcaps.dwPOVs = 0;
468 /* do any user specified configuration */
469 hr = setup_dinput_options(newDevice);
470 if (hr != DI_OK)
471 goto FAILED1;
473 if (newDevice->axis_map == 0) {
474 newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
475 if (newDevice->axis_map == 0)
476 goto FAILED;
478 for (i = 0; i < newDevice->axes; i++)
479 newDevice->axis_map[i] = i;
482 /* Create copy of default data format */
483 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
484 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
486 /* Axes include POVs */
487 df->dwNumObjs = newDevice->axes + newDevice->buttons;
488 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
490 for (i = 0; i < newDevice->axes; i++)
492 int wine_obj = newDevice->axis_map[i];
494 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
495 if (wine_obj < 8)
496 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
497 else
499 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
500 i++; /* POV takes 2 axes */
503 for (i = 0; i < newDevice->buttons; i++)
505 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
506 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
508 newDevice->base.data_format.wine_df = df;
510 /* create default properties */
511 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
512 if (newDevice->props == 0)
513 goto FAILED;
515 /* initialize default properties */
516 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
517 newDevice->props[i].lDevMin = -32767;
518 newDevice->props[i].lDevMax = +32767;
519 newDevice->props[i].lMin = 0;
520 newDevice->props[i].lMax = 0xffff;
521 newDevice->props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
522 newDevice->props[i].lSaturation = 0;
525 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
527 newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
528 newDevice->devcaps.dwFlags = DIDC_ATTACHED;
529 if (newDevice->base.dinput->dwVersion >= 0x0800)
530 newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
531 else
532 newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
533 newDevice->devcaps.dwFFSamplePeriod = 0;
534 newDevice->devcaps.dwFFMinTimeResolution = 0;
535 newDevice->devcaps.dwFirmwareRevision = 0;
536 newDevice->devcaps.dwHardwareRevision = 0;
537 newDevice->devcaps.dwFFDriverVersion = 0;
539 if (TRACE_ON(dinput)) {
540 _dump_DIDATAFORMAT(newDevice->base.data_format.wine_df);
541 for (i = 0; i < (newDevice->axes); i++)
542 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
543 _dump_DIDEVCAPS(&newDevice->devcaps);
546 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
548 return DI_OK;
550 FAILED:
551 hr = DIERR_OUTOFMEMORY;
552 FAILED1:
553 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
554 HeapFree(GetProcessHeap(), 0, df);
555 release_DataFormat(&newDevice->base.data_format);
556 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
557 HeapFree(GetProcessHeap(),0,newDevice->name);
558 HeapFree(GetProcessHeap(),0,newDevice->props);
559 HeapFree(GetProcessHeap(),0,newDevice);
560 *pdev = 0;
562 return hr;
565 /******************************************************************************
566 * get_joystick_index : Get the joystick index from a given GUID
568 static unsigned short get_joystick_index(REFGUID guid)
570 GUID wine_joystick = DInput_Wine_Joystick_GUID;
571 GUID dev_guid = *guid;
573 wine_joystick.Data3 = 0;
574 dev_guid.Data3 = 0;
576 /* for the standard joystick GUID use index 0 */
577 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
579 /* for the wine joystick GUIDs use the index stored in Data3 */
580 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
582 return MAX_JOYSTICKS;
585 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
587 unsigned short index;
589 find_joystick_devices();
590 *pdev = NULL;
592 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
593 joystick_devices_count && index < joystick_devices_count)
595 if ((riid == NULL) ||
596 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
597 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
598 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
599 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
601 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
604 WARN("no interface\n");
605 return DIERR_NOINTERFACE;
608 WARN("invalid device GUID\n");
609 return DIERR_DEVICENOTREG;
612 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
614 unsigned short index;
616 find_joystick_devices();
617 *pdev = NULL;
619 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
620 joystick_devices_count && index < joystick_devices_count)
622 if ((riid == NULL) ||
623 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
624 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
625 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
626 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
628 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
630 WARN("no interface\n");
631 return DIERR_NOINTERFACE;
634 WARN("invalid device GUID\n");
635 return DIERR_DEVICENOTREG;
638 #undef MAX_JOYSTICKS
640 const struct dinput_device joystick_linux_device = {
641 "Wine Linux joystick driver",
642 joydev_enum_deviceA,
643 joydev_enum_deviceW,
644 joydev_create_deviceA,
645 joydev_create_deviceW
648 /******************************************************************************
649 * Acquire : gets exclusive control of the joystick
651 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
653 JoystickImpl *This = (JoystickImpl *)iface;
655 TRACE("(%p)\n",This);
657 if (This->base.acquired) {
658 WARN("already acquired\n");
659 return S_FALSE;
662 /* open the joystick device */
663 if (This->joyfd==-1) {
664 TRACE("opening joystick device %s\n", This->dev);
666 This->joyfd=open(This->dev,O_RDONLY);
667 if (This->joyfd==-1) {
668 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
669 return DIERR_NOTFOUND;
673 This->base.acquired = 1;
675 return DI_OK;
678 /******************************************************************************
679 * Unacquire : frees the joystick
681 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
683 JoystickImpl *This = (JoystickImpl *)iface;
684 HRESULT res;
686 TRACE("(%p)\n",This);
688 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
690 if (This->joyfd!=-1) {
691 TRACE("closing joystick device\n");
692 close(This->joyfd);
693 This->joyfd = -1;
694 return DI_OK;
697 return DI_NOEFFECT;
700 static void joy_polldev(JoystickImpl *This) {
701 struct pollfd plfd;
702 struct js_event jse;
703 TRACE("(%p)\n", This);
705 if (This->joyfd==-1) {
706 WARN("no device\n");
707 return;
709 while (1)
711 LONG value;
712 int inst_id = -1;
714 plfd.fd = This->joyfd;
715 plfd.events = POLLIN;
716 if (poll(&plfd,1,0) != 1)
717 return;
718 /* we have one event, so we can read */
719 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
720 return;
722 TRACE("js_event: type 0x%x, number %d, value %d\n",
723 jse.type,jse.number,jse.value);
724 if (jse.type & JS_EVENT_BUTTON)
726 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
727 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
729 else if (jse.type & JS_EVENT_AXIS)
731 int number = This->axis_map[jse.number]; /* wine format object index */
733 if (number < 0) return;
734 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
735 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
737 TRACE("changing axis %d => %d\n", jse.number, number);
738 switch (number)
740 case 0: This->js.lX = value; break;
741 case 1: This->js.lY = value; break;
742 case 2: This->js.lZ = value; break;
743 case 3: This->js.lRx = value; break;
744 case 4: This->js.lRy = value; break;
745 case 5: This->js.lRz = value; break;
746 case 6: This->js.rglSlider[0] = value; break;
747 case 7: This->js.rglSlider[1] = value; break;
748 case 8: case 9: case 10: case 11:
750 int idx = number - 8;
752 if (jse.number % 2)
753 This->povs[idx].y = jse.value;
754 else
755 This->povs[idx].x = jse.value;
757 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
758 break;
760 default:
761 WARN("axis %d not supported\n", number);
764 if (inst_id >= 0)
765 queue_event((LPDIRECTINPUTDEVICE8A)This,
766 id_to_offset(&This->base.data_format, inst_id),
767 value, jse.time, This->base.dinput->evsequence++);
771 /******************************************************************************
772 * GetDeviceState : returns the "state" of the joystick.
775 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
776 LPDIRECTINPUTDEVICE8A iface,
777 DWORD len,
778 LPVOID ptr)
780 JoystickImpl *This = (JoystickImpl *)iface;
782 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
784 if (!This->base.acquired) {
785 WARN("not acquired\n");
786 return DIERR_NOTACQUIRED;
789 /* update joystick state */
790 joy_polldev(This);
792 /* convert and copy data to user supplied buffer */
793 fill_DataFormat(ptr, &This->js, &This->base.data_format);
795 return DI_OK;
798 /******************************************************************************
799 * SetProperty : change input device properties
801 static HRESULT WINAPI JoystickAImpl_SetProperty(
802 LPDIRECTINPUTDEVICE8A iface,
803 REFGUID rguid,
804 LPCDIPROPHEADER ph)
806 JoystickImpl *This = (JoystickImpl *)iface;
807 int i;
809 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
811 if (ph == NULL) {
812 WARN("invalid parameter: ph == NULL\n");
813 return DIERR_INVALIDPARAM;
816 if (TRACE_ON(dinput))
817 _dump_DIPROPHEADER(ph);
819 if (!HIWORD(rguid)) {
820 switch (LOWORD(rguid)) {
821 case (DWORD)DIPROP_RANGE: {
822 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
823 if (ph->dwHow == DIPH_DEVICE) {
824 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
825 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
826 This->props[i].lMin = pr->lMin;
827 This->props[i].lMax = pr->lMax;
829 } else {
830 int obj = find_property(&This->base.data_format, ph);
832 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
833 if (obj >= 0) {
834 This->props[obj].lMin = pr->lMin;
835 This->props[obj].lMax = pr->lMax;
836 return DI_OK;
839 break;
841 case (DWORD)DIPROP_DEADZONE: {
842 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
843 if (ph->dwHow == DIPH_DEVICE) {
844 TRACE("deadzone(%d) all\n", pd->dwData);
845 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
846 This->props[i].lDeadZone = pd->dwData;
847 } else {
848 int obj = find_property(&This->base.data_format, ph);
850 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
851 if (obj >= 0) {
852 This->props[obj].lDeadZone = pd->dwData;
853 return DI_OK;
856 break;
858 case (DWORD)DIPROP_SATURATION: {
859 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
860 if (ph->dwHow == DIPH_DEVICE) {
861 TRACE("saturation(%d) all\n", pd->dwData);
862 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
863 This->props[i].lSaturation = pd->dwData;
864 } else {
865 int obj = find_property(&This->base.data_format, ph);
867 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
868 if (obj >= 0) {
869 This->props[obj].lSaturation = pd->dwData;
870 return DI_OK;
873 break;
875 default:
876 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
880 return DI_OK;
883 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
884 LPDIRECTINPUTDEVICE8A iface,
885 LPDIDEVCAPS lpDIDevCaps)
887 JoystickImpl *This = (JoystickImpl *)iface;
888 int size;
890 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
892 if (lpDIDevCaps == NULL) {
893 WARN("invalid pointer\n");
894 return E_POINTER;
897 size = lpDIDevCaps->dwSize;
899 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
900 WARN("invalid parameter\n");
901 return DIERR_INVALIDPARAM;
904 CopyMemory(lpDIDevCaps, &This->devcaps, size);
905 lpDIDevCaps->dwSize = size;
907 if (TRACE_ON(dinput))
908 _dump_DIDEVCAPS(lpDIDevCaps);
910 return DI_OK;
913 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
915 JoystickImpl *This = (JoystickImpl *)iface;
917 TRACE("(%p)\n",This);
919 if (!This->base.acquired) {
920 WARN("not acquired\n");
921 return DIERR_NOTACQUIRED;
924 joy_polldev(This);
925 return DI_OK;
928 /******************************************************************************
929 * GetProperty : get input device properties
931 static HRESULT WINAPI JoystickAImpl_GetProperty(
932 LPDIRECTINPUTDEVICE8A iface,
933 REFGUID rguid,
934 LPDIPROPHEADER pdiph)
936 JoystickImpl *This = (JoystickImpl *)iface;
938 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
940 if (TRACE_ON(dinput))
941 _dump_DIPROPHEADER(pdiph);
943 if (!HIWORD(rguid)) {
944 switch (LOWORD(rguid)) {
945 case (DWORD) DIPROP_RANGE: {
946 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
947 int obj = find_property(&This->base.data_format, pdiph);
949 /* The app is querying the current range of the axis
950 * return the lMin and lMax values */
951 if (obj >= 0) {
952 pr->lMin = This->props[obj].lMin;
953 pr->lMax = This->props[obj].lMax;
954 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
955 return DI_OK;
957 break;
959 case (DWORD) DIPROP_DEADZONE: {
960 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
961 int obj = find_property(&This->base.data_format, pdiph);
963 if (obj >= 0) {
964 pd->dwData = This->props[obj].lDeadZone;
965 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
966 return DI_OK;
968 break;
970 case (DWORD) DIPROP_SATURATION: {
971 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
972 int obj = find_property(&This->base.data_format, pdiph);
974 if (obj >= 0) {
975 pd->dwData = This->props[obj].lSaturation;
976 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
977 return DI_OK;
979 break;
981 default:
982 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
986 return DI_OK;
989 /******************************************************************************
990 * GetObjectInfo : get object info
992 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
993 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
995 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
996 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
997 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
998 HRESULT res;
1000 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
1001 if (res != DI_OK) return res;
1003 if (pdidoi->dwType & DIDFT_AXIS)
1004 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
1005 else if (pdidoi->dwType & DIDFT_POV)
1006 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
1007 else if (pdidoi->dwType & DIDFT_BUTTON)
1008 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
1010 _dump_OBJECTINSTANCEW(pdidoi);
1011 return res;
1014 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
1015 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1017 HRESULT res;
1018 DIDEVICEOBJECTINSTANCEW didoiW;
1019 DWORD dwSize = pdidoi->dwSize;
1021 didoiW.dwSize = sizeof(didoiW);
1022 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1023 if (res != DI_OK) return res;
1025 memset(pdidoi, 0, pdidoi->dwSize);
1026 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1027 pdidoi->dwSize = dwSize;
1028 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1029 sizeof(pdidoi->tszName), NULL, NULL);
1031 return res;
1034 /******************************************************************************
1035 * GetDeviceInfo : get information about a device's identity
1037 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1038 LPDIRECTINPUTDEVICE8A iface,
1039 LPDIDEVICEINSTANCEA pdidi)
1041 JoystickImpl *This = (JoystickImpl *)iface;
1043 TRACE("(%p,%p)\n", iface, pdidi);
1045 if (pdidi == NULL) {
1046 WARN("invalid pointer\n");
1047 return E_POINTER;
1050 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1051 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1052 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1053 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1054 sizeof(DIDEVICEINSTANCEA));
1055 return DIERR_INVALIDPARAM;
1058 /* Return joystick */
1059 pdidi->guidInstance = GUID_Joystick;
1060 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1061 /* we only support traditional joysticks for now */
1062 pdidi->dwDevType = This->devcaps.dwDevType;
1063 strcpy(pdidi->tszInstanceName, "Joystick");
1064 strcpy(pdidi->tszProductName, This->name);
1065 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1066 pdidi->guidFFDriver = GUID_NULL;
1067 pdidi->wUsagePage = 0;
1068 pdidi->wUsage = 0;
1071 return DI_OK;
1074 /******************************************************************************
1075 * GetDeviceInfo : get information about a device's identity
1077 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1078 LPDIRECTINPUTDEVICE8W iface,
1079 LPDIDEVICEINSTANCEW pdidi)
1081 JoystickImpl *This = (JoystickImpl *)iface;
1083 TRACE("(%p,%p)\n", iface, pdidi);
1085 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1086 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1087 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1088 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1089 sizeof(DIDEVICEINSTANCEW));
1090 return DIERR_INVALIDPARAM;
1093 /* Return joystick */
1094 pdidi->guidInstance = GUID_Joystick;
1095 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1096 /* we only support traditional joysticks for now */
1097 pdidi->dwDevType = This->devcaps.dwDevType;
1098 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1099 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1100 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1101 pdidi->guidFFDriver = GUID_NULL;
1102 pdidi->wUsagePage = 0;
1103 pdidi->wUsage = 0;
1106 return DI_OK;
1109 static const IDirectInputDevice8AVtbl JoystickAvt =
1111 IDirectInputDevice2AImpl_QueryInterface,
1112 IDirectInputDevice2AImpl_AddRef,
1113 IDirectInputDevice2AImpl_Release,
1114 JoystickAImpl_GetCapabilities,
1115 IDirectInputDevice2AImpl_EnumObjects,
1116 JoystickAImpl_GetProperty,
1117 JoystickAImpl_SetProperty,
1118 JoystickAImpl_Acquire,
1119 JoystickAImpl_Unacquire,
1120 JoystickAImpl_GetDeviceState,
1121 IDirectInputDevice2AImpl_GetDeviceData,
1122 IDirectInputDevice2AImpl_SetDataFormat,
1123 IDirectInputDevice2AImpl_SetEventNotification,
1124 IDirectInputDevice2AImpl_SetCooperativeLevel,
1125 JoystickAImpl_GetObjectInfo,
1126 JoystickAImpl_GetDeviceInfo,
1127 IDirectInputDevice2AImpl_RunControlPanel,
1128 IDirectInputDevice2AImpl_Initialize,
1129 IDirectInputDevice2AImpl_CreateEffect,
1130 IDirectInputDevice2AImpl_EnumEffects,
1131 IDirectInputDevice2AImpl_GetEffectInfo,
1132 IDirectInputDevice2AImpl_GetForceFeedbackState,
1133 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1134 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1135 IDirectInputDevice2AImpl_Escape,
1136 JoystickAImpl_Poll,
1137 IDirectInputDevice2AImpl_SendDeviceData,
1138 IDirectInputDevice7AImpl_EnumEffectsInFile,
1139 IDirectInputDevice7AImpl_WriteEffectToFile,
1140 IDirectInputDevice8AImpl_BuildActionMap,
1141 IDirectInputDevice8AImpl_SetActionMap,
1142 IDirectInputDevice8AImpl_GetImageInfo
1145 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1146 # define XCAST(fun) (typeof(JoystickWvt.fun))
1147 #else
1148 # define XCAST(fun) (void*)
1149 #endif
1151 static const IDirectInputDevice8WVtbl JoystickWvt =
1153 IDirectInputDevice2WImpl_QueryInterface,
1154 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1155 XCAST(Release)IDirectInputDevice2AImpl_Release,
1156 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1157 IDirectInputDevice2WImpl_EnumObjects,
1158 XCAST(GetProperty)JoystickAImpl_GetProperty,
1159 XCAST(SetProperty)JoystickAImpl_SetProperty,
1160 XCAST(Acquire)JoystickAImpl_Acquire,
1161 XCAST(Unacquire)JoystickAImpl_Unacquire,
1162 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1163 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1164 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1165 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1166 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1167 IDirectInputDevice2WImpl_GetObjectInfo,
1168 JoystickWImpl_GetDeviceInfo,
1169 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1170 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1171 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1172 IDirectInputDevice2WImpl_EnumEffects,
1173 IDirectInputDevice2WImpl_GetEffectInfo,
1174 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1175 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1176 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1177 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1178 XCAST(Poll)JoystickAImpl_Poll,
1179 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1180 IDirectInputDevice7WImpl_EnumEffectsInFile,
1181 IDirectInputDevice7WImpl_WriteEffectToFile,
1182 IDirectInputDevice8WImpl_BuildActionMap,
1183 IDirectInputDevice8WImpl_SetActionMap,
1184 IDirectInputDevice8WImpl_GetImageInfo
1186 #undef XCAST
1188 #else /* HAVE_LINUX_22_JOYSTICK_API */
1190 const struct dinput_device joystick_linux_device = {
1191 "Wine Linux joystick driver",
1192 NULL,
1193 NULL,
1194 NULL,
1195 NULL
1198 #endif /* HAVE_LINUX_22_JOYSTICK_API */