mciqtz32: Add support for MCI_WHERE.
[wine.git] / dlls / dinput / joystick_linux.c
blob3a8487b947f773910a0176dbf91a96782467b589
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"
70 #include "joystick_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 JoystickGenericImpl generic;
86 char dev[32];
88 /* joystick private */
89 int joyfd;
90 LONG deadzone;
91 int *axis_map;
92 int axes;
93 POINTL povs[4];
96 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
97 0x9e573ed9,
98 0x7734,
99 0x11d2,
100 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
103 #define MAX_JOYSTICKS 64
104 static INT joystick_devices_count = -1;
105 static LPSTR joystick_devices[MAX_JOYSTICKS];
107 static void joy_polldev(JoystickGenericImpl *This);
109 static INT find_joystick_devices(void)
111 INT i;
113 if (joystick_devices_count != -1) return joystick_devices_count;
115 joystick_devices_count = 0;
116 for (i = 0; i < MAX_JOYSTICKS; i++)
118 CHAR device_name[MAX_PATH], *str;
119 INT len;
120 int fd;
122 len = sprintf(device_name, "%s%d", JOYDEV_NEW, i) + 1;
123 if ((fd = open(device_name, O_RDONLY)) < 0)
125 len = sprintf(device_name, "%s%d", JOYDEV_OLD, i) + 1;
126 if ((fd = open(device_name, O_RDONLY)) < 0) continue;
129 close(fd);
131 if (!(str = HeapAlloc(GetProcessHeap(), 0, len))) break;
132 memcpy(str, device_name, len);
134 joystick_devices[joystick_devices_count++] = str;
137 return joystick_devices_count;
140 static BOOL joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
142 int fd = -1;
144 if (id >= find_joystick_devices()) return FALSE;
146 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
147 WARN("force feedback not supported\n");
148 return FALSE;
151 if ((dwDevType == 0) ||
152 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
153 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
154 /* check whether we have a joystick */
155 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
157 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
158 return FALSE;
161 /* Return joystick */
162 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
163 lpddi->guidInstance.Data3 = id;
164 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
165 /* we only support traditional joysticks for now */
166 if (version >= 0x0800)
167 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
168 else
169 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
170 sprintf(lpddi->tszInstanceName, "Joystick %d", id);
171 #if defined(JSIOCGNAME)
172 if (ioctl(fd,JSIOCGNAME(sizeof(lpddi->tszProductName)),lpddi->tszProductName) < 0) {
173 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
174 strcpy(lpddi->tszProductName, "Wine Joystick");
176 #else
177 strcpy(lpddi->tszProductName, "Wine Joystick");
178 #endif
180 lpddi->guidFFDriver = GUID_NULL;
181 close(fd);
182 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], lpddi->tszProductName);
183 return TRUE;
186 return FALSE;
189 static BOOL joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
191 int fd = -1;
192 char name[MAX_PATH];
193 char friendly[32];
195 if (id >= find_joystick_devices()) return FALSE;
197 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
198 WARN("force feedback not supported\n");
199 return FALSE;
202 if ((dwDevType == 0) ||
203 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
204 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
205 /* check whether we have a joystick */
206 if ((fd = open(joystick_devices[id], O_RDONLY)) < 0)
208 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id], strerror(errno));
209 return FALSE;
212 /* Return joystick */
213 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
214 lpddi->guidInstance.Data3 = id;
215 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
216 /* we only support traditional joysticks for now */
217 if (version >= 0x0800)
218 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
219 else
220 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
221 sprintf(friendly, "Joystick %d", id);
222 MultiByteToWideChar(CP_ACP, 0, friendly, -1, lpddi->tszInstanceName, MAX_PATH);
223 #if defined(JSIOCGNAME)
224 if (ioctl(fd,JSIOCGNAME(sizeof(name)),name) < 0) {
225 WARN("ioctl(%s, JSIOCGNAME) failed: %s\n", joystick_devices[id], strerror(errno));
226 strcpy(name, "Wine Joystick");
228 #else
229 strcpy(name, "Wine Joystick");
230 #endif
231 MultiByteToWideChar(CP_ACP, 0, name, -1, lpddi->tszProductName, MAX_PATH);
232 lpddi->guidFFDriver = GUID_NULL;
233 close(fd);
234 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id], name);
235 return TRUE;
238 return FALSE;
242 * Setup the dinput options.
245 static HRESULT setup_dinput_options(JoystickImpl * device)
247 char buffer[MAX_PATH+16];
248 HKEY hkey, appkey;
249 int tokens = 0;
250 int axis = 0;
251 int pov = 0;
253 buffer[MAX_PATH]='\0';
255 get_app_key(&hkey, &appkey);
257 /* get options */
259 if (!get_config_key( hkey, appkey, "DefaultDeadZone", buffer, MAX_PATH )) {
260 device->deadzone = atoi(buffer);
261 TRACE("setting default deadzone to: \"%s\" %d\n", buffer, device->deadzone);
264 device->axis_map = HeapAlloc(GetProcessHeap(), 0, device->axes * sizeof(int));
265 if (!device->axis_map) return DIERR_OUTOFMEMORY;
267 if (!get_config_key( hkey, appkey, device->generic.name, buffer, MAX_PATH )) {
268 static const char *axis_names[] = {"X", "Y", "Z", "Rx", "Ry", "Rz",
269 "Slider1", "Slider2",
270 "POV1", "POV2", "POV3", "POV4"};
271 const char *delim = ",";
272 char * ptr;
273 TRACE("\"%s\" = \"%s\"\n", device->generic.name, buffer);
275 if ((ptr = strtok(buffer, delim)) != NULL) {
276 do {
277 int i;
279 for (i = 0; i < sizeof(axis_names) / sizeof(axis_names[0]); i++)
280 if (!strcmp(ptr, axis_names[i]))
282 if (!strncmp(ptr, "POV", 3))
284 if (pov >= 4)
286 WARN("Only 4 POVs supported - ignoring extra\n");
287 i = -1;
289 else
291 /* Pov takes two axes */
292 device->axis_map[tokens++] = i;
293 pov++;
296 else
298 if (axis >= 8)
300 FIXME("Only 8 Axes supported - ignoring extra\n");
301 i = -1;
303 else
304 axis++;
306 break;
309 if (i == sizeof(axis_names) / sizeof(axis_names[0]))
311 ERR("invalid joystick axis type: \"%s\"\n", ptr);
312 i = -1;
315 device->axis_map[tokens] = i;
316 tokens++;
317 } while ((ptr = strtok(NULL, delim)) != NULL);
319 if (tokens != device->axes) {
320 ERR("not all joystick axes mapped: %d axes(%d,%d), %d arguments\n", device->axes, axis, pov,tokens);
321 while (tokens < device->axes) {
322 device->axis_map[tokens] = -1;
323 tokens++;
328 else
330 for (tokens = 0; tokens < device->axes; tokens++)
332 if (tokens < 8)
333 device->axis_map[tokens] = axis++;
334 else if (tokens < 16)
336 device->axis_map[tokens++] = 8 + pov;
337 device->axis_map[tokens ] = 8 + pov++;
339 else
340 device->axis_map[tokens] = -1;
343 device->generic.devcaps.dwAxes = axis;
344 device->generic.devcaps.dwPOVs = pov;
346 if (appkey)
347 RegCloseKey( appkey );
349 if (hkey)
350 RegCloseKey( hkey );
352 return DI_OK;
355 static HRESULT alloc_device(REFGUID rguid, const void *jvt, IDirectInputImpl *dinput,
356 LPDIRECTINPUTDEVICEA* pdev, unsigned short index)
358 DWORD i;
359 JoystickImpl* newDevice;
360 char name[MAX_PATH];
361 HRESULT hr;
362 LPDIDATAFORMAT df = NULL;
363 int idx = 0;
365 TRACE("%s %p %p %p %hu\n", debugstr_guid(rguid), jvt, dinput, pdev, index);
367 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
368 if (newDevice == 0) {
369 WARN("out of memory\n");
370 *pdev = 0;
371 return DIERR_OUTOFMEMORY;
374 if (!lstrcpynA(newDevice->dev, joystick_devices[index], sizeof(newDevice->dev)) ||
375 (newDevice->joyfd = open(newDevice->dev, O_RDONLY)) < 0)
377 WARN("open(%s, O_RDONLY) failed: %s\n", newDevice->dev, strerror(errno));
378 HeapFree(GetProcessHeap(), 0, newDevice);
379 return DIERR_DEVICENOTREG;
382 newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
383 newDevice->generic.guidInstance.Data3 = index;
384 newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
385 newDevice->generic.joy_polldev = joy_polldev;
387 /* get the device name */
388 #if defined(JSIOCGNAME)
389 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
390 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
391 strcpy(name, "Wine Joystick");
393 #else
394 strcpy(name, "Wine Joystick");
395 #endif
397 /* copy the device name */
398 newDevice->generic.name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
399 strcpy(newDevice->generic.name, name);
401 #ifdef JSIOCGAXES
402 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
403 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
404 newDevice->axes = 2;
406 #endif
407 #ifdef JSIOCGBUTTONS
408 if (ioctl(newDevice->joyfd, JSIOCGBUTTONS, &newDevice->generic.devcaps.dwButtons) < 0) {
409 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
410 newDevice->generic.devcaps.dwButtons = 2;
412 #endif
414 if (newDevice->generic.devcaps.dwButtons > 128)
416 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
417 newDevice->generic.devcaps.dwButtons = 128;
420 newDevice->generic.base.lpVtbl = jvt;
421 newDevice->generic.base.ref = 1;
422 newDevice->generic.base.dinput = dinput;
423 newDevice->generic.base.guid = *rguid;
424 InitializeCriticalSection(&newDevice->generic.base.crit);
425 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
427 /* setup_dinput_options may change these */
428 newDevice->deadzone = 0;
430 /* do any user specified configuration */
431 hr = setup_dinput_options(newDevice);
432 if (hr != DI_OK)
433 goto FAILED1;
435 /* Create copy of default data format */
436 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
437 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
439 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
440 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
442 for (i = 0; i < newDevice->axes; i++)
444 int wine_obj = newDevice->axis_map[i];
446 if (wine_obj < 0) continue;
448 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
449 if (wine_obj < 8)
450 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
451 else
453 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
454 i++; /* POV takes 2 axes */
457 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
459 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
460 df->rgodf[idx ].pguid = &GUID_Button;
461 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
463 newDevice->generic.base.data_format.wine_df = df;
465 /* initialize default properties */
466 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
467 newDevice->generic.props[i].lDevMin = -32767;
468 newDevice->generic.props[i].lDevMax = +32767;
469 newDevice->generic.props[i].lMin = 0;
470 newDevice->generic.props[i].lMax = 0xffff;
471 newDevice->generic.props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
472 newDevice->generic.props[i].lSaturation = 0;
475 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->generic.base.dinput);
477 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
478 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
479 if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
480 newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
481 else
482 newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
483 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
484 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
485 newDevice->generic.devcaps.dwFirmwareRevision = 0;
486 newDevice->generic.devcaps.dwHardwareRevision = 0;
487 newDevice->generic.devcaps.dwFFDriverVersion = 0;
489 if (TRACE_ON(dinput)) {
490 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
491 for (i = 0; i < (newDevice->axes); i++)
492 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
493 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
496 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
498 return DI_OK;
500 FAILED:
501 hr = DIERR_OUTOFMEMORY;
502 FAILED1:
503 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
504 HeapFree(GetProcessHeap(), 0, df);
505 release_DataFormat(&newDevice->generic.base.data_format);
506 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
507 HeapFree(GetProcessHeap(),0,newDevice->generic.name);
508 HeapFree(GetProcessHeap(),0,newDevice);
509 *pdev = 0;
511 return hr;
514 /******************************************************************************
515 * get_joystick_index : Get the joystick index from a given GUID
517 static unsigned short get_joystick_index(REFGUID guid)
519 GUID wine_joystick = DInput_Wine_Joystick_GUID;
520 GUID dev_guid = *guid;
522 wine_joystick.Data3 = 0;
523 dev_guid.Data3 = 0;
525 /* for the standard joystick GUID use index 0 */
526 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
528 /* for the wine joystick GUIDs use the index stored in Data3 */
529 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
531 return MAX_JOYSTICKS;
534 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
536 unsigned short index;
538 TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
539 find_joystick_devices();
540 *pdev = NULL;
542 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
543 joystick_devices_count && index < joystick_devices_count)
545 if ((riid == NULL) ||
546 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
547 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
548 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
549 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
551 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
554 WARN("no interface\n");
555 return DIERR_NOINTERFACE;
558 return DIERR_DEVICENOTREG;
561 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
563 unsigned short index;
565 TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
566 find_joystick_devices();
567 *pdev = NULL;
569 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
570 joystick_devices_count && index < joystick_devices_count)
572 if ((riid == NULL) ||
573 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
574 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
575 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
576 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
578 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
580 WARN("no interface\n");
581 return DIERR_NOINTERFACE;
584 WARN("invalid device GUID %s\n",debugstr_guid(rguid));
585 return DIERR_DEVICENOTREG;
588 #undef MAX_JOYSTICKS
590 const struct dinput_device joystick_linux_device = {
591 "Wine Linux joystick driver",
592 joydev_enum_deviceA,
593 joydev_enum_deviceW,
594 joydev_create_deviceA,
595 joydev_create_deviceW
598 /******************************************************************************
599 * Acquire : gets exclusive control of the joystick
601 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
603 JoystickImpl *This = (JoystickImpl *)iface;
604 HRESULT res;
606 TRACE("(%p)\n",This);
608 res = IDirectInputDevice2AImpl_Acquire(iface);
609 if (res != DI_OK)
610 return res;
612 /* open the joystick device */
613 if (This->joyfd==-1) {
614 TRACE("opening joystick device %s\n", This->dev);
616 This->joyfd=open(This->dev,O_RDONLY);
617 if (This->joyfd==-1) {
618 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
619 IDirectInputDevice2AImpl_Unacquire(iface);
620 return DIERR_NOTFOUND;
624 return DI_OK;
627 /******************************************************************************
628 * Unacquire : frees the joystick
630 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
632 JoystickImpl *This = (JoystickImpl *)iface;
633 HRESULT res;
635 TRACE("(%p)\n",This);
637 res = IDirectInputDevice2AImpl_Unacquire(iface);
639 if (res != DI_OK)
640 return res;
642 if (This->joyfd!=-1) {
643 TRACE("closing joystick device\n");
644 close(This->joyfd);
645 This->joyfd = -1;
646 return DI_OK;
649 return DI_NOEFFECT;
652 static void joy_polldev(JoystickGenericImpl *This_in) {
653 struct pollfd plfd;
654 struct js_event jse;
655 JoystickImpl *This = (JoystickImpl*) This_in;
657 TRACE("(%p)\n", This);
659 if (This->joyfd==-1) {
660 WARN("no device\n");
661 return;
663 while (1)
665 LONG value;
666 int inst_id = -1;
668 plfd.fd = This->joyfd;
669 plfd.events = POLLIN;
670 if (poll(&plfd,1,0) != 1)
671 return;
672 /* we have one event, so we can read */
673 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
674 return;
676 TRACE("js_event: type 0x%x, number %d, value %d\n",
677 jse.type,jse.number,jse.value);
678 if (jse.type & JS_EVENT_BUTTON)
680 if (jse.number >= This->generic.devcaps.dwButtons) return;
682 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
683 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
685 else if (jse.type & JS_EVENT_AXIS)
687 int number = This->axis_map[jse.number]; /* wine format object index */
689 if (number < 0) return;
690 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
691 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
693 TRACE("changing axis %d => %d\n", jse.number, number);
694 switch (number)
696 case 0: This->generic.js.lX = value; break;
697 case 1: This->generic.js.lY = value; break;
698 case 2: This->generic.js.lZ = value; break;
699 case 3: This->generic.js.lRx = value; break;
700 case 4: This->generic.js.lRy = value; break;
701 case 5: This->generic.js.lRz = value; break;
702 case 6: This->generic.js.rglSlider[0] = value; break;
703 case 7: This->generic.js.rglSlider[1] = value; break;
704 case 8: case 9: case 10: case 11:
706 int idx = number - 8;
708 if (jse.number % 2)
709 This->povs[idx].y = jse.value;
710 else
711 This->povs[idx].x = jse.value;
713 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
714 break;
716 default:
717 WARN("axis %d not supported\n", number);
720 if (inst_id >= 0)
721 queue_event((LPDIRECTINPUTDEVICE8A)This,
722 id_to_offset(&This->generic.base.data_format, inst_id),
723 value, jse.time, This->generic.base.dinput->evsequence++);
727 static const IDirectInputDevice8AVtbl JoystickAvt =
729 IDirectInputDevice2AImpl_QueryInterface,
730 IDirectInputDevice2AImpl_AddRef,
731 IDirectInputDevice2AImpl_Release,
732 JoystickAGenericImpl_GetCapabilities,
733 IDirectInputDevice2AImpl_EnumObjects,
734 JoystickAGenericImpl_GetProperty,
735 JoystickAGenericImpl_SetProperty,
736 JoystickLinuxAImpl_Acquire,
737 JoystickLinuxAImpl_Unacquire,
738 JoystickAGenericImpl_GetDeviceState,
739 IDirectInputDevice2AImpl_GetDeviceData,
740 IDirectInputDevice2AImpl_SetDataFormat,
741 IDirectInputDevice2AImpl_SetEventNotification,
742 IDirectInputDevice2AImpl_SetCooperativeLevel,
743 JoystickAGenericImpl_GetObjectInfo,
744 JoystickAGenericImpl_GetDeviceInfo,
745 IDirectInputDevice2AImpl_RunControlPanel,
746 IDirectInputDevice2AImpl_Initialize,
747 IDirectInputDevice2AImpl_CreateEffect,
748 IDirectInputDevice2AImpl_EnumEffects,
749 IDirectInputDevice2AImpl_GetEffectInfo,
750 IDirectInputDevice2AImpl_GetForceFeedbackState,
751 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
752 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
753 IDirectInputDevice2AImpl_Escape,
754 JoystickAGenericImpl_Poll,
755 IDirectInputDevice2AImpl_SendDeviceData,
756 IDirectInputDevice7AImpl_EnumEffectsInFile,
757 IDirectInputDevice7AImpl_WriteEffectToFile,
758 IDirectInputDevice8AImpl_BuildActionMap,
759 IDirectInputDevice8AImpl_SetActionMap,
760 IDirectInputDevice8AImpl_GetImageInfo
763 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
764 # define XCAST(fun) (typeof(JoystickWvt.fun))
765 #else
766 # define XCAST(fun) (void*)
767 #endif
769 static const IDirectInputDevice8WVtbl JoystickWvt =
771 IDirectInputDevice2WImpl_QueryInterface,
772 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
773 XCAST(Release)IDirectInputDevice2AImpl_Release,
774 XCAST(GetCapabilities)JoystickAGenericImpl_GetCapabilities,
775 IDirectInputDevice2WImpl_EnumObjects,
776 XCAST(GetProperty)JoystickAGenericImpl_GetProperty,
777 XCAST(SetProperty)JoystickAGenericImpl_SetProperty,
778 XCAST(Acquire)JoystickLinuxAImpl_Acquire,
779 XCAST(Unacquire)JoystickLinuxAImpl_Unacquire,
780 XCAST(GetDeviceState)JoystickAGenericImpl_GetDeviceState,
781 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
782 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
783 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
784 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
785 JoystickWGenericImpl_GetObjectInfo,
786 JoystickWGenericImpl_GetDeviceInfo,
787 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
788 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
789 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
790 IDirectInputDevice2WImpl_EnumEffects,
791 IDirectInputDevice2WImpl_GetEffectInfo,
792 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
793 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
794 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
795 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
796 XCAST(Poll)JoystickAGenericImpl_Poll,
797 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
798 IDirectInputDevice7WImpl_EnumEffectsInFile,
799 IDirectInputDevice7WImpl_WriteEffectToFile,
800 IDirectInputDevice8WImpl_BuildActionMap,
801 IDirectInputDevice8WImpl_SetActionMap,
802 IDirectInputDevice8WImpl_GetImageInfo
804 #undef XCAST
806 #else /* HAVE_LINUX_22_JOYSTICK_API */
808 const struct dinput_device joystick_linux_device = {
809 "Wine Linux joystick driver",
810 NULL,
811 NULL,
812 NULL,
813 NULL
816 #endif /* HAVE_LINUX_22_JOYSTICK_API */