mscoree: Extract a helper function for getting file name from assembly.
[wine/multimedia.git] / dlls / dinput / joystick_linux.c
blobd973a8493d0e5553a341f853a9f8c644d013ed29
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_LINUX_IOCTL_H
47 # include <linux/ioctl.h>
48 #endif
49 #ifdef HAVE_LINUX_JOYSTICK_H
50 # include <linux/joystick.h>
51 # undef SW_MAX
52 #endif
53 #ifdef HAVE_SYS_POLL_H
54 # include <sys/poll.h>
55 #endif
57 #include "wine/debug.h"
58 #include "wine/unicode.h"
59 #include "windef.h"
60 #include "winbase.h"
61 #include "winerror.h"
62 #include "dinput.h"
64 #include "dinput_private.h"
65 #include "device_private.h"
66 #include "joystick_private.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
70 #ifdef HAVE_LINUX_22_JOYSTICK_API
72 #define JOYDEV_NEW "/dev/input/js"
73 #define JOYDEV_OLD "/dev/js"
74 #define JOYDEVDRIVER " (js)"
76 struct JoyDev
78 char device[MAX_PATH];
79 char name[MAX_PATH];
81 BYTE axis_count;
82 BYTE button_count;
83 int *dev_axes_map;
86 typedef struct JoystickImpl JoystickImpl;
87 static const IDirectInputDevice8AVtbl JoystickAvt;
88 static const IDirectInputDevice8WVtbl JoystickWvt;
89 struct JoystickImpl
91 struct JoystickGenericImpl generic;
93 struct JoyDev *joydev;
95 /* joystick private */
96 int joyfd;
97 POINTL povs[4];
100 static inline JoystickImpl *impl_from_IDirectInputDevice8A(IDirectInputDevice8A *iface)
102 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8A_iface),
103 JoystickGenericImpl, base), JoystickImpl, generic);
105 static inline JoystickImpl *impl_from_IDirectInputDevice8W(IDirectInputDevice8W *iface)
107 return CONTAINING_RECORD(CONTAINING_RECORD(CONTAINING_RECORD(iface, IDirectInputDeviceImpl, IDirectInputDevice8W_iface),
108 JoystickGenericImpl, base), JoystickImpl, generic);
110 static inline IDirectInputDevice8A *IDirectInputDevice8A_from_impl(JoystickImpl *This)
112 return &This->generic.base.IDirectInputDevice8A_iface;
114 static inline IDirectInputDevice8W *IDirectInputDevice8W_from_impl(JoystickImpl *This)
116 return &This->generic.base.IDirectInputDevice8W_iface;
119 static const GUID DInput_Wine_Joystick_GUID = { /* 9e573ed9-7734-11d2-8d4a-23903fb6bdf7 */
120 0x9e573ed9,
121 0x7734,
122 0x11d2,
123 {0x8d, 0x4a, 0x23, 0x90, 0x3f, 0xb6, 0xbd, 0xf7}
126 #define MAX_JOYSTICKS 64
127 static INT joystick_devices_count = -1;
128 static struct JoyDev *joystick_devices;
130 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface);
132 static INT find_joystick_devices(void)
134 INT i;
136 if (joystick_devices_count != -1) return joystick_devices_count;
138 joystick_devices_count = 0;
139 for (i = 0; i < MAX_JOYSTICKS; i++)
141 int fd;
142 struct JoyDev joydev, *new_joydevs;
143 BYTE axes_map[ABS_MAX + 1];
145 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_NEW, i);
146 if ((fd = open(joydev.device, O_RDONLY)) < 0)
148 snprintf(joydev.device, sizeof(joydev.device), "%s%d", JOYDEV_OLD, i);
149 if ((fd = open(joydev.device, O_RDONLY)) < 0) continue;
152 strcpy(joydev.name, "Wine Joystick");
153 #if defined(JSIOCGNAME)
154 if (ioctl(fd, JSIOCGNAME(sizeof(joydev.name) - sizeof(JOYDEVDRIVER)), joydev.name) < 0)
155 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
156 #endif
158 /* Append driver name */
159 strcat(joydev.name, JOYDEVDRIVER);
161 if (device_disabled_registry(joydev.name)) {
162 close(fd);
163 continue;
166 #ifdef JSIOCGAXES
167 if (ioctl(fd, JSIOCGAXES, &joydev.axis_count) < 0)
169 WARN("ioctl(%s,JSIOCGAXES) failed: %s, defauting to 2\n", joydev.device, strerror(errno));
170 joydev.axis_count = 2;
172 #endif
173 #ifdef JSIOCGBUTTONS
174 if (ioctl(fd, JSIOCGBUTTONS, &joydev.button_count) < 0)
176 WARN("ioctl(%s,JSIOCGBUTTONS) failed: %s, defauting to 2\n", joydev.device, strerror(errno));
177 joydev.button_count = 2;
179 #endif
181 if (ioctl(fd, JSIOCGAXMAP, axes_map) < 0)
183 WARN("ioctl(%s,JSIOCGNAME) failed: %s\n", joydev.device, strerror(errno));
184 joydev.dev_axes_map = NULL;
186 else
187 if ((joydev.dev_axes_map = HeapAlloc(GetProcessHeap(), 0, joydev.axis_count * sizeof(int))))
189 INT j;
191 /* Remap to DI numbers */
192 for (j = 0; j < joydev.axis_count; j++)
193 if (axes_map[j] < 8)
194 /* Axis match 1-to-1 */
195 joydev.dev_axes_map[j] = j;
196 else if (axes_map[j] == 16 ||
197 axes_map[j] == 17)
198 /* POV axis */
199 joydev.dev_axes_map[j] = 8;
200 else
201 joydev.dev_axes_map[j] = -1;
204 close(fd);
206 if (!joystick_devices_count)
207 new_joydevs = HeapAlloc(GetProcessHeap(), 0, sizeof(struct JoyDev));
208 else
209 new_joydevs = HeapReAlloc(GetProcessHeap(), 0, joystick_devices,
210 (joystick_devices_count + 1) * sizeof(struct JoyDev));
211 if (!new_joydevs) continue;
213 TRACE("Found a joystick on %s: %s\n with %d axes and %d buttons\n", joydev.device,
214 joydev.name, joydev.axis_count, joydev.button_count);
216 joystick_devices = new_joydevs;
217 joystick_devices[joystick_devices_count++] = joydev;
220 return joystick_devices_count;
223 static HRESULT joydev_enum_deviceA(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEA lpddi, DWORD version, int id)
225 int fd = -1;
227 if (id >= find_joystick_devices()) return E_FAIL;
229 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
230 WARN("force feedback not supported\n");
231 return S_FALSE;
234 if ((dwDevType == 0) ||
235 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
236 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
237 /* check whether we have a joystick */
238 if ((fd = open(joystick_devices[id].device, O_RDONLY)) < 0)
240 WARN("open(%s, O_RDONLY) failed: %s\n", joystick_devices[id].name, strerror(errno));
241 return S_FALSE;
244 /* Return joystick */
245 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
246 lpddi->guidInstance.Data3 = id;
247 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
248 /* we only support traditional joysticks for now */
249 if (version >= 0x0800)
250 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
251 else
252 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
254 strcpy(lpddi->tszInstanceName, joystick_devices[id].name);
255 strcpy(lpddi->tszProductName, joystick_devices[id].name);
257 lpddi->guidFFDriver = GUID_NULL;
258 close(fd);
259 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, lpddi->tszProductName);
260 return S_OK;
263 return S_FALSE;
266 static HRESULT joydev_enum_deviceW(DWORD dwDevType, DWORD dwFlags, LPDIDEVICEINSTANCEW lpddi, DWORD version, int id)
268 int fd = -1;
270 if (id >= find_joystick_devices()) return E_FAIL;
272 if (dwFlags & DIEDFL_FORCEFEEDBACK) {
273 WARN("force feedback not supported\n");
274 return S_FALSE;
277 if ((dwDevType == 0) ||
278 ((dwDevType == DIDEVTYPE_JOYSTICK) && (version > 0x0300 && version < 0x0800)) ||
279 (((dwDevType == DI8DEVCLASS_GAMECTRL) || (dwDevType == DI8DEVTYPE_JOYSTICK)) && (version >= 0x0800))) {
280 /* check whether we have a joystick */
281 if ((fd = open(joystick_devices[id].device, O_RDONLY)) < 0)
283 WARN("open(%s,O_RDONLY) failed: %s\n", joystick_devices[id].device, strerror(errno));
284 return S_FALSE;
287 /* Return joystick */
288 lpddi->guidInstance = DInput_Wine_Joystick_GUID;
289 lpddi->guidInstance.Data3 = id;
290 lpddi->guidProduct = DInput_Wine_Joystick_GUID;
291 /* we only support traditional joysticks for now */
292 if (version >= 0x0800)
293 lpddi->dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
294 else
295 lpddi->dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
297 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszInstanceName, MAX_PATH);
298 MultiByteToWideChar(CP_ACP, 0, joystick_devices[id].name, -1, lpddi->tszProductName, MAX_PATH);
299 lpddi->guidFFDriver = GUID_NULL;
300 close(fd);
301 TRACE("Enumerating the linux Joystick device: %s (%s)\n", joystick_devices[id].device, joystick_devices[id].name);
302 return S_OK;
305 return S_FALSE;
308 static HRESULT alloc_device(REFGUID rguid, IDirectInputImpl *dinput,
309 JoystickImpl **pdev, unsigned short index)
311 DWORD i;
312 JoystickImpl* newDevice;
313 HRESULT hr;
314 LPDIDATAFORMAT df = NULL;
315 int idx = 0;
317 TRACE("%s %p %p %hu\n", debugstr_guid(rguid), dinput, pdev, index);
319 newDevice = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(JoystickImpl));
320 if (newDevice == 0) {
321 WARN("out of memory\n");
322 *pdev = 0;
323 return DIERR_OUTOFMEMORY;
326 newDevice->joydev = &joystick_devices[index];
327 newDevice->joyfd = -1;
328 newDevice->generic.guidInstance = DInput_Wine_Joystick_GUID;
329 newDevice->generic.guidInstance.Data3 = index;
330 newDevice->generic.guidProduct = DInput_Wine_Joystick_GUID;
331 newDevice->generic.joy_polldev = joy_polldev;
332 newDevice->generic.name = newDevice->joydev->name;
333 newDevice->generic.device_axis_count = newDevice->joydev->axis_count;
334 newDevice->generic.devcaps.dwButtons = newDevice->joydev->button_count;
336 if (newDevice->generic.devcaps.dwButtons > 128)
338 WARN("Can't support %d buttons. Clamping down to 128\n", newDevice->generic.devcaps.dwButtons);
339 newDevice->generic.devcaps.dwButtons = 128;
342 newDevice->generic.base.IDirectInputDevice8A_iface.lpVtbl = &JoystickAvt;
343 newDevice->generic.base.IDirectInputDevice8W_iface.lpVtbl = &JoystickWvt;
344 newDevice->generic.base.ref = 1;
345 newDevice->generic.base.dinput = dinput;
346 newDevice->generic.base.guid = *rguid;
347 InitializeCriticalSection(&newDevice->generic.base.crit);
348 newDevice->generic.base.crit.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": JoystickImpl*->generic.base.crit");
350 /* setup_dinput_options may change these */
351 newDevice->generic.deadzone = 0;
353 /* do any user specified configuration */
354 hr = setup_dinput_options(&newDevice->generic, newDevice->joydev->dev_axes_map);
355 if (hr != DI_OK)
356 goto FAILED1;
358 /* Create copy of default data format */
359 if (!(df = HeapAlloc(GetProcessHeap(), 0, c_dfDIJoystick2.dwSize))) goto FAILED;
360 memcpy(df, &c_dfDIJoystick2, c_dfDIJoystick2.dwSize);
362 df->dwNumObjs = newDevice->generic.devcaps.dwAxes + newDevice->generic.devcaps.dwPOVs + newDevice->generic.devcaps.dwButtons;
363 if (!(df->rgodf = HeapAlloc(GetProcessHeap(), 0, df->dwNumObjs * df->dwObjSize))) goto FAILED;
365 for (i = 0; i < newDevice->generic.device_axis_count; i++)
367 int wine_obj = newDevice->generic.axis_map[i];
369 if (wine_obj < 0) continue;
371 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[wine_obj], df->dwObjSize);
372 if (wine_obj < 8)
373 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj) | DIDFT_ABSAXIS;
374 else
376 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(wine_obj - 8) | DIDFT_POV;
377 i++; /* POV takes 2 axes */
380 for (i = 0; i < newDevice->generic.devcaps.dwButtons; i++)
382 memcpy(&df->rgodf[idx], &c_dfDIJoystick2.rgodf[i + 12], df->dwObjSize);
383 df->rgodf[idx ].pguid = &GUID_Button;
384 df->rgodf[idx++].dwType = DIDFT_MAKEINSTANCE(i) | DIDFT_PSHBUTTON;
386 newDevice->generic.base.data_format.wine_df = df;
388 /* initialize default properties */
389 for (i = 0; i < c_dfDIJoystick2.dwNumObjs; i++) {
390 newDevice->generic.props[i].lDevMin = -32767;
391 newDevice->generic.props[i].lDevMax = +32767;
392 newDevice->generic.props[i].lMin = 0;
393 newDevice->generic.props[i].lMax = 0xffff;
394 newDevice->generic.props[i].lDeadZone = newDevice->generic.deadzone; /* % * 1000 */
395 newDevice->generic.props[i].lSaturation = 0;
398 IDirectInput_AddRef(&newDevice->generic.base.dinput->IDirectInput7A_iface);
400 EnterCriticalSection(&dinput->crit);
401 list_add_tail(&dinput->devices_list, &newDevice->generic.base.entry);
402 LeaveCriticalSection(&dinput->crit);
404 newDevice->generic.devcaps.dwSize = sizeof(newDevice->generic.devcaps);
405 newDevice->generic.devcaps.dwFlags = DIDC_ATTACHED;
406 if (newDevice->generic.base.dinput->dwVersion >= 0x0800)
407 newDevice->generic.devcaps.dwDevType = DI8DEVTYPE_JOYSTICK | (DI8DEVTYPEJOYSTICK_STANDARD << 8);
408 else
409 newDevice->generic.devcaps.dwDevType = DIDEVTYPE_JOYSTICK | (DIDEVTYPEJOYSTICK_TRADITIONAL << 8);
410 newDevice->generic.devcaps.dwFFSamplePeriod = 0;
411 newDevice->generic.devcaps.dwFFMinTimeResolution = 0;
412 newDevice->generic.devcaps.dwFirmwareRevision = 0;
413 newDevice->generic.devcaps.dwHardwareRevision = 0;
414 newDevice->generic.devcaps.dwFFDriverVersion = 0;
416 if (TRACE_ON(dinput)) {
417 _dump_DIDATAFORMAT(newDevice->generic.base.data_format.wine_df);
418 for (i = 0; i < (newDevice->generic.device_axis_count); i++)
419 TRACE("axis_map[%d] = %d\n", i, newDevice->generic.axis_map[i]);
420 _dump_DIDEVCAPS(&newDevice->generic.devcaps);
423 *pdev = newDevice;
425 return DI_OK;
427 FAILED:
428 hr = DIERR_OUTOFMEMORY;
429 FAILED1:
430 if (df) HeapFree(GetProcessHeap(), 0, df->rgodf);
431 HeapFree(GetProcessHeap(), 0, df);
432 release_DataFormat(&newDevice->generic.base.data_format);
433 HeapFree(GetProcessHeap(),0,newDevice->generic.axis_map);
434 HeapFree(GetProcessHeap(),0,newDevice);
435 *pdev = 0;
437 return hr;
440 /******************************************************************************
441 * get_joystick_index : Get the joystick index from a given GUID
443 static unsigned short get_joystick_index(REFGUID guid)
445 GUID wine_joystick = DInput_Wine_Joystick_GUID;
446 GUID dev_guid = *guid;
448 wine_joystick.Data3 = 0;
449 dev_guid.Data3 = 0;
451 /* for the standard joystick GUID use index 0 */
452 if(IsEqualGUID(&GUID_Joystick,guid)) return 0;
454 /* for the wine joystick GUIDs use the index stored in Data3 */
455 if(IsEqualGUID(&wine_joystick, &dev_guid)) return guid->Data3;
457 return MAX_JOYSTICKS;
460 static HRESULT joydev_create_device(IDirectInputImpl *dinput, REFGUID rguid, REFIID riid, LPVOID *pdev, int unicode)
462 unsigned short index;
464 TRACE("%p %s %s %p %i\n", dinput, debugstr_guid(rguid), debugstr_guid(riid), pdev, unicode);
465 find_joystick_devices();
466 *pdev = NULL;
468 if ((index = get_joystick_index(rguid)) < MAX_JOYSTICKS &&
469 joystick_devices_count && index < joystick_devices_count)
471 JoystickImpl *This;
472 HRESULT hr;
474 if (riid == NULL)
475 ;/* nothing */
476 else if (IsEqualGUID(&IID_IDirectInputDeviceA, riid) ||
477 IsEqualGUID(&IID_IDirectInputDevice2A, riid) ||
478 IsEqualGUID(&IID_IDirectInputDevice7A, riid) ||
479 IsEqualGUID(&IID_IDirectInputDevice8A, riid))
481 unicode = 0;
483 else if (IsEqualGUID(&IID_IDirectInputDeviceW, riid) ||
484 IsEqualGUID(&IID_IDirectInputDevice2W, riid) ||
485 IsEqualGUID(&IID_IDirectInputDevice7W, riid) ||
486 IsEqualGUID(&IID_IDirectInputDevice8W, riid))
488 unicode = 1;
490 else
492 WARN("no interface\n");
493 return DIERR_NOINTERFACE;
496 hr = alloc_device(rguid, dinput, &This, index);
497 if (!This) return hr;
499 if (unicode)
500 *pdev = &This->generic.base.IDirectInputDevice8W_iface;
501 else
502 *pdev = &This->generic.base.IDirectInputDevice8A_iface;
504 return hr;
507 return DIERR_DEVICENOTREG;
510 #undef MAX_JOYSTICKS
512 const struct dinput_device joystick_linux_device = {
513 "Wine Linux joystick driver",
514 joydev_enum_deviceA,
515 joydev_enum_deviceW,
516 joydev_create_device
519 /******************************************************************************
520 * Acquire : gets exclusive control of the joystick
522 static HRESULT WINAPI JoystickLinuxWImpl_Acquire(LPDIRECTINPUTDEVICE8W iface)
524 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
525 HRESULT res;
527 TRACE("(%p)\n",This);
529 res = IDirectInputDevice2WImpl_Acquire(iface);
530 if (res != DI_OK)
531 return res;
533 /* open the joystick device */
534 if (This->joyfd==-1) {
535 TRACE("opening joystick device %s\n", This->joydev->device);
537 This->joyfd = open(This->joydev->device, O_RDONLY);
538 if (This->joyfd==-1) {
539 ERR("open(%s) failed: %s\n", This->joydev->device, strerror(errno));
540 IDirectInputDevice2WImpl_Unacquire(iface);
541 return DIERR_NOTFOUND;
545 return DI_OK;
548 static HRESULT WINAPI JoystickLinuxAImpl_Acquire(LPDIRECTINPUTDEVICE8A iface)
550 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
551 return JoystickLinuxWImpl_Acquire(IDirectInputDevice8W_from_impl(This));
554 /******************************************************************************
555 * GetProperty : get input device properties
557 static HRESULT WINAPI JoystickLinuxWImpl_GetProperty(LPDIRECTINPUTDEVICE8W iface, REFGUID rguid, LPDIPROPHEADER pdiph)
559 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
561 TRACE("(this=%p,%s,%p)\n", iface, debugstr_guid(rguid), pdiph);
562 _dump_DIPROPHEADER(pdiph);
564 if (!IS_DIPROP(rguid)) return DI_OK;
566 switch (LOWORD(rguid)) {
568 case (DWORD_PTR) DIPROP_JOYSTICKID:
570 LPDIPROPDWORD pd = (LPDIPROPDWORD)pdiph;
572 pd->dwData = get_joystick_index(&This->generic.base.guid);
573 TRACE("DIPROP_JOYSTICKID(%d)\n", pd->dwData);
574 break;
577 default:
578 return JoystickWGenericImpl_GetProperty(iface, rguid, pdiph);
581 return DI_OK;
584 static HRESULT WINAPI JoystickLinuxAImpl_GetProperty(LPDIRECTINPUTDEVICE8A iface, REFGUID rguid, LPDIPROPHEADER pdiph)
586 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
587 return JoystickLinuxWImpl_GetProperty(IDirectInputDevice8W_from_impl(This), rguid, pdiph);
590 /******************************************************************************
591 * Unacquire : frees the joystick
593 static HRESULT WINAPI JoystickLinuxWImpl_Unacquire(LPDIRECTINPUTDEVICE8W iface)
595 JoystickImpl *This = impl_from_IDirectInputDevice8W(iface);
596 HRESULT res;
598 TRACE("(%p)\n",This);
600 res = IDirectInputDevice2WImpl_Unacquire(iface);
602 if (res != DI_OK)
603 return res;
605 if (This->joyfd!=-1) {
606 TRACE("closing joystick device\n");
607 close(This->joyfd);
608 This->joyfd = -1;
609 return DI_OK;
612 return DI_NOEFFECT;
615 static HRESULT WINAPI JoystickLinuxAImpl_Unacquire(LPDIRECTINPUTDEVICE8A iface)
617 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
618 return JoystickLinuxWImpl_Unacquire(IDirectInputDevice8W_from_impl(This));
621 static void joy_polldev(LPDIRECTINPUTDEVICE8A iface)
623 struct pollfd plfd;
624 struct js_event jse;
625 JoystickImpl *This = impl_from_IDirectInputDevice8A(iface);
627 TRACE("(%p)\n", This);
629 if (This->joyfd==-1) {
630 WARN("no device\n");
631 return;
633 while (1)
635 LONG value;
636 int inst_id = -1;
638 plfd.fd = This->joyfd;
639 plfd.events = POLLIN;
640 if (poll(&plfd,1,0) != 1)
641 return;
642 /* we have one event, so we can read */
643 if (sizeof(jse)!=read(This->joyfd,&jse,sizeof(jse))) {
644 return;
646 TRACE("js_event: type 0x%x, number %d, value %d\n",
647 jse.type,jse.number,jse.value);
648 if (jse.type & JS_EVENT_BUTTON)
650 if (jse.number >= This->generic.devcaps.dwButtons) return;
652 inst_id = DIDFT_MAKEINSTANCE(jse.number) | DIDFT_PSHBUTTON;
653 This->generic.js.rgbButtons[jse.number] = value = jse.value ? 0x80 : 0x00;
655 else if (jse.type & JS_EVENT_AXIS)
657 int number = This->generic.axis_map[jse.number]; /* wine format object index */
659 if (number < 0) return;
660 inst_id = number < 8 ? DIDFT_MAKEINSTANCE(number) | DIDFT_ABSAXIS :
661 DIDFT_MAKEINSTANCE(number - 8) | DIDFT_POV;
662 value = joystick_map_axis(&This->generic.props[id_to_object(This->generic.base.data_format.wine_df, inst_id)], jse.value);
664 TRACE("changing axis %d => %d\n", jse.number, number);
665 switch (number)
667 case 0: This->generic.js.lX = value; break;
668 case 1: This->generic.js.lY = value; break;
669 case 2: This->generic.js.lZ = value; break;
670 case 3: This->generic.js.lRx = value; break;
671 case 4: This->generic.js.lRy = value; break;
672 case 5: This->generic.js.lRz = value; break;
673 case 6: This->generic.js.rglSlider[0] = value; break;
674 case 7: This->generic.js.rglSlider[1] = value; break;
675 case 8: case 9: case 10: case 11:
677 int idx = number - 8;
679 if (jse.number % 2)
680 This->povs[idx].y = jse.value;
681 else
682 This->povs[idx].x = jse.value;
684 This->generic.js.rgdwPOV[idx] = value = joystick_map_pov(&This->povs[idx]);
685 break;
687 default:
688 WARN("axis %d not supported\n", number);
691 if (inst_id >= 0)
692 queue_event(iface, inst_id,
693 value, jse.time, This->generic.base.dinput->evsequence++);
697 static const IDirectInputDevice8AVtbl JoystickAvt =
699 IDirectInputDevice2AImpl_QueryInterface,
700 IDirectInputDevice2AImpl_AddRef,
701 IDirectInputDevice2AImpl_Release,
702 JoystickAGenericImpl_GetCapabilities,
703 IDirectInputDevice2AImpl_EnumObjects,
704 JoystickLinuxAImpl_GetProperty,
705 JoystickAGenericImpl_SetProperty,
706 JoystickLinuxAImpl_Acquire,
707 JoystickLinuxAImpl_Unacquire,
708 JoystickAGenericImpl_GetDeviceState,
709 IDirectInputDevice2AImpl_GetDeviceData,
710 IDirectInputDevice2AImpl_SetDataFormat,
711 IDirectInputDevice2AImpl_SetEventNotification,
712 IDirectInputDevice2AImpl_SetCooperativeLevel,
713 JoystickAGenericImpl_GetObjectInfo,
714 JoystickAGenericImpl_GetDeviceInfo,
715 IDirectInputDevice2AImpl_RunControlPanel,
716 IDirectInputDevice2AImpl_Initialize,
717 IDirectInputDevice2AImpl_CreateEffect,
718 IDirectInputDevice2AImpl_EnumEffects,
719 IDirectInputDevice2AImpl_GetEffectInfo,
720 IDirectInputDevice2AImpl_GetForceFeedbackState,
721 IDirectInputDevice2AImpl_SendForceFeedbackCommand,
722 IDirectInputDevice2AImpl_EnumCreatedEffectObjects,
723 IDirectInputDevice2AImpl_Escape,
724 JoystickAGenericImpl_Poll,
725 IDirectInputDevice2AImpl_SendDeviceData,
726 IDirectInputDevice7AImpl_EnumEffectsInFile,
727 IDirectInputDevice7AImpl_WriteEffectToFile,
728 JoystickAGenericImpl_BuildActionMap,
729 JoystickAGenericImpl_SetActionMap,
730 IDirectInputDevice8AImpl_GetImageInfo
733 static const IDirectInputDevice8WVtbl JoystickWvt =
735 IDirectInputDevice2WImpl_QueryInterface,
736 IDirectInputDevice2WImpl_AddRef,
737 IDirectInputDevice2WImpl_Release,
738 JoystickWGenericImpl_GetCapabilities,
739 IDirectInputDevice2WImpl_EnumObjects,
740 JoystickLinuxWImpl_GetProperty,
741 JoystickWGenericImpl_SetProperty,
742 JoystickLinuxWImpl_Acquire,
743 JoystickLinuxWImpl_Unacquire,
744 JoystickWGenericImpl_GetDeviceState,
745 IDirectInputDevice2WImpl_GetDeviceData,
746 IDirectInputDevice2WImpl_SetDataFormat,
747 IDirectInputDevice2WImpl_SetEventNotification,
748 IDirectInputDevice2WImpl_SetCooperativeLevel,
749 JoystickWGenericImpl_GetObjectInfo,
750 JoystickWGenericImpl_GetDeviceInfo,
751 IDirectInputDevice2WImpl_RunControlPanel,
752 IDirectInputDevice2WImpl_Initialize,
753 IDirectInputDevice2WImpl_CreateEffect,
754 IDirectInputDevice2WImpl_EnumEffects,
755 IDirectInputDevice2WImpl_GetEffectInfo,
756 IDirectInputDevice2WImpl_GetForceFeedbackState,
757 IDirectInputDevice2WImpl_SendForceFeedbackCommand,
758 IDirectInputDevice2WImpl_EnumCreatedEffectObjects,
759 IDirectInputDevice2WImpl_Escape,
760 JoystickWGenericImpl_Poll,
761 IDirectInputDevice2WImpl_SendDeviceData,
762 IDirectInputDevice7WImpl_EnumEffectsInFile,
763 IDirectInputDevice7WImpl_WriteEffectToFile,
764 JoystickWGenericImpl_BuildActionMap,
765 JoystickWGenericImpl_SetActionMap,
766 IDirectInputDevice8WImpl_GetImageInfo
769 #else /* HAVE_LINUX_22_JOYSTICK_API */
771 const struct dinput_device joystick_linux_device = {
772 "Wine Linux joystick driver",
773 NULL,
774 NULL,
775 NULL
778 #endif /* HAVE_LINUX_22_JOYSTICK_API */