push 97f44e0adb27fff75ba63d8fb97c65db9edfbe82
[wine/hacks.git] / dlls / dinput / joystick_linux.c
blobbb967b9463bb205255e08b73b5cc8522d8c5cdf5
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 <sys/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 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 * Setup the dinput options.
268 static HRESULT setup_dinput_options(JoystickImpl * device)
270 char buffer[MAX_PATH+16];
271 HKEY hkey, appkey;
273 buffer[MAX_PATH]='\0';
275 get_app_key(&hkey, &appkey);
277 /* get options */
279 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
280 device->deadzone = atoi(buffer);
281 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
284 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
285 int tokens = 0;
286 int axis = 0;
287 int pov = 0;
288 const char *delim = ",";
289 char * ptr;
290 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
292 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
293 if (device->axis_map == 0)
294 return DIERR_OUTOFMEMORY;
296 if ((ptr = strtok(buffer, delim)) != NULL) {
297 do {
298 if (strcmp(ptr, "X") == 0) {
299 device->axis_map[tokens] = 0;
300 axis++;
301 } else if (strcmp(ptr, "Y") == 0) {
302 device->axis_map[tokens] = 1;
303 axis++;
304 } else if (strcmp(ptr, "Z") == 0) {
305 device->axis_map[tokens] = 2;
306 axis++;
307 } else if (strcmp(ptr, "Rx") == 0) {
308 device->axis_map[tokens] = 3;
309 axis++;
310 } else if (strcmp(ptr, "Ry") == 0) {
311 device->axis_map[tokens] = 4;
312 axis++;
313 } else if (strcmp(ptr, "Rz") == 0) {
314 device->axis_map[tokens] = 5;
315 axis++;
316 } else if (strcmp(ptr, "Slider1") == 0) {
317 device->axis_map[tokens] = 6;
318 axis++;
319 } else if (strcmp(ptr, "Slider2") == 0) {
320 device->axis_map[tokens] = 7;
321 axis++;
322 } else if (strcmp(ptr, "POV1") == 0) {
323 device->axis_map[tokens++] = 8;
324 device->axis_map[tokens] = 8;
325 pov++;
326 } else if (strcmp(ptr, "POV2") == 0) {
327 device->axis_map[tokens++] = 9;
328 device->axis_map[tokens] = 9;
329 pov++;
330 } else if (strcmp(ptr, "POV3") == 0) {
331 device->axis_map[tokens++] = 10;
332 device->axis_map[tokens] = 10;
333 pov++;
334 } else if (strcmp(ptr, "POV4") == 0) {
335 device->axis_map[tokens++] = 11;
336 device->axis_map[tokens] = 11;
337 pov++;
338 } else {
339 ERR("invalid joystick axis type: %s\n", ptr);
340 device->axis_map[tokens] = tokens;
341 axis++;
344 tokens++;
345 } while ((ptr = strtok(NULL, delim)) != NULL);
347 if (tokens != device->devcaps.dwAxes) {
348 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
349 while (tokens < device->axes) {
350 device->axis_map[tokens] = tokens;
351 tokens++;
356 device->devcaps.dwAxes = axis;
357 device->devcaps.dwPOVs = pov;
360 if (appkey)
361 RegCloseKey( appkey );
363 if (hkey)
364 RegCloseKey( hkey );
366 return DI_OK;
369 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
370 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
372 DWORD i;
373 JoystickImpl* newDevice;
374 char name[MAX_PATH];
375 HRESULT hr;
376 LPDIDATAFORMAT df = NULL;
377 int idx = 0;
379 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
380 if (newDevice == 0) {
381 WARN("out of memory\n");
382 *pdev = 0;
383 return DIERR_OUTOFMEMORY;
386 if (!lstrcpynA(newDevice->dev, joystick_devices[index], sizeof(newDevice->dev)) ||
387 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
389 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
390 HeapFree(GetProcessHeap(), 0, newDevice);
391 return DIERR_DEVICENOTREG;
394 /* get the device name */
395 #if defined(JSIOCGNAME)
396 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
397 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
398 strcpy(name, "Wine Joystick");
400 #else
401 strcpy(name, "Wine Joystick");
402 #endif
404 /* copy the device name */
405 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
406 strcpy(newDevice->name, name);
408 #ifdef JSIOCGAXES
409 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
410 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
411 newDevice->axes = 2;
413 #endif
414 #ifdef JSIOCGBUTTONS
415 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
416 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
417 newDevice->buttons = 2;
419 #endif
421 newDevice->base.lpVtbl = jvt;
422 newDevice->base.ref = 1;
423 newDevice->base.dinput = dinput;
424 CopyMemory(&newDevice->base.guid, rguid, sizeof(*rguid));
425 InitializeCriticalSection(&newDevice->base.crit);
426 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
428 /* setup_dinput_options may change these */
429 newDevice->deadzone = 0;
430 newDevice->devcaps.dwButtons = newDevice->buttons;
431 newDevice->devcaps.dwAxes = newDevice->axes;
432 newDevice->devcaps.dwPOVs = 0;
434 /* do any user specified configuration */
435 hr = setup_dinput_options(newDevice);
436 if (hr != DI_OK)
437 goto FAILED1;
439 if (newDevice->axis_map == 0) {
440 newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
441 if (newDevice->axis_map == 0)
442 goto FAILED;
444 for (i = 0; i < newDevice->axes; i++)
445 newDevice->axis_map[i] = i;
448 /* Create copy of default data format */
449 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
450 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
452 /* Axes include POVs */
453 df->dwNumObjs = newDevice->axes + newDevice->buttons;
454 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
456 for (i = 0; i < newDevice->axes; i++)
458 int wine_obj = newDevice->axis_map[i];
460 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
461 if (wine_obj < 8)
462 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
463 else
465 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
466 i++; /* POV takes 2 axes */
469 for (i = 0; i < newDevice->buttons; i++)
471 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
472 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
474 newDevice->base.data_format.wine_df = df;
476 /* create default properties */
477 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
478 if (newDevice->props == 0)
479 goto FAILED;
481 /* initialize default properties */
482 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
483 newDevice->props[i].lDevMin = -32767;
484 newDevice->props[i].lDevMax = +32767;
485 newDevice->props[i].lMin = 0;
486 newDevice->props[i].lMax = 0xffff;
487 newDevice->props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
488 newDevice->props[i].lSaturation = 0;
491 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
493 newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
494 newDevice->devcaps.dwFlags = DIDC_ATTACHED;
495 if (newDevice->base.dinput->dwVersion >= 0x0800)
496 newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
497 else
498 newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
499 newDevice->devcaps.dwFFSamplePeriod = 0;
500 newDevice->devcaps.dwFFMinTimeResolution = 0;
501 newDevice->devcaps.dwFirmwareRevision = 0;
502 newDevice->devcaps.dwHardwareRevision = 0;
503 newDevice->devcaps.dwFFDriverVersion = 0;
505 if (TRACE_ON(dinput)) {
506 _dump_DIDATAFORMAT(newDevice->base.data_format.wine_df);
507 for (i = 0; i < (newDevice->axes); i++)
508 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
509 _dump_DIDEVCAPS(&newDevice->devcaps);
512 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
514 return DI_OK;
516 FAILED:
517 hr = DIERR_OUTOFMEMORY;
518 FAILED1:
519 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
520 HeapFree(GetProcessHeap(), 0, df);
521 release_DataFormat(&newDevice->base.data_format);
522 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
523 HeapFree(GetProcessHeap(),0,newDevice->name);
524 HeapFree(GetProcessHeap(),0,newDevice->props);
525 HeapFree(GetProcessHeap(),0,newDevice);
526 *pdev = 0;
528 return hr;
531 /******************************************************************************
532 * get_joystick_index : Get the joystick index from a given GUID
534 static unsigned short get_joystick_index(REFGUID guid)
536 GUID wine_joystick = DInput_Wine_Joystick_GUID;
537 GUID dev_guid = *guid;
539 wine_joystick.Data3 = 0;
540 dev_guid.Data3 = 0;
542 /* for the standard joystick GUID use index 0 */
543 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
545 /* for the wine joystick GUIDs use the index stored in Data3 */
546 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
548 return MAX_JOYSTICKS;
551 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
553 unsigned short index;
555 find_joystick_devices();
556 *pdev = NULL;
558 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
559 joystick_devices_count && index < joystick_devices_count)
561 if ((riid == NULL) ||
562 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
563 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
564 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
565 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
567 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
570 WARN("no interface\n");
571 return DIERR_NOINTERFACE;
574 WARN("invalid device GUID\n");
575 return DIERR_DEVICENOTREG;
578 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
580 unsigned short index;
582 find_joystick_devices();
583 *pdev = NULL;
585 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
586 joystick_devices_count && index < joystick_devices_count)
588 if ((riid == NULL) ||
589 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
590 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
591 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
592 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
594 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
596 WARN("no interface\n");
597 return DIERR_NOINTERFACE;
600 WARN("invalid device GUID\n");
601 return DIERR_DEVICENOTREG;
604 #undef MAX_JOYSTICKS
606 const struct dinput_device joystick_linux_device = {
607 "Wine Linux joystick driver",
608 joydev_enum_deviceA,
609 joydev_enum_deviceW,
610 joydev_create_deviceA,
611 joydev_create_deviceW
614 /******************************************************************************
615 * Acquire : gets exclusive control of the joystick
617 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
619 JoystickImpl *This = (JoystickImpl *)iface;
621 TRACE("(%p)\n",This);
623 if (This->base.acquired) {
624 WARN("already acquired\n");
625 return S_FALSE;
628 /* open the joystick device */
629 if (This->joyfd==-1) {
630 TRACE("opening joystick device %s\n", This->dev);
632 This->joyfd=open(This->dev,O_RDONLY);
633 if (This->joyfd==-1) {
634 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
635 return DIERR_NOTFOUND;
639 This->base.acquired = 1;
641 return DI_OK;
644 /******************************************************************************
645 * Unacquire : frees the joystick
647 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
649 JoystickImpl *This = (JoystickImpl *)iface;
650 HRESULT res;
652 TRACE("(%p)\n",This);
654 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
656 if (This->joyfd!=-1) {
657 TRACE("closing joystick device\n");
658 close(This->joyfd);
659 This->joyfd = -1;
660 return DI_OK;
663 return DI_NOEFFECT;
666 static void joy_polldev(JoystickImpl *This) {
667 struct pollfd plfd;
668 struct js_event jse;
669 TRACE("(%p)\n", This);
671 if (This->joyfd==-1) {
672 WARN("no device\n");
673 return;
675 while (1)
677 LONG value;
678 int inst_id = -1;
680 plfd.fd = This->joyfd;
681 plfd.events = POLLIN;
682 if (poll(&plfd,1,0) != 1)
683 return;
684 /* we have one event, so we can read */
685 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
686 return;
688 TRACE("js_event: type 0x%x, number %d, value %d\n",
689 jse.type,jse.number,jse.value);
690 if (jse.type & JS_EVENT_BUTTON)
692 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
693 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
695 else if (jse.type & JS_EVENT_AXIS)
697 int number = This->axis_map[jse.number]; /* wine format object index */
699 if (number < 0) return;
700 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
701 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
703 TRACE("changing axis %d => %d\n", jse.number, number);
704 switch (number)
706 case 0: This->js.lX = value; break;
707 case 1: This->js.lY = value; break;
708 case 2: This->js.lZ = value; break;
709 case 3: This->js.lRx = value; break;
710 case 4: This->js.lRy = value; break;
711 case 5: This->js.lRz = value; break;
712 case 6: This->js.rglSlider[0] = value; break;
713 case 7: This->js.rglSlider[1] = value; break;
714 case 8: case 9: case 10: case 11:
716 int idx = number - 8;
718 if (jse.number % 2)
719 This->povs[idx].y = jse.value;
720 else
721 This->povs[idx].x = jse.value;
723 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
724 break;
726 default:
727 WARN("axis %d not supported\n", number);
730 if (inst_id >= 0)
731 queue_event((LPDIRECTINPUTDEVICE8A)This,
732 id_to_offset(&This->base.data_format, inst_id),
733 value, jse.time, This->base.dinput->evsequence++);
737 /******************************************************************************
738 * GetDeviceState : returns the "state" of the joystick.
741 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
742 LPDIRECTINPUTDEVICE8A iface,
743 DWORD len,
744 LPVOID ptr)
746 JoystickImpl *This = (JoystickImpl *)iface;
748 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
750 if (!This->base.acquired) {
751 WARN("not acquired\n");
752 return DIERR_NOTACQUIRED;
755 /* update joystick state */
756 joy_polldev(This);
758 /* convert and copy data to user supplied buffer */
759 fill_DataFormat(ptr, &This->js, &This->base.data_format);
761 return DI_OK;
764 /******************************************************************************
765 * SetProperty : change input device properties
767 static HRESULT WINAPI JoystickAImpl_SetProperty(
768 LPDIRECTINPUTDEVICE8A iface,
769 REFGUID rguid,
770 LPCDIPROPHEADER ph)
772 JoystickImpl *This = (JoystickImpl *)iface;
773 int i;
775 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
777 if (ph == NULL) {
778 WARN("invalid parameter: ph == NULL\n");
779 return DIERR_INVALIDPARAM;
782 if (TRACE_ON(dinput))
783 _dump_DIPROPHEADER(ph);
785 if (!HIWORD(rguid)) {
786 switch (LOWORD(rguid)) {
787 case (DWORD)DIPROP_RANGE: {
788 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
789 if (ph->dwHow == DIPH_DEVICE) {
790 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
791 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
792 This->props[i].lMin = pr->lMin;
793 This->props[i].lMax = pr->lMax;
795 } else {
796 int obj = find_property(&This->base.data_format, ph);
798 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
799 if (obj >= 0) {
800 This->props[obj].lMin = pr->lMin;
801 This->props[obj].lMax = pr->lMax;
802 return DI_OK;
805 break;
807 case (DWORD)DIPROP_DEADZONE: {
808 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
809 if (ph->dwHow == DIPH_DEVICE) {
810 TRACE("deadzone(%d) all\n", pd->dwData);
811 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
812 This->props[i].lDeadZone = pd->dwData;
813 } else {
814 int obj = find_property(&This->base.data_format, ph);
816 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
817 if (obj >= 0) {
818 This->props[obj].lDeadZone = pd->dwData;
819 return DI_OK;
822 break;
824 case (DWORD)DIPROP_SATURATION: {
825 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
826 if (ph->dwHow == DIPH_DEVICE) {
827 TRACE("saturation(%d) all\n", pd->dwData);
828 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
829 This->props[i].lSaturation = pd->dwData;
830 } else {
831 int obj = find_property(&This->base.data_format, ph);
833 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
834 if (obj >= 0) {
835 This->props[obj].lSaturation = pd->dwData;
836 return DI_OK;
839 break;
841 default:
842 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
846 return DI_OK;
849 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
850 LPDIRECTINPUTDEVICE8A iface,
851 LPDIDEVCAPS lpDIDevCaps)
853 JoystickImpl *This = (JoystickImpl *)iface;
854 int size;
856 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
858 if (lpDIDevCaps == NULL) {
859 WARN("invalid pointer\n");
860 return E_POINTER;
863 size = lpDIDevCaps->dwSize;
865 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
866 WARN("invalid parameter\n");
867 return DIERR_INVALIDPARAM;
870 CopyMemory(lpDIDevCaps, &This->devcaps, size);
871 lpDIDevCaps->dwSize = size;
873 if (TRACE_ON(dinput))
874 _dump_DIDEVCAPS(lpDIDevCaps);
876 return DI_OK;
879 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
881 JoystickImpl *This = (JoystickImpl *)iface;
883 TRACE("(%p)\n",This);
885 if (!This->base.acquired) {
886 WARN("not acquired\n");
887 return DIERR_NOTACQUIRED;
890 joy_polldev(This);
891 return DI_OK;
894 /******************************************************************************
895 * GetProperty : get input device properties
897 static HRESULT WINAPI JoystickAImpl_GetProperty(
898 LPDIRECTINPUTDEVICE8A iface,
899 REFGUID rguid,
900 LPDIPROPHEADER pdiph)
902 JoystickImpl *This = (JoystickImpl *)iface;
904 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
906 if (TRACE_ON(dinput))
907 _dump_DIPROPHEADER(pdiph);
909 if (!HIWORD(rguid)) {
910 switch (LOWORD(rguid)) {
911 case (DWORD) DIPROP_RANGE: {
912 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
913 int obj = find_property(&This->base.data_format, pdiph);
915 /* The app is querying the current range of the axis
916 * return the lMin and lMax values */
917 if (obj >= 0) {
918 pr->lMin = This->props[obj].lMin;
919 pr->lMax = This->props[obj].lMax;
920 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
921 return DI_OK;
923 break;
925 case (DWORD) DIPROP_DEADZONE: {
926 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
927 int obj = find_property(&This->base.data_format, pdiph);
929 if (obj >= 0) {
930 pd->dwData = This->props[obj].lDeadZone;
931 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
932 return DI_OK;
934 break;
936 case (DWORD) DIPROP_SATURATION: {
937 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
938 int obj = find_property(&This->base.data_format, pdiph);
940 if (obj >= 0) {
941 pd->dwData = This->props[obj].lSaturation;
942 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
943 return DI_OK;
945 break;
947 default:
948 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
952 return DI_OK;
955 /******************************************************************************
956 * GetObjectInfo : get object info
958 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
959 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
961 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
962 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
963 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
964 HRESULT res;
966 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
967 if (res != DI_OK) return res;
969 if (pdidoi->dwType & DIDFT_AXIS)
970 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
971 else if (pdidoi->dwType & DIDFT_POV)
972 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
973 else if (pdidoi->dwType & DIDFT_BUTTON)
974 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
976 _dump_OBJECTINSTANCEW(pdidoi);
977 return res;
980 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
981 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
983 HRESULT res;
984 DIDEVICEOBJECTINSTANCEW didoiW;
985 DWORD dwSize = pdidoi->dwSize;
987 didoiW.dwSize = sizeof(didoiW);
988 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
989 if (res != DI_OK) return res;
991 memset(pdidoi, 0, pdidoi->dwSize);
992 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
993 pdidoi->dwSize = dwSize;
994 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
995 sizeof(pdidoi->tszName), NULL, NULL);
997 return res;
1000 /******************************************************************************
1001 * GetDeviceInfo : get information about a device's identity
1003 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1004 LPDIRECTINPUTDEVICE8A iface,
1005 LPDIDEVICEINSTANCEA pdidi)
1007 JoystickImpl *This = (JoystickImpl *)iface;
1009 TRACE("(%p,%p)\n", iface, pdidi);
1011 if (pdidi == NULL) {
1012 WARN("invalid pointer\n");
1013 return E_POINTER;
1016 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1017 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1018 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1019 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1020 sizeof(DIDEVICEINSTANCEA));
1021 return DIERR_INVALIDPARAM;
1024 /* Return joystick */
1025 pdidi->guidInstance = GUID_Joystick;
1026 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1027 /* we only support traditional joysticks for now */
1028 pdidi->dwDevType = This->devcaps.dwDevType;
1029 strcpy(pdidi->tszInstanceName, "Joystick");
1030 strcpy(pdidi->tszProductName, This->name);
1031 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1032 pdidi->guidFFDriver = GUID_NULL;
1033 pdidi->wUsagePage = 0;
1034 pdidi->wUsage = 0;
1037 return DI_OK;
1040 /******************************************************************************
1041 * GetDeviceInfo : get information about a device's identity
1043 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1044 LPDIRECTINPUTDEVICE8W iface,
1045 LPDIDEVICEINSTANCEW pdidi)
1047 JoystickImpl *This = (JoystickImpl *)iface;
1049 TRACE("(%p,%p)\n", iface, pdidi);
1051 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1052 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1053 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1054 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1055 sizeof(DIDEVICEINSTANCEW));
1056 return DIERR_INVALIDPARAM;
1059 /* Return joystick */
1060 pdidi->guidInstance = GUID_Joystick;
1061 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1062 /* we only support traditional joysticks for now */
1063 pdidi->dwDevType = This->devcaps.dwDevType;
1064 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1065 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1066 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1067 pdidi->guidFFDriver = GUID_NULL;
1068 pdidi->wUsagePage = 0;
1069 pdidi->wUsage = 0;
1072 return DI_OK;
1075 static const IDirectInputDevice8AVtbl JoystickAvt =
1077 IDirectInputDevice2AImpl_QueryInterface,
1078 IDirectInputDevice2AImpl_AddRef,
1079 IDirectInputDevice2AImpl_Release,
1080 JoystickAImpl_GetCapabilities,
1081 IDirectInputDevice2AImpl_EnumObjects,
1082 JoystickAImpl_GetProperty,
1083 JoystickAImpl_SetProperty,
1084 JoystickAImpl_Acquire,
1085 JoystickAImpl_Unacquire,
1086 JoystickAImpl_GetDeviceState,
1087 IDirectInputDevice2AImpl_GetDeviceData,
1088 IDirectInputDevice2AImpl_SetDataFormat,
1089 IDirectInputDevice2AImpl_SetEventNotification,
1090 IDirectInputDevice2AImpl_SetCooperativeLevel,
1091 JoystickAImpl_GetObjectInfo,
1092 JoystickAImpl_GetDeviceInfo,
1093 IDirectInputDevice2AImpl_RunControlPanel,
1094 IDirectInputDevice2AImpl_Initialize,
1095 IDirectInputDevice2AImpl_CreateEffect,
1096 IDirectInputDevice2AImpl_EnumEffects,
1097 IDirectInputDevice2AImpl_GetEffectInfo,
1098 IDirectInputDevice2AImpl_GetForceFeedbackState,
1099 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1100 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1101 IDirectInputDevice2AImpl_Escape,
1102 JoystickAImpl_Poll,
1103 IDirectInputDevice2AImpl_SendDeviceData,
1104 IDirectInputDevice7AImpl_EnumEffectsInFile,
1105 IDirectInputDevice7AImpl_WriteEffectToFile,
1106 IDirectInputDevice8AImpl_BuildActionMap,
1107 IDirectInputDevice8AImpl_SetActionMap,
1108 IDirectInputDevice8AImpl_GetImageInfo
1111 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1112 # define XCAST(fun) (typeof(JoystickWvt.fun))
1113 #else
1114 # define XCAST(fun) (void*)
1115 #endif
1117 static const IDirectInputDevice8WVtbl JoystickWvt =
1119 IDirectInputDevice2WImpl_QueryInterface,
1120 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1121 XCAST(Release)IDirectInputDevice2AImpl_Release,
1122 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1123 IDirectInputDevice2WImpl_EnumObjects,
1124 XCAST(GetProperty)JoystickAImpl_GetProperty,
1125 XCAST(SetProperty)JoystickAImpl_SetProperty,
1126 XCAST(Acquire)JoystickAImpl_Acquire,
1127 XCAST(Unacquire)JoystickAImpl_Unacquire,
1128 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1129 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1130 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1131 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1132 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1133 IDirectInputDevice2WImpl_GetObjectInfo,
1134 JoystickWImpl_GetDeviceInfo,
1135 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1136 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1137 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1138 IDirectInputDevice2WImpl_EnumEffects,
1139 IDirectInputDevice2WImpl_GetEffectInfo,
1140 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1141 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1142 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1143 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1144 XCAST(Poll)JoystickAImpl_Poll,
1145 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1146 IDirectInputDevice7WImpl_EnumEffectsInFile,
1147 IDirectInputDevice7WImpl_WriteEffectToFile,
1148 IDirectInputDevice8WImpl_BuildActionMap,
1149 IDirectInputDevice8WImpl_SetActionMap,
1150 IDirectInputDevice8WImpl_GetImageInfo
1152 #undef XCAST
1154 #else /* HAVE_LINUX_22_JOYSTICK_API */
1156 const struct dinput_device joystick_linux_device = {
1157 "Wine Linux joystick driver",
1158 NULL,
1159 NULL,
1160 NULL,
1161 NULL
1164 #endif /* HAVE_LINUX_22_JOYSTICK_API */