setupapi: Implement SetupDiCreateDevRegKeyA on top of SetupDiCreateDevRegKeyW.
[wine/hacks.git] / dlls / dinput / joystick_linux.c
blob000837df83db2cd7efef78147590f95a7b569695
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;
274 buffer[MAX_PATH]='\0';
276 get_app_key(&hkey, &appkey);
278 /* get options */
280 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
281 device->deadzone = atoi(buffer);
282 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
285 if (!get_config_key( hkey, appkey, device->name, buffer, MAX_PATH )) {
286 int tokens = 0;
287 int axis = 0;
288 int pov = 0;
289 const char *delim = ",";
290 char * ptr;
291 TRACE("\"%s\" = \"%s\"\n", device->name, buffer);
293 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
294 if (device->axis_map == 0)
295 return DIERR_OUTOFMEMORY;
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++;
357 device->devcaps.dwAxes = axis;
358 device->devcaps.dwPOVs = pov;
361 if (appkey)
362 RegCloseKey( appkey );
364 if (hkey)
365 RegCloseKey( hkey );
367 return DI_OK;
370 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
371 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
373 DWORD i;
374 JoystickImpl* newDevice;
375 char name[MAX_PATH];
376 HRESULT hr;
377 LPDIDATAFORMAT df = NULL;
378 int idx = 0;
380 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
381 if (newDevice == 0) {
382 WARN("out of memory\n");
383 *pdev = 0;
384 return DIERR_OUTOFMEMORY;
387 if (!lstrcpynA(newDevice->dev, joystick_devices[index], sizeof(newDevice->dev)) ||
388 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
390 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
391 HeapFree(GetProcessHeap(), 0, newDevice);
392 return DIERR_DEVICENOTREG;
395 /* get the device name */
396 #if defined(JSIOCGNAME)
397 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
398 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
399 strcpy(name, "Wine Joystick");
401 #else
402 strcpy(name, "Wine Joystick");
403 #endif
405 /* copy the device name */
406 newDevice->name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
407 strcpy(newDevice->name, name);
409 #ifdef JSIOCGAXES
410 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
411 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
412 newDevice->axes = 2;
414 #endif
415 #ifdef JSIOCGBUTTONS
416 if (ioctl(newDevice->joyfd,JSIOCGBUTTONS,&newDevice->buttons) < 0) {
417 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
418 newDevice->buttons = 2;
420 #endif
422 newDevice->base.lpVtbl = jvt;
423 newDevice->base.ref = 1;
424 newDevice->base.dinput = dinput;
425 CopyMemory(&newDevice->base.guid, rguid, sizeof(*rguid));
426 InitializeCriticalSection(&newDevice->base.crit);
427 newDevice->base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->base.crit");
429 /* setup_dinput_options may change these */
430 newDevice->deadzone = 0;
431 newDevice->devcaps.dwButtons = newDevice->buttons;
432 newDevice->devcaps.dwAxes = newDevice->axes;
433 newDevice->devcaps.dwPOVs = 0;
435 /* do any user specified configuration */
436 hr = setup_dinput_options(newDevice);
437 if (hr != DI_OK)
438 goto FAILED1;
440 if (newDevice->axis_map == 0) {
441 newDevice->axis_map = HeapAlloc(GetProcessHeap(), 0, newDevice->axes * sizeof(int));
442 if (newDevice->axis_map == 0)
443 goto FAILED;
445 for (i = 0; i < newDevice->axes; i++)
446 newDevice->axis_map[i] = i;
449 /* Create copy of default data format */
450 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
451 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
453 /* Axes include POVs */
454 df->dwNumObjs = newDevice->axes + newDevice->buttons;
455 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
457 for (i = 0; i < newDevice->axes; i++)
459 int wine_obj = newDevice->axis_map[i];
461 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
462 if (wine_obj < 8)
463 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
464 else
466 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
467 i++; /* POV takes 2 axes */
470 for (i = 0; i < newDevice->buttons; i++)
472 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
473 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
475 newDevice->base.data_format.wine_df = df;
477 /* create default properties */
478 newDevice->props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
479 if (newDevice->props == 0)
480 goto FAILED;
482 /* initialize default properties */
483 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
484 newDevice->props[i].lDevMin = -32767;
485 newDevice->props[i].lDevMax = +32767;
486 newDevice->props[i].lMin = 0;
487 newDevice->props[i].lMax = 0xffff;
488 newDevice->props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
489 newDevice->props[i].lSaturation = 0;
492 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->base.dinput);
494 newDevice->devcaps.dwSize = sizeof(newDevice->devcaps);
495 newDevice->devcaps.dwFlags = DIDC_ATTACHED;
496 if (newDevice->base.dinput->dwVersion >= 0x0800)
497 newDevice->devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
498 else
499 newDevice->devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
500 newDevice->devcaps.dwFFSamplePeriod = 0;
501 newDevice->devcaps.dwFFMinTimeResolution = 0;
502 newDevice->devcaps.dwFirmwareRevision = 0;
503 newDevice->devcaps.dwHardwareRevision = 0;
504 newDevice->devcaps.dwFFDriverVersion = 0;
506 if (TRACE_ON(dinput)) {
507 _dump_DIDATAFORMAT(newDevice->base.data_format.wine_df);
508 for (i = 0; i < (newDevice->axes); i++)
509 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
510 _dump_DIDEVCAPS(&newDevice->devcaps);
513 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
515 return DI_OK;
517 FAILED:
518 hr = DIERR_OUTOFMEMORY;
519 FAILED1:
520 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
521 HeapFree(GetProcessHeap(), 0, df);
522 release_DataFormat(&newDevice->base.data_format);
523 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
524 HeapFree(GetProcessHeap(),0,newDevice->name);
525 HeapFree(GetProcessHeap(),0,newDevice->props);
526 HeapFree(GetProcessHeap(),0,newDevice);
527 *pdev = 0;
529 return hr;
532 /******************************************************************************
533 * get_joystick_index : Get the joystick index from a given GUID
535 static unsigned short get_joystick_index(REFGUID guid)
537 GUID wine_joystick = DInput_Wine_Joystick_GUID;
538 GUID dev_guid = *guid;
540 wine_joystick.Data3 = 0;
541 dev_guid.Data3 = 0;
543 /* for the standard joystick GUID use index 0 */
544 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
546 /* for the wine joystick GUIDs use the index stored in Data3 */
547 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
549 return MAX_JOYSTICKS;
552 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
554 unsigned short index;
556 find_joystick_devices();
557 *pdev = NULL;
559 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
560 joystick_devices_count && index < joystick_devices_count)
562 if ((riid == NULL) ||
563 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
564 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
565 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
566 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
568 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
571 WARN("no interface\n");
572 return DIERR_NOINTERFACE;
575 WARN("invalid device GUID\n");
576 return DIERR_DEVICENOTREG;
579 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
581 unsigned short index;
583 find_joystick_devices();
584 *pdev = NULL;
586 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
587 joystick_devices_count && index < joystick_devices_count)
589 if ((riid == NULL) ||
590 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
591 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
592 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
593 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
595 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
597 WARN("no interface\n");
598 return DIERR_NOINTERFACE;
601 WARN("invalid device GUID\n");
602 return DIERR_DEVICENOTREG;
605 #undef MAX_JOYSTICKS
607 const struct dinput_device joystick_linux_device = {
608 "Wine Linux joystick driver",
609 joydev_enum_deviceA,
610 joydev_enum_deviceW,
611 joydev_create_deviceA,
612 joydev_create_deviceW
615 /******************************************************************************
616 * Acquire : gets exclusive control of the joystick
618 static HRESULT WINAPI JoystickAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
620 JoystickImpl *This = (JoystickImpl *)iface;
622 TRACE("(%p)\n",This);
624 if (This->base.acquired) {
625 WARN("already acquired\n");
626 return S_FALSE;
629 /* open the joystick device */
630 if (This->joyfd==-1) {
631 TRACE("opening joystick device %s\n", This->dev);
633 This->joyfd=open(This->dev,O_RDONLY);
634 if (This->joyfd==-1) {
635 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
636 return DIERR_NOTFOUND;
640 This->base.acquired = 1;
642 return DI_OK;
645 /******************************************************************************
646 * Unacquire : frees the joystick
648 static HRESULT WINAPI JoystickAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
650 JoystickImpl *This = (JoystickImpl *)iface;
651 HRESULT res;
653 TRACE("(%p)\n",This);
655 if ((res = IDirectInputDevice2AImpl_Unacquire(iface)) != DI_OK) return res;
657 if (This->joyfd!=-1) {
658 TRACE("closing joystick device\n");
659 close(This->joyfd);
660 This->joyfd = -1;
661 return DI_OK;
664 return DI_NOEFFECT;
667 static void joy_polldev(JoystickImpl *This) {
668 struct pollfd plfd;
669 struct js_event jse;
670 TRACE("(%p)\n", This);
672 if (This->joyfd==-1) {
673 WARN("no device\n");
674 return;
676 while (1)
678 LONG value;
679 int inst_id = -1;
681 plfd.fd = This->joyfd;
682 plfd.events = POLLIN;
683 if (poll(&plfd,1,0) != 1)
684 return;
685 /* we have one event, so we can read */
686 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
687 return;
689 TRACE("js_event: type 0x%x, number %d, value %d\n",
690 jse.type,jse.number,jse.value);
691 if (jse.type & JS_EVENT_BUTTON)
693 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
694 This->js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
696 else if (jse.type & JS_EVENT_AXIS)
698 int number = This->axis_map[jse.number]; /* wine format object index */
700 if (number < 0) return;
701 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
702 value = joystick_map_axis(&This->props[id_to_object(This->base.data_format.wine_df, inst_id)], jse.value);
704 TRACE("changing axis %d => %d\n", jse.number, number);
705 switch (number)
707 case 0: This->js.lX = value; break;
708 case 1: This->js.lY = value; break;
709 case 2: This->js.lZ = value; break;
710 case 3: This->js.lRx = value; break;
711 case 4: This->js.lRy = value; break;
712 case 5: This->js.lRz = value; break;
713 case 6: This->js.rglSlider[0] = value; break;
714 case 7: This->js.rglSlider[1] = value; break;
715 case 8: case 9: case 10: case 11:
717 int idx = number - 8;
719 if (jse.number % 2)
720 This->povs[idx].y = jse.value;
721 else
722 This->povs[idx].x = jse.value;
724 This->js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
725 break;
727 default:
728 WARN("axis %d not supported\n", number);
731 if (inst_id >= 0)
732 queue_event((LPDIRECTINPUTDEVICE8A)This,
733 id_to_offset(&This->base.data_format, inst_id),
734 value, jse.time, This->base.dinput->evsequence++);
738 /******************************************************************************
739 * GetDeviceState : returns the "state" of the joystick.
742 static HRESULT WINAPI JoystickAImpl_GetDeviceState(
743 LPDIRECTINPUTDEVICE8A iface,
744 DWORD len,
745 LPVOID ptr)
747 JoystickImpl *This = (JoystickImpl *)iface;
749 TRACE("(%p,0x%08x,%p)\n", This, len, ptr);
751 if (!This->base.acquired) {
752 WARN("not acquired\n");
753 return DIERR_NOTACQUIRED;
756 /* update joystick state */
757 joy_polldev(This);
759 /* convert and copy data to user supplied buffer */
760 fill_DataFormat(ptr, &This->js, &This->base.data_format);
762 return DI_OK;
765 /******************************************************************************
766 * SetProperty : change input device properties
768 static HRESULT WINAPI JoystickAImpl_SetProperty(
769 LPDIRECTINPUTDEVICE8A iface,
770 REFGUID rguid,
771 LPCDIPROPHEADER ph)
773 JoystickImpl *This = (JoystickImpl *)iface;
774 int i;
776 TRACE("(%p,%s,%p)\n",This,debugstr_guid(rguid),ph);
778 if (ph == NULL) {
779 WARN("invalid parameter: ph == NULL\n");
780 return DIERR_INVALIDPARAM;
783 if (TRACE_ON(dinput))
784 _dump_DIPROPHEADER(ph);
786 if (!HIWORD(rguid)) {
787 switch (LOWORD(rguid)) {
788 case (DWORD)DIPROP_RANGE: {
789 LPCDIPROPRANGE pr = (LPCDIPROPRANGE)ph;
790 if (ph->dwHow == DIPH_DEVICE) {
791 TRACE("proprange(%d,%d) all\n", pr->lMin, pr->lMax);
792 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++) {
793 This->props[i].lMin = pr->lMin;
794 This->props[i].lMax = pr->lMax;
796 } else {
797 int obj = find_property(&This->base.data_format, ph);
799 TRACE("proprange(%d,%d) obj=%d\n", pr->lMin, pr->lMax, obj);
800 if (obj >= 0) {
801 This->props[obj].lMin = pr->lMin;
802 This->props[obj].lMax = pr->lMax;
803 return DI_OK;
806 break;
808 case (DWORD)DIPROP_DEADZONE: {
809 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
810 if (ph->dwHow == DIPH_DEVICE) {
811 TRACE("deadzone(%d) all\n", pd->dwData);
812 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
813 This->props[i].lDeadZone = pd->dwData;
814 } else {
815 int obj = find_property(&This->base.data_format, ph);
817 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
818 if (obj >= 0) {
819 This->props[obj].lDeadZone = pd->dwData;
820 return DI_OK;
823 break;
825 case (DWORD)DIPROP_SATURATION: {
826 LPCDIPROPDWORD pd = (LPCDIPROPDWORD)ph;
827 if (ph->dwHow == DIPH_DEVICE) {
828 TRACE("saturation(%d) all\n", pd->dwData);
829 for (i = 0; i < This->base.data_format.wine_df->dwNumObjs; i++)
830 This->props[i].lSaturation = pd->dwData;
831 } else {
832 int obj = find_property(&This->base.data_format, ph);
834 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
835 if (obj >= 0) {
836 This->props[obj].lSaturation = pd->dwData;
837 return DI_OK;
840 break;
842 default:
843 return IDirectInputDevice2AImpl_SetProperty(iface, rguid, ph);
847 return DI_OK;
850 static HRESULT WINAPI JoystickAImpl_GetCapabilities(
851 LPDIRECTINPUTDEVICE8A iface,
852 LPDIDEVCAPS lpDIDevCaps)
854 JoystickImpl *This = (JoystickImpl *)iface;
855 int size;
857 TRACE("%p->(%p)\n",iface,lpDIDevCaps);
859 if (lpDIDevCaps == NULL) {
860 WARN("invalid pointer\n");
861 return E_POINTER;
864 size = lpDIDevCaps->dwSize;
866 if (!(size == sizeof(DIDEVCAPS) || size == sizeof(DIDEVCAPS_DX3))) {
867 WARN("invalid parameter\n");
868 return DIERR_INVALIDPARAM;
871 CopyMemory(lpDIDevCaps, &This->devcaps, size);
872 lpDIDevCaps->dwSize = size;
874 if (TRACE_ON(dinput))
875 _dump_DIDEVCAPS(lpDIDevCaps);
877 return DI_OK;
880 static HRESULT WINAPI JoystickAImpl_Poll(LPDIRECTINPUTDEVICE8A iface)
882 JoystickImpl *This = (JoystickImpl *)iface;
884 TRACE("(%p)\n",This);
886 if (!This->base.acquired) {
887 WARN("not acquired\n");
888 return DIERR_NOTACQUIRED;
891 joy_polldev(This);
892 return DI_OK;
895 /******************************************************************************
896 * GetProperty : get input device properties
898 static HRESULT WINAPI JoystickAImpl_GetProperty(
899 LPDIRECTINPUTDEVICE8A iface,
900 REFGUID rguid,
901 LPDIPROPHEADER pdiph)
903 JoystickImpl *This = (JoystickImpl *)iface;
905 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
907 if (TRACE_ON(dinput))
908 _dump_DIPROPHEADER(pdiph);
910 if (!HIWORD(rguid)) {
911 switch (LOWORD(rguid)) {
912 case (DWORD) DIPROP_RANGE: {
913 LPDIPROPRANGE pr = (LPDIPROPRANGE)pdiph;
914 int obj = find_property(&This->base.data_format, pdiph);
916 /* The app is querying the current range of the axis
917 * return the lMin and lMax values */
918 if (obj >= 0) {
919 pr->lMin = This->props[obj].lMin;
920 pr->lMax = This->props[obj].lMax;
921 TRACE("range(%d, %d) obj=%d\n", pr->lMin, pr->lMax, obj);
922 return DI_OK;
924 break;
926 case (DWORD) DIPROP_DEADZONE: {
927 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
928 int obj = find_property(&This->base.data_format, pdiph);
930 if (obj >= 0) {
931 pd->dwData = This->props[obj].lDeadZone;
932 TRACE("deadzone(%d) obj=%d\n", pd->dwData, obj);
933 return DI_OK;
935 break;
937 case (DWORD) DIPROP_SATURATION: {
938 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
939 int obj = find_property(&This->base.data_format, pdiph);
941 if (obj >= 0) {
942 pd->dwData = This->props[obj].lSaturation;
943 TRACE("saturation(%d) obj=%d\n", pd->dwData, obj);
944 return DI_OK;
946 break;
948 default:
949 return IDirectInputDevice2AImpl_GetProperty(iface, rguid, pdiph);
953 return DI_OK;
956 /******************************************************************************
957 * GetObjectInfo : get object info
959 static HRESULT WINAPI JoystickWImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8W iface,
960 LPDIDEVICEOBJECTINSTANCEW pdidoi, DWORD dwObj, DWORD dwHow)
962 static const WCHAR axisW[] = {'A','x','i','s',' ','%','d',0};
963 static const WCHAR povW[] = {'P','O','V',' ','%','d',0};
964 static const WCHAR buttonW[] = {'B','u','t','t','o','n',' ','%','d',0};
965 HRESULT res;
967 res = IDirectInputDevice2WImpl_GetObjectInfo(iface, pdidoi, dwObj, dwHow);
968 if (res != DI_OK) return res;
970 if (pdidoi->dwType & DIDFT_AXIS)
971 sprintfW(pdidoi->tszName, axisW, DIDFT_GETINSTANCE(pdidoi->dwType));
972 else if (pdidoi->dwType & DIDFT_POV)
973 sprintfW(pdidoi->tszName, povW, DIDFT_GETINSTANCE(pdidoi->dwType));
974 else if (pdidoi->dwType & DIDFT_BUTTON)
975 sprintfW(pdidoi->tszName, buttonW, DIDFT_GETINSTANCE(pdidoi->dwType));
977 _dump_OBJECTINSTANCEW(pdidoi);
978 return res;
981 static HRESULT WINAPI JoystickAImpl_GetObjectInfo(LPDIRECTINPUTDEVICE8A iface,
982 LPDIDEVICEOBJECTINSTANCEA pdidoi, DWORD dwObj, DWORD dwHow)
984 HRESULT res;
985 DIDEVICEOBJECTINSTANCEW didoiW;
986 DWORD dwSize = pdidoi->dwSize;
988 didoiW.dwSize = sizeof(didoiW);
989 res = JoystickWImpl_GetObjectInfo((LPDIRECTINPUTDEVICE8W)iface, &didoiW, dwObj, dwHow);
990 if (res != DI_OK) return res;
992 memset(pdidoi, 0, pdidoi->dwSize);
993 memcpy(pdidoi, &didoiW, FIELD_OFFSET(DIDEVICEOBJECTINSTANCEW, tszName));
994 pdidoi->dwSize = dwSize;
995 WideCharToMultiByte(CP_ACP, 0, didoiW.tszName, -1, pdidoi->tszName,
996 sizeof(pdidoi->tszName), NULL, NULL);
998 return res;
1001 /******************************************************************************
1002 * GetDeviceInfo : get information about a device's identity
1004 static HRESULT WINAPI JoystickAImpl_GetDeviceInfo(
1005 LPDIRECTINPUTDEVICE8A iface,
1006 LPDIDEVICEINSTANCEA pdidi)
1008 JoystickImpl *This = (JoystickImpl *)iface;
1010 TRACE("(%p,%p)\n", iface, pdidi);
1012 if (pdidi == NULL) {
1013 WARN("invalid pointer\n");
1014 return E_POINTER;
1017 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3A)) &&
1018 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEA))) {
1019 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1020 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3A),
1021 sizeof(DIDEVICEINSTANCEA));
1022 return DIERR_INVALIDPARAM;
1025 /* Return joystick */
1026 pdidi->guidInstance = GUID_Joystick;
1027 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1028 /* we only support traditional joysticks for now */
1029 pdidi->dwDevType = This->devcaps.dwDevType;
1030 strcpy(pdidi->tszInstanceName, "Joystick");
1031 strcpy(pdidi->tszProductName, This->name);
1032 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3A)) {
1033 pdidi->guidFFDriver = GUID_NULL;
1034 pdidi->wUsagePage = 0;
1035 pdidi->wUsage = 0;
1038 return DI_OK;
1041 /******************************************************************************
1042 * GetDeviceInfo : get information about a device's identity
1044 static HRESULT WINAPI JoystickWImpl_GetDeviceInfo(
1045 LPDIRECTINPUTDEVICE8W iface,
1046 LPDIDEVICEINSTANCEW pdidi)
1048 JoystickImpl *This = (JoystickImpl *)iface;
1050 TRACE("(%p,%p)\n", iface, pdidi);
1052 if ((pdidi->dwSize != sizeof(DIDEVICEINSTANCE_DX3W)) &&
1053 (pdidi->dwSize != sizeof(DIDEVICEINSTANCEW))) {
1054 WARN("invalid parameter: pdidi->dwSize = %d != %d or %d\n",
1055 pdidi->dwSize, sizeof(DIDEVICEINSTANCE_DX3W),
1056 sizeof(DIDEVICEINSTANCEW));
1057 return DIERR_INVALIDPARAM;
1060 /* Return joystick */
1061 pdidi->guidInstance = GUID_Joystick;
1062 pdidi->guidProduct = DInput_Wine_Joystick_GUID;
1063 /* we only support traditional joysticks for now */
1064 pdidi->dwDevType = This->devcaps.dwDevType;
1065 MultiByteToWideChar(CP_ACP, 0, "Joystick", -1, pdidi->tszInstanceName, MAX_PATH);
1066 MultiByteToWideChar(CP_ACP, 0, This->name, -1, pdidi->tszProductName, MAX_PATH);
1067 if (pdidi->dwSize > sizeof(DIDEVICEINSTANCE_DX3W)) {
1068 pdidi->guidFFDriver = GUID_NULL;
1069 pdidi->wUsagePage = 0;
1070 pdidi->wUsage = 0;
1073 return DI_OK;
1076 static const IDirectInputDevice8AVtbl JoystickAvt =
1078 IDirectInputDevice2AImpl_QueryInterface,
1079 IDirectInputDevice2AImpl_AddRef,
1080 IDirectInputDevice2AImpl_Release,
1081 JoystickAImpl_GetCapabilities,
1082 IDirectInputDevice2AImpl_EnumObjects,
1083 JoystickAImpl_GetProperty,
1084 JoystickAImpl_SetProperty,
1085 JoystickAImpl_Acquire,
1086 JoystickAImpl_Unacquire,
1087 JoystickAImpl_GetDeviceState,
1088 IDirectInputDevice2AImpl_GetDeviceData,
1089 IDirectInputDevice2AImpl_SetDataFormat,
1090 IDirectInputDevice2AImpl_SetEventNotification,
1091 IDirectInputDevice2AImpl_SetCooperativeLevel,
1092 JoystickAImpl_GetObjectInfo,
1093 JoystickAImpl_GetDeviceInfo,
1094 IDirectInputDevice2AImpl_RunControlPanel,
1095 IDirectInputDevice2AImpl_Initialize,
1096 IDirectInputDevice2AImpl_CreateEffect,
1097 IDirectInputDevice2AImpl_EnumEffects,
1098 IDirectInputDevice2AImpl_GetEffectInfo,
1099 IDirectInputDevice2AImpl_GetForceFeedbackState,
1100 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1101 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1102 IDirectInputDevice2AImpl_Escape,
1103 JoystickAImpl_Poll,
1104 IDirectInputDevice2AImpl_SendDeviceData,
1105 IDirectInputDevice7AImpl_EnumEffectsInFile,
1106 IDirectInputDevice7AImpl_WriteEffectToFile,
1107 IDirectInputDevice8AImpl_BuildActionMap,
1108 IDirectInputDevice8AImpl_SetActionMap,
1109 IDirectInputDevice8AImpl_GetImageInfo
1112 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
1113 # define XCAST(fun) (typeof(JoystickWvt.fun))
1114 #else
1115 # define XCAST(fun) (void*)
1116 #endif
1118 static const IDirectInputDevice8WVtbl JoystickWvt =
1120 IDirectInputDevice2WImpl_QueryInterface,
1121 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
1122 XCAST(Release)IDirectInputDevice2AImpl_Release,
1123 XCAST(GetCapabilities)JoystickAImpl_GetCapabilities,
1124 IDirectInputDevice2WImpl_EnumObjects,
1125 XCAST(GetProperty)JoystickAImpl_GetProperty,
1126 XCAST(SetProperty)JoystickAImpl_SetProperty,
1127 XCAST(Acquire)JoystickAImpl_Acquire,
1128 XCAST(Unacquire)JoystickAImpl_Unacquire,
1129 XCAST(GetDeviceState)JoystickAImpl_GetDeviceState,
1130 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
1131 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
1132 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
1133 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
1134 IDirectInputDevice2WImpl_GetObjectInfo,
1135 JoystickWImpl_GetDeviceInfo,
1136 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
1137 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
1138 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
1139 IDirectInputDevice2WImpl_EnumEffects,
1140 IDirectInputDevice2WImpl_GetEffectInfo,
1141 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
1142 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
1143 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
1144 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
1145 XCAST(Poll)JoystickAImpl_Poll,
1146 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
1147 IDirectInputDevice7WImpl_EnumEffectsInFile,
1148 IDirectInputDevice7WImpl_WriteEffectToFile,
1149 IDirectInputDevice8WImpl_BuildActionMap,
1150 IDirectInputDevice8WImpl_SetActionMap,
1151 IDirectInputDevice8WImpl_GetImageInfo
1153 #undef XCAST
1155 #else /* HAVE_LINUX_22_JOYSTICK_API */
1157 const struct dinput_device joystick_linux_device = {
1158 "Wine Linux joystick driver",
1159 NULL,
1160 NULL,
1161 NULL,
1162 NULL
1165 #endif /* HAVE_LINUX_22_JOYSTICK_API */