dinput: Move get_config_key to a common place.
[wine/wine-jacek.git] / dlls / dinput / joystick_linux.c
blob0f821de4e8116ae7c3a94dc6a9154ba3a4d1d01f
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 * Setup the dinput options.
269 static HRESULT setup_dinput_options(JoystickImpl * device)
271 char buffer[MAX_PATH+16];
272 HKEY hkey, appkey = 0;
273 DWORD len;
275 buffer[MAX_PATH]='\0';
277 /* @@ Wine registry key: HKCU\Software\Wine\DirectInput */
278 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectInput", &hkey)) hkey = 0;
280 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
281 if (len && len < MAX_PATH) {
282 HKEY tmpkey;
283 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectInput */
284 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
286 char *p, *appname = buffer;
287 if ((p = strrchr( appname, '/' ))) appname = p + 1;
288 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
289 strcat( appname, "\\DirectInput" );
290 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
291 RegCloseKey( tmpkey );
295 /* get options */
297 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
298 device->deadzone = atoi(buffer);
299 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
302 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
303 int tokens = 0;
304 int axis = 0;
305 int pov = 0;
306 const char *delim = ",";
307 char * ptr;
308 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
310 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
311 if (device->axis_map == 0)
312 return DIERR_OUTOFMEMORY;
314 if ((ptr = strtok(buffer, delim)) != NULL) {
315 do {
316 if (strcmp(ptr, "X") == 0) {
317 device->axis_map[tokens] = 0;
318 axis++;
319 } else if (strcmp(ptr, "Y") == 0) {
320 device->axis_map[tokens] = 1;
321 axis++;
322 } else if (strcmp(ptr, "Z") == 0) {
323 device->axis_map[tokens] = 2;
324 axis++;
325 } else if (strcmp(ptr, "Rx") == 0) {
326 device->axis_map[tokens] = 3;
327 axis++;
328 } else if (strcmp(ptr, "Ry") == 0) {
329 device->axis_map[tokens] = 4;
330 axis++;
331 } else if (strcmp(ptr, "Rz") == 0) {
332 device->axis_map[tokens] = 5;
333 axis++;
334 } else if (strcmp(ptr, "Slider1") == 0) {
335 device->axis_map[tokens] = 6;
336 axis++;
337 } else if (strcmp(ptr, "Slider2") == 0) {
338 device->axis_map[tokens] = 7;
339 axis++;
340 } else if (strcmp(ptr, "POV1") == 0) {
341 device->axis_map[tokens++] = 8;
342 device->axis_map[tokens] = 8;
343 pov++;
344 } else if (strcmp(ptr, "POV2") == 0) {
345 device->axis_map[tokens++] = 9;
346 device->axis_map[tokens] = 9;
347 pov++;
348 } else if (strcmp(ptr, "POV3") == 0) {
349 device->axis_map[tokens++] = 10;
350 device->axis_map[tokens] = 10;
351 pov++;
352 } else if (strcmp(ptr, "POV4") == 0) {
353 device->axis_map[tokens++] = 11;
354 device->axis_map[tokens] = 11;
355 pov++;
356 } else {
357 ERR("invalid joystick axis type: %s\n", ptr);
358 device->axis_map[tokens] = tokens;
359 axis++;
362 tokens++;
363 } while ((ptr = strtok(NULL, delim)) != NULL);
365 if (tokens != device->devcaps.dwAxes) {
366 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
367 while (tokens < device->axes) {
368 device->axis_map[tokens] = tokens;
369 tokens++;
374 device->devcaps.dwAxes = axis;
375 device->devcaps.dwPOVs = pov;
378 if (appkey)
379 RegCloseKey( appkey );
381 if (hkey)
382 RegCloseKey( hkey );
384 return DI_OK;
387 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
388 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
390 DWORD i;
391 JoystickImpl* newDevice;
392 char name[MAX_PATH];
393 HRESULT hr;
394 LPDIDATAFORMAT df = NULL;
395 int idx = 0;
397 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
398 if (newDevice == 0) {
399 WARN("out of memory\n");
400 *pdev = 0;
401 return DIERR_OUTOFMEMORY;
404 if (!lstrcpynA(newDevice->dev, joystick_devices[index], sizeof(newDevice->dev)) ||
405 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
407 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
408 HeapFree(GetProcessHeap(), 0, newDevice);
409 return DIERR_DEVICENOTREG;
412 /* get the device name */
413 #if defined(JSIOCGNAME)
414 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
415 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
416 strcpy(name, "Wine Joystick");
418 #else
419 strcpy(name, "Wine Joystick");
420 #endif
422 /* copy the device name */
423 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
424 strcpy(newDevice->name, name);
426 #ifdef JSIOCGAXES
427 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
428 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
429 newDevice->axes = 2;
431 #endif
432 #ifdef JSIOCGBUTTONS
433 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
434 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
435 newDevice->buttons = 2;
437 #endif
439 newDevice->base.lpVtbl = jvt;
440 newDevice->base.ref = 1;
441 newDevice->base.dinput = dinput;
442 CopyMemory(&newDevice->base.guid, rguid, sizeof(*rguid));
443 InitializeCriticalSection(&newDevice->base.crit);
444 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
446 /* setup_dinput_options may change these */
447 newDevice->deadzone = 0;
448 newDevice->devcaps.dwButtons = newDevice->buttons;
449 newDevice->devcaps.dwAxes = newDevice->axes;
450 newDevice->devcaps.dwPOVs = 0;
452 /* do any user specified configuration */
453 hr = setup_dinput_options(newDevice);
454 if (hr != DI_OK)
455 goto FAILED1;
457 if (newDevice->axis_map == 0) {
458 newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
459 if (newDevice->axis_map == 0)
460 goto FAILED;
462 for (i = 0; i < newDevice->axes; i++)
463 newDevice->axis_map[i] = i;
466 /* Create copy of default data format */
467 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
468 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
470 /* Axes include POVs */
471 df->dwNumObjs = newDevice->axes + newDevice->buttons;
472 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
474 for (i = 0; i < newDevice->axes; i++)
476 int wine_obj = newDevice->axis_map[i];
478 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
479 if (wine_obj < 8)
480 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
481 else
483 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
484 i++; /* POV takes 2 axes */
487 for (i = 0; i < newDevice->buttons; i++)
489 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
490 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
492 newDevice->base.data_format.wine_df = df;
494 /* create default properties */
495 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
496 if (newDevice->props == 0)
497 goto FAILED;
499 /* initialize default properties */
500 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
501 newDevice->props[i].lDevMin = -32767;
502 newDevice->props[i].lDevMax = +32767;
503 newDevice->props[i].lMin = 0;
504 newDevice->props[i].lMax = 0xffff;
505 newDevice->props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
506 newDevice->props[i].lSaturation = 0;
509 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
511 newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
512 newDevice->devcaps.dwFlags = DIDC_ATTACHED;
513 if (newDevice->base.dinput->dwVersion >= 0x0800)
514 newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
515 else
516 newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
517 newDevice->devcaps.dwFFSamplePeriod = 0;
518 newDevice->devcaps.dwFFMinTimeResolution = 0;
519 newDevice->devcaps.dwFirmwareRevision = 0;
520 newDevice->devcaps.dwHardwareRevision = 0;
521 newDevice->devcaps.dwFFDriverVersion = 0;
523 if (TRACE_ON(dinput)) {
524 _dump_DIDATAFORMAT(newDevice->base.data_format.wine_df);
525 for (i = 0; i < (newDevice->axes); i++)
526 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
527 _dump_DIDEVCAPS(&newDevice->devcaps);
530 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
532 return DI_OK;
534 FAILED:
535 hr = DIERR_OUTOFMEMORY;
536 FAILED1:
537 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
538 HeapFree(GetProcessHeap(), 0, df);
539 release_DataFormat(&newDevice->base.data_format);
540 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
541 HeapFree(GetProcessHeap(),0,newDevice->name);
542 HeapFree(GetProcessHeap(),0,newDevice->props);
543 HeapFree(GetProcessHeap(),0,newDevice);
544 *pdev = 0;
546 return hr;
549 /******************************************************************************
550 * get_joystick_index : Get the joystick index from a given GUID
552 static unsigned short get_joystick_index(REFGUID guid)
554 GUID wine_joystick = DInput_Wine_Joystick_GUID;
555 GUID dev_guid = *guid;
557 wine_joystick.Data3 = 0;
558 dev_guid.Data3 = 0;
560 /* for the standard joystick GUID use index 0 */
561 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
563 /* for the wine joystick GUIDs use the index stored in Data3 */
564 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
566 return MAX_JOYSTICKS;
569 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
571 unsigned short index;
573 find_joystick_devices();
574 *pdev = NULL;
576 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
577 joystick_devices_count && index < joystick_devices_count)
579 if ((riid == NULL) ||
580 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
581 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
582 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
583 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
585 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
588 WARN("no interface\n");
589 return DIERR_NOINTERFACE;
592 WARN("invalid device GUID\n");
593 return DIERR_DEVICENOTREG;
596 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
598 unsigned short index;
600 find_joystick_devices();
601 *pdev = NULL;
603 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
604 joystick_devices_count && index < joystick_devices_count)
606 if ((riid == NULL) ||
607 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
608 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
609 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
610 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
612 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
614 WARN("no interface\n");
615 return DIERR_NOINTERFACE;
618 WARN("invalid device GUID\n");
619 return DIERR_DEVICENOTREG;
622 #undef MAX_JOYSTICKS
624 const struct dinput_device joystick_linux_device = {
625 "Wine Linux joystick driver",
626 joydev_enum_deviceA,
627 joydev_enum_deviceW,
628 joydev_create_deviceA,
629 joydev_create_deviceW
632 /******************************************************************************
633 * Acquire : gets exclusive control of the joystick
635 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
637 JoystickImpl *This = (JoystickImpl *)iface;
639 TRACE("(%p)\n",This);
641 if (This->base.acquired) {
642 WARN("already acquired\n");
643 return S_FALSE;
646 /* open the joystick device */
647 if (This->joyfd==-1) {
648 TRACE("opening joystick device %s\n", This->dev);
650 This->joyfd=open(This->dev,O_RDONLY);
651 if (This->joyfd==-1) {
652 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
653 return DIERR_NOTFOUND;
657 This->base.acquired = 1;
659 return DI_OK;
662 /******************************************************************************
663 * Unacquire : frees the joystick
665 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
667 JoystickImpl *This = (JoystickImpl *)iface;
668 HRESULT res;
670 TRACE("(%p)\n",This);
672 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
674 if (This->joyfd!=-1) {
675 TRACE("closing joystick device\n");
676 close(This->joyfd);
677 This->joyfd = -1;
678 return DI_OK;
681 return DI_NOEFFECT;
684 static void joy_polldev(JoystickImpl *This) {
685 struct pollfd plfd;
686 struct js_event jse;
687 TRACE("(%p)\n", This);
689 if (This->joyfd==-1) {
690 WARN("no device\n");
691 return;
693 while (1)
695 LONG value;
696 int inst_id = -1;
698 plfd.fd = This->joyfd;
699 plfd.events = POLLIN;
700 if (poll(&plfd,1,0) != 1)
701 return;
702 /* we have one event, so we can read */
703 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
704 return;
706 TRACE("js_event: type 0x%x, number %d, value %d\n",
707 jse.type,jse.number,jse.value);
708 if (jse.type & JS_EVENT_BUTTON)
710 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
711 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
713 else if (jse.type & JS_EVENT_AXIS)
715 int number = This->axis_map[jse.number]; /* wine format object index */
717 if (number < 0) return;
718 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
719 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
721 TRACE("changing axis %d => %d\n", jse.number, number);
722 switch (number)
724 case 0: This->js.lX = value; break;
725 case 1: This->js.lY = value; break;
726 case 2: This->js.lZ = value; break;
727 case 3: This->js.lRx = value; break;
728 case 4: This->js.lRy = value; break;
729 case 5: This->js.lRz = value; break;
730 case 6: This->js.rglSlider[0] = value; break;
731 case 7: This->js.rglSlider[1] = value; break;
732 case 8: case 9: case 10: case 11:
734 int idx = number - 8;
736 if (jse.number % 2)
737 This->povs[idx].y = jse.value;
738 else
739 This->povs[idx].x = jse.value;
741 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
742 break;
744 default:
745 WARN("axis %d not supported\n", number);
748 if (inst_id >= 0)
749 queue_event((LPDIRECTINPUTDEVICE8A)This,
750 id_to_offset(&This->base.data_format, inst_id),
751 value, jse.time, This->base.dinput->evsequence++);
755 /******************************************************************************
756 * GetDeviceState : returns the "state" of the joystick.
759 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
760 LPDIRECTINPUTDEVICE8A iface,
761 DWORD len,
762 LPVOID ptr)
764 JoystickImpl *This = (JoystickImpl *)iface;
766 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
768 if (!This->base.acquired) {
769 WARN("not acquired\n");
770 return DIERR_NOTACQUIRED;
773 /* update joystick state */
774 joy_polldev(This);
776 /* convert and copy data to user supplied buffer */
777 fill_DataFormat(ptr, &This->js, &This->base.data_format);
779 return DI_OK;
782 /******************************************************************************
783 * SetProperty : change input device properties
785 static HRESULT WINAPI JoystickAImpl_SetProperty(
786 LPDIRECTINPUTDEVICE8A iface,
787 REFGUID rguid,
788 LPCDIPROPHEADER ph)
790 JoystickImpl *This = (JoystickImpl *)iface;
791 int i;
793 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
795 if (ph == NULL) {
796 WARN("invalid parameter: ph == NULL\n");
797 return DIERR_INVALIDPARAM;
800 if (TRACE_ON(dinput))
801 _dump_DIPROPHEADER(ph);
803 if (!HIWORD(rguid)) {
804 switch (LOWORD(rguid)) {
805 case (DWORD)DIPROP_RANGE: {
806 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
807 if (ph->dwHow == DIPH_DEVICE) {
808 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
809 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
810 This->props[i].lMin = pr->lMin;
811 This->props[i].lMax = pr->lMax;
813 } else {
814 int obj = find_property(&This->base.data_format, ph);
816 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
817 if (obj >= 0) {
818 This->props[obj].lMin = pr->lMin;
819 This->props[obj].lMax = pr->lMax;
820 return DI_OK;
823 break;
825 case (DWORD)DIPROP_DEADZONE: {
826 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
827 if (ph->dwHow == DIPH_DEVICE) {
828 TRACE("deadzone(%d) all\n", pd->dwData);
829 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
830 This->props[i].lDeadZone = pd->dwData;
831 } else {
832 int obj = find_property(&This->base.data_format, ph);
834 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
835 if (obj >= 0) {
836 This->props[obj].lDeadZone = pd->dwData;
837 return DI_OK;
840 break;
842 case (DWORD)DIPROP_SATURATION: {
843 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
844 if (ph->dwHow == DIPH_DEVICE) {
845 TRACE("saturation(%d) all\n", pd->dwData);
846 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
847 This->props[i].lSaturation = pd->dwData;
848 } else {
849 int obj = find_property(&This->base.data_format, ph);
851 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
852 if (obj >= 0) {
853 This->props[obj].lSaturation = pd->dwData;
854 return DI_OK;
857 break;
859 default:
860 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
864 return DI_OK;
867 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
868 LPDIRECTINPUTDEVICE8A iface,
869 LPDIDEVCAPS lpDIDevCaps)
871 JoystickImpl *This = (JoystickImpl *)iface;
872 int size;
874 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
876 if (lpDIDevCaps == NULL) {
877 WARN("invalid pointer\n");
878 return E_POINTER;
881 size = lpDIDevCaps->dwSize;
883 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
884 WARN("invalid parameter\n");
885 return DIERR_INVALIDPARAM;
888 CopyMemory(lpDIDevCaps, &This->devcaps, size);
889 lpDIDevCaps->dwSize = size;
891 if (TRACE_ON(dinput))
892 _dump_DIDEVCAPS(lpDIDevCaps);
894 return DI_OK;
897 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
899 JoystickImpl *This = (JoystickImpl *)iface;
901 TRACE("(%p)\n",This);
903 if (!This->base.acquired) {
904 WARN("not acquired\n");
905 return DIERR_NOTACQUIRED;
908 joy_polldev(This);
909 return DI_OK;
912 /******************************************************************************
913 * GetProperty : get input device properties
915 static HRESULT WINAPI JoystickAImpl_GetProperty(
916 LPDIRECTINPUTDEVICE8A iface,
917 REFGUID rguid,
918 LPDIPROPHEADER pdiph)
920 JoystickImpl *This = (JoystickImpl *)iface;
922 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
924 if (TRACE_ON(dinput))
925 _dump_DIPROPHEADER(pdiph);
927 if (!HIWORD(rguid)) {
928 switch (LOWORD(rguid)) {
929 case (DWORD) DIPROP_RANGE: {
930 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
931 int obj = find_property(&This->base.data_format, pdiph);
933 /* The app is querying the current range of the axis
934 * return the lMin and lMax values */
935 if (obj >= 0) {
936 pr->lMin = This->props[obj].lMin;
937 pr->lMax = This->props[obj].lMax;
938 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
939 return DI_OK;
941 break;
943 case (DWORD) DIPROP_DEADZONE: {
944 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
945 int obj = find_property(&This->base.data_format, pdiph);
947 if (obj >= 0) {
948 pd->dwData = This->props[obj].lDeadZone;
949 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
950 return DI_OK;
952 break;
954 case (DWORD) DIPROP_SATURATION: {
955 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
956 int obj = find_property(&This->base.data_format, pdiph);
958 if (obj >= 0) {
959 pd->dwData = This->props[obj].lSaturation;
960 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
961 return DI_OK;
963 break;
965 default:
966 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
970 return DI_OK;
973 /******************************************************************************
974 * GetObjectInfo : get object info
976 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
977 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
979 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
980 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
981 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
982 HRESULT res;
984 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
985 if (res != DI_OK) return res;
987 if (pdidoi->dwType & DIDFT_AXIS)
988 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
989 else if (pdidoi->dwType & DIDFT_POV)
990 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
991 else if (pdidoi->dwType & DIDFT_BUTTON)
992 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
994 _dump_OBJECTINSTANCEW(pdidoi);
995 return res;
998 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
999 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1001 HRESULT res;
1002 DIDEVICEOBJECTINSTANCEW didoiW;
1003 DWORD dwSize = pdidoi->dwSize;
1005 didoiW.dwSize = sizeof(didoiW);
1006 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1007 if (res != DI_OK) return res;
1009 memset(pdidoi, 0, pdidoi->dwSize);
1010 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1011 pdidoi->dwSize = dwSize;
1012 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1013 sizeof(pdidoi->tszName), NULL, NULL);
1015 return res;
1018 /******************************************************************************
1019 * GetDeviceInfo : get information about a device's identity
1021 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1022 LPDIRECTINPUTDEVICE8A iface,
1023 LPDIDEVICEINSTANCEA pdidi)
1025 JoystickImpl *This = (JoystickImpl *)iface;
1027 TRACE("(%p,%p)\n", iface, pdidi);
1029 if (pdidi == NULL) {
1030 WARN("invalid pointer\n");
1031 return E_POINTER;
1034 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1035 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1036 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1037 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1038 sizeof(DIDEVICEINSTANCEA));
1039 return DIERR_INVALIDPARAM;
1042 /* Return joystick */
1043 pdidi->guidInstance = GUID_Joystick;
1044 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1045 /* we only support traditional joysticks for now */
1046 pdidi->dwDevType = This->devcaps.dwDevType;
1047 strcpy(pdidi->tszInstanceName, "Joystick");
1048 strcpy(pdidi->tszProductName, This->name);
1049 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1050 pdidi->guidFFDriver = GUID_NULL;
1051 pdidi->wUsagePage = 0;
1052 pdidi->wUsage = 0;
1055 return DI_OK;
1058 /******************************************************************************
1059 * GetDeviceInfo : get information about a device's identity
1061 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1062 LPDIRECTINPUTDEVICE8W iface,
1063 LPDIDEVICEINSTANCEW pdidi)
1065 JoystickImpl *This = (JoystickImpl *)iface;
1067 TRACE("(%p,%p)\n", iface, pdidi);
1069 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1070 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1071 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1072 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1073 sizeof(DIDEVICEINSTANCEW));
1074 return DIERR_INVALIDPARAM;
1077 /* Return joystick */
1078 pdidi->guidInstance = GUID_Joystick;
1079 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1080 /* we only support traditional joysticks for now */
1081 pdidi->dwDevType = This->devcaps.dwDevType;
1082 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1083 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1084 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1085 pdidi->guidFFDriver = GUID_NULL;
1086 pdidi->wUsagePage = 0;
1087 pdidi->wUsage = 0;
1090 return DI_OK;
1093 static const IDirectInputDevice8AVtbl JoystickAvt =
1095 IDirectInputDevice2AImpl_QueryInterface,
1096 IDirectInputDevice2AImpl_AddRef,
1097 IDirectInputDevice2AImpl_Release,
1098 JoystickAImpl_GetCapabilities,
1099 IDirectInputDevice2AImpl_EnumObjects,
1100 JoystickAImpl_GetProperty,
1101 JoystickAImpl_SetProperty,
1102 JoystickAImpl_Acquire,
1103 JoystickAImpl_Unacquire,
1104 JoystickAImpl_GetDeviceState,
1105 IDirectInputDevice2AImpl_GetDeviceData,
1106 IDirectInputDevice2AImpl_SetDataFormat,
1107 IDirectInputDevice2AImpl_SetEventNotification,
1108 IDirectInputDevice2AImpl_SetCooperativeLevel,
1109 JoystickAImpl_GetObjectInfo,
1110 JoystickAImpl_GetDeviceInfo,
1111 IDirectInputDevice2AImpl_RunControlPanel,
1112 IDirectInputDevice2AImpl_Initialize,
1113 IDirectInputDevice2AImpl_CreateEffect,
1114 IDirectInputDevice2AImpl_EnumEffects,
1115 IDirectInputDevice2AImpl_GetEffectInfo,
1116 IDirectInputDevice2AImpl_GetForceFeedbackState,
1117 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1118 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1119 IDirectInputDevice2AImpl_Escape,
1120 JoystickAImpl_Poll,
1121 IDirectInputDevice2AImpl_SendDeviceData,
1122 IDirectInputDevice7AImpl_EnumEffectsInFile,
1123 IDirectInputDevice7AImpl_WriteEffectToFile,
1124 IDirectInputDevice8AImpl_BuildActionMap,
1125 IDirectInputDevice8AImpl_SetActionMap,
1126 IDirectInputDevice8AImpl_GetImageInfo
1129 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1130 # define XCAST(fun) (typeof(JoystickWvt.fun))
1131 #else
1132 # define XCAST(fun) (void*)
1133 #endif
1135 static const IDirectInputDevice8WVtbl JoystickWvt =
1137 IDirectInputDevice2WImpl_QueryInterface,
1138 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1139 XCAST(Release)IDirectInputDevice2AImpl_Release,
1140 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1141 IDirectInputDevice2WImpl_EnumObjects,
1142 XCAST(GetProperty)JoystickAImpl_GetProperty,
1143 XCAST(SetProperty)JoystickAImpl_SetProperty,
1144 XCAST(Acquire)JoystickAImpl_Acquire,
1145 XCAST(Unacquire)JoystickAImpl_Unacquire,
1146 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1147 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1148 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1149 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1150 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1151 IDirectInputDevice2WImpl_GetObjectInfo,
1152 JoystickWImpl_GetDeviceInfo,
1153 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1154 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1155 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1156 IDirectInputDevice2WImpl_EnumEffects,
1157 IDirectInputDevice2WImpl_GetEffectInfo,
1158 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1159 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1160 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1161 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1162 XCAST(Poll)JoystickAImpl_Poll,
1163 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1164 IDirectInputDevice7WImpl_EnumEffectsInFile,
1165 IDirectInputDevice7WImpl_WriteEffectToFile,
1166 IDirectInputDevice8WImpl_BuildActionMap,
1167 IDirectInputDevice8WImpl_SetActionMap,
1168 IDirectInputDevice8WImpl_GetImageInfo
1170 #undef XCAST
1172 #else /* HAVE_LINUX_22_JOYSTICK_API */
1174 const struct dinput_device joystick_linux_device = {
1175 "Wine Linux joystick driver",
1176 NULL,
1177 NULL,
1178 NULL,
1179 NULL
1182 #endif /* HAVE_LINUX_22_JOYSTICK_API */