Release 0.9.61.
[wine/gsoc-2012-control.git] / dlls / dinput / joystick_linux.c
blobdf5d77b69647da19aa8d2e4c381610c6b0f5cd83
1 /* DirectInput Joystick device
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998,1999 Lionel Ulmer
5 * Copyright 2000-2001 TransGaming Technologies Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * To Do:
24 * dead zone
25 * force feedback
28 #include "config.h"
29 #include "wine/port.h"
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <time.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 # include <sys/time.h>
40 #endif
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #include <errno.h>
46 #ifdef HAVE_SYS_ERRNO_H
47 # include <sys/errno.h>
48 #endif
49 #ifdef HAVE_LINUX_IOCTL_H
50 # include <linux/ioctl.h>
51 #endif
52 #ifdef HAVE_LINUX_JOYSTICK_H
53 # include <linux/joystick.h>
54 # undef SW_MAX
55 #endif
56 #ifdef HAVE_SYS_POLL_H
57 # include <sys/poll.h>
58 #endif
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
62 #include "windef.h"
63 #include "winbase.h"
64 #include "winerror.h"
65 #include "winreg.h"
66 #include "dinput.h"
68 #include "dinput_private.h"
69 #include "device_private.h"
71 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
73 #ifdef HAVE_LINUX_22_JOYSTICK_API
75 #define JOYDEV_NEW "/dev/input/js"
76 #define JOYDEV_OLD "/dev/js"
78 typedef struct JoystickImpl JoystickImpl;
79 static const IDirectInputDevice8AVtbl JoystickAvt;
80 static const IDirectInputDevice8WVtbl JoystickWvt;
81 struct JoystickImpl
83 struct IDirectInputDevice2AImpl base;
85 char dev[32];
87 /* joystick private */
88 int joyfd;
89 DIJOYSTATE2 js; /* wine data */
90 ObjProps *props;
91 char *name;
92 DIDEVCAPS devcaps;
93 LONG deadzone;
94 int *axis_map;
95 int axes;
96 int buttons;
97 POINTL povs[4];
100 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
101 0x9e573ed9,
102 0x7734,
103 0x11d2,
104 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
107 static void _dump_DIDEVCAPS(const DIDEVCAPS *lpDIDevCaps)
109 TRACE("dwSize: %d\n", lpDIDevCaps->dwSize);
110 TRACE("dwFlags: %08x\n", lpDIDevCaps->dwFlags);
111 TRACE("dwDevType: %08x %s\n", lpDIDevCaps->dwDevType,
112 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
113 lpDIDevCaps->dwDevType == DIDEVTYPE_DEVICE ? "DIDEVTYPE_DEVICE" :
114 lpDIDevCaps->dwDevType == DIDEVTYPE_MOUSE ? "DIDEVTYPE_MOUSE" :
115 lpDIDevCaps->dwDevType == DIDEVTYPE_KEYBOARD ? "DIDEVTYPE_KEYBOARD" :
116 lpDIDevCaps->dwDevType == DIDEVTYPE_JOYSTICK ? "DIDEVTYPE_JOYSTICK" :
117 lpDIDevCaps->dwDevType == DIDEVTYPE_HID ? "DIDEVTYPE_HID" : "UNKNOWN");
118 TRACE("dwAxes: %d\n", lpDIDevCaps->dwAxes);
119 TRACE("dwButtons: %d\n", lpDIDevCaps->dwButtons);
120 TRACE("dwPOVs: %d\n", lpDIDevCaps->dwPOVs);
121 if (lpDIDevCaps->dwSize > sizeof(DIDEVCAPS_DX3)) {
122 TRACE("dwFFSamplePeriod: %d\n", lpDIDevCaps->dwFFSamplePeriod);
123 TRACE("dwFFMinTimeResolution: %d\n", lpDIDevCaps->dwFFMinTimeResolution);
124 TRACE("dwFirmwareRevision: %d\n", lpDIDevCaps->dwFirmwareRevision);
125 TRACE("dwHardwareRevision: %d\n", lpDIDevCaps->dwHardwareRevision);
126 TRACE("dwFFDriverVersion: %d\n", lpDIDevCaps->dwFFDriverVersion);
130 #define MAX_JOYSTICKS 64
131 static INT joystick_devices_count = -1;
132 static LPSTR joystick_devices[MAX_JOYSTICKS];
134 static INT find_joystick_devices(void)
136 INT i;
138 if (joystick_devices_count != -1) return joystick_devices_count;
140 joystick_devices_count = 0;
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 close(fd);
156 if (!(str = HeapAlloc(GetProcessHeap(), 0, len))) break;
157 memcpy(str, device_name, len);
159 joystick_devices[joystick_devices_count++] = str;
162 return joystick_devices_count;
165 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
167 int fd = -1;
169 if (id >= find_joystick_devices()) return FALSE;
171 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
172 WARN("force feedback not supported\n");
173 return FALSE;
176 if ((dwDevType == 0) ||
177 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
178 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
179 /* check whether we have a joystick */
180 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
182 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
183 return FALSE;
186 /* Return joystick */
187 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
188 lpddi->guidInstance.Data3 = id;
189 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
190 /* we only support traditional joysticks for now */
191 if (version >= 0x0800)
192 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
193 else
194 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
195 sprintf(lpddi->tszInstanceName, "Joystick %d", id);
196 #if defined(JSIOCGNAME)
197 if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
198 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
199 strcpy(lpddi->tszProductName, "Wine Joystick");
201 #else
202 strcpy(lpddi->tszProductName, "Wine Joystick");
203 #endif
205 lpddi->guidFFDriver = GUID_NULL;
206 close(fd);
207 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], lpddi->tszProductName);
208 return TRUE;
211 return FALSE;
214 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
216 int fd = -1;
217 char name[MAX_PATH];
218 char friendly[32];
220 if (id >= find_joystick_devices()) return FALSE;
222 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
223 WARN("force feedback not supported\n");
224 return FALSE;
227 if ((dwDevType == 0) ||
228 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
229 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
230 /* check whether we have a joystick */
231 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
233 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
234 return FALSE;
237 /* Return joystick */
238 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
239 lpddi->guidInstance.Data3 = id;
240 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
241 /* we only support traditional joysticks for now */
242 if (version >= 0x0800)
243 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
244 else
245 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
246 sprintf(friendly, "Joystick %d", id);
247 MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
248 #if defined(JSIOCGNAME)
249 if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
250 WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
251 strcpy(name, "Wine Joystick");
253 #else
254 strcpy(name, "Wine Joystick");
255 #endif
256 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
257 lpddi->guidFFDriver = GUID_NULL;
258 close(fd);
259 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], name);
260 return TRUE;
263 return FALSE;
267 * Setup the dinput options.
270 static HRESULT setup_dinput_options(JoystickImpl * device)
272 char buffer[MAX_PATH+16];
273 HKEY hkey, appkey;
274 int tokens = 0;
275 int axis = 0;
276 int pov = 0;
278 buffer[MAX_PATH]='\0';
280 get_app_key(&hkey, &appkey);
282 /* get options */
284 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
285 device->deadzone = atoi(buffer);
286 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
289 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
290 if (!device->axis_map) return DIERR_OUTOFMEMORY;
292 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
293 const char *delim = ",";
294 char * ptr;
295 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
297 if ((ptr = strtok(buffer, delim)) != NULL) {
298 do {
299 if (strcmp(ptr, "X") == 0) {
300 device->axis_map[tokens] = 0;
301 axis++;
302 } else if (strcmp(ptr, "Y") == 0) {
303 device->axis_map[tokens] = 1;
304 axis++;
305 } else if (strcmp(ptr, "Z") == 0) {
306 device->axis_map[tokens] = 2;
307 axis++;
308 } else if (strcmp(ptr, "Rx") == 0) {
309 device->axis_map[tokens] = 3;
310 axis++;
311 } else if (strcmp(ptr, "Ry") == 0) {
312 device->axis_map[tokens] = 4;
313 axis++;
314 } else if (strcmp(ptr, "Rz") == 0) {
315 device->axis_map[tokens] = 5;
316 axis++;
317 } else if (strcmp(ptr, "Slider1") == 0) {
318 device->axis_map[tokens] = 6;
319 axis++;
320 } else if (strcmp(ptr, "Slider2") == 0) {
321 device->axis_map[tokens] = 7;
322 axis++;
323 } else if (strcmp(ptr, "POV1") == 0) {
324 device->axis_map[tokens++] = 8;
325 device->axis_map[tokens] = 8;
326 pov++;
327 } else if (strcmp(ptr, "POV2") == 0) {
328 device->axis_map[tokens++] = 9;
329 device->axis_map[tokens] = 9;
330 pov++;
331 } else if (strcmp(ptr, "POV3") == 0) {
332 device->axis_map[tokens++] = 10;
333 device->axis_map[tokens] = 10;
334 pov++;
335 } else if (strcmp(ptr, "POV4") == 0) {
336 device->axis_map[tokens++] = 11;
337 device->axis_map[tokens] = 11;
338 pov++;
339 } else {
340 ERR("invalid joystick axis type: %s\n", ptr);
341 device->axis_map[tokens] = tokens;
342 axis++;
345 tokens++;
346 } while ((ptr = strtok(NULL, delim)) != NULL);
348 if (tokens != device->devcaps.dwAxes) {
349 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
350 while (tokens < device->axes) {
351 device->axis_map[tokens] = tokens;
352 tokens++;
358 else
360 for (tokens = 0; tokens < device->axes; tokens++)
362 if (tokens < 8)
363 device->axis_map[tokens] = axis++;
364 else
366 device->axis_map[tokens++] = 8 + pov;
367 device->axis_map[tokens ] = 8 + pov++;
371 device->devcaps.dwAxes = axis;
372 device->devcaps.dwPOVs = pov;
374 if (appkey)
375 RegCloseKey( appkey );
377 if (hkey)
378 RegCloseKey( hkey );
380 return DI_OK;
383 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
384 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
386 DWORD i;
387 JoystickImpl* newDevice;
388 char name[MAX_PATH];
389 HRESULT hr;
390 LPDIDATAFORMAT df = NULL;
391 int idx = 0;
393 TRACE("%s %p %p %p %hu\n", debugstr_guid(rguid), jvt, dinput, pdev, index);
395 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
396 if (newDevice == 0) {
397 WARN("out of memory\n");
398 *pdev = 0;
399 return DIERR_OUTOFMEMORY;
402 if (!lstrcpynA(newDevice->dev, joystick_devices[index], sizeof(newDevice->dev)) ||
403 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
405 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
406 HeapFree(GetProcessHeap(), 0, newDevice);
407 return DIERR_DEVICENOTREG;
410 /* get the device name */
411 #if defined(JSIOCGNAME)
412 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
413 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
414 strcpy(name, "Wine Joystick");
416 #else
417 strcpy(name, "Wine Joystick");
418 #endif
420 /* copy the device name */
421 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
422 strcpy(newDevice->name, name);
424 #ifdef JSIOCGAXES
425 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
426 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
427 newDevice->axes = 2;
429 #endif
430 #ifdef JSIOCGBUTTONS
431 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
432 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
433 newDevice->buttons = 2;
435 #endif
437 if (newDevice->axes > 16)
439 /* There are 24 more axes for velocity that we can use */
440 FIXME("Can't support %d axes. Clamping down to 16\n", newDevice->axes);
441 newDevice->axes = 16;
444 if (newDevice->buttons > 128)
446 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->buttons);
447 newDevice->buttons = 128;
450 newDevice->base.lpVtbl = jvt;
451 newDevice->base.ref = 1;
452 newDevice->base.dinput = dinput;
453 newDevice->base.guid = *rguid;
454 InitializeCriticalSection(&newDevice->base.crit);
455 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
457 /* setup_dinput_options may change these */
458 newDevice->deadzone = 0;
459 newDevice->devcaps.dwButtons = newDevice->buttons;
461 /* do any user specified configuration */
462 hr = setup_dinput_options(newDevice);
463 if (hr != DI_OK)
464 goto FAILED1;
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 df->dwNumObjs = newDevice->devcaps.dwAxes + newDevice->devcaps.dwPOVs + newDevice->buttons;
471 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
473 for (i = 0; i < newDevice->axes; i++)
475 int wine_obj = newDevice->axis_map[i];
477 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
478 if (wine_obj < 8)
479 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
480 else
482 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
483 i++; /* POV takes 2 axes */
486 for (i = 0; i < newDevice->buttons; i++)
488 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
489 df->rgodf[idx ].pguid = &GUID_Button;
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 TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
574 find_joystick_devices();
575 *pdev = NULL;
577 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
578 joystick_devices_count && index < joystick_devices_count)
580 if ((riid == NULL) ||
581 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
582 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
583 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
584 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
586 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
589 WARN("no interface\n");
590 return DIERR_NOINTERFACE;
593 return DIERR_DEVICENOTREG;
596 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
598 unsigned short index;
600 TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
601 find_joystick_devices();
602 *pdev = NULL;
604 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
605 joystick_devices_count && index < joystick_devices_count)
607 if ((riid == NULL) ||
608 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
609 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
610 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
611 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
613 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
615 WARN("no interface\n");
616 return DIERR_NOINTERFACE;
619 WARN("invalid device GUID %s\n",debugstr_guid(rguid));
620 return DIERR_DEVICENOTREG;
623 #undef MAX_JOYSTICKS
625 const struct dinput_device joystick_linux_device = {
626 "Wine Linux joystick driver",
627 joydev_enum_deviceA,
628 joydev_enum_deviceW,
629 joydev_create_deviceA,
630 joydev_create_deviceW
633 /******************************************************************************
634 * Acquire : gets exclusive control of the joystick
636 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
638 JoystickImpl *This = (JoystickImpl *)iface;
640 TRACE("(%p)\n",This);
642 if (This->base.acquired) {
643 WARN("already acquired\n");
644 return S_FALSE;
647 /* open the joystick device */
648 if (This->joyfd==-1) {
649 TRACE("opening joystick device %s\n", This->dev);
651 This->joyfd=open(This->dev,O_RDONLY);
652 if (This->joyfd==-1) {
653 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
654 return DIERR_NOTFOUND;
658 This->base.acquired = 1;
660 return DI_OK;
663 /******************************************************************************
664 * Unacquire : frees the joystick
666 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
668 JoystickImpl *This = (JoystickImpl *)iface;
669 HRESULT res;
671 TRACE("(%p)\n",This);
673 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
675 if (This->joyfd!=-1) {
676 TRACE("closing joystick device\n");
677 close(This->joyfd);
678 This->joyfd = -1;
679 return DI_OK;
682 return DI_NOEFFECT;
685 static void joy_polldev(JoystickImpl *This) {
686 struct pollfd plfd;
687 struct js_event jse;
688 TRACE("(%p)\n", This);
690 if (This->joyfd==-1) {
691 WARN("no device\n");
692 return;
694 while (1)
696 LONG value;
697 int inst_id = -1;
699 plfd.fd = This->joyfd;
700 plfd.events = POLLIN;
701 if (poll(&plfd,1,0) != 1)
702 return;
703 /* we have one event, so we can read */
704 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
705 return;
707 TRACE("js_event: type 0x%x, number %d, value %d\n",
708 jse.type,jse.number,jse.value);
709 if (jse.type & JS_EVENT_BUTTON)
711 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
712 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
714 else if (jse.type & JS_EVENT_AXIS)
716 int number = This->axis_map[jse.number]; /* wine format object index */
718 if (number < 0) return;
719 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
720 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
722 TRACE("changing axis %d => %d\n", jse.number, number);
723 switch (number)
725 case 0: This->js.lX = value; break;
726 case 1: This->js.lY = value; break;
727 case 2: This->js.lZ = value; break;
728 case 3: This->js.lRx = value; break;
729 case 4: This->js.lRy = value; break;
730 case 5: This->js.lRz = value; break;
731 case 6: This->js.rglSlider[0] = value; break;
732 case 7: This->js.rglSlider[1] = value; break;
733 case 8: case 9: case 10: case 11:
735 int idx = number - 8;
737 if (jse.number % 2)
738 This->povs[idx].y = jse.value;
739 else
740 This->povs[idx].x = jse.value;
742 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
743 break;
745 default:
746 WARN("axis %d not supported\n", number);
749 if (inst_id >= 0)
750 queue_event((LPDIRECTINPUTDEVICE8A)This,
751 id_to_offset(&This->base.data_format, inst_id),
752 value, jse.time, This->base.dinput->evsequence++);
756 /******************************************************************************
757 * GetDeviceState : returns the "state" of the joystick.
760 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
761 LPDIRECTINPUTDEVICE8A iface,
762 DWORD len,
763 LPVOID ptr)
765 JoystickImpl *This = (JoystickImpl *)iface;
767 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
769 if (!This->base.acquired) {
770 WARN("not acquired\n");
771 return DIERR_NOTACQUIRED;
774 /* update joystick state */
775 joy_polldev(This);
777 /* convert and copy data to user supplied buffer */
778 fill_DataFormat(ptr, &This->js, &This->base.data_format);
780 return DI_OK;
783 /******************************************************************************
784 * SetProperty : change input device properties
786 static HRESULT WINAPI JoystickAImpl_SetProperty(
787 LPDIRECTINPUTDEVICE8A iface,
788 REFGUID rguid,
789 LPCDIPROPHEADER ph)
791 JoystickImpl *This = (JoystickImpl *)iface;
792 int i;
794 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
796 if (ph == NULL) {
797 WARN("invalid parameter: ph == NULL\n");
798 return DIERR_INVALIDPARAM;
801 if (TRACE_ON(dinput))
802 _dump_DIPROPHEADER(ph);
804 if (!HIWORD(rguid)) {
805 switch (LOWORD(rguid)) {
806 case (DWORD)DIPROP_RANGE: {
807 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
808 if (ph->dwHow == DIPH_DEVICE) {
809 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
810 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
811 This->props[i].lMin = pr->lMin;
812 This->props[i].lMax = pr->lMax;
814 } else {
815 int obj = find_property(&This->base.data_format, ph);
817 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
818 if (obj >= 0) {
819 This->props[obj].lMin = pr->lMin;
820 This->props[obj].lMax = pr->lMax;
821 return DI_OK;
824 break;
826 case (DWORD)DIPROP_DEADZONE: {
827 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
828 if (ph->dwHow == DIPH_DEVICE) {
829 TRACE("deadzone(%d) all\n", pd->dwData);
830 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
831 This->props[i].lDeadZone = pd->dwData;
832 } else {
833 int obj = find_property(&This->base.data_format, ph);
835 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
836 if (obj >= 0) {
837 This->props[obj].lDeadZone = pd->dwData;
838 return DI_OK;
841 break;
843 case (DWORD)DIPROP_SATURATION: {
844 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
845 if (ph->dwHow == DIPH_DEVICE) {
846 TRACE("saturation(%d) all\n", pd->dwData);
847 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
848 This->props[i].lSaturation = pd->dwData;
849 } else {
850 int obj = find_property(&This->base.data_format, ph);
852 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
853 if (obj >= 0) {
854 This->props[obj].lSaturation = pd->dwData;
855 return DI_OK;
858 break;
860 default:
861 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
865 return DI_OK;
868 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
869 LPDIRECTINPUTDEVICE8A iface,
870 LPDIDEVCAPS lpDIDevCaps)
872 JoystickImpl *This = (JoystickImpl *)iface;
873 int size;
875 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
877 if (lpDIDevCaps == NULL) {
878 WARN("invalid pointer\n");
879 return E_POINTER;
882 size = lpDIDevCaps->dwSize;
884 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
885 WARN("invalid parameter\n");
886 return DIERR_INVALIDPARAM;
889 CopyMemory(lpDIDevCaps, &This->devcaps, size);
890 lpDIDevCaps->dwSize = size;
892 if (TRACE_ON(dinput))
893 _dump_DIDEVCAPS(lpDIDevCaps);
895 return DI_OK;
898 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
900 JoystickImpl *This = (JoystickImpl *)iface;
902 TRACE("(%p)\n",This);
904 if (!This->base.acquired) {
905 WARN("not acquired\n");
906 return DIERR_NOTACQUIRED;
909 joy_polldev(This);
910 return DI_OK;
913 /******************************************************************************
914 * GetProperty : get input device properties
916 static HRESULT WINAPI JoystickAImpl_GetProperty(
917 LPDIRECTINPUTDEVICE8A iface,
918 REFGUID rguid,
919 LPDIPROPHEADER pdiph)
921 JoystickImpl *This = (JoystickImpl *)iface;
923 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
925 if (TRACE_ON(dinput))
926 _dump_DIPROPHEADER(pdiph);
928 if (!HIWORD(rguid)) {
929 switch (LOWORD(rguid)) {
930 case (DWORD) DIPROP_RANGE: {
931 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
932 int obj = find_property(&This->base.data_format, pdiph);
934 /* The app is querying the current range of the axis
935 * return the lMin and lMax values */
936 if (obj >= 0) {
937 pr->lMin = This->props[obj].lMin;
938 pr->lMax = This->props[obj].lMax;
939 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
940 return DI_OK;
942 break;
944 case (DWORD) DIPROP_DEADZONE: {
945 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
946 int obj = find_property(&This->base.data_format, pdiph);
948 if (obj >= 0) {
949 pd->dwData = This->props[obj].lDeadZone;
950 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
951 return DI_OK;
953 break;
955 case (DWORD) DIPROP_SATURATION: {
956 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
957 int obj = find_property(&This->base.data_format, pdiph);
959 if (obj >= 0) {
960 pd->dwData = This->props[obj].lSaturation;
961 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
962 return DI_OK;
964 break;
966 default:
967 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
971 return DI_OK;
974 /******************************************************************************
975 * GetObjectInfo : get object info
977 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
978 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
980 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
981 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
982 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
983 HRESULT res;
985 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
986 if (res != DI_OK) return res;
988 if (pdidoi->dwType & DIDFT_AXIS)
989 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
990 else if (pdidoi->dwType & DIDFT_POV)
991 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
992 else if (pdidoi->dwType & DIDFT_BUTTON)
993 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
995 _dump_OBJECTINSTANCEW(pdidoi);
996 return res;
999 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
1000 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
1002 HRESULT res;
1003 DIDEVICEOBJECTINSTANCEW didoiW;
1004 DWORD dwSize = pdidoi->dwSize;
1006 didoiW.dwSize = sizeof(didoiW);
1007 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
1008 if (res != DI_OK) return res;
1010 memset(pdidoi, 0, pdidoi->dwSize);
1011 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
1012 pdidoi->dwSize = dwSize;
1013 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
1014 sizeof(pdidoi->tszName), NULL, NULL);
1016 return res;
1019 /******************************************************************************
1020 * GetDeviceInfo : get information about a device's identity
1022 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1023 LPDIRECTINPUTDEVICE8A iface,
1024 LPDIDEVICEINSTANCEA pdidi)
1026 JoystickImpl *This = (JoystickImpl *)iface;
1028 TRACE("(%p,%p)\n", iface, pdidi);
1030 if (pdidi == NULL) {
1031 WARN("invalid pointer\n");
1032 return E_POINTER;
1035 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1036 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1037 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1038 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1039 sizeof(DIDEVICEINSTANCEA));
1040 return DIERR_INVALIDPARAM;
1043 /* Return joystick */
1044 pdidi->guidInstance = GUID_Joystick;
1045 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1046 /* we only support traditional joysticks for now */
1047 pdidi->dwDevType = This->devcaps.dwDevType;
1048 strcpy(pdidi->tszInstanceName, "Joystick");
1049 strcpy(pdidi->tszProductName, This->name);
1050 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1051 pdidi->guidFFDriver = GUID_NULL;
1052 pdidi->wUsagePage = 0;
1053 pdidi->wUsage = 0;
1056 return DI_OK;
1059 /******************************************************************************
1060 * GetDeviceInfo : get information about a device's identity
1062 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1063 LPDIRECTINPUTDEVICE8W iface,
1064 LPDIDEVICEINSTANCEW pdidi)
1066 JoystickImpl *This = (JoystickImpl *)iface;
1068 TRACE("(%p,%p)\n", iface, pdidi);
1070 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1071 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1072 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1073 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1074 sizeof(DIDEVICEINSTANCEW));
1075 return DIERR_INVALIDPARAM;
1078 /* Return joystick */
1079 pdidi->guidInstance = GUID_Joystick;
1080 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1081 /* we only support traditional joysticks for now */
1082 pdidi->dwDevType = This->devcaps.dwDevType;
1083 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1084 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1085 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1086 pdidi->guidFFDriver = GUID_NULL;
1087 pdidi->wUsagePage = 0;
1088 pdidi->wUsage = 0;
1091 return DI_OK;
1094 static const IDirectInputDevice8AVtbl JoystickAvt =
1096 IDirectInputDevice2AImpl_QueryInterface,
1097 IDirectInputDevice2AImpl_AddRef,
1098 IDirectInputDevice2AImpl_Release,
1099 JoystickAImpl_GetCapabilities,
1100 IDirectInputDevice2AImpl_EnumObjects,
1101 JoystickAImpl_GetProperty,
1102 JoystickAImpl_SetProperty,
1103 JoystickAImpl_Acquire,
1104 JoystickAImpl_Unacquire,
1105 JoystickAImpl_GetDeviceState,
1106 IDirectInputDevice2AImpl_GetDeviceData,
1107 IDirectInputDevice2AImpl_SetDataFormat,
1108 IDirectInputDevice2AImpl_SetEventNotification,
1109 IDirectInputDevice2AImpl_SetCooperativeLevel,
1110 JoystickAImpl_GetObjectInfo,
1111 JoystickAImpl_GetDeviceInfo,
1112 IDirectInputDevice2AImpl_RunControlPanel,
1113 IDirectInputDevice2AImpl_Initialize,
1114 IDirectInputDevice2AImpl_CreateEffect,
1115 IDirectInputDevice2AImpl_EnumEffects,
1116 IDirectInputDevice2AImpl_GetEffectInfo,
1117 IDirectInputDevice2AImpl_GetForceFeedbackState,
1118 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1119 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1120 IDirectInputDevice2AImpl_Escape,
1121 JoystickAImpl_Poll,
1122 IDirectInputDevice2AImpl_SendDeviceData,
1123 IDirectInputDevice7AImpl_EnumEffectsInFile,
1124 IDirectInputDevice7AImpl_WriteEffectToFile,
1125 IDirectInputDevice8AImpl_BuildActionMap,
1126 IDirectInputDevice8AImpl_SetActionMap,
1127 IDirectInputDevice8AImpl_GetImageInfo
1130 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1131 # define XCAST(fun) (typeof(JoystickWvt.fun))
1132 #else
1133 # define XCAST(fun) (void*)
1134 #endif
1136 static const IDirectInputDevice8WVtbl JoystickWvt =
1138 IDirectInputDevice2WImpl_QueryInterface,
1139 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1140 XCAST(Release)IDirectInputDevice2AImpl_Release,
1141 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1142 IDirectInputDevice2WImpl_EnumObjects,
1143 XCAST(GetProperty)JoystickAImpl_GetProperty,
1144 XCAST(SetProperty)JoystickAImpl_SetProperty,
1145 XCAST(Acquire)JoystickAImpl_Acquire,
1146 XCAST(Unacquire)JoystickAImpl_Unacquire,
1147 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1148 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1149 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1150 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1151 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1152 IDirectInputDevice2WImpl_GetObjectInfo,
1153 JoystickWImpl_GetDeviceInfo,
1154 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1155 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1156 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1157 IDirectInputDevice2WImpl_EnumEffects,
1158 IDirectInputDevice2WImpl_GetEffectInfo,
1159 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1160 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1161 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1162 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1163 XCAST(Poll)JoystickAImpl_Poll,
1164 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1165 IDirectInputDevice7WImpl_EnumEffectsInFile,
1166 IDirectInputDevice7WImpl_WriteEffectToFile,
1167 IDirectInputDevice8WImpl_BuildActionMap,
1168 IDirectInputDevice8WImpl_SetActionMap,
1169 IDirectInputDevice8WImpl_GetImageInfo
1171 #undef XCAST
1173 #else /* HAVE_LINUX_22_JOYSTICK_API */
1175 const struct dinput_device joystick_linux_device = {
1176 "Wine Linux joystick driver",
1177 NULL,
1178 NULL,
1179 NULL,
1180 NULL
1183 #endif /* HAVE_LINUX_22_JOYSTICK_API */