ntdll: Add a raise_status function and avoid exporting __regs_RtlRaiseException.
[wine/multimedia.git] / dlls / dinput / joystick_linux.c
blobafabebe2903c85db423781d03312d84d1b8ba6f8
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.guidProduct = DInput_Wine_Joystick_GUID;
383 newDevice->generic.joy_polldev = joy_polldev;
385 /* get the device name */
386 #if defined(JSIOCGNAME)
387 if (ioctl(newDevice->joyfd,JSIOCGNAME(MAX_PATH),name) < 0) {
388 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", newDevice->dev, strerror(errno));
389 strcpy(name, "Wine Joystick");
391 #else
392 strcpy(name, "Wine Joystick");
393 #endif
395 /* copy the device name */
396 newDevice->generic.name = HeapAlloc(GetProcessHeap(),0,strlen(name) + 1);
397 strcpy(newDevice->generic.name, name);
399 #ifdef JSIOCGAXES
400 if (ioctl(newDevice->joyfd,JSIOCGAXES,&newDevice->axes) < 0) {
401 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
402 newDevice->axes = 2;
404 #endif
405 #ifdef JSIOCGBUTTONS
406 if (ioctl(newDevice->joyfd, JSIOCGBUTTONS, &newDevice->generic.devcaps.dwButtons) < 0) {
407 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", newDevice->dev, strerror(errno));
408 newDevice->generic.devcaps.dwButtons = 2;
410 #endif
412 if (newDevice->generic.devcaps.dwButtons > 128)
414 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
415 newDevice->generic.devcaps.dwButtons = 128;
418 newDevice->generic.base.lpVtbl = jvt;
419 newDevice->generic.base.ref = 1;
420 newDevice->generic.base.dinput = dinput;
421 newDevice->generic.base.guid = *rguid;
422 InitializeCriticalSection(&newDevice->generic.base.crit);
423 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
425 /* setup_dinput_options may change these */
426 newDevice->deadzone = 0;
428 /* do any user specified configuration */
429 hr = setup_dinput_options(newDevice);
430 if (hr != DI_OK)
431 goto FAILED1;
433 /* Create copy of default data format */
434 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
435 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
437 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
438 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
440 for (i = 0; i < newDevice->axes; i++)
442 int wine_obj = newDevice->axis_map[i];
444 if (wine_obj < 0) continue;
446 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
447 if (wine_obj < 8)
448 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
449 else
451 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
452 i++; /* POV takes 2 axes */
455 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
457 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
458 df->rgodf[idx ].pguid = &GUID_Button;
459 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
461 newDevice->generic.base.data_format.wine_df = df;
463 /* create default properties */
464 newDevice->generic.props = HeapAlloc(GetProcessHeap(),0,c_dfDIJoystick2.dwNumObjs*sizeof(ObjProps));
465 if (newDevice->generic.props == 0)
466 goto FAILED;
468 /* initialize default properties */
469 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
470 newDevice->generic.props[i].lDevMin = -32767;
471 newDevice->generic.props[i].lDevMax = +32767;
472 newDevice->generic.props[i].lMin = 0;
473 newDevice->generic.props[i].lMax = 0xffff;
474 newDevice->generic.props[i].lDeadZone = newDevice->deadzone; /* % * 1000 */
475 newDevice->generic.props[i].lSaturation = 0;
478 IDirectInput_AddRef((LPDIRECTINPUTDEVICE8A)newDevice->generic.base.dinput);
480 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
481 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
482 if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
483 newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
484 else
485 newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
486 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
487 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
488 newDevice->generic.devcaps.dwFirmwareRevision = 0;
489 newDevice->generic.devcaps.dwHardwareRevision = 0;
490 newDevice->generic.devcaps.dwFFDriverVersion = 0;
492 if (TRACE_ON(dinput)) {
493 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
494 for (i = 0; i < (newDevice->axes); i++)
495 TRACE("axis_map[%d] = %d\n", i, newDevice->axis_map[i]);
496 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
499 *pdev = (LPDIRECTINPUTDEVICEA)newDevice;
501 return DI_OK;
503 FAILED:
504 hr = DIERR_OUTOFMEMORY;
505 FAILED1:
506 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
507 HeapFree(GetProcessHeap(), 0, df);
508 release_DataFormat(&newDevice->generic.base.data_format);
509 HeapFree(GetProcessHeap(),0,newDevice->axis_map);
510 HeapFree(GetProcessHeap(),0,newDevice->generic.name);
511 HeapFree(GetProcessHeap(),0,newDevice->generic.props);
512 HeapFree(GetProcessHeap(),0,newDevice);
513 *pdev = 0;
515 return hr;
518 /******************************************************************************
519 * get_joystick_index : Get the joystick index from a given GUID
521 static unsigned short get_joystick_index(REFGUID guid)
523 GUID wine_joystick = DInput_Wine_Joystick_GUID;
524 GUID dev_guid = *guid;
526 wine_joystick.Data3 = 0;
527 dev_guid.Data3 = 0;
529 /* for the standard joystick GUID use index 0 */
530 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
532 /* for the wine joystick GUIDs use the index stored in Data3 */
533 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
535 return MAX_JOYSTICKS;
538 static HRESULT joydev_create_deviceA(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEA* pdev)
540 unsigned short index;
542 TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
543 find_joystick_devices();
544 *pdev = NULL;
546 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
547 joystick_devices_count && index < joystick_devices_count)
549 if ((riid == NULL) ||
550 IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
551 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
552 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
553 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
555 return alloc_device(rguid, &JoystickAvt, dinput, pdev, index);
558 WARN("no interface\n");
559 return DIERR_NOINTERFACE;
562 return DIERR_DEVICENOTREG;
565 static HRESULT joydev_create_deviceW(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPDIRECTINPUTDEVICEW* pdev)
567 unsigned short index;
569 TRACE("%p %s %p %p\n",dinput, debugstr_guid(rguid), riid, pdev);
570 find_joystick_devices();
571 *pdev = NULL;
573 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
574 joystick_devices_count && index < joystick_devices_count)
576 if ((riid == NULL) ||
577 IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
578 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
579 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
580 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
582 return alloc_device(rguid, &JoystickWvt, dinput, (LPDIRECTINPUTDEVICEA *)pdev, index);
584 WARN("no interface\n");
585 return DIERR_NOINTERFACE;
588 WARN("invalid device GUID %s\n",debugstr_guid(rguid));
589 return DIERR_DEVICENOTREG;
592 #undef MAX_JOYSTICKS
594 const struct dinput_device joystick_linux_device = {
595 "Wine Linux joystick driver",
596 joydev_enum_deviceA,
597 joydev_enum_deviceW,
598 joydev_create_deviceA,
599 joydev_create_deviceW
602 /******************************************************************************
603 * Acquire : gets exclusive control of the joystick
605 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
607 JoystickImpl *This = (JoystickImpl *)iface;
608 HRESULT res;
610 TRACE("(%p)\n",This);
612 res = JoystickAGenericImpl_Acquire(iface);
613 if (res != DI_OK)
614 return res;
616 /* open the joystick device */
617 if (This->joyfd==-1) {
618 TRACE("opening joystick device %s\n", This->dev);
620 This->joyfd=open(This->dev,O_RDONLY);
621 if (This->joyfd==-1) {
622 ERR("open(%s) failed: %s\n", This->dev, strerror(errno));
623 JoystickAGenericImpl_Unacquire(iface);
624 return DIERR_NOTFOUND;
628 return DI_OK;
631 /******************************************************************************
632 * Unacquire : frees the joystick
634 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
636 JoystickImpl *This = (JoystickImpl *)iface;
637 HRESULT res;
639 TRACE("(%p)\n",This);
641 res = JoystickAGenericImpl_Unacquire(iface);
643 if (res != DI_OK)
644 return res;
646 if (This->joyfd!=-1) {
647 TRACE("closing joystick device\n");
648 close(This->joyfd);
649 This->joyfd = -1;
650 return DI_OK;
653 return DI_NOEFFECT;
656 static void joy_polldev(JoystickGenericImpl *This_in) {
657 struct pollfd plfd;
658 struct js_event jse;
659 JoystickImpl *This = (JoystickImpl*) This_in;
661 TRACE("(%p)\n", This);
663 if (This->joyfd==-1) {
664 WARN("no device\n");
665 return;
667 while (1)
669 LONG value;
670 int inst_id = -1;
672 plfd.fd = This->joyfd;
673 plfd.events = POLLIN;
674 if (poll(&plfd,1,0) != 1)
675 return;
676 /* we have one event, so we can read */
677 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
678 return;
680 TRACE("js_event: type 0x%x, number %d, value %d\n",
681 jse.type,jse.number,jse.value);
682 if (jse.type & JS_EVENT_BUTTON)
684 if (jse.number >= This->generic.devcaps.dwButtons) return;
686 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
687 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
689 else if (jse.type & JS_EVENT_AXIS)
691 int number = This->axis_map[jse.number]; /* wine format object index */
693 if (number < 0) return;
694 inst_id = DIDFT_MAKEINSTANCE(number) | (number < 8 ? DIDFT_ABSAXIS : DIDFT_POV);
695 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
697 TRACE("changing axis %d => %d\n", jse.number, number);
698 switch (number)
700 case 0: This->generic.js.lX = value; break;
701 case 1: This->generic.js.lY = value; break;
702 case 2: This->generic.js.lZ = value; break;
703 case 3: This->generic.js.lRx = value; break;
704 case 4: This->generic.js.lRy = value; break;
705 case 5: This->generic.js.lRz = value; break;
706 case 6: This->generic.js.rglSlider[0] = value; break;
707 case 7: This->generic.js.rglSlider[1] = value; break;
708 case 8: case 9: case 10: case 11:
710 int idx = number - 8;
712 if (jse.number % 2)
713 This->povs[idx].y = jse.value;
714 else
715 This->povs[idx].x = jse.value;
717 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
718 break;
720 default:
721 WARN("axis %d not supported\n", number);
724 if (inst_id >= 0)
725 queue_event((LPDIRECTINPUTDEVICE8A)This,
726 id_to_offset(&This->generic.base.data_format, inst_id),
727 value, jse.time, This->generic.base.dinput->evsequence++);
731 static const IDirectInputDevice8AVtbl JoystickAvt =
733 IDirectInputDevice2AImpl_QueryInterface,
734 IDirectInputDevice2AImpl_AddRef,
735 IDirectInputDevice2AImpl_Release,
736 JoystickAGenericImpl_GetCapabilities,
737 IDirectInputDevice2AImpl_EnumObjects,
738 JoystickAGenericImpl_GetProperty,
739 JoystickAGenericImpl_SetProperty,
740 JoystickLinuxAImpl_Acquire,
741 JoystickLinuxAImpl_Unacquire,
742 JoystickAGenericImpl_GetDeviceState,
743 IDirectInputDevice2AImpl_GetDeviceData,
744 IDirectInputDevice2AImpl_SetDataFormat,
745 IDirectInputDevice2AImpl_SetEventNotification,
746 IDirectInputDevice2AImpl_SetCooperativeLevel,
747 JoystickAGenericImpl_GetObjectInfo,
748 JoystickAGenericImpl_GetDeviceInfo,
749 IDirectInputDevice2AImpl_RunControlPanel,
750 IDirectInputDevice2AImpl_Initialize,
751 IDirectInputDevice2AImpl_CreateEffect,
752 IDirectInputDevice2AImpl_EnumEffects,
753 IDirectInputDevice2AImpl_GetEffectInfo,
754 IDirectInputDevice2AImpl_GetForceFeedbackState,
755 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
756 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
757 IDirectInputDevice2AImpl_Escape,
758 JoystickAGenericImpl_Poll,
759 IDirectInputDevice2AImpl_SendDeviceData,
760 IDirectInputDevice7AImpl_EnumEffectsInFile,
761 IDirectInputDevice7AImpl_WriteEffectToFile,
762 IDirectInputDevice8AImpl_BuildActionMap,
763 IDirectInputDevice8AImpl_SetActionMap,
764 IDirectInputDevice8AImpl_GetImageInfo
767 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
768 # define XCAST(fun) (typeof(JoystickWvt.fun))
769 #else
770 # define XCAST(fun) (void*)
771 #endif
773 static const IDirectInputDevice8WVtbl JoystickWvt =
775 IDirectInputDevice2WImpl_QueryInterface,
776 XCAST(AddRef)IDirectInputDevice2AImpl_AddRef,
777 XCAST(Release)IDirectInputDevice2AImpl_Release,
778 XCAST(GetCapabilities)JoystickAGenericImpl_GetCapabilities,
779 IDirectInputDevice2WImpl_EnumObjects,
780 XCAST(GetProperty)JoystickAGenericImpl_GetProperty,
781 XCAST(SetProperty)JoystickAGenericImpl_SetProperty,
782 XCAST(Acquire)JoystickLinuxAImpl_Acquire,
783 XCAST(Unacquire)JoystickLinuxAImpl_Unacquire,
784 XCAST(GetDeviceState)JoystickAGenericImpl_GetDeviceState,
785 XCAST(GetDeviceData)IDirectInputDevice2AImpl_GetDeviceData,
786 XCAST(SetDataFormat)IDirectInputDevice2AImpl_SetDataFormat,
787 XCAST(SetEventNotification)IDirectInputDevice2AImpl_SetEventNotification,
788 XCAST(SetCooperativeLevel)IDirectInputDevice2AImpl_SetCooperativeLevel,
789 JoystickWGenericImpl_GetObjectInfo,
790 JoystickWGenericImpl_GetDeviceInfo,
791 XCAST(RunControlPanel)IDirectInputDevice2AImpl_RunControlPanel,
792 XCAST(Initialize)IDirectInputDevice2AImpl_Initialize,
793 XCAST(CreateEffect)IDirectInputDevice2AImpl_CreateEffect,
794 IDirectInputDevice2WImpl_EnumEffects,
795 IDirectInputDevice2WImpl_GetEffectInfo,
796 XCAST(GetForceFeedbackState)IDirectInputDevice2AImpl_GetForceFeedbackState,
797 XCAST(SendForceFeedbackCommand)IDirectInputDevice2AImpl_SendForceFeedbackCommand,
798 XCAST(EnumCreatedEffectObjects)IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
799 XCAST(Escape)IDirectInputDevice2AImpl_Escape,
800 XCAST(Poll)JoystickAGenericImpl_Poll,
801 XCAST(SendDeviceData)IDirectInputDevice2AImpl_SendDeviceData,
802 IDirectInputDevice7WImpl_EnumEffectsInFile,
803 IDirectInputDevice7WImpl_WriteEffectToFile,
804 IDirectInputDevice8WImpl_BuildActionMap,
805 IDirectInputDevice8WImpl_SetActionMap,
806 IDirectInputDevice8WImpl_GetImageInfo
808 #undef XCAST
810 #else /* HAVE_LINUX_22_JOYSTICK_API */
812 const struct dinput_device joystick_linux_device = {
813 "Wine Linux joystick driver",
814 NULL,
815 NULL,
816 NULL,
817 NULL
820 #endif /* HAVE_LINUX_22_JOYSTICK_API */