push 421a6d0d1068c98851feb4eb01bb0e754f615ce9
[wine/hacks.git] / dlls / dinput / joystick_linux.c
blobef5b9f39d2783aea32a394566a4704bdf44157ad
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;
163 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
165 int fd = -1;
167 if (id > find_joystick_devices()) return FALSE;
169 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
170 WARN("force feedback not supported\n");
171 return FALSE;
174 if ((dwDevType == 0) ||
175 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
176 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
177 /* check whether we have a joystick */
178 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
180 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
181 return FALSE;
184 /* Return joystick */
185 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
186 lpddi->guidInstance.Data3 = id;
187 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
188 /* we only support traditional joysticks for now */
189 if (version >= 0x0800)
190 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
191 else
192 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
193 sprintf(lpddi->tszInstanceName, "Joystick %d", id);
194 #if defined(JSIOCGNAME)
195 if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
196 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
197 strcpy(lpddi->tszProductName, "Wine Joystick");
199 #else
200 strcpy(lpddi->tszProductName, "Wine Joystick");
201 #endif
203 lpddi->guidFFDriver = GUID_NULL;
204 close(fd);
205 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], lpddi->tszProductName);
206 return TRUE;
209 return FALSE;
212 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
214 int fd = -1;
215 char name[MAX_PATH];
216 char friendly[32];
218 if (id > find_joystick_devices()) return FALSE;
220 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
221 WARN("force feedback not supported\n");
222 return FALSE;
225 if ((dwDevType == 0) ||
226 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
227 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
228 /* check whether we have a joystick */
229 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
231 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
232 return FALSE;
235 /* Return joystick */
236 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
237 lpddi->guidInstance.Data3 = id;
238 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
239 /* we only support traditional joysticks for now */
240 if (version >= 0x0800)
241 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
242 else
243 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
244 sprintf(friendly, "Joystick %d", id);
245 MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
246 #if defined(JSIOCGNAME)
247 if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
248 WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
249 strcpy(name, "Wine Joystick");
251 #else
252 strcpy(name, "Wine Joystick");
253 #endif
254 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
255 lpddi->guidFFDriver = GUID_NULL;
256 close(fd);
257 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], name);
258 return TRUE;
261 return FALSE;
265 * Get a config key from either the app-specific or the default config
268 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
269 char *buffer, DWORD size )
271 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size ))
272 return 0;
274 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size ))
275 return 0;
277 return ERROR_FILE_NOT_FOUND;
281 * Setup the dinput options.
284 static HRESULT setup_dinput_options(JoystickImpl * device)
286 char buffer[MAX_PATH+16];
287 HKEY hkey, appkey = 0;
288 DWORD len;
290 buffer[MAX_PATH]='\0';
292 /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
293 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", &hkey)) hkey = 0;
295 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
296 if (len && len < MAX_PATH) {
297 HKEY tmpkey;
298 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
299 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
301 char *p, *appname = buffer;
302 if ((p = strrchr( appname, '/' ))) appname = p + 1;
303 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
304 strcat( appname, "\\DirectInput" );
305 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
306 RegCloseKey( tmpkey );
310 /* get options */
312 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
313 device->deadzone = atoi(buffer);
314 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
317 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
318 int tokens = 0;
319 int axis = 0;
320 int pov = 0;
321 const char *delim = ",";
322 char * ptr;
323 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
325 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
326 if (device->axis_map == 0)
327 return DIERR_OUTOFMEMORY;
329 if ((ptr = strtok(buffer, delim)) != NULL) {
330 do {
331 if (strcmp(ptr, "X") == 0) {
332 device->axis_map[tokens] = 0;
333 axis++;
334 } else if (strcmp(ptr, "Y") == 0) {
335 device->axis_map[tokens] = 1;
336 axis++;
337 } else if (strcmp(ptr, "Z") == 0) {
338 device->axis_map[tokens] = 2;
339 axis++;
340 } else if (strcmp(ptr, "Rx") == 0) {
341 device->axis_map[tokens] = 3;
342 axis++;
343 } else if (strcmp(ptr, "Ry") == 0) {
344 device->axis_map[tokens] = 4;
345 axis++;
346 } else if (strcmp(ptr, "Rz") == 0) {
347 device->axis_map[tokens] = 5;
348 axis++;
349 } else if (strcmp(ptr, "Slider1") == 0) {
350 device->axis_map[tokens] = 6;
351 axis++;
352 } else if (strcmp(ptr, "Slider2") == 0) {
353 device->axis_map[tokens] = 7;
354 axis++;
355 } else if (strcmp(ptr, "POV1") == 0) {
356 device->axis_map[tokens++] = 8;
357 device->axis_map[tokens] = 8;
358 pov++;
359 } else if (strcmp(ptr, "POV2") == 0) {
360 device->axis_map[tokens++] = 9;
361 device->axis_map[tokens] = 9;
362 pov++;
363 } else if (strcmp(ptr, "POV3") == 0) {
364 device->axis_map[tokens++] = 10;
365 device->axis_map[tokens] = 10;
366 pov++;
367 } else if (strcmp(ptr, "POV4") == 0) {
368 device->axis_map[tokens++] = 11;
369 device->axis_map[tokens] = 11;
370 pov++;
371 } else {
372 ERR("invalid joystick axis type: %s\n", ptr);
373 device->axis_map[tokens] = tokens;
374 axis++;
377 tokens++;
378 } while ((ptr = strtok(NULL, delim)) != NULL);
380 if (tokens != device->devcaps.dwAxes) {
381 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
382 while (tokens < device->axes) {
383 device->axis_map[tokens] = tokens;
384 tokens++;
389 device->devcaps.dwAxes = axis;
390 device->devcaps.dwPOVs = pov;
393 if (appkey)
394 RegCloseKey( appkey );
396 if (hkey)
397 RegCloseKey( hkey );
399 return DI_OK;
402 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
403 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
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[index], 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 /******************************************************************************
565 * get_joystick_index : Get the joystick index from a given GUID
567 static unsigned short get_joystick_index(REFGUID guid)
569 GUID wine_joystick = DInput_Wine_Joystick_GUID;
570 GUID dev_guid = *guid;
572 wine_joystick.Data3 = 0;
573 dev_guid.Data3 = 0;
575 /* for the standard joystick GUID use index 0 */
576 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
578 /* for the wine joystick GUIDs use the index stored in Data3 */
579 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
581 return MAX_JOYSTICKS;
584 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
586 unsigned short index;
588 find_joystick_devices();
589 *pdev = NULL;
591 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
592 joystick_devices_count && index < joystick_devices_count)
594 if ((riid == NULL) ||
595 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
596 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
597 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
598 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
600 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
603 WARN("no interface\n");
604 return DIERR_NOINTERFACE;
607 WARN("invalid device GUID\n");
608 return DIERR_DEVICENOTREG;
611 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
613 unsigned short index;
615 find_joystick_devices();
616 *pdev = NULL;
618 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
619 joystick_devices_count && index < joystick_devices_count)
621 if ((riid == NULL) ||
622 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
623 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
624 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
625 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
627 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
629 WARN("no interface\n");
630 return DIERR_NOINTERFACE;
633 WARN("invalid device GUID\n");
634 return DIERR_DEVICENOTREG;
637 #undef MAX_JOYSTICKS
639 const struct dinput_device joystick_linux_device = {
640 "Wine Linux joystick driver",
641 joydev_enum_deviceA,
642 joydev_enum_deviceW,
643 joydev_create_deviceA,
644 joydev_create_deviceW
647 /******************************************************************************
648 * Acquire : gets exclusive control of the joystick
650 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
652 JoystickImpl *This = (JoystickImpl *)iface;
654 TRACE("(%p)\n",This);
656 if (This->base.acquired) {
657 WARN("already acquired\n");
658 return S_FALSE;
661 /* open the joystick device */
662 if (This->joyfd==-1) {
663 TRACE("opening joystick device %s\n", This->dev);
665 This->joyfd=open(This->dev,O_RDONLY);
666 if (This->joyfd==-1) {
667 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
668 return DIERR_NOTFOUND;
672 This->base.acquired = 1;
674 return DI_OK;
677 /******************************************************************************
678 * Unacquire : frees the joystick
680 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
682 JoystickImpl *This = (JoystickImpl *)iface;
683 HRESULT res;
685 TRACE("(%p)\n",This);
687 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
689 if (This->joyfd!=-1) {
690 TRACE("closing joystick device\n");
691 close(This->joyfd);
692 This->joyfd = -1;
693 return DI_OK;
696 return DI_NOEFFECT;
699 static void joy_polldev(JoystickImpl *This) {
700 struct pollfd plfd;
701 struct js_event jse;
702 TRACE("(%p)\n", This);
704 if (This->joyfd==-1) {
705 WARN("no device\n");
706 return;
708 while (1)
710 LONG value;
711 int inst_id = -1;
713 plfd.fd = This->joyfd;
714 plfd.events = POLLIN;
715 if (poll(&plfd,1,0) != 1)
716 return;
717 /* we have one event, so we can read */
718 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
719 return;
721 TRACE("js_event: type 0x%x, number %d, value %d\n",
722 jse.type,jse.number,jse.value);
723 if (jse.type & JS_EVENT_BUTTON)
725 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
726 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
728 else if (jse.type & JS_EVENT_AXIS)
730 int number = This->axis_map[jse.number]; /* wine format object index */
732 if (number < 0) return;
733 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
734 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
736 TRACE("changing axis %d => %d\n", jse.number, number);
737 switch (number)
739 case 0: This->js.lX = value; break;
740 case 1: This->js.lY = value; break;
741 case 2: This->js.lZ = value; break;
742 case 3: This->js.lRx = value; break;
743 case 4: This->js.lRy = value; break;
744 case 5: This->js.lRz = value; break;
745 case 6: This->js.rglSlider[0] = value; break;
746 case 7: This->js.rglSlider[1] = value; break;
747 case 8: case 9: case 10: case 11:
749 int idx = number - 8;
751 if (jse.number % 2)
752 This->povs[idx].y = jse.value;
753 else
754 This->povs[idx].x = jse.value;
756 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
757 break;
759 default:
760 WARN("axis %d not supported\n", number);
763 if (inst_id >= 0)
764 queue_event((LPDIRECTINPUTDEVICE8A)This,
765 id_to_offset(&This->base.data_format, inst_id),
766 value, jse.time, This->base.dinput->evsequence++);
770 /******************************************************************************
771 * GetDeviceState : returns the "state" of the joystick.
774 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
775 LPDIRECTINPUTDEVICE8A iface,
776 DWORD len,
777 LPVOID ptr)
779 JoystickImpl *This = (JoystickImpl *)iface;
781 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
783 if (!This->base.acquired) {
784 WARN("not acquired\n");
785 return DIERR_NOTACQUIRED;
788 /* update joystick state */
789 joy_polldev(This);
791 /* convert and copy data to user supplied buffer */
792 fill_DataFormat(ptr, &This->js, &This->base.data_format);
794 return DI_OK;
797 /******************************************************************************
798 * SetProperty : change input device properties
800 static HRESULT WINAPI JoystickAImpl_SetProperty(
801 LPDIRECTINPUTDEVICE8A iface,
802 REFGUID rguid,
803 LPCDIPROPHEADER ph)
805 JoystickImpl *This = (JoystickImpl *)iface;
806 int i;
808 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
810 if (ph == NULL) {
811 WARN("invalid parameter: ph == NULL\n");
812 return DIERR_INVALIDPARAM;
815 if (TRACE_ON(dinput))
816 _dump_DIPROPHEADER(ph);
818 if (!HIWORD(rguid)) {
819 switch (LOWORD(rguid)) {
820 case (DWORD)DIPROP_RANGE: {
821 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
822 if (ph->dwHow == DIPH_DEVICE) {
823 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
824 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
825 This->props[i].lMin = pr->lMin;
826 This->props[i].lMax = pr->lMax;
828 } else {
829 int obj = find_property(&This->base.data_format, ph);
831 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
832 if (obj >= 0) {
833 This->props[obj].lMin = pr->lMin;
834 This->props[obj].lMax = pr->lMax;
835 return DI_OK;
838 break;
840 case (DWORD)DIPROP_DEADZONE: {
841 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
842 if (ph->dwHow == DIPH_DEVICE) {
843 TRACE("deadzone(%d) all\n", pd->dwData);
844 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
845 This->props[i].lDeadZone = pd->dwData;
846 } else {
847 int obj = find_property(&This->base.data_format, ph);
849 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
850 if (obj >= 0) {
851 This->props[obj].lDeadZone = pd->dwData;
852 return DI_OK;
855 break;
857 case (DWORD)DIPROP_SATURATION: {
858 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
859 if (ph->dwHow == DIPH_DEVICE) {
860 TRACE("saturation(%d) all\n", pd->dwData);
861 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
862 This->props[i].lSaturation = pd->dwData;
863 } else {
864 int obj = find_property(&This->base.data_format, ph);
866 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
867 if (obj >= 0) {
868 This->props[obj].lSaturation = pd->dwData;
869 return DI_OK;
872 break;
874 default:
875 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
879 return DI_OK;
882 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
883 LPDIRECTINPUTDEVICE8A iface,
884 LPDIDEVCAPS lpDIDevCaps)
886 JoystickImpl *This = (JoystickImpl *)iface;
887 int size;
889 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
891 if (lpDIDevCaps == NULL) {
892 WARN("invalid pointer\n");
893 return E_POINTER;
896 size = lpDIDevCaps->dwSize;
898 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
899 WARN("invalid parameter\n");
900 return DIERR_INVALIDPARAM;
903 CopyMemory(lpDIDevCaps, &This->devcaps, size);
904 lpDIDevCaps->dwSize = size;
906 if (TRACE_ON(dinput))
907 _dump_DIDEVCAPS(lpDIDevCaps);
909 return DI_OK;
912 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
914 JoystickImpl *This = (JoystickImpl *)iface;
916 TRACE("(%p)\n",This);
918 if (!This->base.acquired) {
919 WARN("not acquired\n");
920 return DIERR_NOTACQUIRED;
923 joy_polldev(This);
924 return DI_OK;
927 /******************************************************************************
928 * GetProperty : get input device properties
930 static HRESULT WINAPI JoystickAImpl_GetProperty(
931 LPDIRECTINPUTDEVICE8A iface,
932 REFGUID rguid,
933 LPDIPROPHEADER pdiph)
935 JoystickImpl *This = (JoystickImpl *)iface;
937 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
939 if (TRACE_ON(dinput))
940 _dump_DIPROPHEADER(pdiph);
942 if (!HIWORD(rguid)) {
943 switch (LOWORD(rguid)) {
944 case (DWORD) DIPROP_RANGE: {
945 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
946 int obj = find_property(&This->base.data_format, pdiph);
948 /* The app is querying the current range of the axis
949 * return the lMin and lMax values */
950 if (obj >= 0) {
951 pr->lMin = This->props[obj].lMin;
952 pr->lMax = This->props[obj].lMax;
953 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
954 return DI_OK;
956 break;
958 case (DWORD) DIPROP_DEADZONE: {
959 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
960 int obj = find_property(&This->base.data_format, pdiph);
962 if (obj >= 0) {
963 pd->dwData = This->props[obj].lDeadZone;
964 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
965 return DI_OK;
967 break;
969 case (DWORD) DIPROP_SATURATION: {
970 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
971 int obj = find_property(&This->base.data_format, pdiph);
973 if (obj >= 0) {
974 pd->dwData = This->props[obj].lSaturation;
975 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
976 return DI_OK;
978 break;
980 default:
981 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
985 return DI_OK;
988 /******************************************************************************
989 * GetObjectInfo : get object info
991 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
992 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
994 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
995 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
996 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
997 HRESULT res;
999 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
1000 if (res != DI_OK) return res;
1002 if (pdidoi->dwType & DIDFT_AXIS)
1003 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
1004 else if (pdidoi->dwType & DIDFT_POV)
1005 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
1006 else if (pdidoi->dwType & DIDFT_BUTTON)
1007 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
1009 _dump_OBJECTINSTANCEW(pdidoi);
1010 return res;
1013 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
1014 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1016 HRESULT res;
1017 DIDEVICEOBJECTINSTANCEW didoiW;
1018 DWORD dwSize = pdidoi->dwSize;
1020 didoiW.dwSize = sizeof(didoiW);
1021 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1022 if (res != DI_OK) return res;
1024 memset(pdidoi, 0, pdidoi->dwSize);
1025 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1026 pdidoi->dwSize = dwSize;
1027 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1028 sizeof(pdidoi->tszName), NULL, NULL);
1030 return res;
1033 /******************************************************************************
1034 * GetDeviceInfo : get information about a device's identity
1036 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1037 LPDIRECTINPUTDEVICE8A iface,
1038 LPDIDEVICEINSTANCEA pdidi)
1040 JoystickImpl *This = (JoystickImpl *)iface;
1042 TRACE("(%p,%p)\n", iface, pdidi);
1044 if (pdidi == NULL) {
1045 WARN("invalid pointer\n");
1046 return E_POINTER;
1049 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1050 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1051 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1052 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1053 sizeof(DIDEVICEINSTANCEA));
1054 return DIERR_INVALIDPARAM;
1057 /* Return joystick */
1058 pdidi->guidInstance = GUID_Joystick;
1059 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1060 /* we only support traditional joysticks for now */
1061 pdidi->dwDevType = This->devcaps.dwDevType;
1062 strcpy(pdidi->tszInstanceName, "Joystick");
1063 strcpy(pdidi->tszProductName, This->name);
1064 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1065 pdidi->guidFFDriver = GUID_NULL;
1066 pdidi->wUsagePage = 0;
1067 pdidi->wUsage = 0;
1070 return DI_OK;
1073 /******************************************************************************
1074 * GetDeviceInfo : get information about a device's identity
1076 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1077 LPDIRECTINPUTDEVICE8W iface,
1078 LPDIDEVICEINSTANCEW pdidi)
1080 JoystickImpl *This = (JoystickImpl *)iface;
1082 TRACE("(%p,%p)\n", iface, pdidi);
1084 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1085 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1086 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1087 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1088 sizeof(DIDEVICEINSTANCEW));
1089 return DIERR_INVALIDPARAM;
1092 /* Return joystick */
1093 pdidi->guidInstance = GUID_Joystick;
1094 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1095 /* we only support traditional joysticks for now */
1096 pdidi->dwDevType = This->devcaps.dwDevType;
1097 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1098 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1099 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1100 pdidi->guidFFDriver = GUID_NULL;
1101 pdidi->wUsagePage = 0;
1102 pdidi->wUsage = 0;
1105 return DI_OK;
1108 static const IDirectInputDevice8AVtbl JoystickAvt =
1110 IDirectInputDevice2AImpl_QueryInterface,
1111 IDirectInputDevice2AImpl_AddRef,
1112 IDirectInputDevice2AImpl_Release,
1113 JoystickAImpl_GetCapabilities,
1114 IDirectInputDevice2AImpl_EnumObjects,
1115 JoystickAImpl_GetProperty,
1116 JoystickAImpl_SetProperty,
1117 JoystickAImpl_Acquire,
1118 JoystickAImpl_Unacquire,
1119 JoystickAImpl_GetDeviceState,
1120 IDirectInputDevice2AImpl_GetDeviceData,
1121 IDirectInputDevice2AImpl_SetDataFormat,
1122 IDirectInputDevice2AImpl_SetEventNotification,
1123 IDirectInputDevice2AImpl_SetCooperativeLevel,
1124 JoystickAImpl_GetObjectInfo,
1125 JoystickAImpl_GetDeviceInfo,
1126 IDirectInputDevice2AImpl_RunControlPanel,
1127 IDirectInputDevice2AImpl_Initialize,
1128 IDirectInputDevice2AImpl_CreateEffect,
1129 IDirectInputDevice2AImpl_EnumEffects,
1130 IDirectInputDevice2AImpl_GetEffectInfo,
1131 IDirectInputDevice2AImpl_GetForceFeedbackState,
1132 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1133 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1134 IDirectInputDevice2AImpl_Escape,
1135 JoystickAImpl_Poll,
1136 IDirectInputDevice2AImpl_SendDeviceData,
1137 IDirectInputDevice7AImpl_EnumEffectsInFile,
1138 IDirectInputDevice7AImpl_WriteEffectToFile,
1139 IDirectInputDevice8AImpl_BuildActionMap,
1140 IDirectInputDevice8AImpl_SetActionMap,
1141 IDirectInputDevice8AImpl_GetImageInfo
1144 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1145 # define XCAST(fun) (typeof(JoystickWvt.fun))
1146 #else
1147 # define XCAST(fun) (void*)
1148 #endif
1150 static const IDirectInputDevice8WVtbl JoystickWvt =
1152 IDirectInputDevice2WImpl_QueryInterface,
1153 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1154 XCAST(Release)IDirectInputDevice2AImpl_Release,
1155 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1156 IDirectInputDevice2WImpl_EnumObjects,
1157 XCAST(GetProperty)JoystickAImpl_GetProperty,
1158 XCAST(SetProperty)JoystickAImpl_SetProperty,
1159 XCAST(Acquire)JoystickAImpl_Acquire,
1160 XCAST(Unacquire)JoystickAImpl_Unacquire,
1161 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1162 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1163 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1164 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1165 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1166 IDirectInputDevice2WImpl_GetObjectInfo,
1167 JoystickWImpl_GetDeviceInfo,
1168 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1169 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1170 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1171 IDirectInputDevice2WImpl_EnumEffects,
1172 IDirectInputDevice2WImpl_GetEffectInfo,
1173 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1174 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1175 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1176 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1177 XCAST(Poll)JoystickAImpl_Poll,
1178 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1179 IDirectInputDevice7WImpl_EnumEffectsInFile,
1180 IDirectInputDevice7WImpl_WriteEffectToFile,
1181 IDirectInputDevice8WImpl_BuildActionMap,
1182 IDirectInputDevice8WImpl_SetActionMap,
1183 IDirectInputDevice8WImpl_GetImageInfo
1185 #undef XCAST
1187 #else /* HAVE_LINUX_22_JOYSTICK_API */
1189 const struct dinput_device joystick_linux_device = {
1190 "Wine Linux joystick driver",
1191 NULL,
1192 NULL,
1193 NULL,
1194 NULL
1197 #endif /* HAVE_LINUX_22_JOYSTICK_API */